diff mbox series

[U-Boot,v6,7/7] cmd: efidebug: add memmap command

Message ID 20190124110437.6837-8-takahiro.akashi@linaro.org
State Superseded
Delegated to: Alexander Graf
Headers show
Series cmd: add efidebug for efi environment | expand

Commit Message

AKASHI Takahiro Jan. 24, 2019, 11:04 a.m. UTC
"memmap" command prints uefi-specific memory map information.
=> efi memmap
Type             Start            End              Attributes

Comments

Heinrich Schuchardt Feb. 19, 2019, 7:11 p.m. UTC | #1
On 1/24/19 12:04 PM, AKASHI Takahiro wrote:
> "memmap" command prints uefi-specific memory map information.
> => efi memmap
> Type             Start            End              Attributes
> ================ ================ ================ ==========
> CONVENTIONAL     0000000040000000-000000007de27000 WB
> RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
> RESERVED         000000007de28000-000000007de2a000 WB
> RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
> RESERVED         000000007de2b000-000000007de2c000 WB
> RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
> LOADER DATA      000000007de2d000-000000007ff37000 WB
> RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
> LOADER DATA      000000007ff38000-0000000080000000 WB

On qemu_arm_defconfig I got this ouptut:

=> efidebug memmap
Type             Start    End      Attributes
================ ======== ======== ==========
CONVENTIONAL     0000000040000000-000000007ddf8000 WB
BOOT DATA        000000007ddf8000-000000007ddfd000 WB
RUNTIME DATA     000000007ddfd000-000000007ddfe000 WB|RT
RESERVED         000000007ddfe000-000000007ddff000 WB
RUNTIME DATA     000000007ddff000-000000007de00000 WB|RT
LOADER DATA      000000007de00000-000000007ff42000 WB
RUNTIME CODE     000000007ff42000-000000007ff43000 WB|RT
LOADER DATA      000000007ff43000-0000000080000000 WB

Something is wrong with you format codes.

> 
> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> ---
>  cmd/efidebug.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 99 insertions(+), 1 deletion(-)
> 
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> index 81ab3654f746..39398669e18f 100644
> --- a/cmd/efidebug.c
> +++ b/cmd/efidebug.c
> @@ -309,6 +309,100 @@ 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"},
> +};
> +
> +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);
> +		}
> +}
> +
> +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_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> +	if (ret == EFI_BUFFER_TOO_SMALL) {
> +		memmap = malloc(map_size);
> +		if (!memmap)
> +			return CMD_RET_FAILURE;
> +		ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> +	}
> +	if (ret != EFI_SUCCESS) {
> +		free(memmap);
> +		return CMD_RET_FAILURE;
> +	}
> +
> +	printf("Type             Start%.*s End%.*s Attributes\n",
> +	       EFI_HANDLE_WIDTH - 5, spc, EFI_HANDLE_WIDTH - 3, spc);
> +	printf("================ %.*s %.*s ==========\n",
> +	       EFI_HANDLE_WIDTH, sep, EFI_HANDLE_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 %016llx-%016llx", type, map->physical_start,

Width 16 is ok on 64bit systems but not on 32bit ones.

Best regards

Heinrich

> +		       map->physical_start + map->num_pages * EFI_PAGE_SIZE);
> +
> +		print_memory_attributes(map->attribute);
> +		putc('\n');
> +	}
> +
> +	free(memmap);
> +
> +	return CMD_RET_SUCCESS;
> +}
> +
>  static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
>  			   int argc, char * const argv[])
>  {
> @@ -708,6 +802,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
>  			 "", ""),
>  	U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
>  			 "", ""),
> +	U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
> +			 "", ""),
>  };
>  
>  /* Interpreter command to configure UEFI environment */
> @@ -761,7 +857,9 @@ static char efidebug_help_text[] =
>  	"efidebug dh\n"
>  	"  - show uefi handles\n"
>  	"efidebug images\n"
> -	"  - show loaded images\n";
> +	"  - show loaded images\n"
> +	"efidebug memmap\n"
> +	"  - show uefi memory map\n";
>  #endif
>  
>  U_BOOT_CMD(
>
AKASHI Takahiro Feb. 20, 2019, 12:53 a.m. UTC | #2
On Tue, Feb 19, 2019 at 08:11:05PM +0100, Heinrich Schuchardt wrote:
> On 1/24/19 12:04 PM, AKASHI Takahiro wrote:
> > "memmap" command prints uefi-specific memory map information.
> > => efi memmap
> > Type             Start            End              Attributes
> > ================ ================ ================ ==========
> > CONVENTIONAL     0000000040000000-000000007de27000 WB
> > RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
> > RESERVED         000000007de28000-000000007de2a000 WB
> > RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
> > RESERVED         000000007de2b000-000000007de2c000 WB
> > RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
> > LOADER DATA      000000007de2d000-000000007ff37000 WB
> > RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
> > LOADER DATA      000000007ff38000-0000000080000000 WB
> 
> On qemu_arm_defconfig I got this ouptut:
> 
> => efidebug memmap
> Type             Start    End      Attributes
> ================ ======== ======== ==========
> CONVENTIONAL     0000000040000000-000000007ddf8000 WB
> BOOT DATA        000000007ddf8000-000000007ddfd000 WB
> RUNTIME DATA     000000007ddfd000-000000007ddfe000 WB|RT
> RESERVED         000000007ddfe000-000000007ddff000 WB
> RUNTIME DATA     000000007ddff000-000000007de00000 WB|RT
> LOADER DATA      000000007de00000-000000007ff42000 WB
> RUNTIME CODE     000000007ff42000-000000007ff43000 WB|RT
> LOADER DATA      000000007ff43000-0000000080000000 WB
> 
> Something is wrong with you format codes.
> 
> > 
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  cmd/efidebug.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 99 insertions(+), 1 deletion(-)
> > 
> > diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> > index 81ab3654f746..39398669e18f 100644
> > --- a/cmd/efidebug.c
> > +++ b/cmd/efidebug.c
> > @@ -309,6 +309,100 @@ 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"},
> > +};
> > +
> > +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);
> > +		}
> > +}
> > +
> > +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_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> > +	if (ret == EFI_BUFFER_TOO_SMALL) {
> > +		memmap = malloc(map_size);
> > +		if (!memmap)
> > +			return CMD_RET_FAILURE;
> > +		ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> > +	}
> > +	if (ret != EFI_SUCCESS) {
> > +		free(memmap);
> > +		return CMD_RET_FAILURE;
> > +	}
> > +
> > +	printf("Type             Start%.*s End%.*s Attributes\n",
> > +	       EFI_HANDLE_WIDTH - 5, spc, EFI_HANDLE_WIDTH - 3, spc);
> > +	printf("================ %.*s %.*s ==========\n",
> > +	       EFI_HANDLE_WIDTH, sep, EFI_HANDLE_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 %016llx-%016llx", type, map->physical_start,
> 
> Width 16 is ok on 64bit systems but not on 32bit ones.

It sounds reasonable, but the reality is not so trivial.
In struct efi_mem_desc, physical_start is defined as efi_physical_addr_t,
and efi_physical_addr_t is defined as u64 whatever the arch is.

So how do we know the system has 64-bit address space?

There is a config, CONFIG_PHYS_64BIT, but it is never defined on x86(_64).

So workable but ugly solution to meet your requirement would be
a)
        if (sizeof(phys_addr_t) >= 8)
		printf("%-16s %016llx-%016llx", ...);
        else
		printf("%-16s %08llx-%08llx", ...);
   or
b)
        printf("%-16s %p-%p", type, (void *)map->physical_start, ...);
        (I don't think "void *" always reflects *physical* bit width
        on LPAE arch though.)

Which do you like better?

Thanks,
-Takahiro Akashi

> Best regards
> 
> Heinrich
> 
> > +		       map->physical_start + map->num_pages * EFI_PAGE_SIZE);
> > +
> > +		print_memory_attributes(map->attribute);
> > +		putc('\n');
> > +	}
> > +
> > +	free(memmap);
> > +
> > +	return CMD_RET_SUCCESS;
> > +}
> > +
> >  static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
> >  			   int argc, char * const argv[])
> >  {
> > @@ -708,6 +802,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
> >  			 "", ""),
> >  	U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
> >  			 "", ""),
> > +	U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
> > +			 "", ""),
> >  };
> >  
> >  /* Interpreter command to configure UEFI environment */
> > @@ -761,7 +857,9 @@ static char efidebug_help_text[] =
> >  	"efidebug dh\n"
> >  	"  - show uefi handles\n"
> >  	"efidebug images\n"
> > -	"  - show loaded images\n";
> > +	"  - show loaded images\n"
> > +	"efidebug memmap\n"
> > +	"  - show uefi memory map\n";
> >  #endif
> >  
> >  U_BOOT_CMD(
> > 
>
Heinrich Schuchardt Feb. 20, 2019, 6:53 a.m. UTC | #3
On 2/20/19 1:53 AM, AKASHI Takahiro wrote:
> On Tue, Feb 19, 2019 at 08:11:05PM +0100, Heinrich Schuchardt wrote:
>> On 1/24/19 12:04 PM, AKASHI Takahiro wrote:
>>> "memmap" command prints uefi-specific memory map information.
>>> => efi memmap
>>> Type             Start            End              Attributes
>>> ================ ================ ================ ==========
>>> CONVENTIONAL     0000000040000000-000000007de27000 WB
>>> RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
>>> RESERVED         000000007de28000-000000007de2a000 WB
>>> RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
>>> RESERVED         000000007de2b000-000000007de2c000 WB
>>> RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
>>> LOADER DATA      000000007de2d000-000000007ff37000 WB
>>> RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
>>> LOADER DATA      000000007ff38000-0000000080000000 WB
>>
>> On qemu_arm_defconfig I got this ouptut:
>>
>> => efidebug memmap
>> Type             Start    End      Attributes
>> ================ ======== ======== ==========
>> CONVENTIONAL     0000000040000000-000000007ddf8000 WB
>> BOOT DATA        000000007ddf8000-000000007ddfd000 WB
>> RUNTIME DATA     000000007ddfd000-000000007ddfe000 WB|RT
>> RESERVED         000000007ddfe000-000000007ddff000 WB
>> RUNTIME DATA     000000007ddff000-000000007de00000 WB|RT
>> LOADER DATA      000000007de00000-000000007ff42000 WB
>> RUNTIME CODE     000000007ff42000-000000007ff43000 WB|RT
>> LOADER DATA      000000007ff43000-0000000080000000 WB
>>
>> Something is wrong with you format codes.
>>
>>>
>>> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
>>> ---
>>>  cmd/efidebug.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-
>>>  1 file changed, 99 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
>>> index 81ab3654f746..39398669e18f 100644
>>> --- a/cmd/efidebug.c
>>> +++ b/cmd/efidebug.c
>>> @@ -309,6 +309,100 @@ 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"},
>>> +};
>>> +
>>> +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);
>>> +		}
>>> +}
>>> +
>>> +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_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
>>> +	if (ret == EFI_BUFFER_TOO_SMALL) {
>>> +		memmap = malloc(map_size);
>>> +		if (!memmap)
>>> +			return CMD_RET_FAILURE;
>>> +		ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
>>> +	}
>>> +	if (ret != EFI_SUCCESS) {
>>> +		free(memmap);
>>> +		return CMD_RET_FAILURE;
>>> +	}
>>> +
>>> +	printf("Type             Start%.*s End%.*s Attributes\n",
>>> +	       EFI_HANDLE_WIDTH - 5, spc, EFI_HANDLE_WIDTH - 3, spc);
>>> +	printf("================ %.*s %.*s ==========\n",
>>> +	       EFI_HANDLE_WIDTH, sep, EFI_HANDLE_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 %016llx-%016llx", type, map->physical_start,
>>
>> Width 16 is ok on 64bit systems but not on 32bit ones.
> 
> It sounds reasonable, but the reality is not so trivial.
> In struct efi_mem_desc, physical_start is defined as efi_physical_addr_t,
> and efi_physical_addr_t is defined as u64 whatever the arch is.
> 
> So how do we know the system has 64-bit address space?
> 
> There is a config, CONFIG_PHYS_64BIT, but it is never defined on x86(_64).
> 
> So workable but ugly solution to meet your requirement would be
> a)
>         if (sizeof(phys_addr_t) >= 8)
> 		printf("%-16s %016llx-%016llx", ...);
>         else
> 		printf("%-16s %08llx-%08llx", ...);
>    or
> b)
>         printf("%-16s %p-%p", type, (void *)map->physical_start, ...);
>         (I don't think "void *" always reflects *physical* bit width
>         on LPAE arch though.)
> 
> Which do you like better?

Please, use the same constant for both the header line and the list output.

The UEFI spec uses
typedef UINT64 EFI_PHYSICAL_ADDRESS;

So it would be appropriate to always print 16 characters.

Best regards

Heinrich


> 
> Thanks,
> -Takahiro Akashi
> 
>> Best regards
>>
>> Heinrich
>>
>>> +		       map->physical_start + map->num_pages * EFI_PAGE_SIZE);
>>> +
>>> +		print_memory_attributes(map->attribute);
>>> +		putc('\n');
>>> +	}
>>> +
>>> +	free(memmap);
>>> +
>>> +	return CMD_RET_SUCCESS;
>>> +}
>>> +
>>>  static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
>>>  			   int argc, char * const argv[])
>>>  {
>>> @@ -708,6 +802,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
>>>  			 "", ""),
>>>  	U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
>>>  			 "", ""),
>>> +	U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
>>> +			 "", ""),
>>>  };
>>>  
>>>  /* Interpreter command to configure UEFI environment */
>>> @@ -761,7 +857,9 @@ static char efidebug_help_text[] =
>>>  	"efidebug dh\n"
>>>  	"  - show uefi handles\n"
>>>  	"efidebug images\n"
>>> -	"  - show loaded images\n";
>>> +	"  - show loaded images\n"
>>> +	"efidebug memmap\n"
>>> +	"  - show uefi memory map\n";
>>>  #endif
>>>  
>>>  U_BOOT_CMD(
>>>
>>
>
AKASHI Takahiro Feb. 20, 2019, 7:45 a.m. UTC | #4
On Wed, Feb 20, 2019 at 07:53:48AM +0100, Heinrich Schuchardt wrote:
> On 2/20/19 1:53 AM, AKASHI Takahiro wrote:
> > On Tue, Feb 19, 2019 at 08:11:05PM +0100, Heinrich Schuchardt wrote:
> >> On 1/24/19 12:04 PM, AKASHI Takahiro wrote:
> >>> "memmap" command prints uefi-specific memory map information.
> >>> => efi memmap
> >>> Type             Start            End              Attributes
> >>> ================ ================ ================ ==========
> >>> CONVENTIONAL     0000000040000000-000000007de27000 WB
> >>> RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
> >>> RESERVED         000000007de28000-000000007de2a000 WB
> >>> RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
> >>> RESERVED         000000007de2b000-000000007de2c000 WB
> >>> RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
> >>> LOADER DATA      000000007de2d000-000000007ff37000 WB
> >>> RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
> >>> LOADER DATA      000000007ff38000-0000000080000000 WB
> >>
> >> On qemu_arm_defconfig I got this ouptut:
> >>
> >> => efidebug memmap
> >> Type             Start    End      Attributes
> >> ================ ======== ======== ==========
> >> CONVENTIONAL     0000000040000000-000000007ddf8000 WB
> >> BOOT DATA        000000007ddf8000-000000007ddfd000 WB
> >> RUNTIME DATA     000000007ddfd000-000000007ddfe000 WB|RT
> >> RESERVED         000000007ddfe000-000000007ddff000 WB
> >> RUNTIME DATA     000000007ddff000-000000007de00000 WB|RT
> >> LOADER DATA      000000007de00000-000000007ff42000 WB
> >> RUNTIME CODE     000000007ff42000-000000007ff43000 WB|RT
> >> LOADER DATA      000000007ff43000-0000000080000000 WB
> >>
> >> Something is wrong with you format codes.
> >>
> >>>
> >>> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> >>> ---
> >>>  cmd/efidebug.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-
> >>>  1 file changed, 99 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> >>> index 81ab3654f746..39398669e18f 100644
> >>> --- a/cmd/efidebug.c
> >>> +++ b/cmd/efidebug.c
> >>> @@ -309,6 +309,100 @@ 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"},
> >>> +};
> >>> +
> >>> +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);
> >>> +		}
> >>> +}
> >>> +
> >>> +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_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> >>> +	if (ret == EFI_BUFFER_TOO_SMALL) {
> >>> +		memmap = malloc(map_size);
> >>> +		if (!memmap)
> >>> +			return CMD_RET_FAILURE;
> >>> +		ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
> >>> +	}
> >>> +	if (ret != EFI_SUCCESS) {
> >>> +		free(memmap);
> >>> +		return CMD_RET_FAILURE;
> >>> +	}
> >>> +
> >>> +	printf("Type             Start%.*s End%.*s Attributes\n",
> >>> +	       EFI_HANDLE_WIDTH - 5, spc, EFI_HANDLE_WIDTH - 3, spc);
> >>> +	printf("================ %.*s %.*s ==========\n",
> >>> +	       EFI_HANDLE_WIDTH, sep, EFI_HANDLE_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 %016llx-%016llx", type, map->physical_start,
> >>
> >> Width 16 is ok on 64bit systems but not on 32bit ones.
> > 
> > It sounds reasonable, but the reality is not so trivial.
> > In struct efi_mem_desc, physical_start is defined as efi_physical_addr_t,
> > and efi_physical_addr_t is defined as u64 whatever the arch is.
> > 
> > So how do we know the system has 64-bit address space?
> > 
> > There is a config, CONFIG_PHYS_64BIT, but it is never defined on x86(_64).
> > 
> > So workable but ugly solution to meet your requirement would be
> > a)
> >         if (sizeof(phys_addr_t) >= 8)
> > 		printf("%-16s %016llx-%016llx", ...);
> >         else
> > 		printf("%-16s %08llx-%08llx", ...);
> >    or
> > b)
> >         printf("%-16s %p-%p", type, (void *)map->physical_start, ...);
> >         (I don't think "void *" always reflects *physical* bit width
> >         on LPAE arch though.)
> > 
> > Which do you like better?
> 
> Please, use the same constant for both the header line and the list output.
> 
> The UEFI spec uses
> typedef UINT64 EFI_PHYSICAL_ADDRESS;
> 
> So it would be appropriate to always print 16 characters.

Okay, so you changed your mind.

-Takahiro Akashi

> Best regards
> 
> Heinrich
> 
> 
> > 
> > Thanks,
> > -Takahiro Akashi
> > 
> >> Best regards
> >>
> >> Heinrich
> >>
> >>> +		       map->physical_start + map->num_pages * EFI_PAGE_SIZE);
> >>> +
> >>> +		print_memory_attributes(map->attribute);
> >>> +		putc('\n');
> >>> +	}
> >>> +
> >>> +	free(memmap);
> >>> +
> >>> +	return CMD_RET_SUCCESS;
> >>> +}
> >>> +
> >>>  static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
> >>>  			   int argc, char * const argv[])
> >>>  {
> >>> @@ -708,6 +802,8 @@ static cmd_tbl_t cmd_efidebug_sub[] = {
> >>>  			 "", ""),
> >>>  	U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
> >>>  			 "", ""),
> >>> +	U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
> >>> +			 "", ""),
> >>>  };
> >>>  
> >>>  /* Interpreter command to configure UEFI environment */
> >>> @@ -761,7 +857,9 @@ static char efidebug_help_text[] =
> >>>  	"efidebug dh\n"
> >>>  	"  - show uefi handles\n"
> >>>  	"efidebug images\n"
> >>> -	"  - show loaded images\n";
> >>> +	"  - show loaded images\n"
> >>> +	"efidebug memmap\n"
> >>> +	"  - show uefi memory map\n";
> >>>  #endif
> >>>  
> >>>  U_BOOT_CMD(
> >>>
> >>
> > 
>
diff mbox series

Patch

================ ================ ================ ==========
CONVENTIONAL     0000000040000000-000000007de27000 WB
RUNTIME DATA     000000007de27000-000000007de28000 WB|RT
RESERVED         000000007de28000-000000007de2a000 WB
RUNTIME DATA     000000007de2a000-000000007de2b000 WB|RT
RESERVED         000000007de2b000-000000007de2c000 WB
RUNTIME DATA     000000007de2c000-000000007de2d000 WB|RT
LOADER DATA      000000007de2d000-000000007ff37000 WB
RUNTIME CODE     000000007ff37000-000000007ff38000 WB|RT
LOADER DATA      000000007ff38000-0000000080000000 WB

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 cmd/efidebug.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 99 insertions(+), 1 deletion(-)

diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index 81ab3654f746..39398669e18f 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -309,6 +309,100 @@  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"},
+};
+
+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);
+		}
+}
+
+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_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
+	if (ret == EFI_BUFFER_TOO_SMALL) {
+		memmap = malloc(map_size);
+		if (!memmap)
+			return CMD_RET_FAILURE;
+		ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
+	}
+	if (ret != EFI_SUCCESS) {
+		free(memmap);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("Type             Start%.*s End%.*s Attributes\n",
+	       EFI_HANDLE_WIDTH - 5, spc, EFI_HANDLE_WIDTH - 3, spc);
+	printf("================ %.*s %.*s ==========\n",
+	       EFI_HANDLE_WIDTH, sep, EFI_HANDLE_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 %016llx-%016llx", type, map->physical_start,
+		       map->physical_start + map->num_pages * EFI_PAGE_SIZE);
+
+		print_memory_attributes(map->attribute);
+		putc('\n');
+	}
+
+	free(memmap);
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
 			   int argc, char * const argv[])
 {
@@ -708,6 +802,8 @@  static cmd_tbl_t cmd_efidebug_sub[] = {
 			 "", ""),
 	U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
 			 "", ""),
+	U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
+			 "", ""),
 };
 
 /* Interpreter command to configure UEFI environment */
@@ -761,7 +857,9 @@  static char efidebug_help_text[] =
 	"efidebug dh\n"
 	"  - show uefi handles\n"
 	"efidebug images\n"
-	"  - show loaded images\n";
+	"  - show loaded images\n"
+	"efidebug memmap\n"
+	"  - show uefi memory map\n";
 #endif
 
 U_BOOT_CMD(