Home | History | Annotate | Line # | Download | only in efiboot
boot.c revision 1.9
      1  1.9  jmcneill /*	$NetBSD: boot.c,v 1.9 2018/09/09 18:00:20 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.8  jmcneill #include "efienv.h"
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/bootblock.h>
     36  1.1  jmcneill #include <sys/boot_flag.h>
     37  1.1  jmcneill #include <machine/limits.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <loadfile.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
     42  1.1  jmcneill 
     43  1.1  jmcneill extern char twiddle_toggle;
     44  1.1  jmcneill 
     45  1.1  jmcneill static const char * const names[][2] = {
     46  1.3  jmcneill 	{ "netbsd", "netbsd.gz" },
     47  1.3  jmcneill 	{ "onetbsd", "onetbsd.gz" },
     48  1.3  jmcneill 	{ "netbsd.old", "netbsd.old.gz" },
     49  1.1  jmcneill };
     50  1.1  jmcneill 
     51  1.1  jmcneill #define NUMNAMES	__arraycount(names)
     52  1.1  jmcneill #define DEFFILENAME	names[0][0]
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	DEFTIMEOUT	5
     55  1.1  jmcneill 
     56  1.3  jmcneill static char default_device[32];
     57  1.6  jmcneill static char initrd_path[255];
     58  1.7  jmcneill static char dtb_path[255];
     59  1.3  jmcneill 
     60  1.1  jmcneill void	command_boot(char *);
     61  1.3  jmcneill void	command_dev(char *);
     62  1.7  jmcneill void	command_dtb(char *);
     63  1.6  jmcneill void	command_initrd(char *);
     64  1.3  jmcneill void	command_ls(char *);
     65  1.8  jmcneill void	command_printenv(char *);
     66  1.8  jmcneill void	command_setenv(char *);
     67  1.8  jmcneill void	command_clearenv(char *);
     68  1.8  jmcneill void	command_resetenv(char *);
     69  1.1  jmcneill void	command_reset(char *);
     70  1.1  jmcneill void	command_version(char *);
     71  1.1  jmcneill void	command_quit(char *);
     72  1.1  jmcneill 
     73  1.1  jmcneill const struct boot_command commands[] = {
     74  1.6  jmcneill 	{ "boot",	command_boot,		"boot [dev:][filename] [args]\n     (ex. \"hd0a:\\netbsd.old -s\"" },
     75  1.3  jmcneill 	{ "dev",	command_dev,		"dev" },
     76  1.7  jmcneill 	{ "dtb",	command_dtb,		"dtb [dev:][filename]" },
     77  1.6  jmcneill 	{ "initrd",	command_initrd,		"initrd [dev:][filename]" },
     78  1.4  jmcneill 	{ "ls",		command_ls,		"ls [hdNn:/path]" },
     79  1.8  jmcneill 	{ "printenv",	command_printenv,	"printenv [key]" },
     80  1.8  jmcneill 	{ "setenv",	command_setenv,		"setenv <key> <value>" },
     81  1.8  jmcneill 	{ "clearenv",	command_clearenv,	"clearenv <key>" },
     82  1.8  jmcneill 	{ "resetenv",	command_resetenv,	"resetenv" },
     83  1.9  jmcneill 	{ "reboot",	command_reset,		"reboot|reset" },
     84  1.9  jmcneill 	{ "reset",	command_reset,		NULL },
     85  1.1  jmcneill 	{ "version",	command_version,	"version" },
     86  1.1  jmcneill 	{ "help",	command_help,		"help|?" },
     87  1.1  jmcneill 	{ "?",		command_help,		NULL },
     88  1.1  jmcneill 	{ "quit",	command_quit,		"quit" },
     89  1.1  jmcneill 	{ NULL,		NULL },
     90  1.1  jmcneill };
     91  1.1  jmcneill 
     92  1.1  jmcneill void
     93  1.1  jmcneill command_help(char *arg)
     94  1.1  jmcneill {
     95  1.1  jmcneill 	int n;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	printf("commands are:\n");
     98  1.1  jmcneill 	for (n = 0; commands[n].c_name; n++) {
     99  1.1  jmcneill 		if (commands[n].c_help)
    100  1.1  jmcneill 			printf("%s\n", commands[n].c_help);
    101  1.1  jmcneill 	}
    102  1.1  jmcneill }
    103  1.1  jmcneill 
    104  1.1  jmcneill void
    105  1.1  jmcneill command_boot(char *arg)
    106  1.1  jmcneill {
    107  1.1  jmcneill 	char *fname = arg;
    108  1.1  jmcneill 	char *bootargs = gettrailer(arg);
    109  1.1  jmcneill 
    110  1.1  jmcneill 	exec_netbsd(*fname ? fname : DEFFILENAME, bootargs);
    111  1.1  jmcneill }
    112  1.1  jmcneill 
    113  1.1  jmcneill void
    114  1.3  jmcneill command_dev(char *arg)
    115  1.3  jmcneill {
    116  1.3  jmcneill 	if (arg && *arg) {
    117  1.3  jmcneill 		set_default_device(arg);
    118  1.3  jmcneill 	} else {
    119  1.3  jmcneill 		efi_block_show();
    120  1.4  jmcneill 		efi_net_show();
    121  1.4  jmcneill 		efi_pxe_show();
    122  1.3  jmcneill 	}
    123  1.3  jmcneill 
    124  1.3  jmcneill 	if (strlen(default_device) > 0) {
    125  1.3  jmcneill 		printf("\n");
    126  1.3  jmcneill 		printf("default: %s\n", default_device);
    127  1.3  jmcneill 	}
    128  1.3  jmcneill }
    129  1.3  jmcneill 
    130  1.3  jmcneill void
    131  1.7  jmcneill command_dtb(char *arg)
    132  1.7  jmcneill {
    133  1.7  jmcneill 	set_dtb_path(arg);
    134  1.7  jmcneill }
    135  1.7  jmcneill 
    136  1.7  jmcneill void
    137  1.6  jmcneill command_initrd(char *arg)
    138  1.6  jmcneill {
    139  1.6  jmcneill 	set_initrd_path(arg);
    140  1.6  jmcneill }
    141  1.6  jmcneill 
    142  1.6  jmcneill void
    143  1.3  jmcneill command_ls(char *arg)
    144  1.3  jmcneill {
    145  1.3  jmcneill 	ls(arg);
    146  1.3  jmcneill }
    147  1.3  jmcneill 
    148  1.3  jmcneill void
    149  1.8  jmcneill command_printenv(char *arg)
    150  1.8  jmcneill {
    151  1.8  jmcneill 	char *val;
    152  1.8  jmcneill 
    153  1.8  jmcneill 	if (arg && *arg) {
    154  1.8  jmcneill 		val = efi_env_get(arg);
    155  1.8  jmcneill 		if (val) {
    156  1.8  jmcneill 			printf("\"%s\" = \"%s\"\n", arg, val);
    157  1.8  jmcneill 			FreePool(val);
    158  1.8  jmcneill 		}
    159  1.8  jmcneill 	} else {
    160  1.8  jmcneill 		efi_env_print();
    161  1.8  jmcneill 	}
    162  1.8  jmcneill }
    163  1.8  jmcneill 
    164  1.8  jmcneill void
    165  1.8  jmcneill command_setenv(char *arg)
    166  1.8  jmcneill {
    167  1.8  jmcneill 	char *spc;
    168  1.8  jmcneill 
    169  1.8  jmcneill 	spc = strchr(arg, ' ');
    170  1.8  jmcneill 	if (spc == NULL || spc[1] == '\0') {
    171  1.8  jmcneill 		command_help("");
    172  1.8  jmcneill 		return;
    173  1.8  jmcneill 	}
    174  1.8  jmcneill 
    175  1.8  jmcneill 	*spc = '\0';
    176  1.8  jmcneill 	efi_env_set(arg, spc + 1);
    177  1.8  jmcneill }
    178  1.8  jmcneill 
    179  1.8  jmcneill void
    180  1.8  jmcneill command_clearenv(char *arg)
    181  1.8  jmcneill {
    182  1.8  jmcneill 	if (*arg == '\0') {
    183  1.8  jmcneill 		command_help("");
    184  1.8  jmcneill 		return;
    185  1.8  jmcneill 	}
    186  1.8  jmcneill 	efi_env_clear(arg);
    187  1.8  jmcneill }
    188  1.8  jmcneill 
    189  1.8  jmcneill void
    190  1.8  jmcneill command_resetenv(char *arg)
    191  1.8  jmcneill {
    192  1.8  jmcneill 	efi_env_reset();
    193  1.8  jmcneill }
    194  1.8  jmcneill 
    195  1.8  jmcneill void
    196  1.1  jmcneill command_version(char *arg)
    197  1.1  jmcneill {
    198  1.1  jmcneill 	char *ufirmware;
    199  1.1  jmcneill 	int rv;
    200  1.1  jmcneill 
    201  1.1  jmcneill 	printf("EFI version: %d.%02d\n",
    202  1.1  jmcneill 	    ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
    203  1.1  jmcneill 	ufirmware = NULL;
    204  1.1  jmcneill 	rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
    205  1.1  jmcneill 	if (rv == 0) {
    206  1.1  jmcneill 		printf("EFI Firmware: %s (rev %d.%02d)\n", ufirmware,
    207  1.1  jmcneill 		    ST->FirmwareRevision >> 16,
    208  1.1  jmcneill 		    ST->FirmwareRevision & 0xffff);
    209  1.1  jmcneill 		FreePool(ufirmware);
    210  1.1  jmcneill 	}
    211  1.5  jmcneill 
    212  1.5  jmcneill 	efi_fdt_show();
    213  1.1  jmcneill }
    214  1.1  jmcneill 
    215  1.1  jmcneill void
    216  1.1  jmcneill command_quit(char *arg)
    217  1.1  jmcneill {
    218  1.1  jmcneill 	efi_exit();
    219  1.1  jmcneill }
    220  1.1  jmcneill 
    221  1.9  jmcneill void
    222  1.9  jmcneill command_reset(char *arg)
    223  1.9  jmcneill {
    224  1.9  jmcneill 	efi_reboot();
    225  1.9  jmcneill }
    226  1.9  jmcneill 
    227  1.3  jmcneill int
    228  1.3  jmcneill set_default_device(char *arg)
    229  1.3  jmcneill {
    230  1.3  jmcneill 	if (strlen(arg) + 1 > sizeof(default_device))
    231  1.3  jmcneill 		return ERANGE;
    232  1.3  jmcneill 	strcpy(default_device, arg);
    233  1.3  jmcneill 	return 0;
    234  1.3  jmcneill }
    235  1.3  jmcneill 
    236  1.3  jmcneill char *
    237  1.3  jmcneill get_default_device(void)
    238  1.3  jmcneill {
    239  1.3  jmcneill 	return default_device;
    240  1.3  jmcneill }
    241  1.3  jmcneill 
    242  1.6  jmcneill int
    243  1.6  jmcneill set_initrd_path(char *arg)
    244  1.6  jmcneill {
    245  1.6  jmcneill 	if (strlen(arg) + 1 > sizeof(initrd_path))
    246  1.6  jmcneill 		return ERANGE;
    247  1.6  jmcneill 	strcpy(initrd_path, arg);
    248  1.6  jmcneill 	return 0;
    249  1.6  jmcneill }
    250  1.6  jmcneill 
    251  1.6  jmcneill char *
    252  1.6  jmcneill get_initrd_path(void)
    253  1.6  jmcneill {
    254  1.6  jmcneill 	return initrd_path;
    255  1.6  jmcneill }
    256  1.6  jmcneill 
    257  1.7  jmcneill int
    258  1.7  jmcneill set_dtb_path(char *arg)
    259  1.7  jmcneill {
    260  1.7  jmcneill 	if (strlen(arg) + 1 > sizeof(dtb_path))
    261  1.7  jmcneill 		return ERANGE;
    262  1.7  jmcneill 	strcpy(dtb_path, arg);
    263  1.7  jmcneill 	return 0;
    264  1.7  jmcneill }
    265  1.7  jmcneill 
    266  1.7  jmcneill char *
    267  1.7  jmcneill get_dtb_path(void)
    268  1.7  jmcneill {
    269  1.7  jmcneill 	return dtb_path;
    270  1.7  jmcneill }
    271  1.7  jmcneill 
    272  1.1  jmcneill void
    273  1.1  jmcneill print_banner(void)
    274  1.1  jmcneill {
    275  1.1  jmcneill 	printf("\n\n"
    276  1.1  jmcneill 	    ">> %s, Revision %s (from NetBSD %s)\n",
    277  1.1  jmcneill 	    bootprog_name, bootprog_rev, bootprog_kernrev);
    278  1.1  jmcneill }
    279  1.1  jmcneill 
    280  1.8  jmcneill static void
    281  1.8  jmcneill read_env(void)
    282  1.8  jmcneill {
    283  1.8  jmcneill 	char *s;
    284  1.8  jmcneill 
    285  1.8  jmcneill 	s = efi_env_get("fdtfile");
    286  1.8  jmcneill 	if (s) {
    287  1.8  jmcneill #ifdef EFIBOOT_DEBUG
    288  1.8  jmcneill 		printf(">> Setting DTB path to '%s' from environment\n", s);
    289  1.8  jmcneill #endif
    290  1.8  jmcneill 		set_dtb_path(s);
    291  1.8  jmcneill 		FreePool(s);
    292  1.8  jmcneill 	}
    293  1.8  jmcneill 
    294  1.8  jmcneill 	s = efi_env_get("initrd");
    295  1.8  jmcneill 	if (s) {
    296  1.8  jmcneill #ifdef EFIBOOT_DEBUG
    297  1.8  jmcneill 		printf(">> Setting initrd path to '%s' from environment\n", s);
    298  1.8  jmcneill #endif
    299  1.8  jmcneill 		set_initrd_path(s);
    300  1.8  jmcneill 		FreePool(s);
    301  1.8  jmcneill 	}
    302  1.8  jmcneill 
    303  1.8  jmcneill 	s = efi_env_get("rootdev");
    304  1.8  jmcneill 	if (s) {
    305  1.8  jmcneill #ifdef EFIBOOT_DEBUG
    306  1.8  jmcneill 		printf(">> Setting default device to '%s' from environment\n", s);
    307  1.8  jmcneill #endif
    308  1.8  jmcneill 		set_default_device(s);
    309  1.8  jmcneill 		FreePool(s);
    310  1.8  jmcneill 	}
    311  1.8  jmcneill }
    312  1.8  jmcneill 
    313  1.1  jmcneill void
    314  1.1  jmcneill boot(void)
    315  1.1  jmcneill {
    316  1.1  jmcneill 	int currname, c;
    317  1.1  jmcneill 
    318  1.8  jmcneill 	read_env();
    319  1.8  jmcneill 
    320  1.1  jmcneill 	print_banner();
    321  1.1  jmcneill 
    322  1.1  jmcneill 	printf("Press return to boot now, any other key for boot prompt\n");
    323  1.1  jmcneill 	for (currname = 0; currname < NUMNAMES; currname++) {
    324  1.1  jmcneill 		printf("booting %s - starting in ", names[currname][0]);
    325  1.1  jmcneill 
    326  1.1  jmcneill 		c = awaitkey(DEFTIMEOUT, 1);
    327  1.1  jmcneill 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
    328  1.1  jmcneill 			bootprompt(); /* does not return */
    329  1.1  jmcneill 		}
    330  1.1  jmcneill 
    331  1.1  jmcneill 		/*
    332  1.1  jmcneill 		 * try pairs of names[] entries, foo and foo.gz
    333  1.1  jmcneill 		 */
    334  1.1  jmcneill 		exec_netbsd(names[currname][0], "");
    335  1.1  jmcneill 		exec_netbsd(names[currname][1], "");
    336  1.1  jmcneill 	}
    337  1.1  jmcneill 
    338  1.1  jmcneill 	bootprompt();	/* does not return */
    339  1.1  jmcneill }
    340