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>
29 #include <efivar-dp.h>
44 /* options descriptor */
45 static struct option longopts[] = {
46 { "append", no_argument, NULL, 'a' },
47 { "ascii", no_argument, NULL, 'A' },
48 { "attributes", required_argument, NULL, 't' },
49 { "binary", no_argument, NULL, 'b' },
50 { "delete", no_argument, NULL, 'D' },
51 { "device", no_argument, NULL, 'd' },
52 { "device-path", no_argument, NULL, 'd' },
53 { "fromfile", required_argument, NULL, 'f' },
54 { "guid", no_argument, NULL, 'g' },
55 { "hex", no_argument, NULL, 'H' },
56 { "list-guids", no_argument, NULL, 'L' },
57 { "list", no_argument, NULL, 'l' },
58 { "load-option", no_argument, NULL, 'O' },
59 { "name", required_argument, NULL, 'n' },
60 { "no-name", no_argument, NULL, 'N' },
61 { "print", no_argument, NULL, 'p' },
62 // { "print-decimal", no_argument, NULL, 'd' }, /* unimplemnted clash with linux version */
63 { "quiet", no_argument, NULL, 'q' },
64 { "raw-guid", no_argument, NULL, 'R' },
65 { "utf8", no_argument, NULL, 'u' },
66 { "write", no_argument, NULL, 'w' },
71 static bool aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
72 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag, quiet;
74 static char *fromfile;
75 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
81 errx(1, "efivar [-abdDHlLNpqRtuw] [-n name] [-f file] [--append] [--ascii]\n"
82 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
83 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n"
84 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n"
90 rep_err(int eval, const char *fmt, ...)
103 rep_errx(int eval, const char *fmt, ...)
111 verrx(eval, fmt, ap);
116 breakdown_name(char *name, efi_guid_t *guid, char **vname)
122 cp = strrchr(name, '-');
126 rep_errx(1, "Invalid guid in: %s", name);
133 if (efi_name_to_guid(name, guid) >= 0)
139 get_value(char *val, size_t *datalen)
141 static char buffer[16*1024];
144 *datalen = strlen(val);
145 return ((uint8_t *)val);
147 /* Read from stdin */
148 *datalen = sizeof(buffer);
149 *datalen = read(0, buffer, *datalen);
150 return ((uint8_t *)buffer);
154 append_variable(char *name, char *val)
161 breakdown_name(name, &guid, &vname);
162 data = get_value(val, &datalen);
163 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
164 rep_err(1, "efi_append_variable");
168 delete_variable(char *name)
173 breakdown_name(name, &guid, &vname);
174 if (efi_del_variable(guid, vname) < 0)
175 rep_err(1, "efi_del_variable");
179 write_variable(char *name, char *val)
186 breakdown_name(name, &guid, &vname);
187 data = get_value(val, &datalen);
188 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
189 rep_err(1, "efi_set_variable");
193 devpath_dump(uint8_t *data, size_t datalen)
197 efidp_format_device_path(buffer, sizeof(buffer),
198 (const_efidp)data, datalen);
201 printf("%s\n", buffer);
205 pretty_guid(efi_guid_t *guid, char **gname)
210 efi_guid_to_name(guid, &pretty);
213 efi_guid_to_str(guid, gname);
219 print_var(efi_guid_t *guid, char *name)
228 pretty_guid(guid, &gname);
229 if (pflag || fromfile) {
233 fd = open(fromfile, O_RDONLY);
235 rep_err(1, "open %s", fromfile);
236 data = malloc(64 * 1024);
238 rep_err(1, "malloc");
239 datalen = read(fd, data, 64 * 1024);
240 if ((ssize_t)datalen < 0)
243 rep_errx(1, "empty file");
246 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
248 rep_err(1, "fetching %s-%s", gname, name);
253 printf("%s-%s\n", gname, name);
255 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
257 asciidump(data, datalen);
259 utf8dump(data, datalen);
261 bindump(data, datalen);
263 devpath_dump(data, datalen);
265 hexdump(data, datalen);
267 printf("%s-%s", gname, name);
275 print_variable(char *name)
280 breakdown_name(name, &guid, &vname);
281 print_var(&guid, vname);
285 print_variables(void)
289 efi_guid_t *guid = NULL;
291 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
292 print_var(guid, name);
295 rep_err(1, "Error listing names");
299 print_known_guid(void)
301 struct uuid_table *tbl;
304 n = efi_known_guid(&tbl);
305 for (i = 0; i < n; i++)
306 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
310 parse_args(int argc, char **argv)
314 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpqRt:uw",
315 longopts, NULL)) != -1) {
351 load_opt_flag = true;
363 attrib = strtoul(optarg, NULL, 16);
373 fromfile = strdup(optarg);
376 rep_errx(1, "unknown or unimplemented option\n");
388 if ((int)aflag + (int)Dflag + (int)wflag > 1) {
389 warnx("Can only use one of -a (--append), "
390 "-D (--delete) and -w (--write)");
394 if ((int)aflag + (int)Dflag + (int)wflag > 0 && varname == NULL) {
395 warnx("Must specify a variable for -a (--append), "
396 "-D (--delete) or -w (--write)");
401 append_variable(varname, NULL);
403 delete_variable(varname);
405 write_variable(varname, NULL);
410 print_var(NULL, NULL);
411 } else if (varname) {
413 print_variable(varname);
414 } else if (argc > 0) {
416 for (i = 0; i < argc; i++)
417 print_variable(argv[i]);
423 main(int argc, char **argv)
426 parse_args(argc, argv);