Reading Sectors 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.
Post Reply
johnsa
Member
Member
Posts: 296
Joined: Mon Oct 15, 2007 3:04 pm

Reading Sectors from USB

Post by johnsa »

Hey,

Ok so i've ruled out all other possibilities now with the situation regarding my boot loader not loading the stage 2 loader when on USB apart from having a dodgy bios or stick or code issue :)
For some reason, reading a sector from the USB stick thats been booted successfully just loads 00's into mem. I've tried it on two other machines (all Dell's) with the same result.
I've tried the original int 13h funcs as well as the extended funcs.. same outcome, no carry or error reported.

Anyone have any thoughts?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Reading Sectors from USB

Post by Dex »

johnsa
Member
Member
Posts: 296
Joined: Mon Oct 15, 2007 3:04 pm

Re: Reading Sectors from USB

Post by johnsa »

Ok.. I've read these posts.. but now I'm more confused. They all deal with how to GET a USB stick to boot at all. Now this brings me to two points
1) My image boots from USB, no problems.. its about 100 lines down in code when it calls to 42h/int 13h or 02h/int 13h to load the actual sectors for stage 2 that it goes funny. The read works and BIOS doesn't report any error or carry but the data loaded into memory is blank (0's).
2) I don't follow WHY I need to have a FAT formatted disk, or a partition table at all. Sure if I want to follow standards and allow multi-boot (play nicely with other OS'es and have partitions) then it would be necessary but does the BIOS or Hardware in any way require this? As far it's concerned a sector is a sector and thats it..? Or am I mistaken?
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Reading Sectors from USB

Post by JohnnyTheDon »

No, you don't need a partition table. The bios loads the first 512 bytes on the disk and thats it.

Check your code, you must be doing something wrong. Make sure you aren't mixing up cylinder/head/sector numbers (ie you're reading from 2:0:0 instead of 0:0:2). Make sure that ES:BX points to your buffer. I don't think there are any BIOSes so buggy they won't read from disk.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Reading Sectors from USB

Post by Dex »

johnsa wrote:Ok.. I've read these posts.. but now I'm more confused. They all deal with how to GET a USB stick to boot at all. Now this brings me to two points
1) My image boots from USB, no problems.. its about 100 lines down in code when it calls to 42h/int 13h or 02h/int 13h to load the actual sectors for stage 2 that it goes funny. The read works and BIOS doesn't report any error or carry but the data loaded into memory is blank (0's).
2) I don't follow WHY I need to have a FAT formatted disk, or a partition table at all. Sure if I want to follow standards and allow multi-boot (play nicely with other OS'es and have partitions) then it would be necessary but does the BIOS or Hardware in any way require this? As far it's concerned a sector is a sector and thats it..? Or am I mistaken?
What you and JohnnyTheDon have written is right (as far as standards goes,) but anyone who have used usb booting on many differant PC think differantlly (or some do).
example see these comments http://menuet.2.forumer.com/index.php?showtopic=1341
http://menuet.2.forumer.com/index.php?showtopic=1323
http://menuet.2.forumer.com/index.php?s ... =1112&st=0

I do beleave that in the usb emulation of hdd or floppy there is some forum of knowledge of partion's etc :shock:
Or maybe hooking int, to test regs etc ?

That is to say if your code is all OK.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Reading Sectors from USB

Post by quok »

Dex wrote:
johnsa wrote:Ok.. I've read these posts.. but now I'm more confused. They all deal with how to GET a USB stick to boot at all. Now this brings me to two points
1) My image boots from USB, no problems.. its about 100 lines down in code when it calls to 42h/int 13h or 02h/int 13h to load the actual sectors for stage 2 that it goes funny. The read works and BIOS doesn't report any error or carry but the data loaded into memory is blank (0's).
2) I don't follow WHY I need to have a FAT formatted disk, or a partition table at all. Sure if I want to follow standards and allow multi-boot (play nicely with other OS'es and have partitions) then it would be necessary but does the BIOS or Hardware in any way require this? As far it's concerned a sector is a sector and thats it..? Or am I mistaken?
What you and JohnnyTheDon have written is right (as far as standards goes,) but anyone who have used usb booting on many differant PC think differantlly (or some do).
example see these comments http://menuet.2.forumer.com/index.php?showtopic=1341
http://menuet.2.forumer.com/index.php?showtopic=1323
http://menuet.2.forumer.com/index.php?s ... =1112&st=0

I do beleave that in the usb emulation of hdd or floppy there is some forum of knowledge of partion's etc :shock:
Or maybe hooking int, to test regs etc ?

That is to say if your code is all OK.
Hrm, in the past I've blindly followed all the posts I saw that said a USB stick must be partitioned for USB-HDD booting. So today I went and read the standards, and nowhere does any standard actually say that. A USB device, to be bootable as a USB-HDD or as a USB-FDD must have the traditional bios boot signature, and that is all. USB-CDROMs follow the El Torito standard (and that is defined in the "USB Mass Storage Specification for Bootability" standard). All USB sticks that I've ever had were partitioned from the factory, however there is no real reason for doing this as far as the standards go. In practice, all modern operating systems expect a block device to also have a partition table of some sort.

So in short, JohnnyTheDon is correct, nothing too special is required to boot a USB stick (other than BIOS and the correct chipset support), and you only need a partition table if you want to be friendly with other operating systems.
johnsa
Member
Member
Posts: 296
Joined: Mon Oct 15, 2007 3:04 pm

Re: Reading Sectors from USB

Post by johnsa »

Problem solved!

I changed quite a bit of the code and put debug output in after every step.
There seemed to be 2 major problems:
1) I had a CLI at the beginning of the code.. in the VM's that didn't cause a problem on real h/w once that was removed the load worked!
2) I was using int 13h extensions and I was making two (incorrect) assumptions. #1 that I had EDD3.0 .. my real machine is only EDD2.1 AND the real machine doesn't support the get drive params int 13h extension call it always returns with carry no matter what's passed to it. So I've changed the code to support any EDD level, and if the drive params call fails assume 512byte sectors and don't perform any compensation. If it works however I check the sector size in bytes to adjust the load appropriately.

In any event I've now tested it on 3 real PC's using CD-ROM(El-Torito), USB, HDD, FDD and under VM's (qemu/bochs/vpc) and they all work! woohoo!

Only one machine failed to boot via USB, but it is a very dodgy old laptop and I suspect it's at fault when booting from USB.
So now the 2nd stage loader is also working and things are progressing nicely.

Thanks for all the help!
John
Post Reply