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> |
| 12 | #include <environment.h> |
| 13 | #include <exports.h> |
| 14 | #include <malloc.h> |
| 15 | #include <search.h> |
| 16 | #include <linux/ctype.h> |
| 17 | |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 18 | #define BS systab.boottime |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 19 | #define RT systab.runtime |
| 20 | |
| 21 | /** |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 22 | * efi_get_device_handle_info() - get information of UEFI device |
| 23 | * |
| 24 | * @handle: Handle of UEFI device |
| 25 | * @dev_path_text: Pointer to text of device path |
| 26 | * Return: 0 on success, -1 on failure |
| 27 | * |
| 28 | * Currently return a formatted text of device path. |
| 29 | */ |
| 30 | static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text) |
| 31 | { |
| 32 | struct efi_device_path *dp; |
| 33 | efi_status_t ret; |
| 34 | |
| 35 | ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path, |
| 36 | (void **)&dp, NULL /* FIXME */, NULL, |
| 37 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 38 | if (ret == EFI_SUCCESS) { |
| 39 | *dev_path_text = efi_dp_str(dp); |
| 40 | return 0; |
| 41 | } else { |
| 42 | return -1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) |
| 47 | |
| 48 | static const char spc[] = " "; |
| 49 | static const char sep[] = "================"; |
| 50 | |
| 51 | /** |
| 52 | * do_efi_show_devices() - show UEFI devices |
| 53 | * |
| 54 | * @cmdtp: Command table |
| 55 | * @flag: Command flag |
| 56 | * @argc: Number of arguments |
| 57 | * @argv: Argument array |
| 58 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 59 | * |
| 60 | * Implement efidebug "devices" sub-command. |
| 61 | * Show all UEFI devices and their information. |
| 62 | */ |
| 63 | static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag, |
| 64 | int argc, char * const argv[]) |
| 65 | { |
| 66 | efi_handle_t *handles; |
| 67 | efi_uintn_t num, i; |
| 68 | u16 *dev_path_text; |
| 69 | efi_status_t ret; |
| 70 | |
| 71 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
| 72 | &num, &handles)); |
| 73 | if (ret != EFI_SUCCESS) |
| 74 | return CMD_RET_FAILURE; |
| 75 | |
| 76 | if (!num) |
| 77 | return CMD_RET_SUCCESS; |
| 78 | |
| 79 | printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc); |
| 80 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); |
| 81 | for (i = 0; i < num; i++) { |
| 82 | if (!efi_get_device_handle_info(handles[i], &dev_path_text)) { |
| 83 | printf("%p %ls\n", handles[i], dev_path_text); |
| 84 | efi_free_pool(dev_path_text); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | EFI_CALL(BS->free_pool(handles)); |
| 89 | |
| 90 | return CMD_RET_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | /** |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 94 | * efi_get_driver_handle_info() - get information of UEFI driver |
| 95 | * |
| 96 | * @handle: Handle of UEFI device |
| 97 | * @driver_name: Driver name |
| 98 | * @image_path: Pointer to text of device path |
| 99 | * Return: 0 on success, -1 on failure |
| 100 | * |
| 101 | * Currently return no useful information as all UEFI drivers are |
| 102 | * built-in.. |
| 103 | */ |
| 104 | static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name, |
| 105 | u16 **image_path) |
| 106 | { |
| 107 | struct efi_handler *handler; |
| 108 | struct efi_loaded_image *image; |
| 109 | efi_status_t ret; |
| 110 | |
| 111 | /* |
| 112 | * driver name |
| 113 | * TODO: support EFI_COMPONENT_NAME2_PROTOCOL |
| 114 | */ |
| 115 | *driver_name = NULL; |
| 116 | |
| 117 | /* image name */ |
| 118 | ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); |
| 119 | if (ret != EFI_SUCCESS) { |
| 120 | *image_path = NULL; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | image = handler->protocol_interface; |
| 125 | *image_path = efi_dp_str(image->file_path); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * do_efi_show_drivers() - show UEFI drivers |
| 132 | * |
| 133 | * @cmdtp: Command table |
| 134 | * @flag: Command flag |
| 135 | * @argc: Number of arguments |
| 136 | * @argv: Argument array |
| 137 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 138 | * |
| 139 | * Implement efidebug "drivers" sub-command. |
| 140 | * Show all UEFI drivers and their information. |
| 141 | */ |
| 142 | static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag, |
| 143 | int argc, char * const argv[]) |
| 144 | { |
| 145 | efi_handle_t *handles; |
| 146 | efi_uintn_t num, i; |
| 147 | u16 *driver_name, *image_path_text; |
| 148 | efi_status_t ret; |
| 149 | |
| 150 | ret = EFI_CALL(BS->locate_handle_buffer( |
| 151 | BY_PROTOCOL, &efi_guid_driver_binding_protocol, |
| 152 | NULL, &num, &handles)); |
| 153 | if (ret != EFI_SUCCESS) |
| 154 | return CMD_RET_FAILURE; |
| 155 | |
| 156 | if (!num) |
| 157 | return CMD_RET_SUCCESS; |
| 158 | |
| 159 | printf("Driver%.*s Name Image Path\n", |
| 160 | EFI_HANDLE_WIDTH - 6, spc); |
| 161 | printf("%.*s ==================== ====================\n", |
| 162 | EFI_HANDLE_WIDTH, sep); |
| 163 | for (i = 0; i < num; i++) { |
| 164 | if (!efi_get_driver_handle_info(handles[i], &driver_name, |
| 165 | &image_path_text)) { |
| 166 | if (image_path_text) |
| 167 | printf("%p %-20ls %ls\n", handles[i], |
| 168 | driver_name, image_path_text); |
| 169 | else |
| 170 | printf("%p %-20ls <built-in>\n", |
| 171 | handles[i], driver_name); |
| 172 | EFI_CALL(BS->free_pool(driver_name)); |
| 173 | EFI_CALL(BS->free_pool(image_path_text)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | EFI_CALL(BS->free_pool(handles)); |
| 178 | |
| 179 | return CMD_RET_SUCCESS; |
| 180 | } |
| 181 | |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame^] | 182 | static const struct { |
| 183 | const char *text; |
| 184 | const efi_guid_t guid; |
| 185 | } guid_list[] = { |
| 186 | { |
| 187 | "Device Path", |
| 188 | DEVICE_PATH_GUID, |
| 189 | }, |
| 190 | { |
| 191 | "Device Path To Text", |
| 192 | EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, |
| 193 | }, |
| 194 | { |
| 195 | "Device Path Utilities", |
| 196 | EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, |
| 197 | }, |
| 198 | { |
| 199 | "Unicode Collation 2", |
| 200 | EFI_UNICODE_COLLATION_PROTOCOL2_GUID, |
| 201 | }, |
| 202 | { |
| 203 | "Driver Binding", |
| 204 | EFI_DRIVER_BINDING_PROTOCOL_GUID, |
| 205 | }, |
| 206 | { |
| 207 | "Simple Text Input", |
| 208 | EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, |
| 209 | }, |
| 210 | { |
| 211 | "Simple Text Input Ex", |
| 212 | EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, |
| 213 | }, |
| 214 | { |
| 215 | "Simple Text Output", |
| 216 | EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, |
| 217 | }, |
| 218 | { |
| 219 | "Block IO", |
| 220 | BLOCK_IO_GUID, |
| 221 | }, |
| 222 | { |
| 223 | "Simple File System", |
| 224 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, |
| 225 | }, |
| 226 | { |
| 227 | "Loaded Image", |
| 228 | LOADED_IMAGE_PROTOCOL_GUID, |
| 229 | }, |
| 230 | { |
| 231 | "GOP", |
| 232 | EFI_GOP_GUID, |
| 233 | }, |
| 234 | }; |
| 235 | |
| 236 | /** |
| 237 | * get_guid_text - get string of protocol guid |
| 238 | * @guid: Protocol guid |
| 239 | * Return: String |
| 240 | * |
| 241 | * Return string for display to represent the protocol. |
| 242 | */ |
| 243 | static const char *get_guid_text(const efi_guid_t *guid) |
| 244 | { |
| 245 | int i; |
| 246 | |
| 247 | for (i = 0; i < ARRAY_SIZE(guid_list); i++) |
| 248 | if (!guidcmp(&guid_list[i].guid, guid)) |
| 249 | break; |
| 250 | |
| 251 | if (i != ARRAY_SIZE(guid_list)) |
| 252 | return guid_list[i].text; |
| 253 | else |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * do_efi_show_handles() - show UEFI handles |
| 259 | * |
| 260 | * @cmdtp: Command table |
| 261 | * @flag: Command flag |
| 262 | * @argc: Number of arguments |
| 263 | * @argv: Argument array |
| 264 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 265 | * |
| 266 | * Implement efidebug "dh" sub-command. |
| 267 | * Show all UEFI handles and their information, currently all protocols |
| 268 | * added to handle. |
| 269 | */ |
| 270 | static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag, |
| 271 | int argc, char * const argv[]) |
| 272 | { |
| 273 | efi_handle_t *handles; |
| 274 | efi_guid_t **guid; |
| 275 | efi_uintn_t num, count, i, j; |
| 276 | const char *guid_text; |
| 277 | efi_status_t ret; |
| 278 | |
| 279 | ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
| 280 | &num, &handles)); |
| 281 | if (ret != EFI_SUCCESS) |
| 282 | return CMD_RET_FAILURE; |
| 283 | |
| 284 | if (!num) |
| 285 | return CMD_RET_SUCCESS; |
| 286 | |
| 287 | printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc); |
| 288 | printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); |
| 289 | for (i = 0; i < num; i++) { |
| 290 | printf("%p", handles[i]); |
| 291 | ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, |
| 292 | &count)); |
| 293 | if (ret || !count) { |
| 294 | putc('\n'); |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | for (j = 0; j < count; j++) { |
| 299 | if (j) |
| 300 | printf(", "); |
| 301 | else |
| 302 | putc(' '); |
| 303 | |
| 304 | guid_text = get_guid_text(guid[j]); |
| 305 | if (guid_text) |
| 306 | puts(guid_text); |
| 307 | else |
| 308 | printf("%pUl", guid[j]); |
| 309 | } |
| 310 | putc('\n'); |
| 311 | } |
| 312 | |
| 313 | EFI_CALL(BS->free_pool(handles)); |
| 314 | |
| 315 | return CMD_RET_SUCCESS; |
| 316 | } |
| 317 | |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 318 | /** |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 319 | * do_efi_boot_add() - set UEFI load option |
| 320 | * |
| 321 | * @cmdtp: Command table |
| 322 | * @flag: Command flag |
| 323 | * @argc: Number of arguments |
| 324 | * @argv: Argument array |
| 325 | * Return: CMD_RET_SUCCESS on success, |
| 326 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 327 | * |
| 328 | * Implement efidebug "boot add" sub-command. |
| 329 | * Create or change UEFI load option. |
| 330 | * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options> |
| 331 | */ |
| 332 | static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag, |
| 333 | int argc, char * const argv[]) |
| 334 | { |
| 335 | int id; |
| 336 | char *endp; |
| 337 | char var_name[9]; |
| 338 | u16 var_name16[9], *p; |
| 339 | efi_guid_t guid; |
| 340 | size_t label_len, label_len16; |
| 341 | u16 *label; |
| 342 | struct efi_device_path *device_path = NULL, *file_path = NULL; |
| 343 | struct efi_load_option lo; |
| 344 | void *data = NULL; |
| 345 | efi_uintn_t size; |
| 346 | int ret; |
| 347 | |
| 348 | if (argc < 6 || argc > 7) |
| 349 | return CMD_RET_USAGE; |
| 350 | |
| 351 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 352 | if (*endp != '\0' || id > 0xffff) |
| 353 | return CMD_RET_FAILURE; |
| 354 | |
| 355 | sprintf(var_name, "Boot%04X", id); |
| 356 | p = var_name16; |
| 357 | utf8_utf16_strncpy(&p, var_name, 9); |
| 358 | |
| 359 | guid = efi_global_variable_guid; |
| 360 | |
| 361 | /* attributes */ |
| 362 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ |
| 363 | |
| 364 | /* label */ |
| 365 | label_len = strlen(argv[2]); |
| 366 | label_len16 = utf8_utf16_strnlen(argv[2], label_len); |
| 367 | label = malloc((label_len16 + 1) * sizeof(u16)); |
| 368 | if (!label) |
| 369 | return CMD_RET_FAILURE; |
| 370 | lo.label = label; /* label will be changed below */ |
| 371 | utf8_utf16_strncpy(&label, argv[2], label_len); |
| 372 | |
| 373 | /* file path */ |
| 374 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path, |
| 375 | &file_path); |
| 376 | if (ret != EFI_SUCCESS) { |
| 377 | printf("Cannot create device path for \"%s %s\"\n", |
| 378 | argv[3], argv[4]); |
| 379 | ret = CMD_RET_FAILURE; |
| 380 | goto out; |
| 381 | } |
| 382 | lo.file_path = file_path; |
| 383 | lo.file_path_length = efi_dp_size(file_path) |
| 384 | + sizeof(struct efi_device_path); /* for END */ |
| 385 | |
| 386 | /* optional data */ |
| 387 | lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]); |
| 388 | |
| 389 | size = efi_serialize_load_option(&lo, (u8 **)&data); |
| 390 | if (!size) { |
| 391 | ret = CMD_RET_FAILURE; |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, |
| 396 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 397 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 398 | size, data)); |
| 399 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); |
| 400 | out: |
| 401 | free(data); |
| 402 | efi_free_pool(device_path); |
| 403 | efi_free_pool(file_path); |
| 404 | free(lo.label); |
| 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * do_efi_boot_rm() - delete UEFI load options |
| 411 | * |
| 412 | * @cmdtp: Command table |
| 413 | * @flag: Command flag |
| 414 | * @argc: Number of arguments |
| 415 | * @argv: Argument array |
| 416 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 417 | * |
| 418 | * Implement efidebug "boot rm" sub-command. |
| 419 | * Delete UEFI load options. |
| 420 | * - boot rm <id> ... |
| 421 | */ |
| 422 | static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, |
| 423 | int argc, char * const argv[]) |
| 424 | { |
| 425 | efi_guid_t guid; |
| 426 | int id, i; |
| 427 | char *endp; |
| 428 | char var_name[9]; |
| 429 | u16 var_name16[9]; |
| 430 | efi_status_t ret; |
| 431 | |
| 432 | if (argc == 1) |
| 433 | return CMD_RET_USAGE; |
| 434 | |
| 435 | guid = efi_global_variable_guid; |
| 436 | for (i = 1; i < argc; i++, argv++) { |
| 437 | id = (int)simple_strtoul(argv[1], &endp, 16); |
| 438 | if (*endp != '\0' || id > 0xffff) |
| 439 | return CMD_RET_FAILURE; |
| 440 | |
| 441 | sprintf(var_name, "Boot%04X", id); |
| 442 | utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9); |
| 443 | |
| 444 | ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL)); |
| 445 | if (ret) { |
| 446 | printf("cannot remove Boot%04X", id); |
| 447 | return CMD_RET_FAILURE; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return CMD_RET_SUCCESS; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * show_efi_boot_opt_data() - dump UEFI load option |
| 456 | * |
| 457 | * @id: Load option number |
| 458 | * @data: Value of UEFI load option variable |
| 459 | * |
| 460 | * Decode the value of UEFI load option variable and print information. |
| 461 | */ |
| 462 | static void show_efi_boot_opt_data(int id, void *data) |
| 463 | { |
| 464 | struct efi_load_option lo; |
| 465 | char *label, *p; |
| 466 | size_t label_len16, label_len; |
| 467 | u16 *dp_str; |
| 468 | |
| 469 | efi_deserialize_load_option(&lo, data); |
| 470 | |
| 471 | label_len16 = u16_strlen(lo.label); |
| 472 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 473 | label = malloc(label_len + 1); |
| 474 | if (!label) |
| 475 | return; |
| 476 | p = label; |
| 477 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
| 478 | |
| 479 | printf("Boot%04X:\n", id); |
| 480 | printf("\tattributes: %c%c%c (0x%08x)\n", |
| 481 | /* ACTIVE */ |
| 482 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', |
| 483 | /* FORCE RECONNECT */ |
| 484 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', |
| 485 | /* HIDDEN */ |
| 486 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', |
| 487 | lo.attributes); |
| 488 | printf("\tlabel: %s\n", label); |
| 489 | |
| 490 | dp_str = efi_dp_str(lo.file_path); |
| 491 | printf("\tfile_path: %ls\n", dp_str); |
| 492 | efi_free_pool(dp_str); |
| 493 | |
| 494 | printf("\tdata: %s\n", lo.optional_data); |
| 495 | |
| 496 | free(label); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * show_efi_boot_opt() - dump UEFI load option |
| 501 | * |
| 502 | * @id: Load option number |
| 503 | * |
| 504 | * Dump information defined by UEFI load option. |
| 505 | */ |
| 506 | static void show_efi_boot_opt(int id) |
| 507 | { |
| 508 | char var_name[9]; |
| 509 | u16 var_name16[9], *p; |
| 510 | efi_guid_t guid; |
| 511 | void *data = NULL; |
| 512 | efi_uintn_t size; |
| 513 | int ret; |
| 514 | |
| 515 | sprintf(var_name, "Boot%04X", id); |
| 516 | p = var_name16; |
| 517 | utf8_utf16_strncpy(&p, var_name, 9); |
| 518 | guid = efi_global_variable_guid; |
| 519 | |
| 520 | size = 0; |
| 521 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL)); |
| 522 | if (ret == (int)EFI_BUFFER_TOO_SMALL) { |
| 523 | data = malloc(size); |
| 524 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 525 | data)); |
| 526 | } |
| 527 | if (ret == EFI_SUCCESS) |
| 528 | show_efi_boot_opt_data(id, data); |
| 529 | else if (ret == EFI_NOT_FOUND) |
| 530 | printf("Boot%04X: not found\n", id); |
| 531 | |
| 532 | free(data); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * show_efi_boot_dump() - dump all UEFI load options |
| 537 | * |
| 538 | * @cmdtp: Command table |
| 539 | * @flag: Command flag |
| 540 | * @argc: Number of arguments |
| 541 | * @argv: Argument array |
| 542 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 543 | * |
| 544 | * Implement efidebug "boot dump" sub-command. |
| 545 | * Dump information of all UEFI load options defined. |
| 546 | * - boot dump |
| 547 | */ |
| 548 | static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag, |
| 549 | int argc, char * const argv[]) |
| 550 | { |
| 551 | char regex[256]; |
| 552 | char * const regexlist[] = {regex}; |
| 553 | char *variables = NULL, *boot, *value; |
| 554 | int len; |
| 555 | int id; |
| 556 | |
| 557 | if (argc > 1) |
| 558 | return CMD_RET_USAGE; |
| 559 | |
| 560 | snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+"); |
| 561 | |
| 562 | /* TODO: use GetNextVariableName? */ |
| 563 | len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY, |
| 564 | &variables, 0, 1, regexlist); |
| 565 | |
| 566 | if (!len) |
| 567 | return CMD_RET_SUCCESS; |
| 568 | |
| 569 | if (len < 0) |
| 570 | return CMD_RET_FAILURE; |
| 571 | |
| 572 | boot = variables; |
| 573 | while (*boot) { |
| 574 | value = strstr(boot, "Boot") + 4; |
| 575 | id = (int)simple_strtoul(value, NULL, 16); |
| 576 | show_efi_boot_opt(id); |
| 577 | boot = strchr(boot, '\n'); |
| 578 | if (!*boot) |
| 579 | break; |
| 580 | boot++; |
| 581 | } |
| 582 | free(variables); |
| 583 | |
| 584 | return CMD_RET_SUCCESS; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * show_efi_boot_order() - show order of UEFI load options |
| 589 | * |
| 590 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 591 | * |
| 592 | * Show order of UEFI load options defined by BootOrder variable. |
| 593 | */ |
| 594 | static int show_efi_boot_order(void) |
| 595 | { |
| 596 | efi_guid_t guid; |
| 597 | u16 *bootorder = NULL; |
| 598 | efi_uintn_t size; |
| 599 | int num, i; |
| 600 | char var_name[9]; |
| 601 | u16 var_name16[9], *p16; |
| 602 | void *data; |
| 603 | struct efi_load_option lo; |
| 604 | char *label, *p; |
| 605 | size_t label_len16, label_len; |
| 606 | efi_status_t ret; |
| 607 | |
| 608 | guid = efi_global_variable_guid; |
| 609 | size = 0; |
| 610 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size, |
| 611 | NULL)); |
| 612 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 613 | bootorder = malloc(size); |
| 614 | ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, |
| 615 | &size, bootorder)); |
| 616 | } |
| 617 | if (ret == EFI_NOT_FOUND) { |
| 618 | printf("BootOrder not defined\n"); |
| 619 | ret = CMD_RET_SUCCESS; |
| 620 | goto out; |
| 621 | } else if (ret != EFI_SUCCESS) { |
| 622 | ret = CMD_RET_FAILURE; |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | num = size / sizeof(u16); |
| 627 | for (i = 0; i < num; i++) { |
| 628 | sprintf(var_name, "Boot%04X", bootorder[i]); |
| 629 | p16 = var_name16; |
| 630 | utf8_utf16_strncpy(&p16, var_name, 9); |
| 631 | |
| 632 | size = 0; |
| 633 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 634 | NULL)); |
| 635 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 636 | printf("%2d: Boot%04X: (not defined)\n", |
| 637 | i + 1, bootorder[i]); |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | data = malloc(size); |
| 642 | if (!data) { |
| 643 | ret = CMD_RET_FAILURE; |
| 644 | goto out; |
| 645 | } |
| 646 | ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, |
| 647 | data)); |
| 648 | if (ret != EFI_SUCCESS) { |
| 649 | free(data); |
| 650 | ret = CMD_RET_FAILURE; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | efi_deserialize_load_option(&lo, data); |
| 655 | |
| 656 | label_len16 = u16_strlen(lo.label); |
| 657 | label_len = utf16_utf8_strnlen(lo.label, label_len16); |
| 658 | label = malloc(label_len + 1); |
| 659 | if (!label) { |
| 660 | free(data); |
| 661 | ret = CMD_RET_FAILURE; |
| 662 | goto out; |
| 663 | } |
| 664 | p = label; |
| 665 | utf16_utf8_strncpy(&p, lo.label, label_len16); |
| 666 | printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label); |
| 667 | free(label); |
| 668 | |
| 669 | free(data); |
| 670 | } |
| 671 | out: |
| 672 | free(bootorder); |
| 673 | |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * do_efi_boot_next() - manage UEFI BootNext variable |
| 679 | * |
| 680 | * @cmdtp: Command table |
| 681 | * @flag: Command flag |
| 682 | * @argc: Number of arguments |
| 683 | * @argv: Argument array |
| 684 | * Return: CMD_RET_SUCCESS on success, |
| 685 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 686 | * |
| 687 | * Implement efidebug "boot next" sub-command. |
| 688 | * Set BootNext variable. |
| 689 | * - boot next <id> |
| 690 | */ |
| 691 | static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag, |
| 692 | int argc, char * const argv[]) |
| 693 | { |
| 694 | u16 bootnext; |
| 695 | efi_uintn_t size; |
| 696 | char *endp; |
| 697 | efi_guid_t guid; |
| 698 | efi_status_t ret; |
| 699 | |
| 700 | if (argc != 2) |
| 701 | return CMD_RET_USAGE; |
| 702 | |
| 703 | bootnext = (u16)simple_strtoul(argv[1], &endp, 16); |
| 704 | if (*endp != '\0' || bootnext > 0xffff) { |
| 705 | printf("invalid value: %s\n", argv[1]); |
| 706 | ret = CMD_RET_FAILURE; |
| 707 | goto out; |
| 708 | } |
| 709 | |
| 710 | guid = efi_global_variable_guid; |
| 711 | size = sizeof(u16); |
| 712 | ret = EFI_CALL(RT->set_variable(L"BootNext", &guid, |
| 713 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 714 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 715 | size, &bootnext)); |
| 716 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); |
| 717 | out: |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * do_efi_boot_order() - manage UEFI BootOrder variable |
| 723 | * |
| 724 | * @cmdtp: Command table |
| 725 | * @flag: Command flag |
| 726 | * @argc: Number of arguments |
| 727 | * @argv: Argument array |
| 728 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 729 | * |
| 730 | * Implement efidebug "boot order" sub-command. |
| 731 | * Show order of UEFI load options, or change it in BootOrder variable. |
| 732 | * - boot order [<id> ...] |
| 733 | */ |
| 734 | static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag, |
| 735 | int argc, char * const argv[]) |
| 736 | { |
| 737 | u16 *bootorder = NULL; |
| 738 | efi_uintn_t size; |
| 739 | int id, i; |
| 740 | char *endp; |
| 741 | efi_guid_t guid; |
| 742 | efi_status_t ret; |
| 743 | |
| 744 | if (argc == 1) |
| 745 | return show_efi_boot_order(); |
| 746 | |
| 747 | argc--; |
| 748 | argv++; |
| 749 | |
| 750 | size = argc * sizeof(u16); |
| 751 | bootorder = malloc(size); |
| 752 | if (!bootorder) |
| 753 | return CMD_RET_FAILURE; |
| 754 | |
| 755 | for (i = 0; i < argc; i++) { |
| 756 | id = (int)simple_strtoul(argv[i], &endp, 16); |
| 757 | if (*endp != '\0' || id > 0xffff) { |
| 758 | printf("invalid value: %s\n", argv[i]); |
| 759 | ret = CMD_RET_FAILURE; |
| 760 | goto out; |
| 761 | } |
| 762 | |
| 763 | bootorder[i] = (u16)id; |
| 764 | } |
| 765 | |
| 766 | guid = efi_global_variable_guid; |
| 767 | ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid, |
| 768 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 769 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 770 | size, bootorder)); |
| 771 | ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE); |
| 772 | out: |
| 773 | free(bootorder); |
| 774 | |
| 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | static cmd_tbl_t cmd_efidebug_boot_sub[] = { |
| 779 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
| 780 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), |
| 781 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), |
| 782 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), |
| 783 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, |
| 784 | "", ""), |
| 785 | }; |
| 786 | |
| 787 | /** |
| 788 | * do_efi_boot_opt() - manage 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, |
| 795 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 796 | * |
| 797 | * Implement efidebug "boot" sub-command. |
| 798 | * See above for details of sub-commands. |
| 799 | */ |
| 800 | static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag, |
| 801 | int argc, char * const argv[]) |
| 802 | { |
| 803 | cmd_tbl_t *cp; |
| 804 | |
| 805 | if (argc < 2) |
| 806 | return CMD_RET_USAGE; |
| 807 | |
| 808 | argc--; argv++; |
| 809 | |
| 810 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, |
| 811 | ARRAY_SIZE(cmd_efidebug_boot_sub)); |
| 812 | if (!cp) |
| 813 | return CMD_RET_USAGE; |
| 814 | |
| 815 | return cp->cmd(cmdtp, flag, argc, argv); |
| 816 | } |
| 817 | |
| 818 | static cmd_tbl_t cmd_efidebug_sub[] = { |
| 819 | 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] | 820 | U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices, |
| 821 | "", ""), |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 822 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
| 823 | "", ""), |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame^] | 824 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
| 825 | "", ""), |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | /** |
| 829 | * do_efidebug() - display and configure UEFI environment |
| 830 | * |
| 831 | * @cmdtp: Command table |
| 832 | * @flag: Command flag |
| 833 | * @argc: Number of arguments |
| 834 | * @argv: Argument array |
| 835 | * Return: CMD_RET_SUCCESS on success, |
| 836 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 837 | * |
| 838 | * Implement efidebug command which allows us to display and |
| 839 | * configure UEFI environment. |
| 840 | * See above for details of sub-commands. |
| 841 | */ |
| 842 | static int do_efidebug(cmd_tbl_t *cmdtp, int flag, |
| 843 | int argc, char * const argv[]) |
| 844 | { |
| 845 | cmd_tbl_t *cp; |
| 846 | efi_status_t r; |
| 847 | |
| 848 | if (argc < 2) |
| 849 | return CMD_RET_USAGE; |
| 850 | |
| 851 | argc--; argv++; |
| 852 | |
| 853 | /* Initialize UEFI drivers */ |
| 854 | r = efi_init_obj_list(); |
| 855 | if (r != EFI_SUCCESS) { |
| 856 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 857 | r & ~EFI_ERROR_MASK); |
| 858 | return CMD_RET_FAILURE; |
| 859 | } |
| 860 | |
| 861 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, |
| 862 | ARRAY_SIZE(cmd_efidebug_sub)); |
| 863 | if (!cp) |
| 864 | return CMD_RET_USAGE; |
| 865 | |
| 866 | return cp->cmd(cmdtp, flag, argc, argv); |
| 867 | } |
| 868 | |
| 869 | #ifdef CONFIG_SYS_LONGHELP |
| 870 | static char efidebug_help_text[] = |
| 871 | " - UEFI Shell-like interface to configure UEFI environment\n" |
| 872 | "\n" |
| 873 | "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n" |
| 874 | " - set UEFI BootXXXX variable\n" |
| 875 | " <load options> will be passed to UEFI application\n" |
| 876 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" |
| 877 | " - delete UEFI BootXXXX variables\n" |
| 878 | "efidebug boot dump\n" |
| 879 | " - dump all UEFI BootXXXX variables\n" |
| 880 | "efidebug boot next <bootid>\n" |
| 881 | " - set UEFI BootNext variable\n" |
| 882 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" |
| 883 | " - set/show UEFI boot order\n" |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 884 | "\n" |
| 885 | "efidebug devices\n" |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 886 | " - show uefi devices\n" |
| 887 | "efidebug drivers\n" |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame^] | 888 | " - show uefi drivers\n" |
| 889 | "efidebug dh\n" |
| 890 | " - show uefi handles\n"; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 891 | #endif |
| 892 | |
| 893 | U_BOOT_CMD( |
| 894 | efidebug, 10, 0, do_efidebug, |
| 895 | "Configure UEFI environment", |
| 896 | efidebug_help_text |
| 897 | ); |