-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloader.asm
743 lines (559 loc) · 9.92 KB
/
loader.asm
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
org 10000h
jmp Label_Start
%include "fat12.inc"
KernelFileBase equ 0x00
KernelFileOffset equ 0x100000
KernelAddrBaseTmp equ 0x00
KernelFileOffsetTmp equ 0x7E00
memStructBuffAddr equ 0x7E00
[SECTION gtd]
LABEL_GDT: dd 0, 0
LABEL_DESC_CODE32: dd 0x0000FFFF, 0x00CF9A00
LABEL_DESC_DATA32: dd 0x0000FFFF, 0x00CF9200
GdtLen equ $ - LABEL_GDT
GdtPtr dw GdtLen - 1
dd LABEL_GDT
selCode32 equ LABEL_DESC_CODE32 - LABEL_GDT
selData32 equ LABEL_DESC_DATA32 - LABEL_GDT
[SECTION gdt64]
LABEL_GDT64: dq 0x0000000000000000
LABEL_DESC_CODE64: dq 0x0020980000000000
LABEL_DESC_DATA64: dq 0x0000920000000000
GdtLen64 equ $ - LABEL_GDT64
GdtPtr64 dw GdtLen64 - 1
dd LABEL_GDT64
selCode64 equ LABEL_DESC_CODE64 - LABEL_GDT64
selData64 equ LABEL_DESC_DATA64 - LABEL_GDT64
[SECTION .s16]
[BITS 16]
Label_Start:
mov ax, cs
mov ds, ax
mov es, ax
mov ax, 0x00
mov ss, ax
mov sp, 0x7c00
; display loader start
mov ax, 1301h
mov bx, 000fh
mov dx, 0200h ; row 2
mov cx, 12
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, startLoaderMsg
int 10h
; open A20
push ax
in al, 92h
or al, 00000010b
out 92h, al
pop ax
cli
lgdt [GdtPtr]
mov eax, cr0
or eax, 1
mov cr0, eax
mov ax, selData32
mov fs, ax
mov eax, cr0
and al, 11111110b
mov cr0, eax
sti
; reset floppy
xor ah, ah
xor dl, dl
int 13h
; Kernel searcher
mov word [SecNo], SecNumOfRootDirStart
Label_Search_Begin:
cmp word [RootDirSizeForLoop], 0
jz Label_No_Kernel_Bin
dec word [RootDirSizeForLoop]
;set arg for read sector
xor ax, ax
mov es, ax
mov bx, 8000h
mov ax, [SecNo]
mov cl, 1
call Func_ReadSectors
mov si, KernelFileName
mov di, 8000h
cld
; set dir num
mov dx, 10h
Label_Search_KernelBin:
cmp dx, 0
jz Label_Goto_Next_Sec_In_Root_Dir
dec dx
;set fileName size
mov cx, 11
Label_Cmp_FileName:
cmp cx, 0
jz Label_FileFound
dec cx
lodsb
cmp al, byte [es:di]
jz Label_Go_On
jmp Label_Diff
Label_Go_On:
inc di
jmp Label_Cmp_FileName
Label_Diff:
and di, 0ffe0h
add di, 20h
mov si, KernelFileName
; search the next dir
jmp Label_Search_KernelBin
Label_Goto_Next_Sec_In_Root_Dir:
add word [SecNo], 1
jmp Label_Search_Begin
; Kernel not found exception
Label_No_Kernel_Bin:
mov ax, 1301h
mov bx, 008ch
mov dx, 0300h
mov cx, 22
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, noKernelMsg
int 10h
jmp $
; loader loader
Label_FileFound:
mov ax, rootDirSecs
and di, 0ffe0h
add di, 01ah
mov cx, word [es:di]
push cx
add cx, ax
add cx, SecBal
mov ax, KernelAddrBaseTmp
mov es, ax
mov bx, KernelFileOffsetTmp
mov ax, cx
Label_Loading_File:
; print Loading
push ax
push bx
mov ah, 0eh
mov al, '.'
mov bl, 0fh
int 10h
pop bx
pop ax
mov cl, 1
call Func_ReadSectors
pop ax
;
push cx
push eax
push fs
push edi
push ds
push esi
mov cx, 200h
mov ax, KernelFileBase
mov fs, ax
mov edi, dword [KernelFileCountOffset]
mov ax, KernelAddrBaseTmp
mov ds, ax
mov esi, KernelFileOffsetTmp
Label_Mov_Kernel:
mov al, byte [ds:esi]
mov byte [fs:edi], al
inc esi
inc edi
loop Label_Mov_Kernel
mov eax, 0x1000
mov ds, eax
mov dword [KernelFileCountOffset], edi
pop esi
pop ds
pop edi
pop fs
pop eax
pop cx
;
call Func_GetFATEntry
cmp ax, 0fffh
jz Label_File_Loaded
push ax
mov dx, rootDirSecs
add ax, dx
add ax, SecBal
jmp Label_Loading_File
Label_File_Loaded:
mov ax, 0B800h
mov gs, ax
mov ah, 0Fh
mov al, 'G'
mov [gs:((80 * 0 + 39) * 2)], ax
KillMotor:
push dx
mov dx, 03F2h
mov al, 0
out dx, al
pop dx
; get memory address size type
; readMsgWrite
mov ax, 1301h
mov bx, 000Fh
mov dx, 0400h ; row 4
mov cx, 44
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, startGetMemStructMsg
int 10h
mov ebx, 0
xor ax, ax
mov es, ax
mov di, memStructBuffAddr
Label_Get_Mem_Struct:
mov eax, 0x0E820
mov ecx, 20
mov edx, 0x534D4150
int 15h
jc Label_Get_Mem_Failed
add di, 20
inc dword [memStructNumber]
cmp ebx, 0
jne Label_Get_Mem_Struct
jmp Label_Get_Mem_OK
Label_Get_Mem_Failed:
mov dword [memStructNumber], 0
mov ax, 1301h
mov bx, 008Ch
mov dx, 0500h ;row 5
mov cx, 23
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, getMemStructErrMsg
int 10h
jmp $
Label_Get_Mem_OK:
mov ax, 1301h
mov bx, 000Fh
mov dx, 0600h
mov cx, 29
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, getMemStructOKMsg
int 10h
; set the SVGA mode
; print msg
mov ax, 1301h
mov bx, 000Fh
mov dx, 0800h ; row 8
mov cx, 23
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAInfoVBEMsg
int 10h
xor ax, ax
mov es, ax
mov di, 8000h
mov ax, 4F00h
int 10h
cmp ax, 004Fh
jz .KO
; failed
mov ax, 1301h
mov bx, 008ch
mov dx, 0900h ; row 9
mov cx, 23
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAInfoVBEErrMsg
int 10h
jmp $
.KO:
mov ax, 1301h
mov bx, 000Fh
mov dx, 0A00h ; row 10
mov cx, 29
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAInfoVBEOKMsg
int 10h
; get SVGAModeInfo
mov ax, 1301h
mov bx, 000Fh
mov dx, 0C00h ; row 12
mov cx, 24
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAModeInfoMsg
int 10h
xor ax, ax
mov es, ax
mov si, 800eh
mov esi, dword[es:si]
mov edi, 8200h
Label_SVGA_Mode_Info_Get:
mov cx, word [es:esi]
; display SVGA Mode Info
push ax
xor ax, ax
mov al, ch
call Func_DispAL
mov ax, 00h
mov al, cl
call Func_DispAL
pop ax
cmp cx, 0xffff
jz Label_SVGA_Mode_Info_Finish
mov ax, 4F01h
int 10h
cmp ax, 004Fh
jnz Label_SVGA_Mode_Info_Failed
inc dword [SVGAModeCounter]
add esi, 2
add edi, 100h
jmp Label_SVGA_Mode_Info_Get
Label_SVGA_Mode_Info_Failed:
mov ax, 1301h
mov bx, 008Ch
mov dx, 0D00h ; row 13
mov cx, 24
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAModeInfoErrMsg
int 10h
jmp $
Label_SVGA_Mode_Info_Finish:
mov ax, 1301h
mov bx, 000Fh
mov dx, 0E00h ; row 14
mov cx, 30
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, SVGAModeInfoOKMsg
int 10h
; set SVGA mode
mov ax, 4F02h
mov bx, 4180h
int 10h
cmp ax, 004Fh
jz Label_SET_SVGA_SUCESS
jmp $
Label_SET_SVGA_SUCESS:
; init IDT GTD and goto protect mode
cli
db 0x66
lgdt [GdtPtr]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword selCode32:GO_TO_TMP_Protect
[SECTION .32]
[BITS 32]
GO_TO_TMP_Protect:
; go to tmp long mode
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov ss, ax
mov esp, 7E00h
call support_long_mode
test eax, eax
jz no_support
mov dword [0x90000], 0x91007
mov dword [0x90800], 0x91007
mov dword [0x91000], 0x92007
mov dword [0x92000], 0x000083
mov dword [0x92008], 0x200083
mov dword [0x92010], 0x400083
mov dword [0x92018], 0x600083
mov dword [0x02020], 0x800083
mov dword [0x92028], 0xa00083
; load GDTR
db 0x66
lgdt [GdtPtr64]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, 7E00h
; open PAE
mov eax, cr4
bts eax, 5
mov cr4, eax
; load cr3
mov eax, 0x90000
mov cr3, eax
mov ecx, 0C0000080h
rdmsr
bts eax, 8
wrmsr
; open PE and paging
mov eax, cr0
bts eax, 0
bts eax, 31
mov cr0, eax
jmp selCode64:KernelFileOffset
support_long_mode:
mov eax, 0x80000000
cpuid
cmp eax, 0x80000001
setnb al
jb support_long_mode_done
mov eax, 0x00000001
cpuid
bt edx, 29
setc al
support_long_mode_done:
movzx eax, al
ret
no_support:
jmp $
[SECTION .s16lib]
[BITS 16]
; read a sector
; ax=start sector(LBA)
; cl=sector num to read
; es:bx=buffer location
Func_ReadSectors:
push bp
mov bp, sp
sub esp, 2
mov byte [bp - 2], cl
push bx
;LBA to CHS
mov bl, [BPB_SecPerTrk]
div bl
inc ah
mov cl, ah
mov dh, al
shr al, 1
mov ch, al
and dh, 1
pop bx
mov dl, [BS_DrvNum]
Label_Reading_Sector:
mov ah, 2
mov al, byte [bp - 2]
int 13h
jc Label_Reading_Sector
add esp, 2
pop bp
ret
; get FAT12 Entry
; ah=FATEntry
Func_GetFATEntry:
push es
push bx
push ax
xor ax, ax
mov es, ax
pop ax
mov byte [Odd], 0
mov bx, 3
mul bx
mov bx, 2
div bx
cmp dx, 0
jz Label_Even
mov byte [Odd], 1
Label_Even:
xor dx, dx
mov bx, [BPB_BytesPerSec]
div bx
push dx
mov bx, 8000h
add ax, SecNumOfFAT1Start
mov cl, 2
call Func_ReadSectors
pop dx
add bx, dx
mov ax, [es:bx]
cmp byte [Odd], 1
jnz Label_Even_2
shr ax, 4
Label_Even_2:
and ax, 0fffh
pop bx
pop es
ret
; display num in al
; al=the num you want to display
Func_DispAL:
push ecx
push edx
push edi
mov edi, [displayPos]
mov ah, 0Fh
mov dl, al
shr al, 4
mov ecx, 2
.begin:
and al, 0Fh
cmp al, 9
ja .1
add al, '0'
jmp .2
.1:
sub al, 0Ah
add al, 'A'
.2:
mov [gs:edi], ax
add edi, 2
mov al, dl
loop .begin
mov [displayPos], edi
pop edi
pop edx
pop ecx
ret
; tmp IDT
IDT:
times 0x50 dq 0
IDT_END:
IDT_POINTER:
dw IDT_END - IDT - 1
dw IDT
; tmp variable
RootDirSizeForLoop dw rootDirSecs
SecNo dw 0
Odd db 0
KernelFileCountOffset dd KernelFileOffset
memStructNumber dd 0
SVGAModeCounter dd 0
displayPos dd 0
; msgs
startLoaderMsg: db "Start Loader"
noKernelMsg: db "ERROR: NO KERNAL Found"
KernelFileName: db "KERNEL BIN", 0
startGetMemStructMsg: db "Start Get Memory Structure."
getMemStructErrMsg: db "Get Memory Struct ERROR"
getMemStructOKMsg: db "Get Memory Struct SUCESSFUL!"
SVGAInfoVBEMsg: db "Start Get SVGA VBE Info"
SVGAInfoVBEErrMsg: db "Get SVGA VBE Info ERROR"
SVGAInfoVBEOKMsg: db "Get SVGA VBE Info SUCESSFUL!"
SVGAModeInfoMsg: db "Start Get SVGA Mode Info"
SVGAModeInfoErrMsg: db "Get SVGA Mode Info ERROR"
SVGAModeInfoOKMsg: db "Get SVGA Mode Info SUCESSFUL!"