blob: 191da7fc451751ea5cca10aec4f6be77b60c72af [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02002/*
3 * EFI efi_selftest
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02006 */
7
Heinrich Schuchardtfa637532020-08-22 09:14:56 +02008#include <command.h>
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02009#include <efi_selftest.h>
10#include <vsprintf.h>
11
Simon Glass404ea592018-06-18 08:08:21 -060012/* Constants for test step bitmap */
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +020013#define EFI_ST_SETUP 1
14#define EFI_ST_EXECUTE 2
15#define EFI_ST_TEARDOWN 4
16
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020017const struct efi_system_table *st_systable;
18const struct efi_boot_services *st_boottime;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020019static const struct efi_runtime_services *runtime;
20static efi_handle_t handle;
Simon Glass156ccbc2022-01-23 12:55:12 -070021static u16 reset_message[] = u"Selftest completed";
Heinrich Schuchardt3c2c54c2018-10-22 23:15:10 +020022static int *setup_status;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020023
24/*
25 * Exit the boot services.
26 *
27 * The size of the memory map is determined.
28 * Pool memory is allocated to copy the memory map.
Simon Glass404ea592018-06-18 08:08:21 -060029 * The memory map is copied and the map key is obtained.
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020030 * The map key is used to exit the boot services.
31 */
32void efi_st_exit_boot_services(void)
33{
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +010034 efi_uintn_t map_size = 0;
35 efi_uintn_t map_key;
36 efi_uintn_t desc_size;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020037 u32 desc_version;
38 efi_status_t ret;
39 struct efi_mem_desc *memory_map;
40
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +010041 /* Do not detach devices in ExitBootServices. We need the console. */
42 efi_st_keep_devices = true;
43
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020044 ret = st_boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020045 &desc_version);
46 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020047 efi_st_error(
48 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020049 return;
50 }
51 /* Allocate extra space for newly allocated memory */
52 map_size += sizeof(struct efi_mem_desc);
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020053 ret = st_boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020054 (void **)&memory_map);
55 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020056 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020057 return;
58 }
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020059 ret = st_boottime->get_memory_map(&map_size, memory_map, &map_key,
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020060 &desc_size, &desc_version);
61 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020062 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020063 return;
64 }
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020065 ret = st_boottime->exit_boot_services(handle, map_key);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020066 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020067 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020068 return;
69 }
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010070 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020071}
72
73/*
74 * Set up a test.
75 *
76 * @test the test to be executed
77 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010078 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020079 */
80static int setup(struct efi_unit_test *test, unsigned int *failures)
81{
Heinrich Schuchardt4c174392018-10-19 07:51:26 +020082 int ret;
83
84 if (!test->setup)
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020085 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010086 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020087 ret = test->setup(handle, st_systable);
Heinrich Schuchardt4c174392018-10-19 07:51:26 +020088 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020089 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020090 ++*failures;
91 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010092 efi_st_printc(EFI_LIGHTGREEN,
93 "Setting up '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020094 }
Heinrich Schuchardt4c174392018-10-19 07:51:26 +020095 return ret;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020096}
97
98/*
99 * Execute a test.
100 *
101 * @test the test to be executed
102 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100103 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200104 */
105static int execute(struct efi_unit_test *test, unsigned int *failures)
106{
107 int ret;
108
109 if (!test->execute)
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200110 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100111 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200112 ret = test->execute();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200113 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200114 efi_st_error("Executing '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200115 ++*failures;
116 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100117 efi_st_printc(EFI_LIGHTGREEN,
118 "Executing '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200119 }
120 return ret;
121}
122
123/*
124 * Tear down a test.
125 *
126 * @test the test to be torn down
127 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100128 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200129 */
130static int teardown(struct efi_unit_test *test, unsigned int *failures)
131{
132 int ret;
133
134 if (!test->teardown)
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200135 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100136 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200137 ret = test->teardown();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200138 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200139 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200140 ++*failures;
141 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100142 efi_st_printc(EFI_LIGHTGREEN,
143 "Tearing down '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200144 }
145 return ret;
146}
147
148/*
Heinrich Schuchardteb0d1d82020-09-30 21:52:09 +0200149 * Check that a test requiring reset exists.
150 *
151 * @testname: name of the test
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100152 * Return: test, or NULL if not found
Heinrich Schuchardteb0d1d82020-09-30 21:52:09 +0200153 */
154static bool need_reset(const u16 *testname)
155{
156 struct efi_unit_test *test;
157
158 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
159 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
160 if (testname && efi_st_strcmp_16_8(testname, test->name))
161 continue;
162 if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT ||
Heinrich Schuchardt5e219582021-03-24 17:48:01 +0100163 test->phase == EFI_SETTING_VIRTUAL_ADDRESS_MAP)
Heinrich Schuchardteb0d1d82020-09-30 21:52:09 +0200164 return true;
165 }
166 return false;
167}
168
169/*
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200170 * Check that a test exists.
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200171 *
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200172 * @testname: name of the test
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100173 * Return: test, or NULL if not found
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200174 */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200175static struct efi_unit_test *find_test(const u16 *testname)
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200176{
177 struct efi_unit_test *test;
178
179 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
180 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200181 if (!efi_st_strcmp_16_8(testname, test->name))
182 return test;
183 }
184 efi_st_printf("\nTest '%ps' not found\n", testname);
185 return NULL;
186}
187
188/*
189 * List all available tests.
190 */
191static void list_all_tests(void)
192{
193 struct efi_unit_test *test;
194
195 /* List all tests */
196 efi_st_printf("\nAvailable tests:\n");
197 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
198 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
199 efi_st_printf("'%s'%s\n", test->name,
200 test->on_request ? " - on request" : "");
201 }
202}
203
204/*
205 * Execute test steps of one phase.
206 *
207 * @testname name of a single selected test or NULL
208 * @phase test phase
Simon Glass404ea592018-06-18 08:08:21 -0600209 * @steps steps to execute (mask with bits from EFI_ST_...)
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200210 * failures returns EFI_ST_SUCCESS if all test steps succeeded
211 */
212void efi_st_do_tests(const u16 *testname, unsigned int phase,
213 unsigned int steps, unsigned int *failures)
214{
Heinrich Schuchardt4c174392018-10-19 07:51:26 +0200215 int i = 0;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200216 struct efi_unit_test *test;
217
218 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
Heinrich Schuchardt4c174392018-10-19 07:51:26 +0200219 test < ll_entry_end(struct efi_unit_test, efi_unit_test);
220 ++test, ++i) {
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200221 if (testname ?
222 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
223 continue;
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200224 if (test->phase != phase)
225 continue;
226 if (steps & EFI_ST_SETUP)
Heinrich Schuchardt3c2c54c2018-10-22 23:15:10 +0200227 setup_status[i] = setup(test, failures);
228 if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS)
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200229 execute(test, failures);
230 if (steps & EFI_ST_TEARDOWN)
231 teardown(test, failures);
232 }
233}
234
235/*
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200236 * Execute selftest of the EFI API
237 *
238 * This is the main entry point of the EFI selftest application.
239 *
240 * All tests use a driver model and are run in three phases:
241 * setup, execute, teardown.
242 *
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200243 * A test may be setup and executed at st_boottime,
244 * it may be setup at st_boottime and executed at runtime,
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200245 * or it may be setup and executed at runtime.
246 *
247 * After executing all tests the system is reset.
248 *
249 * @image_handle: handle of the loaded EFI image
250 * @systab: EFI system table
251 */
252efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
253 struct efi_system_table *systab)
254{
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200255 unsigned int failures = 0;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200256 const u16 *testname = NULL;
257 struct efi_loaded_image *loaded_image;
258 efi_status_t ret;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200259
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200260 st_systable = systab;
261 st_boottime = st_systable->boottime;
262 runtime = st_systable->runtime;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200263 handle = image_handle;
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200264 con_out = st_systable->con_out;
265 con_in = st_systable->con_in;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200266
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200267 ret = st_boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200268 (void **)&loaded_image);
269 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc51e7df2017-11-26 14:05:19 +0100270 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200271 return ret;
272 }
273
274 if (loaded_image->load_options)
275 testname = (u16 *)loaded_image->load_options;
276
277 if (testname) {
278 if (!efi_st_strcmp_16_8(testname, "list") ||
279 !find_test(testname)) {
280 list_all_tests();
281 /*
282 * TODO:
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200283 * Once the Exit st_boottime service is correctly
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200284 * implemented we should call
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200285 * st_boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200286 * here, cf.
287 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
288 */
289 return EFI_SUCCESS;
290 }
291 }
292
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100293 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200294
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200295 if (testname)
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100296 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200297 else
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100298 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200299 ll_entry_count(struct efi_unit_test,
300 efi_unit_test));
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200301
Heinrich Schuchardt4c174392018-10-19 07:51:26 +0200302 /* Allocate buffer for setup results */
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200303 ret = st_boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
Heinrich Schuchardt4c174392018-10-19 07:51:26 +0200304 ll_entry_count(struct efi_unit_test,
305 efi_unit_test),
Heinrich Schuchardt3c2c54c2018-10-22 23:15:10 +0200306 (void **)&setup_status);
Heinrich Schuchardt4c174392018-10-19 07:51:26 +0200307 if (ret != EFI_SUCCESS) {
308 efi_st_error("Allocate pool failed\n");
309 return ret;
310 }
311
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200312 /* Execute st_boottime tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200313 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200314 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
315 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200316
Heinrich Schuchardteb0d1d82020-09-30 21:52:09 +0200317 if (!need_reset(testname)) {
318 if (failures)
319 ret = EFI_PROTOCOL_ERROR;
320
321 /* Give feedback */
322 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n",
323 failures);
324 return ret;
325 }
326
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200327 /* Execute mixed tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200328 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200329 EFI_ST_SETUP, &failures);
Heinrich Schuchardt5e219582021-03-24 17:48:01 +0100330 efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP,
331 EFI_ST_SETUP, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200332
333 efi_st_exit_boot_services();
334
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200335 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200336 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardt5e219582021-03-24 17:48:01 +0100337 /* Execute test setting the virtual address map */
338 efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP,
339 EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200340 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200341
342 /* Give feedback */
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100343 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200344
345 /* Reset system */
Simon Glass404ea592018-06-18 08:08:21 -0600346 efi_st_printf("Preparing for reset. Press any key...\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200347 efi_st_get_key();
Heinrich Schuchardtfa637532020-08-22 09:14:56 +0200348
Heinrich Schuchardt5bf12a72020-09-10 07:47:58 +0200349 if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
Heinrich Schuchardtfa637532020-08-22 09:14:56 +0200350 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
351 sizeof(reset_message), reset_message);
Heinrich Schuchardt5bf12a72020-09-10 07:47:58 +0200352 } else {
353 efi_restore_gd();
Heinrich Schuchardtfa637532020-08-22 09:14:56 +0200354 do_reset(NULL, 0, 0, NULL);
Heinrich Schuchardt5bf12a72020-09-10 07:47:58 +0200355 }
Heinrich Schuchardtfa637532020-08-22 09:14:56 +0200356
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200357 efi_st_printf("\n");
Simon Glass404ea592018-06-18 08:08:21 -0600358 efi_st_error("Reset failed\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200359
360 return EFI_UNSUPPORTED;
361}