]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/efivar/efivar.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / usr.sbin / efivar / efivar.c
1 /*-
2  * Copyright (c) 2016 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <ctype.h>
30 #include <efivar.h>
31 #include <efivar-dp.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include "efiutil.h"
41 #include "efichar.h"
42
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' },
65         { NULL,                 0,                      NULL,   0 }
66 };
67
68
69 static int aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
70         lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag;
71 static char *varname;
72 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
73
74 static void
75 usage(void)
76 {
77
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"
82             "\tname[=value]");
83 }
84
85 static void
86 breakdown_name(char *name, efi_guid_t *guid, char **vname)
87 {
88         char *cp;
89
90         cp = strrchr(name, '-');
91         if (cp == NULL)
92                 errx(1, "Invalid name: %s", name);
93         *vname = cp + 1;
94         *cp = '\0';
95         if (efi_name_to_guid(name, guid) < 0)
96                 errx(1, "Invalid guid %s", name);
97 }
98
99 static uint8_t *
100 get_value(char *val, size_t *datalen)
101 {
102         static char buffer[16*1024];
103
104         if (val != NULL) {
105                 *datalen = strlen(val);
106                 return ((uint8_t *)val);
107         }
108         /* Read from stdin */
109         *datalen = sizeof(buffer);
110         *datalen = read(0, buffer, *datalen);
111         return ((uint8_t *)buffer);
112 }
113
114 static void
115 append_variable(char *name, char *val)
116 {
117         char *vname;
118         efi_guid_t guid;
119         size_t datalen;
120         uint8_t *data;
121
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");
126 }
127
128 static void
129 delete_variable(char *name)
130 {
131         char *vname;
132         efi_guid_t guid;
133
134         breakdown_name(name, &guid, &vname);
135         if (efi_del_variable(guid, vname) < 0)
136                 err(1, "efi_del_variable");
137 }
138
139 static void
140 write_variable(char *name, char *val)
141 {
142         char *vname;
143         efi_guid_t guid;
144         size_t datalen;
145         uint8_t *data;
146
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");
151 }
152
153 static void
154 devpath_dump(uint8_t *data, size_t datalen)
155 {
156         char buffer[1024];
157
158         efidp_format_device_path(buffer, sizeof(buffer),
159             (const_efidp)data, datalen);
160         if (!Nflag)
161                 printf(": ");
162         printf("%s\n", buffer);
163 }
164
165 static void
166 pretty_guid(efi_guid_t *guid, char **gname)
167 {
168         char *pretty = NULL;
169
170         if (gflag)
171                 efi_guid_to_name(guid, &pretty);
172
173         if (pretty == NULL)
174                 efi_guid_to_str(guid, gname);
175         else
176                 *gname = pretty;
177 }
178
179 static void
180 print_var(efi_guid_t *guid, char *name)
181 {
182         uint32_t att;
183         uint8_t *data;
184         size_t datalen;
185         char *gname;
186         int rv;
187
188         pretty_guid(guid, &gname);
189         if (pflag) {
190                 rv = efi_get_variable(*guid, name, &data, &datalen, &att);
191
192                 if (rv < 0)
193                         err(1, "%s-%s", gname, name);
194
195                 if (!Nflag)
196                         printf("%s-%s\n", gname, name);
197                 if (load_opt_flag)
198                         efi_print_load_option(data, datalen, Aflag, bflag, uflag);
199                 else if (Aflag)
200                         asciidump(data, datalen);
201                 else if (uflag)
202                         utf8dump(data, datalen);
203                 else if (bflag)
204                         bindump(data, datalen);
205                 else if (dflag)
206                         devpath_dump(data, datalen);
207                 else
208                         hexdump(data, datalen);
209         } else {
210                 printf("%s-%s", gname, name);
211         }
212         free(gname);
213         if (!Nflag)
214                 printf("\n");
215 }
216
217 static void
218 print_variable(char *name)
219 {
220         char *vname;
221         efi_guid_t guid;
222
223         breakdown_name(name, &guid, &vname);
224         print_var(&guid, vname);
225 }
226
227 static void
228 print_variables(void)
229 {
230         int rv;
231         char *name = NULL;
232         efi_guid_t *guid = NULL;
233
234         while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
235                 print_var(guid, name);
236
237         if (rv < 0)
238                 err(1, "Error listing names");
239 }
240
241 static void
242 print_known_guid(void)
243 {
244         struct uuid_table *tbl;
245         int i, n;
246
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);
250 }
251
252 static void
253 parse_args(int argc, char **argv)
254 {
255         int ch, i;
256
257         while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpRt:uw",
258                     longopts, NULL)) != -1) {
259                 switch (ch) {
260                 case 'a':
261                         aflag++;
262                         break;
263                 case 'A':
264                         Aflag++;
265                         break;
266                 case 'b':
267                         bflag++;
268                         break;
269                 case 'd':
270                         dflag++;
271                         break;
272                 case 'D':
273                         Dflag++;
274                         break;
275                 case 'g':
276                         gflag++;
277                         break;
278                 case 'H':
279                         Hflag++;
280                         break;
281                 case 'l':
282                         lflag++;
283                         break;
284                 case 'L':
285                         Lflag++;
286                         break;
287                 case 'n':
288                         varname = optarg;
289                         break;
290                 case 'N':
291                         Nflag++;
292                         break;
293                 case 'O':
294                         load_opt_flag++;
295                         break;
296                 case 'p':
297                         pflag++;
298                         break;
299                 case 'R':
300                         Rflag++;
301                         break;
302                 case 't':
303                         attrib = strtoul(optarg, NULL, 16);
304                         break;
305                 case 'u':
306                         uflag++;
307                         break;
308                 case 'w':
309                         wflag++;
310                         break;
311                 case 'f':
312                 case 0:
313                         errx(1, "unknown or unimplemented option\n");
314                         break;
315                 default:
316                         usage();
317                 }
318         }
319         argc -= optind;
320         argv += optind;
321
322         if (argc == 1)
323                 varname = argv[0];
324
325         if (aflag + Dflag + wflag > 1) {
326                 warnx("Can only use one of -a (--append), "
327                     "-D (--delete) and -w (--write)");
328                 usage();
329         }
330
331         if (aflag + Dflag + wflag > 0 && varname == NULL) {
332                 warnx("Must specify a variable for -a (--append), "
333                     "-D (--delete) or -w (--write)");
334                 usage();
335         }
336
337         if (aflag)
338                 append_variable(varname, NULL);
339         else if (Dflag)
340                 delete_variable(varname);
341         else if (wflag)
342                 write_variable(varname, NULL);
343         else if (Lflag)
344                 print_known_guid();
345         else if (varname) {
346                 pflag++;
347                 print_variable(varname);
348         } else if (argc > 0) {
349                 pflag++;
350                 for (i = 0; i < argc; i++)
351                         print_variable(argv[i]);
352         } else
353                 print_variables();
354 }
355
356 int
357 main(int argc, char **argv)
358 {
359
360         parse_args(argc, argv);
361 }