1 /* $NetBSD: main.c,v 1.5 2025/03/02 01:07:11 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.5 2025/03/02 01:07:11 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, 308 "sorry: %s only runs on little-endian machines!", 309 getprogname()); 310 } 311 312 setprogname(argv[0]); 313 314 optreset = 1; 315 optind = 1; 316 opterr = 1; 317 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 318 switch (ch) { 319 case OPT_BRIEF: 320 opt.brief = true; 321 break; 322 323 case OPT_DEBUG: 324 if (optarg) 325 opt.debug = strtous(optarg, NULL, 0); 326 else 327 opt.debug++; 328 opt.debug &= DEBUG_MASK; 329 break; 330 331 case '@': 332 opt.opt_fname = estrdup(optarg); 333 break; 334 335 case 'A': 336 if (optarg) 337 get_bootnum(&opt, optarg); 338 339 opt.active = false; 340 action = act_set_active; 341 break; 342 343 case 'a': 344 if (optarg) 345 get_bootnum(&opt, optarg); 346 347 opt.active = true; 348 action = act_set_active; 349 break; 350 351 case 'B': 352 if (optarg) 353 get_bootnum(&opt, optarg); 354 355 action = act_del_variable; 356 break; 357 358 case 'b': 359 get_bootnum(&opt, optarg); 360 break; 361 362 case 'C': 363 opt.prefix_bootorder = false; 364 action = act_create; 365 break; 366 367 case 'c': 368 opt.prefix_bootorder = true; 369 action = act_create; 370 break; 371 372 case 'D': 373 action = act_del_bootorder_dups; 374 break; 375 376 case 'd': 377 opt.device = estrdup(optarg); 378 break; 379 380 case 'F': 381 opt.reconnect = false; 382 break; 383 384 case 'f': 385 opt.reconnect = true; 386 break; 387 388 case 'G': 389 fname = estrdup(optarg ? optarg : "."); 390 action = act_show_gpt; 391 break; 392 393 case 'L': 394 opt.label = estrdup(optarg); 395 break; 396 397 case 'l': 398 opt.loader = estrdup(optarg); 399 break; 400 401 case 'N': 402 action = act_del_bootnext; 403 break; 404 405 case 'n': 406 opt.b_flag = true; 407 opt.bootnum = strtous(optarg, NULL, 16); 408 action = act_set_bootnext; 409 break; 410 411 case 'O': 412 action = act_del_bootorder; 413 break; 414 415 case 'o': 416 opt.bootorder = estrdup(optarg); 417 action = act_set_bootorder; 418 break; 419 420 case 'p': 421 opt.partnum = strtous(optarg, NULL, 0); 422 break; 423 424 case 'R': 425 if (opt.regexp != NULL) 426 free(opt.regexp); 427 opt.regexp = estrdup(optarg); 428 break; 429 430 case 'r': 431 if (opt.target != NULL) 432 errx(EXIT_FAILURE, 433 "only one of '-r' or '-y' are allowed"); 434 opt.target = TARGET_DRIVER; 435 break; 436 437 case 'T': 438 action = act_del_timeout; 439 break; 440 441 case 't': 442 opt.timeout = strtous(optarg, NULL, 0); 443 action = act_set_timeout; 444 break; 445 446 case 'q': 447 opt.quiet = true; 448 opt.verbose = 0; 449 break; 450 451 case 'V': 452 printf("version: %u\n", VERSION); 453 exit(EXIT_SUCCESS); 454 455 case 'v': 456 opt.verbose++; 457 opt.brief = false; 458 break; 459 460 case 'w': 461 if (optarg != NULL) { 462 opt.mbr_sig_write = MBR_SIG_WRITE_FORCE; 463 opt.mbr_sig = (uint32_t)estrtou(optarg, 0, 464 0, 0xffffffff); 465 } 466 else { 467 opt.mbr_sig_write = MBR_SIG_WRITE_MAYBE; 468 srandom((uint)time(NULL)); 469 opt.mbr_sig = (uint32_t)random(); 470 } 471 break; 472 473 case 'X': 474 action = act_remove_bootorder; 475 if (opt.csus != NULL) { 476 usage("Comma Separated Hex list" 477 " already specified!\n"); 478 } 479 opt.csus = estrdup(optarg); 480 break; 481 482 case 'x': 483 action = act_prefix_bootorder; 484 if (opt.csus != NULL) { 485 usage("Comma Separated Hex list" 486 " already specified!\n"); 487 } 488 opt.csus = estrdup(optarg); 489 break; 490 491 case 'y': 492 if (opt.target != NULL) 493 errx(EXIT_FAILURE, 494 "only one of '-r' or '-y' are allowed"); 495 opt.target = TARGET_SYSPREP; 496 break; 497 498 case 'h': 499 usage(NULL); 500 default: 501 usage("unknown option: '%c'\n", ch); 502 } 503 } 504 if (opt.target == NULL) 505 opt.target = TARGET_BOOT; 506 507 argv += optind; 508 argc -= optind; 509 510 if (argc != 0) 511 usage(NULL); 512 513 /* 514 * Check some option requirements/overrides here. 515 */ 516 if (opt.quiet) 517 opt.verbose = 0; 518 519 switch (action) { 520 case act_create: 521 if (opt.regexp != NULL) {/* override any previous setting */ 522 printf("Ignoring specified regexp: '%s'\n", 523 opt.regexp); 524 free(opt.regexp); 525 } 526 break; 527 528 case act_show_gpt: 529 return show_gpt(fname, opt.verbose); 530 531 case act_set_active: 532 case act_del_variable: 533 if (!opt.b_flag) 534 usage("please specify a boot number\n"); 535 /*FALLTHROUGH*/ 536 default: 537 if (opt.mbr_sig_write) { 538 /* 539 * This overrides all but act_create and 540 * act_show_gpt. 541 */ 542 return mbr_sig_write(opt.device, opt.mbr_sig, 543 IS_MBR_SIG_FORCE(opt), opt.verbose); 544 } 545 break; 546 } 547 548 efi_fd = open(_PATH_EFI, O_RDONLY); 549 if (efi_fd == -1) 550 err(EXIT_FAILURE, "open"); 551 552 switch (action) { 553 case act_del_bootorder_dups: return del_bootorder_dups(efi_fd, opt.target); 554 case act_del_bootorder: return del_bootorder(efi_fd, opt.target); 555 case act_del_bootnext: return del_bootnext(efi_fd); 556 case act_del_timeout: return del_timeout(efi_fd); 557 case act_del_variable: return del_variable(efi_fd, opt.target, opt.bootnum); 558 559 case act_set_active: return set_active(efi_fd, opt.target, opt.bootnum, opt.active); 560 case act_set_bootnext: return set_bootnext(efi_fd, opt.bootnum); 561 case act_set_bootorder: return set_bootorder(efi_fd, opt.target, opt.bootorder); 562 case act_set_timeout: return set_timeout(efi_fd, opt.timeout); 563 564 case act_remove_bootorder: return remove_bootorder(efi_fd, opt.target, opt.csus, 0); 565 case act_prefix_bootorder: return prefix_bootorder(efi_fd, opt.target, opt.csus, 0); 566 567 case act_show_gpt: assert(0); break; /* handled above */ 568 default: break; 569 } 570 571 /* 572 * The following actions are handled below and require a call 573 * to get_variables() using a regexp. Setup the regexp here. 574 * XXX: merge with above switch()? 575 */ 576 switch (action) { 577 case act_create: 578 easprintf(&opt.regexp, "^%s[0-9,A-F]{4}$", opt.target); 579 break; 580 581 case act_show: 582 default: 583 if (opt.regexp != NULL) 584 break; 585 586 if (opt.b_flag) { 587 easprintf(&opt.regexp, "^%s%04X$", 588 opt.target, opt.bootnum); 589 } else { 590 easprintf(&opt.regexp, "^%s", opt.target); 591 } 592 break; 593 } 594 595 var_hdl = get_variables(efi_fd, opt.regexp, &var_array, &var_cnt); 596 597 free(opt.regexp); 598 opt.regexp = NULL; 599 600 /* 601 * preform the remaining actions. 602 */ 603 switch (action) { 604 case act_create: { 605 uint16_t bootnum; 606 efi_var_t v; 607 uint32_t attrib; 608 int rv; 609 610 if (opt.device == NULL) { 611 opt.device = DEFAULT_DEVICE; 612 if (opt.device == NULL) 613 errx(EXIT_FAILURE, "specify disk with '-d'"); 614 } 615 attrib = LOAD_OPTION_ACTIVE; 616 if (opt.reconnect && IS_TARGET_DRIVER(opt)) 617 attrib |= LOAD_OPTION_FORCE_RECONNECT; 618 619 /* 620 * Get a new variable name 621 */ 622 bootnum = (uint16_t)find_new_bootvar(var_array, var_cnt, 623 opt.target); 624 easprintf(&v.name, "%s%04X", opt.target, bootnum); 625 626 if (!opt.quiet) 627 printf("creating: %s\n", v.name); 628 629 /* 630 * Initialize efi_ioc structure. 631 */ 632 efi_var_init(&v.ev, v.name, 633 &EFI_GLOBAL_VARIABLE, 634 EFI_VARIABLE_NON_VOLATILE | 635 EFI_VARIABLE_BOOTSERVICE_ACCESS | 636 EFI_VARIABLE_RUNTIME_ACCESS); 637 638 /* 639 * Setup the efi_ioc data section 640 */ 641 v.ev.data = make_bootvar_data(opt.device, opt.partnum, 642 attrib, opt.label, opt.loader, opt.opt_fname, 643 &v.ev.datasize); 644 #if 1 645 if (!opt.quiet) { 646 /* 647 * Prompt user for confirmation. 648 * XXX: Should this go away? 649 */ 650 opt.debug &= (uint)~DEBUG_BRIEF_BIT; 651 opt.debug |= DEBUG_VERBOSE_BIT; 652 show_variable(&v, opt.debug, 0); 653 654 printf("are you sure? [y/n] "); 655 if (getchar() != 'y') 656 goto done; 657 } 658 #endif 659 /* 660 * Write the variable. 661 */ 662 rv = set_variable(efi_fd, &v.ev); 663 if (rv == -1) 664 err(EXIT_FAILURE, "set_variable"); 665 666 /* 667 * Prefix the boot order if required. 668 */ 669 if (opt.prefix_bootorder) 670 rv = prefix_bootorder(efi_fd, opt.target, NULL, 671 bootnum); 672 673 /* 674 * Possibly write the MBR signature. 675 * XXX: do we really want this here? 676 */ 677 if (opt.mbr_sig_write) { 678 assert(opt.device != NULL); 679 mbr_sig_write(opt.device, opt.mbr_sig, 680 IS_MBR_SIG_FORCE(opt), opt.verbose); 681 } 682 break; 683 } 684 685 case act_show: { 686 uint max_namelen = get_max_namelen(var_array, var_cnt); 687 uint flags = opt.debug; 688 689 if (opt.verbose) 690 flags |= DEBUG_VERBOSE_BIT; 691 692 if (opt.brief) 693 flags |= DEBUG_BRIEF_BIT; 694 695 if (max_namelen > 32) 696 max_namelen = 32; 697 698 for (i = 0; i < var_cnt; i++) { 699 if (opt.brief) 700 show_generic_data(var_array[i], max_namelen); 701 else 702 show_variable(var_array[i], flags, 0); 703 } 704 break; 705 } 706 707 default: 708 assert(0); 709 break; 710 } 711 712 done: 713 free_variables(var_hdl); 714 close(efi_fd); 715 716 return 0; 717 } 718