The article, game and visual aids are delightfully well-crafted. I enjoyed reading this. Thank you!
Like many others, this brought back memories, but it also leaves me with a certain feeling of wistfulness: personal computing technology is now _harder_ to learn than it was. My kids are approaching the age that I was when I started learning about topics like this, and I think it's harder for them than it was for me to access the first principles of a machine. The common retort to this is "oh, higher level abstractions are more useful". Yes, but something of value is lost when the technical (and informational) barriers to tinkering have been raised so high.
xyzmrm 7 hours ago [-]
Yep kids these days know how to use tech devices: computers, tablets mobiles you name it. Thery are even pretty capable to spot that hidden menu that you always overlook but in terms of deep knowledge I find them pretty lacking, they dont understand the logic behind a filesystem etc.
compsciphd 10 hours ago [-]
Without even reading this, it made me ponder if defrag program "wasted" a lot of time making files contiguous that were rarely read and when read (on this era of PC) performance didn't matter much (think log files that are mostly append only).
compiler-guy 2 days ago [-]
Terrific, reminded me of many nights watching norton do its thing.
A bonus for the game might be to defrag in such a way that the free space is all contiguous. I seem to recall that all files were moved to the lowest cluster numbers, which maximizes contiguous free space at the end.
Anyway brought back some good memories.
Firehawke 2 days ago [-]
Correct. You'd prefer a full defragment putting free space at the end so that new files written to the drive have a much higher chance of not being fragmented from the start. It often takes a fair bit longer to do a full defrag, though, so most people went with quick defrags and only used full when the free space was getting too fragmented.
rkagerer 1 days ago [-]
I kind of wonder what the Internet would look like now if IPv4 had evolved similarly to FAT16 --> FAT32.
lock1 1 days ago [-]
Man, reminds me of my TA days. Staring at Microsoft FAT specification docs, designing FAT-like toy FS, writing manim code manually.
Looking at this project, I'm tempted to revisit & rewrite my TA guidebook using LLM assistance for interactive visualization.
Cool work
giant_fern 1 days ago [-]
This made my day, thanks :)
1 days ago [-]
reactor3d 2 days ago [-]
The two reading exercises make a nice pair. Starting with a file that fits in one cluster makes it clear what the allocation table adds when a file is scattered across the disk.
mradex77 1 days ago [-]
This is a cool way to learn how filesystems work.
Are you thinking about covering FAT32 or other filesystems next?
giant_fern 1 days ago [-]
I might jump around to different topics, but NTFS still looms large in my mind so maybe some day :)
nticompass 1 days ago [-]
I like the little defrag game, it's fun.
abeauvois 3 days ago [-]
such an emotion! Amiga, FAT16 & 32 ...colored DOS screen, I remember the boot viruses that used to change the first pointer to the boot code. The FAT backup saved my day several times.
dfe 23 hours 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.
giant_fern 15 hours ago [-]
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...
dfe 7 hours ago [-]
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.
giant_fern 4 hours ago [-]
I would not have expected Microsoft's boot sector to come in handy for hackintosh work!
Very cool, thanks for sharing.
fuzzfactor 1 days ago [-]
>Would love to hear corrections
You presented good info quite well and I am impressed :)
More understanding please ;)
This is not meant to be harsh but I would bring the terminology up to the next level at least.
That's a little correction but mostly "color" from me :)
A "disk" is actually most correctly a single piece of hardware, basically otherwise known equally correctly as a "drive". Probably why they call them HDDs and SSDs ;)
Unfortunately both terms were spread conventionally by Microsoft as interchangeably poor substitutes for the more accurate terms "partition" and "volume". At one time those who were fortunate enough to even have a HDD were taught to refer to it as their "C:" drive. "A:" and "B:" were certainly always floppy hardware drives by definition, regardless of which drive you inserted a floppy disk into, and that basic terminology logic was simply extended when you got the long-coveted HDD, so that worked somewhat as a learning shortcut. You have addressed this pretty well by skipping over the partitioning stuff, I don't blame you a bit ;)
But at the time it's also as if it was not possible to end up having up to 4 bootable primary partitions per HDD, each independently containing your choice of folders & files somewhat like a 4-drawer filing cabinet. Whether you chose to put a bootable fileset having a different OS on each partition, or merely use one or more partitions for plain ordinary storage was supposed to be your choice. On powerup I kinda liked having the choice of DOS, WindowsNT, Windows 3.1, and Linux from the "standard" Microsoft bootmenu myself :) Instead most users never dreamed they would ever see any "drive" other than "C:". DOS & Windows PCs intentionally sold with the entire HDD already "divided" into a single FAT partition taking up the whole disk, with the OS pre-"installed" to it, and plenty of space left over for your valuable data storage to be intimately intermixed with the OS files & folders. IOW if you wanted to take advantage of multibooting, or merely confine your data more safely away from your OS by using different volumes, you would most likely be expected to buy an additional HDD to use as a mainstream "D: drive". Otherwise you were on your own with no documentation nor tools from Microsoft which would be as helpful as they could be for multi-partitioning a single HDD, especially when it comes to booting a secondary OS. Remember, until CDROMs and later USB sticks started showing up all over the place, any existence of a "D:" volume or any other letter of the alphabet beyond C: was considered voodoo of some kind and discouraged as being not for the faint of heart.
But the wrong false move on your only volume (C:, where the OS is more likely to become self-corrupt than your own more-valuable data it is sharing space with), and you were expected to lose your OS and data at the same time. Even now to some extent this concept lingers :\
So if some geek came along with two HDDs in one PC, a "C:" and a "D:" "drive", it was looked at by regular office workers as about the most confusing undocumented thing at the time. Even worse if there was only one HDD and it was partitioned into more than one partition so you had like a D: E: & F: to go along with your C:. However for most users CDROMs usually were the first time they had ever seen a D:, then if they were advanced, "risked it all" and added 3 more primary partitions to their HDD they could end up with C:, E:, F: & G: on the same HDD, some users' head could explode with that ;) Plus whichever volume you boot to at the time is designated as C:, and the remaining alphabetic letters assigned in relation to that according to a fairly interesting hierarchy.
Of course multi-partitioning was usually only done using third-party tools, Microsoft was not your friend here. Actually not usual at all, more like "seldom if ever" in most offices.
Anyway, namely a volume is a formatted partition, so partition and volume are not even supposed to have the same meaning. Neither one is best referred to as a "disk" nor a "drive" when you start really focusing on filesystems, leaving you responsible for making the distinction more correctly than most people have been taught to do.
Now under conventional (legacy) BIOS, sector 0 of the HDD contains the Master Boot Record (MBR) within the first 440 bytes followed by the NT serial number if present, then the next bytes consist of the partition table followed by the final "signature bytes". All within 512 bytes. The MBR is the code that interprets the partition table, and then jumps to the particular partition that is marked "Active" at the time.
Any one of these partitions is where your designated "sector 0" comes in, not exactly "start of disk" proper at all. Note that the first sector of each partition is always a bit "downstream" from the actual sector 0 of the HDD (as defined by the particular partition table), even when you only have a single partition encompassing the whole HDD. Simply enough, your "start of disk" is actually a start of a single volume contained within the disk. At the first sector of the volume like this is where you correctly follow the Boot Sector code proper which interprets the particular filesystem on the volume. And from which point the booted partition acts about as closely to a floppy as it's going to.
A volume's boot sector is more descriptively known as a Volume Boot Record (VBR), or even a Partition Boot Record (PBR) sometimes. Also it's not the VBR, it's the MBR where the code must be restricted to the first 440 bytes of HDD sector 0. Bytes 441 thru 444 are reserved or occupied by the NT serial number, 445 & 446 were always zeros and unused (although these days some non-zero data finds its way there somehow sometimes, I don't know from where yet). 447 & 448 are the first two bytes of the partition table itself which extends to 510, so off-limits to MBR code as well. 511 & 512 are the final signature bytes.
For a VBR sector, much more variation can be seen between different filesystems, the code can be more or less than 440 bytes plus usually followed by error messages that can extend almost to the end of the sector since the later bytes do not contain a partition table so are available for other use, because a VBR is not an MBR. Even though there is some cross-functionality tracing back to floppies, the MBR is supposed to be OS-agnostic and simply jump to the active partition according to the partition table. Any VBR code found then interprets the specific filesystem on that particular volume and runs the expected boot files contained and found in that filesystem.
So that's more or less a numerical correction there :)
I say keep doing what you're doing because it is coming out good so far :) Excellent really.
Prepare the terminology to be consistent enough now to better address the partitioning process in detail later.
If you haven't looked over the Starman this is old info (newer than FAT16 though) but contemporary with FAT32 and early NTFS, extensive but not as complete as it could be and not always perfect, but very useful. For instance hidden sectors are now usually 2048, instead of 63 which was the most common first-partition starting point decades ago, but there were also sometimes 32 or 64 hidden among others like 128. BIOS is supposed to read the sectors well enough to handle this but lots of motherboards would only boot to a volume having 63:
Keep posting as you make progress, when you get to FAT32 I'll have more to add since that's mainly what UEFI is expected to boot with.
I confirm these kind of things using extensive experimentation.
Oh yeah, keep in mind that FAT32 boot sectors can come in a variety of forms appropriate for the different generations of boot files which are intended to be found and executed.
Originally they were a FAT DOS bootsector, written to the volume by formatting or "SYSing" the volume while booted to DOS or W9x, a DOS bootsector seeks the IO.SYS file. Then FAT NT3 and NT4 were not as well-developed before reaching FAT NT5 for W2K & XP, seeking NTLDR as a boot file not much differently than an NTFS NT5 bootsector does. Next we find ourselves at NT6 FAT bootsectors since Vista, now seeking a BOOTMGR file in the filesystem as well as the expected well-crafted BOOT folder to launch the OS, as far as BIOS goes. OTOH under UEFI mode instead of BIOS, the firmware is OK without any BOOT folder to start Windows[0] and seeks the EFI folder regardless of what OS the FAT boot volume was formatted with.
[0] Some Linux distros in UEFI mode can depend on having a BOOT folder containing their specific extra files though, in addition to the nominal EFI folder in the FAT ESP bootvolume.
giant_fern 1 days ago [-]
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.
WanderZil 6 hours ago [-]
[dead]
visionwei 1 days ago [-]
What made the cluster chain click for me was §4: following a file's chain by hopping between the FAT and the data area, watching 3 → 7 → 5 resolve step by step, beat any static diagram of a linked list. Same in §5: deleting ALICE.TXT showed 'delete' just means stamping 0xE5 on the name and freeing one FAT entry, which made the whole undelete story obvious. One question: the toy disk uses a single FAT, so the backup copy never comes into play — in real life, when the two FAT copies disagreed, did DOS just trust the first one? §8 flags long filenames and subdirectories as out of scope, but I'd love a VFAT follow-up — that's the part that still bites when mounting old drives today.
giant_fern 1 days ago [-]
> 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!
ukdstore 1 days ago [-]
Opening on a working defrag screen as the title card is a lovely touch, and the page still reads fine with JavaScript off, which most explorable explanations skip.
Is the cluster map on the title screen driven by the same FAT model the later demos use, or is it a separate animation?
giant_fern 1 days ago [-]
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.
precious_reads 4 days ago [-]
[flagged]
Rendered at 19:18:11 GMT+0000 (Coordinated Universal Time) with Vercel.
Like many others, this brought back memories, but it also leaves me with a certain feeling of wistfulness: personal computing technology is now _harder_ to learn than it was. My kids are approaching the age that I was when I started learning about topics like this, and I think it's harder for them than it was for me to access the first principles of a machine. The common retort to this is "oh, higher level abstractions are more useful". Yes, but something of value is lost when the technical (and informational) barriers to tinkering have been raised so high.
A bonus for the game might be to defrag in such a way that the free space is all contiguous. I seem to recall that all files were moved to the lowest cluster numbers, which maximizes contiguous free space at the end.
Anyway brought back some good memories.
Looking at this project, I'm tempted to revisit & rewrite my TA guidebook using LLM assistance for interactive visualization.
Cool work
Are you thinking about covering FAT32 or other filesystems next?
> 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...
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.
Very cool, thanks for sharing.
You presented good info quite well and I am impressed :)
More understanding please ;)
This is not meant to be harsh but I would bring the terminology up to the next level at least.
That's a little correction but mostly "color" from me :)
A "disk" is actually most correctly a single piece of hardware, basically otherwise known equally correctly as a "drive". Probably why they call them HDDs and SSDs ;)
Unfortunately both terms were spread conventionally by Microsoft as interchangeably poor substitutes for the more accurate terms "partition" and "volume". At one time those who were fortunate enough to even have a HDD were taught to refer to it as their "C:" drive. "A:" and "B:" were certainly always floppy hardware drives by definition, regardless of which drive you inserted a floppy disk into, and that basic terminology logic was simply extended when you got the long-coveted HDD, so that worked somewhat as a learning shortcut. You have addressed this pretty well by skipping over the partitioning stuff, I don't blame you a bit ;)
But at the time it's also as if it was not possible to end up having up to 4 bootable primary partitions per HDD, each independently containing your choice of folders & files somewhat like a 4-drawer filing cabinet. Whether you chose to put a bootable fileset having a different OS on each partition, or merely use one or more partitions for plain ordinary storage was supposed to be your choice. On powerup I kinda liked having the choice of DOS, WindowsNT, Windows 3.1, and Linux from the "standard" Microsoft bootmenu myself :) Instead most users never dreamed they would ever see any "drive" other than "C:". DOS & Windows PCs intentionally sold with the entire HDD already "divided" into a single FAT partition taking up the whole disk, with the OS pre-"installed" to it, and plenty of space left over for your valuable data storage to be intimately intermixed with the OS files & folders. IOW if you wanted to take advantage of multibooting, or merely confine your data more safely away from your OS by using different volumes, you would most likely be expected to buy an additional HDD to use as a mainstream "D: drive". Otherwise you were on your own with no documentation nor tools from Microsoft which would be as helpful as they could be for multi-partitioning a single HDD, especially when it comes to booting a secondary OS. Remember, until CDROMs and later USB sticks started showing up all over the place, any existence of a "D:" volume or any other letter of the alphabet beyond C: was considered voodoo of some kind and discouraged as being not for the faint of heart.
But the wrong false move on your only volume (C:, where the OS is more likely to become self-corrupt than your own more-valuable data it is sharing space with), and you were expected to lose your OS and data at the same time. Even now to some extent this concept lingers :\
So if some geek came along with two HDDs in one PC, a "C:" and a "D:" "drive", it was looked at by regular office workers as about the most confusing undocumented thing at the time. Even worse if there was only one HDD and it was partitioned into more than one partition so you had like a D: E: & F: to go along with your C:. However for most users CDROMs usually were the first time they had ever seen a D:, then if they were advanced, "risked it all" and added 3 more primary partitions to their HDD they could end up with C:, E:, F: & G: on the same HDD, some users' head could explode with that ;) Plus whichever volume you boot to at the time is designated as C:, and the remaining alphabetic letters assigned in relation to that according to a fairly interesting hierarchy.
Of course multi-partitioning was usually only done using third-party tools, Microsoft was not your friend here. Actually not usual at all, more like "seldom if ever" in most offices.
Anyway, namely a volume is a formatted partition, so partition and volume are not even supposed to have the same meaning. Neither one is best referred to as a "disk" nor a "drive" when you start really focusing on filesystems, leaving you responsible for making the distinction more correctly than most people have been taught to do.
Now under conventional (legacy) BIOS, sector 0 of the HDD contains the Master Boot Record (MBR) within the first 440 bytes followed by the NT serial number if present, then the next bytes consist of the partition table followed by the final "signature bytes". All within 512 bytes. The MBR is the code that interprets the partition table, and then jumps to the particular partition that is marked "Active" at the time.
Any one of these partitions is where your designated "sector 0" comes in, not exactly "start of disk" proper at all. Note that the first sector of each partition is always a bit "downstream" from the actual sector 0 of the HDD (as defined by the particular partition table), even when you only have a single partition encompassing the whole HDD. Simply enough, your "start of disk" is actually a start of a single volume contained within the disk. At the first sector of the volume like this is where you correctly follow the Boot Sector code proper which interprets the particular filesystem on the volume. And from which point the booted partition acts about as closely to a floppy as it's going to.
A volume's boot sector is more descriptively known as a Volume Boot Record (VBR), or even a Partition Boot Record (PBR) sometimes. Also it's not the VBR, it's the MBR where the code must be restricted to the first 440 bytes of HDD sector 0. Bytes 441 thru 444 are reserved or occupied by the NT serial number, 445 & 446 were always zeros and unused (although these days some non-zero data finds its way there somehow sometimes, I don't know from where yet). 447 & 448 are the first two bytes of the partition table itself which extends to 510, so off-limits to MBR code as well. 511 & 512 are the final signature bytes.
For a VBR sector, much more variation can be seen between different filesystems, the code can be more or less than 440 bytes plus usually followed by error messages that can extend almost to the end of the sector since the later bytes do not contain a partition table so are available for other use, because a VBR is not an MBR. Even though there is some cross-functionality tracing back to floppies, the MBR is supposed to be OS-agnostic and simply jump to the active partition according to the partition table. Any VBR code found then interprets the specific filesystem on that particular volume and runs the expected boot files contained and found in that filesystem.
So that's more or less a numerical correction there :)
I say keep doing what you're doing because it is coming out good so far :) Excellent really.
Prepare the terminology to be consistent enough now to better address the partitioning process in detail later.
If you haven't looked over the Starman this is old info (newer than FAT16 though) but contemporary with FAT32 and early NTFS, extensive but not as complete as it could be and not always perfect, but very useful. For instance hidden sectors are now usually 2048, instead of 63 which was the most common first-partition starting point decades ago, but there were also sometimes 32 or 64 hidden among others like 128. BIOS is supposed to read the sectors well enough to handle this but lots of motherboards would only boot to a volume having 63:
https://thestarman.narod.ru/asm/mbr/index.html
Keep posting as you make progress, when you get to FAT32 I'll have more to add since that's mainly what UEFI is expected to boot with.
I confirm these kind of things using extensive experimentation.
Oh yeah, keep in mind that FAT32 boot sectors can come in a variety of forms appropriate for the different generations of boot files which are intended to be found and executed.
Originally they were a FAT DOS bootsector, written to the volume by formatting or "SYSing" the volume while booted to DOS or W9x, a DOS bootsector seeks the IO.SYS file. Then FAT NT3 and NT4 were not as well-developed before reaching FAT NT5 for W2K & XP, seeking NTLDR as a boot file not much differently than an NTFS NT5 bootsector does. Next we find ourselves at NT6 FAT bootsectors since Vista, now seeking a BOOTMGR file in the filesystem as well as the expected well-crafted BOOT folder to launch the OS, as far as BIOS goes. OTOH under UEFI mode instead of BIOS, the firmware is OK without any BOOT folder to start Windows[0] and seeks the EFI folder regardless of what OS the FAT boot volume was formatted with.
[0] Some Linux distros in UEFI mode can depend on having a BOOT folder containing their specific extra files though, in addition to the nominal EFI folder in the FAT ESP bootvolume.
The bootloader process is also fascinating to me, and is exactly where all these boot sector variations become extra pertinent.
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!
Is the cluster map on the title screen driven by the same FAT model the later demos use, or is it a separate animation?