How to read boot sector of USB keys

Programming, for all ages and all languages.
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

How to read boot sector of USB keys

Post by Hercules »

How can I read boot sector of USB keys using INT or not using?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: How to read boot sector of USB keys

Post 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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

Re: How to read boot sector of USB keys

Post 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.
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: How to read boot sector of USB keys

Post 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.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: How to read boot sector of USB keys

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

Re: How to read boot sector of USB keys

Post 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.
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

Re: How to read boot sector of USB keys

Post by Hercules »

For removable medias what is the drive number at DL?
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

Re: How to read boot sector of USB keys

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

Re: How to read boot sector of USB keys

Post 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.
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

Re: How to read boot sector of USB keys

Post 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
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: How to read boot sector of USB keys

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: How to read boot sector of USB keys

Post 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.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: How to read boot sector of USB keys

Post by BMW »

This should be in OS Development, not General Programming.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Hercules
Posts: 15
Joined: Sun Dec 01, 2013 6:08 am

Re: How to read boot sector of USB keys

Post 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
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: How to read boot sector of USB keys

Post by Brynet-Inc »

Hercules wrote:Why nobody help me at this forum? Gggghhhhrrr! :x
Certainly not because you're an
Image.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply