blob: 73f074d9e15ce835a7c0c2878818ac2801dc8a72 [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 Schuchardtfd4a1c42017-10-18 18:13:10 +020035 unsigned long map_size = 0;
36 unsigned long map_key;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020037 unsigned long desc_size;
38 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 }
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 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 Schuchardt623b3a52017-09-15 10:06:11 +020084 efi_st_printf("\nSetting up '%s'\n", test->name);
85 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 {
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 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 Schuchardt623b3a52017-09-15 10:06:11 +0200108 efi_st_printf("\nExecuting '%s'\n", test->name);
109 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 {
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 Schuchardte67e7242017-10-04 15:31:26 +0200124 * @return EFI_ST_SUCCESS for success
Heinrich Schuchardt623b3a52017-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 Schuchardte67e7242017-10-04 15:31:26 +0200131 return EFI_ST_SUCCESS;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200132 efi_st_printf("\nTearing down '%s'\n", test->name);
133 ret = test->teardown();
Heinrich Schuchardte67e7242017-10-04 15:31:26 +0200134 if (ret != EFI_ST_SUCCESS) {
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200135 efi_st_error("Tearing down '%s' failed\n", test->name);
Heinrich Schuchardt623b3a52017-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 Schuchardt1f66a122017-10-18 18:13:12 +0200144 * Execute test steps of one phase.
145 *
146 * @phase test phase
147 * @steps steps to execute
148 * failures returns EFI_ST_SUCCESS if all test steps succeeded
149 */
150void efi_st_do_tests(unsigned int phase, unsigned int steps,
151 unsigned int *failures)
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 (test->phase != phase)
158 continue;
159 if (steps & EFI_ST_SETUP)
160 setup(test, failures);
161 if (steps & EFI_ST_EXECUTE)
162 execute(test, failures);
163 if (steps & EFI_ST_TEARDOWN)
164 teardown(test, failures);
165 }
166}
167
168/*
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200169 * Execute selftest of the EFI API
170 *
171 * This is the main entry point of the EFI selftest application.
172 *
173 * All tests use a driver model and are run in three phases:
174 * setup, execute, teardown.
175 *
176 * A test may be setup and executed at boottime,
177 * it may be setup at boottime and executed at runtime,
178 * or it may be setup and executed at runtime.
179 *
180 * After executing all tests the system is reset.
181 *
182 * @image_handle: handle of the loaded EFI image
183 * @systab: EFI system table
184 */
185efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
186 struct efi_system_table *systab)
187{
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200188 unsigned int failures = 0;
189
190 systable = systab;
191 boottime = systable->boottime;
192 runtime = systable->runtime;
193 handle = image_handle;
194 con_out = systable->con_out;
195 con_in = systable->con_in;
196
197 efi_st_printf("\nTesting EFI API implementation\n");
198
199 efi_st_printf("\nNumber of tests to execute: %u\n",
200 ll_entry_count(struct efi_unit_test, efi_unit_test));
201
202 /* Execute boottime tests */
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200203 efi_st_do_tests(EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
204 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
205 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200206
207 /* Execute mixed tests */
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200208 efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT,
209 EFI_ST_SETUP, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200210
211 efi_st_exit_boot_services();
212
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200213 efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT,
214 EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200215
216 /* Execute runtime tests */
Heinrich Schuchardt1f66a122017-10-18 18:13:12 +0200217 efi_st_do_tests(EFI_SETUP_AFTER_BOOTTIME_EXIT,
218 EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
219 &failures);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200220
221 /* Give feedback */
222 efi_st_printf("\nSummary: %u failures\n\n", failures);
223
224 /* Reset system */
225 efi_st_printf("Preparing for reset. Press any key.\n");
226 efi_st_get_key();
227 runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
228 sizeof(reset_message), reset_message);
Heinrich Schuchardt037ee6f2017-10-04 12:37:02 +0200229 efi_st_printf("\n");
230 efi_st_error("Reset failed.\n");
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200231
232 return EFI_UNSUPPORTED;
233}