]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
623b3a57 HS |
2 | /* |
3 | * EFI efi_selftest | |
4 | * | |
5 | * Copyright (c) 2017 Heinrich Schuchardt <[email protected]> | |
623b3a57 HS |
6 | */ |
7 | ||
fa63753f | 8 | #include <command.h> |
623b3a57 HS |
9 | #include <efi_selftest.h> |
10 | #include <vsprintf.h> | |
11 | ||
404ea593 | 12 | /* Constants for test step bitmap */ |
1f66a12e HS |
13 | #define EFI_ST_SETUP 1 |
14 | #define EFI_ST_EXECUTE 2 | |
15 | #define EFI_ST_TEARDOWN 4 | |
16 | ||
b33f246c HS |
17 | const struct efi_system_table *st_systable; |
18 | const struct efi_boot_services *st_boottime; | |
623b3a57 HS |
19 | static const struct efi_runtime_services *runtime; |
20 | static efi_handle_t handle; | |
156ccbc3 | 21 | static u16 reset_message[] = u"Selftest completed"; |
3c2c54ca | 22 | static int *setup_status; |
623b3a57 HS |
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. | |
404ea593 | 29 | * The memory map is copied and the map key is obtained. |
623b3a57 HS |
30 | * The map key is used to exit the boot services. |
31 | */ | |
32 | void efi_st_exit_boot_services(void) | |
33 | { | |
f5a2a938 HS |
34 | efi_uintn_t map_size = 0; |
35 | efi_uintn_t map_key; | |
36 | efi_uintn_t desc_size; | |
623b3a57 HS |
37 | u32 desc_version; |
38 | efi_status_t ret; | |
39 | struct efi_mem_desc *memory_map; | |
40 | ||
fccd3d9c HS |
41 | /* Do not detach devices in ExitBootServices. We need the console. */ |
42 | efi_st_keep_devices = true; | |
43 | ||
b33f246c | 44 | ret = st_boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size, |
623b3a57 HS |
45 | &desc_version); |
46 | if (ret != EFI_BUFFER_TOO_SMALL) { | |
037ee6f9 HS |
47 | efi_st_error( |
48 | "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n"); | |
623b3a57 HS |
49 | return; |
50 | } | |
51 | /* Allocate extra space for newly allocated memory */ | |
52 | map_size += sizeof(struct efi_mem_desc); | |
b33f246c | 53 | ret = st_boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size, |
623b3a57 HS |
54 | (void **)&memory_map); |
55 | if (ret != EFI_SUCCESS) { | |
037ee6f9 | 56 | efi_st_error("AllocatePool did not return EFI_SUCCESS\n"); |
623b3a57 HS |
57 | return; |
58 | } | |
b33f246c | 59 | ret = st_boottime->get_memory_map(&map_size, memory_map, &map_key, |
623b3a57 HS |
60 | &desc_size, &desc_version); |
61 | if (ret != EFI_SUCCESS) { | |
037ee6f9 | 62 | efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n"); |
623b3a57 HS |
63 | return; |
64 | } | |
b33f246c | 65 | ret = st_boottime->exit_boot_services(handle, map_key); |
623b3a57 | 66 | if (ret != EFI_SUCCESS) { |
037ee6f9 | 67 | efi_st_error("ExitBootServices did not return EFI_SUCCESS\n"); |
623b3a57 HS |
68 | return; |
69 | } | |
853540c8 | 70 | efi_st_printc(EFI_WHITE, "\nBoot services terminated\n"); |
623b3a57 HS |
71 | } |
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 | |
185f812c | 78 | * Return: EFI_ST_SUCCESS for success |
623b3a57 HS |
79 | */ |
80 | static int setup(struct efi_unit_test *test, unsigned int *failures) | |
81 | { | |
4c174394 HS |
82 | int ret; |
83 | ||
84 | if (!test->setup) | |
e67e7249 | 85 | return EFI_ST_SUCCESS; |
853540c8 | 86 | efi_st_printc(EFI_LIGHTBLUE, "\nSetting up '%s'\n", test->name); |
b33f246c | 87 | ret = test->setup(handle, st_systable); |
4c174394 | 88 | if (ret != EFI_ST_SUCCESS) { |
037ee6f9 | 89 | efi_st_error("Setting up '%s' failed\n", test->name); |
623b3a57 HS |
90 | ++*failures; |
91 | } else { | |
853540c8 HS |
92 | efi_st_printc(EFI_LIGHTGREEN, |
93 | "Setting up '%s' succeeded\n", test->name); | |
623b3a57 | 94 | } |
4c174394 | 95 | return ret; |
623b3a57 HS |
96 | } |
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 | |
185f812c | 103 | * Return: EFI_ST_SUCCESS for success |
623b3a57 HS |
104 | */ |
105 | static int execute(struct efi_unit_test *test, unsigned int *failures) | |
106 | { | |
107 | int ret; | |
108 | ||
109 | if (!test->execute) | |
e67e7249 | 110 | return EFI_ST_SUCCESS; |
853540c8 | 111 | efi_st_printc(EFI_LIGHTBLUE, "\nExecuting '%s'\n", test->name); |
623b3a57 | 112 | ret = test->execute(); |
e67e7249 | 113 | if (ret != EFI_ST_SUCCESS) { |
037ee6f9 | 114 | efi_st_error("Executing '%s' failed\n", test->name); |
623b3a57 HS |
115 | ++*failures; |
116 | } else { | |
853540c8 HS |
117 | efi_st_printc(EFI_LIGHTGREEN, |
118 | "Executing '%s' succeeded\n", test->name); | |
623b3a57 HS |
119 | } |
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 | |
185f812c | 128 | * Return: EFI_ST_SUCCESS for success |
623b3a57 HS |
129 | */ |
130 | static int teardown(struct efi_unit_test *test, unsigned int *failures) | |
131 | { | |
132 | int ret; | |
133 | ||
134 | if (!test->teardown) | |
e67e7249 | 135 | return EFI_ST_SUCCESS; |
853540c8 | 136 | efi_st_printc(EFI_LIGHTBLUE, "\nTearing down '%s'\n", test->name); |
623b3a57 | 137 | ret = test->teardown(); |
e67e7249 | 138 | if (ret != EFI_ST_SUCCESS) { |
037ee6f9 | 139 | efi_st_error("Tearing down '%s' failed\n", test->name); |
623b3a57 HS |
140 | ++*failures; |
141 | } else { | |
853540c8 HS |
142 | efi_st_printc(EFI_LIGHTGREEN, |
143 | "Tearing down '%s' succeeded\n", test->name); | |
623b3a57 HS |
144 | } |
145 | return ret; | |
146 | } | |
147 | ||
eb0d1d83 HS |
148 | /* |
149 | * Check that a test requiring reset exists. | |
150 | * | |
151 | * @testname: name of the test | |
3dd719d4 | 152 | * Return: test, or NULL if not found |
eb0d1d83 HS |
153 | */ |
154 | static 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 || | |
5e21958c | 163 | test->phase == EFI_SETTING_VIRTUAL_ADDRESS_MAP) |
eb0d1d83 HS |
164 | return true; |
165 | } | |
166 | return false; | |
167 | } | |
168 | ||
d78e40d6 HS |
169 | /* |
170 | * Check that a test exists. | |
171 | * | |
172 | * @testname: name of the test | |
3dd719d4 | 173 | * Return: test, or NULL if not found |
d78e40d6 HS |
174 | */ |
175 | static 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 | */ | |
191 | static 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 | ||
1f66a12e HS |
204 | /* |
205 | * Execute test steps of one phase. | |
206 | * | |
d78e40d6 | 207 | * @testname name of a single selected test or NULL |
1f66a12e | 208 | * @phase test phase |
404ea593 | 209 | * @steps steps to execute (mask with bits from EFI_ST_...) |
1f66a12e HS |
210 | * failures returns EFI_ST_SUCCESS if all test steps succeeded |
211 | */ | |
d78e40d6 HS |
212 | void efi_st_do_tests(const u16 *testname, unsigned int phase, |
213 | unsigned int steps, unsigned int *failures) | |
1f66a12e | 214 | { |
4c174394 | 215 | int i = 0; |
1f66a12e HS |
216 | struct efi_unit_test *test; |
217 | ||
218 | for (test = ll_entry_start(struct efi_unit_test, efi_unit_test); | |
4c174394 HS |
219 | test < ll_entry_end(struct efi_unit_test, efi_unit_test); |
220 | ++test, ++i) { | |
d78e40d6 HS |
221 | if (testname ? |
222 | efi_st_strcmp_16_8(testname, test->name) : test->on_request) | |
223 | continue; | |
1f66a12e HS |
224 | if (test->phase != phase) |
225 | continue; | |
226 | if (steps & EFI_ST_SETUP) | |
3c2c54ca HS |
227 | setup_status[i] = setup(test, failures); |
228 | if (steps & EFI_ST_EXECUTE && setup_status[i] == EFI_ST_SUCCESS) | |
1f66a12e HS |
229 | execute(test, failures); |
230 | if (steps & EFI_ST_TEARDOWN) | |
231 | teardown(test, failures); | |
232 | } | |
233 | } | |
234 | ||
623b3a57 HS |
235 | /* |
236 | * 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 | * | |
b33f246c HS |
243 | * A test may be setup and executed at st_boottime, |
244 | * it may be setup at st_boottime and executed at runtime, | |
623b3a57 HS |
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 | */ | |
252 | efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle, | |
253 | struct efi_system_table *systab) | |
254 | { | |
623b3a57 | 255 | unsigned int failures = 0; |
d78e40d6 HS |
256 | const u16 *testname = NULL; |
257 | struct efi_loaded_image *loaded_image; | |
258 | efi_status_t ret; | |
623b3a57 | 259 | |
b33f246c HS |
260 | st_systable = systab; |
261 | st_boottime = st_systable->boottime; | |
262 | runtime = st_systable->runtime; | |
623b3a57 | 263 | handle = image_handle; |
b33f246c HS |
264 | con_out = st_systable->con_out; |
265 | con_in = st_systable->con_in; | |
623b3a57 | 266 | |
b33f246c | 267 | ret = st_boottime->handle_protocol(image_handle, &efi_guid_loaded_image, |
d78e40d6 HS |
268 | (void **)&loaded_image); |
269 | if (ret != EFI_SUCCESS) { | |
c51e7df9 | 270 | efi_st_error("Cannot open loaded image protocol\n"); |
d78e40d6 HS |
271 | 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: | |
b33f246c | 283 | * Once the Exit st_boottime service is correctly |
d78e40d6 | 284 | * implemented we should call |
b33f246c | 285 | * st_boottime->exit(image_handle, EFI_SUCCESS, 0, NULL); |
d78e40d6 HS |
286 | * here, cf. |
287 | * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html | |
288 | */ | |
289 | return EFI_SUCCESS; | |
290 | } | |
291 | } | |
292 | ||
853540c8 | 293 | efi_st_printc(EFI_WHITE, "\nTesting EFI API implementation\n"); |
623b3a57 | 294 | |
d78e40d6 | 295 | if (testname) |
853540c8 | 296 | efi_st_printc(EFI_WHITE, "\nSelected test: '%ps'\n", testname); |
d78e40d6 | 297 | else |
853540c8 | 298 | efi_st_printc(EFI_WHITE, "\nNumber of tests to execute: %u\n", |
d78e40d6 HS |
299 | ll_entry_count(struct efi_unit_test, |
300 | efi_unit_test)); | |
623b3a57 | 301 | |
4c174394 | 302 | /* Allocate buffer for setup results */ |
b33f246c | 303 | ret = st_boottime->allocate_pool(EFI_RUNTIME_SERVICES_DATA, sizeof(int) * |
4c174394 HS |
304 | ll_entry_count(struct efi_unit_test, |
305 | efi_unit_test), | |
3c2c54ca | 306 | (void **)&setup_status); |
4c174394 HS |
307 | if (ret != EFI_SUCCESS) { |
308 | efi_st_error("Allocate pool failed\n"); | |
309 | return ret; | |
310 | } | |
311 | ||
b33f246c | 312 | /* Execute st_boottime tests */ |
d78e40d6 | 313 | efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
1f66a12e HS |
314 | EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, |
315 | &failures); | |
623b3a57 | 316 | |
eb0d1d83 HS |
317 | 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 | ||
623b3a57 | 327 | /* Execute mixed tests */ |
d78e40d6 | 328 | efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
1f66a12e | 329 | EFI_ST_SETUP, &failures); |
5e21958c HS |
330 | efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP, |
331 | EFI_ST_SETUP, &failures); | |
623b3a57 HS |
332 | |
333 | efi_st_exit_boot_services(); | |
334 | ||
d78e40d6 | 335 | efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
1f66a12e | 336 | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures); |
5e21958c HS |
337 | /* Execute test setting the virtual address map */ |
338 | efi_st_do_tests(testname, EFI_SETTING_VIRTUAL_ADDRESS_MAP, | |
339 | EFI_ST_EXECUTE | EFI_ST_TEARDOWN, | |
1f66a12e | 340 | &failures); |
623b3a57 HS |
341 | |
342 | /* Give feedback */ | |
853540c8 | 343 | efi_st_printc(EFI_WHITE, "\nSummary: %u failures\n\n", failures); |
623b3a57 HS |
344 | |
345 | /* Reset system */ | |
404ea593 | 346 | efi_st_printf("Preparing for reset. Press any key...\n"); |
623b3a57 | 347 | efi_st_get_key(); |
fa63753f | 348 | |
5bf12a78 | 349 | if (IS_ENABLED(CONFIG_EFI_HAVE_RUNTIME_RESET)) { |
fa63753f HS |
350 | runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY, |
351 | sizeof(reset_message), reset_message); | |
5bf12a78 HS |
352 | } else { |
353 | efi_restore_gd(); | |
fa63753f | 354 | do_reset(NULL, 0, 0, NULL); |
5bf12a78 | 355 | } |
fa63753f | 356 | |
037ee6f9 | 357 | efi_st_printf("\n"); |
404ea593 | 358 | efi_st_error("Reset failed\n"); |
623b3a57 HS |
359 | |
360 | return EFI_UNSUPPORTED; | |
361 | } |