aboutsummaryrefslogtreecommitdiffstats
path: root/src/post_menu.c
blob: 8b6e58a182e1ab6b95e2c894328826618a2ce258 (plain)
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
// Menu presented during final phase of "post".
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002  MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "biosvar.h" // GET_EBDA
#include "util.h" // mdelay
#include "bregs.h" // struct bregs
#include "boot.h" // IPL

static int
check_for_keystroke()
{
    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ah = 1;
    call16_int(0x16, &br);
    return !(br.flags & F_ZF);
}

static int
get_keystroke()
{
    struct bregs br;
    memset(&br, 0, sizeof(br));
    call16_int(0x16, &br);
    return br.ah;
}

static void
usleep(u32 usec)
{
    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ah = 0x86;
    br.cx = usec >> 16;
    br.dx = usec;
    call16_int(0x15, &br);
}

static int
timed_check_for_keystroke(int msec)
{
    while (msec > 0) {
        if (check_for_keystroke())
            return 1;
        usleep(50*1000);
        msec -= 50;
    }
    return 0;
}

void
interactive_bootmenu()
{
    if (! CONFIG_BOOTMENU)
        return;

    while (check_for_keystroke())
        get_keystroke();

    printf("Press F12 for boot menu.\n\n");

    if (!timed_check_for_keystroke(2500))
        return;
    int scan_code = get_keystroke();
    if (scan_code != 0x86)
        /* not F12 */
        return;

    while (check_for_keystroke())
        get_keystroke();

    printf("Select boot device:\n\n");

    int count = IPL.count;
    int i;
    for (i = 0; i < count; i++) {
        printf("%d. ", i+1);
        printf_bootdev(i);
        printf("\n");
    }

    for (;;) {
        scan_code = get_keystroke();
        if (scan_code == 0x01 || scan_code == 0x58)
            /* ESC or F12 */
            break;
        if (scan_code <= count + 1) {
            // Add user choice to the boot order.
            u16 choice = scan_code - 1;
            u32 bootorder = IPL.bootorder;
            IPL.bootorder = (bootorder << 4) | choice;
            break;
        }
    }
    printf("\n");
}