xref: /freebsd/usr.sbin/efivar/efivar.c (revision 48363f39f1417df3e39da53f219596f8501c9452)
1 /*-
2  * Copyright (c) 2016-2021 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 #include <ctype.h>
28 #include <efivar.h>
29 #include <efivar-dp.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.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' }, /* unimplemnted clash with linux version */
62 	{ "quiet",		no_argument,		NULL,	'q' },
63 	{ "raw-guid",		no_argument,		NULL,   'R' },
64 	{ "utf8",		no_argument,		NULL,	'u' },
65 	{ "write",		no_argument,		NULL,	'w' },
66 	{ NULL,			0,			NULL,	0 }
67 };
68 
69 
70 static bool aflag, Aflag, bflag, dflag, Dflag, gflag, Hflag, Nflag,
71 	lflag, Lflag, Rflag, wflag, pflag, uflag, load_opt_flag, quiet;
72 static char *varname;
73 static char *fromfile;
74 static u_long attrib = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
75 
76 static void
usage(void)77 usage(void)
78 {
79 
80 	errx(1, "efivar [-abdDHlLNpqRtuw] [-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"
84 	    "\t[--quiet]\n"
85 	    "\tname[=value]");
86 }
87 
88 static void
rep_err(int eval,const char * fmt,...)89 rep_err(int eval, const char *fmt, ...)
90 {
91 	va_list ap;
92 
93 	if (quiet)
94 		exit(eval);
95 
96 	va_start(ap, fmt);
97 	verr(eval, fmt, ap);
98 	va_end(ap);
99 }
100 
101 static void
rep_errx(int eval,const char * fmt,...)102 rep_errx(int eval, const char *fmt, ...)
103 {
104 	va_list ap;
105 
106 	if (quiet)
107 		exit(eval);
108 
109 	va_start(ap, fmt);
110 	verrx(eval, fmt, ap);
111 	va_end(ap);
112 }
113 
114 static void
breakdown_name(char * name,efi_guid_t * guid,char ** vname)115 breakdown_name(char *name, efi_guid_t *guid, char **vname)
116 {
117 	char *cp, *ocp;
118 
119 	ocp = NULL;
120 	while (true) {
121 		cp = strrchr(name, '-');
122 		if (cp == NULL) {
123 			if (ocp != NULL)
124 				*ocp = '-';
125 			rep_errx(1, "Invalid guid in: %s", name);
126 		}
127 		if (ocp != NULL)
128 			*ocp = '-';
129 		*vname = cp + 1;
130 		*cp = '\0';
131 		ocp = cp;
132 		if (efi_name_to_guid(name, guid) >= 0)
133 			break;
134 	}
135 }
136 
137 static uint8_t *
get_value(char * val,size_t * datalen)138 get_value(char *val, size_t *datalen)
139 {
140 	static char buffer[16*1024];
141 
142 	if (val != NULL) {
143 		*datalen = strlen(val);
144 		return ((uint8_t *)val);
145 	}
146 	/* Read from stdin */
147 	*datalen = sizeof(buffer);
148 	*datalen = read(0, buffer, *datalen);
149 	return ((uint8_t *)buffer);
150 }
151 
152 static void
append_variable(char * name,char * val)153 append_variable(char *name, char *val)
154 {
155 	char *vname;
156 	efi_guid_t guid;
157 	size_t datalen;
158 	uint8_t *data;
159 
160 	breakdown_name(name, &guid, &vname);
161 	data = get_value(val, &datalen);
162 	if (efi_append_variable(guid, vname, data, datalen, attrib) < 0)
163 		rep_err(1, "efi_append_variable");
164 }
165 
166 static void
delete_variable(char * name)167 delete_variable(char *name)
168 {
169 	char *vname;
170 	efi_guid_t guid;
171 
172 	breakdown_name(name, &guid, &vname);
173 	if (efi_del_variable(guid, vname) < 0)
174 		rep_err(1, "efi_del_variable");
175 }
176 
177 static void
write_variable(char * name,char * val)178 write_variable(char *name, char *val)
179 {
180 	char *vname;
181 	efi_guid_t guid;
182 	size_t datalen;
183 	uint8_t *data;
184 
185 	breakdown_name(name, &guid, &vname);
186 	data = get_value(val, &datalen);
187 	if (efi_set_variable(guid, vname, data, datalen, attrib) < 0)
188 		rep_err(1, "efi_set_variable");
189 }
190 
191 static void
devpath_dump(uint8_t * data,size_t datalen)192 devpath_dump(uint8_t *data, size_t datalen)
193 {
194 	char buffer[1024];
195 
196 	efidp_format_device_path(buffer, sizeof(buffer),
197 	    (const_efidp)data, datalen);
198 	if (!Nflag)
199 		printf(": ");
200 	printf("%s\n", buffer);
201 }
202 
203 static void
pretty_guid(efi_guid_t * guid,char ** gname)204 pretty_guid(efi_guid_t *guid, char **gname)
205 {
206 	char *pretty = NULL;
207 
208 	if (gflag)
209 		efi_guid_to_name(guid, &pretty);
210 
211 	if (pretty == NULL)
212 		efi_guid_to_str(guid, gname);
213 	else
214 		*gname = pretty;
215 }
216 
217 static void
print_var(efi_guid_t * guid,char * name)218 print_var(efi_guid_t *guid, char *name)
219 {
220 	uint32_t att;
221 	uint8_t *data;
222 	size_t datalen;
223 	char *gname = NULL;
224 	int rv;
225 
226 	if (guid)
227 		pretty_guid(guid, &gname);
228 	if (pflag || fromfile) {
229 		if (fromfile) {
230 			int fd;
231 
232 			fd = open(fromfile, O_RDONLY);
233 			if (fd < 0)
234 				rep_err(1, "open %s", fromfile);
235 			data = malloc(64 * 1024);
236 			if (data == NULL)
237 				rep_err(1, "malloc");
238 			datalen = read(fd, data, 64 * 1024);
239 			if ((ssize_t)datalen < 0)
240 				rep_err(1, "read");
241 			if (datalen == 0)
242 				rep_errx(1, "empty file");
243 			close(fd);
244 		} else {
245 			rv = efi_get_variable(*guid, name, &data, &datalen, &att);
246 			if (rv < 0)
247 				rep_err(1, "fetching %s-%s", gname, name);
248 		}
249 
250 
251 		if (!Nflag)
252 			printf("%s-%s\n", gname, name);
253 		if (load_opt_flag)
254 			efi_print_load_option(data, datalen, Aflag, bflag, uflag);
255 		else if (Aflag)
256 			efi_asciidump(data, datalen, 0);
257 		else if (uflag)
258 			efi_utf8dump(data, datalen, 0);
259 		else if (bflag)
260 			efi_bindump(data, datalen);
261 		else if (dflag)
262 			devpath_dump(data, datalen);
263 		else
264 			efi_hexdump(data, datalen, 0);
265 	} else {
266 		printf("%s-%s", gname, name);
267 	}
268 	free(gname);
269 	if (!Nflag)
270 		printf("\n");
271 }
272 
273 static void
print_variable(char * name)274 print_variable(char *name)
275 {
276 	char *vname;
277 	efi_guid_t guid;
278 
279 	breakdown_name(name, &guid, &vname);
280 	print_var(&guid, vname);
281 }
282 
283 static void
print_variables(void)284 print_variables(void)
285 {
286 	int rv;
287 	char *name = NULL;
288 	efi_guid_t *guid = NULL;
289 
290 	while ((rv = efi_get_next_variable_name(&guid, &name)) > 0)
291 		print_var(guid, name);
292 
293 	if (rv < 0)
294 		rep_err(1, "Error listing names");
295 }
296 
297 static void
print_known_guid(void)298 print_known_guid(void)
299 {
300 	struct guid_table *tbl;
301 	int i, n;
302 
303 	n = efi_known_guid(&tbl);
304 	for (i = 0; i < n; i++)
305 		printf("%s %s\n", tbl[i].uuid_str, tbl[i].name);
306 }
307 
308 static void
parse_args(int argc,char ** argv)309 parse_args(int argc, char **argv)
310 {
311 	int ch, i;
312 
313 	while ((ch = getopt_long(argc, argv, "aAbdDf:gHlLNn:OpqRt:uw",
314 		    longopts, NULL)) != -1) {
315 		switch (ch) {
316 		case 'a':
317 			aflag = true;
318 			break;
319 		case 'A':
320 			Aflag = true;
321 			break;
322 		case 'b':
323 			bflag = true;
324 			break;
325 		case 'd':
326 			dflag = true;
327 			break;
328 		case 'D':
329 			Dflag = true;
330 			break;
331 		case 'g':
332 			gflag = true;
333 			break;
334 		case 'H':
335 			Hflag = true;
336 			break;
337 		case 'l':
338 			lflag = true;
339 			break;
340 		case 'L':
341 			Lflag = true;
342 			break;
343 		case 'n':
344 			varname = optarg;
345 			break;
346 		case 'N':
347 			Nflag = true;
348 			break;
349 		case 'O':
350 			load_opt_flag = true;
351 			break;
352 		case 'p':
353 			pflag = true;
354 			break;
355 		case 'q':
356 			quiet = true;
357 			break;
358 		case 'R':
359 			Rflag = true;
360 			break;
361 		case 't':
362 			attrib = strtoul(optarg, NULL, 16);
363 			break;
364 		case 'u':
365 			uflag = true;
366 			break;
367 		case 'w':
368 			wflag = true;
369 			break;
370 		case 'f':
371 			free(fromfile);
372 			fromfile = strdup(optarg);
373 			break;
374 		case 0:
375 			rep_errx(1, "unknown or unimplemented option\n");
376 			break;
377 		default:
378 			usage();
379 		}
380 	}
381 	argc -= optind;
382 	argv += optind;
383 
384 	if (argc == 1)
385 		varname = argv[0];
386 
387 	if ((int)aflag + (int)Dflag + (int)wflag > 1) {
388 		warnx("Can only use one of -a (--append), "
389 		    "-D (--delete) and -w (--write)");
390 		usage();
391 	}
392 
393 	if ((int)aflag + (int)Dflag + (int)wflag > 0 && varname == NULL) {
394 		warnx("Must specify a variable for -a (--append), "
395 		    "-D (--delete) or -w (--write)");
396 		usage();
397 	}
398 
399 	if (aflag)
400 		append_variable(varname, NULL);
401 	else if (Dflag)
402 		delete_variable(varname);
403 	else if (wflag)
404 		write_variable(varname, NULL);
405 	else if (Lflag)
406 		print_known_guid();
407 	else if (fromfile) {
408 		Nflag = true;
409 		print_var(NULL, NULL);
410 	} else if (varname) {
411 		pflag = true;
412 		print_variable(varname);
413 	} else if (argc > 0) {
414 		pflag = true;
415 		for (i = 0; i < argc; i++)
416 			print_variable(argv[i]);
417 	} else
418 		print_variables();
419 }
420 
421 int
main(int argc,char ** argv)422 main(int argc, char **argv)
423 {
424 
425 	parse_args(argc, argv);
426 }
427