A lot of web sites describe how to create a bootable floppy disk using an existing image (via
dd). But what about the image makes it bootable?
I'm interested in making my own bootable floppy images for an IBM PC and want to understand the details.
The notion of a bootable-vs-non-bootable floppy is a little odd. It's worth noting that almost all floppies you're likely to have are actually bootable: it's just that they boot a program that isn't especially useful (it either displays a message saying to insert a system disk, or they execute
INT 18h - which runs BASIC if it's installed in ROM, or displays a boot error if it isn't).
In any case, the key to writing a useful bootable disk is to understand what the BIOS does when it boots from floppy (booting from a hard disk is subtly different, and adds a little extra complexity), which is this:
In addition to this, most useful boot programs will also include the data structures that are necessary for some file system or other, typically a FAT12 header which occupies the space between bytes 3 and 30.
The rest of the sector is available for the boot program. The DOS boot program simply loads a bunch of sectors from the start of the disk, performs some basic checks to make sure they were loaded correctly, and jumps into them to start the actual OS. Some other boot sectors are smarter; I wrote one back in the 90s that:
Writing a boot sector is fun. Good luck. :)
what about the image makes it bootable?
The program(s loader) starts at Track 0 Sector 0 of a device
I'm interested in making my own bootable floppy images for an IBM PC
Write the program to be executed (like this minimal one) to Sector 0 Track 0 and press reset.
[I] want to understand the details.
It's that simple. The BIOS looks for a floppy in drive 0, reads the first block and starts whatever is located there.
Wikibooks offers a real nice and easy to follow writeup about how to build a boot sector / bootable program, including an analysis how Linux does it.
Now, if you want to make floppies that also adhere to other standards - for example the FAT file system or the like, then you have to format it according to those specs. Like using a Volume Boot Record and squeezing your program in there. Plus adding a FAT structure and packing further boot stages into files according to that structure - or at least reserved by dummy entries.
But strictly nothing of that is needed when your goal is a bootable disk to do your own stuff. BIOS doesn't care for directories, clusters or assignment. Just Sector 0 on Track 0 and from there on Track/Sector numbers for reading, but that's already the domain of your program.
Addon, answering an aspect explicitly asked about in the question.
A "dd image" of a complete floppy disk, unless dd is used with special (skip/count) parameters, will always carry the parts that make a floppy disk bootable or not bootable along with it - dd if=/dev/fd0 of=temp ; change floppy to empty one; dd if=temp of=/dev/fd0 will create a bootable copy of a bootable original, unless special techniques (copy protection that works by (mis)using normally non-addressable parts of the media) were used in the original.
what about the image makes it bootable?
An operating system "kernel" sitting at track 0 sector 0.
EDIT: if "data", or a program that doesn't have the necessary code to finish booting the system, is at track 0 sector 0, then the BIOS' bootstrap code will throw an error saying that an operating system is not found.
EDIT 2: if it's a fresh or unformatted disk, the BIOS is still going to try and look where it thinks track 0 sector 0 is, not find anything and then throw an error.
Short answer: A magic number (0xAA55) being at the end of the first sector on the floppy.
Slightly longer answer: "Booting" means that the first 512 bytes of the floppy are copied to memory (usually at real mode segment 0x0000:0x7C00 or physical address 0x7C00). This is the first sector of the floppy, sector 0. It's also known as the bootsector. The processor then jumps here and starts execution. Some older BIOSes will check that the last 2 bytes of the segment are 0xAA55. This is a magic number.
DD is a tool that is used to write to specific sectors on the disk. It is one of the more preferable tools for accessing raw disks on *nix systems.
EDIT: This is for IBM-PC & compatibles. See this article.