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