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>
45 /* options descriptor */
46 static struct option longopts[] = {
47 { "append", no_argument, NULL, 'a' },
48 { "ascii", no_argument, NULL, 'A' },
49 { "attributes", required_argument, NULL, 't' },
50 { "binary", no_argument, NULL, 'b' },
51 { "delete", no_argument, NULL, 'D' },
52 { "device", no_argument, NULL, 'd' },
53 { "device-path", no_argument, NULL, 'd' },
54 { "fromfile", required_argument, NULL, 'f' },
55 { "guid", no_argument, NULL, 'g' },
56 { "hex", no_argument, NULL, 'H' },
57 { "list-guids", no_argument, NULL, 'L' },
58 { "list", no_argument, NULL, 'l' },
59 { "load-option", no_argument, NULL, 'O' },
60 { "name", required_argument, NULL, 'n' },
61 { "no-name", no_argument, NULL, 'N' },
62 { "print", no_argument, NULL, 'p' },
63 { "print-decimal", no_argument, NULL, 'd' },
64 { "raw-guid", no_argument, NULL, 'R' },
65 { "utf8", no_argument, NULL, 'u' },
66 { "write", no_argument, NULL, 'w' },
71 static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
72 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag;
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 [-abdDHlLNpRtuw] [-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"
89 breakdown_name(char *name, efi_guid_t *guid, char **vname)
93 cp = strrchr(name, '-');
95 errx(1, "Invalid name: %s", name);
98 if (efi_name_to_guid(name, guid) < 0)
99 errx(1, "Invalid guid %s", name);
103 get_value(char *val, size_t *datalen)
105 static char buffer[16*1024];
108 *datalen = strlen(val);
109 return ((uint8_t *)val);
111 /* Read from stdin */
112 *datalen = sizeof(buffer);
113 *datalen = read(0, buffer, *datalen);
114 return ((uint8_t *)buffer);
118 append_variable(char *name, char *val)
125 breakdown_name(name, &guid, &vname);
126 data = get_value(val, &datalen);
127 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
128 err(1, "efi_append_variable");
132 delete_variable(char *name)
137 breakdown_name(name, &guid, &vname);
138 if (efi_del_variable(guid, vname) < 0)
139 err(1, "efi_del_variable");
143 write_variable(char *name, char *val)
150 breakdown_name(name, &guid, &vname);
151 data = get_value(val, &datalen);
152 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
153 err(1, "efi_set_variable");
157 devpath_dump(uint8_t *data, size_t datalen)
161 efidp_format_device_path(buffer, sizeof(buffer),
162 (const_efidp)data, datalen);
165 printf("%s\n", buffer);
169 pretty_guid(efi_guid_t *guid, char **gname)
174 efi_guid_to_name(guid, &pretty);
177 efi_guid_to_str(guid, gname);
183 print_var(efi_guid_t *guid, char *name)
192 pretty_guid(guid, &gname);
193 if (pflag || fromfile) {
197 fd = open(fromfile, O_RDONLY);
199 err(1, "open %s", fromfile);
200 data = malloc(64 * 1024);
203 datalen = read(fd, data, 64 * 1024);
208 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
210 err(1, "fetching %s-%s", gname, name);
215 printf("%s-%s\n", gname, name);
217 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
219 asciidump(data, datalen);
221 utf8dump(data, datalen);
223 bindump(data, datalen);
225 devpath_dump(data, datalen);
227 hexdump(data, datalen);
229 printf("%s-%s", gname, name);
237 print_variable(char *name)
242 breakdown_name(name, &guid, &vname);
243 print_var(&guid, vname);
247 print_variables(void)
251 efi_guid_t *guid = NULL;
253 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
254 print_var(guid, name);
257 err(1, "Error listing names");
261 print_known_guid(void)
263 struct uuid_table *tbl;
266 n = efi_known_guid(&tbl);
267 for (i = 0; i < n; i++)
268 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
272 parse_args(int argc, char **argv)
276 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw",
277 longopts, NULL)) != -1) {
322 attrib = strtoul(optarg, NULL, 16);
332 fromfile = strdup(optarg);
335 errx(1, "unknown or unimplemented option\n");
347 if (aflag + Dflag + wflag > 1) {
348 warnx("Can only use one of -a (--append), "
349 "-D (--delete) and -w (--write)");
353 if (aflag + Dflag + wflag > 0 && varname == NULL) {
354 warnx("Must specify a variable for -a (--append), "
355 "-D (--delete) or -w (--write)");
360 append_variable(varname, NULL);
362 delete_variable(varname);
364 write_variable(varname, NULL);
369 print_var(NULL, NULL);
370 } else if (varname) {
372 print_variable(varname);
373 } else if (argc > 0) {
375 for (i = 0; i < argc; i++)
376 print_variable(argv[i]);
382 main(int argc, char **argv)
385 parse_args(argc, argv);