Forrest4096
Forrest4096

Reputation: 169

How to go about making a two-stage FAT32 USB bootloader?

I'm making my own OS and I've read everything there is to read about homemade OSdev. However, I do not understand how to make a two-stage bootloader. More specifically, I can't understand how to get my first stage to locate the second stage on the FAT32 filesystem.

DIAGRAM: first stage -> second stage (enable pmode, gdt, etc) -> kernel

USB Specs:

Any suggestions???

Upvotes: 1

Views: 1704

Answers (2)

Martin Rosenau
Martin Rosenau

Reputation: 18493

I myself am working on a self-written OS two (over one year now). I have written a two-stage boot loader for FAT12/16 and FAT32 and there is a significant difference:

  • On FAT12/16 it is quite easy to write a boot loader
  • On FAT32 it is very difficult because a lot of things that are located in the first sector of the second stage in the FAT12/16 loader must already be in the boot sector for FAT32.

As far as I know even Windows either insists that the stage two file is one of the first files on the root directory (in this case less code in the boot sector is required) or some extra sectors with fixed sector numbers are used to "extend" the boot sector to a size > 512 bytes.

For an own operating system that should co-exist with other systems on the same disk both approaches won't work.

My boot loader works the following way:

It calculates the sector number of the first cluster. It contains a sub-routine that can read a sector by a given cluster number (they are 32-bit!) and a cluster-relative sector.

The first sector of the root directory (the cluster number is known) is read and searched for the file containing the stage two code. Then the second sector is read and so on.

When all sectors of a cluster are read then the FAT must be read to get the cluster number of the next cluster of the root directory. This is the part that is in the stage two code for FAT12/16 boot loaders but must be located in the boot sector itself for FAT32 boot loaders.

As soon as the directory entry is found you'll load the first sector of the first cluster of the stage two file. Loading the rest of the file can be done by code in the first sector of the stage two file!

When using INT 13h, function 42h (linear disk read supported by BIOSs newer than 1995) the 512 bytes of the boot sector are just enough if you use very short error messages only. Using INT 13h, function 2 (C/H/S disk read) I didn't manage to write a FAT32 boot sector because 512 bytes are just not enough.

Upvotes: 2

user149341
user149341

Reputation:

Are you sure you need to write your own bootloader?

There are already plenty of x86 bootloaders available (Lilo, Grub, and Extlinux among others). They are not specific to Linux; so long as your OS conforms to the multiboot specification, any of them will be able to boot your OS. Using any one of them will save you a lot of development time.

That all being said, the most common approaches to locating the second-stage bootloader are to either:

  1. Store it in the slack space after the MBR, before the first partition. (This assumes that such space exists, but it does on most disks.)

OR

  1. Embed a list of sector numbers into the first-stage bootloader, directing it to where it will find the second-stage bootloader.

Upvotes: 0

Related Questions