From 384e68529ad6553a559727e07d1b1f679444d222 Mon Sep 17 00:00:00 2001 From: "C. Masloch" Date: Tue, 10 Jan 2023 12:51:12 +0100 Subject: [PATCH] sys: detect small FAT32 as FAT32 (zero in word SPF) Also checks that the total amount of clusters is possible for the detected file system, erroring out otherwise. Small FAT32 is displayed in the verbose output specifically. --- sys/sys.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/sys/sys.c b/sys/sys.c index 712df99..120636a 100644 --- a/sys/sys.c +++ b/sys/sys.c @@ -394,6 +394,7 @@ struct bootsectortype32 { * globals needed by put_boot & check_space */ enum {FAT12 = 12, FAT16 = 16, FAT32 = 32} fs; /* file system type */ +unsigned smallfat32; /* static */ struct xfreespace x; /* we make this static to be 0 by default - this avoids FAT misdetections */ @@ -1507,13 +1508,25 @@ void put_boot(SYSOptions *opts) dataSectors = totalSectors - bs32->bsResSectors - (bs32->bsFATs * fatSize) - rootDirSectors; clusters = dataSectors / (((bs32->bsSecPerClust - 1) & 0xFF) + 1); - - if (clusters < FAT_MAGIC) /* < 4085 */ - fs = FAT12; - else if (clusters < FAT_MAGIC16) /* < 65525 */ - fs = FAT16; - else + + if (bs32->bsFATsecs == 0) { + if (clusters >= 0xFFFfff5) { /* FAT32 has 28 significant bits */ + printf("Too many clusters (%lXh) for FAT32 file system!\n", clusters); + exit(1); + } fs = FAT32; + if (clusters < FAT_MAGIC16) + smallfat32 = 1; + } else { + if (clusters < FAT_MAGIC) /* < 4085 */ + fs = FAT12; + else if (clusters < FAT_MAGIC16) /* < 65525 */ + fs = FAT16; + else { + printf("Too many clusters (%lXh) for non-FAT32 file system!\n", clusters); + exit(1); + } + } } /* bit 0 set if function to use current BPB, clear if Device @@ -1526,7 +1539,7 @@ void put_boot(SYSOptions *opts) if (fs == FAT32) { if (opts->verbose) - printf("FAT type: FAT32\n"); + printf("FAT type: FAT32%s\n", smallfat32 ? " (small)" : ""); /* get default bpb (but not for floppies) */ if (opts->dstDrive >= 2 && generic_block_ioctl(opts->dstDrive + 1, 0x4860, default_bpb) == 0)