stdio.asm
[pwnd.git] / boot / main.asm
blob8bd14f063521cd94207592aca8107bc5499d80c3
1 bits 16
2 org 0x7c00
4 start: jmp loader
7 TIMES 0Bh-$+start DB 0
9 bpbBytesPerSector: DW 512
10 bpbSectorsPerCluster: DB 1
11 bpbReservedSectors: DW 1
12 bpbNumberOfFATs: DB 2
13 bpbRootEntries: DW 224
14 bpbTotalSectors: DW 2880
15 bpbMedia: DB 0xF0
16 bpbSectorsPerFAT: DW 9
17 bpbSectorsPerTrack: DW 18
18 bpbHeadsPerCylinder: DW 2
19 bpbHiddenSectors: DD 0
20 bpbTotalSectorsBig: DD 0
21 bsDriveNumber: DB 0
22 bsUnused: DB 0
23 bsExtBootSignature: DB 0x29
24 bsSerialNumber: DD 0xa0a1a2a3
25 bsVolumeLabel: DB "MOS FLOPPY "
26 bsFileSystem: DB "FAT12 "
29 Print:
30 pusha
31 mov ah,0x0e
32 .loop:
33 mov al,[si]
34 test al,al
35 jz .end
36 int 0x10
37 inc si
38 jmp .loop
39 .end:
40 popa
41 ret
44 LBAtoCHS:
45 pusha
47 xor dx,dx
48 div word [bpbSectorsPerTrack]
49 inc dl
50 mov byte [absSector],dl
52 xor dx,dx
53 div word [bpbHeadsPerCylinder]
54 mov byte [absHead],dl
56 mov byte [absTrack],al
57 popa
58 ret
60 ;CX Count
61 ;ES:BX offset
62 Read:
64 .read:
65 call Reset
67 push ax
68 push bx
69 push cx
71 call LBAtoCHS
72 mov ah,0x02
73 xor dl,dl
74 mov dh,[absHead]
75 mov ch,[absTrack]
76 mov cl,[absSector]
77 mov al,1
79 int 0x13
80 pop cx
81 pop bx
82 pop ax
83 jc .read
84 dec cx
85 add bx,word [bpbBytesPerSector]
86 inc ax
87 test cx,cx
88 jnz .read
89 ret
91 Reset:
92 pusha
94 .reset:
95 mov ah,0
96 mov dl,0
97 int 0x13
98 jc .reset
100 popa
103 ClusterLBA:
104 sub ax,2
105 xor cx,cx
106 mov cl,byte [bpbSectorsPerCluster]
107 mul cx
108 add ax,word [data]
111 loader:
113 cli
114 mov ax,cs
115 mov ds,ax
118 mov ax,0x0003
119 int 0x10
121 mov si,banner
122 call Print
124 xor cx,cx
125 xor dx,dx
126 mov ax,0x0020
127 mul word [bpbRootEntries]
128 div word [bpbBytesPerSector]
129 xchg ax,cx
131 mov al,[bpbNumberOfFATs]
132 mul word [bpbSectorsPerFAT]
133 add ax,[bpbReservedSectors]
135 mov word [data],ax
136 add word [data],cx
138 push 0x0000
139 pop es
140 mov bx,0x7e00
142 call Read
144 mov cx,[bpbRootEntries]
145 mov di,0x7e00
146 .loop:
147 push cx
148 mov cx,11
149 mov si,imageName
150 push di
151 rep cmpsb
152 pop di
153 je .ok
154 pop cx
155 add di,32
156 loop .loop
157 jmp .bad
159 .ok:
160 mov si,ok
161 call Print
162 mov dx,word [di+0x1A]
163 mov word [Cluster],dx
165 xor ax,ax
166 mov al,[bpbNumberOfFATs]
167 mul word [bpbSectorsPerFAT]
168 xchg ax,cx
170 mov ax,[bpbReservedSectors]
172 push 0x0000
173 pop es
174 mov bx,0x7e00
176 call Read
179 push 0x2000
180 pop es
181 xor bx,bx
182 push bx
183 push es
185 .load:
186 pop es
187 pop bx
189 mov ax,word [Cluster]
190 call ClusterLBA
191 mov cx,1
192 call Read
193 push bx
194 push es
196 mov ax,word [Cluster]
197 mov cx,ax
198 mov dx,ax
199 shr dx,0x0001
200 add cx,dx
201 push 0x0
202 pop es
203 mov bx,0x7e00
204 add bx,cx
205 mov dx,word [bx]
206 test ax,0x0001
207 jnz .odd
208 .even:
209 and dx,0000111111111111b
210 jmp .done
211 .odd:
212 shr dx,0x0004
213 .done:
214 mov word [Cluster],dx
215 cmp dx,0x0FF0
216 jb .load
220 jmp 0x2000:0
221 jmp .end
222 .bad:
223 mov si,bad
224 call Print
225 .end:
227 hlt
229 absSector db 0
230 absHead db 0
231 absTrack db 0
232 Cluster dw 0
233 data dw 0
234 imageName db "STAGE2 SYS"
235 ok db "Stage2 was find...",13,10,0
236 bad db "Stage2 dont find...",13,10,0
237 banner db "Boot start...",13,10,0
238 times 510 - ($-$$) db 0
239 dw 0xAA55