diff mbox series

[v4] efi/loongarch: Use load address to calculate kernel entry address

Message ID 20231206002427.25480-1-wangyao@lemote.com
State Superseded
Headers show
Series [v4] efi/loongarch: Use load address to calculate kernel entry address | expand

Commit Message

wangyao@lemote.com Dec. 6, 2023, 12:24 a.m. UTC
From: Wang Yao <wangyao@lemote.com>

The efi_relocate_kernel() may load the PIE kernel to anywhere, the loaded
address may not be equal to link address or EFI_KIMG_PREFERRED_ADDRESS.

Acked-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Wang Yao <wangyao@lemote.com>
---

v3->v4:
Add Acked-by: Huacai Chen <chenhuacai@loongson.cn>

v2->v3:
Dropped TO_CACHE:
kernel_addr may be DA/PG address, both access kernel entry.

v1->v2:
Use link address that from DOS header to calculate kernel entry,
corrected by Huacai Chen.

 arch/loongarch/include/asm/efi.h              | 2 +-
 drivers/firmware/efi/libstub/loongarch-stub.c | 4 ++--
 drivers/firmware/efi/libstub/loongarch.c      | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)

Comments

Ard Biesheuvel Dec. 7, 2023, 4:08 p.m. UTC | #1
On Wed, 6 Dec 2023 at 01:25, <wangyao@lemote.com> wrote:
>
> From: Wang Yao <wangyao@lemote.com>
>
> The efi_relocate_kernel() may load the PIE kernel to anywhere, the loaded
> address may not be equal to link address or EFI_KIMG_PREFERRED_ADDRESS.
>
> Acked-by: Huacai Chen <chenhuacai@loongson.cn>
> Signed-off-by: Wang Yao <wangyao@lemote.com>

Queued up now in the efi/next tree - thanks.

> ---
>
> v3->v4:
> Add Acked-by: Huacai Chen <chenhuacai@loongson.cn>
>
> v2->v3:
> Dropped TO_CACHE:
> kernel_addr may be DA/PG address, both access kernel entry.
>
> v1->v2:
> Use link address that from DOS header to calculate kernel entry,
> corrected by Huacai Chen.
>
>  arch/loongarch/include/asm/efi.h              | 2 +-
>  drivers/firmware/efi/libstub/loongarch-stub.c | 4 ++--
>  drivers/firmware/efi/libstub/loongarch.c      | 6 +++---
>  3 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/arch/loongarch/include/asm/efi.h b/arch/loongarch/include/asm/efi.h
> index 091897d40b03..91d81f9730ab 100644
> --- a/arch/loongarch/include/asm/efi.h
> +++ b/arch/loongarch/include/asm/efi.h
> @@ -32,6 +32,6 @@ static inline unsigned long efi_get_kimg_min_align(void)
>
>  #define EFI_KIMG_PREFERRED_ADDRESS     PHYSADDR(VMLINUX_LOAD_ADDRESS)
>
> -unsigned long kernel_entry_address(void);
> +unsigned long kernel_entry_address(unsigned long kernel_addr);
>
>  #endif /* _ASM_LOONGARCH_EFI_H */
> diff --git a/drivers/firmware/efi/libstub/loongarch-stub.c b/drivers/firmware/efi/libstub/loongarch-stub.c
> index 72c71ae201f0..d6ec5d4b8dbe 100644
> --- a/drivers/firmware/efi/libstub/loongarch-stub.c
> +++ b/drivers/firmware/efi/libstub/loongarch-stub.c
> @@ -35,9 +35,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
>         return status;
>  }
>
> -unsigned long kernel_entry_address(void)
> +unsigned long kernel_entry_address(unsigned long kernel_addr)
>  {
>         unsigned long base = (unsigned long)&kernel_offset - kernel_offset;
>
> -       return (unsigned long)&kernel_entry - base + VMLINUX_LOAD_ADDRESS;
> +       return (unsigned long)&kernel_entry - base + kernel_addr;
>  }
> diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
> index 807cba2693fc..0e0aa6cda73f 100644
> --- a/drivers/firmware/efi/libstub/loongarch.c
> +++ b/drivers/firmware/efi/libstub/loongarch.c
> @@ -37,9 +37,9 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
>         return EFI_SUCCESS;
>  }
>
> -unsigned long __weak kernel_entry_address(void)
> +unsigned long __weak kernel_entry_address(unsigned long kernel_addr)
>  {
> -       return *(unsigned long *)(PHYSADDR(VMLINUX_LOAD_ADDRESS) + 8);
> +       return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
>  }
>
>  efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
> @@ -73,7 +73,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
>         csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
>         csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
>
> -       real_kernel_entry = (void *)kernel_entry_address();
> +       real_kernel_entry = (void *)kernel_entry_address(kernel_addr);
>
>         real_kernel_entry(true, (unsigned long)cmdline_ptr,
>                           (unsigned long)efi_system_table);
> --
> 2.27.0
>
diff mbox series

Patch

diff --git a/arch/loongarch/include/asm/efi.h b/arch/loongarch/include/asm/efi.h
index 091897d40b03..91d81f9730ab 100644
--- a/arch/loongarch/include/asm/efi.h
+++ b/arch/loongarch/include/asm/efi.h
@@ -32,6 +32,6 @@  static inline unsigned long efi_get_kimg_min_align(void)
 
 #define EFI_KIMG_PREFERRED_ADDRESS	PHYSADDR(VMLINUX_LOAD_ADDRESS)
 
-unsigned long kernel_entry_address(void);
+unsigned long kernel_entry_address(unsigned long kernel_addr);
 
 #endif /* _ASM_LOONGARCH_EFI_H */
diff --git a/drivers/firmware/efi/libstub/loongarch-stub.c b/drivers/firmware/efi/libstub/loongarch-stub.c
index 72c71ae201f0..d6ec5d4b8dbe 100644
--- a/drivers/firmware/efi/libstub/loongarch-stub.c
+++ b/drivers/firmware/efi/libstub/loongarch-stub.c
@@ -35,9 +35,9 @@  efi_status_t handle_kernel_image(unsigned long *image_addr,
 	return status;
 }
 
-unsigned long kernel_entry_address(void)
+unsigned long kernel_entry_address(unsigned long kernel_addr)
 {
 	unsigned long base = (unsigned long)&kernel_offset - kernel_offset;
 
-	return (unsigned long)&kernel_entry - base + VMLINUX_LOAD_ADDRESS;
+	return (unsigned long)&kernel_entry - base + kernel_addr;
 }
diff --git a/drivers/firmware/efi/libstub/loongarch.c b/drivers/firmware/efi/libstub/loongarch.c
index 807cba2693fc..0e0aa6cda73f 100644
--- a/drivers/firmware/efi/libstub/loongarch.c
+++ b/drivers/firmware/efi/libstub/loongarch.c
@@ -37,9 +37,9 @@  static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
 	return EFI_SUCCESS;
 }
 
-unsigned long __weak kernel_entry_address(void)
+unsigned long __weak kernel_entry_address(unsigned long kernel_addr)
 {
-	return *(unsigned long *)(PHYSADDR(VMLINUX_LOAD_ADDRESS) + 8);
+	return *(unsigned long *)(kernel_addr + 8) - VMLINUX_LOAD_ADDRESS + kernel_addr;
 }
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
@@ -73,7 +73,7 @@  efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
 	csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
 
-	real_kernel_entry = (void *)kernel_entry_address();
+	real_kernel_entry = (void *)kernel_entry_address(kernel_addr);
 
 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
 			  (unsigned long)efi_system_table);