2 * efi_selftest_manageprotocols
4 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 * SPDX-License-Identifier: GPL-2.0+
8 * This unit test checks the following protocol services:
9 * InstallProtocolInterface, UninstallProtocolInterface,
10 * InstallMultipleProtocolsInterfaces, UninstallMultipleProtocolsInterfaces,
11 * HandleProtocol, ProtocolsPerHandle,
12 * LocateHandle, LocateHandleBuffer.
15 #include <efi_selftest.h>
18 * The test currently does not actually call the interface function.
19 * So this is just a dummy structure.
22 void (EFIAPI * inc)(void);
25 static struct efi_boot_services *boottime;
26 static efi_guid_t guid1 =
27 EFI_GUID(0x2e7ca819, 0x21d3, 0x0a3a,
28 0xf7, 0x91, 0x82, 0x1f, 0x7a, 0x83, 0x67, 0xaf);
29 static efi_guid_t guid2 =
30 EFI_GUID(0xf909f2bb, 0x90a8, 0x0d77,
31 0x94, 0x0c, 0x3e, 0xa8, 0xea, 0x38, 0xd6, 0x6f);
32 static efi_guid_t guid3 =
33 EFI_GUID(0x06d641a3, 0xf4e7, 0xe0c9,
34 0xe7, 0x8d, 0x41, 0x2d, 0x72, 0xa6, 0xb1, 0x24);
35 static efi_handle_t handle1;
36 static efi_handle_t handle2;
37 static struct interface interface1;
38 static struct interface interface2;
39 static struct interface interface3;
40 static struct interface interface4;
43 * Find a handle in an array.
45 * @handle: handle to find
46 * @count: number of entries in the array
47 * @buffer: array to search
49 efi_status_t find_in_buffer(efi_handle_t handle, size_t count,
54 for (i = 0; i < count; ++i) {
55 if (buffer[i] == handle)
64 * Create two handles and install two out of three protocol interfaces on each
74 * @handle: handle of the loaded image
75 * @systable: system table
77 static int setup(const efi_handle_t img_handle,
78 const struct efi_system_table *systable)
83 boottime = systable->boottime;
85 ret = boottime->install_protocol_interface(&handle1, &guid3,
88 if (ret != EFI_SUCCESS) {
89 efi_st_error("InstallProtocolInterface failed\n");
90 return EFI_ST_FAILURE;
93 efi_st_error("InstallProtocolInterface failed to create handle\n");
94 return EFI_ST_FAILURE;
97 ret = boottime->install_protocol_interface(&handle1, &guid1,
100 if (ret != EFI_SUCCESS) {
101 efi_st_error("InstallProtocolInterface failed\n");
102 return EFI_ST_FAILURE;
104 if (handle != handle1) {
105 efi_st_error("InstallProtocolInterface failed to use handle\n");
106 return EFI_ST_FAILURE;
108 ret = boottime->install_multiple_protocol_interfaces(&handle2,
109 &guid1, &interface4, &guid2, &interface2, NULL);
110 if (ret != EFI_SUCCESS) {
111 efi_st_error("InstallMultipleProtocolInterfaces failed\n");
112 return EFI_ST_FAILURE;
114 if (!handle2 || handle1 == handle2) {
115 efi_st_error("InstallMultipleProtocolInterfaces failed to create handle\n");
116 return EFI_ST_FAILURE;
119 return EFI_ST_SUCCESS;
123 * Tear down unit test.
126 static int teardown(void)
128 return EFI_ST_SUCCESS;
135 static int execute(void)
137 struct interface *interface;
139 efi_handle_t *buffer;
141 efi_uintn_t count = 0;
142 efi_guid_t **prot_buffer;
143 efi_uintn_t prot_count;
146 * Test HandleProtocol
148 ret = boottime->handle_protocol(handle1, &guid3, (void **)&interface);
149 if (ret != EFI_SUCCESS) {
150 efi_st_error("HandleProtocol failed to retrieve interface\n");
151 return EFI_ST_FAILURE;
153 if (interface != &interface3) {
154 efi_st_error("HandleProtocol returned wrong interface\n");
155 return EFI_ST_FAILURE;
157 ret = boottime->handle_protocol(handle1, &guid2, (void **)&interface);
158 if (ret == EFI_SUCCESS) {
159 efi_st_error("HandleProtocol returned not installed interface\n");
160 return EFI_ST_FAILURE;
164 * Test LocateHandleBuffer with AllHandles
166 ret = boottime->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
168 if (ret != EFI_SUCCESS) {
169 efi_st_error("LocateHandleBuffer with AllHandles failed\n");
170 return EFI_ST_FAILURE;
173 ret = find_in_buffer(handle1, count, buffer);
174 if (ret != EFI_SUCCESS) {
175 efi_st_error("LocateHandleBuffer failed to locate new handle\n");
176 return EFI_ST_FAILURE;
178 ret = find_in_buffer(handle2, count, buffer);
179 if (ret != EFI_SUCCESS) {
180 efi_st_error("LocateHandleBuffer failed to locate new handle\n");
181 return EFI_ST_FAILURE;
183 boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
186 * Test error handling in UninstallMultipleProtocols
188 * Try to uninstall more protocols than there are installed.
190 ret = boottime->uninstall_multiple_protocol_interfaces(
196 if (ret == EFI_SUCCESS) {
197 efi_st_error("UninstallMultipleProtocolInterfaces did not catch error\n");
198 return EFI_ST_FAILURE;
202 * Test LocateHandleBuffer with ByProtocol
205 ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
207 if (ret != EFI_SUCCESS) {
208 efi_st_error("LocateHandleBuffer failed to locate new handles\n");
209 return EFI_ST_FAILURE;
212 efi_st_error("LocateHandleBuffer failed to locate new handles\n");
213 return EFI_ST_FAILURE;
215 ret = find_in_buffer(handle1, count, buffer);
216 if (ret != EFI_SUCCESS) {
217 efi_st_error("LocateHandleBuffer failed to locate new handle\n");
218 return EFI_ST_FAILURE;
220 ret = find_in_buffer(handle2, count, buffer);
221 if (ret != EFI_SUCCESS) {
222 efi_st_error("LocateHandleBuffer failed to locate new handle\n");
223 return EFI_ST_FAILURE;
225 boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
228 * Test LocateHandle with ByProtocol
230 count = buffer_size * sizeof(efi_handle_t);
231 ret = boottime->locate_handle(BY_PROTOCOL, &guid1, NULL,
233 if (ret != EFI_SUCCESS) {
234 efi_st_error("LocateHandle with ByProtocol failed\n");
235 return EFI_ST_FAILURE;
237 if (count / sizeof(efi_handle_t) != 2) {
238 efi_st_error("LocateHandle failed to locate new handles\n");
239 return EFI_ST_FAILURE;
242 ret = find_in_buffer(handle1, count, buffer);
243 if (ret != EFI_SUCCESS) {
244 efi_st_error("LocateHandle failed to locate new handles\n");
245 return EFI_ST_FAILURE;
247 ret = find_in_buffer(handle2, count, buffer);
248 if (ret != EFI_SUCCESS) {
249 efi_st_error("LocateHandle failed to locate new handles\n");
250 return EFI_ST_FAILURE;
252 boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
255 * Test LocateProtocol
257 ret = boottime->locate_protocol(&guid1, NULL, (void **)&interface);
258 if (ret != EFI_SUCCESS) {
259 efi_st_error("LocateProtocol failed\n");
260 return EFI_ST_FAILURE;
262 if (interface != &interface1 && interface != &interface4) {
263 efi_st_error("LocateProtocol failed to locate protocol\n");
264 return EFI_ST_FAILURE;
268 * Test UninstallMultipleProtocols
270 ret = boottime->uninstall_multiple_protocol_interfaces(
275 if (ret != EFI_SUCCESS) {
276 efi_st_error("UninstallMultipleProtocolInterfaces failed\n");
277 return EFI_ST_FAILURE;
280 * Check that the protocols are really uninstalled.
283 ret = boottime->locate_handle_buffer(BY_PROTOCOL, &guid1, NULL,
285 if (ret != EFI_SUCCESS) {
286 efi_st_error("LocateHandleBuffer failed\n");
287 return EFI_ST_FAILURE;
290 efi_st_error("UninstallMultipleProtocolInterfaces failed to uninstall protocols\n");
291 return EFI_ST_FAILURE;
293 ret = find_in_buffer(handle1, count, buffer);
294 if (ret != EFI_SUCCESS) {
295 efi_st_error("Failed to locate new handle\n");
296 return EFI_ST_FAILURE;
298 boottime->set_mem(buffer, sizeof(efi_handle_t) * buffer_size, 0);
301 * Test ProtocolsPerHandle
303 ret = boottime->protocols_per_handle(handle1,
304 &prot_buffer, &prot_count);
305 if (ret != EFI_SUCCESS) {
306 efi_st_error("Failed to get protocols per handle\n");
307 return EFI_ST_FAILURE;
309 if (prot_count != 2) {
310 efi_st_error("Failed to get protocols per handle\n");
311 return EFI_ST_FAILURE;
313 if (efi_st_memcmp(prot_buffer[0], &guid1, 16) &&
314 efi_st_memcmp(prot_buffer[1], &guid1, 16)) {
315 efi_st_error("Failed to get protocols per handle\n");
316 return EFI_ST_FAILURE;
318 if (efi_st_memcmp(prot_buffer[0], &guid3, 16) &&
319 efi_st_memcmp(prot_buffer[1], &guid3, 16)) {
320 efi_st_error("Failed to get protocols per handle\n");
321 return EFI_ST_FAILURE;
325 * Uninstall remaining protocols
327 ret = boottime->uninstall_protocol_interface(handle1, &guid1,
329 if (ret != EFI_SUCCESS) {
330 efi_st_error("UninstallProtocolInterface failed\n");
331 return EFI_ST_FAILURE;
333 ret = boottime->handle_protocol(handle1, &guid1, (void **)&interface);
334 if (ret == EFI_SUCCESS) {
335 efi_st_error("UninstallProtocolInterface failed\n");
336 return EFI_ST_FAILURE;
338 ret = boottime->uninstall_protocol_interface(handle1, &guid3,
340 if (ret != EFI_SUCCESS) {
341 efi_st_error("UninstallProtocolInterface failed\n");
342 return EFI_ST_FAILURE;
345 return EFI_ST_SUCCESS;
348 EFI_UNIT_TEST(protserv) = {
349 .name = "manage protocols",
350 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
353 .teardown = teardown,