How do I createa bootable floppy disk?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Vik2015
Posts: 15
Joined: Fri Sep 20, 2013 2:34 am

How do I createa bootable floppy disk?

Post 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.
My own MS-DOS like OS :)
https://github.com/Vik2015/weevil/
http://bestsoft.azurewebsites.net/ wrote: With Bestsoft Space you can write operating system eaven if it your first software.
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: How do I createa bootable floppy disk?

Post 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.
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I createa bootable floppy disk?

Post by Octocontrabass »

Floppy disk booting is simple enough that it can be described completely in only a few sentences.
Vik2015
Posts: 15
Joined: Fri Sep 20, 2013 2:34 am

Re: How do I createa bootable floppy disk?

Post 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).
My own MS-DOS like OS :)
https://github.com/Vik2015/weevil/
http://bestsoft.azurewebsites.net/ wrote: With Bestsoft Space you can write operating system eaven if it your first software.
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I createa bootable floppy disk?

Post by Octocontrabass »

In that case, you want to look for more information about FAT12.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: How do I createa bootable floppy disk?

Post 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?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I createa bootable floppy disk?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
TightCoderEx
Member
Member
Posts: 90
Joined: Sun Jan 13, 2013 6:24 pm
Location: Grande Prairie AB

Re: How do I createa bootable floppy disk?

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I createa bootable floppy disk?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How do I createa bootable floppy disk?

Post by Combuster »

Why would you write something that already exists? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I createa bootable floppy disk?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How do I createa bootable floppy disk?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: How do I createa bootable floppy disk?

Post 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...
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I createa bootable floppy disk?

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I createa bootable floppy disk?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply