Skip to content

Commit 02e43c2

Browse files
Baoquan HeIngo Molnar
Baoquan He
authored and
Ingo Molnar
committed
efi: Introduce efi_early_memdesc_ptr to get pointer to memmap descriptor
The existing map iteration helper for_each_efi_memory_desc_in_map can only be used after the kernel initializes the EFI subsystem to set up struct efi_memory_map. Before that we also need iterate map descriptors which are stored in several intermediate structures, like struct efi_boot_memmap for arch independent usage and struct efi_info for x86 arch only. Introduce efi_early_memdesc_ptr() to get pointer to a map descriptor, and replace several places where that primitive is open coded. Signed-off-by: Baoquan He <[email protected]> [ Various improvements to the text. ] Acked-by: Matt Fleming <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/20170816134651.GF21273@x1 Signed-off-by: Ingo Molnar <[email protected]>
1 parent 2257e26 commit 02e43c2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

arch/x86/boot/compressed/eboot.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ static efi_status_t setup_e820(struct boot_params *params,
767767
m |= (u64)efi->efi_memmap_hi << 32;
768768
#endif
769769

770-
d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
770+
d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
771771
switch (d->type) {
772772
case EFI_RESERVED_TYPE:
773773
case EFI_RUNTIME_SERVICES_CODE:

drivers/firmware/efi/libstub/efi-stub-helper.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
205205
unsigned long m = (unsigned long)map;
206206
u64 start, end;
207207

208-
desc = (efi_memory_desc_t *)(m + (i * desc_size));
208+
desc = efi_early_memdesc_ptr(m, desc_size, i);
209209
if (desc->type != EFI_CONVENTIONAL_MEMORY)
210210
continue;
211211

@@ -298,7 +298,7 @@ efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
298298
unsigned long m = (unsigned long)map;
299299
u64 start, end;
300300

301-
desc = (efi_memory_desc_t *)(m + (i * desc_size));
301+
desc = efi_early_memdesc_ptr(m, desc_size, i);
302302

303303
if (desc->type != EFI_CONVENTIONAL_MEMORY)
304304
continue;

include/linux/efi.h

+22
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,28 @@ extern int efi_memattr_init(void);
10201020
extern int efi_memattr_apply_permissions(struct mm_struct *mm,
10211021
efi_memattr_perm_setter fn);
10221022

1023+
/*
1024+
* efi_early_memdesc_ptr - get the n-th EFI memmap descriptor
1025+
* @map: the start of efi memmap
1026+
* @desc_size: the size of space for each EFI memmap descriptor
1027+
* @n: the index of efi memmap descriptor
1028+
*
1029+
* EFI boot service provides the GetMemoryMap() function to get a copy of the
1030+
* current memory map which is an array of memory descriptors, each of
1031+
* which describes a contiguous block of memory. It also gets the size of the
1032+
* map, and the size of each descriptor, etc.
1033+
*
1034+
* Note that per section 6.2 of UEFI Spec 2.6 Errata A, the returned size of
1035+
* each descriptor might not be equal to sizeof(efi_memory_memdesc_t),
1036+
* since efi_memory_memdesc_t may be extended in the future. Thus the OS
1037+
* MUST use the returned size of the descriptor to find the start of each
1038+
* efi_memory_memdesc_t in the memory map array. This should only be used
1039+
* during bootup since for_each_efi_memory_desc_xxx() is available after the
1040+
* kernel initializes the EFI subsystem to set up struct efi_memory_map.
1041+
*/
1042+
#define efi_early_memdesc_ptr(map, desc_size, n) \
1043+
(efi_memory_desc_t *)((void *)(map) + ((n) * (desc_size)))
1044+
10231045
/* Iterate through an efi_memory_map */
10241046
#define for_each_efi_memory_desc_in_map(m, md) \
10251047
for ((md) = (m)->map; \

0 commit comments

Comments
 (0)