1 /*- 2 * Copyright (c) 2017 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 once; 86 int cp_src; 87 bool set_active; 88 bool set_bootnext; 89 bool set_inactive; 90 bool set_timeout; 91 int timeout; 92 bool verbose; 93 } bmgr_opts_t; 94 95 static struct option lopts[] = { 96 {"activate", required_argument, NULL, 'a'}, 97 {"bootnext", required_argument, NULL, 'n'}, /* set bootnext */ 98 {"bootorder", required_argument, NULL, 'o'}, /* set order */ 99 {"copy", required_argument, NULL, 'C'}, /* Copy boot method */ 100 {"create", no_argument, NULL, 'c'}, 101 {"deactivate", required_argument, NULL, 'A'}, 102 {"del-timout", no_argument, NULL, 'T'}, 103 {"delete", required_argument, NULL, 'B'}, 104 {"delete-bootnext", required_argument, NULL, 'N'}, 105 {"device", required_argument, NULL, 'd'}, 106 {"dry-run", no_argument, NULL, 'D'}, 107 {"env", required_argument, NULL, 'e'}, 108 {"help", no_argument, NULL, 'h'}, 109 {"kernel", required_argument, NULL, 'k'}, 110 {"label", required_argument, NULL, 'L'}, 111 {"loader", required_argument, NULL, 'l'}, 112 {"once", no_argument, NULL, 'O'}, 113 {"partition", required_argument, NULL, 'p'}, 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 part; 134 135 LIST_ENTRY(entry) entries; 136 }; 137 138 #define MAX_DP_LEN 4096 139 #define MAX_LOADOPT_LEN 8192 140 141 142 static char * 143 mangle_loader(char *loader) 144 { 145 char *c; 146 147 for (c = loader; *c; c++) 148 if (*c == '/') 149 *c = '\\'; 150 151 return loader; 152 } 153 154 155 #define COMMON_ATTRS EFI_VARIABLE_NON_VOLATILE | \ 156 EFI_VARIABLE_BOOTSERVICE_ACCESS | \ 157 EFI_VARIABLE_RUNTIME_ACCESS 158 159 /* 160 * We use global guid, and common var attrs and 161 * find it better to just delete and re-create a var. 162 */ 163 static int 164 set_bootvar(const char *name, uint8_t *data, size_t size) 165 { 166 167 efi_del_variable(EFI_GLOBAL_GUID, name); 168 return efi_set_variable(EFI_GLOBAL_GUID, name, data, size, 169 COMMON_ATTRS); 170 } 171 172 173 #define USAGE \ 174 " [-aAnNB Bootvar] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help] \n \ 175 [-c -d device -p partition -l loader [-L label] [--dry-run]]" 176 177 #define CREATE_USAGE \ 178 " efibootmgr -c -d device -p partition -loader loader [-L label ] [--dry-run]" 179 #define ORDER_USAGE \ 180 " efibootmgr -o bootvarnum1,bootvarnum2,..." 181 #define TIMEOUT_USAGE \ 182 " efibootmgr -t seconds" 183 #define DELETE_USAGE \ 184 " efibootmgr -B bootvarnum" 185 #define ACTIVE_USAGE \ 186 " efibootmgr [-a | -A] bootvarnum" 187 #define BOOTNEXT_USAGE \ 188 " efibootmgr [-n | -N] bootvarnum" 189 190 static void 191 parse_args(int argc, char *argv[]) 192 { 193 int ch; 194 195 while ((ch = getopt_long(argc, argv, "A:a:B:C:cDe:hk:L:l:Nn:Oo:Tt:v", 196 lopts, NULL)) != -1) { 197 switch (ch) { 198 case 'A': 199 opts.set_inactive = true; 200 opts.bootnum = strtoul(optarg, NULL, 16); 201 break; 202 case 'a': 203 opts.set_active = true; 204 opts.bootnum = strtoul(optarg, NULL, 16); 205 break; 206 case 'B': 207 opts.delete = true; 208 opts.bootnum = strtoul(optarg, NULL, 16); 209 break; 210 case 'C': 211 opts.copy = true; 212 opts.cp_src = strtoul(optarg, NULL, 16); 213 case 'c': 214 opts.create = true; 215 break; 216 case 'D': /* should be remove dups XXX */ 217 opts.dry_run = true; 218 break; 219 case 'e': 220 opts.env = strdup(optarg); 221 break; 222 case 'h': 223 default: 224 errx(1, "%s", USAGE); 225 break; 226 case 'k': 227 opts.kernel = strdup(optarg); 228 break; 229 case 'L': 230 opts.label = strdup(optarg); 231 break; 232 case 'l': 233 opts.loader = strdup(optarg); 234 opts.loader = mangle_loader(opts.loader); 235 break; 236 case 'N': 237 opts.delete_bootnext = true; 238 break; 239 case 'n': 240 opts.set_bootnext = true; 241 opts.bootnum = strtoul(optarg, NULL, 16); 242 break; 243 case 'O': 244 opts.once = true; 245 break; 246 case 'o': 247 opts.order = strdup(optarg); 248 break; 249 case 'T': 250 opts.del_timeout = true; 251 break; 252 case 't': 253 opts.set_timeout = true; 254 opts.timeout = strtoul(optarg, NULL, 10); 255 break; 256 case 'v': 257 opts.verbose = true; 258 break; 259 } 260 } 261 if (opts.create) { 262 if (!opts.loader) 263 errx(1, "%s",CREATE_USAGE); 264 return; 265 } 266 if (opts.set_bootnext && !(opts.bootnum)) 267 errx(1, "%s", BOOTNEXT_USAGE); 268 269 if ((opts.set_active || opts.set_inactive) && !(opts.bootnum)) 270 errx(1, "%s", ACTIVE_USAGE); 271 272 if (opts.order && !(opts.order)) 273 errx(1, "%s", ORDER_USAGE); 274 } 275 276 277 static void 278 print_order(void) 279 { 280 uint32_t attrs; 281 uint8_t *data; 282 size_t size, i; 283 284 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) 285 errx(1, "Couldn't get value for BootOrder\n"); 286 287 if (size % 2 == 1) 288 errx(1, "Bad BootOrder variable: odd length"); 289 290 printf("BootOrder : "); 291 for (i = 0; i < size; i += 2) 292 printf("%04x%s", le16dec(data + i), i == size - 2 ? "\n" : ", "); 293 } 294 295 296 static void 297 read_vars(void) 298 { 299 300 efi_guid_t *guid; 301 char *next_name = NULL; 302 int ret = 0; 303 304 struct entry *nent; 305 306 LIST_INIT(&efivars); 307 while ((ret = efi_get_next_variable_name(&guid, &next_name)) > 0) { 308 /* 309 * Only pay attention to EFI:BootXXXX variables to get the list. 310 */ 311 if (efi_guid_cmp(guid, &EFI_GLOBAL_GUID) != 0 || 312 strlen(next_name) != 8 || 313 strncmp(next_name, "Boot", 4) != 0 || 314 !isxdigit(next_name[4]) || 315 !isxdigit(next_name[5]) || 316 !isxdigit(next_name[6]) || 317 !isxdigit(next_name[7])) 318 continue; 319 nent = malloc(sizeof(struct entry)); 320 nent->name = strdup(next_name); 321 322 ret = efi_get_variable(*guid, next_name, &nent->data, 323 &nent->size, &nent->attrs); 324 if (ret < 0) 325 err(1, "efi_get_variable"); 326 nent->guid = *guid; 327 nent->idx = strtoul(&next_name[4], NULL, 16); 328 LIST_INSERT_HEAD(&efivars, nent, entries); 329 } 330 } 331 332 333 static void 334 set_boot_order(char *order) 335 { 336 uint16_t *new_data; 337 size_t size; 338 char *next, *cp; 339 int cnt; 340 int i; 341 342 cp = order; 343 cnt = 1; 344 while (*cp) { 345 if (*cp++ == ',') 346 cnt++; 347 } 348 size = sizeof(uint16_t) * cnt; 349 new_data = malloc(size); 350 351 i = 0; 352 cp = strdup(order); 353 while ((next = strsep(&cp, ",")) != NULL) { 354 new_data[i] = strtoul(next, NULL, 16); 355 if (new_data[i] == 0 && errno == EINVAL) { 356 warnx("can't parse %s as a numb", next); 357 errx(1, "%s", ORDER_USAGE); 358 } 359 i++; 360 } 361 free(cp); 362 if (set_bootvar("BootOrder", (uint8_t*)new_data, size) < 0) 363 err(1, "Unabke to set BootOrder to %s", order); 364 free(new_data); 365 } 366 367 static void 368 handle_activity(int bootnum, bool active) 369 { 370 uint32_t attrs, load_attrs; 371 uint8_t *data; 372 size_t size; 373 char *name; 374 375 asprintf(&name, "%s%04X", "Boot", bootnum); 376 if (name == NULL) 377 err(1, "asprintf"); 378 if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, &attrs) < 0) 379 err(1, "No such bootvar %s\n", name); 380 381 load_attrs = le32dec(data); 382 383 if (active) 384 load_attrs |= LOAD_OPTION_ACTIVE; 385 else 386 load_attrs &= ~LOAD_OPTION_ACTIVE; 387 388 le32enc(data, load_attrs); 389 390 if (set_bootvar(name, data, size) < 0) 391 err(1, "handle activity efi_set_variable"); 392 } 393 394 395 /* 396 * add boot var to boot order. 397 * called by create boot var. There is no option 398 * to add one independent of create. 399 * 400 * Note: we currently don't support where it goes 401 * so it goes on the front, inactive. 402 * use -o 2,3,7 etc to affect order, -a to activate. 403 */ 404 static void 405 add_to_boot_order(char *bootvar) 406 { 407 size_t size; 408 uint32_t attrs; 409 uint16_t val; 410 uint8_t *data, *new; 411 412 val = strtoul(&bootvar[4], NULL, 16); 413 414 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) { 415 if (errno == ENOENT) { /* create it and set this bootvar to active */ 416 size = 0; 417 data = NULL; 418 } else 419 err(1, "efi_get_variable BootOrder"); 420 } 421 422 /* 423 * We have BootOrder with the current order 424 * so grow the array by one, add the value 425 * and write the new variable value. 426 */ 427 size += sizeof(uint16_t); 428 new = malloc(size); 429 if (!new) 430 err(1, "malloc"); 431 432 le16enc(new, val); 433 if (size > sizeof(uint16_t)) 434 memcpy(new + sizeof(uint16_t), data, size - sizeof(uint16_t)); 435 436 if (set_bootvar("BootOrder", new, size) < 0) 437 err(1, "set_bootvar"); 438 free(new); 439 } 440 441 442 static void 443 remove_from_order(uint16_t bootnum) 444 { 445 uint32_t attrs; 446 size_t size, i, j; 447 uint8_t *new, *data; 448 449 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) 450 return; 451 452 new = malloc(size); 453 if (new == NULL) 454 err(1, "malloc"); 455 456 for (j = i = 0; i < size; i += sizeof(uint16_t)) { 457 if (le16dec(data + i) == bootnum) 458 continue; 459 memcpy(new + j, data + i, sizeof(uint16_t)); 460 j += sizeof(uint16_t); 461 } 462 if (i == j) 463 warnx("Boot variable %04x not in BootOrder", bootnum); 464 else if (set_bootvar("BootOrder", new, j) < 0) 465 err(1, "Unable to update BootOrder with new value"); 466 free(new); 467 } 468 469 470 static void 471 delete_bootvar(int bootnum) 472 { 473 char *name; 474 int defer = 0; 475 476 /* 477 * Try to delete the boot variable and remocve it 478 * from the boot order. We always do both actions 479 * to make it easy to clean up from oopses. 480 */ 481 if (bootnum < 0 || bootnum > 0xffff) 482 errx(1, "Bad boot variable %#x", bootnum); 483 asprintf(&name, "%s%04X", "Boot", bootnum); 484 if (name == NULL) 485 err(1, "asprintf"); 486 printf("Removing boot variable '%s'\n", name); 487 if (efi_del_variable(EFI_GLOBAL_GUID, name) < 0) { 488 defer = 1; 489 warn("cannot delete variable %s", name); 490 } 491 printf("Removing 0x%x from BootOrder\n", bootnum); 492 remove_from_order(bootnum); 493 free(name); 494 if (defer) 495 exit(defer); 496 } 497 498 499 static void 500 del_bootnext(void) 501 { 502 503 if (efi_del_variable(EFI_GLOBAL_GUID, "BootNext") < 0) 504 err(1, "efi_del_variable"); 505 } 506 507 static void 508 handle_bootnext(uint16_t bootnum) 509 { 510 uint16_t num; 511 512 le16enc(&num, bootnum); 513 if (set_bootvar("BootNext", (uint8_t*)&bootnum, sizeof(uint16_t)) < 0) 514 err(1, "set_bootvar"); 515 } 516 517 518 static int 519 compare(const void *a, const void *b) 520 { 521 uint16_t c; 522 uint16_t d; 523 524 memcpy(&c, a, sizeof(uint16_t)); 525 memcpy(&d, b, sizeof(uint16_t)); 526 527 if (c < d) 528 return -1; 529 if (c == d) 530 return 0; 531 return 1; 532 } 533 534 static char * 535 make_next_boot_var_name(void) 536 { 537 struct entry *v; 538 uint16_t *vals, next_free = 0; 539 char *name; 540 int cnt = 0; 541 int i; 542 543 LIST_FOREACH(v, &efivars, entries) { 544 cnt++; 545 } 546 547 vals = malloc(sizeof(uint16_t) * cnt); 548 if (!vals) 549 return NULL; 550 551 i = 0; 552 LIST_FOREACH(v, &efivars, entries) { 553 vals[i++] = v->idx; 554 } 555 qsort(vals, cnt, sizeof(uint16_t), compare); 556 /* if the hole is at the beginning, just return zero */ 557 if (vals[0] > 0) { 558 next_free = 0; 559 } else { 560 /* now just run the list looking for the first hole */ 561 for (i = 0; i < cnt - 1 && next_free == 0; i++) 562 if (vals[i] != vals[i + 1] + 1) 563 next_free = vals[i] + 1; 564 if (next_free == 0) 565 next_free = vals[cnt - 1] + 1; 566 /* In theory we could have used all 65k slots -- what to do? */ 567 } 568 free(vals); 569 570 asprintf(&name, "%s%04X", "Boot", next_free); 571 if (name == NULL) 572 err(1, "asprintf"); 573 return name; 574 } 575 576 577 static size_t 578 create_loadopt(uint8_t *buf, size_t bufmax, uint32_t attributes, efidp dp, size_t dp_size, 579 const char *description, const uint8_t *optional_data, size_t optional_data_size) 580 { 581 efi_char *bbuf = NULL; 582 uint8_t *pos = buf; 583 size_t desc_len = 0; 584 size_t len; 585 586 if (optional_data == NULL && optional_data_size != 0) 587 return BAD_LENGTH; 588 if (dp == NULL && dp_size != 0) 589 return BAD_LENGTH; 590 591 /* 592 * Compute the length to make sure the passed in buffer is long enough. 593 */ 594 if (description) 595 utf8_to_ucs2(description, &bbuf, &desc_len); 596 else { 597 desc_len = 0; 598 bbuf = NULL; 599 } 600 len = sizeof(uint32_t) + sizeof(uint16_t) + desc_len + dp_size + optional_data_size; 601 if (len > bufmax) { 602 free(bbuf); 603 return BAD_LENGTH; 604 } 605 606 le32enc(pos, attributes); 607 pos += sizeof (attributes); 608 609 le16enc(pos, dp_size); 610 pos += sizeof (uint16_t); 611 612 memcpy(pos, bbuf, desc_len); /* NB:desc_len includes strailing NUL */ 613 pos += desc_len; 614 free(bbuf); 615 616 memcpy(pos, dp, dp_size); 617 pos += dp_size; 618 619 if (optional_data && optional_data_size > 0) { 620 memcpy(pos, optional_data, optional_data_size); 621 pos += optional_data_size; 622 } 623 624 return pos - buf; 625 } 626 627 628 static int 629 make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run) 630 { 631 struct entry *new_ent; 632 uint32_t load_attrs = 0; 633 uint8_t *load_opt_buf; 634 size_t lopt_size, llen, klen; 635 efidp dp, loaderdp, kerneldp; 636 char *bootvar = NULL; 637 int ret; 638 639 bootvar = make_next_boot_var_name(); 640 if (bootvar == NULL) 641 err(1, "bootvar creation"); 642 if (loader == NULL) 643 errx(1, "Must specify boot loader"); 644 if (efivar_unix_path_to_device_path(loader, &loaderdp) != 0) 645 err(1, "Cannot translate unix loader path '%s' to UEFI", loader); 646 if (kernel != NULL) { 647 if (efivar_unix_path_to_device_path(kernel, &kerneldp) != 0) 648 err(1, "Cannot translate unix kernel path '%s' to UEFI", kernel); 649 } else { 650 kerneldp = NULL; 651 } 652 llen = efidp_size(loaderdp); 653 if (llen > MAX_DP_LEN) 654 errx(1, "Loader path too long."); 655 klen = efidp_size(kerneldp); 656 if (klen > MAX_DP_LEN) 657 errx(1, "Kernel path too long."); 658 dp = malloc(llen + klen); 659 if (dp == NULL) 660 errx(1, "Can't allocate memory for new device paths"); 661 memcpy(dp, loaderdp, llen); 662 if (kerneldp != NULL) 663 memcpy((char *)dp + llen, kerneldp, klen); 664 665 /* don't make the new bootvar active by default, use the -a option later */ 666 load_attrs = LOAD_OPTION_CATEGORY_BOOT; 667 load_opt_buf = malloc(MAX_LOADOPT_LEN); 668 if (load_opt_buf == NULL) 669 err(1, "malloc"); 670 671 lopt_size = create_loadopt(load_opt_buf, MAX_LOADOPT_LEN, load_attrs, 672 dp, llen + klen, label, env, env ? strlen(env) + 1 : 0); 673 if (lopt_size == BAD_LENGTH) 674 errx(1, "Can't crate loadopt"); 675 676 ret = 0; 677 if (!dry_run) { 678 ret = efi_set_variable(EFI_GLOBAL_GUID, bootvar, 679 (uint8_t*)load_opt_buf, lopt_size, COMMON_ATTRS); 680 } 681 682 if (ret) 683 err(1, "efi_set_variable"); 684 685 add_to_boot_order(bootvar); /* first, still not active */ 686 new_ent = malloc(sizeof(struct entry)); 687 if (new_ent == NULL) 688 err(1, "malloc"); 689 memset(new_ent, 0, sizeof(struct entry)); 690 new_ent->name = bootvar; 691 new_ent->guid = EFI_GLOBAL_GUID; 692 LIST_INSERT_HEAD(&efivars, new_ent, entries); 693 free(load_opt_buf); 694 free(dp); 695 696 return 0; 697 } 698 699 700 static void 701 print_loadopt_str(uint8_t *data, size_t datalen) 702 { 703 char *dev, *relpath, *abspath; 704 uint32_t attr; 705 uint16_t fplen; 706 efi_char *descr; 707 uint8_t *ep = data + datalen; 708 uint8_t *walker = data; 709 efidp dp, edp; 710 void *opt; 711 char buf[1024]; 712 int len; 713 int optlen; 714 int rv; 715 int indent; 716 717 if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char)) 718 return; 719 // First 4 bytes are attribute flags 720 attr = le32dec(walker); 721 walker += sizeof(attr); 722 // Next two bytes are length of the file paths 723 fplen = le16dec(walker); 724 walker += sizeof(fplen); 725 // Next we have a 0 terminated UCS2 string that we know to be aligned 726 descr = (efi_char *)(intptr_t)(void *)walker; 727 len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2) 728 walker += (len + 1) * sizeof(efi_char); 729 if (walker > ep) 730 return; 731 // Now we have fplen bytes worth of file path stuff 732 dp = (efidp)walker; 733 walker += fplen; 734 if (walker > ep) 735 return; 736 edp = (efidp)walker; 737 // Everything left is the binary option args 738 opt = walker; 739 optlen = ep - walker; 740 741 indent = 1; 742 while (dp < edp) { 743 efidp_format_device_path(buf, sizeof(buf), dp, 744 (intptr_t)(void *)edp - (intptr_t)(void *)dp); 745 printf("%*s%s\n", indent, "", buf); 746 indent = 10 + len + 1; 747 rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath); 748 if (rv == 0) { 749 printf("%*s%s:%s %s\n", indent + 4, "", dev, relpath, abspath); 750 free(dev); 751 free(relpath); 752 free(abspath); 753 } 754 dp = (efidp)((char *)dp + efidp_size(dp)); 755 } 756 if (optlen == 0) 757 return; 758 } 759 760 static char * 761 get_descr(uint8_t* data) 762 { 763 uint8_t *pos = data; 764 efi_char *desc; 765 int len; 766 char *buf; 767 int i = 0; 768 769 pos += sizeof(uint32_t) + sizeof(uint16_t); 770 desc = (efi_char*)(intptr_t)(void *)pos; 771 len = ucs2len(desc); 772 buf = malloc(len + 1); 773 memset(buf, 0, len + 1); 774 while (desc[i]) { 775 buf[i] = desc[i]; 776 i++; 777 } 778 return (char*)buf; 779 } 780 781 782 /* Cmd epilogue, or just the default with no args. 783 * The order is [bootnext] bootcurrent, timeout, order, and the bootvars [-v] 784 */ 785 static int 786 print_boot_vars(bool verbose) 787 { 788 /* 789 * just read and print the current values 790 * as a command epilogue 791 */ 792 struct entry *v; 793 uint8_t *data; 794 char *d; 795 size_t size; 796 uint32_t attrs, load_attrs; 797 int ret; 798 799 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootNext", &data, &size, &attrs); 800 if (ret > 0) { 801 printf("BootNext : %04x\n", le16dec(data)); 802 } 803 804 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs); 805 printf("BootCurrent: %04x\n", le16dec(data)); 806 807 ret = efi_get_variable(EFI_GLOBAL_GUID, "Timeout", &data, &size, &attrs); 808 if (ret > 0) { 809 printf("Timeout : %d seconds\n", le16dec(data)); 810 } 811 print_order(); 812 813 /* now we want to fetch 'em all fresh again 814 * which possibly includes a newly created bootvar 815 */ 816 LIST_FOREACH(v, &efivars, entries) { 817 attrs = 0; 818 ret = efi_get_variable(EFI_GLOBAL_GUID, v->name, &data, 819 &size, &attrs); 820 if (ret < 0) 821 continue; /* we must have deleted it */ 822 load_attrs = le32dec(data); 823 d = get_descr(data); 824 printf("%s%c %s", v->name, 825 ((load_attrs & LOAD_OPTION_ACTIVE) ? '*': ' '), d); 826 free(d); 827 if (verbose) 828 print_loadopt_str(data, size); 829 else 830 printf("\n"); 831 } 832 return 0; 833 } 834 835 static void 836 delete_timeout(void) 837 { 838 839 efi_del_variable(EFI_GLOBAL_GUID,"Timeout"); 840 } 841 842 static void 843 handle_timeout(int to) 844 { 845 uint16_t timeout; 846 847 le16enc(&timeout, to); 848 if (set_bootvar("Timeout", (uint8_t *)&timeout, sizeof(timeout)) < 0) 849 errx(1, "Can't set Timeout for booting."); 850 } 851 852 int 853 main(int argc, char *argv[]) 854 { 855 856 if (!efi_variables_supported()) 857 errx(1, "efi variables not supported on this system. root? kldload efirt?"); 858 859 memset(&opts, 0, sizeof (bmgr_opts_t)); 860 parse_args(argc, argv); 861 read_vars(); 862 863 if (opts.create) 864 /* 865 * side effect, adds to boot order, but not yet active. 866 */ 867 make_boot_var(opts.label, opts.loader, opts.kernel, opts.env, 868 opts.dry_run); 869 else if (opts.set_active || opts.set_inactive ) 870 handle_activity(opts.bootnum, opts.set_active); 871 else if (opts.order != NULL) 872 set_boot_order(opts.order); /* create a new bootorder with opts.order */ 873 else if (opts.set_bootnext) 874 handle_bootnext(opts.bootnum); 875 else if (opts.delete_bootnext) 876 del_bootnext(); 877 else if (opts.delete) 878 delete_bootvar(opts.bootnum); 879 else if (opts.del_timeout) 880 delete_timeout(); 881 else if (opts.set_timeout) 882 handle_timeout(opts.timeout); 883 884 print_boot_vars(opts.verbose); 885 } 886