blob: fc5ef254a1d14c1e256c2ff304f07c22cdd50614 [file] [log] [blame]
Heinrich Schuchardt623b3a52017-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 Schuchardt1f66a122017-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 Schuchardt623b3a52017-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 Schuchardtf5a2a932017-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 Schuchardt623b3a52017-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 Schuchardt037ee6f2017-10-04 12:37:02 +020045 efi_st_error(
46 "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
Heinrich Schuchardt623b3a52017-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 Schuchardt037ee6f2017-10-04 12:37:02 +020054 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-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 Schuchardt037ee6f2017-10-04 12:37:02 +020060 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020061 return;
62 }
63 ret = boottime->exit_boot_services(handle, map_key);
64 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020065 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020066 return;
67 }
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010068 efi_st_printc(EFI_WHITE, "\nBoot services terminated\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020069}
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 Schuchardte67e7242017-10-04 15:31:26 +020076 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +020083 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010084 efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020085 ret = test->setup(handle, systable);
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020086 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +020087 efi_st_error("Setting up '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020088 ++*failures;
89 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010090 efi_st_printc(EFI_LIGHTGREEN,
91 "Setting up '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020092 }
93 return ret;
94}
95
96/*
97 * Execute a test.
98 *
99 * @test the test to be executed
100 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200101 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200102 */
103static int execute(struct efi_unit_test *test, unsigned int *failures)
104{
105 int ret;
106
107 if (!test->execute)
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200108 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100109 efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200110 ret = test->execute();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200111 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200112 efi_st_error("Executing '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200113 ++*failures;
114 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100115 efi_st_printc(EFI_LIGHTGREEN,
116 "Executing '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200117 }
118 return ret;
119}
120
121/*
122 * Tear down a test.
123 *
124 * @test the test to be torn down
125 * @failures counter that will be incremented if a failure occurs
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200126 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200127 */
128static int teardown(struct efi_unit_test *test, unsigned int *failures)
129{
130 int ret;
131
132 if (!test->teardown)
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200133 return EFI_ST_SUCCESS;
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100134 efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200135 ret = test->teardown();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200136 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200137 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200138 ++*failures;
139 } else {
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100140 efi_st_printc(EFI_LIGHTGREEN,
141 "Tearing down '%s' succeeded\n", test->name);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200142 }
143 return ret;
144}
145
146/*
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200147 * Check that a test exists.
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200148 *
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200149 * @testname: name of the test
150 * @return: test
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200151 */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200152static struct efi_unit_test *find_test(const u16 *testname)
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200153{
154 struct efi_unit_test *test;
155
156 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
157 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200158 if (!efi_st_strcmp_16_8(testname, test->name))
159 return test;
160 }
161 efi_st_printf("\nTest '%ps' not found\n", testname);
162 return NULL;
163}
164
165/*
166 * List all available tests.
167 */
168static void list_all_tests(void)
169{
170 struct efi_unit_test *test;
171
172 /* List all tests */
173 efi_st_printf("\nAvailable tests:\n");
174 for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
175 test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
176 efi_st_printf("'%s'%s\n", test->name,
177 test->on_request ? " - on request" : "");
178 }
179}
180
181/*
182 * Execute test steps of one phase.
183 *
184 * @testname name of a single selected test or NULL
185 * @phase test phase
186 * @steps steps to execute
187 * failures returns EFI_ST_SUCCESS if all test steps succeeded
188 */
189void efi_st_do_tests(const u16 *testname, unsigned int phase,
190 unsigned int steps, unsigned int *failures)
191{
192 struct efi_unit_test *test;
193
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 if (testname ?
197 efi_st_strcmp_16_8(testname, test->name) : test->on_request)
198 continue;
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200199 if (test->phase != phase)
200 continue;
201 if (steps & EFI_ST_SETUP)
202 setup(test, failures);
203 if (steps & EFI_ST_EXECUTE)
204 execute(test, failures);
205 if (steps & EFI_ST_TEARDOWN)
206 teardown(test, failures);
207 }
208}
209
210/*
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200211 * Execute selftest of the EFI API
212 *
213 * This is the main entry point of the EFI selftest application.
214 *
215 * All tests use a driver model and are run in three phases:
216 * setup, execute, teardown.
217 *
218 * A test may be setup and executed at boottime,
219 * it may be setup at boottime and executed at runtime,
220 * or it may be setup and executed at runtime.
221 *
222 * After executing all tests the system is reset.
223 *
224 * @image_handle: handle of the loaded EFI image
225 * @systab: EFI system table
226 */
227efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
228 struct efi_system_table *systab)
229{
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200230 unsigned int failures = 0;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200231 const u16 *testname = NULL;
232 struct efi_loaded_image *loaded_image;
233 efi_status_t ret;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200234
235 systable = systab;
236 boottime = systable->boottime;
237 runtime = systable->runtime;
238 handle = image_handle;
239 con_out = systable->con_out;
240 con_in = systable->con_in;
241
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200242 ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
243 (void **)&loaded_image);
244 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc51e7df2017-11-26 14:05:19 +0100245 efi_st_error("Cannot open loaded image protocol\n");
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200246 return ret;
247 }
248
249 if (loaded_image->load_options)
250 testname = (u16 *)loaded_image->load_options;
251
252 if (testname) {
253 if (!efi_st_strcmp_16_8(testname, "list") ||
254 !find_test(testname)) {
255 list_all_tests();
256 /*
257 * TODO:
258 * Once the Exit boottime service is correctly
259 * implemented we should call
260 * boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
261 * here, cf.
262 * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
263 */
264 return EFI_SUCCESS;
265 }
266 }
267
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100268 efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200269
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200270 if (testname)
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100271 efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200272 else
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100273 efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n",
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200274 ll_entry_count(struct efi_unit_test,
275 efi_unit_test));
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200276
277 /* Execute boottime tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200278 efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200279 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
280 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200281
282 /* Execute mixed tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200283 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200284 EFI_ST_SETUP, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200285
286 efi_st_exit_boot_services();
287
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200288 efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200289 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200290
291 /* Execute runtime tests */
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200292 efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200293 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
294 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200295
296 /* Give feedback */
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100297 efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200298
299 /* Reset system */
300 efi_st_printf("Preparing for reset. Press any key.\n");
301 efi_st_get_key();
302 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
303 sizeof(reset_message), reset_message);
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200304 efi_st_printf("\n");
305 efi_st_error("Reset failed.\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200306
307 return EFI_UNSUPPORTED;
308}