]> git.sur5r.net Git - u-boot/blob - lib/efi_selftest/efi_selftest.c
ff00254c21936f113975c052b670bb694cd69108
[u-boot] / lib / efi_selftest / efi_selftest.c
1 /*
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
12 static const struct efi_system_table *systable;
13 static const struct efi_boot_services *boottime;
14 static const struct efi_runtime_services *runtime;
15 static efi_handle_t handle;
16 static u16 reset_message[] = L"Selftest completed";
17
18 /*
19  * Exit the boot services.
20  *
21  * The size of the memory map is determined.
22  * Pool memory is allocated to copy the memory map.
23  * The memory amp is copied and the map key is obtained.
24  * The map key is used to exit the boot services.
25  */
26 void efi_st_exit_boot_services(void)
27 {
28         unsigned long  map_size = 0;
29         unsigned long  map_key;
30         unsigned long desc_size;
31         u32 desc_version;
32         efi_status_t ret;
33         struct efi_mem_desc *memory_map;
34
35         ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
36                                        &desc_version);
37         if (ret != EFI_BUFFER_TOO_SMALL) {
38                 efi_st_error(
39                         "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
40                 return;
41         }
42         /* Allocate extra space for newly allocated memory */
43         map_size += sizeof(struct efi_mem_desc);
44         ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
45                                       (void **)&memory_map);
46         if (ret != EFI_SUCCESS) {
47                 efi_st_error("AllocatePool did not return EFI_SUCCESS\n");
48                 return;
49         }
50         ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
51                                        &desc_size, &desc_version);
52         if (ret != EFI_SUCCESS) {
53                 efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n");
54                 return;
55         }
56         ret = boottime->exit_boot_services(handle, map_key);
57         if (ret != EFI_SUCCESS) {
58                 efi_st_error("ExitBootServices did not return EFI_SUCCESS\n");
59                 return;
60         }
61         efi_st_printf("\nBoot services terminated\n");
62 }
63
64 /*
65  * Set up a test.
66  *
67  * @test        the test to be executed
68  * @failures    counter that will be incremented if a failure occurs
69  */
70 static int setup(struct efi_unit_test *test, unsigned int *failures)
71 {
72         int ret;
73
74         if (!test->setup)
75                 return 0;
76         efi_st_printf("\nSetting up '%s'\n", test->name);
77         ret = test->setup(handle, systable);
78         if (ret) {
79                 efi_st_error("Setting up '%s' failed\n", test->name);
80                 ++*failures;
81         } else {
82                 efi_st_printf("Setting up '%s' succeeded\n", test->name);
83         }
84         return ret;
85 }
86
87 /*
88  * Execute a test.
89  *
90  * @test        the test to be executed
91  * @failures    counter that will be incremented if a failure occurs
92  */
93 static int execute(struct efi_unit_test *test, unsigned int *failures)
94 {
95         int ret;
96
97         if (!test->execute)
98                 return 0;
99         efi_st_printf("\nExecuting '%s'\n", test->name);
100         ret = test->execute();
101         if (ret) {
102                 efi_st_error("Executing '%s' failed\n", test->name);
103                 ++*failures;
104         } else {
105                 efi_st_printf("Executing '%s' succeeded\n", test->name);
106         }
107         return ret;
108 }
109
110 /*
111  * Tear down a test.
112  *
113  * @test        the test to be torn down
114  * @failures    counter that will be incremented if a failure occurs
115  */
116 static int teardown(struct efi_unit_test *test, unsigned int *failures)
117 {
118         int ret;
119
120         if (!test->teardown)
121                 return 0;
122         efi_st_printf("\nTearing down '%s'\n", test->name);
123         ret = test->teardown();
124         if (ret) {
125                 efi_st_error("Tearing down '%s' failed\n", test->name);
126                 ++*failures;
127         } else {
128                 efi_st_printf("Tearing down '%s' succeeded\n", test->name);
129         }
130         return ret;
131 }
132
133 /*
134  * Execute selftest of the EFI API
135  *
136  * This is the main entry point of the EFI selftest application.
137  *
138  * All tests use a driver model and are run in three phases:
139  * setup, execute, teardown.
140  *
141  * A test may be setup and executed at boottime,
142  * it may be setup at boottime and executed at runtime,
143  * or it may be setup and executed at runtime.
144  *
145  * After executing all tests the system is reset.
146  *
147  * @image_handle:       handle of the loaded EFI image
148  * @systab:             EFI system table
149  */
150 efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
151                                  struct efi_system_table *systab)
152 {
153         struct efi_unit_test *test;
154         unsigned int failures = 0;
155
156         systable = systab;
157         boottime = systable->boottime;
158         runtime = systable->runtime;
159         handle = image_handle;
160         con_out = systable->con_out;
161         con_in = systable->con_in;
162
163         efi_st_printf("\nTesting EFI API implementation\n");
164
165         efi_st_printf("\nNumber of tests to execute: %u\n",
166                       ll_entry_count(struct efi_unit_test, efi_unit_test));
167
168         /* Execute boottime tests */
169         for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
170              test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
171                 if (test->phase == EFI_EXECUTE_BEFORE_BOOTTIME_EXIT) {
172                         setup(test, &failures);
173                         execute(test, &failures);
174                         teardown(test, &failures);
175                 }
176         }
177
178         /* Execute mixed tests */
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 (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT)
182                         setup(test, &failures);
183         }
184
185         efi_st_exit_boot_services();
186
187         for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
188              test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
189                 if (test->phase == EFI_SETUP_BEFORE_BOOTTIME_EXIT) {
190                         execute(test, &failures);
191                         teardown(test, &failures);
192                 }
193         }
194
195         /* Execute runtime tests */
196         for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
197              test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
198                 if (test->phase == EFI_SETUP_AFTER_BOOTTIME_EXIT) {
199                         setup(test, &failures);
200                         execute(test, &failures);
201                         teardown(test, &failures);
202                 }
203         }
204
205         /* Give feedback */
206         efi_st_printf("\nSummary: %u failures\n\n", failures);
207
208         /* Reset system */
209         efi_st_printf("Preparing for reset. Press any key.\n");
210         efi_st_get_key();
211         runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY,
212                               sizeof(reset_message), reset_message);
213         efi_st_printf("\n");
214         efi_st_error("Reset failed.\n");
215
216         return EFI_UNSUPPORTED;
217 }