USB-HDD bug, or a code bug?

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
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

USB-HDD bug, or a code bug?

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

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

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

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

Post 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!
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

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

Post 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.
Post Reply