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