2 * Copyright (c) 2016-2021 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
31 #include <efivar-dp.h>
46 /* options descriptor */
47 static struct option longopts[] = {
48 { "append", no_argument, NULL, 'a' },
49 { "ascii", no_argument, NULL, 'A' },
50 { "attributes", required_argument, NULL, 't' },
51 { "binary", no_argument, NULL, 'b' },
52 { "delete", no_argument, NULL, 'D' },
53 { "device", no_argument, NULL, 'd' },
54 { "device-path", no_argument, NULL, 'd' },
55 { "fromfile", required_argument, NULL, 'f' },
56 { "guid", no_argument, NULL, 'g' },
57 { "hex", no_argument, NULL, 'H' },
58 { "list-guids", no_argument, NULL, 'L' },
59 { "list", no_argument, NULL, 'l' },
60 { "load-option", no_argument, NULL, 'O' },
61 { "name", required_argument, NULL, 'n' },
62 { "no-name", no_argument, NULL, 'N' },
63 { "print", no_argument, NULL, 'p' },
64 // { "print-decimal", no_argument, NULL, 'd' }, /* unimplemnted clash with linux version */
65 { "quiet", no_argument, NULL, 'q' },
66 { "raw-guid", no_argument, NULL, 'R' },
67 { "utf8", no_argument, NULL, 'u' },
68 { "write", no_argument, NULL, 'w' },
73 static bool aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
74 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag, quiet;
76 static char *fromfile;
77 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
83 errx(1, "efivar [-abdDHlLNpqRtuw] [-n name] [-f file] [--append] [--ascii]\n"
84 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
85 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n"
86 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n"
92 rep_err(int eval, const char *fmt, ...)
105 rep_errx(int eval, const char *fmt, ...)
113 verrx(eval, fmt, ap);
118 breakdown_name(char *name, efi_guid_t *guid, char **vname)
124 cp = strrchr(name, '-');
128 rep_errx(1, "Invalid guid in: %s", name);
135 if (efi_name_to_guid(name, guid) >= 0)
141 get_value(char *val, size_t *datalen)
143 static char buffer[16*1024];
146 *datalen = strlen(val);
147 return ((uint8_t *)val);
149 /* Read from stdin */
150 *datalen = sizeof(buffer);
151 *datalen = read(0, buffer, *datalen);
152 return ((uint8_t *)buffer);
156 append_variable(char *name, char *val)
163 breakdown_name(name, &guid, &vname);
164 data = get_value(val, &datalen);
165 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
166 rep_err(1, "efi_append_variable");
170 delete_variable(char *name)
175 breakdown_name(name, &guid, &vname);
176 if (efi_del_variable(guid, vname) < 0)
177 rep_err(1, "efi_del_variable");
181 write_variable(char *name, char *val)
188 breakdown_name(name, &guid, &vname);
189 data = get_value(val, &datalen);
190 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
191 rep_err(1, "efi_set_variable");
195 devpath_dump(uint8_t *data, size_t datalen)
199 efidp_format_device_path(buffer, sizeof(buffer),
200 (const_efidp)data, datalen);
203 printf("%s\n", buffer);
207 pretty_guid(efi_guid_t *guid, char **gname)
212 efi_guid_to_name(guid, &pretty);
215 efi_guid_to_str(guid, gname);
221 print_var(efi_guid_t *guid, char *name)
230 pretty_guid(guid, &gname);
231 if (pflag || fromfile) {
235 fd = open(fromfile, O_RDONLY);
237 rep_err(1, "open %s", fromfile);
238 data = malloc(64 * 1024);
240 rep_err(1, "malloc");
241 datalen = read(fd, data, 64 * 1024);
242 if ((ssize_t)datalen < 0)
245 rep_errx(1, "empty file");
248 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
250 rep_err(1, "fetching %s-%s", gname, name);
255 printf("%s-%s\n", gname, name);
257 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
259 asciidump(data, datalen);
261 utf8dump(data, datalen);
263 bindump(data, datalen);
265 devpath_dump(data, datalen);
267 hexdump(data, datalen);
269 printf("%s-%s", gname, name);
277 print_variable(char *name)
282 breakdown_name(name, &guid, &vname);
283 print_var(&guid, vname);
287 print_variables(void)
291 efi_guid_t *guid = NULL;
293 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
294 print_var(guid, name);
297 rep_err(1, "Error listing names");
301 print_known_guid(void)
303 struct uuid_table *tbl;
306 n = efi_known_guid(&tbl);
307 for (i = 0; i < n; i++)
308 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
312 parse_args(int argc, char **argv)
316 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpqRt:uw",
317 longopts, NULL)) != -1) {
353 load_opt_flag = true;
365 attrib = strtoul(optarg, NULL, 16);
375 fromfile = strdup(optarg);
378 rep_errx(1, "unknown or unimplemented option\n");
390 if ((int)aflag + (int)Dflag + (int)wflag > 1) {
391 warnx("Can only use one of -a (--append), "
392 "-D (--delete) and -w (--write)");
396 if ((int)aflag + (int)Dflag + (int)wflag > 0 && varname == NULL) {
397 warnx("Must specify a variable for -a (--append), "
398 "-D (--delete) or -w (--write)");
403 append_variable(varname, NULL);
405 delete_variable(varname);
407 write_variable(varname, NULL);
412 print_var(NULL, NULL);
413 } else if (varname) {
415 print_variable(varname);
416 } else if (argc > 0) {
418 for (i = 0; i < argc; i++)
419 print_variable(argv[i]);
425 main(int argc, char **argv)
428 parse_args(argc, argv);