Ver Fonte

attempt to fix fat

Johannes Zellner há 8 meses atrás
pai
commit
572d105fa2
8 ficheiros alterados com 154 adições e 25 exclusões
  1. 2 2
      Makefile
  2. 69 12
      disk.c
  3. 55 7
      fat.c
  4. 13 0
      io.c
  5. 1 0
      io.h
  6. 4 0
      prog.c
  7. 3 2
      run.c
  8. 7 2
      space.c

+ 2 - 2
Makefile

@@ -41,10 +41,10 @@ hello.o: hello.c io.h
 reboot.o: reboot.c
 	$(CC) $(CFLAGS) -o $@ $<
 
-disk.o: disk.c disk.h timer.h
+disk.o: disk.c disk.h io.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-fat.o: fat.c fat.h disk.h
+fat.o: fat.c fat.h disk.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 ls.o: ls.c fat.h io.h

+ 69 - 12
disk.c

@@ -1,6 +1,7 @@
 /* Disk layer: read sectors via direct FDC + ISA DMA in protected mode. */
 
 #include "disk.h"
+#include "io.h"
 #include "timer.h"
 
 #define SECTOR_SIZE       512
@@ -31,12 +32,13 @@
 #define CMD_SPECIFY      0x03
 #define CMD_RECALIBRATE  0x07
 #define CMD_SENSE_INT    0x08
-#define CMD_READ_DATA    0xE6   /* READ | MFM | SK */
+#define CMD_READ_DATA    0x66   /* READ | MFM | SK  (no Multi-Track) */
 #define CMD_SEEK         0x0F
 
 #define MOTOR_DELAY_MS   300
 #define IRQ_TIMEOUT_MS   2000
-#define MAX_RETRIES      3
+#define MAX_RETRIES      10
+#define SEEK_SETTLE_MS   25   /* head settle after seek */
 
 /* PIC master mask port */
 #define PIC1_DATA  0x21
@@ -164,12 +166,15 @@ static int fdc_reset(void)
 
 	outb(FDC_CCR, 0x00); /* 500 kbps for 1.44 MB */
 
-	/* SPECIFY: SRT=3ms, HUT=240ms, HLT=16ms, DMA mode */
+	/* SPECIFY: SRT=12ms, HUT=240ms, HLT=30ms, DMA mode
+	 * Conservative timings for real hardware compatibility.
+	 * Byte 1: SRT_nibble=4 (16-4=12ms), HUT_nibble=F (15*16=240ms) → 0x4F
+	 * Byte 2: HLT=15 (15*2=30ms)<<1 | NDMA=0 → 0x1E */
 	if (fdc_send(CMD_SPECIFY) != 0)
 		return -1;
-	if (fdc_send(0xDF) != 0)
+	if (fdc_send(0x4F) != 0)
 		return -1;
-	if (fdc_send(0x02) != 0)
+	if (fdc_send(0x1E) != 0)
 		return -1;
 
 	return 0;
@@ -216,6 +221,12 @@ static int fdc_seek(unsigned int cylinder, unsigned int head)
 		return -1;
 
 	cur_cylinder = cylinder;
+	/* Let head settle on real hardware before issuing read (avoids LBA 33+ read failures). */
+	{
+		unsigned int t0 = get_ticks_ms();
+		while ((get_ticks_ms() - t0) < SEEK_SETTLE_MS)
+			;
+	}
 	return 0;
 }
 
@@ -268,15 +279,29 @@ static int fdc_do_read(unsigned int cyl, unsigned int head, unsigned int sec)
 	if (fdc_send(0xFF) != 0)            return -1; /* data length */
 
 	if (fdc_wait_irq() != 0)
-		return -1;
+		return -2;
 
 	for (i = 0; i < 7; i++) {
 		if (fdc_recv(&results[i]) != 0)
-			return -1;
+			return -3;
 	}
 
-	if (results[0] & 0xC0)
+	if (results[0] & 0xC0) {
+		io_putstr("fdc: CHS=");
+		io_puthex(cyl);
+		io_putchar('/');
+		io_puthex(head);
+		io_putchar('/');
+		io_puthex(sec);
+		io_putstr(" ST0=");
+		io_puthex(results[0]);
+		io_putstr(" ST1=");
+		io_puthex(results[1]);
+		io_putstr(" ST2=");
+		io_puthex(results[2]);
+		io_putchar('\n');
 		return -1;
+	}
 
 	return 0;
 }
@@ -295,6 +320,22 @@ static int fdc_read_one(unsigned int lba)
 	return fdc_do_read(cyl, head, sec);
 }
 
+/* Aggressive retry: seek far away then back to improve head alignment. */
+static int fdc_read_one_retry(unsigned int lba)
+{
+	unsigned int cyl, head, sec;
+
+	lba_to_chs(lba, &cyl, &head, &sec);
+
+	/* Seek to cylinder 40 (mid-disk) first, then to target.
+	 * The long travel forces the stepper to settle accurately. */
+	fdc_seek(cyl > 40 ? 0 : 40, 0);
+	if (fdc_seek(cyl, head) != 0)
+		return -1;
+
+	return fdc_do_read(cyl, head, sec);
+}
+
 /* ---- public API ---- */
 
 static int fdc_init_once(void)
@@ -331,9 +372,9 @@ static int fdc_init_once(void)
 
 		if (fdc_send(CMD_SPECIFY) != 0)
 			return -1;
-		if (fdc_send(0xDF) != 0)
+		if (fdc_send(0x4F) != 0)
 			return -1;
-		if (fdc_send(0x02) != 0)
+		if (fdc_send(0x1E) != 0)
 			return -1;
 	}
 
@@ -373,7 +414,15 @@ int disk_read_sectors(unsigned int lba, void *buf, unsigned int count)
 	for (i = 0; i < count; i++) {
 		int attempt, ok = 0;
 		for (attempt = 0; attempt < MAX_RETRIES; attempt++) {
-			if (fdc_read_one(lba + i) == 0) {
+			int rc;
+			if (attempt < 3) {
+				rc = fdc_read_one(lba + i);
+			} else {
+				/* Long-seek retry: seek far away then back to
+				 * force accurate head positioning. */
+				rc = fdc_read_one_retry(lba + i);
+			}
+			if (rc == 0) {
 				ok = 1;
 				break;
 			}
@@ -381,8 +430,16 @@ int disk_read_sectors(unsigned int lba, void *buf, unsigned int count)
 			fdc_motor_on();
 			fdc_recalibrate();
 		}
-		if (!ok)
+		if (!ok) {
+			io_putstr("disk: read failed LBA=");
+			io_puthex(lba + i);
+			io_putstr(" (sector ");
+			io_puthex(i);
+			io_putstr(" of ");
+			io_puthex(count);
+			io_putstr(")\n");
 			return -1;
+		}
 		{
 			unsigned int j;
 			unsigned char *dst = (unsigned char *)buf + (i * SECTOR_SIZE);

+ 55 - 7
fat.c

@@ -2,6 +2,7 @@
 
 #include "disk.h"
 #include "fat.h"
+#include "io.h"
 
 #define SECTOR_SIZE 512
 #define ROOT_ENTRY_SIZE 32
@@ -49,8 +50,10 @@ int fat_init(void)
 {
 	unsigned int num_fats, root_entries, sec_per_fat;
 
-	if (fat_read_sector(0) != 0)
+	if (fat_read_sector(0) != 0) {
+		io_putstr("run: disk read error (boot sector LBA=0)\n");
 		return -2;  /* disk I/O error */
+	}
 	if (sector_buf[BPB_SIGNATURE] != 0x55 || sector_buf[BPB_SIGNATURE + 1] != 0xAA)
 		return -1;  /* no valid FAT filesystem */
 	fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
@@ -88,8 +91,14 @@ int fat_list_root(fat_root_callback_t callback)
 	if (!fat_ready || !callback)
 		return -1;
 	for (sec = 0; sec < fat_root_sectors; sec++) {
-		if (fat_read_sector(fat_root_start + sec) != 0)
+		if (fat_read_sector(fat_root_start + sec) != 0) {
+			io_putstr("run: disk read error (root dir sec=");
+			io_puthex(sec);
+			io_putstr(" LBA=");
+			io_puthex(fat_root_start + sec);
+			io_putstr(")\n");
 			return -2;
+		}
 		for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
 			unsigned char *e = sector_buf + off;
 			if (e[0] == 0x00)
@@ -121,8 +130,14 @@ static int fat_find_root(const char *name_8_3, unsigned int *out_cluster, unsign
 	want[11] = '\0';
 
 	for (sec = 0; sec < fat_root_sectors; sec++) {
-		if (fat_read_sector(fat_root_start + sec) != 0)
+		if (fat_read_sector(fat_root_start + sec) != 0) {
+			io_putstr("run: disk read error (find root sec=");
+			io_puthex(sec);
+			io_putstr(" LBA=");
+			io_puthex(fat_root_start + sec);
+			io_putstr(")\n");
 			return -2;
+		}
 		for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
 			unsigned char *e = sector_buf + off;
 			if (e[0] == 0x00)
@@ -149,6 +164,7 @@ int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
 	unsigned int fat_start = fat_reserved;
 	unsigned int copied = 0;
 	unsigned char *dst = (unsigned char *)buf;
+	unsigned int loaded_fat_sec;
 
 	if (!fat_ready || !buf)
 		return -1;
@@ -162,6 +178,18 @@ int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
 	if (size > max_len)
 		size = max_len;
 
+	/* Pre-load the FAT sector covering the first cluster so we can follow
+	 * the chain without re-reading the FAT between every data cluster.
+	 * Data reads use DMA + memcpy to dst and never touch sector_buf,
+	 * so the cached FAT data stays valid throughout. */
+	loaded_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
+	if (fat_read_sector(loaded_fat_sec) != 0) {
+		io_putstr("run: disk read error (FAT preload LBA=");
+		io_puthex(loaded_fat_sec);
+		io_putstr(")\n");
+		return -2;
+	}
+
 	while (cluster >= 2 && cluster < 0xFF8 && copied < size) {
 		unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
 		unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
@@ -169,14 +197,34 @@ int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
 		if (chunk > size - copied)
 			chunk = size - copied;
 		n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
-		if (disk_read_sectors(sector, dst + copied, n_sec) != 0)
+		if (disk_read_sectors(sector, dst + copied, n_sec) != 0) {
+			io_putstr("run: disk read error (file data cluster=");
+			io_puthex(cluster);
+			io_putstr(" LBA=");
+			io_puthex(sector);
+			io_putstr(" n_sec=");
+			io_puthex(n_sec);
+			io_putstr(")\n");
 			return -2;
+		}
 		copied += chunk;
 		if (copied >= size)
 			break;
-		/* Next cluster from FAT */
-		if (fat_read_sector(fat_start + (cluster * 3) / (2 * SECTOR_SIZE)) != 0)
-			return -2;
+		/* Only re-read FAT if the chain crosses into a different FAT sector. */
+		{
+			unsigned int need_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
+			if (need_fat_sec != loaded_fat_sec) {
+				if (fat_read_sector(need_fat_sec) != 0) {
+					io_putstr("run: disk read error (FAT cluster=");
+					io_puthex(cluster);
+					io_putstr(" LBA=");
+					io_puthex(need_fat_sec);
+					io_putstr(")\n");
+					return -2;
+				}
+				loaded_fat_sec = need_fat_sec;
+			}
+		}
 		cluster = fat_get_entry(cluster);
 	}
 	return (int)copied;

+ 13 - 0
io.c

@@ -174,6 +174,19 @@ void io_putstr(const char *s)
 	while (*s) io_putchar(*s++);
 }
 
+void io_puthex(unsigned int n)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	char buf[9];
+	unsigned int i = 8;
+	buf[8] = '\0';
+	do {
+		buf[--i] = hex[n & 0xF];
+		n >>= 4;
+	} while (i > 0);
+	io_putstr(buf);
+}
+
 unsigned int io_get_cursor(void)
 {
 	return cursor;

+ 1 - 0
io.h

@@ -20,6 +20,7 @@ void io_clear_screen(void);
 int io_did_scroll(void);  /* returns 1 once after a scroll, then 0 until next scroll */
 void io_putchar(char c);
 void io_putstr(const char *s);
+void io_puthex(unsigned int n);  /* print n as 8 hex digits, no 0x prefix */
 unsigned int io_get_cursor(void);
 void io_set_cursor(unsigned int pos);
 void io_cursor_show_block(void);

+ 4 - 0
prog.c

@@ -8,8 +8,12 @@
 
 static const struct prog_api *const api = (const struct prog_api *)PROGRAM_API_ADDR;
 
+/* Pad binary past 1KB so it uses cluster 8+ (reproduces LBA 0x27 read failure on real HW). */
+static const char prog_padding[3072] __attribute__((used)) = { 0 };
+
 void _start(void)
 {
+	(void)prog_padding;  /* keep in binary */
 	unsigned int cols, rows, cx, cy;
 	const char *msg = "omg!";
 

+ 3 - 2
run.c

@@ -49,7 +49,7 @@ void program_run_main(const char *filename)
 	to_8_3(filename, 64, name_8_3);
 	n = fat_init();
 	if (n == -2) {
-		io_putstr("run: disk read error\n");
+		io_putstr("run: disk read error (see above)\n");
 		return;
 	}
 	if (n != 0) {
@@ -58,13 +58,14 @@ void program_run_main(const char *filename)
 	}
 	n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
 	if (n == -2) {
-		io_putstr("run: disk read error\n");
+		io_putstr("run: disk read error (see above)\n");
 		return;
 	}
 	if (n <= 0) {
 		io_putstr("run: file not found\n");
 		return;
 	}
+	io_drain_keyboard();
 	{
 		struct prog_api api = {
 			.draw_string_xy = fbtext_draw_string_xy,

+ 7 - 2
space.c

@@ -175,7 +175,7 @@ void _start(void)
 	for (;;) {
 		int want_left = 0, want_right = 0, want_fire = 0;
 		while (api->get_ticks_ms() - last_tick < FRAME_MS) {
-			k = api->poll_key_with_hw();
+			k = api->poll_key();
 			if (k == IO_KEY_ESC)
 				return;
 			if (k == IO_KEY_LEFT)
@@ -197,10 +197,15 @@ void _start(void)
 		update_invaders();
 
 		api->clear_rgb(8, 8, 32);
+		if (api->poll_key() == IO_KEY_ESC)
+			return;
 		draw_ship();
-		for (r = 0; r < INV_ROWS; r++)
+		for (r = 0; r < INV_ROWS; r++) {
 			for (c = 0; c < INV_COLS; c++)
 				draw_invader(r, c);
+			if (api->poll_key() == IO_KEY_ESC)
+				return;
+		}
 		draw_bullets();
 
 		if (num_alive <= 0)