File: main-utils.asm

package info (click to toggle)
sbm 3.7.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,060 kB
  • ctags: 1,590
  • sloc: asm: 11,940; ansic: 2,870; makefile: 531
file content (1916 lines) | stat: -rw-r--r-- 46,503 bytes parent folder | download | duplicates (4)
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
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
; asmsyntax=nasm
;
; main-utils.asm
;
; utility functions for main program
;
; Copyright (C) 2000, Suzhe. See file COPYING for details.
;

%ifdef MAIN

;=============================================================================
;init_theme ---- initialize the theme data.
;=============================================================================
init_theme:
        mov bx, [icon.brand]
        or bx, bx
        jz .adjust_bkgnd                            ; no brand icon
        add word [icon.brand], theme_start          ;
.adjust_bkgnd:
        mov bx, [icon.background]
        or bx, bx
        jz .adjust_font                             ; no background icon
        add word [icon.background], theme_start
.adjust_font:
        mov bx, [font.data]
        or bx, bx
        jz .adjust_keymap
        add word [font.data], theme_start
.adjust_keymap:
        mov bx, [keymap.data]
        or bx, bx
        jz .adjust_str
        add word [keymap.data], theme_start

.adjust_str:
        lea si, [str_idx]
        mov cx, (end_of_str_idx - str_idx)/2
        
.loop_adjust:
        mov bx, [si]
        add bx, theme_start
        mov [si], bx
        add si, 2
        loop .loop_adjust

        mov al, 0x10
        and [keyboard_type], al

        ret
        
;=============================================================================
;init_cmd_menus ---- initialize the command menus
;=============================================================================
init_cmd_menus:
        pusha
;initialize main menu
        lea si, [main_menu]
        mov bx, [str_idx.main_menu_title]
        call init_popup_menu
;initialize record menu
        lea si, [record_menu]
        mov bx, [str_idx.record_menu_title]
        call init_popup_menu
;initialize system menu
        lea si, [sys_menu]
        mov bx, [str_idx.sys_menu_title]
        call init_popup_menu
        popa
        ret

;ds:si -> menu struct
;bx = addr of string index
init_popup_menu:
        mov [si + struc_menu.title], bx
        mov ax, [position.cmd_menu_col]
        mov [si + struc_menu.win_pos], ax
        mov ax, [color.cmd_menu_winframe]
        mov [si + struc_menu.win_attr], ax
        mov ax, [color.cmd_menu_normal]
        mov [si + struc_menu.norm_attr], ax
        mov ax, [color.cmd_menu_focus]
        mov [si + struc_menu.focus_attr], ax
        call calc_menusize
        ret

;=============================================================================
;init_video ---- init the video mode
;input:
;      none
;output:
;      none
;=============================================================================
init_video:
        mov al, [video_mode]
        mov bl, 8
        mov bp, [font.data]
        mov cx, [font.number]
        
        call set_video_mode
        ret

;=============================================================================
;init_good_record_list ---- init the good boot record list
;input:
;      none
;output:
;      cf = 0 sucess
;      cf = 1 failed, no good record
;=============================================================================
init_good_record_list:
        cld
        pusha
        mov cx, MAX_RECORD_NUM
        lea di, [good_record_list]
        lea si, [boot_records]
        xor ax, ax
.loop_check:
        call check_bootrecord                   ; check if it's valid
        jc .check_next
        stosb                                   ; store it's index to buffer
        inc ah
        
.check_next:
        inc al
        add si, SIZE_OF_BOOTRECORD
        loop .loop_check

        mov [good_record_number], ah
        or ah, ah
        jnz .ok
        stc
.ok:
        popa
        ret

;=============================================================================
;init_boot_records ---- init the boot records list
;input:
;      none
;output:
;      none
;=============================================================================
init_boot_records:
        pusha
        inc byte [change_occured]         ; some changes occured.

        cld
        mov al, SIZE_OF_BOOTRECORD        ;
        mov cl, MAX_RECORD_NUM            ; ax = size of bootrecord
        xor ah, ah                        ;
        xor ch, ch                        ; cx = max record number

        lea si, [boot_records]
        lea di, [tmp_records_buf]
        push di
        push cx
        push ax
        mul cl                            ; cx = size of boot records array
        mov cx, ax                        ;
        xor ax, ax                        ;
        rep stosb                         ; clear tmp_records_buf
        pop ax
        pop cx
        pop di

        mov bl, cl

        push si
        push cx                           ; cx = MAX_RECORD_NUM
.bkp_good_records:
        call check_bootrecord
        jc .bad_record

        push si
        push cx
        mov cx, ax
        rep movsb
        pop cx
        pop si

        dec bl
        
.bad_record:
        add si, ax
        loop .bkp_good_records

        pop cx                           ; cx = MAX_RECORD_NUM
        pop si                           ; si -> boot_records
        xchg si, di                      ; di -> boot_records

        push di
	push ax
        xor dl, dl
        test byte [kernel_flags], KNLFLAG_ONLYPARTS
	setnz al
        call search_records
	pop ax
        pop di

;search finished, find out new records
        mov cl, MAX_RECORD_NUM
        xor ch, ch
        xchg si, di                      ; si -> boot_records

        push si

        or bl, bl
        jz .no_space

.search_news:
        push di
        lea di, [tmp_records_buf]
        call find_record_in_buf
        pop di

        jnc .found

        push cx
        push si
        mov cx, ax
        rep movsb
        pop si
        pop cx

        dec bl

.found:
        or bl, bl
        jz .no_space
        add si, ax
        loop .search_news

.no_space:

        pop si
        lea di, [tmp_records_buf]
        mov cl, MAX_RECORD_NUM
        mul cl
        mov cx, ax
        xchg di, si
        rep movsb

        popa
        ret

;=============================================================================
; find_record_in_buf ---- find a record in a buffer
; input:
;      ds:si -> the record
;      es:di -> the buffer
; output:
;      cf = 1 not found
;=============================================================================
find_record_in_buf:
        pusha
	mov bx, [si + struc_bootrecord.flags]
        test bx, DRVFLAG_DRIVEOK|INFOFLAG_ISSPECIAL

        jz .not_found

        mov cx, MAX_RECORD_NUM

.compare_next:
	test bx, INFOFLAG_ISSPECIAL
	jz .normal_rec
	test word [di + struc_bootrecord.flags], INFOFLAG_ISSPECIAL
	jnz .special_rec
	jmp short .not_same

.normal_rec:
	test word [di + struc_bootrecord.flags], DRVFLAG_DRIVEOK
	jz .not_same
        mov ax, [di + struc_bootrecord.drive_id]
        cmp [si + struc_bootrecord.drive_id], ax
        jne .not_same
        mov eax, [di + struc_bootrecord.father_abs_addr]
        cmp [si + struc_bootrecord.father_abs_addr], eax
        jne .not_same
        mov eax, [di + struc_bootrecord.abs_addr]
        cmp [si + struc_bootrecord.abs_addr], eax
        jne .not_same

.special_rec:
        mov al, [di + struc_bootrecord.type]
        cmp [si + struc_bootrecord.type], al
        jne .not_same

        jmp short .found_same

.not_same:
        add di, SIZE_OF_BOOTRECORD
        loop .compare_next

.not_found:
        stc
        popa
        ret

.found_same:
        clc
        popa
        ret

;=============================================================================
;draw_screen ---- draw the whole screen
;input:
;      none
;output:
;      none
;=============================================================================
draw_screen:
        call lock_screen

        mov bh, [color.background]              ;
        mov si, [icon.background]               ; draw background
        mov cx, [icon.background_size]          ;
        call draw_background                    ;

        xor dx, dx                              ;
        mov bx, [color.copyright]               ;
        mov al, [screen_width]                  ; draw copyright message
        push ax                                 ; save screen width
        mov cl, [size.copyright]
        mul cl
        mov cx, ax
        mov al, ' '                             ;
        call draw_char                          ;
        mov si, [str_idx.copyright]
        call draw_string_hl
        
        mov bx, [color.hint]                    ;
        mov dh, [screen_height]                 ;
        mov cl, [size.hint]                     ;
        sub dh, cl                              ; draw hint message
        pop ax                                  ; get screen width
        mul cl                                  ;
        mov cx, ax                              ;
        mov al, ' '                             ;
        call draw_char                          ;
        mov si, [str_idx.hint]                  ;
        call draw_string_hl                     ;

        mov dx, [position.brand_col]            ; draw brand icon
        mov cx, [icon.brand_size]               ;
        cmp dl, 0xFF                            ;
        jne .not_justify                        ;
        mov dl, [screen_width]                  ; right justify
        sub dl, cl                              ;
.not_justify:                                   ;
        mov si, [icon.brand]                    ;
        call draw_icon                          ;

        call draw_date
        call draw_time
        call draw_delay_time
        call draw_knl_flags
        call draw_main_window
        call draw_bootmenu

        mov al, [cmd_menu_stat]
        test al, MAIN_MENU_ON
        jz .chk_recordmenu
        lea si, [main_menu]
        jmp short .draw_cmd_menu

.chk_recordmenu:
        test al, RECORD_MENU_ON
        jz .chk_sysmenu
        lea si, [record_menu]
        jmp short .draw_cmd_menu

.chk_sysmenu:
        test al, SYS_MENU_ON
        jz .end
        lea si, [sys_menu]
 
.draw_cmd_menu:
        call draw_menu_box
        call draw_menu

.end:
        call unlock_screen
        ret
        
;=============================================================================
;draw_date ---- draw the date string
;input:
;      none
;output:
;      none
;=============================================================================
draw_date:
        pusha
        lea di, [tmp_buffer]                    ; draw date
        mov al, [show_date_method]              ;
        call get_cur_date_str                   ;
        mov si, di                              ;
        mov bl, [color.date]                    ;
        mov dx, [position.date_col]             ;
        call draw_string                        ;
        popa
        ret

;=============================================================================
;draw_time ---- draw the time string
;input:
;      none
;output:
;      none
;=============================================================================
draw_time:
        pusha
        lea di, [tmp_buffer]                    ; draw date
        mov al, [show_time_method]              ;
        call get_cur_time_str                   ;
        mov si, di                              ;
        mov bl, [color.time]                    ;
        mov dx, [position.time_col]             ;
        call draw_string                        ;
        popa
        ret

;=============================================================================
;update_time_info ---- update the time info, it the time is changed.
;=============================================================================
update_time_info:
        pusha
        mov ah, 0x02
        int 0x1a
        cmp [last_time], cx
        je .end

        call draw_date
        call draw_time

        mov [last_time], cx

.end:
        popa
        ret

;=============================================================================
;draw_main_window ---- draw the main window
;input:
;      none
;output:
;      none
;=============================================================================
draw_main_window:
        pusha
        mov cx, [main_window_col]                       ; get main window's
        mov dx, cx                                      ; coordinate
        add dh, [size.main_win_height]
        dec dh
        add dl, [main_win_width]
        dec dl

        mov bx, [color.main_win_frame]                  ; get main window's color
        mov si, [str_idx.main_win_title]                ; get main window's title

        call draw_window

        add cx, (MENU_TITLE_POS_ROW << 8) + MENU_TITLE_POS_COL
        mov dx, cx
        mov bl, [color.menu_title]
        mov al, ' '
        movzx cx, byte [menu_width]
        call draw_char
        inc dl
	mov si, [bootmenu_title_str]
        call draw_string                                ; draw title string
        popa
        ret

;=============================================================================
;draw_knl_flags ---- draw root passwd, login, secure mode, remember last 
;                    and int13 ext flags.
;=============================================================================
draw_knl_flags:
        mov dh, [screen_height]
        mov dl, [screen_width]
        sub dx, 0x0113

        mov bl, [color.hint]
        mov al, '|'
        mov cx, 1
        call draw_char
        inc dl

        lea di, [tmp_buffer]

	push di
	push dx
	mov dl, [kernel_drvid]
	call get_drvid_str
	pop dx
	pop si

        mov bl, [color.knl_drvid]

        call draw_string
        add dl, 3

        mov bl, [color.hint]
        mov cx, 1
        mov al, '|'
        call draw_char
        inc dl

        mov bl, [color.knl_flags]
        cmp dword [root_password], 0
        jz .no_root_password
        
        mov al, 'P'
        jmp short .draw_pwd
.no_root_password:
        mov al, '-'
.draw_pwd:
        call draw_char
        inc dl

        test byte [kernel_flags], KNLFLAG_SECURITY
        jz .no_security

        mov al, 'S'
        jmp short .draw_security
.no_security:
        mov al, '-'
.draw_security:
        call draw_char
        inc dl

        mov al, [root_login]
        or al, al
        jz .no_root_login

        mov al, 'A'
        jmp short .draw_login
.no_root_login:
        mov al, '-'
.draw_login:
        call draw_char

        inc dl

        test byte [kernel_flags], KNLFLAG_REMLAST
        jz .no_remlast
        mov al, 'L'
        jmp short .draw_remlast
.no_remlast:
        mov al, '-'
.draw_remlast:
        call draw_char

        inc dl

        test byte [kernel_flags], KNLFLAG_NOINT13EXT
        jnz .no_int13ext
        mov al, 'E'
        jmp .draw_int13ext
.no_int13ext:
        mov al, '-'
.draw_int13ext:
        call draw_char
        inc dl

        mov bl, [color.hint]
        mov al, '|'
        call draw_char

        ret

;=============================================================================
;draw_scrollbar ---- draw the scroll bar
;=============================================================================
draw_scrollbar:
        xor cx, cx
        inc cl

        mov bl, [color.scrollbar]
        mov dx, [main_window_col]
        add dx, (MENU_POS_ROW << 8) + MENU_POS_COL
        add dl, [menu_width]

        mov bh, [size.bootmenu_height]
        push dx
        push bx

;draw scroll bar
        mov al, 0x1e
        call draw_char

        mov al, ' '
        dec bh
.loop_draw_bar:
        dec bh
        inc dh
        call draw_char
        or bh, bh
        jnz .loop_draw_bar

        mov al, 0x1f
        call draw_char

;calculate the bar cursor position
        pop bx
        pop dx

        cmp bh, [good_record_number]
        jae .no_cursor
        cmp bh, 3
        jb .no_cursor

        sub bh, 2

        movzx ax, byte [focus_record]
        mul bh
        mov bh, [good_record_number]
        or bh, bh
        jz .no_cursor
        div bh

        add dh, al
        inc dh

        mov al, 'O'
        call draw_char

.no_cursor:
        ret
 
;=============================================================================
;draw_bootmenu_item ---- draw a boot menu item
;input:
;      bl = high 4 bit Background color and low 4 bit Foreground color
;      cl = item index in good_record_list
;output:
;      none
;=============================================================================
draw_bootmenu_item:
        pusha
        cmp byte [good_record_number], 0
        je .no_menu_item

        xor ch, ch
        lea si, [good_record_list]                  ; get real index of the
        add si, cx                                  ; boot record.
        lodsb                                       ; al = the index
        
        xor ah, ah                                  ; get the pointer of
        push ax                                     ;
        mov dl, SIZE_OF_BOOTRECORD                  ; selected boot record.
        mul dl                                      ;
        lea si, [boot_records]                      ;
        add si, ax                                  ;
        
        lea di, [tmp_buffer]
	mov al, [bootmenu_style]
        call get_record_string

        mov dh, cl                                  ; calculate the draw
        sub dh, [first_visible_rec]                 ; coordinate of the
        add dh, MENU_POS_ROW                        ; menu item.
        mov dl, MENU_POS_COL                        ;
        add dx, [main_window_col]

        mov cl, 1
        mov al, ' '
        call draw_char                              ; draw a leading space
        inc dl
        pop ax                                      ; al = the real index

        cmp al, [default_record]                    ;
        jne .not_default                            ;
        mov al, '*'                                 ; draw a flag before
        jmp .draw_def_flag                          ; the menu item, if
.not_default:                                       ; it's default record.
        mov al, ' '                                 ;
.draw_def_flag:                                     ;
        call draw_char                              ;
        inc dl                                      ;
        
        mov si, di
        call draw_string                            ; draw the menu item.

        call strlen                                 ; get the length of the item.
        mov al, ' '
        mov ah, [menu_width]
        sub ah, cl
        sub ah, 3
        add dl, cl
        mov cl, ah
        inc cl
        call draw_char                              ; fill ending space chars
.no_menu_item:
        popa
        ret

;=============================================================================
;draw_bootmenu ---- draw the boot menu
;input:
;      none
;output:
;      none
;=============================================================================
draw_bootmenu:
        pusha
        mov bl, [color.menu_normal]
        mov bh, bl
        mov cx, [main_window_col]                       ; get the top left
        add cx, (MENU_POS_ROW << 8) + MENU_POS_COL      ; and bottom right
        mov dx, cx                                      ; corner's coordinate
        add dh, [size.bootmenu_height]
        dec dh
        add dl, [menu_width]
        sub dl, 1                                       ; of menu area

        call clear_screen                               ; clear the menu area

        mov cl, [first_visible_rec]
        mov ch, cl
        add ch, [size.bootmenu_height]
.loop_draw:
        cmp cl, [good_record_number]
        jae .end
        cmp cl, ch
        jae .end
        call draw_bootmenu_item
        inc cl
        jmp .loop_draw
.end:

;draw focused item
        mov bl, [color.menu_focus]
        mov cl, [focus_record]

        call draw_bootmenu_item                     ; draw focus menu item
        call draw_scrollbar

        popa
        ret

;=============================================================================
;draw_delay_time ---- draw the delay_time and time_count
;=============================================================================
draw_delay_time:
        movzx ax, byte [time_count]
        mov cx, 3
        lea di, [tmp_buffer]
        call itoa
        
        mov bl, [color.delay_time]
        mov dh, [screen_height]
        mov dl, [screen_width]
        sub dx, 0x0108
        mov si, di
        call draw_string

        movzx ax, byte [delay_time]
        mov cx, 3
        call itoa

        mov al, ':'
        mov cl, 1
        add dl, 3
        call draw_char
        inc dl
        call draw_string
        mov al, ' '
        add dl, 3
        call draw_char
        ret
        

;=============================================================================
;get_key ---- get a key stroke while count down the delay time and refresh the
;             calendar info
;input:
;      none
;output:
;      ax = the key code
;=============================================================================
get_key:

.wait_key:
        call update_time_info

        cmp byte [delay_time], 0                        ; if delay_time set to zero
        je .no_count                                    ; then don't count time.
        cmp byte [key_pressed], 0                       ; if key pressed then don't
        jne .no_count                                   ; count time.

        xor ah, ah                                      ; get time ticks
        int 0x1a                                        ;

        cmp dx, [ticks_count]                           ;
        jae .next_time                                  ; dx must greater than
        mov [ticks_count], dx                           ; ticks_count
.next_time:
        mov cx, dx                                      ; every 18 ticks approxmiately
        sub cx, [ticks_count]                           ; equal to 1 second,
        cmp cx, 18                                      ; decrease time_count
        jbe .not_add                                    ; until to zero.
        mov [ticks_count], dx                           ;
        dec byte [time_count]                           ;
        call draw_delay_time                            ;
.not_add:
        cmp byte [time_count], 0                        ; if time is up, then
        jbe .esc_key                                    ; send ESC key.

.no_count:
        mov ah,1                                        ; if no key pressed
        or ah, [keyboard_type]
        call bioskey                                    ; go back to check
	jz .check_alt_key
        mov ah, [keyboard_type]                         ; if any key pressed

.read_key:
        call bioskey                                    ; then get it.
	jmp short .have_key

.check_alt_key:
	mov ah,2
	call bioskey
	test al, kbAltMask
	jz .wait_key

.wait_alt_release:
	mov ah, 2
	call bioskey
	test al, kbAltMask
	jnz .wait_alt_release

        mov ah,1                                        ; if no key pressed
        or ah, [keyboard_type]
        call bioskey                                    ; go back to check
	jnz .read_key

	mov ax, kbTab

.have_key:
	
        mov byte [key_pressed], 1
        ret

.esc_key:
        mov ax, kbEsc
        inc byte [key_pressed]
        ret

;=============================================================================
;recheck_same_records ---- recheck all records that same as given record
;input:
;      ds:si -> record
;output:
;      cf = 0  success
;      cf = 1  failed
;=============================================================================
recheck_same_records:
        pusha
        mov ax, [si + struc_bootrecord.drive_id]
        mov ebx, [si + struc_bootrecord.father_abs_addr]
        mov edx, [si + struc_bootrecord.abs_addr]

        lea si, [boot_records]
        mov cx, MAX_RECORD_NUM

.loop_check:
        test byte [si + struc_bootrecord.flags], DRVFLAG_DRIVEOK
        jz .check_next
        cmp [si + struc_bootrecord.drive_id], ax
        jne .check_next
        cmp [si + struc_bootrecord.father_abs_addr], ebx
        jne .check_next
        cmp [si + struc_bootrecord.abs_addr], edx
        jne .check_next

        call check_bootrecord
        jc .end

.check_next:
        add si, SIZE_OF_BOOTRECORD
        loop .loop_check
        clc
.end:
        popa
        ret

;=============================================================================
;get_realtime ---- get the machine real time in minutes
;input:
;      none
;output:
;      cf = 0 success, ax = real time in minutes, dx = day (set a bit)
;      cf = 1 failed
;=============================================================================
get_realtime:
        push bx
        push cx

        mov ah, 0x04
        int 0x1a
        jc .end

        movzx ax, dh
        call bcd_to_bin
        mov dh, al
        mov al, dl
        call bcd_to_bin
        mov dl, al

        mov ax, cx
        call bcd_to_bin

        call day_in_week

        mov dx, 1
        shl dx, cl

        push dx
        mov ah, 0x02
        int 0x1a
        pop dx
        jc .end

        movzx ax, ch
        call bcd_to_bin
        mov ch, al

        mov al, cl
        call bcd_to_bin
        mov cl, al

;convert hour and minute into minute
        mov al, 60
        mul ch
        xor ch, ch
        add ax, cx

        clc
.end:
        pop cx
        pop bx

        ret


;=============================================================================
; get_cur_time_str ---- get current time string
; input: al = show method, es:di -> buffer
;=============================================================================
get_cur_time_str:
       pusha
       or al, al
       jz .end

       mov ah, 0x02
       int 0x1a
       jc .end

       mov bx, cx

       mov cx, 2

       movzx ax, bh
       call bcd_to_str
       add di, cx
       mov al, ':'
       stosb

       mov al, bl
       call bcd_to_str
       add di, cx
       
.end:
       xor al, al
       stosb
       popa
       ret      


;=============================================================================
; get_cur_date_str ---- get current date string
; input: al = show method, es:di -> buffer
;        the method of show date:
;           0 = don't show date
;           1 = day mm-dd-yyyy
;           2 = day yyyy-mm-dd
;           3 = day dd-mm-yyyy
; output: none
;=============================================================================
get_cur_date_str:
       pusha

       or al, al
       jz .end

       push ax
       mov ah, 0x04
       int 0x1a
       pop ax
       jc .end

       push ax
       push cx
       push dx

       movzx ax, dh
       call bcd_to_bin
       mov dh, al
       mov al, dl
       call bcd_to_bin
       mov dl, al

       mov ax, cx
       call bcd_to_bin

       call day_in_week

       mov bx, cx
       shl bx, 1
       mov si, [str_idx.sunday+bx]

       call strcpy
       mov al, ' '
       stosb

       pop dx
       pop bx
       pop ax

       xor cx, cx

       cmp al, 1
       je .mmddyy
       cmp al, 2
       je .yymmdd
       cmp al, 3
       je .ddmmyy
       jmp .end

.end:       
       xor al, al
       stosb
       popa
       ret

.mmddyy:
       mov al, '-'
       push ax
       call .write_mm
       pop ax
       stosb
       push ax
       call .write_dd
       pop ax
       stosb
       call .write_yy

       jmp .end

.yymmdd:
       mov al, '-'
       push ax
       call .write_yy
       pop ax
       stosb
       push ax
       call .write_mm
       pop ax
       stosb
       call .write_dd

       jmp .end

.ddmmyy:
       mov al, '-'
       push ax
       call .write_dd
       pop ax
       stosb
       push ax
       call .write_mm
       pop ax
       stosb
       call .write_yy

       jmp .end

.write_mm:
       movzx ax, dh
       mov cl, 2
       call bcd_to_str
       add di, cx
       ret

.write_dd:
       movzx ax, dl
       mov cl, 2
       call bcd_to_str
       add di, cx
       ret

.write_yy:
       mov ax, bx
       mov cl, 4
       call bcd_to_str
       add di, cx
       ret
 
;=============================================================================
;get_record_pointer ---- get current record pointer
;input:
;      none
;output:
;      ds:si -> record pointer
;=============================================================================
get_record_pointer:
        push dx
        push ax
        movzx dx, [focus_record]                ; get real record index
        lea si, [good_record_list]              ;
        add si, dx                              ;
        lodsb                                   ;

        mov dl, SIZE_OF_BOOTRECORD              ; get the pointer to
        mul dl                                  ; the record.
        lea si, [boot_records]                  ;
        add si, ax                              ;
        pop ax
        pop dx
        ret
        
;=============================================================================
;confirm_root_passwd ---- confirm the root password
;input:
;      none
;output:
;      cf = 0 success
;      cf = 1 failed or cancel
;=============================================================================
confirm_root_passwd:
        pusha
        mov bx, [root_password]
        mov cx, [root_password+2]
        or bx, bx
        jnz .have_password
        or cx, cx
        jnz .have_password
        jmp .auth_ok
        
.have_password:                                     
        mov si, [str_idx.root_passwd]               ; check root
        call confirm_passwd                         ; password
.auth_ok:
        popa
        ret


;=============================================================================
;confirm_security_passwd ---- confirm the secure password
;                           in normal mode it confirms record password
;                           in security mode it confirms root password
;=============================================================================
confirm_security_passwd:
        test byte [kernel_flags], KNLFLAG_SECURITY
        jz confirm_record_passwd
        jmp confirm_root_passwd

;=============================================================================
;confirm_record_passwd ---- confirm the record password
;=============================================================================
confirm_record_passwd:
        pusha
        call get_record_pointer
        mov bx, [si + struc_bootrecord.password]
        mov cx, [si + struc_bootrecord.password+2]
        or bx, bx
        jnz .have_password
        or cx, cx
        jnz .have_password
        jmp .auth_ok
        
.have_password:
        mov si, [str_idx.record_passwd]             ; check record
        call confirm_passwd                         ; password
.auth_ok:
        popa
        ret

;=============================================================================
;confirm_passwd ---- let user input a password and confirm it.
;input:
;      bx:cx = password
;      ds:si -> message string
;output:
;      cf = 0 success
;      cf = 1 failed or cancel
;=============================================================================
confirm_passwd:
        mov al, [root_login]                        ; check if root
        or al, al                                   ; has login
        jnz .ok                                ;

        call input_passwd
        jc .cancel

        cmp bx, ax
        jne .cmp_root
        cmp cx, dx
        jne .cmp_root
        jmp .ok
        
.cmp_root:
        cmp [root_password], ax
        jne .failed
        cmp [root_password+2], dx
        jne .failed
.ok:
        clc
        ret
        
.failed:
        mov si, [str_idx.wrong_passwd]
        call error_box
        
.cancel:
        stc
        ret


;=============================================================================
;input_passwd ---- input a passwd
;input:
;      ds:si -> message string
;output:
;      cf = 0 success, ax:dx = password
;      cf = 1 cancel
;=============================================================================
input_passwd:
        push bx
        push cx
        
        mov ah, 1
        mov al, [color.input_box_msg]
        mov bx, [color.input_box_frame]
        mov cx, (MAX_PASSWORD_LENGTH << 8) | MAX_PASSWORD_LENGTH
        lea di, [tmp_buffer]
        mov byte [di], 0
        call input_box
        jc .cancel_input

        mov si, di
        movzx cx, ch
        call calc_password
        clc
.cancel_input:
        push ax
        push dx
        pushf
        call draw_screen
        popf
        pop dx
        pop ax
        pop cx
        pop bx
        ret

;=============================================================================
;error_box ---- draw error message box.
;input:
;      ds:si -> error message
;output:
;      ax = return keycode
;=============================================================================
error_box:
        call count_lines
        mov dl, cl
        add dl, [size.box_width]
        mov dh, [size.box_height]
        add dh, ch

        mov al, [color.error_box_msg]
        mov bx, [color.error_box_frame]
        mov di, [str_idx.error]
        xchg di, si
        call message_box
        xor ah, ah
        call bioskey
        push ax
        call draw_screen
        pop ax
        ret

;=============================================================================
;info_box ---- draw infomation message box.
;input:
;      ds:si -> infomation message
;output:
;      ax = return keycode
;=============================================================================
info_box:
        call count_lines
 
        mov dl, cl
        add dl, [size.box_width]
        mov dh, [size.box_height]
        add dh, ch

        mov al, [color.info_box_msg]
        mov bx, [color.info_box_frame]
        mov di, [str_idx.info]
        xchg di, si
        call message_box
        xor ah, ah
        call bioskey
        push ax
        call draw_screen
        pop ax
        ret

;=============================================================================
;boot_default ---- boot the default record
;=============================================================================
boot_default:
        mov ah, [default_record]
        lea si, [good_record_list]
        movzx cx, [good_record_number]
        or cl, cl
        jz .no_default
        cld
.loop_search:
        lodsb
        cmp al, ah
        je .found_it
        loop .loop_search
        
.no_default:                                ; no default record, do nothing.
        ret
        
.found_it:
	push ax
	call hide_auto_hides
	pop ax
	xor ah, ah
        call do_boot_record
        ret
        
;=============================================================================
;do_boot_record ---- really boot the given record.
;input:
;      ax =  the boot record number.
;=============================================================================
do_boot_record:
        mov bl, SIZE_OF_BOOTRECORD
        mul bl

        lea si, [boot_records]
        add si, ax

	mov bx, [si + struc_bootrecord.flags]

	test bx, INFOFLAG_ISSPECIAL
	jz .boot_drv_part

	call do_special_record
	jmp .end_clean

.boot_drv_part:
%ifndef DISABLE_CDBOOT
	test bx, DRVFLAG_ISCDROM
	jz .normal_boot

	mov dl, [si + struc_bootrecord.drive_id]
	mov di, disk_buf1
	call get_cdrom_boot_catalog
	jc .disk_error

	push si
	mov si, di
	mov di, disk_buf
	call find_boot_catalog
	pop si

	or cx, cx
	jz .no_system
	cmp cx, 1
	je .go_boot_cdrom

	push si
	mov si, di
	call choose_cdimg
	pop si
	jc .end_clean

	mov cl, SIZE_OF_BOOT_CATALOG
	mul cl

	add di, ax

.go_boot_cdrom:
	push dx
	push di
        call preload_keystrokes     ; preload the keystrokes into key buffer.
        call reset_video_mode
	pop di
	pop dx
	call boot_cdrom
	jmp short .boot_fail

%endif

.normal_boot:
        call boot_the_record

.boot_fail:
        or al, al
        jz .no_system

.disk_error:
        call show_disk_error
        ret

.no_system:
        mov si, [str_idx.no_system]
        call error_box
	ret

.end_clean:
	call draw_screen
        ret
        

;=============================================================================
;do_special_record ---- execute a special boot record.
;input:
;      si ->  the boot record.
;=============================================================================
do_special_record:
	call reset_video_mode
	mov al, [si + struc_bootrecord.type]

	cmp al, SPREC_POWEROFF
	jne .chk_rst
	call power_off

.chk_rst:
	cmp al, SPREC_RESTART
	jne .chk_quit
	call reboot

.chk_quit:
	cmp al, SPREC_QUIT
	jne .chk_bootprev

%ifdef EMULATE_PROG
        mov ax, 0x4c00                          ; exit to dos
        int 0x21                                ;
%else
        int 0x18                                ; return to BIOS
%endif

.chk_bootprev:
	cmp al, SPREC_BOOTPREV
	jne .end
	call boot_prev_mbr

.end:
	ret

;=============================================================================
;do_schedule ---- implement the schedule table
;input:
;      none
;output:
;      default_record set to the scheduled record
;=============================================================================
do_schedule:
        pusha
        call get_realtime
        jc .end

        mov [tmp_schedule_begin], ax
        mov [tmp_schedule_day], dx
        xor cx, cx
        lea si, [boot_records]

.loop_check:
        test word [si + struc_bootrecord.flags], INFOFLAG_SCHEDULED
        jz .check_next

        call check_bootrecord
        jc .check_next

        call get_record_schedule

        cmp [tmp_schedule_begin], ax 
        jb .check_next
        cmp [tmp_schedule_begin], bx
        ja .check_next

        test dx, [tmp_schedule_day]
        jz .check_next

        mov [default_record], cl

.check_next:
        inc cl
        add si, SIZE_OF_BOOTRECORD
        cmp cl, MAX_RECORD_NUM
        jb .loop_check

.end:
        popa
        ret

;=============================================================================
; auth_record_cmd ---- check if a record command can be ran.
; output:
;       cf = 0, auth granted.
;       cf = 1, auth failed.
;=============================================================================
auth_record_cmd:
        cmp byte [good_record_number], 0
        je .failed
        
        call confirm_security_passwd
        jc .failed

        ret

.failed:
        stc
        ret


;=============================================================================
; show_disk_error ---- show the disk error box.
;=============================================================================
show_disk_error:
        mov si, [str_idx.disk_error]
        lea di, [tmp_buffer]
        push di
        call strcpy
        movzx ax, [disk_errno]
        mov cl, 2
        call htoa
        pop si
        call error_box
        ret

;=============================================================================
init_bootmenu_width:
        mov byte [main_win_width], DEF_MAIN_WIN_WIDTH
        mov byte [menu_width], DEF_MENU_WIDTH

	mov al, [bootmenu_style]
	xor cx, cx

	or al, al
	jz .end
	add cl, BM_FLAGS_WIDTH

	cmp al, 1
	je .end
	add cl, BM_NUMBER_WIDTH

	cmp al, 2
	je .end
	add cl, BM_TYPE_WIDTH
.end:
	sub [main_win_width], cl
	sub [menu_width], cl
	mov bx, [str_idx.menu_title]
	add bx, cx
	mov [bootmenu_title_str], bx

        mov al, [main_window_col]
        mov ah, [screen_width]
        sub ah, [main_win_width]
        sub ah, 2
        cmp al, ah
        jbe .no_move

	mov [main_window_col], ah

.no_move:
	ret

;=============================================================================
;save_boot_manager ---- save boot manager to disk.
;input:
;      none
;output:
;      cf = 0 success
;      cf = 1 failed
;=============================================================================
save_boot_manager:
	pusha
	push es
	push ds

;Copy data area to backup seg
	xor si, si
	xor di, di

	push word KNLBACKUP_SEG
	pop es

	mov cx, end_of_sbmk_data
	cld
	rep movsb

;calculate checksum
	push es
	pop ds

	xor si, si
	mov cx, [total_size]
	mov byte [checksum], 0
	call calc_checksum                      ; calculate the checksum.
	neg bl
	mov [checksum], bl

	mov dl, [kernel_drvid]
	lea si, [kernel_sects1]
	mov cx, SBM_SAVE_NBLKS
	xor di, di

	pop ds

.loop_save_blk:
	push cx

	lodsb
	mov cl, al			; number of sectors for this block
	lodsd
	mov ebx,eax			; lba address for this block
        
	mov ax, ( INT13H_WRITE << 8 ) | 1 

	clc
	or cx, cx
	jz .write_end

.loop_write:
	call disk_access
	jc .write_end
        
	add di, SECTOR_SIZE
	inc ebx
	loop .loop_write

	pop cx
	loop .loop_save_blk

	clc
	jmp short .end

.write_end:
	pop cx

.end:
	pop es
	popa
	ret


;=============================================================================
;hide_auto_hides ---- hide all partitions that marked auto hide,
;                     except the focus record.
;input:
;      none
;output:
;      cf = 0 success
;      cf = 1 failed
;=============================================================================
hide_auto_hides:
        movzx cx, byte [good_record_number]
        or cl, cl                               ; if no good record then go to
        jz .end_ok                              ; init directly.
        
        lea di, [good_record_list]
        mov dl, SIZE_OF_BOOTRECORD

; hide all auto hide partitions.
.loop_hide:
        mov al, [di]
        inc di

        cmp ch, [focus_record]                  ; do not hide the focus record.
        je .not_hide

        mul dl
        lea si, [boot_records]
        add si, ax
        mov ax, [si + struc_bootrecord.flags]
        test ax, INFOFLAG_AUTOHIDE
        jz .not_hide
        test ax, INFOFLAG_HIDDEN
        jnz .not_hide
        call toggle_record_hidden
        jc .hidden_error
        call recheck_same_records
        jc .disk_error

.not_hide:
        inc ch
        cmp ch, cl
        jb .loop_hide
        
.end_ok:
        clc
        ret
        
.hidden_error:
        or ax, ax
        jz .cannot_hide
.disk_error:
        call show_disk_error
        jmp short .end

.cannot_hide:
        mov si, [str_idx.toggle_hid_failed]
        call error_box
.end:
        stc
        ret

;=============================================================================
; boot_prev_mbr ---- boot previous MBR
;=============================================================================
boot_prev_mbr:
; read partition table
        push es
        xor ebx, ebx
        mov es, bx
        mov dl, [kernel_drvid]
        mov di, BOOT_OFF
        mov ax, (INT13H_READ << 8) | 0x01
        call disk_access
        pop es
        jc .disk_failed

        push dx
        push di
        call ask_save_changes
        call hide_auto_hides
        call reset_video_mode
        pop di
        pop dx

	call uninstall_myint13h

; copy previous mbr to Boot Offset 0x7c00
        cld
        mov cx, SIZE_OF_MBR
        lea si, [previous_mbr]
        xor ax, ax
        push ax
        pop es
        rep movsb

        push ax
        pop ds

        xor bp, bp                          ; might help some boot problems
        mov ax, BR_GOOD_FLAG                ; boot signature (just in case ...)
        jmp 0:BOOT_OFF                      ; start boot sector

.disk_failed:
        call show_disk_error
.end:
        ret

;=============================================================================
;calc_checksum ---- calculate the checksum of the kernel program.
;input:
;      ds:si -> start of the checksum area
;      cx = checksum size
;output:
;      bl = the checksum value.
;=============================================================================
calc_checksum:
        push cx
        push ax
        push si
        xor bl, bl
        cld
.loop_calc:
        lodsb
        add bl, al
        loop .loop_calc
        pop si
        pop ax
        pop cx
        ret


;==============================================================================
; CD-ROM Boot Stuff
;==============================================================================

%ifndef DISABLE_CDBOOT
;==============================================================================
;choose_cdimg ---- let user choose a cdimg to boot
;input ds:si -> buffer to store boot catalogs
;      cx = number of entries
;output cf =0 ok, ax = user choice
;       cf =1 cancel
;==============================================================================
choose_cdimg:
	pusha

	cmp cl, 8
	jb .no_adjust
	mov cl, 8

.no_adjust:
	mov ax, [str_idx.cdimg_menu_title]
	mov [tmp_cdimg_menu + struc_menu.title], ax
	mov [tmp_cdimg_menu + struc_menu.n_items], cx
	xor ax, ax
	mov [tmp_cdimg_menu + struc_menu.cur_item], ax
	mov word [tmp_cdimg_menu + struc_menu.cmd_tbl], tmp_cmd_table
	mov word [tmp_cdimg_menu + struc_menu.hkey_tbl], tmp_hkey_table
	mov word [tmp_cdimg_menu + struc_menu.str_tbl], tmp_string_table

        mov ax, [color.cmd_menu_winframe]
        mov [tmp_cdimg_menu + struc_menu.win_attr], ax
        mov ax, [color.cmd_menu_normal]
        mov [tmp_cdimg_menu + struc_menu.norm_attr], ax
        mov ax, [color.cmd_menu_focus]
        mov [tmp_cdimg_menu + struc_menu.focus_attr], ax

	push cx

	xor bx, bx
	mov dx, '1 '
	mov di, tmp_menuitem_str

	cld
.loop_init_cdmenu:
	movzx ax, [si + struc_boot_catalog.media_type]
	or al, al
	jz .end_init

	shl al, 1

	push si
	push di
	push cx

	mov si, str_idx.cdimg_menu_strings
	add si, ax
	mov si, [si]

	mov ax, dx
	stosw
	mov ax, '- '
	stosw
	mov cx, 26
	call strncpy
	pop cx
	pop di
	pop si

	mov [tmp_string_table + bx], di
	mov ax, do_cdimg_choose
	mov [tmp_cmd_table + bx], ax

	inc bx
	inc bx
	inc dl
	add di, 32
	add si, SIZE_OF_BOOT_CATALOG
	loop .loop_init_cdmenu

.end_init:
	mov si, tmp_cdimg_menu
	call calc_menusize

	mov ax, [si + struc_menu.win_size]
	mov bx, [screen_width]
	mov cl, 1
	shr bx, cl
	shr ax, cl
	mov cx, 0x7F7F
	and bx, cx
	and ax, cx
	sub bx, ax
	mov [si + struc_menu.win_pos], bx

	xor ax, ax
	dec ax
	mov [cdimg_choice_result], ax

	pop cx
	call popup_menu
	cmp cx, [cdimg_choice_result]
	ja .ok
	stc
.ok:
	popa
	mov ax, [cdimg_choice_result]
	ret
	
do_cdimg_choose:
	mov ax, [si + struc_menu.cur_item]
	mov [cdimg_choice_result], ax
	ret

%endif

%endif