Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/floppy/boot.asm
blob: 6200196be29c6fd2f24e14599f24f93ac614b67d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
; stage 1

; 16-bit real-mode
[bits 16]

; BIOS always loads us to this location
[org 0x7c00]

; export a map of boot loader symbols for debugging	
[map symbols boot.map]

stage1:

; no interrupts, initializing segment registers and a real mode stack
; growing downwards from 0x7c00 where the stage 1 code and data lives
	cli
	cld
	xor ax, ax
	mov ds, ax
	mov ss, ax
	mov sp, 0x7c00
	mov bp, sp
	sti

; dl contains the boot drive, primarily because that's the last function
; called by the BIOS MBR loader, we remember that for loading additional
; blocks from the boot medium
	mov [BOOT_DRIVE], dl

; print greeting message
	mov si, MESSAGE_GREETING
	call print_string

; size of stage 2 in sectors (3584-512 bytes, size of boot.img minus stage 1)
NOF_SECTORS_STAGE2 equ 6

; load stage 2 with simple load method to 0x7e00 (directly
; after the boot sector), we assume stage 2 fits on a track
	mov ah, 0x02			; read sectors from drive
	mov al, NOF_SECTORS_STAGE2	; read sectors of stage 2
	mov ch, 0			; select first cylinder
	mov dl, [BOOT_DRIVE]		; drive to read from
	mov dh, 0			; first head
	mov cl, 2			; second sector after boot sector
	mov bx, 0			; where to store the data
	mov es, bx
	mov bx, 0x7e00			; 512 bytes after first sector		
	int 0x13
	jc .read_error
	jmp .read_ok
	
.read_error:
	mov di, ERR_DISK
	mov ax, 0x01
	call print_error
.read_ok:
	cmp al, NOF_SECTORS_STAGE2	; correct number of sectors read?
	jne .short_read_error		; if not, short read
	jmp .short_read_ok
.short_read_error:
	mov di, ERR_DISK
	mov ax, 0x02
	call print_error
.short_read_ok:
	jmp stage2

; di: pointer to additional message
; ax: error code in ASCII, never returns
print_error:
	mov si, NL
	call print_string
	mov si, MESSAGE_ERROR
	call print_string
	mov si, di
	call print_string
	call print_hex_nice
	call kill_motor
	mov si, NL
	call print_string
	jmp reboot

; IN si
print_string:
	push ax
	push bx
.loop:
	lodsb
	cmp al, 0
	je .fini
	call print_char
	jmp .loop
.fini:
	pop bx
	pop ax
	ret

; IN al: character to print
; MOD ah
print_char:
	mov ah, 0x0e
	int 0x10
	ret

; IN ah: hex value to print	
print_hex_nice:
	push si
	mov si, HEX_PREFIX
	call print_string
	call print_hex
	mov si, SPACE
	call print_string	
	pop si
	ret

; IN ah: hex value to print	
print_hex:
	push bx
	push si
	mov si, HEX_TEMPLATE
	mov bx, ax
	and bx, 0x00FF
	shr bx, 4
	mov bx, [HEXABET+bx]
	mov [HEX_TEMPLATE], bl
	mov bx, ax
	and bx, 0x000F
	mov bx, [HEXABET+bx]
	mov [HEX_TEMPLATE+1], bl
	call print_string
	pop si
	pop bx
	ret

; IN: eax: value to print in hex
print_hex_dword:
	push eax
	push ebx
	mov ebx, eax
	mov eax, ebx
	shr eax, 24
	call print_hex
	mov eax, ebx
	shr eax, 16
	call print_hex
	mov eax, ebx
	shr eax, 8
	call print_hex
	mov eax, ebx
	call print_hex
	pop ebx
	pop eax
	ret

kill_motor:
	push dx
	mov dx, 0x3F2
	mov al, 0x00
	out dx, al
	pop dx
        ret

reset_drive:
	push ax
	push dx
	mov ax, 0x00
	mov dl, [BOOT_DRIVE]
	int 0x13
	pop dx
	pop ax
        ret

HEX_TEMPLATE:
	db '??', 0

HEX_PREFIX:
	db '0x', 0

HEXABET:
	db '0123456789ABCDEF'

MESSAGE_GREETING:
	db "Archlinux32 i486 floppy..", 13, 10, 0

MESSAGE_ERROR:
	db "ERR ", 0

SPACE:
	db " ", 0

NL:
	db 13, 10, 0

BOOT_DRIVE:
	db 0

; pad rest of sector with zeroes so we get 512 bytes in the end
	times 510-($-$$) db 0

; magic number of a boot sector
	dw 0xaa55

; stage 2
stage2:

; check A20 gate
	mov si, MESSAGE_CHECKING_A20
	call print_string
	call check_and_enable_A20
	cmp ax, 1
	je .A20_enabled
	mov si, MESSAGE_DISABLED
	call print_string
	mov di, ERR_A20
	mov ax, 0x01
	call print_error

.A20_enabled:
	mov si, MESSAGE_ENABLED
	call print_string

unreal_mode:
; now switching to bigger data and extra segments (so copying kernels and ramdisks
; is not overly painful)
	mov si, MESSAGE_SWITCHING_TO_UNREAL_MODE
	call print_string

; disable interrupts for now as we do mode switching and segment manipulations
	cli
	
; load GDT (global descriptor table)
	lgdt [gdt_descriptor]

; switch to protected mode
	mov eax, cr0
	or eax, 0x1
	mov cr0, eax
	
; unconditional far jump into code segment,
; wipes the instruction prefetch pipeline
	jmp $+2

; data descriptor in GDT
	mov ax, CODE_DATA_SEGMENT
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

; back to real mode
	and al, 0xFE
	mov cr0, eax

; restore segment values - now limits are removed but seg regs still work as normal
; we keep cs and ss in real mode locations as before, code is small and stack
; is not expected to get big, besides they stop working above 1 MB as SP and IP
; are used and not ESP and EIP!
	xor ax,ax 
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

; now we are in UNREAL mode, we can also reenable real mode interrupts, as the
; code segment is still small, so saving IP should work just fine
	sti
	mov si, MESSAGE_ENABLED
	call print_string

; detect disk geometry
; IN dl: drive
detect_disk_geometry:
	xor ax, ax
	mov es, ax
	mov di, ax
	mov ah, 0x08
	mov dl, [BOOT_DRIVE]
	int 0x13
	jc .error
	jmp .ok

.error:
; AH contains return code
	mov si, ERR_DISK
	call print_error

; all went well, remember and print drive parameters
.ok:
	mov si, MESSAGE_DRIVE_PARAMETERS
	call print_string
	mov ax, [BOOT_DRIVE]
	call print_hex_nice
	xor ax, ax
	mov [FLOPPY_TYPE], BYTE bl
	mov al, [FLOPPY_TYPE]
	call print_hex_nice
	add dh, 1
	mov [NOF_HEADS], BYTE dh
	add cl, 1
	mov [SECTORS_PER_CYLINDER], BYTE cl
	mov al, [NOF_HEADS]
	call print_hex_nice
	xor ax, ax
	mov al, [SECTORS_PER_CYLINDER]
	call print_hex_nice
	mov si, NL
	call print_string
	
	call reset_drive

; read now sectors after stage 2 as long as we can find
; tar index sectors containing filenames,
; depending on the filename we have to load things to different places
; (and move them to memory above 1MB)
; start to read after stage 1 (512 on sector 1) and stage 2 (starting on sector 2)
	mov [CURRENT_SECTOR], byte NOF_SECTORS_STAGE2 + 2

read_next_sector:

	mov bx, 0x0900			; where to store the data (our floppy data read buffer)
	mov es, bx
	mov bx, 0x0

; print (C/H/S) info where we are currently reading and in which state
; and how many data sectors are left in the current tar entry
	;~ jmp .no_verbose

; states
	mov al, 0x0d
	call print_char
	mov al, byte [READ_STATE]
	call print_hex
	mov al, ' '
	call print_char
	mov al, byte [KERNEL_STATE]
	call print_hex
	mov al, ' '
	call print_char

; tar data sectors to read for current file
	mov dx, word [READ_DATA_SECTORS]
	mov ax, dx
	shr ax, 8
	call print_hex
	mov ax, dx
	call print_hex	
	mov al, ' '
	call print_char
	
; disk geometry (current position on the floppy)	
	mov al, '('
	call print_char
	xor ax, ax
	mov al, byte [CURRENT_CYLINDER]
	call print_hex
	mov al, '/'
	call print_char
	xor ax, ax
	mov al, byte [CURRENT_HEAD]
	call print_hex
	mov al, '/'
	call print_char
	xor ax, ax
	mov al, byte [CURRENT_SECTOR]
	call print_hex
	mov al, ')'
	call print_char

.no_verbose:
	call read_one_sector_from_disk

; states in the main sector read loop
STATE_READ_METADATA equ 0
STATE_READ_KERNEL equ 1
STATE_READ_INITRD equ 2
STATE_KERNEL_READ_SECTOR_0 equ 1
STATE_KERNEL_READ_SECTOR_1 equ 2
STATE_KERNEL_READ_SECTORS_REAL_MODE equ 3
STATE_KERNEL_READ_SECTORS_PROTECTED_MODE equ 4
STATE_KERNEL_FINISHED equ 5

; depending on the read state we have to do different stuff now
	mov ah, byte [READ_STATE]
	cmp ah, STATE_READ_METADATA
	je handle_read_metadata
	cmp ah, STATE_READ_KERNEL
	je handle_read_kernel
	cmp ah, STATE_READ_INITRD
	je handle_read_initrd
	mov di, ERR_DISK
	mov ax, 0x03
	call print_error

; state: STATE_READ_METADATA
handle_read_metadata:
	push bx
	mov esi, 0x09000 + 0x101			; 5 chars must match 'ustar' for being a tar metadata block
	mov edi, USTAR_MAGIC
	mov bx, 5
	call strncmp
	pop bx
	cmp ax, 0
	je .print_tar_metadata
	jmp advance_to_next_sector			; read over unknown data or non-tar entries

.print_tar_metadata:
	mov al, ' '
	call print_char
	mov si, 0x09000 + 0x00				; the filename in the tar
	call print_string
	mov al, ' '
	call print_char
	mov si, 0x09000 + 0x7c				; the size in ASCII octal
	call print_string
	mov si, SPACE
	call print_string
	mov si, 0x09000 + 0x7c
	mov ecx, 11

	; compute size in decimal and compute number of data sectors we have to read
	call octal_string_to_int
	a32 mov [READ_DATA_SIZE], eax
	call print_hex_dword
	
	mov ecx, eax					; ecx contains the number of 512 byte sectors
	shr ecx, 9					; number of sectors a 512 bytes
	mov edx, ebx					; compute if we have a remainder
	and edx, 0x01FF
	cmp edx, 0					; no remainder, ending at full sector
	je .full_sector
	inc ecx						; one non-full sector more to read
.full_sector:
	mov [READ_DATA_SECTORS], ecx

	; enable to debug metadata in tar
	;~ mov si, NL
	;~ call print_string
	;~ mov ax, 0x0900
	;~ mov es, ax
	;~ call print_memory
	
.search_kernel:
	mov esi, 0x09000 + 0x00
	mov edi, FILE_BZIMAGE
	call strcmp
	cmp ax, 0
	je .found_kernel
	jmp .search_initrd

.found_kernel:
	mov [READ_STATE], byte STATE_READ_KERNEL
	mov [KERNEL_STATE], byte STATE_KERNEL_READ_SECTOR_0
	mov [READ_DESTINATION_PTR], dword 0x10000
	mov al, '!'
	call print_char
	mov si, NL
	call print_string
	jmp advance_to_next_sector

.search_initrd:
	mov esi, 0x09000 + 0x00
	mov edi, FILE_RAMDISK
	call strcmp
	cmp ax, 0
	je .found_initrd
	jmp .search_eof

.found_initrd:
	mov [READ_STATE], byte STATE_READ_INITRD
; qemu initrd start location 7fab000, 133869568 (this is 128MB) too high for us,
; kernel gives us alignment hints and hints where to load initrd to?
; let's use 8MB, TODO: does the kernel release the initial ramdisk? I think so. is
; it relocating it's structures? or do we get fragmented heap and stuff?
	a32 mov [READ_DESTINATION_PTR], dword 0x00800000
	a32 mov [INITRD_ADDRESS], dword 0x00800000
	a32 mov eax, [READ_DATA_SIZE]
	a32 mov [INITRD_SIZE], eax
	mov al, '!'
	call print_char
	mov si, NL
	call print_string

	jmp advance_to_next_sector

.search_eof:
	mov esi, 0x09000 + 0x00
	mov edi, FILE_EOF
	call strcmp
	cmp ax, 0
	je .found_eof
	jmp .unknown_file

.found_eof:
	mov si, NL
	call print_string
	mov si, MESSAGE_EOF_REACHED
	call print_string
	jmp finished_reading

.unknown_file:
	mov al, '?'
	call print_char
	mov si, NL
	call print_string
	jmp advance_to_next_sector

; STATE: STATE_READ_KERNEL
handle_read_kernel:

	; move kernel code/data from 0x09000 (our disk read scratch space
	; in low memory to 0x10000 (which is the real mode location for the
	; kernel code)
	mov ax, ds
	mov es, ax
	mov esi, 0x09000
	mov edi, dword [READ_DESTINATION_PTR]
	mov ecx, 512
	cld
	a32 rep movsb
	mov [READ_DESTINATION_PTR], edi
	
	mov ax, word [READ_DATA_SECTORS]
	dec ax
	mov word [READ_DATA_SECTORS], ax
	cmp ax, 0
	je .data_end
	jmp kernel_switch
.data_end:
	mov [READ_STATE], byte STATE_READ_METADATA
	jmp advance_to_next_sector
	
	; depending on the kernel substate
kernel_switch:
	xor ax, ax
	mov ah, byte [KERNEL_STATE]
	cmp ah, STATE_KERNEL_READ_SECTOR_0
	je handle_data_kernel_sector_0
	cmp ah, STATE_KERNEL_READ_SECTOR_1
	je handle_data_kernel_sector_1
	cmp ah, STATE_KERNEL_READ_SECTORS_REAL_MODE
	je handle_data_kernel_real_mode
	cmp ah, STATE_KERNEL_READ_SECTORS_PROTECTED_MODE
	je handle_data_kernel_protected_mode
	mov di, ERR_KERN
	mov ax, 0x01
	call print_error
	
; STATE: STATE_KERNEL_READ_SECTOR_0
handle_data_kernel_sector_0:

; first sector if real mode kernel

	; enable to debug real mode zero page metadata
	;~ mov si, NL
	;~ call print_string
	;~ mov ax, 0x1000
	;~ mov es, ax
	;~ call print_memory

	; get the number of real mode kernel sectors to read
	; (or mininmally 4 if 0 is returned), each 512 bytes
	mov si, NL
	call print_string
	a32 mov al, byte [0x10000+0x1f1]
	cmp al, 0
	jne .nof_sectors_ok
	mov al, 4					; minimally 4 sectors
	
.nof_sectors_ok:
	mov si, MESSAGE_KERNEL_NOF_REAL_SECTORS
	call print_string
	mov [KERNEL_NOF_REAL_MODE_SECTORS], al
	call print_hex
	mov si, NL
	call print_string

	; get size of protected mode kernel in 16 bytes
	a32 mov eax, [0x10000+0x1f4]
	mov ecx, eax
	shr ecx, 5					; 16-pages, shifting by 5 to the right gives
							; us the number of sectors a 512 bytes
	mov ebx, eax					; compute if we have to read one sector more with partial data
	and ebx, 0x001F					; 32 times 16 bytes per page
	cmp ebx, 0
	je .full_sector
	inc ecx
.full_sector:
	mov [KERNEL_NOF_PROTECTED_MODE_SECTORS], word cx
	mov si, MESSAGE_KERNEL_NOF_PROTECTED_SECTORS
	call print_string
	mov bx, [KERNEL_NOF_PROTECTED_MODE_SECTORS]
	mov ax, bx
	and ax, 0xFF00
	shr ax, 8
	call print_hex
	mov ax, bx
	and ax, 0x00FF
	call print_hex
	mov si, NL
	call print_string

	mov [KERNEL_STATE], byte STATE_KERNEL_READ_SECTOR_1

	jmp advance_to_next_sector

; STATE: STATE_KERNEL_READ_SECTOR_1
handle_data_kernel_sector_1:
	; enable to debug real mode zero page metadata
	;~ mov si, NL
	;~ call print_string
	;~ mov ax, 0x1020
	;~ mov es, ax
	;~ call print_memory

	; compare header
	mov esi, 0x10000 + 0x202
	mov edi, KERNEL_MAGIC
	mov bx, 4
	call strncmp
	cmp ax, 0
	je .kernel_HdrS_found
	mov di, ERR_KERN
	mov ax, 0x02
	call print_error
	
.kernel_HdrS_found:
	; get protocol version, don't allow anothing below 2.15 (which
	; os kernel 5.5 or above)
	mov al, 0x0d
	call print_char
	mov si, NL
	call print_string
	mov si, MESSAGE_KERNEL_BOOT_PROTOCOL
	call print_string
	a32 mov al, byte [0x10000+0x207]
	mov byte [KERNEL_BOOT_PROTOCOL_MAJOR], al
	call print_hex	
	mov al, '.'
	call print_char
	a32 mov al, byte [0x10000+0x206]
	mov byte [KERNEL_BOOT_PROTOCOL_MINOR], al
	call print_hex	
	mov si, NL
	call print_string
	xor ax, ax
	mov al, byte [KERNEL_BOOT_PROTOCOL_MAJOR]
	cmp al, 2
	jl .protocol_error
	xor ax, ax
	mov al, byte [KERNEL_BOOT_PROTOCOL_MINOR]
	cmp al, 15
	jl .protocol_error
	jmp .get_kernel_version_offset
	
.protocol_error:
	mov di, ERR_KERN
	mov ax, 0x03
	call print_error

	; get offset pointing to human readable kernel message,
	; we can print it only after having read the real mode part
	; of the kernel (or just before we actually start the kernel)
.get_kernel_version_offset:
	a32 mov ax, word [0x10000+0x20e]
	mov [KERNEL_VERSION_PTR], ax
	jmp .set_boot_data

.set_boot_data:
	; not quite clear what the kernel does with this data, TODO: must read kernel code
	a32 mov byte  [0x10000+0x210], 0xe1	; Extended bootloader type
	a32 mov byte  [0x10000+0x226], 0x00	; ext_loader_ver
	a32 mov byte  [0x10000+0x227], 0x01	; ext_loader_type (id: 0x11)
	a32 or  byte  [0x10000+0x211], 0x80	; set CAN_USE_HEAP
	a32 mov word  [0x10000+0x224], 0xde00 	; head_end_ptr
	
	; set up area and copy command line from the boot loader area to
	; a area registered with the kernel (0x1e000)
	a32 mov	dword [0x10000+0x228], 0x1e000	; set cmd_line_ptr
	xor ax, ax
	mov es, ax
	mov esi, KERNEL_CMD_LINE
	mov edi, 0x1e000
	mov ecx, KERNEL_CMD_SIZE
	cld
	a32 rep movsb
	
	jmp .change_state

.change_state:
	; enable to debug real mode zero page metadata after modifying and setting parameters
	;~ mov si, NL
	;~ call print_string
	;~ mov ax, 0x1020
	;~ mov es, ax
	;~ call print_memory

	mov [KERNEL_STATE], byte STATE_KERNEL_READ_SECTORS_REAL_MODE
	; intentional fallthrough, decrement real mode sectors below

; STATE: STATE_KERNEL_READ_SECTORS_REAL_MODE
handle_data_kernel_real_mode:
	; read at most KERNEL_NOF_REAL_MODE_SECTORS sectors for real
	; mode code/data
	mov al, byte [KERNEL_NOF_REAL_MODE_SECTORS]
	dec al
	mov byte [KERNEL_NOF_REAL_MODE_SECTORS], al
	cmp al, 0
	je .last_real_mode_sector_read
	jmp advance_to_next_sector

.last_real_mode_sector_read:
	; show kernel version
	mov al, 0x0d
	call print_char
	mov si, NL
	call print_string
	mov si, MESSAGE_KERNEL_VERSION
	call print_string
	mov esi, [KERNEL_VERSION_PTR]
	add esi, 0x200
	push ds
	mov ax, 0x1000
	mov ds, ax
	call print_string
	pop ds
	mov si, NL
	call print_string
	
	; change load pointer to protected mode area 0x100000
	mov [KERNEL_STATE], byte STATE_KERNEL_READ_SECTORS_PROTECTED_MODE
	mov [READ_DESTINATION_PTR], dword 0x100000
	
	jmp advance_to_next_sector

; STATE: STATE_KERNEL_READ_SECTORS_PROTECTED_MODE
handle_data_kernel_protected_mode:
	; read at most KERNEL_NOF_PROTECTED_MODE_SECTORS sectors for protected
	; mode code/data
	mov ax, word [KERNEL_NOF_PROTECTED_MODE_SECTORS]
	dec ax
	mov word [KERNEL_NOF_PROTECTED_MODE_SECTORS], ax
	cmp ax, 0
	je .last_protected_mode_sector_read
	jmp advance_to_next_sector
.last_protected_mode_sector_read:
	mov [READ_STATE], byte STATE_READ_METADATA
	mov [KERNEL_STATE], byte STATE_KERNEL_FINISHED
	jmp advance_to_next_sector

; STATE: STATE_READ_INITRD
handle_read_initrd:
	; move ramdisk data from 0x09000 (our disk read scratch space
	; in low memory to high memory 0xaaaa0000
	mov ax, ds
	mov es, ax
	mov esi, 0x09000
	
	mov edi, dword [READ_DESTINATION_PTR]
	mov ecx, 512
	cld
	a32 rep movsb
	mov [READ_DESTINATION_PTR], edi

	mov ax, word [READ_DATA_SECTORS]
	dec ax
	mov word [READ_DATA_SECTORS], ax
	cmp ax, 0
	je .data_end
	jmp .nothing_todo
.data_end:
	mov [READ_STATE], byte STATE_READ_METADATA
	jmp advance_to_next_sector
	
.nothing_todo:

advance_to_next_sector:
	add [CURRENT_SECTOR], byte 1	; next sector
	mov ch, [SECTORS_PER_CYLINDER]
	cmp [CURRENT_SECTOR], ch	; after the end of the current track?
	je .next_head
	jmp read_next_sector

.next_head:
	shr bx, 4			; make it a segment offset..
	mov ax, es
	add ax, bx
	mov es, ax			; ..and add it to ES
	mov bx, 0x0			; we also reset bx and update es to avoid hitting the 64k wrap around point
	mov [CURRENT_SECTOR], byte 1	; start from first sector again
	add [CURRENT_HEAD], byte 1	; advance head
	mov ch, [NOF_HEADS]
	cmp [CURRENT_HEAD], ch		; after the number of heads?
	je .next_track
	jmp read_next_sector
	
.next_track:
	mov [CURRENT_HEAD], byte 0	; start from head 0 again
	add [CURRENT_CYLINDER], byte 1	; advance track
	; TODO depends on boot parameters (the floppy media, add a table)
	cmp [CURRENT_CYLINDER], byte 80
	jae .next_floppy
	jmp read_next_sector

.next_floppy:
	call kill_motor
	mov si, NL
	call print_string
	mov si, MESSAGE_NEXT_FLOPPY
	call print_string
	call wait_for_keypress
	call reset_drive
	; TODO: check for floppy disk change (is there a BIOS function for this?)
	; TODO: maybe also check some checksum or so of the floppy data and see
	; if we indeed have a new floppy
	mov [CURRENT_SECTOR], byte 0	; will be incremented at advance_to_next_sector
	mov [CURRENT_HEAD], byte 0
	mov [CURRENT_CYLINDER], byte 0
	jmp advance_to_next_sector
	
finished_reading:
	; make sure the floppy is not spinnig (also in print_error)
	call kill_motor

start_kernel:

	; set ramdisk
	a32 mov eax, [INITRD_ADDRESS]		; ramdisk size in bytes
	a32 mov [0x10000+0x218], eax
	mov si, MESSAGE_INITRD_ADDRESS
	call print_string
	a32 mov eax, [INITRD_ADDRESS]
	call print_hex_dword
	mov si, NL
	call print_string
	a32 mov eax, [INITRD_SIZE]		; ramdisk address in bytes
	a32 mov [0x10000+0x21c], eax
	mov si, MESSAGE_INITRD_SIZE
	call print_string
	a32 mov eax, [INITRD_SIZE]
	call print_hex_dword
	mov si, NL
	call print_string

	; TODO: check if we actually do have reached the KERNEL_FINISHED state
	mov si, MESSAGE_BOOTING_KERNEL
	call print_string

	; set up segments for running the real mode kernel
	cli
	mov ax, 0x1000
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	mov sp, 0xe000

	; jump to kernel real mode entry
	jmp 0x1020:0

; we should not return here, rather the machine will reset, hang or kernel will OUPS,
; just in case, restore segments and print an error message just in case it happens..
	mov ax, CODE_DATA_SEGMENT
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	mov sp, 0x7c00
	
	mov si, ERR_KERN
	mov ax, 0x01
	call print_error

; allow rebooting if something goes sour in the kernel
reboot:
	mov si, MESSAGE_REBOOT
	call print_string
	
	call wait_for_keypress

; reboot (cannot use jmp here)
	db 0xea
	dw 0x0000
	dw 0xFFFF

; endless loop if reboot fails
	jmp $

; wait for key to be pressed
wait_for_keypress:
	push ax
	xor ax, ax
	int 0x16
	pop ax
	ret

; GDT global descriptor table

gdt_start:

; mandatory null entry
gdt_null:
	dd 0x0
	dd 0x0

; on big unreal segment, code and data are in the same unprotected segment
gdt_code_data:
	dw 0xffff	; limit (bits 0-15)
	dw 0x0		; base (bits 0-15)
	db 0x0		; base (bits 16-23)
	db 10010010b	; flags
	db 11001111b	; flags, limit (bits 16-19)
	db 0x0		; base (bit 24-31)

gdt_end:

gdt_descriptor:	
	dw gdt_end - gdt_start - 1	; size
	dd gdt_start			; start address of the GDT
	
; constants representing the segment bases
CODE_DATA_SEGMENT equ gdt_code_data - gdt_start

check_and_enable_A20:
	call check_A20_enabled
	cmp ax, 1
	je A20_ENABLED

A20_FAST_SPECIAL_PORT:

	mov al, 'F'
	call print_char

	in al, 0x92
	or al, 2
	out 0x92, al

	call check_A20_enabled
	cmp ax, 1
	je A20_ENABLED
	
A20_ENABLE_KBD_PORT:
	mov al, 'K'
	call print_char
	mov al, 0xdd
	out 0x64, al

	call check_A20_enabled
	cmp ax, 1
	je A20_ENABLED

A20_ENABLE_VIA_BIOS:
	mov al, 'B'
	call print_char
	mov ax, 0x2401
	int 0x15

	call check_A20_enabled
	cmp ax, 1

A20_ENABLE_KBD_OUT:

	mov al, 'k'
	call print_char

	cli			; disable interrupts, we talk directly to the keyboard ports

        call    .wait_input
        mov     al,0xAD
        out     0x64,al		; disable keyboard
        call    .wait_input

        mov     al,0xD0
        out     0x64,al		; tell controller to read output port
        call    .wait_output

        in      al,0x60
        push    eax		; get output port data and store it
        call    .wait_input

        mov     al,0xD1
        out     0x64,al		; tell controller to write output port
        call    .wait_input

        pop     eax
        or      al,2		; set bit 1 (enable a20)
        out     0x60,al		; write out data back to the output port

        call    .wait_input
        mov     al,0xAE		; enable keyboard
        out     0x64,al

        call    .wait_input

        sti

        jmp .retest

; wait for input buffer to be clear
.wait_input:
        in      al,0x64
        test    al,2
        jnz     .wait_input
        ret

; wait for output buffer to be clear
.wait_output:
        in      al,0x64
        test    al,1
        jz      .wait_output
        ret

.retest:
	call check_A20_enabled
	cmp ax, 1
	je A20_ENABLED
  	
A20_ENABLED:
	ret

; returns 0 if not A20_ENABLED, 1 if A20_ENABLED in AX
check_A20_enabled:
	pushf
	push ds
	push es
	push di
	push si
	
	cli
	
	xor ax, ax
	mov es, ax
	mov di, 0x0500			; es:di = 0000:0500
	
	mov ax, 0xffff
	mov ds, ax
	mov si, 0x0510			; ds:si = ffff:0510
	
	mov al, byte [es:di]		; preserve values in memory
	push ax				; on stack
	mov al, byte [ds:si]
	push ax
	
	mov byte [es:di], 0x00		; now the test: write 0x00 to 0000:0500
	mov byte [ds:si], 0xFF		; write 0xff to ffff:0510
	
	cmp byte [es:di], 0xFF		; memory wrap? A20 not enabled
	je .disabled
	jmp .enabled

.restore:
	pop bx				; restore original memory contents
	mov byte [ds:si], bl
	pop bx
	mov byte [es:di], bl
	jmp .exit
		
.enabled:
	mov al, '+'
	call print_char
	mov ax, 1			; not wrapped around
	jmp .restore

.disabled:
	mov al, '-'
	call print_char
	mov ax, 0			; wrapped around (last cmp)
	jmp .restore
	
.exit:
	pop si
	pop di
	pop es
	pop ds
	popf

	ret

; Read one sector from floppy using CHS adressing
read_one_sector_from_disk:

	mov ah, 0x02			; read sectors from drive
	mov al, 1			; read 1 sector
	mov ch, BYTE [CURRENT_CYLINDER]
	mov dh, BYTE [CURRENT_HEAD]
	mov dl, BYTE [BOOT_DRIVE]
	mov cl, BYTE [CURRENT_SECTOR]
	mov BYTE [CURRENT_RETRIES], 0
			
	int 0x13
	
	jc .read_error
	
	cmp al, 1			; 1 sector read?
	jne .short_read			; if not, short read
	
	ret
	
.read_error:
	cmp BYTE [CURRENT_RETRIES], 3
	jl .read_again
	jmp .print_error
.read_again:
	dec BYTE [CURRENT_RETRIES]
	call reset_drive
	jmp read_one_sector_from_disk
	
.print_error:
	;~ xor ax, ax
	;~ mov dh, 0
	;~ mov dl, ah
	mov di, ERR_DISK
	call print_error

.short_read:
	mov di, ERR_DISK
	call print_string

; IN al: character to print if printable, dot otherwise
; MOD ah
print_dump_char:
	cmp byte al, 0x20		; space
	jl .print_dot
	test byte al, al		; above 0x7f ~
	js .print_dot
	call print_char
	jmp .done
.print_dot:
	mov al, 0x2e			; .
	call print_char
.done:
	ret

; print a dump of 512 bytes in memory (a sector) for debugging purposes
; IN: es base address (as segment) where to start printing
print_memory:
	xor bx, bx
.next_line:
	xor cx, cx
	mov ax, es
	and ax, 0xFF00
	shr ax, 8
	call print_hex
	mov ax, es
	and ax, 0x00FF
	call print_hex
	xor ax, ax
	mov ah, ':'
	call print_char
	mov al, bh
	call print_hex
	mov al, bl
	call print_hex
	mov si, DUMP_SEP
	call print_string
.next_byte:
	mov al, [es:bx]
	call print_hex
	mov si, SPACE
	call print_string
	inc bx
	inc cx
	cmp cx, 8
	je .gap
	jmp .after_gap
.gap:
	mov si, SPACE
	call print_string
.after_gap:
	cmp cx, 16
	jne .next_byte
.print_chars:
	xor cx, cx
	sub bx, 16
.next_char:
	mov al, [es:bx]
	call print_dump_char
	inc bx
	inc cx
	cmp cx, 16
	jne .next_char
.newline:
	mov si, NL
	call print_string
	cmp bx, 256
	je .wait
	jmp .cont
.wait:
	mov si, MESSAGE_PRESS_KEY_TO_CONTINUE
	call print_string
	call wait_for_keypress	
.cont:
	cmp bx, 512
	jne .next_line
	ret

DUMP_SEP:
	db ": ", 0

; compare strings
; IN: si, di: start of two zero terminated strings
; OUT: ax <0 si < di, >0 si > di; =0 si = di
; CLOBBERS: si, di
strcmp:
	push dx
.loop:
	a32 mov dl, [esi]
	a32 mov dh, [edi]
	cmp dh, $0
	je .done
	cmp dl, $0
	je .done
	cmp dh, dl
	jne .done
	inc si
	inc di
	jmp .loop
.done:
	xor ax, ax
	mov al, byte dh
	sub al, byte dl
.end:
	pop dx
	ret

; compare strings up to a maximal number of characters
; IN: si, di: start of two zero terminated strings, bx: number of chars
; OUT: ax <0 si < di, >0 si > di; =0 si = di
; CLOBBERS: si, di, bx
strncmp:
	push dx
.loop:
	a32 mov dl, [esi]
	a32 mov dh, [edi]
	cmp dh, $0
	je .done
	cmp dl, $0
	je .done
	cmp dh, dl
	jne .done
	inc si
	inc di
	dec bx
	jz .done
	jmp .loop
.done:
	xor ax, ax
	mov al, byte dh
	sub al, byte dl
.end:
	pop dx
	ret
	
; convert octal string to integer
; IN: si, cx: pointer to the string and length of the string
; OUT: eax: converted integer
; clobbers: ebx
octal_string_to_int:
	xor ebx, ebx
.next:
	movzx eax, byte [esi]
	inc esi
	sub al, '0'		; assuming ASCII
	shl ebx, 3		; octal, multiply by 8
	add ebx, eax		; add current digit
	loop .next		; cx--
	mov eax, ebx
	ret
	
MESSAGE_CHECKING_A20:
	db "Checking A20 address gate.. ", 0
	
MESSAGE_ENABLED:
	db " enabled", 13, 10, 0
	
MESSAGE_DISABLED:
	db " disabled", 13, 10, 0

MESSAGE_SWITCHING_TO_UNREAL_MODE:
	db "Switching to unreal mode..", 0

MESSAGE_DRIVE_PARAMETERS:
	db "Boot parameters ", 0

MESSAGE_EOF_REACHED:
	db "Reached end of tar file..", 13, 10, 0
	
MESSAGE_BOOTING_KERNEL:
	db "Booting kernel..", 13, 10, 0

MESSAGE_REBOOT:
	db "Press any key for rebooting..", 13, 10, 0

MESSAGE_NEXT_FLOPPY:
	db "Insert next floppy and press any key to continue..", 13, 10, 0
	
MESSAGE_PRESS_KEY_TO_CONTINUE:
	db "Press any key to continue..", 13, 10, 0

MESSAGE_KERNEL_NOF_REAL_SECTORS:
	db "Number of real-mode kernel sectors: ", 0

MESSAGE_KERNEL_NOF_PROTECTED_SECTORS:
	db "Number of protected-mode kernel sectors: ", 0

MESSAGE_KERNEL_BOOT_PROTOCOL:
	db "Linux boot protocol version: ", 0

MESSAGE_KERNEL_VERSION:
	db "Linux kernel version: ", 0
	
MESSAGE_INITRD_ADDRESS:
	db "Ramdisk address: ", 0

MESSAGE_INITRD_SIZE:
	db "Ramdisk size: ", 0

ERR_A20:
	db "A20 ", 0

ERR_DISK:
	db "DISK ", 0

ERR_KERN:
	db "KERN ", 0

USTAR_MAGIC:
	db "ustar", 0

KERNEL_MAGIC:
	db 'HdrS', 0

; data sections used for reading from floppy, default to some sane values
; get probed and filled in during floppy drive probing
FLOPPY_TYPE:
	db 0x00				; drive type, usually 0x04 after probing for 3 1/4" 1.44MB

SECTORS_PER_CYLINDER:
	db 0x3F				; detect parameters enters the correct value here (sectors + 1)
					; if detection fails, force int13 to read ahead
NOF_HEADS:
	db 0x01				; number of heads + 1

; read route reads and updates those values while reading from floppy
CURRENT_SECTOR:
	db 1

CURRENT_CYLINDER:
	db 0

CURRENT_HEAD:
	db 0				

CURRENT_RETRIES:
	db 0

FILE_BZIMAGE:
	db "bzImage", 0

FILE_RAMDISK:
	db "ramdisk.img", 0

FILE_EOF:
	db "EOF", 0
	
READ_STATE:
	db STATE_READ_METADATA

READ_DATA_SECTORS:
	dd 0

READ_DESTINATION_PTR:
	dd 0x10000

READ_DATA_SIZE:
	dd 0

KERNEL_STATE:
	db STATE_KERNEL_READ_SECTOR_0

KERNEL_NOF_REAL_MODE_SECTORS:
	db 0

KERNEL_NOF_PROTECTED_MODE_SECTORS:
	dw 0

KERNEL_CMD_LINE:
	db "debug loglevel=7 earlycon=uart8250,io,0x3f8,9600n8 console=tty0 console=ttyS0,9600n8 rdinit=/sbin/init root=/dev/ram0 iommu=off", 0

KERNEL_CMD_SIZE equ $-KERNEL_CMD_LINE

KERNEL_BOOT_PROTOCOL_MAJOR:
	db 0

KERNEL_BOOT_PROTOCOL_MINOR:
	db 0

KERNEL_VERSION_PTR:
	dw 0

INITRD_ADDRESS:
	dd 0

INITRD_SIZE:
	dd 0

; make sure we have full sectors
times (NOF_SECTORS_STAGE2+1)*512-($-$$) db 0