Booting from USB Flash Drive

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.
Post Reply
circle
Posts: 3
Joined: Sun Sep 25, 2005 11:00 pm

Booting from USB Flash Drive

Post by circle »

Booting from USB Flash Drive

I've written a boot sector, which displays a message to demonstrate it has been loaded and executed, and it then loads a file (well a sequence of known sectors really) into memory and executes it. A good start for an OS so far. I originally wrote it for a floppy but now I want to run it on a USB flash drive, and I've run into difficulties.

The floppy was formatted as FAT12. (which dosen't matter to the boot sector or the image file, as they don't understand filesystems as of now). The flash drive, a 64 megabyte capacity one, is formatted with FAT16. Again my binaries still don't know how to access the filesystem, so it is only of importance because my image file has to reside in the first file sectors on the device in both cases, so I modified my original boot code to point to the correct sectors. (I use FAT formatted disks for convinience, to access the image file from Windows).

Anyway as I said, the floppy version boots and executes the image (which just displays a screenfull of text, and waits for a keystroke which starts a reboot), and it works perfectly.

However, the flash drive gets as far as the boot sector loading (and displaying its message) before it hangs. I double checked the settings, and I was sure it should work. Therefore I created an image file from the flash drive, and put it into Bochs, and tried booting from it. It worked perfectly in Bochs!

I'm not sure, but I think the problem lies in the BIOS Int13 function, which I use to read in the sectors. It expects Cylinders, Heads and Sectors as parameters, and obviously they are translated in the BIOS in some way, as USB devices use block addressing. As windows reports the device as having 7 cylinders, 255 heads and 63 sectors, I did a logical block translation to work out the CHS combination for block 591 - the first file sector on a FAT16 disk. This translation I came up with is obvioulsy correct, as Bochs correctly finds and loads my image file. However, I feel that perhaps the BIOS on the few machines I tried either do not perform the same translation, or possibly does not even do any translation as the "disk" is smaller than 528MB?

Does anyone else have any further information that might help me with this?

Thanks
circle
Posts: 3
Joined: Sun Sep 25, 2005 11:00 pm

Re: Booting from USB Flash Drive

Post by circle »

Okay, I've solved this problem; got my code to boot off my flash drive at last.

Just in case anyone else needs this for future reference, the problem was that I was using the original BIOS INT13 Function 02h to read in sectors. This works fine on old floppy drives, and on old hard drives as well. In all the documentation I had (quite a few years old now), this was the only real way to use BIOS to read sectors in your boot code, unless you wanted to program the drive hardware directly (not a good idea for boot code that wouldn't know what hardware it might be booting on).

Anyway, it turns out that there were BIOS extensions put in place as disk drives got bigger, and when other storage hardware, that was block addressable only became available. It appears that all modern PCs have these extensions built in, and although there is documentation, it's a bit more difficult to uncover using Google than I might have thought.

The new way of accessing disk sectors is using the INT13 function 42h (Extended Read). This uses Logical Block Addressing only. So to read in the sector I need, I use the actual LBA block address; 591. The format of the INT is quite a bit different (you have to create a data structure containing the interrupt parameters, called a packet), but you can find out more by looking up the Ralf Brown INT lists and The Phoenix Enhanced Disk Drive Specification.

It works; I have the code booting from the flash drive on a Dell desktop, a Toshiba notebook, and in Bochs (using the updated image of the flash drive). I'll be trying it out on a CD and DVD at some stage, but I'm pretty happy with this.

Hope this helps someone else.
Last edited by circle on Tue Sep 27, 2005 11:00 pm, edited 1 time in total.
Post Reply