2 * Copyright (c) 2017 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
52 #include <geom/geom.h>
53 #include <geom/geom_ctl.h>
54 #include <geom/geom_int.h>
59 #include <efivar-dp.h>
61 #ifndef LOAD_OPTION_ACTIVE
62 #define LOAD_OPTION_ACTIVE 0x00000001
65 #ifndef LOAD_OPTION_CATEGORY_BOOT
66 #define LOAD_OPTION_CATEGORY_BOOT 0x00000000
69 #define BAD_LENGTH ((size_t)-1)
71 typedef struct _bmgr_opts {
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'},
119 /* global efibootmgr opts */
120 static bmgr_opts_t opts;
122 static LIST_HEAD(efivars_head, entry) efivars =
123 LIST_HEAD_INITIALIZER(efivars);
135 LIST_ENTRY(entry) entries;
138 #define MAX_DP_LEN 4096
139 #define MAX_LOADOPT_LEN 8192
143 mangle_loader(char *loader)
147 for (c = loader; *c; c++)
155 #define COMMON_ATTRS EFI_VARIABLE_NON_VOLATILE | \
156 EFI_VARIABLE_BOOTSERVICE_ACCESS | \
157 EFI_VARIABLE_RUNTIME_ACCESS
160 * We use global guid, and common var attrs and
161 * find it better to just delete and re-create a var.
164 set_bootvar(const char *name, uint8_t *data, size_t size)
167 return efi_set_variable(EFI_GLOBAL_GUID, name, data, size,
173 " [-aAnNB Bootvar] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help] \n\
174 [-c -l loader [-k kernel ] [-L label] [--dry-run]]"
176 #define CREATE_USAGE \
177 " efibootmgr -c -l loader [-k kernel] [-L label] [--dry-run]"
178 #define ORDER_USAGE \
179 " efibootmgr -o bootvarnum1,bootvarnum2,..."
180 #define TIMEOUT_USAGE \
181 " efibootmgr -t seconds"
182 #define DELETE_USAGE \
183 " efibootmgr -B bootvarnum"
184 #define ACTIVE_USAGE \
185 " efibootmgr [-a | -A] bootvarnum"
186 #define BOOTNEXT_USAGE \
187 " efibootmgr [-n | -N] bootvarnum"
190 parse_args(int argc, char *argv[])
194 while ((ch = getopt_long(argc, argv, "A:a:B:C:cDe:hk:L:l:Nn:Oo:Tt:v",
195 lopts, NULL)) != -1) {
198 opts.set_inactive = true;
199 opts.bootnum = strtoul(optarg, NULL, 16);
202 opts.set_active = true;
203 opts.bootnum = strtoul(optarg, NULL, 16);
207 opts.bootnum = strtoul(optarg, NULL, 16);
211 opts.cp_src = strtoul(optarg, NULL, 16);
215 case 'D': /* should be remove dups XXX */
220 opts.env = strdup(optarg);
224 errx(1, "%s", USAGE);
228 opts.kernel = strdup(optarg);
232 opts.label = strdup(optarg);
236 opts.loader = strdup(optarg);
237 opts.loader = mangle_loader(opts.loader);
240 opts.delete_bootnext = true;
243 opts.set_bootnext = true;
244 opts.bootnum = strtoul(optarg, NULL, 16);
251 opts.order = strdup(optarg);
254 opts.del_timeout = true;
257 opts.set_timeout = true;
258 opts.timeout = strtoul(optarg, NULL, 10);
267 errx(1, "%s",CREATE_USAGE);
270 if (opts.set_bootnext && !(opts.bootnum))
271 errx(1, "%s", BOOTNEXT_USAGE);
273 if ((opts.set_active || opts.set_inactive) && !(opts.bootnum))
274 errx(1, "%s", ACTIVE_USAGE);
276 if (opts.order && !(opts.order))
277 errx(1, "%s", ORDER_USAGE);
288 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) {
289 printf("BootOrder : Couldn't get value for BootOrder\n");
294 errx(1, "Bad BootOrder variable: odd length");
296 printf("BootOrder : ");
297 for (i = 0; i < size; i += 2)
298 printf("%04x%s", le16dec(data + i), i == size - 2 ? "\n" : ", ");
307 char *next_name = NULL;
313 while ((ret = efi_get_next_variable_name(&guid, &next_name)) > 0) {
315 * Only pay attention to EFI:BootXXXX variables to get the list.
317 if (efi_guid_cmp(guid, &EFI_GLOBAL_GUID) != 0 ||
318 strlen(next_name) != 8 ||
319 strncmp(next_name, "Boot", 4) != 0 ||
320 !isxdigit(next_name[4]) ||
321 !isxdigit(next_name[5]) ||
322 !isxdigit(next_name[6]) ||
323 !isxdigit(next_name[7]))
325 nent = malloc(sizeof(struct entry));
326 nent->name = strdup(next_name);
328 ret = efi_get_variable(*guid, next_name, &nent->data,
329 &nent->size, &nent->attrs);
331 err(1, "efi_get_variable");
333 nent->idx = strtoul(&next_name[4], NULL, 16);
334 LIST_INSERT_HEAD(&efivars, nent, entries);
340 set_boot_order(char *order)
354 size = sizeof(uint16_t) * cnt;
355 new_data = malloc(size);
359 while ((next = strsep(&cp, ",")) != NULL) {
360 new_data[i] = strtoul(next, NULL, 16);
361 if (new_data[i] == 0 && errno == EINVAL) {
362 warnx("can't parse %s as a numb", next);
363 errx(1, "%s", ORDER_USAGE);
368 if (set_bootvar("BootOrder", (uint8_t*)new_data, size) < 0)
369 err(1, "Unabke to set BootOrder to %s", order);
374 handle_activity(int bootnum, bool active)
376 uint32_t attrs, load_attrs;
381 asprintf(&name, "%s%04X", "Boot", bootnum);
384 if (efi_get_variable(EFI_GLOBAL_GUID, name, &data, &size, &attrs) < 0)
385 err(1, "No such bootvar %s\n", name);
387 load_attrs = le32dec(data);
390 load_attrs |= LOAD_OPTION_ACTIVE;
392 load_attrs &= ~LOAD_OPTION_ACTIVE;
394 le32enc(data, load_attrs);
396 if (set_bootvar(name, data, size) < 0)
397 err(1, "handle activity efi_set_variable");
402 * add boot var to boot order.
403 * called by create boot var. There is no option
404 * to add one independent of create.
406 * Note: we currently don't support where it goes
407 * so it goes on the front, inactive.
408 * use -o 2,3,7 etc to affect order, -a to activate.
411 add_to_boot_order(char *bootvar)
418 val = strtoul(&bootvar[4], NULL, 16);
420 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0) {
421 if (errno == ENOENT) { /* create it and set this bootvar to active */
425 err(1, "efi_get_variable BootOrder");
429 * We have BootOrder with the current order
430 * so grow the array by one, add the value
431 * and write the new variable value.
433 size += sizeof(uint16_t);
439 if (size > sizeof(uint16_t))
440 memcpy(new + sizeof(uint16_t), data, size - sizeof(uint16_t));
442 if (set_bootvar("BootOrder", new, size) < 0)
443 err(1, "set_bootvar");
449 remove_from_order(uint16_t bootnum)
455 if (efi_get_variable(EFI_GLOBAL_GUID, "BootOrder", &data, &size, &attrs) < 0)
462 for (j = i = 0; i < size; i += sizeof(uint16_t)) {
463 if (le16dec(data + i) == bootnum)
465 memcpy(new + j, data + i, sizeof(uint16_t));
466 j += sizeof(uint16_t);
469 warnx("Boot variable %04x not in BootOrder", bootnum);
470 else if (set_bootvar("BootOrder", new, j) < 0)
471 err(1, "Unable to update BootOrder with new value");
477 delete_bootvar(int bootnum)
483 * Try to delete the boot variable and remocve it
484 * from the boot order. We always do both actions
485 * to make it easy to clean up from oopses.
487 if (bootnum < 0 || bootnum > 0xffff)
488 errx(1, "Bad boot variable %#x", bootnum);
489 asprintf(&name, "%s%04X", "Boot", bootnum);
492 printf("Removing boot variable '%s'\n", name);
493 if (efi_del_variable(EFI_GLOBAL_GUID, name) < 0) {
495 warn("cannot delete variable %s", name);
497 printf("Removing 0x%x from BootOrder\n", bootnum);
498 remove_from_order(bootnum);
509 if (efi_del_variable(EFI_GLOBAL_GUID, "BootNext") < 0)
510 err(1, "efi_del_variable");
514 handle_bootnext(uint16_t bootnum)
518 le16enc(&num, bootnum);
519 if (set_bootvar("BootNext", (uint8_t*)&bootnum, sizeof(uint16_t)) < 0)
520 err(1, "set_bootvar");
525 compare(const void *a, const void *b)
530 memcpy(&c, a, sizeof(uint16_t));
531 memcpy(&d, b, sizeof(uint16_t));
541 make_next_boot_var_name(void)
544 uint16_t *vals, next_free = 0;
549 LIST_FOREACH(v, &efivars, entries) {
553 vals = malloc(sizeof(uint16_t) * cnt);
558 LIST_FOREACH(v, &efivars, entries) {
561 qsort(vals, cnt, sizeof(uint16_t), compare);
562 /* if the hole is at the beginning, just return zero */
566 /* now just run the list looking for the first hole */
567 for (i = 0; i < cnt - 1 && next_free == 0; i++)
568 if (vals[i] + 1 != vals[i + 1])
569 next_free = vals[i] + 1;
571 next_free = vals[cnt - 1] + 1;
572 /* In theory we could have used all 65k slots -- what to do? */
576 asprintf(&name, "%s%04X", "Boot", next_free);
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)
587 efi_char *bbuf = NULL;
592 if (optional_data == NULL && optional_data_size != 0)
594 if (dp == NULL && dp_size != 0)
598 * Compute the length to make sure the passed in buffer is long enough.
600 utf8_to_ucs2(description, &bbuf, &desc_len);
601 len = sizeof(uint32_t) + sizeof(uint16_t) + desc_len + dp_size + optional_data_size;
607 le32enc(pos, attributes);
608 pos += sizeof (attributes);
610 le16enc(pos, dp_size);
611 pos += sizeof (uint16_t);
613 memcpy(pos, bbuf, desc_len); /* NB:desc_len includes strailing NUL */
617 memcpy(pos, dp, dp_size);
620 if (optional_data && optional_data_size > 0) {
621 memcpy(pos, optional_data, optional_data_size);
622 pos += optional_data_size;
630 make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run)
632 struct entry *new_ent;
633 uint32_t load_attrs = 0;
634 uint8_t *load_opt_buf;
635 size_t lopt_size, llen, klen;
636 efidp dp, loaderdp, kerneldp;
637 char *bootvar = NULL;
640 assert(label != NULL);
642 bootvar = make_next_boot_var_name();
644 err(1, "bootvar creation");
646 errx(1, "Must specify boot loader");
647 if (efivar_unix_path_to_device_path(loader, &loaderdp) != 0)
648 err(1, "Cannot translate unix loader path '%s' to UEFI", loader);
649 if (kernel != NULL) {
650 if (efivar_unix_path_to_device_path(kernel, &kerneldp) != 0)
651 err(1, "Cannot translate unix kernel path '%s' to UEFI", kernel);
655 llen = efidp_size(loaderdp);
656 if (llen > MAX_DP_LEN)
657 errx(1, "Loader path too long.");
658 klen = efidp_size(kerneldp);
659 if (klen > MAX_DP_LEN)
660 errx(1, "Kernel path too long.");
661 dp = malloc(llen + klen);
663 errx(1, "Can't allocate memory for new device paths");
664 memcpy(dp, loaderdp, llen);
665 if (kerneldp != NULL)
666 memcpy((char *)dp + llen, kerneldp, klen);
668 /* don't make the new bootvar active by default, use the -a option later */
669 load_attrs = LOAD_OPTION_CATEGORY_BOOT;
670 load_opt_buf = malloc(MAX_LOADOPT_LEN);
671 if (load_opt_buf == NULL)
674 lopt_size = create_loadopt(load_opt_buf, MAX_LOADOPT_LEN, load_attrs,
675 dp, llen + klen, label, env, env ? strlen(env) + 1 : 0);
676 if (lopt_size == BAD_LENGTH)
677 errx(1, "Can't crate loadopt");
681 ret = efi_set_variable(EFI_GLOBAL_GUID, bootvar,
682 (uint8_t*)load_opt_buf, lopt_size, COMMON_ATTRS);
686 err(1, "efi_set_variable");
688 add_to_boot_order(bootvar); /* first, still not active */
689 new_ent = malloc(sizeof(struct entry));
692 memset(new_ent, 0, sizeof(struct entry));
693 new_ent->name = bootvar;
694 new_ent->guid = EFI_GLOBAL_GUID;
695 LIST_INSERT_HEAD(&efivars, new_ent, entries);
704 print_loadopt_str(uint8_t *data, size_t datalen)
706 char *dev, *relpath, *abspath;
710 uint8_t *ep = data + datalen;
711 uint8_t *walker = data;
718 if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
720 // First 4 bytes are attribute flags
721 attr = le32dec(walker);
722 walker += sizeof(attr);
723 // Next two bytes are length of the file paths
724 fplen = le16dec(walker);
725 walker += sizeof(fplen);
726 // Next we have a 0 terminated UCS2 string that we know to be aligned
727 descr = (efi_char *)(intptr_t)(void *)walker;
728 len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
729 walker += (len + 1) * sizeof(efi_char);
732 // Now we have fplen bytes worth of file path stuff
739 * Everything left is the binary option args
741 * optlen = ep - walker;
745 efidp_format_device_path(buf, sizeof(buf), dp,
746 (intptr_t)(void *)edp - (intptr_t)(void *)dp);
747 printf("%*s%s\n", indent, "", buf);
748 indent = 10 + len + 1;
749 rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
751 printf("%*s%s:%s %s\n", indent + 4, "", dev, relpath, abspath);
756 dp = (efidp)((char *)dp + efidp_size(dp));
761 get_descr(uint8_t *data)
769 pos += sizeof(uint32_t) + sizeof(uint16_t);
770 desc = (efi_char*)(intptr_t)(void *)pos;
772 buf = malloc(len + 1);
773 memset(buf, 0, len + 1);
782 /* Cmd epilogue, or just the default with no args.
783 * The order is [bootnext] bootcurrent, timeout, order, and the bootvars [-v]
786 print_boot_vars(bool verbose)
789 * just read and print the current values
790 * as a command epilogue
796 uint32_t attrs, load_attrs;
799 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootNext", &data, &size, &attrs);
801 printf("BootNext : %04x\n", le16dec(data));
804 ret = efi_get_variable(EFI_GLOBAL_GUID, "BootCurrent", &data, &size,&attrs);
805 printf("BootCurrent: %04x\n", le16dec(data));
807 ret = efi_get_variable(EFI_GLOBAL_GUID, "Timeout", &data, &size, &attrs);
809 printf("Timeout : %d seconds\n", le16dec(data));
813 /* now we want to fetch 'em all fresh again
814 * which possibly includes a newly created bootvar
816 LIST_FOREACH(v, &efivars, entries) {
818 ret = efi_get_variable(EFI_GLOBAL_GUID, v->name, &data,
821 continue; /* we must have deleted it */
822 load_attrs = le32dec(data);
824 printf("%s%c %s", v->name,
825 ((load_attrs & LOAD_OPTION_ACTIVE) ? '*': ' '), d);
828 print_loadopt_str(data, size);
839 efi_del_variable(EFI_GLOBAL_GUID,"Timeout");
843 handle_timeout(int to)
847 le16enc(&timeout, to);
848 if (set_bootvar("Timeout", (uint8_t *)&timeout, sizeof(timeout)) < 0)
849 errx(1, "Can't set Timeout for booting.");
853 main(int argc, char *argv[])
856 if (!efi_variables_supported())
857 errx(1, "efi variables not supported on this system. root? kldload efirt?");
859 memset(&opts, 0, sizeof (bmgr_opts_t));
860 parse_args(argc, argv);
865 * side effect, adds to boot order, but not yet active.
867 make_boot_var(opts.label ? opts.label : "",
868 opts.loader, opts.kernel, opts.env, 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)
877 else if (opts.delete)
878 delete_bootvar(opts.bootnum);
879 else if (opts.del_timeout)
881 else if (opts.set_timeout)
882 handle_timeout(opts.timeout);
884 print_boot_vars(opts.verbose);