2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-09-15 08:06:11 +00:00
|
|
|
/*
|
|
|
|
* EFI efi_selftest
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
|
|
|
|
*/
|
|
|
|
|
2020-08-22 07:14:56 +00:00
|
|
|
#include <command.h>
|
2017-09-15 08:06:11 +00:00
|
|
|
#include <efi_selftest.h>
|
|
|
|
#include <vsprintf.h>
|
|
|
|
|
2018-06-18 14:08:21 +00:00
|
|
|
/* Constants for test step bitmap */
|
2017-10-18 16:13:12 +00:00
|
|
|
#define EFI_ST_SETUP 1
|
|
|
|
#define EFI_ST_EXECUTE 2
|
|
|
|
#define EFI_ST_TEARDOWN 4
|
|
|
|
|
2022-09-03 13:56:51 +00:00
|
|
|
const struct efi_system_table *st_systable;
|
|
|
|
const struct efi_boot_services *st_boottime;
|
2017-09-15 08:06:11 +00:00
|
|
|
static const struct efi_runtime_services *runtime;
|
|
|
|
static efi_handle_t handle;
|
2022-01-23 19:55:12 +00:00
|
|
|
static u16 reset_message[] = u"Selftest completed";
|
2018-10-22 21:15:10 +00:00
|
|
|
static int *setup_status;
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit the boot services.
|
|
|
|
*
|
|
|
|
* The size of the memory map is determined.
|
|
|
|
* Pool memory is allocated to copy the memory map.
|
2018-06-18 14:08:21 +00:00
|
|
|
* The memory map is copied and the map key is obtained.
|
2017-09-15 08:06:11 +00:00
|
|
|
* The map key is used to exit the boot services.
|
|
|
|
*/
|
|
|
|
void efi_st_exit_boot_services(void)
|
|
|
|
{
|
2017-11-06 20:17:48 +00:00
|
|
|
efi_uintn_t map_size = 0;
|
|
|
|
efi_uintn_t map_key;
|
|
|
|
efi_uintn_t desc_size;
|
2017-09-15 08:06:11 +00:00
|
|
|
u32 desc_version;
|
|
|
|
efi_status_t ret;
|
|
|
|
struct efi_mem_desc *memory_map;
|
|
|
|
|
2020-11-12 20:26:28 +00:00
|
|
|
/* Do not detach devices in ExitBootServices. We need the console. */
|
|
|
|
efi_st_keep_devices = true;
|
|
|
|
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
|
2017-09-15 08:06:11 +00:00
|
|
|
&desc_version);
|
|
|
|
if (ret != EFI_BUFFER_TOO_SMALL) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error(
|
|
|
|
"GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Allocate extra space for newly allocated memory */
|
|
|
|
map_size += sizeof(struct efi_mem_desc);
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
|
2017-09-15 08:06:11 +00:00
|
|
|
(void **)&memory_map);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->get_memory_map(&map_size, memory_map, &map_key,
|
2017-09-15 08:06:11 +00:00
|
|
|
&desc_size, &desc_version);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->exit_boot_services(handle, map_key);
|
2017-09-15 08:06:11 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a test.
|
|
|
|
*
|
|
|
|
* @test the test to be executed
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
|
|
|
static int setup(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
2018-10-19 05:51:26 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->setup)
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = test->setup(handle, st_systable);
|
2018-10-19 05:51:26 +00:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("Setting up '%s' failed\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
++*failures;
|
|
|
|
} else {
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTGREEN,
|
|
|
|
"Setting up '%s' succeeded\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
2018-10-19 05:51:26 +00:00
|
|
|
return ret;
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a test.
|
|
|
|
*
|
|
|
|
* @test the test to be executed
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
|
|
|
static int execute(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->execute)
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
ret = test->execute();
|
2017-10-04 13:31:26 +00:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("Executing '%s' failed\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
++*failures;
|
|
|
|
} else {
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTGREEN,
|
|
|
|
"Executing '%s' succeeded\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down a test.
|
|
|
|
*
|
|
|
|
* @test the test to be torn down
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: EFI_ST_SUCCESS for success
|
2017-09-15 08:06:11 +00:00
|
|
|
*/
|
|
|
|
static int teardown(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->teardown)
|
2017-10-04 13:31:26 +00:00
|
|
|
return EFI_ST_SUCCESS;
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
ret = test->teardown();
|
2017-10-04 13:31:26 +00:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_error("Tearing down '%s' failed\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
++*failures;
|
|
|
|
} else {
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_LIGHTGREEN,
|
|
|
|
"Tearing down '%s' succeeded\n", test->name);
|
2017-09-15 08:06:11 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:52:09 +00:00
|
|
|
/*
|
|
|
|
* Check that a test requiring reset exists.
|
|
|
|
*
|
|
|
|
* @testname: name of the test
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: test, or NULL if not found
|
2020-09-30 19:52:09 +00:00
|
|
|
*/
|
|
|
|
static bool need_reset(const u16 *testname)
|
|
|
|
{
|
|
|
|
struct efi_unit_test *test;
|
|
|
|
|
|
|
|
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
|
|
|
|
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
|
|
|
|
if (testname && efi_st_strcmp_16_8(testname, test->name))
|
|
|
|
continue;
|
|
|
|
if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT ||
|
2021-03-24 16:48:01 +00:00
|
|
|
test->phase == EFI_SETTING_VIRTUAL_ADDRESS_MAP)
|
2020-09-30 19:52:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:13:13 +00:00
|
|
|
/*
|
|
|
|
* Check that a test exists.
|
|
|
|
*
|
|
|
|
* @testname: name of the test
|
2022-01-20 18:48:20 +00:00
|
|
|
* Return: test, or NULL if not found
|
2017-10-18 16:13:13 +00:00
|
|
|
*/
|
|
|
|
static struct efi_unit_test *find_test(const u16 *testname)
|
|
|
|
{
|
|
|
|
struct efi_unit_test *test;
|
|
|
|
|
|
|
|
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
|
|
|
|
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
|
|
|
|
if (!efi_st_strcmp_16_8(testname, test->name))
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
efi_st_printf("\nTest '%ps' not found\n", testname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List all available tests.
|
|
|
|
*/
|
|
|
|
static void list_all_tests(void)
|
|
|
|
{
|
|
|
|
struct efi_unit_test *test;
|
|
|
|
|
|
|
|
/* List all tests */
|
|
|
|
efi_st_printf("\nAvailable tests:\n");
|
|
|
|
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
|
|
|
|
test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
|
|
|
|
efi_st_printf("'%s'%s\n", test->name,
|
|
|
|
test->on_request ? " - on request" : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:13:12 +00:00
|
|
|
/*
|
|
|
|
* Execute test steps of one phase.
|
|
|
|
*
|
2017-10-18 16:13:13 +00:00
|
|
|
* @testname name of a single selected test or NULL
|
2017-10-18 16:13:12 +00:00
|
|
|
* @phase test phase
|
2018-06-18 14:08:21 +00:00
|
|
|
* @steps steps to execute (mask with bits from EFI_ST_...)
|
2017-10-18 16:13:12 +00:00
|
|
|
* failures returns EFI_ST_SUCCESS if all test steps succeeded
|
|
|
|
*/
|
2017-10-18 16:13:13 +00:00
|
|
|
void efi_st_do_tests(const u16 *testname, unsigned int phase,
|
|
|
|
unsigned int steps, unsigned int *failures)
|
2017-10-18 16:13:12 +00:00
|
|
|
{
|
2018-10-19 05:51:26 +00:00
|
|
|
int i = 0;
|
2017-10-18 16:13:12 +00:00
|
|
|
struct efi_unit_test *test;
|
|
|
|
|
|
|
|
for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
|
2018-10-19 05:51:26 +00:00
|
|
|
test < ll_entry_end(struct efi_unit_test, efi_unit_test);
|
|
|
|
++test, ++i) {
|
2017-10-18 16:13:13 +00:00
|
|
|
if (testname ?
|
|
|
|
efi_st_strcmp_16_8(testname, test->name) : test->on_request)
|
|
|
|
continue;
|
2017-10-18 16:13:12 +00:00
|
|
|
if (test->phase != phase)
|
|
|
|
continue;
|
|
|
|
if (steps & EFI_ST_SETUP)
|
2018-10-22 21:15:10 +00:00
|
|
|
setup_status[i] = setup(test, failures);
|
|
|
|
if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS)
|
2017-10-18 16:13:12 +00:00
|
|
|
execute(test, failures);
|
|
|
|
if (steps & EFI_ST_TEARDOWN)
|
|
|
|
teardown(test, failures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 08:06:11 +00:00
|
|
|
/*
|
|
|
|
* Execute selftest of the EFI API
|
|
|
|
*
|
|
|
|
* This is the main entry point of the EFI selftest application.
|
|
|
|
*
|
|
|
|
* All tests use a driver model and are run in three phases:
|
|
|
|
* setup, execute, teardown.
|
|
|
|
*
|
2022-09-03 13:56:51 +00:00
|
|
|
* A test may be setup and executed at st_boottime,
|
|
|
|
* it may be setup at st_boottime and executed at runtime,
|
2017-09-15 08:06:11 +00:00
|
|
|
* or it may be setup and executed at runtime.
|
|
|
|
*
|
|
|
|
* After executing all tests the system is reset.
|
|
|
|
*
|
|
|
|
* @image_handle: handle of the loaded EFI image
|
|
|
|
* @systab: EFI system table
|
|
|
|
*/
|
|
|
|
efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
|
|
|
|
struct efi_system_table *systab)
|
|
|
|
{
|
|
|
|
unsigned int failures = 0;
|
2017-10-18 16:13:13 +00:00
|
|
|
const u16 *testname = NULL;
|
|
|
|
struct efi_loaded_image *loaded_image;
|
|
|
|
efi_status_t ret;
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2022-09-03 13:56:51 +00:00
|
|
|
st_systable = systab;
|
|
|
|
st_boottime = st_systable->boottime;
|
|
|
|
runtime = st_systable->runtime;
|
2017-09-15 08:06:11 +00:00
|
|
|
handle = image_handle;
|
2022-09-03 13:56:51 +00:00
|
|
|
con_out = st_systable->con_out;
|
|
|
|
con_in = st_systable->con_in;
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
|
2017-10-18 16:13:13 +00:00
|
|
|
(void **)&loaded_image);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-11-26 13:05:19 +00:00
|
|
|
efi_st_error("Cannot open loaded image protocol\n");
|
2017-10-18 16:13:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loaded_image->load_options)
|
|
|
|
testname = (u16 *)loaded_image->load_options;
|
|
|
|
|
|
|
|
if (testname) {
|
|
|
|
if (!efi_st_strcmp_16_8(testname, "list") ||
|
|
|
|
!find_test(testname)) {
|
|
|
|
list_all_tests();
|
|
|
|
/*
|
|
|
|
* TODO:
|
2022-09-03 13:56:51 +00:00
|
|
|
* Once the Exit st_boottime service is correctly
|
2017-10-18 16:13:13 +00:00
|
|
|
* implemented we should call
|
2022-09-03 13:56:51 +00:00
|
|
|
* st_boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
|
2017-10-18 16:13:13 +00:00
|
|
|
* here, cf.
|
|
|
|
* https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
|
|
|
|
*/
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2017-10-18 16:13:13 +00:00
|
|
|
if (testname)
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
|
2017-10-18 16:13:13 +00:00
|
|
|
else
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
|
2017-10-18 16:13:13 +00:00
|
|
|
ll_entry_count(struct efi_unit_test,
|
|
|
|
efi_unit_test));
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2018-10-19 05:51:26 +00:00
|
|
|
/* Allocate buffer for setup results */
|
2022-09-03 13:56:51 +00:00
|
|
|
ret = st_boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
|
2018-10-19 05:51:26 +00:00
|
|
|
ll_entry_count(struct efi_unit_test,
|
|
|
|
efi_unit_test),
|
2018-10-22 21:15:10 +00:00
|
|
|
(void **)&setup_status);
|
2018-10-19 05:51:26 +00:00
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Allocate pool failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:56:51 +00:00
|
|
|
/* Execute st_boottime tests */
|
2017-10-18 16:13:13 +00:00
|
|
|
efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
|
2017-10-18 16:13:12 +00:00
|
|
|
EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
|
|
|
|
&failures);
|
2017-09-15 08:06:11 +00:00
|
|
|
|
2020-09-30 19:52:09 +00:00
|
|
|
if (!need_reset(testname)) {
|
|
|
|
if (failures)
|
|
|
|
ret = EFI_PROTOCOL_ERROR;
|
|
|
|
|
|
|
|
/* Give feedback */
|
|
|
|
efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n",
|
|
|
|
failures);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-15 08:06:11 +00:00
|
|
|
/* Execute mixed tests */
|
2017-10-18 16:13:13 +00:00
|
|
|
efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
|
2017-10-18 16:13:12 +00:00
|
|
|
EFI_ST_SETUP, &failures);
|
2021-03-24 16:48:01 +00:00
|
|
|
efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP,
|
|
|
|
EFI_ST_SETUP, &failures);
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
efi_st_exit_boot_services();
|
|
|
|
|
2017-10-18 16:13:13 +00:00
|
|
|
efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
|
2017-10-18 16:13:12 +00:00
|
|
|
EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
|
2021-03-24 16:48:01 +00:00
|
|
|
/* Execute test setting the virtual address map */
|
|
|
|
efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP,
|
|
|
|
EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
|
2017-10-18 16:13:12 +00:00
|
|
|
&failures);
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
/* Give feedback */
|
2018-01-11 07:15:54 +00:00
|
|
|
efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
/* Reset system */
|
2018-06-18 14:08:21 +00:00
|
|
|
efi_st_printf("Preparing for reset. Press any key...\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
efi_st_get_key();
|
2020-08-22 07:14:56 +00:00
|
|
|
|
2020-09-10 05:47:58 +00:00
|
|
|
if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
|
2020-08-22 07:14:56 +00:00
|
|
|
runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
|
|
|
|
sizeof(reset_message), reset_message);
|
2020-09-10 05:47:58 +00:00
|
|
|
} else {
|
|
|
|
efi_restore_gd();
|
2020-08-22 07:14:56 +00:00
|
|
|
do_reset(NULL, 0, 0, NULL);
|
2020-09-10 05:47:58 +00:00
|
|
|
}
|
2020-08-22 07:14:56 +00:00
|
|
|
|
2017-10-04 10:37:02 +00:00
|
|
|
efi_st_printf("\n");
|
2018-06-18 14:08:21 +00:00
|
|
|
efi_st_error("Reset failed\n");
|
2017-09-15 08:06:11 +00:00
|
|
|
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|