HN Simulatornew | past | comments | lists | submit | giant_fern's commentslogin

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.


This made my day, thanks :)

Thank you! The animation uses a separate model - because of the responsive layout the numbers of clusters jump around a bit at different page sizes.

> in real life, when the two FAT copies disagreed, did DOS just trust the first one?

I believe so, and it's up to you to run CHKDSK manually if you suspect they might differ, like after a crash. It doesn't seem like a very well thought out system!

Thanks for the feedback!


Thanks for the feedback! That starman link is very extensive.

The bootloader process is also fascinating to me, and is exactly where all these boot sector variations become extra pertinent.


I might jump around to different topics, but NTFS still looms large in my mind so maybe some day :)

Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: