AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * UEFI Shell-like command |
| 4 | * |
| 5 | * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <charset.h> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <efi_loader.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 12 | #include <exports.h> |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 13 | #include <hexdump.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <search.h> |
| 16 | #include <linux/ctype.h> |
| 17 | |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 18 | #define BS systab.boottime |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 19 | #define RT systab.runtime |
| 20 | |
| 21 | /** |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 22 | * efi_get_device_handle_info() - get information of UEFI device |
| 23 | * |
| 24 | * @handle: Handle of UEFI device |
| 25 | * @dev_path_text: Pointer to text of device path |
| 26 | * Return: 0 on success, -1 on failure |
| 27 | * |
| 28 | * Currently return a formatted text of device path. |
| 29 | */ |
| 30 | static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text) |
| 31 | { |
| 32 | struct efi_device_path *dp; |
| 33 | efi_status_t ret; |
| 34 | |
| 35 | ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path, |
| 36 | (void **)&dp, NULL /* FIXME */, NULL, |
| 37 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 38 | if (ret == EFI_SUCCESS) { |
| 39 | *dev_path_text = efi_dp_str(dp); |
| 40 | return 0; |
| 41 | } else { |
| 42 | return -1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) |
| 47 | |
| 48 | static const char spc[] = " "; |
| 49 | static const char sep[] = "================"; |
| 50 | |
| 51 | /** |
| 52 | * do_efi_show_devices() - show UEFI devices |
| 53 | * |
| 54 | * @cmdtp: Command table |
| 55 | * @flag: Command flag |
| 56 | * @argc: Number of arguments |
| 57 | * @argv: Argument array |
| 58 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 59 | * |
| 60 | * Implement efidebug "devices" sub-command. |
| 61 | * Show all UEFI devices and their information. |
| 62 | */ |
| 63 | static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag, |
| 64 | int argc, char * const argv[]) |
| 65 | { |
| 66 | efi_handle_t *handles; |
| 67 | efi_uintn_t num, i; |
| 68 | u16 *dev_path_text; |
| 69 | efi_status_t ret; |
| 70 | |
| 71 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
| 72 | &num, &handles)); |
| 73 | if (ret != EFI_SUCCESS) |
| 74 | return CMD_RET_FAILURE; |
| 75 | |
| 76 | if (!num) |
| 77 | return CMD_RET_SUCCESS; |
| 78 | |
| 79 | printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc); |
| 80 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); |
| 81 | for (i = 0; i < num; i++) { |
| 82 | if (!efi_get_device_handle_info(handles[i], &dev_path_text)) { |
| 83 | printf("%p %ls\n", handles[i], dev_path_text); |
| 84 | efi_free_pool(dev_path_text); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | EFI_CALL(BS->free_pool(handles)); |
| 89 | |
| 90 | return CMD_RET_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | /** |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 94 | * efi_get_driver_handle_info() - get information of UEFI driver |
| 95 | * |
| 96 | * @handle: Handle of UEFI device |
| 97 | * @driver_name: Driver name |
| 98 | * @image_path: Pointer to text of device path |
| 99 | * Return: 0 on success, -1 on failure |
| 100 | * |
| 101 | * Currently return no useful information as all UEFI drivers are |
| 102 | * built-in.. |
| 103 | */ |
| 104 | static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name, |
| 105 | u16 **image_path) |
| 106 | { |
| 107 | struct efi_handler *handler; |
| 108 | struct efi_loaded_image *image; |
| 109 | efi_status_t ret; |
| 110 | |
| 111 | /* |
| 112 | * driver name |
| 113 | * TODO: support EFI_COMPONENT_NAME2_PROTOCOL |
| 114 | */ |
| 115 | *driver_name = NULL; |
| 116 | |
| 117 | /* image name */ |
| 118 | ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); |
| 119 | if (ret != EFI_SUCCESS) { |
| 120 | *image_path = NULL; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | image = handler->protocol_interface; |
| 125 | *image_path = efi_dp_str(image->file_path); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * do_efi_show_drivers() - show UEFI drivers |
| 132 | * |
| 133 | * @cmdtp: Command table |
| 134 | * @flag: Command flag |
| 135 | * @argc: Number of arguments |
| 136 | * @argv: Argument array |
| 137 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 138 | * |
| 139 | * Implement efidebug "drivers" sub-command. |
| 140 | * Show all UEFI drivers and their information. |
| 141 | */ |
| 142 | static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag, |
| 143 | int argc, char * const argv[]) |
| 144 | { |
| 145 | efi_handle_t *handles; |
| 146 | efi_uintn_t num, i; |
| 147 | u16 *driver_name, *image_path_text; |
| 148 | efi_status_t ret; |
| 149 | |
| 150 | ret = EFI_CALL(BS->locate_handle_buffer( |
| 151 | BY_PROTOCOL, &efi_guid_driver_binding_protocol, |
| 152 | NULL, &num, &handles)); |
| 153 | if (ret != EFI_SUCCESS) |
| 154 | return CMD_RET_FAILURE; |
| 155 | |
| 156 | if (!num) |
| 157 | return CMD_RET_SUCCESS; |
| 158 | |
| 159 | printf("Driver%.*s Name Image Path\n", |
| 160 | EFI_HANDLE_WIDTH - 6, spc); |
| 161 | printf("%.*s ==================== ====================\n", |
| 162 | EFI_HANDLE_WIDTH, sep); |
| 163 | for (i = 0; i < num; i++) { |
| 164 | if (!efi_get_driver_handle_info(handles[i], &driver_name, |
| 165 | &image_path_text)) { |
| 166 | if (image_path_text) |
| 167 | printf("%p %-20ls %ls\n", handles[i], |
| 168 | driver_name, image_path_text); |
| 169 | else |
| 170 | printf("%p %-20ls <built-in>\n", |
| 171 | handles[i], driver_name); |
| 172 | EFI_CALL(BS->free_pool(driver_name)); |
| 173 | EFI_CALL(BS->free_pool(image_path_text)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | EFI_CALL(BS->free_pool(handles)); |
| 178 | |
| 179 | return CMD_RET_SUCCESS; |
| 180 | } |
| 181 | |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 182 | static const struct { |
| 183 | const char *text; |
| 184 | const efi_guid_t guid; |
| 185 | } guid_list[] = { |
| 186 | { |
| 187 | "Device Path", |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 188 | EFI_DEVICE_PATH_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 189 | }, |
| 190 | { |
| 191 | "Device Path To Text", |
| 192 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, |
| 193 | }, |
| 194 | { |
| 195 | "Device Path Utilities", |
| 196 | EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, |
| 197 | }, |
| 198 | { |
| 199 | "Unicode Collation 2", |
| 200 | EFI_UNICODE_COLLATION_PROTOCOL2_GUID, |
| 201 | }, |
| 202 | { |
| 203 | "Driver Binding", |
| 204 | EFI_DRIVER_BINDING_PROTOCOL_GUID, |
| 205 | }, |
| 206 | { |
| 207 | "Simple Text Input", |
| 208 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, |
| 209 | }, |
| 210 | { |
| 211 | "Simple Text Input Ex", |
| 212 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, |
| 213 | }, |
| 214 | { |
| 215 | "Simple Text Output", |
| 216 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, |
| 217 | }, |
| 218 | { |
| 219 | "Block IO", |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 220 | EFI_BLOCK_IO_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 221 | }, |
| 222 | { |
| 223 | "Simple File System", |
| 224 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, |
| 225 | }, |
| 226 | { |
| 227 | "Loaded Image", |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 228 | EFI_LOADED_IMAGE_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 229 | }, |
| 230 | { |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 231 | "Graphics Output", |
| 232 | EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 233 | }, |
Heinrich Schuchardt | 391bc8a | 2019-04-20 07:57:28 +0200 | [diff] [blame] | 234 | { |
| 235 | "HII String", |
| 236 | EFI_HII_STRING_PROTOCOL_GUID, |
| 237 | }, |
| 238 | { |
| 239 | "HII Database", |
| 240 | EFI_HII_DATABASE_PROTOCOL_GUID, |
| 241 | }, |
| 242 | { |
| 243 | "HII Config Routing", |
| 244 | EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, |
| 245 | }, |
| 246 | { |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 247 | "Load File2", |
| 248 | EFI_LOAD_FILE2_PROTOCOL_GUID, |
| 249 | }, |
| 250 | { |
Heinrich Schuchardt | 391bc8a | 2019-04-20 07:57:28 +0200 | [diff] [blame] | 251 | "Simple Network", |
| 252 | EFI_SIMPLE_NETWORK_PROTOCOL_GUID, |
| 253 | }, |
| 254 | { |
| 255 | "PXE Base Code", |
| 256 | EFI_PXE_BASE_CODE_PROTOCOL_GUID, |
| 257 | }, |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 258 | /* Configuration table GUIDs */ |
| 259 | { |
| 260 | "ACPI table", |
| 261 | EFI_ACPI_TABLE_GUID, |
| 262 | }, |
| 263 | { |
| 264 | "device tree", |
| 265 | EFI_FDT_GUID, |
| 266 | }, |
| 267 | { |
| 268 | "SMBIOS table", |
| 269 | SMBIOS_TABLE_GUID, |
| 270 | }, |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 271 | { |
| 272 | "Runtime properties", |
| 273 | EFI_RT_PROPERTIES_TABLE_GUID, |
| 274 | }, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /** |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 278 | * get_guid_text - get string of GUID |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 279 | * |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 280 | * Return description of GUID. |
| 281 | * |
| 282 | * @guid: GUID |
| 283 | * Return: description of GUID or NULL |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 284 | */ |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 285 | static const char *get_guid_text(const void *guid) |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 286 | { |
| 287 | int i; |
| 288 | |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 289 | for (i = 0; i < ARRAY_SIZE(guid_list); i++) { |
| 290 | /* |
| 291 | * As guidcmp uses memcmp() we can safely accept unaligned |
| 292 | * GUIDs. |
| 293 | */ |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 294 | if (!guidcmp(&guid_list[i].guid, guid)) |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 295 | return guid_list[i].text; |
| 296 | } |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 297 | |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 298 | return NULL; |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * do_efi_show_handles() - show UEFI handles |
| 303 | * |
| 304 | * @cmdtp: Command table |
| 305 | * @flag: Command flag |
| 306 | * @argc: Number of arguments |
| 307 | * @argv: Argument array |
| 308 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 309 | * |
| 310 | * Implement efidebug "dh" sub-command. |
| 311 | * Show all UEFI handles and their information, currently all protocols |
| 312 | * added to handle. |
| 313 | */ |
| 314 | static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag, |
| 315 | int argc, char * const argv[]) |
| 316 | { |
| 317 | efi_handle_t *handles; |
| 318 | efi_guid_t **guid; |
| 319 | efi_uintn_t num, count, i, j; |
| 320 | const char *guid_text; |
| 321 | efi_status_t ret; |
| 322 | |
| 323 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
| 324 | &num, &handles)); |
| 325 | if (ret != EFI_SUCCESS) |
| 326 | return CMD_RET_FAILURE; |
| 327 | |
| 328 | if (!num) |
| 329 | return CMD_RET_SUCCESS; |
| 330 | |
| 331 | printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc); |
| 332 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); |
| 333 | for (i = 0; i < num; i++) { |
| 334 | printf("%p", handles[i]); |
| 335 | ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, |
| 336 | &count)); |
| 337 | if (ret || !count) { |
| 338 | putc('\n'); |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | for (j = 0; j < count; j++) { |
| 343 | if (j) |
| 344 | printf(", "); |
| 345 | else |
| 346 | putc(' '); |
| 347 | |
| 348 | guid_text = get_guid_text(guid[j]); |
| 349 | if (guid_text) |
| 350 | puts(guid_text); |
| 351 | else |
| 352 | printf("%pUl", guid[j]); |
| 353 | } |
| 354 | putc('\n'); |
| 355 | } |
| 356 | |
| 357 | EFI_CALL(BS->free_pool(handles)); |
| 358 | |
| 359 | return CMD_RET_SUCCESS; |
| 360 | } |
| 361 | |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 362 | /** |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 363 | * do_efi_show_images() - show UEFI images |
| 364 | * |
| 365 | * @cmdtp: Command table |
| 366 | * @flag: Command flag |
| 367 | * @argc: Number of arguments |
| 368 | * @argv: Argument array |
| 369 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 370 | * |
| 371 | * Implement efidebug "images" sub-command. |
| 372 | * Show all UEFI loaded images and their information. |
| 373 | */ |
| 374 | static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag, |
| 375 | int argc, char * const argv[]) |
| 376 | { |
| 377 | efi_print_image_infos(NULL); |
| 378 | |
| 379 | return CMD_RET_SUCCESS; |
| 380 | } |
| 381 | |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 382 | static const char * const efi_mem_type_string[] = { |
| 383 | [EFI_RESERVED_MEMORY_TYPE] = "RESERVED", |
| 384 | [EFI_LOADER_CODE] = "LOADER CODE", |
| 385 | [EFI_LOADER_DATA] = "LOADER DATA", |
| 386 | [EFI_BOOT_SERVICES_CODE] = "BOOT CODE", |
| 387 | [EFI_BOOT_SERVICES_DATA] = "BOOT DATA", |
| 388 | [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE", |
| 389 | [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA", |
| 390 | [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL", |
| 391 | [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM", |
| 392 | [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM", |
| 393 | [EFI_ACPI_MEMORY_NVS] = "ACPI NVS", |
| 394 | [EFI_MMAP_IO] = "IO", |
| 395 | [EFI_MMAP_IO_PORT] = "IO PORT", |
| 396 | [EFI_PAL_CODE] = "PAL", |
| 397 | }; |
| 398 | |
| 399 | static const struct efi_mem_attrs { |
| 400 | const u64 bit; |
| 401 | const char *text; |
| 402 | } efi_mem_attrs[] = { |
| 403 | {EFI_MEMORY_UC, "UC"}, |
| 404 | {EFI_MEMORY_UC, "UC"}, |
| 405 | {EFI_MEMORY_WC, "WC"}, |
| 406 | {EFI_MEMORY_WT, "WT"}, |
| 407 | {EFI_MEMORY_WB, "WB"}, |
| 408 | {EFI_MEMORY_UCE, "UCE"}, |
| 409 | {EFI_MEMORY_WP, "WP"}, |
| 410 | {EFI_MEMORY_RP, "RP"}, |
| 411 | {EFI_MEMORY_XP, "WP"}, |
| 412 | {EFI_MEMORY_NV, "NV"}, |
| 413 | {EFI_MEMORY_MORE_RELIABLE, "REL"}, |
| 414 | {EFI_MEMORY_RO, "RO"}, |
| 415 | {EFI_MEMORY_RUNTIME, "RT"}, |
| 416 | }; |
| 417 | |
| 418 | /** |
| 419 | * print_memory_attributes() - print memory map attributes |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 420 | * |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 421 | * @attributes: Attribute value |
| 422 | * |
| 423 | * Print memory map attributes |
| 424 | */ |
| 425 | static void print_memory_attributes(u64 attributes) |
| 426 | { |
| 427 | int sep, i; |
| 428 | |
| 429 | for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) |
| 430 | if (attributes & efi_mem_attrs[i].bit) { |
| 431 | if (sep) { |
| 432 | putc('|'); |
| 433 | } else { |
| 434 | putc(' '); |
| 435 | sep = 1; |
| 436 | } |
| 437 | puts(efi_mem_attrs[i].text); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) |
| 442 | |
| 443 | /** |
| 444 | * do_efi_show_memmap() - show UEFI memory map |
| 445 | * |
| 446 | * @cmdtp: Command table |
| 447 | * @flag: Command flag |
| 448 | * @argc: Number of arguments |
| 449 | * @argv: Argument array |
| 450 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 451 | * |
| 452 | * Implement efidebug "memmap" sub-command. |
| 453 | * Show UEFI memory map. |
| 454 | */ |
| 455 | static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag, |
| 456 | int argc, char * const argv[]) |
| 457 | { |
| 458 | struct efi_mem_desc *memmap = NULL, *map; |
| 459 | efi_uintn_t map_size = 0; |
| 460 | const char *type; |
| 461 | int i; |
| 462 | efi_status_t ret; |
| 463 | |
| 464 | ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL)); |
| 465 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 466 | map_size += sizeof(struct efi_mem_desc); /* for my own */ |
| 467 | ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA, |
| 468 | map_size, (void *)&memmap)); |
| 469 | if (ret != EFI_SUCCESS) |
| 470 | return CMD_RET_FAILURE; |
| 471 | ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, |
| 472 | NULL, NULL, NULL)); |
| 473 | } |
| 474 | if (ret != EFI_SUCCESS) { |
| 475 | EFI_CALL(BS->free_pool(memmap)); |
| 476 | return CMD_RET_FAILURE; |
| 477 | } |
| 478 | |
| 479 | printf("Type Start%.*s End%.*s Attributes\n", |
| 480 | EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc); |
| 481 | printf("================ %.*s %.*s ==========\n", |
| 482 | EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep); |
| 483 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { |
| 484 | if (map->type < EFI_MAX_MEMORY_TYPE) |
| 485 | type = efi_mem_type_string[map->type]; |
| 486 | else |
| 487 | type = "(unknown)"; |
| 488 | |
| 489 | printf("%-16s %.*llx-%.*llx", type, |
| 490 | EFI_PHYS_ADDR_WIDTH, |
| 491 | map->physical_start, |
| 492 | EFI_PHYS_ADDR_WIDTH, |
| 493 | map->physical_start + map->num_pages * EFI_PAGE_SIZE); |
| 494 | |
| 495 | print_memory_attributes(map->attribute); |
| 496 | putc('\n'); |
| 497 | } |
| 498 | |
| 499 | EFI_CALL(BS->free_pool(memmap)); |
| 500 | |
| 501 | return CMD_RET_SUCCESS; |
| 502 | } |
| 503 | |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 504 | /** |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 505 | * do_efi_show_tables() - show UEFI configuration tables |
| 506 | * |
| 507 | * @cmdtp: Command table |
| 508 | * @flag: Command flag |
| 509 | * @argc: Number of arguments |
| 510 | * @argv: Argument array |
| 511 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 512 | * |
| 513 | * Implement efidebug "tables" sub-command. |
| 514 | * Show UEFI configuration tables. |
| 515 | */ |
| 516 | static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag, |
| 517 | int argc, char * const argv[]) |
| 518 | { |
| 519 | efi_uintn_t i; |
| 520 | const char *guid_str; |
| 521 | |
| 522 | for (i = 0; i < systab.nr_tables; ++i) { |
| 523 | guid_str = get_guid_text(&systab.tables[i].guid); |
| 524 | if (!guid_str) |
| 525 | guid_str = ""; |
| 526 | printf("%pUl %s\n", &systab.tables[i].guid, guid_str); |
| 527 | } |
| 528 | |
| 529 | return CMD_RET_SUCCESS; |
| 530 | } |
| 531 | |
| 532 | /** |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 533 | * do_efi_boot_add() - set UEFI load option |
| 534 | * |
| 535 | * @cmdtp: Command table |
| 536 | * @flag: Command flag |
| 537 | * @argc: Number of arguments |
| 538 | * @argv: Argument array |
| 539 | * Return: CMD_RET_SUCCESS on success, |
| 540 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 541 | * |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 542 | * Implement efidebug "boot add" sub-command. Create or change UEFI load option. |
| 543 | * |
| 544 | * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 545 | */ |
| 546 | static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag, |
| 547 | int argc, char * const argv[]) |
| 548 | { |
| 549 | int id; |
| 550 | char *endp; |
| 551 | char var_name[9]; |
| 552 | u16 var_name16[9], *p; |
| 553 | efi_guid_t guid; |
| 554 | size_t label_len, label_len16; |
| 555 | u16 *label; |
| 556 | struct efi_device_path *device_path = NULL, *file_path = NULL; |
| 557 | struct efi_load_option lo; |
| 558 | void *data = NULL; |
| 559 | efi_uintn_t size; |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 560 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 561 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 562 | |
| 563 | if (argc < 6 || argc > 7) |
| 564 | return CMD_RET_USAGE; |
| 565 | |
| 566 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 567 | if (*endp != '\0' || id > 0xffff) |
Heinrich Schuchardt | 1fa442e | 2019-02-28 20:41:58 +0100 | [diff] [blame] | 568 | return CMD_RET_USAGE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 569 | |
| 570 | sprintf(var_name, "Boot%04X", id); |
| 571 | p = var_name16; |
| 572 | utf8_utf16_strncpy(&p, var_name, 9); |
| 573 | |
| 574 | guid = efi_global_variable_guid; |
| 575 | |
| 576 | /* attributes */ |
| 577 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ |
| 578 | |
| 579 | /* label */ |
| 580 | label_len = strlen(argv[2]); |
| 581 | label_len16 = utf8_utf16_strnlen(argv[2], label_len); |
| 582 | label = malloc((label_len16 + 1) * sizeof(u16)); |
| 583 | if (!label) |
| 584 | return CMD_RET_FAILURE; |
| 585 | lo.label = label; /* label will be changed below */ |
| 586 | utf8_utf16_strncpy(&label, argv[2], label_len); |
| 587 | |
| 588 | /* file path */ |
| 589 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path, |
| 590 | &file_path); |
| 591 | if (ret != EFI_SUCCESS) { |
| 592 | printf("Cannot create device path for \"%s %s\"\n", |
| 593 | argv[3], argv[4]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 594 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 595 | goto out; |
| 596 | } |
| 597 | lo.file_path = file_path; |
| 598 | lo.file_path_length = efi_dp_size(file_path) |
| 599 | + sizeof(struct efi_device_path); /* for END */ |
| 600 | |
| 601 | /* optional data */ |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 602 | if (argc < 6) |
| 603 | lo.optional_data = NULL; |
| 604 | else |
| 605 | lo.optional_data = (const u8 *)argv[6]; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 606 | |
| 607 | size = efi_serialize_load_option(&lo, (u8 **)&data); |
| 608 | if (!size) { |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 609 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 610 | goto out; |
| 611 | } |
| 612 | |
| 613 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 614 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 615 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 616 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 617 | size, data)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 618 | if (ret != EFI_SUCCESS) { |
| 619 | printf("Cannot set %ls\n", var_name16); |
| 620 | r = CMD_RET_FAILURE; |
| 621 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 622 | out: |
| 623 | free(data); |
| 624 | efi_free_pool(device_path); |
| 625 | efi_free_pool(file_path); |
| 626 | free(lo.label); |
| 627 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 628 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * do_efi_boot_rm() - delete UEFI load options |
| 633 | * |
| 634 | * @cmdtp: Command table |
| 635 | * @flag: Command flag |
| 636 | * @argc: Number of arguments |
| 637 | * @argv: Argument array |
| 638 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 639 | * |
| 640 | * Implement efidebug "boot rm" sub-command. |
| 641 | * Delete UEFI load options. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 642 | * |
| 643 | * efidebug boot rm <id> ... |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 644 | */ |
| 645 | static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, |
| 646 | int argc, char * const argv[]) |
| 647 | { |
| 648 | efi_guid_t guid; |
| 649 | int id, i; |
| 650 | char *endp; |
| 651 | char var_name[9]; |
| 652 | u16 var_name16[9]; |
| 653 | efi_status_t ret; |
| 654 | |
| 655 | if (argc == 1) |
| 656 | return CMD_RET_USAGE; |
| 657 | |
| 658 | guid = efi_global_variable_guid; |
| 659 | for (i = 1; i < argc; i++, argv++) { |
| 660 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 661 | if (*endp != '\0' || id > 0xffff) |
| 662 | return CMD_RET_FAILURE; |
| 663 | |
| 664 | sprintf(var_name, "Boot%04X", id); |
| 665 | utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9); |
| 666 | |
| 667 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL)); |
| 668 | if (ret) { |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 669 | printf("Cannot remove Boot%04X", id); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 670 | return CMD_RET_FAILURE; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | return CMD_RET_SUCCESS; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * show_efi_boot_opt_data() - dump UEFI load option |
| 679 | * |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 680 | * @id: load option number |
| 681 | * @data: value of UEFI load option variable |
| 682 | * @size: size of the boot option |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 683 | * |
| 684 | * Decode the value of UEFI load option variable and print information. |
| 685 | */ |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 686 | static void show_efi_boot_opt_data(int id, void *data, size_t size) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 687 | { |
| 688 | struct efi_load_option lo; |
| 689 | char *label, *p; |
| 690 | size_t label_len16, label_len; |
| 691 | u16 *dp_str; |
| 692 | |
| 693 | efi_deserialize_load_option(&lo, data); |
| 694 | |
| 695 | label_len16 = u16_strlen(lo.label); |
| 696 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 697 | label = malloc(label_len + 1); |
| 698 | if (!label) |
| 699 | return; |
| 700 | p = label; |
| 701 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
| 702 | |
| 703 | printf("Boot%04X:\n", id); |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 704 | printf(" attributes: %c%c%c (0x%08x)\n", |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 705 | /* ACTIVE */ |
| 706 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', |
| 707 | /* FORCE RECONNECT */ |
| 708 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', |
| 709 | /* HIDDEN */ |
| 710 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', |
| 711 | lo.attributes); |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 712 | printf(" label: %s\n", label); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 713 | |
| 714 | dp_str = efi_dp_str(lo.file_path); |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 715 | printf(" file_path: %ls\n", dp_str); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 716 | efi_free_pool(dp_str); |
| 717 | |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 718 | printf(" data:\n"); |
| 719 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 720 | lo.optional_data, size + (u8 *)data - |
| 721 | (u8 *)lo.optional_data, true); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 722 | free(label); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * show_efi_boot_opt() - dump UEFI load option |
| 727 | * |
| 728 | * @id: Load option number |
| 729 | * |
| 730 | * Dump information defined by UEFI load option. |
| 731 | */ |
| 732 | static void show_efi_boot_opt(int id) |
| 733 | { |
| 734 | char var_name[9]; |
| 735 | u16 var_name16[9], *p; |
| 736 | efi_guid_t guid; |
| 737 | void *data = NULL; |
| 738 | efi_uintn_t size; |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 739 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 740 | |
| 741 | sprintf(var_name, "Boot%04X", id); |
| 742 | p = var_name16; |
| 743 | utf8_utf16_strncpy(&p, var_name, 9); |
| 744 | guid = efi_global_variable_guid; |
| 745 | |
| 746 | size = 0; |
| 747 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL)); |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 748 | if (ret == EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 749 | data = malloc(size); |
| 750 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 751 | data)); |
| 752 | } |
| 753 | if (ret == EFI_SUCCESS) |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 754 | show_efi_boot_opt_data(id, data, size); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 755 | else if (ret == EFI_NOT_FOUND) |
| 756 | printf("Boot%04X: not found\n", id); |
| 757 | |
| 758 | free(data); |
| 759 | } |
| 760 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 761 | static int u16_tohex(u16 c) |
| 762 | { |
| 763 | if (c >= '0' && c <= '9') |
| 764 | return c - '0'; |
| 765 | if (c >= 'A' && c <= 'F') |
| 766 | return c - 'A' + 10; |
| 767 | |
| 768 | /* not hexadecimal */ |
| 769 | return -1; |
| 770 | } |
| 771 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 772 | /** |
| 773 | * show_efi_boot_dump() - dump all UEFI load options |
| 774 | * |
| 775 | * @cmdtp: Command table |
| 776 | * @flag: Command flag |
| 777 | * @argc: Number of arguments |
| 778 | * @argv: Argument array |
| 779 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 780 | * |
| 781 | * Implement efidebug "boot dump" sub-command. |
| 782 | * Dump information of all UEFI load options defined. |
Heinrich Schuchardt | a6ccba0 | 2019-07-25 20:52:23 +0200 | [diff] [blame] | 783 | * |
| 784 | * efidebug boot dump |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 785 | */ |
| 786 | static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag, |
| 787 | int argc, char * const argv[]) |
| 788 | { |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 789 | u16 *var_name16, *p; |
| 790 | efi_uintn_t buf_size, size; |
| 791 | efi_guid_t guid; |
| 792 | int id, i, digit; |
| 793 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 794 | |
| 795 | if (argc > 1) |
| 796 | return CMD_RET_USAGE; |
| 797 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 798 | buf_size = 128; |
| 799 | var_name16 = malloc(buf_size); |
| 800 | if (!var_name16) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 801 | return CMD_RET_FAILURE; |
| 802 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 803 | var_name16[0] = 0; |
| 804 | for (;;) { |
| 805 | size = buf_size; |
| 806 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, |
| 807 | &guid)); |
| 808 | if (ret == EFI_NOT_FOUND) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 809 | break; |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 810 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 811 | buf_size = size; |
| 812 | p = realloc(var_name16, buf_size); |
| 813 | if (!p) { |
| 814 | free(var_name16); |
| 815 | return CMD_RET_FAILURE; |
| 816 | } |
| 817 | var_name16 = p; |
| 818 | ret = EFI_CALL(efi_get_next_variable_name(&size, |
| 819 | var_name16, |
| 820 | &guid)); |
| 821 | } |
| 822 | if (ret != EFI_SUCCESS) { |
| 823 | free(var_name16); |
| 824 | return CMD_RET_FAILURE; |
| 825 | } |
| 826 | |
| 827 | if (memcmp(var_name16, L"Boot", 8)) |
| 828 | continue; |
| 829 | |
| 830 | for (id = 0, i = 0; i < 4; i++) { |
| 831 | digit = u16_tohex(var_name16[4 + i]); |
| 832 | if (digit < 0) |
| 833 | break; |
| 834 | id = (id << 4) + digit; |
| 835 | } |
| 836 | if (i == 4 && !var_name16[8]) |
| 837 | show_efi_boot_opt(id); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 838 | } |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 839 | |
| 840 | free(var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 841 | |
| 842 | return CMD_RET_SUCCESS; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * show_efi_boot_order() - show order of UEFI load options |
| 847 | * |
| 848 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 849 | * |
| 850 | * Show order of UEFI load options defined by BootOrder variable. |
| 851 | */ |
| 852 | static int show_efi_boot_order(void) |
| 853 | { |
| 854 | efi_guid_t guid; |
| 855 | u16 *bootorder = NULL; |
| 856 | efi_uintn_t size; |
| 857 | int num, i; |
| 858 | char var_name[9]; |
| 859 | u16 var_name16[9], *p16; |
| 860 | void *data; |
| 861 | struct efi_load_option lo; |
| 862 | char *label, *p; |
| 863 | size_t label_len16, label_len; |
| 864 | efi_status_t ret; |
| 865 | |
| 866 | guid = efi_global_variable_guid; |
| 867 | size = 0; |
| 868 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size, |
| 869 | NULL)); |
| 870 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 871 | bootorder = malloc(size); |
| 872 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, |
| 873 | &size, bootorder)); |
| 874 | } |
| 875 | if (ret == EFI_NOT_FOUND) { |
| 876 | printf("BootOrder not defined\n"); |
| 877 | ret = CMD_RET_SUCCESS; |
| 878 | goto out; |
| 879 | } else if (ret != EFI_SUCCESS) { |
| 880 | ret = CMD_RET_FAILURE; |
| 881 | goto out; |
| 882 | } |
| 883 | |
| 884 | num = size / sizeof(u16); |
| 885 | for (i = 0; i < num; i++) { |
| 886 | sprintf(var_name, "Boot%04X", bootorder[i]); |
| 887 | p16 = var_name16; |
| 888 | utf8_utf16_strncpy(&p16, var_name, 9); |
| 889 | |
| 890 | size = 0; |
| 891 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 892 | NULL)); |
| 893 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 894 | printf("%2d: Boot%04X: (not defined)\n", |
| 895 | i + 1, bootorder[i]); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | data = malloc(size); |
| 900 | if (!data) { |
| 901 | ret = CMD_RET_FAILURE; |
| 902 | goto out; |
| 903 | } |
| 904 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 905 | data)); |
| 906 | if (ret != EFI_SUCCESS) { |
| 907 | free(data); |
| 908 | ret = CMD_RET_FAILURE; |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | efi_deserialize_load_option(&lo, data); |
| 913 | |
| 914 | label_len16 = u16_strlen(lo.label); |
| 915 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 916 | label = malloc(label_len + 1); |
| 917 | if (!label) { |
| 918 | free(data); |
| 919 | ret = CMD_RET_FAILURE; |
| 920 | goto out; |
| 921 | } |
| 922 | p = label; |
| 923 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
| 924 | printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label); |
| 925 | free(label); |
| 926 | |
| 927 | free(data); |
| 928 | } |
| 929 | out: |
| 930 | free(bootorder); |
| 931 | |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * do_efi_boot_next() - manage UEFI BootNext variable |
| 937 | * |
| 938 | * @cmdtp: Command table |
| 939 | * @flag: Command flag |
| 940 | * @argc: Number of arguments |
| 941 | * @argv: Argument array |
| 942 | * Return: CMD_RET_SUCCESS on success, |
| 943 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 944 | * |
| 945 | * Implement efidebug "boot next" sub-command. |
| 946 | * Set BootNext variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 947 | * |
| 948 | * efidebug boot next <id> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 949 | */ |
| 950 | static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag, |
| 951 | int argc, char * const argv[]) |
| 952 | { |
| 953 | u16 bootnext; |
| 954 | efi_uintn_t size; |
| 955 | char *endp; |
| 956 | efi_guid_t guid; |
| 957 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 958 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 959 | |
| 960 | if (argc != 2) |
| 961 | return CMD_RET_USAGE; |
| 962 | |
| 963 | bootnext = (u16)simple_strtoul(argv[1], &endp, 16); |
| 964 | if (*endp != '\0' || bootnext > 0xffff) { |
| 965 | printf("invalid value: %s\n", argv[1]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 966 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 967 | goto out; |
| 968 | } |
| 969 | |
| 970 | guid = efi_global_variable_guid; |
| 971 | size = sizeof(u16); |
| 972 | ret = EFI_CALL(RT->set_variable(L"BootNext", &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 973 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 974 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 975 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 976 | size, &bootnext)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 977 | if (ret != EFI_SUCCESS) { |
| 978 | printf("Cannot set BootNext\n"); |
| 979 | r = CMD_RET_FAILURE; |
| 980 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 981 | out: |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 982 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
| 986 | * do_efi_boot_order() - manage UEFI BootOrder variable |
| 987 | * |
| 988 | * @cmdtp: Command table |
| 989 | * @flag: Command flag |
| 990 | * @argc: Number of arguments |
| 991 | * @argv: Argument array |
| 992 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 993 | * |
| 994 | * Implement efidebug "boot order" sub-command. |
| 995 | * Show order of UEFI load options, or change it in BootOrder variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 996 | * |
| 997 | * efidebug boot order [<id> ...] |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 998 | */ |
| 999 | static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag, |
| 1000 | int argc, char * const argv[]) |
| 1001 | { |
| 1002 | u16 *bootorder = NULL; |
| 1003 | efi_uintn_t size; |
| 1004 | int id, i; |
| 1005 | char *endp; |
| 1006 | efi_guid_t guid; |
| 1007 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1008 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1009 | |
| 1010 | if (argc == 1) |
| 1011 | return show_efi_boot_order(); |
| 1012 | |
| 1013 | argc--; |
| 1014 | argv++; |
| 1015 | |
| 1016 | size = argc * sizeof(u16); |
| 1017 | bootorder = malloc(size); |
| 1018 | if (!bootorder) |
| 1019 | return CMD_RET_FAILURE; |
| 1020 | |
| 1021 | for (i = 0; i < argc; i++) { |
| 1022 | id = (int)simple_strtoul(argv[i], &endp, 16); |
| 1023 | if (*endp != '\0' || id > 0xffff) { |
| 1024 | printf("invalid value: %s\n", argv[i]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1025 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1026 | goto out; |
| 1027 | } |
| 1028 | |
| 1029 | bootorder[i] = (u16)id; |
| 1030 | } |
| 1031 | |
| 1032 | guid = efi_global_variable_guid; |
| 1033 | ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 1034 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1035 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1036 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1037 | size, bootorder)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1038 | if (ret != EFI_SUCCESS) { |
| 1039 | printf("Cannot set BootOrder\n"); |
| 1040 | r = CMD_RET_FAILURE; |
| 1041 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1042 | out: |
| 1043 | free(bootorder); |
| 1044 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1045 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static cmd_tbl_t cmd_efidebug_boot_sub[] = { |
| 1049 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
| 1050 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), |
| 1051 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), |
| 1052 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), |
| 1053 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, |
| 1054 | "", ""), |
| 1055 | }; |
| 1056 | |
| 1057 | /** |
| 1058 | * do_efi_boot_opt() - manage UEFI load options |
| 1059 | * |
| 1060 | * @cmdtp: Command table |
| 1061 | * @flag: Command flag |
| 1062 | * @argc: Number of arguments |
| 1063 | * @argv: Argument array |
| 1064 | * Return: CMD_RET_SUCCESS on success, |
| 1065 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1066 | * |
| 1067 | * Implement efidebug "boot" sub-command. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1068 | */ |
| 1069 | static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag, |
| 1070 | int argc, char * const argv[]) |
| 1071 | { |
| 1072 | cmd_tbl_t *cp; |
| 1073 | |
| 1074 | if (argc < 2) |
| 1075 | return CMD_RET_USAGE; |
| 1076 | |
| 1077 | argc--; argv++; |
| 1078 | |
| 1079 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, |
| 1080 | ARRAY_SIZE(cmd_efidebug_boot_sub)); |
| 1081 | if (!cp) |
| 1082 | return CMD_RET_USAGE; |
| 1083 | |
| 1084 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1085 | } |
| 1086 | |
| 1087 | static cmd_tbl_t cmd_efidebug_sub[] = { |
| 1088 | U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""), |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 1089 | U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices, |
| 1090 | "", ""), |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1091 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
| 1092 | "", ""), |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1093 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
| 1094 | "", ""), |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1095 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
| 1096 | "", ""), |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1097 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
| 1098 | "", ""), |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1099 | U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, |
| 1100 | "", ""), |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1101 | }; |
| 1102 | |
| 1103 | /** |
| 1104 | * do_efidebug() - display and configure UEFI environment |
| 1105 | * |
| 1106 | * @cmdtp: Command table |
| 1107 | * @flag: Command flag |
| 1108 | * @argc: Number of arguments |
| 1109 | * @argv: Argument array |
| 1110 | * Return: CMD_RET_SUCCESS on success, |
| 1111 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1112 | * |
| 1113 | * Implement efidebug command which allows us to display and |
| 1114 | * configure UEFI environment. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1115 | */ |
| 1116 | static int do_efidebug(cmd_tbl_t *cmdtp, int flag, |
| 1117 | int argc, char * const argv[]) |
| 1118 | { |
| 1119 | cmd_tbl_t *cp; |
| 1120 | efi_status_t r; |
| 1121 | |
| 1122 | if (argc < 2) |
| 1123 | return CMD_RET_USAGE; |
| 1124 | |
| 1125 | argc--; argv++; |
| 1126 | |
| 1127 | /* Initialize UEFI drivers */ |
| 1128 | r = efi_init_obj_list(); |
| 1129 | if (r != EFI_SUCCESS) { |
| 1130 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1131 | r & ~EFI_ERROR_MASK); |
| 1132 | return CMD_RET_FAILURE; |
| 1133 | } |
| 1134 | |
| 1135 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, |
| 1136 | ARRAY_SIZE(cmd_efidebug_sub)); |
| 1137 | if (!cp) |
| 1138 | return CMD_RET_USAGE; |
| 1139 | |
| 1140 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1141 | } |
| 1142 | |
| 1143 | #ifdef CONFIG_SYS_LONGHELP |
| 1144 | static char efidebug_help_text[] = |
| 1145 | " - UEFI Shell-like interface to configure UEFI environment\n" |
| 1146 | "\n" |
| 1147 | "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n" |
| 1148 | " - set UEFI BootXXXX variable\n" |
| 1149 | " <load options> will be passed to UEFI application\n" |
| 1150 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" |
| 1151 | " - delete UEFI BootXXXX variables\n" |
| 1152 | "efidebug boot dump\n" |
| 1153 | " - dump all UEFI BootXXXX variables\n" |
| 1154 | "efidebug boot next <bootid>\n" |
| 1155 | " - set UEFI BootNext variable\n" |
| 1156 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" |
| 1157 | " - set/show UEFI boot order\n" |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 1158 | "\n" |
| 1159 | "efidebug devices\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1160 | " - show UEFI devices\n" |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1161 | "efidebug drivers\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1162 | " - show UEFI drivers\n" |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1163 | "efidebug dh\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1164 | " - show UEFI handles\n" |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1165 | "efidebug images\n" |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1166 | " - show loaded images\n" |
| 1167 | "efidebug memmap\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1168 | " - show UEFI memory map\n" |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1169 | "efidebug tables\n" |
| 1170 | " - show UEFI configuration tables\n"; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1171 | #endif |
| 1172 | |
| 1173 | U_BOOT_CMD( |
| 1174 | efidebug, 10, 0, do_efidebug, |
| 1175 | "Configure UEFI environment", |
| 1176 | efidebug_help_text |
| 1177 | ); |