Re: [U-Boot] [PATCH 1/2] cmd: efidebug: add memmap command

2019-02-26 Thread Meenakshi Aggarwal
Please ignore, it is sent by mistake.


> -Original Message-
> From: U-Boot  On Behalf Of Meenakshi
> Aggarwal
> Sent: Wednesday, February 27, 2019 2:41 PM
> To: u-boot@lists.denx.de; Prabhakar Kushwaha
> 
> Subject: [U-Boot] [PATCH 1/2] cmd: efidebug: add memmap command
> 
> From: AKASHI Takahiro 
> 
> "memmap" command prints uefi-specific memory map information.
> => efi memmap
> Type StartEnd  Attributes
>    ==
> CONVENTIONAL 4000-7de27000 WB
> RUNTIME DATA 7de27000-7de28000 WB|RT
> RESERVED 7de28000-7de2a000 WB
> RUNTIME DATA 7de2a000-7de2b000 WB|RT
> RESERVED 7de2b000-7de2c000 WB
> RUNTIME DATA 7de2c000-7de2d000 WB|RT
> LOADER DATA  7de2d000-7ff37000 WB
> RUNTIME CODE 7ff37000-7ff38000 WB|RT
> LOADER DATA  7ff38000-8000 WB
> 
> Signed-off-by: AKASHI Takahiro 
> Reviewed-by: Heinrich Schuchardt 
> ---
>  cmd/efidebug.c | 127
> -
>  1 file changed, 126 insertions(+), 1 deletion(-)
> 
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c index e3a2d09..5072a7b 100644
> --- a/cmd/efidebug.c
> +++ b/cmd/efidebug.c
> @@ -335,6 +335,127 @@ static int do_efi_show_images(cmd_tbl_t *cmdtp, int
> flag,
>   return CMD_RET_SUCCESS;
>  }
> 
> +static const char * const efi_mem_type_string[] = {
> + [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
> + [EFI_LOADER_CODE] = "LOADER CODE",
> + [EFI_LOADER_DATA] = "LOADER DATA",
> + [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
> + [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
> + [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
> + [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
> + [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
> + [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
> + [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
> + [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
> + [EFI_MMAP_IO] = "IO",
> + [EFI_MMAP_IO_PORT] = "IO PORT",
> + [EFI_PAL_CODE] = "PAL",
> +};
> +
> +static const struct efi_mem_attrs {
> + const u64 bit;
> + const char *text;
> +} efi_mem_attrs[] = {
> + {EFI_MEMORY_UC, "UC"},
> + {EFI_MEMORY_UC, "UC"},
> + {EFI_MEMORY_WC, "WC"},
> + {EFI_MEMORY_WT, "WT"},
> + {EFI_MEMORY_WB, "WB"},
> + {EFI_MEMORY_UCE, "UCE"},
> + {EFI_MEMORY_WP, "WP"},
> + {EFI_MEMORY_RP, "RP"},
> + {EFI_MEMORY_XP, "WP"},
> + {EFI_MEMORY_NV, "NV"},
> + {EFI_MEMORY_MORE_RELIABLE, "REL"},
> + {EFI_MEMORY_RO, "RO"},
> + {EFI_MEMORY_RUNTIME, "RT"},
> +};
> +
> +/**
> + * print_memory_attributes() - print memory map attributes
> + * @attributes:  Attribute value
> + *
> + * Print memory map attributes
> + */
> +static void print_memory_attributes(u64 attributes) {
> + int sep, i;
> +
> + for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
> + if (attributes & efi_mem_attrs[i].bit) {
> + if (sep) {
> + putc('|');
> + } else {
> + putc(' ');
> + sep = 1;
> + }
> + puts(efi_mem_attrs[i].text);
> + }
> +}
> +
> +#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
> +
> +/**
> + * do_efi_show_memmap() - show UEFI memory map
> + *
> + * @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 "memmap" sub-command.
> + * Show UEFI memory map.
> + */
> +static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
> +   int argc, char * const argv[]) {
> + struct efi_mem_desc *memmap = NULL, *map;
> + efi_uintn_t map_size = 0;
> + const char *type;
> + int i;
> + efi_status_t ret;
> +
> + ret = EFI_CALL(BS->get_memory_map(_size, memmap, NULL,
> NULL, NULL));
> + if (ret == EFI_BUFFER_TOO_SMALL) {
> + map_size += sizeof(struct efi_mem_desc); /* for my 

[U-Boot] [PATCH 1/2] cmd: efidebug: add memmap command

2019-02-26 Thread Meenakshi Aggarwal
From: AKASHI Takahiro 

"memmap" command prints uefi-specific memory map information.
=> efi memmap
Type StartEnd  Attributes
   ==
CONVENTIONAL 4000-7de27000 WB
RUNTIME DATA 7de27000-7de28000 WB|RT
RESERVED 7de28000-7de2a000 WB
RUNTIME DATA 7de2a000-7de2b000 WB|RT
RESERVED 7de2b000-7de2c000 WB
RUNTIME DATA 7de2c000-7de2d000 WB|RT
LOADER DATA  7de2d000-7ff37000 WB
RUNTIME CODE 7ff37000-7ff38000 WB|RT
LOADER DATA  7ff38000-8000 WB

Signed-off-by: AKASHI Takahiro 
Reviewed-by: Heinrich Schuchardt 
---
 cmd/efidebug.c | 127 -
 1 file changed, 126 insertions(+), 1 deletion(-)

diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index e3a2d09..5072a7b 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -335,6 +335,127 @@ static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
return CMD_RET_SUCCESS;
 }
 
+static const char * const efi_mem_type_string[] = {
+   [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
+   [EFI_LOADER_CODE] = "LOADER CODE",
+   [EFI_LOADER_DATA] = "LOADER DATA",
+   [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
+   [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
+   [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
+   [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
+   [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
+   [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
+   [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
+   [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
+   [EFI_MMAP_IO] = "IO",
+   [EFI_MMAP_IO_PORT] = "IO PORT",
+   [EFI_PAL_CODE] = "PAL",
+};
+
+static const struct efi_mem_attrs {
+   const u64 bit;
+   const char *text;
+} efi_mem_attrs[] = {
+   {EFI_MEMORY_UC, "UC"},
+   {EFI_MEMORY_UC, "UC"},
+   {EFI_MEMORY_WC, "WC"},
+   {EFI_MEMORY_WT, "WT"},
+   {EFI_MEMORY_WB, "WB"},
+   {EFI_MEMORY_UCE, "UCE"},
+   {EFI_MEMORY_WP, "WP"},
+   {EFI_MEMORY_RP, "RP"},
+   {EFI_MEMORY_XP, "WP"},
+   {EFI_MEMORY_NV, "NV"},
+   {EFI_MEMORY_MORE_RELIABLE, "REL"},
+   {EFI_MEMORY_RO, "RO"},
+   {EFI_MEMORY_RUNTIME, "RT"},
+};
+
+/**
+ * print_memory_attributes() - print memory map attributes
+ * @attributes:Attribute value
+ *
+ * Print memory map attributes
+ */
+static void print_memory_attributes(u64 attributes)
+{
+   int sep, i;
+
+   for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
+   if (attributes & efi_mem_attrs[i].bit) {
+   if (sep) {
+   putc('|');
+   } else {
+   putc(' ');
+   sep = 1;
+   }
+   puts(efi_mem_attrs[i].text);
+   }
+}
+
+#define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
+
+/**
+ * do_efi_show_memmap() - show UEFI memory map
+ *
+ * @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 "memmap" sub-command.
+ * Show UEFI memory map.
+ */
+static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+   struct efi_mem_desc *memmap = NULL, *map;
+   efi_uintn_t map_size = 0;
+   const char *type;
+   int i;
+   efi_status_t ret;
+
+   ret = EFI_CALL(BS->get_memory_map(_size, memmap, NULL, NULL, NULL));
+   if (ret == EFI_BUFFER_TOO_SMALL) {
+   map_size += sizeof(struct efi_mem_desc); /* for my own */
+   ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
+map_size, (void *)));
+   if (ret != EFI_SUCCESS)
+   return CMD_RET_FAILURE;
+   ret = EFI_CALL(BS->get_memory_map(_size, memmap,
+ NULL, NULL, NULL));
+   }
+   if (ret != EFI_SUCCESS) {
+   EFI_CALL(BS->free_pool(memmap));
+   return CMD_RET_FAILURE;
+   }
+
+   printf("Type Start%.*s End%.*s Attributes\n",
+  EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
+   printf(" %.*s %.*s ==\n",
+  EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
+   for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
+   if (map->type < EFI_MAX_MEMORY_TYPE)
+   type = efi_mem_type_string[map->type];
+   else
+   type = "(unknown)";
+
+   printf("%-16s %.*llx-%.*llx",