blob: 85e819bdfa11841bd95b0c1b7ff5021d1794dcda [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
41 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
42 &desc_version);
43 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020044 efi_st_error(
45 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020046 return;
47 }
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) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020053 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020054 return;
55 }
56 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
57 &desc_size, &desc_version);
58 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020059 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020060 return;
61 }
62 ret = boottime->exit_boot_services(handle, map_key);
63 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020064 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020065 return;
66 }
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010067 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020068}
69
70/*
71 * Set up a test.
72 *
73 * @test the test to be executed
74 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020075 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020076 */
77static int setup(struct efi_unit_test *test, unsigned int *failures)
78{
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020079 int ret;
80
81 if (!test->setup)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020082 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010083 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020084 ret = test->setup(handle, systable);
85 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020086 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020087 ++*failures;
88 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +010089 efi_st_printc(EFI_LIGHTGREEN,
90 "Setting up '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020091 }
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +020092 return ret;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020093}
94
95/*
96 * Execute a test.
97 *
98 * @test the test to be executed
99 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200100 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200101 */
102static int execute(struct efi_unit_test *test, unsigned int *failures)
103{
104 int ret;
105
106 if (!test->execute)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200107 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100108 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200109 ret = test->execute();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200110 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200111 efi_st_error("Executing '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200112 ++*failures;
113 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100114 efi_st_printc(EFI_LIGHTGREEN,
115 "Executing '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200116 }
117 return ret;
118}
119
120/*
121 * Tear down a test.
122 *
123 * @test the test to be torn down
124 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200125 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200126 */
127static int teardown(struct efi_unit_test *test, unsigned int *failures)
128{
129 int ret;
130
131 if (!test->teardown)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200132 return EFI_ST_SUCCESS;
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100133 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200134 ret = test->teardown();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200135 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200136 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200137 ++*failures;
138 } else {
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100139 efi_st_printc(EFI_LIGHTGREEN,
140 "Tearing down '%s' succeeded\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200141 }
142 return ret;
143}
144
145/*
Heinrich Schuchardt496005d2020-09-30 21:52:09 +0200146 * Check that a test requiring reset exists.
147 *
148 * @testname: name of the test
149 * @return: test, or NULL if not found
150 */
151static bool need_reset(const u16 *testname)
152{
153 struct efi_unit_test *test;
154
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))
158 continue;
159 if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT ||
160 test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT)
161 return true;
162 }
163 return false;
164}
165
166/*
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200167 * Check that a test exists.
168 *
169 * @testname: name of the test
Simon Glass0b9680f2018-06-18 08:08:21 -0600170 * @return: test, or NULL if not found
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200171 */
172static struct efi_unit_test *find_test(const u16 *testname)
173{
174 struct efi_unit_test *test;
175
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))
179 return test;
180 }
181 efi_st_printf("\nTest '%ps' not found\n", testname);
182 return NULL;
183}
184
185/*
186 * List all available tests.
187 */
188static void list_all_tests(void)
189{
190 struct efi_unit_test *test;
191
192 /* List all tests */
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" : "");
198 }
199}
200
201/*
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200202 * Execute test steps of one phase.
203 *
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200204 * @testname name of a single selected test or NULL
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200205 * @phase test phase
Simon Glass0b9680f2018-06-18 08:08:21 -0600206 * @steps steps to execute (mask with bits from EFI_ST_...)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200207 * failures returns EFI_ST_SUCCESS if all test steps succeeded
208 */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200209void efi_st_do_tests(const u16 *testname, unsigned int phase,
210 unsigned int steps, unsigned int *failures)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200211{
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200212 int i = 0;
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200213 struct efi_unit_test *test;
214
215 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200216 test < ll_entry_end(struct efi_unit_test, efi_unit_test);
217 ++test, ++i) {
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200218 if (testname ?
219 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
220 continue;
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200221 if (test->phase != phase)
222 continue;
223 if (steps & EFI_ST_SETUP)
Heinrich Schuchardt72819332018-10-22 23:15:10 +0200224 setup_status[i] = setup(test, failures);
225 if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200226 execute(test, failures);
227 if (steps & EFI_ST_TEARDOWN)
228 teardown(test, failures);
229 }
230}
231
232/*
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200233 * Execute selftest of the EFI API
234 *
235 * This is the main entry point of the EFI selftest application.
236 *
237 * All tests use a driver model and are run in three phases:
238 * setup, execute, teardown.
239 *
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.
243 *
244 * After executing all tests the system is reset.
245 *
246 * @image_handle: handle of the loaded EFI image
247 * @systab: EFI system table
248 */
249efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
250 struct efi_system_table *systab)
251{
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200252 unsigned int failures = 0;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200253 const u16 *testname = NULL;
254 struct efi_loaded_image *loaded_image;
255 efi_status_t ret;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200256
257 systable = systab;
258 boottime = systable->boottime;
259 runtime = systable->runtime;
260 handle = image_handle;
261 con_out = systable->con_out;
262 con_in = systable->con_in;
263
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200264 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
265 (void **)&loaded_image);
266 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbf7ea1d2017-11-26 14:05:19 +0100267 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200268 return ret;
269 }
270
271 if (loaded_image->load_options)
272 testname = (u16 *)loaded_image->load_options;
273
274 if (testname) {
275 if (!efi_st_strcmp_16_8(testname, "list") ||
276 !find_test(testname)) {
277 list_all_tests();
278 /*
279 * TODO:
280 * Once the Exit boottime service is correctly
281 * implemented we should call
282 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
283 * here, cf.
284 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
285 */
286 return EFI_SUCCESS;
287 }
288 }
289
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100290 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200291
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200292 if (testname)
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100293 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200294 else
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100295 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200296 ll_entry_count(struct efi_unit_test,
297 efi_unit_test));
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200298
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200299 /* Allocate buffer for setup results */
300 ret = boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) *
301 ll_entry_count(struct efi_unit_test,
302 efi_unit_test),
Heinrich Schuchardt72819332018-10-22 23:15:10 +0200303 (void **)&setup_status);
Heinrich Schuchardt0e77ca92018-10-19 07:51:26 +0200304 if (ret != EFI_SUCCESS) {
305 efi_st_error("Allocate pool failed\n");
306 return ret;
307 }
308
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200309 /* Execute boottime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200310 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200311 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
312 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200313
Heinrich Schuchardt496005d2020-09-30 21:52:09 +0200314 if (!need_reset(testname)) {
315 if (failures)
316 ret = EFI_PROTOCOL_ERROR;
317
318 /* Give feedback */
319 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n",
320 failures);
321 return ret;
322 }
323
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200324 /* Execute mixed tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200325 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200326 EFI_ST_SETUP, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200327
328 efi_st_exit_boot_services();
329
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200330 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200331 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200332
333 /* Execute runtime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200334 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200335 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
336 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200337
338 /* Give feedback */
Heinrich Schuchardt9137df82018-01-11 08:15:54 +0100339 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200340
341 /* Reset system */
Simon Glass0b9680f2018-06-18 08:08:21 -0600342 efi_st_printf("Preparing for reset. Press any key...\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200343 efi_st_get_key();
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200344
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200345 if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) {
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200346 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
347 sizeof(reset_message), reset_message);
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200348 } else {
349 efi_restore_gd();
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200350 do_reset(NULL, 0, 0, NULL);
Heinrich Schuchardtc450db52020-09-10 07:47:58 +0200351 }
Heinrich Schuchardtd5e85712020-08-22 09:14:56 +0200352
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200353 efi_st_printf("\n");
Simon Glass0b9680f2018-06-18 08:08:21 -0600354 efi_st_error("Reset failed\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200355
356 return EFI_UNSUPPORTED;
357}