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