xref: /freebsd/usr.sbin/efivar/efiutil.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * Copyright (c) 2017-2019 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 #include <efivar-dp.h>
43 
44 /*
45  * Dump the data as ASCII data, which is a pretty
46  * printed form
47  */
48 void
49 asciidump(uint8_t *data, size_t datalen)
50 {
51 	size_t i;
52 	int len;
53 
54 	len = 0;
55 	for (i = 0; i < datalen; i++) {
56 		if (isprint(data[i])) {
57 			len++;
58 			if (len > 80) {
59 				len = 0;
60 				printf("\n");
61 			}
62 			printf("%c", data[i]);
63 		} else {
64 			len +=3;
65 			if (len > 80) {
66 				len = 0;
67 				printf("\n");
68 			}
69 			printf("%%%02x", data[i]);
70 		}
71 	}
72 	printf("\n");
73 }
74 
75 void
76 utf8dump(uint8_t *data, size_t datalen)
77 {
78 	char *utf8 = NULL;
79 	efi_char *ucs2;
80 
81 	/*
82 	 * NUL terminate the string. Not all strings need it, but some
83 	 * do and an extra NUL won't change what's printed.
84 	 */
85 	ucs2 = malloc(datalen + sizeof(efi_char));
86 	memcpy(ucs2, data, datalen);
87 	ucs2[datalen / sizeof(efi_char)] = 0;
88 	ucs2_to_utf8(ucs2, &utf8);
89 	printf("%s\n", utf8);
90 	free(utf8);
91 	free(ucs2);
92 }
93 
94 void
95 hexdump(uint8_t *data, size_t datalen)
96 {
97 	size_t i;
98 
99 	for (i = 0; i < datalen; i++) {
100 		if (i % 16 == 0) {
101 			if (i != 0)
102 				printf("\n");
103 			printf("%04x: ", (int)i);
104 		}
105 		printf("%02x ", data[i]);
106 	}
107 	printf("\n");
108 }
109 
110 void
111 bindump(uint8_t *data, size_t datalen)
112 {
113 	write(1, data, datalen);
114 }
115 
116 #define LOAD_OPTION_ACTIVE 1
117 
118 #define SIZE(dp, edp) (size_t)((intptr_t)(void *)edp - (intptr_t)(void *)dp)
119 
120 void
121 efi_print_load_option(uint8_t *data, size_t datalen, int Aflag, int bflag, int uflag)
122 {
123 	char *dev, *relpath, *abspath;
124 	uint8_t *ep = data + datalen;
125 	uint8_t *walker = data;
126 	uint32_t attr;
127 	uint16_t fplen;
128 	efi_char *descr;
129 	efidp dp, edp;
130 	char *str = NULL;
131 	char buf[1024];
132 	int len;
133 	void *opt;
134 	int optlen;
135 	int rv;
136 
137 	if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
138 		return;
139 	// First 4 bytes are attribute flags
140 	attr = le32dec(walker);
141 	walker += sizeof(attr);
142 	// Next two bytes are length of the file paths
143 	fplen = le16dec(walker);
144 	walker += sizeof(fplen);
145 	// Next we have a 0 terminated UCS2 string that we know to be aligned
146 	descr = (efi_char *)(intptr_t)(void *)walker;
147 	len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
148 	walker += (len + 1) * sizeof(efi_char);
149 	if (walker > ep)
150 		return;
151 	// Now we have fplen bytes worth of file path stuff
152 	dp = (efidp)walker;
153 	walker += fplen;
154 	if (walker > ep)
155 		return;
156 	edp = (efidp)walker;
157 	// Everything left is the binary option args
158 	opt = walker;
159 	optlen = ep - walker;
160 	// We got to here, everything is good
161 	printf("%c ", attr & LOAD_OPTION_ACTIVE ? '*' : ' ');
162 	ucs2_to_utf8(descr, &str);
163 	printf("%s\n", str);
164 	free(str);
165 	if (fplen <= 4) {
166 		printf("Empty path\n");
167 	} else {
168 		while (dp < edp && SIZE(dp, edp) > sizeof(efidp_header)) {
169 			efidp_format_device_path(buf, sizeof(buf), dp, SIZE(dp, edp));
170 			rv = efivar_device_path_to_unix_path(dp, &dev, &relpath, &abspath);
171 			dp = (efidp)((char *)dp + efidp_size(dp));
172 			printf(" %s\n", buf);
173 			if (rv == 0) {
174 				printf("      %*s:%s\n", len + (int)strlen(dev), dev, relpath);
175 				free(dev);
176 				free(relpath);
177 				free(abspath);
178 			}
179 		}
180 	}
181 	if (optlen == 0)
182 		return;
183 	printf("Option:\n");
184 	if (Aflag)
185 		asciidump(opt, optlen);
186 	else if (bflag)
187 		bindump(opt, optlen);
188 	else if (uflag)
189 		utf8dump(opt, optlen);
190 	else
191 		hexdump(opt, optlen);
192 }
193