From 909da5177f6353d1578f6b9954f5d33f2154f1ae Mon Sep 17 00:00:00 2001 From: James Stanley Date: Wed, 16 Oct 2019 19:55:12 +0100 Subject: [PATCH] Fix filesystem in shell emulator We now treat the block device as fixed-size rather than trying to grow it in response to kernel activity. Previously, if you tried to create 2 files in a row, only the first 1 would actually appear, because the device only ever got larger when a byte was written immediately past the end of the device. Now we just let the kernel write bytes to the disk anywhere, so new files can be created even when the previous file is not completely full. Also, fix buffer overrun in reading filesystem image, and use a more idiomatic fgetc loop. --- tools/emul/shell/shell.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/tools/emul/shell/shell.c b/tools/emul/shell/shell.c index 6def224..03bb936 100644 --- a/tools/emul/shell/shell.c +++ b/tools/emul/shell/shell.c @@ -42,7 +42,6 @@ static Z80Context cpu; static uint8_t mem[0x10000] = {0}; static uint8_t fsdev[MAX_FSDEV_SIZE] = {0}; -static uint32_t fsdev_size = 0; static uint32_t fsdev_ptr = 0; // 0 = idle, 1 = received MSB (of 24bit addr), 2 = received middle addr static int fsdev_addr_lvl = 0; @@ -62,14 +61,13 @@ static uint8_t io_read(int unused, uint16_t addr) fprintf(stderr, "Reading FSDEV in the middle of an addr op (%d)\n", fsdev_ptr); return 0; } - if (fsdev_ptr < fsdev_size) { + if (fsdev_ptr < MAX_FSDEV_SIZE) { #ifdef DEBUG fprintf(stderr, "Reading FSDEV at offset %d\n", fsdev_ptr); #endif return fsdev[fsdev_ptr]; } else { - // don't warn when ==, we're not out of bounds, just at the edge. - if (fsdev_ptr > fsdev_size) { + if (fsdev_ptr >= MAX_FSDEV_SIZE) { fprintf(stderr, "Out of bounds FSDEV read at %d\n", fsdev_ptr); } return 0; @@ -77,11 +75,8 @@ static uint8_t io_read(int unused, uint16_t addr) } else if (addr == FS_ADDR_PORT) { if (fsdev_addr_lvl != 0) { return 3; - } else if (fsdev_ptr > fsdev_size) { - fprintf(stderr, "Out of bounds FSDEV addr request at %d / %d\n", fsdev_ptr, fsdev_size); + } else if (fsdev_ptr > MAX_FSDEV_SIZE) { return 2; - } else if (fsdev_ptr == fsdev_size) { - return 1; } else { return 0; } @@ -105,18 +100,11 @@ static void io_write(int unused, uint16_t addr, uint8_t val) fprintf(stderr, "Writing to FSDEV in the middle of an addr op (%d)\n", fsdev_ptr); return; } - if (fsdev_ptr < fsdev_size) { + if (fsdev_ptr < MAX_FSDEV_SIZE) { #ifdef DEBUG fprintf(stderr, "Writing to FSDEV (%d)\n", fsdev_ptr); #endif fsdev[fsdev_ptr] = val; - } else if ((fsdev_ptr == fsdev_size) && (fsdev_ptr < MAX_FSDEV_SIZE)) { - // We're at the end of fsdev, grow it - fsdev[fsdev_ptr] = val; - fsdev_size++; -#ifdef DEBUG - fprintf(stderr, "Growing FSDEV (%d)\n", fsdev_ptr); -#endif } else { fprintf(stderr, "Out of bounds FSDEV write at %d\n", fsdev_ptr); } @@ -156,13 +144,14 @@ int main() if (fp != NULL) { printf("Initializing filesystem\n"); int i = 0; - int c = fgetc(fp); - while (c != EOF) { - fsdev[i] = c & 0xff; - i++; - c = fgetc(fp); + int c; + while ((c = fgetc(fp)) != EOF && i < MAX_FSDEV_SIZE) { + fsdev[i++] = c & 0xff; + } + if (i == MAX_FSDEV_SIZE) { + fprintf(stderr, "Filesytem image too large.\n"); + return 1; } - fsdev_size = i; pclose(fp); } else { printf("Can't initialize filesystem. Leaving blank.\n");