1 /*- 2 * Copyright (c) 2017-2018 Netflix, Inc. 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 * in this position and unchanged. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/stat.h> 30 #include <sys/vtoc.h> 31 #include <sys/param.h> 32 #include <assert.h> 33 #include <ctype.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <libgeom.h> 38 #include <paths.h> 39 #include <signal.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <getopt.h> 44 #include <limits.h> 45 #include <inttypes.h> 46 #include <stdbool.h> 47 #include <string.h> 48 #include <strings.h> 49 #include <unistd.h> 50 #include <libgeom.h> 51 #include <geom/geom.h> 52 #include <geom/geom_ctl.h> 53 #include <geom/geom_int.h> 54 55 #include <efivar.h> 56 #include <efiutil.h> 57 #include <efichar.h> 58 #include <efivar-dp.h> 59 60 #ifndef LOAD_OPTION_ACTIVE 61 #define LOAD_OPTION_ACTIVE 0x00000001 62 #endif 63 64 #ifndef LOAD_OPTION_CATEGORY_BOOT 65 #define LOAD_OPTION_CATEGORY_BOOT 0x00000000 66 #endif 67 68 #define BAD_LENGTH ((size_t)-1) 69 70 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001 71 72 typedef struct _bmgr_opts { 73 char *env; 74 char *loader; 75 char *label; 76 char *kernel; 77 char *name; 78 char *order; 79 int bootnum; 80 bool copy; 81 bool create; 82 bool delete; 83 bool delete_bootnext; 84 bool del_timeout; 85 bool dry_run; 86 bool device_path; 87 bool esp_device; 88 bool fw_ui; 89 bool no_fw_ui; 90 bool has_bootnum; 91 bool once; 92 int cp_src; 93 bool set_active; 94 bool set_bootnext; 95 bool set_inactive; 96 bool set_timeout; 97 int timeout; 98 bool unix_path; 99 bool verbose; 100 } bmgr_opts_t; 101 102 static struct option lopts[] = { 103 {"activate", no_argument, NULL, 'a'}, 104 {"bootnext", no_argument, NULL, 'n'}, /* set bootnext */ 105 {"bootnum", required_argument, NULL, 'b'}, 106 {"bootorder", required_argument, NULL, 'o'}, /* set order */ 107 {"copy", required_argument, NULL, 'C'}, /* Copy boot method */ 108 {"create", no_argument, NULL, 'c'}, 109 {"deactivate", no_argument, NULL, 'A'}, 110 {"del-timeout", no_argument, NULL, 'T'}, 111 {"delete", no_argument, NULL, 'B'}, 112 {"delete-bootnext", no_argument, NULL, 'N'}, 113 {"device-path", no_argument, NULL, 'd'}, 114 {"dry-run", no_argument, NULL, 'D'}, 115 {"env", required_argument, NULL, 'e'}, 116 {"esp", no_argument, NULL, 'E'}, 117 {"fw-ui", no_argument, NULL, 'f'}, 118 {"no-fw-ui", no_argument, NULL, 'F'}, 119 {"help", no_argument, NULL, 'h'}, 120 {"kernel", required_argument, NULL, 'k'}, 121 {"label", required_argument, NULL, 'L'}, 122 {"loader", required_argument, NULL, 'l'}, 123 {"once", no_argument, NULL, 'O'}, 124 {"set-timeout", required_argument, NULL, 't'}, 125 {"unix-path", no_argument, NULL, 'p'}, 126 {"verbose", no_argument, NULL, 'v'}, 127 { NULL, 0, NULL, 0} 128 }; 129 130 /* global efibootmgr opts */ 131 static bmgr_opts_t opts; 132 133 static LIST_HEAD(efivars_head, entry) efivars = 134 LIST_HEAD_INITIALIZER(efivars); 135 136 struct entry { 137 efi_guid_t guid; 138 uint32_t attrs; 139 uint8_t *data; 140 size_t size; 141 char *name; 142 char *label; 143 int idx; 144 int flags; 145 #define SEEN 1 146 147 LIST_ENTRY(entry) entries; 148 }; 149 150 #define MAX_DP_LEN 4096 151 #define MAX_LOADOPT_LEN 8192 152 153 154 static char * 155 mangle_loader(char *loader) 156 { 157 char *c; 158 159 for (c = loader; *c; c++) 160 if (*c == '/') 161 *c = '\\'; 162 163 return loader; 164 } 165 166 167 #define COMMON_ATTRS EFI_VARIABLE_NON_VOLATILE | \ 168 EFI_VARIABLE_BOOTSERVICE_ACCESS | \ 169 EFI_VARIABLE_RUNTIME_ACCESS 170 171 /* 172 * We use global guid, and common var attrs and 173 * find it better to just delete and re-create a var. 174 */ 175 static int 176 set_bootvar(const char *name, uint8_t *data, size_t size) 177 { 178 179 return efi_set_variable(EFI_GLOBAL_GUID, name, data, size, 180 COMMON_ATTRS); 181 } 182 183 184 #define USAGE \ 185 " [-aAnB -b bootnum] [-N] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help]\n\ 186 [-c -l loader [-k kernel] [-L label] [--dry-run] [-b bootnum]]" 187 188 #define CREATE_USAGE \ 189 " efibootmgr -c -l loader [-k kernel] [-L label] [--dry-run] [-b bootnum] [-a]" 190 #define ORDER_USAGE \ 191 " efibootmgr -o bootvarnum1,bootvarnum2,..." 192 #define TIMEOUT_USAGE \ 193 " efibootmgr -t seconds" 194 #define DELETE_USAGE \ 195 " efibootmgr -B -b bootnum" 196 #define ACTIVE_USAGE \ 197 " efibootmgr [-a | -A] -b bootnum" 198 #define BOOTNEXT_USAGE \ 199 " efibootmgr [-n | -N] -b bootnum" 200 201 static void 202 parse_args(int argc, char *argv[]) 203 { 204 int ch; 205 206 while ((ch = getopt_long(argc, argv, "AaBb:C:cdDe:EFfhk:L:l:NnOo:pTt:v", 207 lopts, NULL)) != -1) { 208 switch (ch) { 209 case 'A': 210 opts.set_inactive = true; 211 break; 212 case 'a': 213 opts.set_active = true; 214 break; 215 case 'b': 216 opts.has_bootnum = true; 217 opts.bootnum = strtoul(optarg, NULL, 16); 218 break; 219 case 'B': 220 opts.delete = true; 221 break; 222 case 'C': 223 opts.copy = true; 224 opts.cp_src = strtoul(optarg, NULL, 16); 225 case 'c': 226 opts.create = true; 227 break; 228 case 'D': /* should be remove dups XXX */ 229 opts.dry_run = true; 230 break; 231 case 'd': 232 opts.device_path = true; 233 break; 234 case 'e': 235 free(opts.env); 236 opts.env = strdup(optarg); 237 break; 238 case 'E': 239 opts.esp_device = true; 240 break; 241 case 'F': 242 opts.no_fw_ui = true; 243 break; 244 case 'f': 245 opts.fw_ui = true; 246 break; 247 case 'h': 248 default: 249 errx(1, "%s", USAGE); 250 break; 251 case 'k': 252 free(opts.kernel); 253 opts.kernel = strdup(optarg); 254 break; 255 case 'L': 256 free(opts.label); 257 opts.label = strdup(optarg); 258 break; 259 case 'l': 260 free(opts.loader); 261 opts.loader = strdup(optarg); 262 opts.loader = mangle_loader(opts.loader); 263 break; 264 case 'N': 265 opts.delete_bootnext = true; 266 break; 267 case 'n': 268 opts.set_bootnext = true; 269 break; 270 case 'O': 271 opts.once = true; 272 break; 273 case 'o': 274 free(opts.order); 275 opts.order = strdup(optarg); 276 break; 277 case 'p': 278 opts.unix_path = true; 279 break; 280 case 'T': 281 opts.del_timeout = true; 282 break; 283 case 't': 284 opts.set_timeout = true; 285 opts.timeout = strtoul(optarg, NULL, 10); 286 break; 287 case 'v': 288 opts.verbose = true; 289 break; 290 } 291 } 292 if (opts.create) { 293 if (!opts.loader) 294 errx(1, "%s",CREATE_USAGE); 295 return; 296 } 297 298 if (opts.order != NULL && *opts.order == '\0') 299 errx(1, "%s", ORDER_USAGE); 300 301 if ((opts.set_inactive || opts.set_active) && !opts.has_bootnum) 302 errx(1, "%s", ACTIVE_USAGE); 303 304 if (opts.delete && !opts.has_bootnum) 305 errx(1, "%s", DELETE_USAGE); 306 307 if (opts.set_bootnext && !opts.has_bootnum) 308 errx(1, "%s", BOOTNEXT_USAGE); 309 } 310 311 312 static void 313 read_vars(void) 314 { 315 316 efi_guid_t *guid; 317 char *next_name = NULL; 318 int ret = 0; 319 320 struct entry *nent; 321 322 LIST_INIT(&efivars); 323 while ((ret = efi_get_next_variable_name(&guid, &next_name)) > 0) { 324 /* 325 * Only pay attention to EFI:BootXXXX variables to get the list. 326 */ 327 if (efi_guid_cmp(guid, &EFI_GLOBAL_GUID) != 0 || 328 strlen(next_name) != 8 || 329 strncmp(next_name, "Boot", 4) != 0 || 330 !isxdigit(next_name[4]) || 331 !isxdigit(next_name[5]) || 332 !isxdigit(next_name[6]) || 333 !isxdigit(next_name[7])) 334 continue; 335 nent = malloc(sizeof(struct entry)); 336 nent->name = strdup(next_name); 337 338 ret = efi_get_variable(*guid, next_name, &nent->data, 339 &nent->size, &nent->attrs); 340 if (ret < 0) 341 err(1, "efi_get_variable"); 342 nent->guid = *guid; 343 nent->idx = strtoul(&next_name[4], NULL, 16); 344 LIST_INSERT_HEAD(&efivars, nent, entries); 345 } 346 } 347 348 349 static void 350 set_boot_order(char *order) 351 { 352 uint16_t *new_data; 353 size_t size; 354 char *next, *cp; 355 int cnt; 356 int i; 357 358 cp = order; 359 cnt = 1; 360 while (*cp) { 361 if (*cp++ == ',') 362 cnt++; 363 } 364 size = sizeof(uint16_t) * cnt; 365 new_data = malloc(size); 366 367 i = 0; 368 cp = strdup(order); 369 while ((next = strsep(&cp, ",")) != NULL) { 370 new_data[i] = strtoul(next, NULL, 16); 371 if (new_data[i] == 0 && errno == EINVAL) { 372 warnx("can't parse %s as a numb", next); 373 errx(1, "%s", ORDER_USAGE); 374 } 375 i++; 376 } 377 free(cp); 378 if (set_bootvar("BootOrder", (uint8_t*)new_data, size) < 0) 379 err(1, "Unabke to set BootOrder to %s", order); 380 free(new_data); 381 } 382 383 static void 384 handle_activity(int bootnum, bool active) 385 { 386 uint32_t attrs, load_attrs; 387 uint8_t *data; 388 size_t size; 389 char *name; 390 391 asprintf(&name, "%s%04X", "Boot", bootnum); 392 if (name == NULL) 393 err(1, "asprintf"); 394 if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, &attrs) < 0) 395 err(1, "No such bootvar %s\n", name); 396 397 load_attrs = le32dec(data); 398 399 if (active) 400 load_attrs |= LOAD_OPTION_ACTIVE; 401 else 402 load_attrs &= ~LOAD_OPTION_ACTIVE; 403 404 le32enc(data, load_attrs); 405 406 if (set_bootvar(name, data, size) < 0) 407 err(1, "handle activity efi_set_variable"); 408 } 409 410 411 /* 412 * add boot var to boot order. 413 * called by create boot var. There is no option 414 * to add one independent of create. 415 * 416 * Note: we currently don't support where it goes 417 * so it goes on the front, inactive. 418 * use -o 2,3,7 etc to affect order, -a to activate. 419 */ 420 static void 421 add_to_boot_order(char *bootvar) 422 { 423 size_t size; 424 uint32_t attrs; 425 uint16_t val; 426 uint8_t *data, *new; 427 428 val = strtoul(&bootvar[4], NULL, 16); 429 430 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) { 431 if (errno == ENOENT) { /* create it and set this bootvar to active */ 432 size = 0; 433 data = NULL; 434 } else 435 err(1, "efi_get_variable BootOrder"); 436 } 437 438 /* 439 * We have BootOrder with the current order 440 * so grow the array by one, add the value 441 * and write the new variable value. 442 */ 443 size += sizeof(uint16_t); 444 new = malloc(size); 445 if (!new) 446 err(1, "malloc"); 447 448 le16enc(new, val); 449 if (size > sizeof(uint16_t)) 450 memcpy(new + sizeof(uint16_t), data, size - sizeof(uint16_t)); 451 452 if (set_bootvar("BootOrder", new, size) < 0) 453 err(1, "set_bootvar"); 454 free(new); 455 } 456 457 458 static void 459 remove_from_order(uint16_t bootnum) 460 { 461 uint32_t attrs; 462 size_t size, i, j; 463 uint8_t *new, *data; 464 465 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) 466 return; 467 468 new = malloc(size); 469 if (new == NULL) 470 err(1, "malloc"); 471 472 for (j = i = 0; i < size; i += sizeof(uint16_t)) { 473 if (le16dec(data + i) == bootnum) 474 continue; 475 memcpy(new + j, data + i, sizeof(uint16_t)); 476 j += sizeof(uint16_t); 477 } 478 if (i == j) 479 warnx("Boot variable %04x not in BootOrder", bootnum); 480 else if (set_bootvar("BootOrder", new, j) < 0) 481 err(1, "Unable to update BootOrder with new value"); 482 free(new); 483 } 484 485 486 static void 487 delete_bootvar(int bootnum) 488 { 489 char *name; 490 int defer = 0; 491 492 /* 493 * Try to delete the boot variable and remocve it 494 * from the boot order. We always do both actions 495 * to make it easy to clean up from oopses. 496 */ 497 if (bootnum < 0 || bootnum > 0xffff) 498 errx(1, "Bad boot variable %#x", bootnum); 499 asprintf(&name, "%s%04X", "Boot", bootnum); 500 if (name == NULL) 501 err(1, "asprintf"); 502 printf("Removing boot variable '%s'\n", name); 503 if (efi_del_variable(EFI_GLOBAL_GUID, name) < 0) { 504 defer = 1; 505 warn("cannot delete variable %s", name); 506 } 507 printf("Removing 0x%x from BootOrder\n", bootnum); 508 remove_from_order(bootnum); 509 free(name); 510 if (defer) 511 exit(defer); 512 } 513 514 515 static void 516 del_bootnext(void) 517 { 518 519 if (efi_del_variable(EFI_GLOBAL_GUID, "BootNext") < 0) 520 err(1, "efi_del_variable"); 521 } 522 523 static void 524 handle_bootnext(uint16_t bootnum) 525 { 526 uint16_t num; 527 528 le16enc(&num, bootnum); 529 if (set_bootvar("BootNext", (uint8_t*)&bootnum, sizeof(uint16_t)) < 0) 530 err(1, "set_bootvar"); 531 } 532 533 534 static int 535 compare(const void *a, const void *b) 536 { 537 uint16_t c; 538 uint16_t d; 539 540 memcpy(&c, a, sizeof(uint16_t)); 541 memcpy(&d, b, sizeof(uint16_t)); 542 543 if (c < d) 544 return -1; 545 if (c == d) 546 return 0; 547 return 1; 548 } 549 550 static char * 551 make_next_boot_var_name(void) 552 { 553 struct entry *v; 554 uint16_t *vals, next_free = 0; 555 char *name; 556 int cnt = 0; 557 int i; 558 559 LIST_FOREACH(v, &efivars, entries) { 560 cnt++; 561 } 562 563 vals = malloc(sizeof(uint16_t) * cnt); 564 if (!vals) 565 return NULL; 566 567 i = 0; 568 LIST_FOREACH(v, &efivars, entries) { 569 vals[i++] = v->idx; 570 } 571 qsort(vals, cnt, sizeof(uint16_t), compare); 572 /* if the hole is at the beginning, just return zero */ 573 if (vals[0] > 0) { 574 next_free = 0; 575 } else { 576 /* now just run the list looking for the first hole */ 577 for (i = 0; i < cnt - 1 && next_free == 0; i++) 578 if (vals[i] + 1 != vals[i + 1]) 579 next_free = vals[i] + 1; 580 if (next_free == 0) 581 next_free = vals[cnt - 1] + 1; 582 /* In theory we could have used all 65k slots -- what to do? */ 583 } 584 free(vals); 585 586 asprintf(&name, "%s%04X", "Boot", next_free); 587 if (name == NULL) 588 err(1, "asprintf"); 589 return name; 590 } 591 592 static char * 593 make_boot_var_name(uint16_t bootnum) 594 { 595 struct entry *v; 596 char *name; 597 598 LIST_FOREACH(v, &efivars, entries) { 599 if (v->idx == bootnum) 600 return NULL; 601 } 602 603 asprintf(&name, "%s%04X", "Boot", bootnum); 604 if (name == NULL) 605 err(1, "asprintf"); 606 return name; 607 } 608 609 static size_t 610 create_loadopt(uint8_t *buf, size_t bufmax, uint32_t attributes, efidp dp, size_t dp_size, 611 const char *description, const uint8_t *optional_data, size_t optional_data_size) 612 { 613 efi_char *bbuf = NULL; 614 uint8_t *pos = buf; 615 size_t desc_len = 0; 616 size_t len; 617 618 if (optional_data == NULL && optional_data_size != 0) 619 return BAD_LENGTH; 620 if (dp == NULL && dp_size != 0) 621 return BAD_LENGTH; 622 623 /* 624 * Compute the length to make sure the passed in buffer is long enough. 625 */ 626 utf8_to_ucs2(description, &bbuf, &desc_len); 627 len = sizeof(uint32_t) + sizeof(uint16_t) + desc_len + dp_size + optional_data_size; 628 if (len > bufmax) { 629 free(bbuf); 630 return BAD_LENGTH; 631 } 632 633 le32enc(pos, attributes); 634 pos += sizeof (attributes); 635 636 le16enc(pos, dp_size); 637 pos += sizeof (uint16_t); 638 639 memcpy(pos, bbuf, desc_len); /* NB:desc_len includes strailing NUL */ 640 pos += desc_len; 641 free(bbuf); 642 643 memcpy(pos, dp, dp_size); 644 pos += dp_size; 645 646 if (optional_data && optional_data_size > 0) { 647 memcpy(pos, optional_data, optional_data_size); 648 pos += optional_data_size; 649 } 650 651 return pos - buf; 652 } 653 654 655 static int 656 make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run, 657 int bootnum, bool activate) 658 { 659 struct entry *new_ent; 660 uint32_t load_attrs = 0; 661 uint8_t *load_opt_buf; 662 size_t lopt_size, llen, klen; 663 efidp dp, loaderdp, kerneldp; 664 char *bootvar = NULL; 665 int ret; 666 667 assert(label != NULL); 668 669 if (bootnum == -1) 670 bootvar = make_next_boot_var_name(); 671 else 672 bootvar = make_boot_var_name((uint16_t)bootnum); 673 if (bootvar == NULL) 674 err(1, "bootvar creation"); 675 if (loader == NULL) 676 errx(1, "Must specify boot loader"); 677 if (efivar_unix_path_to_device_path(loader, &loaderdp) != 0) 678 err(1, "Cannot translate unix loader path '%s' to UEFI", loader); 679 if (kernel != NULL) { 680 if (efivar_unix_path_to_device_path(kernel, &kerneldp) != 0) 681 err(1, "Cannot translate unix kernel path '%s' to UEFI", kernel); 682 } else { 683 kerneldp = NULL; 684 } 685 llen = efidp_size(loaderdp); 686 if (llen > MAX_DP_LEN) 687 errx(1, "Loader path too long."); 688 klen = efidp_size(kerneldp); 689 if (klen > MAX_DP_LEN) 690 errx(1, "Kernel path too long."); 691 dp = malloc(llen + klen); 692 if (dp == NULL) 693 errx(1, "Can't allocate memory for new device paths"); 694 memcpy(dp, loaderdp, llen); 695 if (kerneldp != NULL) 696 memcpy((char *)dp + llen, kerneldp, klen); 697 698 /* don't make the new bootvar active by default, use the -a option later */ 699 load_attrs = LOAD_OPTION_CATEGORY_BOOT; 700 if (activate) 701 load_attrs |= LOAD_OPTION_ACTIVE; 702 load_opt_buf = malloc(MAX_LOADOPT_LEN); 703 if (load_opt_buf == NULL) 704 err(1, "malloc"); 705 706 lopt_size = create_loadopt(load_opt_buf, MAX_LOADOPT_LEN, load_attrs, 707 dp, llen + klen, label, env, env ? strlen(env) + 1 : 0); 708 if (lopt_size == BAD_LENGTH) 709 errx(1, "Can't create loadopt"); 710 711 ret = 0; 712 if (!dry_run) { 713 ret = efi_set_variable(EFI_GLOBAL_GUID, bootvar, 714 (uint8_t*)load_opt_buf, lopt_size, COMMON_ATTRS); 715 } 716 717 if (ret) 718 err(1, "efi_set_variable"); 719 720 if (!dry_run) 721 add_to_boot_order(bootvar); /* first, still not active */ 722 new_ent = malloc(sizeof(struct entry)); 723 if (new_ent == NULL) 724 err(1, "malloc"); 725 memset(new_ent, 0, sizeof(struct entry)); 726 new_ent->name = bootvar; 727 new_ent->guid = EFI_GLOBAL_GUID; 728 LIST_INSERT_HEAD(&efivars, new_ent, entries); 729 free(load_opt_buf); 730 free(dp); 731 732 return 0; 733 } 734 735 736 static void 737 print_loadopt_str(uint8_t *data, size_t datalen) 738 { 739 char *dev, *relpath, *abspath; 740 uint32_t attr; 741 uint16_t fplen; 742 efi_char *descr; 743 uint8_t *ep = data + datalen; 744 uint8_t *walker = data; 745 efidp dp, edp; 746 char buf[1024]; 747 int len; 748 int rv; 749 int indent; 750 751 if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char)) 752 return; 753 // First 4 bytes are attribute flags 754 attr = le32dec(walker); 755 walker += sizeof(attr); 756 // Next two bytes are length of the file paths 757 fplen = le16dec(walker); 758 walker += sizeof(fplen); 759 // Next we have a 0 terminated UCS2 string that we know to be aligned 760 descr = (efi_char *)(intptr_t)(void *)walker; 761 len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2) 762 walker += (len + 1) * sizeof(efi_char); 763 if (walker > ep) 764 return; 765 // Now we have fplen bytes worth of file path stuff 766 dp = (efidp)walker; 767 walker += fplen; 768 if (walker > ep) 769 return; 770 edp = (efidp)walker; 771 /* 772 * Everything left is the binary option args 773 * opt = walker; 774 * optlen = ep - walker; 775 */ 776 indent = 1; 777 while (dp < edp) { 778 efidp_format_device_path(buf, sizeof(buf), dp, 779 (intptr_t)(void *)edp - (intptr_t)(void *)dp); 780 printf("%*s%s\n", indent, "", buf); 781 indent = 10 + len + 1; 782 rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath); 783 if (rv == 0) { 784 printf("%*s%s:%s %s\n", indent + 4, "", dev, relpath, abspath); 785 free(dev); 786 free(relpath); 787 free(abspath); 788 } 789 dp = (efidp)((char *)dp + efidp_size(dp)); 790 } 791 } 792 793 static char * 794 get_descr(uint8_t *data) 795 { 796 uint8_t *pos = data; 797 efi_char *desc; 798 int len; 799 char *buf; 800 int i = 0; 801 802 pos += sizeof(uint32_t) + sizeof(uint16_t); 803 desc = (efi_char*)(intptr_t)(void *)pos; 804 len = ucs2len(desc); 805 buf = malloc(len + 1); 806 memset(buf, 0, len + 1); 807 while (desc[i]) { 808 buf[i] = desc[i]; 809 i++; 810 } 811 return (char*)buf; 812 } 813 814 815 static bool 816 print_boot_var(const char *name, bool verbose, bool curboot) 817 { 818 size_t size; 819 uint32_t load_attrs; 820 uint8_t *data; 821 int ret; 822 char *d; 823 824 ret = efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, NULL); 825 if (ret < 0) 826 return false; 827 load_attrs = le32dec(data); 828 d = get_descr(data); 829 printf("%c%s%c %s", curboot ? '+' : ' ', name, 830 ((load_attrs & LOAD_OPTION_ACTIVE) ? '*': ' '), d); 831 free(d); 832 if (verbose) 833 print_loadopt_str(data, size); 834 else 835 printf("\n"); 836 return true; 837 } 838 839 840 static bool 841 os_indication_supported(uint64_t indication) 842 { 843 uint8_t *data; 844 size_t size; 845 uint32_t attrs; 846 int ret; 847 848 ret = efi_get_variable(EFI_GLOBAL_GUID, "OsIndicationsSupported", &data, 849 &size, &attrs); 850 if (ret < 0) 851 return false; 852 return (le64dec(data) & indication) == indication; 853 } 854 855 static uint64_t 856 os_indications(void) 857 { 858 uint8_t *data; 859 size_t size; 860 uint32_t attrs; 861 int ret; 862 863 ret = efi_get_variable(EFI_GLOBAL_GUID, "OsIndications", &data, &size, 864 &attrs); 865 if (ret < 0) 866 return 0; 867 return le64dec(data); 868 } 869 870 static int 871 os_indications_set(uint64_t mask, uint64_t val) 872 { 873 uint8_t new[sizeof(uint64_t)]; 874 875 le64enc(&new, (os_indications() & ~mask) | (val & mask)); 876 return set_bootvar("OsIndications", new, sizeof(new)); 877 } 878 879 /* Cmd epilogue, or just the default with no args. 880 * The order is [bootnext] bootcurrent, timeout, order, and the bootvars [-v] 881 */ 882 static int 883 print_boot_vars(bool verbose) 884 { 885 /* 886 * just read and print the current values 887 * as a command epilogue 888 */ 889 struct entry *v; 890 uint8_t *data; 891 size_t size; 892 uint32_t attrs; 893 int ret, bolen; 894 uint16_t *boot_order = NULL, current; 895 bool boot_to_fw_ui; 896 897 if (os_indication_supported(EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) { 898 boot_to_fw_ui = 899 (os_indications() & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) != 0; 900 printf("Boot to FW : %s\n", boot_to_fw_ui != 0 ? 901 "true" : "false"); 902 } 903 904 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootNext", &data, &size, &attrs); 905 if (ret > 0) { 906 printf("BootNext : %04x\n", le16dec(data)); 907 } 908 909 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs); 910 current = le16dec(data); 911 printf("BootCurrent: %04x\n", current); 912 913 ret = efi_get_variable(EFI_GLOBAL_GUID, "Timeout", &data, &size, &attrs); 914 if (ret > 0) { 915 printf("Timeout : %d seconds\n", le16dec(data)); 916 } 917 918 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) > 0) { 919 if (size % 2 == 1) 920 warn("Bad BootOrder variable: odd length %d", (int)size); 921 boot_order = malloc(size); 922 bolen = size / 2; 923 printf("BootOrder : "); 924 for (size_t i = 0; i < size; i += 2) { 925 boot_order[i / 2] = le16dec(data + i); 926 printf("%04X%s", boot_order[i / 2], i == size - 2 ? "\n" : ", "); 927 } 928 } 929 930 if (boot_order == NULL) { 931 /* 932 * now we want to fetch 'em all fresh again 933 * which possibly includes a newly created bootvar 934 */ 935 LIST_FOREACH(v, &efivars, entries) { 936 print_boot_var(v->name, verbose, v->idx == current); 937 } 938 } else { 939 LIST_FOREACH(v, &efivars, entries) { 940 v->flags = 0; 941 } 942 for (int i = 0; i < bolen; i++) { 943 char buffer[10]; 944 945 snprintf(buffer, sizeof(buffer), "Boot%04X", boot_order[i]); 946 if (!print_boot_var(buffer, verbose, boot_order[i] == current)) 947 printf("%s: MISSING!\n", buffer); 948 LIST_FOREACH(v, &efivars, entries) { 949 if (v->idx == boot_order[i]) { 950 v->flags |= SEEN; 951 break; 952 } 953 } 954 } 955 if (verbose) { 956 printf("\n\nUnreferenced Variables:\n"); 957 LIST_FOREACH(v, &efivars, entries) { 958 if (v->flags == 0) 959 print_boot_var(v->name, verbose, v->idx == current); 960 } 961 } 962 } 963 return 0; 964 } 965 966 static void 967 delete_timeout(void) 968 { 969 970 efi_del_variable(EFI_GLOBAL_GUID,"Timeout"); 971 } 972 973 static void 974 handle_timeout(int to) 975 { 976 uint16_t timeout; 977 978 le16enc(&timeout, to); 979 if (set_bootvar("Timeout", (uint8_t *)&timeout, sizeof(timeout)) < 0) 980 errx(1, "Can't set Timeout for booting."); 981 } 982 983 static void 984 report_esp_device(bool do_dp, bool do_unix) 985 { 986 uint8_t *data; 987 size_t size, len; 988 uint32_t attrs; 989 int ret; 990 uint16_t current, fplen; 991 char *name, *dev, *relpath, *abspath; 992 uint8_t *walker, *ep; 993 efi_char *descr; 994 efidp dp, edp; 995 char buf[PATH_MAX]; 996 997 if (do_dp && do_unix) 998 errx(1, "Can't report both UEFI device-path and Unix path together"); 999 1000 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs); 1001 if (ret < 0) 1002 err(1, "Can't get BootCurrent"); 1003 current = le16dec(data); 1004 if (asprintf(&name, "Boot%04X", current) < 0) 1005 err(1, "Can't format boot var\n"); 1006 if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, NULL) < 0) 1007 err(1, "Can't retrieve EFI var %s", name); 1008 // First 4 bytes are attribute flags 1009 walker = data; 1010 ep = walker + size; 1011 walker += sizeof(uint32_t); 1012 // Next two bytes are length of the file paths 1013 fplen = le16dec(walker); 1014 walker += sizeof(fplen); 1015 // Next we have a 0 terminated UCS2 string that we know to be aligned 1016 descr = (efi_char *)(intptr_t)(void *)walker; 1017 len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2) 1018 walker += (len + 1) * sizeof(efi_char); 1019 if (walker > ep) 1020 errx(1, "malformed boot variable %s", name); 1021 // Now we have fplen bytes worth of file path stuff 1022 dp = (efidp)walker; 1023 walker += fplen; 1024 edp = (efidp)walker; 1025 if (walker > ep) 1026 errx(1, "malformed boot variable %s", name); 1027 if (do_dp) { 1028 efidp_format_device_path_node(buf, sizeof(buf), dp); 1029 printf("%s\n", buf); 1030 exit(0); 1031 } 1032 if (efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath) < 0) 1033 errx(1, "Can't convert to unix path"); 1034 if (do_unix) { 1035 if (abspath == NULL) 1036 errx(1, "Can't find where %s:%s is mounted", 1037 dev, relpath); 1038 abspath[strlen(abspath) - strlen(relpath) - 1] = '\0'; 1039 printf("%s\n", abspath); 1040 } else { 1041 printf("%s\n", dev); 1042 } 1043 free(dev); 1044 free(relpath); 1045 free(abspath); 1046 exit(0); 1047 } 1048 1049 static void 1050 set_boot_to_fw_ui(bool to_fw) 1051 { 1052 int ret; 1053 1054 if (!os_indication_supported(EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) { 1055 if (to_fw) 1056 errx(1, "boot to fw ui not supported"); 1057 else 1058 return; 1059 } 1060 ret = os_indications_set(EFI_OS_INDICATIONS_BOOT_TO_FW_UI, 1061 to_fw ? ~0 : 0); 1062 if (ret < 0) 1063 errx(1, "failed to set boot to fw ui"); 1064 } 1065 1066 int 1067 main(int argc, char *argv[]) 1068 { 1069 1070 if (!efi_variables_supported()) 1071 errx(1, "efi variables not supported on this system. root? kldload efirt?"); 1072 1073 memset(&opts, 0, sizeof (bmgr_opts_t)); 1074 parse_args(argc, argv); 1075 read_vars(); 1076 1077 if (opts.create) 1078 /* 1079 * side effect, adds to boot order, but not yet active. 1080 */ 1081 make_boot_var(opts.label ? opts.label : "", 1082 opts.loader, opts.kernel, opts.env, opts.dry_run, 1083 opts.has_bootnum ? opts.bootnum : -1, opts.set_active); 1084 else if (opts.set_active || opts.set_inactive ) 1085 handle_activity(opts.bootnum, opts.set_active); 1086 else if (opts.order != NULL) 1087 set_boot_order(opts.order); /* create a new bootorder with opts.order */ 1088 else if (opts.set_bootnext) 1089 handle_bootnext(opts.bootnum); 1090 else if (opts.delete_bootnext) 1091 del_bootnext(); 1092 else if (opts.delete) 1093 delete_bootvar(opts.bootnum); 1094 else if (opts.del_timeout) 1095 delete_timeout(); 1096 else if (opts.set_timeout) 1097 handle_timeout(opts.timeout); 1098 else if (opts.esp_device) 1099 report_esp_device(opts.device_path, opts.unix_path); 1100 else if (opts.fw_ui) 1101 set_boot_to_fw_ui(true); 1102 else if (opts.no_fw_ui) 1103 set_boot_to_fw_ui(false); 1104 1105 print_boot_vars(opts.verbose); 1106 } 1107