1d7071743SAtish Patra // SPDX-License-Identifier: GPL-2.0
2d7071743SAtish Patra /*
3d7071743SAtish Patra  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
4d7071743SAtish Patra  */
5d7071743SAtish Patra 
6d7071743SAtish Patra #include <linux/efi.h>
7d7071743SAtish Patra 
8d7071743SAtish Patra #include <asm/efi.h>
9d7071743SAtish Patra #include <asm/sections.h>
10171549f8SSunil V L #include <asm/unaligned.h>
11d7071743SAtish Patra 
12d7071743SAtish Patra #include "efistub.h"
13d7071743SAtish Patra 
stext_offset(void)14f1a116c0SArd Biesheuvel unsigned long stext_offset(void)
15d7071743SAtish Patra {
16d7071743SAtish Patra 	/*
17f1a116c0SArd Biesheuvel 	 * When built as part of the kernel, the EFI stub cannot branch to the
18f1a116c0SArd Biesheuvel 	 * kernel proper via the image header, as the PE/COFF header is
19f1a116c0SArd Biesheuvel 	 * strictly not part of the in-memory presentation of the image, only
20f1a116c0SArd Biesheuvel 	 * of the file representation. So instead, we need to jump to the
21f1a116c0SArd Biesheuvel 	 * actual entrypoint in the .text region of the image.
22d7071743SAtish Patra 	 */
23f1a116c0SArd Biesheuvel 	return _start_kernel - _start;
24d7071743SAtish Patra }
25d7071743SAtish Patra 
handle_kernel_image(unsigned long * image_addr,unsigned long * image_size,unsigned long * reserve_addr,unsigned long * reserve_size,efi_loaded_image_t * image,efi_handle_t image_handle)26d7071743SAtish Patra efi_status_t handle_kernel_image(unsigned long *image_addr,
27d7071743SAtish Patra 				 unsigned long *image_size,
28d7071743SAtish Patra 				 unsigned long *reserve_addr,
29d7071743SAtish Patra 				 unsigned long *reserve_size,
30416a9f84SArd Biesheuvel 				 efi_loaded_image_t *image,
31416a9f84SArd Biesheuvel 				 efi_handle_t image_handle)
32d7071743SAtish Patra {
33*b7ac4b8eSAlexandre Ghiti 	unsigned long kernel_size, kernel_codesize, kernel_memsize;
34d7071743SAtish Patra 	efi_status_t status;
35d7071743SAtish Patra 
36d7071743SAtish Patra 	kernel_size = _edata - _start;
37*b7ac4b8eSAlexandre Ghiti 	kernel_codesize = __init_text_end - _start;
38*b7ac4b8eSAlexandre Ghiti 	kernel_memsize = kernel_size + (_end - _edata);
39d7071743SAtish Patra 	*image_addr = (unsigned long)_start;
40*b7ac4b8eSAlexandre Ghiti 	*image_size = kernel_memsize;
41*b7ac4b8eSAlexandre Ghiti 	*reserve_size = *image_size;
42d7071743SAtish Patra 
43*b7ac4b8eSAlexandre Ghiti 	status = efi_kaslr_relocate_kernel(image_addr,
44*b7ac4b8eSAlexandre Ghiti 					   reserve_addr, reserve_size,
45*b7ac4b8eSAlexandre Ghiti 					   kernel_size, kernel_codesize, kernel_memsize,
46*b7ac4b8eSAlexandre Ghiti 					   efi_kaslr_get_phys_seed(image_handle));
47d7071743SAtish Patra 	if (status != EFI_SUCCESS) {
48d7071743SAtish Patra 		efi_err("Failed to relocate kernel\n");
49d7071743SAtish Patra 		*image_size = 0;
50d7071743SAtish Patra 	}
51*b7ac4b8eSAlexandre Ghiti 
52d7071743SAtish Patra 	return status;
53d7071743SAtish Patra }
54*b7ac4b8eSAlexandre Ghiti 
efi_icache_sync(unsigned long start,unsigned long end)55*b7ac4b8eSAlexandre Ghiti void efi_icache_sync(unsigned long start, unsigned long end)
56*b7ac4b8eSAlexandre Ghiti {
57*b7ac4b8eSAlexandre Ghiti 	asm volatile ("fence.i" ::: "memory");
58*b7ac4b8eSAlexandre Ghiti }
59