-
Notifications
You must be signed in to change notification settings - Fork 130
/
stage2.c
2329 lines (2127 loc) · 64.9 KB
/
stage2.c
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) 2000,2001,2002,2004,2005 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.
*/
#include <shared.h>
#include <term.h>
/* indicates whether or not the next char printed will be highlighted */
unsigned int is_highlight = 0;
static int open_preset_menu (void);
static int
open_preset_menu (void)
{
if (! use_preset_menu) //如果不存在预置菜单
return 0; //返回0
return grub_open (preset_menu); //打开压缩菜单 位置0x110000 最大尺寸0x40000=256k
}
static int read_from_preset_menu (char *buf, int max_len);
static int
read_from_preset_menu (char *buf, int max_len)
{
if (! use_preset_menu) //if (preset_menu == 0)
return 0;
return grub_read ((unsigned long long)(grub_size_t)buf, max_len, 0xedde0d90);
}
#define MENU_BOX_X ((menu_border.menu_box_x > 2) ? menu_border.menu_box_x : 2)
#define MENU_BOX_W ((menu_border.menu_box_w && menu_border.menu_box_w < (current_term->chars_per_line - MENU_BOX_X - 1)) ? menu_border.menu_box_w : (current_term->chars_per_line - MENU_BOX_X * 2)) //UEFI固件的右上角(0x2510)是宽字符
#define MENU_BOX_Y (menu_border.menu_box_y)
#define MENU_KEYHELP_Y_OFFSET ((menu_border.menu_keyhelp_y_offset < 5) ? menu_border.menu_keyhelp_y_offset : 0)
/* window height */
#define MENU_BOX_H ((menu_border.menu_box_h && menu_border.menu_box_h < (current_term->max_lines - MENU_BOX_Y - 6 - MENU_KEYHELP_Y_OFFSET)) ? menu_border.menu_box_h : (current_term->max_lines - MENU_BOX_Y - 6 - MENU_KEYHELP_Y_OFFSET))
/* line end */
#define MENU_BOX_E (MENU_BOX_X + MENU_BOX_W)
/* window bottom */
#define MENU_BOX_B ((menu_border.menu_box_b) ? menu_border.menu_box_b : (current_term->max_lines - 6 - MENU_KEYHELP_Y_OFFSET))
#define MENU_HELP_X ((menu_border.menu_help_x && menu_border.menu_help_x < current_term->chars_per_line) ? menu_border.menu_help_x : (MENU_BOX_X - 2))
#define MENU_HELP_E (menu_border.menu_help_x ? (menu_border.menu_help_w ? (menu_border.menu_help_x + menu_border.menu_help_w) : (current_term->chars_per_line - MENU_HELP_X)) : (MENU_BOX_E + 1))
#define NUM_LINE_ENTRYHELP ((MENU_KEYHELP_Y_OFFSET == 0) ? 4 : MENU_KEYHELP_Y_OFFSET) //
static int temp_entryno;
static short temp_num;
static char * *titles; /* title array, point to 256 strings. 标题数组,指向256个字符串*/
static unsigned short *title_boot;
static int default_help_message_destoyed = 1;
extern int num_text_char(char *p);
struct border menu_border = {218,191,192,217,196,179,2,0,2,0,0,2,0,0,0}; /* console */
char* print_color_characters (char *message, unsigned char *character, unsigned long long *color64_back, unsigned int *color_back);
char*
print_color_characters (char *message, unsigned char *character, unsigned long long *color64_back, unsigned int *color_back)
{
if (message[2] == ']')
{
current_color_64bit = *color64_back;
current_color = *color_back;
message += 3;
*character = *(message);
}
else if ((message[3] | 0x20) == 'x')
{
unsigned long long ull;
message += 2;
if (safe_parse_maxint((char **)&message,&ull) && *message == ']')
{
*color_back = current_color;
*color64_back = current_color_64bit;
current_color_64bit = ull;
current_color = color_64_to_8 (current_color_64bit);
*character = *(++message);
}
}
return message;
}
static void print_help_message (const char *message,int flags);
static void print_help_message (const char *message,int flags)
{
grub_u32_t j,x,k;
grub_u8_t c = *message;
int start_offcet = 0;
unsigned long long current_color_64bit_back;
unsigned int current_color_back;
if (flags==2)
{
for (j=0; j<NUM_LINE_ENTRYHELP; ++j)
{
if (MENU_BOX_B + 1 + j > current_term->max_lines)
return;
gotoxy (MENU_HELP_X, MENU_BOX_B + 1 + j);
for (x = MENU_HELP_X; x < (grub_u32_t)MENU_HELP_E; x++)
grub_putchar (' ', 255);
}
}
else
{
if(flags==0)
k = 4;
else
k = NUM_LINE_ENTRYHELP;
for (j=0; j<k; ++j)
{
if(flags==0)
{
if (MENU_BOX_B + 1 + menu_border.menu_keyhelp_y_offset + j > current_term->max_lines)
return;
gotoxy (MENU_HELP_X, MENU_BOX_B + 1 + menu_border.menu_keyhelp_y_offset + j);
}
else if(flags==1)
{
if (MENU_BOX_B + 1 + j > current_term->max_lines)
return;
gotoxy (MENU_HELP_X, MENU_BOX_B + 1 + j);
}
if(flags==1 || flags==0)
{
if (c == '\n')
c = *(++message);
if((menu_tab & 8) && flags==1)
start_offcet = MENU_HELP_X + ((MENU_HELP_E - MENU_HELP_X - num_text_char((char *)message))>>1);
else if((menu_tab & 0x40) && flags==1)
{
start_offcet = MENU_HELP_E - num_text_char((char *)message);
if(start_offcet<MENU_HELP_X)
start_offcet=MENU_HELP_X;
}
for (; fontx < (grub_u32_t)MENU_HELP_E;)
{
if (c && c != '\n' && fontx >= (grub_u32_t)start_offcet)
{
if (fontx == (grub_u32_t)MENU_HELP_E - (grub_u32_t)1)
if (c > 0x7F && !(menu_tab & 0x40))
{
grub_putchar (' ', 255);
continue;
}
//处理菜单项注释中的彩色字符
if (*(unsigned short*)message == 0x5B24)//$[
message = print_color_characters ((char *)message,&c,¤t_color_64bit_back,¤t_color_back);
grub_putchar (c, 255);
c = *(++message);
}
else
grub_putchar (' ', 255);
}
for (x = fontx; x < (grub_u32_t)MENU_HELP_E; x++)
grub_putchar (' ', 255);
}
}
}
}
static void print_default_help_message (char *config_entries);
static void
print_default_help_message (char *config_entries)
{
grub_u32_t i;
char buff[256];
if (menu_tab & 0x20)
i = grub_sprintf (buff,"\n按↑和↓选择菜单。");
else
i = grub_sprintf (buff,"\nUse the ↑ and ↓ keys to highlight an entry.");
if (! auth && password_buf)
{
if (menu_tab & 0x20)
grub_strcpy (buff + i,"按回车或b启动。\n"
"按p获得特权控制。");
else
grub_strcpy (buff + i,"Press ENTER or \'b\' to boot.\n"
"Press \'p\' to gain privileged control.");
}
else
{
if (menu_tab & 0x20)
grub_strcpy(buff + i,(config_entries?("按回车或b启动。\n"
"按e可在启动前编辑菜单命令,按c进入命令行。")
:("在当前行按e进行编辑,按d进行删除。\n"
"按O和o分别在其之前或之后插入新行。\n"
"编辑结束时按b启动,按c进入命令行,按ESC退回到主菜单。")));
else
grub_strcpy(buff + i,(config_entries?(" Press ENTER or \'b\' to boot.\n"
"Press \'e\' to edit the commands before booting, or \'c\' for a command-line.")
:(" At a selected line, press \'e\' to edit, \'d\' to delete,\n"
"or \'O\'/\'o\' to open a new line before/after.\n"
"When done, press \'b\' to boot, \'c\' for a command-line,\n"
"or ESC to go back to the main menu.")));
}
if(keyhelp_color)
{
if (!(splashimage_loaded & 2))
{
current_color_64bit = keyhelp_color | (console_color_64bit[COLOR_STATE_NORMAL] & 0x00ffffff00000000);
}
else
current_color_64bit = keyhelp_color;
current_term->setcolorstate (color_64_to_8 (current_color_64bit) | 0x100);
}
print_help_message(buff,0);
default_help_message_destoyed = 0;
}
// ADDED By STEVE6375
static int num_entries;
static char *get_entry (char *list, int num);
static char *
get_entry (char *list, int num)
{
int i;
if (list == (char *)titles)
return num < num_entries ? titles[num] : (int)0;
for (i = 0; i < num && *list; i++)
{
while (*(list++));
}
return list;
}
static char ntext[256];
static void clean_entry (char *text);
static void
clean_entry (char *text)
{
int i;
//(void) strcpy (ntext, text);
for (i = 0; i < 255 && text[i]; ++i)
{
int a = (int)(char)text[i];
// if (text[i] < (char)32 || text[i] > (char)127)
if (a < (int)32 || a > (int)127)
ntext[i] = 32;
else
ntext[i] = text[i];
}
ntext[i] = 0;
return;
}
static int checkvalue (void);
static int
checkvalue (void)
{
int i, value;
// Converts a string of ASCII numbers in a string to an integer
// leading spaces are allowed
// followed by a number of digits terminated by any non-digit character or EOS nul character
// return value is number of valid numeric characters found (0 = no valid number found)
for (i = 0, value = 0; ntext[i] != '\0'; ++i)
{
if ( (ntext[i] - '0' >= 0) && (ntext[i] - '0' <= 9) )
++value;
if ( (value == 0) && (ntext[i] != 32)
&& (ntext[i] - '0' < 0 || ntext[i] - '0' > 9) )
return 0;
}
return value;
}
static int myatoi (void);
static int
myatoi (void)
{
int i, value, j;
for (i = 0, j = 0, value = 0; ntext[i] != '\0'; ++i)
{
if ( (ntext[i] - '0' >= 0) && (ntext[i] - '0' <= 9) )
{
value *= 10;
value += ntext[i] - '0';
j = 1;
}
else
{
if ( ntext[i] != ' ' || j )
return value;
}
}
return value;
}
// END OF STEVE6375 ADDED CODE
int num_text_char(char *p);
int
num_text_char(char *p)
{
int i=0;
unsigned int un16=0;
for(;*p && *p != '\n' && *p != '\r';)
{
if((*p & 0x80) == 0)
{
p++;
i++;
continue;
}else if((*p & 0xe0) == 0xc0)
{
p+=2;
i++;
continue;
}else if((*p & 0xf0) == 0xe0)
{
un16=(int)(*p & 0xf)<<12 | (int)(*(p+1) & 0x3f)<<6 | (int)(*(p+2) & 0x3f);
if(un16 >= 0x4e00 && un16 <= 0x9fff)
i+=2;
else
i++;
p+=3;
}
}
return i;
}
/* Print an entry in a line of the menu box. */
char menu_cfg[2];
unsigned char menu_num_ctrl[4];
char graphic_file_shift[32];
static void print_entry (int y, int highlight,int entryno, char *config_entries);
static void
print_entry (int y, int highlight,int entryno, char *config_entries)
{
int x;
unsigned char c = 0;
char *entry = get_entry (config_entries, entryno);
int start_offcet = 0;
int end_offcet = 0;
int first_brackets = 0;
if (current_term->setcolorstate)
current_term->setcolorstate (highlight ? COLOR_STATE_HIGHLIGHT : COLOR_STATE_NORMAL);
unsigned long long clo64_back = current_color_64bit;
unsigned int clo_back = current_color;
unsigned long long clo64_tmp;
unsigned int clo_tmp;
is_highlight = highlight;
if (graphic_type)
{
char tmp[128];
int file_len=grub_strlen(graphic_file);
int www = (font_w * MENU_BOX_W) / graphic_list;
int graphic_x_offset, graphic_y_offset, text_x_offset;
int text_y_offset = MENU_BOX_Y +
(((y-MENU_BOX_Y)/graphic_list) * (graphic_high+row_space) + (graphic_high-font_h)/2) / (font_h+line_spacing);
graphic_x_offset = font_w*MENU_BOX_X+((y-MENU_BOX_Y)%graphic_list)*(www);
if (graphic_type & 0x10)
graphic_y_offset = (font_h+line_spacing)*text_y_offset - (graphic_high-font_h)/2;
else
graphic_y_offset = font_h*MENU_BOX_Y+((y-MENU_BOX_Y)/graphic_list)*(graphic_high+row_space);
if (entryno >= num_entries)
{
clear_entry (graphic_x_offset,graphic_y_offset,graphic_wide,graphic_high);
entry++;
}
else
{
graphic_file[file_len-6] = ((entryno + graphic_file_shift[entryno]) / 10) | 0x30;
graphic_file[file_len-5] = ((entryno + graphic_file_shift[entryno]) % 10) | 0x30;
sprintf(tmp,"--offset=%d=%d=%d %s",0,graphic_x_offset,graphic_y_offset,graphic_file);
use_phys_base=1;
graphic_enable = 1;
splashimage_func(tmp,1);
graphic_enable = 0;
use_phys_base=0;
if ((graphic_type & 4) && highlight)
rectangle(graphic_x_offset,graphic_y_offset,graphic_wide,graphic_high,3);
}
c = *entry;
if (graphic_type & 0x10)
{
text_x_offset = (graphic_x_offset + graphic_wide + font_w -1) / (font_w + font_spacing);
end_offcet = MENU_BOX_E - ((www - graphic_wide) / (font_w + font_spacing)) - text_x_offset;
gotoxy (text_x_offset, text_y_offset);
goto graphic_mixing;
}
else
goto graphic_end;
}
gotoxy (MENU_BOX_X, y);
if(menu_tab & 0x40)
end_offcet = 1;
if (highlight)
menu_num_ctrl[2] = entryno;
// if (*(unsigned short *)IMG(0x8308))
if (*(unsigned short *)IMG(0x82c8))
{
if(!(menu_tab & 0x40))
{
gotoxy (MENU_BOX_X - 1, y);
grub_putchar(highlight ? (menu_num_ctrl[2] = entryno, menu_cfg[0]) : ' ', 255);
}
else
{
gotoxy (MENU_BOX_E - 1, y);
grub_putchar(highlight ? (menu_num_ctrl[2] = entryno,menu_cfg[1]) : ' ', 255);
}
}
graphic_mixing:
if (entry)
{
if (config_entries == (char*)titles)
{
c = *entry++;
expand_var (entry, (char *)SCRATCHADDR, 0x400);
entry = (char *)SCRATCHADDR;
if (menu_num_ctrl[0])
{
if(menu_tab & 0x40)
{
gotoxy (MENU_BOX_E - 4, y);
end_offcet = 4;
}
if ((!(c & menu_num_ctrl[0]) && menu_num_ctrl[0] == 1) || !*entry || *entry == '\n')
printf(" ");
else
{
if(!(menu_tab & 0x40))
printf("%2d%c",(menu_num_ctrl[0] > 1)?entryno:title_boot[entryno],menu_num_ctrl[1]);
else
printf("%c%2d",menu_num_ctrl[1],(menu_num_ctrl[0] > 1)?entryno:title_boot[entryno]);
}
}
}
c = *entry;
}
if(menu_tab & 0x40)
gotoxy (MENU_BOX_X, y);
if(entry)
{
if(menu_tab & 8)
{
if(!(menu_tab & 0x40))
start_offcet = MENU_BOX_X + ((MENU_BOX_W - num_text_char(entry) + (menu_num_ctrl[0]?3:0)) >> 1);
else
start_offcet = MENU_BOX_X + ((MENU_BOX_W - num_text_char(entry) - end_offcet) >> 1);
}
else if((menu_tab & 0x40))
{
start_offcet = MENU_BOX_E - num_text_char(entry) - end_offcet;
if(start_offcet < MENU_BOX_X)
start_offcet = MENU_BOX_X;
}
}
for (x = fontx; x < MENU_BOX_E - end_offcet; x = fontx)
{
int ret;
ret = MENU_BOX_E - x - end_offcet;
if (c && c != '\n' /* && x <= MENU_BOX_W*/ && x >= start_offcet)
{
hotkey_start:
if (hotkey_func && hotkey_color_64bit)
{
if (c == '^')
{
c = *(++entry);
goto color;
}
else if (first_brackets == 0 && c == '[')
{
first_brackets = 1;
goto color_no;
}
else if (first_brackets == 1 && c == ']')
{
first_brackets = 2;
goto color_no;
}
else if (first_brackets == 0 || first_brackets == 2)
{
first_brackets = 2;
goto color_no;
}
color:
clo64_tmp = current_color_64bit;
clo_tmp = current_color;
current_color_64bit = hotkey_color_64bit;
current_color = hotkey_color;
if (current_term == term_table)
console_setcolorstate (current_color | 0x100);
ret = grub_putchar ((unsigned char)c, ret);
current_color_64bit = clo64_tmp;
current_color = clo_tmp;
if (current_term == term_table)
current_term->setcolorstate (current_color | 0x100);
goto hotkey_end;
}
color_no:
//处理菜单项中的彩色字符
if (*(unsigned short*)entry == 0x5B24)//$[
{
entry = print_color_characters (entry,&c,&clo64_back,&clo_back);
goto hotkey_start;
}
ret = grub_putchar ((unsigned char)c, ret);
hotkey_end:
//is_highlight = 0;
if (ret < (int)0)
{
c = 0;
continue;
}
c = *(++entry);
}
else
{
if (!(menu_tab & 0x10))
{
clo64_tmp = current_color_64bit;
if(splashimage_loaded & 2)
current_color_64bit = 0;
else
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_NORMAL);
ret = grub_putchar (' ', ret);
current_color_64bit = clo64_tmp;
}
else
grub_putchar (' ', ret);
}
}
graphic_end:
is_highlight = 0;
if (highlight && ((config_entries == (char*)titles)))
{
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
while (c && c != '\n')
c = *(++entry);
if (c == '\n')
{
default_help_message_destoyed = 1;
print_help_message(entry,1);
}
else
{
if(!(menu_border.menu_keyhelp_y_offset) && !(menu_tab & 4))
{
if (default_help_message_destoyed)
print_default_help_message (config_entries);
}
else
print_help_message(entry,2);
}
}
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_STANDARD);
}
/* Print entries in the menu box. */
static void print_entries (int first, int entryno, char *menu_entries);
static void
print_entries (int first, int entryno, char *menu_entries)
{
int i;
gotoxy (MENU_BOX_E, MENU_BOX_Y);
#ifdef SUPPORT_GRAPHICS
if (!graphics_inited || graphics_mode < 0xff)
#endif
{
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_BORDER);
if (first)
console_print_unicode (DISP_UP, 255);
else
console_print_unicode (DISP_VERT, 255);
}
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_NORMAL);
for (i = 0; i < MENU_BOX_H/*size*/; i++)
{
print_entry (MENU_BOX_Y + i, entryno == i, first + i, menu_entries);
}
#ifdef SUPPORT_GRAPHICS
if (!graphics_inited || graphics_mode < 0xff)
#endif
{
gotoxy (MENU_BOX_E, MENU_BOX_Y - 1 + MENU_BOX_H/*size*/);
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_BORDER);
char *last_entry = get_entry(menu_entries,first+MENU_BOX_H);
if (last_entry && *last_entry)
console_print_unicode (DISP_DOWN, 255);
else
console_print_unicode (DISP_VERT, 255);
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_STANDARD);
}
gotoxy (MENU_BOX_E, MENU_BOX_Y + entryno); /* XXX: Why? */
}
static void print_entries_raw (int size, int first, char *menu_entries);
static void
print_entries_raw (int size, int first, char *menu_entries)
{
int i;
char *p;
for (i = 0; i < MENU_BOX_W; i++)
grub_putchar ('-', 255);
grub_putchar ('\n', 255);
for (i = first; i < size; i++)
{
p = get_entry (menu_entries, i);
grub_printf ("%02d: %s\n", i, (((*p) & 0xF0) ? p : ++p));
}
for (i = 0; i < MENU_BOX_W; i++)
grub_putchar ('-', 255);
grub_putchar ('\n', 255);
grub_putchar ('\n', 255);
}
extern int command_func (char *arg1, int flags);
extern int commandline_func (char *arg1, int flags);
extern int errnum_func (char *arg1, int flags);
extern int checkrange_func (char *arg1, int flags);
/* Run an entry from the script SCRIPT. HEAP is used for the
command-line buffer. If an error occurs, return non-zero, otherwise
return zero. */
static int run_script (char *script, char *heap);
static int
run_script (char *script, char *heap)
{
char *old_entry = 0;
char *cur_entry = script;
struct builtin *builtin = 0;
char cmd_add[16];
char *menu_bat;
char *p;
/* Initialize the data. */
current_drive = GRUB_INVALID_DRIVE;
count_lines = -1;
/* Initialize the data for the builtin commands. */
kernel_type = KERNEL_TYPE_NONE;
errnum = 0;
errorcheck = 1; /* errorcheck on */
if (grub_memcmp (cur_entry, "!BAT", 4) == 0)
{
while (1)
{
while (*cur_entry++);
if (! *cur_entry)
break;
*(cur_entry - 1) = 0x0a;
}
// menu_bat = (char *)grub_malloc(cur_entry - script + 10 + 512);
menu_bat = (char *)grub_memalign(512, cur_entry - script + 10 + 512); //对齐分配内存
if (menu_bat == NULL)
return 0;
// p = (char *)(((grub_size_t)menu_bat + 511) & ~511);
p = menu_bat;
grub_memmove (p, script, cur_entry - script);
grub_sprintf (cmd_add, "(md)%d+%d", (grub_size_t)p >> 9, ((cur_entry - script + 10 + 511) & ~511) >> 9);
command_func (cmd_add, BUILTIN_SCRIPT);
grub_free(menu_bat);
menu_bat = 0;
if (errnum >= 1000)
{
errnum=ERR_NONE;
return 0;
}
if (errnum && errorcheck)
goto ppp;
/* If any kernel is not loaded, just exit successfully. */
if (kernel_type == KERNEL_TYPE_NONE)
return 0; /* return to main menu. */
/* Otherwise, the command boot is run implicitly. */
grub_sprintf (cmd_add, "boot", 5);
run_line (cmd_add , BUILTIN_SCRIPT);
goto ppp;
}
while (1)
{
if (errnum == MAX_ERR_NUM)
{
errnum=ERR_NONE;
return 0;
}
if (errnum && errorcheck)
break;
old_entry = cur_entry;
while (*cur_entry++)
;
grub_memmove (heap, old_entry, (grub_size_t) cur_entry - (grub_size_t) old_entry);
if (! *heap)
{
/* If there is no more command in SCRIPT... */
/* If any kernel is not loaded, just exit successfully. */
if (kernel_type == KERNEL_TYPE_NONE)
return 0; /* return to main menu. */
/* Otherwise, the command boot is run implicitly. */
grub_memmove (heap, "boot", 5);
}
/* Find a builtin. */
builtin = find_command (heap);
if (! builtin)
{
grub_printf ("%s\n", old_entry);
continue;
}
run_line (heap , BUILTIN_SCRIPT);
if (! *old_entry) /* HEAP holds the implicit BOOT command */
break;
} /* while (1) */
ppp:
kernel_type = KERNEL_TYPE_NONE;
/* If a fallback entry is defined, don't prompt a user's intervention. */
if (fallback_entryno < 0)
{
print_error ();
grub_printf ("\nPress any key to continue...");
(void) getkey ();
}
errnum = ERR_NONE;
return 1; /* use fallback. */
}
void clear_delay_display (int entryno);
void
clear_delay_display (int entryno)
{
if (grub_timeout >= 0)
{
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
if (current_term->flags & TERM_DUMB)
grub_putchar ('\r', 255);
if(timeout_x || timeout_y)
gotoxy (timeout_x,timeout_y);
else
{
if(!(menu_tab & 0x40))
gotoxy (MENU_BOX_E - 2, MENU_BOX_Y + entryno);
else
gotoxy (MENU_BOX_X - 1, MENU_BOX_Y + entryno);
}
if(timeout_color)
{
current_term->setcolorstate (COLOR_STATE_NORMAL);
if ((timeout_color & 0xffffffff00000000) == 0)
current_color_64bit = timeout_color | (current_color_64bit & 0xffffffff00000000);
else
current_color_64bit = timeout_color;
}
else if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HIGHLIGHT);
grub_printf(" ");
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
grub_timeout = -1;
timeout_enable = 0;
fallback_entryno = -1;
if (! (current_term->flags & TERM_DUMB))
gotoxy (MENU_BOX_E, MENU_BOX_Y + entryno);
}
}
unsigned short beep_buf[256];
int new_menu;
int new_hotkey;
int color_counting;
int password_x;
unsigned char timeout_enable = 0;
int time0 = 0, time1 = 0;
static int fallbacked_entries;
static int old_c;
static int old_c_count;
static int old_c_count_end;
void timeout_refresh(void);
void timeout_refresh(void)
{
time0++;
if (time0 == 1000)
{
time0 = 0;
time1++;
}
}
static void run_menu (char *menu_entries, char *config_entries, /*int num_entries,*/ char *heap, int entryno);
static void
run_menu (char *menu_entries, char *config_entries, /*int num_entries,*/ char *heap, int entryno)
{
int i, /*time1,*/ time2 = -1, first_entry = 0;
unsigned short c;
char *cur_entry = 0;
char *pass_config = 0;
color_counting = 0;
/*
* Main loop for menu UI.
*/
if (password_buf)//Make sure that PASSWORD is NUL-terminated.
pass_config = wee_skip_to(password_buf,SKIP_WITH_TERMINATE);
/* clear keyboard buffer before boot */
while ((unsigned short)console_checkkey () == 0x000d);
restart1:
//clear temp_num when restart menu
temp_num = 0;
font_spacing = menu_font_spacing;
line_spacing = menu_line_spacing;
if (graphics_mode > 0xff) //含字符间隙时,需重新计算 2023-02-22
{
current_term->max_lines = current_y_resolution / (font_h + line_spacing);
current_term->chars_per_line = current_x_resolution / (font_w + font_spacing);
}
/* Dumb terminal always use all entries for display
invariant for TERM_DUMB: first_entry == 0 */
if (! (current_term->flags & TERM_DUMB))
{
if (entryno > MENU_BOX_H - 1)
{
first_entry += entryno - (MENU_BOX_H - 1);
entryno = MENU_BOX_H - 1;
}
}
/* If the timeout was expired or wasn't set, force to show the menu
interface. */
if (grub_timeout < 0)
show_menu = 1;
cls();
/* If SHOW_MENU is false, don't display the menu until a key-press. */
if (! show_menu)
{
/* Get current time. */
// while ((time1 = getrtsecs ()) == 0xFF)
// ;
if (grub_timeout >= 0)
timeout_enable = 1;
while (1)
{
/* Unhide the menu on any keypress. */
if ((i = checkkey ()) != (int)-1 /*&& ASCII_CHAR (getkey ()) == '\e'*/)
{
c = i;
if (silent_hiddenmenu > 1)
{
if (c != silent_hiddenmenu)
goto boot_entry;
if (password_buf)
{
if (check_password (password_buf, password_type))
{
grub_printf ("auth failed!\n");
grub_timeout = 5;
continue;
}
if (*pass_config)
{
strcpy(config_file,pass_config);
auth = 0;
return;
}
auth = 1;
}
}
grub_timeout = -1;
timeout_enable = 0;
show_menu = 1;
break;
}
/* If GRUB_TIMEOUT is expired, boot the default entry. */
if (grub_timeout >=0
// && (time1 = getrtsecs ()) != time2
&& time1 != time2
/* && time1 != 0xFF */)
{
if (grub_timeout <= 0)
{
// grub_timeout = -1;
goto boot_entry;
}
time2 = time1;
grub_timeout--;
/* Print a message. */
if (! silent_hiddenmenu)
{
grub_printf ("\rPress any key to enter the menu... %d ",
grub_timeout);
}
}
}
}
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_NORMAL);
setcursor (2);
cls(); //clear screen so splash image will work
if (animated_type)
animated_enable = animated_enable_backup;
/* Only display the menu if the user wants to see it. */
if (show_menu)
{
cls ();
if (!(menu_tab & 0x80))
init_page ();
if (graphics_inited && graphics_mode > 0xff)/*vbe mode call rectangle_func*/
{
unsigned long long col = current_color_64bit;
for (i=0; i<16; i++)
{
if (DrawBox[i].enable == 0)
continue;
current_color_64bit = DrawBox[i].color;
rectangle(DrawBox[i].start_x, DrawBox[i].start_y, DrawBox[i].horiz, DrawBox[i].vert, DrawBox[i].linewidth);
}
current_color_64bit = col;
}
if (num_string)
{
int j;
char y;
char *p;
unsigned long long current_color_64bit_back;
unsigned int current_color_back;
grub_u8_t c1;
for (j=0; j<16; j++)