So I decided to try and split my bootloader into two parts, a single stage loader that will load the second stage. Problem is, for some reason or another, the first stage is having trouble reading from the USB. Here's the code for the first stage bootloader:
Code: Select all
BITS 16
org 0x7C00
start:
mov ax, 0
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax ;Puts 0 into the segment pointer, we are using real memory.
mov sp, 0x7C00 ;Moves 7C00 into the stack pointer, so that all data <7C00 is stack.
;moves the drive number into memory
mov dl, [DRIVE_NUMBER]
xor bx, bx
mov ah, 0x0E
mov al, 'S'
int 10h
;Will attempt to read from the disk
.Reset:
xor bx, bx
mov ah, 0x0E
mov al, 'L'
int 10h
mov ah, 0 ; reset floppy disk function
mov dl, [DRIVE_NUMBER] ; drive 0 is floppy drive
int 0x13 ; call BIOS
jc .Reset ; If Carry Flag (CF) is set, there was an error. Try resetting again
mov ax, 0x7E00 ; we are going to read sector to into address 0x7E00:0
mov es, ax
xor bx, bx
.Read:
xor bx, bx
mov ah, 0x0E
mov al, 'R'
int 10h
xor bx, bx
mov ah, 0x02 ; function 2
mov al, 4 ; read 4 sectors from drive.
mov ch, 1 ; we are reading the second sector past us, so its still on track 1
mov cl, 2 ; sector to read (The second sector)
mov dh, 0 ; head number. USBs only have one head, right?
mov dl, [DRIVE_NUMBER] ; drive number that the BIOS passed to us from earlier.
int 0x13 ; call BIOS - Read the sector
jc .Read ; Error, so try again
mov ax, 0
;Resets es to 0.
mov es, ax
jmp 0x7E00 ; jump to execute the sector!
jmp Exit ;shouldn't be called!
DRIVE_NUMBER db 0
Exit:
hlt ;halts the program for now
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
dw 0xAA55 ; The standard PC boot signature
EDIT:
Worth noting, both bootloaders are in the same unformatted USB. Bootloader 1 is in sector 0, and bootloader 2 is in sectors 1-4.