--- trunk/src/emul.c 2007/10/08 16:18:38 12 +++ trunk/src/emul.c 2007/10/08 16:18:51 14 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.225 2005/08/14 19:35:54 debug Exp $ + * $Id: emul.c,v 1.234 2005/09/17 21:55:19 debug Exp $ * * Emulation startup and misc. routines. */ @@ -435,6 +435,70 @@ /* + * apple_load_bootblock(): + * + * Try to load a kernel from a disk image with an Apple Partition Table. + * + * TODO: This function uses too many magic offsets and so on; it should be + * cleaned up some day. See http://www.awprofessional.com/articles/ + * article.asp?p=376123&seqNum=3&rl=1 for some info on the Apple + * partition format. + * + * Returns 1 on success, 0 on failure. + */ +static int apple_load_bootblock(struct machine *m, struct cpu *cpu, + int disk_id, int disk_type, int *n_loadp, char ***load_namesp) +{ + unsigned char buf[0x8000]; + int res, partnr, n_partitions = 0, n_hfs_partitions = 0; + uint64_t hfs_start, hfs_length; + + res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf)); + if (!res) { + fatal("apple_load_bootblock: couldn't read the disk " + "image. Aborting.\n"); + return 0; + } + + partnr = 0; + do { + int start, length; + int ofs = 0x200 * (partnr + 1); + if (partnr == 0) + n_partitions = buf[ofs + 7]; + start = (buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) + + (buf[ofs + 10] << 8) + buf[ofs + 11]; + length = (buf[ofs + 12] << 24) + (buf[ofs + 13] << 16) + + (buf[ofs + 14] << 8) + buf[ofs + 15]; + + debug("partition %i: '%s', type '%s', start %i, length %i\n", + partnr, buf + ofs + 0x10, buf + ofs + 0x30, + start, length); + + if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) { + n_hfs_partitions ++; + hfs_start = 512 * start; + hfs_length = 512 * length; + } + + /* Any more partitions? */ + partnr ++; + } while (partnr < n_partitions); + + if (n_hfs_partitions == 0) { + fatal("Error: No HFS partition found! TODO\n"); + return 0; + } + if (n_hfs_partitions >= 2) { + fatal("Error: Too many HFS partitions found! TODO\n"); + return 0; + } + + return 0; +} + + +/* * load_bootblock(): * * For some emulation modes, it is possible to boot from a harddisk image by @@ -623,6 +687,30 @@ n_loadp, load_namesp); } + if (retval != 0) + goto ret_ok; + + /* Apple parition table: */ + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, 0x0, bootblock_buf, 0x800); + if (!res) { + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; + } + if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' && + bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') { + /* We can't load a kernel if the name + isn't specified. */ + if (cpu->machine->boot_kernel_filename == NULL || + cpu->machine->boot_kernel_filename[0] == '\0') + fatal("\nApple partition table, but no kernel " + "specified? (Use the -j option.)\n"); + else + retval = apple_load_bootblock(m, cpu, boot_disk_id, + boot_disk_type, n_loadp, load_namesp); + } + +ret_ok: free(bootblock_buf); return retval; } @@ -1120,6 +1208,37 @@ cpu->pc = entrypoint; switch (m->arch) { + + case ARCH_ALPHA: + /* For position-independant code: */ + cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; + break; + + case ARCH_ARM: + cpu->pc &= 0xfffffffc; + cpu->cd.arm.r[ARM_PC] = cpu->pc; + break; + + case ARCH_AVR: + cpu->pc &= 0xfffff; + if (cpu->pc & 1) { + fatal("AVR: lowest bit of pc set: TODO\n"); + exit(1); + } + break; + + case ARCH_HPPA: + break; + + case ARCH_I960: + break; + + case ARCH_IA64: + break; + + case ARCH_M68K: + break; + case ARCH_MIPS: if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL)) @@ -1138,27 +1257,19 @@ spec/x458.html for more info. */ cpu->cd.ppc.gpr[2] = toc; /* TODO */ + if (cpu->cd.ppc.bits == 32) + cpu->pc &= 0xffffffffULL; break; - case ARCH_ALPHA: - /* For position-independant code: */ - cpu->cd.alpha.r[ALPHA_T12] = cpu->pc; + case ARCH_SH: + if (cpu->cd.sh.bits == 32) + cpu->pc &= 0xffffffffULL; + cpu->pc &= ~1; break; case ARCH_SPARC: break; - case ARCH_IA64: - break; - - case ARCH_M68K: - break; - - case ARCH_ARM: - cpu->pc &= 0xfffffffc; - cpu->cd.arm.r[ARM_PC] = cpu->pc; - break; - case ARCH_X86: /* * NOTE: The toc field is used to indicate an ELF32 @@ -1243,6 +1354,17 @@ debug("starting cpu%i at ", m->bootstrap_cpu); switch (m->arch) { + + case ARCH_ARM: + /* ARM cpus aren't 64-bit: */ + debug("0x%08x", (int)entrypoint); + break; + + case ARCH_AVR: + /* Atmel AVR uses a 16-bit or 22-bit program counter: */ + debug("0x%04x", (int)entrypoint); + break; + case ARCH_MIPS: if (cpu->is_32bit) { debug("0x%08x", (int)m->cpus[ @@ -1259,20 +1381,19 @@ cpu->cd.mips.gpr[MIPS_GPR_GP]); } break; + case ARCH_PPC: if (cpu->cd.ppc.bits == 32) debug("0x%08x", (int)entrypoint); else debug("0x%016llx", (long long)entrypoint); break; - case ARCH_ARM: - /* ARM cpus aren't 64-bit: */ - debug("0x%08x", (int)entrypoint); - break; + case ARCH_X86: debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], (long long)cpu->pc); break; + default: debug("0x%016llx", (long long)cpu->pc); }