Home | History | Annotate | Line # | Download | only in efiboot
boot.c revision 1.29
      1 /*	$NetBSD: boot.c,v 1.29 2020/11/28 14:02:09 jmcneill 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 	 * Special handling of boot.cfg on ISO9660 because fs protocol doesn't
    137 	 * seem to work.
    138 	 */
    139 	if (default_fstype == FS_ISO9660) {
    140 		snprintf(pathbuf, pathbuflen, "%s:%s", default_device, BOOTCFG_FILENAME);
    141 		return 0;
    142 	}
    143 
    144 	/*
    145 	 * Fall back to fs protocol for loading boot.cfg
    146 	 */
    147 	if (efi_bootdp == NULL)
    148 		return ENXIO;
    149 
    150 	return efi_file_path(efi_bootdp, BOOTCFG_FILENAME, pathbuf, pathbuflen);
    151 }
    152 
    153 void
    154 command_help(char *arg)
    155 {
    156 	int n;
    157 
    158 	printf("commands are:\n");
    159 	for (n = 0; commands[n].c_name; n++) {
    160 		if (commands[n].c_help)
    161 			printf("%s\n", commands[n].c_help);
    162 	}
    163 }
    164 
    165 void
    166 command_boot(char *arg)
    167 {
    168 	char *fname = arg;
    169 	const char *kernel = *fname ? fname : bootfile;
    170 	char *bootargs = gettrailer(arg);
    171 
    172 	if (!kernel || !*kernel)
    173 		kernel = DEFFILENAME;
    174 
    175 	if (!*bootargs)
    176 		bootargs = netbsd_args;
    177 
    178 	exec_netbsd(kernel, bootargs);
    179 }
    180 
    181 void
    182 command_dev(char *arg)
    183 {
    184 	if (arg && *arg) {
    185 		set_default_device(arg);
    186 	} else {
    187 		efi_block_show();
    188 		efi_net_show();
    189 	}
    190 
    191 	if (strlen(default_device) > 0) {
    192 		printf("\n");
    193 		printf("default: %s\n", default_device);
    194 	}
    195 }
    196 
    197 void
    198 command_dtb(char *arg)
    199 {
    200 	set_dtb_path(arg);
    201 }
    202 
    203 void
    204 command_initrd(char *arg)
    205 {
    206 	set_initrd_path(arg);
    207 }
    208 
    209 void
    210 command_rndseed(char *arg)
    211 {
    212 	set_rndseed_path(arg);
    213 }
    214 
    215 void
    216 command_dtoverlays(char *arg)
    217 {
    218 	if (arg && *arg) {
    219 		if (strcmp(arg, "on") == 0)
    220 			dtoverlay_enable(1);
    221 		else if (strcmp(arg, "off") == 0)
    222 			dtoverlay_enable(0);
    223 		else if (strcmp(arg, "reset") == 0)
    224 			dtoverlay_remove_all();
    225 		else {
    226 			command_help("");
    227 			return;
    228 		}
    229 	} else {
    230 		printf("Device Tree overlays are %sabled\n",
    231 		    dtoverlay_enabled ? "en" : "dis");
    232 	}
    233 }
    234 
    235 void
    236 command_dtoverlay(char *arg)
    237 {
    238 	if (!arg || !*arg) {
    239 		command_help("");
    240 		return;
    241 	}
    242 
    243 	dtoverlay_add(arg);
    244 }
    245 
    246 void
    247 command_modules(char *arg)
    248 {
    249 	if (arg && *arg) {
    250 		if (strcmp(arg, "on") == 0)
    251 			module_enable(1);
    252 		else if (strcmp(arg, "off") == 0)
    253 			module_enable(0);
    254 		else if (strcmp(arg, "reset") == 0)
    255 			module_remove_all();
    256 		else {
    257 			command_help("");
    258 			return;
    259 		}
    260 	} else {
    261 		printf("modules are %sabled\n", module_enabled ? "en" : "dis");
    262 	}
    263 }
    264 
    265 void
    266 command_load(char *arg)
    267 {
    268 	if (!arg || !*arg) {
    269 		command_help("");
    270 		return;
    271 	}
    272 
    273 	module_add(arg);
    274 }
    275 
    276 void
    277 command_unload(char *arg)
    278 {
    279 	if (!arg || !*arg) {
    280 		command_help("");
    281 		return;
    282 	}
    283 
    284 	module_remove(arg);
    285 }
    286 
    287 void
    288 command_ls(char *arg)
    289 {
    290 	ls(arg);
    291 }
    292 
    293 void
    294 command_mem(char *arg)
    295 {
    296 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    297 	UINTN nentries, mapkey, descsize;
    298 	UINT32 descver;
    299 	int n;
    300 
    301 	printf("Type                    Start             End               Attributes\n");
    302 	printf("----------------------  ----------------  ----------------  ----------------\n");
    303 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    304 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    305 		const char *mem_type = "<unknown>";
    306 		if (md->Type < __arraycount(efi_memory_type))
    307 			mem_type = efi_memory_type[md->Type];
    308 
    309 		printf("%-22s  %016" PRIx64 "  %016" PRIx64 "  %016" PRIx64 "\n",
    310 		    mem_type, md->PhysicalStart, md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) - 1,
    311 		    md->Attribute);
    312 	}
    313 }
    314 
    315 void
    316 command_menu(char *arg)
    317 {
    318 	if (bootcfg_info.nummenu == 0) {
    319 		printf("No menu defined in boot.cfg\n");
    320 		return;
    321 	}
    322 
    323 	doboottypemenu();	/* Does not return */
    324 }
    325 
    326 void
    327 command_version(char *arg)
    328 {
    329 	char pathbuf[80];
    330 	char *ufirmware;
    331 	int rv;
    332 
    333 	printf("Version: %s (%s)\n", bootprog_rev, bootprog_kernrev);
    334 	printf("EFI: %d.%02d\n",
    335 	    ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
    336 	ufirmware = NULL;
    337 	rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
    338 	if (rv == 0) {
    339 		printf("Firmware: %s (rev 0x%x)\n", ufirmware,
    340 		    ST->FirmwareRevision);
    341 		FreePool(ufirmware);
    342 	}
    343 	if (bootcfg_path(pathbuf, sizeof(pathbuf)) == 0) {
    344 		printf("Config path: %s\n", pathbuf);
    345 	}
    346 
    347 	efi_fdt_show();
    348 	efi_acpi_show();
    349 	efi_rng_show();
    350 	efi_md_show();
    351 }
    352 
    353 void
    354 command_quit(char *arg)
    355 {
    356 	efi_exit();
    357 }
    358 
    359 void
    360 command_reset(char *arg)
    361 {
    362 	efi_reboot();
    363 }
    364 
    365 int
    366 set_default_device(const char *arg)
    367 {
    368 	if (strlen(arg) + 1 > sizeof(default_device))
    369 		return ERANGE;
    370 	strcpy(default_device, arg);
    371 	return 0;
    372 }
    373 
    374 char *
    375 get_default_device(void)
    376 {
    377 	return default_device;
    378 }
    379 
    380 void
    381 set_default_fstype(int fstype)
    382 {
    383 	default_fstype = fstype;
    384 }
    385 
    386 int
    387 get_default_fstype(void)
    388 {
    389 	return default_fstype;
    390 }
    391 
    392 int
    393 set_initrd_path(const char *arg)
    394 {
    395 	if (strlen(arg) + 1 > sizeof(initrd_path))
    396 		return ERANGE;
    397 	strcpy(initrd_path, arg);
    398 	return 0;
    399 }
    400 
    401 char *
    402 get_initrd_path(void)
    403 {
    404 	return initrd_path;
    405 }
    406 
    407 int
    408 set_dtb_path(const char *arg)
    409 {
    410 	if (strlen(arg) + 1 > sizeof(dtb_path))
    411 		return ERANGE;
    412 	strcpy(dtb_path, arg);
    413 	return 0;
    414 }
    415 
    416 char *
    417 get_dtb_path(void)
    418 {
    419 	return dtb_path;
    420 }
    421 
    422 int
    423 set_rndseed_path(const char *arg)
    424 {
    425 	if (strlen(arg) + 1 > sizeof(rndseed_path))
    426 		return ERANGE;
    427 	strcpy(rndseed_path, arg);
    428 	return 0;
    429 }
    430 
    431 char *
    432 get_rndseed_path(void)
    433 {
    434 	return rndseed_path;
    435 }
    436 
    437 int
    438 set_bootfile(const char *arg)
    439 {
    440 	if (strlen(arg) + 1 > sizeof(netbsd_path))
    441 		return ERANGE;
    442 	strcpy(netbsd_path, arg);
    443 	return 0;
    444 }
    445 
    446 int
    447 set_bootargs(const char *arg)
    448 {
    449 	if (strlen(arg) + 1 > sizeof(netbsd_args))
    450 		return ERANGE;
    451 	strcpy(netbsd_args, arg);
    452 	return 0;
    453 }
    454 
    455 void
    456 print_banner(void)
    457 {
    458 	printf("\n\n"
    459 	    ">> %s, Revision %s\n",
    460 	    bootprog_name, bootprog_rev);
    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