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, UninstallProtocol,
10 * OpenProtocol, CloseProtcol, OpenProtocolInformation
13 #include <efi_selftest.h>
15 #define NUMBER_OF_CHILD_CONTROLLERS 4
17 static struct efi_boot_services *boottime;
18 const efi_guid_t guid_driver_binding_protocol =
19 EFI_DRIVER_BINDING_PROTOCOL_GUID;
20 static efi_guid_t guid_controller =
21 EFI_GUID(0xe6ab1d96, 0x6bff, 0xdb42,
22 0xaa, 0x05, 0xc8, 0x1f, 0x7f, 0x45, 0x26, 0x34);
23 static efi_guid_t guid_child_controller =
24 EFI_GUID(0x1d41f6f5, 0x2c41, 0xddfb,
25 0xe2, 0x9b, 0xb8, 0x0e, 0x2e, 0xe8, 0x3a, 0x85);
26 static efi_handle_t handle_controller;
27 static efi_handle_t handle_child_controller[NUMBER_OF_CHILD_CONTROLLERS];
28 static efi_handle_t handle_driver;
31 * Count child controllers
33 * @handle handle on which child controllers are installed
34 * @protocol protocol for which the child controlles where installed
35 * @count number of child controllers
38 static efi_status_t count_child_controllers(efi_handle_t handle,
43 efi_uintn_t entry_count;
44 struct efi_open_protocol_info_entry *entry_buffer;
47 ret = boottime->open_protocol_information(handle, protocol,
48 &entry_buffer, &entry_count);
49 if (ret != EFI_SUCCESS)
54 if (entry_buffer[--entry_count].attributes &
55 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
58 ret = boottime->free_pool(entry_buffer);
59 if (ret != EFI_SUCCESS)
60 efi_st_error("Cannot free buffer\n");
65 * Check if the driver supports the controller.
67 * @this driver binding protocol
68 * @controller_handle handle of the controller
69 * @remaining_device_path path specifying the child controller
72 static efi_status_t EFIAPI supported(
73 struct efi_driver_binding_protocol *this,
74 efi_handle_t controller_handle,
75 struct efi_device_path *remaining_device_path)
80 ret = boottime->open_protocol(
81 controller_handle, &guid_controller,
82 &interface, handle_driver,
83 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER);
85 case EFI_ACCESS_DENIED:
86 case EFI_ALREADY_STARTED:
91 return EFI_UNSUPPORTED;
93 ret = boottime->close_protocol(
94 controller_handle, &guid_controller,
95 handle_driver, controller_handle);
96 if (ret != EFI_SUCCESS)
97 ret = EFI_UNSUPPORTED;
102 * Create child controllers and attach driver.
104 * @this driver binding protocol
105 * @controller_handle handle of the controller
106 * @remaining_device_path path specifying the child controller
107 * @return status code
109 static efi_status_t EFIAPI start(
110 struct efi_driver_binding_protocol *this,
111 efi_handle_t controller_handle,
112 struct efi_device_path *remaining_device_path)
118 /* Attach driver to controller */
119 ret = boottime->open_protocol(
120 controller_handle, &guid_controller,
121 &interface, handle_driver,
122 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER);
124 case EFI_ACCESS_DENIED:
125 case EFI_ALREADY_STARTED:
130 return EFI_UNSUPPORTED;
133 /* Create child controllers */
134 for (i = 0; i < NUMBER_OF_CHILD_CONTROLLERS; ++i) {
135 ret = boottime->install_protocol_interface(
136 &handle_child_controller[i], &guid_child_controller,
137 EFI_NATIVE_INTERFACE, NULL);
138 if (ret != EFI_SUCCESS) {
139 efi_st_error("InstallProtocolInterface failed\n");
140 return EFI_ST_FAILURE;
142 ret = boottime->open_protocol(
143 controller_handle, &guid_controller,
144 &interface, handle_child_controller[i],
145 handle_child_controller[i],
146 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
147 if (ret != EFI_SUCCESS) {
148 efi_st_error("OpenProtocol failed\n");
149 return EFI_ST_FAILURE;
156 * Remove a single child controller from the parent controller.
158 * @controller_handle parent controller
159 * @child_handle child controller
160 * @return status code
162 static efi_status_t disconnect_child(efi_handle_t controller_handle,
163 efi_handle_t child_handle)
167 ret = boottime->close_protocol(
168 controller_handle, &guid_controller,
169 child_handle, child_handle);
170 if (ret != EFI_SUCCESS) {
171 efi_st_error("Cannot close protocol\n");
174 ret = boottime->uninstall_protocol_interface(
175 child_handle, &guid_child_controller, NULL);
176 if (ret != EFI_SUCCESS) {
177 efi_st_error("Cannot uninstall protocol interface\n");
184 * Remove child controllers and disconnect the controller.
186 * @this driver binding protocol
187 * @controller_handle handle of the controller
188 * @number_of_children number of child controllers to remove
189 * @child_handle_buffer handles of the child controllers to remove
190 * @return status code
192 static efi_status_t EFIAPI stop(
193 struct efi_driver_binding_protocol *this,
194 efi_handle_t controller_handle,
195 size_t number_of_children,
196 efi_handle_t *child_handle_buffer)
200 struct efi_open_protocol_info_entry *entry_buffer;
202 /* Destroy provided child controllers */
203 if (number_of_children) {
206 for (i = 0; i < number_of_children; ++i) {
207 ret = disconnect_child(controller_handle,
208 child_handle_buffer[i]);
209 if (ret != EFI_SUCCESS)
215 /* Destroy all children */
216 ret = boottime->open_protocol_information(
217 controller_handle, &guid_controller,
218 &entry_buffer, &count);
219 if (ret != EFI_SUCCESS) {
220 efi_st_error("OpenProtocolInformation failed\n");
224 if (entry_buffer[--count].attributes &
225 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
226 ret = disconnect_child(
228 entry_buffer[count].agent_handle);
229 if (ret != EFI_SUCCESS)
233 ret = boottime->free_pool(entry_buffer);
234 if (ret != EFI_SUCCESS)
235 efi_st_error("Cannot free buffer\n");
237 /* Detach driver from controller */
238 ret = boottime->close_protocol(
239 controller_handle, &guid_controller,
240 handle_driver, controller_handle);
241 if (ret != EFI_SUCCESS) {
242 efi_st_error("Cannot close protocol\n");
248 /* Driver binding protocol interface */
249 static struct efi_driver_binding_protocol binding_interface = {
261 * @handle handle of the loaded image
262 * @systable system table
264 static int setup(const efi_handle_t img_handle,
265 const struct efi_system_table *systable)
269 boottime = systable->boottime;
271 /* Create controller handle */
272 ret = boottime->install_protocol_interface(
273 &handle_controller, &guid_controller,
274 EFI_NATIVE_INTERFACE, NULL);
275 if (ret != EFI_SUCCESS) {
276 efi_st_error("InstallProtocolInterface failed\n");
277 return EFI_ST_FAILURE;
279 /* Create driver handle */
280 ret = boottime->install_protocol_interface(
281 &handle_driver, &guid_driver_binding_protocol,
282 EFI_NATIVE_INTERFACE, &binding_interface);
283 if (ret != EFI_SUCCESS) {
284 efi_st_error("InstallProtocolInterface failed\n");
285 return EFI_ST_FAILURE;
288 return EFI_ST_SUCCESS;
294 * The number of child controllers is checked after each of the following
297 * Connect a controller to a driver.
298 * Disconnect and destroy a child controller.
299 * Disconnect and destroy the remaining child controllers.
301 * Connect a controller to a driver.
302 * Uninstall the driver protocol from the controller.
304 static int execute(void)
309 /* Connect controller to driver */
310 ret = boottime->connect_controller(handle_controller, NULL, NULL, 1);
311 if (ret != EFI_SUCCESS) {
312 efi_st_error("Failed to connect controller\n");
313 return EFI_ST_FAILURE;
315 /* Check number of child controllers */
316 ret = count_child_controllers(handle_controller, &guid_controller,
318 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS) {
319 efi_st_error("Number of children %u != %u\n",
320 (unsigned int)count, NUMBER_OF_CHILD_CONTROLLERS);
322 /* Destroy second child controller */
323 ret = boottime->disconnect_controller(handle_controller,
325 handle_child_controller[1]);
326 if (ret != EFI_SUCCESS) {
327 efi_st_error("Failed to disconnect child controller\n");
328 return EFI_ST_FAILURE;
330 /* Check number of child controllers */
331 ret = count_child_controllers(handle_controller, &guid_controller,
333 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS - 1) {
334 efi_st_error("Destroying single child controller failed\n");
335 return EFI_ST_FAILURE;
337 /* Destroy remaining child controllers and disconnect controller */
338 ret = boottime->disconnect_controller(handle_controller, NULL, NULL);
339 if (ret != EFI_SUCCESS) {
340 efi_st_error("Failed to disconnect controller\n");
341 return EFI_ST_FAILURE;
343 /* Check number of child controllers */
344 ret = count_child_controllers(handle_controller, &guid_controller,
346 if (ret != EFI_SUCCESS || count) {
347 efi_st_error("Destroying child controllers failed\n");
348 return EFI_ST_FAILURE;
351 /* Connect controller to driver */
352 ret = boottime->connect_controller(handle_controller, NULL, NULL, 1);
353 if (ret != EFI_SUCCESS) {
354 efi_st_error("Failed to connect controller\n");
355 return EFI_ST_FAILURE;
357 /* Check number of child controllers */
358 ret = count_child_controllers(handle_controller, &guid_controller,
360 if (ret != EFI_SUCCESS || count != NUMBER_OF_CHILD_CONTROLLERS) {
361 efi_st_error("Number of children %u != %u\n",
362 (unsigned int)count, NUMBER_OF_CHILD_CONTROLLERS);
364 /* Uninstall controller protocol */
365 ret = boottime->uninstall_protocol_interface(handle_controller,
366 &guid_controller, NULL);
367 if (ret != EFI_SUCCESS) {
368 efi_st_error("Failed to uninstall protocols\n");
369 return EFI_ST_FAILURE;
371 /* Check number of child controllers */
372 ret = count_child_controllers(handle_controller, &guid_controller,
374 if (ret == EFI_SUCCESS)
375 efi_st_error("Uninstall failed\n");
376 return EFI_ST_SUCCESS;
379 EFI_UNIT_TEST(controllers) = {
380 .name = "controllers",
381 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,