Home | History | Annotate | Line # | Download | only in efi
main.c revision 1.4
      1 /* $NetBSD: main.c,v 1.4 2025/03/02 00:03:41 riastradh Exp $ */
      2 
      3 /*
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     14  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  * SUCH DAMAGE.
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 #ifndef lint
     28 __RCSID("$NetBSD: main.c,v 1.4 2025/03/02 00:03:41 riastradh Exp $");
     29 #endif /* not lint */
     30 
     31 #include <sys/efiio.h>
     32 #include <sys/queue.h>
     33 
     34 #include <assert.h>
     35 #include <ctype.h>
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <fcntl.h>
     39 #include <getopt.h>
     40 #include <inttypes.h>
     41 #include <limits.h>
     42 #include <regex.h>
     43 #include <stdbool.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <time.h>
     48 #include <util.h>
     49 
     50 #include "efiio.h"
     51 #include "defs.h"
     52 #include "bootvar.h"
     53 #include "devpath.h"
     54 #include "getvars.h"
     55 #include "gptsubr.h"
     56 #include "map.h"
     57 #include "setvar.h"
     58 #include "showvar.h"
     59 #include "utils.h"
     60 
     61 /*
     62  * The UEFI spec is quite clear that it is intended for little endian
     63  * machines only.  As a result (and lazyness), byte ordering is
     64  * ignored in this code and we build/run only on little endian
     65  * machines.
     66  */
     67 __CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN);
     68 
     69 #define _PATH_EFI	"/dev/efi"	/* XXX: should be in <paths.h> */
     70 
     71 #define DEFAULT_PARTITION	1
     72 #define DEFAULT_LABEL		"NetBSD"
     73 #define DEFAULT_LOADER		"\\EFI\\NetBSD\\grubx64.efi"
     74 #define DEFAULT_DEVICE		parent_of_fname(".")
     75 
     76 /****************************************/
     77 
     78 static uint __used
     79 get_max_namelen(efi_var_t **var_array, size_t var_cnt)
     80 {
     81 	uint max_len = 0;
     82 
     83 	for (size_t i = 0; i < var_cnt; i++) {
     84 		uint len = (uint)strlen(var_array[i]->name);
     85 		if (len > max_len)
     86 			max_len = len;
     87 	}
     88 	return max_len;
     89 }
     90 
     91 /************************************************************************/
     92 
     93 enum {
     94 	OPT_BRIEF = UCHAR_MAX + 1,
     95 	OPT_DEBUG = UCHAR_MAX + 2,
     96 };
     97 
     98 #define OPTIONS		"@:A::a::B::b:CcDd:FfG::hL:l:Nn:Oo:p:qR:rTt:Vvw::X:x:y"
     99 #define OPTION_LIST \
    100   _X("brief",		  _NA, OPT_BRIEF, "set brief mode") \
    101   _X("debug",		  _OA, OPT_DEBUG, "raise or set the debug level") \
    102   _X("append-binary-args",_RA, '@',	 "Append extra variable args from file") \
    103   _X("inactive",	  _OA, 'A',	 "clear active bit on '-b' variable") \
    104   _X("active",		  _OA, 'a',	 "set active bit on '-b' variable") \
    105   _X("delete-bootnum",	  _OA, 'B',	 "delete '-b' variable or arg") \
    106   _X("bootnum",		  _RA, 'b',	 "specify a boot number") \
    107   _X("create-only",	  _NA, 'C',	 "create a new boot variable") \
    108   _X("create",		  _NA, 'c',	 "create a new boot variable and prefix BootOrder") \
    109   _X("remove-dups",	  _NA, 'D',	 "remove duplicates in BootOrder") \
    110   _X("disk",		  _RA, 'd',	 "specify disk device") \
    111   _X("no-reconnect",	  _NA, 'F',	 "do not force driver reconnect after loading (requires '-r')") \
    112   _X("reconnect",	  _NA, 'f',	 "force driver reconnect after loading (requires '-r')") \
    113   _X("show-gpt",	  _OA, 'G',	 "show GPT for device") \
    114   _X("help",		  _NA, 'h',	 "this help") \
    115   _X("label",		  _RA, 'L',	 "specify boot label") \
    116   _X("loader",		  _RA, 'l',	 "specify boot loader on EFI partition") \
    117   _X("delete-bootnext",	  _NA, 'N',	 "delete NextBoot variable") \
    118   _X("bootnext",	  _RA, 'n',	 "set NextBoot variable") \
    119   _X("delete-bootorder",  _NA, 'O',	 "delete BootOrder entirely") \
    120   _X("bootorder",	  _RA, 'o',	 "set BootOrder") \
    121   _X("part",		  _RA, 'p',	 "specify partition number") \
    122   _X("quiet",		  _NA, 'q',	 "quiet mode") \
    123   _X("regexp",		  _RA, 'R',	 "regular expression for variable search (default: '^Boot')") \
    124   _X("driver",		  _NA, 'r',	 "operate on Driver#### instead of Boot####") \
    125   _X("delete-timeout",	  _NA, 'T',	 "delete timeout") \
    126   _X("timeout",		  _RA, 't',	 "set timeout to argument, in seconds") \
    127   _X("version",		  _NA, 'V',	 "show Version") \
    128   _X("verbose",		  _NA, 'v',	 "increment verboseness") \
    129   _X("write-signature",	  _OA, 'w',	 "write MBR signature") \
    130   _X("remove-bootorder",  _RA, 'X',	 "remove argument from BootOrder") \
    131   _X("prefix-bootorder",  _RA, 'x',	 "prefix argument to BootOrder") \
    132   _X("sysprep",		  _NA, 'y',	 "operate on SysPrep#### instead of Boot####")
    133 
    134 #define TARGET_BOOT	"Boot"
    135 #define TARGET_DRIVER	"Driver"
    136 #define TARGET_SYSPREP	"SysPrep"
    137 
    138 #define IS_TARGET_DRIVER(opt)	((opt).target[0] == TARGET_DRIVER[0])
    139 
    140 #define ACTION_LIST \
    141   _X(active,			NULL) \
    142   _X(create,			NULL) \
    143   _X(delete,			NULL) \
    144   _X(del_bootnext,		del_bootnext) \
    145   _X(set_bootnext,		set_bootnext) \
    146   _X(del_bootorder,		del_bootorder) \
    147   _X(set_bootorder,		set_bootorder) \
    148   _X(prefix_bootorder,		prefix_bootorder) \
    149   _X(remove_bootorder,		remove_bootorder) \
    150   _X(del_bootorder_dups,	del_bootorder_dups) \
    151   _X(set_timeout,		set_timeout) \
    152   _X(show,			NULL) \
    153   _X(show_gpt,			show_gpt)
    154 
    155 static void __dead __printflike(1, 2)
    156 usage(const char *fmt, ...)
    157 {
    158 	const char *progname = getprogname();
    159 	struct {
    160 		const char *help;
    161 		const char *name;
    162 		int opt;
    163 	} tbl[] = {
    164 #define _NA	""
    165 #define _OA	"[=arg]"
    166 #define _RA	"=<arg>"
    167 #define _X(n,a,o,m) { .name = n a, .opt = o, .help = m, },
    168 		OPTION_LIST
    169 #undef _X
    170 #undef _RA
    171 #undef _OA
    172 #undef _NA
    173 	};
    174 
    175 	if (fmt != NULL) {
    176 		va_list ap;
    177 
    178 		va_start(ap, fmt);
    179 		vfprintf(stderr, fmt, ap);
    180 		va_end(ap);
    181 	}
    182 
    183 	printf("%s version %u\n", progname, VERSION);
    184 	printf("Usage: %s [options]\n", progname);
    185 	for (size_t i = 0; i < __arraycount(tbl); i++) {
    186 		int n;
    187 		if (isprint(tbl[i].opt))
    188 			n = printf("-%c | --%s", tbl[i].opt, tbl[i].name);
    189 		else
    190 			n = printf("     --%s", tbl[i].name);
    191 		printf("%*s %s\n", 32 - n, "", tbl[i].help);
    192 	}
    193 	exit(EXIT_SUCCESS);
    194 }
    195 
    196 static int __used
    197 append_optional_data(const char *fname, efi_var_ioc_t *ev)
    198 {
    199 	char *buf, *cp;
    200 	size_t cnt;
    201 
    202 	buf = read_file(fname, &cnt);
    203 
    204 	ev->data = erealloc(ev->data, ev->datasize + cnt);
    205 	cp = ev->data;
    206 	cp += ev->datasize;
    207 	memcpy(cp, buf, cnt);
    208 	ev->datasize += cnt;
    209 
    210 	return 0;
    211 }
    212 
    213 typedef enum {
    214 	MBR_SIG_WRITE_NEVER = 0,
    215 	MBR_SIG_WRITE_MAYBE,
    216 	MBR_SIG_WRITE_FORCE,
    217 } mbr_sig_write_t;
    218 
    219 #define OPT_LIST \
    220   _X(bool,		active,			  false ) \
    221   _X(bool,		b_flag,		  false ) \
    222   _X(bool,		brief,		  false ) \
    223   _X(bool,		prefix_bootorder, false ) \
    224   _X(bool,		quiet,		  false ) \
    225   _X(bool,		reconnect,	  false ) \
    226   _X(char *,		bootorder,	  NULL ) \
    227   _X(char *,		csus,		  NULL ) \
    228   _X(char *,		device,		  NULL ) \
    229   _X(char *,		opt_fname,	  NULL ) \
    230   _X(char *,		regexp,		  NULL ) \
    231   _X(const char *,	label,		  DEFAULT_LABEL ) \
    232   _X(const char *,	loader,		  DEFAULT_LOADER ) \
    233   _X(const char *,	target,		  NULL ) \
    234   _X(int,		verbose,	  0 ) \
    235   _X(uint,		debug,		  0 ) \
    236   _X(mbr_sig_write_t,	mbr_sig_write,    MBR_SIG_WRITE_NEVER ) \
    237   _X(uint16_t,		bootnum,	  0 ) \
    238   _X(uint16_t,		partnum,	  DEFAULT_PARTITION ) \
    239   _X(uint16_t,		timeout,	  0 ) \
    240   _X(uint32_t,		mbr_sig,	  0 )
    241 
    242 #define IS_MBR_SIG_FORCE(o)	((o).mbr_sig_write == MBR_SIG_WRITE_FORCE)
    243 
    244 static struct options {	/* setable options */
    245 #define _X(t,n,v)	t n;
    246 	OPT_LIST
    247 #undef _X
    248 } opt = {
    249 #define _X(t,n,v)	.n = v,
    250 	OPT_LIST
    251 #undef _X
    252 };
    253 
    254 static inline void
    255 get_bootnum(struct options *op, const char *oarg)
    256 {
    257 
    258 	op->b_flag = true;
    259 	op->bootnum = strtous(oarg, NULL, 16);
    260 }
    261 
    262 int
    263 main(int argc, char **argv)
    264 {
    265 	static struct option longopts[] = {
    266 #define _NA	no_argument
    267 #define _OA	optional_argument
    268 #define _RA	required_argument
    269 #define _X(n,a,o,m) { n, a, NULL, o },
    270 		OPTION_LIST
    271 		{ NULL, 0, NULL, 0 },
    272 #undef _X
    273 #undef _RA
    274 #undef _OA
    275 #undef _NA
    276 	};
    277 	enum {
    278 		act_create,
    279 		act_set_active,
    280 		act_del_variable,
    281 		act_del_bootnext,
    282 		act_set_bootnext,
    283 		act_del_bootorder,
    284 		act_set_bootorder,
    285 		act_prefix_bootorder,
    286 		act_remove_bootorder,
    287 		act_del_bootorder_dups,
    288 		act_set_timeout,
    289 		act_del_timeout,
    290 		act_show,
    291 		act_show_gpt,
    292 	} action = act_show;
    293 	efi_var_t **var_array;
    294 	void *var_hdl;
    295 	char *fname = NULL;
    296 	size_t i, var_cnt;
    297 	int ch, efi_fd;
    298 
    299 	union {	/* Just in case the above __CTASSERT() is ignored ... */
    300 		uint32_t val;
    301 		uint8_t b[4];
    302 	} byte_order = { .val = 0x01020304, };
    303 	if (byte_order.b[0] != 4 ||
    304 	    byte_order.b[1] != 3 ||
    305 	    byte_order.b[2] != 2 ||
    306 	    byte_order.b[3] != 1) {
    307 		errx(EXIT_FAILURE, "sorry: %s only runs on little-endian machines!",
    308 		    getprogname());
    309 	}
    310 
    311 	setprogname(argv[0]);
    312 
    313 	optreset = 1;
    314 	optind = 1;
    315 	opterr = 1;
    316 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
    317 		switch (ch) {
    318 		case OPT_BRIEF:
    319 			opt.brief = true;
    320 			break;
    321 
    322 		case OPT_DEBUG:
    323 			if (optarg)
    324 				opt.debug = strtous(optarg, NULL, 0);
    325 			else
    326 				opt.debug++;
    327 			opt.debug &= DEBUG_MASK;
    328 			break;
    329 
    330 		case '@':
    331 			opt.opt_fname = estrdup(optarg);
    332 			break;
    333 
    334 		case 'A':
    335 			if (optarg)
    336 				get_bootnum(&opt, optarg);
    337 
    338 			opt.active = false;
    339 			action = act_set_active;
    340 			break;
    341 
    342 		case 'a':
    343 			if (optarg)
    344 				get_bootnum(&opt, optarg);
    345 
    346 			opt.active = true;
    347 			action = act_set_active;
    348 			break;
    349 
    350 		case 'B':
    351 			if (optarg)
    352 				get_bootnum(&opt, optarg);
    353 
    354 			action = act_del_variable;
    355 			break;
    356 
    357 		case 'b':
    358 			get_bootnum(&opt, optarg);
    359 			break;
    360 
    361 		case 'C':
    362 			opt.prefix_bootorder = false;
    363 			action = act_create;
    364 			break;
    365 
    366 		case 'c':
    367 			opt.prefix_bootorder = true;
    368 			action = act_create;
    369 			break;
    370 
    371 		case 'D':
    372 			action = act_del_bootorder_dups;
    373 			break;
    374 
    375 		case 'd':
    376 			opt.device = estrdup(optarg);
    377 			break;
    378 
    379 		case 'F':
    380 			opt.reconnect = false;
    381 			break;
    382 
    383 		case 'f':
    384 			opt.reconnect = true;
    385 			break;
    386 
    387 		case 'G':
    388 			fname = estrdup(optarg ? optarg : ".");
    389 			action = act_show_gpt;
    390 			break;
    391 
    392 		case 'L':
    393 			opt.label = estrdup(optarg);
    394 			break;
    395 
    396 		case 'l':
    397 			opt.loader = estrdup(optarg);
    398 			break;
    399 
    400 		case 'N':
    401 			action = act_del_bootnext;
    402 			break;
    403 
    404 		case 'n':
    405 			opt.b_flag = true;
    406 			opt.bootnum = strtous(optarg, NULL, 16);
    407 			action = act_set_bootnext;
    408 			break;
    409 
    410 		case 'O':
    411 			action = act_del_bootorder;
    412 			break;
    413 
    414 		case 'o':
    415 			opt.bootorder = estrdup(optarg);
    416 			action = act_set_bootorder;
    417 			break;
    418 
    419 		case 'p':
    420 			opt.partnum = strtous(optarg, NULL, 0);
    421 			break;
    422 
    423 		case 'R':
    424 			if (opt.regexp != NULL)
    425 				free(opt.regexp);
    426 			opt.regexp = estrdup(optarg);
    427 			break;
    428 
    429 		case 'r':
    430 			if (opt.target != NULL)
    431 				errx(EXIT_FAILURE,
    432 				    "only one of '-r' or '-y' are allowed");
    433 			opt.target = TARGET_DRIVER;
    434 			break;
    435 
    436 		case 'T':
    437 			action = act_del_timeout;
    438 			break;
    439 
    440 		case 't':
    441 			opt.timeout = strtous(optarg, NULL, 0);
    442 			action = act_set_timeout;
    443 			break;
    444 
    445 		case 'q':
    446 			opt.quiet = true;
    447 			opt.verbose = 0;
    448 			break;
    449 
    450 		case 'V':
    451 			printf("version: %u\n", VERSION);
    452 			exit(EXIT_SUCCESS);
    453 
    454 		case 'v':
    455 			opt.verbose++;
    456 			opt.brief = false;
    457 			break;
    458 
    459 		case 'w':
    460 			if (optarg != NULL) {
    461 				opt.mbr_sig_write = MBR_SIG_WRITE_FORCE;
    462 				opt.mbr_sig = (uint32_t)estrtou(optarg, 0, 0, 0xffffffff);
    463 			}
    464 			else {
    465 				opt.mbr_sig_write = MBR_SIG_WRITE_MAYBE;
    466 				srandom((uint)time(NULL));
    467 				opt.mbr_sig = (uint32_t)random();
    468 			}
    469 			break;
    470 
    471 		case 'X':
    472 			action = act_remove_bootorder;
    473 			if (opt.csus != NULL) {
    474 				usage("Comma Separated Hex list already specified!\n");
    475 			}
    476 			opt.csus = estrdup(optarg);
    477 			break;
    478 
    479 		case 'x':
    480 			action = act_prefix_bootorder;
    481 			if (opt.csus != NULL) {
    482 				usage("Comma Separated Hex list already specified!\n");
    483 			}
    484 			opt.csus = estrdup(optarg);
    485 			break;
    486 
    487 		case 'y':
    488 			if (opt.target != NULL)
    489 				errx(EXIT_FAILURE,
    490 				    "only one of '-r' or '-y' are allowed");
    491 			opt.target = TARGET_SYSPREP;
    492 			break;
    493 
    494 		case 'h':
    495 			usage(NULL);
    496 		default:
    497 			usage("unknown option: '%c'\n", ch);
    498 		}
    499 	}
    500 	if (opt.target == NULL)
    501 		opt.target = TARGET_BOOT;
    502 
    503 	argv += optind;
    504 	argc -= optind;
    505 
    506 	if (argc != 0)
    507 		usage(NULL);
    508 
    509 	/*
    510 	 * Check some option requirements/overrides here.
    511 	 */
    512 	if (opt.quiet)
    513 		opt.verbose = 0;
    514 
    515 	switch (action) {
    516 	case act_create:
    517 		if (opt.regexp != NULL) {/* override any previous setting */
    518 			printf("Ignoring specified regexp: '%s'\n",
    519 			    opt.regexp);
    520 			free(opt.regexp);
    521 		}
    522 		break;
    523 
    524 	case act_show_gpt:
    525 		return show_gpt(fname, opt.verbose);
    526 
    527 	case act_set_active:
    528 	case act_del_variable:
    529 		if (!opt.b_flag)
    530 			usage("please specify a boot number\n");
    531 		/*FALLTHROUGH*/
    532 	default:
    533 		if (opt.mbr_sig_write) {
    534 			/*
    535 			 * This overrides all but act_create and
    536 			 * act_show_gpt.
    537 			 */
    538 			return mbr_sig_write(opt.device, opt.mbr_sig,
    539 			    IS_MBR_SIG_FORCE(opt), opt.verbose);
    540 		}
    541 		break;
    542 	}
    543 
    544 	efi_fd = open(_PATH_EFI, O_RDONLY);
    545 	if (efi_fd == -1)
    546 		err(EXIT_FAILURE, "open");
    547 
    548 	switch (action) {
    549 	case act_del_bootorder_dups:	return del_bootorder_dups(efi_fd, opt.target);
    550 	case act_del_bootorder:		return del_bootorder(efi_fd, opt.target);
    551 	case act_del_bootnext:		return del_bootnext(efi_fd);
    552 	case act_del_timeout:		return del_timeout(efi_fd);
    553 	case act_del_variable:		return del_variable(efi_fd, opt.target, opt.bootnum);
    554 
    555 	case act_set_active:		return set_active(efi_fd, opt.target, opt.bootnum, opt.active);
    556 	case act_set_bootnext:		return set_bootnext(efi_fd, opt.bootnum);
    557 	case act_set_bootorder:		return set_bootorder(efi_fd, opt.target, opt.bootorder);
    558 	case act_set_timeout:		return set_timeout(efi_fd, opt.timeout);
    559 
    560 	case act_remove_bootorder:	return remove_bootorder(efi_fd, opt.target, opt.csus, 0);
    561 	case act_prefix_bootorder:	return prefix_bootorder(efi_fd, opt.target, opt.csus, 0);
    562 
    563 	case act_show_gpt:		assert(0); break; /* handled above */
    564 	default:			break;
    565 	}
    566 
    567 	/*
    568 	 * The following actions are handled below and require a call
    569 	 * to get_variables() using a regexp.  Setup the regexp here.
    570 	 * XXX: merge with above switch()?
    571 	 */
    572 	switch (action) {
    573 	case act_create:
    574 		easprintf(&opt.regexp, "^%s[0-9,A-F]{4}$", opt.target);
    575 		break;
    576 
    577 	case act_show:
    578 	default:
    579 		if (opt.regexp != NULL)
    580 			break;
    581 
    582 		if (opt.b_flag)
    583 			easprintf(&opt.regexp, "^%s%04X$", opt.target, opt.bootnum);
    584 		else
    585 			easprintf(&opt.regexp, "^%s", opt.target);
    586 		break;
    587 	}
    588 
    589 	var_hdl = get_variables(efi_fd, opt.regexp, &var_array, &var_cnt);
    590 
    591 	free(opt.regexp);
    592 	opt.regexp = NULL;
    593 
    594 	/*
    595 	 * preform the remaining actions.
    596 	 */
    597 	switch (action) {
    598 	case act_create: {
    599 		uint16_t bootnum;
    600 		efi_var_t v;
    601 		uint32_t attrib;
    602 		int rv;
    603 
    604 		if (opt.device == NULL) {
    605 			opt.device = DEFAULT_DEVICE;
    606 			if (opt.device == NULL)
    607 				errx(EXIT_FAILURE, "specify disk with '-d'");
    608 		}
    609 		attrib = LOAD_OPTION_ACTIVE;
    610 		if (opt.reconnect && IS_TARGET_DRIVER(opt))
    611 			attrib |= LOAD_OPTION_FORCE_RECONNECT;
    612 
    613 		/*
    614 		 * Get a new variable name
    615 		 */
    616 		bootnum = (uint16_t)find_new_bootvar(var_array, var_cnt, opt.target);
    617 		easprintf(&v.name, "%s%04X", opt.target, bootnum);
    618 
    619 		if (!opt.quiet)
    620 			printf("creating: %s\n", v.name);
    621 
    622 		/*
    623 		 * Initialize efi_ioc structure.
    624 		 */
    625 		efi_var_init(&v.ev, v.name,
    626 		    &EFI_GLOBAL_VARIABLE,
    627 		    EFI_VARIABLE_NON_VOLATILE |
    628 		    EFI_VARIABLE_BOOTSERVICE_ACCESS |
    629 		    EFI_VARIABLE_RUNTIME_ACCESS);
    630 
    631 		/*
    632 		 * Setup the efi_ioc data section
    633 		 */
    634 		v.ev.data = make_bootvar_data(opt.device, opt.partnum,
    635 		    attrib, opt.label, opt.loader, opt.opt_fname, &v.ev.datasize);
    636 #if 1
    637 		if (!opt.quiet) {
    638 			/*
    639 			 * Prompt user for confirmation.
    640 			 * XXX: Should this go away?
    641 			 */
    642 			opt.debug &= (uint)~DEBUG_BRIEF_BIT;
    643 			opt.debug |= DEBUG_VERBOSE_BIT;
    644 			show_variable(&v, opt.debug, 0);
    645 
    646 			printf("are you sure? [y/n] ");
    647 			if (getchar() != 'y')
    648 				goto done;
    649 		}
    650 #endif
    651 		/*
    652 		 * Write the variable.
    653 		 */
    654 		rv = set_variable(efi_fd, &v.ev);
    655 		if (rv == -1)
    656 			err(EXIT_FAILURE, "set_variable");
    657 
    658 		/*
    659 		 * Prefix the boot order if required.
    660 		 */
    661 		if (opt.prefix_bootorder)
    662 			rv = prefix_bootorder(efi_fd, opt.target, NULL,
    663 			    bootnum);
    664 
    665 		/*
    666 		 * Possibly write the MBR signature.
    667 		 * XXX: do we really want this here?
    668 		 */
    669 		if (opt.mbr_sig_write) {
    670 			assert(opt.device != NULL);
    671 			mbr_sig_write(opt.device, opt.mbr_sig,
    672 			    IS_MBR_SIG_FORCE(opt), opt.verbose);
    673 		}
    674 		break;
    675 	}
    676 
    677 	case act_show: {
    678 		uint max_namelen = get_max_namelen(var_array, var_cnt);
    679 		uint flags = opt.debug;
    680 
    681 		if (opt.verbose)
    682 			flags |= DEBUG_VERBOSE_BIT;
    683 
    684 		if (opt.brief)
    685 			flags |= DEBUG_BRIEF_BIT;
    686 
    687 		if (max_namelen > 32)
    688 			max_namelen = 32;
    689 
    690 		for (i = 0; i < var_cnt; i++) {
    691 			if (opt.brief)
    692 				show_generic_data(var_array[i], max_namelen);
    693 			else
    694 				show_variable(var_array[i], flags, 0);
    695 		}
    696 		break;
    697 	}
    698 
    699 	default:
    700 		assert(0);
    701 		break;
    702 	}
    703 
    704  done:
    705 	free_variables(var_hdl);
    706 	close(efi_fd);
    707 
    708 	return 0;
    709 }
    710