Booting from USB?

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

Re: Booting from USB?

Post by Octocontrabass »

ThisMayWork wrote:Going with the second option, that means I just have to format my flash drive in FAT12 and then directly copy my bootloader on it using dd, right? LBA 0 is just the first 'block' of the drive. (I think)
Most utilities will obey the partition table and place the filesystem inside the first partition, which isn't what you want. You'll need to find some way to work around that.
ThisMayWork wrote:(Can't change this, my bootloader will only read my kernel off FAT12)
You will be writing a new bootloader that supports better filesystems once you've got the flash drive booting, right?
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Booting from USB?

Post by SpyderTL »

ThisMayWork wrote:Going with the second option, that means I just have to format my flash drive in FAT12 (Can't change this, my bootloader will only read my kernel off FAT12) and then directly copy my bootloader on it using dd, right? LBA 0 is just the first 'block' of the drive. (I think)
I'm assuming you're using the BIOS INT 0x13 functions to read the filesystem and your kernel into memory...

I'm fairly certain that the floppy BIOS functions won't work on a USB device, and I'm pretty certain that the Extended functions won't work for reading a floppy disk, so you are probably going to have to rewrite your boot loader anyway...

You may want to look into GRUB if you don't want to write separate boot loaders for different media.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Octocontrabass
Member
Member
Posts: 5590
Joined: Mon Mar 25, 2013 7:01 pm

Re: Booting from USB?

Post by Octocontrabass »

SpyderTL wrote:I'm fairly certain that the floppy BIOS functions won't work on a USB device,
The basic read/write functions are always available. The extended functions are only available if the BIOS has chosen to use hard disk emulation (which requires a partition table in LBA 0 and at least one valid partition with the boot flag set).
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Booting from USB?

Post by Brendan »

Hi,
ThisMayWork wrote:I need to do my research, but before that, one more quick question. I believe an MBR looks something like this:
-Bootloader
-Partition 1
-Partition 2
.
.
.
-0x55
-0xAA
Definitely NOT.

An MBR looks like this:
-First sector of boot manager/chain loader code
-Partition 1
-Partition 2
-Partition 3
-Partition 4
-0x55
-0xAA

At an absolute minimum, the boot manager/chain loader code must:
  • Relocate itself
  • Find the active partition
  • Load the first sector of that active partition at 0x7C00
  • Set DL to the drive number, and (both) DS:SI and DS:BP to point to the active partition table entry
  • Kump to 0x7C00
It may (optionally):
  • Display a pretty "boot menu" thing (allowing the user to select which OS to boot), possibly with time-out, etc.
  • Do strange shenanigans with partition tables (e.g. to work around the "max. 4 primary partitions" limitation, and for security - e.g. hiding DOS partitions from Linux, etc)
  • Do sanity checks, like testing to make sure the first sector of the selected/active partition contains the 0x55,0xAA marker before jumping to it
  • Include additional utilities; like tools to create/delete partitions, tools to backup/restore/clone partitions, tools to auto-detect installed OSs, tools to test RAM and/or other hardware, etc.
  • Extended boot capabilities; like allowing you to boot completely different disk drives (e.g. able to boot an OS installed on the second hard disk, etc)
The important thing is none of this has anything to do with any specific OS or any specific OS's boot loader.

An OS's boot sector (which is in the first sector of the OS's main partition, and NOT in the first sector of the a partitioned disk) looks like this:
-First sector of bootloader's code
-Partition table (optional, for "extended partitions")
-0x55
-0xAA

For un-partitioned devices (floppy), the entire device is treated like a partition. There is no MBR in this case.

For USB, when it was first introduced there was no standard for it (unlike CD, where "El Torito" was invented for it). To work around that; a lot of BIOSs try to auto-detect if the device should be treated as a floppy or a hard disk and then try to emulate either a floppy disk ("device 0x00") or a hard disk ("device 0x80"). If the BIOS sees a sane partition table then it assumes it should emulate a hard disk; and if the BIOS doesn't see a sane partition table but does see a sane [url=http://en.wikipedia.org/wiki/BIOS_parameter_block]BPB[url] then it assumes the device should emulate a floppy disk. If the BIOS doesn't see either, then you could end up with anything.

Of course some BIOSs have a "USB emulates floppy or hard disk" setting in their BIOS configuration. Other's don't support "USB emulates floppy" at all. Also, for floppy emulation, most BIOSs don't support "int 0x13 extensions" so you'd be limited to "tiny".

If you add all this up; there's only really one sane alternative that works in almost all the cases: make sure there is a sane partition table and make sure there isn't anything that looks like a BPB (to guarantee that any auto-detection results in the BIOS deciding to emulate a hard disk).

This means that you'd need an MBR and a boot sector (but you're going to need that for booting from hard disk anyway, so...).


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