Page 1 of 2

How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 11:24 am
by Vik2015
A totally dumb question I can't find answer for anywhere :/
I want to create a bootable floppy disk (like MS-DOS had) but cannot seem to find any resources. Wiki only has page for making a bootable ISO image or I am missing something? Does anybody know how do I do this? Thanks.

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 11:37 am
by Techel
You simply put your bootloader in sector 1 of your floppy disk and 0x55 and 0xAA in the last two bytes of this sector.

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 11:41 am
by Octocontrabass
Floppy disk booting is simple enough that it can be described completely in only a few sentences.

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 11:46 am
by Vik2015
Octocontrabass wrote:Floppy disk booting is simple enough that it can be described completely in only a few sentences.
Oh man, sorry, forgot to say. I am trying to create a bootable floppy disk with FAT12 filesystem on it, not just plain binary (that's simple DD command).

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 11:53 am
by Octocontrabass
In that case, you want to look for more information about FAT12.

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 12:27 pm
by Roman
Vik2015 wrote:
Octocontrabass wrote:Floppy disk booting is simple enough that it can be described completely in only a few sentences.
Oh man, sorry, forgot to say. I am trying to create a bootable floppy disk with FAT12 filesystem on it, not just plain binary (that's simple DD command).
Are you asking about creating your own boot code, which would boot from a FAT12 partition? Or are you asking about formatting an image with FAT and putting GRUB (or any other compatible boot loader) there?

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 1:59 pm
by Brendan
Hi,
Vik2015 wrote:
Octocontrabass wrote:Floppy disk booting is simple enough that it can be described completely in only a few sentences.
Oh man, sorry, forgot to say. I am trying to create a bootable floppy disk with FAT12 filesystem on it, not just plain binary (that's simple DD command).
These options aren't mutually exclusive.

The FAT file system uses a BPB to describe its layout. The BPB has a "reserved sectors" field which specifies the number of sectors that are reserved at the beginning of the file system (that won't be used by the file system itself). Normally (e.g. for data disks) the number of reserved sectors is 1 (and there's a boot sector that displays "This disk isn't bootable" when you boot it).

Nothing prevents you from setting the number of reserved sectors to (e.g.) 1234 and storing the boot sector plus whatever else you like (second stage, kernel, etc) in that area; and in that case you can use the DD command to copy the boot sector plus whatever else you like onto the disk while also having a perfectly valid FAT file system on the same disk.

The only real problems with this are:
  • The BPB in the boot sector must match the disk and its layout (which is also a problem if/when you're supporting multiple different floppy disk formats; and is relatively trivial to work around by writing a simple utility)
  • It's probably hard to find a utility that's capable of formatting a FAT file system in a suitable way (e.g. you can't just format the file system and then modify the reserved sectors field after, because it effects the location of the cluster allocation table and root directory). Of course it's relatively easy to create your own utility (to either format a disk or generate a blank disk image) to work around that
Also; if/when you feel like replacing FAT with something else (e.g. your own file system); or if/when you realise your boot code and kernel fills the entire floppy and there's no space left to waste on the overhead of a file system; you can continue using the exact same boot code without FAT.


Cheers,

Brendan

Re: How do I createa bootable floppy disk?

Posted: Tue Jun 16, 2015 4:00 pm
by TightCoderEx
Brendan wrote:
  • It's probably hard to find a utility that's capable of formatting a FAT file system in a suitable way (e.g. you can't just format the file system and then modify the reserved sectors field after, because it effects the location of the cluster allocation table and root directory). Of course it's relatively easy to create your own utility (to either format a disk or generate a blank disk image) to work around that
One of the ways I got around that is to format a data disk, change BPB reserved sectors and then used windows Properties\Tools\"Check now" for drive A: and let that utility fix the problem. Probably not the most elegant way, but it did work.

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 12:37 am
by Brendan
Hi,
TightCoderEx wrote:
Brendan wrote:
  • It's probably hard to find a utility that's capable of formatting a FAT file system in a suitable way (e.g. you can't just format the file system and then modify the reserved sectors field after, because it effects the location of the cluster allocation table and root directory). Of course it's relatively easy to create your own utility (to either format a disk or generate a blank disk image) to work around that
One of the ways I got around that is to format a data disk, change BPB reserved sectors and then used windows Properties\Tools\"Check now" for drive A: and let that utility fix the problem. Probably not the most elegant way, but it did work.
Hmm - that's clever (I didn't realise something like that would work).

I mostly just write a "create bootable floppy image" utility that takes the media type (e.g. 720 KiB, 1440 KiB, 1200 KiB, 1680 KiB floppy, etc) and the file names for parts of my OS (boot loader file name, boot image/initrd file name, etc) as command line arguments; where the utility generates a BPB to suit the media type, installs my OS into the image and sets a few special fields in the boot sector ("starting LBA for boot image/initrd", etc).

Of course I don't bother with FAT (I expect the minimal OS will eventually fill the floppy leaving no space for cluster allocation table, directory metadata, etc); but FAT isn't too complicated and it wouldn't be hard to write a "create bootable floppy image" utility that also creates a FAT file system.

Note: The main reason I like the "special utility" approach is that you can slap it into your OS's build system so that it generate 5 different floppy disk images (one for each format) whenever you build the OS (in addition to building files for PXE/network boot, CD images, whatever).


Cheers,

Brendan

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 1:48 am
by Combuster
Why would you write something that already exists? :wink:

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 3:52 am
by Brendan
Hi,
Combuster wrote:Why would you write something that already exists? :wink:
Because mtools isn't capable of doing this (their "mformat" has no option to set/change the "reserved sectors" field).

Of course even if mtools did support it properly, it still won't copy your file/s into the reserved area or set any special fields (e.g. like my "LBA for first sector of boot image/initrd" field); and won't do things like (e.g.) auto-determine the number of reserved sectors to use from the size/s of the files you want to put in the reserved area. Because of these things it'd end up being an ugly pile of hassles (e.g. rather than just an "mformat" that doesn't work; you'd need an "mformat" that doesn't work plus multiple "dd" commands that carefully avoid touching the BPB; and this pile of hassles will be fragile and break and need "manual tweaking" when the sizes of files change).


Cheers,

Brendan

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 4:16 am
by Combuster
FAT has been historically tried and tested to make half a sector be enough for loading further sectors by filename. That is the normal approach, and that is what mtools supports perfectly fine. If you deviate from that, that's a choice of creating your own challenge for the sake of it, rather than one of practicality. In fact, you have enough accompanying bootsectors of various licences up for the taking where even finding and loading a file is not an issue anymore.

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 10:44 am
by Antti
Combuster wrote:FAT has been historically tried and tested to make half a sector be enough for loading further sectors by filename.
Just for interest, have you ever seen a FAT12 floppy with "reserved sectors" not equal one? I mean historical floppies that are not made by a "hobby OS" community. All the commercial operating systems seem to have used only one reserved sector. I have no idea why because using those reserved sectors would make things far easier. Although I use one sector also...

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 2:23 pm
by Octocontrabass
Antti wrote:I have no idea why because using those reserved sectors would make things far easier.
It's much easier to debug and improve your second stage when you just have to replace a file on the disk instead of directly accessing sectors.

Re: How do I createa bootable floppy disk?

Posted: Wed Jun 17, 2015 2:51 pm
by Brendan
Hi,
Octocontrabass wrote:
Antti wrote:I have no idea why because using those reserved sectors would make things far easier.
It's much easier to debug and improve your second stage when you just have to replace a file on the disk instead of directly accessing sectors.
It's much easier to use the exact same boot code for FAT12, FAT16 and FAT32, and also for the file system you implement after you realise FAT is unusable trash, and also for "boot code only" partitions and floppies that have no file system at all.

Note that every time I press F12 (to rebuild my project), within 2 seconds I've got an "up to date" set of floppy disk images I can boot (in emulators, etc). Directly accessing sectors is not hard (and is easier and faster than diddling with files on a FAT file system, even if you do use mtools).


Cheers,

Brendan