1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_config_tables
7 * This test checks the following service:
8 * InstallConfigurationTable.
11 #include <efi_selftest.h>
12 #include <u-boot/crc.h>
14 static const struct efi_system_table *sys_table;
15 static struct efi_boot_services *boottime;
17 static efi_guid_t table_guid =
18 EFI_GUID(0xff1c3f9e, 0x795b, 0x1529, 0xf1, 0x55,
19 0x17, 0x2e, 0x51, 0x6b, 0x49, 0x75);
22 * Notification function, increments the notification count if parameter
23 * context is provided.
25 * @event notified event
26 * @context pointer to the notification count
28 static void EFIAPI notify(struct efi_event *event, void *context)
30 unsigned int *count = context;
37 * Check CRC32 of a table.
39 static int check_table(const void *table)
43 /* Casting from constant to not constant */
44 struct efi_table_hdr *hdr = (struct efi_table_hdr *)table;
48 * Setting the CRC32 of the 'const' table to zero is easier than
52 ret = boottime->calculate_crc32(table, hdr->headersize, &res);
53 /* Reset table CRC32 so it stays constant */
55 if (ret != EFI_ST_SUCCESS) {
56 efi_st_error("CalculateCrc32 failed\n");
57 return EFI_ST_FAILURE;
60 efi_st_error("Incorrect CRC32\n");
61 return EFI_ST_FAILURE;
63 return EFI_ST_SUCCESS;
69 * @handle: handle of the loaded image
70 * @systable: system table
71 * Return: EFI_ST_SUCCESS for success
73 static int setup(const efi_handle_t handle,
74 const struct efi_system_table *systable)
77 boottime = systable->boottime;
79 return EFI_ST_SUCCESS;
85 * A table is installed, updated, removed. The table entry and the
86 * triggering of events is checked.
88 * Return: EFI_ST_SUCCESS for success
90 static int execute(void)
93 unsigned int counter = 0;
94 struct efi_event *event;
96 const unsigned int tables[2];
99 efi_uintn_t table_count = sys_table->nr_tables;
101 ret = boottime->create_event_ex(0, TPL_NOTIFY,
102 notify, (void *)&counter,
103 &table_guid, &event);
104 if (ret != EFI_SUCCESS) {
105 efi_st_error("Failed to create event\n");
106 return EFI_ST_FAILURE;
109 /* Try to delete non-existent table */
110 ret = boottime->install_configuration_table(&table_guid, NULL);
111 if (ret != EFI_NOT_FOUND) {
112 efi_st_error("Failed to detect missing table\n");
113 return EFI_ST_FAILURE;
116 efi_st_error("Notification function was called.\n");
117 return EFI_ST_FAILURE;
119 /* Check if the event was signaled */
120 ret = boottime->check_event(event);
121 if (ret == EFI_SUCCESS) {
122 efi_st_error("Event was signaled on EFI_NOT_FOUND\n");
123 return EFI_ST_FAILURE;
126 efi_st_error("Notification function was not called.\n");
127 return EFI_ST_FAILURE;
129 if (table_count != sys_table->nr_tables) {
130 efi_st_error("Incorrect table count %u, expected %u\n",
131 (unsigned int)sys_table->nr_tables,
132 (unsigned int)table_count);
133 return EFI_ST_FAILURE;
137 ret = boottime->install_configuration_table(&table_guid,
139 if (ret != EFI_SUCCESS) {
140 efi_st_error("Failed to install table\n");
141 return EFI_ST_FAILURE;
143 /* Check signaled state */
144 ret = boottime->check_event(event);
145 if (ret != EFI_SUCCESS) {
146 efi_st_error("Event was not signaled on insert\n");
147 return EFI_ST_FAILURE;
149 if (++table_count != sys_table->nr_tables) {
150 efi_st_error("Incorrect table count %u, expected %u\n",
151 (unsigned int)sys_table->nr_tables,
152 (unsigned int)table_count);
153 return EFI_ST_FAILURE;
156 for (i = 0; i < sys_table->nr_tables; ++i) {
157 if (!memcmp(&sys_table->tables[i].guid, &table_guid,
159 table = sys_table->tables[i].table;
162 efi_st_error("Installed table not found\n");
163 return EFI_ST_FAILURE;
165 if (table != &tables[0]) {
166 efi_st_error("Incorrect table address\n");
167 return EFI_ST_FAILURE;
169 if (check_table(sys_table) != EFI_ST_SUCCESS) {
170 efi_st_error("Checking system table\n");
171 return EFI_ST_FAILURE;
175 ret = boottime->install_configuration_table(&table_guid,
177 if (ret != EFI_SUCCESS) {
178 efi_st_error("Failed to update table\n");
179 return EFI_ST_FAILURE;
181 /* Check signaled state */
182 ret = boottime->check_event(event);
183 if (ret != EFI_SUCCESS) {
184 efi_st_error("Event was not signaled on update\n");
185 return EFI_ST_FAILURE;
187 if (table_count != sys_table->nr_tables) {
188 efi_st_error("Incorrect table count %u, expected %u\n",
189 (unsigned int)sys_table->nr_tables,
190 (unsigned int)table_count);
191 return EFI_ST_FAILURE;
195 for (i = 0; i < sys_table->nr_tables; ++i) {
196 if (!memcmp(&sys_table->tables[i].guid, &table_guid,
197 sizeof(efi_guid_t))) {
198 table = sys_table->tables[i].table;
203 efi_st_error("Installed table not found\n");
204 return EFI_ST_FAILURE;
207 efi_st_error("Duplicate table GUID\n");
208 return EFI_ST_FAILURE;
210 if (table != &tables[1]) {
211 efi_st_error("Incorrect table address\n");
212 return EFI_ST_FAILURE;
214 if (check_table(sys_table) != EFI_ST_SUCCESS) {
215 efi_st_error("Checking system table\n");
216 return EFI_ST_FAILURE;
220 ret = boottime->install_configuration_table(&table_guid, NULL);
221 if (ret != EFI_SUCCESS) {
222 efi_st_error("Failed to delete table\n");
223 return EFI_ST_FAILURE;
225 /* Check signaled state */
226 ret = boottime->check_event(event);
227 if (ret != EFI_SUCCESS) {
228 efi_st_error("Event was not signaled on delete\n");
229 return EFI_ST_FAILURE;
231 if (--table_count != sys_table->nr_tables) {
232 efi_st_error("Incorrect table count %u, expected %u\n",
233 (unsigned int)sys_table->nr_tables,
234 (unsigned int)table_count);
235 return EFI_ST_FAILURE;
238 for (i = 0; i < sys_table->nr_tables; ++i) {
239 if (!memcmp(&sys_table->tables[i].guid, &table_guid,
240 sizeof(efi_guid_t))) {
241 table = sys_table->tables[i].table;
245 efi_st_error("Wrong table deleted\n");
246 return EFI_ST_FAILURE;
249 ret = boottime->close_event(event);
250 if (ret != EFI_SUCCESS) {
251 efi_st_error("Failed to close event\n");
252 return EFI_ST_FAILURE;
254 if (check_table(sys_table) != EFI_ST_SUCCESS) {
255 efi_st_error("Checking system table\n");
256 return EFI_ST_FAILURE;
259 return EFI_ST_SUCCESS;
262 EFI_UNIT_TEST(configtables) = {
263 .name = "configuration tables",
264 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,