Home | History | Annotate | Line # | Download | only in efiboot
bootmenu.c revision 1.3
      1  1.3  jmcneill /*	$NetBSD: bootmenu.c,v 1.3 2021/10/06 10:13:19 jmcneill 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 #if notyet
     64  1.1  jmcneill 	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
     65  1.1  jmcneill 		userconf_add(arg);
     66  1.1  jmcneill #endif
     67  1.3  jmcneill #ifdef EFIBOOT_FDT
     68  1.2   thorpej 	else if (strcmp(cmd, "dtoverlay") == 0)
     69  1.2   thorpej 		dtoverlay_add(arg);
     70  1.3  jmcneill #endif
     71  1.1  jmcneill }
     72  1.1  jmcneill 
     73  1.1  jmcneill int
     74  1.1  jmcneill parsebootconf(const char *conf)
     75  1.1  jmcneill {
     76  1.1  jmcneill 	return perform_bootcfg(conf, &do_bootcfg_command, 32768);
     77  1.1  jmcneill }
     78  1.1  jmcneill 
     79  1.1  jmcneill /*
     80  1.1  jmcneill  * doboottypemenu will render the menu and parse any user input
     81  1.1  jmcneill  */
     82  1.1  jmcneill static int
     83  1.1  jmcneill getchoicefrominput(char *input, int def)
     84  1.1  jmcneill {
     85  1.1  jmcneill 	int choice, usedef;
     86  1.1  jmcneill 
     87  1.1  jmcneill 	choice = -1;
     88  1.1  jmcneill 	usedef = 0;
     89  1.1  jmcneill 
     90  1.1  jmcneill 	if (*input == '\0' || *input == '\r' || *input == '\n') {
     91  1.1  jmcneill 		choice = def;
     92  1.1  jmcneill 		usedef = 1;
     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 (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
     96  1.1  jmcneill 		choice = (*input) - 'a';
     97  1.1  jmcneill 	else if (isdigit(*input)) {
     98  1.1  jmcneill 		choice = atoi(input) - 1;
     99  1.1  jmcneill 		if (choice < 0 || choice >= bootcfg_info.nummenu)
    100  1.1  jmcneill 			choice = -1;
    101  1.1  jmcneill 	}
    102  1.1  jmcneill 
    103  1.1  jmcneill 	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
    104  1.1  jmcneill 	    !isdigit(*input) && !usedef)
    105  1.1  jmcneill 		choice = -1;
    106  1.1  jmcneill 
    107  1.1  jmcneill 	return choice;
    108  1.1  jmcneill }
    109  1.1  jmcneill 
    110  1.1  jmcneill static void
    111  1.1  jmcneill docommandchoice(int choice)
    112  1.1  jmcneill {
    113  1.1  jmcneill 	char input[80], *ic, *oc;
    114  1.1  jmcneill 
    115  1.1  jmcneill 	ic = bootcfg_info.command[choice];
    116  1.1  jmcneill 	/* Split command string at ; into separate commands */
    117  1.1  jmcneill 	do {
    118  1.1  jmcneill 		oc = input;
    119  1.1  jmcneill 		/* Look for ; separator */
    120  1.1  jmcneill 		for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    121  1.1  jmcneill 			*oc++ = *ic;
    122  1.1  jmcneill 		if (*input == '\0')
    123  1.1  jmcneill 			continue;
    124  1.1  jmcneill 		/* Strip out any trailing spaces */
    125  1.1  jmcneill 		oc--;
    126  1.1  jmcneill 		for (; *oc == ' ' && oc > input; oc--);
    127  1.1  jmcneill 		*++oc = '\0';
    128  1.1  jmcneill 		if (*ic == COMMAND_SEPARATOR)
    129  1.1  jmcneill 			ic++;
    130  1.1  jmcneill 		/* Stop silly command strings like ;;; */
    131  1.1  jmcneill 		if (*input != '\0')
    132  1.1  jmcneill 			docommand(input);
    133  1.1  jmcneill 		/* Skip leading spaces */
    134  1.1  jmcneill 		for (; *ic == ' '; ic++);
    135  1.1  jmcneill 	} while (*ic);
    136  1.1  jmcneill }
    137  1.1  jmcneill 
    138  1.1  jmcneill void
    139  1.1  jmcneill bootdefault(void)
    140  1.1  jmcneill {
    141  1.1  jmcneill 	int choice;
    142  1.1  jmcneill 	static int entered;
    143  1.1  jmcneill 
    144  1.1  jmcneill 	if (bootcfg_info.nummenu > 0) {
    145  1.1  jmcneill 		if (entered) {
    146  1.1  jmcneill 			printf("default boot twice, skipping...\n");
    147  1.1  jmcneill 			return;
    148  1.1  jmcneill 		}
    149  1.1  jmcneill 		entered = 1;
    150  1.1  jmcneill 		choice = bootcfg_info.def;
    151  1.1  jmcneill 		printf("command(s): %s\n", bootcfg_info.command[choice]);
    152  1.1  jmcneill 		docommandchoice(choice);
    153  1.1  jmcneill 	}
    154  1.1  jmcneill }
    155  1.1  jmcneill 
    156  1.1  jmcneill __dead void
    157  1.1  jmcneill doboottypemenu(void)
    158  1.1  jmcneill {
    159  1.1  jmcneill 	int choice;
    160  1.1  jmcneill 	char input[80];
    161  1.1  jmcneill 
    162  1.1  jmcneill 	printf("\n");
    163  1.1  jmcneill 	/* Display menu */
    164  1.1  jmcneill 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
    165  1.1  jmcneill 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
    166  1.1  jmcneill 			printf("    %c. %s\n", choice + 'A',
    167  1.1  jmcneill 			    bootcfg_info.desc[choice]);
    168  1.1  jmcneill 	} else {
    169  1.1  jmcneill 		/* Can't use %2d format string with libsa */
    170  1.1  jmcneill 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
    171  1.1  jmcneill 			printf("    %s%d. %s\n",
    172  1.1  jmcneill 			    (choice < 9) ?  " " : "",
    173  1.1  jmcneill 			    choice + 1,
    174  1.1  jmcneill 			    bootcfg_info.desc[choice]);
    175  1.1  jmcneill 	}
    176  1.1  jmcneill 	choice = -1;
    177  1.1  jmcneill 	for (;;) {
    178  1.1  jmcneill 		input[0] = '\0';
    179  1.1  jmcneill 
    180  1.1  jmcneill 		if (bootcfg_info.timeout < 0) {
    181  1.1  jmcneill 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    182  1.1  jmcneill 				printf("\nOption: [%c]:",
    183  1.1  jmcneill 				    bootcfg_info.def + 'A');
    184  1.1  jmcneill 			else
    185  1.1  jmcneill 				printf("\nOption: [%d]:",
    186  1.1  jmcneill 				    bootcfg_info.def + 1);
    187  1.1  jmcneill 
    188  1.1  jmcneill 			kgets(input, sizeof(input));
    189  1.1  jmcneill 			choice = getchoicefrominput(input, bootcfg_info.def);
    190  1.1  jmcneill 		} else if (bootcfg_info.timeout == 0)
    191  1.1  jmcneill 			choice = bootcfg_info.def;
    192  1.1  jmcneill 		else  {
    193  1.1  jmcneill 			printf("\nChoose an option; RETURN for default; "
    194  1.1  jmcneill 			       "SPACE to stop countdown.\n");
    195  1.1  jmcneill 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    196  1.1  jmcneill 				printf("Option %c will be chosen in ",
    197  1.1  jmcneill 				    bootcfg_info.def + 'A');
    198  1.1  jmcneill 			else
    199  1.1  jmcneill 				printf("Option %d will be chosen in ",
    200  1.1  jmcneill 				    bootcfg_info.def + 1);
    201  1.1  jmcneill 			input[0] = awaitkey(bootcfg_info.timeout, 1);
    202  1.1  jmcneill 			input[1] = '\0';
    203  1.1  jmcneill 			choice = getchoicefrominput(input, bootcfg_info.def);
    204  1.1  jmcneill 			/* If invalid key pressed, drop to menu */
    205  1.1  jmcneill 			if (choice == -1)
    206  1.1  jmcneill 				bootcfg_info.timeout = -1;
    207  1.1  jmcneill 		}
    208  1.1  jmcneill 		if (choice < 0)
    209  1.1  jmcneill 			continue;
    210  1.1  jmcneill 		if (!strcmp(bootcfg_info.command[choice], "prompt")) {
    211  1.1  jmcneill 			printf("type \"?\" or \"help\" for help.\n");
    212  1.1  jmcneill 			bootprompt(); /* does not return */
    213  1.1  jmcneill 		} else {
    214  1.1  jmcneill 			docommandchoice(choice);
    215  1.1  jmcneill 		}
    216  1.1  jmcneill 
    217  1.1  jmcneill 	}
    218  1.1  jmcneill }
    219  1.1  jmcneill 
    220  1.1  jmcneill #endif	/* !SMALL */
    221