From 49d3c8d76bbfad2b1d7077029727573c8b3ef082 Mon Sep 17 00:00:00 2001
From: Kenneth J Davis <PerditionC@gmail.com>
Date: Tue, 11 May 2021 21:39:29 -0400
Subject: [PATCH] add safety check to FAT12/16 boot sector to warn about
 breaking sys, update new offset in sys.c for NOPing out use of BIOS boot
 drive during boot (fix error about boot sector unsupported format)

---
 boot/boot.asm | 26 +++++++++++++++++++-------
 sys/sys.c     |  2 +-
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/boot/boot.asm b/boot/boot.asm
index 6e9e652..b38e145 100644
--- a/boot/boot.asm
+++ b/boot/boot.asm
@@ -58,6 +58,14 @@
 
 ;%define ISFAT12         1
 ;%define ISFAT16         1
+;verify one and only one of ISFAT12 or ISFAT16 is defined
+%ifdef ISFAT12 
+ %ifdef ISFAT16
+  %error Must select one FS
+ %endif
+%elifndef ISFAT16
+  %error Must select one FS
+%endif
 
 
 segment .text
@@ -104,15 +112,11 @@ Entry:          jmp     short real_start
                 ; The filesystem ID is used by lDOS's instsect (by ecm)
                 ;  by default to validate that the filesystem matches.
 %ifdef ISFAT12
-                db "FAT12"
- %ifdef ISFAT16
- %error Must select one FS
- %endif
+ %define FATFS "FAT12"
 %elifdef ISFAT16
-                db "FAT16"
-%else
- %error Must select one FS
+ %define FATFS "FAT16"
 %endif
+                db FATFS
                 times   3Eh - ($ - $$) db 32
 
 ; using bp-Entry+loadseg_xxx generates smaller code than using just
@@ -417,6 +421,14 @@ read_next:
                 mov     dl, [drive]
 
                 ; NOTE: sys must be updated if location changes!!!
+%ifdef ISFAT12
+  %define LBA_TEST_OFFSET 179h
+%elifdef ISFAT16
+  %define LBA_TEST_OFFSET 176h
+%endif
+%if ($ - Entry) != LBA_TEST_OFFSET
+    %error Must update constant offset (LBA_TEST_OFFSET) to test dl,dl here and in sys.c for FATFS
+%endif
                 test    dl,dl                   ; don't use LBA addressing on A:
                 jz      read_normal_BIOS        ; might be a (buggy)
                                                 ; CDROM-BOOT floppy emulation
diff --git a/sys/sys.c b/sys/sys.c
index c9cbc92..d674fa4 100644
--- a/sys/sys.c
+++ b/sys/sys.c
@@ -1560,7 +1560,7 @@ void put_boot(SYSOptions *opts)
       /* !!! if boot sector changes then update these locations !!! */
       {
           unsigned offset;
-          offset = (fs == FAT16) ? 0x175 : 0x178;
+          offset = (fs == FAT16) ? 0x176 : 0x179;
           
           if ( (newboot[offset]==0x84) && (newboot[offset+1]==0xD2) ) /* test dl,dl */
           {