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