Implement tsc based delay timers, and use them throughout code.
[seabios.git] / src / post_menu.c
1 // Menu presented during final phase of "post".
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "biosvar.h" // GET_EBDA
9 #include "util.h" // mdelay
10 #include "bregs.h" // struct bregs
11
12 static u8
13 check_for_keystroke()
14 {
15     struct bregs br;
16     memset(&br, 0, sizeof(br));
17     br.ah = 1;
18     call16_int(0x16, &br);
19     return !(br.flags & F_ZF);
20 }
21
22 static u8
23 get_keystroke()
24 {
25     struct bregs br;
26     memset(&br, 0, sizeof(br));
27     call16_int(0x16, &br);
28     return br.ah;
29 }
30
31 static void
32 mdelay_and_check_for_keystroke(u32 msec, int count)
33 {
34     int i;
35     for (i = 1; i <= count; i++) {
36         mdelay(msec);
37         if (check_for_keystroke())
38             break;
39     }
40 }
41
42 void
43 interactive_bootmenu()
44 {
45     if (! CONFIG_BOOTMENU)
46         return;
47
48     while (check_for_keystroke())
49         get_keystroke();
50
51     printf("Press F12 for boot menu.\n\n");
52
53     mdelay_and_check_for_keystroke(500, 5);
54     if (! check_for_keystroke())
55         return;
56     u8 scan_code = get_keystroke();
57     if (scan_code != 0x86)
58         /* not F12 */
59         return;
60
61     while (check_for_keystroke())
62         get_keystroke();
63
64     printf("Select boot device:\n\n");
65
66     int count = GET_EBDA(ipl.count);
67     int i;
68     for (i = 0; i < count; i++) {
69         printf("%d. ", i+1);
70         printf_bootdev(i);
71         printf("\n");
72     }
73
74     for (;;) {
75         scan_code = get_keystroke();
76         if (scan_code == 0x01 || scan_code == 0x58)
77             /* ESC or F12 */
78             break;
79         if (scan_code <= count + 1) {
80             // Add user choice to the boot order.
81             u16 choice = scan_code - 1;
82             u32 bootorder = GET_EBDA(ipl.bootorder);
83             SET_EBDA(ipl.bootorder, (bootorder << 4) | choice);
84             break;
85         }
86     }
87     printf("\n");
88 }