/
stage2.c
2566 lines (2211 loc) · 64.3 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 long is_highlight = 0;
/* preset_menu is defined in asm.S */
#define preset_menu *(const char **)0x307FFC
static unsigned long preset_menu_offset;
static int
open_preset_menu (void)
{
if (! use_preset_menu)
return 0;
if (preset_menu != (const char *)0x800)
goto lzma;
preset_menu_offset = 0;
return 1;//preset_menu != 0;
lzma:
return grub_open ("(md)0x880+0x200");
}
static int
read_from_preset_menu (char *buf, int max_len)
{
int len;
if (! use_preset_menu) //if (preset_menu == 0)
return 0;
if (preset_menu != (const char *)0x800)
goto lzma;
len = grub_strlen (preset_menu + preset_menu_offset);
if (len > max_len)
len = max_len;
grub_memmove (buf, preset_menu + preset_menu_offset, len);
preset_menu_offset += len;
return len;
lzma:
return grub_read ((unsigned long long)(unsigned int)buf, max_len, 0xedde0d90);
}
#define MENU_BOX_X (menu_border.menu_box_x)
//#define MENU_BOX_W (menu_border.menu_box_w)
//#define MENU_BOX_W (menu_border.menu_box_w ? menu_border.menu_box_w : current_term->chars_per_line - 4)
#define MENU_BOX_W (menu_border.menu_box_w ? menu_border.menu_box_w : current_term->chars_per_line - 3)
#define MENU_BOX_Y (menu_border.menu_box_y)
/* window height */
#define MENU_BOX_H (menu_border.menu_box_h?menu_border.menu_box_h:(current_term->max_lines - 6 - menu_border.menu_box_y)) //current_term->max_lines - 8
/* 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)) //current_term->max_lines - 7
static long temp_entryno;
static short temp_num;
static char * *titles; /* title array, point to 256 strings. */
extern int (*hotkey_func)(char *titles,int flags);
static unsigned short *title_boot;
static int default_help_message_destoyed = 1;
/*
unsigned char disp_ul;
unsigned char disp_ur;
unsigned char disp_ll;
unsigned char disp_lr;
unsigned char disp_horiz;
unsigned char disp_vert;
unsigned char menu_box_x; // line start
unsigned char menu_box_w; // line width
unsigned char menu_box_y; // first line number
unsigned char menu_box_h;
unsigned char menu_box_b;
*/
struct border menu_border = {218,191,192,217,196,179,2,0,2,0,0,2}; /* console */
//struct border menu_border = {20,21,22,19,15,14,2,0,2,0,0}; /* graphics */
static void print_help_message (const char *message)
{
grub_u32_t j,x;
grub_u8_t c = *message;
for (j = 1; j < 5; ++j)
{
if (c == '\n')
c = *(++message);
gotoxy (0, MENU_BOX_B + j);
for (x = 0; x < MENU_BOX_X - 2; ++x)
grub_putchar (' ', 255);
for (; fontx <= MENU_BOX_E;)
{
if (c && c != '\n')
{
grub_putchar (c, 255);
c = *(++message);
}
else
grub_putchar (' ', 255);
}
for (x = fontx; x < current_term->chars_per_line/* - 1*/; x++)
grub_putchar (' ', 255);
}
}
static void
print_default_help_message (char *config_entries)
{
grub_u32_t i;
char buff[256];
for(i=1;i<5;++i)
{
gotoxy (0, MENU_BOX_B + i);
printf("%.*s",current_term->chars_per_line,0);
}
i = grub_sprintf (buff,"\n Use the %c and %c keys to highlight an entry.",
(unsigned long)(unsigned char)DISP_UP, (unsigned long)(unsigned char)DISP_DOWN);
if (! auth && password_buf)
{
#if 0
#ifdef SUPPORT_GFX
if (*graphics_file)
{
grub_strcpy (buff + i,"\
WARNING: graphical menu doesn\'t work\
in conjunction with the password feature\n" );
i = grub_strlen(buff);
}
#endif
#endif
grub_strcpy (buff + i," Press ENTER or \'b\' to boot.\n"
" Press \'p\' to gain privileged control.");
}
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,"
" or \'O\'/\'o\' to open a new line before/after. When done, "
"press \'b\' to boot, \'c\' for a command-line, or ESC to go back to the main menu.")
));
}
print_help_message(buff);
default_help_message_destoyed = 0;
}
// ADDED By STEVE6375
static unsigned int num_entries;
static char *
get_entry (char *list, int num)
{
int i;
if (list == (char *)titles)
return num < num_entries ? titles[num] : 0;
for (i = 0; i < num && *list; i++)
{
while (*(list++));
}
return list;
}
static char ntext[256];
static void
clean_entry (char *text)
{
int i;
//(void) strcpy (ntext, text);
for (i = 0; i < 255 && text[i]; ++i)
{
if (text[i] < 32 || text[i] > 127)
ntext[i] = 32;
else
ntext[i] = text[i];
}
ntext[i] = 0;
return;
}
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)
{
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;
}
}
// printf ("Mv%dMv",value);
return value;
}
// END OF STEVE6375 ADDED CODE
/* Print an entry in a line of the menu box. */
extern char menu_cfg[];
extern unsigned char menu_num_ctrl[];
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);
if (current_term->setcolorstate)
current_term->setcolorstate (highlight ? COLOR_STATE_HIGHLIGHT : COLOR_STATE_NORMAL);
is_highlight = highlight;
gotoxy (MENU_BOX_X - 1, y);
grub_putchar(highlight ? (menu_num_ctrl[2] = entryno,menu_cfg[0]) : ' ', 255);
if (entry)
{
if (config_entries == (char*)titles)
{
c = *entry++;
expand_var (entry, (char *)SCRATCHADDR, 0x400);
entry = (char *)SCRATCHADDR;
if (menu_num_ctrl[0])
{
if (!(c & menu_num_ctrl[0]) || !*entry || *entry == '\n')
printf(" ");
else
printf("%2d%c",(menu_num_ctrl[0] > 1)?entryno:title_boot[entryno],menu_num_ctrl[1]);
}
}
c = *entry;
}
for (x = MENU_BOX_X; x < MENU_BOX_E; x = fontx)
{
unsigned int ret;
ret = MENU_BOX_E - x;
if (c && c != '\n' /* && x <= MENU_BOX_W*/)
{
ret = grub_putchar ((unsigned char)c, ret);
//is_highlight = 0;
if ((long)ret < 0)
{
c = 0;
continue;
}
c = *(++entry);
}
else
{
//ret = grub_putchar (' ', ret);
//if ((long)ret < 0)
// break;
grub_putchar (' ', ret);
}
}
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);
}
else if (default_help_message_destoyed)
{
print_default_help_message (config_entries);
}
gotoxy (MENU_BOX_E, y);
}
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)
{
int i;
//int main_menu = (menu_entries == (char *)titles);
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)
grub_putchar (DISP_UP, 255);
else
grub_putchar (DISP_VERT, 255);
}
//if (main_menu || *menu_entries)
// menu_entries = get_entry (menu_entries, first);
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);
// if (main_menu)
// {
//if (menu_entries)
//{
// if ((++first) < 256)
// menu_entries = titles[first];
// else
// menu_entries = 0;
//}
//continue;
// }
// if (*menu_entries)
//while (*(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)
grub_putchar (DISP_DOWN, 255);
else
grub_putchar (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)
{
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)
{
char *old_entry = 0;
char *cur_entry = script;
struct builtin *builtin = 0;
// char *arg;
// grub_error_t errnum_old;//2010-12-16
/* Initialize the data. */
//saved_drive = boot_drive;
//saved_partition = install_partition;
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 */
while (1)
{
if (errnum && errorcheck)
break;
// errnum_old = errnum; //2010-12-16
/* Copy the first string in CUR_ENTRY to HEAP. */
// errnum = ERR_NONE;//2010-12-16
old_entry = cur_entry;
while (*cur_entry++)
;
grub_memmove (heap, old_entry, (int) cur_entry - (int) 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;
}
/* now command echoing is considered no use for a successful command. */
// if (! (builtin->flags & BUILTIN_NO_ECHO))
// grub_printf ("%s\n", old_entry);
/*comment by chenall on 2010-12-16,will do it in run_line*/
#if 0
/* If BUILTIN cannot be run in the command-line, skip it. */
if ((int)builtin != -1 && ! (builtin->flags & BUILTIN_CMDLINE))
{
errnum = ERR_UNRECOGNIZED;
continue;
}
/* Invalidate the cache, because the user may exchange removable
disks. */
buf_drive = -1;
if ((int)builtin != -1)
if ((builtin->func) == errnum_func || (builtin->func) == checkrange_func)
errnum = errnum_old;
/* find && and || */
#endif
run_line (heap , BUILTIN_SCRIPT);
if (! *old_entry) /* HEAP holds the implicit BOOT command */
break;
} /* while (1) */
kernel_type = KERNEL_TYPE_NONE;
/* If a fallback entry is defined, don't prompt a user's intervention. */
if (fallback_entryno < 0)
{
if (! (builtin->flags & BUILTIN_NO_ECHO))
grub_printf ("%s\n", heap);
print_error ();
grub_printf ("\nPress any key to continue...");
(void) getkey ();
}
errnum = ERR_NONE;
return 1; /* use fallback. */
}
static int fallbacked_entries;
static int old_c;
static int old_c_count;
static int old_c_count_end;
static void
run_menu (char *menu_entries, char *config_entries, /*int num_entries,*/ char *heap, int entryno)
{
int c, time1, time2 = -1, first_entry = 0;
char *cur_entry = 0;
char *pass_config = 0;
// struct term_entry *prev_term = NULL;
/*
* 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);
restart1:
/* 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)
;
while (1)
{
/* Unhide the menu on any keypress. */
if (checkkey () != -1 /*&& ASCII_CHAR (getkey ()) == '\e'*/)
{
c = getkey();/* eat the key */
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;
show_menu = 1;
break;
}
/* If GRUB_TIMEOUT is expired, boot the default entry. */
if (grub_timeout >=0
&& (time1 = getrtsecs ()) != 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
/* Only display the menu if the user wants to see it. */
if (show_menu)
{
init_page ();
//setcursor (0);
if (current_term->flags & TERM_DUMB)
print_entries_raw (num_entries, first_entry, menu_entries);
else /* print border */
#ifdef SUPPORT_GRAPHICS
if (graphics_inited && graphics_mode > 0xff)/*vbe mode call rectangle_func*/
{
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_BORDER);
unsigned long x,y,w,h,i,j;
i = font_w + font_spacing;
j = font_h + line_spacing;
x = (MENU_BOX_X - 2) * i + (i>>1);
y = (MENU_BOX_Y)*j-(j>>1);
w = (MENU_BOX_W + 2) * i;
h = (MENU_BOX_H + 1) * j;
rectangle(x,y,w,h,menu_border.border_w);
}
else
#endif
{
int i;
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_BORDER);
/* upper-left corner */
gotoxy (MENU_BOX_X - 2, MENU_BOX_Y - 1);
grub_putchar (DISP_UL, 255);
/* top horizontal line */
for (i = 0; i < MENU_BOX_W + 1; i++)
grub_putchar (DISP_HORIZ, 255);
/* upper-right corner */
grub_putchar (DISP_UR, 255);
for (i = 0; i < MENU_BOX_H; i++)
{
/* left vertical line */
gotoxy (MENU_BOX_X - 2, MENU_BOX_Y + i);
grub_putchar (DISP_VERT, 255);
/* right vertical line */
gotoxy (MENU_BOX_E, MENU_BOX_Y + i);
grub_putchar (DISP_VERT, 255);
}
/* lower-left corner */
gotoxy (MENU_BOX_X - 2, MENU_BOX_Y + MENU_BOX_H);
grub_putchar (DISP_LL, 255);
/* bottom horizontal line */
for (i = 0; i < MENU_BOX_W + 1; i++)
grub_putchar (DISP_HORIZ, 255);
/* lower-right corner */
grub_putchar (DISP_LR, 255);
//if (current_term->setcolorstate)
// current_term->setcolorstate (COLOR_STATE_STANDARD);
}
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
print_default_help_message (config_entries);
if (current_term->flags & TERM_DUMB)
grub_printf ("\n\nThe selected entry is %d ", entryno);
else
print_entries (first_entry, entryno, menu_entries);
}
if (menu_init_script_file[0] != 0 ) command_func(menu_init_script_file,BUILTIN_MENU);
/* XX using RT clock now, need to initialize value */
while ((time1 = getrtsecs()) == 0xFF);
old_c = 0;
old_c_count = 0;
old_c_count_end = (force_lba & 4); /* non-zero to disable single-key-selection feature. */
temp_entryno = 0;
/* Initialize to NULL just in case... */
cur_entry = NULL;
while (1)
{
/* Initialize to NULL just in case... */
//cur_entry = NULL;
//cur_entry = menu_entries; /* for modified menu */
if (grub_timeout >= 0 && (time1 = getrtsecs()) != time2 && time1 != 0xFF)
{
if (grub_timeout <= 0)
{
grub_timeout = -1;
break;
}
/* else not booting yet! */
time2 = time1;
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
if (current_term->flags & TERM_DUMB)
grub_printf ("\r Entry %d will be booted automatically in %d seconds. ",
entryno, grub_timeout);
else
{
unsigned long x;
unsigned long ret;
unsigned char tmp_buf[512];
unsigned char *p;
unsigned char ch = ' ';
grub_sprintf ((char*)tmp_buf, " The highlighted entry will be booted automatically in %d seconds.", grub_timeout);
gotoxy (0, MENU_BOX_B + 5);
p = tmp_buf;
for (x = 0; x < current_term->chars_per_line; x = fontx)
{
if (ch >= ' ')
ch = *p++;
ret = current_term->chars_per_line - x;
ret = grub_putchar ((ch >= ' ' ? ch : ' '), ret);
if ((long)ret < 0)
break;
}
gotoxy (MENU_BOX_E, MENU_BOX_Y + entryno);
}
grub_timeout--;
}
/* Check for a keypress, however if TIMEOUT has been expired
(GRUB_TIMEOUT == -1) relax in GETKEY even if no key has been
pressed.
This avoids polling (relevant in the grub-shell and later on
in grub if interrupt driven I/O is done). */
if (checkkey () >= 0 || grub_timeout < 0)
{
/* Key was pressed, show which entry is selected before GETKEY,
since we're comming in here also on GRUB_TIMEOUT == -1 and
hang in GETKEY */
if (current_term->flags & TERM_DUMB)
grub_printf ("\r Highlighted entry is %d: ", entryno);
if (config_entries && hotkey_func)
{
putchar_hooked = (unsigned char*)0x800;
c = hotkey_func(0,-1);
putchar_hooked = 0;
if (c == -1)
goto restart1;
if (c>>16)
{
temp_entryno = (long)(unsigned char)(c>>16);
if (c & 1<<30)
goto check_update;
entryno = temp_entryno;
first_entry = 0;
goto boot_entry;
}
}
else
c = /*ASCII_CHAR*/ (getkey ());
if (! old_c_count_end)
{
if (old_c == 0)
old_c = c;
if (c == old_c && old_c_count < 0x7FFFFFFF)
old_c_count++;
if (c != old_c)
{
old_c_count_end = 1;
}
}
if (grub_timeout >= 0)
{
unsigned long x;
if (current_term->setcolorstate)
current_term->setcolorstate (COLOR_STATE_HELPTEXT);
if (current_term->flags & TERM_DUMB)
grub_putchar ('\r', 255);
else
gotoxy (0, MENU_BOX_B + 5);
for (x = 0; x < current_term->chars_per_line; x++)
{
grub_putchar (' ', 255);
}
grub_timeout = -1;
fallback_entryno = -1;
if (! (current_term->flags & TERM_DUMB))
gotoxy (MENU_BOX_E, MENU_BOX_Y + entryno);
}
if (num_entries == 0)
{
first_entry = entryno = 0;
goto done_key_handling;
}
/* We told them above (at least in SUPPORT_SERIAL) to use
'^' or 'v' so accept these keys. */
if (c == KEY_UP/*16*/ || c == KEY_LEFT || ((char)c) == '^')
{
temp_entryno = 0;
if (current_term->flags & TERM_DUMB)
{
if (entryno > 0)
entryno--;
}
else
{
if (c == KEY_UP)
{
temp_entryno = first_entry + entryno;
for (;;)
{
temp_entryno = (temp_entryno + num_entries - 1) % num_entries;
if (temp_entryno == first_entry + entryno)
goto done_key_handling;
cur_entry = get_entry (menu_entries, temp_entryno);
if (*cur_entry != 0x08)
goto check_update;
}
}
if (entryno > 0)
{
//cur_entry = get_entry (menu_entries, first_entry + entryno);
/* un-highlight the current entry */
print_entry (MENU_BOX_Y + entryno, 0, first_entry + entryno, menu_entries);
--entryno;
//cur_entry = get_entry (menu_entries, first_entry + entryno);
/* highlight the previous entry */
print_entry (MENU_BOX_Y + entryno, 1, first_entry + entryno, menu_entries);
}
else if (first_entry > 0)
{
first_entry--;
print_entries (first_entry, entryno, menu_entries);
}
else /* loop forward to END */
{
temp_entryno = num_entries - 1;
goto check_update;
}
}
}
else if ((c == KEY_DOWN/*14*/ || c == KEY_RIGHT || ((char)c) == 'v' || (!old_c_count_end && c == old_c && old_c_count >= 8))
/* && first_entry + entryno + 1 < num_entries */)
{
temp_entryno = 0;
if (current_term->flags & TERM_DUMB)
{
if (first_entry + entryno + 1 < num_entries)
entryno++;
}
else
{
if (c == KEY_DOWN)
{
temp_entryno = first_entry + entryno;
for (;;)
{
temp_entryno = (temp_entryno + 1) % num_entries;
if (temp_entryno == first_entry + entryno)
goto done_key_handling;
cur_entry = get_entry (menu_entries, temp_entryno);
if (*cur_entry != 0x08)
goto check_update;
}
}
if (first_entry + entryno + 1 >= num_entries)
{
temp_entryno = 0; /* loop backward to HOME */
goto check_update;
}
if (entryno < MENU_BOX_H - 1)
{
//cur_entry = get_entry (menu_entries, first_entry + entryno);
/* un-highlight the current entry */
print_entry (MENU_BOX_Y + entryno, 0, first_entry + entryno, menu_entries);
//entryno++;
//cur_entry = get_entry (menu_entries, first_entry + entryno);
/* highlight the next entry */
++entryno;
print_entry (MENU_BOX_Y + entryno, 1, first_entry + entryno, menu_entries);
}
else if (num_entries > MENU_BOX_H + first_entry)
{
first_entry++;
print_entries (first_entry, entryno, menu_entries);
}
}
}
else if (c == KEY_PPAGE/*7*/)
{
/* Page Up */
temp_entryno = 0;
first_entry -= MENU_BOX_H;
if (first_entry < 0)
{
entryno += first_entry;
first_entry = 0;
if (entryno < 0)
entryno = 0;
}
print_entries (first_entry, entryno, menu_entries);
}
else if (c == KEY_NPAGE/*3*/)
{
/* Page Down */
temp_entryno = 0;
first_entry += MENU_BOX_H;
if (first_entry + entryno + 1 >= num_entries)
{
first_entry = num_entries - MENU_BOX_H;
if (first_entry < 0)
first_entry = 0;
entryno = num_entries - first_entry - 1;
}
print_entries (first_entry, entryno, menu_entries);
}
else if ( ((char)c) >= '0' && ((char)c) <= '9')
{
temp_num *= 10;
temp_num += ((char)c) - '0';
// if (temp_entryno >= num_entries)
// temp_entryno = num_entries - 1;
if (temp_num >= num_entries) /* too big an entryno */
{
temp_num = ((char)c - '0'>= num_entries)?0:(char)c - '0';
}
temp_entryno = temp_num;
if (temp_entryno != 0 || (char)c == '0')
{
// temp_entryno has users number - check if it matches a title number
// If menu items are numbered then there must be no unnumbered items in the first few entries
// e.g. if you have 35 menu items, then menu entries 0 - 3 must all numbered - otherwise double-digit user entry will not work - e.g. 34 will not work
int j;
if (menu_num_ctrl[0])
{
if (menu_num_ctrl[0] == 1)
{
j = temp_num;