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