DOS boot sector, BPB, and the media descriptor byte

All DOS programmers know that DOS storage media (floppies, fixed disks, even RAM drives) have a BPB (BIOS Parameter Block) which describes the basic layout of the storage medium. All DOS programmers also know that every DOS disk contains a boot sector with a 0x55, 0xAA signature in the last two bytes and the BPB stored at offset 3 and starting with an 8-byte OEM identifier. Sadly, it’s not true. There are DOS disks with no signature in the boot sector and no BPB stored in the boot sector. Even when there is a BPB, it’s not necessarily correct.

The basic problem with BPBs is that they were only introduced in DOS 2.0; previous versions of DOS did not use the concept at all. It’s easy to see why: DOS 1.0 only supported a single storage medium format (a single-sided 5¼” floppy with 40 tracks and 8 sector per track, 160KB total capacity). There was no real need to distinguish between various formats, although there was a way to do so through the media descriptor byte in the FAT.

DOS 1.1 was shipped with updated PCs that had double-sided floppy drives. An additional format utilizing both sides was supported, with 320KB total capacity. The media descriptor byte was used to distinguish between the single-sided and double-sided formats.

When DOS 2.0 was shipped with the PC/XT, additional floppy formats with 9 sectors per track (180KB and 360KB) were introduced. The media descriptor byte could still be used to distinguish between 1/2 sides and 8/9 sectors per track. But the XT also supported fixed disks, and the media descriptor byte was woefully inadequate for describing fixed disk formats.

DOS 2.0 therefore created the concept of a BIOS Parameter Block—or BPB—which contained basic disk geometry information. It was stored in the disk’s boot sector. There was just one problem—none of the existing DOS 1.x disks contained any BPB.

Microsoft’s solution was to build the BPB on the fly. For fixed disks there was no problem; DOS 1.x could not work with them, hence there was no backwards compatibility to worry about. For floppies, only two BPB-less formats existed and were easy to distinguish from each other by the media descriptor byte. The upshot was that for fixed disks, the BPB had to be always correct, but for floppies, it was not always used.

When formatting disks, DOS 2.x and later always wrote the BPB, but the BPB was not always used when accessing floppies. This inconsistency led to an embarrassing bug: DOS versions starting with 2.0 and ending with 3.1 wrote incorrect BPBs in certain cases. I was recently bitten by this bug when working with a single-sided floppy formatted by DOS 2.0 (in a virtual machine, of course). The BPB claimed the disk used two sectors per cluster, but in reality it was just one. Any software which trusts the BPB is not going to work.

When adding 3½” floppy support, Microsoft tried to use the BPBs whenever available (instead of the media descriptor byte which was no longer adequate even for floppies), the bug became obvious. Microsoft’s solution was a classic hack: use the OEM ID signature (which in theory was purely informational and might contain any data) and check for DOS versions older than 3.2. For old versions, fix up the BPB with presumably correct data. For newer versions, use the BPB as it exists on the disk. The solution worked since the PC-DOS and MS-DOS wrote OEM ID signatures containing the DOS version. Disks formatted by third party software hopefully had correct BPBs.

Years later, Microsoft forgot about how all that stuff had worked. Windows 9x reused the OEM ID signature as a hash and destroyed the original OEM identifier.  In theory, a user might have a disk with an unusual format but a valid BPB, recognized as such by Windows. Then Windows would destroy the OEM ID and would no longer be able to access the data on the disk. Very clever!

To my knowledge, the relationship between the media descriptor bytes and BPBs was never documented by Microsoft, and the exact algorithms for building BPBs were not documented either. That led to third party software often getting things subtly wrong. Eventually Microsoft became the victim of its own design blunders, as if attempting to prove that quick hacks can in the long run cause more problems than they solve.

This entry was posted in DOS. Bookmark the permalink.

3 Responses to DOS boot sector, BPB, and the media descriptor byte

  1. Reseul says:

    Ha, this triggers memories..

    I can add a twist to this.
    Long time ago a friend of mine an I built a PC-like computer, XT compatible, and with some mumbo-jumbo (that is a long story on itself) we got it to work with high density disks (1.2M) as well.
    To support that we implemented the AT like BIOS calls (the set format, etc.) in a TSR (we could burn new ROMs, but I guess we took some shortcuts).

    Anyway, one day, some disks started to misbehave, a DIR would show happy faces, etc. Same disks worked just fine on similar computers (without the AT tainting).
    It turned out a boot virus (I think it was named Michelangelo) infected the disks and corrupted the BPBs as well since it didn’t care.
    Our conclusion was that on regular XT computers MS-DOS was falling back to the descriptor from the first FAT sector, whereas on our computer it chose to obey the BPB.

  2. Josh Rodd says:

    An ongoing mystery is why DOS 1.0’s floppies were so uncomfortable small. A 1,024-byte sector format (with 5 sectors per track) would have supplied a 200kB disk instead of 160kB (25% bigger), with lots of space for gaps between sectors (unlike the more exotic 10 sector per track format that technically works on standard PC floppy controllers).

    A 1,024-byte boot sector would have been an improvement, and the location at 7C0:0000 of the boot sector meant it was clearly intended for the extra room anyway. Somehow, 512-byte sectors happened anyway.

    The double-sided format ended up having 1,024-byte clusters anyway, so DOS was effectively using this from DOS 1.1 on.

    We finally got sector sizes bigger than 512 bytes around ~ 2010, when 4K disks become popular.

  3. Michal Necasek says:

    Good question. It should be pointed out that at the time, there was no standard sector size whatsoever. 128 bytes was the old standard, but 1,024-byte sectors were also common on 8″ disks. Systems with 256-byte sectors also existed, 512 bytes were not so common in 1980 from what I can tell. There was a similar situation with hard disks, sector sizes varied.

    I can only imagine that maximizing storage capacity was not the primary goal (reliability was). Even if it were, the allocation granularity definitely was a factor; I don’t know what calculations, if any, IBM did, but they may well have decided that a 160K disk with 512-byte sectors is better than a 200K disk with 1K sectors for users who need to store lots of small files.

    Yes, 320K disks ended up with 1K clusters anyway, but that was a software decision, we know that HD floppies went back to 512-byte clusters.

    Bad sectors were surely also a consideration — losing 512 bytes is not as bad as losing 1,024.

    Also, for a machine that’s typically expected to have something like 64K RAM, I’d say a 160K disk is not that small.

    From a modern perspective, 1K sectors surely would have been better… but I’m not at all convinced that that’s what things looked like in 1980-1981.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.