Blame view

lib/efi_selftest/efi_selftest_config_table.c 6.64 KB
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * efi_selftest_config_tables
   *
   * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
   *
   * This test checks the following service:
   * InstallConfigurationTable.
   */
  
  #include <efi_selftest.h>
  
  static const struct efi_system_table *sys_table;
  static struct efi_boot_services *boottime;
  
  static efi_guid_t table_guid =
  	EFI_GUID(0xff1c3f9e, 0x795b, 0x1529, 0xf1, 0x55,
  		 0x17, 0x2e, 0x51, 0x6b, 0x49, 0x75);
  
  /*
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
21
   * Notification function, increments the notification count if parameter
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
   * context is provided.
   *
   * @event	notified event
   * @context	pointer to the notification count
   */
  static void EFIAPI notify(struct efi_event *event, void *context)
  {
  	unsigned int *count = context;
  
  	if (count)
  		++*count;
  }
  
  /*
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
36
   * Check CRC32 of a table.
74fc04457   Heinrich Schuchardt   efi_selftest: che...
37
38
39
40
41
   */
  static int check_table(const void *table)
  {
  	efi_status_t ret;
  	u32 crc32, res;
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
42
  	/* Casting from constant to not constant */
74fc04457   Heinrich Schuchardt   efi_selftest: che...
43
44
45
46
  	struct efi_table_hdr *hdr = (struct efi_table_hdr *)table;
  
  	crc32 = hdr->crc32;
  	/*
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
47
  	 * Setting the CRC32 of the 'const' table to zero is easier than
74fc04457   Heinrich Schuchardt   efi_selftest: che...
48
49
50
51
  	 * copying
  	 */
  	hdr->crc32 = 0;
  	ret = boottime->calculate_crc32(table, hdr->headersize, &res);
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
52
  	/* Reset table CRC32 so it stays constant */
74fc04457   Heinrich Schuchardt   efi_selftest: che...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  	hdr->crc32 = crc32;
  	if (ret != EFI_ST_SUCCESS) {
  		efi_st_error("CalculateCrc32 failed
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (res != crc32) {
  		efi_st_error("Incorrect CRC32
  ");
  		return EFI_ST_FAILURE;
  	}
  	return EFI_ST_SUCCESS;
  }
  
  /*
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
   * Setup unit test.
   *
   * @handle:	handle of the loaded image
   * @systable:	system table
   * @return:	EFI_ST_SUCCESS for success
   */
  static int setup(const efi_handle_t handle,
  		 const struct efi_system_table *systable)
  {
  	sys_table = systable;
  	boottime = systable->boottime;
  
  	return EFI_ST_SUCCESS;
  }
  
  /*
   * Execute unit test.
   *
   * A table is installed, updated, removed. The table entry and the
   * triggering of events is checked.
   *
   * @return:	EFI_ST_SUCCESS for success
   */
  static int execute(void)
  {
  	efi_status_t ret;
  	unsigned int counter = 0;
  	struct efi_event *event;
  	void *table;
  	const unsigned int tables[2];
  	efi_uintn_t i;
  	efi_uintn_t tabcnt;
  	efi_uintn_t table_count = sys_table->nr_tables;
  
  	ret = boottime->create_event_ex(0, TPL_NOTIFY,
  					notify, (void *)&counter,
  					&table_guid, &event);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Failed to create event
  ");
  		return EFI_ST_FAILURE;
  	}
  
  	/* Try to delete non-existent table */
  	ret = boottime->install_configuration_table(&table_guid, NULL);
  	if (ret != EFI_NOT_FOUND) {
  		efi_st_error("Failed to detect missing table
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (counter) {
  		efi_st_error("Notification function was called.
  ");
  		return EFI_ST_FAILURE;
  	}
  	/* Check if the event was signaled  */
  	ret = boottime->check_event(event);
  	if (ret == EFI_SUCCESS) {
  		efi_st_error("Event was signaled on EFI_NOT_FOUND
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (counter != 1) {
  		efi_st_error("Notification function was not called.
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (table_count != sys_table->nr_tables) {
  		efi_st_error("Incorrect table count %u, expected %u
  ",
  			     (unsigned int)sys_table->nr_tables,
  			     (unsigned int)table_count);
  		return EFI_ST_FAILURE;
  	}
  
  	/* Install table */
  	ret = boottime->install_configuration_table(&table_guid,
  						    (void *)&tables[0]);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Failed to install table
  ");
  		return EFI_ST_FAILURE;
  	}
  	/* Check signaled state */
  	ret = boottime->check_event(event);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Event was not signaled on insert
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (++table_count != sys_table->nr_tables) {
  		efi_st_error("Incorrect table count %u, expected %u
  ",
  			     (unsigned int)sys_table->nr_tables,
  			     (unsigned int)table_count);
  		return EFI_ST_FAILURE;
  	}
  	table = NULL;
  	for (i = 0; i < sys_table->nr_tables; ++i) {
  		if (!efi_st_memcmp(&sys_table->tables[i].guid, &table_guid,
  				   sizeof(efi_guid_t)))
  			table = sys_table->tables[i].table;
  	}
  	if (!table) {
  		efi_st_error("Installed table not found
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (table != &tables[0]) {
  		efi_st_error("Incorrect table address
  ");
  		return EFI_ST_FAILURE;
  	}
74fc04457   Heinrich Schuchardt   efi_selftest: che...
181
182
183
184
185
  	if (check_table(sys_table) != EFI_ST_SUCCESS) {
  		efi_st_error("Checking system table
  ");
  		return EFI_ST_FAILURE;
  	}
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  	/* Update table */
  	ret = boottime->install_configuration_table(&table_guid,
  						    (void *)&tables[1]);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Failed to update table
  ");
  		return EFI_ST_FAILURE;
  	}
  	/* Check signaled state */
  	ret = boottime->check_event(event);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Event was not signaled on update
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (table_count != sys_table->nr_tables) {
  		efi_st_error("Incorrect table count %u, expected %u
  ",
  			     (unsigned int)sys_table->nr_tables,
  			     (unsigned int)table_count);
  		return EFI_ST_FAILURE;
  	}
  	table = NULL;
  	tabcnt = 0;
  	for (i = 0; i < sys_table->nr_tables; ++i) {
  		if (!efi_st_memcmp(&sys_table->tables[i].guid, &table_guid,
  				   sizeof(efi_guid_t))) {
  			table = sys_table->tables[i].table;
  			++tabcnt;
  		}
  	}
  	if (!table) {
  		efi_st_error("Installed table not found
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (tabcnt > 1) {
d8b2216c8   Heinrich Schuchardt   efi_selftest: fix...
223
224
  		efi_st_error("Duplicate table GUID
  ");
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
225
226
227
228
229
230
231
  		return EFI_ST_FAILURE;
  	}
  	if (table != &tables[1]) {
  		efi_st_error("Incorrect table address
  ");
  		return EFI_ST_FAILURE;
  	}
74fc04457   Heinrich Schuchardt   efi_selftest: che...
232
233
234
235
236
  	if (check_table(sys_table) != EFI_ST_SUCCESS) {
  		efi_st_error("Checking system table
  ");
  		return EFI_ST_FAILURE;
  	}
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  
  	/* Delete table */
  	ret = boottime->install_configuration_table(&table_guid, NULL);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Failed to delete table
  ");
  		return EFI_ST_FAILURE;
  	}
  	/* Check signaled state */
  	ret = boottime->check_event(event);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Event was not signaled on delete
  ");
  		return EFI_ST_FAILURE;
  	}
  	if (--table_count != sys_table->nr_tables) {
  		efi_st_error("Incorrect table count %u, expected %u
  ",
  			     (unsigned int)sys_table->nr_tables,
  			     (unsigned int)table_count);
  		return EFI_ST_FAILURE;
  	}
  	table = NULL;
  	for (i = 0; i < sys_table->nr_tables; ++i) {
  		if (!efi_st_memcmp(&sys_table->tables[i].guid, &table_guid,
  				   sizeof(efi_guid_t))) {
  			table = sys_table->tables[i].table;
  		}
  	}
  	if (table) {
  		efi_st_error("Wrong table deleted
  ");
  		return EFI_ST_FAILURE;
  	}
  
  	ret = boottime->close_event(event);
  	if (ret != EFI_SUCCESS) {
  		efi_st_error("Failed to close event
  ");
  		return EFI_ST_FAILURE;
  	}
74fc04457   Heinrich Schuchardt   efi_selftest: che...
278
279
280
281
282
  	if (check_table(sys_table) != EFI_ST_SUCCESS) {
  		efi_st_error("Checking system table
  ");
  		return EFI_ST_FAILURE;
  	}
9b30232bf   Heinrich Schuchardt   efi_selftest: tes...
283
284
285
286
287
288
289
290
291
292
  
  	return EFI_ST_SUCCESS;
  }
  
  EFI_UNIT_TEST(configtables) = {
  	.name = "configuration tables",
  	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  	.setup = setup,
  	.execute = execute,
  };