0

So I've been trying to write a little bootloader myself (for fun and education). So far I've completed a bootloader (or rather "bootable program") which behaves exactly like MSDOS. Then when I tried to implement more stuff, I exceeded 512 byte limit. I decided to expand my tiny OS. I've moved on to writing sth like a 2 stage bootloader where stage 2 behaves as the main kernel so I would have more than 512 bytes.

I've written the first stage of my bootloader. Almost. I got the idea of jumping to the sector where the stage 2 is located to execute it, but the problem is, I'm using a FAT32 USB instead of those floppy images that everyone is writing codes and tutorials for.

My question will be very childish: How can I load stage2.bin using stage1.bin, in a FAT32 filesystem?

So far this failed to help me: Loading second stage of a bootloader

Community
  • 1
  • 1
ArdaGuler
  • 95
  • 2
  • 8
  • You have just enough room to write a 512 byte single block volume boot record that can scan through the FAT32 main directory and load a file into memory, both steps require that you follow their respective allocation chains in the FAT. You need to reserve space for the FAT32 BPB since that's part of the file system metadata, but that still leaves enough room – Ross Ridge Oct 23 '16 at 17:59
  • Figure out what sector it's in by looking at the FAT entry for it and read it. – David Hoelzer Oct 23 '16 at 22:14

1 Answers1

0

OK, the solution seems to be rather easy (as long as you know where your second stage binary starts). Instead of trying to load the binary on sector 0x0200 (which is the second block of FAT32), loading this section has worked as it worked on other FAT partitioned drives:

0x7E00 : 0x00

The type of FAT filesystem doesn't seem to be affecting where that points. Also, another one of my problems was about the second stage. It should've had a

[ORG 0x0000]

at the begining.

Final code:

[BITS 16] 
[ORG 0x7C00] 

Boot:
xor ax, ax   ; AX=0
mov ds, ax   ; DS=0  
mov es, ax   ; ES=0


add ax, 0x9000
mov ss, ax
mov sp, 0xF000 

mov ah, 0x00
int 0x13

mov ax, 0x7E00
mov es, ax ; Load to 0x7E00 : 0x00
mov bx, 0x00
SurucuOku:
mov ah, 0x02
mov al, 0x01 ; Read 1 sector
mov ch, 0x00 ; Read on cylinder 0
mov cl, 0x02 ; Read sector 2
mov dh, 0x00 ; Head number 0
int 0x13

jnc Basari
mov al, 0x46
call hata
jmp SurucuOku

hata: 
pusha
mov ah, 0x09
mov bh, 0x00
mov bl, 0x0F
mov cx, 0x01
int 0x10
popa
ret

Basari:
jmp 0x07E00:0x00 

TIMES 510 - ($ - $$) db 0
DW 0xAA55 
ArdaGuler
  • 95
  • 2
  • 8