1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_controllers
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * This unit test checks the following protocol services:
8 * ConnectController, DisconnectController,
9 * InstallProtocol, ReinstallProtocol, UninstallProtocol,
10 * OpenProtocol, CloseProtcol, OpenProtocolInformation
13 #include <efi_selftest.h>
15 #define NUMBER_OF_CHILD_CONTROLLERS 4
17 static int interface1 = 1;
18 static int interface2 = 2;
19 static struct efi_boot_services *boottime;
20 const efi_guid_t guid_driver_binding_protocol =
21 EFI_DRIVER_BINDING_PROTOCOL_GUID;
22 static efi_guid_t guid_controller =
23 EFI_GUID(0xe6ab1d96, 0x6bff, 0xdb42,
24 0xaa, 0x05, 0xc8, 0x1f, 0x7f, 0x45, 0x26, 0x34);
25 static efi_guid_t guid_child_controller =
26 EFI_GUID(0x1d41f6f5, 0x2c41, 0xddfb,
27 0xe2, 0x9b, 0xb8, 0x0e, 0x2e, 0xe8, 0x3a, 0x85);
28 static efi_handle_t handle_controller;
29 static efi_handle_t handle_child_controller[NUMBER_OF_CHILD_CONTROLLERS];
30 static efi_handle_t handle_driver;
33 * Count child controllers
35 * @handle handle on which child controllers are installed
36 * @protocol protocol for which the child controlles where installed
37 * @count number of child controllers
40 static efi_status_t count_child_controllers(efi_handle_t handle,
45 efi_uintn_t entry_count;
46 struct efi_open_protocol_info_entry *entry_buffer;
49 ret = boottime->open_protocol_information(handle, protocol,
50 &entry_buffer, &entry_count);
51 if (ret != EFI_SUCCESS)
56 if (entry_buffer[--entry_count].attributes &
57 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
60 ret = boottime->free_pool(entry_buffer);
61 if (ret != EFI_SUCCESS)
62 efi_st_error("Cannot free buffer\n");
67 * Check if the driver supports the controller.
69 * @this driver binding protocol
70 * @controller_handle handle of the controller
71 * @remaining_device_path path specifying the child controller
74 static efi_status_t EFIAPI supported(
75 struct efi_driver_binding_protocol *this,
76 efi_handle_t controller_handle,
77 struct efi_device_path *remaining_device_path)
82 ret = boottime->open_protocol(
83 controller_handle, &guid_controller,
84 &interface, handle_driver,
85 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER);
87 case EFI_ACCESS_DENIED:
88 case EFI_ALREADY_STARTED:
93 return EFI_UNSUPPORTED;
95 ret = boottime->close_protocol(
96 controller_handle, &guid_controller,
97 handle_driver, controller_handle);
98 if (ret != EFI_SUCCESS)
99 ret = EFI_UNSUPPORTED;
104 * Create child controllers and attach driver.
106 * @this driver binding protocol
107 * @controller_handle handle of the controller
108 * @remaining_device_path path specifying the child controller
109 * @return status code
111 static efi_status_t EFIAPI start(
112 struct efi_driver_binding_protocol *this,
113 efi_handle_t controller_handle,
114 struct efi_device_path *remaining_device_path)
120 /* Attach driver to controller */
121 ret = boottime->open_protocol(
122 controller_handle, &guid_controller,
123 &interface, handle_driver,
124 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER);
126 case EFI_ACCESS_DENIED:
127 case EFI_ALREADY_STARTED:
132 return EFI_UNSUPPORTED;
135 /* Create child controllers */
136 for (i = 0; i < NUMBER_OF_CHILD_CONTROLLERS; ++i) {
137 ret = boottime->install_protocol_interface(
138 &handle_child_controller[i], &guid_child_controller,
139 EFI_NATIVE_INTERFACE, NULL);
140 if (ret != EFI_SUCCESS) {
141 efi_st_error("InstallProtocolInterface failed\n");
142 return EFI_ST_FAILURE;
144 ret = boottime->open_protocol(
145 controller_handle, &guid_controller,
146 &interface, handle_child_controller[i],
147 handle_child_controller[i],
148 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
149 if (ret != EFI_SUCCESS) {
150 efi_st_error("OpenProtocol failed\n");
151 return EFI_ST_FAILURE;
158 * Remove a single child controller from the parent controller.
160 * @controller_handle parent controller
161 * @child_handle child controller
162 * @return status code
164 static efi_status_t disconnect_child(efi_handle_t controller_handle,
165 efi_handle_t child_handle)
169 ret = boottime->close_protocol(
170 controller_handle, &guid_controller,
171 child_handle, child_handle);
172 if (ret != EFI_SUCCESS) {
173 efi_st_error("Cannot close protocol\n");
176 ret = boottime->uninstall_protocol_interface(
177 child_handle, &guid_child_controller, NULL);
178 if (ret != EFI_SUCCESS) {
179 efi_st_error("Cannot uninstall protocol interface\n");
186 * Remove child controllers and disconnect the controller.
188 * @this driver binding protocol
189 * @controller_handle handle of the controller
190 * @number_of_children number of child controllers to remove
191 * @child_handle_buffer handles of the child controllers to remove
192 * @return status code
194 static efi_status_t EFIAPI stop(
195 struct efi_driver_binding_protocol *this,
196 efi_handle_t controller_handle,
197 size_t number_of_children,
198 efi_handle_t *child_handle_buffer)
202 struct efi_open_protocol_info_entry *entry_buffer;
204 /* Destroy provided child controllers */
205 if (number_of_children) {
208 for (i = 0; i < number_of_children; ++i) {
209 ret = disconnect_child(controller_handle,
210 child_handle_buffer[i]);
211 if (ret != EFI_SUCCESS)
217 /* Destroy all children */
218 ret = boottime->open_protocol_information(
219 controller_handle, &guid_controller,
220 &entry_buffer, &count);
221 if (ret != EFI_SUCCESS) {
222 efi_st_error("OpenProtocolInformation failed\n");
226 if (entry_buffer[--count].attributes &
227 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
228 ret = disconnect_child(
230 entry_buffer[count].agent_handle);
231 if (ret != EFI_SUCCESS)
235 ret = boottime->free_pool(entry_buffer);
236 if (ret != EFI_SUCCESS)
237 efi_st_error("Cannot free buffer\n");
239 /* Detach driver from controller */
240 ret = boottime->close_protocol(
241 controller_handle, &guid_controller,
242 handle_driver, controller_handle);
243 if (ret != EFI_SUCCESS) {
244 efi_st_error("Cannot close protocol\n");
250 /* Driver binding protocol interface */
251 static struct efi_driver_binding_protocol binding_interface = {
263 * @handle handle of the loaded image
264 * @systable system table
266 static int setup(const efi_handle_t img_handle,
267 const struct efi_system_table *systable)
271 boottime = systable->boottime;
273 /* Create controller handle */
274 ret = boottime->install_protocol_interface(
275 &handle_controller, &guid_controller,
276 EFI_NATIVE_INTERFACE, &interface1);
277 if (ret != EFI_SUCCESS) {
278 efi_st_error("InstallProtocolInterface failed\n");
279 return EFI_ST_FAILURE;
281 /* Create driver handle */
282 ret = boottime->install_protocol_interface(
283 &handle_driver, &guid_driver_binding_protocol,
284 EFI_NATIVE_INTERFACE, &binding_interface);
285 if (ret != EFI_SUCCESS) {
286 efi_st_error("InstallProtocolInterface failed\n");
287 return EFI_ST_FAILURE;
290 return EFI_ST_SUCCESS;
296 * The number of child controllers is checked after each of the following
299 * Connect a controller to a driver.
300 * Disconnect and destroy a child controller.
301 * Disconnect and destroy the remaining child controllers.
303 * Connect a controller to a driver.
304 * Reinstall the driver protocol on the controller.
305 * Uninstall the driver protocol from the controller.
307 static int execute(void)
312 /* Connect controller to driver */
313 ret = boottime->connect_controller(handle_controller, NULL, NULL, 1);
314 if (ret != EFI_SUCCESS) {
315 efi_st_error("Failed to connect controller\n");
316 return EFI_ST_FAILURE;
318 /* Check number of child controllers */
319 ret = count_child_controllers(handle_controller, &guid_controller,
321 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS) {
322 efi_st_error("Number of children %u != %u\n",
323 (unsigned int)count, NUMBER_OF_CHILD_CONTROLLERS);
325 /* Destroy second child controller */
326 ret = boottime->disconnect_controller(handle_controller,
328 handle_child_controller[1]);
329 if (ret != EFI_SUCCESS) {
330 efi_st_error("Failed to disconnect child controller\n");
331 return EFI_ST_FAILURE;
333 /* Check number of child controllers */
334 ret = count_child_controllers(handle_controller, &guid_controller,
336 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS - 1) {
337 efi_st_error("Destroying single child controller failed\n");
338 return EFI_ST_FAILURE;
340 /* Destroy remaining child controllers and disconnect controller */
341 ret = boottime->disconnect_controller(handle_controller, NULL, NULL);
342 if (ret != EFI_SUCCESS) {
343 efi_st_error("Failed to disconnect controller\n");
344 return EFI_ST_FAILURE;
346 /* Check number of child controllers */
347 ret = count_child_controllers(handle_controller, &guid_controller,
349 if (ret != EFI_SUCCESS || count) {
350 efi_st_error("Destroying child controllers failed\n");
351 return EFI_ST_FAILURE;
354 /* Connect controller to driver */
355 ret = boottime->connect_controller(handle_controller, NULL, NULL, 1);
356 if (ret != EFI_SUCCESS) {
357 efi_st_error("Failed to connect controller\n");
358 return EFI_ST_FAILURE;
360 /* Check number of child controllers */
361 ret = count_child_controllers(handle_controller, &guid_controller,
363 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS) {
364 efi_st_error("Number of children %u != %u\n",
365 (unsigned int)count, NUMBER_OF_CHILD_CONTROLLERS);
367 /* Try to uninstall controller protocol using the wrong interface */
368 ret = boottime->uninstall_protocol_interface(handle_controller,
371 if (ret == EFI_SUCCESS) {
373 "Interface not checked when uninstalling protocol\n");
374 return EFI_ST_FAILURE;
376 /* Reinstall controller protocol */
377 ret = boottime->reinstall_protocol_interface(handle_controller,
381 if (ret != EFI_SUCCESS) {
382 efi_st_error("Failed to reinstall protocols\n");
383 return EFI_ST_FAILURE;
385 /* Check number of child controllers */
386 ret = count_child_controllers(handle_controller, &guid_controller,
388 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS) {
389 efi_st_error("Number of children %u != %u\n",
390 (unsigned int)count, NUMBER_OF_CHILD_CONTROLLERS);
392 /* Uninstall controller protocol */
393 ret = boottime->uninstall_protocol_interface(handle_controller,
396 if (ret != EFI_SUCCESS) {
397 efi_st_error("Failed to uninstall protocols\n");
398 return EFI_ST_FAILURE;
400 /* Check number of child controllers */
401 ret = count_child_controllers(handle_controller, &guid_controller,
403 if (ret == EFI_SUCCESS)
404 efi_st_error("Uninstall failed\n");
405 return EFI_ST_SUCCESS;
408 EFI_UNIT_TEST(controllers) = {
409 .name = "controllers",
410 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,