For about the past week, I've been developing a simple OS for learning purposes and... "fun". VirtualBox and NASM in tow, I actually got off to a pretty good start. Eventually, I decided that I wanted to also develop a bootloader (after hitting the 512-byte wall pretty hard) by powering through the infamous Brokenthorn tutorial, up until the point of loading from filesystems.
With some HexFiend shenanigans and some blank FAT16 images, I eventually got the BPB worked out. With some additional Assembly hackery (the basis being Brokenthorn's tutorial, part 6), I also got file loading working with my bootloader, which loads the aptly-named 'boot' file from my virtual disk (made using dd if=/dev/zero of=boot.img bs=512 count=2880)
So, what is the issue then? It's what I see when I load on to actual hardware via a USB stick (in this case, /dev/disk3, where the compiled file is boot.bin):
dd bs=512 count=1 if=compiled/boot.bin of=/dev/disk3
Here is the expected output (in VirtualBox):
Compared to the actual output (on an old laptop)
'-' indicates a sector is being loaded
'_' indicates a sector was loaded
'!' indicates all of the desired sectors were loaded properly
'R' indicates a read error
'T' indicates the FAT table is being loaded
'D' indicates the FAT table was loaded properly
'F' means the file is being located (or Found, hence the F)
'L' means the file is being loaded
(I would have used actual debug messages, but the 512-byte limit is pretty gruesome.)
So, the difference is that one is a USB stick, and one is a (virtual) floppy disk. They both have the exact same information loaded on each, including the BPB. However, one works, and one doesn't. Here is the main part of my code for loading a sector (using ah 02h/int 13h, which I heard worked properly for a USB):
ReadSectors:
mov di, 0x0005 ; How many times should we retry the read?
ReadSectors.loop:
; DEBUG
push ax
mov ah, 0eh
mov al, '-'
int 10h
pop ax
push ax
push bx
push cx
call LBAToCHS
mov ah, 02h ; Set the interrupt to the
; 'read sector' function
mov al, 1 ; Only read one sector
mov ch, byte[chs.track] ; The track to read from
mov cl, byte[chs.sector] ; The sector to read from
mov dh, byte[chs.head] ; The head to read from
mov dl, byte[_bpb.driveNumber] ; The drive to read from
int 13h ; Call our 'disk IO' interrupt
jnc ReadSectors.success ; If we successfully read the data,
; we don't have to try again
mov ah, 00h ; Set the interrupt to the
; 'reset disk' function
int 13h ; Call our 'disk IO' interrupt
dec di ; Decrement our error counter
pop cx
pop bx
pop ax
jnz ReadSectors.loop ; Try again if we've failed
jmp ReadSectors.fail ; RED ALERT
(The full source, including the BPB, can be found on Pastebin (http://pastebin.com/SeUm7xu6)
I've overcome a number of issues with Assembly so far, but this one has me stumped. Hopefully, I can get past the bootloader and abstract the file IO as soon as possible.
Any suggestions would be greatly appreciated. Thanks in advance!
Your code reads from drive number 0 which may not be the device bootloader was loaded from (and very often isn't if you boot from usb stick). The drive number you should read from is loaded by BIOS into dl
register. It is already an answered question on SO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With