bootefi.c 14.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8
/*
 *  EFI application loader
 *
 *  Copyright (c) 2016 Alexander Graf
 */

#include <common.h>
9 10
#include <bootm.h>
#include <charset.h>
11
#include <command.h>
12
#include <dm.h>
13
#include <efi_loader.h>
14
#include <efi_selftest.h>
15
#include <errno.h>
16 17
#include <linux/libfdt.h>
#include <linux/libfdt_env.h>
18
#include <mapmem.h>
19
#include <memalign.h>
20
#include <asm/global_data.h>
S
Simon Glass 已提交
21
#include <asm-generic/sections.h>
22
#include <asm-generic/unaligned.h>
S
Simon Glass 已提交
23
#include <linux/linkage.h>
24 25

DECLARE_GLOBAL_DATA_PTR;
26

27 28
static struct efi_device_path *bootefi_image_path;
static struct efi_device_path *bootefi_device_path;
29

30 31 32 33 34 35 36 37 38
/*
 * Allow unaligned memory access.
 *
 * This routine is overridden by architectures providing this feature.
 */
void __weak allow_unaligned(void)
{
}

39 40 41 42 43 44 45 46 47 48 49
/*
 * Set the load options of an image from an environment variable.
 *
 * @loaded_image_info:	the image
 * @env_var:		name of the environment variable
 */
static void set_load_options(struct efi_loaded_image *loaded_image_info,
			     const char *env_var)
{
	size_t size;
	const char *env = env_get(env_var);
50
	u16 *pos;
51 52 53 54 55

	loaded_image_info->load_options = NULL;
	loaded_image_info->load_options_size = 0;
	if (!env)
		return;
56
	size = utf8_utf16_strlen(env) + 1;
57 58 59 60 61
	loaded_image_info->load_options = calloc(size, sizeof(u16));
	if (!loaded_image_info->load_options) {
		printf("ERROR: Out of memory\n");
		return;
	}
62 63
	pos = loaded_image_info->load_options;
	utf8_utf16_strcpy(&pos, env);
64 65 66
	loaded_image_info->load_options_size = size * 2;
}

67 68 69
/**
 * copy_fdt() - Copy the device tree to a new location available to EFI
 *
70
 * The FDT is copied to a suitable location within the EFI memory map.
71
 * Additional 12 KiB are added to the space in case the device tree needs to be
72 73
 * expanded later with fdt_open_into().
 *
74 75
 * @fdtp:	On entry a pointer to the flattened device tree.
 *		On exit a pointer to the copy of the flattened device tree.
76 77
 *		FDT start
 * Return:	status code
78
 */
79
static efi_status_t copy_fdt(void **fdtp)
80
{
81
	unsigned long fdt_ram_start = -1L, fdt_pages;
82 83
	efi_status_t ret = 0;
	void *fdt, *new_fdt;
84
	u64 new_fdt_addr;
85
	uint fdt_size;
86
	int i;
87

88 89 90
	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
		u64 ram_start = gd->bd->bi_dram[i].start;
		u64 ram_size = gd->bd->bi_dram[i].size;
91

92 93 94 95 96 97 98
		if (!ram_size)
			continue;

		if (ram_start < fdt_ram_start)
			fdt_ram_start = ram_start;
	}

99
	/*
100 101
	 * Give us at least 12 KiB of breathing room in case the device tree
	 * needs to be expanded later.
102
	 */
103
	fdt = *fdtp;
104 105
	fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
	fdt_size = fdt_pages << EFI_PAGE_SHIFT;
106

107 108 109 110 111 112
	/*
	 * Safe fdt location is at 127 MiB.
	 * On the sandbox convert from the sandbox address space.
	 */
	new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
					     fdt_size, 0);
113 114 115 116
	ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
				 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
				 &new_fdt_addr);
	if (ret != EFI_SUCCESS) {
117
		/* If we can't put it there, put it somewhere */
118
		new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
119 120 121 122
		ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
					 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
					 &new_fdt_addr);
		if (ret != EFI_SUCCESS) {
123
			printf("ERROR: Failed to reserve space for FDT\n");
124
			goto done;
125
		}
126
	}
127
	new_fdt = (void *)(uintptr_t)new_fdt_addr;
128 129 130
	memcpy(new_fdt, fdt, fdt_totalsize(fdt));
	fdt_set_totalsize(new_fdt, fdt_size);

131
	*fdtp = (void *)(uintptr_t)new_fdt_addr;
132 133
done:
	return ret;
134 135
}

136 137 138 139 140 141 142 143 144 145
/*
 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
 *
 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
 * ignored because this is not critical and we would rather continue to try to
 * boot.
 *
 * @fdt: Pointer to device tree
 */
static void efi_carve_out_dt_rsv(void *fdt)
146 147 148 149 150 151 152 153 154 155 156
{
	int nr_rsv, i;
	uint64_t addr, size, pages;

	nr_rsv = fdt_num_mem_rsv(fdt);

	/* Look for an existing entry and add it to the efi mem map. */
	for (i = 0; i < nr_rsv; i++) {
		if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
			continue;

157 158 159
		/* Convert from sandbox address space. */
		addr = (uintptr_t)map_sysmem(addr, 0);

160
		pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
161
		addr &= ~EFI_PAGE_MASK;
162 163 164
		if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
					false))
			printf("FDT memrsv map %d: Failed to add to map\n", i);
165 166 167
	}
}

168
static efi_status_t efi_install_fdt(ulong fdt_addr)
169 170 171
{
	bootm_headers_t img = { 0 };
	efi_status_t ret;
172
	void *fdt;
173

174
	fdt = map_sysmem(fdt_addr, 0);
175 176 177 178 179
	if (fdt_check_header(fdt)) {
		printf("ERROR: invalid device tree\n");
		return EFI_INVALID_PARAMETER;
	}

180 181 182
	/* Create memory reservation as indicated by the device tree */
	efi_carve_out_dt_rsv(fdt);

183
	/* Prepare fdt for payload */
184
	ret = copy_fdt(&fdt);
185 186
	if (ret)
		return ret;
187 188 189 190 191 192 193 194 195 196 197 198 199 200

	if (image_setup_libfdt(&img, fdt, 0, NULL)) {
		printf("ERROR: failed to process device tree\n");
		return EFI_LOAD_ERROR;
	}

	/* Link to it in the efi tables */
	ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
	if (ret != EFI_SUCCESS)
		return EFI_OUT_OF_RESOURCES;

	return ret;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static efi_status_t bootefi_run_prepare(const char *load_options_path,
		struct efi_device_path *device_path,
		struct efi_device_path *image_path,
		struct efi_loaded_image_obj **image_objp,
		struct efi_loaded_image **loaded_image_infop)
{
	efi_status_t ret;

	ret = efi_setup_loaded_image(device_path, image_path, image_objp,
				     loaded_image_infop);
	if (ret != EFI_SUCCESS)
		return ret;

	/* Transfer environment variable as load options */
	set_load_options(*loaded_image_infop, load_options_path);

	return 0;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233
/**
 * bootefi_run_finish() - finish up after running an EFI test
 *
 * @loaded_image_info: Pointer to a struct which holds the loaded image info
 * @image_objj: Pointer to a struct which holds the loaded image object
 */
static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
			       struct efi_loaded_image *loaded_image_info)
{
	efi_restore_gd();
	free(loaded_image_info->load_options);
	efi_delete_handle(&image_obj->header);
}

234 235 236 237 238 239 240 241 242 243
/**
 * do_bootefi_exec() - execute EFI binary
 *
 * @efi:		address of the binary
 * @device_path:	path of the device from which the binary was loaded
 * @image_path:		device path of the binary
 * Return:		status code
 *
 * Load the EFI binary into a newly assigned memory unwinding the relocation
 * information, install the loaded image protocol, and call the binary.
244
 */
245
static efi_status_t do_bootefi_exec(void *efi,
246 247
				    struct efi_device_path *device_path,
				    struct efi_device_path *image_path)
248
{
249
	efi_handle_t mem_handle = NULL;
250
	struct efi_device_path *memdp = NULL;
251
	efi_status_t ret;
252
	struct efi_loaded_image_obj *image_obj = NULL;
253
	struct efi_loaded_image *loaded_image_info = NULL;
254

255 256 257
	/*
	 * Special case for efi payload not loaded from disk, such as
	 * 'bootefi hello' or for example payload loaded directly into
H
Heinrich Schuchardt 已提交
258
	 * memory via JTAG, etc:
259 260 261 262
	 */
	if (!device_path && !image_path) {
		printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
		/* actual addresses filled in after efi_load_pe() */
263
		memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
264
		device_path = image_path = memdp;
265 266 267 268 269 270
		/*
		 * Grub expects that the device path of the loaded image is
		 * installed on a handle.
		 */
		ret = efi_create_handle(&mem_handle);
		if (ret != EFI_SUCCESS)
271
			return ret; /* TODO: leaks device_path */
272
		ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
273 274
				       device_path);
		if (ret != EFI_SUCCESS)
275
			goto err_add_protocol;
276 277 278 279
	} else {
		assert(device_path && image_path);
	}

280 281 282
	ret = bootefi_run_prepare("bootargs", device_path, image_path,
				  &image_obj, &loaded_image_info);
	if (ret)
283
		goto err_prepare;
284

285
	/* Load the EFI payload */
286 287
	ret = efi_load_pe(image_obj, efi, loaded_image_info);
	if (ret != EFI_SUCCESS)
288
		goto err_prepare;
289

290 291
	if (memdp) {
		struct efi_device_path_memory *mdp = (void *)memdp;
292 293
		mdp->memory_type = loaded_image_info->image_code_type;
		mdp->start_address = (uintptr_t)loaded_image_info->image_base;
294
		mdp->end_address = mdp->start_address +
295
				loaded_image_info->image_size;
296 297
	}

R
Rob Clark 已提交
298 299 300 301
	/* we don't support much: */
	env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
		"{ro,boot}(blob)0000000000000000");

302
	/* Call our payload! */
303
	debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry);
304
	ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL));
305

306
err_prepare:
307
	/* image has returned, loaded-image obj goes *poof*: */
308 309 310
	bootefi_run_finish(image_obj, loaded_image_info);

err_add_protocol:
311 312
	if (mem_handle)
		efi_delete_handle(mem_handle);
313 314

	return ret;
315 316
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
/**
 * bootefi_test_prepare() - prepare to run an EFI test
 *
 * This sets things up so we can call EFI functions. This involves preparing
 * the 'gd' pointer and setting up the load ed image data structures.
 *
 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
 *    loaded image object. This struct will be inited by this function before
 *    use.
 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
 *    info. This struct will be inited by this function before use.
 * @path: File path to the test being run (often just the test name with a
 *    backslash before it
 * @test_func: Address of the test function that is being run
332
 * @load_options_path: U-Boot environment variable to use as load options
333 334 335 336
 * @return 0 if OK, -ve on error
 */
static efi_status_t bootefi_test_prepare
		(struct efi_loaded_image_obj **image_objp,
337 338
		struct efi_loaded_image **loaded_image_infop, const char *path,
		ulong test_func, const char *load_options_path)
339 340 341 342 343 344 345 346 347 348 349
{
	/* Construct a dummy device path */
	bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
					      (uintptr_t)test_func,
					      (uintptr_t)test_func);
	if (!bootefi_device_path)
		return EFI_OUT_OF_RESOURCES;
	bootefi_image_path = efi_dp_from_file(NULL, 0, path);
	if (!bootefi_image_path)
		return EFI_OUT_OF_RESOURCES;

350 351 352
	return bootefi_run_prepare(load_options_path, bootefi_device_path,
				   bootefi_image_path, image_objp,
				   loaded_image_infop);
353 354 355 356
}

#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */

357
static int do_bootefi_bootmgr_exec(void)
R
Rob Clark 已提交
358 359 360 361 362 363 364 365 366 367
{
	struct efi_device_path *device_path, *file_path;
	void *addr;
	efi_status_t r;

	addr = efi_bootmgr_load(&device_path, &file_path);
	if (!addr)
		return 1;

	printf("## Starting EFI application at %p ...\n", addr);
368
	r = do_bootefi_exec(addr, device_path, file_path);
R
Rob Clark 已提交
369 370 371 372 373 374 375 376 377
	printf("## Application terminated, r = %lu\n",
	       r & ~EFI_ERROR_MASK);

	if (r != EFI_SUCCESS)
		return 1;

	return 0;
}

378 379 380
/* Interpreter command to boot an arbitrary EFI image from memory */
static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
381 382
	unsigned long addr;
	char *saddr;
383
	efi_status_t r;
384
	unsigned long fdt_addr;
385

386 387 388
	/* Allow unaligned memory access */
	allow_unaligned();

389 390
	switch_to_non_secure_mode();

391 392 393 394 395 396 397 398
	/* Initialize EFI drivers */
	r = efi_init_obj_list();
	if (r != EFI_SUCCESS) {
		printf("Error: Cannot set up EFI drivers, r = %lu\n",
		       r & ~EFI_ERROR_MASK);
		return CMD_RET_FAILURE;
	}

399
	if (argc < 2)
400
		return CMD_RET_USAGE;
401 402

	if (argc > 2) {
403
		fdt_addr = simple_strtoul(argv[2], NULL, 16);
404 405 406
		if (!fdt_addr && *argv[2] != '0')
			return CMD_RET_USAGE;
		/* Install device tree */
407
		r = efi_install_fdt(fdt_addr);
408 409 410 411 412 413 414 415 416
		if (r != EFI_SUCCESS) {
			printf("ERROR: failed to install device tree\n");
			return CMD_RET_FAILURE;
		}
	} else {
		/* Remove device tree. EFI_NOT_FOUND can be ignored here */
		efi_install_configuration_table(&efi_guid_fdt, NULL);
		printf("WARNING: booting without device tree\n");
	}
417 418
#ifdef CONFIG_CMD_BOOTEFI_HELLO
	if (!strcmp(argv[1], "hello")) {
419
		ulong size = __efi_helloworld_end - __efi_helloworld_begin;
420

421 422 423 424 425
		saddr = env_get("loadaddr");
		if (saddr)
			addr = simple_strtoul(saddr, NULL, 16);
		else
			addr = CONFIG_SYS_LOAD_ADDR;
426
		memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
427
	} else
428 429 430
#endif
#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
	if (!strcmp(argv[1], "selftest")) {
431
		struct efi_loaded_image_obj *image_obj;
432
		struct efi_loaded_image *loaded_image_info;
433

434
		if (bootefi_test_prepare(&image_obj, &loaded_image_info,
435 436
					 "\\selftest", (uintptr_t)&efi_selftest,
					 "efi_selftest"))
437
			return CMD_RET_FAILURE;
438

439
		/* Execute the test */
440
		r = efi_selftest(&image_obj->header, &systab);
441
		bootefi_run_finish(image_obj, loaded_image_info);
442
		return r != EFI_SUCCESS;
443
	} else
444
#endif
R
Rob Clark 已提交
445
	if (!strcmp(argv[1], "bootmgr")) {
446
		return do_bootefi_bootmgr_exec();
R
Rob Clark 已提交
447
	} else {
448
		saddr = argv[1];
449

450
		addr = simple_strtoul(saddr, NULL, 16);
451 452 453
		/* Check that a numeric value was passed */
		if (!addr && *saddr != '0')
			return CMD_RET_USAGE;
454

455 456
	}

457
	printf("## Starting EFI application at %08lx ...\n", addr);
458
	r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
459
			    bootefi_image_path);
460 461
	printf("## Application terminated, r = %lu\n",
	       r & ~EFI_ERROR_MASK);
462

463 464 465 466
	if (r != EFI_SUCCESS)
		return 1;
	else
		return 0;
467 468 469 470
}

#ifdef CONFIG_SYS_LONGHELP
static char bootefi_help_text[] =
471 472 473
	"<image address> [fdt address]\n"
	"  - boot EFI payload stored at address <image address>.\n"
	"    If specified, the device tree located at <fdt address> gets\n"
474 475
	"    exposed as EFI configuration table.\n"
#ifdef CONFIG_CMD_BOOTEFI_HELLO
476 477 478 479
	"bootefi hello\n"
	"  - boot a sample Hello World application stored within U-Boot\n"
#endif
#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
480
	"bootefi selftest [fdt address]\n"
481
	"  - boot an EFI selftest application stored within U-Boot\n"
482 483
	"    Use environment variable efi_selftest to select a single test.\n"
	"    Use 'setenv efi_selftest list' to enumerate all tests.\n"
484
#endif
485
	"bootefi bootmgr [fdt addr]\n"
R
Rob Clark 已提交
486 487 488 489
	"  - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
	"\n"
	"    If specified, the device tree located at <fdt address> gets\n"
	"    exposed as EFI configuration table.\n";
490 491 492
#endif

U_BOOT_CMD(
493
	bootefi, 3, 0, do_bootefi,
S
Sergey Kubushyn 已提交
494
	"Boots an EFI payload from memory",
495 496
	bootefi_help_text
);
497

498 499
void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
{
500 501
	struct efi_device_path *device, *image;
	efi_status_t ret;
502

503 504 505
	/* efi_set_bootdev is typically called repeatedly, recover memory */
	efi_free_pool(bootefi_device_path);
	efi_free_pool(bootefi_image_path);
R
Rob Clark 已提交
506

507 508 509 510
	ret = efi_dp_from_name(dev, devnr, path, &device, &image);
	if (ret == EFI_SUCCESS) {
		bootefi_device_path = device;
		bootefi_image_path = image;
511
	} else {
512 513
		bootefi_device_path = NULL;
		bootefi_image_path = NULL;
514
	}
515
}