• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * EFI stub implementation that is shared by arm and arm64 architectures.
3  * This should be #included by the EFI stub implementation files.
4  *
5  * Copyright (C) 2013,2014 Linaro Limited
6  *     Roy Franz <roy.franz@linaro.org
7  * Copyright (C) 2013 Red Hat, Inc.
8  *     Mark Salter <msalter@redhat.com>
9  *
10  * This file is part of the Linux kernel, and is made available under the
11  * terms of the GNU General Public License version 2.
12  *
13  */
14 
15 #include <linux/efi.h>
16 #include <asm/efi.h>
17 
18 #include "efistub.h"
19 
efi_secureboot_enabled(efi_system_table_t * sys_table_arg)20 static int __init efi_secureboot_enabled(efi_system_table_t *sys_table_arg)
21 {
22 	static efi_guid_t const var_guid __initconst = EFI_GLOBAL_VARIABLE_GUID;
23 	static efi_char16_t const var_name[] __initconst = {
24 		'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
25 
26 	efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
27 	unsigned long size = sizeof(u8);
28 	efi_status_t status;
29 	u8 val;
30 
31 	status = f_getvar((efi_char16_t *)var_name, (efi_guid_t *)&var_guid,
32 			  NULL, &size, &val);
33 
34 	switch (status) {
35 	case EFI_SUCCESS:
36 		return val;
37 	case EFI_NOT_FOUND:
38 		return 0;
39 	default:
40 		return 1;
41 	}
42 }
43 
efi_open_volume(efi_system_table_t * sys_table_arg,void * __image,void ** __fh)44 efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
45 			     void *__image, void **__fh)
46 {
47 	efi_file_io_interface_t *io;
48 	efi_loaded_image_t *image = __image;
49 	efi_file_handle_t *fh;
50 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
51 	efi_status_t status;
52 	void *handle = (void *)(unsigned long)image->device_handle;
53 
54 	status = sys_table_arg->boottime->handle_protocol(handle,
55 				 &fs_proto, (void **)&io);
56 	if (status != EFI_SUCCESS) {
57 		efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
58 		return status;
59 	}
60 
61 	status = io->open_volume(io, &fh);
62 	if (status != EFI_SUCCESS)
63 		efi_printk(sys_table_arg, "Failed to open volume\n");
64 
65 	*__fh = fh;
66 	return status;
67 }
68 
efi_file_close(void * handle)69 efi_status_t efi_file_close(void *handle)
70 {
71 	efi_file_handle_t *fh = handle;
72 
73 	return fh->close(handle);
74 }
75 
76 efi_status_t
efi_file_read(void * handle,unsigned long * size,void * addr)77 efi_file_read(void *handle, unsigned long *size, void *addr)
78 {
79 	efi_file_handle_t *fh = handle;
80 
81 	return fh->read(handle, size, addr);
82 }
83 
84 
85 efi_status_t
efi_file_size(efi_system_table_t * sys_table_arg,void * __fh,efi_char16_t * filename_16,void ** handle,u64 * file_sz)86 efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
87 	      efi_char16_t *filename_16, void **handle, u64 *file_sz)
88 {
89 	efi_file_handle_t *h, *fh = __fh;
90 	efi_file_info_t *info;
91 	efi_status_t status;
92 	efi_guid_t info_guid = EFI_FILE_INFO_ID;
93 	unsigned long info_sz;
94 
95 	status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
96 	if (status != EFI_SUCCESS) {
97 		efi_printk(sys_table_arg, "Failed to open file: ");
98 		efi_char16_printk(sys_table_arg, filename_16);
99 		efi_printk(sys_table_arg, "\n");
100 		return status;
101 	}
102 
103 	*handle = h;
104 
105 	info_sz = 0;
106 	status = h->get_info(h, &info_guid, &info_sz, NULL);
107 	if (status != EFI_BUFFER_TOO_SMALL) {
108 		efi_printk(sys_table_arg, "Failed to get file info size\n");
109 		return status;
110 	}
111 
112 grow:
113 	status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
114 				 info_sz, (void **)&info);
115 	if (status != EFI_SUCCESS) {
116 		efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
117 		return status;
118 	}
119 
120 	status = h->get_info(h, &info_guid, &info_sz,
121 						   info);
122 	if (status == EFI_BUFFER_TOO_SMALL) {
123 		sys_table_arg->boottime->free_pool(info);
124 		goto grow;
125 	}
126 
127 	*file_sz = info->file_size;
128 	sys_table_arg->boottime->free_pool(info);
129 
130 	if (status != EFI_SUCCESS)
131 		efi_printk(sys_table_arg, "Failed to get initrd info\n");
132 
133 	return status;
134 }
135 
136 
137 
efi_char16_printk(efi_system_table_t * sys_table_arg,efi_char16_t * str)138 void efi_char16_printk(efi_system_table_t *sys_table_arg,
139 			      efi_char16_t *str)
140 {
141 	struct efi_simple_text_output_protocol *out;
142 
143 	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
144 	out->output_string(out, str);
145 }
146 
147 
148 /*
149  * This function handles the architcture specific differences between arm and
150  * arm64 regarding where the kernel image must be loaded and any memory that
151  * must be reserved. On failure it is required to free all
152  * all allocations it has made.
153  */
154 efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
155 				 unsigned long *image_addr,
156 				 unsigned long *image_size,
157 				 unsigned long *reserve_addr,
158 				 unsigned long *reserve_size,
159 				 unsigned long dram_base,
160 				 efi_loaded_image_t *image);
161 /*
162  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
163  * that is described in the PE/COFF header.  Most of the code is the same
164  * for both archictectures, with the arch-specific code provided in the
165  * handle_kernel_image() function.
166  */
efi_entry(void * handle,efi_system_table_t * sys_table,unsigned long * image_addr)167 unsigned long __init efi_entry(void *handle, efi_system_table_t *sys_table,
168 			       unsigned long *image_addr)
169 {
170 	efi_loaded_image_t *image;
171 	efi_status_t status;
172 	unsigned long image_size = 0;
173 	unsigned long dram_base;
174 	/* addr/point and size pairs for memory management*/
175 	unsigned long initrd_addr;
176 	u64 initrd_size = 0;
177 	unsigned long fdt_addr = 0;  /* Original DTB */
178 	u64 fdt_size = 0;  /* We don't get size from configuration table */
179 	char *cmdline_ptr = NULL;
180 	int cmdline_size = 0;
181 	unsigned long new_fdt_addr;
182 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
183 	unsigned long reserve_addr = 0;
184 	unsigned long reserve_size = 0;
185 
186 	/* Check if we were booted by the EFI firmware */
187 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
188 		goto fail;
189 
190 	pr_efi(sys_table, "Booting Linux Kernel...\n");
191 
192 	/*
193 	 * Get a handle to the loaded image protocol.  This is used to get
194 	 * information about the running image, such as size and the command
195 	 * line.
196 	 */
197 	status = sys_table->boottime->handle_protocol(handle,
198 					&loaded_image_proto, (void *)&image);
199 	if (status != EFI_SUCCESS) {
200 		pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
201 		goto fail;
202 	}
203 
204 	dram_base = get_dram_base(sys_table);
205 	if (dram_base == EFI_ERROR) {
206 		pr_efi_err(sys_table, "Failed to find DRAM base\n");
207 		goto fail;
208 	}
209 	status = handle_kernel_image(sys_table, image_addr, &image_size,
210 				     &reserve_addr,
211 				     &reserve_size,
212 				     dram_base, image);
213 	if (status != EFI_SUCCESS) {
214 		pr_efi_err(sys_table, "Failed to relocate kernel\n");
215 		goto fail;
216 	}
217 
218 	/*
219 	 * Get the command line from EFI, using the LOADED_IMAGE
220 	 * protocol. We are going to copy the command line into the
221 	 * device tree, so this can be allocated anywhere.
222 	 */
223 	cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
224 	if (!cmdline_ptr) {
225 		pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
226 		goto fail_free_image;
227 	}
228 
229 	status = efi_parse_options(cmdline_ptr);
230 	if (status != EFI_SUCCESS)
231 		pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n");
232 
233 	/*
234 	 * Unauthenticated device tree data is a security hazard, so
235 	 * ignore 'dtb=' unless UEFI Secure Boot is disabled.
236 	 */
237 	if (efi_secureboot_enabled(sys_table)) {
238 		pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
239 	} else {
240 		status = handle_cmdline_files(sys_table, image, cmdline_ptr,
241 					      "dtb=",
242 					      ~0UL, (unsigned long *)&fdt_addr,
243 					      (unsigned long *)&fdt_size);
244 
245 		if (status != EFI_SUCCESS) {
246 			pr_efi_err(sys_table, "Failed to load device tree!\n");
247 			goto fail_free_cmdline;
248 		}
249 	}
250 
251 	if (fdt_addr) {
252 		pr_efi(sys_table, "Using DTB from command line\n");
253 	} else {
254 		/* Look for a device tree configuration table entry. */
255 		fdt_addr = (uintptr_t)get_fdt(sys_table);
256 		if (fdt_addr)
257 			pr_efi(sys_table, "Using DTB from configuration table\n");
258 	}
259 
260 	if (!fdt_addr)
261 		pr_efi(sys_table, "Generating empty DTB\n");
262 
263 	status = handle_cmdline_files(sys_table, image, cmdline_ptr,
264 				      "initrd=", dram_base + SZ_512M,
265 				      (unsigned long *)&initrd_addr,
266 				      (unsigned long *)&initrd_size);
267 	if (status != EFI_SUCCESS)
268 		pr_efi_err(sys_table, "Failed initrd from command line!\n");
269 
270 	new_fdt_addr = fdt_addr;
271 	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
272 				&new_fdt_addr, dram_base + MAX_FDT_OFFSET,
273 				initrd_addr, initrd_size, cmdline_ptr,
274 				fdt_addr, fdt_size);
275 
276 	/*
277 	 * If all went well, we need to return the FDT address to the
278 	 * calling function so it can be passed to kernel as part of
279 	 * the kernel boot protocol.
280 	 */
281 	if (status == EFI_SUCCESS)
282 		return new_fdt_addr;
283 
284 	pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
285 
286 	efi_free(sys_table, initrd_size, initrd_addr);
287 	efi_free(sys_table, fdt_size, fdt_addr);
288 
289 fail_free_cmdline:
290 	efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
291 
292 fail_free_image:
293 	efi_free(sys_table, image_size, *image_addr);
294 	efi_free(sys_table, reserve_size, reserve_addr);
295 fail:
296 	return EFI_ERROR;
297 }
298 
299 /*
300  * This is the base address at which to start allocating virtual memory ranges
301  * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
302  * any allocation we choose, and eliminate the risk of a conflict after kexec.
303  * The value chosen is the largest non-zero power of 2 suitable for this purpose
304  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
305  * be mapped efficiently.
306  */
307 #define EFI_RT_VIRTUAL_BASE	0x40000000
308 
309 /*
310  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
311  *
312  * This function populates the virt_addr fields of all memory region descriptors
313  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
314  * are also copied to @runtime_map, and their total count is returned in @count.
315  */
efi_get_virtmap(efi_memory_desc_t * memory_map,unsigned long map_size,unsigned long desc_size,efi_memory_desc_t * runtime_map,int * count)316 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
317 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
318 		     int *count)
319 {
320 	u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
321 	efi_memory_desc_t *out = runtime_map;
322 	int l;
323 
324 	for (l = 0; l < map_size; l += desc_size) {
325 		efi_memory_desc_t *in = (void *)memory_map + l;
326 		u64 paddr, size;
327 
328 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
329 			continue;
330 
331 		/*
332 		 * Make the mapping compatible with 64k pages: this allows
333 		 * a 4k page size kernel to kexec a 64k page size kernel and
334 		 * vice versa.
335 		 */
336 		paddr = round_down(in->phys_addr, SZ_64K);
337 		size = round_up(in->num_pages * EFI_PAGE_SIZE +
338 				in->phys_addr - paddr, SZ_64K);
339 
340 		/*
341 		 * Avoid wasting memory on PTEs by choosing a virtual base that
342 		 * is compatible with section mappings if this region has the
343 		 * appropriate size and physical alignment. (Sections are 2 MB
344 		 * on 4k granule kernels)
345 		 */
346 		if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
347 			efi_virt_base = round_up(efi_virt_base, SZ_2M);
348 
349 		in->virt_addr = efi_virt_base + in->phys_addr - paddr;
350 		efi_virt_base += size;
351 
352 		memcpy(out, in, desc_size);
353 		out = (void *)out + desc_size;
354 		++*count;
355 	}
356 }
357