2017-09-15 16:06:11 +08:00
|
|
|
/*
|
|
|
|
* EFI efi_selftest
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <efi_selftest.h>
|
|
|
|
#include <vsprintf.h>
|
|
|
|
|
2017-10-19 00:13:12 +08:00
|
|
|
/*
|
|
|
|
* Constants for test step bitmap
|
|
|
|
*/
|
|
|
|
#define EFI_ST_SETUP 1
|
|
|
|
#define EFI_ST_EXECUTE 2
|
|
|
|
#define EFI_ST_TEARDOWN 4
|
|
|
|
|
2017-09-15 16:06:11 +08:00
|
|
|
static const struct efi_system_table *systable;
|
|
|
|
static const struct efi_boot_services *boottime;
|
|
|
|
static const struct efi_runtime_services *runtime;
|
|
|
|
static efi_handle_t handle;
|
|
|
|
static u16 reset_message[] = L"Selftest completed";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit the boot services.
|
|
|
|
*
|
|
|
|
* The size of the memory map is determined.
|
|
|
|
* Pool memory is allocated to copy the memory map.
|
|
|
|
* The memory amp is copied and the map key is obtained.
|
|
|
|
* The map key is used to exit the boot services.
|
|
|
|
*/
|
|
|
|
void efi_st_exit_boot_services(void)
|
|
|
|
{
|
2017-10-19 00:13:10 +08:00
|
|
|
unsigned long map_size = 0;
|
|
|
|
unsigned long map_key;
|
2017-09-15 16:06:11 +08:00
|
|
|
unsigned long desc_size;
|
|
|
|
u32 desc_version;
|
|
|
|
efi_status_t ret;
|
|
|
|
struct efi_mem_desc *memory_map;
|
|
|
|
|
|
|
|
ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
|
|
|
|
&desc_version);
|
|
|
|
if (ret != EFI_BUFFER_TOO_SMALL) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error(
|
|
|
|
"GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
|
2017-09-15 16:06:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Allocate extra space for newly allocated memory */
|
|
|
|
map_size += sizeof(struct efi_mem_desc);
|
|
|
|
ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
|
|
|
|
(void **)&memory_map);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
|
2017-09-15 16:06:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
|
|
|
|
&desc_size, &desc_version);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
|
2017-09-15 16:06:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = boottime->exit_boot_services(handle, map_key);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
|
2017-09-15 16:06:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
efi_st_printf("\nBoot services terminated\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a test.
|
|
|
|
*
|
|
|
|
* @test the test to be executed
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2017-10-04 21:31:26 +08:00
|
|
|
* @return EFI_ST_SUCCESS for success
|
2017-09-15 16:06:11 +08:00
|
|
|
*/
|
|
|
|
static int setup(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->setup)
|
2017-10-04 21:31:26 +08:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 16:06:11 +08:00
|
|
|
efi_st_printf("\nSetting up '%s'\n", test->name);
|
|
|
|
ret = test->setup(handle, systable);
|
2017-10-04 21:31:26 +08:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("Setting up '%s' failed\n", test->name);
|
2017-09-15 16:06:11 +08:00
|
|
|
++*failures;
|
|
|
|
} else {
|
|
|
|
efi_st_printf("Setting up '%s' succeeded\n", test->name);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a test.
|
|
|
|
*
|
|
|
|
* @test the test to be executed
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2017-10-04 21:31:26 +08:00
|
|
|
* @return EFI_ST_SUCCESS for success
|
2017-09-15 16:06:11 +08:00
|
|
|
*/
|
|
|
|
static int execute(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->execute)
|
2017-10-04 21:31:26 +08:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 16:06:11 +08:00
|
|
|
efi_st_printf("\nExecuting '%s'\n", test->name);
|
|
|
|
ret = test->execute();
|
2017-10-04 21:31:26 +08:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("Executing '%s' failed\n", test->name);
|
2017-09-15 16:06:11 +08:00
|
|
|
++*failures;
|
|
|
|
} else {
|
|
|
|
efi_st_printf("Executing '%s' succeeded\n", test->name);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down a test.
|
|
|
|
*
|
|
|
|
* @test the test to be torn down
|
|
|
|
* @failures counter that will be incremented if a failure occurs
|
2017-10-04 21:31:26 +08:00
|
|
|
* @return EFI_ST_SUCCESS for success
|
2017-09-15 16:06:11 +08:00
|
|
|
*/
|
|
|
|
static int teardown(struct efi_unit_test *test, unsigned int *failures)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!test->teardown)
|
2017-10-04 21:31:26 +08:00
|
|
|
return EFI_ST_SUCCESS;
|
2017-09-15 16:06:11 +08:00
|
|
|
efi_st_printf("\nTearing down '%s'\n", test->name);
|
|
|
|
ret = test->teardown();
|
2017-10-04 21:31:26 +08:00
|
|
|
if (ret != EFI_ST_SUCCESS) {
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_error("Tearing down '%s' failed\n", test->name);
|
2017-09-15 16:06:11 +08:00
|
|
|
++*failures;
|
|
|
|
} else {
|
|
|
|
efi_st_printf("Tearing down '%s' succeeded\n", test->name);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:13:13 +08:00
|
|
|
/*
|
|
|
|
* Check that a test exists.
|
|
|
|
*
|
|
|
|
* @testname: name of the test
|
|
|
|
* @return: test
|
|
|
|
*/
|
|
|
|
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-19 00:13:12 +08:00
|
|
|
/*
|
|
|
|
* Execute test steps of one phase.
|
|
|
|
*
|
2017-10-19 00:13:13 +08:00
|
|
|
* @testname name of a single selected test or NULL
|
2017-10-19 00:13:12 +08:00
|
|
|
* @phase test phase
|
|
|
|
* @steps steps to execute
|
|
|
|
* failures returns EFI_ST_SUCCESS if all test steps succeeded
|
|
|
|
*/
|
2017-10-19 00:13:13 +08:00
|
|
|
void efi_st_do_tests(const u16 *testname, unsigned int phase,
|
|
|
|
unsigned int steps, unsigned int *failures)
|
2017-10-19 00:13:12 +08:00
|
|
|
{
|
|
|
|
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) {
|
2017-10-19 00:13:13 +08:00
|
|
|
if (testname ?
|
|
|
|
efi_st_strcmp_16_8(testname, test->name) : test->on_request)
|
|
|
|
continue;
|
2017-10-19 00:13:12 +08:00
|
|
|
if (test->phase != phase)
|
|
|
|
continue;
|
|
|
|
if (steps & EFI_ST_SETUP)
|
|
|
|
setup(test, failures);
|
|
|
|
if (steps & EFI_ST_EXECUTE)
|
|
|
|
execute(test, failures);
|
|
|
|
if (steps & EFI_ST_TEARDOWN)
|
|
|
|
teardown(test, failures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 16:06:11 +08: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.
|
|
|
|
*
|
|
|
|
* A test may be setup and executed at boottime,
|
|
|
|
* it may be setup at boottime and executed at runtime,
|
|
|
|
* 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-19 00:13:13 +08:00
|
|
|
const u16 *testname = NULL;
|
|
|
|
struct efi_loaded_image *loaded_image;
|
|
|
|
efi_status_t ret;
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
systable = systab;
|
|
|
|
boottime = systable->boottime;
|
|
|
|
runtime = systable->runtime;
|
|
|
|
handle = image_handle;
|
|
|
|
con_out = systable->con_out;
|
|
|
|
con_in = systable->con_in;
|
|
|
|
|
2017-10-19 00:13:13 +08:00
|
|
|
ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
|
|
|
|
(void **)&loaded_image);
|
|
|
|
if (ret != EFI_SUCCESS) {
|
|
|
|
efi_st_error("Cannot open loaded image protocol");
|
|
|
|
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:
|
|
|
|
* Once the Exit boottime service is correctly
|
|
|
|
* implemented we should call
|
|
|
|
* boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
|
|
|
|
* here, cf.
|
|
|
|
* https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
|
|
|
|
*/
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 16:06:11 +08:00
|
|
|
efi_st_printf("\nTesting EFI API implementation\n");
|
|
|
|
|
2017-10-19 00:13:13 +08:00
|
|
|
if (testname)
|
|
|
|
efi_st_printf("\nSelected test: '%ps'\n", testname);
|
|
|
|
else
|
|
|
|
efi_st_printf("\nNumber of tests to execute: %u\n",
|
|
|
|
ll_entry_count(struct efi_unit_test,
|
|
|
|
efi_unit_test));
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
/* Execute boottime tests */
|
2017-10-19 00:13:13 +08:00
|
|
|
efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
|
2017-10-19 00:13:12 +08:00
|
|
|
EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
|
|
|
|
&failures);
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
/* Execute mixed tests */
|
2017-10-19 00:13:13 +08:00
|
|
|
efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
|
2017-10-19 00:13:12 +08:00
|
|
|
EFI_ST_SETUP, &failures);
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
efi_st_exit_boot_services();
|
|
|
|
|
2017-10-19 00:13:13 +08:00
|
|
|
efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
|
2017-10-19 00:13:12 +08:00
|
|
|
EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
/* Execute runtime tests */
|
2017-10-19 00:13:13 +08:00
|
|
|
efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
|
2017-10-19 00:13:12 +08:00
|
|
|
EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
|
|
|
|
&failures);
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
/* Give feedback */
|
|
|
|
efi_st_printf("\nSummary: %u failures\n\n", failures);
|
|
|
|
|
|
|
|
/* Reset system */
|
|
|
|
efi_st_printf("Preparing for reset. Press any key.\n");
|
|
|
|
efi_st_get_key();
|
|
|
|
runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
|
|
|
|
sizeof(reset_message), reset_message);
|
2017-10-04 18:37:02 +08:00
|
|
|
efi_st_printf("\n");
|
|
|
|
efi_st_error("Reset failed.\n");
|
2017-09-15 16:06:11 +08:00
|
|
|
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|