efi_loader: fix 'efidebug boot dump'
[oweals/u-boot.git] / cmd / efidebug.c
index 21dfd44fcc94f9b2a67af660aadb9e1890b21bb6..7ff2ce4ce1c27d29a7949487a3971c4b7b2b9be2 100644 (file)
@@ -12,6 +12,7 @@
 #include <exports.h>
 #include <hexdump.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <search.h>
 #include <linux/ctype.h>
 
@@ -488,9 +489,12 @@ static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
 
                printf("%-16s %.*llx-%.*llx", type,
                       EFI_PHYS_ADDR_WIDTH,
-                      map->physical_start,
+                      (u64)map_to_sysmem((void *)(uintptr_t)
+                                         map->physical_start),
                       EFI_PHYS_ADDR_WIDTH,
-                      map->physical_start + map->num_pages * EFI_PAGE_SIZE);
+                      (u64)map_to_sysmem((void *)(uintptr_t)
+                                         (map->physical_start +
+                                          map->num_pages * EFI_PAGE_SIZE)));
 
                print_memory_attributes(map->attribute);
                putc('\n');
@@ -649,7 +653,7 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
        int id, i;
        char *endp;
        char var_name[9];
-       u16 var_name16[9];
+       u16 var_name16[9], *p;
        efi_status_t ret;
 
        if (argc == 1)
@@ -662,11 +666,12 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
                        return CMD_RET_FAILURE;
 
                sprintf(var_name, "Boot%04X", id);
-               utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
+               p = var_name16;
+               utf8_utf16_strncpy(&p, var_name, 9);
 
                ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
                if (ret) {
-                       printf("Cannot remove Boot%04X", id);
+                       printf("Cannot remove %ls\n", var_name16);
                        return CMD_RET_FAILURE;
                }
        }
@@ -677,13 +682,13 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
 /**
  * show_efi_boot_opt_data() - dump UEFI load option
  *
- * @id:                load option number
+ * @varname16: variable name
  * @data:      value of UEFI load option variable
  * @size:      size of the boot option
  *
  * Decode the value of UEFI load option variable and print information.
  */
-static void show_efi_boot_opt_data(int id, void *data, size_t size)
+static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t size)
 {
        struct efi_load_option lo;
        char *label, *p;
@@ -700,8 +705,8 @@ static void show_efi_boot_opt_data(int id, void *data, size_t size)
        p = label;
        utf16_utf8_strncpy(&p, lo.label, label_len16);
 
-       printf("Boot%04X:\n", id);
-       printf("  attributes: %c%c%c (0x%08x)\n",
+       printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
+              varname16,
               /* ACTIVE */
               lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
               /* FORCE RECONNECT */
@@ -725,37 +730,32 @@ static void show_efi_boot_opt_data(int id, void *data, size_t size)
 /**
  * show_efi_boot_opt() - dump UEFI load option
  *
- * @id:                Load option number
+ * @varname16: variable name
  *
  * Dump information defined by UEFI load option.
  */
-static void show_efi_boot_opt(int id)
+static void show_efi_boot_opt(u16 *varname16)
 {
-       char var_name[9];
-       u16 var_name16[9], *p;
-       efi_guid_t guid;
-       void *data = NULL;
+       void *data;
        efi_uintn_t size;
        efi_status_t ret;
 
-       sprintf(var_name, "Boot%04X", id);
-       p = var_name16;
-       utf8_utf16_strncpy(&p, var_name, 9);
-       guid = efi_global_variable_guid;
-
        size = 0;
-       ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
+       ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
+                                       NULL, &size, NULL));
        if (ret == EFI_BUFFER_TOO_SMALL) {
                data = malloc(size);
-               ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
-                                               data));
+               if (!data) {
+                       printf("ERROR: Out of memory\n");
+                       return;
+               }
+               ret = EFI_CALL(efi_get_variable(varname16,
+                                               &efi_global_variable_guid,
+                                               NULL, &size, data));
+               if (ret == EFI_SUCCESS)
+                       show_efi_boot_opt_data(varname16, data, size);
+               free(data);
        }
-       if (ret == EFI_SUCCESS)
-               show_efi_boot_opt_data(id, data, size);
-       else if (ret == EFI_NOT_FOUND)
-               printf("Boot%04X: not found\n", id);
-
-       free(data);
 }
 
 static int u16_tohex(u16 c)
@@ -834,7 +834,7 @@ static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
                        id = (id << 4) + digit;
                }
                if (i == 4 && !var_name16[8])
-                       show_efi_boot_opt(id);
+                       show_efi_boot_opt(var_name16);
        }
 
        free(var_name16);
@@ -1084,6 +1084,78 @@ static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
        return cp->cmd(cmdtp, flag, argc, argv);
 }
 
+/**
+ * do_efi_test_bootmgr() - run simple bootmgr for test
+ *
+ * @cmdtp:     Command table
+ * @flag:      Command flag
+ * @argc:      Number of arguments
+ * @argv:      Argument array
+ * Return:     CMD_RET_SUCCESS on success,
+ *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "test bootmgr" sub-command.
+ * Run simple bootmgr for test.
+ *
+ *     efidebug test bootmgr
+ */
+static int do_efi_test_bootmgr(cmd_tbl_t *cmdtp, int flag,
+                              int argc, char * const argv[])
+{
+       efi_handle_t image;
+       efi_uintn_t exit_data_size = 0;
+       u16 *exit_data = NULL;
+       efi_status_t ret;
+
+       ret = efi_bootmgr_load(&image);
+       printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
+
+       /* We call efi_start_image() even if error for test purpose. */
+       ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
+       printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
+       if (ret && exit_data)
+               efi_free_pool(exit_data);
+
+       efi_restore_gd();
+
+       return CMD_RET_SUCCESS;
+}
+
+static cmd_tbl_t cmd_efidebug_test_sub[] = {
+       U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
+                        "", ""),
+};
+
+/**
+ * do_efi_test() - manage UEFI load options
+ *
+ * @cmdtp:     Command table
+ * @flag:      Command flag
+ * @argc:      Number of arguments
+ * @argv:      Argument array
+ * Return:     CMD_RET_SUCCESS on success,
+ *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "test" sub-command.
+ */
+static int do_efi_test(cmd_tbl_t *cmdtp, int flag,
+                      int argc, char * const argv[])
+{
+       cmd_tbl_t *cp;
+
+       if (argc < 2)
+               return CMD_RET_USAGE;
+
+       argc--; argv++;
+
+       cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
+                         ARRAY_SIZE(cmd_efidebug_test_sub));
+       if (!cp)
+               return CMD_RET_USAGE;
+
+       return cp->cmd(cmdtp, flag, argc, argv);
+}
+
 static cmd_tbl_t cmd_efidebug_sub[] = {
        U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
        U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
@@ -1098,6 +1170,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
                         "", ""),
        U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
                         "", ""),
+       U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
+                        "", ""),
 };
 
 /**
@@ -1167,7 +1241,9 @@ static char efidebug_help_text[] =
        "efidebug memmap\n"
        "  - show UEFI memory map\n"
        "efidebug tables\n"
-       "  - show UEFI configuration tables\n";
+       "  - show UEFI configuration tables\n"
+       "efidebug test bootmgr\n"
+       "  - run simple bootmgr for test\n";
 #endif
 
 U_BOOT_CMD(