6

I have recently been studying some bootstrap code which was intended for use with a floppy drive. My goal is to modify the program so that it uses my USB flash drive. Now I see how the INT 13H function has been used with the floppy device, but I guess my question is, how will communicating with the USB drive differ?

For example, here is a snippet of the floppy code (GNU assembler):



    movb    $0x00,%dl       /* select 1st floppy */

    /* later */

    movw    sec,%cx     /* get sector number */
    movw    head,%dx    /* get head number */

    movw    $0x0201,%ax /* read 1 sector */
    int $0x13

Now I have read that moving 0x80 into %dl will select the first HDD in the BIOS. In my particular bios I can change the drive order, which would include a USB drive. I am quite sure this is becoming BIOS dependant, but I was thinking that the order listed in the BIOS could correspond to the value I move into %dl. I need to track down some documentation...

I am really unfamiliar with working with block devices as it is, can someone point me to a good place to start learning more?

Thanks!

Siguza
  • 21,155
  • 6
  • 52
  • 89
Mr. Shickadance
  • 5,283
  • 9
  • 45
  • 61

3 Answers3

5

The simple answer is that if the BIOS can boot from the USB flash drive the same BIOS functions for floppy disk / hard drive access can be used.

The happy answer is that a simple technique allows the same boot sector code to access a floppy disk image on a USB flash drive whether it was booted with floppy disk emulation or hard drive emulation. If dl=80h (hard drive emulation)

GET DRIVE PARAMETERS
int 13h, ah=8
Return:
ch=maximum sector number (same as number of sectors per track)
dh=maximum head number (just add 1 to get number of heads)

This returned information describes the geometry of the emulated device (if dl=0 then it's standard floppy disk geometry - 18 sectors per track and 2 heads). This can be used to calculate the required Cylinder Head Sector information required for:

READ SECTOR(S)
int 13h, ah=2

and

WRITE SECTOR(S)
int 13h, ah=3

See Ralf Brown's Interrupt List - int 13h

See my post here: USB Booting Secrets

Mike Gonta
  • 644
  • 6
  • 11
  • OK, having sort of revisited this I was looking at some code (working code) which explicitly loads dl=0x00 even though it is using a USB flash drive. Now I guess this is BIOS dependent behavior, perhaps other BIOS' won't allow you to treat a flash drive as a floppy. – Mr. Shickadance Aug 10 '11 at 18:44
  • 1
    It is possible and convenient to boot and run a FAT12 floppy disk image from a USB flash drive on any BIOS/version that will boot from a USB flash drive. The dl value must be used. Even if dl=80h (which will be the case on all late model BIOS/version) the hard drive geometry retuned from int 13h, ah=8 can be used. The 1.44 MB format, even though restrictive, is plenty for starting out and is universal. – Mike Gonta Aug 20 '11 at 14:30
3

If the BIOS "sees" the USB device as a hard drive it will assign a number to it. The assigned number starts at 0x80 for the first hard drive, 0x81 for the second, etc. So depending how many hdds are installed your USB drive will be at 0x81 or greater. Also if you change the order in BIOS the USB drive number will change to reflect this.

Camilo Estevez
  • 637
  • 1
  • 8
  • 16
  • Really helped out. Also, not that some BIOSes label the flash drive 0x80 directly, as per [my question](http://stackoverflow.com/questions/4703595/nasm-load-code-from-usb-drive). – new123456 Jan 16 '11 at 03:49
0

The flash drive is only available if the BIOS supports it. And if it does, it would probably let you boot from it already. Most of this is done by emulation, so the calls to boot the flash drive are probably the same.

I've dumped out the boot blocks from my thumb drives, and have found both floppy and hard drive formats.

Maybe you should just try a bunch of numbers for accessing the drives and see which ones answer.

I think Google is your friend here. Start with "INT 13H". And ask more questions.

gbarry
  • 10,352
  • 6
  • 33
  • 43