xref: /minix3/sys/arch/i386/stand/lib/bootmenu.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: bootmenu.c,v 1.14 2014/08/10 07:40:49 isaki Exp $	*/
258a2b000SEvgeniy Ivanov 
358a2b000SEvgeniy Ivanov /*-
458a2b000SEvgeniy Ivanov  * Copyright (c) 2008 The NetBSD Foundation, Inc.
558a2b000SEvgeniy Ivanov  * All rights reserved.
658a2b000SEvgeniy Ivanov  *
758a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
858a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
958a2b000SEvgeniy Ivanov  * are met:
1058a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
1158a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
1258a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
1358a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
1458a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
1558a2b000SEvgeniy Ivanov  *
1658a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1758a2b000SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1858a2b000SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1958a2b000SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2058a2b000SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2158a2b000SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2258a2b000SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2358a2b000SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2458a2b000SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2558a2b000SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2658a2b000SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
2758a2b000SEvgeniy Ivanov  */
2858a2b000SEvgeniy Ivanov 
2958a2b000SEvgeniy Ivanov #ifndef SMALL
3058a2b000SEvgeniy Ivanov 
3158a2b000SEvgeniy Ivanov #include <sys/types.h>
3258a2b000SEvgeniy Ivanov #include <sys/reboot.h>
3358a2b000SEvgeniy Ivanov #include <sys/bootblock.h>
3458a2b000SEvgeniy Ivanov 
3558a2b000SEvgeniy Ivanov #include <lib/libsa/stand.h>
36*0a6a1f1dSLionel Sambuc #include <lib/libsa/bootcfg.h>
3758a2b000SEvgeniy Ivanov #include <lib/libsa/ufs.h>
3858a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
3958a2b000SEvgeniy Ivanov 
4058a2b000SEvgeniy Ivanov #include <libi386.h>
4158a2b000SEvgeniy Ivanov #include <bootmenu.h>
4258a2b000SEvgeniy Ivanov 
4384d9c625SLionel Sambuc static void docommandchoice(int);
4484d9c625SLionel Sambuc 
4558a2b000SEvgeniy Ivanov extern struct x86_boot_params boot_params;
4658a2b000SEvgeniy Ivanov extern	const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
4758a2b000SEvgeniy Ivanov 
4858a2b000SEvgeniy Ivanov #define MENUFORMAT_AUTO	  0
4958a2b000SEvgeniy Ivanov #define MENUFORMAT_NUMBER 1
5058a2b000SEvgeniy Ivanov #define MENUFORMAT_LETTER 2
5158a2b000SEvgeniy Ivanov 
52*0a6a1f1dSLionel Sambuc /*
53*0a6a1f1dSLionel Sambuc  * XXX
54*0a6a1f1dSLionel Sambuc  * if module_add, userconf_add are strictly mi they can be folded back
55*0a6a1f1dSLionel Sambuc  * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
56*0a6a1f1dSLionel Sambuc  */
57*0a6a1f1dSLionel Sambuc static void
do_bootcfg_command(const char * cmd,char * arg)58*0a6a1f1dSLionel Sambuc do_bootcfg_command(const char *cmd, char *arg)
5958a2b000SEvgeniy Ivanov {
60*0a6a1f1dSLionel Sambuc 	if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
61*0a6a1f1dSLionel Sambuc 		module_add(arg);
62*0a6a1f1dSLionel Sambuc 	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
63*0a6a1f1dSLionel Sambuc 		userconf_add(arg);
6458a2b000SEvgeniy Ivanov }
6558a2b000SEvgeniy Ivanov 
6658a2b000SEvgeniy Ivanov void
parsebootconf(const char * conf)6758a2b000SEvgeniy Ivanov parsebootconf(const char *conf)
6858a2b000SEvgeniy Ivanov {
69*0a6a1f1dSLionel Sambuc 	perform_bootcfg(conf, &do_bootcfg_command, 32768);
7058a2b000SEvgeniy Ivanov }
7158a2b000SEvgeniy Ivanov 
7258a2b000SEvgeniy Ivanov /*
7358a2b000SEvgeniy Ivanov  * doboottypemenu will render the menu and parse any user input
7458a2b000SEvgeniy Ivanov  */
7558a2b000SEvgeniy Ivanov static int
getchoicefrominput(char * input,int def)7658a2b000SEvgeniy Ivanov getchoicefrominput(char *input, int def)
7758a2b000SEvgeniy Ivanov {
7858a2b000SEvgeniy Ivanov 	int choice, usedef;
7958a2b000SEvgeniy Ivanov 
8058a2b000SEvgeniy Ivanov 	choice = -1;
8158a2b000SEvgeniy Ivanov 	usedef = 0;
8258a2b000SEvgeniy Ivanov 
8358a2b000SEvgeniy Ivanov 	if (*input == '\0' || *input == '\r' || *input == '\n') {
8458a2b000SEvgeniy Ivanov 		choice = def;
8558a2b000SEvgeniy Ivanov 		usedef = 1;
86*0a6a1f1dSLionel Sambuc 	} else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
8758a2b000SEvgeniy Ivanov 		choice = (*input) - 'A';
88*0a6a1f1dSLionel Sambuc 	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
8958a2b000SEvgeniy Ivanov 		choice = (*input) - 'a';
90*0a6a1f1dSLionel Sambuc 	else if (isdigit(*input)) {
9158a2b000SEvgeniy Ivanov 		choice = atoi(input) - 1;
92*0a6a1f1dSLionel Sambuc 		if (choice < 0 || choice >= bootcfg_info.nummenu)
9358a2b000SEvgeniy Ivanov 			choice = -1;
9458a2b000SEvgeniy Ivanov 	}
9558a2b000SEvgeniy Ivanov 
96*0a6a1f1dSLionel Sambuc 	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
97*0a6a1f1dSLionel Sambuc 	    !isdigit(*input) && !usedef)
9858a2b000SEvgeniy Ivanov 		choice = -1;
9958a2b000SEvgeniy Ivanov 
10058a2b000SEvgeniy Ivanov 	return choice;
10158a2b000SEvgeniy Ivanov }
10258a2b000SEvgeniy Ivanov 
1039733fcdbSDavid van Moolenbroek static void
docommandchoice(int choice)10484d9c625SLionel Sambuc docommandchoice(int choice)
10584d9c625SLionel Sambuc {
10684d9c625SLionel Sambuc 	char input[80], *ic, *oc;
10784d9c625SLionel Sambuc 
108*0a6a1f1dSLionel Sambuc 	ic = bootcfg_info.command[choice];
10984d9c625SLionel Sambuc 	/* Split command string at ; into separate commands */
11084d9c625SLionel Sambuc 	do {
11184d9c625SLionel Sambuc 		oc = input;
11284d9c625SLionel Sambuc 		/* Look for ; separator */
11384d9c625SLionel Sambuc 		for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
11484d9c625SLionel Sambuc 			*oc++ = *ic;
11584d9c625SLionel Sambuc 		if (*input == '\0')
11684d9c625SLionel Sambuc 			continue;
11784d9c625SLionel Sambuc 		/* Strip out any trailing spaces */
11884d9c625SLionel Sambuc 		oc--;
11984d9c625SLionel Sambuc 		for (; *oc == ' ' && oc > input; oc--);
12084d9c625SLionel Sambuc 		*++oc = '\0';
12184d9c625SLionel Sambuc 		if (*ic == COMMAND_SEPARATOR)
12284d9c625SLionel Sambuc 			ic++;
12384d9c625SLionel Sambuc 		/* Stop silly command strings like ;;; */
12484d9c625SLionel Sambuc 		if (*input != '\0')
12584d9c625SLionel Sambuc 			docommand(input);
12684d9c625SLionel Sambuc 		/* Skip leading spaces */
12784d9c625SLionel Sambuc 		for (; *ic == ' '; ic++);
12884d9c625SLionel Sambuc 	} while (*ic);
12984d9c625SLionel Sambuc }
13084d9c625SLionel Sambuc 
13184d9c625SLionel Sambuc void
bootdefault(void)13284d9c625SLionel Sambuc bootdefault(void)
13384d9c625SLionel Sambuc {
13484d9c625SLionel Sambuc 	int choice;
13584d9c625SLionel Sambuc 	static int entered;
13684d9c625SLionel Sambuc 
137*0a6a1f1dSLionel Sambuc 	if (bootcfg_info.nummenu > 0) {
13884d9c625SLionel Sambuc 		if (entered) {
13984d9c625SLionel Sambuc 			printf("default boot twice, skipping...\n");
14084d9c625SLionel Sambuc 			return;
14184d9c625SLionel Sambuc 		}
14284d9c625SLionel Sambuc 		entered = 1;
143*0a6a1f1dSLionel Sambuc 		choice = bootcfg_info.def;
144*0a6a1f1dSLionel Sambuc 		printf("command(s): %s\n", bootcfg_info.command[choice]);
14584d9c625SLionel Sambuc 		docommandchoice(choice);
14684d9c625SLionel Sambuc 	}
14784d9c625SLionel Sambuc }
14884d9c625SLionel Sambuc 
14984d9c625SLionel Sambuc #if defined(__minix)
15084d9c625SLionel Sambuc static void
showmenu(void)1519733fcdbSDavid van Moolenbroek showmenu(void)
15258a2b000SEvgeniy Ivanov {
15358a2b000SEvgeniy Ivanov 	int choice;
15458a2b000SEvgeniy Ivanov 
15558a2b000SEvgeniy Ivanov 	printf("\n");
15658a2b000SEvgeniy Ivanov 	/* Display menu */
157*0a6a1f1dSLionel Sambuc 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
158*0a6a1f1dSLionel Sambuc 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
15958a2b000SEvgeniy Ivanov 			printf("    %c. %s\n", choice + 'A',
160*0a6a1f1dSLionel Sambuc 			    bootcfg_info.desc[choice]);
16158a2b000SEvgeniy Ivanov 	} else {
16258a2b000SEvgeniy Ivanov 		/* Can't use %2d format string with libsa */
163*0a6a1f1dSLionel Sambuc 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
16458a2b000SEvgeniy Ivanov 			printf("    %s%d. %s\n",
16558a2b000SEvgeniy Ivanov 			    (choice < 9) ?  " " : "",
16658a2b000SEvgeniy Ivanov 			    choice + 1,
167*0a6a1f1dSLionel Sambuc 			    bootcfg_info.desc[choice]);
16858a2b000SEvgeniy Ivanov 	}
1699733fcdbSDavid van Moolenbroek }
17084d9c625SLionel Sambuc #endif /* defined(__minix) */
1719733fcdbSDavid van Moolenbroek 
172*0a6a1f1dSLionel Sambuc __dead void
doboottypemenu(void)1739733fcdbSDavid van Moolenbroek doboottypemenu(void)
1749733fcdbSDavid van Moolenbroek {
17584d9c625SLionel Sambuc #if !defined(__minix)
17684d9c625SLionel Sambuc 	int choice;
17784d9c625SLionel Sambuc 	char input[80];
17884d9c625SLionel Sambuc 
17984d9c625SLionel Sambuc 	printf("\n");
18084d9c625SLionel Sambuc 	/* Display menu */
181*0a6a1f1dSLionel Sambuc 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
182*0a6a1f1dSLionel Sambuc 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
18384d9c625SLionel Sambuc 			printf("    %c. %s\n", choice + 'A',
184*0a6a1f1dSLionel Sambuc 			    bootcfg_info.desc[choice]);
18584d9c625SLionel Sambuc 	} else {
18684d9c625SLionel Sambuc 		/* Can't use %2d format string with libsa */
187*0a6a1f1dSLionel Sambuc 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
18884d9c625SLionel Sambuc 			printf("    %s%d. %s\n",
18984d9c625SLionel Sambuc 			    (choice < 9) ?  " " : "",
19084d9c625SLionel Sambuc 			    choice + 1,
191*0a6a1f1dSLionel Sambuc 			    bootcfg_info.desc[choice]);
19284d9c625SLionel Sambuc 	}
19384d9c625SLionel Sambuc #else
1949733fcdbSDavid van Moolenbroek 	int choice, editing;
1959733fcdbSDavid van Moolenbroek 	char input[256], *ic, *oc;
19684d9c625SLionel Sambuc #endif /* !defined(__minix) */
19784d9c625SLionel Sambuc #if defined(__minix)
1989733fcdbSDavid van Moolenbroek 	showmenu();
19984d9c625SLionel Sambuc #endif /* defined(__minix) */
20058a2b000SEvgeniy Ivanov 	choice = -1;
20184d9c625SLionel Sambuc #if defined(__minix)
2029733fcdbSDavid van Moolenbroek 	editing = 0;
20384d9c625SLionel Sambuc #endif /* defined(__minix) */
20458a2b000SEvgeniy Ivanov 	for (;;) {
20558a2b000SEvgeniy Ivanov 		input[0] = '\0';
20658a2b000SEvgeniy Ivanov 
207*0a6a1f1dSLionel Sambuc 		if (bootcfg_info.timeout < 0) {
208*0a6a1f1dSLionel Sambuc 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
20984d9c625SLionel Sambuc #if !defined(__minix)
21084d9c625SLionel Sambuc 				printf("\nOption: [%c]:",
21184d9c625SLionel Sambuc #else
2129733fcdbSDavid van Moolenbroek 				printf("\nOption%s: [%c]:",
2139733fcdbSDavid van Moolenbroek 				    editing ? " (edit)" : "",
21484d9c625SLionel Sambuc #endif /* !defined(__minix) */
215*0a6a1f1dSLionel Sambuc 				    bootcfg_info.def + 'A');
21658a2b000SEvgeniy Ivanov 			else
21784d9c625SLionel Sambuc #if !defined(__minix)
21884d9c625SLionel Sambuc 				printf("\nOption: [%d]:",
21984d9c625SLionel Sambuc #else
2209733fcdbSDavid van Moolenbroek 				printf("\nOption%s: [%d]:",
2219733fcdbSDavid van Moolenbroek 				    editing ? " (edit)" : "",
22284d9c625SLionel Sambuc #endif /* !defined(__minix) */
223*0a6a1f1dSLionel Sambuc 				    bootcfg_info.def + 1);
22458a2b000SEvgeniy Ivanov 
22584d9c625SLionel Sambuc #if !defined(__minix)
22684d9c625SLionel Sambuc 			gets(input);
22784d9c625SLionel Sambuc #else
2289733fcdbSDavid van Moolenbroek 			editline(input, sizeof(input), NULL);
22984d9c625SLionel Sambuc #endif /* !defined(__minix) */
230*0a6a1f1dSLionel Sambuc 			choice = getchoicefrominput(input, bootcfg_info.def);
231*0a6a1f1dSLionel Sambuc 		} else if (bootcfg_info.timeout == 0)
232*0a6a1f1dSLionel Sambuc 			choice = bootcfg_info.def;
23358a2b000SEvgeniy Ivanov 		else  {
23458a2b000SEvgeniy Ivanov 			printf("\nChoose an option; RETURN for default; "
23558a2b000SEvgeniy Ivanov 			       "SPACE to stop countdown.\n");
236*0a6a1f1dSLionel Sambuc 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
23758a2b000SEvgeniy Ivanov 				printf("Option %c will be chosen in ",
238*0a6a1f1dSLionel Sambuc 				    bootcfg_info.def + 'A');
23958a2b000SEvgeniy Ivanov 			else
24058a2b000SEvgeniy Ivanov 				printf("Option %d will be chosen in ",
241*0a6a1f1dSLionel Sambuc 				    bootcfg_info.def + 1);
242*0a6a1f1dSLionel Sambuc 			input[0] = awaitkey(bootcfg_info.timeout, 1);
24358a2b000SEvgeniy Ivanov 			input[1] = '\0';
244*0a6a1f1dSLionel Sambuc 			choice = getchoicefrominput(input, bootcfg_info.def);
24558a2b000SEvgeniy Ivanov 			/* If invalid key pressed, drop to menu */
24658a2b000SEvgeniy Ivanov 			if (choice == -1)
247*0a6a1f1dSLionel Sambuc 				bootcfg_info.timeout = -1;
24858a2b000SEvgeniy Ivanov 		}
24958a2b000SEvgeniy Ivanov 		if (choice < 0)
25058a2b000SEvgeniy Ivanov 			continue;
25184d9c625SLionel Sambuc #if !defined(__minix)
252*0a6a1f1dSLionel Sambuc 		if (!strcmp(bootcfg_info.command[choice], "prompt") &&
25384d9c625SLionel Sambuc 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
25484d9c625SLionel Sambuc 		    check_password((char *)boot_params.bp_password))) {
25584d9c625SLionel Sambuc 			printf("type \"?\" or \"help\" for help.\n");
25684d9c625SLionel Sambuc 			bootmenu(); /* does not return */
25784d9c625SLionel Sambuc 		} else {
25884d9c625SLionel Sambuc 			docommandchoice(choice);
25984d9c625SLionel Sambuc 		}
26084d9c625SLionel Sambuc #else
261*0a6a1f1dSLionel Sambuc 		ic = bootcfg_info.command[choice];
2629733fcdbSDavid van Moolenbroek 		if (editing) {
2639733fcdbSDavid van Moolenbroek 			printf("> ");
2649733fcdbSDavid van Moolenbroek 			editline(input, sizeof(input), ic);
2659733fcdbSDavid van Moolenbroek 			ic = input;
2669733fcdbSDavid van Moolenbroek 		}
2679733fcdbSDavid van Moolenbroek 		if (!strcmp(ic, "edit") &&
26858a2b000SEvgeniy Ivanov 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
26958a2b000SEvgeniy Ivanov 		    check_password((char *)boot_params.bp_password))) {
2709733fcdbSDavid van Moolenbroek 			editing = 1;
271*0a6a1f1dSLionel Sambuc 			bootcfg_info.timeout = -1;
2729733fcdbSDavid van Moolenbroek 		} else if (!strcmp(ic, "prompt") &&
2739733fcdbSDavid van Moolenbroek 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
2749733fcdbSDavid van Moolenbroek 		    check_password((char *)boot_params.bp_password))) {
2759733fcdbSDavid van Moolenbroek 			printf("type \"?\" or \"help\" for help, "
2769733fcdbSDavid van Moolenbroek 			    "or \"menu\" to return to the menu.\n");
2779733fcdbSDavid van Moolenbroek 			prompt(1);
2789733fcdbSDavid van Moolenbroek 			showmenu();
2799733fcdbSDavid van Moolenbroek 			editing = 0;
280*0a6a1f1dSLionel Sambuc 			bootcfg_info.timeout = -1;
28158a2b000SEvgeniy Ivanov 		} else {
28258a2b000SEvgeniy Ivanov 			/* Split command string at ; into separate commands */
28358a2b000SEvgeniy Ivanov 			do {
2849733fcdbSDavid van Moolenbroek 				/*
2859733fcdbSDavid van Moolenbroek 				 * This must support inline editing, since ic
2869733fcdbSDavid van Moolenbroek 				 * may also point to input.
2879733fcdbSDavid van Moolenbroek 				 */
28858a2b000SEvgeniy Ivanov 				oc = input;
28958a2b000SEvgeniy Ivanov 				/* Look for ; separator */
29058a2b000SEvgeniy Ivanov 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
29158a2b000SEvgeniy Ivanov 					*oc++ = *ic;
2929733fcdbSDavid van Moolenbroek 				if (*ic == COMMAND_SEPARATOR)
2939733fcdbSDavid van Moolenbroek 					ic++;
2949733fcdbSDavid van Moolenbroek 				if (oc == input)
29558a2b000SEvgeniy Ivanov 					continue;
29658a2b000SEvgeniy Ivanov 				/* Strip out any trailing spaces */
29758a2b000SEvgeniy Ivanov 				oc--;
29858a2b000SEvgeniy Ivanov 				for (; *oc == ' ' && oc > input; oc--);
29958a2b000SEvgeniy Ivanov 				*++oc = '\0';
30058a2b000SEvgeniy Ivanov 				/* Stop silly command strings like ;;; */
30158a2b000SEvgeniy Ivanov 				if (*input != '\0')
30258a2b000SEvgeniy Ivanov 					docommand(input);
30358a2b000SEvgeniy Ivanov 				/* Skip leading spaces */
30458a2b000SEvgeniy Ivanov 				for (; *ic == ' '; ic++);
30558a2b000SEvgeniy Ivanov 			} while (*ic);
30658a2b000SEvgeniy Ivanov 		}
30784d9c625SLionel Sambuc #endif /* !defined(__minix) */
30858a2b000SEvgeniy Ivanov 
30958a2b000SEvgeniy Ivanov 	}
31058a2b000SEvgeniy Ivanov }
31158a2b000SEvgeniy Ivanov 
31258a2b000SEvgeniy Ivanov #endif	/* !SMALL */
313