--- trunk/src/emul.c 2007/10/08 16:18:00 4 +++ trunk/src/emul.c 2007/10/08 16:18:11 6 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.184 2005/04/20 04:43:52 debug Exp $ + * $Id: emul.c,v 1.203 2005/06/03 07:39:27 debug Exp $ * * Emulation startup and misc. routines. */ @@ -66,6 +66,8 @@ extern struct emul *debugger_emul; extern struct diskimage *diskimages[]; +static char *diskimage_types[] = DISKIMAGE_TYPES; + /* * add_dump_points(): @@ -131,25 +133,329 @@ /* + * iso_load_bootblock(): + * + * Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for + * "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra). + * + * TODO: This function uses too many magic offsets and so on; it should be + * cleaned up some day. + * + * Returns 1 on success, 0 on failure. + */ +static int iso_load_bootblock(struct machine *m, struct cpu *cpu, + int disk_id, int disk_type, int iso_type, unsigned char *buf, + int *n_loadp, char ***load_namesp) +{ + char str[35]; + int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4; + int found_dir; + uint64_t dirofs; + uint64_t fileofs, filelen; + unsigned char *dirbuf = NULL, *dp; + unsigned char *match_entry = NULL; + char *p, *filename_orig; + char *filename = strdup(cpu->machine->boot_kernel_filename); + unsigned char *filebuf = NULL; + char *tmpfilename = NULL; + char **new_array; + int tmpfile_handle; + + if (filename == NULL) { + fatal("out of memory\n"); + exit(1); + } + filename_orig = filename; + + debug("ISO9660 boot:\n"); + debug_indentation(iadd); + + /* Volume ID: */ + ofs = iso_type == 3? 48 : 40; + memcpy(str, buf + ofs, sizeof(str)); + str[32] = '\0'; i = 31; + while (i >= 0 && str[i]==' ') + str[i--] = '\0'; + if (str[0]) + debug("\"%s\"", str); + else { + /* System ID: */ + ofs = iso_type == 3? 16 : 8; + memcpy(str, buf + ofs, sizeof(str)); + str[32] = '\0'; i = 31; + while (i >= 0 && str[i]==' ') + str[i--] = '\0'; + if (str[0]) + debug("\"%s\"", str); + else + debug("(no ID)"); + } + + debug(":%s\n", filename); + + + /* + * Traverse the directory structure to find the kernel. + */ + + dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86]; + if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89]) + fatal("WARNING: Root directory length mismatch?\n"); + + dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + + (buf[0x8f] << 24)) * 2048; + + /* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ + + dirbuf = malloc(dirlen); + if (dirbuf == NULL) { + fatal("out of memory in iso_load_bootblock()\n"); + exit(1); + } + + res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf, + dirlen); + if (!res2) { + fatal("Couldn't read the disk image. Aborting.\n"); + goto ret; + } + + found_dir = 1; /* Assume root dir */ + dp = dirbuf; filenr = 1; + p = NULL; + while (dp < dirbuf + dirlen) { + int i, nlen = dp[0]; + int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24); + int y = dp[6] + (dp[7] << 8); + char direntry[65]; + + dp += 8; + + /* + * As long as there is an \ or / in the filename, then we + * have not yet found the directory. + */ + p = strchr(filename, '/'); + if (p == NULL) + p = strchr(filename, '\\'); + + /* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? + " [CURRENT]" : "", x, y); */ + for (i=0; i 2047) { + dirofs = (dirofs | 2047) + 1; + /* debug("realign dirofs = 0x%llx\n", dirofs); */ + } + + res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, + dirbuf, 256); + if (!res2) { + fatal("Couldn't read the disk image. Aborting.\n"); + goto ret; + } + + dp = dirbuf; + len = dp[0]; + if (len < 2) + break; + + /* + * TODO: Actually parse the directory entry! + * + * Haha, this must be rewritten. + */ + for (i=32; imachine_type) { case MACHINE_DEC: @@ -169,7 +475,7 @@ * nr of blocks to read and offset are repeated until nr of * blocks to read is zero. */ - res = diskimage_access(m, boot_disk_id, 0, 0, + res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, minibuf, sizeof(minibuf)); bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8) @@ -196,11 +502,12 @@ readofs = 0x18; for (;;) { - res = diskimage_access(m, boot_disk_id, 0, readofs, - minibuf, sizeof(minibuf)); + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, readofs, minibuf, sizeof(minibuf)); if (!res) { - printf("couldn't read disk?\n"); - exit(1); + fatal("Couldn't read the disk image. " + "Aborting.\n"); + return 0; } n_blocks = minibuf[0] + (minibuf[1] << 8) @@ -225,8 +532,8 @@ exit(1); } - res = diskimage_access(m, boot_disk_id, 0, - bootblock_offset, bootblock_buf, n_blocks * 512); + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, bootblock_offset, bootblock_buf, n_blocks * 512); if (!res) { fatal("WARNING: could not load bootblocks from" " disk offset 0x%llx\n", @@ -242,11 +549,13 @@ } debug(readofs == 0x18? ": no blocks?\n" : " blocks\n"); - break; + return 1; case MACHINE_X86: - cpu->cd.x86.mode = 16; - cpu->pc = 0x7c00; + /* TODO: "El Torito" etc? */ + if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id, + boot_disk_type)) + break; bootblock_buf = malloc(512); if (bootblock_buf == NULL) { @@ -254,26 +563,67 @@ exit(1); } - res = diskimage_access(m, boot_disk_id, 0, 0, + debug("loading PC bootsector from %s id %i\n", + diskimage_types[boot_disk_type], boot_disk_id); + + res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0, bootblock_buf, 512); if (!res) { - printf("Couldn't read the disk image. Aborting.\n"); - exit(1); + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; } - debug("loading PC bootsector from disk %i\n", boot_disk_id); if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa) debug("WARNING! The 0x55,0xAA marker is missing! " "Booting anyway.\n"); store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512); free(bootblock_buf); - break; - default: - fatal("Booting from disk without a separate kernel " - "doesn't work in this emulation mode.\n"); + return 1; + } + + + /* + * Try reading a kernel manually from the disk. The code here + * does not rely on machine-dependant boot blocks etc. + */ + /* ISO9660: (0x800 bytes at 0x8000) */ + bootblock_buf = malloc(0x800); + if (bootblock_buf == NULL) { + fprintf(stderr, "Out of memory.\n"); exit(1); } + + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, 0x8000, bootblock_buf, 0x800); + if (!res) { + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; + } + + iso_type = 0; + if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0) + iso_type = 1; + if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0) + iso_type = 2; + if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0) + iso_type = 3; + + if (iso_type != 0) { + /* 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("\nISO9660 filesystem, but no kernel " + "specified? (Use the -j option.)\n"); + else + retval = iso_load_bootblock(m, cpu, boot_disk_id, + boot_disk_type, iso_type, bootblock_buf, + n_loadp, load_namesp); + } + + free(bootblock_buf); + return retval; } @@ -365,7 +715,7 @@ arcbios_add_memory_descriptor(cpu, start, len, ARCBIOS_MEM_LoadedProgram); - scsicontroller = arcbios_get_scsicontroller(); + scsicontroller = arcbios_get_scsicontroller(m); if (scsicontroller == 0) return; @@ -413,25 +763,25 @@ snprintf(component_string, sizeof(component_string), "scsi(0)cdrom(%i)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidevice); snprintf(component_string, sizeof(component_string), "scsi(0)cdrom(%i)fdisk(0)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidisk); } else { snprintf(component_string, sizeof(component_string), "scsi(0)disk(%i)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidevice); snprintf(component_string, sizeof(component_string), "scsi(0)disk(%i)rdisk(0)", d->id); - arcbios_add_string_to_component( + arcbios_add_string_to_component(m, component_string, scsidisk); } } @@ -457,7 +807,7 @@ struct emul *emul; struct cpu *cpu; int i, iadd=4; - uint64_t addr, memory_amount, entrypoint = 0, gp = 0, toc = 0; + uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0; int byte_order; emul = m->emul; @@ -562,23 +912,17 @@ x11_init(m); /* Fill memory with random bytes: */ - /* TODO: This is MIPS-specific! */ if (m->random_mem_contents) { for (i=0; iphysical_ram_in_mb * 1048576; i+=256) { unsigned char data[256]; unsigned int j; for (j=0; jmemory_rw(cpu, m->memory, addr, data, sizeof(data), - MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS); + cpu->memory_rw(cpu, m->memory, i, data, sizeof(data), + MEM_WRITE, CACHE_NONE | NO_EXCEPTIONS | PHYSICAL); } } - if ((m->machine_type == MACHINE_ARC || - m->machine_type == MACHINE_SGI) && m->prom_emulation) - arcbios_init(); - if (m->userland_emul != NULL) { /* * For userland-only emulation, no machine emulation @@ -595,9 +939,14 @@ /* Load files (ROM code, boot code, ...) into memory: */ if (n_load == 0) { - if (m->first_diskimage != NULL) - load_bootblock(m, cpu); - else { + if (m->first_diskimage != NULL) { + if (!load_bootblock(m, cpu, &n_load, &load_names)) { + fprintf(stderr, "\nNo executable files were" + " specified, and booting directly from disk" + " failed.\n"); + exit(1); + } + } else { fprintf(stderr, "No executable file(s) loaded, and " "we are not booting directly from a disk image." "\nAborting.\n"); @@ -606,11 +955,66 @@ } while (n_load > 0) { + FILE *tmp_f; + char *name_to_load = *load_names; + int remove_after_load = 0; + + /* Special hack for removing temporary files: */ + if (name_to_load[0] == 8) { + name_to_load ++; + remove_after_load = 1; + } + + /* + * Another special hack for temporary files; running gunzip + * on them, if they have a gzip header. TODO: Change this + * into some kind of generic support for gzipped files! + */ + tmp_f = fopen(name_to_load, "r"); + if (tmp_f != NULL) { + unsigned char buf[2]; /* gzip header */ + memset(buf, 0, sizeof(buf)); + fread(buf, 1, sizeof(buf), tmp_f); + if (buf[0]==0x1f && buf[1]==0x8b) { + char *zz = malloc(strlen(name_to_load)*2 + 100); + debug("gunziping %s\n", name_to_load); + sprintf(zz, "mv %s %s.gz", name_to_load, + name_to_load); + system(zz); + sprintf(zz, "gunzip %s.gz", name_to_load); + system(zz); + free(zz); + } + fclose(tmp_f); + } + + /* Special things required _before_ loading the file: */ + switch (m->arch) { + case ARCH_X86: + /* + * X86 machines normally don't need to load any files, + * they can boot from disk directly. Therefore, an x86 + * machine usually boots up in 16-bit real mode. When + * loading a 32-bit (or even 64-bit) ELF, that's not + * very nice, hence this special case. + */ + pc_bios_simple_pmode_setup(cpu); + break; + } + byte_order = NO_BYTE_ORDER_OVERRIDE; - file_load(m, m->memory, *load_names, &entrypoint, + /* + * Load the file: :-) + */ + file_load(m, m->memory, name_to_load, &entrypoint, m->arch, &gp, &byte_order, &toc); + if (remove_after_load) { + debug("removing %s\n", name_to_load); + unlink(name_to_load); + } + if (byte_order != NO_BYTE_ORDER_OVERRIDE) cpu->byte_order = byte_order; @@ -631,7 +1035,10 @@ break; case ARCH_PPC: + /* See http://www.linuxbase.org/spec/ELF/ppc64/ + spec/x458.html for more info. */ cpu->cd.ppc.gpr[2] = toc; + /* TODO */ break; case ARCH_ALPHA: @@ -640,15 +1047,26 @@ case ARCH_URISC: break; + case ARCH_ARM: + cpu->pc &= 0xffffffff; + break; + case ARCH_X86: /* - * NOTE: The toc field is used to indicate an ELF64 - * load, on AMD64! + * NOTE: The toc field is used to indicate an ELF32 + * or ELF64 load. */ - if (toc != 0) { - cpu->cd.x86.mode = 64; - } else + switch (toc) { + case 0: /* 16-bit? TODO */ + cpu->pc &= 0xffffffffULL; + break; + case 1: /* 32-bit. */ cpu->pc &= 0xffffffffULL; + break; + case 2: /* 64-bit: TODO */ + fatal("64-bit x86 load. TODO\n"); + exit(1); + } break; default: @@ -740,6 +1158,10 @@ 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_URISC: { char tmps[100]; @@ -764,13 +1186,8 @@ } break; case ARCH_X86: - if (cpu->cd.x86.mode == 16) - debug("0x%04x:0x%04x", cpu->cd.x86.s[X86_S_CS], - (int)cpu->pc); - else if (cpu->cd.x86.mode == 32) - debug("0x%08x", (int)cpu->pc); - else - debug("0x%016llx", (long long)cpu->pc); + 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);