Skip to content

Commit 98d2a6c

Browse files
Laszlo ErsekMatt Fleming
authored andcommitted
efi: Introduce efi_md_typeattr_format()
At the moment, there are three architectures debug-printing the EFI memory map at initialization: x86, ia64, and arm64. They all use different format strings, plus the EFI memory type and the EFI memory attributes are similarly hard to decode for a human reader. Introduce a helper __init function that formats the memory type and the memory attributes in a unified way, to a user-provided character buffer. The array "memory_type_name" is copied from the arm64 code, temporarily duplicating it. The (otherwise optional) braces around each string literal in the initializer list are dropped in order to match the kernel coding style more closely. The element size is tightened from 32 to 20 bytes (maximum actual string length + 1) so that we can derive the field width from the element size. Signed-off-by: Laszlo Ersek <lersek@redhat.com> Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> [ Dropped useless 'register' keyword, which compiler will ignore ] Signed-off-by: Matt Fleming <matt.fleming@intel.com>
1 parent 9c97e0b commit 98d2a6c

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎drivers/firmware/efi/efi.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,60 @@ int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
445445
return ret;
446446
}
447447
#endif /* CONFIG_EFI_PARAMS_FROM_FDT */
448+
449+
static __initdata char memory_type_name[][20] = {
450+
"Reserved",
451+
"Loader Code",
452+
"Loader Data",
453+
"Boot Code",
454+
"Boot Data",
455+
"Runtime Code",
456+
"Runtime Data",
457+
"Conventional Memory",
458+
"Unusable Memory",
459+
"ACPI Reclaim Memory",
460+
"ACPI Memory NVS",
461+
"Memory Mapped I/O",
462+
"MMIO Port Space",
463+
"PAL Code"
464+
};
465+
466+
char * __init efi_md_typeattr_format(char *buf, size_t size,
467+
const efi_memory_desc_t *md)
468+
{
469+
char *pos;
470+
int type_len;
471+
u64 attr;
472+
473+
pos = buf;
474+
if (md->type >= ARRAY_SIZE(memory_type_name))
475+
type_len = snprintf(pos, size, "[type=%u", md->type);
476+
else
477+
type_len = snprintf(pos, size, "[%-*s",
478+
(int)(sizeof(memory_type_name[0]) - 1),
479+
memory_type_name[md->type]);
480+
if (type_len >= size)
481+
return buf;
482+
483+
pos += type_len;
484+
size -= type_len;
485+
486+
attr = md->attribute;
487+
if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
488+
EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_WP |
489+
EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_RUNTIME))
490+
snprintf(pos, size, "|attr=0x%016llx]",
491+
(unsigned long long)attr);
492+
else
493+
snprintf(pos, size, "|%3s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
494+
attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
495+
attr & EFI_MEMORY_XP ? "XP" : "",
496+
attr & EFI_MEMORY_RP ? "RP" : "",
497+
attr & EFI_MEMORY_WP ? "WP" : "",
498+
attr & EFI_MEMORY_UCE ? "UCE" : "",
499+
attr & EFI_MEMORY_WB ? "WB" : "",
500+
attr & EFI_MEMORY_WT ? "WT" : "",
501+
attr & EFI_MEMORY_WC ? "WC" : "",
502+
attr & EFI_MEMORY_UC ? "UC" : "");
503+
return buf;
504+
}

‎include/linux/efi.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,13 @@ extern bool efi_poweroff_required(void);
887887
(md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
888888
(md) = (void *)(md) + (m)->desc_size)
889889

890+
/*
891+
* Format an EFI memory descriptor's type and attributes to a user-provided
892+
* character buffer, as per snprintf(), and return the buffer.
893+
*/
894+
char * __init efi_md_typeattr_format(char *buf, size_t size,
895+
const efi_memory_desc_t *md);
896+
890897
/**
891898
* efi_range_is_wc - check the WC bit on an address range
892899
* @start: starting kvirt address

0 commit comments

Comments
 (0)