1 /* $NetBSD: bootmenu.c,v 1.21 2025/05/06 18:16:12 pgoyette 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 #ifndef SMALL 30 31 #include <sys/types.h> 32 #include <sys/reboot.h> 33 #include <sys/bootblock.h> 34 35 #include <lib/libsa/stand.h> 36 #include <lib/libsa/bootcfg.h> 37 #include <lib/libsa/ufs.h> 38 #include <lib/libkern/libkern.h> 39 40 #include <libi386.h> 41 #include <bootmenu.h> 42 43 static void docommandchoice(int); 44 45 extern struct x86_boot_params boot_params; 46 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[]; 47 48 #define MENUFORMAT_AUTO 0 49 #define MENUFORMAT_NUMBER 1 50 #define MENUFORMAT_LETTER 2 51 52 /* 53 * XXX 54 * if module_add, userconf_add are strictly mi they can be folded back 55 * into sys/lib/libsa/bootcfg.c:perform_bootcfg(). 56 */ 57 static void 58 do_bootcfg_command(const char *cmd, char *arg) 59 { 60 if (strcmp(cmd, BOOTCFG_CMD_DEV) == 0) 61 command_dev(arg); 62 else if (strcmp(cmd, BOOTCFG_CMD_FS) == 0) 63 fs_add(arg); 64 else if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0) 65 module_add(arg); 66 else if (strcmp(cmd, BOOTCFG_CMD_RNDSEED) == 0) 67 rnd_add(arg); 68 else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0) 69 userconf_add(arg); 70 } 71 72 int 73 parsebootconf(const char *conf) 74 { 75 return perform_bootcfg(conf, &do_bootcfg_command, 32768); 76 } 77 78 /* 79 * doboottypemenu will render the menu and parse any user input 80 */ 81 static int 82 getchoicefrominput(char *input, int def) 83 { 84 int choice, usedef; 85 86 choice = -1; 87 usedef = 0; 88 89 if (*input == '\0' || *input == '\r' || *input == '\n') { 90 choice = def; 91 usedef = 1; 92 } else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A') 93 choice = (*input) - 'A'; 94 else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a') 95 choice = (*input) - 'a'; 96 else if (isdigit(*input)) { 97 choice = atoi(input) - 1; 98 if (choice < 0 || choice >= bootcfg_info.nummenu) 99 choice = -1; 100 } 101 102 if (bootcfg_info.menuformat != MENUFORMAT_LETTER && 103 !isdigit(*input) && !usedef) 104 choice = -1; 105 106 return choice; 107 } 108 109 static void 110 docommandchoice(int choice) 111 { 112 char input[80], *ic, *oc; 113 114 ic = bootcfg_info.command[choice]; 115 /* Split command string at ; into separate commands */ 116 do { 117 oc = input; 118 /* Look for ; separator */ 119 for (; *ic && *ic != COMMAND_SEPARATOR; ic++) 120 *oc++ = *ic; 121 if (*input == '\0') 122 continue; 123 /* Strip out any trailing spaces */ 124 oc--; 125 for (; *oc == ' ' && oc > input; oc--); 126 *++oc = '\0'; 127 if (*ic == COMMAND_SEPARATOR) 128 ic++; 129 /* Stop silly command strings like ;;; */ 130 if (*input != '\0') 131 docommand(input); 132 /* Skip leading spaces */ 133 for (; *ic == ' '; ic++); 134 } while (*ic); 135 } 136 137 __dead void 138 doboottypemenu(void) 139 { 140 int choice; 141 char input[80]; 142 143 /* 144 * If we have a single menu entry with empty description and 145 * timeout = 0 we do not display any menu. 146 */ 147 if ((bootcfg_info.nummenu > 0 && 148 bootcfg_info.desc[0] != bootcfg_info.command[0] && 149 bootcfg_info.desc[0][0] != 0) || bootcfg_info.timeout > 0) { 150 printf("\n"); 151 152 /* Display menu */ 153 if (bootcfg_info.menuformat == MENUFORMAT_LETTER) { 154 for (choice = 0; choice < bootcfg_info.nummenu; 155 choice++) 156 printf(" %c. %s\n", choice + 'A', 157 bootcfg_info.desc[choice]); 158 } else { 159 /* Can't use %2d format string with libsa */ 160 for (choice = 0; choice < bootcfg_info.nummenu; 161 choice++) 162 printf(" %s%d. %s\n", 163 (choice < 9) ? " " : "", 164 choice + 1, 165 bootcfg_info.desc[choice]); 166 } 167 } 168 choice = -1; 169 for (;;) { 170 input[0] = '\0'; 171 172 if (bootcfg_info.timeout < 0) { 173 if (bootcfg_info.menuformat == MENUFORMAT_LETTER) 174 printf("\nOption: [%c]:", 175 bootcfg_info.def + 'A'); 176 else 177 printf("\nOption: [%d]:", 178 bootcfg_info.def + 1); 179 180 kgets(input, sizeof(input)); 181 choice = getchoicefrominput(input, bootcfg_info.def); 182 } else if (bootcfg_info.timeout == 0) 183 choice = bootcfg_info.def; 184 else { 185 printf("\nChoose an option; RETURN for default; " 186 "SPACE to stop countdown.\n"); 187 if (bootcfg_info.menuformat == MENUFORMAT_LETTER) 188 printf("Option %c will be chosen in ", 189 bootcfg_info.def + 'A'); 190 else 191 printf("Option %d will be chosen in ", 192 bootcfg_info.def + 1); 193 input[0] = awaitkey(bootcfg_info.timeout, 1); 194 input[1] = '\0'; 195 choice = getchoicefrominput(input, bootcfg_info.def); 196 /* If invalid key pressed, drop to menu */ 197 if (choice == -1) 198 bootcfg_info.timeout = -1; 199 } 200 if (choice < 0) 201 continue; 202 if (!strcmp(bootcfg_info.command[choice], "prompt") && 203 ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 || 204 check_password((char *)boot_params.bp_password))) { 205 printf("type \"?\" or \"help\" for help.\n"); 206 bootmenu(); /* does not return */ 207 } else { 208 docommandchoice(choice); 209 } 210 211 } 212 } 213 214 #endif /* !SMALL */ 215