diff --git a/jsemu.c b/jsemu.c index 3641db4..3ae8889 100644 --- a/jsemu.c +++ b/jsemu.c @@ -181,8 +181,20 @@ static void init_vm_fs(void *arg); static void init_vm_drive(void *arg); void vm_start(const char *url, int ram_size, const char *cmdline, - const char *pwd, int width, int height, BOOL has_network) + const char *pwd, int width, int height, BOOL has_network, + const char *drive_url, uint8_t *elf_data0, int elf_len0) { + //// Begin Test: Patch the ELF Data to a.out in Initial RAM Disk + extern uint8_t elf_data[]; // From riscv_machine.c + extern int elf_len; + elf_len = elf_len0; + + // Must copy ELF Data to Local Buffer because it will get overwritten + printf("elf_len=%d\n", elf_len); + if (elf_len > 4096) { puts("*** ERROR: elf_len exceeds 4096, increase elf_data and a.out size"); } + memcpy(elf_data, elf_data0, elf_len); + //// End Test + VMStartState *s; s = mallocz(sizeof(*s)); diff --git a/riscv_machine.c b/riscv_machine.c index bdd3668..316f557 100644 --- a/riscv_machine.c +++ b/riscv_machine.c @@ -43,6 +43,80 @@ #define _info(...) {} //// // #define _info printf //// +#ifdef NOTUSED +uint8_t elf_data2[] = { //// + 0x7F, 0x45, 0x4C, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0xF3, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x09, 0x00, 0x08, 0x00, + 0x13, 0x01, 0x01, 0xFA, 0x23, 0x3C, 0x11, 0x04, 0x23, 0x38, 0x81, 0x04, 0x13, 0x04, 0x01, 0x06, + 0x13, 0x00, 0x00, 0x00, 0x23, 0x34, 0xA4, 0xFE, 0x23, 0x30, 0xB4, 0xFE, 0x1B, 0x05, 0xD0, 0x03, + 0x23, 0x2E, 0xA4, 0xFC, 0x1B, 0x05, 0x10, 0x00, 0x23, 0x38, 0xA4, 0xFC, 0x17, 0x05, 0x00, 0x00, + 0x13, 0x05, 0x05, 0x00, 0x23, 0x34, 0xA4, 0xFC, 0x1B, 0x05, 0xF0, 0x00, 0x23, 0x30, 0xA4, 0xFC, + 0x03, 0x25, 0xC4, 0xFD, 0x13, 0x15, 0x05, 0x02, 0x13, 0x55, 0x05, 0x02, 0x23, 0x3C, 0xA4, 0xFA, + 0x03, 0x35, 0x04, 0xFD, 0x23, 0x38, 0xA4, 0xFA, 0x03, 0x35, 0x84, 0xFC, 0x23, 0x34, 0xA4, 0xFA, + 0x03, 0x35, 0x04, 0xFC, 0x23, 0x30, 0xA4, 0xFA, 0x13, 0x05, 0xD0, 0x03, 0x93, 0x05, 0x10, 0x00, + 0x37, 0x06, 0x08, 0x00, 0x1B, 0x06, 0x16, 0x10, 0x13, 0x16, 0xC6, 0x00, 0x93, 0x06, 0xF0, 0x00, + 0x73, 0x00, 0x00, 0x00, 0x01, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x83, 0x30, 0x81, 0x05, 0x03, 0x34, + 0x01, 0x05, 0x13, 0x01, 0x01, 0x06, 0x67, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x21, 0x0A, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0xF1, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0E, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x9A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x2E, 0x63, 0x00, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x4C, 0x2E, + 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x2E, 0x74, 0x65, 0x78, 0x74, 0x00, 0x2E, 0x64, 0x61, 0x74, 0x61, 0x00, 0x2E, 0x64, 0x61, + 0x74, 0x61, 0x2E, 0x72, 0x6F, 0x00, 0x2E, 0x62, 0x73, 0x73, 0x00, 0x2E, 0x73, 0x79, 0x6D, 0x74, + 0x61, 0x62, 0x00, 0x2E, 0x73, 0x74, 0x72, 0x74, 0x61, 0x62, 0x00, 0x2E, 0x72, 0x65, 0x6C, 0x61, + 0x2E, 0x74, 0x65, 0x78, 0x74, 0x00, 0x2E, 0x73, 0x68, 0x73, 0x74, 0x72, 0x74, 0x61, 0x62, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x9A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0D, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x16, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1B, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x23, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2B, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x36, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; //// +const int elf_len2 = sizeof(elf_data2); //// TODO +#endif // NOTUSED + /* RISCV machine */ typedef struct RISCVMachine { @@ -91,6 +165,8 @@ uint64_t rdtime_addr = 0; uint64_t dcache_iall_addr = 0; uint64_t sync_s_addr = 0; uint64_t real_time = 0; +uint8_t elf_data[4096]; +int elf_len = 0; static uint64_t rtc_get_real_time(RISCVMachine *s) { @@ -955,6 +1031,24 @@ static void copy_bios(RISCVMachine *s, const uint8_t *buf, int buf_len, printf("Patched SYNC.S (Ensure that all Cache Operations are completed) at %p\n", sync_s_addr); } } + + // Patch the ELF Data to a.out in Initial RAM Disk + uint64_t elf_addr = 0; + printf("elf_len=%d\n", elf_len); + if (elf_len > 0) { + //// TODO: Fix the Image Size + for (int i = 0; i < 0xD61680; i++) { + const uint8_t pattern[] = { 0x22, 0x05, 0x69, 0x00 }; + if (memcmp(&kernel_ptr[i], pattern, sizeof(pattern)) == 0) { + //// TODO: Catch overflow of a.out + memcpy(&kernel_ptr[i], elf_data, elf_len); + elf_addr = RAM_BASE_ADDR + i; + printf("Patched ELF Data to a.out at %p\n", elf_addr); + break; + } + } + if (elf_addr == 0) { puts("*** ERROR: Pattern for ELF Data a.out is missing"); } + } //// End Test }