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