2018-05-07 21:02:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-09 23:27:20 +00:00
|
|
|
/*
|
|
|
|
* EFI application loader
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Alexander Graf
|
|
|
|
*/
|
|
|
|
|
2020-07-17 18:21:00 +00:00
|
|
|
#define LOG_CATEGORY LOGC_EFI
|
|
|
|
|
2016-03-09 23:27:20 +00:00
|
|
|
#include <command.h>
|
2023-11-21 01:29:44 +00:00
|
|
|
#include <efi.h>
|
2016-03-09 23:27:20 +00:00
|
|
|
#include <efi_loader.h>
|
2023-11-21 01:29:44 +00:00
|
|
|
#include <exports.h>
|
2020-07-17 18:21:00 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2018-06-18 15:22:58 +00:00
|
|
|
#include <mapmem.h>
|
2023-11-21 01:29:44 +00:00
|
|
|
#include <vsprintf.h>
|
2016-09-25 21:27:32 +00:00
|
|
|
#include <asm-generic/sections.h>
|
2023-11-21 01:29:44 +00:00
|
|
|
#include <asm/global_data.h>
|
|
|
|
#include <linux/string.h>
|
2016-04-11 14:55:26 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2016-03-09 23:27:20 +00:00
|
|
|
|
2023-11-21 01:29:43 +00:00
|
|
|
static struct efi_device_path *test_image_path;
|
|
|
|
static struct efi_device_path *test_device_path;
|
2023-11-21 01:29:42 +00:00
|
|
|
|
2019-04-19 03:22:31 +00:00
|
|
|
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;
|
2020-01-03 21:53:42 +00:00
|
|
|
u16 *load_options;
|
2019-04-19 03:22:31 +00:00
|
|
|
|
|
|
|
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 */
|
2020-08-07 15:47:13 +00:00
|
|
|
return efi_env_set_load_options((efi_handle_t)*image_objp,
|
|
|
|
load_options_path,
|
|
|
|
&load_options);
|
2019-04-19 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:14:37 +00:00
|
|
|
/**
|
|
|
|
* bootefi_test_prepare() - prepare to run an EFI test
|
|
|
|
*
|
2019-01-12 13:42:40 +00:00
|
|
|
* Prepare to run a test as if it were provided by a loaded image.
|
2018-11-26 03:14:37 +00:00
|
|
|
*
|
2019-01-12 13:42:40 +00:00
|
|
|
* @image_objp: pointer to be set to the loaded image handle
|
|
|
|
* @loaded_image_infop: pointer to be set to the loaded image protocol
|
|
|
|
* @path: dummy file path used to construct the device path
|
|
|
|
* set in the loaded image protocol
|
|
|
|
* @load_options_path: name of a U-Boot environment variable. Its value is
|
|
|
|
* set as load options in the loaded image protocol.
|
|
|
|
* Return: status code
|
2018-11-26 03:14:37 +00:00
|
|
|
*/
|
|
|
|
static efi_status_t bootefi_test_prepare
|
|
|
|
(struct efi_loaded_image_obj **image_objp,
|
2019-01-12 13:42:40 +00:00
|
|
|
struct efi_loaded_image **loaded_image_infop, const char *path,
|
|
|
|
const char *load_options_path)
|
2018-11-26 03:14:37 +00:00
|
|
|
{
|
2019-01-12 13:42:40 +00:00
|
|
|
efi_status_t ret;
|
|
|
|
|
2018-11-26 03:14:37 +00:00
|
|
|
/* Construct a dummy device path */
|
2023-11-21 01:29:43 +00:00
|
|
|
test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
|
|
|
|
if (!test_device_path)
|
2018-11-26 03:14:37 +00:00
|
|
|
return EFI_OUT_OF_RESOURCES;
|
2019-01-12 13:42:40 +00:00
|
|
|
|
2023-11-21 01:29:43 +00:00
|
|
|
test_image_path = efi_dp_from_file(NULL, path);
|
|
|
|
if (!test_image_path) {
|
2019-01-12 13:42:40 +00:00
|
|
|
ret = EFI_OUT_OF_RESOURCES;
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:29:43 +00:00
|
|
|
ret = bootefi_run_prepare(load_options_path, test_device_path,
|
|
|
|
test_image_path, image_objp,
|
2019-01-12 13:42:40 +00:00
|
|
|
loaded_image_infop);
|
|
|
|
if (ret == EFI_SUCCESS)
|
|
|
|
return ret;
|
2018-11-26 03:14:37 +00:00
|
|
|
|
2019-01-12 13:42:40 +00:00
|
|
|
failure:
|
2023-11-21 01:29:43 +00:00
|
|
|
efi_free_pool(test_device_path);
|
|
|
|
efi_free_pool(test_image_path);
|
|
|
|
/* TODO: not sure calling clear function is necessary */
|
2021-01-12 11:46:24 +00:00
|
|
|
efi_clear_bootdev();
|
2019-01-12 13:42:40 +00:00
|
|
|
return ret;
|
2018-11-26 03:14:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 03:22:31 +00:00
|
|
|
/**
|
2019-05-12 18:16:25 +00:00
|
|
|
* do_efi_selftest() - execute EFI selftest
|
2019-04-19 03:22:31 +00:00
|
|
|
*
|
|
|
|
* Return: status code
|
|
|
|
*/
|
2019-05-12 18:16:25 +00:00
|
|
|
static int do_efi_selftest(void)
|
2019-04-19 03:22:31 +00:00
|
|
|
{
|
|
|
|
struct efi_loaded_image_obj *image_obj;
|
|
|
|
struct efi_loaded_image *loaded_image_info;
|
|
|
|
efi_status_t ret;
|
|
|
|
|
|
|
|
ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
|
|
|
|
"\\selftest", "efi_selftest");
|
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
|
|
|
/* Execute the test */
|
|
|
|
ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
|
2023-07-24 10:17:36 +00:00
|
|
|
efi_restore_gd();
|
|
|
|
free(loaded_image_info->load_options);
|
2023-11-21 01:29:43 +00:00
|
|
|
efi_free_pool(test_device_path);
|
|
|
|
efi_free_pool(test_image_path);
|
2023-07-24 10:17:36 +00:00
|
|
|
if (ret != EFI_SUCCESS)
|
|
|
|
efi_delete_handle(&image_obj->header);
|
|
|
|
else
|
|
|
|
ret = efi_delete_handle(&image_obj->header);
|
2019-04-19 03:22:31 +00:00
|
|
|
|
|
|
|
return ret != EFI_SUCCESS;
|
|
|
|
}
|
2018-11-26 03:14:37 +00:00
|
|
|
|
2019-05-12 18:16:25 +00:00
|
|
|
/**
|
|
|
|
* do_bootefi() - execute `bootefi` command
|
|
|
|
*
|
|
|
|
* @cmdtp: table entry describing command
|
|
|
|
* @flag: bitmap indicating how the command was invoked
|
|
|
|
* @argc: number of arguments
|
|
|
|
* @argv: command line arguments
|
|
|
|
* Return: status code
|
|
|
|
*/
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2016-03-09 23:27:20 +00:00
|
|
|
{
|
2019-05-12 18:16:25 +00:00
|
|
|
efi_status_t ret;
|
2023-11-21 01:29:39 +00:00
|
|
|
char *p;
|
|
|
|
void *fdt, *image_buf;
|
|
|
|
unsigned long addr, size;
|
2023-11-21 01:29:44 +00:00
|
|
|
void *image_addr;
|
|
|
|
size_t image_size;
|
2019-05-12 18:16:25 +00:00
|
|
|
|
2019-04-19 03:22:31 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return CMD_RET_USAGE;
|
2019-04-19 03:22:33 +00:00
|
|
|
|
2022-05-19 06:00:56 +00:00
|
|
|
if (argc > 2) {
|
2019-12-08 00:07:01 +00:00
|
|
|
uintptr_t fdt_addr;
|
|
|
|
|
2021-07-24 15:03:29 +00:00
|
|
|
fdt_addr = hextoul(argv[2], NULL);
|
2019-12-08 00:07:01 +00:00
|
|
|
fdt = map_sysmem(fdt_addr, 0);
|
|
|
|
} else {
|
|
|
|
fdt = EFI_FDT_USE_INTERNAL;
|
|
|
|
}
|
2019-05-12 18:16:25 +00:00
|
|
|
|
2023-11-21 01:29:40 +00:00
|
|
|
if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
|
|
|
|
!strcmp(argv[1], "bootmgr")) {
|
2023-11-21 01:29:41 +00:00
|
|
|
ret = efi_bootmgr_run(fdt);
|
2023-11-21 01:29:40 +00:00
|
|
|
|
|
|
|
if (ret == EFI_INVALID_PARAMETER)
|
|
|
|
return CMD_RET_USAGE;
|
2023-11-21 01:29:41 +00:00
|
|
|
else if (ret)
|
2023-11-21 01:29:40 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
2023-11-21 01:29:41 +00:00
|
|
|
return CMD_RET_SUCCESS;
|
2021-01-15 18:02:50 +00:00
|
|
|
}
|
2023-11-21 01:29:40 +00:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_CMD_BOOTEFI_SELFTEST) &&
|
|
|
|
!strcmp(argv[1], "selftest")) {
|
|
|
|
/* Initialize EFI drivers */
|
|
|
|
ret = efi_init_obj_list();
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
|
|
|
|
ret & ~EFI_ERROR_MASK);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = efi_install_fdt(fdt);
|
|
|
|
if (ret == EFI_INVALID_PARAMETER)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
else if (ret != EFI_SUCCESS)
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
2019-05-12 18:16:25 +00:00
|
|
|
return do_efi_selftest();
|
2023-11-21 01:29:40 +00:00
|
|
|
}
|
2023-11-21 01:29:39 +00:00
|
|
|
|
2023-11-21 01:29:40 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY))
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) &&
|
|
|
|
!strcmp(argv[1], "hello")) {
|
2023-11-21 01:29:39 +00:00
|
|
|
image_buf = __efi_helloworld_begin;
|
|
|
|
size = __efi_helloworld_end - __efi_helloworld_begin;
|
2023-11-21 01:29:44 +00:00
|
|
|
/* TODO: not sure calling clear function is necessary */
|
2023-11-21 01:29:39 +00:00
|
|
|
efi_clear_bootdev();
|
2023-11-21 01:29:40 +00:00
|
|
|
} else {
|
2023-11-21 01:29:39 +00:00
|
|
|
addr = strtoul(argv[1], NULL, 16);
|
|
|
|
/* Check that a numeric value was passed */
|
|
|
|
if (!addr)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
image_buf = map_sysmem(addr, 0);
|
|
|
|
|
|
|
|
p = strchr(argv[1], ':');
|
|
|
|
if (p) {
|
|
|
|
size = strtoul(++p, NULL, 16);
|
|
|
|
if (!size)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
efi_clear_bootdev();
|
|
|
|
} else {
|
2023-11-21 01:29:44 +00:00
|
|
|
/* Image should be already loaded */
|
|
|
|
efi_get_image_parameters(&image_addr, &image_size);
|
|
|
|
|
2023-11-21 01:29:39 +00:00
|
|
|
if (image_buf != image_addr) {
|
|
|
|
log_err("No UEFI binary known at %s\n",
|
|
|
|
argv[1]);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
size = image_size;
|
|
|
|
}
|
2022-05-19 06:00:56 +00:00
|
|
|
}
|
2023-11-21 01:29:40 +00:00
|
|
|
|
2023-11-21 01:29:42 +00:00
|
|
|
ret = efi_binary_run(image_buf, size, fdt);
|
2023-11-21 01:29:40 +00:00
|
|
|
|
|
|
|
if (ret == EFI_INVALID_PARAMETER)
|
|
|
|
return CMD_RET_USAGE;
|
2023-11-21 01:29:42 +00:00
|
|
|
else if (ret)
|
2023-11-21 01:29:39 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
2016-03-09 23:27:20 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 19:13:08 +00:00
|
|
|
U_BOOT_LONGHELP(bootefi,
|
2022-05-19 06:00:56 +00:00
|
|
|
"<image address>[:<image size>] [<fdt address>]\n"
|
|
|
|
" - boot EFI payload\n"
|
2016-11-07 15:47:08 +00:00
|
|
|
#ifdef CONFIG_CMD_BOOTEFI_HELLO
|
2017-09-15 08:06:11 +00:00
|
|
|
"bootefi hello\n"
|
|
|
|
" - boot a sample Hello World application stored within U-Boot\n"
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
|
2018-03-03 14:29:03 +00:00
|
|
|
"bootefi selftest [fdt address]\n"
|
2017-09-15 08:06:11 +00:00
|
|
|
" - boot an EFI selftest application stored within U-Boot\n"
|
2017-10-18 16:13:13 +00:00
|
|
|
" Use environment variable efi_selftest to select a single test.\n"
|
|
|
|
" Use 'setenv efi_selftest list' to enumerate all tests.\n"
|
2016-11-07 15:47:08 +00:00
|
|
|
#endif
|
2021-01-15 18:02:50 +00:00
|
|
|
#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
|
2019-04-19 03:22:35 +00:00
|
|
|
"bootefi bootmgr [fdt address]\n"
|
2017-09-13 22:05:38 +00:00
|
|
|
" - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
|
|
|
|
"\n"
|
|
|
|
" If specified, the device tree located at <fdt address> gets\n"
|
2021-01-15 18:02:50 +00:00
|
|
|
" exposed as EFI configuration table.\n"
|
|
|
|
#endif
|
2023-10-07 19:13:08 +00:00
|
|
|
);
|
2016-03-09 23:27:20 +00:00
|
|
|
|
|
|
|
U_BOOT_CMD(
|
2022-04-10 21:05:55 +00:00
|
|
|
bootefi, 4, 0, do_bootefi,
|
2016-06-07 18:14:31 +00:00
|
|
|
"Boots an EFI payload from memory",
|
2016-03-09 23:27:20 +00:00
|
|
|
bootefi_help_text
|
|
|
|
);
|