How to make a floppy image and overwrite the MBR

3

1

I am trying to make a floppy image with a working file system so that I can test a 2 stage boot loader. When I attempt to mount the floppy and then cp the second binary over to it, mount gets all unhappy. Here are the steps I am trying to use:

dd if=/dev/zero of=floppy.img bs=512 count=2948
mkfs.vfat floppy.img
dd if=boot1.bin of=floppy.img bs=512 count=1
su -c 'mount -t msdos -o loop floppy.img /mnt'

dmesg | tail

[591461.669870] FAT: invalid media value (0x09)
[591461.669876] VFS: Can't find a valid FAT filesystem on dev loop0.

Chris

Posted 2011-08-15T01:36:24.660

Reputation: 238

Answers

3

You're learning some of the problems with using a POSIX toolset for this sort of stuff. Installing a bootstrap program into the VBR of a volume is not a matter of just blamming a sector full of data into sector #0 from an image. You must preserve the existing BIOS Parameter Block, coping with the several BPB variants.

The tools that come with operating systems like MS/PC/DR-DOS, OS/2, and Windows NT know all about things like BPBs. Their various sys, sysinstx, bootsect and other tools know how things are supposed to be handled. But if you use POSIX tools like dd you have to manually tell them which bytes to overwrite in the VBR and which to preserve as-is.

ms-sys knows the how to preserve BPBs as well, but it doesn't have the option of applying a custom bootstrap program. Go and request it as a new feature.

JdeBP

Posted 2011-08-15T01:36:24.660

Reputation: 23 855

"request it as a new feature" - The Solaris fdformat utility had an option to install a custom floppy boot program. – sawdust – 2011-08-15T06:54:30.927

The Linux fdformat utility has not, and Linux is what the questioner is asking about. The Solaris fdformat utility was superseded by rmformat, which had no such option, since the functionality for high-level formatting was transferred to mkfs_pcfs. mkfs_pcfs has the reserve= option, too. – JdeBP – 2011-08-15T10:41:11.547

2

I've actually written a two-stage floppy boot loader a long time ago. Despite what various specifications might say (from Microsoft or other "experts"), the first sector of the diskette must conform to a standard MS-DOS floppy with a BIOS Parameter Block, BPB. The error message you're seeing seems to indicate that your "boot1.bin" does not meet this requirement. The only thing you can really change in the floppy boot sector about 400 or so bytes of code. You cannot customize the BPB values from what MS-DOS or Windows uses for floppies.

Even though there is a value to indicate the number of sectors that the boot code occupies, every BIOS and other software I encountered only expects or tolerates one reserved sector for boot. Two copies of the FAT are required, and then the first sector of the root directory. The method I used for storing the secondary boot loader was to hard allocate some sectors following the root directory, and then mark those sectors as "bad" in the FAT. This accomplished the following goals:

  1. the additional sectors used by the secondary loader were at a fixed location on the floppy and known to the first-stage boot, which had to read/load them.

  2. hid the secondary loader from users.

  3. made the secondary loader undeleteable.

  4. the floppy was usable in DOS and mountable in Unix.

BTW floppies have a boot sector. It is not called a MBR, as there is no partition table.

sawdust

Posted 2011-08-15T01:36:24.660

Reputation: 14 697

You'll have a hard time pointing to where Microsoft, or indeed an expert, has said that a BPB is not used on a FAT format floppy disc. None will have said any such thing. And you've missed out on a decade and a half of Microsoft operating systems from DOS+Windows 95 OSR2 onwards reserving 32 sectors for bootstrap code (and FSInfo blocks). The notion that firmwares don't like anything other than 1 reserved sector is nonsense. Firmwares don't care one whit. As for "other softwares": What do you think that the -R option to the questioner's mkfs.vfat is there for? – JdeBP – 2011-08-15T07:07:18.923

I don't expect anybody to document the shortcuts other software have taken which ignores values in the floppy BPB that I've encountered. After designing and implementing a multi-stage floppy loader (that went into release & production) that would work on any PC, I'm offering my real-world experience, not analysis from reading a spec. – sawdust – 2011-08-15T07:42:03.953

My comments are based on actually customizing the BPB, e.g. reserving 4 sectors instead of the standard 1 sector, and seeing various MS-DOS utilities totally ignore my nonstandard specification (i.e. root directory was still read from a hardcoded location). IIRC the "MS-DOS Bible" mentioned that the 2nd FAT was not used on a floppy; I found that to be false. I've never seen a floppy with 32 sectors for bootstrap; have you? The BPB also encompasses "logical disk drives" in HDD partitions, as well as floppies. The usage of the large bootstrap is only on HDD, not floppies. – sawdust – 2011-08-15T07:45:37.927

I hate to break this to you kiddo, but your "real-world experience" is outweighed by the real world experiences of the millions of people who have happily bootstrapped DOS+Windows 95 OSR2 and later operating systems from FAT volumes with 32 reserved sectors. It's outweighed by mine, too. I actually have a FAT formatted floppy boot disc with 32 reserved sectors in one of my machines right now. The ideas that this is specific to hard discs, or that firmwares care about it, are simply erroneous, and demonstrably so by people who've used DOS+Windows 95 OSR2 or later. – JdeBP – 2011-08-15T10:33:57.627

And stop taking what the "MS-DOS Bible" says as definitive on the subject. If it's the "MS-DOS Bible" that I have, it pre-dates Microsoft's widespread introduction of FAT volumes with 32 reserved sectors by some five years. The only tools that I've encountered that cannot handle such FAT partitions were made years before DOS+Windows 95 OSR2. Modern tools can cope, as indeed can old tools written by people who took heed of the existence of the reserved sectors field of the BPB right from the get-go and never hit the problem when the field value started to be something other than 1. – JdeBP – 2011-08-15T10:39:01.130

1

I want to write a self-contained answer to this. First off, in Linux begin with your two instructions:

dd if=/dev/zero of=floppy.img bs=512 count=2948
mkfs.vfat floppy.img

This creates a DOS4/OS2 compatible FAT12 disk with an extended BIOS parameter block as described at Wikipedia "Design of the FAT file system"

The first 3 bytes "eb 3c 90" are machine language for: JMP 3E \ NOP, meaning skip over the BIOS parameter block and start running the code at byte offset 62 (decimal) [3E = 3C + 2 (base 16) = 62 (base 10)].

From offset 62, the next 378 bytes are yours to write your bootstrap. The default program prints, a message, waits for a key, and then reboots.

Create a file called myBoot.bin with a 378 byte x86 binary program.

It's only 378 bytes... you CAN do this, but if you absolutely need a starting place, use the command:

 dd bs=1 count=378 of=myBoot.bin if=floppy.img skip=62 conv=notrunc

Finally, overwrite the stock MBR of the floppy with your custom myBoot.bin program:

dd bs=1 count=378 if=myBoot.bin of=floppy.img seek=62 conv=notrunc

You have now successfully created a floppy image and overwritten the default boot-program.

ABridgeTooFar

Posted 2011-08-15T01:36:24.660

Reputation: 11

Some disk formats reserve 6 bytes starting at offset 220 (DC hex) for an optional timestamp or OEM loader signature. You can investigate this further at Wikipedia "Master boot record"

– ABridgeTooFar – 2019-02-06T15:44:52.163

I have to make a shout-out to Nick Blundell, author of "Writing a Simple Operating System -- from Scratch" for an excellent discussion that can take the reader from this topic to a "fully" functional O/S. I'd also suggest readers familiarize themselves with UEFI firmware libraries which are aimed at modernizing and securing the boot process.

– ABridgeTooFar – 2019-02-08T13:10:59.773