xref: /freebsd/usr.sbin/efitable/efitable.c (revision 3a4e049c38143980e472b2926abde3c7549530c4)
1 /*-
2  * Copyright (c) 2021 3mdeb Embedded Systems Consulting <contact@3mdeb.com>
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/types.h>
27 #include <sys/efi.h>
28 #include <sys/efiio.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sysexits.h>
38 #include <unistd.h>
39 #include <uuid.h>
40 #include <libxo/xo.h>
41 
42 #define TABLE_MAX_LEN 30
43 #define EFITABLE_XO_VERSION "1"
44 
45 static void efi_table_print_esrt(const void *data);
46 static void efi_table_print_prop(const void *data);
47 static void efi_table_print_memory(const void *data);
48 static void usage(void) __dead2;
49 
50 struct efi_table_op {
51 	char name[TABLE_MAX_LEN];
52 	void (*parse) (const void *);
53 	efi_guid_t guid;
54 };
55 
56 static const struct efi_table_op efi_table_ops[] = {
57 	{ .name = "esrt", .parse = efi_table_print_esrt,
58 	    .guid = EFI_TABLE_ESRT },
59 	{ .name = "prop", .parse = efi_table_print_prop,
60 	    .guid = EFI_PROPERTIES_TABLE },
61 	{ .name = "memory", .parse = efi_table_print_memory,
62 	    .guid = EFI_MEMORY_ATTRIBUTES_TABLE }
63 };
64 
65 int
main(int argc,char ** argv)66 main(int argc, char **argv)
67 {
68 	struct efi_get_table_ioc table = {
69 		.buf = NULL,
70 		.buf_len = 0,
71 		.table_len = 0
72 	};
73 	int efi_fd, ch, rc = 1, efi_idx = -1;
74 	bool got_table = false;
75 	bool table_set = false;
76 	bool uuid_set = false;
77 	struct option longopts[] = {
78 		{ "guid",  required_argument, NULL, 'g' },
79 		{ "table", required_argument, NULL, 't' },
80 		{ "uuid",  required_argument, NULL, 'u' },
81 		{ NULL,    0,                 NULL,  0  }
82 	};
83 
84 	argc = xo_parse_args(argc, argv);
85 	if (argc < 0)
86 		exit(EXIT_FAILURE);
87 
88 	while ((ch = getopt_long(argc, argv, "g:t:u:", longopts, NULL)) != -1) {
89 		switch (ch) {
90 		case 'g':
91 		case 'u':
92 		{
93 			char *uuid_str = optarg;
94 			struct uuid uuid;
95 			uint32_t status;
96 
97 			/*
98 			 * Note: we use the uuid parsing routine to parse the
99 			 * guid strings. However, EFI defines a slightly
100 			 * different structure to access them. We unify on
101 			 * using a structure that's compatible with EDK2
102 			 * EFI_GUID structure.
103 			 */
104 
105 			uuid_set = 1;
106 
107 			uuid_from_string(uuid_str, &uuid, &status);
108 			if (status != uuid_s_ok)
109 				xo_errx(EX_DATAERR, "invalid UUID");
110 
111 			for (size_t n = 0; n < nitems(efi_table_ops); n++) {
112 				if (!memcmp(&uuid, &efi_table_ops[n].guid,
113 				    sizeof(uuid))) {
114 					efi_idx = n;
115 					got_table = true;
116 					break;
117 				}
118 			}
119 			break;
120 		}
121 		case 't':
122 		{
123 			char *table_name = optarg;
124 
125 			table_set = true;
126 
127 			for (size_t n = 0; n < nitems(efi_table_ops); n++) {
128 				if (!strcmp(table_name,
129 				    efi_table_ops[n].name)) {
130 					efi_idx = n;
131 					got_table = true;
132 					break;
133 				}
134 			}
135 
136 			if (!got_table)
137 				xo_errx(EX_DATAERR, "unsupported efi table");
138 
139 			break;
140 		}
141 		default:
142 			usage();
143 		}
144 	}
145 
146 	if (!table_set && !uuid_set)
147 		xo_errx(EX_USAGE, "table is not set");
148 
149 	if (!got_table)
150 		xo_errx(EX_DATAERR, "unsupported table");
151 
152 	efi_fd = open("/dev/efi", O_RDWR);
153 	if (efi_fd < 0)
154 		xo_err(EX_OSFILE, "/dev/efi");
155 
156 	memcpy(&table.uuid, &efi_table_ops[efi_idx].guid, sizeof(struct uuid));
157 	if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1)
158 		xo_err(EX_OSERR, "EFIIOC_GET_TABLE (len == 0)");
159 
160 	table.buf = malloc(table.table_len);
161 	table.buf_len = table.table_len;
162 
163 	if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1)
164 		xo_err(EX_OSERR, "EFIIOC_GET_TABLE");
165 
166 	efi_table_ops[efi_idx].parse(table.buf);
167 	close(efi_fd);
168 
169 	return (rc);
170 }
171 
172 static void
efi_table_print_esrt(const void * data)173 efi_table_print_esrt(const void *data)
174 {
175 	const struct efi_esrt_entry_v1 *entries_v1;
176 	const struct efi_esrt_table *esrt;
177 
178 	esrt = (const struct efi_esrt_table *)data;
179 
180 	xo_set_version(EFITABLE_XO_VERSION);
181 	xo_open_container("esrt");
182 	xo_emit("{Lwc:FwResourceCount}{:fw_resource_count/%u}\n",
183 	    esrt->fw_resource_count);
184 	xo_emit("{Lwc:FwResourceCountMax}{:fw_resource_count_max/%u}\n",
185 	    esrt->fw_resource_count_max);
186 	xo_emit("{Lwc:FwResourceVersion}{:fw_resource_version/%u}\n",
187 	    esrt->fw_resource_version);
188 	xo_open_list("entries");
189 	xo_emit("\nEntries:\n");
190 
191 	entries_v1 = (const void *) esrt->entries;
192 	for (uint32_t i = 0; i < esrt->fw_resource_count; i++) {
193 		const struct efi_esrt_entry_v1 *e = &entries_v1[i];
194 		uint32_t status;
195 		char *uuid;
196 
197 		uuid_to_string((const uuid_t *)&e->fw_class, &uuid, &status);
198 		if (status != uuid_s_ok) {
199 			xo_errx(EX_DATAERR, "uuid_to_string error");
200 		}
201 
202 		xo_open_instance("entries");
203 		xo_emit("\n");
204 		xo_emit("{P:  }{Lwc:FwClass}{:fw_class/%s}\n", uuid);
205 		xo_emit("{P:  }{Lwc:FwType}{:fw_type/%u}\n", e->fw_type);
206 		xo_emit("{P:  }{Lwc:FwVersion}{:fw_version/%u}\n",
207 		    e->fw_version);
208 		xo_emit("{P:  }{Lwc:LowestSupportedFwVersion}"
209 		    "{:lowest_supported_fw_version/%u}\n",
210 		    e->lowest_supported_fw_version);
211 		xo_emit("{P:  }{Lwc:CapsuleFlags}{:capsule_flags/%#x}\n",
212 		    e->capsule_flags);
213 		xo_emit("{P:  }{Lwc:LastAttemptVersion"
214 		    "}{:last_attempt_version/%u}\n", e->last_attempt_version);
215 		xo_emit("{P:  }{Lwc:LastAttemptStatus"
216 		    "}{:last_attempt_status/%u}\n", e->last_attempt_status);
217 
218 		xo_close_instance("entries");
219 	}
220 
221 	xo_close_list("entries");
222 	xo_close_container("esrt");
223 	if (xo_finish() < 0)
224 		xo_err(EX_IOERR, "stdout");
225 }
226 
227 static void
efi_table_print_prop(const void * data)228 efi_table_print_prop(const void *data)
229 {
230 	const struct efi_prop_table *prop;
231 
232 	prop = (const struct efi_prop_table *)data;
233 
234 	xo_set_version(EFITABLE_XO_VERSION);
235 	xo_open_container("prop");
236 	xo_emit("{Lwc:Version}{:version/%#x}\n", prop->version);
237 	xo_emit("{Lwc:Length}{:length/%u}\n", prop->length);
238 	xo_emit("{Lwc:MemoryProtectionAttribute}"
239 	    "{:memory_protection_attribute/%#lx}\n",
240 	    prop->memory_protection_attribute);
241 	xo_close_container("prop");
242 	if (xo_finish() < 0)
243 		xo_err(EX_IOERR, "stdout");
244 }
245 
246 static void
efi_table_print_memory(const void * data)247 efi_table_print_memory(const void *data)
248 {
249 	const struct efi_memory_attribute_table *attr =
250 	    (const struct efi_memory_attribute_table *)data;
251 	const struct efi_memory_descriptor *desc;
252 	int i, nentries;
253 
254 	nentries = attr->num_ents;
255 	desc = attr->tables;
256 
257 	xo_set_version(EFITABLE_XO_VERSION);
258 
259 	xo_open_container("memory");
260 	xo_emit("{Lwc:Version}{:version/%#x}\n", attr->version);
261 	xo_emit("{Lwc:Length}{:length/%u}\n", attr->descriptor_size);
262 	xo_emit("{Lwc:Entries}{:entries/%u}\n", attr->num_ents);
263 
264 	xo_open_container("attributes");
265 
266 	/*
267 	 * According to https://forum.osdev.org/viewtopic.php?t=32953, the size
268 	 * of records into the attribute table never equals to
269 	 * sizeof(efi_memory_descriptor). The correct one for indexing the array
270 	 * resides in the attributet table.
271 	 */
272 	for (i = 0; i < nentries; i++) {
273 		xo_emit("{Lwc:ID}{:id/%#x}\n", i);
274 		xo_emit("{Lwc:Attributes}{:attributes/%#x}\n", desc->attrs);
275 		xo_emit("{Lwc:Type}{:type/%#x}\n", desc->type);
276 		xo_emit("{Lwc:Pages}{:pages/%#x}\n", desc->pages);
277 		xo_emit("{Lwc:Phyaddr}{:phyaddr/%#p}\n", desc->phy_addr);
278 		xo_emit("{Lwc:Virtaddr}{:virtaddr/%#p}\n", desc->virt_addr);
279 		desc = (const struct efi_memory_descriptor *)(const void *)
280 		    ((const char *)desc + attr->descriptor_size);
281 	}
282 
283 	xo_close_container("attributes");
284 
285 	xo_close_container("memory");
286 
287 	if (xo_finish() < 0)
288 		xo_err(EX_IOERR, "stdout");
289 }
290 
usage(void)291 static void usage(void)
292 {
293 	xo_error("usage: efitable [-g guid | -t name] [--libxo]\n");
294 	exit(EX_USAGE);
295 }
296