1bf457786SArd Biesheuvel /*
2bf457786SArd Biesheuvel  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
3bf457786SArd Biesheuvel  *
4bf457786SArd Biesheuvel  * This file implements the EFI boot stub for the arm64 kernel.
5bf457786SArd Biesheuvel  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
6bf457786SArd Biesheuvel  *
7bf457786SArd Biesheuvel  * This program is free software; you can redistribute it and/or modify
8bf457786SArd Biesheuvel  * it under the terms of the GNU General Public License version 2 as
9bf457786SArd Biesheuvel  * published by the Free Software Foundation.
10bf457786SArd Biesheuvel  *
11bf457786SArd Biesheuvel  */
12bf457786SArd Biesheuvel #include <linux/efi.h>
13bf457786SArd Biesheuvel #include <asm/efi.h>
14bf457786SArd Biesheuvel #include <asm/sections.h>
15bf457786SArd Biesheuvel 
16bf457786SArd Biesheuvel efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg,
17bf457786SArd Biesheuvel 					unsigned long *image_addr,
18bf457786SArd Biesheuvel 					unsigned long *image_size,
19bf457786SArd Biesheuvel 					unsigned long *reserve_addr,
20bf457786SArd Biesheuvel 					unsigned long *reserve_size,
21bf457786SArd Biesheuvel 					unsigned long dram_base,
22bf457786SArd Biesheuvel 					efi_loaded_image_t *image)
23bf457786SArd Biesheuvel {
24bf457786SArd Biesheuvel 	efi_status_t status;
25bf457786SArd Biesheuvel 	unsigned long kernel_size, kernel_memsize = 0;
26bf457786SArd Biesheuvel 	unsigned long nr_pages;
27bf457786SArd Biesheuvel 	void *old_image_addr = (void *)*image_addr;
28bf457786SArd Biesheuvel 
29bf457786SArd Biesheuvel 	/* Relocate the image, if required. */
30bf457786SArd Biesheuvel 	kernel_size = _edata - _text;
31bf457786SArd Biesheuvel 	if (*image_addr != (dram_base + TEXT_OFFSET)) {
32bf457786SArd Biesheuvel 		kernel_memsize = kernel_size + (_end - _edata);
33bf457786SArd Biesheuvel 
34bf457786SArd Biesheuvel 		/*
35bf457786SArd Biesheuvel 		 * First, try a straight allocation at the preferred offset.
36bf457786SArd Biesheuvel 		 * This will work around the issue where, if dram_base == 0x0,
37bf457786SArd Biesheuvel 		 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
38bf457786SArd Biesheuvel 		 * address of the allocation to be mistaken for a FAIL return
39bf457786SArd Biesheuvel 		 * value or a NULL pointer). It will also ensure that, on
40bf457786SArd Biesheuvel 		 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
41bf457786SArd Biesheuvel 		 * interval is partially occupied by the firmware (like on APM
42bf457786SArd Biesheuvel 		 * Mustang), we can still place the kernel at the address
43bf457786SArd Biesheuvel 		 * 'dram_base + TEXT_OFFSET'.
44bf457786SArd Biesheuvel 		 */
45bf457786SArd Biesheuvel 		*image_addr = *reserve_addr = dram_base + TEXT_OFFSET;
46bf457786SArd Biesheuvel 		nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) /
47bf457786SArd Biesheuvel 			   EFI_PAGE_SIZE;
48bf457786SArd Biesheuvel 		status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
49bf457786SArd Biesheuvel 					EFI_LOADER_DATA, nr_pages,
50bf457786SArd Biesheuvel 					(efi_physical_addr_t *)reserve_addr);
51bf457786SArd Biesheuvel 		if (status != EFI_SUCCESS) {
52bf457786SArd Biesheuvel 			kernel_memsize += TEXT_OFFSET;
53bf457786SArd Biesheuvel 			status = efi_low_alloc(sys_table_arg, kernel_memsize,
54bf457786SArd Biesheuvel 					       SZ_2M, reserve_addr);
55bf457786SArd Biesheuvel 
56bf457786SArd Biesheuvel 			if (status != EFI_SUCCESS) {
57bf457786SArd Biesheuvel 				pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
58bf457786SArd Biesheuvel 				return status;
59bf457786SArd Biesheuvel 			}
60bf457786SArd Biesheuvel 			*image_addr = *reserve_addr + TEXT_OFFSET;
61bf457786SArd Biesheuvel 		}
62bf457786SArd Biesheuvel 		memcpy((void *)*image_addr, old_image_addr, kernel_size);
63bf457786SArd Biesheuvel 		*reserve_size = kernel_memsize;
64bf457786SArd Biesheuvel 	}
65bf457786SArd Biesheuvel 
66bf457786SArd Biesheuvel 
67bf457786SArd Biesheuvel 	return EFI_SUCCESS;
68bf457786SArd Biesheuvel }
69