blob: 13eb2cd60487ff20052d8d17ad297f61c44f5437 [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
8#include <efi_selftest.h>
9#include <vsprintf.h>
10
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +020011/*
12 * Constants for test step bitmap
13 */
14#define EFI_ST_SETUP 1
15#define EFI_ST_EXECUTE 2
16#define EFI_ST_TEARDOWN 4
17
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020018static const struct efi_system_table *systable;
19static const struct efi_boot_services *boottime;
20static const struct efi_runtime_services *runtime;
21static efi_handle_t handle;
22static u16 reset_message[] = L"Selftest completed";
23
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.
29 * The memory amp is copied and the map key is obtained.
30 * 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
41 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
42 &desc_version);
43 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020044 efi_st_error(
45 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardt623b3a52017-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 Schuchardt037ee6f2017-10-04 12:37:02 +020053 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-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 Schuchardt037ee6f2017-10-04 12:37:02 +020059 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020060 return;
61 }
62 ret = boottime->exit_boot_services(handle, map_key);
63 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020064 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020065 return;
66 }
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010067 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +020075 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020076 */
77static int setup(struct efi_unit_test *test, unsigned int *failures)
78{
Heinrich Schuchardtb5cd6872018-04-16 07:59:03 +020079 if (!test->setup) {
80 test->setup_ok = EFI_ST_SUCCESS;
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020081 return EFI_ST_SUCCESS;
Heinrich Schuchardtb5cd6872018-04-16 07:59:03 +020082 }
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010083 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
Heinrich Schuchardtb5cd6872018-04-16 07:59:03 +020084 test->setup_ok = test->setup(handle, systable);
85 if (test->setup_ok != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020086 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020087 ++*failures;
88 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010089 efi_st_printc(EFI_LIGHTGREEN,
90 "Setting up '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020091 }
Heinrich Schuchardtb5cd6872018-04-16 07:59:03 +020092 return test->setup_ok;
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +0200100 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +0200107 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100108 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200109 ret = test->execute();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200110 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200111 efi_st_error("Executing '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200112 ++*failures;
113 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100114 efi_st_printc(EFI_LIGHTGREEN,
115 "Executing '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +0200125 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +0200132 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100133 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200134 ret = test->teardown();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200135 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200136 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200137 ++*failures;
138 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100139 efi_st_printc(EFI_LIGHTGREEN,
140 "Tearing down '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200141 }
142 return ret;
143}
144
145/*
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200146 * Check that a test exists.
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200147 *
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200148 * @testname: name of the test
149 * @return: test
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200150 */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200151static struct efi_unit_test *find_test(const u16 *testname)
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200152{
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) {
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200157 if (!efi_st_strcmp_16_8(testname, test->name))
158 return test;
159 }
160 efi_st_printf("\nTest '%ps' not found\n", testname);
161 return NULL;
162}
163
164/*
165 * List all available tests.
166 */
167static void list_all_tests(void)
168{
169 struct efi_unit_test *test;
170
171 /* List all tests */
172 efi_st_printf("\nAvailable tests:\n");
173 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
174 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
175 efi_st_printf("'%s'%s\n", test->name,
176 test->on_request ? " - on request" : "");
177 }
178}
179
180/*
181 * Execute test steps of one phase.
182 *
183 * @testname name of a single selected test or NULL
184 * @phase test phase
185 * @steps steps to execute
186 * failures returns EFI_ST_SUCCESS if all test steps succeeded
187 */
188void efi_st_do_tests(const u16 *testname, unsigned int phase,
189 unsigned int steps, unsigned int *failures)
190{
191 struct efi_unit_test *test;
192
193 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
194 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
195 if (testname ?
196 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
197 continue;
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200198 if (test->phase != phase)
199 continue;
200 if (steps & EFI_ST_SETUP)
201 setup(test, failures);
Heinrich Schuchardtb5cd6872018-04-16 07:59:03 +0200202 if (steps & EFI_ST_EXECUTE && test->setup_ok == EFI_ST_SUCCESS)
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200203 execute(test, failures);
204 if (steps & EFI_ST_TEARDOWN)
205 teardown(test, failures);
206 }
207}
208
209/*
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200210 * Execute selftest of the EFI API
211 *
212 * This is the main entry point of the EFI selftest application.
213 *
214 * All tests use a driver model and are run in three phases:
215 * setup, execute, teardown.
216 *
217 * A test may be setup and executed at boottime,
218 * it may be setup at boottime and executed at runtime,
219 * or it may be setup and executed at runtime.
220 *
221 * After executing all tests the system is reset.
222 *
223 * @image_handle: handle of the loaded EFI image
224 * @systab: EFI system table
225 */
226efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
227 struct efi_system_table *systab)
228{
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200229 unsigned int failures = 0;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200230 const u16 *testname = NULL;
231 struct efi_loaded_image *loaded_image;
232 efi_status_t ret;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200233
234 systable = systab;
235 boottime = systable->boottime;
236 runtime = systable->runtime;
237 handle = image_handle;
238 con_out = systable->con_out;
239 con_in = systable->con_in;
240
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200241 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
242 (void **)&loaded_image);
243 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc51e7df2017-11-26 14:05:19 +0100244 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200245 return ret;
246 }
247
248 if (loaded_image->load_options)
249 testname = (u16 *)loaded_image->load_options;
250
251 if (testname) {
252 if (!efi_st_strcmp_16_8(testname, "list") ||
253 !find_test(testname)) {
254 list_all_tests();
255 /*
256 * TODO:
257 * Once the Exit boottime service is correctly
258 * implemented we should call
259 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
260 * here, cf.
261 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
262 */
263 return EFI_SUCCESS;
264 }
265 }
266
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100267 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200268
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200269 if (testname)
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100270 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200271 else
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100272 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200273 ll_entry_count(struct efi_unit_test,
274 efi_unit_test));
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200275
276 /* Execute boottime tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200277 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200278 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
279 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200280
281 /* Execute mixed tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200282 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200283 EFI_ST_SETUP, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200284
285 efi_st_exit_boot_services();
286
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200287 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200288 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200289
290 /* Execute runtime tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200291 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200292 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
293 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200294
295 /* Give feedback */
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100296 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200297
298 /* Reset system */
299 efi_st_printf("Preparing for reset. Press any key.\n");
300 efi_st_get_key();
301 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
302 sizeof(reset_message), reset_message);
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200303 efi_st_printf("\n");
304 efi_st_error("Reset failed.\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200305
306 return EFI_UNSUPPORTED;
307}