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