xref: /freebsd/usr.sbin/efitable/efitable.c (revision 6366114c716f9dca099a4090269a700b94a6173f)
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 usage(void) __dead2;
48 
49 struct efi_table_op {
50 	char name[TABLE_MAX_LEN];
51 	void (*parse) (const void *);
52 	efi_guid_t guid;
53 };
54 
55 static const struct efi_table_op efi_table_ops[] = {
56 	{ .name = "esrt", .parse = efi_table_print_esrt,
57 	    .guid = EFI_TABLE_ESRT },
58 	{ .name = "prop", .parse = efi_table_print_prop,
59 	    .guid = EFI_PROPERTIES_TABLE }
60 };
61 
62 int
main(int argc,char ** argv)63 main(int argc, char **argv)
64 {
65 	struct efi_get_table_ioc table = {
66 		.buf = NULL,
67 		.buf_len = 0,
68 		.table_len = 0
69 	};
70 	int efi_fd, ch, rc = 1, efi_idx = -1;
71 	bool got_table = false;
72 	bool table_set = false;
73 	bool uuid_set = false;
74 	struct option longopts[] = {
75 		{ "uuid",  required_argument, NULL, 'u' },
76 		{ "table", required_argument, NULL, 't' },
77 		{ NULL,    0,                 NULL,  0  }
78 	};
79 
80 	argc = xo_parse_args(argc, argv);
81 	if (argc < 0)
82 		exit(EXIT_FAILURE);
83 
84 	while ((ch = getopt_long(argc, argv, "g:t:u:", longopts, NULL)) != -1) {
85 		switch (ch) {
86 		case 'g':
87 		case 'u':
88 		{
89 			char *uuid_str = optarg;
90 			struct uuid uuid;
91 			uint32_t status;
92 
93 			/*
94 			 * Note: we use the uuid parsing routine to parse the
95 			 * guid strings. However, EFI defines a slightly
96 			 * different structure to access them. We unify on
97 			 * using a structure that's compatible with EDK2
98 			 * EFI_GUID structure.
99 			 */
100 
101 			uuid_set = 1;
102 
103 			uuid_from_string(uuid_str, &uuid, &status);
104 			if (status != uuid_s_ok)
105 				xo_errx(EX_DATAERR, "invalid UUID");
106 
107 			for (size_t n = 0; n < nitems(efi_table_ops); n++) {
108 				if (!memcmp(&uuid, &efi_table_ops[n].guid,
109 				    sizeof(uuid))) {
110 					efi_idx = n;
111 					got_table = true;
112 					break;
113 				}
114 			}
115 			break;
116 		}
117 		case 't':
118 		{
119 			char *table_name = optarg;
120 
121 			table_set = true;
122 
123 			for (size_t n = 0; n < nitems(efi_table_ops); n++) {
124 				if (!strcmp(table_name,
125 				    efi_table_ops[n].name)) {
126 					efi_idx = n;
127 					got_table = true;
128 					break;
129 				}
130 			}
131 
132 			if (!got_table)
133 				xo_errx(EX_DATAERR, "unsupported efi table");
134 
135 			break;
136 		}
137 		default:
138 			usage();
139 		}
140 	}
141 
142 	if (!table_set && !uuid_set)
143 		xo_errx(EX_USAGE, "table is not set");
144 
145 	if (!got_table)
146 		xo_errx(EX_DATAERR, "unsupported table");
147 
148 	efi_fd = open("/dev/efi", O_RDWR);
149 	if (efi_fd < 0)
150 		xo_err(EX_OSFILE, "/dev/efi");
151 
152 	memcpy(&table.uuid, &efi_table_ops[efi_idx].guid, sizeof(struct uuid));
153 	if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1)
154 		xo_err(EX_OSERR, "EFIIOC_GET_TABLE (len == 0)");
155 
156 	table.buf = malloc(table.table_len);
157 	table.buf_len = table.table_len;
158 
159 	if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1)
160 		xo_err(EX_OSERR, "EFIIOC_GET_TABLE");
161 
162 	efi_table_ops[efi_idx].parse(table.buf);
163 	close(efi_fd);
164 
165 	return (rc);
166 }
167 
168 static void
efi_table_print_esrt(const void * data)169 efi_table_print_esrt(const void *data)
170 {
171 	const struct efi_esrt_entry_v1 *entries_v1;
172 	const struct efi_esrt_table *esrt;
173 
174 	esrt = (const struct efi_esrt_table *)data;
175 
176 	xo_set_version(EFITABLE_XO_VERSION);
177 	xo_open_container("esrt");
178 	xo_emit("{Lwc:FwResourceCount}{:fw_resource_count/%u}\n",
179 	    esrt->fw_resource_count);
180 	xo_emit("{Lwc:FwResourceCountMax}{:fw_resource_count_max/%u}\n",
181 	    esrt->fw_resource_count_max);
182 	xo_emit("{Lwc:FwResourceVersion}{:fw_resource_version/%u}\n",
183 	    esrt->fw_resource_version);
184 	xo_open_list("entries");
185 	xo_emit("\nEntries:\n");
186 
187 	entries_v1 = (const void *) esrt->entries;
188 	for (uint32_t i = 0; i < esrt->fw_resource_count; i++) {
189 		const struct efi_esrt_entry_v1 *e = &entries_v1[i];
190 		uint32_t status;
191 		char *uuid;
192 
193 		uuid_to_string((const uuid_t *)&e->fw_class, &uuid, &status);
194 		if (status != uuid_s_ok) {
195 			xo_errx(EX_DATAERR, "uuid_to_string error");
196 		}
197 
198 		xo_open_instance("entries");
199 		xo_emit("\n");
200 		xo_emit("{P:  }{Lwc:FwClass}{:fw_class/%s}\n", uuid);
201 		xo_emit("{P:  }{Lwc:FwType}{:fw_type/%u}\n", e->fw_type);
202 		xo_emit("{P:  }{Lwc:FwVersion}{:fw_version/%u}\n",
203 		    e->fw_version);
204 		xo_emit("{P:  }{Lwc:LowestSupportedFwVersion}"
205 		    "{:lowest_supported_fw_version/%u}\n",
206 		    e->lowest_supported_fw_version);
207 		xo_emit("{P:  }{Lwc:CapsuleFlags}{:capsule_flags/%#x}\n",
208 		    e->capsule_flags);
209 		xo_emit("{P:  }{Lwc:LastAttemptVersion"
210 		    "}{:last_attempt_version/%u}\n", e->last_attempt_version);
211 		xo_emit("{P:  }{Lwc:LastAttemptStatus"
212 		    "}{:last_attempt_status/%u}\n", e->last_attempt_status);
213 
214 		xo_close_instance("entries");
215 	}
216 
217 	xo_close_list("entries");
218 	xo_close_container("esrt");
219 	if (xo_finish() < 0)
220 		xo_err(EX_IOERR, "stdout");
221 }
222 
223 static void
efi_table_print_prop(const void * data)224 efi_table_print_prop(const void *data)
225 {
226 	const struct efi_prop_table *prop;
227 
228 	prop = (const struct efi_prop_table *)data;
229 
230 	xo_set_version(EFITABLE_XO_VERSION);
231 	xo_open_container("prop");
232 	xo_emit("{Lwc:Version}{:version/%#x}\n", prop->version);
233 	xo_emit("{Lwc:Length}{:length/%u}\n", prop->length);
234 	xo_emit("{Lwc:MemoryProtectionAttribute}"
235 	    "{:memory_protection_attribute/%#lx}\n",
236 	    prop->memory_protection_attribute);
237 	xo_close_container("prop");
238 	if (xo_finish() < 0)
239 		xo_err(EX_IOERR, "stdout");
240 }
241 
usage(void)242 static void usage(void)
243 {
244 	xo_error("usage: efitable [-g guid | -t name] [--libxo]\n");
245 	exit(EX_USAGE);
246 }
247