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