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> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 15 | #include <malloc.h> |
Heinrich Schuchardt | a415d61 | 2020-03-14 08:44:07 +0100 | [diff] [blame] | 16 | #include <mapmem.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 17 | #include <search.h> |
| 18 | #include <linux/ctype.h> |
| 19 | |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 20 | #define BS systab.boottime |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 21 | |
| 22 | /** |
AKASHI Takahiro | 355cdb5 | 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 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 64 | static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag, |
| 65 | int argc, char *const argv[]) |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 66 | { |
| 67 | efi_handle_t *handles; |
| 68 | efi_uintn_t num, i; |
| 69 | u16 *dev_path_text; |
| 70 | efi_status_t ret; |
| 71 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 72 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 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 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 89 | efi_free_pool(handles); |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 90 | |
| 91 | return CMD_RET_SUCCESS; |
| 92 | } |
| 93 | |
| 94 | /** |
AKASHI Takahiro | 66eaf56 | 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 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 143 | static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag, |
| 144 | int argc, char *const argv[]) |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 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 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 151 | ret = EFI_CALL(efi_locate_handle_buffer( |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 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); |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 173 | efi_free_pool(driver_name); |
| 174 | efi_free_pool(image_path_text); |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 178 | efi_free_pool(handles); |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 179 | |
| 180 | return CMD_RET_SUCCESS; |
| 181 | } |
| 182 | |
AKASHI Takahiro | a801462 | 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 | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 189 | EFI_DEVICE_PATH_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 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 | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 221 | EFI_BLOCK_IO_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 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 | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 229 | EFI_LOADED_IMAGE_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 230 | }, |
| 231 | { |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 232 | "Graphics Output", |
| 233 | EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 234 | }, |
Heinrich Schuchardt | 391bc8a | 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 | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 248 | "Load File2", |
| 249 | EFI_LOAD_FILE2_PROTOCOL_GUID, |
| 250 | }, |
| 251 | { |
Heinrich Schuchardt | 391bc8a | 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 | 986e064 | 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 | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 272 | { |
| 273 | "Runtime properties", |
| 274 | EFI_RT_PROPERTIES_TABLE_GUID, |
| 275 | }, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /** |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 279 | * get_guid_text - get string of GUID |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 280 | * |
Heinrich Schuchardt | 173cd9e | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 285 | */ |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 286 | static const char *get_guid_text(const void *guid) |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 287 | { |
| 288 | int i; |
| 289 | |
Heinrich Schuchardt | 173cd9e | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 295 | if (!guidcmp(&guid_list[i].guid, guid)) |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 296 | return guid_list[i].text; |
| 297 | } |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 298 | |
Heinrich Schuchardt | 173cd9e | 2020-01-07 06:02:33 +0100 | [diff] [blame] | 299 | return NULL; |
AKASHI Takahiro | a801462 | 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 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 315 | static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag, |
| 316 | int argc, char *const argv[]) |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 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 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 324 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 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 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 358 | efi_free_pool(handles); |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 359 | |
| 360 | return CMD_RET_SUCCESS; |
| 361 | } |
| 362 | |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 363 | /** |
AKASHI Takahiro | fa53673 | 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 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 375 | static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag, |
| 376 | int argc, char *const argv[]) |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 377 | { |
| 378 | efi_print_image_infos(NULL); |
| 379 | |
| 380 | return CMD_RET_SUCCESS; |
| 381 | } |
| 382 | |
AKASHI Takahiro | 00358bb | 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", |
Heinrich Schuchardt | dd9056c | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 398 | [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT", |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | static const struct efi_mem_attrs { |
| 402 | const u64 bit; |
| 403 | const char *text; |
| 404 | } efi_mem_attrs[] = { |
| 405 | {EFI_MEMORY_UC, "UC"}, |
| 406 | {EFI_MEMORY_UC, "UC"}, |
| 407 | {EFI_MEMORY_WC, "WC"}, |
| 408 | {EFI_MEMORY_WT, "WT"}, |
| 409 | {EFI_MEMORY_WB, "WB"}, |
| 410 | {EFI_MEMORY_UCE, "UCE"}, |
| 411 | {EFI_MEMORY_WP, "WP"}, |
| 412 | {EFI_MEMORY_RP, "RP"}, |
| 413 | {EFI_MEMORY_XP, "WP"}, |
| 414 | {EFI_MEMORY_NV, "NV"}, |
| 415 | {EFI_MEMORY_MORE_RELIABLE, "REL"}, |
| 416 | {EFI_MEMORY_RO, "RO"}, |
| 417 | {EFI_MEMORY_RUNTIME, "RT"}, |
| 418 | }; |
| 419 | |
| 420 | /** |
| 421 | * print_memory_attributes() - print memory map attributes |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 422 | * |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 423 | * @attributes: Attribute value |
| 424 | * |
| 425 | * Print memory map attributes |
| 426 | */ |
| 427 | static void print_memory_attributes(u64 attributes) |
| 428 | { |
| 429 | int sep, i; |
| 430 | |
| 431 | for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) |
| 432 | if (attributes & efi_mem_attrs[i].bit) { |
| 433 | if (sep) { |
| 434 | putc('|'); |
| 435 | } else { |
| 436 | putc(' '); |
| 437 | sep = 1; |
| 438 | } |
| 439 | puts(efi_mem_attrs[i].text); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) |
| 444 | |
| 445 | /** |
| 446 | * do_efi_show_memmap() - show UEFI memory map |
| 447 | * |
| 448 | * @cmdtp: Command table |
| 449 | * @flag: Command flag |
| 450 | * @argc: Number of arguments |
| 451 | * @argv: Argument array |
| 452 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 453 | * |
| 454 | * Implement efidebug "memmap" sub-command. |
| 455 | * Show UEFI memory map. |
| 456 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 457 | static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag, |
| 458 | int argc, char *const argv[]) |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 459 | { |
| 460 | struct efi_mem_desc *memmap = NULL, *map; |
| 461 | efi_uintn_t map_size = 0; |
| 462 | const char *type; |
| 463 | int i; |
| 464 | efi_status_t ret; |
| 465 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 466 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 467 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 468 | map_size += sizeof(struct efi_mem_desc); /* for my own */ |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 469 | ret = efi_allocate_pool(EFI_LOADER_DATA, map_size, |
| 470 | (void *)&memmap); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 471 | if (ret != EFI_SUCCESS) |
| 472 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 473 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 474 | } |
| 475 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 476 | efi_free_pool(memmap); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 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); |
AKASHI Takahiro | e2a5b86 | 2020-05-08 14:50:18 +0900 | [diff] [blame] | 484 | /* |
| 485 | * Coverity check: dereferencing null pointer "map." |
| 486 | * This is a false positive as memmap will always be |
| 487 | * populated by allocate_pool() above. |
| 488 | */ |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 489 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { |
Heinrich Schuchardt | dd9056c | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 490 | if (map->type < ARRAY_SIZE(efi_mem_type_string)) |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 491 | type = efi_mem_type_string[map->type]; |
| 492 | else |
| 493 | type = "(unknown)"; |
| 494 | |
| 495 | printf("%-16s %.*llx-%.*llx", type, |
| 496 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 6c0ef35 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 497 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 498 | map->physical_start), |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 499 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 6c0ef35 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 500 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 501 | (map->physical_start + |
| 502 | map->num_pages * EFI_PAGE_SIZE))); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 503 | |
| 504 | print_memory_attributes(map->attribute); |
| 505 | putc('\n'); |
| 506 | } |
| 507 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 508 | efi_free_pool(memmap); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 509 | |
| 510 | return CMD_RET_SUCCESS; |
| 511 | } |
| 512 | |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 513 | /** |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 514 | * do_efi_show_tables() - show UEFI configuration tables |
| 515 | * |
| 516 | * @cmdtp: Command table |
| 517 | * @flag: Command flag |
| 518 | * @argc: Number of arguments |
| 519 | * @argv: Argument array |
| 520 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 521 | * |
| 522 | * Implement efidebug "tables" sub-command. |
| 523 | * Show UEFI configuration tables. |
| 524 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 525 | static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag, |
| 526 | int argc, char *const argv[]) |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 527 | { |
| 528 | efi_uintn_t i; |
| 529 | const char *guid_str; |
| 530 | |
| 531 | for (i = 0; i < systab.nr_tables; ++i) { |
| 532 | guid_str = get_guid_text(&systab.tables[i].guid); |
| 533 | if (!guid_str) |
| 534 | guid_str = ""; |
| 535 | printf("%pUl %s\n", &systab.tables[i].guid, guid_str); |
| 536 | } |
| 537 | |
| 538 | return CMD_RET_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | /** |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 542 | * do_efi_boot_add() - set UEFI load option |
| 543 | * |
| 544 | * @cmdtp: Command table |
| 545 | * @flag: Command flag |
| 546 | * @argc: Number of arguments |
| 547 | * @argv: Argument array |
| 548 | * Return: CMD_RET_SUCCESS on success, |
| 549 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 550 | * |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 551 | * Implement efidebug "boot add" sub-command. Create or change UEFI load option. |
| 552 | * |
| 553 | * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 554 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 555 | static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag, |
| 556 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 557 | { |
| 558 | int id; |
| 559 | char *endp; |
| 560 | char var_name[9]; |
| 561 | u16 var_name16[9], *p; |
| 562 | efi_guid_t guid; |
| 563 | size_t label_len, label_len16; |
| 564 | u16 *label; |
| 565 | struct efi_device_path *device_path = NULL, *file_path = NULL; |
| 566 | struct efi_load_option lo; |
| 567 | void *data = NULL; |
| 568 | efi_uintn_t size; |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 569 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 570 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 571 | |
| 572 | if (argc < 6 || argc > 7) |
| 573 | return CMD_RET_USAGE; |
| 574 | |
| 575 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 576 | if (*endp != '\0' || id > 0xffff) |
Heinrich Schuchardt | 1fa442e | 2019-02-28 20:41:58 +0100 | [diff] [blame] | 577 | return CMD_RET_USAGE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 578 | |
| 579 | sprintf(var_name, "Boot%04X", id); |
| 580 | p = var_name16; |
| 581 | utf8_utf16_strncpy(&p, var_name, 9); |
| 582 | |
| 583 | guid = efi_global_variable_guid; |
| 584 | |
| 585 | /* attributes */ |
| 586 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ |
| 587 | |
| 588 | /* label */ |
| 589 | label_len = strlen(argv[2]); |
| 590 | label_len16 = utf8_utf16_strnlen(argv[2], label_len); |
| 591 | label = malloc((label_len16 + 1) * sizeof(u16)); |
| 592 | if (!label) |
| 593 | return CMD_RET_FAILURE; |
| 594 | lo.label = label; /* label will be changed below */ |
| 595 | utf8_utf16_strncpy(&label, argv[2], label_len); |
| 596 | |
| 597 | /* file path */ |
| 598 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path, |
| 599 | &file_path); |
| 600 | if (ret != EFI_SUCCESS) { |
| 601 | printf("Cannot create device path for \"%s %s\"\n", |
| 602 | argv[3], argv[4]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 603 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 604 | goto out; |
| 605 | } |
| 606 | lo.file_path = file_path; |
| 607 | lo.file_path_length = efi_dp_size(file_path) |
| 608 | + sizeof(struct efi_device_path); /* for END */ |
| 609 | |
| 610 | /* optional data */ |
AKASHI Takahiro | d67591d | 2020-05-08 14:50:47 +0900 | [diff] [blame] | 611 | if (argc == 6) |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 612 | lo.optional_data = NULL; |
| 613 | else |
| 614 | lo.optional_data = (const u8 *)argv[6]; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 615 | |
| 616 | size = efi_serialize_load_option(&lo, (u8 **)&data); |
| 617 | if (!size) { |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 618 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 619 | goto out; |
| 620 | } |
| 621 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 622 | ret = EFI_CALL(efi_set_variable(var_name16, &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 623 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 624 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 625 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 626 | size, data)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 627 | if (ret != EFI_SUCCESS) { |
| 628 | printf("Cannot set %ls\n", var_name16); |
| 629 | r = CMD_RET_FAILURE; |
| 630 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 631 | out: |
| 632 | free(data); |
| 633 | efi_free_pool(device_path); |
| 634 | efi_free_pool(file_path); |
| 635 | free(lo.label); |
| 636 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 637 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * do_efi_boot_rm() - delete UEFI load options |
| 642 | * |
| 643 | * @cmdtp: Command table |
| 644 | * @flag: Command flag |
| 645 | * @argc: Number of arguments |
| 646 | * @argv: Argument array |
| 647 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 648 | * |
| 649 | * Implement efidebug "boot rm" sub-command. |
| 650 | * Delete UEFI load options. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 651 | * |
| 652 | * efidebug boot rm <id> ... |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 653 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 654 | static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag, |
| 655 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 656 | { |
| 657 | efi_guid_t guid; |
| 658 | int id, i; |
| 659 | char *endp; |
| 660 | char var_name[9]; |
AKASHI Takahiro | e8bced6 | 2020-02-28 09:05:04 +0900 | [diff] [blame] | 661 | u16 var_name16[9], *p; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 662 | efi_status_t ret; |
| 663 | |
| 664 | if (argc == 1) |
| 665 | return CMD_RET_USAGE; |
| 666 | |
| 667 | guid = efi_global_variable_guid; |
| 668 | for (i = 1; i < argc; i++, argv++) { |
| 669 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 670 | if (*endp != '\0' || id > 0xffff) |
| 671 | return CMD_RET_FAILURE; |
| 672 | |
| 673 | sprintf(var_name, "Boot%04X", id); |
AKASHI Takahiro | e8bced6 | 2020-02-28 09:05:04 +0900 | [diff] [blame] | 674 | p = var_name16; |
| 675 | utf8_utf16_strncpy(&p, var_name, 9); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 676 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 677 | ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL)); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 678 | if (ret) { |
Heinrich Schuchardt | 30efb5d | 2020-03-02 20:13:10 +0100 | [diff] [blame] | 679 | printf("Cannot remove %ls\n", var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 680 | return CMD_RET_FAILURE; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return CMD_RET_SUCCESS; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * show_efi_boot_opt_data() - dump UEFI load option |
| 689 | * |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 690 | * @varname16: variable name |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 691 | * @data: value of UEFI load option variable |
| 692 | * @size: size of the boot option |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 693 | * |
| 694 | * Decode the value of UEFI load option variable and print information. |
| 695 | */ |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 696 | static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t size) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 697 | { |
| 698 | struct efi_load_option lo; |
| 699 | char *label, *p; |
| 700 | size_t label_len16, label_len; |
| 701 | u16 *dp_str; |
| 702 | |
| 703 | efi_deserialize_load_option(&lo, data); |
| 704 | |
| 705 | label_len16 = u16_strlen(lo.label); |
| 706 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 707 | label = malloc(label_len + 1); |
| 708 | if (!label) |
| 709 | return; |
| 710 | p = label; |
| 711 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
| 712 | |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 713 | printf("%ls:\nattributes: %c%c%c (0x%08x)\n", |
| 714 | varname16, |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 715 | /* ACTIVE */ |
| 716 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', |
| 717 | /* FORCE RECONNECT */ |
| 718 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', |
| 719 | /* HIDDEN */ |
| 720 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', |
| 721 | lo.attributes); |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 722 | printf(" label: %s\n", label); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 723 | |
| 724 | dp_str = efi_dp_str(lo.file_path); |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 725 | printf(" file_path: %ls\n", dp_str); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 726 | efi_free_pool(dp_str); |
| 727 | |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 728 | printf(" data:\n"); |
| 729 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
| 730 | lo.optional_data, size + (u8 *)data - |
| 731 | (u8 *)lo.optional_data, true); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 732 | free(label); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * show_efi_boot_opt() - dump UEFI load option |
| 737 | * |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 738 | * @varname16: variable name |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 739 | * |
| 740 | * Dump information defined by UEFI load option. |
| 741 | */ |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 742 | static void show_efi_boot_opt(u16 *varname16) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 743 | { |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 744 | void *data; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 745 | efi_uintn_t size; |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 746 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 747 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 748 | size = 0; |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 749 | ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid, |
| 750 | NULL, &size, NULL)); |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 751 | if (ret == EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 752 | data = malloc(size); |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 753 | if (!data) { |
| 754 | printf("ERROR: Out of memory\n"); |
| 755 | return; |
| 756 | } |
| 757 | ret = EFI_CALL(efi_get_variable(varname16, |
| 758 | &efi_global_variable_guid, |
| 759 | NULL, &size, data)); |
| 760 | if (ret == EFI_SUCCESS) |
| 761 | show_efi_boot_opt_data(varname16, data, size); |
| 762 | free(data); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 763 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 764 | } |
| 765 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 766 | static int u16_tohex(u16 c) |
| 767 | { |
| 768 | if (c >= '0' && c <= '9') |
| 769 | return c - '0'; |
| 770 | if (c >= 'A' && c <= 'F') |
| 771 | return c - 'A' + 10; |
| 772 | |
| 773 | /* not hexadecimal */ |
| 774 | return -1; |
| 775 | } |
| 776 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 777 | /** |
| 778 | * show_efi_boot_dump() - dump all UEFI load options |
| 779 | * |
| 780 | * @cmdtp: Command table |
| 781 | * @flag: Command flag |
| 782 | * @argc: Number of arguments |
| 783 | * @argv: Argument array |
| 784 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 785 | * |
| 786 | * Implement efidebug "boot dump" sub-command. |
| 787 | * Dump information of all UEFI load options defined. |
Heinrich Schuchardt | a6ccba0 | 2019-07-25 20:52:23 +0200 | [diff] [blame] | 788 | * |
| 789 | * efidebug boot dump |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 790 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 791 | static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag, |
| 792 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 793 | { |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 794 | u16 *var_name16, *p; |
| 795 | efi_uintn_t buf_size, size; |
| 796 | efi_guid_t guid; |
| 797 | int id, i, digit; |
| 798 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 799 | |
| 800 | if (argc > 1) |
| 801 | return CMD_RET_USAGE; |
| 802 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 803 | buf_size = 128; |
| 804 | var_name16 = malloc(buf_size); |
| 805 | if (!var_name16) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 806 | return CMD_RET_FAILURE; |
| 807 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 808 | var_name16[0] = 0; |
| 809 | for (;;) { |
| 810 | size = buf_size; |
| 811 | ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16, |
| 812 | &guid)); |
| 813 | if (ret == EFI_NOT_FOUND) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 814 | break; |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 815 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 816 | buf_size = size; |
| 817 | p = realloc(var_name16, buf_size); |
| 818 | if (!p) { |
| 819 | free(var_name16); |
| 820 | return CMD_RET_FAILURE; |
| 821 | } |
| 822 | var_name16 = p; |
| 823 | ret = EFI_CALL(efi_get_next_variable_name(&size, |
| 824 | var_name16, |
| 825 | &guid)); |
| 826 | } |
| 827 | if (ret != EFI_SUCCESS) { |
| 828 | free(var_name16); |
| 829 | return CMD_RET_FAILURE; |
| 830 | } |
| 831 | |
| 832 | if (memcmp(var_name16, L"Boot", 8)) |
| 833 | continue; |
| 834 | |
| 835 | for (id = 0, i = 0; i < 4; i++) { |
| 836 | digit = u16_tohex(var_name16[4 + i]); |
| 837 | if (digit < 0) |
| 838 | break; |
| 839 | id = (id << 4) + digit; |
| 840 | } |
| 841 | if (i == 4 && !var_name16[8]) |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 842 | show_efi_boot_opt(var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 843 | } |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 844 | |
| 845 | free(var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 846 | |
| 847 | return CMD_RET_SUCCESS; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * show_efi_boot_order() - show order of UEFI load options |
| 852 | * |
| 853 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 854 | * |
| 855 | * Show order of UEFI load options defined by BootOrder variable. |
| 856 | */ |
| 857 | static int show_efi_boot_order(void) |
| 858 | { |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 859 | u16 *bootorder; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 860 | efi_uintn_t size; |
| 861 | int num, i; |
| 862 | char var_name[9]; |
| 863 | u16 var_name16[9], *p16; |
| 864 | void *data; |
| 865 | struct efi_load_option lo; |
| 866 | char *label, *p; |
| 867 | size_t label_len16, label_len; |
| 868 | efi_status_t ret; |
| 869 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 870 | size = 0; |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 871 | ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid, |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 872 | NULL, &size, NULL)); |
| 873 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 874 | if (ret == EFI_NOT_FOUND) { |
| 875 | printf("BootOrder not defined\n"); |
| 876 | return CMD_RET_SUCCESS; |
| 877 | } else { |
| 878 | return CMD_RET_FAILURE; |
| 879 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 880 | } |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 881 | bootorder = malloc(size); |
| 882 | if (!bootorder) { |
| 883 | printf("ERROR: Out of memory\n"); |
| 884 | return CMD_RET_FAILURE; |
| 885 | } |
| 886 | ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid, |
| 887 | NULL, &size, bootorder)); |
| 888 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 889 | ret = CMD_RET_FAILURE; |
| 890 | goto out; |
| 891 | } |
| 892 | |
| 893 | num = size / sizeof(u16); |
| 894 | for (i = 0; i < num; i++) { |
| 895 | sprintf(var_name, "Boot%04X", bootorder[i]); |
| 896 | p16 = var_name16; |
| 897 | utf8_utf16_strncpy(&p16, var_name, 9); |
| 898 | |
| 899 | size = 0; |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 900 | ret = EFI_CALL(efi_get_variable(var_name16, |
| 901 | &efi_global_variable_guid, NULL, |
| 902 | &size, NULL)); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 903 | if (ret != EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 904 | printf("%2d: %s: (not defined)\n", i + 1, var_name); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 905 | continue; |
| 906 | } |
| 907 | |
| 908 | data = malloc(size); |
| 909 | if (!data) { |
| 910 | ret = CMD_RET_FAILURE; |
| 911 | goto out; |
| 912 | } |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 913 | ret = EFI_CALL(efi_get_variable(var_name16, |
| 914 | &efi_global_variable_guid, NULL, |
| 915 | &size, data)); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 916 | if (ret != EFI_SUCCESS) { |
| 917 | free(data); |
| 918 | ret = CMD_RET_FAILURE; |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | efi_deserialize_load_option(&lo, data); |
| 923 | |
| 924 | label_len16 = u16_strlen(lo.label); |
| 925 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 926 | label = malloc(label_len + 1); |
| 927 | if (!label) { |
| 928 | free(data); |
| 929 | ret = CMD_RET_FAILURE; |
| 930 | goto out; |
| 931 | } |
| 932 | p = label; |
| 933 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 934 | printf("%2d: %s: %s\n", i + 1, var_name, label); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 935 | free(label); |
| 936 | |
| 937 | free(data); |
| 938 | } |
| 939 | out: |
| 940 | free(bootorder); |
| 941 | |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * do_efi_boot_next() - manage UEFI BootNext variable |
| 947 | * |
| 948 | * @cmdtp: Command table |
| 949 | * @flag: Command flag |
| 950 | * @argc: Number of arguments |
| 951 | * @argv: Argument array |
| 952 | * Return: CMD_RET_SUCCESS on success, |
| 953 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 954 | * |
| 955 | * Implement efidebug "boot next" sub-command. |
| 956 | * Set BootNext variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 957 | * |
| 958 | * efidebug boot next <id> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 959 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 960 | static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag, |
| 961 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 962 | { |
| 963 | u16 bootnext; |
| 964 | efi_uintn_t size; |
| 965 | char *endp; |
| 966 | efi_guid_t guid; |
| 967 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 968 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 969 | |
| 970 | if (argc != 2) |
| 971 | return CMD_RET_USAGE; |
| 972 | |
| 973 | bootnext = (u16)simple_strtoul(argv[1], &endp, 16); |
Heinrich Schuchardt | bdb1577 | 2020-05-09 17:59:19 +0200 | [diff] [blame] | 974 | if (*endp) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 975 | printf("invalid value: %s\n", argv[1]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 976 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | guid = efi_global_variable_guid; |
| 981 | size = sizeof(u16); |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 982 | ret = EFI_CALL(efi_set_variable(L"BootNext", &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 983 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 984 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 985 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 986 | size, &bootnext)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 987 | if (ret != EFI_SUCCESS) { |
| 988 | printf("Cannot set BootNext\n"); |
| 989 | r = CMD_RET_FAILURE; |
| 990 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 991 | out: |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 992 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
| 996 | * do_efi_boot_order() - manage UEFI BootOrder variable |
| 997 | * |
| 998 | * @cmdtp: Command table |
| 999 | * @flag: Command flag |
| 1000 | * @argc: Number of arguments |
| 1001 | * @argv: Argument array |
| 1002 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1003 | * |
| 1004 | * Implement efidebug "boot order" sub-command. |
| 1005 | * Show order of UEFI load options, or change it in BootOrder variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 1006 | * |
| 1007 | * efidebug boot order [<id> ...] |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1008 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1009 | static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag, |
| 1010 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1011 | { |
| 1012 | u16 *bootorder = NULL; |
| 1013 | efi_uintn_t size; |
| 1014 | int id, i; |
| 1015 | char *endp; |
| 1016 | efi_guid_t guid; |
| 1017 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1018 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1019 | |
| 1020 | if (argc == 1) |
| 1021 | return show_efi_boot_order(); |
| 1022 | |
| 1023 | argc--; |
| 1024 | argv++; |
| 1025 | |
| 1026 | size = argc * sizeof(u16); |
| 1027 | bootorder = malloc(size); |
| 1028 | if (!bootorder) |
| 1029 | return CMD_RET_FAILURE; |
| 1030 | |
| 1031 | for (i = 0; i < argc; i++) { |
| 1032 | id = (int)simple_strtoul(argv[i], &endp, 16); |
| 1033 | if (*endp != '\0' || id > 0xffff) { |
| 1034 | printf("invalid value: %s\n", argv[i]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1035 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1036 | goto out; |
| 1037 | } |
| 1038 | |
| 1039 | bootorder[i] = (u16)id; |
| 1040 | } |
| 1041 | |
| 1042 | guid = efi_global_variable_guid; |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 1043 | ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid, |
AKASHI Takahiro | f658c2e | 2019-06-04 15:52:10 +0900 | [diff] [blame] | 1044 | EFI_VARIABLE_NON_VOLATILE | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1045 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1046 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1047 | size, bootorder)); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1048 | if (ret != EFI_SUCCESS) { |
| 1049 | printf("Cannot set BootOrder\n"); |
| 1050 | r = CMD_RET_FAILURE; |
| 1051 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1052 | out: |
| 1053 | free(bootorder); |
| 1054 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1055 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1056 | } |
| 1057 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1058 | static struct cmd_tbl cmd_efidebug_boot_sub[] = { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1059 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
| 1060 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), |
| 1061 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), |
| 1062 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), |
| 1063 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, |
| 1064 | "", ""), |
| 1065 | }; |
| 1066 | |
| 1067 | /** |
| 1068 | * do_efi_boot_opt() - manage UEFI load options |
| 1069 | * |
| 1070 | * @cmdtp: Command table |
| 1071 | * @flag: Command flag |
| 1072 | * @argc: Number of arguments |
| 1073 | * @argv: Argument array |
| 1074 | * Return: CMD_RET_SUCCESS on success, |
| 1075 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1076 | * |
| 1077 | * Implement efidebug "boot" sub-command. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1078 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1079 | static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag, |
| 1080 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1081 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1082 | struct cmd_tbl *cp; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1083 | |
| 1084 | if (argc < 2) |
| 1085 | return CMD_RET_USAGE; |
| 1086 | |
| 1087 | argc--; argv++; |
| 1088 | |
| 1089 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, |
| 1090 | ARRAY_SIZE(cmd_efidebug_boot_sub)); |
| 1091 | if (!cp) |
| 1092 | return CMD_RET_USAGE; |
| 1093 | |
| 1094 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1095 | } |
| 1096 | |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1097 | /** |
| 1098 | * do_efi_test_bootmgr() - run simple bootmgr for test |
| 1099 | * |
| 1100 | * @cmdtp: Command table |
| 1101 | * @flag: Command flag |
| 1102 | * @argc: Number of arguments |
| 1103 | * @argv: Argument array |
| 1104 | * Return: CMD_RET_SUCCESS on success, |
| 1105 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1106 | * |
| 1107 | * Implement efidebug "test bootmgr" sub-command. |
| 1108 | * Run simple bootmgr for test. |
| 1109 | * |
| 1110 | * efidebug test bootmgr |
| 1111 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1112 | static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag, |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1113 | int argc, char * const argv[]) |
| 1114 | { |
| 1115 | efi_handle_t image; |
| 1116 | efi_uintn_t exit_data_size = 0; |
| 1117 | u16 *exit_data = NULL; |
| 1118 | efi_status_t ret; |
| 1119 | |
| 1120 | ret = efi_bootmgr_load(&image); |
| 1121 | printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1122 | |
| 1123 | /* We call efi_start_image() even if error for test purpose. */ |
| 1124 | ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data)); |
| 1125 | printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1126 | if (ret && exit_data) |
| 1127 | efi_free_pool(exit_data); |
| 1128 | |
| 1129 | efi_restore_gd(); |
| 1130 | |
| 1131 | return CMD_RET_SUCCESS; |
| 1132 | } |
| 1133 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1134 | static struct cmd_tbl cmd_efidebug_test_sub[] = { |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1135 | U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr, |
| 1136 | "", ""), |
| 1137 | }; |
| 1138 | |
| 1139 | /** |
| 1140 | * do_efi_test() - manage UEFI load options |
| 1141 | * |
| 1142 | * @cmdtp: Command table |
| 1143 | * @flag: Command flag |
| 1144 | * @argc: Number of arguments |
| 1145 | * @argv: Argument array |
| 1146 | * Return: CMD_RET_SUCCESS on success, |
| 1147 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1148 | * |
| 1149 | * Implement efidebug "test" sub-command. |
| 1150 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1151 | static int do_efi_test(struct cmd_tbl *cmdtp, int flag, |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1152 | int argc, char * const argv[]) |
| 1153 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1154 | struct cmd_tbl *cp; |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1155 | |
| 1156 | if (argc < 2) |
| 1157 | return CMD_RET_USAGE; |
| 1158 | |
| 1159 | argc--; argv++; |
| 1160 | |
| 1161 | cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub, |
| 1162 | ARRAY_SIZE(cmd_efidebug_test_sub)); |
| 1163 | if (!cp) |
| 1164 | return CMD_RET_USAGE; |
| 1165 | |
| 1166 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1167 | } |
| 1168 | |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1169 | /** |
| 1170 | * do_efi_query_info() - QueryVariableInfo EFI service |
| 1171 | * |
| 1172 | * @cmdtp: Command table |
| 1173 | * @flag: Command flag |
| 1174 | * @argc: Number of arguments |
| 1175 | * @argv: Argument array |
| 1176 | * Return: CMD_RET_SUCCESS on success, |
| 1177 | * CMD_RET_USAGE or CMD_RET_FAILURE on failure |
| 1178 | * |
| 1179 | * Implement efidebug "test" sub-command. |
| 1180 | */ |
| 1181 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1182 | static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag, |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1183 | int argc, char * const argv[]) |
| 1184 | { |
| 1185 | efi_status_t ret; |
| 1186 | u32 attr = 0; |
| 1187 | u64 max_variable_storage_size; |
| 1188 | u64 remain_variable_storage_size; |
| 1189 | u64 max_variable_size; |
| 1190 | int i; |
| 1191 | |
| 1192 | for (i = 1; i < argc; i++) { |
| 1193 | if (!strcmp(argv[i], "-bs")) |
| 1194 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 1195 | else if (!strcmp(argv[i], "-rt")) |
| 1196 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 1197 | else if (!strcmp(argv[i], "-nv")) |
| 1198 | attr |= EFI_VARIABLE_NON_VOLATILE; |
| 1199 | else if (!strcmp(argv[i], "-at")) |
| 1200 | attr |= |
| 1201 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
| 1202 | } |
| 1203 | |
| 1204 | ret = EFI_CALL(efi_query_variable_info(attr, |
| 1205 | &max_variable_storage_size, |
| 1206 | &remain_variable_storage_size, |
| 1207 | &max_variable_size)); |
| 1208 | if (ret != EFI_SUCCESS) { |
| 1209 | printf("Error: Cannot query UEFI variables, r = %lu\n", |
| 1210 | ret & ~EFI_ERROR_MASK); |
| 1211 | return CMD_RET_FAILURE; |
| 1212 | } |
| 1213 | |
| 1214 | printf("Max storage size %llu\n", max_variable_storage_size); |
| 1215 | printf("Remaining storage size %llu\n", remain_variable_storage_size); |
| 1216 | printf("Max variable size %llu\n", max_variable_size); |
| 1217 | |
| 1218 | return CMD_RET_SUCCESS; |
| 1219 | } |
| 1220 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1221 | static struct cmd_tbl cmd_efidebug_sub[] = { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1222 | 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] | 1223 | U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices, |
| 1224 | "", ""), |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1225 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
| 1226 | "", ""), |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1227 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
| 1228 | "", ""), |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1229 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
| 1230 | "", ""), |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1231 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
| 1232 | "", ""), |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1233 | U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, |
| 1234 | "", ""), |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1235 | U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test, |
| 1236 | "", ""), |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1237 | U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info, |
| 1238 | "", ""), |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1239 | }; |
| 1240 | |
| 1241 | /** |
| 1242 | * do_efidebug() - display and configure UEFI environment |
| 1243 | * |
| 1244 | * @cmdtp: Command table |
| 1245 | * @flag: Command flag |
| 1246 | * @argc: Number of arguments |
| 1247 | * @argv: Argument array |
| 1248 | * Return: CMD_RET_SUCCESS on success, |
| 1249 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1250 | * |
| 1251 | * Implement efidebug command which allows us to display and |
| 1252 | * configure UEFI environment. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1253 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1254 | static int do_efidebug(struct cmd_tbl *cmdtp, int flag, |
| 1255 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1256 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1257 | struct cmd_tbl *cp; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1258 | efi_status_t r; |
| 1259 | |
| 1260 | if (argc < 2) |
| 1261 | return CMD_RET_USAGE; |
| 1262 | |
| 1263 | argc--; argv++; |
| 1264 | |
| 1265 | /* Initialize UEFI drivers */ |
| 1266 | r = efi_init_obj_list(); |
| 1267 | if (r != EFI_SUCCESS) { |
| 1268 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1269 | r & ~EFI_ERROR_MASK); |
| 1270 | return CMD_RET_FAILURE; |
| 1271 | } |
| 1272 | |
| 1273 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, |
| 1274 | ARRAY_SIZE(cmd_efidebug_sub)); |
| 1275 | if (!cp) |
| 1276 | return CMD_RET_USAGE; |
| 1277 | |
| 1278 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1279 | } |
| 1280 | |
| 1281 | #ifdef CONFIG_SYS_LONGHELP |
| 1282 | static char efidebug_help_text[] = |
| 1283 | " - UEFI Shell-like interface to configure UEFI environment\n" |
| 1284 | "\n" |
| 1285 | "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n" |
| 1286 | " - set UEFI BootXXXX variable\n" |
| 1287 | " <load options> will be passed to UEFI application\n" |
| 1288 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" |
| 1289 | " - delete UEFI BootXXXX variables\n" |
| 1290 | "efidebug boot dump\n" |
| 1291 | " - dump all UEFI BootXXXX variables\n" |
| 1292 | "efidebug boot next <bootid>\n" |
| 1293 | " - set UEFI BootNext variable\n" |
| 1294 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" |
| 1295 | " - set/show UEFI boot order\n" |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 1296 | "\n" |
| 1297 | "efidebug devices\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1298 | " - show UEFI devices\n" |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1299 | "efidebug drivers\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1300 | " - show UEFI drivers\n" |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1301 | "efidebug dh\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1302 | " - show UEFI handles\n" |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1303 | "efidebug images\n" |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1304 | " - show loaded images\n" |
| 1305 | "efidebug memmap\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1306 | " - show UEFI memory map\n" |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1307 | "efidebug tables\n" |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1308 | " - show UEFI configuration tables\n" |
| 1309 | "efidebug test bootmgr\n" |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1310 | " - run simple bootmgr for test\n" |
| 1311 | "efidebug query [-nv][-bs][-rt][-at]\n" |
| 1312 | " - show size of UEFI variables store\n"; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1313 | #endif |
| 1314 | |
| 1315 | U_BOOT_CMD( |
| 1316 | efidebug, 10, 0, do_efidebug, |
| 1317 | "Configure UEFI environment", |
| 1318 | efidebug_help_text |
| 1319 | ); |