boot.c revision 1.35 1 1.35 jmcneill /* $NetBSD: boot.c,v 1.35 2021/07/24 10:22:28 jmcneill 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.25 jmcneill #include "efifile.h"
33 1.5 jmcneill #include "efifdt.h"
34 1.10 jmcneill #include "efiacpi.h"
35 1.21 riastrad #include "efirng.h"
36 1.22 jmcneill #include "module.h"
37 1.24 thorpej #include "overlay.h"
38 1.23 jmcneill #include "bootmenu.h"
39 1.1 jmcneill
40 1.1 jmcneill #include <sys/bootblock.h>
41 1.1 jmcneill #include <sys/boot_flag.h>
42 1.1 jmcneill #include <machine/limits.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <loadfile.h>
45 1.23 jmcneill #include <bootcfg.h>
46 1.1 jmcneill
47 1.1 jmcneill extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
48 1.1 jmcneill
49 1.1 jmcneill extern char twiddle_toggle;
50 1.1 jmcneill
51 1.11 mrg static const char * const names[] = {
52 1.11 mrg "netbsd", "netbsd.gz",
53 1.11 mrg "onetbsd", "onetbsd.gz",
54 1.11 mrg "netbsd.old", "netbsd.old.gz",
55 1.1 jmcneill };
56 1.1 jmcneill
57 1.1 jmcneill #define NUMNAMES __arraycount(names)
58 1.1 jmcneill
59 1.13 jmcneill static const char *efi_memory_type[] = {
60 1.13 jmcneill [EfiReservedMemoryType] = "Reserved Memory Type",
61 1.13 jmcneill [EfiLoaderCode] = "Loader Code",
62 1.13 jmcneill [EfiLoaderData] = "Loader Data",
63 1.13 jmcneill [EfiBootServicesCode] = "Boot Services Code",
64 1.13 jmcneill [EfiBootServicesData] = "Boot Services Data",
65 1.13 jmcneill [EfiRuntimeServicesCode] = "Runtime Services Code",
66 1.13 jmcneill [EfiRuntimeServicesData] = "Runtime Services Data",
67 1.13 jmcneill [EfiConventionalMemory] = "Conventional Memory",
68 1.13 jmcneill [EfiUnusableMemory] = "Unusable Memory",
69 1.13 jmcneill [EfiACPIReclaimMemory] = "ACPI Reclaim Memory",
70 1.13 jmcneill [EfiACPIMemoryNVS] = "ACPI Memory NVS",
71 1.13 jmcneill [EfiMemoryMappedIO] = "MMIO",
72 1.13 jmcneill [EfiMemoryMappedIOPortSpace] = "MMIO (Port Space)",
73 1.13 jmcneill [EfiPalCode] = "Pal Code",
74 1.13 jmcneill [EfiPersistentMemory] = "Persistent Memory",
75 1.13 jmcneill };
76 1.13 jmcneill
77 1.3 jmcneill static char default_device[32];
78 1.28 jmcneill static int default_fstype = FS_UNUSED;
79 1.6 jmcneill static char initrd_path[255];
80 1.7 jmcneill static char dtb_path[255];
81 1.14 jmcneill static char netbsd_path[255];
82 1.15 skrll static char netbsd_args[255];
83 1.19 riastrad static char rndseed_path[255];
84 1.11 mrg
85 1.11 mrg #define DEFTIMEOUT 5
86 1.11 mrg #define DEFFILENAME names[0]
87 1.11 mrg
88 1.11 mrg int set_bootfile(const char *);
89 1.15 skrll int set_bootargs(const char *);
90 1.3 jmcneill
91 1.1 jmcneill void command_boot(char *);
92 1.3 jmcneill void command_dev(char *);
93 1.7 jmcneill void command_dtb(char *);
94 1.6 jmcneill void command_initrd(char *);
95 1.19 riastrad void command_rndseed(char *);
96 1.24 thorpej void command_dtoverlay(char *);
97 1.24 thorpej void command_dtoverlays(char *);
98 1.22 jmcneill void command_modules(char *);
99 1.22 jmcneill void command_load(char *);
100 1.22 jmcneill void command_unload(char *);
101 1.3 jmcneill void command_ls(char *);
102 1.13 jmcneill void command_mem(char *);
103 1.23 jmcneill void command_menu(char *);
104 1.1 jmcneill void command_reset(char *);
105 1.1 jmcneill void command_version(char *);
106 1.1 jmcneill void command_quit(char *);
107 1.1 jmcneill
108 1.1 jmcneill const struct boot_command commands[] = {
109 1.6 jmcneill { "boot", command_boot, "boot [dev:][filename] [args]\n (ex. \"hd0a:\\netbsd.old -s\"" },
110 1.3 jmcneill { "dev", command_dev, "dev" },
111 1.7 jmcneill { "dtb", command_dtb, "dtb [dev:][filename]" },
112 1.6 jmcneill { "initrd", command_initrd, "initrd [dev:][filename]" },
113 1.35 jmcneill { "fs", command_initrd, NULL },
114 1.19 riastrad { "rndseed", command_rndseed, "rndseed [dev:][filename]" },
115 1.24 thorpej { "dtoverlay", command_dtoverlay, "dtoverlay [dev:][filename]" },
116 1.24 thorpej { "dtoverlays", command_dtoverlays, "dtoverlays [{on|off|reset}]" },
117 1.22 jmcneill { "modules", command_modules, "modules [{on|off|reset}]" },
118 1.22 jmcneill { "load", command_load, "load <module_name>" },
119 1.22 jmcneill { "unload", command_unload, "unload <module_name>" },
120 1.4 jmcneill { "ls", command_ls, "ls [hdNn:/path]" },
121 1.13 jmcneill { "mem", command_mem, "mem" },
122 1.23 jmcneill { "menu", command_menu, "menu" },
123 1.9 jmcneill { "reboot", command_reset, "reboot|reset" },
124 1.9 jmcneill { "reset", command_reset, NULL },
125 1.1 jmcneill { "version", command_version, "version" },
126 1.20 jmcneill { "ver", command_version, NULL },
127 1.1 jmcneill { "help", command_help, "help|?" },
128 1.1 jmcneill { "?", command_help, NULL },
129 1.1 jmcneill { "quit", command_quit, "quit" },
130 1.1 jmcneill { NULL, NULL },
131 1.1 jmcneill };
132 1.1 jmcneill
133 1.28 jmcneill static int
134 1.28 jmcneill bootcfg_path(char *pathbuf, size_t pathbuflen)
135 1.28 jmcneill {
136 1.28 jmcneill
137 1.28 jmcneill /*
138 1.30 rin * Fallback to default_device
139 1.30 rin * - for ISO9660 (efi_file_path() succeeds but does not work correctly)
140 1.30 rin * - or whenever efi_file_path() fails (due to broken firmware)
141 1.28 jmcneill */
142 1.30 rin if (default_fstype == FS_ISO9660 || efi_bootdp == NULL ||
143 1.30 rin efi_file_path(efi_bootdp, BOOTCFG_FILENAME, pathbuf, pathbuflen))
144 1.30 rin snprintf(pathbuf, pathbuflen, "%s:%s", default_device,
145 1.30 rin BOOTCFG_FILENAME);
146 1.28 jmcneill
147 1.30 rin return 0;
148 1.28 jmcneill }
149 1.28 jmcneill
150 1.1 jmcneill void
151 1.1 jmcneill command_help(char *arg)
152 1.1 jmcneill {
153 1.1 jmcneill int n;
154 1.1 jmcneill
155 1.1 jmcneill printf("commands are:\n");
156 1.1 jmcneill for (n = 0; commands[n].c_name; n++) {
157 1.1 jmcneill if (commands[n].c_help)
158 1.1 jmcneill printf("%s\n", commands[n].c_help);
159 1.1 jmcneill }
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill void
163 1.1 jmcneill command_boot(char *arg)
164 1.1 jmcneill {
165 1.1 jmcneill char *fname = arg;
166 1.11 mrg const char *kernel = *fname ? fname : bootfile;
167 1.1 jmcneill char *bootargs = gettrailer(arg);
168 1.1 jmcneill
169 1.11 mrg if (!kernel || !*kernel)
170 1.11 mrg kernel = DEFFILENAME;
171 1.11 mrg
172 1.16 skrll if (!*bootargs)
173 1.16 skrll bootargs = netbsd_args;
174 1.16 skrll
175 1.32 jmcneill efi_block_set_readahead(true);
176 1.11 mrg exec_netbsd(kernel, bootargs);
177 1.32 jmcneill efi_block_set_readahead(false);
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill void
181 1.3 jmcneill command_dev(char *arg)
182 1.3 jmcneill {
183 1.3 jmcneill if (arg && *arg) {
184 1.3 jmcneill set_default_device(arg);
185 1.3 jmcneill } else {
186 1.3 jmcneill efi_block_show();
187 1.4 jmcneill efi_net_show();
188 1.3 jmcneill }
189 1.3 jmcneill
190 1.3 jmcneill if (strlen(default_device) > 0) {
191 1.3 jmcneill printf("\n");
192 1.3 jmcneill printf("default: %s\n", default_device);
193 1.3 jmcneill }
194 1.3 jmcneill }
195 1.3 jmcneill
196 1.3 jmcneill void
197 1.7 jmcneill command_dtb(char *arg)
198 1.7 jmcneill {
199 1.7 jmcneill set_dtb_path(arg);
200 1.7 jmcneill }
201 1.7 jmcneill
202 1.7 jmcneill void
203 1.24 thorpej command_initrd(char *arg)
204 1.18 thorpej {
205 1.24 thorpej set_initrd_path(arg);
206 1.18 thorpej }
207 1.18 thorpej
208 1.18 thorpej void
209 1.24 thorpej command_rndseed(char *arg)
210 1.24 thorpej {
211 1.24 thorpej set_rndseed_path(arg);
212 1.24 thorpej }
213 1.24 thorpej
214 1.24 thorpej void
215 1.24 thorpej command_dtoverlays(char *arg)
216 1.6 jmcneill {
217 1.24 thorpej if (arg && *arg) {
218 1.24 thorpej if (strcmp(arg, "on") == 0)
219 1.24 thorpej dtoverlay_enable(1);
220 1.24 thorpej else if (strcmp(arg, "off") == 0)
221 1.24 thorpej dtoverlay_enable(0);
222 1.24 thorpej else if (strcmp(arg, "reset") == 0)
223 1.24 thorpej dtoverlay_remove_all();
224 1.24 thorpej else {
225 1.24 thorpej command_help("");
226 1.24 thorpej return;
227 1.24 thorpej }
228 1.24 thorpej } else {
229 1.24 thorpej printf("Device Tree overlays are %sabled\n",
230 1.24 thorpej dtoverlay_enabled ? "en" : "dis");
231 1.24 thorpej }
232 1.6 jmcneill }
233 1.6 jmcneill
234 1.6 jmcneill void
235 1.24 thorpej command_dtoverlay(char *arg)
236 1.19 riastrad {
237 1.24 thorpej if (!arg || !*arg) {
238 1.24 thorpej command_help("");
239 1.24 thorpej return;
240 1.24 thorpej }
241 1.24 thorpej
242 1.24 thorpej dtoverlay_add(arg);
243 1.19 riastrad }
244 1.19 riastrad
245 1.19 riastrad void
246 1.22 jmcneill command_modules(char *arg)
247 1.22 jmcneill {
248 1.22 jmcneill if (arg && *arg) {
249 1.22 jmcneill if (strcmp(arg, "on") == 0)
250 1.22 jmcneill module_enable(1);
251 1.22 jmcneill else if (strcmp(arg, "off") == 0)
252 1.22 jmcneill module_enable(0);
253 1.22 jmcneill else if (strcmp(arg, "reset") == 0)
254 1.22 jmcneill module_remove_all();
255 1.22 jmcneill else {
256 1.22 jmcneill command_help("");
257 1.22 jmcneill return;
258 1.22 jmcneill }
259 1.22 jmcneill } else {
260 1.22 jmcneill printf("modules are %sabled\n", module_enabled ? "en" : "dis");
261 1.22 jmcneill }
262 1.22 jmcneill }
263 1.22 jmcneill
264 1.22 jmcneill void
265 1.22 jmcneill command_load(char *arg)
266 1.22 jmcneill {
267 1.22 jmcneill if (!arg || !*arg) {
268 1.22 jmcneill command_help("");
269 1.22 jmcneill return;
270 1.22 jmcneill }
271 1.22 jmcneill
272 1.22 jmcneill module_add(arg);
273 1.22 jmcneill }
274 1.22 jmcneill
275 1.22 jmcneill void
276 1.22 jmcneill command_unload(char *arg)
277 1.22 jmcneill {
278 1.22 jmcneill if (!arg || !*arg) {
279 1.22 jmcneill command_help("");
280 1.22 jmcneill return;
281 1.22 jmcneill }
282 1.22 jmcneill
283 1.22 jmcneill module_remove(arg);
284 1.22 jmcneill }
285 1.22 jmcneill
286 1.22 jmcneill void
287 1.3 jmcneill command_ls(char *arg)
288 1.3 jmcneill {
289 1.3 jmcneill ls(arg);
290 1.3 jmcneill }
291 1.3 jmcneill
292 1.3 jmcneill void
293 1.13 jmcneill command_mem(char *arg)
294 1.13 jmcneill {
295 1.13 jmcneill EFI_MEMORY_DESCRIPTOR *md, *memmap;
296 1.13 jmcneill UINTN nentries, mapkey, descsize;
297 1.13 jmcneill UINT32 descver;
298 1.13 jmcneill int n;
299 1.13 jmcneill
300 1.13 jmcneill printf("Type Start End Attributes\n");
301 1.13 jmcneill printf("---------------------- ---------------- ---------------- ----------------\n");
302 1.13 jmcneill memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
303 1.13 jmcneill for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
304 1.13 jmcneill const char *mem_type = "<unknown>";
305 1.13 jmcneill if (md->Type < __arraycount(efi_memory_type))
306 1.13 jmcneill mem_type = efi_memory_type[md->Type];
307 1.13 jmcneill
308 1.13 jmcneill printf("%-22s %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
309 1.13 jmcneill mem_type, md->PhysicalStart, md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) - 1,
310 1.13 jmcneill md->Attribute);
311 1.13 jmcneill }
312 1.13 jmcneill }
313 1.13 jmcneill
314 1.13 jmcneill void
315 1.23 jmcneill command_menu(char *arg)
316 1.23 jmcneill {
317 1.23 jmcneill if (bootcfg_info.nummenu == 0) {
318 1.23 jmcneill printf("No menu defined in boot.cfg\n");
319 1.23 jmcneill return;
320 1.23 jmcneill }
321 1.23 jmcneill
322 1.23 jmcneill doboottypemenu(); /* Does not return */
323 1.23 jmcneill }
324 1.23 jmcneill
325 1.23 jmcneill void
326 1.1 jmcneill command_version(char *arg)
327 1.1 jmcneill {
328 1.26 jmcneill char pathbuf[80];
329 1.1 jmcneill char *ufirmware;
330 1.1 jmcneill int rv;
331 1.1 jmcneill
332 1.20 jmcneill printf("Version: %s (%s)\n", bootprog_rev, bootprog_kernrev);
333 1.20 jmcneill printf("EFI: %d.%02d\n",
334 1.1 jmcneill ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
335 1.1 jmcneill ufirmware = NULL;
336 1.1 jmcneill rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
337 1.1 jmcneill if (rv == 0) {
338 1.20 jmcneill printf("Firmware: %s (rev 0x%x)\n", ufirmware,
339 1.17 jmcneill ST->FirmwareRevision);
340 1.1 jmcneill FreePool(ufirmware);
341 1.1 jmcneill }
342 1.28 jmcneill if (bootcfg_path(pathbuf, sizeof(pathbuf)) == 0) {
343 1.26 jmcneill printf("Config path: %s\n", pathbuf);
344 1.26 jmcneill }
345 1.5 jmcneill
346 1.5 jmcneill efi_fdt_show();
347 1.10 jmcneill efi_acpi_show();
348 1.21 riastrad efi_rng_show();
349 1.29 jmcneill efi_md_show();
350 1.1 jmcneill }
351 1.1 jmcneill
352 1.1 jmcneill void
353 1.1 jmcneill command_quit(char *arg)
354 1.1 jmcneill {
355 1.1 jmcneill efi_exit();
356 1.1 jmcneill }
357 1.1 jmcneill
358 1.9 jmcneill void
359 1.9 jmcneill command_reset(char *arg)
360 1.9 jmcneill {
361 1.9 jmcneill efi_reboot();
362 1.9 jmcneill }
363 1.9 jmcneill
364 1.3 jmcneill int
365 1.11 mrg set_default_device(const char *arg)
366 1.3 jmcneill {
367 1.3 jmcneill if (strlen(arg) + 1 > sizeof(default_device))
368 1.3 jmcneill return ERANGE;
369 1.3 jmcneill strcpy(default_device, arg);
370 1.3 jmcneill return 0;
371 1.3 jmcneill }
372 1.3 jmcneill
373 1.3 jmcneill char *
374 1.3 jmcneill get_default_device(void)
375 1.3 jmcneill {
376 1.3 jmcneill return default_device;
377 1.3 jmcneill }
378 1.3 jmcneill
379 1.28 jmcneill void
380 1.28 jmcneill set_default_fstype(int fstype)
381 1.28 jmcneill {
382 1.28 jmcneill default_fstype = fstype;
383 1.28 jmcneill }
384 1.28 jmcneill
385 1.28 jmcneill int
386 1.28 jmcneill get_default_fstype(void)
387 1.28 jmcneill {
388 1.28 jmcneill return default_fstype;
389 1.28 jmcneill }
390 1.28 jmcneill
391 1.6 jmcneill int
392 1.11 mrg set_initrd_path(const char *arg)
393 1.6 jmcneill {
394 1.6 jmcneill if (strlen(arg) + 1 > sizeof(initrd_path))
395 1.6 jmcneill return ERANGE;
396 1.6 jmcneill strcpy(initrd_path, arg);
397 1.6 jmcneill return 0;
398 1.6 jmcneill }
399 1.6 jmcneill
400 1.6 jmcneill char *
401 1.6 jmcneill get_initrd_path(void)
402 1.6 jmcneill {
403 1.6 jmcneill return initrd_path;
404 1.6 jmcneill }
405 1.6 jmcneill
406 1.7 jmcneill int
407 1.11 mrg set_dtb_path(const char *arg)
408 1.7 jmcneill {
409 1.7 jmcneill if (strlen(arg) + 1 > sizeof(dtb_path))
410 1.7 jmcneill return ERANGE;
411 1.7 jmcneill strcpy(dtb_path, arg);
412 1.7 jmcneill return 0;
413 1.7 jmcneill }
414 1.7 jmcneill
415 1.7 jmcneill char *
416 1.7 jmcneill get_dtb_path(void)
417 1.7 jmcneill {
418 1.7 jmcneill return dtb_path;
419 1.7 jmcneill }
420 1.7 jmcneill
421 1.11 mrg int
422 1.19 riastrad set_rndseed_path(const char *arg)
423 1.19 riastrad {
424 1.19 riastrad if (strlen(arg) + 1 > sizeof(rndseed_path))
425 1.19 riastrad return ERANGE;
426 1.19 riastrad strcpy(rndseed_path, arg);
427 1.19 riastrad return 0;
428 1.19 riastrad }
429 1.19 riastrad
430 1.19 riastrad char *
431 1.19 riastrad get_rndseed_path(void)
432 1.19 riastrad {
433 1.19 riastrad return rndseed_path;
434 1.19 riastrad }
435 1.19 riastrad
436 1.19 riastrad int
437 1.11 mrg set_bootfile(const char *arg)
438 1.11 mrg {
439 1.14 jmcneill if (strlen(arg) + 1 > sizeof(netbsd_path))
440 1.11 mrg return ERANGE;
441 1.14 jmcneill strcpy(netbsd_path, arg);
442 1.11 mrg return 0;
443 1.11 mrg }
444 1.11 mrg
445 1.15 skrll int
446 1.15 skrll set_bootargs(const char *arg)
447 1.15 skrll {
448 1.15 skrll if (strlen(arg) + 1 > sizeof(netbsd_args))
449 1.15 skrll return ERANGE;
450 1.15 skrll strcpy(netbsd_args, arg);
451 1.15 skrll return 0;
452 1.15 skrll }
453 1.15 skrll
454 1.33 jmcneill static void
455 1.33 jmcneill get_memory_info(uint64_t *ptotal)
456 1.33 jmcneill {
457 1.33 jmcneill EFI_MEMORY_DESCRIPTOR *md, *memmap;
458 1.33 jmcneill UINTN nentries, mapkey, descsize;
459 1.33 jmcneill UINT32 descver;
460 1.33 jmcneill uint64_t totalpg = 0;
461 1.33 jmcneill int n;
462 1.33 jmcneill
463 1.33 jmcneill memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
464 1.33 jmcneill for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
465 1.33 jmcneill if ((md->Attribute & EFI_MEMORY_WB) == 0) {
466 1.33 jmcneill continue;
467 1.33 jmcneill }
468 1.33 jmcneill totalpg += md->NumberOfPages;
469 1.33 jmcneill }
470 1.33 jmcneill
471 1.33 jmcneill *ptotal = totalpg * EFI_PAGE_SIZE;
472 1.33 jmcneill }
473 1.33 jmcneill
474 1.33 jmcneill static void
475 1.33 jmcneill format_bytes(uint64_t val, uint64_t *pdiv, const char **punit)
476 1.33 jmcneill {
477 1.33 jmcneill static const char *units[] = { "KB", "MB", "GB" };
478 1.33 jmcneill unsigned n;
479 1.33 jmcneill
480 1.33 jmcneill *punit = "bytes";
481 1.33 jmcneill *pdiv = 1;
482 1.33 jmcneill
483 1.34 jmcneill for (n = 0; n < __arraycount(units) && val >= 1024 * 10; n++) {
484 1.33 jmcneill *punit = units[n];
485 1.33 jmcneill *pdiv *= 1024;
486 1.33 jmcneill val /= 1024;
487 1.33 jmcneill }
488 1.33 jmcneill }
489 1.33 jmcneill
490 1.1 jmcneill void
491 1.1 jmcneill print_banner(void)
492 1.1 jmcneill {
493 1.33 jmcneill const char *total_unit;
494 1.33 jmcneill uint64_t total, total_div;
495 1.33 jmcneill
496 1.33 jmcneill get_memory_info(&total);
497 1.33 jmcneill format_bytes(total, &total_div, &total_unit);
498 1.33 jmcneill
499 1.31 nia printf(" \\-__,------,___.\n");
500 1.31 nia printf(" \\ __,---` %s\n", bootprog_name);
501 1.31 nia printf(" \\ `---,_. Revision %s\n", bootprog_rev);
502 1.33 jmcneill printf(" \\-,_____,.---` Memory: %" PRIu64 " %s\n",
503 1.33 jmcneill total / total_div, total_unit);
504 1.31 nia printf(" \\\n");
505 1.31 nia printf(" \\\n");
506 1.31 nia printf(" \\\n\n");
507 1.1 jmcneill }
508 1.1 jmcneill
509 1.1 jmcneill void
510 1.1 jmcneill boot(void)
511 1.1 jmcneill {
512 1.25 jmcneill char pathbuf[80];
513 1.1 jmcneill int currname, c;
514 1.1 jmcneill
515 1.28 jmcneill if (bootcfg_path(pathbuf, sizeof(pathbuf)) == 0) {
516 1.25 jmcneill twiddle_toggle = 1;
517 1.25 jmcneill parsebootconf(pathbuf);
518 1.25 jmcneill }
519 1.23 jmcneill
520 1.23 jmcneill if (bootcfg_info.clear)
521 1.23 jmcneill uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
522 1.23 jmcneill
523 1.1 jmcneill print_banner();
524 1.23 jmcneill
525 1.23 jmcneill /* Display menu if configured */
526 1.23 jmcneill if (bootcfg_info.nummenu > 0) {
527 1.23 jmcneill doboottypemenu(); /* No return */
528 1.23 jmcneill }
529 1.23 jmcneill
530 1.11 mrg printf("Press return to boot now, any other key for boot prompt\n");
531 1.1 jmcneill
532 1.14 jmcneill if (netbsd_path[0] != '\0')
533 1.11 mrg currname = -1;
534 1.11 mrg else
535 1.11 mrg currname = 0;
536 1.11 mrg
537 1.12 mrg for (; currname < (int)NUMNAMES; currname++) {
538 1.11 mrg if (currname >= 0)
539 1.11 mrg set_bootfile(names[currname]);
540 1.16 skrll printf("booting %s%s%s - starting in ", netbsd_path,
541 1.16 skrll netbsd_args[0] != '\0' ? " " : "", netbsd_args);
542 1.1 jmcneill
543 1.1 jmcneill c = awaitkey(DEFTIMEOUT, 1);
544 1.11 mrg if (c != '\r' && c != '\n' && c != '\0')
545 1.1 jmcneill bootprompt(); /* does not return */
546 1.1 jmcneill
547 1.32 jmcneill efi_block_set_readahead(true);
548 1.15 skrll exec_netbsd(netbsd_path, netbsd_args);
549 1.32 jmcneill efi_block_set_readahead(false);
550 1.1 jmcneill }
551 1.1 jmcneill
552 1.1 jmcneill bootprompt(); /* does not return */
553 1.1 jmcneill }
554