From: Ma Wupeng <mawupeng1@xxxxxxxxxx> Add support to relocate kernel image to mirrord regions if KASLR doesn't work. If a suiable mirrored slot if found, call efi_random_alloc() with random_seed be zero to relocate kernel code and data to the first slot available. Signed-off-by: Ma Wupeng <mawupeng1@xxxxxxxxxx> --- drivers/firmware/efi/libstub/arm64-stub.c | 10 ++++++++++ drivers/firmware/efi/libstub/randomalloc.c | 1 + 2 files changed, 11 insertions(+) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index 39b774853b93..851a8948cafb 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -190,6 +190,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, } if (status != EFI_SUCCESS) { + if (efi_mirror_found) { + status = efi_random_alloc(*reserve_size, min_kimg_align, + reserve_addr, 0, + efi_mirror_found); + if (status == EFI_SUCCESS) + goto out; + + efi_err("Failed to relocate kernel to mirrored region\n"); + } + if (!check_image_region((u64)_text, kernel_memsize)) { efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"); } else if (IS_ALIGNED((u64)_text, min_kimg_align)) { diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index dd81d6c3c406..d5f4249943a7 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -50,6 +50,7 @@ unsigned long get_entry_num_slots(efi_memory_desc_t *md, */ #define MD_NUM_SLOTS(md) ((md)->virt_addr) +/* random_seed == 0 means alloc mem from the first suitable slot */ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, -- 2.25.1