xref: /freebsd/usr.sbin/efibootmgr/efibootmgr.c (revision 3af42ca7c1b64ed62388d25d18d0ac0ab7b38c9a)
11285bcc8SWarner Losh /*-
21285bcc8SWarner Losh  * Copyright (c) 2017 Netflix, Inc.
31285bcc8SWarner Losh  * All rights reserved.
41285bcc8SWarner Losh  *
51285bcc8SWarner Losh  * Redistribution and use in source and binary forms, with or without
61285bcc8SWarner Losh  * modification, are permitted provided that the following conditions
71285bcc8SWarner Losh  * are met:
81285bcc8SWarner Losh  * 1. Redistributions of source code must retain the above copyright
91285bcc8SWarner Losh  *    notice, this list of conditions and the following disclaimer
101285bcc8SWarner Losh  *    in this position and unchanged.
111285bcc8SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
121285bcc8SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
131285bcc8SWarner Losh  *    documentation and/or other materials provided with the distribution.
141285bcc8SWarner Losh  *
151285bcc8SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161285bcc8SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171285bcc8SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181285bcc8SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191285bcc8SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201285bcc8SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211285bcc8SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221285bcc8SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231285bcc8SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241285bcc8SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251285bcc8SWarner Losh  */
261285bcc8SWarner Losh 
271285bcc8SWarner Losh #include <sys/cdefs.h>
281285bcc8SWarner Losh __FBSDID("$FreeBSD$");
291285bcc8SWarner Losh 
301285bcc8SWarner Losh #include <sys/stat.h>
311285bcc8SWarner Losh #include <sys/vtoc.h>
321285bcc8SWarner Losh #include <sys/param.h>
331285bcc8SWarner Losh #include <assert.h>
341285bcc8SWarner Losh #include <ctype.h>
351285bcc8SWarner Losh #include <err.h>
361285bcc8SWarner Losh #include <errno.h>
371285bcc8SWarner Losh #include <fcntl.h>
381285bcc8SWarner Losh #include <libgeom.h>
391285bcc8SWarner Losh #include <paths.h>
401285bcc8SWarner Losh #include <signal.h>
411285bcc8SWarner Losh #include <stdint.h>
421285bcc8SWarner Losh #include <stdio.h>
431285bcc8SWarner Losh #include <stdlib.h>
441285bcc8SWarner Losh #include <getopt.h>
451285bcc8SWarner Losh #include <limits.h>
461285bcc8SWarner Losh #include <inttypes.h>
471285bcc8SWarner Losh #include <stdbool.h>
481285bcc8SWarner Losh #include <string.h>
491285bcc8SWarner Losh #include <strings.h>
501285bcc8SWarner Losh #include <unistd.h>
511285bcc8SWarner Losh #include <libgeom.h>
521285bcc8SWarner Losh #include <geom/geom.h>
531285bcc8SWarner Losh #include <geom/geom_ctl.h>
541285bcc8SWarner Losh #include <geom/geom_int.h>
551285bcc8SWarner Losh 
561285bcc8SWarner Losh #include <efivar.h>
571285bcc8SWarner Losh #include <efiutil.h>
581285bcc8SWarner Losh #include <efichar.h>
591285bcc8SWarner Losh #include <efivar-dp.h>
601285bcc8SWarner Losh 
611285bcc8SWarner Losh #ifndef LOAD_OPTION_ACTIVE
621285bcc8SWarner Losh #define LOAD_OPTION_ACTIVE 0x00000001
631285bcc8SWarner Losh #endif
641285bcc8SWarner Losh 
651285bcc8SWarner Losh #ifndef LOAD_OPTION_CATEGORY_BOOT
661285bcc8SWarner Losh #define LOAD_OPTION_CATEGORY_BOOT 0x00000000
671285bcc8SWarner Losh #endif
681285bcc8SWarner Losh 
691285bcc8SWarner Losh #define BAD_LENGTH	((size_t)-1)
701285bcc8SWarner Losh 
711285bcc8SWarner Losh typedef struct _bmgr_opts {
721285bcc8SWarner Losh 	char	*env;
731285bcc8SWarner Losh 	char	*loader;
741285bcc8SWarner Losh 	char	*label;
751285bcc8SWarner Losh 	char	*kernel;
761285bcc8SWarner Losh 	char	*name;
771285bcc8SWarner Losh 	char	*order;
781285bcc8SWarner Losh 	int     bootnum;
791285bcc8SWarner Losh 	bool	copy;
801285bcc8SWarner Losh 	bool    create;
811285bcc8SWarner Losh 	bool    delete;
821285bcc8SWarner Losh 	bool    delete_bootnext;
831285bcc8SWarner Losh 	bool    del_timeout;
841285bcc8SWarner Losh 	bool    dry_run;
851285bcc8SWarner Losh 	bool    once;
861285bcc8SWarner Losh 	int	cp_src;
871285bcc8SWarner Losh 	bool    set_active;
881285bcc8SWarner Losh 	bool    set_bootnext;
891285bcc8SWarner Losh 	bool    set_inactive;
901285bcc8SWarner Losh 	bool    set_timeout;
911285bcc8SWarner Losh 	int     timeout;
921285bcc8SWarner Losh 	bool    verbose;
931285bcc8SWarner Losh } bmgr_opts_t;
941285bcc8SWarner Losh 
951285bcc8SWarner Losh static struct option lopts[] = {
961285bcc8SWarner Losh 	{"activate", required_argument, NULL, 'a'},
971285bcc8SWarner Losh 	{"bootnext", required_argument, NULL, 'n'}, /* set bootnext */
981285bcc8SWarner Losh 	{"bootorder", required_argument, NULL, 'o'}, /* set order */
991285bcc8SWarner Losh 	{"copy", required_argument, NULL, 'C'},		/* Copy boot method */
1001285bcc8SWarner Losh 	{"create", no_argument, NULL, 'c'},
1011285bcc8SWarner Losh 	{"deactivate", required_argument, NULL, 'A'},
1021285bcc8SWarner Losh 	{"del-timout", no_argument, NULL, 'T'},
1031285bcc8SWarner Losh 	{"delete", required_argument, NULL, 'B'},
1041285bcc8SWarner Losh 	{"delete-bootnext", required_argument, NULL, 'N'},
1051285bcc8SWarner Losh 	{"device", required_argument, NULL, 'd'},
1061285bcc8SWarner Losh 	{"dry-run", no_argument, NULL, 'D'},
1071285bcc8SWarner Losh 	{"env", required_argument, NULL, 'e'},
1081285bcc8SWarner Losh 	{"help", no_argument, NULL, 'h'},
1091285bcc8SWarner Losh 	{"kernel", required_argument, NULL, 'k'},
1101285bcc8SWarner Losh 	{"label", required_argument, NULL, 'L'},
1111285bcc8SWarner Losh 	{"loader", required_argument, NULL, 'l'},
1121285bcc8SWarner Losh 	{"once", no_argument, NULL, 'O'},
1131285bcc8SWarner Losh 	{"partition", required_argument, NULL, 'p'},
1141285bcc8SWarner Losh 	{"set-timeout", required_argument, NULL, 't'},
1151285bcc8SWarner Losh 	{"verbose", no_argument, NULL, 'v'},
1161285bcc8SWarner Losh 	{ NULL, 0, NULL, 0}
1171285bcc8SWarner Losh };
1181285bcc8SWarner Losh 
1191285bcc8SWarner Losh /* global efibootmgr opts */
1201285bcc8SWarner Losh static bmgr_opts_t opts;
1211285bcc8SWarner Losh 
1221285bcc8SWarner Losh static LIST_HEAD(efivars_head, entry) efivars =
1231285bcc8SWarner Losh 	LIST_HEAD_INITIALIZER(efivars);
1241285bcc8SWarner Losh 
1251285bcc8SWarner Losh struct entry {
1261285bcc8SWarner Losh 	efi_guid_t	guid;
1271285bcc8SWarner Losh 	uint32_t	attrs;
1281285bcc8SWarner Losh 	uint8_t		*data;
1291285bcc8SWarner Losh 	size_t		size;
1301285bcc8SWarner Losh 	char		*name;
1311285bcc8SWarner Losh 	char		*label;
1321285bcc8SWarner Losh 	int		idx;
1331285bcc8SWarner Losh 	int		part;
1341285bcc8SWarner Losh 
1351285bcc8SWarner Losh 	LIST_ENTRY(entry) entries;
1361285bcc8SWarner Losh };
1371285bcc8SWarner Losh 
1381285bcc8SWarner Losh #define MAX_DP_LEN	4096
1391285bcc8SWarner Losh #define MAX_LOADOPT_LEN	8192
1401285bcc8SWarner Losh 
1411285bcc8SWarner Losh 
1421285bcc8SWarner Losh static char *
1431285bcc8SWarner Losh mangle_loader(char *loader)
1441285bcc8SWarner Losh {
1451285bcc8SWarner Losh 	char *c;
1461285bcc8SWarner Losh 
1471285bcc8SWarner Losh 	for (c = loader; *c; c++)
1481285bcc8SWarner Losh 		if (*c == '/')
1491285bcc8SWarner Losh 			*c = '\\';
1501285bcc8SWarner Losh 
1511285bcc8SWarner Losh 	return loader;
1521285bcc8SWarner Losh }
1531285bcc8SWarner Losh 
1541285bcc8SWarner Losh 
1551285bcc8SWarner Losh #define COMMON_ATTRS EFI_VARIABLE_NON_VOLATILE | \
1561285bcc8SWarner Losh 	EFI_VARIABLE_BOOTSERVICE_ACCESS | \
1571285bcc8SWarner Losh 	EFI_VARIABLE_RUNTIME_ACCESS
1581285bcc8SWarner Losh 
1591285bcc8SWarner Losh /*
1601285bcc8SWarner Losh  * We use global guid, and common var attrs and
1611285bcc8SWarner Losh  * find it better to just delete and re-create a var.
1621285bcc8SWarner Losh  */
1631285bcc8SWarner Losh static int
1641285bcc8SWarner Losh set_bootvar(const char *name, uint8_t *data, size_t size)
1651285bcc8SWarner Losh {
1661285bcc8SWarner Losh 
1671285bcc8SWarner Losh 	efi_del_variable(EFI_GLOBAL_GUID, name);
1681285bcc8SWarner Losh 	return efi_set_variable(EFI_GLOBAL_GUID, name, data, size,
1691285bcc8SWarner Losh 	    COMMON_ATTRS);
1701285bcc8SWarner Losh }
1711285bcc8SWarner Losh 
1721285bcc8SWarner Losh 
1731285bcc8SWarner Losh #define USAGE \
1741285bcc8SWarner Losh 	"   [-aAnNB Bootvar] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help] \n \
1751285bcc8SWarner Losh   [-c -d device -p partition -l loader [-L label] [--dry-run]]"
1761285bcc8SWarner Losh 
1771285bcc8SWarner Losh #define CREATE_USAGE \
1781285bcc8SWarner Losh 	"       efibootmgr -c -d device -p partition -loader loader [-L label ] [--dry-run]"
1791285bcc8SWarner Losh #define ORDER_USAGE \
1801285bcc8SWarner Losh 	"       efibootmgr -o bootvarnum1,bootvarnum2,..."
1811285bcc8SWarner Losh #define TIMEOUT_USAGE \
1821285bcc8SWarner Losh 	"       efibootmgr -t seconds"
1831285bcc8SWarner Losh #define DELETE_USAGE \
1841285bcc8SWarner Losh 	"       efibootmgr -B bootvarnum"
1851285bcc8SWarner Losh #define ACTIVE_USAGE \
1861285bcc8SWarner Losh 	"       efibootmgr [-a | -A] bootvarnum"
1871285bcc8SWarner Losh #define BOOTNEXT_USAGE \
1881285bcc8SWarner Losh 	"       efibootmgr [-n | -N] bootvarnum"
1891285bcc8SWarner Losh 
1901285bcc8SWarner Losh static void
1911285bcc8SWarner Losh parse_args(int argc, char *argv[])
1921285bcc8SWarner Losh {
1931285bcc8SWarner Losh 	int ch;
1941285bcc8SWarner Losh 
1951127aea3SWarner Losh 	while ((ch = getopt_long(argc, argv, "A:a:B:C:cDe:hk:L:l:Nn:Oo:Tt:v",
1961285bcc8SWarner Losh 		    lopts, NULL)) != -1) {
1971285bcc8SWarner Losh 		switch (ch) {
1981285bcc8SWarner Losh 		case 'A':
1991285bcc8SWarner Losh 			opts.set_inactive = true;
2001285bcc8SWarner Losh 			opts.bootnum = strtoul(optarg, NULL, 16);
2011285bcc8SWarner Losh 			break;
2021285bcc8SWarner Losh 		case 'a':
2031285bcc8SWarner Losh 			opts.set_active = true;
2041285bcc8SWarner Losh 			opts.bootnum = strtoul(optarg, NULL, 16);
2051285bcc8SWarner Losh 			break;
2061285bcc8SWarner Losh 		case 'B':
2071285bcc8SWarner Losh 			opts.delete = true;
2081285bcc8SWarner Losh 			opts.bootnum = strtoul(optarg, NULL, 16);
2091285bcc8SWarner Losh 			break;
2101285bcc8SWarner Losh 		case 'C':
2111285bcc8SWarner Losh 			opts.copy = true;
2121285bcc8SWarner Losh 			opts.cp_src = strtoul(optarg, NULL, 16);
2131285bcc8SWarner Losh 		case 'c':
2141285bcc8SWarner Losh 			opts.create = true;
2151285bcc8SWarner Losh 			break;
2161285bcc8SWarner Losh 		case 'D': /* should be remove dups XXX */
2171285bcc8SWarner Losh 			opts.dry_run = true;
2181285bcc8SWarner Losh 			break;
2191285bcc8SWarner Losh 		case 'e':
2201285bcc8SWarner Losh 			opts.env = strdup(optarg);
2211285bcc8SWarner Losh 			break;
2221285bcc8SWarner Losh 		case 'h':
2231285bcc8SWarner Losh 		default:
2241285bcc8SWarner Losh 			errx(1, "%s", USAGE);
2251285bcc8SWarner Losh 			break;
2261285bcc8SWarner Losh 		case 'k':
2271285bcc8SWarner Losh 			opts.kernel = strdup(optarg);
2281285bcc8SWarner Losh 			break;
2291285bcc8SWarner Losh 		case 'L':
2301285bcc8SWarner Losh 			opts.label = strdup(optarg);
2311285bcc8SWarner Losh 			break;
2321285bcc8SWarner Losh 		case 'l':
2331285bcc8SWarner Losh 			opts.loader = strdup(optarg);
2341285bcc8SWarner Losh 			opts.loader = mangle_loader(opts.loader);
2351285bcc8SWarner Losh 			break;
2361285bcc8SWarner Losh 		case 'N':
2371285bcc8SWarner Losh 			opts.delete_bootnext = true;
2381285bcc8SWarner Losh 			break;
2391285bcc8SWarner Losh 		case 'n':
2401285bcc8SWarner Losh 			opts.set_bootnext = true;
2411285bcc8SWarner Losh 			opts.bootnum = strtoul(optarg, NULL, 16);
2421285bcc8SWarner Losh 			break;
2431285bcc8SWarner Losh 		case 'O':
2441285bcc8SWarner Losh 			opts.once = true;
2451285bcc8SWarner Losh 			break;
2461285bcc8SWarner Losh 		case 'o':
2471285bcc8SWarner Losh 			opts.order = strdup(optarg);
2481285bcc8SWarner Losh 			break;
2491285bcc8SWarner Losh 		case 'T':
2501285bcc8SWarner Losh 			opts.del_timeout = true;
2511285bcc8SWarner Losh 			break;
2521285bcc8SWarner Losh 		case 't':
2531285bcc8SWarner Losh 			opts.set_timeout = true;
2541285bcc8SWarner Losh 			opts.timeout = strtoul(optarg, NULL, 10);
2551285bcc8SWarner Losh 			break;
2561285bcc8SWarner Losh 		case 'v':
2571285bcc8SWarner Losh 			opts.verbose = true;
2581285bcc8SWarner Losh 			break;
2591285bcc8SWarner Losh 		}
2601285bcc8SWarner Losh 	}
2611285bcc8SWarner Losh 	if (opts.create) {
2621127aea3SWarner Losh 		if (!opts.loader)
2631285bcc8SWarner Losh 			errx(1, "%s",CREATE_USAGE);
2641285bcc8SWarner Losh 		return;
2651285bcc8SWarner Losh 	}
2661285bcc8SWarner Losh 	if (opts.set_bootnext && !(opts.bootnum))
2671285bcc8SWarner Losh 		errx(1, "%s", BOOTNEXT_USAGE);
2681285bcc8SWarner Losh 
2691285bcc8SWarner Losh 	if ((opts.set_active ||  opts.set_inactive) && !(opts.bootnum))
2701285bcc8SWarner Losh 		errx(1, "%s", ACTIVE_USAGE);
2711285bcc8SWarner Losh 
2721285bcc8SWarner Losh 	if (opts.order && !(opts.order))
2731285bcc8SWarner Losh 		errx(1, "%s", ORDER_USAGE);
2741285bcc8SWarner Losh }
2751285bcc8SWarner Losh 
2761285bcc8SWarner Losh 
2771285bcc8SWarner Losh static void
2786604afe9SWarner Losh print_order(void)
2791285bcc8SWarner Losh {
2801285bcc8SWarner Losh 	uint32_t attrs;
2811285bcc8SWarner Losh 	uint8_t *data;
2821285bcc8SWarner Losh 	size_t size, i;
2831285bcc8SWarner Losh 
2841285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0)
2851285bcc8SWarner Losh 		errx(1, "Couldn't get value for BootOrder\n");
2861285bcc8SWarner Losh 
2871285bcc8SWarner Losh 	if (size % 2 == 1)
2881285bcc8SWarner Losh 		errx(1, "Bad BootOrder variable: odd length");
2891285bcc8SWarner Losh 
2901285bcc8SWarner Losh 	printf("BootOrder : ");
2911285bcc8SWarner Losh 	for (i = 0; i < size; i += 2)
2921285bcc8SWarner Losh 		printf("%04x%s", le16dec(data + i), i == size - 2 ? "\n" : ", ");
2931285bcc8SWarner Losh }
2941285bcc8SWarner Losh 
2951285bcc8SWarner Losh 
2961285bcc8SWarner Losh static void
2971285bcc8SWarner Losh read_vars(void)
2981285bcc8SWarner Losh {
2991285bcc8SWarner Losh 
3001285bcc8SWarner Losh 	efi_guid_t *guid;
3011285bcc8SWarner Losh 	char *next_name = NULL;
3021285bcc8SWarner Losh 	int ret = 0;
3031285bcc8SWarner Losh 
3041285bcc8SWarner Losh 	struct entry *nent;
3051285bcc8SWarner Losh 
3061285bcc8SWarner Losh 	LIST_INIT(&efivars);
3071285bcc8SWarner Losh 	while ((ret = efi_get_next_variable_name(&guid, &next_name)) > 0) {
3081285bcc8SWarner Losh 		/*
309a3e6c4a6SWarner Losh 		 * Only pay attention to EFI:BootXXXX variables to get the list.
3101285bcc8SWarner Losh 		 */
3111285bcc8SWarner Losh 		if (efi_guid_cmp(guid, &EFI_GLOBAL_GUID) != 0 ||
3121285bcc8SWarner Losh 		    strlen(next_name) != 8 ||
3131285bcc8SWarner Losh 		    strncmp(next_name, "Boot", 4) != 0 ||
3141285bcc8SWarner Losh 		    !isxdigit(next_name[4]) ||
3151285bcc8SWarner Losh 		    !isxdigit(next_name[5]) ||
3161285bcc8SWarner Losh 		    !isxdigit(next_name[6]) ||
3171285bcc8SWarner Losh 		    !isxdigit(next_name[7]))
3181285bcc8SWarner Losh 			continue;
3191285bcc8SWarner Losh 		nent = malloc(sizeof(struct entry));
3201285bcc8SWarner Losh 		nent->name = strdup(next_name);
3211285bcc8SWarner Losh 
3221285bcc8SWarner Losh 		ret = efi_get_variable(*guid, next_name, &nent->data,
3231285bcc8SWarner Losh 		    &nent->size, &nent->attrs);
3241285bcc8SWarner Losh 		if (ret < 0)
3251285bcc8SWarner Losh 			err(1, "efi_get_variable");
3261285bcc8SWarner Losh 		nent->guid = *guid;
3271285bcc8SWarner Losh 		nent->idx = strtoul(&next_name[4], NULL, 16);
3281285bcc8SWarner Losh 		LIST_INSERT_HEAD(&efivars, nent, entries);
3291285bcc8SWarner Losh 	}
3301285bcc8SWarner Losh }
3311285bcc8SWarner Losh 
3321285bcc8SWarner Losh 
3331285bcc8SWarner Losh static void
3341285bcc8SWarner Losh set_boot_order(char *order)
3351285bcc8SWarner Losh {
3361285bcc8SWarner Losh 	uint16_t *new_data;
3371285bcc8SWarner Losh 	size_t size;
3381285bcc8SWarner Losh 	char *next, *cp;
3391285bcc8SWarner Losh 	int cnt;
3401285bcc8SWarner Losh 	int i;
3411285bcc8SWarner Losh 
3421285bcc8SWarner Losh 	cp = order;
3431285bcc8SWarner Losh 	cnt = 1;
3441285bcc8SWarner Losh 	while (*cp) {
3451285bcc8SWarner Losh 		if (*cp++ == ',')
3461285bcc8SWarner Losh 			cnt++;
3471285bcc8SWarner Losh 	}
3481285bcc8SWarner Losh 	size = sizeof(uint16_t) * cnt;
3491285bcc8SWarner Losh 	new_data = malloc(size);
3501285bcc8SWarner Losh 
3511285bcc8SWarner Losh 	i = 0;
3521285bcc8SWarner Losh 	cp = strdup(order);
3531285bcc8SWarner Losh 	while ((next = strsep(&cp, ",")) != NULL) {
3541285bcc8SWarner Losh 		new_data[i] = strtoul(next, NULL, 16);
3551285bcc8SWarner Losh 		if (new_data[i] == 0 && errno == EINVAL) {
3561285bcc8SWarner Losh 			warnx("can't parse %s as a numb", next);
3571285bcc8SWarner Losh 			errx(1, "%s", ORDER_USAGE);
3581285bcc8SWarner Losh 		}
3591285bcc8SWarner Losh 		i++;
3601285bcc8SWarner Losh 	}
3611285bcc8SWarner Losh 	free(cp);
3621285bcc8SWarner Losh 	if (set_bootvar("BootOrder", (uint8_t*)new_data, size) < 0)
3631285bcc8SWarner Losh 		err(1, "Unabke to set BootOrder to %s", order);
364*3af42ca7SWarner Losh 	free(new_data);
3651285bcc8SWarner Losh }
3661285bcc8SWarner Losh 
3671285bcc8SWarner Losh static void
3681285bcc8SWarner Losh handle_activity(int bootnum, bool active)
3691285bcc8SWarner Losh {
3701285bcc8SWarner Losh 	uint32_t attrs, load_attrs;
3711285bcc8SWarner Losh 	uint8_t *data;
3721285bcc8SWarner Losh 	size_t size;
3731285bcc8SWarner Losh 	char *name;
3741285bcc8SWarner Losh 
3751285bcc8SWarner Losh 	asprintf(&name, "%s%04X", "Boot", bootnum);
3761285bcc8SWarner Losh 	if (name == NULL)
3771285bcc8SWarner Losh 		err(1, "asprintf");
3781285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, &attrs) < 0)
3791285bcc8SWarner Losh 		err(1, "No such bootvar %s\n", name);
3801285bcc8SWarner Losh 
3811285bcc8SWarner Losh 	load_attrs = le32dec(data);
3821285bcc8SWarner Losh 
3831285bcc8SWarner Losh 	if (active)
3841285bcc8SWarner Losh 		load_attrs |= LOAD_OPTION_ACTIVE;
3851285bcc8SWarner Losh 	else
3861285bcc8SWarner Losh 		load_attrs &= ~LOAD_OPTION_ACTIVE;
3871285bcc8SWarner Losh 
3881285bcc8SWarner Losh 	le32enc(data, load_attrs);
3891285bcc8SWarner Losh 
3901285bcc8SWarner Losh 	if (set_bootvar(name, data, size) < 0)
3911285bcc8SWarner Losh 		err(1, "handle activity efi_set_variable");
3921285bcc8SWarner Losh }
3931285bcc8SWarner Losh 
3941285bcc8SWarner Losh 
3951285bcc8SWarner Losh /*
3961285bcc8SWarner Losh  * add boot var to boot order.
3971285bcc8SWarner Losh  * called by create boot var. There is no option
3981285bcc8SWarner Losh  * to add one independent of create.
3991285bcc8SWarner Losh  *
4001285bcc8SWarner Losh  * Note: we currently don't support where it goes
4011285bcc8SWarner Losh  * so it goes on the front, inactive.
4021285bcc8SWarner Losh  * use -o 2,3,7 etc to affect order, -a to activate.
4031285bcc8SWarner Losh  */
4041285bcc8SWarner Losh static void
4051285bcc8SWarner Losh add_to_boot_order(char *bootvar)
4061285bcc8SWarner Losh {
4071285bcc8SWarner Losh 	size_t size;
4081285bcc8SWarner Losh 	uint32_t attrs;
4091285bcc8SWarner Losh 	uint16_t val;
4101285bcc8SWarner Losh 	uint8_t *data, *new;
4111285bcc8SWarner Losh 
4121285bcc8SWarner Losh 	val = strtoul(&bootvar[4], NULL, 16);
4131285bcc8SWarner Losh 
4141285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) {
4151285bcc8SWarner Losh 		if (errno == ENOENT) { /* create it and set this bootvar to active */
4161285bcc8SWarner Losh 			size = 0;
4171285bcc8SWarner Losh 			data = NULL;
4181285bcc8SWarner Losh 		} else
4191285bcc8SWarner Losh 			err(1, "efi_get_variable BootOrder");
4201285bcc8SWarner Losh 	}
4211285bcc8SWarner Losh 
4221285bcc8SWarner Losh 	/*
4231285bcc8SWarner Losh 	 * We have BootOrder with the current order
4241285bcc8SWarner Losh 	 * so grow the array by one, add the value
4251285bcc8SWarner Losh 	 * and write the new variable value.
4261285bcc8SWarner Losh 	 */
4271285bcc8SWarner Losh 	size += sizeof(uint16_t);
4281285bcc8SWarner Losh 	new = malloc(size);
4291285bcc8SWarner Losh 	if (!new)
4301285bcc8SWarner Losh 		err(1, "malloc");
4311285bcc8SWarner Losh 
4321285bcc8SWarner Losh 	le16enc(new, val);
4331285bcc8SWarner Losh 	if (size > sizeof(uint16_t))
4341285bcc8SWarner Losh 		memcpy(new + sizeof(uint16_t), data, size - sizeof(uint16_t));
4351285bcc8SWarner Losh 
4361285bcc8SWarner Losh 	if (set_bootvar("BootOrder", new, size) < 0)
4371285bcc8SWarner Losh 		err(1, "set_bootvar");
4381285bcc8SWarner Losh 	free(new);
4391285bcc8SWarner Losh }
4401285bcc8SWarner Losh 
4411285bcc8SWarner Losh 
4421285bcc8SWarner Losh static void
4431285bcc8SWarner Losh remove_from_order(uint16_t bootnum)
4441285bcc8SWarner Losh {
4451285bcc8SWarner Losh 	uint32_t attrs;
4461285bcc8SWarner Losh 	size_t size, i, j;
4471285bcc8SWarner Losh 	uint8_t *new, *data;
4481285bcc8SWarner Losh 
4491285bcc8SWarner Losh 	if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0)
4501285bcc8SWarner Losh 		return;
4511285bcc8SWarner Losh 
4521285bcc8SWarner Losh 	new = malloc(size);
4531285bcc8SWarner Losh 	if (new == NULL)
4541285bcc8SWarner Losh 		err(1, "malloc");
4551285bcc8SWarner Losh 
4561285bcc8SWarner Losh 	for (j = i = 0; i < size; i += sizeof(uint16_t)) {
4571285bcc8SWarner Losh 		if (le16dec(data + i) == bootnum)
4581285bcc8SWarner Losh 			continue;
4591285bcc8SWarner Losh 		memcpy(new + j, data + i, sizeof(uint16_t));
4601285bcc8SWarner Losh 		j += sizeof(uint16_t);
4611285bcc8SWarner Losh 	}
4621285bcc8SWarner Losh 	if (i == j)
4631285bcc8SWarner Losh 		warnx("Boot variable %04x not in BootOrder", bootnum);
4641285bcc8SWarner Losh 	else if (set_bootvar("BootOrder", new, j) < 0)
4651285bcc8SWarner Losh 		err(1, "Unable to update BootOrder with new value");
4661285bcc8SWarner Losh 	free(new);
4671285bcc8SWarner Losh }
4681285bcc8SWarner Losh 
4691285bcc8SWarner Losh 
4701285bcc8SWarner Losh static void
4711285bcc8SWarner Losh delete_bootvar(int bootnum)
4721285bcc8SWarner Losh {
4731285bcc8SWarner Losh 	char *name;
4741285bcc8SWarner Losh 	int defer = 0;
4751285bcc8SWarner Losh 
4761285bcc8SWarner Losh 	/*
4771285bcc8SWarner Losh 	 * Try to delete the boot variable and remocve it
4781285bcc8SWarner Losh 	 * from the boot order. We always do both actions
4791285bcc8SWarner Losh 	 * to make it easy to clean up from oopses.
4801285bcc8SWarner Losh 	 */
4811285bcc8SWarner Losh 	if (bootnum < 0 || bootnum > 0xffff)
4821285bcc8SWarner Losh 		errx(1, "Bad boot variable %#x", bootnum);
4831285bcc8SWarner Losh 	asprintf(&name, "%s%04X", "Boot", bootnum);
4841285bcc8SWarner Losh 	if (name == NULL)
4851285bcc8SWarner Losh 		err(1, "asprintf");
4861285bcc8SWarner Losh 	printf("Removing boot variable '%s'\n", name);
4871285bcc8SWarner Losh 	if (efi_del_variable(EFI_GLOBAL_GUID, name) < 0) {
4881285bcc8SWarner Losh 		defer = 1;
4891285bcc8SWarner Losh 		warn("cannot delete variable %s", name);
4901285bcc8SWarner Losh 	}
4911285bcc8SWarner Losh 	printf("Removing 0x%x from BootOrder\n", bootnum);
4921285bcc8SWarner Losh 	remove_from_order(bootnum);
4931285bcc8SWarner Losh 	free(name);
4941285bcc8SWarner Losh 	if (defer)
4951285bcc8SWarner Losh 		exit(defer);
4961285bcc8SWarner Losh }
4971285bcc8SWarner Losh 
4981285bcc8SWarner Losh 
4991285bcc8SWarner Losh static void
5001285bcc8SWarner Losh del_bootnext(void)
5011285bcc8SWarner Losh {
5021285bcc8SWarner Losh 
5031285bcc8SWarner Losh 	if (efi_del_variable(EFI_GLOBAL_GUID, "BootNext") < 0)
5041285bcc8SWarner Losh 		err(1, "efi_del_variable");
5051285bcc8SWarner Losh }
5061285bcc8SWarner Losh 
5071285bcc8SWarner Losh static void
5081285bcc8SWarner Losh handle_bootnext(uint16_t bootnum)
5091285bcc8SWarner Losh {
5101285bcc8SWarner Losh 	uint16_t num;
5111285bcc8SWarner Losh 
5121285bcc8SWarner Losh 	le16enc(&num, bootnum);
5131285bcc8SWarner Losh 	if (set_bootvar("BootNext", (uint8_t*)&bootnum, sizeof(uint16_t)) < 0)
5141285bcc8SWarner Losh 		err(1, "set_bootvar");
5151285bcc8SWarner Losh }
5161285bcc8SWarner Losh 
5171285bcc8SWarner Losh 
5181285bcc8SWarner Losh static int
5191285bcc8SWarner Losh compare(const void *a, const void *b)
5201285bcc8SWarner Losh {
5211285bcc8SWarner Losh 	uint16_t c;
5221285bcc8SWarner Losh 	uint16_t d;
5231285bcc8SWarner Losh 
5241285bcc8SWarner Losh 	memcpy(&c, a, sizeof(uint16_t));
5251285bcc8SWarner Losh 	memcpy(&d, b, sizeof(uint16_t));
5261285bcc8SWarner Losh 
5271285bcc8SWarner Losh 	if (c < d)
5281285bcc8SWarner Losh 		return -1;
5291285bcc8SWarner Losh 	if (c == d)
5301285bcc8SWarner Losh 		return  0;
5311285bcc8SWarner Losh 	return  1;
5321285bcc8SWarner Losh }
5331285bcc8SWarner Losh 
5341285bcc8SWarner Losh static char *
5356604afe9SWarner Losh make_next_boot_var_name(void)
5361285bcc8SWarner Losh {
5371285bcc8SWarner Losh 	struct entry *v;
5381285bcc8SWarner Losh 	uint16_t *vals, next_free = 0;
5391285bcc8SWarner Losh 	char *name;
5401285bcc8SWarner Losh 	int cnt = 0;
5411285bcc8SWarner Losh 	int i;
5421285bcc8SWarner Losh 
5431285bcc8SWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
5441285bcc8SWarner Losh 		cnt++;
5451285bcc8SWarner Losh 	}
5461285bcc8SWarner Losh 
5471285bcc8SWarner Losh 	vals = malloc(sizeof(uint16_t) * cnt);
5481285bcc8SWarner Losh 	if (!vals)
5491285bcc8SWarner Losh 		return NULL;
5501285bcc8SWarner Losh 
5511285bcc8SWarner Losh 	i = 0;
5521285bcc8SWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
5531285bcc8SWarner Losh 		vals[i++] = v->idx;
5541285bcc8SWarner Losh 	}
5551285bcc8SWarner Losh 	qsort(vals, cnt, sizeof(uint16_t), compare);
5561285bcc8SWarner Losh 	/* if the hole is at the beginning, just return zero */
5571285bcc8SWarner Losh 	if (vals[0] > 0) {
5581285bcc8SWarner Losh 		next_free = 0;
5591285bcc8SWarner Losh 	} else {
5601285bcc8SWarner Losh 		/* now just run the list looking for the first hole */
5611285bcc8SWarner Losh 		for (i = 0; i < cnt - 1 && next_free == 0; i++)
5621285bcc8SWarner Losh 			if (vals[i] != vals[i + 1] + 1)
5631285bcc8SWarner Losh 				next_free = vals[i] + 1;
5641285bcc8SWarner Losh 		if (next_free == 0)
5651285bcc8SWarner Losh 			next_free = vals[cnt - 1] + 1;
5661285bcc8SWarner Losh 		/* In theory we could have used all 65k slots -- what to do? */
5671285bcc8SWarner Losh 	}
5681285bcc8SWarner Losh 	free(vals);
5691285bcc8SWarner Losh 
5701285bcc8SWarner Losh 	asprintf(&name, "%s%04X", "Boot", next_free);
5711285bcc8SWarner Losh 	if (name == NULL)
5721285bcc8SWarner Losh 		err(1, "asprintf");
5731285bcc8SWarner Losh 	return name;
5741285bcc8SWarner Losh }
5751285bcc8SWarner Losh 
5761285bcc8SWarner Losh 
5771285bcc8SWarner Losh static size_t
5781285bcc8SWarner Losh create_loadopt(uint8_t *buf, size_t bufmax, uint32_t attributes, efidp dp, size_t dp_size,
5791285bcc8SWarner Losh     const char *description, const uint8_t *optional_data, size_t optional_data_size)
5801285bcc8SWarner Losh {
5811285bcc8SWarner Losh 	efi_char *bbuf = NULL;
5821285bcc8SWarner Losh 	uint8_t *pos = buf;
5831285bcc8SWarner Losh 	size_t desc_len = 0;
5841285bcc8SWarner Losh 	size_t len;
5851285bcc8SWarner Losh 
5861285bcc8SWarner Losh 	if (optional_data == NULL && optional_data_size != 0)
5871285bcc8SWarner Losh 		return BAD_LENGTH;
5881285bcc8SWarner Losh 	if (dp == NULL && dp_size != 0)
5891285bcc8SWarner Losh 		return BAD_LENGTH;
5901285bcc8SWarner Losh 
5911285bcc8SWarner Losh 	/*
5921285bcc8SWarner Losh 	 * Compute the length to make sure the passed in buffer is long enough.
5931285bcc8SWarner Losh 	 */
5941127aea3SWarner Losh 	if (description)
5951285bcc8SWarner Losh 		utf8_to_ucs2(description, &bbuf, &desc_len);
5961127aea3SWarner Losh 	else {
5971127aea3SWarner Losh 		desc_len = 0;
5981127aea3SWarner Losh 		bbuf = NULL;
5991127aea3SWarner Losh 	}
6001285bcc8SWarner Losh 	len = sizeof(uint32_t) + sizeof(uint16_t) + desc_len + dp_size + optional_data_size;
6011285bcc8SWarner Losh 	if (len > bufmax) {
6021285bcc8SWarner Losh 		free(bbuf);
6031285bcc8SWarner Losh 		return BAD_LENGTH;
6041285bcc8SWarner Losh 	}
6051285bcc8SWarner Losh 
6061285bcc8SWarner Losh 	le32enc(pos, attributes);
6071285bcc8SWarner Losh 	pos += sizeof (attributes);
6081285bcc8SWarner Losh 
6091285bcc8SWarner Losh 	le16enc(pos, dp_size);
6101285bcc8SWarner Losh 	pos += sizeof (uint16_t);
6111285bcc8SWarner Losh 
6121285bcc8SWarner Losh 	memcpy(pos, bbuf, desc_len);	/* NB:desc_len includes strailing NUL */
6131285bcc8SWarner Losh 	pos += desc_len;
6141285bcc8SWarner Losh 	free(bbuf);
6151285bcc8SWarner Losh 
6161285bcc8SWarner Losh 	memcpy(pos, dp, dp_size);
6171285bcc8SWarner Losh 	pos += dp_size;
6181285bcc8SWarner Losh 
6191285bcc8SWarner Losh 	if (optional_data && optional_data_size > 0) {
6201285bcc8SWarner Losh 		memcpy(pos, optional_data, optional_data_size);
6211285bcc8SWarner Losh 		pos += optional_data_size;
6221285bcc8SWarner Losh 	}
6231285bcc8SWarner Losh 
6241285bcc8SWarner Losh 	return pos - buf;
6251285bcc8SWarner Losh }
6261285bcc8SWarner Losh 
6271285bcc8SWarner Losh 
6281285bcc8SWarner Losh static int
6291285bcc8SWarner Losh make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run)
6301285bcc8SWarner Losh {
6311285bcc8SWarner Losh 	struct entry *new_ent;
6321285bcc8SWarner Losh 	uint32_t load_attrs = 0;
6331285bcc8SWarner Losh 	uint8_t *load_opt_buf;
6341285bcc8SWarner Losh 	size_t lopt_size, llen, klen;
6351285bcc8SWarner Losh 	efidp dp, loaderdp, kerneldp;
6361285bcc8SWarner Losh 	char *bootvar = NULL;
6371285bcc8SWarner Losh 	int ret;
6381285bcc8SWarner Losh 
6391285bcc8SWarner Losh 	bootvar = make_next_boot_var_name();
6401285bcc8SWarner Losh 	if (bootvar == NULL)
6411285bcc8SWarner Losh 		err(1, "bootvar creation");
6421285bcc8SWarner Losh 	if (loader == NULL)
6431285bcc8SWarner Losh 		errx(1, "Must specify boot loader");
6441285bcc8SWarner Losh 	if (efivar_unix_path_to_device_path(loader, &loaderdp) != 0)
6451285bcc8SWarner Losh 		err(1, "Cannot translate unix loader path '%s' to UEFI", loader);
6461285bcc8SWarner Losh 	if (kernel != NULL) {
6471285bcc8SWarner Losh 		if (efivar_unix_path_to_device_path(kernel, &kerneldp) != 0)
6481285bcc8SWarner Losh 			err(1, "Cannot translate unix kernel path '%s' to UEFI", kernel);
6491285bcc8SWarner Losh 	} else {
6501285bcc8SWarner Losh 		kerneldp = NULL;
6511285bcc8SWarner Losh 	}
6521285bcc8SWarner Losh 	llen = efidp_size(loaderdp);
6531285bcc8SWarner Losh 	klen = efidp_size(kerneldp);
6541285bcc8SWarner Losh 	dp = malloc(llen + klen);
6551285bcc8SWarner Losh 	memcpy(dp, loaderdp, llen);
6561285bcc8SWarner Losh 	if (kerneldp != NULL)
6571285bcc8SWarner Losh 		memcpy((char *)dp + llen, kerneldp, klen);
6581285bcc8SWarner Losh 
6591285bcc8SWarner Losh 	/* don't make the new bootvar active by default, use the -a option later */
6601285bcc8SWarner Losh 	load_attrs = LOAD_OPTION_CATEGORY_BOOT;
6611285bcc8SWarner Losh 	load_opt_buf = malloc(MAX_LOADOPT_LEN);
6621285bcc8SWarner Losh 	if (load_opt_buf == NULL)
6631285bcc8SWarner Losh 		err(1, "malloc");
6641285bcc8SWarner Losh 
6651285bcc8SWarner Losh 	lopt_size = create_loadopt(load_opt_buf, MAX_LOADOPT_LEN, load_attrs,
6661127aea3SWarner Losh 	    dp, llen + klen, label, env, env ? strlen(env) + 1 : 0);
6671285bcc8SWarner Losh 	if (lopt_size == BAD_LENGTH)
6681285bcc8SWarner Losh 		errx(1, "Can't crate loadopt");
6691285bcc8SWarner Losh 
6701285bcc8SWarner Losh 	ret = 0;
6711285bcc8SWarner Losh 	if (!dry_run) {
6721285bcc8SWarner Losh 		ret = efi_set_variable(EFI_GLOBAL_GUID, bootvar,
6731285bcc8SWarner Losh 		    (uint8_t*)load_opt_buf, lopt_size, COMMON_ATTRS);
6741285bcc8SWarner Losh 	}
6751285bcc8SWarner Losh 
6761285bcc8SWarner Losh 	if (ret)
6771285bcc8SWarner Losh 		err(1, "efi_set_variable");
6781285bcc8SWarner Losh 
6791285bcc8SWarner Losh 	add_to_boot_order(bootvar); /* first, still not active */
6801285bcc8SWarner Losh 	new_ent = malloc(sizeof(struct entry));
6811285bcc8SWarner Losh 	if (new_ent == NULL)
6821285bcc8SWarner Losh 		err(1, "malloc");
6831285bcc8SWarner Losh 	memset(new_ent, 0, sizeof(struct entry));
6841285bcc8SWarner Losh 	new_ent->name = bootvar;
6851285bcc8SWarner Losh 	new_ent->guid = EFI_GLOBAL_GUID;
6861285bcc8SWarner Losh 	LIST_INSERT_HEAD(&efivars, new_ent, entries);
6871285bcc8SWarner Losh 
6881285bcc8SWarner Losh 	free(dp);
6891285bcc8SWarner Losh 	return 0;
6901285bcc8SWarner Losh }
6911285bcc8SWarner Losh 
6921285bcc8SWarner Losh 
6931285bcc8SWarner Losh static void
6941285bcc8SWarner Losh print_loadopt_str(uint8_t *data, size_t datalen)
6951285bcc8SWarner Losh {
6961285bcc8SWarner Losh 	char *dev, *relpath, *abspath;
6971285bcc8SWarner Losh 	uint32_t attr;
6981285bcc8SWarner Losh 	uint16_t fplen;
6991285bcc8SWarner Losh 	efi_char *descr;
7001285bcc8SWarner Losh 	uint8_t *ep = data + datalen;
7011285bcc8SWarner Losh 	uint8_t *walker = data;
7021285bcc8SWarner Losh 	efidp dp, edp;
7031285bcc8SWarner Losh 	void *opt;
7041285bcc8SWarner Losh 	char buf[1024];
7051285bcc8SWarner Losh 	int len;
7061285bcc8SWarner Losh 	int optlen;
7071285bcc8SWarner Losh 	int rv;
708a2aa6671SWarner Losh 	int indent;
7091285bcc8SWarner Losh 
7101285bcc8SWarner Losh 	if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
7111285bcc8SWarner Losh 		return;
7121285bcc8SWarner Losh 	// First 4 bytes are attribute flags
7131285bcc8SWarner Losh 	attr = le32dec(walker);
7141285bcc8SWarner Losh 	walker += sizeof(attr);
7151285bcc8SWarner Losh 	// Next two bytes are length of the file paths
7161285bcc8SWarner Losh 	fplen = le16dec(walker);
7171285bcc8SWarner Losh 	walker += sizeof(fplen);
7181285bcc8SWarner Losh 	// Next we have a 0 terminated UCS2 string that we know to be aligned
7191285bcc8SWarner Losh 	descr = (efi_char *)(intptr_t)(void *)walker;
7201285bcc8SWarner Losh 	len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
7211285bcc8SWarner Losh 	walker += (len + 1) * sizeof(efi_char);
7221285bcc8SWarner Losh 	if (walker > ep)
7231285bcc8SWarner Losh 		return;
7241285bcc8SWarner Losh 	// Now we have fplen bytes worth of file path stuff
7251285bcc8SWarner Losh 	dp = (efidp)walker;
7261285bcc8SWarner Losh 	walker += fplen;
7271285bcc8SWarner Losh 	if (walker > ep)
7281285bcc8SWarner Losh 		return;
7291285bcc8SWarner Losh 	edp = (efidp)walker;
7301285bcc8SWarner Losh 	// Everything left is the binary option args
7311285bcc8SWarner Losh 	opt = walker;
7321285bcc8SWarner Losh 	optlen = ep - walker;
7331285bcc8SWarner Losh 
734a2aa6671SWarner Losh 	indent = 1;
7351285bcc8SWarner Losh 	while (dp < edp) {
7361285bcc8SWarner Losh 		efidp_format_device_path(buf, sizeof(buf), dp,
7371285bcc8SWarner Losh 		    (intptr_t)(void *)edp - (intptr_t)(void *)dp);
738a2aa6671SWarner Losh 		printf("%*s%s\n", indent, "", buf);
739a2aa6671SWarner Losh 		indent = 10 + len + 1;
7401285bcc8SWarner Losh 		rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
7411285bcc8SWarner Losh 		if (rv == 0) {
742a2aa6671SWarner Losh 			printf("%*s%s:%s %s\n", indent + 4, "", dev, relpath, abspath);
7431285bcc8SWarner Losh 			free(dev);
7441285bcc8SWarner Losh 			free(relpath);
7451285bcc8SWarner Losh 			free(abspath);
7461285bcc8SWarner Losh 		}
7471285bcc8SWarner Losh 		dp = (efidp)((char *)dp + efidp_size(dp));
7481285bcc8SWarner Losh 	}
7491285bcc8SWarner Losh 	if (optlen == 0)
7501285bcc8SWarner Losh 		return;
7511285bcc8SWarner Losh }
7521285bcc8SWarner Losh 
7531285bcc8SWarner Losh static char *
7541285bcc8SWarner Losh get_descr(uint8_t* data)
7551285bcc8SWarner Losh {
7561285bcc8SWarner Losh 	uint8_t *pos = data;
7571285bcc8SWarner Losh 	efi_char *desc;
7581285bcc8SWarner Losh 	int  len;
7591285bcc8SWarner Losh 	char *buf;
7601285bcc8SWarner Losh 	int i = 0;
7611285bcc8SWarner Losh 
7621285bcc8SWarner Losh 	pos += sizeof(uint32_t) + sizeof(uint16_t);
7631285bcc8SWarner Losh 	desc = (efi_char*)(intptr_t)(void *)pos;
7641285bcc8SWarner Losh 	len = ucs2len(desc);
7651285bcc8SWarner Losh 	buf = malloc(len + 1);
7661285bcc8SWarner Losh 	memset(buf, 0, len + 1);
7671285bcc8SWarner Losh 	while (desc[i]) {
7681285bcc8SWarner Losh 		buf[i] = desc[i];
7691285bcc8SWarner Losh 		i++;
7701285bcc8SWarner Losh 	}
7711285bcc8SWarner Losh 	return (char*)buf;
7721285bcc8SWarner Losh }
7731285bcc8SWarner Losh 
7741285bcc8SWarner Losh 
7751285bcc8SWarner Losh /* Cmd epilogue, or just the default with no args.
7761285bcc8SWarner Losh  * The order is [bootnext] bootcurrent, timeout, order, and the bootvars [-v]
7771285bcc8SWarner Losh  */
7781285bcc8SWarner Losh static int
7791285bcc8SWarner Losh print_boot_vars(bool verbose)
7801285bcc8SWarner Losh {
7811285bcc8SWarner Losh 	/*
7821285bcc8SWarner Losh 	 * just read and print the current values
7831285bcc8SWarner Losh 	 * as a command epilogue
7841285bcc8SWarner Losh 	 */
7851285bcc8SWarner Losh 	struct entry *v;
7861285bcc8SWarner Losh 	uint32_t attrs, load_attrs;
7871285bcc8SWarner Losh 	uint8_t *data;
7881285bcc8SWarner Losh 	size_t size;
7891285bcc8SWarner Losh 	int ret;
7901285bcc8SWarner Losh 
7911285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "BootNext", &data, &size, &attrs);
7921285bcc8SWarner Losh 	if (ret > 0) {
7931285bcc8SWarner Losh 		printf("BootNext : %04x\n", le16dec(data));
7941285bcc8SWarner Losh 	}
7951285bcc8SWarner Losh 
7961285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs);
7971285bcc8SWarner Losh 	printf("BootCurrent: %04x\n", le16dec(data));
7981285bcc8SWarner Losh 
7991285bcc8SWarner Losh 	ret = efi_get_variable(EFI_GLOBAL_GUID, "Timeout", &data, &size, &attrs);
8001285bcc8SWarner Losh 	if (ret > 0) {
8011285bcc8SWarner Losh 		printf("Timeout : %d seconds\n", le16dec(data));
8021285bcc8SWarner Losh 	}
8031285bcc8SWarner Losh 	print_order();
8041285bcc8SWarner Losh 
8051285bcc8SWarner Losh 	/* now we want to fetch 'em all fresh again
8061285bcc8SWarner Losh 	 * which possibly includes a newly created bootvar
8071285bcc8SWarner Losh 	 */
8081285bcc8SWarner Losh 	LIST_FOREACH(v, &efivars, entries) {
8091285bcc8SWarner Losh 		attrs = 0;
8101285bcc8SWarner Losh 		ret = efi_get_variable(EFI_GLOBAL_GUID, v->name, &data,
8111285bcc8SWarner Losh 		    &size, &attrs);
8121285bcc8SWarner Losh 		if (ret < 0)
8131285bcc8SWarner Losh 			continue; /* we must have deleted it */
8141285bcc8SWarner Losh 		load_attrs = le32dec(data);
8151285bcc8SWarner Losh 		printf("%s%c %s", v->name,
8161285bcc8SWarner Losh 		    ((load_attrs & LOAD_OPTION_ACTIVE) ? '*': ' '),
8171285bcc8SWarner Losh 		    get_descr(data));
8181285bcc8SWarner Losh 		if (verbose)
8191285bcc8SWarner Losh 			print_loadopt_str(data, size);
8201285bcc8SWarner Losh 		else
8211285bcc8SWarner Losh 			printf("\n");
8221285bcc8SWarner Losh 	}
8231285bcc8SWarner Losh 	return 0;
8241285bcc8SWarner Losh }
8251285bcc8SWarner Losh 
8261285bcc8SWarner Losh static void
8276604afe9SWarner Losh delete_timeout(void)
8281285bcc8SWarner Losh {
8291285bcc8SWarner Losh 
8301285bcc8SWarner Losh 	efi_del_variable(EFI_GLOBAL_GUID,"Timeout");
8311285bcc8SWarner Losh }
8321285bcc8SWarner Losh 
8331285bcc8SWarner Losh static void
8341285bcc8SWarner Losh handle_timeout(int to)
8351285bcc8SWarner Losh {
8361285bcc8SWarner Losh 	uint16_t timeout;
8371285bcc8SWarner Losh 
8381285bcc8SWarner Losh 	le16enc(&timeout, to);
839c66805a5SWarner Losh 	if (set_bootvar("Timeout", (uint8_t *)&timeout, sizeof(timeout)) < 0)
840c66805a5SWarner Losh 		errx(1, "Can't set Timeout for booting.");
8411285bcc8SWarner Losh }
8421285bcc8SWarner Losh 
8431285bcc8SWarner Losh int
8441285bcc8SWarner Losh main(int argc, char *argv[])
8451285bcc8SWarner Losh {
8461285bcc8SWarner Losh 
8471285bcc8SWarner Losh 	if (!efi_variables_supported())
8481285bcc8SWarner Losh 		errx(1, "efi variables not supported on this system. root? kldload efirt?");
8491285bcc8SWarner Losh 
8501285bcc8SWarner Losh 	memset(&opts, 0, sizeof (bmgr_opts_t));
8511285bcc8SWarner Losh 	parse_args(argc, argv);
8521285bcc8SWarner Losh 	read_vars();
8531285bcc8SWarner Losh 
8541285bcc8SWarner Losh 	if (opts.create)
8551285bcc8SWarner Losh 		/*
8561285bcc8SWarner Losh 		 * side effect, adds to boot order, but not yet active.
8571285bcc8SWarner Losh 		 */
8581285bcc8SWarner Losh 		make_boot_var(opts.label, opts.loader, opts.kernel, opts.env,
8591285bcc8SWarner Losh 		    opts.dry_run);
8601285bcc8SWarner Losh 	else if (opts.set_active || opts.set_inactive )
8611285bcc8SWarner Losh 		handle_activity(opts.bootnum, opts.set_active);
8621285bcc8SWarner Losh 	else if (opts.order != NULL)
8631285bcc8SWarner Losh 		set_boot_order(opts.order); /* create a new bootorder with opts.order */
8641285bcc8SWarner Losh 	else if (opts.set_bootnext)
8651285bcc8SWarner Losh 		handle_bootnext(opts.bootnum);
8661285bcc8SWarner Losh 	else if (opts.delete_bootnext)
8671285bcc8SWarner Losh 		del_bootnext();
8681285bcc8SWarner Losh 	else if (opts.delete)
8691285bcc8SWarner Losh 		delete_bootvar(opts.bootnum);
8701285bcc8SWarner Losh 	else if (opts.del_timeout)
8711285bcc8SWarner Losh 		delete_timeout();
8721285bcc8SWarner Losh 	else if (opts.set_timeout)
8731285bcc8SWarner Losh 		handle_timeout(opts.timeout);
8741285bcc8SWarner Losh 
8751285bcc8SWarner Losh 	print_boot_vars(opts.verbose);
8761285bcc8SWarner Losh }
877