1 /*        $NetBSD: bootmenu.c,v 1.5 2016/06/11 06:58:42 dholland Exp $          */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/reboot.h>
31 #include <sys/bootblock.h>
32 
33 #include "boot.h"
34 #include "unixdev.h"
35 #include "bootmenu.h"
36 #include "pathnames.h"
37 
38 #define MENUFORMAT_AUTO         0
39 #define MENUFORMAT_NUMBER 1
40 #define MENUFORMAT_LETTER 2
41 
42 void
parsebootconf(const char * conf)43 parsebootconf(const char *conf)
44 {
45           perform_bootcfg(conf, &bootcfg_do_noop, 0);
46 }
47 
48 /*
49  * doboottypemenu will render the menu and parse any user input
50  */
51 static int
getchoicefrominput(char * input,int def)52 getchoicefrominput(char *input, int def)
53 {
54           int choice = -1;
55 
56           if (*input == '\0' || *input == '\r' || *input == '\n')
57                     choice = def;
58           else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
59                     choice = (*input) - 'A';
60           else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
61                     choice = (*input) - 'a';
62           else if (isdigit(*input)) {
63                     choice = atoi(input) - 1;
64                     if (choice < 0 || choice >= bootcfg_info.nummenu)
65                               choice = -1;
66           }
67           return choice;
68 }
69 
70 void
doboottypemenu(void)71 doboottypemenu(void)
72 {
73           char input[80], *ic, *oc;
74           int choice;
75 
76           printf("\n");
77           /* Display menu */
78           if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
79                     for (choice = 0; choice < bootcfg_info.nummenu; choice++)
80                               printf("    %c. %s\n", choice + 'A',
81                                   bootcfg_info.desc[choice]);
82           } else {
83                     /* Can't use %2d format string with libsa */
84                     for (choice = 0; choice < bootcfg_info.nummenu; choice++)
85                               printf("    %s%d. %s\n",
86                                   (choice < 9) ?  " " : "",
87                                   choice + 1,
88                                   bootcfg_info.desc[choice]);
89           }
90           choice = -1;
91           for (;;) {
92                     input[0] = '\0';
93 
94                     if (bootcfg_info.timeout < 0) {
95                               if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
96                                         printf("\nOption: [%c]:",
97                                             bootcfg_info.def + 'A');
98                               else
99                                         printf("\nOption: [%d]:",
100                                             bootcfg_info.def + 1);
101 
102                               kgets(input, sizeof(input));
103                               choice = getchoicefrominput(input, bootcfg_info.def);
104                     } else if (bootcfg_info.timeout == 0)
105                               choice = bootcfg_info.def;
106                     else  {
107                               printf("\nChoose an option; RETURN for default; "
108                                      "SPACE to stop countdown.\n");
109                               if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
110                                         printf("Option %c will be chosen in ",
111                                             bootcfg_info.def + 'A');
112                               else
113                                         printf("Option %d will be chosen in ",
114                                             bootcfg_info.def + 1);
115                               input[0] = awaitkey(bootcfg_info.timeout, 1);
116                               input[1] = '\0';
117                               choice = getchoicefrominput(input, bootcfg_info.def);
118                               /* If invalid key pressed, drop to menu */
119                               if (choice == -1)
120                                         bootcfg_info.timeout = -1;
121                     }
122                     if (choice < 0)
123                               continue;
124                     if (!strcmp(bootcfg_info.command[choice], "prompt")) {
125                               printf("type \"?\" or \"help\" for help.\n");
126                               bootmenu(); /* does not return */
127                     } else {
128                               ic = bootcfg_info.command[choice];
129                               /* Split command string at ; into separate commands */
130                               do {
131                                         oc = input;
132                                         /* Look for ; separator */
133                                         for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
134                                                   *oc++ = *ic;
135                                         if (*input == '\0')
136                                                   continue;
137                                         /* Strip out any trailing spaces */
138                                         oc--;
139                                         for (; *oc == ' ' && oc > input; oc--);
140                                         *++oc = '\0';
141                                         if (*ic == COMMAND_SEPARATOR)
142                                                   ic++;
143                                         /* Stop silly command strings like ;;; */
144                                         if (*input != '\0')
145                                                   docommand(input);
146                                         /* Skip leading spaces */
147                                         for (; *ic == ' '; ic++);
148                               } while (*ic);
149                     }
150           }
151 }
152