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