2 * Copyright (c) 2016 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <efivar-dp.h>
42 /* options descriptor */
43 static struct option longopts[] = {
44 { "append", no_argument, NULL, 'a' },
45 { "ascii", no_argument, NULL, 'A' },
46 { "attributes", required_argument, NULL, 't' },
47 { "binary", no_argument, NULL, 'b' },
48 { "delete", no_argument, NULL, 'D' },
49 { "device", no_argument, NULL, 'd' },
50 { "device-path", no_argument, NULL, 'd' },
51 { "fromfile", required_argument, NULL, 'f' },
52 { "guid", no_argument, NULL, 'g' },
53 { "hex", no_argument, NULL, 'H' },
54 { "list-guids", no_argument, NULL, 'L' },
55 { "list", no_argument, NULL, 'l' },
56 { "name", required_argument, NULL, 'n' },
57 { "no-name", no_argument, NULL, 'N' },
58 { "print", no_argument, NULL, 'p' },
59 { "print-decimal", no_argument, NULL, 'd' },
60 { "raw-guid", no_argument, NULL, 'R' },
61 { "write", no_argument, NULL, 'w' },
66 static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
67 lflag, Lflag, Rflag, wflag, pflag;
69 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
75 errx(1, "efivar [-abdDHlLNpRtw] [-n name] [-f file] [--append] [--ascii]\n"
76 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
77 "\t[--list-guids] [--list] [--name name] [--no-name] [--print]\n"
78 "\t[--print-decimal] [--raw-guid] [--write] name[=value]");
82 breakdown_name(char *name, efi_guid_t *guid, char **vname)
86 cp = strrchr(name, '-');
88 errx(1, "Invalid name: %s", name);
91 if (efi_name_to_guid(name, guid) < 0)
92 errx(1, "Invalid guid %s", name);
96 get_value(char *val, size_t *datalen)
98 static char buffer[16*1024];
101 *datalen = strlen(val);
102 return ((uint8_t *)val);
104 /* Read from stdin */
105 *datalen = sizeof(buffer);
106 *datalen = read(0, buffer, *datalen);
107 return ((uint8_t *)buffer);
111 append_variable(char *name, char *val)
118 breakdown_name(name, &guid, &vname);
119 data = get_value(val, &datalen);
120 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
121 err(1, "efi_append_variable");
125 delete_variable(char *name)
130 breakdown_name(name, &guid, &vname);
131 if (efi_del_variable(guid, vname) < 0)
132 err(1, "efi_del_variable");
136 write_variable(char *name, char *val)
143 breakdown_name(name, &guid, &vname);
144 data = get_value(val, &datalen);
145 if (efi_set_variable(guid, vname, data, datalen, attrib, 0) < 0)
146 err(1, "efi_set_variable");
150 asciidump(uint8_t *data, size_t datalen)
158 for (i = 0; i < datalen; i++) {
159 if (isprint(data[i])) {
165 printf("%c", data[i]);
172 printf("%%%02x", data[i]);
179 hexdump(uint8_t *data, size_t datalen)
185 for (i = 0; i < datalen; i++) {
189 printf("%04x: ", (int)i);
191 printf("%02x ", data[i]);
197 bindump(uint8_t *data, size_t datalen)
199 write(1, data, datalen);
203 devpath_dump(uint8_t *data, size_t datalen)
207 efidp_format_device_path(buffer, sizeof(buffer),
208 (const_efidp)data, datalen);
211 printf("%s\n", buffer);
215 pretty_guid(efi_guid_t *guid, char **gname)
220 efi_guid_to_name(guid, &pretty);
223 efi_guid_to_str(guid, gname);
229 print_var(efi_guid_t *guid, char *name)
237 pretty_guid(guid, &gname);
239 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
242 err(1, "%s-%s", gname, name);
245 printf("%s-%s", gname, name);
247 asciidump(data, datalen);
249 bindump(data, datalen);
251 devpath_dump(data, datalen);
253 hexdump(data, datalen);
255 printf("%s-%s", gname, name);
263 print_variable(char *name)
268 breakdown_name(name, &guid, &vname);
269 print_var(&guid, vname);
273 print_variables(void)
277 efi_guid_t *guid = NULL;
279 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
280 print_var(guid, name);
283 err(1, "Error listing names");
287 print_known_guid(void)
289 struct uuid_table *tbl;
292 n = efi_known_guid(&tbl);
293 for (i = 0; i < n; i++)
294 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
298 parse_args(int argc, char **argv)
302 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:pRt:w",
303 longopts, NULL)) != -1) {
345 attrib = strtoul(optarg, NULL, 16);
352 errx(1, "unknown or unimplemented option\n");
364 if (aflag + Dflag + wflag > 1) {
365 warnx("Can only use one of -a (--append), "
366 "-D (--delete) and -w (--write)");
370 if (aflag + Dflag + wflag > 0 && varname == NULL) {
371 warnx("Must specify a variable for -a (--append), "
372 "-D (--delete) or -w (--write)");
377 append_variable(varname, NULL);
379 delete_variable(varname);
381 write_variable(varname, NULL);
386 print_variable(varname);
387 } else if (argc > 0) {
389 for (i = 0; i < argc; i++)
390 print_variable(argv[i]);
396 main(int argc, char **argv)
399 parse_args(argc, argv);