]> git.dujemihanovic.xyz Git - nameless-os.git/blob - boot/x86/vbr-fat32.s
Fixes and improvements to FAT32 driver
[nameless-os.git] / boot / x86 / vbr-fat32.s
1 ; x86 bootloader for nameless-os, FAT32 VBR portion
2 ; This is what's going to be on most USB sticks and HDDs, for now
3
4 bits 16
5 org 0x7c00
6 cpu 686
7
8 STAGE3_ADDRESS equ 0x8000
9 STAGE3_SEGMENT equ STAGE3_ADDRESS >> 4
10 STAGE3_OFFSET equ STAGE3_ADDRESS & 0xf
11
12 %include "fat32-structs.s"
13
14 _start:
15 jmp short real_start
16 nop
17
18 times 0x57 db 0 ; skip past BPB
19
20 real_start:
21 sti
22 ; no need to set up segments and stack again, because MBR did it for us
23 mov bp, 0x7c00
24
25 ; we expect the boot drive to be in DL and our partition table entry in DS:SI
26 mov [BOOT_DRIVE], dl
27
28 ; calculate the 1st sector of the data area
29 call get_1st_data_sec
30
31 .load_root:
32 ; load the root directory
33 mov ax, 0x1000
34 mov es, ax
35 xor di, di
36 mov eax, BPB_RootClus
37 call read_cluster_chain
38
39 push cx
40 mov cx, 11
41 push si
42 .find_stage_3:
43 mov si, STAGE3_NAME
44 cmp byte [es:di], 0 ; we have no more entries to look at
45 je .stage3_missing
46 cmp byte [es:di], 0xe5 ; the entry was only previously used and as such not worth looking at
47 je .increment
48 repe cmpsb
49 je .stage3_found
50 .increment:
51 add di, 32
52 jno .find_stage_3
53 mov bx, es
54 add bx, 0x1000
55 mov es, bx
56 .stage3_found:
57 pop si
58 pop cx
59 add di, 9 ; knowing that cmpsb incremented this by 11, we can also increment it by 9
60 ; right here so we can use 1 less offset below
61 ; stage 3 has been found and ES:DI points to its directory entry
62 mov ax, [es:di] ; load high half of the 1st cluster
63 shl eax, 16
64 mov ax, [es:di+(dir_entry.firstcluslo-dir_entry.firstclushi)] ; load low half of the 1st cluster
65 mov bx, STAGE3_SEGMENT
66 mov es, bx
67 mov di, STAGE3_OFFSET
68 call read_cluster_chain ; read stage 3
69 mov ds, bx
70 call STAGE3_SEGMENT:STAGE3_OFFSET ; call stage 3
71 jmp .halt ; halt in case we return, which should never happen
72
73 .stage3_missing:
74 mov si, stage3_missing
75 call print
76 .halt:
77 hlt
78 jmp short $-1
79
80 ; ds:si - string
81 print:
82 pusha
83 mov ah, 0xe
84 xor bh, bh
85 .loop:
86 lodsb
87 cmp al, 0
88 je .done
89 int 0x10
90 jmp .loop
91 .done:
92 mov al, 0xd
93 int 0x10
94 mov al, 0xa
95 int 0x10
96 popa
97 ret
98
99 %include "fat32.s"
100
101 stage3_missing: db "LOADER.BIN is missing", 0
102 STAGE3_NAME: db "LOADER BIN"
103
104 times 510-($-$$) db 0
105 dw 0xaa55