1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
8 #include <efi_selftest.h>
11 /* Constants for test step bitmap */
12 #define EFI_ST_SETUP 1
13 #define EFI_ST_EXECUTE 2
14 #define EFI_ST_TEARDOWN 4
16 static const struct efi_system_table *systable;
17 static const struct efi_boot_services *boottime;
18 static const struct efi_runtime_services *runtime;
19 static efi_handle_t handle;
20 static u16 reset_message[] = L"Selftest completed";
23 * Exit the boot services.
25 * The size of the memory map is determined.
26 * Pool memory is allocated to copy the memory map.
27 * The memory map is copied and the map key is obtained.
28 * The map key is used to exit the boot services.
30 void efi_st_exit_boot_services(void)
32 efi_uintn_t map_size = 0;
34 efi_uintn_t desc_size;
37 struct efi_mem_desc *memory_map;
39 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
41 if (ret != EFI_BUFFER_TOO_SMALL) {
43 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
46 /* Allocate extra space for newly allocated memory */
47 map_size += sizeof(struct efi_mem_desc);
48 ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
49 (void **)&memory_map);
50 if (ret != EFI_SUCCESS) {
51 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
54 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
55 &desc_size, &desc_version);
56 if (ret != EFI_SUCCESS) {
57 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
60 ret = boottime->exit_boot_services(handle, map_key);
61 if (ret != EFI_SUCCESS) {
62 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
65 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
71 * @test the test to be executed
72 * @failures counter that will be incremented if a failure occurs
73 * @return EFI_ST_SUCCESS for success
75 static int setup(struct efi_unit_test *test, unsigned int *failures)
78 test->setup_ok = EFI_ST_SUCCESS;
79 return EFI_ST_SUCCESS;
81 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
82 test->setup_ok = test->setup(handle, systable);
83 if (test->setup_ok != EFI_ST_SUCCESS) {
84 efi_st_error("Setting up '%s' failed\n", test->name);
87 efi_st_printc(EFI_LIGHTGREEN,
88 "Setting up '%s' succeeded\n", test->name);
90 return test->setup_ok;
96 * @test the test to be executed
97 * @failures counter that will be incremented if a failure occurs
98 * @return EFI_ST_SUCCESS for success
100 static int execute(struct efi_unit_test *test, unsigned int *failures)
105 return EFI_ST_SUCCESS;
106 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
107 ret = test->execute();
108 if (ret != EFI_ST_SUCCESS) {
109 efi_st_error("Executing '%s' failed\n", test->name);
112 efi_st_printc(EFI_LIGHTGREEN,
113 "Executing '%s' succeeded\n", test->name);
121 * @test the test to be torn down
122 * @failures counter that will be incremented if a failure occurs
123 * @return EFI_ST_SUCCESS for success
125 static int teardown(struct efi_unit_test *test, unsigned int *failures)
130 return EFI_ST_SUCCESS;
131 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
132 ret = test->teardown();
133 if (ret != EFI_ST_SUCCESS) {
134 efi_st_error("Tearing down '%s' failed\n", test->name);
137 efi_st_printc(EFI_LIGHTGREEN,
138 "Tearing down '%s' succeeded\n", test->name);
144 * Check that a test exists.
146 * @testname: name of the test
147 * @return: test, or NULL if not found
149 static struct efi_unit_test *find_test(const u16 *testname)
151 struct efi_unit_test *test;
153 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
154 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
155 if (!efi_st_strcmp_16_8(testname, test->name))
158 efi_st_printf("\nTest '%ps' not found\n", testname);
163 * List all available tests.
165 static void list_all_tests(void)
167 struct efi_unit_test *test;
170 efi_st_printf("\nAvailable tests:\n");
171 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
172 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
173 efi_st_printf("'%s'%s\n", test->name,
174 test->on_request ? " - on request" : "");
179 * Execute test steps of one phase.
181 * @testname name of a single selected test or NULL
183 * @steps steps to execute (mask with bits from EFI_ST_...)
184 * failures returns EFI_ST_SUCCESS if all test steps succeeded
186 void efi_st_do_tests(const u16 *testname, unsigned int phase,
187 unsigned int steps, unsigned int *failures)
189 struct efi_unit_test *test;
191 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
192 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
194 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
196 if (test->phase != phase)
198 if (steps & EFI_ST_SETUP)
199 setup(test, failures);
200 if (steps & EFI_ST_EXECUTE && test->setup_ok == EFI_ST_SUCCESS)
201 execute(test, failures);
202 if (steps & EFI_ST_TEARDOWN)
203 teardown(test, failures);
208 * Execute selftest of the EFI API
210 * This is the main entry point of the EFI selftest application.
212 * All tests use a driver model and are run in three phases:
213 * setup, execute, teardown.
215 * A test may be setup and executed at boottime,
216 * it may be setup at boottime and executed at runtime,
217 * or it may be setup and executed at runtime.
219 * After executing all tests the system is reset.
221 * @image_handle: handle of the loaded EFI image
222 * @systab: EFI system table
224 efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
225 struct efi_system_table *systab)
227 unsigned int failures = 0;
228 const u16 *testname = NULL;
229 struct efi_loaded_image *loaded_image;
233 boottime = systable->boottime;
234 runtime = systable->runtime;
235 handle = image_handle;
236 con_out = systable->con_out;
237 con_in = systable->con_in;
239 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
240 (void **)&loaded_image);
241 if (ret != EFI_SUCCESS) {
242 efi_st_error("Cannot open loaded image protocol\n");
246 if (loaded_image->load_options)
247 testname = (u16 *)loaded_image->load_options;
250 if (!efi_st_strcmp_16_8(testname, "list") ||
251 !find_test(testname)) {
255 * Once the Exit boottime service is correctly
256 * implemented we should call
257 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
259 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
265 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
268 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
270 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
271 ll_entry_count(struct efi_unit_test,
274 /* Execute boottime tests */
275 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
276 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
279 /* Execute mixed tests */
280 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
281 EFI_ST_SETUP, &failures);
283 efi_st_exit_boot_services();
285 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
286 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
288 /* Execute runtime tests */
289 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
290 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
294 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
297 efi_st_printf("Preparing for reset. Press any key...\n");
299 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
300 sizeof(reset_message), reset_message);
302 efi_st_error("Reset failed\n");
304 return EFI_UNSUPPORTED;