-
Notifications
You must be signed in to change notification settings - Fork 130
/
asm.S
17262 lines (14379 loc) · 353 KB
/
asm.S
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
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2004 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Note: These functions defined in this file may be called from C.
* Be careful of that you must not modify some registers. Quote
* from gcc-2.95.2/gcc/config/i386/i386.h:
1 for registers not available across function calls.
These must include the FIXED_REGISTERS and also any
registers that can be used without being saved.
The latter must include the registers where values are returned
and the register where structure-value addresses are passed.
Aside from that, you can include as many other registers as you like.
ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg
{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
*/
#define ASM_FILE
#define repair_memory_holes 1
#include "filesys.h"
#include "shared.h"
# define ABS(x) ((x) - EXT_C(main) + 0x8200)
.file "asm.S"
.text
/*
* In stage2, do not link start.S with the rest of the source
* files directly, so define the start symbols here just to
* force ld quiet. These are not referred anyway.
*/
.globl start, _start
start:
_start:
ENTRY(main)
.code16
/* If BP=0x07C0, it indicates boot from pxe or no-emulation cdrom */
/*
* Guarantee that "main" is loaded at 0x0:0x8200 in stage2 and
* at 0x0:0x2200 in stage1.5.
*/
ljmp $0, $ABS(codestart)
. = EXT_C(main) + 0x5
/* control byte: pxe, DUCE, tune
* bit 0 = 1: disable pxe
* bit 1 = 1: disable keyboard intervention in boot process
* bit 2 = 1: disable the "unconditional command-line entrance" feature
* bit 3 = 1: disable geometry tune
* bit 4 = 1: disable startup cdrom drive look-up.
* bit 5 = 1: enable HLT instruction for checkkey idle loop.
*/
.byte 0
. = EXT_C(main) + 0x6
.byte COMPAT_VERSION_MAJOR, COMPAT_VERSION_MINOR
. = EXT_C(main) + 0x8
VARIABLE(install_partition)
.long 0x00FFFFFF
#define preset_menu 0x307FFC
#define use_preset_menu 0x307FF8
. = EXT_C(main) + 0xC
VARIABLE(saved_entryno)
/* Note: GRUB for DOS uses this for the commandline preset_menu.
* A preset_menu can be embedded in the commandline of GRUB.EXE.
* This new preset_menu overrides the built-in preset_menu.
* If the variable is not touched, and the first byte at
* config_file == 0, then the new menu at 0x0800 will work.
* If the variable here is cleared to 0, or the first byte at
* config_file != 0, then the built-in preset_menu will work.
*
* Do NOT change this variable to other value than 0.
*/
/* Some external utilities use this to locate the preset_menu. */
.long ABS(preset_menu_old)
//.long preset_menu /* it is now at a fixed address 0x307FFC */
. = EXT_C(main) + 0x10
VARIABLE(stage2_id)
.byte STAGE2_ID
VARIABLE(force_lba)
/* only bit 0 is now used for force_lba. */
/* bit 1=1 means we are booting as a Linux kernel. */
/* bit 2=0 enable single-key-selection feature. */
.byte 0x04
VARIABLE(version_string)
.string VERSION
VARIABLE(config_file) //0x821e 配置文件 最长65字节
.string "/menu.lst"
. = EXT_C(main) + 0x60
VARIABLE(hotkey_func) //int* //0x8260 外置热键功能
.long 0
VARIABLE(ext_timer) //0x8264 外部定时器 int*
.long 0
VARIABLE(grub_timeout) //0x8268 倒计时 int
.long -1
/* Utilities may use this to locate the bss starting address,
* where the "bootlace" signature is placed and preset-menu
* follows.
*/
. = EXT_C(main) + 0x6C #; bss starting address
preset_menu_old:
#if defined(HAVE_USCORE_USCORE_BSS_START_SYMBOL)
.long __bss_start - 0x300000
#elif defined(HAVE_USCORE_EDATA_SYMBOL)
.long _edata - 0x300000
#elif defined(HAVE_EDATA_SYMBOL)
.long edata - 0x300000
#else
#error no bss starting address
#endif
. = EXT_C(main) + 0x70
/* the real mode code continues... */
codestart:
jmp real_codestart
. = EXT_C(main) + 0x73
VARIABLE(debug_msg)
.byte 2
/* internal variables follow */
. = EXT_C(main) + 0x74
VARIABLE(menu_num_ctrl)
.long 0x2000
#include "grub4dos_int_ver.h"
#ifdef GRUB4DOS_INT_VER
.long GRUB4DOS_INT_VER
#else
.long 0
#endif
#undef GRUB4DOS_INT_VER
.long 0
. = EXT_C(main) + 0x80
VARIABLE(boot_drive)
.long 0xFFFFFFFF /* can be setup by external programs. */
VARIABLE(pxe_yip)
.long 0
VARIABLE(pxe_sip)
.long 0
VARIABLE(pxe_gip)
.long 0
. = EXT_C(main) + 0x90
VARIABLE(filesize)
.long 0, 0
VARIABLE(saved_mem_upper) /* maximum contiguous memory in KB starting at 1M and below 4G */
.long 0
VARIABLE(saved_partition)
.long 0
. = EXT_C(main) + 0xA0
VARIABLE(saved_drive)
.long 0
VARIABLE(no_decompression)
.long 0
VARIABLE(part_start)
.long 0, 0
. = EXT_C(main) + 0xB0
VARIABLE(part_length)
.long 0, 0
VARIABLE(fb_status)
.long 0
VARIABLE(is64bit)
.long 0
. = EXT_C(main) + 0xC0
VARIABLE(saved_mem_higher) /* maximum contiguous memory in KB starting at 4G */
.long 0, 0
VARIABLE(cdrom_drive)
.long 0xFFFFFFFF /* default is the invalid drive number */
VARIABLE(ram_drive)
.long 0x7F /* default is a floppy */
. = EXT_C(main) + 0xD0
VARIABLE(rd_base)
.long 0, 0
VARIABLE(rd_size)
.long 0, 1 /* default is 4G */
. = EXT_C(main) + 0xE0
/* mem alloc array structure
long // mem block starting address(16 byte align)
// bit0=0 for free mem, 1 for used mem
// bit 2:1 =0 for program code
// =1 for environment
// =2 for extra data
// =3 reserved
// bit 3=reserved
long // process id.
// 0 - 255 for kernel, others for user program
*/
VARIABLE(mem_alloc_array_start)
.long 0x800000 /* at 8M */
VARIABLE(mem_alloc_array_end)
.long 0xA00000 /* at 10M */
VARIABLE(page_map_start)
.long 0xA00000 /* at 10M */
VARIABLE(page_map_end)
.long 0xE00000 /* at 14M */
. = EXT_C(main) + 0xF0
VARIABLE(free_mem_start) /* unused mem starting address. */
.long SYSTEM_RESERVED_MEMORY /* may increase later. */
VARIABLE(free_mem_end) /* unused mem ending address */
.long 0 /* 0 for 4G(i.e., unlimited) */
VARIABLE(saved_mmap_addr) /* system memory map starting addess */
.long ABS(EXT_C(end_of_low_16bit_code))
VARIABLE(saved_mmap_length) /* system memory map length */
.long 0
. = EXT_C(main) + 0x100
VARIABLE(addr_system_functions)
.long ABS(EXT_C(system_functions))
VARIABLE(addr_system_variables)
.long ABS(EXT_C(system_variables))
VARIABLE(menu_cfg) /*char menu_cfg[]*/
.long 0x1110
VARIABLE(iso_types)/*char iso_types*/
.byte 0xff
.byte 0 //0x830D 未使用
.byte 0 //0x830E 未使用
.byte 0 //0x830F 未使用
. = EXT_C(main) + 0x110
//VARIABLE(addr_saved_dir)
// .long ABS(EXT_C(saved_dir))
.byte 0 //0x8310 未使用
VARIABLE(menu_tab) //0x8311 菜单标签 byte
.byte 0
/*
位2: 0/1=显示按键帮助/不显示按键帮助 04
位3: 0/1=侧边对齐/中心对齐 08
位4: 0/1=高亮项目背景短/满 10
位5: 0/1=英文/中文 20
位6: 0/1=左对齐/右对齐 40
位7: 0/1=打印版本信息/不打印版本信息 80
*/
VARIABLE(menu_tab_ext) //0x8312 菜单标签 byte
.byte 0
/*
位0: 1=已在图形模式 01
位1: 1=已加载背景图像 02
位2: 1=已加载字库 04
*/
.byte 0 //0x8313 未使用
VARIABLE(errnum)
.long 0
VARIABLE(current_drive)
.long 0xFFFFFFFF
VARIABLE(current_partition)
.long 0
. = EXT_C(main) + 0x120
VARIABLE(filemax)
.long 0, 0
VARIABLE(filepos)
.long 0, 0
. = EXT_C(main) + 0x130
VARIABLE(debug)
/* 0 for silent, 1 for normal, 2..MAX_UINT for verbose.
* if debug == MAX_UINT or debug < 0, then filesystem debug mode is on.
* (MAX_UINT is 0x7FFFFFFF)
*/
.long 0
VARIABLE(current_slice)
.long 0
VARIABLE(dos_drive_geometry)
# byte 0=DOS boot drive: 0 for floppy, 0x80 for harddrive.
# byte 1=max head number for DOS boot drive=(number of heads - 1).
# byte 2=sectors per track for DOS boot drive.
# byte 3=0(reserved).
.long 0
VARIABLE(dos_part_start)
# partition start sector number(or hidden sectors) for DOS boot drive.
.long 0
. = EXT_C(main) + 0x140
VARIABLE(buf_track)
.long -1
.long 0 /* buf_track will grow to 64-bit */
VARIABLE(buf_drive)
.long -1
VARIABLE(current_x_resolution) //0x834c 当前水平像素 int
.long 0
. = EXT_C(main) + 0x150
VARIABLE(usb_count_error) //bit 7: 0 = usb_count; 1 = usb_error
.byte 0
VARIABLE(usb_drive_num)
.byte 0,0,0,0
VARIABLE(usb_md_address)
.word 0
VARIABLE(count_ms)
.word 0
VARIABLE(beep_frequency)
.word 0
.byte 0 //0x835b 未使用
VARIABLE(cursor_state) //0x835C 鼠标状态 int
.long 0 // bit 0=1 show cursor,bit 1=1 show splashimage
. = EXT_C(main) + 0x160
real_codestart:
cli
cld
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
movl $STACKOFF, %esp
sti /* added 2008-08-04 */
/* save DX at 0x8206, overwriting the unused version numbers. */
movw %dx, ABS(EXT_C(install_partition)) - 2
# it is time to do this after DX has been saved.
pushal
movw $0x0003, %ax /* set display mode: 80*25 color text */
int $0x10 # this might destroy our DX
popal
/* check for fb structure */
cmpl $0x46424246, (0x21b4)
jnz 1f
movw %ax, (0x21b4) //destroy it to prevent repeated fb recognition
movl (0x21ae), %eax
movb (0x21ad), %al
movl %eax, ABS(EXT_C(fb_status))
cmpb $0,%ah
jnz 1f
/* it is the floppy (fd0) */
testb $0x01,0x0410
jnz 1f
/* no floppy in BDA, so we set floppy count to 1. */
orb $0x01,0x0410 /* diskette set as present */
andb $0x3F,0x0410 /* clear bit 6-7 */
// movb $(FB_DRIVE),%dl
1:
movb 0x0410, %al
movb %al, ABS(EXT_C(floppies_orig))
movb 0x0475, %al
movb %al, ABS(EXT_C(harddrives_orig))
//movb $((int13_handler_end - int13_handler + 0x3ff) / 0x400), ABS(int13_handler)
movb $1, %cl // CX != 0, indicating our int13 handler not present
movl 0x54, %eax
movl %eax, ABS(EXT_C(ROM_int15))
movl 0x4C, %eax
movl %eax, ABS(EXT_C(ROM_int13))
movl %eax, ABS(EXT_C(ROM_int13_dup))
cmpl $0xC0000000, %eax
jnb 1f
cmpl $0x5A000000, %eax // cmpl $0x9A000000, %eax
jb 1f
andl $0x3FFFFF, %eax
// cmpl $0x100, %eax
cmpl $(int13_handler_entry - int13_handler), %eax
jnz 1f
movw 0x413, %ax /* Memory size in Kb */
shlw $6, %ax /* Memory size in paragragh */
cmpw 0x4E, %ax /* 0000:004E=current int 13 segment */
jne 1f /* not hooked */
movw %ax, %ds /* DS=current int13 code segment */
/* check our int13 signature "$INT13SFGRUB4DOS" */
// cmpl $0x544E4924, 0x103 /* $INT */
cmpl $0x544E4924, (int13_handler_entry - int13_handler + 3) /* $INT */
jnz 2f
// cmpl $0x46533331, 0x107 /* 13SF */
cmpl $0x46533331, (int13_handler_entry - int13_handler + 7) /* 13SF */
jnz 2f
// cmpl $0x42555247, 0x10B /* GRUB */
cmpl $0x42555247, (int13_handler_entry - int13_handler + 0xb) /* GRUB */
jnz 2f
// cmpl $0x534F4434, 0x10F /* 4DOS */
cmpl $0x534F4434, (int13_handler_entry - int13_handler + 0xf) /* 4DOS */
jnz 2f
movl (EXT_C(ROM_int13) - int13_handler), %eax /* 0x1C=ROM int 13 */
cmpl $0x5A000000, %eax // cmpl $0x9A000000, %eax
jb 2f /* not our handler */
movl (EXT_C(ROM_int15) - int13_handler), %eax /* 0x0C=ROM int 15 */
cmpl $0x5A000000, %eax // cmpl $0x9A000000, %eax
jb 2f /* not our handler */
/* restore old emu data, except the first byte of handler size. */
// movw $(0x140 - 1), %cx
mov $(restore_old_emu - int13_handler - 1), %cx
movw $1, %si /* DS=current int13 code segment */
movw $ABS(int13_handler + 1), %di /* ES=0 */
repz movsb
#ifdef CDROM_INIT
/* calculate the new max_cdrom_id. */
movw $2, %si
lodsw /* AL=atapi_dev_count, AH=min_cdrom_id */
addb %ah, %al
decw %ax /* AL=max_cdrom_id */
movb %al, %cs:ABS(max_cdrom_id) /* CS=0 */
#endif
/* initialize bios_drive_map with hooked_drive_map_1 */
#if MAP_NUM_16
movw $(DRIVE_MAP_SIZE * DRIVE_MAP_SLOT_SIZE / 4), %cx
movw $ABS(EXT_C(hooked_drive_map_1)), %si /* CS=0 */
#else
movw $(DRIVE_MAP_SIZE * DRIVE_MAP_SLOT_SIZE / 2), %cx
movw $ABS(EXT_C(hooked_drive_map)), %si /* CS=0 */
#endif
movw $ABS(EXT_C(bios_drive_map)), %di /* ES=0 */
cs repz movsw
movw $(0x3000 - 0x2800)/8, %cx
movw $0x2800, %si
3:
cmpl $0x47415246, (%si) /* FRAGMENT */
je 3f
addw $8, %si
loop 3b
jmp 2f
3:
cmpl $16, 0xfc //16 FRAGMENT
jne 3f
#if MAP_NUM_16
pushw %si
// BIOS-hooked_drive_map_2 -> G4D-hooked_drive_map_2
movw $(DRIVE_MAP_SIZE * DRIVE_MAP_SLOT_SIZE / 4), %cx
pushw %cx
subw $((DRIVE_MAP_SIZE / 2 + 1) * DRIVE_MAP_SLOT_SIZE), %si
pushw %si
movw $ABS(EXT_C(hooked_drive_map_2)), %di /* ES=0 */
repz movsw
//BIOS_hooked_drive_map_2 -> G4D-bios_drive_map_8
popw %si
popw %cx
movw $ABS(EXT_C(bios_drive_map) + ((DRIVE_MAP_SIZE / 2) * DRIVE_MAP_SLOT_SIZE)), %di /* ES=0 */
repz movsw
popw %si
#endif
3:
addw $8, %si
movw $ABS(EXT_C(hooked_fragment_map)), %di
movw $FRAGMENT_MAP_SLOT_SIZE/2, %cx
repz movsw
/* Now CX = 0, indicating our int13 handler is present. */
2:
xorw %ax, %ax
movw %ax, %ds /* DS=0 */
1:
jcxz 1f /* our int13 handler is present, skip */
/* clear highest 2 bits of byte at 0x410. */
andb $0x3F, 0x0410 /* clear bit 6-7 */
cmpw $0x07C0, %bp /* boot from pxe or cdrom? */
je 3f /* yes, directly mask off floppy. */
/* check fbinst boot */
movl ABS(EXT_C(fb_status)), %ecx
jecxz 2f /* did not boot by fbinst, continue */
cmpb $0, %ch /* fbinst boot drive is floppy? */
jz 1f /* yes, skip */
2:
/* check DOS boot of grub.exe */
movl ABS(EXT_C(dos_drive_geometry)), %ecx
jecxz 2f /* did not boot off DOS, continue */
cmpb $0, %cl /* DOS boot drive is floppy? */
jz 1f /* yes, skip */
2:
/* check saved DX register */
movw ABS(EXT_C(install_partition)) - 2, %cx
cmpb $0, %cl /* BIOS boot drive is floppy? */
jz 1f /* yes, skip */
3:
/* did not boot from floppy. clear lowest bit of byte at 0x410. */
andb $0xFE, 0x0410 /* clear bit 0 */
1:
/* check the BIOS type (currently only for Bochs) */
movw $0xF000, %ax
movw %ax, %es /* ES=0xF000 */
movw $0xFF00, %di
movw $ABS(bochs_copygrght_string), %si
movw $0x22, %cx
repz cmpsw
setz ABS(EXT_C(bios_id)) /* 1 for bochs, 0 for unknown. */
xorw %ax, %ax
movw %ax, %es /* ES=0 */
// pushw %dx /* DL=boot drive */
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
sti
/* check the DUCE indicator */
movl 0x5FC, %eax
movl %eax, %ecx
testb $0x0C, %al
jz 1f
orb $0x0C, %al
cmpl $0x4543554C, %eax
jne 1f
andb $0x0C, %cl /* bit2=DUCE, bit3=chs-no-tune */
orb %cl, ABS(EXT_C(main)) + 5 /* at 0x8205 */
1:
movw $1, %bx /* BL=1, BH=0 */
testb $0x02, ABS(EXT_C(main)) + 5 /* disable keyboard intervention? */
jnz 4f /* yes */
movl 0x46c, %eax /* initial tick */
addl $5, %eax /* wait 0.27 seconds */
pushl %eax
2:
/* checkkey 'c' */
pushw %bx
movb $0x01, %ah /* checkkey */
int $0x16
popw %bx
jz 1f /* no keypress */
/* getkey */
pushw %bx
movb $0x00, %ah /* getkey */
int $0x16
popw %bx
cmpw $KEY_IC, %ax /* insert char */
jne 3f
orb $1, %bh /* DEBUG_KEY pressed */
jmp 2b /* get next key */
3:
orb $0x20, %al
cmpb $0x73, %al /* is "S"? */
jne 3f
orb $2, %bh /* Single Sector for Buggy BIOS */
3:
cmpb $0x63, %al /* is "C"? */
jne 2b /* no, get next key */
/* "C" is pressed. */
testb $0x04, ABS(EXT_C(main)) + 5 /* at 0x8205, bit2=DUCE */
jnz 2b
/* Bypass all config files */
movb $0, %bl
jmp 2b
1:
popl %eax
pushl %eax
movl 0x46c, %ecx /* current tick */
cmpl %eax, %ecx
jnb 1f
subl $5, %eax
cmpl %eax, %ecx
jnb 2b
cmpl $5, %ecx
jb 2b
1:
popl %eax
4:
movb %bh, ABS(EXT_C(debug_boot))
testb %bl, %bl
jnz 1f
movl $0, ABS(EXT_C(use_config_file))
1:
// popw %dx /* DL=boot drive */
// movzbl %dl, %eax
// movl %eax, ABS(EXT_C(boot_drive))
xorw %ax, %ax
movw %ax, %ds
movw %ax, %es
testb $0x02, ABS(EXT_C(main)) + 5 /* disable keyboard intervention? */
jnz 4f /* yes */
//pushw %bx
/* clear keyboard buffer */
2:
movb $0x01, %ah /* checkkey */
int $0x16
jz 1f /* no keypress */
movb $0x00, %ah /* getkey */
int $0x16
jmp 2b
1:
//popw %bx
4:
/* Oops! I forgot to enable A20. Thanks to zw2312914. */
movw $0x00ff, %cx # try so many times on failure
movw $0x0101, %dx # DL=1(enable A20), DH=1(debug on)
cli /* yes, keep interrupt off when controlling A20 */
call enable_disable_a20 # EAX, CX modified
sti
jz 1f /* enabled A20 with success */
/* A20 control failed. Notify the user. */
pushw $ABS(bootup_a20_failure) # the format string
call realmode_printf
//addw $2, %sp # adjust the stack pointer, but not required for now.
jmp hard_stop
bootup_a20_failure:
.ascii "\r\ngrub4dos: A20 failure. Please report with error code(if any in the above line).\r\n\0"
1:
movw %ds:0x046c, %dx
xorl %ecx, %ecx
1:
movw %ds:0x046c, %ax
cmpw %dx, %ax
je 1b
1:
cmpw %ds:0x046c, %ax
jne 2f
pushw %dx
pushw %ax
movw $0xcf8, %dx
inw %dx, %ax
popw %ax
popw %dx
incl %ecx
cmpl $0, %ecx
jmp 1b
2:
xorl %edx, %edx
movl %ecx, %eax
movl $0x37, %ecx
divl %ecx
movw %ax, ABS(EXT_C(count_ms))
xorb %bh, %bh
movb $0, %bl
movw $0x1003, %ax
int $0x10
/* transition to protected mode */
call EXT_C(real_to_prot)
.code32
/* enable preset_menu */
movl $1, use_preset_menu
/* if force_cdrom_as_boot_device==0, we are running by configfile, so we skip preset menu check */
cmpl $0, ABS(EXT_C(force_cdrom_as_boot_device))
je 1f /* skip all */
/* before clearing the bss, we move preset_menu to 0x800 */
#if 0
movl preset_menu, %eax
#if defined(HAVE_USCORE_USCORE_BSS_START_SYMBOL)
cmpl $__bss_start - 0x300000, %eax
#elif defined(HAVE_USCORE_EDATA_SYMBOL)
cmpl $_edata - 0x300000, %eax
#elif defined(HAVE_EDATA_SYMBOL)
cmpl $edata - 0x300000, %eax
#else
#error no bss starting address
#endif
jnz 4f /* use old bootp for diskless */
#endif
xorl %eax, %eax
cmpb %al, ABS(EXT_C(config_file)) /* AL == 0 */
jnz 2f
movl ABS(EXT_C(saved_entryno)), %ebx
testl %ebx, %ebx
jnz 3f /* use menu embedded in commnad-line of grub.exe */
2:
/* use builtin preset_menu */
/* set the starting address of the preset_menu */
#if defined(HAVE_USCORE_USCORE_BSS_START_SYMBOL)
movl $__bss_start - 0x300000, %esi
#elif defined(HAVE_USCORE_EDATA_SYMBOL)
movl $_edata - 0x300000, %esi
#elif defined(HAVE_EDATA_SYMBOL)
movl $edata - 0x300000, %esi
#else
#error no bss starting address
#endif
cld
addl $16, %esi /* skip 4 bytes of B0 02 1A CE */
/* skip 4 bytes of reserved */
/* skip 4 bytes of reserved */
/* skip 4 bytes of zeroes */
/* check LZMA */
cmpb $0xE0, (%esi)
ja 5f /* not lzma */
movl 1(%esi), %ecx
jecxz 5f /* not lzma */
cmpl $0x1000, %ecx
jb 5f /* unaccepted lzma */
movl $0x80000000, %eax
xorl %edx, %edx /* dividend=EDX:EAX */
divl %ecx /* divisor=ECX */
/* result: EAX=quotient, EDX=remainder */
testl %edx, %edx
jnz 5f /* unaccepted lzma */
cmpl %edx, 9(%esi) /* EDX=0 */
jnz 5f /* unaccepted lzma */
/* now it is LZMA */
movl $0x10000, %ecx /* move 256KB of the menu ... */
movl $0x110000, %edi /* ... to address 1M+64K */
repz movsl
movl $0x110000, preset_menu /* use new menu at 1M+64K */
jmp 4f
5:
movl $0x400, %ecx /* move 4KB of the menu ... */
movl $0x800, %edi /* ... to 0x800 */
repz movsl
3:
movl $0x0800, preset_menu /* use new menu at 0x800 */
4:
// /* if force_cdrom_as_boot_device==0, we are running by configfile, so we do not clear bss */
// cmpl $0, ABS(EXT_C(force_cdrom_as_boot_device))
// je 1f
/* before clearing bss, move 32-bit code to above 3M. */
/* This need A20 to be enabled. */
movl $ABS(EXT_C(end_of_low_16bit_code)), %esi
leal 0x300000(%esi), %edi
#if defined(HAVE_USCORE_USCORE_BSS_START_SYMBOL)
movl $__bss_start, %ecx
#elif defined(HAVE_USCORE_EDATA_SYMBOL)
movl $_edata, %ecx
#elif defined(HAVE_EDATA_SYMBOL)
movl $edata, %ecx
#else
#error no bss starting address
#endif
subl %edi, %ecx
shrl $2, %ecx
cld
repz movsl
/* clean out the bss */
// /* set %edi to the bss starting address */
//#if defined(HAVE_USCORE_USCORE_BSS_START_SYMBOL)
// movl $__bss_start, %edi
//#elif defined(HAVE_USCORE_EDATA_SYMBOL)
// movl $_edata, %edi
//#elif defined(HAVE_EDATA_SYMBOL)
// movl $edata, %edi
//#else
//#error no bss starting address
//#endif
/* set %ecx to the bss end */
#if defined(HAVE_END_SYMBOL)
movl $end, %ecx
#elif defined(HAVE_USCORE_END_SYMBOL)
movl $_end, %ecx
#else
#error no bss ending address
#endif
/* compute the bss length */
subl %edi, %ecx
shrl $2, %ecx
/* zero %eax */
xorl %eax, %eax
/* set the direction */
cld
/* clean out */
rep stosl
/* initialize IDT */
movl $0x300000, %edi
movl $0x100, %ecx
xorl %ebx, %ebx
2:
movl $ABS(default_iret), %eax
cmpb $0xFE, %cl // int 0x02 nmi
jne 3f
movl $ABS(int2_nmi), %eax
jmp 5f
3:
cmpb $0xF8, %cl
jg 5f // int 0x00 - 0x07
cmpb $0xF1, %cl
jl 3f // int 0x10 - 0xFF
cmpb $8, %bl
jne 4f
movl $ABS(irq_0), %eax // int 0x08
jmp 5f
4:
movl $(irq_2 - irq_1), %eax // int 0x09 - 0x0F
subl $0x09, %ebx
mull %ebx
addl $0x09, %ebx
addl $ABS(irq_1), %eax
jmp 5f
3:
cmpb $0x90, %cl
jg 5f // int 0x10 - 0x6F
cmpb $0x88, %cl
jl 3f // int 0x78 - 0xFF
movl $(irq_9 - irq_8), %eax // int 0x70 - 0x77
subl $0x70, %ebx
mull %ebx
addl $0x70, %ebx
addl $ABS(irq_8), %eax
//jmp 5f
3:
5:
stosw
shrl $16, %eax
movl $(0x8E000000 + 40), %edx
xchgl %eax, %edx
stosl
xchgl %eax, %edx
stosw
incl %ebx
loop 2b
1:
call EXT_C(prot_to_real)
.code16
call EXT_C(real_to_prot)
.code32
sti
/*
* Call the start of main body of C code, which does some
* of it's own initialization before transferring to "cmain".
*/
call EXT_C(init_bios_info)
call EXT_C(cmain)
/*
* This call is special... it never returns... in fact it should simply
* hang at this point!
*/
ENTRY(stop)
call EXT_C(prot_to_real)
/*
* This next part is sort of evil. It takes advantage of the
* byte ordering on the x86 to work in either 16-bit or 32-bit
* mode, so think about it before changing it.
*/
/* No external program ever calls HARD_STOP. HARD_STOP is only called
* by the asm.S itself, and all calls are from real mode. So we
* could(and should) use .code16 here clearly.
*/
.code16
hard_stop:
sti
//hlt
call conditional_hlt
jmp hard_stop
conditional_hlt:
//DS should already be 0, but ...
pushw $0
popw %ds // just in case DS changed by buggy BIOS
testb $0x20, 0x8205 // Enable HLT?
jz 1f // No. Skip the HLT instruction.
hlt // Yes. Execute the HLT instruction.
1:
ret // on return, DS=0
.code32
VARIABLE(force_cdrom_as_boot_device)
.long 1
VARIABLE(use_config_file)
.long 1