blob: 4e5a12c47cae33543b15c230675467f040bca52e [file] [log] [blame]
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +02001/*
2 * EFI efi_selftest
3 *
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <efi_selftest.h>
10#include <vsprintf.h>
11
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +020012/*
13 * Constants for test step bitmap
14 */
15#define EFI_ST_SETUP 1
16#define EFI_ST_EXECUTE 2
17#define EFI_ST_TEARDOWN 4
18
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020019static const struct efi_system_table *systable;
20static const struct efi_boot_services *boottime;
21static const struct efi_runtime_services *runtime;
22static efi_handle_t handle;
23static u16 reset_message[] = L"Selftest completed";
24
25/*
26 * Exit the boot services.
27 *
28 * The size of the memory map is determined.
29 * Pool memory is allocated to copy the memory map.
30 * The memory amp is copied and the map key is obtained.
31 * The map key is used to exit the boot services.
32 */
33void efi_st_exit_boot_services(void)
34{
Heinrich Schuchardt798a4412017-11-06 21:17:48 +010035 efi_uintn_t map_size = 0;
36 efi_uintn_t map_key;
37 efi_uintn_t desc_size;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020038 u32 desc_version;
39 efi_status_t ret;
40 struct efi_mem_desc *memory_map;
41
42 ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
43 &desc_version);
44 if (ret != EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020045 efi_st_error(
46 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020047 return;
48 }
49 /* Allocate extra space for newly allocated memory */
50 map_size += sizeof(struct efi_mem_desc);
51 ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
52 (void **)&memory_map);
53 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020054 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020055 return;
56 }
57 ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
58 &desc_size, &desc_version);
59 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020060 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020061 return;
62 }
63 ret = boottime->exit_boot_services(handle, map_key);
64 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020065 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020066 return;
67 }
68 efi_st_printf("\nBoot services terminated\n");
69}
70
71/*
72 * Set up a test.
73 *
74 * @test the test to be executed
75 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020076 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020077 */
78static int setup(struct efi_unit_test *test, unsigned int *failures)
79{
80 int ret;
81
82 if (!test->setup)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020083 return EFI_ST_SUCCESS;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020084 efi_st_printf("\nSetting up '%s'\n", test->name);
85 ret = test->setup(handle, systable);
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +020086 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +020087 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +020088 ++*failures;
89 } else {
90 efi_st_printf("Setting up '%s' succeeded\n", test->name);
91 }
92 return ret;
93}
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 Schuchardtd33ae3e2017-09-15 10:06:11 +0200108 efi_st_printf("\nExecuting '%s'\n", test->name);
109 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 {
114 efi_st_printf("Executing '%s' succeeded\n", test->name);
115 }
116 return ret;
117}
118
119/*
120 * Tear down a test.
121 *
122 * @test the test to be torn down
123 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200124 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200125 */
126static int teardown(struct efi_unit_test *test, unsigned int *failures)
127{
128 int ret;
129
130 if (!test->teardown)
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200131 return EFI_ST_SUCCESS;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200132 efi_st_printf("\nTearing down '%s'\n", test->name);
133 ret = test->teardown();
Heinrich Schuchardt1afe5f72017-10-04 15:31:26 +0200134 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200135 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200136 ++*failures;
137 } else {
138 efi_st_printf("Tearing down '%s' succeeded\n", test->name);
139 }
140 return ret;
141}
142
143/*
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200144 * Check that a test exists.
145 *
146 * @testname: name of the test
147 * @return: test
148 */
149static struct efi_unit_test *find_test(const u16 *testname)
150{
151 struct efi_unit_test *test;
152
153 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
154 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
155 if (!efi_st_strcmp_16_8(testname, test->name))
156 return test;
157 }
158 efi_st_printf("\nTest '%ps' not found\n", testname);
159 return NULL;
160}
161
162/*
163 * List all available tests.
164 */
165static void list_all_tests(void)
166{
167 struct efi_unit_test *test;
168
169 /* List all tests */
170 efi_st_printf("\nAvailable tests:\n");
171 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
172 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
173 efi_st_printf("'%s'%s\n", test->name,
174 test->on_request ? " - on request" : "");
175 }
176}
177
178/*
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200179 * Execute test steps of one phase.
180 *
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200181 * @testname name of a single selected test or NULL
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200182 * @phase test phase
183 * @steps steps to execute
184 * failures returns EFI_ST_SUCCESS if all test steps succeeded
185 */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200186void efi_st_do_tests(const u16 *testname, unsigned int phase,
187 unsigned int steps, unsigned int *failures)
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200188{
189 struct efi_unit_test *test;
190
191 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
192 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200193 if (testname ?
194 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
195 continue;
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200196 if (test->phase != phase)
197 continue;
198 if (steps & EFI_ST_SETUP)
199 setup(test, failures);
200 if (steps & EFI_ST_EXECUTE)
201 execute(test, failures);
202 if (steps & EFI_ST_TEARDOWN)
203 teardown(test, failures);
204 }
205}
206
207/*
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200208 * Execute selftest of the EFI API
209 *
210 * This is the main entry point of the EFI selftest application.
211 *
212 * All tests use a driver model and are run in three phases:
213 * setup, execute, teardown.
214 *
215 * A test may be setup and executed at boottime,
216 * it may be setup at boottime and executed at runtime,
217 * or it may be setup and executed at runtime.
218 *
219 * After executing all tests the system is reset.
220 *
221 * @image_handle: handle of the loaded EFI image
222 * @systab: EFI system table
223 */
224efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
225 struct efi_system_table *systab)
226{
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200227 unsigned int failures = 0;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200228 const u16 *testname = NULL;
229 struct efi_loaded_image *loaded_image;
230 efi_status_t ret;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200231
232 systable = systab;
233 boottime = systable->boottime;
234 runtime = systable->runtime;
235 handle = image_handle;
236 con_out = systable->con_out;
237 con_in = systable->con_in;
238
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200239 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
240 (void **)&loaded_image);
241 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtbf7ea1d2017-11-26 14:05:19 +0100242 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200243 return ret;
244 }
245
246 if (loaded_image->load_options)
247 testname = (u16 *)loaded_image->load_options;
248
249 if (testname) {
250 if (!efi_st_strcmp_16_8(testname, "list") ||
251 !find_test(testname)) {
252 list_all_tests();
253 /*
254 * TODO:
255 * Once the Exit boottime service is correctly
256 * implemented we should call
257 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
258 * here, cf.
259 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
260 */
261 return EFI_SUCCESS;
262 }
263 }
264
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200265 efi_st_printf("\nTesting EFI API implementation\n");
266
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200267 if (testname)
268 efi_st_printf("\nSelected test: '%ps'\n", testname);
269 else
270 efi_st_printf("\nNumber of tests to execute: %u\n",
271 ll_entry_count(struct efi_unit_test,
272 efi_unit_test));
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200273
274 /* Execute boottime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200275 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200276 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
277 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200278
279 /* Execute mixed tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200280 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200281 EFI_ST_SETUP, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200282
283 efi_st_exit_boot_services();
284
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200285 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200286 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200287
288 /* Execute runtime tests */
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200289 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
Heinrich Schuchardt2eb15582017-10-18 18:13:12 +0200290 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
291 &failures);
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200292
293 /* Give feedback */
294 efi_st_printf("\nSummary: %u failures\n\n", failures);
295
296 /* Reset system */
297 efi_st_printf("Preparing for reset. Press any key.\n");
298 efi_st_get_key();
299 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
300 sizeof(reset_message), reset_message);
Heinrich Schuchardtcbfdf812017-10-04 12:37:02 +0200301 efi_st_printf("\n");
302 efi_st_error("Reset failed.\n");
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200303
304 return EFI_UNSUPPORTED;
305}