Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org>
4 *
5 * This file implements the EFI boot stub for the arm64 kernel.
6 * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7 */
8
9
10#include <linux/efi.h>
11#include <asm/efi.h>
12#include <asm/memory.h>
13#include <asm/sections.h>
14
15#include "efistub.h"
16
17efi_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 efi_status_t status;
25 unsigned long kernel_size, kernel_codesize, kernel_memsize;
26
27 if (image->image_base != _text) {
28 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
29 image->image_base = _text;
30 }
31
32 if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
33 efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
34 SEGMENT_ALIGN >> 10);
35
36 kernel_size = _edata - _text;
37 kernel_codesize = __inittext_end - _text;
38 kernel_memsize = kernel_size + (_end - _edata);
39 *reserve_size = kernel_memsize;
40 *image_addr = (unsigned long)_text;
41
42 status = efi_kaslr_relocate_kernel(image_addr,
43 reserve_addr, reserve_size,
44 kernel_size, kernel_codesize,
45 kernel_memsize,
46 efi_kaslr_get_phys_seed(image_handle));
47 if (status != EFI_SUCCESS)
48 return status;
49
50 return EFI_SUCCESS;
51}
52
53asmlinkage void primary_entry(void);
54
55unsigned long primary_entry_offset(void)
56{
57 /*
58 * When built as part of the kernel, the EFI stub cannot branch to the
59 * kernel proper via the image header, as the PE/COFF header is
60 * strictly not part of the in-memory presentation of the image, only
61 * of the file representation. So instead, we need to jump to the
62 * actual entrypoint in the .text region of the image.
63 */
64 return (char *)primary_entry - _text;
65}
66
67void efi_icache_sync(unsigned long start, unsigned long end)
68{
69 caches_clean_inval_pou(start, end);
70}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org>
4 *
5 * This file implements the EFI boot stub for the arm64 kernel.
6 * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7 */
8
9
10#include <linux/efi.h>
11#include <asm/efi.h>
12#include <asm/memory.h>
13#include <asm/sections.h>
14
15#include "efistub.h"
16
17/*
18 * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
19 * to provide space, and fail to zero it). Check for this condition by double
20 * checking that the first and the last byte of the image are covered by the
21 * same EFI memory map entry.
22 */
23static bool check_image_region(u64 base, u64 size)
24{
25 struct efi_boot_memmap *map;
26 efi_status_t status;
27 bool ret = false;
28 int map_offset;
29
30 status = efi_get_memory_map(&map, false);
31 if (status != EFI_SUCCESS)
32 return false;
33
34 for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
35 efi_memory_desc_t *md = (void *)map->map + map_offset;
36 u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
37
38 /*
39 * Find the region that covers base, and return whether
40 * it covers base+size bytes.
41 */
42 if (base >= md->phys_addr && base < end) {
43 ret = (base + size) <= end;
44 break;
45 }
46 }
47
48 efi_bs_call(free_pool, map);
49
50 return ret;
51}
52
53efi_status_t handle_kernel_image(unsigned long *image_addr,
54 unsigned long *image_size,
55 unsigned long *reserve_addr,
56 unsigned long *reserve_size,
57 efi_loaded_image_t *image,
58 efi_handle_t image_handle)
59{
60 efi_status_t status;
61 unsigned long kernel_size, kernel_memsize = 0;
62 u32 phys_seed = 0;
63 u64 min_kimg_align = efi_get_kimg_min_align();
64
65 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
66 efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
67 void *p;
68
69 if (efi_nokaslr) {
70 efi_info("KASLR disabled on kernel command line\n");
71 } else if (efi_bs_call(handle_protocol, image_handle,
72 &li_fixed_proto, &p) == EFI_SUCCESS) {
73 efi_info("Image placement fixed by loader\n");
74 } else {
75 status = efi_get_random_bytes(sizeof(phys_seed),
76 (u8 *)&phys_seed);
77 if (status == EFI_NOT_FOUND) {
78 efi_info("EFI_RNG_PROTOCOL unavailable\n");
79 efi_nokaslr = true;
80 } else if (status != EFI_SUCCESS) {
81 efi_err("efi_get_random_bytes() failed (0x%lx)\n",
82 status);
83 efi_nokaslr = true;
84 }
85 }
86 }
87
88 if (image->image_base != _text)
89 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
90
91 if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
92 efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
93 SEGMENT_ALIGN >> 10);
94
95 kernel_size = _edata - _text;
96 kernel_memsize = kernel_size + (_end - _edata);
97 *reserve_size = kernel_memsize;
98
99 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
100 /*
101 * If KASLR is enabled, and we have some randomness available,
102 * locate the kernel at a randomized offset in physical memory.
103 */
104 status = efi_random_alloc(*reserve_size, min_kimg_align,
105 reserve_addr, phys_seed,
106 EFI_LOADER_CODE);
107 if (status != EFI_SUCCESS)
108 efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
109 } else {
110 status = EFI_OUT_OF_RESOURCES;
111 }
112
113 if (status != EFI_SUCCESS) {
114 if (!check_image_region((u64)_text, kernel_memsize)) {
115 efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
116 } else if (IS_ALIGNED((u64)_text, min_kimg_align) &&
117 (u64)_end < EFI_ALLOC_LIMIT) {
118 /*
119 * Just execute from wherever we were loaded by the
120 * UEFI PE/COFF loader if the placement is suitable.
121 */
122 *image_addr = (u64)_text;
123 *reserve_size = 0;
124 goto clean_image_to_poc;
125 }
126
127 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
128 ULONG_MAX, min_kimg_align,
129 EFI_LOADER_CODE);
130
131 if (status != EFI_SUCCESS) {
132 efi_err("Failed to relocate kernel\n");
133 *reserve_size = 0;
134 return status;
135 }
136 }
137
138 *image_addr = *reserve_addr;
139 memcpy((void *)*image_addr, _text, kernel_size);
140
141clean_image_to_poc:
142 /*
143 * Clean the copied Image to the PoC, and ensure it is not shadowed by
144 * stale icache entries from before relocation.
145 */
146 dcache_clean_poc(*image_addr, *image_addr + kernel_size);
147 asm("ic ialluis");
148
149 return EFI_SUCCESS;
150}