Merge tag 'u-boot-rockchip-20200501' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/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 <malloc.h>
15 #include <mapmem.h>
16 #include <search.h>
17 #include <linux/ctype.h>
18
19 #define BS systab.boottime
20 #define RT systab.runtime
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(cmd_tbl_t *cmdtp, int flag,
65                                int argc, char * const argv[])
66 {
67         efi_handle_t *handles;
68         efi_uintn_t num, i;
69         u16 *dev_path_text;
70         efi_status_t ret;
71
72         ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
73                                                 &num, &handles));
74         if (ret != EFI_SUCCESS)
75                 return CMD_RET_FAILURE;
76
77         if (!num)
78                 return CMD_RET_SUCCESS;
79
80         printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
81         printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
82         for (i = 0; i < num; i++) {
83                 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
84                         printf("%p %ls\n", handles[i], dev_path_text);
85                         efi_free_pool(dev_path_text);
86                 }
87         }
88
89         EFI_CALL(BS->free_pool(handles));
90
91         return CMD_RET_SUCCESS;
92 }
93
94 /**
95  * efi_get_driver_handle_info() - get information of UEFI driver
96  *
97  * @handle:             Handle of UEFI device
98  * @driver_name:        Driver name
99  * @image_path:         Pointer to text of device path
100  * Return:              0 on success, -1 on failure
101  *
102  * Currently return no useful information as all UEFI drivers are
103  * built-in..
104  */
105 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
106                                       u16 **image_path)
107 {
108         struct efi_handler *handler;
109         struct efi_loaded_image *image;
110         efi_status_t ret;
111
112         /*
113          * driver name
114          * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
115          */
116         *driver_name = NULL;
117
118         /* image name */
119         ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
120         if (ret != EFI_SUCCESS) {
121                 *image_path = NULL;
122                 return 0;
123         }
124
125         image = handler->protocol_interface;
126         *image_path = efi_dp_str(image->file_path);
127
128         return 0;
129 }
130
131 /**
132  * do_efi_show_drivers() - show UEFI drivers
133  *
134  * @cmdtp:      Command table
135  * @flag:       Command flag
136  * @argc:       Number of arguments
137  * @argv:       Argument array
138  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
139  *
140  * Implement efidebug "drivers" sub-command.
141  * Show all UEFI drivers and their information.
142  */
143 static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
144                                int argc, char * const argv[])
145 {
146         efi_handle_t *handles;
147         efi_uintn_t num, i;
148         u16 *driver_name, *image_path_text;
149         efi_status_t ret;
150
151         ret = EFI_CALL(BS->locate_handle_buffer(
152                                 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
153                                 NULL, &num, &handles));
154         if (ret != EFI_SUCCESS)
155                 return CMD_RET_FAILURE;
156
157         if (!num)
158                 return CMD_RET_SUCCESS;
159
160         printf("Driver%.*s Name                 Image Path\n",
161                EFI_HANDLE_WIDTH - 6, spc);
162         printf("%.*s ==================== ====================\n",
163                EFI_HANDLE_WIDTH, sep);
164         for (i = 0; i < num; i++) {
165                 if (!efi_get_driver_handle_info(handles[i], &driver_name,
166                                                 &image_path_text)) {
167                         if (image_path_text)
168                                 printf("%p %-20ls %ls\n", handles[i],
169                                        driver_name, image_path_text);
170                         else
171                                 printf("%p %-20ls <built-in>\n",
172                                        handles[i], driver_name);
173                         EFI_CALL(BS->free_pool(driver_name));
174                         EFI_CALL(BS->free_pool(image_path_text));
175                 }
176         }
177
178         EFI_CALL(BS->free_pool(handles));
179
180         return CMD_RET_SUCCESS;
181 }
182
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(cmd_tbl_t *cmdtp, int flag,
316                                int argc, char * const argv[])
317 {
318         efi_handle_t *handles;
319         efi_guid_t **guid;
320         efi_uintn_t num, count, i, j;
321         const char *guid_text;
322         efi_status_t ret;
323
324         ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
325                                                 &num, &handles));
326         if (ret != EFI_SUCCESS)
327                 return CMD_RET_FAILURE;
328
329         if (!num)
330                 return CMD_RET_SUCCESS;
331
332         printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
333         printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
334         for (i = 0; i < num; i++) {
335                 printf("%p", handles[i]);
336                 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
337                                                         &count));
338                 if (ret || !count) {
339                         putc('\n');
340                         continue;
341                 }
342
343                 for (j = 0; j < count; j++) {
344                         if (j)
345                                 printf(", ");
346                         else
347                                 putc(' ');
348
349                         guid_text = get_guid_text(guid[j]);
350                         if (guid_text)
351                                 puts(guid_text);
352                         else
353                                 printf("%pUl", guid[j]);
354                 }
355                 putc('\n');
356         }
357
358         EFI_CALL(BS->free_pool(handles));
359
360         return CMD_RET_SUCCESS;
361 }
362
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(cmd_tbl_t *cmdtp, int flag,
376                               int argc, char * const argv[])
377 {
378         efi_print_image_infos(NULL);
379
380         return CMD_RET_SUCCESS;
381 }
382
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_RUNTIME, "RT"},
418 };
419
420 /**
421  * print_memory_attributes() - print memory map attributes
422  *
423  * @attributes: Attribute value
424  *
425  * Print memory map attributes
426  */
427 static void print_memory_attributes(u64 attributes)
428 {
429         int sep, i;
430
431         for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
432                 if (attributes & efi_mem_attrs[i].bit) {
433                         if (sep) {
434                                 putc('|');
435                         } else {
436                                 putc(' ');
437                                 sep = 1;
438                         }
439                         puts(efi_mem_attrs[i].text);
440                 }
441 }
442
443 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
444
445 /**
446  * do_efi_show_memmap() - show UEFI memory map
447  *
448  * @cmdtp:      Command table
449  * @flag:       Command flag
450  * @argc:       Number of arguments
451  * @argv:       Argument array
452  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
453  *
454  * Implement efidebug "memmap" sub-command.
455  * Show UEFI memory map.
456  */
457 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
458                               int argc, char * const argv[])
459 {
460         struct efi_mem_desc *memmap = NULL, *map;
461         efi_uintn_t map_size = 0;
462         const char *type;
463         int i;
464         efi_status_t ret;
465
466         ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
467         if (ret == EFI_BUFFER_TOO_SMALL) {
468                 map_size += sizeof(struct efi_mem_desc); /* for my own */
469                 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
470                                                  map_size, (void *)&memmap));
471                 if (ret != EFI_SUCCESS)
472                         return CMD_RET_FAILURE;
473                 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
474                                                   NULL, NULL, NULL));
475         }
476         if (ret != EFI_SUCCESS) {
477                 EFI_CALL(BS->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         for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
486                 if (map->type < ARRAY_SIZE(efi_mem_type_string))
487                         type = efi_mem_type_string[map->type];
488                 else
489                         type = "(unknown)";
490
491                 printf("%-16s %.*llx-%.*llx", type,
492                        EFI_PHYS_ADDR_WIDTH,
493                        (u64)map_to_sysmem((void *)(uintptr_t)
494                                           map->physical_start),
495                        EFI_PHYS_ADDR_WIDTH,
496                        (u64)map_to_sysmem((void *)(uintptr_t)
497                                           (map->physical_start +
498                                            map->num_pages * EFI_PAGE_SIZE)));
499
500                 print_memory_attributes(map->attribute);
501                 putc('\n');
502         }
503
504         EFI_CALL(BS->free_pool(memmap));
505
506         return CMD_RET_SUCCESS;
507 }
508
509 /**
510  * do_efi_show_tables() - show UEFI configuration tables
511  *
512  * @cmdtp:      Command table
513  * @flag:       Command flag
514  * @argc:       Number of arguments
515  * @argv:       Argument array
516  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
517  *
518  * Implement efidebug "tables" sub-command.
519  * Show UEFI configuration tables.
520  */
521 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
522                               int argc, char * const argv[])
523 {
524         efi_uintn_t i;
525         const char *guid_str;
526
527         for (i = 0; i < systab.nr_tables; ++i) {
528                 guid_str = get_guid_text(&systab.tables[i].guid);
529                 if (!guid_str)
530                         guid_str = "";
531                 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
532         }
533
534         return CMD_RET_SUCCESS;
535 }
536
537 /**
538  * do_efi_boot_add() - set UEFI load option
539  *
540  * @cmdtp:      Command table
541  * @flag:       Command flag
542  * @argc:       Number of arguments
543  * @argv:       Argument array
544  * Return:      CMD_RET_SUCCESS on success,
545  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
546  *
547  * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
548  *
549  *     efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
550  */
551 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
552                            int argc, char * const argv[])
553 {
554         int id;
555         char *endp;
556         char var_name[9];
557         u16 var_name16[9], *p;
558         efi_guid_t guid;
559         size_t label_len, label_len16;
560         u16 *label;
561         struct efi_device_path *device_path = NULL, *file_path = NULL;
562         struct efi_load_option lo;
563         void *data = NULL;
564         efi_uintn_t size;
565         efi_status_t ret;
566         int r = CMD_RET_SUCCESS;
567
568         if (argc < 6 || argc > 7)
569                 return CMD_RET_USAGE;
570
571         id = (int)simple_strtoul(argv[1], &endp, 16);
572         if (*endp != '\0' || id > 0xffff)
573                 return CMD_RET_USAGE;
574
575         sprintf(var_name, "Boot%04X", id);
576         p = var_name16;
577         utf8_utf16_strncpy(&p, var_name, 9);
578
579         guid = efi_global_variable_guid;
580
581         /* attributes */
582         lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
583
584         /* label */
585         label_len = strlen(argv[2]);
586         label_len16 = utf8_utf16_strnlen(argv[2], label_len);
587         label = malloc((label_len16 + 1) * sizeof(u16));
588         if (!label)
589                 return CMD_RET_FAILURE;
590         lo.label = label; /* label will be changed below */
591         utf8_utf16_strncpy(&label, argv[2], label_len);
592
593         /* file path */
594         ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
595                                &file_path);
596         if (ret != EFI_SUCCESS) {
597                 printf("Cannot create device path for \"%s %s\"\n",
598                        argv[3], argv[4]);
599                 r = CMD_RET_FAILURE;
600                 goto out;
601         }
602         lo.file_path = file_path;
603         lo.file_path_length = efi_dp_size(file_path)
604                                 + sizeof(struct efi_device_path); /* for END */
605
606         /* optional data */
607         if (argc < 6)
608                 lo.optional_data = NULL;
609         else
610                 lo.optional_data = (const u8 *)argv[6];
611
612         size = efi_serialize_load_option(&lo, (u8 **)&data);
613         if (!size) {
614                 r = CMD_RET_FAILURE;
615                 goto out;
616         }
617
618         ret = EFI_CALL(RT->set_variable(var_name16, &guid,
619                                         EFI_VARIABLE_NON_VOLATILE |
620                                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
621                                         EFI_VARIABLE_RUNTIME_ACCESS,
622                                         size, data));
623         if (ret != EFI_SUCCESS) {
624                 printf("Cannot set %ls\n", var_name16);
625                 r = CMD_RET_FAILURE;
626         }
627 out:
628         free(data);
629         efi_free_pool(device_path);
630         efi_free_pool(file_path);
631         free(lo.label);
632
633         return r;
634 }
635
636 /**
637  * do_efi_boot_rm() - delete UEFI load options
638  *
639  * @cmdtp:      Command table
640  * @flag:       Command flag
641  * @argc:       Number of arguments
642  * @argv:       Argument array
643  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
644  *
645  * Implement efidebug "boot rm" sub-command.
646  * Delete UEFI load options.
647  *
648  *     efidebug boot rm <id> ...
649  */
650 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
651                           int argc, char * const argv[])
652 {
653         efi_guid_t guid;
654         int id, i;
655         char *endp;
656         char var_name[9];
657         u16 var_name16[9], *p;
658         efi_status_t ret;
659
660         if (argc == 1)
661                 return CMD_RET_USAGE;
662
663         guid = efi_global_variable_guid;
664         for (i = 1; i < argc; i++, argv++) {
665                 id = (int)simple_strtoul(argv[1], &endp, 16);
666                 if (*endp != '\0' || id > 0xffff)
667                         return CMD_RET_FAILURE;
668
669                 sprintf(var_name, "Boot%04X", id);
670                 p = var_name16;
671                 utf8_utf16_strncpy(&p, var_name, 9);
672
673                 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
674                 if (ret) {
675                         printf("Cannot remove %ls\n", var_name16);
676                         return CMD_RET_FAILURE;
677                 }
678         }
679
680         return CMD_RET_SUCCESS;
681 }
682
683 /**
684  * show_efi_boot_opt_data() - dump UEFI load option
685  *
686  * @varname16:  variable name
687  * @data:       value of UEFI load option variable
688  * @size:       size of the boot option
689  *
690  * Decode the value of UEFI load option variable and print information.
691  */
692 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t size)
693 {
694         struct efi_load_option lo;
695         char *label, *p;
696         size_t label_len16, label_len;
697         u16 *dp_str;
698
699         efi_deserialize_load_option(&lo, data);
700
701         label_len16 = u16_strlen(lo.label);
702         label_len = utf16_utf8_strnlen(lo.label, label_len16);
703         label = malloc(label_len + 1);
704         if (!label)
705                 return;
706         p = label;
707         utf16_utf8_strncpy(&p, lo.label, label_len16);
708
709         printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
710                varname16,
711                /* ACTIVE */
712                lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
713                /* FORCE RECONNECT */
714                lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
715                /* HIDDEN */
716                lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
717                lo.attributes);
718         printf("  label: %s\n", label);
719
720         dp_str = efi_dp_str(lo.file_path);
721         printf("  file_path: %ls\n", dp_str);
722         efi_free_pool(dp_str);
723
724         printf("  data:\n");
725         print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1,
726                        lo.optional_data, size + (u8 *)data -
727                        (u8 *)lo.optional_data, true);
728         free(label);
729 }
730
731 /**
732  * show_efi_boot_opt() - dump UEFI load option
733  *
734  * @varname16:  variable name
735  *
736  * Dump information defined by UEFI load option.
737  */
738 static void show_efi_boot_opt(u16 *varname16)
739 {
740         void *data;
741         efi_uintn_t size;
742         efi_status_t ret;
743
744         size = 0;
745         ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
746                                         NULL, &size, NULL));
747         if (ret == EFI_BUFFER_TOO_SMALL) {
748                 data = malloc(size);
749                 if (!data) {
750                         printf("ERROR: Out of memory\n");
751                         return;
752                 }
753                 ret = EFI_CALL(efi_get_variable(varname16,
754                                                 &efi_global_variable_guid,
755                                                 NULL, &size, data));
756                 if (ret == EFI_SUCCESS)
757                         show_efi_boot_opt_data(varname16, data, size);
758                 free(data);
759         }
760 }
761
762 static int u16_tohex(u16 c)
763 {
764         if (c >= '0' && c <= '9')
765                 return c - '0';
766         if (c >= 'A' && c <= 'F')
767                 return c - 'A' + 10;
768
769         /* not hexadecimal */
770         return -1;
771 }
772
773 /**
774  * show_efi_boot_dump() - dump all UEFI load options
775  *
776  * @cmdtp:      Command table
777  * @flag:       Command flag
778  * @argc:       Number of arguments
779  * @argv:       Argument array
780  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
781  *
782  * Implement efidebug "boot dump" sub-command.
783  * Dump information of all UEFI load options defined.
784  *
785  *     efidebug boot dump
786  */
787 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
788                             int argc, char * const argv[])
789 {
790         u16 *var_name16, *p;
791         efi_uintn_t buf_size, size;
792         efi_guid_t guid;
793         int id, i, digit;
794         efi_status_t ret;
795
796         if (argc > 1)
797                 return CMD_RET_USAGE;
798
799         buf_size = 128;
800         var_name16 = malloc(buf_size);
801         if (!var_name16)
802                 return CMD_RET_FAILURE;
803
804         var_name16[0] = 0;
805         for (;;) {
806                 size = buf_size;
807                 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
808                                                           &guid));
809                 if (ret == EFI_NOT_FOUND)
810                         break;
811                 if (ret == EFI_BUFFER_TOO_SMALL) {
812                         buf_size = size;
813                         p = realloc(var_name16, buf_size);
814                         if (!p) {
815                                 free(var_name16);
816                                 return CMD_RET_FAILURE;
817                         }
818                         var_name16 = p;
819                         ret = EFI_CALL(efi_get_next_variable_name(&size,
820                                                                   var_name16,
821                                                                   &guid));
822                 }
823                 if (ret != EFI_SUCCESS) {
824                         free(var_name16);
825                         return CMD_RET_FAILURE;
826                 }
827
828                 if (memcmp(var_name16, L"Boot", 8))
829                         continue;
830
831                 for (id = 0, i = 0; i < 4; i++) {
832                         digit = u16_tohex(var_name16[4 + i]);
833                         if (digit < 0)
834                                 break;
835                         id = (id << 4) + digit;
836                 }
837                 if (i == 4 && !var_name16[8])
838                         show_efi_boot_opt(var_name16);
839         }
840
841         free(var_name16);
842
843         return CMD_RET_SUCCESS;
844 }
845
846 /**
847  * show_efi_boot_order() - show order of UEFI load options
848  *
849  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
850  *
851  * Show order of UEFI load options defined by BootOrder variable.
852  */
853 static int show_efi_boot_order(void)
854 {
855         u16 *bootorder;
856         efi_uintn_t size;
857         int num, i;
858         char var_name[9];
859         u16 var_name16[9], *p16;
860         void *data;
861         struct efi_load_option lo;
862         char *label, *p;
863         size_t label_len16, label_len;
864         efi_status_t ret;
865
866         size = 0;
867         ret = EFI_CALL(RT->get_variable(L"BootOrder", &efi_global_variable_guid,
868                                         NULL, &size, NULL));
869         if (ret != EFI_BUFFER_TOO_SMALL) {
870                 if (ret == EFI_NOT_FOUND) {
871                         printf("BootOrder not defined\n");
872                         return CMD_RET_SUCCESS;
873                 } else {
874                         return CMD_RET_FAILURE;
875                 }
876         }
877         bootorder = malloc(size);
878         if (!bootorder) {
879                 printf("ERROR: Out of memory\n");
880                 return CMD_RET_FAILURE;
881         }
882         ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
883                                         NULL, &size, bootorder));
884         if (ret != EFI_SUCCESS) {
885                 ret = CMD_RET_FAILURE;
886                 goto out;
887         }
888
889         num = size / sizeof(u16);
890         for (i = 0; i < num; i++) {
891                 sprintf(var_name, "Boot%04X", bootorder[i]);
892                 p16 = var_name16;
893                 utf8_utf16_strncpy(&p16, var_name, 9);
894
895                 size = 0;
896                 ret = EFI_CALL(efi_get_variable(var_name16,
897                                                 &efi_global_variable_guid, NULL,
898                                                 &size, NULL));
899                 if (ret != EFI_BUFFER_TOO_SMALL) {
900                         printf("%2d: %s: (not defined)\n", i + 1, var_name);
901                         continue;
902                 }
903
904                 data = malloc(size);
905                 if (!data) {
906                         ret = CMD_RET_FAILURE;
907                         goto out;
908                 }
909                 ret = EFI_CALL(efi_get_variable(var_name16,
910                                                 &efi_global_variable_guid, NULL,
911                                                 &size, data));
912                 if (ret != EFI_SUCCESS) {
913                         free(data);
914                         ret = CMD_RET_FAILURE;
915                         goto out;
916                 }
917
918                 efi_deserialize_load_option(&lo, data);
919
920                 label_len16 = u16_strlen(lo.label);
921                 label_len = utf16_utf8_strnlen(lo.label, label_len16);
922                 label = malloc(label_len + 1);
923                 if (!label) {
924                         free(data);
925                         ret = CMD_RET_FAILURE;
926                         goto out;
927                 }
928                 p = label;
929                 utf16_utf8_strncpy(&p, lo.label, label_len16);
930                 printf("%2d: %s: %s\n", i + 1, var_name, label);
931                 free(label);
932
933                 free(data);
934         }
935 out:
936         free(bootorder);
937
938         return ret;
939 }
940
941 /**
942  * do_efi_boot_next() - manage UEFI BootNext variable
943  *
944  * @cmdtp:      Command table
945  * @flag:       Command flag
946  * @argc:       Number of arguments
947  * @argv:       Argument array
948  * Return:      CMD_RET_SUCCESS on success,
949  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
950  *
951  * Implement efidebug "boot next" sub-command.
952  * Set BootNext variable.
953  *
954  *     efidebug boot next <id>
955  */
956 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
957                             int argc, char * const argv[])
958 {
959         u16 bootnext;
960         efi_uintn_t size;
961         char *endp;
962         efi_guid_t guid;
963         efi_status_t ret;
964         int r = CMD_RET_SUCCESS;
965
966         if (argc != 2)
967                 return CMD_RET_USAGE;
968
969         bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
970         if (*endp != '\0' || bootnext > 0xffff) {
971                 printf("invalid value: %s\n", argv[1]);
972                 r = CMD_RET_FAILURE;
973                 goto out;
974         }
975
976         guid = efi_global_variable_guid;
977         size = sizeof(u16);
978         ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
979                                         EFI_VARIABLE_NON_VOLATILE |
980                                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
981                                         EFI_VARIABLE_RUNTIME_ACCESS,
982                                         size, &bootnext));
983         if (ret != EFI_SUCCESS) {
984                 printf("Cannot set BootNext\n");
985                 r = CMD_RET_FAILURE;
986         }
987 out:
988         return r;
989 }
990
991 /**
992  * do_efi_boot_order() - manage UEFI BootOrder variable
993  *
994  * @cmdtp:      Command table
995  * @flag:       Command flag
996  * @argc:       Number of arguments
997  * @argv:       Argument array
998  * Return:      CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
999  *
1000  * Implement efidebug "boot order" sub-command.
1001  * Show order of UEFI load options, or change it in BootOrder variable.
1002  *
1003  *     efidebug boot order [<id> ...]
1004  */
1005 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
1006                              int argc, char * const argv[])
1007 {
1008         u16 *bootorder = NULL;
1009         efi_uintn_t size;
1010         int id, i;
1011         char *endp;
1012         efi_guid_t guid;
1013         efi_status_t ret;
1014         int r = CMD_RET_SUCCESS;
1015
1016         if (argc == 1)
1017                 return show_efi_boot_order();
1018
1019         argc--;
1020         argv++;
1021
1022         size = argc * sizeof(u16);
1023         bootorder = malloc(size);
1024         if (!bootorder)
1025                 return CMD_RET_FAILURE;
1026
1027         for (i = 0; i < argc; i++) {
1028                 id = (int)simple_strtoul(argv[i], &endp, 16);
1029                 if (*endp != '\0' || id > 0xffff) {
1030                         printf("invalid value: %s\n", argv[i]);
1031                         r = CMD_RET_FAILURE;
1032                         goto out;
1033                 }
1034
1035                 bootorder[i] = (u16)id;
1036         }
1037
1038         guid = efi_global_variable_guid;
1039         ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
1040                                         EFI_VARIABLE_NON_VOLATILE |
1041                                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
1042                                         EFI_VARIABLE_RUNTIME_ACCESS,
1043                                         size, bootorder));
1044         if (ret != EFI_SUCCESS) {
1045                 printf("Cannot set BootOrder\n");
1046                 r = CMD_RET_FAILURE;
1047         }
1048 out:
1049         free(bootorder);
1050
1051         return r;
1052 }
1053
1054 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1055         U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1056         U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1057         U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1058         U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1059         U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1060                          "", ""),
1061 };
1062
1063 /**
1064  * do_efi_boot_opt() - manage UEFI load options
1065  *
1066  * @cmdtp:      Command table
1067  * @flag:       Command flag
1068  * @argc:       Number of arguments
1069  * @argv:       Argument array
1070  * Return:      CMD_RET_SUCCESS on success,
1071  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1072  *
1073  * Implement efidebug "boot" sub-command.
1074  */
1075 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1076                            int argc, char * const argv[])
1077 {
1078         cmd_tbl_t *cp;
1079
1080         if (argc < 2)
1081                 return CMD_RET_USAGE;
1082
1083         argc--; argv++;
1084
1085         cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1086                           ARRAY_SIZE(cmd_efidebug_boot_sub));
1087         if (!cp)
1088                 return CMD_RET_USAGE;
1089
1090         return cp->cmd(cmdtp, flag, argc, argv);
1091 }
1092
1093 /**
1094  * do_efi_test_bootmgr() - run simple bootmgr for test
1095  *
1096  * @cmdtp:      Command table
1097  * @flag:       Command flag
1098  * @argc:       Number of arguments
1099  * @argv:       Argument array
1100  * Return:      CMD_RET_SUCCESS on success,
1101  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1102  *
1103  * Implement efidebug "test bootmgr" sub-command.
1104  * Run simple bootmgr for test.
1105  *
1106  *     efidebug test bootmgr
1107  */
1108 static int do_efi_test_bootmgr(cmd_tbl_t *cmdtp, int flag,
1109                                int argc, char * const argv[])
1110 {
1111         efi_handle_t image;
1112         efi_uintn_t exit_data_size = 0;
1113         u16 *exit_data = NULL;
1114         efi_status_t ret;
1115
1116         ret = efi_bootmgr_load(&image);
1117         printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1118
1119         /* We call efi_start_image() even if error for test purpose. */
1120         ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1121         printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1122         if (ret && exit_data)
1123                 efi_free_pool(exit_data);
1124
1125         efi_restore_gd();
1126
1127         return CMD_RET_SUCCESS;
1128 }
1129
1130 static cmd_tbl_t cmd_efidebug_test_sub[] = {
1131         U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1132                          "", ""),
1133 };
1134
1135 /**
1136  * do_efi_test() - manage UEFI load options
1137  *
1138  * @cmdtp:      Command table
1139  * @flag:       Command flag
1140  * @argc:       Number of arguments
1141  * @argv:       Argument array
1142  * Return:      CMD_RET_SUCCESS on success,
1143  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1144  *
1145  * Implement efidebug "test" sub-command.
1146  */
1147 static int do_efi_test(cmd_tbl_t *cmdtp, int flag,
1148                        int argc, char * const argv[])
1149 {
1150         cmd_tbl_t *cp;
1151
1152         if (argc < 2)
1153                 return CMD_RET_USAGE;
1154
1155         argc--; argv++;
1156
1157         cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1158                           ARRAY_SIZE(cmd_efidebug_test_sub));
1159         if (!cp)
1160                 return CMD_RET_USAGE;
1161
1162         return cp->cmd(cmdtp, flag, argc, argv);
1163 }
1164
1165 static cmd_tbl_t cmd_efidebug_sub[] = {
1166         U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1167         U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1168                          "", ""),
1169         U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1170                          "", ""),
1171         U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1172                          "", ""),
1173         U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1174                          "", ""),
1175         U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1176                          "", ""),
1177         U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1178                          "", ""),
1179         U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1180                          "", ""),
1181 };
1182
1183 /**
1184  * do_efidebug() - display and configure UEFI environment
1185  *
1186  * @cmdtp:      Command table
1187  * @flag:       Command flag
1188  * @argc:       Number of arguments
1189  * @argv:       Argument array
1190  * Return:      CMD_RET_SUCCESS on success,
1191  *              CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1192  *
1193  * Implement efidebug command which allows us to display and
1194  * configure UEFI environment.
1195  */
1196 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1197                        int argc, char * const argv[])
1198 {
1199         cmd_tbl_t *cp;
1200         efi_status_t r;
1201
1202         if (argc < 2)
1203                 return CMD_RET_USAGE;
1204
1205         argc--; argv++;
1206
1207         /* Initialize UEFI drivers */
1208         r = efi_init_obj_list();
1209         if (r != EFI_SUCCESS) {
1210                 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1211                        r & ~EFI_ERROR_MASK);
1212                 return CMD_RET_FAILURE;
1213         }
1214
1215         cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1216                           ARRAY_SIZE(cmd_efidebug_sub));
1217         if (!cp)
1218                 return CMD_RET_USAGE;
1219
1220         return cp->cmd(cmdtp, flag, argc, argv);
1221 }
1222
1223 #ifdef CONFIG_SYS_LONGHELP
1224 static char efidebug_help_text[] =
1225         "  - UEFI Shell-like interface to configure UEFI environment\n"
1226         "\n"
1227         "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1228         "  - set UEFI BootXXXX variable\n"
1229         "    <load options> will be passed to UEFI application\n"
1230         "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1231         "  - delete UEFI BootXXXX variables\n"
1232         "efidebug boot dump\n"
1233         "  - dump all UEFI BootXXXX variables\n"
1234         "efidebug boot next <bootid>\n"
1235         "  - set UEFI BootNext variable\n"
1236         "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1237         "  - set/show UEFI boot order\n"
1238         "\n"
1239         "efidebug devices\n"
1240         "  - show UEFI devices\n"
1241         "efidebug drivers\n"
1242         "  - show UEFI drivers\n"
1243         "efidebug dh\n"
1244         "  - show UEFI handles\n"
1245         "efidebug images\n"
1246         "  - show loaded images\n"
1247         "efidebug memmap\n"
1248         "  - show UEFI memory map\n"
1249         "efidebug tables\n"
1250         "  - show UEFI configuration tables\n"
1251         "efidebug test bootmgr\n"
1252         "  - run simple bootmgr for test\n";
1253 #endif
1254
1255 U_BOOT_CMD(
1256         efidebug, 10, 0, do_efidebug,
1257         "Configure UEFI environment",
1258         efidebug_help_text
1259 );