boot.c revision 1.18 1 1.18 thorpej /* $NetBSD: boot.c,v 1.18 2019/04/21 22:30:41 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
5 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 1.1 jmcneill * All rights reserved.
7 1.1 jmcneill *
8 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
9 1.1 jmcneill * modification, are permitted provided that the following conditions
10 1.1 jmcneill * are met:
11 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
12 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
13 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
15 1.1 jmcneill * documentation and/or other materials provided with the distribution.
16 1.1 jmcneill *
17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 jmcneill * SUCH DAMAGE.
28 1.1 jmcneill */
29 1.1 jmcneill
30 1.1 jmcneill #include "efiboot.h"
31 1.3 jmcneill #include "efiblock.h"
32 1.5 jmcneill #include "efifdt.h"
33 1.10 jmcneill #include "efiacpi.h"
34 1.8 jmcneill #include "efienv.h"
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/bootblock.h>
37 1.1 jmcneill #include <sys/boot_flag.h>
38 1.1 jmcneill #include <machine/limits.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <loadfile.h>
41 1.1 jmcneill
42 1.1 jmcneill extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
43 1.1 jmcneill
44 1.1 jmcneill extern char twiddle_toggle;
45 1.1 jmcneill
46 1.11 mrg static const char * const names[] = {
47 1.11 mrg "netbsd", "netbsd.gz",
48 1.11 mrg "onetbsd", "onetbsd.gz",
49 1.11 mrg "netbsd.old", "netbsd.old.gz",
50 1.1 jmcneill };
51 1.1 jmcneill
52 1.1 jmcneill #define NUMNAMES __arraycount(names)
53 1.1 jmcneill
54 1.13 jmcneill static const char *efi_memory_type[] = {
55 1.13 jmcneill [EfiReservedMemoryType] = "Reserved Memory Type",
56 1.13 jmcneill [EfiLoaderCode] = "Loader Code",
57 1.13 jmcneill [EfiLoaderData] = "Loader Data",
58 1.13 jmcneill [EfiBootServicesCode] = "Boot Services Code",
59 1.13 jmcneill [EfiBootServicesData] = "Boot Services Data",
60 1.13 jmcneill [EfiRuntimeServicesCode] = "Runtime Services Code",
61 1.13 jmcneill [EfiRuntimeServicesData] = "Runtime Services Data",
62 1.13 jmcneill [EfiConventionalMemory] = "Conventional Memory",
63 1.13 jmcneill [EfiUnusableMemory] = "Unusable Memory",
64 1.13 jmcneill [EfiACPIReclaimMemory] = "ACPI Reclaim Memory",
65 1.13 jmcneill [EfiACPIMemoryNVS] = "ACPI Memory NVS",
66 1.13 jmcneill [EfiMemoryMappedIO] = "MMIO",
67 1.13 jmcneill [EfiMemoryMappedIOPortSpace] = "MMIO (Port Space)",
68 1.13 jmcneill [EfiPalCode] = "Pal Code",
69 1.13 jmcneill [EfiPersistentMemory] = "Persistent Memory",
70 1.13 jmcneill };
71 1.13 jmcneill
72 1.3 jmcneill static char default_device[32];
73 1.6 jmcneill static char initrd_path[255];
74 1.7 jmcneill static char dtb_path[255];
75 1.18 thorpej static char efibootplist_path[255];
76 1.14 jmcneill static char netbsd_path[255];
77 1.15 skrll static char netbsd_args[255];
78 1.11 mrg
79 1.11 mrg #define DEFTIMEOUT 5
80 1.11 mrg #define DEFFILENAME names[0]
81 1.11 mrg
82 1.11 mrg int set_bootfile(const char *);
83 1.15 skrll int set_bootargs(const char *);
84 1.3 jmcneill
85 1.1 jmcneill void command_boot(char *);
86 1.3 jmcneill void command_dev(char *);
87 1.7 jmcneill void command_dtb(char *);
88 1.18 thorpej void command_plist(char *);
89 1.6 jmcneill void command_initrd(char *);
90 1.3 jmcneill void command_ls(char *);
91 1.13 jmcneill void command_mem(char *);
92 1.8 jmcneill void command_printenv(char *);
93 1.8 jmcneill void command_setenv(char *);
94 1.8 jmcneill void command_clearenv(char *);
95 1.8 jmcneill void command_resetenv(char *);
96 1.1 jmcneill void command_reset(char *);
97 1.1 jmcneill void command_version(char *);
98 1.1 jmcneill void command_quit(char *);
99 1.1 jmcneill
100 1.1 jmcneill const struct boot_command commands[] = {
101 1.6 jmcneill { "boot", command_boot, "boot [dev:][filename] [args]\n (ex. \"hd0a:\\netbsd.old -s\"" },
102 1.3 jmcneill { "dev", command_dev, "dev" },
103 1.7 jmcneill { "dtb", command_dtb, "dtb [dev:][filename]" },
104 1.18 thorpej { "plist", command_plist, "plist [dev:][filename]" },
105 1.6 jmcneill { "initrd", command_initrd, "initrd [dev:][filename]" },
106 1.4 jmcneill { "ls", command_ls, "ls [hdNn:/path]" },
107 1.13 jmcneill { "mem", command_mem, "mem" },
108 1.8 jmcneill { "printenv", command_printenv, "printenv [key]" },
109 1.8 jmcneill { "setenv", command_setenv, "setenv <key> <value>" },
110 1.8 jmcneill { "clearenv", command_clearenv, "clearenv <key>" },
111 1.8 jmcneill { "resetenv", command_resetenv, "resetenv" },
112 1.9 jmcneill { "reboot", command_reset, "reboot|reset" },
113 1.9 jmcneill { "reset", command_reset, NULL },
114 1.1 jmcneill { "version", command_version, "version" },
115 1.1 jmcneill { "help", command_help, "help|?" },
116 1.1 jmcneill { "?", command_help, NULL },
117 1.1 jmcneill { "quit", command_quit, "quit" },
118 1.1 jmcneill { NULL, NULL },
119 1.1 jmcneill };
120 1.1 jmcneill
121 1.1 jmcneill void
122 1.1 jmcneill command_help(char *arg)
123 1.1 jmcneill {
124 1.1 jmcneill int n;
125 1.1 jmcneill
126 1.1 jmcneill printf("commands are:\n");
127 1.1 jmcneill for (n = 0; commands[n].c_name; n++) {
128 1.1 jmcneill if (commands[n].c_help)
129 1.1 jmcneill printf("%s\n", commands[n].c_help);
130 1.1 jmcneill }
131 1.1 jmcneill }
132 1.1 jmcneill
133 1.1 jmcneill void
134 1.1 jmcneill command_boot(char *arg)
135 1.1 jmcneill {
136 1.1 jmcneill char *fname = arg;
137 1.11 mrg const char *kernel = *fname ? fname : bootfile;
138 1.1 jmcneill char *bootargs = gettrailer(arg);
139 1.1 jmcneill
140 1.11 mrg if (!kernel || !*kernel)
141 1.11 mrg kernel = DEFFILENAME;
142 1.11 mrg
143 1.16 skrll if (!*bootargs)
144 1.16 skrll bootargs = netbsd_args;
145 1.16 skrll
146 1.11 mrg exec_netbsd(kernel, bootargs);
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill void
150 1.3 jmcneill command_dev(char *arg)
151 1.3 jmcneill {
152 1.3 jmcneill if (arg && *arg) {
153 1.3 jmcneill set_default_device(arg);
154 1.3 jmcneill } else {
155 1.3 jmcneill efi_block_show();
156 1.4 jmcneill efi_net_show();
157 1.3 jmcneill }
158 1.3 jmcneill
159 1.3 jmcneill if (strlen(default_device) > 0) {
160 1.3 jmcneill printf("\n");
161 1.3 jmcneill printf("default: %s\n", default_device);
162 1.3 jmcneill }
163 1.3 jmcneill }
164 1.3 jmcneill
165 1.3 jmcneill void
166 1.7 jmcneill command_dtb(char *arg)
167 1.7 jmcneill {
168 1.7 jmcneill set_dtb_path(arg);
169 1.7 jmcneill }
170 1.7 jmcneill
171 1.7 jmcneill void
172 1.18 thorpej command_plist(char *arg)
173 1.18 thorpej {
174 1.18 thorpej if (set_efibootplist_path(arg) == 0)
175 1.18 thorpej load_efibootplist(false);
176 1.18 thorpej }
177 1.18 thorpej
178 1.18 thorpej void
179 1.6 jmcneill command_initrd(char *arg)
180 1.6 jmcneill {
181 1.6 jmcneill set_initrd_path(arg);
182 1.6 jmcneill }
183 1.6 jmcneill
184 1.6 jmcneill void
185 1.3 jmcneill command_ls(char *arg)
186 1.3 jmcneill {
187 1.3 jmcneill ls(arg);
188 1.3 jmcneill }
189 1.3 jmcneill
190 1.3 jmcneill void
191 1.13 jmcneill command_mem(char *arg)
192 1.13 jmcneill {
193 1.13 jmcneill EFI_MEMORY_DESCRIPTOR *md, *memmap;
194 1.13 jmcneill UINTN nentries, mapkey, descsize;
195 1.13 jmcneill UINT32 descver;
196 1.13 jmcneill int n;
197 1.13 jmcneill
198 1.13 jmcneill printf("Type Start End Attributes\n");
199 1.13 jmcneill printf("---------------------- ---------------- ---------------- ----------------\n");
200 1.13 jmcneill memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
201 1.13 jmcneill for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
202 1.13 jmcneill const char *mem_type = "<unknown>";
203 1.13 jmcneill if (md->Type < __arraycount(efi_memory_type))
204 1.13 jmcneill mem_type = efi_memory_type[md->Type];
205 1.13 jmcneill
206 1.13 jmcneill printf("%-22s %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
207 1.13 jmcneill mem_type, md->PhysicalStart, md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) - 1,
208 1.13 jmcneill md->Attribute);
209 1.13 jmcneill }
210 1.13 jmcneill }
211 1.13 jmcneill
212 1.13 jmcneill void
213 1.8 jmcneill command_printenv(char *arg)
214 1.8 jmcneill {
215 1.8 jmcneill char *val;
216 1.8 jmcneill
217 1.8 jmcneill if (arg && *arg) {
218 1.8 jmcneill val = efi_env_get(arg);
219 1.8 jmcneill if (val) {
220 1.8 jmcneill printf("\"%s\" = \"%s\"\n", arg, val);
221 1.8 jmcneill FreePool(val);
222 1.8 jmcneill }
223 1.8 jmcneill } else {
224 1.8 jmcneill efi_env_print();
225 1.8 jmcneill }
226 1.8 jmcneill }
227 1.8 jmcneill
228 1.8 jmcneill void
229 1.8 jmcneill command_setenv(char *arg)
230 1.8 jmcneill {
231 1.8 jmcneill char *spc;
232 1.8 jmcneill
233 1.8 jmcneill spc = strchr(arg, ' ');
234 1.8 jmcneill if (spc == NULL || spc[1] == '\0') {
235 1.8 jmcneill command_help("");
236 1.8 jmcneill return;
237 1.8 jmcneill }
238 1.8 jmcneill
239 1.8 jmcneill *spc = '\0';
240 1.8 jmcneill efi_env_set(arg, spc + 1);
241 1.8 jmcneill }
242 1.8 jmcneill
243 1.8 jmcneill void
244 1.8 jmcneill command_clearenv(char *arg)
245 1.8 jmcneill {
246 1.8 jmcneill if (*arg == '\0') {
247 1.8 jmcneill command_help("");
248 1.8 jmcneill return;
249 1.8 jmcneill }
250 1.8 jmcneill efi_env_clear(arg);
251 1.8 jmcneill }
252 1.8 jmcneill
253 1.8 jmcneill void
254 1.8 jmcneill command_resetenv(char *arg)
255 1.8 jmcneill {
256 1.8 jmcneill efi_env_reset();
257 1.8 jmcneill }
258 1.8 jmcneill
259 1.8 jmcneill void
260 1.1 jmcneill command_version(char *arg)
261 1.1 jmcneill {
262 1.1 jmcneill char *ufirmware;
263 1.1 jmcneill int rv;
264 1.1 jmcneill
265 1.1 jmcneill printf("EFI version: %d.%02d\n",
266 1.1 jmcneill ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
267 1.1 jmcneill ufirmware = NULL;
268 1.1 jmcneill rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
269 1.1 jmcneill if (rv == 0) {
270 1.17 jmcneill printf("EFI Firmware: %s (rev 0x%x)\n", ufirmware,
271 1.17 jmcneill ST->FirmwareRevision);
272 1.1 jmcneill FreePool(ufirmware);
273 1.1 jmcneill }
274 1.5 jmcneill
275 1.5 jmcneill efi_fdt_show();
276 1.10 jmcneill efi_acpi_show();
277 1.1 jmcneill }
278 1.1 jmcneill
279 1.1 jmcneill void
280 1.1 jmcneill command_quit(char *arg)
281 1.1 jmcneill {
282 1.1 jmcneill efi_exit();
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.9 jmcneill void
286 1.9 jmcneill command_reset(char *arg)
287 1.9 jmcneill {
288 1.9 jmcneill efi_reboot();
289 1.9 jmcneill }
290 1.9 jmcneill
291 1.3 jmcneill int
292 1.11 mrg set_default_device(const char *arg)
293 1.3 jmcneill {
294 1.3 jmcneill if (strlen(arg) + 1 > sizeof(default_device))
295 1.3 jmcneill return ERANGE;
296 1.3 jmcneill strcpy(default_device, arg);
297 1.3 jmcneill return 0;
298 1.3 jmcneill }
299 1.3 jmcneill
300 1.3 jmcneill char *
301 1.3 jmcneill get_default_device(void)
302 1.3 jmcneill {
303 1.3 jmcneill return default_device;
304 1.3 jmcneill }
305 1.3 jmcneill
306 1.6 jmcneill int
307 1.11 mrg set_initrd_path(const char *arg)
308 1.6 jmcneill {
309 1.6 jmcneill if (strlen(arg) + 1 > sizeof(initrd_path))
310 1.6 jmcneill return ERANGE;
311 1.6 jmcneill strcpy(initrd_path, arg);
312 1.6 jmcneill return 0;
313 1.6 jmcneill }
314 1.6 jmcneill
315 1.6 jmcneill char *
316 1.6 jmcneill get_initrd_path(void)
317 1.6 jmcneill {
318 1.6 jmcneill return initrd_path;
319 1.6 jmcneill }
320 1.6 jmcneill
321 1.7 jmcneill int
322 1.11 mrg set_dtb_path(const char *arg)
323 1.7 jmcneill {
324 1.7 jmcneill if (strlen(arg) + 1 > sizeof(dtb_path))
325 1.7 jmcneill return ERANGE;
326 1.7 jmcneill strcpy(dtb_path, arg);
327 1.7 jmcneill return 0;
328 1.7 jmcneill }
329 1.7 jmcneill
330 1.7 jmcneill char *
331 1.7 jmcneill get_dtb_path(void)
332 1.7 jmcneill {
333 1.7 jmcneill return dtb_path;
334 1.7 jmcneill }
335 1.7 jmcneill
336 1.11 mrg int
337 1.18 thorpej set_efibootplist_path(const char *arg)
338 1.18 thorpej {
339 1.18 thorpej if (strlen(arg) + 1 > sizeof(efibootplist_path))
340 1.18 thorpej return ERANGE;
341 1.18 thorpej strcpy(efibootplist_path, arg);
342 1.18 thorpej return 0;
343 1.18 thorpej }
344 1.18 thorpej
345 1.18 thorpej char *get_efibootplist_path(void)
346 1.18 thorpej {
347 1.18 thorpej return efibootplist_path;
348 1.18 thorpej }
349 1.18 thorpej
350 1.18 thorpej int
351 1.11 mrg set_bootfile(const char *arg)
352 1.11 mrg {
353 1.14 jmcneill if (strlen(arg) + 1 > sizeof(netbsd_path))
354 1.11 mrg return ERANGE;
355 1.14 jmcneill strcpy(netbsd_path, arg);
356 1.11 mrg return 0;
357 1.11 mrg }
358 1.11 mrg
359 1.15 skrll int
360 1.15 skrll set_bootargs(const char *arg)
361 1.15 skrll {
362 1.15 skrll if (strlen(arg) + 1 > sizeof(netbsd_args))
363 1.15 skrll return ERANGE;
364 1.15 skrll strcpy(netbsd_args, arg);
365 1.15 skrll return 0;
366 1.15 skrll }
367 1.15 skrll
368 1.1 jmcneill void
369 1.1 jmcneill print_banner(void)
370 1.1 jmcneill {
371 1.1 jmcneill printf("\n\n"
372 1.1 jmcneill ">> %s, Revision %s (from NetBSD %s)\n",
373 1.1 jmcneill bootprog_name, bootprog_rev, bootprog_kernrev);
374 1.1 jmcneill }
375 1.1 jmcneill
376 1.8 jmcneill static void
377 1.8 jmcneill read_env(void)
378 1.8 jmcneill {
379 1.8 jmcneill char *s;
380 1.8 jmcneill
381 1.18 thorpej s = efi_env_get("efibootplist");
382 1.18 thorpej if (s) {
383 1.18 thorpej #ifdef EFIBOOT_DEBUG
384 1.18 thorpej printf(">> Setting efiboot.plist path to '%s' from environment\n", s);
385 1.18 thorpej #endif
386 1.18 thorpej set_efibootplist_path(s);
387 1.18 thorpej FreePool(s);
388 1.18 thorpej }
389 1.18 thorpej
390 1.18 thorpej /*
391 1.18 thorpej * Read the efiboot.plist now as it may contain additional
392 1.18 thorpej * environment variables.
393 1.18 thorpej */
394 1.18 thorpej load_efibootplist(true);
395 1.18 thorpej
396 1.8 jmcneill s = efi_env_get("fdtfile");
397 1.8 jmcneill if (s) {
398 1.8 jmcneill #ifdef EFIBOOT_DEBUG
399 1.8 jmcneill printf(">> Setting DTB path to '%s' from environment\n", s);
400 1.8 jmcneill #endif
401 1.8 jmcneill set_dtb_path(s);
402 1.8 jmcneill FreePool(s);
403 1.8 jmcneill }
404 1.8 jmcneill
405 1.8 jmcneill s = efi_env_get("initrd");
406 1.8 jmcneill if (s) {
407 1.8 jmcneill #ifdef EFIBOOT_DEBUG
408 1.8 jmcneill printf(">> Setting initrd path to '%s' from environment\n", s);
409 1.8 jmcneill #endif
410 1.8 jmcneill set_initrd_path(s);
411 1.8 jmcneill FreePool(s);
412 1.8 jmcneill }
413 1.8 jmcneill
414 1.11 mrg s = efi_env_get("bootfile");
415 1.11 mrg if (s) {
416 1.11 mrg #ifdef EFIBOOT_DEBUG
417 1.11 mrg printf(">> Setting bootfile path to '%s' from environment\n", s);
418 1.11 mrg #endif
419 1.11 mrg set_bootfile(s);
420 1.11 mrg FreePool(s);
421 1.11 mrg }
422 1.11 mrg
423 1.8 jmcneill s = efi_env_get("rootdev");
424 1.8 jmcneill if (s) {
425 1.8 jmcneill #ifdef EFIBOOT_DEBUG
426 1.8 jmcneill printf(">> Setting default device to '%s' from environment\n", s);
427 1.8 jmcneill #endif
428 1.8 jmcneill set_default_device(s);
429 1.8 jmcneill FreePool(s);
430 1.8 jmcneill }
431 1.15 skrll
432 1.15 skrll s = efi_env_get("bootargs");
433 1.15 skrll if (s) {
434 1.15 skrll #ifdef EFIBOOT_DEBUG
435 1.15 skrll printf(">> Setting default boot args to '%s' from environment\n", s);
436 1.15 skrll #endif
437 1.15 skrll set_bootargs(s);
438 1.15 skrll FreePool(s);
439 1.15 skrll }
440 1.8 jmcneill }
441 1.8 jmcneill
442 1.1 jmcneill void
443 1.1 jmcneill boot(void)
444 1.1 jmcneill {
445 1.1 jmcneill int currname, c;
446 1.1 jmcneill
447 1.8 jmcneill read_env();
448 1.1 jmcneill print_banner();
449 1.11 mrg printf("Press return to boot now, any other key for boot prompt\n");
450 1.1 jmcneill
451 1.14 jmcneill if (netbsd_path[0] != '\0')
452 1.11 mrg currname = -1;
453 1.11 mrg else
454 1.11 mrg currname = 0;
455 1.11 mrg
456 1.12 mrg for (; currname < (int)NUMNAMES; currname++) {
457 1.11 mrg if (currname >= 0)
458 1.11 mrg set_bootfile(names[currname]);
459 1.16 skrll printf("booting %s%s%s - starting in ", netbsd_path,
460 1.16 skrll netbsd_args[0] != '\0' ? " " : "", netbsd_args);
461 1.1 jmcneill
462 1.1 jmcneill c = awaitkey(DEFTIMEOUT, 1);
463 1.11 mrg if (c != '\r' && c != '\n' && c != '\0')
464 1.1 jmcneill bootprompt(); /* does not return */
465 1.1 jmcneill
466 1.15 skrll exec_netbsd(netbsd_path, netbsd_args);
467 1.1 jmcneill }
468 1.1 jmcneill
469 1.1 jmcneill bootprompt(); /* does not return */
470 1.1 jmcneill }
471