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