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