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