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