1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author: Yun Liu <liuyun@loongson.cn>
4  *         Huacai Chen <chenhuacai@loongson.cn>
5  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6  */
7 
8 #include <asm/efi.h>
9 #include <asm/addrspace.h>
10 #include "efistub.h"
11 
12 extern int kernel_asize;
13 extern int kernel_fsize;
14 extern int kernel_offset;
15 extern int kernel_entry;
16 
17 efi_status_t handle_kernel_image(unsigned long *image_addr,
18 				 unsigned long *image_size,
19 				 unsigned long *reserve_addr,
20 				 unsigned long *reserve_size,
21 				 efi_loaded_image_t *image,
22 				 efi_handle_t image_handle)
23 {
24 	int nr_pages = round_up(kernel_asize, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
25 	efi_physical_addr_t kernel_addr = EFI_KIMG_PREFERRED_ADDRESS;
26 	efi_status_t status;
27 
28 	/*
29 	 * Allocate space for the kernel image at the preferred offset. This is
30 	 * the only location in memory from where we can execute the image, so
31 	 * no point in falling back to another allocation.
32 	 */
33 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
34 			     EFI_LOADER_DATA, nr_pages, &kernel_addr);
35 	if (status != EFI_SUCCESS)
36 		return status;
37 
38 	*image_addr = EFI_KIMG_PREFERRED_ADDRESS;
39 	*image_size = kernel_asize;
40 
41 	memcpy((void *)EFI_KIMG_PREFERRED_ADDRESS,
42 	       (void *)&kernel_offset - kernel_offset,
43 	       kernel_fsize);
44 
45 	return status;
46 }
47 
48 unsigned long kernel_entry_address(void)
49 {
50 	unsigned long base = (unsigned long)&kernel_offset - kernel_offset;
51 
52 	return (unsigned long)&kernel_entry - base + VMLINUX_LOAD_ADDRESS;
53 }
54