STAGE3_SEGMENT equ STAGE3_ADDRESS >> 4
STAGE3_OFFSET equ STAGE3_ADDRESS & 0xf
-%include "fat32-structs.s"
+%include "fat32/fat32-structs.s"
+
+%macro print 1
+ mov si, %1
+ call print_str
+%endmacro
_start:
jmp short real_start
; we expect the boot drive to be in DL and our partition table entry in DS:SI
mov [BOOT_DRIVE], dl
- mov [part_table_entry], si
; calculate the 1st sector of the data area
call get_1st_data_sec
mov eax, BPB_RootClus
call read_cluster_chain
+ push cx
mov cx, 11
+ push si
.find_stage_3:
mov si, STAGE3_NAME
cmp byte [es:di], 0 ; we have no more entries to look at
add bx, 0x1000
mov es, bx
.stage3_found:
- sub di, 0xb ; "repe cmpsb" incremented this
+ pop si
+ pop cx
+ add di, 9 ; knowing that cmpsb incremented this by 11, we can also increment it by 9
+ ; right here so we can use 1 less offset below
; stage 3 has been found and ES:DI points to its directory entry
- mov ax, [es:di+dir_entry.firstclushi] ; load high half of the 1st cluster
+ mov ax, [es:di] ; load high half of the 1st cluster
shl eax, 16
- mov ax, [es:di+dir_entry.firstcluslo] ; load low half of the 1st cluster
+ mov ax, [es:di+(dir_entry.firstcluslo-dir_entry.firstclushi)] ; load low half of the 1st cluster
mov bx, STAGE3_SEGMENT
mov es, bx
mov di, STAGE3_OFFSET
call read_cluster_chain ; read stage 3
- mov ds, bx
- call STAGE3_SEGMENT:STAGE3_OFFSET ; call stage 3
+ mov dl, [BOOT_DRIVE]
+ call STAGE3_ADDRESS ; call stage 3
jmp .halt ; halt in case we return, which should never happen
.stage3_missing:
- mov si, stage3_missing
- call print
+ print stage3_missing
+ jmp .halt
+.error:
+ print read_error
.halt:
- cli
hlt
jmp short $-1
; ds:si - string
-print:
+print_str:
pusha
mov ah, 0xe
xor bh, bh
popa
ret
-%include "fat32.s"
+%include "fat32/fat32.s"
stage3_missing: db "LOADER.BIN is missing", 0
STAGE3_NAME: db "LOADER BIN"