Home | History | Annotate | Line # | Download | only in efiboot
bootmenu.c revision 1.5
      1  1.5       wiz /*	$NetBSD: bootmenu.c,v 1.5 2022/06/08 21:55:51 wiz Exp $	*/
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #ifndef SMALL
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/types.h>
     32  1.1  jmcneill #include <sys/reboot.h>
     33  1.1  jmcneill #include <sys/bootblock.h>
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <lib/libsa/stand.h>
     36  1.1  jmcneill #include <lib/libsa/bootcfg.h>
     37  1.1  jmcneill #include <lib/libsa/ufs.h>
     38  1.1  jmcneill #include <lib/libkern/libkern.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include "bootmenu.h"
     41  1.1  jmcneill #include "efiboot.h"
     42  1.1  jmcneill #include "module.h"
     43  1.2   thorpej #include "overlay.h"
     44  1.1  jmcneill 
     45  1.1  jmcneill static void docommandchoice(int);
     46  1.1  jmcneill 
     47  1.1  jmcneill extern	const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     48  1.1  jmcneill 
     49  1.1  jmcneill #define MENUFORMAT_AUTO	  0
     50  1.1  jmcneill #define MENUFORMAT_NUMBER 1
     51  1.1  jmcneill #define MENUFORMAT_LETTER 2
     52  1.1  jmcneill 
     53  1.1  jmcneill /*
     54  1.1  jmcneill  * XXX
     55  1.1  jmcneill  * if module_add, userconf_add are strictly mi they can be folded back
     56  1.1  jmcneill  * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
     57  1.1  jmcneill  */
     58  1.1  jmcneill static void
     59  1.1  jmcneill do_bootcfg_command(const char *cmd, char *arg)
     60  1.1  jmcneill {
     61  1.1  jmcneill 	if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
     62  1.1  jmcneill 		module_add(arg);
     63  1.1  jmcneill 	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
     64  1.1  jmcneill 		userconf_add(arg);
     65  1.3  jmcneill #ifdef EFIBOOT_FDT
     66  1.2   thorpej 	else if (strcmp(cmd, "dtoverlay") == 0)
     67  1.2   thorpej 		dtoverlay_add(arg);
     68  1.3  jmcneill #endif
     69  1.1  jmcneill }
     70  1.1  jmcneill 
     71  1.1  jmcneill int
     72  1.1  jmcneill parsebootconf(const char *conf)
     73  1.1  jmcneill {
     74  1.1  jmcneill 	return perform_bootcfg(conf, &do_bootcfg_command, 32768);
     75  1.1  jmcneill }
     76  1.1  jmcneill 
     77  1.1  jmcneill /*
     78  1.1  jmcneill  * doboottypemenu will render the menu and parse any user input
     79  1.1  jmcneill  */
     80  1.1  jmcneill static int
     81  1.1  jmcneill getchoicefrominput(char *input, int def)
     82  1.1  jmcneill {
     83  1.1  jmcneill 	int choice, usedef;
     84  1.1  jmcneill 
     85  1.1  jmcneill 	choice = -1;
     86  1.1  jmcneill 	usedef = 0;
     87  1.1  jmcneill 
     88  1.1  jmcneill 	if (*input == '\0' || *input == '\r' || *input == '\n') {
     89  1.1  jmcneill 		choice = def;
     90  1.1  jmcneill 		usedef = 1;
     91  1.1  jmcneill 	} else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
     92  1.1  jmcneill 		choice = (*input) - 'A';
     93  1.1  jmcneill 	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
     94  1.1  jmcneill 		choice = (*input) - 'a';
     95  1.1  jmcneill 	else if (isdigit(*input)) {
     96  1.1  jmcneill 		choice = atoi(input) - 1;
     97  1.1  jmcneill 		if (choice < 0 || choice >= bootcfg_info.nummenu)
     98  1.1  jmcneill 			choice = -1;
     99  1.1  jmcneill 	}
    100  1.1  jmcneill 
    101  1.1  jmcneill 	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
    102  1.1  jmcneill 	    !isdigit(*input) && !usedef)
    103  1.1  jmcneill 		choice = -1;
    104  1.1  jmcneill 
    105  1.1  jmcneill 	return choice;
    106  1.1  jmcneill }
    107  1.1  jmcneill 
    108  1.1  jmcneill static void
    109  1.1  jmcneill docommandchoice(int choice)
    110  1.1  jmcneill {
    111  1.1  jmcneill 	char input[80], *ic, *oc;
    112  1.1  jmcneill 
    113  1.1  jmcneill 	ic = bootcfg_info.command[choice];
    114  1.1  jmcneill 	/* Split command string at ; into separate commands */
    115  1.1  jmcneill 	do {
    116  1.1  jmcneill 		oc = input;
    117  1.1  jmcneill 		/* Look for ; separator */
    118  1.1  jmcneill 		for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    119  1.1  jmcneill 			*oc++ = *ic;
    120  1.1  jmcneill 		if (*input == '\0')
    121  1.1  jmcneill 			continue;
    122  1.1  jmcneill 		/* Strip out any trailing spaces */
    123  1.1  jmcneill 		oc--;
    124  1.1  jmcneill 		for (; *oc == ' ' && oc > input; oc--);
    125  1.1  jmcneill 		*++oc = '\0';
    126  1.1  jmcneill 		if (*ic == COMMAND_SEPARATOR)
    127  1.1  jmcneill 			ic++;
    128  1.1  jmcneill 		/* Stop silly command strings like ;;; */
    129  1.1  jmcneill 		if (*input != '\0')
    130  1.1  jmcneill 			docommand(input);
    131  1.1  jmcneill 		/* Skip leading spaces */
    132  1.1  jmcneill 		for (; *ic == ' '; ic++);
    133  1.1  jmcneill 	} while (*ic);
    134  1.1  jmcneill }
    135  1.1  jmcneill 
    136  1.1  jmcneill __dead void
    137  1.1  jmcneill doboottypemenu(void)
    138  1.1  jmcneill {
    139  1.1  jmcneill 	int choice;
    140  1.1  jmcneill 	char input[80];
    141  1.1  jmcneill 
    142  1.1  jmcneill 	printf("\n");
    143  1.1  jmcneill 	/* Display menu */
    144  1.1  jmcneill 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
    145  1.1  jmcneill 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
    146  1.1  jmcneill 			printf("    %c. %s\n", choice + 'A',
    147  1.1  jmcneill 			    bootcfg_info.desc[choice]);
    148  1.1  jmcneill 	} else {
    149  1.1  jmcneill 		/* Can't use %2d format string with libsa */
    150  1.1  jmcneill 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
    151  1.1  jmcneill 			printf("    %s%d. %s\n",
    152  1.1  jmcneill 			    (choice < 9) ?  " " : "",
    153  1.1  jmcneill 			    choice + 1,
    154  1.1  jmcneill 			    bootcfg_info.desc[choice]);
    155  1.1  jmcneill 	}
    156  1.1  jmcneill 	choice = -1;
    157  1.1  jmcneill 	for (;;) {
    158  1.1  jmcneill 		input[0] = '\0';
    159  1.1  jmcneill 
    160  1.1  jmcneill 		if (bootcfg_info.timeout < 0) {
    161  1.1  jmcneill 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    162  1.1  jmcneill 				printf("\nOption: [%c]:",
    163  1.1  jmcneill 				    bootcfg_info.def + 'A');
    164  1.1  jmcneill 			else
    165  1.1  jmcneill 				printf("\nOption: [%d]:",
    166  1.1  jmcneill 				    bootcfg_info.def + 1);
    167  1.1  jmcneill 
    168  1.1  jmcneill 			kgets(input, sizeof(input));
    169  1.1  jmcneill 			choice = getchoicefrominput(input, bootcfg_info.def);
    170  1.1  jmcneill 		} else if (bootcfg_info.timeout == 0)
    171  1.1  jmcneill 			choice = bootcfg_info.def;
    172  1.1  jmcneill 		else  {
    173  1.1  jmcneill 			printf("\nChoose an option; RETURN for default; "
    174  1.1  jmcneill 			       "SPACE to stop countdown.\n");
    175  1.1  jmcneill 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    176  1.1  jmcneill 				printf("Option %c will be chosen in ",
    177  1.1  jmcneill 				    bootcfg_info.def + 'A');
    178  1.1  jmcneill 			else
    179  1.1  jmcneill 				printf("Option %d will be chosen in ",
    180  1.1  jmcneill 				    bootcfg_info.def + 1);
    181  1.1  jmcneill 			input[0] = awaitkey(bootcfg_info.timeout, 1);
    182  1.1  jmcneill 			input[1] = '\0';
    183  1.1  jmcneill 			choice = getchoicefrominput(input, bootcfg_info.def);
    184  1.1  jmcneill 			/* If invalid key pressed, drop to menu */
    185  1.1  jmcneill 			if (choice == -1)
    186  1.1  jmcneill 				bootcfg_info.timeout = -1;
    187  1.1  jmcneill 		}
    188  1.1  jmcneill 		if (choice < 0)
    189  1.1  jmcneill 			continue;
    190  1.1  jmcneill 		if (!strcmp(bootcfg_info.command[choice], "prompt")) {
    191  1.1  jmcneill 			printf("type \"?\" or \"help\" for help.\n");
    192  1.1  jmcneill 			bootprompt(); /* does not return */
    193  1.1  jmcneill 		} else {
    194  1.1  jmcneill 			docommandchoice(choice);
    195  1.1  jmcneill 		}
    196  1.1  jmcneill 
    197  1.1  jmcneill 	}
    198  1.1  jmcneill }
    199  1.1  jmcneill 
    200  1.1  jmcneill #endif	/* !SMALL */
    201