NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
▲Show HN: Understanding FAT16 – an interactive filesystem explainer (fat16.asciitweezers.com)
3dbrows 11 hours ago [-]
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.

compiler-guy 20 hours 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 18 hours 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 12 hours ago [-]
I kind of wonder what the Internet would look like now if IPv4 had evolved similarly to FAT16 --> FAT32.
lock1 16 hours 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 6 hours ago [-]
This made my day, thanks :)
11 hours ago [-]
mradex77 12 hours ago [-]
This is a cool way to learn how filesystems work.

Are you thinking about covering FAT32 or other filesystems next?

giant_fern 9 hours ago [-]
I might jump around to different topics, but NTFS still looms large in my mind so maybe some day :)
reactor3d 19 hours 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.
dfe 3 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.

nticompass 9 hours ago [-]
I like the little defrag game, it's fun.
abeauvois 2 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.
fuzzfactor 15 hours 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:

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.

giant_fern 9 hours 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.

visionwei 16 hours 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 9 hours 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 14 hours 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 9 hours 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 3 days ago [-]
[flagged]
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 22:55:31 GMT+0000 (UTC) with Wasmer Edge.