Merge branch 'next' of git://git.denx.de/u-boot-usb into next
[oweals/u-boot.git] / cmd / efidebug.c
index c4ac9dd634e2628fe19472a47fd435d0114fae7a..bb7c13d6a1ce7e636548aff1b64268a986b72b9d 100644 (file)
@@ -9,10 +9,10 @@
 #include <common.h>
 #include <command.h>
 #include <efi_loader.h>
-#include <environment.h>
 #include <exports.h>
 #include <hexdump.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <search.h>
 #include <linux/ctype.h>
 
@@ -244,6 +244,10 @@ static const struct {
                "HII Config Routing",
                EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
        },
+       {
+               "Load File2",
+               EFI_LOAD_FILE2_PROTOCOL_GUID,
+       },
        {
                "Simple Network",
                EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
@@ -252,27 +256,47 @@ static const struct {
                "PXE Base Code",
                EFI_PXE_BASE_CODE_PROTOCOL_GUID,
        },
+       /* Configuration table GUIDs */
+       {
+               "ACPI table",
+               EFI_ACPI_TABLE_GUID,
+       },
+       {
+               "device tree",
+               EFI_FDT_GUID,
+       },
+       {
+               "SMBIOS table",
+               SMBIOS_TABLE_GUID,
+       },
+       {
+               "Runtime properties",
+               EFI_RT_PROPERTIES_TABLE_GUID,
+       },
 };
 
 /**
- * get_guid_text - get string of protocol guid
- * @guid:      Protocol guid
- * Return:     String
+ * get_guid_text - get string of GUID
+ *
+ * Return description of GUID.
  *
- * Return string for display to represent the protocol.
+ * @guid:      GUID
+ * Return:     description of GUID or NULL
  */
-static const char *get_guid_text(const efi_guid_t *guid)
+static const char *get_guid_text(const void *guid)
 {
        int i;
 
-       for (i = 0; i < ARRAY_SIZE(guid_list); i++)
+       for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
+               /*
+                * As guidcmp uses memcmp() we can safely accept unaligned
+                * GUIDs.
+                */
                if (!guidcmp(&guid_list[i].guid, guid))
-                       break;
+                       return guid_list[i].text;
+       }
 
-       if (i != ARRAY_SIZE(guid_list))
-               return guid_list[i].text;
-       else
-               return NULL;
+       return NULL;
 }
 
 /**
@@ -394,6 +418,7 @@ static const struct efi_mem_attrs {
 
 /**
  * print_memory_attributes() - print memory map attributes
+ *
  * @attributes:        Attribute value
  *
  * Print memory map attributes
@@ -464,9 +489,10 @@ 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 *)map->physical_start),
                       EFI_PHYS_ADDR_WIDTH,
-                      map->physical_start + map->num_pages * EFI_PAGE_SIZE);
+                      (u64)map_to_sysmem((void *)map->physical_start +
+                                         map->num_pages * EFI_PAGE_SIZE));
 
                print_memory_attributes(map->attribute);
                putc('\n');
@@ -477,6 +503,34 @@ static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
        return CMD_RET_SUCCESS;
 }
 
+/**
+ * do_efi_show_tables() - show UEFI configuration tables
+ *
+ * @cmdtp:     Command table
+ * @flag:      Command flag
+ * @argc:      Number of arguments
+ * @argv:      Argument array
+ * Return:     CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "tables" sub-command.
+ * Show UEFI configuration tables.
+ */
+static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
+                             int argc, char * const argv[])
+{
+       efi_uintn_t i;
+       const char *guid_str;
+
+       for (i = 0; i < systab.nr_tables; ++i) {
+               guid_str = get_guid_text(&systab.tables[i].guid);
+               if (!guid_str)
+                       guid_str = "";
+               printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
 /**
  * do_efi_boot_add() - set UEFI load option
  *
@@ -487,9 +541,9 @@ static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
  * Return:     CMD_RET_SUCCESS on success,
  *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  *
- * Implement efidebug "boot add" sub-command.
- * Create or change UEFI load option.
- *   - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
+ * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
+ *
+ *     efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
  */
 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
                           int argc, char * const argv[])
@@ -505,7 +559,8 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
        struct efi_load_option lo;
        void *data = NULL;
        efi_uintn_t size;
-       int ret;
+       efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc < 6 || argc > 7)
                return CMD_RET_USAGE;
@@ -538,7 +593,7 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
        if (ret != EFI_SUCCESS) {
                printf("Cannot create device path for \"%s %s\"\n",
                       argv[3], argv[4]);
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
        lo.file_path = file_path;
@@ -553,22 +608,26 @@ static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
 
        size = efi_serialize_load_option(&lo, (u8 **)&data);
        if (!size) {
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
 
        ret = EFI_CALL(RT->set_variable(var_name16, &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, data));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set %ls\n", var_name16);
+               r = CMD_RET_FAILURE;
+       }
 out:
        free(data);
        efi_free_pool(device_path);
        efi_free_pool(file_path);
        free(lo.label);
 
-       return ret;
+       return r;
 }
 
 /**
@@ -582,7 +641,8 @@ out:
  *
  * Implement efidebug "boot rm" sub-command.
  * Delete UEFI load options.
- *   - boot rm <id> ...
+ *
+ *     efidebug boot rm <id> ...
  */
 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
                          int argc, char * const argv[])
@@ -591,7 +651,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)
@@ -604,11 +664,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;
                }
        }
@@ -678,7 +739,7 @@ static void show_efi_boot_opt(int id)
        efi_guid_t guid;
        void *data = NULL;
        efi_uintn_t size;
-       int ret;
+       efi_status_t ret;
 
        sprintf(var_name, "Boot%04X", id);
        p = var_name16;
@@ -687,7 +748,7 @@ static void show_efi_boot_opt(int id)
 
        size = 0;
        ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
-       if (ret == (int)EFI_BUFFER_TOO_SMALL) {
+       if (ret == EFI_BUFFER_TOO_SMALL) {
                data = malloc(size);
                ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
                                                data));
@@ -722,7 +783,8 @@ static int u16_tohex(u16 c)
  *
  * Implement efidebug "boot dump" sub-command.
  * Dump information of all UEFI load options defined.
- *   - boot dump
+ *
+ *     efidebug boot dump
  */
 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
                            int argc, char * const argv[])
@@ -885,7 +947,8 @@ out:
  *
  * Implement efidebug "boot next" sub-command.
  * Set BootNext variable.
- *   - boot next <id>
+ *
+ *     efidebug boot next <id>
  */
 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
                            int argc, char * const argv[])
@@ -895,6 +958,7 @@ static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
        char *endp;
        efi_guid_t guid;
        efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc != 2)
                return CMD_RET_USAGE;
@@ -902,19 +966,23 @@ static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
        bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
        if (*endp != '\0' || bootnext > 0xffff) {
                printf("invalid value: %s\n", argv[1]);
-               ret = CMD_RET_FAILURE;
+               r = CMD_RET_FAILURE;
                goto out;
        }
 
        guid = efi_global_variable_guid;
        size = sizeof(u16);
        ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, &bootnext));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set BootNext\n");
+               r = CMD_RET_FAILURE;
+       }
 out:
-       return ret;
+       return r;
 }
 
 /**
@@ -928,7 +996,8 @@ out:
  *
  * Implement efidebug "boot order" sub-command.
  * Show order of UEFI load options, or change it in BootOrder variable.
- *   - boot order [<id> ...]
+ *
+ *     efidebug boot order [<id> ...]
  */
 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
                             int argc, char * const argv[])
@@ -939,6 +1008,7 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
        char *endp;
        efi_guid_t guid;
        efi_status_t ret;
+       int r = CMD_RET_SUCCESS;
 
        if (argc == 1)
                return show_efi_boot_order();
@@ -955,7 +1025,7 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
                id = (int)simple_strtoul(argv[i], &endp, 16);
                if (*endp != '\0' || id > 0xffff) {
                        printf("invalid value: %s\n", argv[i]);
-                       ret = CMD_RET_FAILURE;
+                       r = CMD_RET_FAILURE;
                        goto out;
                }
 
@@ -964,14 +1034,18 @@ static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
 
        guid = efi_global_variable_guid;
        ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
+                                       EFI_VARIABLE_NON_VOLATILE |
                                        EFI_VARIABLE_BOOTSERVICE_ACCESS |
                                        EFI_VARIABLE_RUNTIME_ACCESS,
                                        size, bootorder));
-       ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
+       if (ret != EFI_SUCCESS) {
+               printf("Cannot set BootOrder\n");
+               r = CMD_RET_FAILURE;
+       }
 out:
        free(bootorder);
 
-       return ret;
+       return r;
 }
 
 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
@@ -994,7 +1068,6 @@ static cmd_tbl_t cmd_efidebug_boot_sub[] = {
  *             CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
  *
  * Implement efidebug "boot" sub-command.
- * See above for details of sub-commands.
  */
 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
                           int argc, char * const argv[])
@@ -1026,6 +1099,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
                         "", ""),
        U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
                         "", ""),
+       U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
+                        "", ""),
 };
 
 /**
@@ -1040,7 +1115,6 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
  *
  * Implement efidebug command which allows us to display and
  * configure UEFI environment.
- * See above for details of sub-commands.
  */
 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
                       int argc, char * const argv[])
@@ -1086,15 +1160,17 @@ static char efidebug_help_text[] =
        "  - set/show UEFI boot order\n"
        "\n"
        "efidebug devices\n"
-       "  - show uefi devices\n"
+       "  - show UEFI devices\n"
        "efidebug drivers\n"
-       "  - show uefi drivers\n"
+       "  - show UEFI drivers\n"
        "efidebug dh\n"
-       "  - show uefi handles\n"
+       "  - show UEFI handles\n"
        "efidebug images\n"
        "  - show loaded images\n"
        "efidebug memmap\n"
-       "  - show uefi memory map\n";
+       "  - show UEFI memory map\n"
+       "efidebug tables\n"
+       "  - show UEFI configuration tables\n";
 #endif
 
 U_BOOT_CMD(