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