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>
43 /* options descriptor */
44 static struct option longopts[] = {
45 { "append", no_argument, NULL, 'a' },
46 { "ascii", no_argument, NULL, 'A' },
47 { "attributes", required_argument, NULL, 't' },
48 { "binary", no_argument, NULL, 'b' },
49 { "delete", no_argument, NULL, 'D' },
50 { "device", no_argument, NULL, 'd' },
51 { "device-path", no_argument, NULL, 'd' },
52 { "fromfile", required_argument, NULL, 'f' },
53 { "guid", no_argument, NULL, 'g' },
54 { "hex", no_argument, NULL, 'H' },
55 { "list-guids", no_argument, NULL, 'L' },
56 { "list", no_argument, NULL, 'l' },
57 { "load-option", no_argument, NULL, 'O' },
58 { "name", required_argument, NULL, 'n' },
59 { "no-name", no_argument, NULL, 'N' },
60 { "print", no_argument, NULL, 'p' },
61 { "print-decimal", no_argument, NULL, 'd' },
62 { "raw-guid", no_argument, NULL, 'R' },
63 { "utf8", no_argument, NULL, 'u' },
64 { "write", no_argument, NULL, 'w' },
69 static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
70 lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag;
72 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
78 errx(1, "efivar [-abdDHlLNpRtuw] [-n name] [-f file] [--append] [--ascii]\n"
79 "\t[--attributes] [--binary] [--delete] [--fromfile file] [--hex]\n"
80 "\t[--list-guids] [--list] [--load-option] [--name name] [--no-name]\n"
81 "\t[--print] [--print-decimal] [--raw-guid] [--utf8] [--write]\n"
86 breakdown_name(char *name, efi_guid_t *guid, char **vname)
90 cp = strrchr(name, '-');
92 errx(1, "Invalid name: %s", name);
95 if (efi_name_to_guid(name, guid) < 0)
96 errx(1, "Invalid guid %s", name);
100 get_value(char *val, size_t *datalen)
102 static char buffer[16*1024];
105 *datalen = strlen(val);
106 return ((uint8_t *)val);
108 /* Read from stdin */
109 *datalen = sizeof(buffer);
110 *datalen = read(0, buffer, *datalen);
111 return ((uint8_t *)buffer);
115 append_variable(char *name, char *val)
122 breakdown_name(name, &guid, &vname);
123 data = get_value(val, &datalen);
124 if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
125 err(1, "efi_append_variable");
129 delete_variable(char *name)
134 breakdown_name(name, &guid, &vname);
135 if (efi_del_variable(guid, vname) < 0)
136 err(1, "efi_del_variable");
140 write_variable(char *name, char *val)
147 breakdown_name(name, &guid, &vname);
148 data = get_value(val, &datalen);
149 if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
150 err(1, "efi_set_variable");
154 devpath_dump(uint8_t *data, size_t datalen)
158 efidp_format_device_path(buffer, sizeof(buffer),
159 (const_efidp)data, datalen);
162 printf("%s\n", buffer);
166 pretty_guid(efi_guid_t *guid, char **gname)
171 efi_guid_to_name(guid, &pretty);
174 efi_guid_to_str(guid, gname);
180 print_var(efi_guid_t *guid, char *name)
188 pretty_guid(guid, &gname);
190 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
193 err(1, "%s-%s", gname, name);
196 printf("%s-%s\n", gname, name);
198 efi_print_load_option(data, datalen, Aflag, bflag, uflag);
200 asciidump(data, datalen);
202 utf8dump(data, datalen);
204 bindump(data, datalen);
206 devpath_dump(data, datalen);
208 hexdump(data, datalen);
210 printf("%s-%s", gname, name);
218 print_variable(char *name)
223 breakdown_name(name, &guid, &vname);
224 print_var(&guid, vname);
228 print_variables(void)
232 efi_guid_t *guid = NULL;
234 while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
235 print_var(guid, name);
238 err(1, "Error listing names");
242 print_known_guid(void)
244 struct uuid_table *tbl;
247 n = efi_known_guid(&tbl);
248 for (i = 0; i < n; i++)
249 printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
253 parse_args(int argc, char **argv)
257 while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw",
258 longopts, NULL)) != -1) {
303 attrib = strtoul(optarg, NULL, 16);
313 errx(1, "unknown or unimplemented option\n");
325 if (aflag + Dflag + wflag > 1) {
326 warnx("Can only use one of -a (--append), "
327 "-D (--delete) and -w (--write)");
331 if (aflag + Dflag + wflag > 0 && varname == NULL) {
332 warnx("Must specify a variable for -a (--append), "
333 "-D (--delete) or -w (--write)");
338 append_variable(varname, NULL);
340 delete_variable(varname);
342 write_variable(varname, NULL);
347 print_variable(varname);
348 } else if (argc > 0) {
350 for (i = 0; i < argc; i++)
351 print_variable(argv[i]);
357 main(int argc, char **argv)
360 parse_args(argc, argv);