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