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