Page 1 of 1

USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 2:06 am
by 01000101
I've been looking into alternatives to floppies for testing on real hardware recently. I decided on putting my OS on a bootable USB stick and trying that out as all the docs say that it's very similar to booting a floppy using INT 0x13 (read sector). The USB stick I have is an PNY OPTIMA Pro Attach'e 4GB and is read by my supermicro serverboard's BIOS as a USB-HDD 4GB.

My code also detects it as a HDD (DL == 0x80), and AH = 0x08, DL = 0x80, INT 0x13 reports typical results for a HDD (63 sect/head, 255 heads).

When I read sector 1 of head 0 in sector 0, I get garbage bytes in the buffer at ES:BX. If I read sector 1 of head 1 in sector 0, I get the expected bootloader hex code in ES:BX. All the docs I've read have not mentioned the need to set head (DH) to 1 for HDD or USB-HDD, and I'm pretty sure it's not supposed to be 1 for the *first* sector of the drive.

here's some stripped down code from my load_kernel function.
this sets up INT 0x13 to read from sector 1, head 1, cylinder 0, and tells it to use the buffer 0x1000:0x0000, and to read the max sectors (63 in this case), and to use the HDD device (0x80 in this case).

[0x7B00] == 0x80
[0x7B04] == 63
I use that space for storing the device parameters from the previous INT 0x13.

Code: Select all

    mov dh, 1               ; start head
    mov ax, 0
    mov ds, ax              ; makes sure DS == 0 (for memory references)
    mov ch, 0               ; start cylinder
 .LOAD_KERNEL:
    xor bx, bx              ; buffer == ES:BX (0x1000:0x0000)
    mov cl, 1               ; start sector
    mov dl, byte [0x7B00]   ; drive number
    mov ax, 0x1000          ; set ES to 0x1000
    mov es, ax
    mov ah, 0x02            ; function 0x02
    mov al, byte [0x7B04]   ; sectors to read (max sectors reported by INT 0x13/AH=0x08)
    
    INT 0x13
    jc .LOAD_KERNEL
The above returns the proper bootsector that the system booted from, and all the sectors are correct (as I can also boot my kernel with it). But if the above code changes DH to 0, it reads garbage on the USB-HDD. viceversa happens with a floppy (DH = 0 works, and DH = 1 fails as expected).

any ideas as to why this is happening?
btw, I'm in unreal mode during that. The floppy code (similar to the above) has worked forever and didn't have any noticable problems when dealing with physical or emulated floppies.

Re: USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 2:20 am
by Brendan
Hi,
01000101 wrote:When I read sector 1 of head 0 in sector 0, I get garbage bytes in the buffer at ES:BX. If I read sector 1 of head 1 in sector 0, I get the expected bootloader hex code in ES:BX. All the docs I've read have not mentioned the need to set head (DH) to 1 for HDD or USB-HDD, and I'm pretty sure it's not supposed to be 1 for the *first* sector of the drive.
There's one common difference between hard drives and floppy drives - partitions. For hard drives the first track is typically used by the MBR and boot manager, while the OS's boot loader is normally in the first sector of the "active" partition.

I'm guessing that your OS's boot loader is in the first sector of the first partition; where the first partition begins on the second track.

Of course you didn't say how your code is placed on the USB stick, so my guess is just a guess.


Cheers,

Brendan

Re: USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 2:24 am
by 01000101
I GParted the USB drive to an unformatted (yet allocated) 4GB drive, and use DD if="kernel.img" of="/dev/sdd1" bs=512 to write to it.

I wouldn't think there would be a 'partition' per-se on it as wouldn't DD obliterate that?

Re: USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 3:47 am
by Brendan
Hi,
01000101 wrote:I GParted the USB drive to an unformatted (yet allocated) 4GB drive, and use DD if="kernel.img" of="/dev/sdd1" bs=512 to write to it.

I wouldn't think there would be a 'partition' per-se on it as wouldn't DD obliterate that?
Hehe - "dd if="kernel.img" of="/dev/sdd" bs=512" will write to the entire USB drive, but "dd if="kernel.img" of="/dev/sdd1" bs=512" will only write to partition 1.... ;)


Cheers,

Brendan

Re: USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 4:50 am
by 01000101
#-o
I guess I should focus a little more of my time towards learning my programming environment than the programming itself. :)

Thanks btw!

Re: USB-HDD bug, or a code bug?

Posted: Sat Jan 03, 2009 3:15 pm
by Dex
@01000101, Maybe this may help you with your USB booting http://dex4u.com/USBboot.htm
It's for my OS, but you just replace the DexOS bin file's with your OS.
I have made a windows program that does it all for you, it's 3/4 finished, if theres enough coders that want it, i will finish it.

PS: Any ? about what in the bin files, just ask.