| I enjoyed this article, but I have a comment. > So it's safe to say that the FAT filesystem was originally designed to be so simple that a 448-byte program can at least read it. I take issue with this statement. The 448 byte count is what's available in the very last version of the FAT 12/16 boot code. There was for more space available for code in the original version, but the original version used far less code. On a very early original FAT 12/16 boot sector, only 28 bytes are taken up by boot sector data at the beginning, including the BPB, and 2 more by the signature at the end so you have 482 to work with, most of it unused. So much of it is unused that I'm sure the original design was "that's plenty of room for expansion." Otherwise they might have considered reserving 2 or 3 sectors instead of merely 1. Which, incidentally, is exactly what FAT 32 does because you just can't fit a practical VBR in one sector for FAT 32. Believe me, I tried! In early DOS, definitely PC DOS 1 and I think also 86-DOS, the boot sector code doesn't have any clue at all how to read FAT. It relies solely on the information in the BPB section of its data. Reserved sectors (practically always 1, the boot sector) plus number of FATs times sectors per FAT, plus number of root directory entries divided by entries per sector. Compute entries per sector as sector size divided by 32-bytes per entry. That gives you the location of the first data sector. These very early DOS do not read the root directory at all. They just assume that IBMBIO.COM (a.k.a. IO.SYS) starts at the first data sector, and they just read a fixed number of sectors. In fact, I gotta be honest, I'm not even sure what I said above about them consulting the BPB is accurate for the very earliest versions of FAT boot sectors, I think they might have just hard-coded what the LBA has to be. Ironically, this is the approach taken by nearly all Linux boot loaders. Don't try to understand anything, just hard-code the starting LBA of the next stage. For DOS 2 (hard disks) a 2-byte "hidden sectors" field was added to hold the LBA of the volume. It's 0 for a floppy, but on a hard drive primary partition it's the LBA start of the volume. This is added to all of the other existing stuff to find the disk (instead of volume) LBA so that FAT will work on partitioned disks. Therefore, DOS 2 used 30 bytes instead of 28, plus 2 signature at end, so it has only 480 bytes for code. The data structures don't change until Compaq DOS 3.31 creates FAT 16 B. I'm not as familiar with the code changes in the in-between DOS versions, but I think at some point, maybe as early as DOS 2, the VBR code stops blindly assuming that IBMBIO.COM is in the first data sector, and instead starts blindly assuming that the first root directory entry is the one for IBMBIO.COM. So it adds the starting sector from that entry, but it still doesn't walk the FAT for next sectors, it continues to blindly assume the file is contiguous. Compaq DOS 3.31's FAT 16 B increased the last field (hidden sectors) from 2 to 4 bytes, giving us now 32 bytes. It also added a new 4 byte total sectors, giving us now 36 bytes. So now we only have 474 bytes left for boot code. And about FAT code in general... FAT 16 B is a lot bigger deal than people realize. The entirety of DOS's IO code had to be rewritten from 16-bit LBA to 32-bit LBA, and it had to run on 16-bit machines because even people with original PCs or XTs had hard drives bigger than 32 MB by this point. And, the boot sector code itself has to handle the 32-bit LBA -> CHS translation. You can start to see a pattern here. We started with a whopping 482 bytes available for code, but with all the new data fields we now only have 474 bytes available for code, and that code has gotten a lot bigger because now it's reading at least the first directory entry and it's doing 32-bit LBA -> CHS calculations. Fortunately it's still assuming contiguous allocation so it doesn't have to walk the FAT. Getting back to the data, DOS 4 (I think, but it might have originated in OS/2) added a new data structure following the BPB which I'm not sure even has a name. This structure has a signature in its 3rd byte (29h) and is 26 bytes long, bringing us up to 62 bytes at the beginning, still 2 at the end, and there's your 448 bytes left for code. This little footer structure is just plain weird. The first byte is the BIOS drive number. Previously, the VBR would just use what's in DL, because essentially the way the BIOS works is it makes the read sector call to 0:7c00h and if it succeeds it jumps to that location. Therefore DL necessarily still contains the BIOS drive number. The next byte is reserved, the third byte is the signature. Then there's 4 bytes for the volume serial number, 11 bytes for the volume label (previously it was a special root directory entry), and 8 bytes for a human-readable "file system type". What a ridiculous waste of space. To me, that has mid-to-late 1980s IBM written all over it. So that's where you get your 448 bytes for code, but that's not the original design. The original design has way less code but way more space for it, 482 bytes. FAT 32 has even less. It extends the BPB, shifting that footer to a new location. But it demands that the second sector (LBA 1) be a particular data structure with no code, so usually 3 sectors (LBA 0, 1, and 2) are reserved. The code is then split across sectors 0 and 2 meaning that sector 0's job is more to make sure the first 3 sectors get loaded. But that's a whole other discussion. |
I think the "footer" is referred to as the extended boot record, not to be confused with the concept of the same name in the partition table.
> Which, incidentally, is exactly what FAT 32 does because you just can't fit a practical VBR in one sector for FAT 32. Believe me, I tried!
Sounds like there's a story here...