2

I have a second stage bootloader which I need to load on a USB flash drive. I have a bootloader which loads my operating system and runs on a floppy drive.

I can copy this boot code to my USB flash drive partition, but it crashes while loading the second stage bootloader. My current code uses CHS calculations to load the file from a FAT12 file system.

I have read about the extended read function, but I'm not sure if that would be the best route to take.

What steps are needed to load a second stage bootloader by name on a FAT32 file system in x86 Assembly? And what would be a good route to take?

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
  • 1
    It's not all that different than your FAT12 code, not that much has changed in the FAT32 file system. There are additional fields in the BPB to consider, the root directory isn't at a fixed location, and directory entry format has changed a bit. You'll want to use LBA addressing instead of CHS. – Ross Ridge Jun 12 '15 at 22:43
  • 2
    See [this](http://wiki.osdev.org/FAT#FAT_32_3) page for some FAT32 details. – I8086 Jun 12 '15 at 23:48
  • 2
    The CHS functions may only be able to access the first ~512 MiB of the device; and your OS's partition may be outside of that area. The best approach is to detect if the extended read function is supported and use it if you can, and fall back to CHS if you must. – Brendan Jun 12 '15 at 23:56
  • 3
    Note that with acceptable error handling/error messages, partition table parsing, "extended read" detection and support for both CHS and extended read; you will not have space in the first 512 bytes for code that's worth writing. Fortunately FAT and its BPB has a "reserved sectors" field that allows you to use as many sectors as you like at the beginning of the partition. Of course these reserved sectors can be used for both the boot loader (e.g. where first sector of boot loader loads remaining sectors of boot loader) and your second stage and whatever else you like. – Brendan Jun 12 '15 at 23:59
  • 3
    Finally, if you use/reserve the first "N sectors" of the partition for everything you need to get file systems running (e.g. boot code, kernel, drivers, etc) none of your boot code would need to care if the file system is FAT or not. This makes it far easier to reuse existing boot code later on when you want to use a different/better file system (e.g. when designing your OS's native file system you'd just reserve sectors at the start of the partition and use the exact same boot code); and it also makes it trivial to have a small special "boot partition" that has no file system at all. – Brendan Jun 13 '15 at 00:04
  • Thank you all for your insightful answers. Should I format my USB flash drive with MBR or as a single partition? I'm not sure what would be a good direction to take. **Another question**: I would like to load my kernel by filename if possible. How can I do this? – Michiel Pater Jun 13 '15 at 10:21
  • How about checking how GRUB developers solved these problems? – Laszlo Valko Jun 21 '15 at 15:25
  • @MichielPater These are now 2 additional questions in a comment-discussion. That should not be. Please consider creating new questions, perhaps even including source if applicable. – turbo Jun 21 '15 at 20:32

0 Answers0