xref: /freebsd/usr.sbin/efibootmgr/efibootmgr.c (revision 65904399db9167b0970e42e14642e1d6bdbf6d3a)
11285bcc8SWarner Losh /*-
29ed08029SRebecca Cran  * Copyright (c) 2017-2018 Netflix, Inc.
31285bcc8SWarner Losh  *
41285bcc8SWarner Losh  * Redistribution and use in source and binary forms, with or without
51285bcc8SWarner Losh  * modification, are permitted provided that the following conditions
61285bcc8SWarner Losh  * are met:
71285bcc8SWarner Losh  * 1. Redistributions of source code must retain the above copyright
81285bcc8SWarner Losh  *    notice, this list of conditions and the following disclaimer
91285bcc8SWarner Losh  *    in this position and unchanged.
101285bcc8SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
111285bcc8SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
121285bcc8SWarner Losh  *    documentation and/or other materials provided with the distribution.
131285bcc8SWarner Losh  *
141285bcc8SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151285bcc8SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161285bcc8SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171285bcc8SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181285bcc8SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191285bcc8SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201285bcc8SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211285bcc8SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221285bcc8SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231285bcc8SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241285bcc8SWarner Losh  */
251285bcc8SWarner Losh 
261285bcc8SWarner Losh #include <sys/cdefs.h>
271285bcc8SWarner Losh #include <sys/stat.h>
281285bcc8SWarner Losh #include <sys/param.h>
291285bcc8SWarner Losh #include <assert.h>
301285bcc8SWarner Losh #include <ctype.h>
311285bcc8SWarner Losh #include <err.h>
321285bcc8SWarner Losh #include <errno.h>
331285bcc8SWarner Losh #include <fcntl.h>
341285bcc8SWarner Losh #include <libgeom.h>
351285bcc8SWarner Losh #include <paths.h>
361285bcc8SWarner Losh #include <signal.h>
371285bcc8SWarner Losh #include <stdint.h>
381285bcc8SWarner Losh #include <stdio.h>
391285bcc8SWarner Losh #include <stdlib.h>
401285bcc8SWarner Losh #include <getopt.h>
411285bcc8SWarner Losh #include <limits.h>
421285bcc8SWarner Losh #include <inttypes.h>
431285bcc8SWarner Losh #include <stdbool.h>
441285bcc8SWarner Losh #include <string.h>
451285bcc8SWarner Losh #include <strings.h>
461285bcc8SWarner Losh #include <unistd.h>
471285bcc8SWarner Losh #include <libgeom.h>
481285bcc8SWarner Losh #include <geom/geom.h>
491285bcc8SWarner Losh #include <geom/geom_ctl.h>
501285bcc8SWarner Losh #include <geom/geom_int.h>
511285bcc8SWarner Losh 
521285bcc8SWarner Losh #include <efivar.h>
531285bcc8SWarner Losh #include <efiutil.h>
541285bcc8SWarner Losh #include <efichar.h>
551285bcc8SWarner Losh #include <efivar-dp.h>
561285bcc8SWarner Losh 
571285bcc8SWarner Losh #ifndef LOAD_OPTION_ACTIVE
581285bcc8SWarner Losh #define LOAD_OPTION_ACTIVE 0x00000001
591285bcc8SWarner Losh #endif
601285bcc8SWarner Losh 
611285bcc8SWarner Losh #ifndef LOAD_OPTION_CATEGORY_BOOT
621285bcc8SWarner Losh #define LOAD_OPTION_CATEGORY_BOOT 0x00000000
631285bcc8SWarner Losh #endif
641285bcc8SWarner Losh 
651285bcc8SWarner Losh #define BAD_LENGTH	((size_t)-1)
661285bcc8SWarner Losh 
6783c42372SD Scott Phillips #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001
6883c42372SD Scott Phillips 
691285bcc8SWarner Losh typedef struct _bmgr_opts {
709a791529SWarner Losh 	char	*dev;
711285bcc8SWarner Losh 	char	*env;
721285bcc8SWarner Losh 	char	*loader;
731285bcc8SWarner Losh 	char	*label;
741285bcc8SWarner Losh 	char	*kernel;
751285bcc8SWarner Losh 	char	*name;
761285bcc8SWarner Losh 	char	*order;
771285bcc8SWarner Losh 	int     bootnum;
781285bcc8SWarner Losh 	bool	copy;
791285bcc8SWarner Losh 	bool    create;
801285bcc8SWarner Losh 	bool    delete;
811285bcc8SWarner Losh 	bool    delete_bootnext;
821285bcc8SWarner Losh 	bool    del_timeout;
831285bcc8SWarner Losh 	bool    dry_run;
841cdb8eb8SWarner Losh 	bool	device_path;
851cdb8eb8SWarner Losh 	bool	esp_device;
869a791529SWarner Losh 	bool	find_dev;
8783c42372SD Scott Phillips 	bool    fw_ui;
8883c42372SD Scott Phillips 	bool    no_fw_ui;
89de26ba4dSWarner Losh 	bool	has_bootnum;
901285bcc8SWarner Losh 	bool    once;
911285bcc8SWarner Losh 	int	cp_src;
921285bcc8SWarner Losh 	bool    set_active;
931285bcc8SWarner Losh 	bool    set_bootnext;
941285bcc8SWarner Losh 	bool    set_inactive;
951285bcc8SWarner Losh 	bool    set_timeout;
961285bcc8SWarner Losh 	int     timeout;
971cdb8eb8SWarner Losh 	bool	unix_path;
981285bcc8SWarner Losh 	bool    verbose;
991285bcc8SWarner Losh } bmgr_opts_t;
1001285bcc8SWarner Losh 
1011285bcc8SWarner Losh static struct option lopts[] = {
1029ed08029SRebecca Cran 	{"activate", no_argument, NULL, 'a'},
1039ed08029SRebecca Cran 	{"bootnext", no_argument, NULL, 'n'}, /* set bootnext */
104a2581e80SRebecca Cran 	{"bootnum", required_argument, NULL, 'b'},
1051285bcc8SWarner Losh 	{"bootorder", required_argument, NULL, 'o'}, /* set order */
1061285bcc8SWarner Losh 	{"copy", required_argument, NULL, 'C'},		/* Copy boot method */
1071285bcc8SWarner Losh 	{"create", no_argument, NULL, 'c'},
1089ed08029SRebecca Cran 	{"deactivate", no_argument, NULL, 'A'},
1096b8b1bfeSToomas Soome 	{"del-timeout", no_argument, NULL, 'T'},
1109ed08029SRebecca Cran 	{"delete", no_argument, NULL, 'B'},
1119ed08029SRebecca Cran 	{"delete-bootnext", no_argument, NULL, 'N'},
1121cdb8eb8SWarner Losh 	{"device-path", no_argument, NULL, 'd'},
1131285bcc8SWarner Losh 	{"dry-run", no_argument, NULL, 'D'},
1141285bcc8SWarner Losh 	{"env", required_argument, NULL, 'e'},
1151cdb8eb8SWarner Losh 	{"esp", no_argument, NULL, 'E'},
1169a791529SWarner Losh 	{"efidev", required_argument, NULL, 'u'},
11783c42372SD Scott Phillips 	{"fw-ui", no_argument, NULL, 'f'},
11883c42372SD Scott Phillips 	{"no-fw-ui", no_argument, NULL, 'F'},
1191285bcc8SWarner Losh 	{"help", no_argument, NULL, 'h'},
1201285bcc8SWarner Losh 	{"kernel", required_argument, NULL, 'k'},
1211285bcc8SWarner Losh 	{"label", required_argument, NULL, 'L'},
1221285bcc8SWarner Losh 	{"loader", required_argument, NULL, 'l'},
1231285bcc8SWarner Losh 	{"once", no_argument, NULL, 'O'},
1241285bcc8SWarner Losh 	{"set-timeout", required_argument, NULL, 't'},
1251cdb8eb8SWarner Losh 	{"unix-path", no_argument, NULL, 'p'},
1261285bcc8SWarner Losh 	{"verbose", no_argument, NULL, 'v'},
1271285bcc8SWarner Losh 	{ NULL, 0, NULL, 0}
1281285bcc8SWarner Losh };
1291285bcc8SWarner Losh 
1301285bcc8SWarner Losh /* global efibootmgr opts */
1311285bcc8SWarner Losh static bmgr_opts_t opts;
1321285bcc8SWarner Losh 
1331285bcc8SWarner Losh static LIST_HEAD(efivars_head, entry) efivars =
1341285bcc8SWarner Losh 	LIST_HEAD_INITIALIZER(efivars);
1351285bcc8SWarner Losh 
1361285bcc8SWarner Losh struct entry {
1371285bcc8SWarner Losh 	efi_guid_t	guid;
1381285bcc8SWarner Losh 	uint32_t	attrs;
1391285bcc8SWarner Losh 	uint8_t		*data;
1401285bcc8SWarner Losh 	size_t		size;
1411285bcc8SWarner Losh 	char		*name;
1421285bcc8SWarner Losh 	char		*label;
1431285bcc8SWarner Losh 	int		idx;
14451922c69SWarner Losh 	int		flags;
14551922c69SWarner Losh #define SEEN	1
1461285bcc8SWarner Losh 
1471285bcc8SWarner Losh 	LIST_ENTRY(entry) entries;
1481285bcc8SWarner Losh };
1491285bcc8SWarner Losh 
1501285bcc8SWarner Losh #define MAX_DP_LEN	4096
1511285bcc8SWarner Losh #define MAX_LOADOPT_LEN	8192
1521285bcc8SWarner Losh 
1531285bcc8SWarner Losh 
1541285bcc8SWarner Losh static char *
mangle_loader(char * loader)1551285bcc8SWarner Losh mangle_loader(char *loader)
1561285bcc8SWarner Losh {
1571285bcc8SWarner Losh 	char *c;
1581285bcc8SWarner Losh 
1591285bcc8SWarner Losh 	for (c = loader; *c; c++)
1601285bcc8SWarner Losh 		if (*c == '/')
1611285bcc8SWarner Losh 			*c = '\\';
1621285bcc8SWarner Losh 
1631285bcc8SWarner Losh 	return loader;
1641285bcc8SWarner Losh }
1651285bcc8SWarner Losh 
1661285bcc8SWarner Losh 
1671285bcc8SWarner Losh #define COMMON_ATTRS EFI_VARIABLE_NON_VOLATILE | \
1681285bcc8SWarner Losh 	EFI_VARIABLE_BOOTSERVICE_ACCESS | \
1691285bcc8SWarner Losh 	EFI_VARIABLE_RUNTIME_ACCESS
1701285bcc8SWarner Losh 
1711285bcc8SWarner Losh /*
1721285bcc8SWarner Losh  * We use global guid, and common var attrs and
1731285bcc8SWarner Losh  * find it better to just delete and re-create a var.
1741285bcc8SWarner Losh  */
1751285bcc8SWarner Losh static int
set_bootvar(const char * name,uint8_t * data,size_t size)1761285bcc8SWarner Losh set_bootvar(const char *name, uint8_t *data, size_t size)
1771285bcc8SWarner Losh {
1781285bcc8SWarner Losh 
1791285bcc8SWarner Losh 	return efi_set_variable(EFI_GLOBAL_GUID, name, data, size,
1801285bcc8SWarner Losh 	    COMMON_ATTRS);
1811285bcc8SWarner Losh }
1821285bcc8SWarner Losh 
1831285bcc8SWarner Losh 
1841285bcc8SWarner Losh #define USAGE \
1859ed08029SRebecca Cran 	"   [-aAnB -b bootnum] [-N] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help]\n\
1869ed08029SRebecca Cran   [-c -l loader [-k kernel] [-L label] [--dry-run] [-b bootnum]]"
1871285bcc8SWarner Losh 
1881285bcc8SWarner Losh #define CREATE_USAGE \
1899ed08029SRebecca Cran 	"       efibootmgr -c -l loader [-k kernel] [-L label] [--dry-run] [-b bootnum] [-a]"
1901285bcc8SWarner Losh #define ORDER_USAGE \
1911285bcc8SWarner Losh 	"       efibootmgr -o bootvarnum1,bootvarnum2,..."
1921285bcc8SWarner Losh #define TIMEOUT_USAGE \
1931285bcc8SWarner Losh 	"       efibootmgr -t seconds"
1941285bcc8SWarner Losh #define DELETE_USAGE \
1959ed08029SRebecca Cran 	"       efibootmgr -B -b bootnum"
1961285bcc8SWarner Losh #define ACTIVE_USAGE \
1979ed08029SRebecca Cran 	"       efibootmgr [-a | -A] -b bootnum"
1981285bcc8SWarner Losh #define BOOTNEXT_USAGE \
1999ed08029SRebecca Cran 	"       efibootmgr [-n | -N] -b bootnum"
2001285bcc8SWarner Losh 
2011285bcc8SWarner Losh static void
parse_args(int argc,char * argv[])2021285bcc8SWarner Losh parse_args(int argc, char *argv[])
2031285bcc8SWarner Losh {
2041285bcc8SWarner Losh 	int ch;
20598d2608aSWarner Losh 	const char *arg;
2061285bcc8SWarner Losh 
207*65904399SMark Peek 	while ((ch = getopt_long(argc, argv,
208*65904399SMark Peek 	    "AaBb:C:cdDe:EFfhk:L:l:NnOo:pTt:u:v", lopts, NULL)) != -1) {
2091285bcc8SWarner Losh 		switch (ch) {
2101285bcc8SWarner Losh 		case 'A':
2111285bcc8SWarner Losh 			opts.set_inactive = true;
2121285bcc8SWarner Losh 			break;
2131285bcc8SWarner Losh 		case 'a':
2141285bcc8SWarner Losh 			opts.set_active = true;
2151285bcc8SWarner Losh 			break;
216de26ba4dSWarner Losh 		case 'b':
217de26ba4dSWarner Losh 			opts.has_bootnum = true;
21898d2608aSWarner Losh 			arg = optarg;
21998d2608aSWarner Losh 			if (strncasecmp(arg, "boot", 4) == 0)
22098d2608aSWarner Losh 				arg += 4;
22198d2608aSWarner Losh 			opts.bootnum = strtoul(arg, NULL, 16);
222de26ba4dSWarner Losh 			break;
2231285bcc8SWarner Losh 		case 'B':
2241285bcc8SWarner Losh 			opts.delete = true;
2251285bcc8SWarner Losh 			break;
2261285bcc8SWarner Losh 		case 'C':
2271285bcc8SWarner Losh 			opts.copy = true;
2281285bcc8SWarner Losh 			opts.cp_src = strtoul(optarg, NULL, 16);
229731f91c0SWarner Losh 			errx(1, "Copy not implemented");
230731f91c0SWarner Losh 			break;
2311285bcc8SWarner Losh 		case 'c':
2321285bcc8SWarner Losh 			opts.create = true;
2331285bcc8SWarner Losh 			break;
2341285bcc8SWarner Losh 		case 'D': /* should be remove dups XXX */
2351285bcc8SWarner Losh 			opts.dry_run = true;
2361285bcc8SWarner Losh 			break;
2371cdb8eb8SWarner Losh 		case 'd':
2381cdb8eb8SWarner Losh 			opts.device_path = true;
2391cdb8eb8SWarner Losh 			break;
2401285bcc8SWarner Losh 		case 'e':
24163b56694SWarner Losh 			free(opts.env);
2421285bcc8SWarner Losh 			opts.env = strdup(optarg);
2431285bcc8SWarner Losh 			break;
2441cdb8eb8SWarner Losh 		case 'E':
2451cdb8eb8SWarner Losh 			opts.esp_device = true;
2461cdb8eb8SWarner Losh 			break;
24783c42372SD Scott Phillips 		case 'F':
24883c42372SD Scott Phillips 			opts.no_fw_ui = true;
24983c42372SD Scott Phillips 			break;
25083c42372SD Scott Phillips 		case 'f':
25183c42372SD Scott Phillips 			opts.fw_ui = true;
25283c42372SD Scott Phillips 			break;
2531285bcc8SWarner Losh 		case 'h':
2541285bcc8SWarner Losh 		default:
2551285bcc8SWarner Losh 			errx(1, "%s", USAGE);
2561285bcc8SWarner Losh 			break;
2571285bcc8SWarner Losh 		case 'k':
25863b56694SWarner Losh 			free(opts.kernel);
2591285bcc8SWarner Losh 			opts.kernel = strdup(optarg);
2601285bcc8SWarner Losh 			break;
2611285bcc8SWarner Losh 		case 'L':
26263b56694SWarner Losh 			free(opts.label);
2631285bcc8SWarner Losh 			opts.label = strdup(optarg);
2641285bcc8SWarner Losh 			break;
2651285bcc8SWarner Losh 		case 'l':
26663b56694SWarner Losh 			free(opts.loader);
2671285bcc8SWarner Losh 			opts.loader = strdup(optarg);
2681285bcc8SWarner Losh 			opts.loader = mangle_loader(opts.loader);
2691285bcc8SWarner Losh 			break;
2701285bcc8SWarner Losh 		case 'N':
2711285bcc8SWarner Losh 			opts.delete_bootnext = true;
2721285bcc8SWarner Losh 			break;
2731285bcc8SWarner Losh 		case 'n':
2741285bcc8SWarner Losh 			opts.set_bootnext = true;
2751285bcc8SWarner Losh 			break;
2761285bcc8SWarner Losh 		case 'O':
2771285bcc8SWarner Losh 			opts.once = true;
2781285bcc8SWarner Losh 			break;
2791285bcc8SWarner Losh 		case 'o':
28063b56694SWarner Losh 			free(opts.order);
2811285bcc8SWarner Losh 			opts.order = strdup(optarg);
2821285bcc8SWarner Losh 			break;
2831cdb8eb8SWarner Losh 		case 'p':
2841cdb8eb8SWarner Losh 			opts.unix_path = true;
2851cdb8eb8SWarner Losh 			break;
2861285bcc8SWarner Losh 		case 'T':
2871285bcc8SWarner Losh 			opts.del_timeout = true;
2881285bcc8SWarner Losh 			break;
2891285bcc8SWarner Losh 		case 't':
2901285bcc8SWarner Losh 			opts.set_timeout = true;
2911285bcc8SWarner Losh 			opts.timeout = strtoul(optarg, NULL, 10);
2921285bcc8SWarner Losh 			break;
2939a791529SWarner Losh 		case 'u':
2949a791529SWarner Losh 			opts.find_dev = true;
2959a791529SWarner Losh 			opts.dev = strdup(optarg);
29617a5a290SEd Maste 			break;
2971285bcc8SWarner Losh 		case 'v':
2981285bcc8SWarner Losh 			opts.verbose = true;
2991285bcc8SWarner Losh 			break;
3001285bcc8SWarner Losh 		}
3011285bcc8SWarner Losh 	}
3021285bcc8SWarner Losh 	if (opts.create) {
3031127aea3SWarner Losh 		if (!opts.loader)
3041285bcc8SWarner Losh 			errx(1, "%s",CREATE_USAGE);
3051285bcc8SWarner Losh 		return;
3061285bcc8SWarner Losh 	}
3071285bcc8SWarner Losh 
308b9580775SToomas Soome 	if (opts.order != NULL && *opts.order == '\0')
3091285bcc8SWarner Losh 		errx(1, "%s", ORDER_USAGE);
3109ed08029SRebecca Cran 
3119ed08029SRebecca Cran 	if ((opts.set_inactive || opts.set_active) && !opts.has_bootnum)
3129ed08029SRebecca Cran 		errx(1, "%s", ACTIVE_USAGE);
3139ed08029SRebecca Cran 
3149ed08029SRebecca Cran 	if (opts.delete && !opts.has_bootnum)
3159ed08029SRebecca Cran 		errx(1, "%s", DELETE_USAGE);
3169ed08029SRebecca Cran 
3179ed08029SRebecca Cran 	if (opts.set_bootnext && !opts.has_bootnum)
3189ed08029SRebecca Cran 		errx(1, "%s", BOOTNEXT_USAGE);
3191285bcc8SWarner Losh }
3201285bcc8SWarner Losh 
3211285bcc8SWarner Losh 
3221285bcc8SWarner Losh static void
read_vars(void)3231285bcc8SWarner Losh read_vars(void)
3241285bcc8SWarner Losh {
3251285bcc8SWarner Losh 
3261285bcc8SWarner Losh 	efi_guid_t *guid;
3271285bcc8SWarner Losh 	char *next_name = NULL;
3281285bcc8SWarner Losh 	int ret = 0;
3291285bcc8SWarner Losh 
3301285bcc8SWarner Losh 	struct entry *nent;
3311285bcc8SWarner Losh 
3321285bcc8SWarner Losh 	LIST_INIT(&efivars);
3331285bcc8SWarner Losh 	while ((ret = efi_get_next_variable_name(&guid, &next_name)) > 0) {
3341285bcc8SWarner Losh 		/*
335a3e6c4a6SWarner Losh 		 * Only pay attention to EFI:BootXXXX variables to get the list.
3361285bcc8SWarner Losh 		 */
3371285bcc8SWarner Losh 		if (efi_guid_cmp(guid, &EFI_GLOBAL_GUID) != 0 ||
3381285bcc8SWarner Losh 		    strlen(next_name) != 8 ||
3391285bcc8SWarner Losh 		    strncmp(next_name, "Boot", 4) != 0 ||
3401285bcc8SWarner Losh 		    !isxdigit(next_name[4]) ||
3411285bcc8SWarner Losh 		    !isxdigit(next_name[5]) ||
3421285bcc8SWarner Losh 		    !isxdigit(next_name[6]) ||
3431285bcc8SWarner Losh 		    !isxdigit(next_name[7]))
3441285bcc8SWarner Losh 			continue;
3451285bcc8SWarner Losh 		nent = malloc(sizeof(struct entry));
3461285bcc8SWarner Losh 		nent->name = strdup(next_name);
3471285bcc8SWarner Losh 
3481285bcc8SWarner Losh 		ret = efi_get_variable(*guid, next_name, &nent->data,
3491285bcc8SWarner Losh 		    &nent->size, &nent->attrs);
3501285bcc8SWarner Losh 		if (ret < 0)
3511285bcc8SWarner Losh 			err(1, "efi_get_variable");
3521285bcc8SWarner Losh 		nent->guid = *guid;
3531285bcc8SWarner Losh 		nent->idx = strtoul(&next_name[4], NULL, 16);
3541285bcc8SWarner Losh 		LIST_INSERT_HEAD(&efivars, nent, entries);
3551285bcc8SWarner Losh 	}
3561285bcc8SWarner Losh }
3571285bcc8SWarner Losh 
3581285bcc8SWarner Losh 
3591285bcc8SWarner Losh static void
set_boot_order(char * order)3601285bcc8SWarner Losh set_boot_order(char *order)
3611285bcc8SWarner Losh {
3621285bcc8SWarner Losh 	uint16_t *new_data;
3631285bcc8SWarner Losh 	size_t size;
3641285bcc8SWarner Losh 	char *next, *cp;
3651285bcc8SWarner Losh 	int cnt;
3661285bcc8SWarner Losh 	int i;
3671285bcc8SWarner Losh 
3681285bcc8SWarner Losh 	cp = order;
3691285bcc8SWarner Losh 	cnt = 1;
3701285bcc8SWarner Losh 	while (*cp) {
3711285bcc8SWarner Losh 		if (*cp++ == ',')
3721285bcc8SWarner Losh 			cnt++;
3731285bcc8SWarner Losh 	}
3741285bcc8SWarner Losh 	size = sizeof(uint16_t) * cnt;
3751285bcc8SWarner Losh 	new_data = malloc(size);
3761285bcc8SWarner Losh 
3771285bcc8SWarner Losh 	i = 0;
3781285bcc8SWarner Losh 	cp = strdup(order);
3791285bcc8SWarner Losh 	while ((next = strsep(&cp, ",")) != NULL) {
3801285bcc8SWarner Losh 		new_data[i] = strtoul(next, NULL, 16);
3811285bcc8SWarner Losh 		if (new_data[i] == 0 && errno == EINVAL) {
3821285bcc8SWarner Losh 			warnx("can't parse %s as a numb", next);
3831285bcc8SWarner Losh 			errx(1, "%s", ORDER_USAGE);
3841285bcc8SWarner Losh 		}
3851285bcc8SWarner Losh 		i++;
3861285bcc8SWarner Losh 	}
3871285bcc8SWarner Losh 	free(cp);
3881285bcc8SWarner Losh 	if (set_bootvar("BootOrder", (uint8_t*)new_data, size) < 0)
3891285bcc8SWarner Losh 		err(1, "Unabke to set BootOrder to %s", order);
3903af42ca7SWarner Losh 	free(new_data);
3911285bcc8SWarner Losh }
3921285bcc8SWarner Losh 
3931285bcc8SWarner Losh static void
handle_activity(int bootnum,bool active)3941285bcc8SWarner Losh handle_activity(int bootnum, bool active)
3951285bcc8SWarner Losh {
3961285bcc8SWarner Losh 	uint32_t attrs, load_attrs;
3971285bcc8SWarner Losh 	uint8_t *data;
3981285bcc8SWarner Losh 	size_t size;
3991285bcc8SWarner Losh 	char *name;
4001285bcc8SWarner Losh 
4011285bcc8SWarner Losh 	asprintf(&name, "%s%04X", "Boot", bootnum);
4021285bcc8SWarner Losh 	if (name == NULL)
4031285bcc8SWarner Losh 		err(1, "asprintf");
4041285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, &attrs) < 0)
4051285bcc8SWarner Losh 		err(1, "No such bootvar %s\n", name);
4061285bcc8SWarner Losh 
4071285bcc8SWarner Losh 	load_attrs = le32dec(data);
4081285bcc8SWarner Losh 
4091285bcc8SWarner Losh 	if (active)
4101285bcc8SWarner Losh 		load_attrs |= LOAD_OPTION_ACTIVE;
4111285bcc8SWarner Losh 	else
4121285bcc8SWarner Losh 		load_attrs &= ~LOAD_OPTION_ACTIVE;
4131285bcc8SWarner Losh 
4141285bcc8SWarner Losh 	le32enc(data, load_attrs);
4151285bcc8SWarner Losh 
4161285bcc8SWarner Losh 	if (set_bootvar(name, data, size) < 0)
4171285bcc8SWarner Losh 		err(1, "handle activity efi_set_variable");
4181285bcc8SWarner Losh }
4191285bcc8SWarner Losh 
4201285bcc8SWarner Losh 
4211285bcc8SWarner Losh /*
4221285bcc8SWarner Losh  * add boot var to boot order.
4231285bcc8SWarner Losh  * called by create boot var. There is no option
4241285bcc8SWarner Losh  * to add one independent of create.
4251285bcc8SWarner Losh  *
4261285bcc8SWarner Losh  * Note: we currently don't support where it goes
4271285bcc8SWarner Losh  * so it goes on the front, inactive.
4281285bcc8SWarner Losh  * use -o 2,3,7 etc to affect order, -a to activate.
4291285bcc8SWarner Losh  */
4301285bcc8SWarner Losh static void
add_to_boot_order(char * bootvar)4311285bcc8SWarner Losh add_to_boot_order(char *bootvar)
4321285bcc8SWarner Losh {
4331285bcc8SWarner Losh 	size_t size;
4341285bcc8SWarner Losh 	uint32_t attrs;
4351285bcc8SWarner Losh 	uint16_t val;
4361285bcc8SWarner Losh 	uint8_t *data, *new;
4371285bcc8SWarner Losh 
4381285bcc8SWarner Losh 	val = strtoul(&bootvar[4], NULL, 16);
4391285bcc8SWarner Losh 
4401285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) {
4411285bcc8SWarner Losh 		if (errno == ENOENT) { /* create it and set this bootvar to active */
4421285bcc8SWarner Losh 			size = 0;
4431285bcc8SWarner Losh 			data = NULL;
4441285bcc8SWarner Losh 		} else
4451285bcc8SWarner Losh 			err(1, "efi_get_variable BootOrder");
4461285bcc8SWarner Losh 	}
4471285bcc8SWarner Losh 
4481285bcc8SWarner Losh 	/*
4491285bcc8SWarner Losh 	 * We have BootOrder with the current order
4501285bcc8SWarner Losh 	 * so grow the array by one, add the value
4511285bcc8SWarner Losh 	 * and write the new variable value.
4521285bcc8SWarner Losh 	 */
4531285bcc8SWarner Losh 	size += sizeof(uint16_t);
4541285bcc8SWarner Losh 	new = malloc(size);
4551285bcc8SWarner Losh 	if (!new)
4561285bcc8SWarner Losh 		err(1, "malloc");
4571285bcc8SWarner Losh 
4581285bcc8SWarner Losh 	le16enc(new, val);
4591285bcc8SWarner Losh 	if (size > sizeof(uint16_t))
4601285bcc8SWarner Losh 		memcpy(new + sizeof(uint16_t), data, size - sizeof(uint16_t));
4611285bcc8SWarner Losh 
4621285bcc8SWarner Losh 	if (set_bootvar("BootOrder", new, size) < 0)
4631285bcc8SWarner Losh 		err(1, "set_bootvar");
4641285bcc8SWarner Losh 	free(new);
4651285bcc8SWarner Losh }
4661285bcc8SWarner Losh 
4671285bcc8SWarner Losh 
4681285bcc8SWarner Losh static void
remove_from_order(uint16_t bootnum)4691285bcc8SWarner Losh remove_from_order(uint16_t bootnum)
4701285bcc8SWarner Losh {
4711285bcc8SWarner Losh 	uint32_t attrs;
4721285bcc8SWarner Losh 	size_t size, i, j;
4731285bcc8SWarner Losh 	uint8_t *new, *data;
4741285bcc8SWarner Losh 
4751285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0)
4761285bcc8SWarner Losh 		return;
4771285bcc8SWarner Losh 
4781285bcc8SWarner Losh 	new = malloc(size);
4791285bcc8SWarner Losh 	if (new == NULL)
4801285bcc8SWarner Losh 		err(1, "malloc");
4811285bcc8SWarner Losh 
4821285bcc8SWarner Losh 	for (j = i = 0; i < size; i += sizeof(uint16_t)) {
4831285bcc8SWarner Losh 		if (le16dec(data + i) == bootnum)
4841285bcc8SWarner Losh 			continue;
4851285bcc8SWarner Losh 		memcpy(new + j, data + i, sizeof(uint16_t));
4861285bcc8SWarner Losh 		j += sizeof(uint16_t);
4871285bcc8SWarner Losh 	}
4881285bcc8SWarner Losh 	if (i == j)
4891285bcc8SWarner Losh 		warnx("Boot variable %04x not in BootOrder", bootnum);
4901285bcc8SWarner Losh 	else if (set_bootvar("BootOrder", new, j) < 0)
4911285bcc8SWarner Losh 		err(1, "Unable to update BootOrder with new value");
4921285bcc8SWarner Losh 	free(new);
4931285bcc8SWarner Losh }
4941285bcc8SWarner Losh 
4951285bcc8SWarner Losh 
4961285bcc8SWarner Losh static void
delete_bootvar(int bootnum)4971285bcc8SWarner Losh delete_bootvar(int bootnum)
4981285bcc8SWarner Losh {
4991285bcc8SWarner Losh 	char *name;
5001285bcc8SWarner Losh 	int defer = 0;
5011285bcc8SWarner Losh 
5021285bcc8SWarner Losh 	/*
5031285bcc8SWarner Losh 	 * Try to delete the boot variable and remocve it
5041285bcc8SWarner Losh 	 * from the boot order. We always do both actions
5051285bcc8SWarner Losh 	 * to make it easy to clean up from oopses.
5061285bcc8SWarner Losh 	 */
5071285bcc8SWarner Losh 	if (bootnum < 0 || bootnum > 0xffff)
5081285bcc8SWarner Losh 		errx(1, "Bad boot variable %#x", bootnum);
5091285bcc8SWarner Losh 	asprintf(&name, "%s%04X", "Boot", bootnum);
5101285bcc8SWarner Losh 	if (name == NULL)
5111285bcc8SWarner Losh 		err(1, "asprintf");
5121285bcc8SWarner Losh 	printf("Removing boot variable '%s'\n", name);
5131285bcc8SWarner Losh 	if (efi_del_variable(EFI_GLOBAL_GUID, name) < 0) {
5141285bcc8SWarner Losh 		defer = 1;
5151285bcc8SWarner Losh 		warn("cannot delete variable %s", name);
5161285bcc8SWarner Losh 	}
5171285bcc8SWarner Losh 	printf("Removing 0x%x from BootOrder\n", bootnum);
5181285bcc8SWarner Losh 	remove_from_order(bootnum);
5191285bcc8SWarner Losh 	free(name);
5201285bcc8SWarner Losh 	if (defer)
5211285bcc8SWarner Losh 		exit(defer);
5221285bcc8SWarner Losh }
5231285bcc8SWarner Losh 
5241285bcc8SWarner Losh 
5251285bcc8SWarner Losh static void
del_bootnext(void)5261285bcc8SWarner Losh del_bootnext(void)
5271285bcc8SWarner Losh {
5281285bcc8SWarner Losh 
5291285bcc8SWarner Losh 	if (efi_del_variable(EFI_GLOBAL_GUID, "BootNext") < 0)
5301285bcc8SWarner Losh 		err(1, "efi_del_variable");
5311285bcc8SWarner Losh }
5321285bcc8SWarner Losh 
5331285bcc8SWarner Losh static void
handle_bootnext(uint16_t bootnum)5341285bcc8SWarner Losh handle_bootnext(uint16_t bootnum)
5351285bcc8SWarner Losh {
5361285bcc8SWarner Losh 	uint16_t num;
5371285bcc8SWarner Losh 
5381285bcc8SWarner Losh 	le16enc(&num, bootnum);
5391285bcc8SWarner Losh 	if (set_bootvar("BootNext", (uint8_t*)&bootnum, sizeof(uint16_t)) < 0)
5401285bcc8SWarner Losh 		err(1, "set_bootvar");
5411285bcc8SWarner Losh }
5421285bcc8SWarner Losh 
5431285bcc8SWarner Losh 
5441285bcc8SWarner Losh static int
compare(const void * a,const void * b)5451285bcc8SWarner Losh compare(const void *a, const void *b)
5461285bcc8SWarner Losh {
5471285bcc8SWarner Losh 	uint16_t c;
5481285bcc8SWarner Losh 	uint16_t d;
5491285bcc8SWarner Losh 
5501285bcc8SWarner Losh 	memcpy(&c, a, sizeof(uint16_t));
5511285bcc8SWarner Losh 	memcpy(&d, b, sizeof(uint16_t));
5521285bcc8SWarner Losh 
5531285bcc8SWarner Losh 	if (c < d)
5541285bcc8SWarner Losh 		return -1;
5551285bcc8SWarner Losh 	if (c == d)
5561285bcc8SWarner Losh 		return  0;
5571285bcc8SWarner Losh 	return  1;
5581285bcc8SWarner Losh }
5591285bcc8SWarner Losh 
5601285bcc8SWarner Losh static char *
make_next_boot_var_name(void)5616604afe9SWarner Losh make_next_boot_var_name(void)
5621285bcc8SWarner Losh {
5631285bcc8SWarner Losh 	struct entry *v;
56409cb8031SJessica Clarke 	uint16_t *vals;
5651285bcc8SWarner Losh 	char *name;
5661285bcc8SWarner Losh 	int cnt = 0;
5671285bcc8SWarner Losh 	int i;
5681285bcc8SWarner Losh 
5691285bcc8SWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
5701285bcc8SWarner Losh 		cnt++;
5711285bcc8SWarner Losh 	}
5721285bcc8SWarner Losh 
5731285bcc8SWarner Losh 	vals = malloc(sizeof(uint16_t) * cnt);
5741285bcc8SWarner Losh 	if (!vals)
5751285bcc8SWarner Losh 		return NULL;
5761285bcc8SWarner Losh 
5771285bcc8SWarner Losh 	i = 0;
5781285bcc8SWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
5791285bcc8SWarner Losh 		vals[i++] = v->idx;
5801285bcc8SWarner Losh 	}
5811285bcc8SWarner Losh 	qsort(vals, cnt, sizeof(uint16_t), compare);
58209cb8031SJessica Clarke 	/* Find the first hole (could be at start or end) */
58309cb8031SJessica Clarke 	for (i = 0; i < cnt; ++i)
58409cb8031SJessica Clarke 		if (vals[i] != i)
58509cb8031SJessica Clarke 			break;
5861285bcc8SWarner Losh 	free(vals);
58709cb8031SJessica Clarke 	/* In theory we could have used all 65k slots -- what to do? */
5881285bcc8SWarner Losh 
58909cb8031SJessica Clarke 	asprintf(&name, "%s%04X", "Boot", i);
5901285bcc8SWarner Losh 	if (name == NULL)
5911285bcc8SWarner Losh 		err(1, "asprintf");
5921285bcc8SWarner Losh 	return name;
5931285bcc8SWarner Losh }
5941285bcc8SWarner Losh 
595de26ba4dSWarner Losh static char *
make_boot_var_name(uint16_t bootnum)596de26ba4dSWarner Losh make_boot_var_name(uint16_t bootnum)
597de26ba4dSWarner Losh {
598de26ba4dSWarner Losh 	struct entry *v;
599de26ba4dSWarner Losh 	char *name;
600de26ba4dSWarner Losh 
601de26ba4dSWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
602de26ba4dSWarner Losh 		if (v->idx == bootnum)
603de26ba4dSWarner Losh 			return NULL;
604de26ba4dSWarner Losh 	}
605de26ba4dSWarner Losh 
606de26ba4dSWarner Losh 	asprintf(&name, "%s%04X", "Boot", bootnum);
607de26ba4dSWarner Losh 	if (name == NULL)
608de26ba4dSWarner Losh 		err(1, "asprintf");
609de26ba4dSWarner Losh 	return name;
610de26ba4dSWarner Losh }
6111285bcc8SWarner Losh 
6121285bcc8SWarner Losh static size_t
create_loadopt(uint8_t * buf,size_t bufmax,uint32_t attributes,efidp dp,size_t dp_size,const char * description,const uint8_t * optional_data,size_t optional_data_size)6131285bcc8SWarner Losh create_loadopt(uint8_t *buf, size_t bufmax, uint32_t attributes, efidp dp, size_t dp_size,
6141285bcc8SWarner Losh     const char *description, const uint8_t *optional_data, size_t optional_data_size)
6151285bcc8SWarner Losh {
6161285bcc8SWarner Losh 	efi_char *bbuf = NULL;
6171285bcc8SWarner Losh 	uint8_t *pos = buf;
6181285bcc8SWarner Losh 	size_t desc_len = 0;
6191285bcc8SWarner Losh 	size_t len;
6201285bcc8SWarner Losh 
6211285bcc8SWarner Losh 	if (optional_data == NULL && optional_data_size != 0)
6221285bcc8SWarner Losh 		return BAD_LENGTH;
6231285bcc8SWarner Losh 	if (dp == NULL && dp_size != 0)
6241285bcc8SWarner Losh 		return BAD_LENGTH;
6251285bcc8SWarner Losh 
6261285bcc8SWarner Losh 	/*
6271285bcc8SWarner Losh 	 * Compute the length to make sure the passed in buffer is long enough.
6281285bcc8SWarner Losh 	 */
6291285bcc8SWarner Losh 	utf8_to_ucs2(description, &bbuf, &desc_len);
6301285bcc8SWarner Losh 	len = sizeof(uint32_t) + sizeof(uint16_t) + desc_len + dp_size + optional_data_size;
6311285bcc8SWarner Losh 	if (len > bufmax) {
6321285bcc8SWarner Losh 		free(bbuf);
6331285bcc8SWarner Losh 		return BAD_LENGTH;
6341285bcc8SWarner Losh 	}
6351285bcc8SWarner Losh 
6361285bcc8SWarner Losh 	le32enc(pos, attributes);
6371285bcc8SWarner Losh 	pos += sizeof (attributes);
6381285bcc8SWarner Losh 
6391285bcc8SWarner Losh 	le16enc(pos, dp_size);
6401285bcc8SWarner Losh 	pos += sizeof (uint16_t);
6411285bcc8SWarner Losh 
6421285bcc8SWarner Losh 	memcpy(pos, bbuf, desc_len);	/* NB:desc_len includes strailing NUL */
6431285bcc8SWarner Losh 	pos += desc_len;
6441285bcc8SWarner Losh 	free(bbuf);
6451285bcc8SWarner Losh 
6461285bcc8SWarner Losh 	memcpy(pos, dp, dp_size);
6471285bcc8SWarner Losh 	pos += dp_size;
6481285bcc8SWarner Losh 
6491285bcc8SWarner Losh 	if (optional_data && optional_data_size > 0) {
6501285bcc8SWarner Losh 		memcpy(pos, optional_data, optional_data_size);
6511285bcc8SWarner Losh 		pos += optional_data_size;
6521285bcc8SWarner Losh 	}
6531285bcc8SWarner Losh 
6541285bcc8SWarner Losh 	return pos - buf;
6551285bcc8SWarner Losh }
6561285bcc8SWarner Losh 
6571285bcc8SWarner Losh 
6581285bcc8SWarner Losh static int
make_boot_var(const char * label,const char * loader,const char * kernel,const char * env,bool dry_run,int bootnum,bool activate)659de26ba4dSWarner Losh make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run,
660de6b4aa8SWarner Losh     int bootnum, bool activate)
6611285bcc8SWarner Losh {
6621285bcc8SWarner Losh 	struct entry *new_ent;
6631285bcc8SWarner Losh 	uint32_t load_attrs = 0;
6641285bcc8SWarner Losh 	uint8_t *load_opt_buf;
6651285bcc8SWarner Losh 	size_t lopt_size, llen, klen;
6661285bcc8SWarner Losh 	efidp dp, loaderdp, kerneldp;
6671285bcc8SWarner Losh 	char *bootvar = NULL;
6681285bcc8SWarner Losh 	int ret;
6691285bcc8SWarner Losh 
670da15338dSWarner Losh 	assert(label != NULL);
671da15338dSWarner Losh 
672de26ba4dSWarner Losh 	if (bootnum == -1)
6731285bcc8SWarner Losh 		bootvar = make_next_boot_var_name();
674de26ba4dSWarner Losh 	else
675de26ba4dSWarner Losh 		bootvar = make_boot_var_name((uint16_t)bootnum);
6761285bcc8SWarner Losh 	if (bootvar == NULL)
6771285bcc8SWarner Losh 		err(1, "bootvar creation");
6781285bcc8SWarner Losh 	if (loader == NULL)
6791285bcc8SWarner Losh 		errx(1, "Must specify boot loader");
680b8adbe1cSRyan Moeller 	ret = efivar_unix_path_to_device_path(loader, &loaderdp);
681b8adbe1cSRyan Moeller 	if (ret != 0)
682b8adbe1cSRyan Moeller 		errc(1, ret, "Cannot translate unix loader path '%s' to UEFI",
683b8adbe1cSRyan Moeller 		    loader);
6841285bcc8SWarner Losh 	if (kernel != NULL) {
685b8adbe1cSRyan Moeller 		ret = efivar_unix_path_to_device_path(kernel, &kerneldp);
686b8adbe1cSRyan Moeller 		if (ret != 0)
687b8adbe1cSRyan Moeller 			errc(1, ret,
688b8adbe1cSRyan Moeller 			    "Cannot translate unix kernel path '%s' to UEFI",
689b8adbe1cSRyan Moeller 			    kernel);
6901285bcc8SWarner Losh 	} else {
6911285bcc8SWarner Losh 		kerneldp = NULL;
6921285bcc8SWarner Losh 	}
6931285bcc8SWarner Losh 	llen = efidp_size(loaderdp);
694af934ea3SWarner Losh 	if (llen > MAX_DP_LEN)
695af934ea3SWarner Losh 		errx(1, "Loader path too long.");
6961285bcc8SWarner Losh 	klen = efidp_size(kerneldp);
697af934ea3SWarner Losh 	if (klen > MAX_DP_LEN)
698af934ea3SWarner Losh 		errx(1, "Kernel path too long.");
6991285bcc8SWarner Losh 	dp = malloc(llen + klen);
700af934ea3SWarner Losh 	if (dp == NULL)
701af934ea3SWarner Losh 		errx(1, "Can't allocate memory for new device paths");
7021285bcc8SWarner Losh 	memcpy(dp, loaderdp, llen);
7031285bcc8SWarner Losh 	if (kerneldp != NULL)
7041285bcc8SWarner Losh 		memcpy((char *)dp + llen, kerneldp, klen);
7051285bcc8SWarner Losh 
7061285bcc8SWarner Losh 	/* don't make the new bootvar active by default, use the -a option later */
7071285bcc8SWarner Losh 	load_attrs = LOAD_OPTION_CATEGORY_BOOT;
708de6b4aa8SWarner Losh 	if (activate)
709de6b4aa8SWarner Losh 		load_attrs |= LOAD_OPTION_ACTIVE;
7101285bcc8SWarner Losh 	load_opt_buf = malloc(MAX_LOADOPT_LEN);
7111285bcc8SWarner Losh 	if (load_opt_buf == NULL)
7121285bcc8SWarner Losh 		err(1, "malloc");
7131285bcc8SWarner Losh 
7141285bcc8SWarner Losh 	lopt_size = create_loadopt(load_opt_buf, MAX_LOADOPT_LEN, load_attrs,
7151127aea3SWarner Losh 	    dp, llen + klen, label, env, env ? strlen(env) + 1 : 0);
7161285bcc8SWarner Losh 	if (lopt_size == BAD_LENGTH)
71767a5e533SEmmanuel Vadot 		errx(1, "Can't create loadopt");
7181285bcc8SWarner Losh 
7191285bcc8SWarner Losh 	ret = 0;
7201285bcc8SWarner Losh 	if (!dry_run) {
7211285bcc8SWarner Losh 		ret = efi_set_variable(EFI_GLOBAL_GUID, bootvar,
7221285bcc8SWarner Losh 		    (uint8_t*)load_opt_buf, lopt_size, COMMON_ATTRS);
7231285bcc8SWarner Losh 	}
7241285bcc8SWarner Losh 
7251285bcc8SWarner Losh 	if (ret)
7261285bcc8SWarner Losh 		err(1, "efi_set_variable");
7271285bcc8SWarner Losh 
72867a5e533SEmmanuel Vadot 	if (!dry_run)
7291285bcc8SWarner Losh 		add_to_boot_order(bootvar); /* first, still not active */
7301285bcc8SWarner Losh 	new_ent = malloc(sizeof(struct entry));
7311285bcc8SWarner Losh 	if (new_ent == NULL)
7321285bcc8SWarner Losh 		err(1, "malloc");
7331285bcc8SWarner Losh 	memset(new_ent, 0, sizeof(struct entry));
7341285bcc8SWarner Losh 	new_ent->name = bootvar;
7351285bcc8SWarner Losh 	new_ent->guid = EFI_GLOBAL_GUID;
7361285bcc8SWarner Losh 	LIST_INSERT_HEAD(&efivars, new_ent, entries);
737a6b6f888SWarner Losh 	free(load_opt_buf);
7381285bcc8SWarner Losh 	free(dp);
739a6b6f888SWarner Losh 
7401285bcc8SWarner Losh 	return 0;
7411285bcc8SWarner Losh }
7421285bcc8SWarner Losh 
7431285bcc8SWarner Losh 
7441285bcc8SWarner Losh static void
print_loadopt_str(uint8_t * data,size_t datalen)7451285bcc8SWarner Losh print_loadopt_str(uint8_t *data, size_t datalen)
7461285bcc8SWarner Losh {
7471285bcc8SWarner Losh 	char *dev, *relpath, *abspath;
7481285bcc8SWarner Losh 	uint32_t attr;
7491285bcc8SWarner Losh 	uint16_t fplen;
7501285bcc8SWarner Losh 	efi_char *descr;
7511285bcc8SWarner Losh 	uint8_t *ep = data + datalen;
7521285bcc8SWarner Losh 	uint8_t *walker = data;
7531285bcc8SWarner Losh 	efidp dp, edp;
7541285bcc8SWarner Losh 	char buf[1024];
7551285bcc8SWarner Losh 	int len;
7561285bcc8SWarner Losh 	int rv;
757a2aa6671SWarner Losh 	int indent;
7581285bcc8SWarner Losh 
7591285bcc8SWarner Losh 	if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
7601285bcc8SWarner Losh 		return;
7611285bcc8SWarner Losh 	// First 4 bytes are attribute flags
7621285bcc8SWarner Losh 	attr = le32dec(walker);
7631285bcc8SWarner Losh 	walker += sizeof(attr);
7641285bcc8SWarner Losh 	// Next two bytes are length of the file paths
7651285bcc8SWarner Losh 	fplen = le16dec(walker);
7661285bcc8SWarner Losh 	walker += sizeof(fplen);
7671285bcc8SWarner Losh 	// Next we have a 0 terminated UCS2 string that we know to be aligned
7681285bcc8SWarner Losh 	descr = (efi_char *)(intptr_t)(void *)walker;
7691285bcc8SWarner Losh 	len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
7701285bcc8SWarner Losh 	walker += (len + 1) * sizeof(efi_char);
7711285bcc8SWarner Losh 	if (walker > ep)
7721285bcc8SWarner Losh 		return;
7731285bcc8SWarner Losh 	// Now we have fplen bytes worth of file path stuff
7741285bcc8SWarner Losh 	dp = (efidp)walker;
7751285bcc8SWarner Losh 	walker += fplen;
7761285bcc8SWarner Losh 	if (walker > ep)
7771285bcc8SWarner Losh 		return;
7781285bcc8SWarner Losh 	edp = (efidp)walker;
77996289d09SAlexander Kabaev 	/*
78096289d09SAlexander Kabaev 	 * Everything left is the binary option args
78196289d09SAlexander Kabaev 	 * opt = walker;
78296289d09SAlexander Kabaev 	 * optlen = ep - walker;
78396289d09SAlexander Kabaev 	 */
784a2aa6671SWarner Losh 	indent = 1;
7851285bcc8SWarner Losh 	while (dp < edp) {
786bf87d4a4SAndriy Gapon 		if (efidp_size(dp) == 0)
787bf87d4a4SAndriy Gapon 			break;
7881285bcc8SWarner Losh 		efidp_format_device_path(buf, sizeof(buf), dp,
7891285bcc8SWarner Losh 		    (intptr_t)(void *)edp - (intptr_t)(void *)dp);
790a2aa6671SWarner Losh 		printf("%*s%s\n", indent, "", buf);
791a2aa6671SWarner Losh 		indent = 10 + len + 1;
7921285bcc8SWarner Losh 		rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
7931285bcc8SWarner Losh 		if (rv == 0) {
794a2aa6671SWarner Losh 			printf("%*s%s:%s %s\n", indent + 4, "", dev, relpath, abspath);
7951285bcc8SWarner Losh 			free(dev);
7961285bcc8SWarner Losh 			free(relpath);
7971285bcc8SWarner Losh 			free(abspath);
7981285bcc8SWarner Losh 		}
7991285bcc8SWarner Losh 		dp = (efidp)((char *)dp + efidp_size(dp));
8001285bcc8SWarner Losh 	}
8011285bcc8SWarner Losh }
8021285bcc8SWarner Losh 
8031285bcc8SWarner Losh static char *
get_descr(uint8_t * data)8041285bcc8SWarner Losh get_descr(uint8_t *data)
8051285bcc8SWarner Losh {
8061285bcc8SWarner Losh 	uint8_t *pos = data;
8071285bcc8SWarner Losh 	efi_char *desc;
8081285bcc8SWarner Losh 	int  len;
8091285bcc8SWarner Losh 	char *buf;
8101285bcc8SWarner Losh 	int i = 0;
8111285bcc8SWarner Losh 
8121285bcc8SWarner Losh 	pos += sizeof(uint32_t) + sizeof(uint16_t);
8131285bcc8SWarner Losh 	desc = (efi_char*)(intptr_t)(void *)pos;
8141285bcc8SWarner Losh 	len = ucs2len(desc);
8151285bcc8SWarner Losh 	buf = malloc(len + 1);
8161285bcc8SWarner Losh 	memset(buf, 0, len + 1);
8171285bcc8SWarner Losh 	while (desc[i]) {
8181285bcc8SWarner Losh 		buf[i] = desc[i];
8191285bcc8SWarner Losh 		i++;
8201285bcc8SWarner Losh 	}
8211285bcc8SWarner Losh 	return (char*)buf;
8221285bcc8SWarner Losh }
8231285bcc8SWarner Losh 
8241285bcc8SWarner Losh 
82551922c69SWarner Losh static bool
print_boot_var(const char * name,bool verbose,bool curboot)82651922c69SWarner Losh print_boot_var(const char *name, bool verbose, bool curboot)
82751922c69SWarner Losh {
82851922c69SWarner Losh 	size_t size;
82951922c69SWarner Losh 	uint32_t load_attrs;
83051922c69SWarner Losh 	uint8_t *data;
83151922c69SWarner Losh 	int ret;
83251922c69SWarner Losh 	char *d;
83351922c69SWarner Losh 
83451922c69SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, NULL);
83551922c69SWarner Losh 	if (ret < 0)
83651922c69SWarner Losh 		return false;
83751922c69SWarner Losh 	load_attrs = le32dec(data);
83851922c69SWarner Losh 	d = get_descr(data);
83951922c69SWarner Losh 	printf("%c%s%c %s", curboot ? '+' : ' ', name,
84051922c69SWarner Losh 	    ((load_attrs & LOAD_OPTION_ACTIVE) ? '*': ' '), d);
84151922c69SWarner Losh 	free(d);
84251922c69SWarner Losh 	if (verbose)
84351922c69SWarner Losh 		print_loadopt_str(data, size);
84451922c69SWarner Losh 	else
84551922c69SWarner Losh 		printf("\n");
84651922c69SWarner Losh 	return true;
84751922c69SWarner Losh }
84851922c69SWarner Losh 
84951922c69SWarner Losh 
85083c42372SD Scott Phillips static bool
os_indication_supported(uint64_t indication)85183c42372SD Scott Phillips os_indication_supported(uint64_t indication)
85283c42372SD Scott Phillips {
85383c42372SD Scott Phillips 	uint8_t *data;
85483c42372SD Scott Phillips 	size_t size;
85583c42372SD Scott Phillips 	uint32_t attrs;
85683c42372SD Scott Phillips 	int ret;
85783c42372SD Scott Phillips 
85883c42372SD Scott Phillips 	ret = efi_get_variable(EFI_GLOBAL_GUID, "OsIndicationsSupported", &data,
85983c42372SD Scott Phillips 	    &size, &attrs);
86083c42372SD Scott Phillips 	if (ret < 0)
86183c42372SD Scott Phillips 		return false;
86283c42372SD Scott Phillips 	return (le64dec(data) & indication) == indication;
86383c42372SD Scott Phillips }
86483c42372SD Scott Phillips 
86583c42372SD Scott Phillips static uint64_t
os_indications(void)86683c42372SD Scott Phillips os_indications(void)
86783c42372SD Scott Phillips {
86883c42372SD Scott Phillips 	uint8_t *data;
86983c42372SD Scott Phillips 	size_t size;
87083c42372SD Scott Phillips 	uint32_t attrs;
87183c42372SD Scott Phillips 	int ret;
87283c42372SD Scott Phillips 
87383c42372SD Scott Phillips 	ret = efi_get_variable(EFI_GLOBAL_GUID, "OsIndications", &data, &size,
87483c42372SD Scott Phillips 	    &attrs);
87583c42372SD Scott Phillips 	if (ret < 0)
87683c42372SD Scott Phillips 		return 0;
87783c42372SD Scott Phillips 	return le64dec(data);
87883c42372SD Scott Phillips }
87983c42372SD Scott Phillips 
88083c42372SD Scott Phillips static int
os_indications_set(uint64_t mask,uint64_t val)88183c42372SD Scott Phillips os_indications_set(uint64_t mask, uint64_t val)
88283c42372SD Scott Phillips {
88383c42372SD Scott Phillips 	uint8_t new[sizeof(uint64_t)];
88483c42372SD Scott Phillips 
88583c42372SD Scott Phillips 	le64enc(&new, (os_indications() & ~mask) | (val & mask));
88683c42372SD Scott Phillips 	return set_bootvar("OsIndications", new, sizeof(new));
88783c42372SD Scott Phillips }
88883c42372SD Scott Phillips 
8891285bcc8SWarner Losh /* Cmd epilogue, or just the default with no args.
8901285bcc8SWarner Losh  * The order is [bootnext] bootcurrent, timeout, order, and the bootvars [-v]
8911285bcc8SWarner Losh  */
8921285bcc8SWarner Losh static int
print_boot_vars(bool verbose)8931285bcc8SWarner Losh print_boot_vars(bool verbose)
8941285bcc8SWarner Losh {
8951285bcc8SWarner Losh 	/*
8961285bcc8SWarner Losh 	 * just read and print the current values
8971285bcc8SWarner Losh 	 * as a command epilogue
8981285bcc8SWarner Losh 	 */
8991285bcc8SWarner Losh 	struct entry *v;
9001285bcc8SWarner Losh 	uint8_t *data;
9011285bcc8SWarner Losh 	size_t size;
90251922c69SWarner Losh 	uint32_t attrs;
90351922c69SWarner Losh 	int ret, bolen;
90451922c69SWarner Losh 	uint16_t *boot_order = NULL, current;
90583c42372SD Scott Phillips 	bool boot_to_fw_ui;
90683c42372SD Scott Phillips 
90783c42372SD Scott Phillips 	if (os_indication_supported(EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) {
90883c42372SD Scott Phillips 		boot_to_fw_ui =
90983c42372SD Scott Phillips 		    (os_indications() & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) != 0;
91083c42372SD Scott Phillips 		printf("Boot to FW : %s\n", boot_to_fw_ui != 0 ?
91183c42372SD Scott Phillips 		    "true" : "false");
91283c42372SD Scott Phillips 	}
9131285bcc8SWarner Losh 
9141285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "BootNext", &data, &size, &attrs);
9151285bcc8SWarner Losh 	if (ret > 0) {
9161285bcc8SWarner Losh 		printf("BootNext : %04x\n", le16dec(data));
9171285bcc8SWarner Losh 	}
9181285bcc8SWarner Losh 
9191285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs);
92051922c69SWarner Losh 	current = le16dec(data);
92151922c69SWarner Losh 	printf("BootCurrent: %04x\n", current);
9221285bcc8SWarner Losh 
9231285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "Timeout", &data, &size, &attrs);
9241285bcc8SWarner Losh 	if (ret > 0) {
9251285bcc8SWarner Losh 		printf("Timeout    : %d seconds\n", le16dec(data));
9261285bcc8SWarner Losh 	}
92712f409ffSWarner Losh 
92812f409ffSWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) > 0) {
92912f409ffSWarner Losh 		if (size % 2 == 1)
93012f409ffSWarner Losh 			warn("Bad BootOrder variable: odd length %d", (int)size);
93151922c69SWarner Losh 		boot_order = malloc(size);
93251922c69SWarner Losh 		bolen = size / 2;
93312f409ffSWarner Losh 		printf("BootOrder  : ");
93451922c69SWarner Losh 		for (size_t i = 0; i < size; i += 2) {
93551922c69SWarner Losh 			boot_order[i / 2] = le16dec(data + i);
93651922c69SWarner Losh 			printf("%04X%s", boot_order[i / 2], i == size - 2 ? "\n" : ", ");
93751922c69SWarner Losh 		}
93812f409ffSWarner Losh 	}
9391285bcc8SWarner Losh 
94051922c69SWarner Losh 	if (boot_order == NULL) {
94151922c69SWarner Losh 		/*
94251922c69SWarner Losh 		 * now we want to fetch 'em all fresh again
9431285bcc8SWarner Losh 		 * which possibly includes a newly created bootvar
9441285bcc8SWarner Losh 		 */
9451285bcc8SWarner Losh 		LIST_FOREACH(v, &efivars, entries) {
94651922c69SWarner Losh 			print_boot_var(v->name, verbose, v->idx == current);
94751922c69SWarner Losh 		}
94851922c69SWarner Losh 	} else {
94951922c69SWarner Losh 		LIST_FOREACH(v, &efivars, entries) {
95051922c69SWarner Losh 			v->flags = 0;
95151922c69SWarner Losh 		}
95251922c69SWarner Losh 		for (int i = 0; i < bolen; i++) {
95351922c69SWarner Losh 			char buffer[10];
95451922c69SWarner Losh 
95551922c69SWarner Losh 			snprintf(buffer, sizeof(buffer), "Boot%04X", boot_order[i]);
95651922c69SWarner Losh 			if (!print_boot_var(buffer, verbose, boot_order[i] == current))
95751922c69SWarner Losh 				printf("%s: MISSING!\n", buffer);
95851922c69SWarner Losh 			LIST_FOREACH(v, &efivars, entries) {
95951922c69SWarner Losh 				if (v->idx == boot_order[i]) {
96051922c69SWarner Losh 					v->flags |= SEEN;
96151922c69SWarner Losh 					break;
96251922c69SWarner Losh 				}
96351922c69SWarner Losh 			}
96451922c69SWarner Losh 		}
96551922c69SWarner Losh 		if (verbose) {
96651922c69SWarner Losh 			printf("\n\nUnreferenced Variables:\n");
96751922c69SWarner Losh 			LIST_FOREACH(v, &efivars, entries) {
96851922c69SWarner Losh 				if (v->flags == 0)
96951922c69SWarner Losh 					print_boot_var(v->name, verbose, v->idx == current);
97051922c69SWarner Losh 			}
97151922c69SWarner Losh 		}
9721285bcc8SWarner Losh 	}
9731285bcc8SWarner Losh 	return 0;
9741285bcc8SWarner Losh }
9751285bcc8SWarner Losh 
9761285bcc8SWarner Losh static void
delete_timeout(void)9776604afe9SWarner Losh delete_timeout(void)
9781285bcc8SWarner Losh {
9791285bcc8SWarner Losh 
9801285bcc8SWarner Losh 	efi_del_variable(EFI_GLOBAL_GUID,"Timeout");
9811285bcc8SWarner Losh }
9821285bcc8SWarner Losh 
9831285bcc8SWarner Losh static void
handle_timeout(int to)9841285bcc8SWarner Losh handle_timeout(int to)
9851285bcc8SWarner Losh {
9861285bcc8SWarner Losh 	uint16_t timeout;
9871285bcc8SWarner Losh 
9881285bcc8SWarner Losh 	le16enc(&timeout, to);
989c66805a5SWarner Losh 	if (set_bootvar("Timeout", (uint8_t *)&timeout, sizeof(timeout)) < 0)
990c66805a5SWarner Losh 		errx(1, "Can't set Timeout for booting.");
9911285bcc8SWarner Losh }
9921285bcc8SWarner Losh 
9931cdb8eb8SWarner Losh static void
report_esp_device(bool do_dp,bool do_unix)9941cdb8eb8SWarner Losh report_esp_device(bool do_dp, bool do_unix)
9951cdb8eb8SWarner Losh {
9961cdb8eb8SWarner Losh 	uint8_t *data;
9971cdb8eb8SWarner Losh 	size_t size, len;
9981cdb8eb8SWarner Losh 	uint32_t attrs;
9991cdb8eb8SWarner Losh 	int ret;
10001cdb8eb8SWarner Losh 	uint16_t current, fplen;
10011cdb8eb8SWarner Losh 	char *name, *dev, *relpath, *abspath;
10021cdb8eb8SWarner Losh 	uint8_t *walker, *ep;
10031cdb8eb8SWarner Losh 	efi_char *descr;
10049a791529SWarner Losh 	efidp dp;
10051cdb8eb8SWarner Losh 	char buf[PATH_MAX];
10061cdb8eb8SWarner Losh 
10071cdb8eb8SWarner Losh 	if (do_dp && do_unix)
10081cdb8eb8SWarner Losh 		errx(1, "Can't report both UEFI device-path and Unix path together");
10091cdb8eb8SWarner Losh 
10101cdb8eb8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs);
10111cdb8eb8SWarner Losh 	if (ret < 0)
10121cdb8eb8SWarner Losh 		err(1, "Can't get BootCurrent");
10131cdb8eb8SWarner Losh 	current = le16dec(data);
10141cdb8eb8SWarner Losh 	if (asprintf(&name, "Boot%04X", current) < 0)
10151cdb8eb8SWarner Losh 		err(1, "Can't format boot var\n");
10161cdb8eb8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, NULL) < 0)
10171cdb8eb8SWarner Losh 		err(1, "Can't retrieve EFI var %s", name);
10181cdb8eb8SWarner Losh 	// First 4 bytes are attribute flags
10191cdb8eb8SWarner Losh 	walker = data;
10201cdb8eb8SWarner Losh 	ep = walker + size;
10211cdb8eb8SWarner Losh 	walker += sizeof(uint32_t);
10221cdb8eb8SWarner Losh 	// Next two bytes are length of the file paths
10231cdb8eb8SWarner Losh 	fplen = le16dec(walker);
10241cdb8eb8SWarner Losh 	walker += sizeof(fplen);
10251cdb8eb8SWarner Losh 	// Next we have a 0 terminated UCS2 string that we know to be aligned
10261cdb8eb8SWarner Losh 	descr = (efi_char *)(intptr_t)(void *)walker;
10271cdb8eb8SWarner Losh 	len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
10281cdb8eb8SWarner Losh 	walker += (len + 1) * sizeof(efi_char);
10291cdb8eb8SWarner Losh 	if (walker > ep)
10301cdb8eb8SWarner Losh 		errx(1, "malformed boot variable %s", name);
10311cdb8eb8SWarner Losh 	// Now we have fplen bytes worth of file path stuff
10321cdb8eb8SWarner Losh 	dp = (efidp)walker;
10331cdb8eb8SWarner Losh 	walker += fplen;
10341cdb8eb8SWarner Losh 	if (walker > ep)
10351cdb8eb8SWarner Losh 		errx(1, "malformed boot variable %s", name);
10361cdb8eb8SWarner Losh 	if (do_dp) {
10371cdb8eb8SWarner Losh 		efidp_format_device_path_node(buf, sizeof(buf), dp);
10381cdb8eb8SWarner Losh 		printf("%s\n", buf);
10391cdb8eb8SWarner Losh 		exit(0);
10401cdb8eb8SWarner Losh 	}
10412e085395SMitchell Horne 	if (efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath) != 0)
10421cdb8eb8SWarner Losh 		errx(1, "Can't convert to unix path");
10431cdb8eb8SWarner Losh 	if (do_unix) {
10441cdb8eb8SWarner Losh 		if (abspath == NULL)
10451cdb8eb8SWarner Losh 			errx(1, "Can't find where %s:%s is mounted",
10461cdb8eb8SWarner Losh 			    dev, relpath);
10471cdb8eb8SWarner Losh 		abspath[strlen(abspath) - strlen(relpath) - 1] = '\0';
10481cdb8eb8SWarner Losh 		printf("%s\n", abspath);
10491cdb8eb8SWarner Losh 	} else {
1050e02bee0bSWarner Losh 		printf("/dev/%s\n", dev);
10511cdb8eb8SWarner Losh 	}
10521cdb8eb8SWarner Losh 	free(dev);
10531cdb8eb8SWarner Losh 	free(relpath);
10541cdb8eb8SWarner Losh 	free(abspath);
10551cdb8eb8SWarner Losh 	exit(0);
10561cdb8eb8SWarner Losh }
10571cdb8eb8SWarner Losh 
105883c42372SD Scott Phillips static void
set_boot_to_fw_ui(bool to_fw)105983c42372SD Scott Phillips set_boot_to_fw_ui(bool to_fw)
106083c42372SD Scott Phillips {
106183c42372SD Scott Phillips 	int ret;
106283c42372SD Scott Phillips 
106383c42372SD Scott Phillips 	if (!os_indication_supported(EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) {
106483c42372SD Scott Phillips 		if (to_fw)
106583c42372SD Scott Phillips 			errx(1, "boot to fw ui not supported");
106683c42372SD Scott Phillips 		else
106783c42372SD Scott Phillips 			return;
106883c42372SD Scott Phillips 	}
106983c42372SD Scott Phillips 	ret = os_indications_set(EFI_OS_INDICATIONS_BOOT_TO_FW_UI,
107083c42372SD Scott Phillips 	    to_fw ? ~0 : 0);
107183c42372SD Scott Phillips 	if (ret < 0)
107283c42372SD Scott Phillips 		errx(1, "failed to set boot to fw ui");
107383c42372SD Scott Phillips }
107483c42372SD Scott Phillips 
10759a791529SWarner Losh static void
find_efi_device(const char * path)10769a791529SWarner Losh find_efi_device(const char *path)
10779a791529SWarner Losh {
10789a791529SWarner Losh 	efidp dp = NULL;
10799a791529SWarner Losh 	size_t len;
10809a791529SWarner Losh 	int ret;
10819a791529SWarner Losh 	char buf[1024];
10829a791529SWarner Losh 
10839a791529SWarner Losh 	ret = efivar_unix_path_to_device_path(path, &dp);
10849a791529SWarner Losh 	if (ret != 0)
10859a791529SWarner Losh 		errc(1, ret,
10869a791529SWarner Losh 		    "Cannot translate path '%s' to UEFI", path);
10879a791529SWarner Losh 	len = efidp_size(dp);
10889a791529SWarner Losh 	if (len > MAX_DP_LEN)
10899a791529SWarner Losh 		errx(1, "Resulting device path too long.");
10909a791529SWarner Losh 	efidp_format_device_path(buf, sizeof(buf), dp, len);
10919a791529SWarner Losh 	printf("%s -> %s\n", path, buf);
10929a791529SWarner Losh 	exit (0);
10939a791529SWarner Losh }
10949a791529SWarner Losh 
10951285bcc8SWarner Losh int
main(int argc,char * argv[])10961285bcc8SWarner Losh main(int argc, char *argv[])
10971285bcc8SWarner Losh {
10981285bcc8SWarner Losh 
10997fe2f504SWarner Losh 	memset(&opts, 0, sizeof (bmgr_opts_t));
11007fe2f504SWarner Losh 	parse_args(argc, argv);
11017fe2f504SWarner Losh 
11029a791529SWarner Losh 	/*
11039a791529SWarner Losh 	 * find_dev can operate without any efi variables
11049a791529SWarner Losh 	 */
1105c08ba4a5SEd Maste 	if (!efi_variables_supported() && !opts.find_dev) {
1106c08ba4a5SEd Maste 		if (errno == EACCES && geteuid() != 0)
1107c08ba4a5SEd Maste 			errx(1, "must be run as root");
1108c08ba4a5SEd Maste 		errx(1, "efi variables not supported on this system. kldload efirt?");
1109c08ba4a5SEd Maste 	}
11101285bcc8SWarner Losh 
11111285bcc8SWarner Losh 	read_vars();
11121285bcc8SWarner Losh 
11131285bcc8SWarner Losh 	if (opts.create)
11141285bcc8SWarner Losh 		/*
11151285bcc8SWarner Losh 		 * side effect, adds to boot order, but not yet active.
11161285bcc8SWarner Losh 		 */
1117da15338dSWarner Losh 		make_boot_var(opts.label ? opts.label : "",
1118de26ba4dSWarner Losh 		    opts.loader, opts.kernel, opts.env, opts.dry_run,
1119de6b4aa8SWarner Losh 		    opts.has_bootnum ? opts.bootnum : -1, opts.set_active);
11201285bcc8SWarner Losh 	else if (opts.set_active || opts.set_inactive )
11211285bcc8SWarner Losh 		handle_activity(opts.bootnum, opts.set_active);
11221285bcc8SWarner Losh 	else if (opts.order != NULL)
11231285bcc8SWarner Losh 		set_boot_order(opts.order); /* create a new bootorder with opts.order */
11241285bcc8SWarner Losh 	else if (opts.set_bootnext)
11251285bcc8SWarner Losh 		handle_bootnext(opts.bootnum);
11261285bcc8SWarner Losh 	else if (opts.delete_bootnext)
11271285bcc8SWarner Losh 		del_bootnext();
11281285bcc8SWarner Losh 	else if (opts.delete)
11291285bcc8SWarner Losh 		delete_bootvar(opts.bootnum);
11301285bcc8SWarner Losh 	else if (opts.del_timeout)
11311285bcc8SWarner Losh 		delete_timeout();
11321285bcc8SWarner Losh 	else if (opts.set_timeout)
11331285bcc8SWarner Losh 		handle_timeout(opts.timeout);
11341cdb8eb8SWarner Losh 	else if (opts.esp_device)
11351cdb8eb8SWarner Losh 		report_esp_device(opts.device_path, opts.unix_path);
113683c42372SD Scott Phillips 	else if (opts.fw_ui)
113783c42372SD Scott Phillips 		set_boot_to_fw_ui(true);
113883c42372SD Scott Phillips 	else if (opts.no_fw_ui)
113983c42372SD Scott Phillips 		set_boot_to_fw_ui(false);
11409a791529SWarner Losh 	else if (opts.find_dev)
11419a791529SWarner Losh 		find_efi_device(opts.dev);
11421285bcc8SWarner Losh 
11431285bcc8SWarner Losh 	print_boot_vars(opts.verbose);
11441285bcc8SWarner Losh }
1145