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