Page 1 of 2

How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 12:26 am
by Hercules
How can I read boot sector of USB keys using INT or not using?

Re: How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 4:39 am
by thepowersgang
The BIOS will read the boot sector for you, and then give you a value in DL that can be used to read more.

If you don't want to use the BIOS, you'll have to write a full USB stack (which is not a small undertaking)

Re: How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 9:32 am
by Hercules
What the number of BIOS INT that read/write sectors of USB removable medias? The INT 13 cannot be because I've already tried with ah=2 and dl=1 and could not read the USB key boot sector.

Re: How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 2:06 pm
by DavidCooper
Hercules wrote:What the number of BIOS INT that read/write sectors of USB removable medias? The INT 13 cannot be because I've already tried with ah=2 and dl=1 and could not read the USB key boot sector.
INT 13h is the one you want, but you need the extended functions which were added later: AH=42h (read) or AH=43h (write). You need to create a DAP: a 16-byte structure which tells the BIOS which sectors and memory are involved in the transfer, then you point DS:SI at the DAP structure. Make sure you align the DAP it on a 16 byte boundary. The format of the DAP is as follows: first byte = 10h (to tell the BIOS the DAP is 16 bytes long); second byte = 0; next two bytes = number of sectors to read/write (maximum 127 for read or 64 for write, so the second of these bytes will always be zero); next four bytes = segment and offset to memory buffer where data's to be written or read from (oOsS represents the order these bytes sit in memory); next eight bytes = absolute start number of sector to read from (LBA). DL needs to hold the drive number, and if you're writing to disk AL = 0 for "don't verify" works fine.

Re: How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 2:50 pm
by Antti
DavidCooper wrote:INT 13h is the one you want, but you need the extended functions which were added later
Is it really so? I have used INT 13h AH=02h (read) with USB removable media and it has worked fine so far. I have understood that using extended functions is not necessary unless you are reading sectors that you cannot reach with CHS or you are reading from El Torito CD (non-emulate).

Re: How to read boot sector of USB keys

Posted: Thu Dec 12, 2013 3:11 pm
by Octocontrabass
The extended functions are not always available when you boot from a USB device, so it's best to use AH=0x02 wherever possible and only use AH=0x42 when CHS addressing won't work.

Re: How to read boot sector of USB keys

Posted: Fri Dec 13, 2013 8:56 am
by Hercules
For removable medias what is the drive number at DL?

Re: How to read boot sector of USB keys

Posted: Fri Dec 13, 2013 1:52 pm
by Hercules
No! Can't be possible that drive number be 80h because this is drive number of Hard Disk. I am writing a forensic tool that must read USB removble boot sector to wipe USB keys and to recover files.

Re: How to read boot sector of USB keys

Posted: Fri Dec 13, 2013 5:56 pm
by Octocontrabass
In that case, your forensic tool should be a Linux application. You will not be able to access the USB device through the BIOS unless you are booting from it.

Re: How to read boot sector of USB keys

Posted: Sat Dec 14, 2013 5:01 am
by Hercules
Octocontrabass wrote:You will not be able to access the USB device through the BIOS unless you are booting from it.
Are you joking..I do bootstrap from DOS so I can access teh removble media through interrupts of BIOS

Re: How to read boot sector of USB keys

Posted: Sat Dec 14, 2013 6:21 am
by thepowersgang
No joking here, the BIOS was only ever "defined" to have the two floppy drives and up to four IDE hard disks, anything else is an extension.

When booting from something that doesn't fall into those two categories (USB key, AHCI SATA drive, CD) then you shouldn't make any assumptions about the drive number used to access it. And the BIOS is not required to expose any other "non-standard" devices other than the boot medium.

[edit] as Octocontrabass said, if you want to write something to do recovery on raw volumes, build it on linux (or another stable OS), because then you get access to far more storage types than the BIOS would give you.

Re: How to read boot sector of USB keys

Posted: Sat Dec 14, 2013 1:20 pm
by DavidCooper
There's probably a lot of luck involved depending on the machine you use. I have only two significantly different machines available, but both of them will allow access (via the BIOS) to a number of flash drives in addition to the drive from which the machine was booted. If I boot my cheap netbook from USB floppy drive, for example, the drive number for that drive will be 0h, the internal hard disk will be 80h, and the flash drives and SD card will get numbers 81h, 82h, and 83h. If I boot from a flash drive, that flash drive will be 80h, the internal hard drive will be 81h, and the other drives will be 82h, 83h and 84h (or 0h if the floppy drive is plugged in). I can't remember where the DVD/CD drive shows up on the other machine (an expensive Sony) - I've only booted it once with my OS, but all the flash drives were fully available.

So, you need to try out a range of numbers to see if there is a drive present for each one, and if there isn't you are probably just unlucky to be using a machine with a BIOS that can't handle them. If you're hoping to make a universal tool for other people to use, you should give up on this approach as it will only work on some machines. If you're only trying to build something for your own use, it may be viable, but you'll need to find a machine with a BIOS that allows you to access USB drives that it didn't boot from.

Re: How to read boot sector of USB keys

Posted: Sun Dec 15, 2013 3:18 pm
by BMW
This should be in OS Development, not General Programming.

Re: How to read boot sector of USB keys

Posted: Sat Dec 28, 2013 9:22 am
by Hercules
DavidCooper wrote:There's probably a lot of luck involved depending on the machine you use. I have only two significantly different machines available, but both of them will allow access (via the BIOS) to a number of flash drives in addition to the drive from which the machine was booted. If I boot my cheap netbook from USB floppy drive, for example, the drive number for that drive will be 0h, the internal hard disk will be 80h, and the flash drives and SD card will get numbers 81h, 82h, and 83h. If I boot from a flash drive, that flash drive will be 80h, the internal hard drive will be 81h, and the other drives will be 82h, 83h and 84h (or 0h if the floppy drive is plugged in). I can't remember where the DVD/CD drive shows up on the other machine (an expensive Sony) - I've only booted it once with my OS, but all the flash drives were fully available.

So, you need to try out a range of numbers to see if there is a drive present for each one, and if there isn't you are probably just unlucky to be using a machine with a BIOS that can't handle them. If you're hoping to make a universal tool for other people to use, you should give up on this approach as it will only work on some machines. If you're only trying to build something for your own use, it may be viable, but you'll need to find a machine with a BIOS that allows you to access USB drives that it didn't boot from.
Do NOT lie to me. I've tested for drive numbers 0x81,0x82,0x83,0x84,0x85 and none of them are the drive number of removable media. Why nobody help me at this forum? Gggghhhhrrr! :x

Re: How to read boot sector of USB keys

Posted: Sat Dec 28, 2013 9:42 am
by Brynet-Inc
Hercules wrote:Why nobody help me at this forum? Gggghhhhrrr! :x
Certainly not because you're an
Image.