boot.c revision 1.2.2.3 1 1.2.2.3 skrll /* $NetBSD: boot.c,v 1.2.2.3 2017/08/28 17:51:41 skrll Exp $ */
2 1.2.2.2 skrll
3 1.2.2.2 skrll /*-
4 1.2.2.2 skrll * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
5 1.2.2.2 skrll * All rights reserved.
6 1.2.2.2 skrll *
7 1.2.2.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 skrll * modification, are permitted provided that the following conditions
9 1.2.2.2 skrll * are met:
10 1.2.2.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 skrll * documentation and/or other materials provided with the distribution.
15 1.2.2.2 skrll *
16 1.2.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 skrll * SUCH DAMAGE.
27 1.2.2.2 skrll */
28 1.2.2.2 skrll
29 1.2.2.2 skrll #include "efiboot.h"
30 1.2.2.2 skrll
31 1.2.2.2 skrll #include <sys/bootblock.h>
32 1.2.2.2 skrll #include <sys/boot_flag.h>
33 1.2.2.3 skrll #include <machine/limits.h>
34 1.2.2.2 skrll
35 1.2.2.3 skrll #include "bootcfg.h"
36 1.2.2.3 skrll #include "bootmod.h"
37 1.2.2.3 skrll #include "bootmenu.h"
38 1.2.2.2 skrll #include "devopen.h"
39 1.2.2.2 skrll
40 1.2.2.2 skrll int errno;
41 1.2.2.2 skrll int boot_biosdev;
42 1.2.2.2 skrll daddr_t boot_biossector;
43 1.2.2.2 skrll
44 1.2.2.2 skrll extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
45 1.2.2.2 skrll
46 1.2.2.2 skrll extern struct x86_boot_params boot_params;
47 1.2.2.2 skrll extern char twiddle_toggle;
48 1.2.2.2 skrll
49 1.2.2.2 skrll static const char * const names[][2] = {
50 1.2.2.2 skrll { "netbsd", "netbsd.gz" },
51 1.2.2.2 skrll { "onetbsd", "onetbsd.gz" },
52 1.2.2.2 skrll { "netbsd.old", "netbsd.old.gz" },
53 1.2.2.2 skrll };
54 1.2.2.2 skrll
55 1.2.2.2 skrll #define NUMNAMES __arraycount(names)
56 1.2.2.2 skrll #define DEFFILENAME names[0][0]
57 1.2.2.2 skrll
58 1.2.2.2 skrll #define MAXDEVNAME 16
59 1.2.2.2 skrll
60 1.2.2.2 skrll void command_help(char *);
61 1.2.2.2 skrll void command_quit(char *);
62 1.2.2.2 skrll void command_boot(char *);
63 1.2.2.2 skrll void command_consdev(char *);
64 1.2.2.2 skrll void command_dev(char *);
65 1.2.2.2 skrll void command_devpath(char *);
66 1.2.2.2 skrll void command_efivar(char *);
67 1.2.2.2 skrll void command_gop(char *);
68 1.2.2.2 skrll #if LIBSA_ENABLE_LS_OP
69 1.2.2.2 skrll void command_ls(char *);
70 1.2.2.2 skrll #endif
71 1.2.2.2 skrll void command_memmap(char *);
72 1.2.2.2 skrll #ifndef SMALL
73 1.2.2.2 skrll void command_menu(char *);
74 1.2.2.2 skrll #endif
75 1.2.2.2 skrll void command_modules(char *);
76 1.2.2.2 skrll void command_multiboot(char *);
77 1.2.2.2 skrll void command_text(char *);
78 1.2.2.2 skrll void command_version(char *);
79 1.2.2.2 skrll
80 1.2.2.2 skrll const struct bootblk_command commands[] = {
81 1.2.2.2 skrll { "help", command_help },
82 1.2.2.2 skrll { "?", command_help },
83 1.2.2.2 skrll { "quit", command_quit },
84 1.2.2.2 skrll { "boot", command_boot },
85 1.2.2.2 skrll { "consdev", command_consdev },
86 1.2.2.2 skrll { "dev", command_dev },
87 1.2.2.2 skrll { "devpath", command_devpath },
88 1.2.2.2 skrll { "efivar", command_efivar },
89 1.2.2.2 skrll { "fs", fs_add },
90 1.2.2.2 skrll { "gop", command_gop },
91 1.2.2.2 skrll { "load", module_add },
92 1.2.2.2 skrll #if LIBSA_ENABLE_LS_OP
93 1.2.2.2 skrll { "ls", command_ls },
94 1.2.2.2 skrll #endif
95 1.2.2.2 skrll { "memmap", command_memmap },
96 1.2.2.2 skrll #ifndef SMALL
97 1.2.2.2 skrll { "menu", command_menu },
98 1.2.2.2 skrll #endif
99 1.2.2.2 skrll { "modules", command_modules },
100 1.2.2.2 skrll { "multiboot", command_multiboot },
101 1.2.2.2 skrll { "rndseed", rnd_add },
102 1.2.2.2 skrll { "splash", splash_add },
103 1.2.2.2 skrll { "text", command_text },
104 1.2.2.2 skrll { "userconf", userconf_add },
105 1.2.2.2 skrll { "version", command_version },
106 1.2.2.2 skrll { NULL, NULL },
107 1.2.2.2 skrll };
108 1.2.2.2 skrll
109 1.2.2.2 skrll static char *default_devname;
110 1.2.2.2 skrll static int default_unit, default_partition;
111 1.2.2.2 skrll static const char *default_filename;
112 1.2.2.2 skrll
113 1.2.2.2 skrll static char *sprint_bootsel(const char *);
114 1.2.2.2 skrll static void bootit(const char *, int);
115 1.2.2.2 skrll
116 1.2.2.2 skrll int
117 1.2.2.2 skrll parsebootfile(const char *fname, char **fsname, char **devname, int *unit,
118 1.2.2.2 skrll int *partition, const char **file)
119 1.2.2.2 skrll {
120 1.2.2.2 skrll const char *col;
121 1.2.2.2 skrll
122 1.2.2.2 skrll *fsname = "ufs";
123 1.2.2.2 skrll *devname = default_devname;
124 1.2.2.2 skrll *unit = default_unit;
125 1.2.2.2 skrll *partition = default_partition;
126 1.2.2.2 skrll *file = default_filename;
127 1.2.2.2 skrll
128 1.2.2.2 skrll if (fname == NULL)
129 1.2.2.2 skrll return 0;
130 1.2.2.2 skrll
131 1.2.2.2 skrll if ((col = strchr(fname, ':')) != NULL) { /* device given */
132 1.2.2.2 skrll static char savedevname[MAXDEVNAME+1];
133 1.2.2.2 skrll int devlen;
134 1.2.2.2 skrll int u = 0, p = 0;
135 1.2.2.2 skrll int i = 0;
136 1.2.2.2 skrll
137 1.2.2.2 skrll devlen = col - fname;
138 1.2.2.2 skrll if (devlen > MAXDEVNAME)
139 1.2.2.2 skrll return EINVAL;
140 1.2.2.2 skrll
141 1.2.2.2 skrll #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
142 1.2.2.2 skrll if (!isvalidname(fname[i]))
143 1.2.2.2 skrll return EINVAL;
144 1.2.2.2 skrll do {
145 1.2.2.2 skrll savedevname[i] = fname[i];
146 1.2.2.2 skrll i++;
147 1.2.2.2 skrll } while (isvalidname(fname[i]));
148 1.2.2.2 skrll savedevname[i] = '\0';
149 1.2.2.2 skrll
150 1.2.2.2 skrll #define isnum(c) ((c) >= '0' && (c) <= '9')
151 1.2.2.2 skrll if (i < devlen) {
152 1.2.2.2 skrll if (!isnum(fname[i]))
153 1.2.2.2 skrll return EUNIT;
154 1.2.2.2 skrll do {
155 1.2.2.2 skrll u *= 10;
156 1.2.2.2 skrll u += fname[i++] - '0';
157 1.2.2.2 skrll } while (isnum(fname[i]));
158 1.2.2.2 skrll }
159 1.2.2.2 skrll
160 1.2.2.2 skrll #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
161 1.2.2.2 skrll if (i < devlen) {
162 1.2.2.2 skrll if (!isvalidpart(fname[i]))
163 1.2.2.2 skrll return EPART;
164 1.2.2.2 skrll p = fname[i++] - 'a';
165 1.2.2.2 skrll }
166 1.2.2.2 skrll
167 1.2.2.2 skrll if (i != devlen)
168 1.2.2.2 skrll return ENXIO;
169 1.2.2.2 skrll
170 1.2.2.2 skrll *devname = savedevname;
171 1.2.2.2 skrll *unit = u;
172 1.2.2.2 skrll *partition = p;
173 1.2.2.2 skrll fname = col + 1;
174 1.2.2.2 skrll }
175 1.2.2.2 skrll
176 1.2.2.2 skrll if (*fname)
177 1.2.2.2 skrll *file = fname;
178 1.2.2.2 skrll
179 1.2.2.2 skrll return 0;
180 1.2.2.2 skrll }
181 1.2.2.2 skrll
182 1.2.2.2 skrll static char *
183 1.2.2.2 skrll sprint_bootsel(const char *filename)
184 1.2.2.2 skrll {
185 1.2.2.2 skrll char *fsname, *devname;
186 1.2.2.2 skrll int unit, partition;
187 1.2.2.2 skrll const char *file;
188 1.2.2.2 skrll static char buf[80];
189 1.2.2.2 skrll
190 1.2.2.2 skrll if (parsebootfile(filename, &fsname, &devname, &unit,
191 1.2.2.2 skrll &partition, &file) == 0) {
192 1.2.2.2 skrll snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit,
193 1.2.2.2 skrll 'a' + partition, file);
194 1.2.2.2 skrll return buf;
195 1.2.2.2 skrll }
196 1.2.2.2 skrll return "(invalid)";
197 1.2.2.2 skrll }
198 1.2.2.2 skrll
199 1.2.2.2 skrll void
200 1.2.2.2 skrll clearit(void)
201 1.2.2.2 skrll {
202 1.2.2.2 skrll
203 1.2.2.2 skrll if (bootcfg_info.clear)
204 1.2.2.2 skrll clear_pc_screen();
205 1.2.2.2 skrll }
206 1.2.2.2 skrll
207 1.2.2.2 skrll static void
208 1.2.2.2 skrll bootit(const char *filename, int howto)
209 1.2.2.2 skrll {
210 1.2.2.3 skrll EFI_STATUS status;
211 1.2.2.3 skrll EFI_PHYSICAL_ADDRESS bouncebuf;
212 1.2.2.3 skrll UINTN npages;
213 1.2.2.3 skrll u_long allocsz;
214 1.2.2.2 skrll
215 1.2.2.2 skrll if (howto & AB_VERBOSE)
216 1.2.2.2 skrll printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename),
217 1.2.2.2 skrll howto);
218 1.2.2.2 skrll
219 1.2.2.3 skrll if (count_netbsd(filename, &allocsz) < 0) {
220 1.2.2.3 skrll printf("boot: %s: %s\n", sprint_bootsel(filename),
221 1.2.2.3 skrll strerror(errno));
222 1.2.2.3 skrll return;
223 1.2.2.3 skrll }
224 1.2.2.3 skrll
225 1.2.2.3 skrll bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
226 1.2.2.3 skrll npages = EFI_SIZE_TO_PAGES(allocsz);
227 1.2.2.3 skrll status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
228 1.2.2.3 skrll EfiLoaderData, npages, &bouncebuf);
229 1.2.2.3 skrll if (EFI_ERROR(status)) {
230 1.2.2.3 skrll printf("boot: %s: %s\n", sprint_bootsel(filename),
231 1.2.2.3 skrll strerror(ENOMEM));
232 1.2.2.3 skrll return;
233 1.2.2.3 skrll }
234 1.2.2.3 skrll
235 1.2.2.3 skrll efi_loadaddr = bouncebuf;
236 1.2.2.3 skrll if (exec_netbsd(filename, bouncebuf, howto, 0, efi_cleanup) < 0)
237 1.2.2.2 skrll printf("boot: %s: %s\n", sprint_bootsel(filename),
238 1.2.2.2 skrll strerror(errno));
239 1.2.2.2 skrll else
240 1.2.2.2 skrll printf("boot returned\n");
241 1.2.2.3 skrll
242 1.2.2.3 skrll (void) uefi_call_wrapper(BS->FreePages, 2, bouncebuf, npages);
243 1.2.2.3 skrll efi_loadaddr = 0;
244 1.2.2.2 skrll }
245 1.2.2.2 skrll
246 1.2.2.2 skrll void
247 1.2.2.2 skrll print_banner(void)
248 1.2.2.2 skrll {
249 1.2.2.2 skrll int n;
250 1.2.2.2 skrll
251 1.2.2.2 skrll clearit();
252 1.2.2.2 skrll if (bootcfg_info.banner[0]) {
253 1.2.2.2 skrll for (n = 0; n < BOOTCFG_MAXBANNER && bootcfg_info.banner[n];
254 1.2.2.2 skrll n++)
255 1.2.2.2 skrll printf("%s\n", bootcfg_info.banner[n]);
256 1.2.2.2 skrll } else
257 1.2.2.2 skrll command_version("short");
258 1.2.2.2 skrll }
259 1.2.2.2 skrll
260 1.2.2.2 skrll void
261 1.2.2.2 skrll boot(void)
262 1.2.2.2 skrll {
263 1.2.2.2 skrll int currname;
264 1.2.2.2 skrll int c;
265 1.2.2.2 skrll
266 1.2.2.2 skrll boot_modules_enabled = !(boot_params.bp_flags & X86_BP_FLAGS_NOMODULES);
267 1.2.2.2 skrll
268 1.2.2.2 skrll /* try to set default device to what BIOS tells us */
269 1.2.2.2 skrll bios2dev(boot_biosdev, boot_biossector, &default_devname, &default_unit,
270 1.2.2.2 skrll &default_partition);
271 1.2.2.2 skrll
272 1.2.2.2 skrll /* if the user types "boot" without filename */
273 1.2.2.2 skrll default_filename = DEFFILENAME;
274 1.2.2.2 skrll
275 1.2.2.2 skrll if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
276 1.2.2.2 skrll parsebootconf(BOOTCFG_FILENAME);
277 1.2.2.2 skrll } else {
278 1.2.2.2 skrll bootcfg_info.timeout = boot_params.bp_timeout;
279 1.2.2.2 skrll }
280 1.2.2.2 skrll
281 1.2.2.2 skrll /*
282 1.2.2.2 skrll * If console set in boot.cfg, switch to it.
283 1.2.2.2 skrll * This will print the banner, so we don't need to explicitly do it
284 1.2.2.2 skrll */
285 1.2.2.2 skrll if (bootcfg_info.consdev)
286 1.2.2.2 skrll command_consdev(bootcfg_info.consdev);
287 1.2.2.2 skrll else
288 1.2.2.2 skrll print_banner();
289 1.2.2.2 skrll
290 1.2.2.2 skrll /* Display the menu, if applicable */
291 1.2.2.2 skrll twiddle_toggle = 0;
292 1.2.2.2 skrll if (bootcfg_info.nummenu > 0) {
293 1.2.2.2 skrll /* Does not return */
294 1.2.2.2 skrll doboottypemenu();
295 1.2.2.2 skrll }
296 1.2.2.2 skrll
297 1.2.2.2 skrll printf("Press return to boot now, any other key for boot menu\n");
298 1.2.2.2 skrll for (currname = 0; currname < NUMNAMES; currname++) {
299 1.2.2.2 skrll printf("booting %s - starting in ",
300 1.2.2.2 skrll sprint_bootsel(names[currname][0]));
301 1.2.2.2 skrll
302 1.2.2.2 skrll c = awaitkey((bootcfg_info.timeout < 0) ? 0
303 1.2.2.2 skrll : bootcfg_info.timeout, 1);
304 1.2.2.2 skrll if ((c != '\r') && (c != '\n') && (c != '\0')) {
305 1.2.2.2 skrll if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
306 1.2.2.2 skrll /* do NOT ask for password */
307 1.2.2.2 skrll bootmenu(); /* does not return */
308 1.2.2.2 skrll } else {
309 1.2.2.2 skrll /* DO ask for password */
310 1.2.2.2 skrll if (check_password((char *)boot_params.bp_password)) {
311 1.2.2.2 skrll /* password ok */
312 1.2.2.2 skrll printf("type \"?\" or \"help\" for help.\n");
313 1.2.2.2 skrll bootmenu(); /* does not return */
314 1.2.2.2 skrll } else {
315 1.2.2.2 skrll /* bad password */
316 1.2.2.2 skrll printf("Wrong password.\n");
317 1.2.2.2 skrll currname = 0;
318 1.2.2.2 skrll continue;
319 1.2.2.2 skrll }
320 1.2.2.2 skrll }
321 1.2.2.2 skrll }
322 1.2.2.2 skrll
323 1.2.2.2 skrll /*
324 1.2.2.2 skrll * try pairs of names[] entries, foo and foo.gz
325 1.2.2.2 skrll */
326 1.2.2.2 skrll /* don't print "booting..." again */
327 1.2.2.2 skrll bootit(names[currname][0], 0);
328 1.2.2.2 skrll /* since it failed, try compressed bootfile. */
329 1.2.2.2 skrll bootit(names[currname][1], AB_VERBOSE);
330 1.2.2.2 skrll }
331 1.2.2.2 skrll
332 1.2.2.2 skrll bootmenu(); /* does not return */
333 1.2.2.2 skrll }
334 1.2.2.2 skrll
335 1.2.2.2 skrll /* ARGSUSED */
336 1.2.2.2 skrll void
337 1.2.2.2 skrll command_help(char *arg)
338 1.2.2.2 skrll {
339 1.2.2.2 skrll
340 1.2.2.2 skrll printf("commands are:\n"
341 1.2.2.2 skrll "boot [xdNx:][filename] [-12acdqsvxz]\n"
342 1.2.2.2 skrll " (ex. \"hd0a:netbsd.old -s\"\n"
343 1.2.2.2 skrll "dev [xd[N[x]]:]\n"
344 1.2.2.3 skrll "consdev {pc|com[0123][,{speed}]|com,{ioport}[,{speed}]}\n"
345 1.2.2.2 skrll "devpath\n"
346 1.2.2.2 skrll "efivar\n"
347 1.2.2.2 skrll "gop [{modenum|list}]\n"
348 1.2.2.2 skrll "load {path_to_module}\n"
349 1.2.2.2 skrll #if LIBSA_ENABLE_LS_OP
350 1.2.2.2 skrll "ls [path]\n"
351 1.2.2.2 skrll #endif
352 1.2.2.2 skrll "memmap [{sorted|unsorted}]\n"
353 1.2.2.2 skrll #ifndef SMALL
354 1.2.2.2 skrll "menu (reenters boot menu, if defined in boot.cfg)\n"
355 1.2.2.2 skrll #endif
356 1.2.2.2 skrll "modules {on|off|enabled|disabled}\n"
357 1.2.2.2 skrll "multiboot [xdNx:][filename] [<args>]\n"
358 1.2.2.2 skrll "rndseed {path_to_rndseed_file}\n"
359 1.2.2.2 skrll "splash {path_to_image_file}\n"
360 1.2.2.2 skrll "text [{modenum|list}]\n"
361 1.2.2.2 skrll "userconf {command}\n"
362 1.2.2.2 skrll "version\n"
363 1.2.2.2 skrll "help|?\n"
364 1.2.2.2 skrll "quit\n");
365 1.2.2.2 skrll }
366 1.2.2.2 skrll
367 1.2.2.2 skrll #if LIBSA_ENABLE_LS_OP
368 1.2.2.2 skrll void
369 1.2.2.2 skrll command_ls(char *arg)
370 1.2.2.2 skrll {
371 1.2.2.2 skrll const char *save = default_filename;
372 1.2.2.2 skrll
373 1.2.2.2 skrll default_filename = "/";
374 1.2.2.2 skrll ls(arg);
375 1.2.2.2 skrll default_filename = save;
376 1.2.2.2 skrll }
377 1.2.2.2 skrll #endif
378 1.2.2.2 skrll
379 1.2.2.2 skrll /* ARGSUSED */
380 1.2.2.2 skrll void
381 1.2.2.2 skrll command_quit(char *arg)
382 1.2.2.2 skrll {
383 1.2.2.2 skrll
384 1.2.2.2 skrll printf("Exiting...\n");
385 1.2.2.2 skrll delay(1 * 1000 * 1000);
386 1.2.2.2 skrll reboot();
387 1.2.2.2 skrll /* Note: we shouldn't get to this point! */
388 1.2.2.2 skrll panic("Could not reboot!");
389 1.2.2.2 skrll }
390 1.2.2.2 skrll
391 1.2.2.2 skrll void
392 1.2.2.2 skrll command_boot(char *arg)
393 1.2.2.2 skrll {
394 1.2.2.2 skrll char *filename;
395 1.2.2.2 skrll int howto;
396 1.2.2.2 skrll
397 1.2.2.2 skrll if (!parseboot(arg, &filename, &howto))
398 1.2.2.2 skrll return;
399 1.2.2.2 skrll
400 1.2.2.2 skrll if (filename != NULL) {
401 1.2.2.2 skrll bootit(filename, howto);
402 1.2.2.2 skrll } else {
403 1.2.2.2 skrll int i;
404 1.2.2.2 skrll
405 1.2.2.2 skrll if (howto == 0)
406 1.2.2.2 skrll bootdefault();
407 1.2.2.2 skrll for (i = 0; i < NUMNAMES; i++) {
408 1.2.2.2 skrll bootit(names[i][0], howto);
409 1.2.2.2 skrll bootit(names[i][1], howto);
410 1.2.2.2 skrll }
411 1.2.2.2 skrll }
412 1.2.2.2 skrll }
413 1.2.2.2 skrll
414 1.2.2.2 skrll void
415 1.2.2.2 skrll command_dev(char *arg)
416 1.2.2.2 skrll {
417 1.2.2.2 skrll static char savedevname[MAXDEVNAME + 1];
418 1.2.2.2 skrll char *fsname, *devname;
419 1.2.2.2 skrll const char *file; /* dummy */
420 1.2.2.2 skrll
421 1.2.2.2 skrll if (*arg == '\0') {
422 1.2.2.2 skrll biosdisk_probe();
423 1.2.2.2 skrll printf("default %s%d%c\n", default_devname, default_unit,
424 1.2.2.2 skrll 'a' + default_partition);
425 1.2.2.2 skrll return;
426 1.2.2.2 skrll }
427 1.2.2.2 skrll
428 1.2.2.2 skrll if (strchr(arg, ':') == NULL ||
429 1.2.2.2 skrll parsebootfile(arg, &fsname, &devname, &default_unit,
430 1.2.2.2 skrll &default_partition, &file)) {
431 1.2.2.2 skrll command_help(NULL);
432 1.2.2.2 skrll return;
433 1.2.2.2 skrll }
434 1.2.2.2 skrll
435 1.2.2.2 skrll /* put to own static storage */
436 1.2.2.2 skrll strncpy(savedevname, devname, MAXDEVNAME + 1);
437 1.2.2.2 skrll default_devname = savedevname;
438 1.2.2.2 skrll }
439 1.2.2.2 skrll
440 1.2.2.3 skrll static const struct cons_devs {
441 1.2.2.3 skrll const char *name;
442 1.2.2.3 skrll u_int tag;
443 1.2.2.3 skrll int ioport;
444 1.2.2.3 skrll } cons_devs[] = {
445 1.2.2.3 skrll { "pc", CONSDEV_PC, 0 },
446 1.2.2.3 skrll { "com0", CONSDEV_COM0, 0 },
447 1.2.2.3 skrll { "com1", CONSDEV_COM1, 0 },
448 1.2.2.3 skrll { "com2", CONSDEV_COM2, 0 },
449 1.2.2.3 skrll { "com3", CONSDEV_COM3, 0 },
450 1.2.2.3 skrll { "com0kbd", CONSDEV_COM0KBD, 0 },
451 1.2.2.3 skrll { "com1kbd", CONSDEV_COM1KBD, 0 },
452 1.2.2.3 skrll { "com2kbd", CONSDEV_COM2KBD, 0 },
453 1.2.2.3 skrll { "com3kbd", CONSDEV_COM3KBD, 0 },
454 1.2.2.3 skrll { "com", CONSDEV_COM0, -1 },
455 1.2.2.3 skrll { "auto", CONSDEV_AUTO, 0 },
456 1.2.2.3 skrll { NULL, 0 }
457 1.2.2.3 skrll };
458 1.2.2.3 skrll
459 1.2.2.2 skrll void
460 1.2.2.2 skrll command_consdev(char *arg)
461 1.2.2.2 skrll {
462 1.2.2.3 skrll const struct cons_devs *cdp;
463 1.2.2.3 skrll char *sep, *sep2 = NULL;
464 1.2.2.3 skrll int ioport, speed = 0;
465 1.2.2.3 skrll
466 1.2.2.3 skrll sep = strchr(arg, ',');
467 1.2.2.3 skrll if (sep != NULL) {
468 1.2.2.3 skrll *sep++ = '\0';
469 1.2.2.3 skrll sep2 = strchr(sep, ',');
470 1.2.2.3 skrll if (sep != NULL)
471 1.2.2.3 skrll *sep2++ = '\0';
472 1.2.2.3 skrll }
473 1.2.2.3 skrll
474 1.2.2.3 skrll for (cdp = cons_devs; cdp->name; cdp++) {
475 1.2.2.3 skrll if (strcmp(arg, cdp->name) == 0) {
476 1.2.2.3 skrll ioport = cdp->ioport;
477 1.2.2.3 skrll if (cdp->tag == CONSDEV_PC || cdp->tag == CONSDEV_AUTO) {
478 1.2.2.3 skrll if (sep != NULL || sep2 != NULL)
479 1.2.2.3 skrll goto error;
480 1.2.2.3 skrll } else {
481 1.2.2.3 skrll /* com? */
482 1.2.2.3 skrll if (ioport == -1) {
483 1.2.2.3 skrll if (sep != NULL) {
484 1.2.2.3 skrll u_long t = strtoul(sep, NULL, 0);
485 1.2.2.3 skrll if (t > INT_MAX)
486 1.2.2.3 skrll goto error;
487 1.2.2.3 skrll ioport = (int)t;
488 1.2.2.3 skrll }
489 1.2.2.3 skrll if (sep2 != NULL) {
490 1.2.2.3 skrll speed = atoi(sep2);
491 1.2.2.3 skrll if (speed < 0)
492 1.2.2.3 skrll goto error;
493 1.2.2.3 skrll }
494 1.2.2.3 skrll } else {
495 1.2.2.3 skrll if (sep != NULL) {
496 1.2.2.3 skrll speed = atoi(sep);
497 1.2.2.3 skrll if (speed < 0)
498 1.2.2.3 skrll goto error;
499 1.2.2.3 skrll }
500 1.2.2.3 skrll if (sep2 != NULL)
501 1.2.2.3 skrll goto error;
502 1.2.2.3 skrll }
503 1.2.2.3 skrll }
504 1.2.2.3 skrll consinit(cdp->tag, ioport, speed);
505 1.2.2.3 skrll print_banner();
506 1.2.2.3 skrll return;
507 1.2.2.3 skrll }
508 1.2.2.3 skrll }
509 1.2.2.3 skrll error:
510 1.2.2.3 skrll printf("invalid console device.\n");
511 1.2.2.2 skrll }
512 1.2.2.2 skrll
513 1.2.2.2 skrll #ifndef SMALL
514 1.2.2.2 skrll /* ARGSUSED */
515 1.2.2.2 skrll void
516 1.2.2.2 skrll command_menu(char *arg)
517 1.2.2.2 skrll {
518 1.2.2.2 skrll
519 1.2.2.2 skrll if (bootcfg_info.nummenu > 0) {
520 1.2.2.2 skrll /* Does not return */
521 1.2.2.2 skrll doboottypemenu();
522 1.2.2.2 skrll } else
523 1.2.2.2 skrll printf("No menu defined in boot.cfg\n");
524 1.2.2.2 skrll }
525 1.2.2.2 skrll #endif /* !SMALL */
526 1.2.2.2 skrll
527 1.2.2.2 skrll void
528 1.2.2.2 skrll command_modules(char *arg)
529 1.2.2.2 skrll {
530 1.2.2.2 skrll
531 1.2.2.2 skrll if (strcmp(arg, "enabled") == 0 ||
532 1.2.2.2 skrll strcmp(arg, "on") == 0)
533 1.2.2.2 skrll boot_modules_enabled = true;
534 1.2.2.2 skrll else if (strcmp(arg, "disabled") == 0 ||
535 1.2.2.2 skrll strcmp(arg, "off") == 0)
536 1.2.2.2 skrll boot_modules_enabled = false;
537 1.2.2.2 skrll else
538 1.2.2.2 skrll printf("invalid flag, must be 'enabled' or 'disabled'.\n");
539 1.2.2.2 skrll }
540 1.2.2.2 skrll
541 1.2.2.2 skrll void
542 1.2.2.2 skrll command_multiboot(char *arg)
543 1.2.2.2 skrll {
544 1.2.2.2 skrll char *filename;
545 1.2.2.2 skrll
546 1.2.2.2 skrll filename = arg;
547 1.2.2.2 skrll if (exec_multiboot(filename, gettrailer(arg)) < 0)
548 1.2.2.2 skrll printf("multiboot: %s: %s\n", sprint_bootsel(filename),
549 1.2.2.2 skrll strerror(errno));
550 1.2.2.2 skrll else
551 1.2.2.2 skrll printf("boot returned\n");
552 1.2.2.2 skrll }
553 1.2.2.2 skrll
554 1.2.2.2 skrll void
555 1.2.2.2 skrll command_version(char *arg)
556 1.2.2.2 skrll {
557 1.2.2.2 skrll
558 1.2.2.2 skrll if (strcmp(arg, "full") == 0) {
559 1.2.2.2 skrll printf("ImageBase: 0x%" PRIxPTR "\n",
560 1.2.2.2 skrll (uintptr_t)efi_li->ImageBase);
561 1.2.2.2 skrll printf("Stack: 0x%" PRIxPTR "\n", efi_main_sp);
562 1.2.2.2 skrll printf("EFI version: %d.%02d\n",
563 1.2.2.2 skrll ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
564 1.2.2.2 skrll Print(L"EFI Firmware: %s (rev %d.%02d)\n", ST->FirmwareVendor,
565 1.2.2.2 skrll ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
566 1.2.2.2 skrll }
567 1.2.2.2 skrll
568 1.2.2.2 skrll printf("\n"
569 1.2.2.2 skrll ">> %s, Revision %s (from NetBSD %s)\n"
570 1.2.2.2 skrll ">> Memory: %d/%d k\n",
571 1.2.2.2 skrll bootprog_name, bootprog_rev, bootprog_kernrev,
572 1.2.2.2 skrll getbasemem(), getextmem());
573 1.2.2.2 skrll }
574 1.2.2.2 skrll
575 1.2.2.2 skrll void
576 1.2.2.2 skrll command_memmap(char *arg)
577 1.2.2.2 skrll {
578 1.2.2.2 skrll bool sorted = true;
579 1.2.2.2 skrll
580 1.2.2.2 skrll if (*arg == '\0' || strcmp(arg, "sorted") == 0)
581 1.2.2.2 skrll /* Already sorted is true. */;
582 1.2.2.2 skrll else if (strcmp(arg, "unsorted") == 0)
583 1.2.2.2 skrll sorted = false;
584 1.2.2.2 skrll else {
585 1.2.2.2 skrll printf("invalid flag, "
586 1.2.2.2 skrll "must be 'sorted' or 'unsorted'.\n");
587 1.2.2.2 skrll return;
588 1.2.2.2 skrll }
589 1.2.2.2 skrll
590 1.2.2.2 skrll efi_memory_show_map(sorted);
591 1.2.2.2 skrll }
592 1.2.2.2 skrll
593 1.2.2.2 skrll void
594 1.2.2.2 skrll command_devpath(char *arg)
595 1.2.2.2 skrll {
596 1.2.2.2 skrll EFI_STATUS status;
597 1.2.2.2 skrll UINTN i, nhandles;
598 1.2.2.2 skrll EFI_HANDLE *handles;
599 1.2.2.2 skrll EFI_DEVICE_PATH *dp0, *dp;
600 1.2.2.2 skrll CHAR16 *path;
601 1.2.2.2 skrll UINTN cols, rows, row = 0;
602 1.2.2.2 skrll
603 1.2.2.2 skrll status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
604 1.2.2.2 skrll ST->ConOut->Mode->Mode, &cols, &rows);
605 1.2.2.2 skrll if (EFI_ERROR(status) || rows <= 2)
606 1.2.2.2 skrll rows = 0;
607 1.2.2.2 skrll else
608 1.2.2.2 skrll rows -= 2;
609 1.2.2.2 skrll
610 1.2.2.2 skrll /*
611 1.2.2.2 skrll * all devices.
612 1.2.2.2 skrll */
613 1.2.2.2 skrll status = LibLocateHandle(ByProtocol, &DevicePathProtocol, NULL,
614 1.2.2.2 skrll &nhandles, &handles);
615 1.2.2.2 skrll if (EFI_ERROR(status))
616 1.2.2.2 skrll return;
617 1.2.2.2 skrll
618 1.2.2.2 skrll for (i = 0; i < nhandles; i++) {
619 1.2.2.2 skrll status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
620 1.2.2.2 skrll &DevicePathProtocol, (void **)&dp0);
621 1.2.2.2 skrll if (EFI_ERROR(status))
622 1.2.2.2 skrll break;
623 1.2.2.2 skrll
624 1.2.2.2 skrll Print(L"DevicePathType %d\n", DevicePathType(dp0));
625 1.2.2.2 skrll for (dp = dp0;
626 1.2.2.2 skrll !IsDevicePathEnd(dp);
627 1.2.2.2 skrll dp = NextDevicePathNode(dp)) {
628 1.2.2.2 skrll path = DevicePathToStr(dp);
629 1.2.2.2 skrll Print(L"%d:%d:%s\n", DevicePathType(dp), DevicePathSubType(dp), path);
630 1.2.2.2 skrll FreePool(path);
631 1.2.2.2 skrll
632 1.2.2.2 skrll if (++row >= rows) {
633 1.2.2.2 skrll row = 0;
634 1.2.2.2 skrll Print(L"Press Any Key to continue :");
635 1.2.2.2 skrll (void) awaitkey(-1, 0);
636 1.2.2.2 skrll Print(L"\n");
637 1.2.2.2 skrll }
638 1.2.2.2 skrll }
639 1.2.2.2 skrll }
640 1.2.2.2 skrll }
641 1.2.2.2 skrll
642 1.2.2.2 skrll void
643 1.2.2.2 skrll command_efivar(char *arg)
644 1.2.2.2 skrll {
645 1.2.2.2 skrll static const CHAR16 header[] =
646 1.2.2.2 skrll L"GUID Variable Name Value\n"
647 1.2.2.2 skrll L"=================================== ==================== ========\n";
648 1.2.2.2 skrll EFI_STATUS status;
649 1.2.2.3 skrll UINTN sz = 64, osz;
650 1.2.2.2 skrll CHAR16 *name = NULL, *tmp, *val;
651 1.2.2.2 skrll EFI_GUID vendor;
652 1.2.2.2 skrll UINTN cols, rows, row = 0;
653 1.2.2.2 skrll
654 1.2.2.2 skrll status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
655 1.2.2.2 skrll ST->ConOut->Mode->Mode, &cols, &rows);
656 1.2.2.2 skrll if (EFI_ERROR(status) || rows <= 2)
657 1.2.2.2 skrll rows = 0;
658 1.2.2.2 skrll else
659 1.2.2.2 skrll rows -= 2;
660 1.2.2.2 skrll
661 1.2.2.2 skrll name = AllocatePool(sz);
662 1.2.2.2 skrll if (name == NULL) {
663 1.2.2.2 skrll Print(L"memory allocation failed: %ld bytes\n",
664 1.2.2.2 skrll (UINT64)sz);
665 1.2.2.2 skrll return;
666 1.2.2.2 skrll }
667 1.2.2.2 skrll
668 1.2.2.3 skrll SetMem(name, sz, 0);
669 1.2.2.2 skrll vendor = NullGuid;
670 1.2.2.2 skrll
671 1.2.2.2 skrll Print(L"%s", header);
672 1.2.2.2 skrll for (;;) {
673 1.2.2.3 skrll osz = sz;
674 1.2.2.2 skrll status = uefi_call_wrapper(RT->GetNextVariableName, 3,
675 1.2.2.2 skrll &sz, name, &vendor);
676 1.2.2.2 skrll if (EFI_ERROR(status)) {
677 1.2.2.2 skrll if (status == EFI_NOT_FOUND)
678 1.2.2.2 skrll break;
679 1.2.2.2 skrll if (status != EFI_BUFFER_TOO_SMALL) {
680 1.2.2.2 skrll Print(L"GetNextVariableName failed: %r\n",
681 1.2.2.2 skrll status);
682 1.2.2.2 skrll break;
683 1.2.2.2 skrll }
684 1.2.2.2 skrll
685 1.2.2.2 skrll tmp = AllocatePool(sz);
686 1.2.2.2 skrll if (tmp == NULL) {
687 1.2.2.2 skrll Print(L"memory allocation failed: %ld bytes\n",
688 1.2.2.2 skrll (UINT64)sz);
689 1.2.2.2 skrll break;
690 1.2.2.2 skrll }
691 1.2.2.3 skrll SetMem(tmp, sz, 0);
692 1.2.2.3 skrll CopyMem(tmp, name, osz);
693 1.2.2.2 skrll FreePool(name);
694 1.2.2.2 skrll name = tmp;
695 1.2.2.3 skrll continue;
696 1.2.2.2 skrll }
697 1.2.2.2 skrll
698 1.2.2.2 skrll val = LibGetVariable(name, &vendor);
699 1.2.2.2 skrll Print(L"%.-35g %.-20s %s\n", &vendor, name,
700 1.2.2.2 skrll val ? val : L"(null)");
701 1.2.2.2 skrll FreePool(val);
702 1.2.2.2 skrll
703 1.2.2.2 skrll if (++row >= rows) {
704 1.2.2.2 skrll row = 0;
705 1.2.2.2 skrll Print(L"Press Any Key to continue :");
706 1.2.2.2 skrll (void) awaitkey(-1, 0);
707 1.2.2.2 skrll Print(L"\n");
708 1.2.2.2 skrll }
709 1.2.2.2 skrll }
710 1.2.2.2 skrll
711 1.2.2.2 skrll FreePool(name);
712 1.2.2.2 skrll }
713