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