From 9c846bc8b6fc621639fca7e425d4f23d1cffe058 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Thu, 27 May 2021 15:41:52 +0300 Subject: [PATCH] Load files to ram --- main.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 99c1041..54b3465 100644 --- a/main.c +++ b/main.c @@ -154,7 +154,7 @@ static void parsesprs(const char name[], struct kvm_regs *regs) { fclose(f); } -static void load(const char arg[]) { +static void load(const char arg[], uint8_t *ram) { char name[PATH_MAX]; const char *ptr = strchr(arg, ':'); @@ -180,7 +180,13 @@ static void load(const char arg[]) { const unsigned len = ftell(f); rewind(f); - // TODO copy it up + if (addr + len >= RAMSIZE) { + printf("Tried to use too much RAM\n"); + exit(1); + } + + if (fread(&ram[addr], len, 1, f) != 1) + abort(); fclose(f); } @@ -211,6 +217,11 @@ int main(int argc, char **argv) { uint8_t *ram, *progmem; const char *binpath; + // Yes, we're frugal + if (posix_memalign((void **) &ram, 64 * 1024, RAMSIZE)) + abort(); + memset(ram, 0, RAMSIZE); + memset(®s, 0, sizeof(struct kvm_regs)); memset(&fpregs, 0, sizeof(struct kvm_fpu)); @@ -247,7 +258,7 @@ int main(int argc, char **argv) { parsesprs(optarg, ®s); break; case 'l': - load(optarg); + load(optarg, ram); break; case 'd': if (num_dumps >= MAXDUMPS) { @@ -282,11 +293,6 @@ int main(int argc, char **argv) { help(argv[0]); } - // Yes, we're frugal - if (posix_memalign((void **) &ram, 64 * 1024, RAMSIZE)) - abort(); - memset(ram, 0, RAMSIZE); - fseek(binary, 0, SEEK_END); const unsigned binlen = ftell(binary); rewind(binary); -- 2.30.2