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
stext_offset(void)14 unsigned 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
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)26 efi_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 = 0;
34 unsigned long preferred_addr;
35 efi_status_t status;
36
37 kernel_size = _edata - _start;
38 *image_addr = (unsigned long)_start;
39 *image_size = kernel_size + (_end - _edata);
40
41 /*
42 * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical
43 * address where kernel is booted. That's why kernel should boot from
44 * as low as possible to avoid wastage of memory. Currently, dram_base
45 * is occupied by the firmware. So the preferred address for kernel to
46 * boot is next aligned address. If preferred address is not available,
47 * relocate_kernel will fall back to efi_low_alloc_above to allocate
48 * lowest possible memory region as long as the address and size meets
49 * the alignment constraints.
50 */
51 preferred_addr = EFI_KIMG_PREFERRED_ADDRESS;
52 status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
53 preferred_addr, efi_get_kimg_min_align(),
54 0x0);
55
56 if (status != EFI_SUCCESS) {
57 efi_err("Failed to relocate kernel\n");
58 *image_size = 0;
59 }
60 return status;
61 }
62