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