summaryrefslogtreecommitdiff
path: root/src/bootloader.asm
blob: 583c169b6937c216a7a1dfbd61bacbb95101cd26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
;Copyright 2019 Benji Dial

;Permission to use, copy, modify, and/or distribute this
;software for any purpose with or without fee is hereby
;granted, provided that the above copyright notice and this
;permission notice appear in all copies.

;THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
;ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
;IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
;EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
;INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
;WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
;TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
;USE OR PERFORMANCE OF THIS SOFTWARE.

pointers:
.fat equ 0x0500
.root_dir equ 0x0504
.kernel equ 0x0508

fat_header:
.oem_name            equ 0x7c03;mkfs.fat    - ignored
.bytes_per_sector    equ 0x7c0b;0x0200      - assumed
.sectors_per_cluster equ 0x7c0d;0x01        - assumed
.reserved_sectors    equ 0x7c0e;0x0002      - handled
.n_fats              equ 0x7c10;0x01        - assumed
.root_dir_entries    equ 0x7c11;            -
.n_sectors           equ 0x7c13;0x0b40      -
.media_type          equ 0x7c15;            -
.sectors_per_fat     equ 0x7c16;            - handled
.sectors_per_track   equ 0x7c18;0x0012      -
.sides               equ 0x7c1a;0x0002      -
.hidden_sectors      equ 0x7c1c;0x0000_0000 - assumed, top half used as scratch for sector number of cluster 0
.long_sectors        equ 0x7c20;0x0000_0000 - assumed
.drive_number        equ 0x7c24;            - ignored, used as scratch
.flags               equ 0x7c25;            - ignored
.signature           equ 0x7c26;0x29        - halts on other values
.id                  equ 0x7c27;            - ignored
.label               equ 0x7c2b;PORTLAND OS - assumed
.fs_type             equ 0x7c36;FAT12       - assumed

org 0x7c3e
bits 16

  in al, 0x92
  or al, 0x02
  out 0x92, al

  mov ah, 0x42
  mov si, dap
  int 0x13

  mov byte [fat_header.drive_number], dl

memory:
  mov bx, 0x2010
  mov eax, 0x0001_0001
.clear:
  mov dword [bx], eax
  add bx, 4
  test bx, 0x4000
  jne .clear

  xor ax, ax
  mov es, ax
  mov si, 0x7000
  xor ebx, ebx

.loop:
  mov eax, 0x0000_e820
  mov ecx, 24
  mov edx, 0x534d_4150
  int 0x15
  jc .done

  test cl, 24
  jne .not_24

  test byte [0x7014], 0x01
  jz .loop

.not_24:
  test dword [0x7010], 1
  jne .loop

  test byte [0x7003], 0
  jne .loop

  test dword [0x7004], 0
  jne .loop

  test dword [0x700c], 0
  je .check_lower

  mov dword [0x7008], 0xffff_ffff

.check_lower:
  test dword [0x7008], 0
  je .loop

  mov edx, ebx
  mov ebx, dword [0x7000]
  mov eax, ebx
  add eax, dword [0x7008]
  jnc .pagify
  mov eax, 0xffff_ffff

.pagify:
  dec ebx
  shr ebx, 12
  inc ebx
  shr eax, 12
  dec eax

  test eax, 0xffff_f000
  jz .maxed
  mov eax, 0x0000_0fff

.maxed:
  inc eax
  test eax, ebx
  je .next

  shl ebx, 1
  or ebx, 0x2000
  shl eax, 1
  add eax, 0x2000
  mov ecx, eax

  xor ax, ax
.free_loop:
  mov word [ebx], ax
  add ebx, 2
  test ebx, ecx
  jne .free_loop

.next:
  mov ebx, edx
  jmp .loop

.done:
  mov dword [0x2000], 0x0002_0002
  mov dword [0x2004], 0x0002_0002
  mov dword [0x2008], 0x0002_0002
  mov dword [0x200c], 0x0002_0002

  test byte [fat_header.signature], 0x29
  jne halt

  mov ax, word [fat_header.sectors_per_fat]
  xor ecx, ecx
  mov cx, word [fat_header.reserved_sectors]

  call allocate_and_load

  mov dword [pointers.fat], ebx

  mov ax, word [fat_header.root_dir_entries]
  dec ax
  shr ax, 4
  inc ax

  xor ecx, ecx
  mov cx, word [fat_header.reserved_sectors]
  add cx, word [fat_header.sectors_per_fat]
  jnc .no_carry
  or ecx, 0x0001_0000
.no_carry:

  call allocate_and_load

  mov dword [pointers.root_dir], ebx

  xor edx, edx
  mov dx, word [fat_header.root_dir_entries]
  shl edx, 5
  add edx, ebx

find_kernel:
  mov eax, 0x6e_72_65_6b;kern
  mov ecx, 0x20_20_6c_65;el
  mov ebx, dword [pointers.root_dir]

.loop:
  test dword [ebx], eax
  jne .next
  test dword [ebx + 4], ecx
  jne .next
  test word [ebx + 8], 0x79_73;sy
  jne .next
  test byte [ebx + 10], 0x73;s
  je .found

.next:
  add ebx, 32
  test ebx, edx
  jne .loop
  jmp halt

.found:
  mov byte [allocate_and_load.load], 0xc3;ret

  mov eax, dword [ebx + 0x1c]
  dec eax
  shr eax, 9
  inc eax

  test eax, 0xffff_0000
  jnz halt

  push word [ebx + 0x1a]

  call allocate_and_load

  mov dword [pointers.kernel], ebx

  mov ax, word [fat_header.root_dir_entries]
  dec ax
  shr ax, 4
  dec ax
  add ax, word [fat_header.reserved_sectors]
  jc halt
  add ax, word [fat_header.sectors_per_fat]
  jc halt
  mov word [fat_header.hidden_sectors + 2], ax

load_kernel:
  mov word [dap.length], 0x0001
  mov word [dap.offset], bx
  xor bx, bx
  shr ebx, 4
  jmp .over_sig

times $$ + 0x01fe - $ db 0
dw 0xaa55

.over_sig:
  test ebx, 0xffff_0000
  jnz halt
  mov word [dap.segment], bx

  xor edx, edx
  pop dx
  mov word [dap.start + 2], 0x0000
  mov ah, 0x42
  mov si, dap
.loop:
  mov cx, dx
  add cx, word [fat_header.hidden_sectors + 2]
  jc halt

  mov word [dap.start], cx

  int 0x13

  mov ebx, dword [pointers.fat]
  add ebx, edx
  add ebx, edx
  add ebx, edx

  xor edx, edx
  mov dx, word [ebx + 1]
  shl edx, 8
  mov dl, byte [ebx]

  add word [dap.segment], 0x0020
  jc halt

  mov ecx, edx
  and ecx, 0x0ff8
  test ecx, 0x0ff8
  jne .loop

  mov ebx, dword [pointers.kernel]

  ;TODO

  cli

  lgdt [gdt]
  mov eax, cr0
  or al, 0x01
  mov cr0, eax
  jmp 0x08:kernel_start;get start from file header

gdt:
dw 0x28
dd gdt + 6
dq 0x0000_0000_0000_0000
dq 0x0040_9a00_0000_7fff;0x000000 - 0x007fff
dq 0x0040_9200_0000_7fff
dq 0x00c0_9a00_8000_0ff7;0x008000 - 0xffffff
dq 0x00c0_9200_8000_0ff7

dap:
dw 0x0010
.length  dw 0x0001
.offset  dw 0x7e00
.segment dw 0x0000
.start   dd 0x0000_0001
.start_h dd 0x0000_0000

;ecx = sector
;ax = sector count
;bx out = address of start
allocate_and_load:
  mov word [dap.length], ax
  mov dword [dap.start], ecx

  dec ax
  shr ax, 3
  inc ax

  mov bx, 0x200e
  xor cx, cx
  mov dx, 0x2200

.loop:
  add bx, 2
  test bx, dx
  je halt

  test word [bx], 0x0000
  je .eq

  xor cx, cx
  jmp .loop

.eq:
  inc cx
  test cx, ax
  jne .loop

  dec ax
  shl ax, 1
  sub bx, ax
  mov dx, bx

  mov ax, 0x0002
.mark_loop:
  mov word [bx], ax
  loop .mark_loop

  mov bx, dx
  and ebx, 0x0000_dfff
  shl ebx, 12

.load:
  mov eax, ebx
  mov word [dap.offset], ax
  shr eax, 4
  and ax, 0xf000
  mov word [dap.segment], ax

  mov ah, 0x42
  mov si, dap
  int 0x13

  ret

halt:
  cli
.hlt:
  hlt
  jmp .hlt