1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
181
182
183
184
185
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
// SPDX-License-Identifier: GPL-2.0+
/*
* efi_selftest_manageprotocols
*
* Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
*
* This unit test checks the following protocol services:
* InstallProtocolInterface, UninstallProtocolInterface,
* InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces,
* HandleProtocol, ProtocolsPerHandle,
* LocateHandle, LocateHandleBuffer.
*/
#include <efi_selftest.h>
/*
* The test currently does not actually call the interface function.
* So this is just a dummy structure.
*/
struct interface {
void (EFIAPI * inc)(void);
};
static struct efi_boot_services *boottime;
static efi_guid_t guid1 =
EFI_GUID(0x2e7ca819, 0x21d3, 0x0a3a,
0xf7, 0x91, 0x82, 0x1f, 0x7a, 0x83, 0x67, 0xaf);
static efi_guid_t guid2 =
EFI_GUID(0xf909f2bb, 0x90a8, 0x0d77,
0x94, 0x0c, 0x3e, 0xa8, 0xea, 0x38, 0xd6, 0x6f);
static efi_guid_t guid3 =
EFI_GUID(0x06d641a3, 0xf4e7, 0xe0c9,
0xe7, 0x8d, 0x41, 0x2d, 0x72, 0xa6, 0xb1, 0x24);
static efi_handle_t handle1;
static efi_handle_t handle2;
static struct interface interface1;
static struct interface interface2;
static struct interface interface3;
static struct interface interface4;
/*
* Find a handle in an array.
*
* @handle: handle to find
* @count: number of entries in the array
* @buffer: array to search
*/
efi_status_t find_in_buffer(efi_handle_t handle, size_t count,
efi_handle_t *buffer)
{
size_t i;
for (i = 0; i < count; ++i) {
if (buffer[i] == handle)
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
/*
* Setup unit test.
*
* Create two handles and install two out of three protocol interfaces on each
* of them:
*
* handle1
* guid1 interface1
* guid3 interface3
* handle2
* guid1 interface4
* guid2 interface2
*
* @handle: handle of the loaded image
* @systable: system table
*/
static int setup(const efi_handle_t img_handle,
const struct efi_system_table *systable)
{
efi_status_t ret;
efi_handle_t handle;
boottime = systable->boottime;
ret = boottime->install_protocol_interface(&handle1, &guid3,
EFI_NATIVE_INTERFACE,
&interface3);
if (ret != EFI_SUCCESS) {
efi_st_error("InstallProtocolInterface failed\n");
return EFI_ST_FAILURE;
}
if (!handle1) {
efi_st_error("InstallProtocolInterface failed to create handle\n");
return EFI_ST_FAILURE;
}
handle = handle1;
ret = boottime->install_protocol_interface(&handle1, &guid1,
EFI_NATIVE_INTERFACE,
&interface1);
if (ret != EFI_SUCCESS) {
efi_st_error("InstallProtocolInterface failed\n");
return EFI_ST_FAILURE;
}
if (handle != handle1) {
efi_st_error("InstallProtocolInterface failed to use handle\n");
return EFI_ST_FAILURE;
}
ret = boottime->install_multiple_protocol_interfaces(&handle2,
&guid1, &interface4, &guid2, &interface2, NULL);
if (ret != EFI_SUCCESS) {
efi_st_error("InstallMultipleProtocolInterfaces failed\n");
return EFI_ST_FAILURE;
}
if (!handle2 || handle1 == handle2) {
efi_st_error("InstallMultipleProtocolInterfaces failed to create handle\n");
return EFI_ST_FAILURE;
}
return EFI_ST_SUCCESS;
}
/*
* Tear down unit test.
*
*/
static int teardown(void)
{
return EFI_ST_SUCCESS;
}
/*
* Execute unit test.
*
*/
static int execute(void)
{
struct interface *interface;
efi_status_t ret;
efi_handle_t *buffer;
size_t buffer_size;
efi_uintn_t count = 0;
efi_guid_t **prot_buffer;
efi_uintn_t prot_count;
/*
* Test HandleProtocol
*/
ret = boottime->handle_protocol(handle1, &guid3, (void **)&interface);
if (ret != EFI_SUCCESS) {
efi_st_error("HandleProtocol failed to retrieve interface\n");
return EFI_ST_FAILURE;
}
if (interface != &interface3) {
efi_st_error("HandleProtocol returned wrong interface\n");
return EFI_ST_FAILURE;
}
ret = boottime->handle_protocol(handle1, &guid2, (void **)&interface);
if (ret == EFI_SUCCESS) {
efi_st_error("HandleProtocol returned not installed interface\n");
return EFI_ST_FAILURE;
}
/*
* Test LocateHandleBuffer with AllHandles
*/
ret = boottime->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
&count, &buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer with AllHandles failed\n");
return EFI_ST_FAILURE;
}
buffer_size = count;
ret = find_in_buffer(handle1, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed to locate new handle\n");
return EFI_ST_FAILURE;
}
ret = find_in_buffer(handle2, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed to locate new handle\n");
return EFI_ST_FAILURE;
}
/* Release buffer */
ret = boottime->free_pool(buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("FreePool failed\n");
return EFI_ST_FAILURE;
}
/*
* Test error handling in UninstallMultipleProtocols
*
* These are the installed protocol interfaces on handle 2:
*
* guid1 interface4
* guid2 interface2
*
* Try to uninstall more protocols than there are installed. This
* should return an error EFI_INVALID_PARAMETER. All deleted protocols
* should be reinstalled.
*/
ret = boottime->uninstall_multiple_protocol_interfaces(
handle2,
&guid1, &interface4,
&guid2, &interface2,
&guid3, &interface3,
NULL);
if (ret != EFI_INVALID_PARAMETER) {
printf("%lx", ret);
efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
return EFI_ST_FAILURE;
}
/*
* Test LocateHandleBuffer with ByProtocol
*
* These are the handles with a guid1 protocol interface installed:
*
* handle1, handle2
*/
count = buffer_size;
ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
&count, &buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed to locate new handles\n");
return EFI_ST_FAILURE;
}
if (count != 2) {
efi_st_error("UninstallMultipleProtocolInterfaces deleted handle\n");
return EFI_ST_FAILURE;
}
ret = find_in_buffer(handle1, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed to locate new handle\n");
return EFI_ST_FAILURE;
}
ret = find_in_buffer(handle2, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed to locate new handle\n");
return EFI_ST_FAILURE;
}
/* Clear the buffer, we are reusing it it the next step. */
boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
/*
* Test LocateHandle with ByProtocol
*/
count = buffer_size * sizeof(efi_handle_t);
ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
&count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandle with ByProtocol failed\n");
return EFI_ST_FAILURE;
}
if (count / sizeof(efi_handle_t) != 2) {
efi_st_error("LocateHandle failed to locate new handles\n");
return EFI_ST_FAILURE;
}
buffer_size = count;
ret = find_in_buffer(handle1, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandle failed to locate new handles\n");
return EFI_ST_FAILURE;
}
ret = find_in_buffer(handle2, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandle failed to locate new handles\n");
return EFI_ST_FAILURE;
}
/* Release buffer */
ret = boottime->free_pool(buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("FreePool failed\n");
return EFI_ST_FAILURE;
}
/*
* Test LocateProtocol
*/
ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateProtocol failed\n");
return EFI_ST_FAILURE;
}
if (interface != &interface1 && interface != &interface4) {
efi_st_error("LocateProtocol failed to locate protocol\n");
return EFI_ST_FAILURE;
}
/*
* Test UninstallMultipleProtocols
*/
ret = boottime->uninstall_multiple_protocol_interfaces(
handle2,
&guid1, &interface4,
&guid2, &interface2,
NULL);
if (ret != EFI_SUCCESS) {
efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
return EFI_ST_FAILURE;
}
/*
* Check that the protocols are really uninstalled.
*/
count = buffer_size;
ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
&count, &buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("LocateHandleBuffer failed\n");
return EFI_ST_FAILURE;
}
if (count != 1) {
efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
return EFI_ST_FAILURE;
}
ret = find_in_buffer(handle1, count, buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("Failed to locate new handle\n");
return EFI_ST_FAILURE;
}
boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
/*
* Test ProtocolsPerHandle
*/
ret = boottime->protocols_per_handle(handle1,
&prot_buffer, &prot_count);
if (ret != EFI_SUCCESS) {
efi_st_error("Failed to get protocols per handle\n");
return EFI_ST_FAILURE;
}
if (prot_count != 2) {
efi_st_error("Failed to get protocols per handle\n");
return EFI_ST_FAILURE;
}
if (memcmp(prot_buffer[0], &guid1, 16) &&
memcmp(prot_buffer[1], &guid1, 16)) {
efi_st_error("Failed to get protocols per handle\n");
return EFI_ST_FAILURE;
}
if (memcmp(prot_buffer[0], &guid3, 16) &&
memcmp(prot_buffer[1], &guid3, 16)) {
efi_st_error("Failed to get protocols per handle\n");
return EFI_ST_FAILURE;
}
/* Release buffer */
ret = boottime->free_pool(prot_buffer);
if (ret != EFI_SUCCESS) {
efi_st_error("FreePool failed\n");
return EFI_ST_FAILURE;
}
/*
* Uninstall remaining protocols
*/
ret = boottime->uninstall_protocol_interface(handle1, &guid1,
&interface1);
if (ret != EFI_SUCCESS) {
efi_st_error("UninstallProtocolInterface failed\n");
return EFI_ST_FAILURE;
}
ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
if (ret == EFI_SUCCESS) {
efi_st_error("UninstallProtocolInterface failed\n");
return EFI_ST_FAILURE;
}
ret = boottime->uninstall_protocol_interface(handle1, &guid3,
&interface3);
if (ret != EFI_SUCCESS) {
efi_st_error("UninstallProtocolInterface failed\n");
return EFI_ST_FAILURE;
}
return EFI_ST_SUCCESS;
}
EFI_UNIT_TEST(protserv) = {
.name = "manage protocols",
.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
.setup = setup,
.execute = execute,
.teardown = teardown,
};
|