xref: /freebsd/usr.sbin/efivar/efiutil.c (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
1 /*-
2  * Copyright (c) 2017 Netflix, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <ctype.h>
31 #include <efivar.h>
32 #include <efivar-dp.h>
33 #include <err.h>
34 #include <errno.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 #include <efivar-dp.h>
44 
45 /*
46  * Dump the data as ASCII data, which is a pretty
47  * printed form
48  */
49 void
50 asciidump(uint8_t *data, size_t datalen)
51 {
52 	size_t i;
53 	int len;
54 
55 	len = 0;
56 	for (i = 0; i < datalen; i++) {
57 		if (isprint(data[i])) {
58 			len++;
59 			if (len > 80) {
60 				len = 0;
61 				printf("\n");
62 			}
63 			printf("%c", data[i]);
64 		} else {
65 			len +=3;
66 			if (len > 80) {
67 				len = 0;
68 				printf("\n");
69 			}
70 			printf("%%%02x", data[i]);
71 		}
72 	}
73 	printf("\n");
74 }
75 
76 void
77 utf8dump(uint8_t *data, size_t datalen)
78 {
79 	char *utf8 = NULL;
80 	efi_char *ucs2;
81 
82 	/*
83 	 * NUL terminate the string. Not all strings need it, but some
84 	 * do and an extra NUL won't change what's printed.
85 	 */
86 	ucs2 = malloc(datalen + sizeof(efi_char));
87 	memcpy(ucs2, data, datalen);
88 	ucs2[datalen / sizeof(efi_char)] = 0;
89 	ucs2_to_utf8(ucs2, &utf8);
90 	printf("%s\n", utf8);
91 	free(utf8);
92 	free(ucs2);
93 }
94 
95 void
96 hexdump(uint8_t *data, size_t datalen)
97 {
98 	size_t i;
99 
100 	for (i = 0; i < datalen; i++) {
101 		if (i % 16 == 0) {
102 			if (i != 0)
103 				printf("\n");
104 			printf("%04x: ", (int)i);
105 		}
106 		printf("%02x ", data[i]);
107 	}
108 	printf("\n");
109 }
110 
111 void
112 bindump(uint8_t *data, size_t datalen)
113 {
114 	write(1, data, datalen);
115 }
116 
117 #define LOAD_OPTION_ACTIVE 1
118 
119 void
120 efi_print_load_option(uint8_t *data, size_t datalen, int Aflag, int bflag, int uflag)
121 {
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 
134 	if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
135 		return;
136 	// First 4 bytes are attribute flags
137 	attr = le32dec(walker);
138 	walker += sizeof(attr);
139 	// Next two bytes are length of the file paths
140 	fplen = le16dec(walker);
141 	walker += sizeof(fplen);
142 	// Next we have a 0 terminated UCS2 string that we know to be aligned
143 	descr = (efi_char *)(intptr_t)(void *)walker;
144 	len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
145 	walker += (len + 1) * sizeof(efi_char);
146 	if (walker > ep)
147 		return;
148 	// Now we have fplen bytes worth of file path stuff
149 	dp = (efidp)walker;
150 	walker += fplen;
151 	if (walker > ep)
152 		return;
153 	edp = (efidp)walker;
154 	// Everything left is the binary option args
155 	opt = walker;
156 	optlen = ep - walker;
157 	// We got to here, everything is good
158 	printf("%c ", attr & LOAD_OPTION_ACTIVE ? '*' : ' ');
159 	ucs2_to_utf8(descr, &str);
160 	printf("%s", str);
161 	free(str);
162 	while (dp < edp) {
163 		efidp_format_device_path(buf, sizeof(buf), dp,
164 		    (intptr_t)(void *)edp - (intptr_t)(void *)dp);
165 		dp = (efidp)((char *)dp + efidp_size(dp));
166 		printf(" %s\n", buf);
167 	}
168 	if (optlen == 0)
169 		return;
170 	printf("Options: ");
171 	if (Aflag)
172 		asciidump(opt, optlen);
173 	else if (bflag)
174 		bindump(opt, optlen);
175 	else if (uflag)
176 		utf8dump(opt, optlen);
177 	else
178 		hexdump(opt, optlen);
179 }
180