]> git.dujemihanovic.xyz Git - nameless-os.git/blob - boot/x86/vbr-fat32.s
WIP: Add new FAT32 bootloader
[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 mov [part_table_entry], si
28
29 ; calculate the 1st sector of the data area
30 call get_1st_data_sec
31
32 .load_root:
33 ; load the root directory
34 mov ax, 0x1000
35 mov es, ax
36 xor di, di
37 mov eax, BPB_RootClus
38 call read_cluster_chain
39
40 mov cx, 11
41 .find_stage_3:
42 mov si, STAGE3_NAME
43 cmp byte [es:di], 0 ; we have no more entries to look at
44 je .stage3_missing
45 cmp byte [es:di], 0xe5 ; the entry was only previously used and as such not worth looking at
46 je .increment
47 repe cmpsb
48 je .stage3_found
49 .increment:
50 add di, 32
51 jno .find_stage_3
52 mov bx, es
53 add bx, 0x1000
54 mov es, bx
55 .stage3_found:
56 sub di, 0xb ; "repe cmpsb" incremented this
57 ; stage 3 has been found and ES:DI points to its directory entry
58 mov ax, [es:di+dir_entry.firstclushi] ; load high half of the 1st cluster
59 shl eax, 16
60 mov ax, [es:di+dir_entry.firstcluslo] ; load low half of the 1st cluster
61 mov bx, STAGE3_SEGMENT
62 mov es, bx
63 mov di, STAGE3_OFFSET
64 call read_cluster_chain ; read stage 3
65 mov ds, bx
66 call STAGE3_SEGMENT:STAGE3_OFFSET ; call stage 3
67 jmp .halt ; halt in case we return, which should never happen
68
69 .stage3_missing:
70 mov si, stage3_missing
71 call print
72 .halt:
73 cli
74 hlt
75 jmp short $-1
76
77 ; ds:si - string
78 print:
79 pusha
80 mov ah, 0xe
81 xor bh, bh
82 .loop:
83 lodsb
84 cmp al, 0
85 je .done
86 int 0x10
87 jmp .loop
88 .done:
89 mov al, 0xd
90 int 0x10
91 mov al, 0xa
92 int 0x10
93 popa
94 ret
95
96 %include "fat32.s"
97
98 stage3_missing: db "LOADER.BIN is missing", 0
99 STAGE3_NAME: db "LOADER BIN"
100
101 times 510-($-$$) db 0
102 dw 0xaa55