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>
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 u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
79 errx(1, "efivar [-abdDHlLNpRtuw] [-n name] [-f file] [--append] [--ascii]\n"
80 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
81 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n"
82 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n"
87 breakdown_name(char *name, efi_guid_t *guid, char **vname)
91 cp = strrchr(name, '-');
93 errx(1, "Invalid name: %s", name);
96 if (efi_name_to_guid(name, guid) < 0)
97 errx(1, "Invalid guid %s", name);
101 get_value(char *val, size_t *datalen)
103 static char buffer[16*1024];
106 *datalen = strlen(val);
107 return ((uint8_t *)val);
109 /* Read from stdin */
110 *datalen = sizeof(buffer);
111 *datalen = read(0, buffer, *datalen);
112 return ((uint8_t *)buffer);
116 append_variable(char *name, char *val)
123 breakdown_name(name, &guid, &vname);
124 data = get_value(val, &datalen);
125 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
126 err(1, "efi_append_variable");
130 delete_variable(char *name)
135 breakdown_name(name, &guid, &vname);
136 if (efi_del_variable(guid, vname) < 0)
137 err(1, "efi_del_variable");
141 write_variable(char *name, char *val)
148 breakdown_name(name, &guid, &vname);
149 data = get_value(val, &datalen);
150 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
151 err(1, "efi_set_variable");
155 devpath_dump(uint8_t *data, size_t datalen)
159 efidp_format_device_path(buffer, sizeof(buffer),
160 (const_efidp)data, datalen);
163 printf("%s\n", buffer);
167 pretty_guid(efi_guid_t *guid, char **gname)
172 efi_guid_to_name(guid, &pretty);
175 efi_guid_to_str(guid, gname);
181 print_var(efi_guid_t *guid, char *name)
189 pretty_guid(guid, &gname);
191 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
194 err(1, "%s-%s", gname, name);
197 printf("%s-%s\n", gname, name);
199 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
201 asciidump(data, datalen);
203 utf8dump(data, datalen);
205 bindump(data, datalen);
207 devpath_dump(data, datalen);
209 hexdump(data, datalen);
211 printf("%s-%s", gname, name);
219 print_variable(char *name)
224 breakdown_name(name, &guid, &vname);
225 print_var(&guid, vname);
229 print_variables(void)
233 efi_guid_t *guid = NULL;
235 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
236 print_var(guid, name);
239 err(1, "Error listing names");
243 print_known_guid(void)
245 struct uuid_table *tbl;
248 n = efi_known_guid(&tbl);
249 for (i = 0; i < n; i++)
250 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
254 parse_args(int argc, char **argv)
258 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw",
259 longopts, NULL)) != -1) {
304 attrib = strtoul(optarg, NULL, 16);
314 errx(1, "unknown or unimplemented option\n");
326 if (aflag + Dflag + wflag > 1) {
327 warnx("Can only use one of -a (--append), "
328 "-D (--delete) and -w (--write)");
332 if (aflag + Dflag + wflag > 0 && varname == NULL) {
333 warnx("Must specify a variable for -a (--append), "
334 "-D (--delete) or -w (--write)");
339 append_variable(varname, NULL);
341 delete_variable(varname);
343 write_variable(varname, NULL);
348 print_variable(varname);
349 } else if (argc > 0) {
351 for (i = 0; i < argc; i++)
352 print_variable(argv[i]);
358 main(int argc, char **argv)
361 parse_args(argc, argv);