4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
9 #include <efi_selftest.h>
13 * Constants for test step bitmap
15 #define EFI_ST_SETUP 1
16 #define EFI_ST_EXECUTE 2
17 #define EFI_ST_TEARDOWN 4
19 static const struct efi_system_table *systable;
20 static const struct efi_boot_services *boottime;
21 static const struct efi_runtime_services *runtime;
22 static efi_handle_t handle;
23 static u16 reset_message[] = L"Selftest completed";
26 * Exit the boot services.
28 * The size of the memory map is determined.
29 * Pool memory is allocated to copy the memory map.
30 * The memory amp is copied and the map key is obtained.
31 * The map key is used to exit the boot services.
33 void efi_st_exit_boot_services(void)
35 efi_uintn_t map_size = 0;
37 efi_uintn_t desc_size;
40 struct efi_mem_desc *memory_map;
42 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
44 if (ret != EFI_BUFFER_TOO_SMALL) {
46 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
49 /* Allocate extra space for newly allocated memory */
50 map_size += sizeof(struct efi_mem_desc);
51 ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
52 (void **)&memory_map);
53 if (ret != EFI_SUCCESS) {
54 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
57 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
58 &desc_size, &desc_version);
59 if (ret != EFI_SUCCESS) {
60 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
63 ret = boottime->exit_boot_services(handle, map_key);
64 if (ret != EFI_SUCCESS) {
65 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
68 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
74 * @test the test to be executed
75 * @failures counter that will be incremented if a failure occurs
76 * @return EFI_ST_SUCCESS for success
78 static int setup(struct efi_unit_test *test, unsigned int *failures)
83 return EFI_ST_SUCCESS;
84 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
85 ret = test->setup(handle, systable);
86 if (ret != EFI_ST_SUCCESS) {
87 efi_st_error("Setting up '%s' failed\n", test->name);
90 efi_st_printc(EFI_LIGHTGREEN,
91 "Setting up '%s' succeeded\n", test->name);
99 * @test the test to be executed
100 * @failures counter that will be incremented if a failure occurs
101 * @return EFI_ST_SUCCESS for success
103 static int execute(struct efi_unit_test *test, unsigned int *failures)
108 return EFI_ST_SUCCESS;
109 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
110 ret = test->execute();
111 if (ret != EFI_ST_SUCCESS) {
112 efi_st_error("Executing '%s' failed\n", test->name);
115 efi_st_printc(EFI_LIGHTGREEN,
116 "Executing '%s' succeeded\n", test->name);
124 * @test the test to be torn down
125 * @failures counter that will be incremented if a failure occurs
126 * @return EFI_ST_SUCCESS for success
128 static int teardown(struct efi_unit_test *test, unsigned int *failures)
133 return EFI_ST_SUCCESS;
134 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
135 ret = test->teardown();
136 if (ret != EFI_ST_SUCCESS) {
137 efi_st_error("Tearing down '%s' failed\n", test->name);
140 efi_st_printc(EFI_LIGHTGREEN,
141 "Tearing down '%s' succeeded\n", test->name);
147 * Check that a test exists.
149 * @testname: name of the test
152 static struct efi_unit_test *find_test(const u16 *testname)
154 struct efi_unit_test *test;
156 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
157 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
158 if (!efi_st_strcmp_16_8(testname, test->name))
161 efi_st_printf("\nTest '%ps' not found\n", testname);
166 * List all available tests.
168 static void list_all_tests(void)
170 struct efi_unit_test *test;
173 efi_st_printf("\nAvailable tests:\n");
174 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
175 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
176 efi_st_printf("'%s'%s\n", test->name,
177 test->on_request ? " - on request" : "");
182 * Execute test steps of one phase.
184 * @testname name of a single selected test or NULL
186 * @steps steps to execute
187 * failures returns EFI_ST_SUCCESS if all test steps succeeded
189 void efi_st_do_tests(const u16 *testname, unsigned int phase,
190 unsigned int steps, unsigned int *failures)
192 struct efi_unit_test *test;
194 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
195 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
197 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
199 if (test->phase != phase)
201 if (steps & EFI_ST_SETUP)
202 setup(test, failures);
203 if (steps & EFI_ST_EXECUTE)
204 execute(test, failures);
205 if (steps & EFI_ST_TEARDOWN)
206 teardown(test, failures);
211 * Execute selftest of the EFI API
213 * This is the main entry point of the EFI selftest application.
215 * All tests use a driver model and are run in three phases:
216 * setup, execute, teardown.
218 * A test may be setup and executed at boottime,
219 * it may be setup at boottime and executed at runtime,
220 * or it may be setup and executed at runtime.
222 * After executing all tests the system is reset.
224 * @image_handle: handle of the loaded EFI image
225 * @systab: EFI system table
227 efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
228 struct efi_system_table *systab)
230 unsigned int failures = 0;
231 const u16 *testname = NULL;
232 struct efi_loaded_image *loaded_image;
236 boottime = systable->boottime;
237 runtime = systable->runtime;
238 handle = image_handle;
239 con_out = systable->con_out;
240 con_in = systable->con_in;
242 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
243 (void **)&loaded_image);
244 if (ret != EFI_SUCCESS) {
245 efi_st_error("Cannot open loaded image protocol\n");
249 if (loaded_image->load_options)
250 testname = (u16 *)loaded_image->load_options;
253 if (!efi_st_strcmp_16_8(testname, "list") ||
254 !find_test(testname)) {
258 * Once the Exit boottime service is correctly
259 * implemented we should call
260 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
262 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
268 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
271 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
273 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
274 ll_entry_count(struct efi_unit_test,
277 /* Execute boottime tests */
278 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
279 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
282 /* Execute mixed tests */
283 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
284 EFI_ST_SETUP, &failures);
286 efi_st_exit_boot_services();
288 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
289 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
291 /* Execute runtime tests */
292 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
293 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
297 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
300 efi_st_printf("Preparing for reset. Press any key.\n");
302 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
303 sizeof(reset_message), reset_message);
305 efi_st_error("Reset failed.\n");
307 return EFI_UNSUPPORTED;