1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
9 #include <efi_selftest.h>
12 /* Constants for test step bitmap */
13 #define EFI_ST_SETUP 1
14 #define EFI_ST_EXECUTE 2
15 #define EFI_ST_TEARDOWN 4
17 static const struct efi_system_table *systable;
18 static const struct efi_boot_services *boottime;
19 static const struct efi_runtime_services *runtime;
20 static efi_handle_t handle;
21 static u16 reset_message[] = L"Selftest completed";
22 static int *setup_status;
25 * Exit the boot services.
27 * The size of the memory map is determined.
28 * Pool memory is allocated to copy the memory map.
29 * The memory map is copied and the map key is obtained.
30 * The map key is used to exit the boot services.
32 void efi_st_exit_boot_services(void)
34 efi_uintn_t map_size = 0;
36 efi_uintn_t desc_size;
39 struct efi_mem_desc *memory_map;
41 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
43 if (ret != EFI_BUFFER_TOO_SMALL) {
45 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
48 /* Allocate extra space for newly allocated memory */
49 map_size += sizeof(struct efi_mem_desc);
50 ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
51 (void **)&memory_map);
52 if (ret != EFI_SUCCESS) {
53 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
56 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
57 &desc_size, &desc_version);
58 if (ret != EFI_SUCCESS) {
59 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
62 ret = boottime->exit_boot_services(handle, map_key);
63 if (ret != EFI_SUCCESS) {
64 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
67 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
73 * @test the test to be executed
74 * @failures counter that will be incremented if a failure occurs
75 * @return EFI_ST_SUCCESS for success
77 static int setup(struct efi_unit_test *test, unsigned int *failures)
82 return EFI_ST_SUCCESS;
83 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
84 ret = test->setup(handle, systable);
85 if (ret != EFI_ST_SUCCESS) {
86 efi_st_error("Setting up '%s' failed\n", test->name);
89 efi_st_printc(EFI_LIGHTGREEN,
90 "Setting up '%s' succeeded\n", test->name);
98 * @test the test to be executed
99 * @failures counter that will be incremented if a failure occurs
100 * @return EFI_ST_SUCCESS for success
102 static int execute(struct efi_unit_test *test, unsigned int *failures)
107 return EFI_ST_SUCCESS;
108 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
109 ret = test->execute();
110 if (ret != EFI_ST_SUCCESS) {
111 efi_st_error("Executing '%s' failed\n", test->name);
114 efi_st_printc(EFI_LIGHTGREEN,
115 "Executing '%s' succeeded\n", test->name);
123 * @test the test to be torn down
124 * @failures counter that will be incremented if a failure occurs
125 * @return EFI_ST_SUCCESS for success
127 static int teardown(struct efi_unit_test *test, unsigned int *failures)
132 return EFI_ST_SUCCESS;
133 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
134 ret = test->teardown();
135 if (ret != EFI_ST_SUCCESS) {
136 efi_st_error("Tearing down '%s' failed\n", test->name);
139 efi_st_printc(EFI_LIGHTGREEN,
140 "Tearing down '%s' succeeded\n", test->name);
146 * Check that a test requiring reset exists.
148 * @testname: name of the test
149 * @return: test, or NULL if not found
151 static bool need_reset(const u16 *testname)
153 struct efi_unit_test *test;
155 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
156 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
157 if (testname && efi_st_strcmp_16_8(testname, test->name))
159 if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT ||
160 test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT)
167 * Check that a test exists.
169 * @testname: name of the test
170 * @return: test, or NULL if not found
172 static struct efi_unit_test *find_test(const u16 *testname)
174 struct efi_unit_test *test;
176 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
177 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
178 if (!efi_st_strcmp_16_8(testname, test->name))
181 efi_st_printf("\nTest '%ps' not found\n", testname);
186 * List all available tests.
188 static void list_all_tests(void)
190 struct efi_unit_test *test;
193 efi_st_printf("\nAvailable tests:\n");
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) {
196 efi_st_printf("'%s'%s\n", test->name,
197 test->on_request ? " - on request" : "");
202 * Execute test steps of one phase.
204 * @testname name of a single selected test or NULL
206 * @steps steps to execute (mask with bits from EFI_ST_...)
207 * failures returns EFI_ST_SUCCESS if all test steps succeeded
209 void efi_st_do_tests(const u16 *testname, unsigned int phase,
210 unsigned int steps, unsigned int *failures)
213 struct efi_unit_test *test;
215 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
216 test < ll_entry_end(struct efi_unit_test, efi_unit_test);
219 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
221 if (test->phase != phase)
223 if (steps & EFI_ST_SETUP)
224 setup_status[i] = setup(test, failures);
225 if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS)
226 execute(test, failures);
227 if (steps & EFI_ST_TEARDOWN)
228 teardown(test, failures);
233 * Execute selftest of the EFI API
235 * This is the main entry point of the EFI selftest application.
237 * All tests use a driver model and are run in three phases:
238 * setup, execute, teardown.
240 * A test may be setup and executed at boottime,
241 * it may be setup at boottime and executed at runtime,
242 * or it may be setup and executed at runtime.
244 * After executing all tests the system is reset.
246 * @image_handle: handle of the loaded EFI image
247 * @systab: EFI system table
249 efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
250 struct efi_system_table *systab)
252 unsigned int failures = 0;
253 const u16 *testname = NULL;
254 struct efi_loaded_image *loaded_image;
258 boottime = systable->boottime;
259 runtime = systable->runtime;
260 handle = image_handle;
261 con_out = systable->con_out;
262 con_in = systable->con_in;
264 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
265 (void **)&loaded_image);
266 if (ret != EFI_SUCCESS) {
267 efi_st_error("Cannot open loaded image protocol\n");
271 if (loaded_image->load_options)
272 testname = (u16 *)loaded_image->load_options;
275 if (!efi_st_strcmp_16_8(testname, "list") ||
276 !find_test(testname)) {
280 * Once the Exit boottime service is correctly
281 * implemented we should call
282 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
284 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
290 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
293 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
295 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
296 ll_entry_count(struct efi_unit_test,
299 /* Allocate buffer for setup results */
300 ret = boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
301 ll_entry_count(struct efi_unit_test,
303 (void **)&setup_status);
304 if (ret != EFI_SUCCESS) {
305 efi_st_error("Allocate pool failed\n");
309 /* Execute boottime tests */
310 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
311 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
314 if (!need_reset(testname)) {
316 ret = EFI_PROTOCOL_ERROR;
319 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n",
324 /* Execute mixed tests */
325 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
326 EFI_ST_SETUP, &failures);
328 efi_st_exit_boot_services();
330 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
331 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
333 /* Execute runtime tests */
334 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
335 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
339 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
342 efi_st_printf("Preparing for reset. Press any key...\n");
345 if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
346 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
347 sizeof(reset_message), reset_message);
350 do_reset(NULL, 0, 0, NULL);
354 efi_st_error("Reset failed\n");
356 return EFI_UNSUPPORTED;