2 * Copyright (c) 2016 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>
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' },
63 { "raw-guid", no_argument, NULL, 'R' },
64 { "utf8", no_argument, NULL, 'u' },
65 { "write", no_argument, NULL, 'w' },
70 static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
71 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag;
73 static char *fromfile;
74 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
80 errx(1, "efivar [-abdDHlLNpRtuw] [-n name] [-f file] [--append] [--ascii]\n"
81 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
82 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n"
83 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n"
88 breakdown_name(char *name, efi_guid_t *guid, char **vname)
92 cp = strrchr(name, '-');
94 errx(1, "Invalid name: %s", name);
97 if (efi_name_to_guid(name, guid) < 0)
98 errx(1, "Invalid guid %s", name);
102 get_value(char *val, size_t *datalen)
104 static char buffer[16*1024];
107 *datalen = strlen(val);
108 return ((uint8_t *)val);
110 /* Read from stdin */
111 *datalen = sizeof(buffer);
112 *datalen = read(0, buffer, *datalen);
113 return ((uint8_t *)buffer);
117 append_variable(char *name, char *val)
124 breakdown_name(name, &guid, &vname);
125 data = get_value(val, &datalen);
126 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
127 err(1, "efi_append_variable");
131 delete_variable(char *name)
136 breakdown_name(name, &guid, &vname);
137 if (efi_del_variable(guid, vname) < 0)
138 err(1, "efi_del_variable");
142 write_variable(char *name, char *val)
149 breakdown_name(name, &guid, &vname);
150 data = get_value(val, &datalen);
151 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
152 err(1, "efi_set_variable");
156 devpath_dump(uint8_t *data, size_t datalen)
160 efidp_format_device_path(buffer, sizeof(buffer),
161 (const_efidp)data, datalen);
164 printf("%s\n", buffer);
168 pretty_guid(efi_guid_t *guid, char **gname)
173 efi_guid_to_name(guid, &pretty);
176 efi_guid_to_str(guid, gname);
182 print_var(efi_guid_t *guid, char *name)
191 pretty_guid(guid, &gname);
192 if (pflag || fromfile) {
196 fd = open(fromfile, O_RDONLY);
198 err(1, "open %s", fromfile);
199 data = malloc(64 * 1024);
202 datalen = read(fd, data, 64 * 1024);
207 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
209 err(1, "fetching %s-%s", gname, name);
214 printf("%s-%s\n", gname, name);
216 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
218 asciidump(data, datalen);
220 utf8dump(data, datalen);
222 bindump(data, datalen);
224 devpath_dump(data, datalen);
226 hexdump(data, datalen);
228 printf("%s-%s", gname, name);
236 print_variable(char *name)
241 breakdown_name(name, &guid, &vname);
242 print_var(&guid, vname);
246 print_variables(void)
250 efi_guid_t *guid = NULL;
252 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
253 print_var(guid, name);
256 err(1, "Error listing names");
260 print_known_guid(void)
262 struct uuid_table *tbl;
265 n = efi_known_guid(&tbl);
266 for (i = 0; i < n; i++)
267 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
271 parse_args(int argc, char **argv)
275 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw",
276 longopts, NULL)) != -1) {
321 attrib = strtoul(optarg, NULL, 16);
331 fromfile = strdup(optarg);
334 errx(1, "unknown or unimplemented option\n");
346 if (aflag + Dflag + wflag > 1) {
347 warnx("Can only use one of -a (--append), "
348 "-D (--delete) and -w (--write)");
352 if (aflag + Dflag + wflag > 0 && varname == NULL) {
353 warnx("Must specify a variable for -a (--append), "
354 "-D (--delete) or -w (--write)");
359 append_variable(varname, NULL);
361 delete_variable(varname);
363 write_variable(varname, NULL);
368 print_var(NULL, NULL);
369 } else if (varname) {
371 print_variable(varname);
372 } else if (argc > 0) {
374 for (i = 0; i < argc; i++)
375 print_variable(argv[i]);
381 main(int argc, char **argv)
384 parse_args(argc, argv);