blob: 2363fee9211c98d597a6863cec5771bcd8058261 [file] [log] [blame]
Ard Biesheuvel4febfb82019-02-02 10:41:15 +01001// SPDX-License-Identifier: GPL-2.0
Mark Salter3c7f2552014-04-15 22:47:52 -04002/*
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>
Mark Salter3c7f2552014-04-15 22:47:52 -04007 */
Ard Biesheuvel0426a4e2017-08-18 20:49:36 +01008
Ard Biesheuvel0426a4e2017-08-18 20:49:36 +01009
Mark Salter3c7f2552014-04-15 22:47:52 -040010#include <linux/efi.h>
Ard Biesheuvela13b0072014-07-02 14:54:41 +020011#include <asm/efi.h>
Mark Rutland170976b2017-07-14 15:54:36 +010012#include <asm/memory.h>
Ard Biesheuvel6f05106e2020-02-10 17:02:32 +010013#include <asm/sections.h>
Ard Biesheuvel42b55732016-02-17 12:36:02 +000014#include <asm/sysreg.h>
Mark Salter3c7f2552014-04-15 22:47:52 -040015
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +010016#include "efistub.h"
17
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +010018efi_status_t check_platform_features(void)
Ard Biesheuvel42b55732016-02-17 12:36:02 +000019{
20 u64 tg;
21
22 /* UEFI mandates support for 4 KB granularity, no need to check */
23 if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
24 return EFI_SUCCESS;
25
26 tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
James Morse26f55382021-03-10 11:23:10 +053027 if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
Ard Biesheuvel42b55732016-02-17 12:36:02 +000028 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
Arvind Sankar793473c2020-04-30 14:28:35 -040029 efi_err("This 64 KB granular kernel is not supported by your CPU\n");
Ard Biesheuvel42b55732016-02-17 12:36:02 +000030 else
Arvind Sankar793473c2020-04-30 14:28:35 -040031 efi_err("This 16 KB granular kernel is not supported by your CPU\n");
Ard Biesheuvel42b55732016-02-17 12:36:02 +000032 return EFI_UNSUPPORTED;
33 }
34 return EFI_SUCCESS;
35}
Mark Salter3c7f2552014-04-15 22:47:52 -040036
Ard Biesheuvel82046702020-03-27 17:23:52 +010037/*
Ard Biesheuvel5b940462021-07-26 11:38:41 +020038 * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
39 * to provide space, and fail to zero it). Check for this condition by double
40 * checking that the first and the last byte of the image are covered by the
41 * same EFI memory map entry.
42 */
43static bool check_image_region(u64 base, u64 size)
44{
45 unsigned long map_size, desc_size, buff_size;
46 efi_memory_desc_t *memory_map;
47 struct efi_boot_memmap map;
48 efi_status_t status;
49 bool ret = false;
50 int map_offset;
51
52 map.map = &memory_map;
53 map.map_size = &map_size;
54 map.desc_size = &desc_size;
55 map.desc_ver = NULL;
56 map.key_ptr = NULL;
57 map.buff_size = &buff_size;
58
59 status = efi_get_memory_map(&map);
60 if (status != EFI_SUCCESS)
61 return false;
62
63 for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
64 efi_memory_desc_t *md = (void *)memory_map + map_offset;
65 u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
66
67 /*
68 * Find the region that covers base, and return whether
69 * it covers base+size bytes.
70 */
71 if (base >= md->phys_addr && base < end) {
72 ret = (base + size) <= end;
73 break;
74 }
75 }
76
77 efi_bs_call(free_pool, memory_map);
78
79 return ret;
80}
81
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +010082efi_status_t handle_kernel_image(unsigned long *image_addr,
Ard Biesheuveldae31fd2016-02-17 12:35:57 +000083 unsigned long *image_size,
84 unsigned long *reserve_addr,
85 unsigned long *reserve_size,
Ard Biesheuveldae31fd2016-02-17 12:35:57 +000086 efi_loaded_image_t *image)
Mark Salter3c7f2552014-04-15 22:47:52 -040087{
88 efi_status_t status;
89 unsigned long kernel_size, kernel_memsize = 0;
Ard Biesheuvel5d12da92020-04-13 15:36:37 +020090 u32 phys_seed = 0;
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +010091
Ard Biesheuvel3a262422021-07-22 12:10:31 +020092 /*
93 * Although relocatable kernels can fix up the misalignment with
94 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
95 * subtly out of sync with those recorded in the vmlinux when kaslr is
96 * disabled but the image required relocation anyway. Therefore retain
97 * 2M alignment if KASLR was explicitly disabled, even if it was not
98 * going to be activated to begin with.
99 */
100 u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
101
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100102 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
Ard Biesheuvel980771f2020-04-16 18:45:24 +0200103 if (!efi_nokaslr) {
Ard Biesheuvelcd33a5c2019-12-24 16:10:19 +0100104 status = efi_get_random_bytes(sizeof(phys_seed),
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100105 (u8 *)&phys_seed);
106 if (status == EFI_NOT_FOUND) {
Mark Brown1c761ee2021-01-20 16:38:10 +0000107 efi_info("EFI_RNG_PROTOCOL unavailable\n");
Ard Biesheuveld32de912020-09-26 10:52:42 +0200108 efi_nokaslr = true;
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100109 } else if (status != EFI_SUCCESS) {
Mark Brown1c761ee2021-01-20 16:38:10 +0000110 efi_err("efi_get_random_bytes() failed (0x%lx)\n",
Ard Biesheuveld32de912020-09-26 10:52:42 +0200111 status);
112 efi_nokaslr = true;
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100113 }
114 } else {
Arvind Sankar793473c2020-04-30 14:28:35 -0400115 efi_info("KASLR disabled on kernel command line\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100116 }
117 }
Ard Biesheuvel73effcc2015-10-29 15:07:25 +0100118
Ard Biesheuvel82046702020-03-27 17:23:52 +0100119 if (image->image_base != _text)
Arvind Sankar793473c2020-04-30 14:28:35 -0400120 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
Mark Salter3c7f2552014-04-15 22:47:52 -0400121
Ard Biesheuvelc32ac112021-07-26 16:31:44 +0200122 if (!IS_ALIGNED((u64)_text, EFI_KIMG_ALIGN))
123 efi_err("FIRMWARE BUG: kernel image not aligned on %ldk boundary\n",
124 EFI_KIMG_ALIGN >> 10);
125
Mark Salter3c7f2552014-04-15 22:47:52 -0400126 kernel_size = _edata - _text;
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100127 kernel_memsize = kernel_size + (_end - _edata);
Ard Biesheuvel120dc602020-08-25 15:54:40 +0200128 *reserve_size = kernel_memsize;
Ard Biesheuvele38457c2015-07-24 12:38:27 +0100129
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100130 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
Ard Biesheuvele38457c2015-07-24 12:38:27 +0100131 /*
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100132 * If KASLR is enabled, and we have some randomness available,
133 * locate the kernel at a randomized offset in physical memory.
134 */
Ard Biesheuvel3a262422021-07-22 12:10:31 +0200135 status = efi_random_alloc(*reserve_size, min_kimg_align,
Ard Biesheuvel5d12da92020-04-13 15:36:37 +0200136 reserve_addr, phys_seed);
Ard Biesheuvelff80ef52021-07-26 16:24:01 +0200137 if (status != EFI_SUCCESS)
138 efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100139 } else {
Ard Biesheuvel82046702020-03-27 17:23:52 +0100140 status = EFI_OUT_OF_RESOURCES;
Mark Salter3c7f2552014-04-15 22:47:52 -0400141 }
142
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100143 if (status != EFI_SUCCESS) {
Ard Biesheuvel5b940462021-07-26 11:38:41 +0200144 if (!check_image_region((u64)_text, kernel_memsize)) {
145 efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
Ard Biesheuvel3a262422021-07-22 12:10:31 +0200146 } else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
Ard Biesheuvel82046702020-03-27 17:23:52 +0100147 /*
148 * Just execute from wherever we were loaded by the
149 * UEFI PE/COFF loader if the alignment is suitable.
150 */
151 *image_addr = (u64)_text;
152 *reserve_size = 0;
153 return EFI_SUCCESS;
154 }
155
Ard Biesheuvele71356f2020-03-31 10:59:39 +0200156 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
Ard Biesheuvel3a262422021-07-22 12:10:31 +0200157 ULONG_MAX, min_kimg_align);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100158
159 if (status != EFI_SUCCESS) {
Arvind Sankar793473c2020-04-30 14:28:35 -0400160 efi_err("Failed to relocate kernel\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100161 *reserve_size = 0;
162 return status;
163 }
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100164 }
Ard Biesheuvelc2136dce2020-03-29 10:05:43 +0200165
Ard Biesheuvel120dc602020-08-25 15:54:40 +0200166 *image_addr = *reserve_addr;
Ard Biesheuvelc2136dce2020-03-29 10:05:43 +0200167 memcpy((void *)*image_addr, _text, kernel_size);
Mark Salter3c7f2552014-04-15 22:47:52 -0400168
169 return EFI_SUCCESS;
170}