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