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