Skip to content

Commit

Permalink
ELF Data tested OK
Browse files Browse the repository at this point in the history
  • Loading branch information
lupyuen committed Jan 31, 2024
1 parent 71ee7ea commit c2f2e5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
13 changes: 8 additions & 5 deletions jsemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,16 @@ 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 *drive_url, uint8_t *elf_data, int elf_len)
const char *drive_url, uint8_t *elf_data0, int elf_len0)
{
//// Begin Test
//// Begin Test: Patch the ELF Data to a.out in Initial RAM Disk
extern uint8_t elf_data[];
extern int elf_len;
elf_len = elf_len0;

printf("elf_len=%d\n", elf_len);
for (int i = 0; i < elf_len; i++) {
printf("elf_data[%d]=0x%x\n", i, elf_data[i]);
}
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;
Expand Down
37 changes: 26 additions & 11 deletions riscv_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
#define _info(...) {} ////
// #define _info printf ////

uint8_t elf_data[] = { ////
#ifndef 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,
Expand Down Expand Up @@ -113,6 +114,8 @@ uint8_t elf_data[] = { ////
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 */

Expand Down Expand Up @@ -162,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)
{
Expand Down Expand Up @@ -1027,20 +1032,30 @@ static void copy_bios(RISCVMachine *s, const uint8_t *buf, int buf_len,
}
}

// Patch the ELF Data to a.out
// Patch the ELF Data to a.out in Initial RAM Disk
uint64_t elf_addr = 0;
const int elf_len = sizeof(elf_data); //// TODO
printf("elf_len=%d\n", elf_len);
for (int i = 0; i < 0xD61680; i++) { //// TODO: Fix the Image Size
const uint8_t pattern[] = { 0x22, 0x05, 0x69, 0x00 };
if (memcmp(&kernel_ptr[i], pattern, sizeof(pattern)) == 0) {
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;
for (int i = 0; i < elf_len; i++) {
printf("elf_data[%d]=0x%x\n", i, elf_data[i]);
if (elf_data[i] != elf_data2[i]) {
printf("*** elf_data2[%d]=0x%x\n", i, elf_data2[i]);
}
}
if (elf_len > 0) {
printf("memcmp=%d\n", memcmp(elf_data, elf_data2, elf_len)); ////
//// 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"); }
}
if (elf_addr == 0) { puts("*** ERROR: Pattern for ELF Data a.out is missing"); }
//// End Test
}

Expand Down

0 comments on commit c2f2e5b

Please sign in to comment.