1*f2766c11Swiz /* $NetBSD: bootmenu.c,v 1.5 2022/06/08 21:55:51 wiz Exp $ */
22b6ae1afSjmcneill
32b6ae1afSjmcneill /*-
42b6ae1afSjmcneill * Copyright (c) 2008 The NetBSD Foundation, Inc.
52b6ae1afSjmcneill * All rights reserved.
62b6ae1afSjmcneill *
72b6ae1afSjmcneill * Redistribution and use in source and binary forms, with or without
82b6ae1afSjmcneill * modification, are permitted provided that the following conditions
92b6ae1afSjmcneill * are met:
102b6ae1afSjmcneill * 1. Redistributions of source code must retain the above copyright
112b6ae1afSjmcneill * notice, this list of conditions and the following disclaimer.
122b6ae1afSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
132b6ae1afSjmcneill * notice, this list of conditions and the following disclaimer in the
142b6ae1afSjmcneill * documentation and/or other materials provided with the distribution.
152b6ae1afSjmcneill *
162b6ae1afSjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172b6ae1afSjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182b6ae1afSjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192b6ae1afSjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202b6ae1afSjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212b6ae1afSjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222b6ae1afSjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232b6ae1afSjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242b6ae1afSjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252b6ae1afSjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262b6ae1afSjmcneill * POSSIBILITY OF SUCH DAMAGE.
272b6ae1afSjmcneill */
282b6ae1afSjmcneill
292b6ae1afSjmcneill #ifndef SMALL
302b6ae1afSjmcneill
312b6ae1afSjmcneill #include <sys/types.h>
322b6ae1afSjmcneill #include <sys/reboot.h>
332b6ae1afSjmcneill #include <sys/bootblock.h>
342b6ae1afSjmcneill
352b6ae1afSjmcneill #include <lib/libsa/stand.h>
362b6ae1afSjmcneill #include <lib/libsa/bootcfg.h>
372b6ae1afSjmcneill #include <lib/libsa/ufs.h>
382b6ae1afSjmcneill #include <lib/libkern/libkern.h>
392b6ae1afSjmcneill
402b6ae1afSjmcneill #include "bootmenu.h"
412b6ae1afSjmcneill #include "efiboot.h"
422b6ae1afSjmcneill #include "module.h"
4312431191Sthorpej #include "overlay.h"
442b6ae1afSjmcneill
452b6ae1afSjmcneill static void docommandchoice(int);
462b6ae1afSjmcneill
472b6ae1afSjmcneill extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
482b6ae1afSjmcneill
492b6ae1afSjmcneill #define MENUFORMAT_AUTO 0
502b6ae1afSjmcneill #define MENUFORMAT_NUMBER 1
512b6ae1afSjmcneill #define MENUFORMAT_LETTER 2
522b6ae1afSjmcneill
532b6ae1afSjmcneill /*
542b6ae1afSjmcneill * XXX
552b6ae1afSjmcneill * if module_add, userconf_add are strictly mi they can be folded back
562b6ae1afSjmcneill * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
572b6ae1afSjmcneill */
582b6ae1afSjmcneill static void
do_bootcfg_command(const char * cmd,char * arg)592b6ae1afSjmcneill do_bootcfg_command(const char *cmd, char *arg)
602b6ae1afSjmcneill {
612b6ae1afSjmcneill if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
622b6ae1afSjmcneill module_add(arg);
632b6ae1afSjmcneill else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
642b6ae1afSjmcneill userconf_add(arg);
650e916d8eSjmcneill #ifdef EFIBOOT_FDT
6612431191Sthorpej else if (strcmp(cmd, "dtoverlay") == 0)
6712431191Sthorpej dtoverlay_add(arg);
680e916d8eSjmcneill #endif
692b6ae1afSjmcneill }
702b6ae1afSjmcneill
712b6ae1afSjmcneill int
parsebootconf(const char * conf)722b6ae1afSjmcneill parsebootconf(const char *conf)
732b6ae1afSjmcneill {
742b6ae1afSjmcneill return perform_bootcfg(conf, &do_bootcfg_command, 32768);
752b6ae1afSjmcneill }
762b6ae1afSjmcneill
772b6ae1afSjmcneill /*
782b6ae1afSjmcneill * doboottypemenu will render the menu and parse any user input
792b6ae1afSjmcneill */
802b6ae1afSjmcneill static int
getchoicefrominput(char * input,int def)812b6ae1afSjmcneill getchoicefrominput(char *input, int def)
822b6ae1afSjmcneill {
832b6ae1afSjmcneill int choice, usedef;
842b6ae1afSjmcneill
852b6ae1afSjmcneill choice = -1;
862b6ae1afSjmcneill usedef = 0;
872b6ae1afSjmcneill
882b6ae1afSjmcneill if (*input == '\0' || *input == '\r' || *input == '\n') {
892b6ae1afSjmcneill choice = def;
902b6ae1afSjmcneill usedef = 1;
912b6ae1afSjmcneill } else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
922b6ae1afSjmcneill choice = (*input) - 'A';
932b6ae1afSjmcneill else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
942b6ae1afSjmcneill choice = (*input) - 'a';
952b6ae1afSjmcneill else if (isdigit(*input)) {
962b6ae1afSjmcneill choice = atoi(input) - 1;
972b6ae1afSjmcneill if (choice < 0 || choice >= bootcfg_info.nummenu)
982b6ae1afSjmcneill choice = -1;
992b6ae1afSjmcneill }
1002b6ae1afSjmcneill
1012b6ae1afSjmcneill if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
1022b6ae1afSjmcneill !isdigit(*input) && !usedef)
1032b6ae1afSjmcneill choice = -1;
1042b6ae1afSjmcneill
1052b6ae1afSjmcneill return choice;
1062b6ae1afSjmcneill }
1072b6ae1afSjmcneill
1082b6ae1afSjmcneill static void
docommandchoice(int choice)1092b6ae1afSjmcneill docommandchoice(int choice)
1102b6ae1afSjmcneill {
1112b6ae1afSjmcneill char input[80], *ic, *oc;
1122b6ae1afSjmcneill
1132b6ae1afSjmcneill ic = bootcfg_info.command[choice];
1142b6ae1afSjmcneill /* Split command string at ; into separate commands */
1152b6ae1afSjmcneill do {
1162b6ae1afSjmcneill oc = input;
1172b6ae1afSjmcneill /* Look for ; separator */
1182b6ae1afSjmcneill for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
1192b6ae1afSjmcneill *oc++ = *ic;
1202b6ae1afSjmcneill if (*input == '\0')
1212b6ae1afSjmcneill continue;
1222b6ae1afSjmcneill /* Strip out any trailing spaces */
1232b6ae1afSjmcneill oc--;
1242b6ae1afSjmcneill for (; *oc == ' ' && oc > input; oc--);
1252b6ae1afSjmcneill *++oc = '\0';
1262b6ae1afSjmcneill if (*ic == COMMAND_SEPARATOR)
1272b6ae1afSjmcneill ic++;
1282b6ae1afSjmcneill /* Stop silly command strings like ;;; */
1292b6ae1afSjmcneill if (*input != '\0')
1302b6ae1afSjmcneill docommand(input);
1312b6ae1afSjmcneill /* Skip leading spaces */
1322b6ae1afSjmcneill for (; *ic == ' '; ic++);
1332b6ae1afSjmcneill } while (*ic);
1342b6ae1afSjmcneill }
1352b6ae1afSjmcneill
1362b6ae1afSjmcneill __dead void
doboottypemenu(void)1372b6ae1afSjmcneill doboottypemenu(void)
1382b6ae1afSjmcneill {
1392b6ae1afSjmcneill int choice;
1402b6ae1afSjmcneill char input[80];
1412b6ae1afSjmcneill
1422b6ae1afSjmcneill printf("\n");
1432b6ae1afSjmcneill /* Display menu */
1442b6ae1afSjmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
1452b6ae1afSjmcneill for (choice = 0; choice < bootcfg_info.nummenu; choice++)
1462b6ae1afSjmcneill printf(" %c. %s\n", choice + 'A',
1472b6ae1afSjmcneill bootcfg_info.desc[choice]);
1482b6ae1afSjmcneill } else {
1492b6ae1afSjmcneill /* Can't use %2d format string with libsa */
1502b6ae1afSjmcneill for (choice = 0; choice < bootcfg_info.nummenu; choice++)
1512b6ae1afSjmcneill printf(" %s%d. %s\n",
1522b6ae1afSjmcneill (choice < 9) ? " " : "",
1532b6ae1afSjmcneill choice + 1,
1542b6ae1afSjmcneill bootcfg_info.desc[choice]);
1552b6ae1afSjmcneill }
1562b6ae1afSjmcneill choice = -1;
1572b6ae1afSjmcneill for (;;) {
1582b6ae1afSjmcneill input[0] = '\0';
1592b6ae1afSjmcneill
1602b6ae1afSjmcneill if (bootcfg_info.timeout < 0) {
1612b6ae1afSjmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
1622b6ae1afSjmcneill printf("\nOption: [%c]:",
1632b6ae1afSjmcneill bootcfg_info.def + 'A');
1642b6ae1afSjmcneill else
1652b6ae1afSjmcneill printf("\nOption: [%d]:",
1662b6ae1afSjmcneill bootcfg_info.def + 1);
1672b6ae1afSjmcneill
1682b6ae1afSjmcneill kgets(input, sizeof(input));
1692b6ae1afSjmcneill choice = getchoicefrominput(input, bootcfg_info.def);
1702b6ae1afSjmcneill } else if (bootcfg_info.timeout == 0)
1712b6ae1afSjmcneill choice = bootcfg_info.def;
1722b6ae1afSjmcneill else {
1732b6ae1afSjmcneill printf("\nChoose an option; RETURN for default; "
1742b6ae1afSjmcneill "SPACE to stop countdown.\n");
1752b6ae1afSjmcneill if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
1762b6ae1afSjmcneill printf("Option %c will be chosen in ",
1772b6ae1afSjmcneill bootcfg_info.def + 'A');
1782b6ae1afSjmcneill else
1792b6ae1afSjmcneill printf("Option %d will be chosen in ",
1802b6ae1afSjmcneill bootcfg_info.def + 1);
1812b6ae1afSjmcneill input[0] = awaitkey(bootcfg_info.timeout, 1);
1822b6ae1afSjmcneill input[1] = '\0';
1832b6ae1afSjmcneill choice = getchoicefrominput(input, bootcfg_info.def);
1842b6ae1afSjmcneill /* If invalid key pressed, drop to menu */
1852b6ae1afSjmcneill if (choice == -1)
1862b6ae1afSjmcneill bootcfg_info.timeout = -1;
1872b6ae1afSjmcneill }
1882b6ae1afSjmcneill if (choice < 0)
1892b6ae1afSjmcneill continue;
1902b6ae1afSjmcneill if (!strcmp(bootcfg_info.command[choice], "prompt")) {
1912b6ae1afSjmcneill printf("type \"?\" or \"help\" for help.\n");
1922b6ae1afSjmcneill bootprompt(); /* does not return */
1932b6ae1afSjmcneill } else {
1942b6ae1afSjmcneill docommandchoice(choice);
1952b6ae1afSjmcneill }
1962b6ae1afSjmcneill
1972b6ae1afSjmcneill }
1982b6ae1afSjmcneill }
1992b6ae1afSjmcneill
2002b6ae1afSjmcneill #endif /* !SMALL */
201