14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2bf457786SArd Biesheuvel /*
3bf457786SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
4bf457786SArd Biesheuvel  *
5bf457786SArd Biesheuvel  * This file implements the EFI boot stub for the arm64 kernel.
6bf457786SArd Biesheuvel  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7bf457786SArd Biesheuvel  */
80426a4e6SArd Biesheuvel 
90426a4e6SArd Biesheuvel 
10bf457786SArd Biesheuvel #include <linux/efi.h>
11bf457786SArd Biesheuvel #include <asm/efi.h>
12170976bcSMark Rutland #include <asm/memory.h>
136f05106eSArd Biesheuvel #include <asm/sections.h>
14bf457786SArd Biesheuvel 
152b5fe07aSArd Biesheuvel #include "efistub.h"
162b5fe07aSArd Biesheuvel 
1782046702SArd Biesheuvel /*
185b94046eSArd Biesheuvel  * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
195b94046eSArd Biesheuvel  * to provide space, and fail to zero it). Check for this condition by double
205b94046eSArd Biesheuvel  * checking that the first and the last byte of the image are covered by the
215b94046eSArd Biesheuvel  * same EFI memory map entry.
225b94046eSArd Biesheuvel  */
235b94046eSArd Biesheuvel static bool check_image_region(u64 base, u64 size)
245b94046eSArd Biesheuvel {
25eab31265SArd Biesheuvel 	struct efi_boot_memmap *map;
265b94046eSArd Biesheuvel 	efi_status_t status;
275b94046eSArd Biesheuvel 	bool ret = false;
285b94046eSArd Biesheuvel 	int map_offset;
295b94046eSArd Biesheuvel 
30171539f5SArd Biesheuvel 	status = efi_get_memory_map(&map, false);
315b94046eSArd Biesheuvel 	if (status != EFI_SUCCESS)
325b94046eSArd Biesheuvel 		return false;
335b94046eSArd Biesheuvel 
34eab31265SArd Biesheuvel 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
35eab31265SArd Biesheuvel 		efi_memory_desc_t *md = (void *)map->map + map_offset;
365b94046eSArd Biesheuvel 		u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
375b94046eSArd Biesheuvel 
385b94046eSArd Biesheuvel 		/*
395b94046eSArd Biesheuvel 		 * Find the region that covers base, and return whether
405b94046eSArd Biesheuvel 		 * it covers base+size bytes.
415b94046eSArd Biesheuvel 		 */
425b94046eSArd Biesheuvel 		if (base >= md->phys_addr && base < end) {
435b94046eSArd Biesheuvel 			ret = (base + size) <= end;
445b94046eSArd Biesheuvel 			break;
455b94046eSArd Biesheuvel 		}
465b94046eSArd Biesheuvel 	}
475b94046eSArd Biesheuvel 
48eab31265SArd Biesheuvel 	efi_bs_call(free_pool, map);
495b94046eSArd Biesheuvel 
505b94046eSArd Biesheuvel 	return ret;
515b94046eSArd Biesheuvel }
525b94046eSArd Biesheuvel 
53cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr,
54bf457786SArd Biesheuvel 				 unsigned long *image_size,
55bf457786SArd Biesheuvel 				 unsigned long *reserve_addr,
56bf457786SArd Biesheuvel 				 unsigned long *reserve_size,
57416a9f84SArd Biesheuvel 				 efi_loaded_image_t *image,
58416a9f84SArd Biesheuvel 				 efi_handle_t image_handle)
59bf457786SArd Biesheuvel {
60bf457786SArd Biesheuvel 	efi_status_t status;
61*61786170SArd Biesheuvel 	unsigned long kernel_size, kernel_codesize, kernel_memsize;
625d12da9dSArd Biesheuvel 	u32 phys_seed = 0;
63895bc3a1SArd Biesheuvel 	u64 min_kimg_align = efi_get_kimg_min_align();
643a262423SArd Biesheuvel 
652b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
6607768c55SArd Biesheuvel 		efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
6707768c55SArd Biesheuvel 		void *p;
6807768c55SArd Biesheuvel 
6907768c55SArd Biesheuvel 		if (efi_nokaslr) {
7007768c55SArd Biesheuvel 			efi_info("KASLR disabled on kernel command line\n");
7107768c55SArd Biesheuvel 		} else if (efi_bs_call(handle_protocol, image_handle,
7207768c55SArd Biesheuvel 				       &li_fixed_proto, &p) == EFI_SUCCESS) {
7307768c55SArd Biesheuvel 			efi_info("Image placement fixed by loader\n");
7407768c55SArd Biesheuvel 		} else {
75cd33a5c1SArd Biesheuvel 			status = efi_get_random_bytes(sizeof(phys_seed),
762b5fe07aSArd Biesheuvel 						      (u8 *)&phys_seed);
772b5fe07aSArd Biesheuvel 			if (status == EFI_NOT_FOUND) {
781c761ee9SMark Brown 				efi_info("EFI_RNG_PROTOCOL unavailable\n");
79d32de913SArd Biesheuvel 				efi_nokaslr = true;
802b5fe07aSArd Biesheuvel 			} else if (status != EFI_SUCCESS) {
811c761ee9SMark Brown 				efi_err("efi_get_random_bytes() failed (0x%lx)\n",
82d32de913SArd Biesheuvel 					status);
83d32de913SArd Biesheuvel 				efi_nokaslr = true;
842b5fe07aSArd Biesheuvel 			}
852b5fe07aSArd Biesheuvel 		}
862b5fe07aSArd Biesheuvel 	}
872dc10ad8SLinus Torvalds 
8882046702SArd Biesheuvel 	if (image->image_base != _text)
89793473c2SArvind Sankar 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
90bf457786SArd Biesheuvel 
91e9b7c3a4SMihai Carabas 	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
92e9b7c3a4SMihai Carabas 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
93e9b7c3a4SMihai Carabas 			SEGMENT_ALIGN >> 10);
94c32ac11dSArd Biesheuvel 
95bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
96*61786170SArd Biesheuvel 	kernel_codesize = __inittext_end - _text;
97bf457786SArd Biesheuvel 	kernel_memsize = kernel_size + (_end - _edata);
98120dc60dSArd Biesheuvel 	*reserve_size = kernel_memsize;
99bf457786SArd Biesheuvel 
1002b5fe07aSArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
101bf457786SArd Biesheuvel 		/*
1022b5fe07aSArd Biesheuvel 		 * If KASLR is enabled, and we have some randomness available,
1032b5fe07aSArd Biesheuvel 		 * locate the kernel at a randomized offset in physical memory.
1042b5fe07aSArd Biesheuvel 		 */
1053a262423SArd Biesheuvel 		status = efi_random_alloc(*reserve_size, min_kimg_align,
1069cf42bcaSArd Biesheuvel 					  reserve_addr, phys_seed,
1079cf42bcaSArd Biesheuvel 					  EFI_LOADER_CODE);
108ff80ef5bSArd Biesheuvel 		if (status != EFI_SUCCESS)
109ff80ef5bSArd Biesheuvel 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
1102b5fe07aSArd Biesheuvel 	} else {
11182046702SArd Biesheuvel 		status = EFI_OUT_OF_RESOURCES;
1122b5fe07aSArd Biesheuvel 	}
1132b5fe07aSArd Biesheuvel 
114bf457786SArd Biesheuvel 	if (status != EFI_SUCCESS) {
1155b94046eSArd Biesheuvel 		if (!check_image_region((u64)_text, kernel_memsize)) {
1165b94046eSArd Biesheuvel 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
117a37dac5cSArd Biesheuvel 		} else if (IS_ALIGNED((u64)_text, min_kimg_align) &&
118a37dac5cSArd Biesheuvel 			   (u64)_end < EFI_ALLOC_LIMIT) {
11982046702SArd Biesheuvel 			/*
12082046702SArd Biesheuvel 			 * Just execute from wherever we were loaded by the
121a37dac5cSArd Biesheuvel 			 * UEFI PE/COFF loader if the placement is suitable.
12282046702SArd Biesheuvel 			 */
12382046702SArd Biesheuvel 			*image_addr = (u64)_text;
12482046702SArd Biesheuvel 			*reserve_size = 0;
125*61786170SArd Biesheuvel 			return EFI_SUCCESS;
12682046702SArd Biesheuvel 		}
12782046702SArd Biesheuvel 
128e71356feSArd Biesheuvel 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
1299cf42bcaSArd Biesheuvel 						    ULONG_MAX, min_kimg_align,
1309cf42bcaSArd Biesheuvel 						    EFI_LOADER_CODE);
131bf457786SArd Biesheuvel 
132bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
133793473c2SArvind Sankar 			efi_err("Failed to relocate kernel\n");
1342b5fe07aSArd Biesheuvel 			*reserve_size = 0;
135bf457786SArd Biesheuvel 			return status;
136bf457786SArd Biesheuvel 		}
137bf457786SArd Biesheuvel 	}
138c2136dceSArd Biesheuvel 
139120dc60dSArd Biesheuvel 	*image_addr = *reserve_addr;
140c2136dceSArd Biesheuvel 	memcpy((void *)*image_addr, _text, kernel_size);
141*61786170SArd Biesheuvel 	caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize);
142aaeb3fc6SArd Biesheuvel 
143bf457786SArd Biesheuvel 	return EFI_SUCCESS;
144bf457786SArd Biesheuvel }
145*61786170SArd Biesheuvel 
146*61786170SArd Biesheuvel asmlinkage void primary_entry(void);
147*61786170SArd Biesheuvel 
148*61786170SArd Biesheuvel unsigned long primary_entry_offset(void)
149*61786170SArd Biesheuvel {
150*61786170SArd Biesheuvel 	/*
151*61786170SArd Biesheuvel 	 * When built as part of the kernel, the EFI stub cannot branch to the
152*61786170SArd Biesheuvel 	 * kernel proper via the image header, as the PE/COFF header is
153*61786170SArd Biesheuvel 	 * strictly not part of the in-memory presentation of the image, only
154*61786170SArd Biesheuvel 	 * of the file representation. So instead, we need to jump to the
155*61786170SArd Biesheuvel 	 * actual entrypoint in the .text region of the image.
156*61786170SArd Biesheuvel 	 */
157*61786170SArd Biesheuvel 	return (char *)primary_entry - _text;
158*61786170SArd Biesheuvel }
159