Booting from USB Flash Drive
Posted: Sun Sep 25, 2005 11:00 pm
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
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