-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
1069 lines (899 loc) · 23.2 KB
/
Copy pathkernel.asm
File metadata and controls
1069 lines (899 loc) · 23.2 KB
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; kernel.asm
; CircleOS Kernel
[BITS 16] ; assemble these instructions for 16-bit mode
[ORG 0x7E00] ; this code lives at 0x7e00
BOOT_INFO_ADDR equ 0x0500
BOOT_SIG0_OFF equ BOOT_INFO_ADDR + 0
BOOT_SIG1_OFF equ BOOT_INFO_ADDR + 1
BOOT_VER_OFF equ BOOT_INFO_ADDR + 2
BOOT_DRIVE_OFF equ BOOT_INFO_ADDR + 3
BOOT_KSECT_OFF equ BOOT_INFO_ADDR + 4
SYSCALL_INT equ 0x80
SYS_PUTC equ 0x01
SYS_PUTS equ 0x02
SYS_NEWLINE equ 0x03
SYS_GETC equ 0x04
SYS_CLEAR equ 0x05
SYS_RUN equ 0x06
SYS_READ_RAW equ 0x07
SYS_WRITE_RAW equ 0x08
%ifndef DEBUG
%define DEBUG 0
%endif
%ifndef FS_TABLE_SECTOR
FS_TABLE_SECTOR equ 21
%endif
%ifndef SHELL_SECTORS
SHELL_SECTORS equ 2
%endif
%ifndef BADAPPLE_SECTOR
BADAPPLE_SECTOR equ 25
%endif
%ifndef BADAPPLE_SECTORS
BADAPPLE_SECTORS equ 1
%endif
PROG_TABLE_ADDR equ 0x0600
PROG_TABLE_MAX_ENTRIES equ 16
start:
mov ax, 0
mov ds, ax
mov es, ax
cmp byte [BOOT_SIG0_OFF], 'C'
jne .boot_info_bad
cmp byte [BOOT_SIG1_OFF], 'B'
jne .boot_info_bad
mov al, [BOOT_DRIVE_OFF]
mov [kernel_boot_drive], al
call enable_a20
call load_boot_gdt
call install_syscall_vector
call console_clear
call show_boot_logo
call delay_5s
call console_clear
call load_program_table
cmp ah, 0
jne .prog_table_bad
; Use kernel as backend and start userspace shell directly.
call launch_shell
jmp .shell_loop
.boot_info_bad:
mov si, boot_info_bad_msg ; boot info bad handler, alerts if boot info bad
call console_puts
jmp halt
.prog_table_bad:
mov si, prog_table_bad_msg
call console_puts
call console_newline
jmp halt
.shell_loop:
mov si, prompt ; move prompt (arcsh >)
call console_puts ; print prompt
; read command from keyboard
xor cx, cx ; cx=0, start at first byte
mov bx, command_buf ; bx points to command buffer start
.read_loop:
; mov ah, 0x00 ; bios wait for key press and return
; int 0x16 ; bios interrupt to return key press
call kbd_getc
; check for enter key
cmp al, 13 ; key press goes into AL for the ascii code, ascii code of carriage return is 13 or 0D
je .command_ready ; enter means command is finished, so jump to command execution
cmp al, 8 ; check for backspace character
je .backspace ; jump if ZF is set, if AL = 8 then the previous line set ZF so jump to .backspace jump target
; print character via BIOS TTY by running int 10 and setting AH to 0E
; mov ah, 0x0E
; int 0x10
call console_putc
; store typed character in command buffer at [BX+CX], bx is base and cx is index
mov si, cx
mov byte [bx + si], al
inc cx
; limit input length to 32 bytes
cmp cx, 32
jl .read_loop
; if at or over 32 keep reading but ignore storage
jmp .read_loop
.backspace:
cmp cx, 0 ; is the cursor already at the start?
je .read_loop ; if yes, exit backspace loop and go to read loop
mov ah, 0x0E
mov al, 8 ; ASCII backspace
int 0x10
mov al, ' ' ; print ' ' to cover/erase old character
int 0x10
mov al, 8 ; backspace again :)
int 0x10
dec cx
jmp .read_loop
.command_ready:
mov si, cx
mov byte [bx + si], 0 ;null terminate the command so routines know where it ends
call console_newline
; Exact command dispatch
cmp byte [command_buf], 0 ; compare the first byte of the command buffer to 0, if it's 0 then the user just hit enter with no command
je .shell_loop
; help
mov si, command_buf ; SI points to command buffer start, which has the user input
mov di, cmd_help_str ; DI points to the "help" string we want to compare against
call str_eq ; str_eq returns 1 in AL if the strings are equal, 0 otherwise
cmp al, 1 ; if the strings are equal, ZF is set and AL=1, so jump to help command handler
je .cmd_help ; jump to help command
; csh
mov si, command_buf
mov di, cmd_csh_str
call str_eq
cmp al, 1
je .cmd_csh
; Unknown command
mov si, unknown_msg
call console_puts
call console_newline
jmp .shell_loop
.cmd_help:
mov si, help_msg
call console_puts
call console_newline
jmp .shell_loop
.cmd_csh:
call launch_shell
jmp .shell_loop
kernel_boot_drive:
db 0
halt:
hlt ; halt the cpu
jmp halt ; infinite loop just in case
; ----------------------------------
; Kernel service wrappers for routines
;-----------------------------------
; input al = character
; clobbers: ah
console_putc:
mov ah, 0x0E
int 0x10
ret
; Input: ds:si = null terminated string
; clobbers: AL, AH, SI
console_puts:
.loop:
lodsb
cmp al, 0
je .done
call console_putc
jmp .loop
.done:
ret
; prints CRLF
; clobbers: al, ah
console_newline:
mov al, 13
call console_putc
mov al, 10
call console_putc
ret
; clears text mode screen and moves cursor to top-left
console_clear:
mov ah, 0x06
mov al, 0
mov bh, 0x07
mov cx, 0
mov dx, 0x184F
int 0x10
mov ah, 0x02
mov bh, 0
mov dx, 0
int 0x10
ret
; wait for keypress and return it
; output al = ascii, ah = scan code
; clobbers ah and al
kbd_getc:
mov ah, 0x00
int 0x16
ret
; enable_a20
; Enables A20 line using port 0x92 fast A20 gate.
; This is required before moving to protected mode memory layouts.
enable_a20:
in al, 0x92
or al, 0x02
out 0x92, al
ret
; load_boot_gdt
; Loads a minimal flat GDT used for a future protected-mode switch.
load_boot_gdt:
lgdt [gdt_descriptor]
ret
show_boot_logo:
call console_newline
call console_newline
call console_newline
mov si, logo_line_01
call console_puts_logo
call console_newline
mov si, logo_line_02
call console_puts_logo
call console_newline
mov si, logo_line_03
call console_puts_logo
call console_newline
mov si, logo_line_04
call console_puts_logo
call console_newline
mov si, logo_line_05
call console_puts_logo
call console_newline
mov si, logo_line_06
call console_puts_logo
call console_newline
mov si, logo_line_07
call console_puts_logo
call console_newline
mov si, logo_line_08
call console_puts_logo
call console_newline
mov si, logo_line_09
call console_puts_logo
call console_newline
mov si, logo_line_10
call console_puts_logo
call console_newline
mov si, logo_line_11
call console_puts_logo
call console_newline
mov si, logo_line_12
call console_puts_logo
call console_newline
mov si, logo_line_13
call console_puts_logo
call console_newline
mov si, logo_line_14
call console_puts_logo
call console_newline
mov si, logo_line_15
call console_puts_logo
call console_newline
mov si, logo_line_16
call console_puts_logo
call console_newline
mov si, logo_line_17
call console_puts_logo
call console_newline
mov si, logo_line_18
call console_puts_logo
call console_newline
call console_newline
ret
; console_puts_logo
; Input: DS:SI = null-terminated logo row with '#'(on) and ' '(off)
; Renders '#' as CP437 full block (0xDB)
console_puts_logo:
.logo_loop:
lodsb
cmp al, 0
je .logo_done
cmp al, '#'
jne .logo_emit
mov al, 0xDB
.logo_emit:
call console_putc
jmp .logo_loop
.logo_done:
ret
; delay_5s
; BIOS wait: CX:DX microseconds = 5,000,000 (0x004C4B40)
delay_5s:
mov ah, 0x86 ; BIOS wait function
mov cx, 0x004C ; high word of 5,000,000 microseconds
mov dx, 0x4B40 ; low word of 5,000,000 microseconds
int 0x15 ; call BIOS
ret
install_syscall_vector:
cli
mov word [SYSCALL_INT * 4], syscall_handler
mov word [SYSCALL_INT * 4 + 2], 0
sti
ret
syscall_handler:
push bx
push cx
push dx
push si
push di
push bp
push ds
push es
cmp ah, SYS_PUTC
je .sys_putc
cmp ah, SYS_PUTS
je .sys_puts
cmp ah, SYS_NEWLINE
je .sys_newline
cmp ah, SYS_GETC
je .sys_getc
cmp ah, SYS_CLEAR
je .sys_clear
cmp ah, SYS_RUN
je .sys_run
cmp ah, SYS_READ_RAW
je .sys_read_raw
cmp ah, SYS_WRITE_RAW
je .sys_write_raw
mov ah, 0xFF
jmp .done
.sys_putc:
call console_putc
xor ah, ah
jmp .done
.sys_puts:
call console_puts
xor ah, ah
jmp .done
.sys_newline:
call console_newline
xor ah, ah
jmp .done
.sys_getc:
call kbd_getc
jmp .done
.sys_clear:
call console_clear
xor ah, ah
jmp .done
.sys_run:
call run_named_program
jmp .done
.sys_read_raw:
call disk_read_chs
jc .sys_read_raw_fail
xor ah, ah
jmp .done
.sys_read_raw_fail:
mov ah, 2
jmp .done
.sys_write_raw:
call disk_write_chs
jc .sys_write_raw_fail
xor ah, ah
jmp .done
.sys_write_raw_fail:
mov ah, 2
jmp .done
.done:
pop es
pop ds
pop bp
pop di
pop si
pop dx
pop cx
pop bx
iret
; ----------------------------------
; Disk wrapper API (BIOS-backed)
; ----------------------------------
; disk_read_chs
; Inputs:
; AL = sector count
; CL = logical sector number (1-based)
; ES:BX = destination buffer
; Uses:
; DL = [kernel_boot_drive]
; Returns:
; CF clear on success
; CF set on error, AH = BIOS status
; Clobbers:
; DL, SI
;
; Optional reliability:
; one retry after INT 13h reset (AH=00h)
disk_read_chs:
; Save requested geometry/count so we can retry with the same inputs
mov [dr_count], al
mov [dr_lba], cl
mov [dr_dest], bx
mov byte [dr_retries], 1 ; one retry after first failure
.read_try:
; Restore inputs for this attempt
mov cl, [dr_lba]
call lba_to_chs
mov al, [dr_count]
mov bx, [dr_dest]
mov dl, [kernel_boot_drive]
mov ah, 0x02 ; BIOS read sectors
int 0x13
jnc .ok ; CF=0 -> success
; Failure: AH has BIOS status code
mov [dr_last_status], ah
; If no retry left, return failure with AH preserved
cmp byte [dr_retries], 0
je .fail
; Consume retry and reset disk system, then try again
dec byte [dr_retries]
mov ah, 0x00 ; BIOS reset disk system
mov dl, [kernel_boot_drive]
int 0x13
jmp .read_try
.ok:
clc ; explicit success
ret
.fail:
mov ah, [dr_last_status] ; return last BIOS error in AH
stc ; explicit failure
ret
; disk_write_chs
; Inputs:
; AL = sector count
; CL = logical sector number (1-based)
; ES:BX = source buffer
; Returns:
; CF clear on success
; CF set on error, AH = BIOS status
disk_write_chs:
mov [dr_count], al
mov [dr_lba], cl
mov [dr_dest], bx
mov byte [dr_retries], 1
.write_try:
mov cl, [dr_lba]
call lba_to_chs
mov al, [dr_count]
mov bx, [dr_dest]
mov dl, [kernel_boot_drive]
mov ah, 0x03 ; BIOS write sectors
int 0x13
jnc .write_ok
mov [dr_last_status], ah
cmp byte [dr_retries], 0
je .write_fail
dec byte [dr_retries]
mov ah, 0x00
mov dl, [kernel_boot_drive]
int 0x13
jmp .write_try
.write_ok:
clc
ret
.write_fail:
mov ah, [dr_last_status]
stc
ret
; lba_to_chs
; Input: CL = logical sector (1-based)
; Output: CH = cylinder, DH = head, CL = sector (1-based)
; Uses: AX, DX
lba_to_chs:
xor ax, ax
mov al, cl
dec al ; convert to zero-based LBA
xor ah, ah
mov dl, 36 ; sectors per cylinder (18*2)
div dl ; AL=cylinder, AH=remainder in cylinder
mov ch, al
mov al, ah
xor ah, ah
mov dl, 18 ; sectors per head/track
div dl ; AL=head, AH=sector index
mov dh, al
mov cl, ah
inc cl ; back to 1-based sector
ret
; str_eq
; Input: DS:SI = string A, DS:DI = string B
; Output: AL = 1 if equal, 0 if not
; Clobbers: AL, BL
str_eq:
.eq_loop:
mov al, [si]
mov bl, [di]
cmp al, bl
jne .no
cmp al, 0
je .yes
inc si
inc di
jmp .eq_loop
.yes:
mov al, 1
ret
.no:
mov al, 0
ret
; str_startswith
; Input: DS:SI = full string, DS:DI = prefix
; Output: AL = 1 if SI starts with DI, else 0
; Clobbers: AL, BL
str_startswith:
.sw_loop:
mov al, [di] ; get next byte of prefix
cmp al, 0 ; if prefix byte is 0, we've reached the end and it's a match
je .yes
mov bl, [si] ; get next byte of full string
cmp bl, al ; compare to prefix byte
jne .no
inc si ; move to next byte of full string
inc di ; move to next byte of prefix
jmp .sw_loop
.yes:
mov al, 1
ret
.no:
mov al, 0
ret
launch_shell:
; Load csh to 0000:9000
mov ax, 0
mov es, ax
mov bx, 0x9000
mov al, SHELL_SECTORS ; shell sector count from build
mov ch, 0 ; cylinder 0
mov cl, [BOOT_KSECT_OFF] ; kernel sectors from boot info
add cl, 2 ; shell starts after boot(1) + kernel
mov dh, 0 ; head 0
call disk_read_chs
jc .load_fail
call 0x9000 ; run shell, return to kernel when shell does RET
ret
.load_fail:
mov si, shell_load_fail_msg
call console_puts
call console_newline
ret
; load_program_table
; Reads a tiny filesystem program table from FS_TABLE_SECTOR into PROG_TABLE_ADDR.
; Output: AH = 0 success, 1 read fail, 2 bad magic, 3 bad entry count, 4 bad layout
load_program_table:
mov ax, 0
mov es, ax
mov bx, PROG_TABLE_ADDR
mov al, 1
mov ch, 0
mov cl, FS_TABLE_SECTOR
mov dh, 0
call disk_read_chs
jc .read_fail
; Compare the magic bytes to make sure it's a valid program table, if any of the bytes don't match then jump to .bad_magic
cmp byte [PROG_TABLE_ADDR + 0], 'C'
jne .bad_magic
cmp byte [PROG_TABLE_ADDR + 1], 'F'
jne .bad_magic
cmp byte [PROG_TABLE_ADDR + 2], 'S'
jne .bad_magic
cmp byte [PROG_TABLE_ADDR + 3], '1'
jne .bad_magic
mov al, [PROG_TABLE_ADDR + 4] ; get program count from table header
cmp al, PROG_TABLE_MAX_ENTRIES ; validate program count is not too high
ja .bad_count
mov [prog_table_count], al ; save program count for later use
call validate_program_table_layout ; ensure program entries have valid start/count and don't overlap FS table
cmp ah, 0 ; if layout is valid, AH=0, if not AH=4
jne .bad_layout
mov byte [prog_table_loaded], 1
%if DEBUG
mov si, debug_loaded_msg ; debug print program count after loading table
call console_puts
mov al, [prog_table_count]
cmp al, 10
jb .dbg_one_digit
mov al, '1'
call console_putc
mov al, [prog_table_count]
sub al, 10
add al, '0'
call console_putc
jmp .dbg_count_done
.dbg_one_digit:
add al, '0'
call console_putc
.dbg_count_done:
mov si, debug_newline
call console_puts
%endif
xor ah, ah
ret
.read_fail:
mov byte [prog_table_loaded], 0
mov ah, 1
ret
.bad_magic:
mov byte [prog_table_loaded], 0
mov ah, 2
ret
.bad_count:
mov byte [prog_table_loaded], 0
mov ah, 3
ret
.bad_layout:
mov byte [prog_table_loaded], 0
mov ah, 4
ret
; validate_program_table_layout
; Ensures every entry has valid start/count and does not overlap FS table sector.
; Output: AH = 0 valid, 4 invalid
validate_program_table_layout:
mov byte [pt_index], 0
.v_loop:
mov bl, [pt_index]
cmp bl, [prog_table_count]
jae .v_ok
xor ax, ax
mov al, bl
shl ax, 4
mov di, PROG_TABLE_ADDR + 16
add di, ax
mov al, [di + 8] ; start sector
mov ah, [di + 9] ; sector count
cmp al, 1
jb .v_bad
cmp ah, 0
je .v_bad
mov bl, al
add bl, ah
jc .v_bad
dec bl ; end sector
mov dl, FS_TABLE_SECTOR
cmp dl, al
jb .v_next
cmp dl, bl
jbe .v_bad
.v_next:
inc byte [pt_index]
jmp .v_loop
.v_ok:
xor ah, ah
ret
.v_bad:
mov ah, 4
ret
; run_named_program
; Input: DS:SI = null-terminated program name
; Output: AH = status (0=ok, 1=unknown name, 2=load fail, 3=fs unavailable)
run_named_program:
mov [run_name_ptr], si ; move the pointer to the program name into a known location for later use
mov si, [run_name_ptr] ; move the pointer back into SI for string operations
mov di, cmd_badapple_str ; point DI to the "badapple" string so we can compare against it
call str_eq ; run the
cmp al, 1
je .run_badapple
cmp byte [prog_table_loaded], 1
jne .fs_unavailable
xor bx, bx
%if DEBUG
mov si, debug_searching
call console_puts
mov si, [run_name_ptr]
call console_puts
mov si, debug_newline
call console_puts
%endif
mov si, [run_name_ptr]
.search_loop:
cmp bl, [prog_table_count]
jae .unknown
; DI = PROG_TABLE_ADDR + 16 + (index * PROG_ENTRY_SIZE)
xor ax, ax
mov al, bl
shl ax, 4
mov di, PROG_TABLE_ADDR + 16
add di, ax
mov si, [run_name_ptr]
push bx
call str_eq
pop bx
cmp al, 1
je .found
inc bl
jmp .search_loop
.found:
; Recompute entry pointer in DI
xor ax, ax
mov al, bl
shl ax, 4
mov di, PROG_TABLE_ADDR + 16
add di, ax
; Entry layout:
; +0..+7 name[8]
; +8 start_sector
; +9 sector_count
; +10..11 load_offset
; +12..13 entry_offset
; +14 entry_type (1=program, 2=text)
cmp byte [di + 14], 1
jne .unknown
; Load program sector-by-sector to avoid BIOS track-crossing issues
; with large AL counts in a single INT 13h read.
mov ax, 0
mov es, ax
mov bx, [di + 10] ; destination offset
xor cx, cx
mov cl, [di + 9] ; sector count
xor dx, dx
mov dl, [di + 8] ; start sector (logical, 1-based)
.prog_load_loop:
cmp cx, 0
je .prog_loaded
push cx
push dx
mov ax, dx
call disk_read_sector_u16 ; reads 1 sector to ES:BX
pop dx
jc .prog_load_fail
add bx, 0x0200 ; next destination sector
jnc .prog_no_carry
mov ax, es
add ax, 0x0020
mov es, ax
.prog_no_carry:
inc dl ; next logical sector
pop cx
dec cx
jmp .prog_load_loop
.prog_load_fail:
pop cx
jmp .load_fail
.prog_loaded:
mov bx, [di + 10]
add bx, [di + 12]
call bx
xor ah, ah
ret
.unknown:
mov ah, 1
ret
.load_fail:
mov ah, 2
ret
.run_badapple:
mov ax, 0
mov es, ax
mov bx, 0xA000
mov cx, BADAPPLE_SECTORS
mov di, BADAPPLE_SECTOR
.badapple_load_loop:
cmp cx, 0
je .badapple_loaded
push cx
push di
mov ax, di
call disk_read_sector_u16
pop di
jc .load_fail
add bx, 0x0200
jnc .badapple_no_carry
mov ax, es
add ax, 0x0020
mov es, ax
.badapple_no_carry:
inc di
pop cx
dec cx
jmp .badapple_load_loop
.badapple_loaded:
call 0xA000
xor ah, ah
ret
.fs_unavailable:
mov ah, 3
ret
; disk_read_sector_u16
; Input: AX = logical sector number (1-based), ES:BX = destination buffer
; Output: CF clear on success, CF set on error
disk_read_sector_u16:
push bx
dec ax
xor dx, dx
mov bx, 36
div bx
mov ch, al
mov ax, dx
xor dx, dx
mov bx, 18
div bx
mov dh, al
mov cl, dl
inc cl
mov al, 1
mov ah, 0x02
mov dl, [kernel_boot_drive]
int 0x13
pop bx
ret
; ----------------------------------
; disk_read_chs scratch state (kernel globals)
; ----------------------------------
dr_count:
db 0
dr_lba:
db 0
dr_dest:
dw 0
dr_retries:
db 0
dr_last_status:
db 0
prog_table_loaded:
db 0
prog_table_count:
db 0
run_name_ptr:
dw 0
pt_index:
db 0
; --------------------------------DATA SECTION------------------------------------
; Minimal flat GDT for future protected-mode transition:
; selector 0x08 -> code segment
; selector 0x10 -> data segment
gdt_start:
dq 0x0000000000000000
dq 0x00CF9A000000FFFF
dq 0x00CF92000000FFFF
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
logo_line_01:
db " ###### ", 0
logo_line_02:
db " #### ## #### ", 0
logo_line_03:
db " ## ## ## ", 0
logo_line_04:
db " ## ## ## ## ", 0