Hacker News new | ask | show | jobs
by dfe 3 days ago
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.

1 comments

That's a good callout. I think it's fair to say that the design was constrained by what is practical to implement in a tiny amount of assembly, but presumably the original pre-FAT12, pre-DOS version of the filesystem wouldn't have had that exact 448 byte constraint. I found it tough to find much info about it online though.

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...

Back in the 2006-20009 timeframe I maintained a fork of Apple's Darwin boot loader, version boot-132, which was the version released with and compatible with the shipped versions of Intel Mac OS X Tiger (10.4). Later I made it compatible with Leopard (10.5) and Snow Leopard (10.6), including its 64-bit kernel.

My fork started simple: I had analyzed other people's modifications to xnu and realized most of it was removing code that was expecting to read the EFI system table but was instead getting a NULL pointer from the x86 BIOS boot loader. To avoid the need to modify xnu every time a new version came out, I added code that created a stubbed-out EFI system table, basically the minimum amount of stubbing that would allow an unmodified xnu to boot. There was also something about measuring the FSB frequency and including it in the hand-off to xnu. Apparently, Apple's boot.efi (no source ever released) does it and xnu started relying on it. I think in the original version I just hard-coded it to my system's frequency, but eventually I implemented the algorithm.

Later, working as an independent contractor for VMware, I added special code that would boot from a small CD (virtual image) containing the boot loader plus a flat disk image containing additional kernel extensions. This CD image was the one that eventually also contained the Mac OS X guest tools, written by other people. It worked really well for VMware because their codebase already had the concept of "insert the tools CD image" and "reinsert the original CD or DVD when it is ejected", so we just leveraged all of that existing machinery.

In brief, this new pre-boot code loaded everything from the first CD, ejected it, waited for the real Mac OS X DVD to appear, then launched the real boot loader with hand-off information pointing at the flat disk image loaded into memory from the pre-boot CD. That flat disk image contained extra kernel extensions needed to make Mac OS X run on non-Apple hardware. The boot loader simply loaded these extra extensions in addition to the ones on the DVD, which were normally in an mkext archive.

That allowed booting unmodified Mac OS X in VMware Fusion 2, or, with the right set of kexts, on any commodity PC.

Eventually, I got interested in booting from FAT 32 because Apple kept unintentionally breaking the HFS+ tooling needed to update the special "startup file" that is only used by this x86 boot loader. No genuine Apple device aside from the by-then fully deprecated Intel Developer Transition Kit ever used this boot loader or ever had a need for the HFS+ startup file. Testing that it kept working was clearly not a priority.

The EFI System Partition in those days was specified to always be FAT 32, and that's what Apple's disk utilities created and still do when you make a GPT disk. In a typical installation this was and still is the only partition that wasn't the main OS partition. That makes it an ideal location for Hackintosh boot code, because the OS simply has no reason to go poking around there.

I started researching what FAT boot sectors did, and I realized that if you simply formatted a volume as FAT 32 in Windows XP, it would get a boot sector that loaded a file named NTLDR to location 2000:0h, then jumped to that location. I needed Darwin's boot2 at 2000:200h, 512 bytes later. I made a separate 512 byte file beginning with a 3-byte jump to 200h, then padded it with 509 additional bytes to bring it to 512 (200h). This was the birth of DRWNLDR as the concatenation of this new code with the existing boot2.

Then I looked into booting it from an XP-formatted floppy or other FAT 12/16 volume and realized quickly that it does not work because the FAT 12/16 boot sector jumps to 2000:3h instead of 2000:0h. In my version, that was the start of my padding. I needed something there other than INT 18h or a HLT loop, but what?

That's when I went down the rabbit hole of reverse engineering the protocol and making my own entrypoint that worked with Microsoft's boot sector.

The NT FAT 12/16 boot sector is clever and elegant. It jumps to this special entrypoint with, among other things, DS:DI pointing to a structure with three fields: a far pointer to the ReadCluster routine, a far pointer to the ReadSectors routine, and a DWORD number of sectors used as input to the ReadSectors routine.

The first sector of NTLDR (part of the STARTUP.COM portion) contains this special entrypoint and all the code needed to load the entirety of the file. It manages to fit in 1 sector because it does not contain the ReadSectors or ReadCluster code. Instead, it makes far calls to the code that was part of the boot sector. To my knowledge, this boot sector is the only one which exports some of its routines to the boot code it loads. Most other boot sectors only hand off data, not pointers to subroutines.

At some point I decided that because of the HFS+ startup file difficulties, the best approach to the boot loader going forward was to keep it away from the HFS+ volume. Rather than explain to people how to acquire a copy Microsoft's NT FAT 32 boot sector, I decided to write my own.

As mentioned, I did at first attempt to squeeze everything into the boot sector, but I came up short and ultimately used the third sector (LBA 2) to continue my code, exactly as Microsoft did. It's available. Why not use it?

I did not ever write a FAT 12/16 boot sector. In my use case this was fine, I'd just use Windows XP to format the rescue floppy, then copy DRWNLDR onto it as NTLDR. Since floppies were already on their way out at this point, and since most practically-sized partitions can be formatted as FAT 32, I didn't see the point.

Quite frankly, I don't think I or anyone else could do better than the NT FAT boot sector already does.

I would not have expected Microsoft's boot sector to come in handy for hackintosh work!

Very cool, thanks for sharing.