blob: b8eed048c23d1275d16c60ece2af01abf581028a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +02002/*
3 * EFI efi_selftest
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +02006 */
7
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +02008#include <command.h>
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +02009#include <efi_selftest.h>
10#include <vsprintf.h>
11
Simon Glass0b9680f2018-06-18 08:08:21 -060012/* Constants for test step bitmap */
Heinrich Schuchardt2eb15582017-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 Schuchardtd33ae3e2017-09-15 10:06:11 +020017static const struct efi_system_table *systable;
18static const struct efi_boot_services *boottime;
19static const struct efi_runtime_services *runtime;
20static efi_handle_t handle;
21static u16 reset_message[] = L"Selftest completed";
Heinrich Schuchardt72819332018-10-22 23:15:10 +020022static int *setup_status;
Heinrich Schuchardtd33ae3e2017-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 Glass0b9680f2018-06-18 08:08:21 -060029 * The memory map is copied and the map key is obtained.
Heinrich Schuchardtd33ae3e2017-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 Schuchardt798a4412017-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 Schuchardtd33ae3e2017-09-15 10:06:11 +020037 u32 desc_version;
38 efi_status_t ret;
39 struct efi_mem_desc *memory_map;
40
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010041 /* Do not detach devices in ExitBootServices. We need the console. */
42 efi_st_keep_devices = true;
43
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020044 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
45 &desc_version);
46 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020047 efi_st_error(
48 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020049 return;
50 }
51 /* Allocate extra space for newly allocated memory */
52 map_size += sizeof(struct efi_mem_desc);
53 ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
54 (void **)&memory_map);
55 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020056 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020057 return;
58 }
59 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
60 &desc_size, &desc_version);
61 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020062 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020063 return;
64 }
65 ret = boottime->exit_boot_services(handle, map_key);
66 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020067 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020068 return;
69 }
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010070 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
Heinrich Schuchardtd33ae3e2017-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 Schuchardt1afe5f72017-10-04 15:31:26 +020078 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020079 */
80static int setup(struct efi_unit_test *test, unsigned int *failures)
81{
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020082 int ret;
83
84 if (!test->setup)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020085 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010086 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020087 ret = test->setup(handle, systable);
88 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020089 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020090 ++*failures;
91 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010092 efi_st_printc(EFI_LIGHTGREEN,
93 "Setting up '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020094 }
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020095 return ret;
Heinrich Schuchardtd33ae3e2017-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 Schuchardt1afe5f72017-10-04 15:31:26 +0200103 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-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 Schuchardt1afe5f72017-10-04 15:31:26 +0200110 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100111 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200112 ret = test->execute();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200113 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200114 efi_st_error("Executing '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200115 ++*failures;
116 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100117 efi_st_printc(EFI_LIGHTGREEN,
118 "Executing '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-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 Schuchardt1afe5f72017-10-04 15:31:26 +0200128 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-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 Schuchardt1afe5f72017-10-04 15:31:26 +0200135 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100136 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200137 ret = test->teardown();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200138 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200139 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200140 ++*failures;
141 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100142 efi_st_printc(EFI_LIGHTGREEN,
143 "Tearing down '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200144 }
145 return ret;
146}
147
148/*
Heinrich Schuchardt496005d2020-09-30 21:52:09 +0200149 * Check that a test requiring reset exists.
150 *
151 * @testname: name of the test
152 * @return: test, or NULL if not found
153 */
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 ||
163 test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT)
164 return true;
165 }
166 return false;
167}
168
169/*
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200170 * Check that a test exists.
171 *
172 * @testname: name of the test
Simon Glass0b9680f2018-06-18 08:08:21 -0600173 * @return: test, or NULL if not found
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200174 */
175static struct efi_unit_test *find_test(const u16 *testname)
176{
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) {
181 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/*
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200205 * Execute test steps of one phase.
206 *
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200207 * @testname name of a single selected test or NULL
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200208 * @phase test phase
Simon Glass0b9680f2018-06-18 08:08:21 -0600209 * @steps steps to execute (mask with bits from EFI_ST_...)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200210 * failures returns EFI_ST_SUCCESS if all test steps succeeded
211 */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200212void efi_st_do_tests(const u16 *testname, unsigned int phase,
213 unsigned int steps, unsigned int *failures)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200214{
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200215 int i = 0;
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200216 struct efi_unit_test *test;
217
218 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200219 test < ll_entry_end(struct efi_unit_test, efi_unit_test);
220 ++test, ++i) {
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200221 if (testname ?
222 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
223 continue;
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200224 if (test->phase != phase)
225 continue;
226 if (steps & EFI_ST_SETUP)
Heinrich Schuchardt72819332018-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 Schuchardt2eb15582017-10-18 18:13:12 +0200229 execute(test, failures);
230 if (steps & EFI_ST_TEARDOWN)
231 teardown(test, failures);
232 }
233}
234
235/*
Heinrich Schuchardtd33ae3e2017-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 *
243 * A test may be setup and executed at boottime,
244 * it may be setup at boottime and executed at runtime,
245 * 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 Schuchardtd33ae3e2017-09-15 10:06:11 +0200255 unsigned int failures = 0;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200256 const u16 *testname = NULL;
257 struct efi_loaded_image *loaded_image;
258 efi_status_t ret;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200259
260 systable = systab;
261 boottime = systable->boottime;
262 runtime = systable->runtime;
263 handle = image_handle;
264 con_out = systable->con_out;
265 con_in = systable->con_in;
266
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200267 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
268 (void **)&loaded_image);
269 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbf7ea1d2017-11-26 14:05:19 +0100270 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardt02efd5d2017-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:
283 * Once the Exit boottime service is correctly
284 * implemented we should call
285 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
286 * here, cf.
287 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
288 */
289 return EFI_SUCCESS;
290 }
291 }
292
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100293 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200294
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200295 if (testname)
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100296 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200297 else
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100298 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200299 ll_entry_count(struct efi_unit_test,
300 efi_unit_test));
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200301
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200302 /* Allocate buffer for setup results */
303 ret = boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
304 ll_entry_count(struct efi_unit_test,
305 efi_unit_test),
Heinrich Schuchardt72819332018-10-22 23:15:10 +0200306 (void **)&setup_status);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200307 if (ret != EFI_SUCCESS) {
308 efi_st_error("Allocate pool failed\n");
309 return ret;
310 }
311
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200312 /* Execute boottime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200313 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200314 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
315 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200316
Heinrich Schuchardt496005d2020-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 Schuchardtd33ae3e2017-09-15 10:06:11 +0200327 /* Execute mixed tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200328 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200329 EFI_ST_SETUP, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200330
331 efi_st_exit_boot_services();
332
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200333 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200334 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200335
336 /* Execute runtime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200337 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200338 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
339 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200340
341 /* Give feedback */
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100342 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200343
344 /* Reset system */
Simon Glass0b9680f2018-06-18 08:08:21 -0600345 efi_st_printf("Preparing for reset. Press any key...\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200346 efi_st_get_key();
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200347
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200348 if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200349 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
350 sizeof(reset_message), reset_message);
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200351 } else {
352 efi_restore_gd();
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200353 do_reset(NULL, 0, 0, NULL);
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200354 }
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200355
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200356 efi_st_printf("\n");
Simon Glass0b9680f2018-06-18 08:08:21 -0600357 efi_st_error("Reset failed\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200358
359 return EFI_UNSUPPORTED;
360}