blob: 74dd0032437445d38034ac9eb3089097f0997d88 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt11206f42018-01-21 19:29:30 +01002/*
3 * Uclass for EFI drivers
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
6 *
Heinrich Schuchardt11206f42018-01-21 19:29:30 +01007 * For each EFI driver the uclass
8 * - creates a handle
9 * - installs the driver binding protocol
10 *
11 * The uclass provides the bind, start, and stop entry points for the driver
12 * binding protocol.
13 *
14 * In bind() and stop() it checks if the controller implements the protocol
15 * supported by the EFI driver. In the start() function it calls the bind()
16 * function of the EFI driver. In the stop() function it destroys the child
17 * controllers.
18 */
19
Simon Glassf6977c12020-07-19 10:15:43 -060020#include <common.h>
21#include <dm.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010022#include <efi_driver.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <malloc.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010025
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020026/**
27 * check_node_type() - check node type
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010028 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020029 * We do not support partitions as controller handles.
30 *
31 * @handle: handle to be checked
32 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010033 */
34static efi_status_t check_node_type(efi_handle_t handle)
35{
36 efi_status_t r, ret = EFI_SUCCESS;
37 const struct efi_device_path *dp;
38
39 /* Open the device path protocol */
40 r = EFI_CALL(systab.boottime->open_protocol(
41 handle, &efi_guid_device_path, (void **)&dp,
42 NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL));
43 if (r == EFI_SUCCESS && dp) {
44 /* Get the last node */
45 const struct efi_device_path *node = efi_dp_last_node(dp);
46 /* We do not support partitions as controller */
47 if (!node || node->type == DEVICE_PATH_TYPE_MEDIA_DEVICE)
48 ret = EFI_UNSUPPORTED;
49 }
50 return ret;
51}
52
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020053/**
54 * efi_uc_supported() - check if the driver supports the controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010055 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020056 * @this: driver binding protocol
57 * @controller_handle: handle of the controller
58 * @remaining_device_path: path specifying the child controller
59 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010060 */
61static efi_status_t EFIAPI efi_uc_supported(
62 struct efi_driver_binding_protocol *this,
63 efi_handle_t controller_handle,
64 struct efi_device_path *remaining_device_path)
65{
66 efi_status_t r, ret;
67 void *interface;
68 struct efi_driver_binding_extended_protocol *bp =
69 (struct efi_driver_binding_extended_protocol *)this;
70
71 EFI_ENTRY("%p, %p, %ls", this, controller_handle,
72 efi_dp_str(remaining_device_path));
73
Heinrich Schuchardt4fdaaf92022-09-09 06:57:58 +000074 /*
75 * U-Boot internal devices install protocols interfaces without calling
76 * ConnectController(). Hence we should not bind an extra driver.
77 */
78 if (controller_handle->dev) {
79 ret = EFI_UNSUPPORTED;
80 goto out;
81 }
82
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010083 ret = EFI_CALL(systab.boottime->open_protocol(
84 controller_handle, bp->ops->protocol,
85 &interface, this->driver_binding_handle,
86 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
87 switch (ret) {
88 case EFI_ACCESS_DENIED:
89 case EFI_ALREADY_STARTED:
90 goto out;
91 case EFI_SUCCESS:
92 break;
93 default:
94 ret = EFI_UNSUPPORTED;
95 goto out;
96 }
97
98 ret = check_node_type(controller_handle);
99
100 r = EFI_CALL(systab.boottime->close_protocol(
101 controller_handle, bp->ops->protocol,
102 this->driver_binding_handle,
103 controller_handle));
104 if (r != EFI_SUCCESS)
105 ret = EFI_UNSUPPORTED;
106out:
107 return EFI_EXIT(ret);
108}
109
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200110/**
111 * efi_uc_start() - create child controllers and attach driver
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100112 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200113 * @this: driver binding protocol
114 * @controller_handle: handle of the controller
115 * @remaining_device_path: path specifying the child controller
116 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100117 */
118static efi_status_t EFIAPI efi_uc_start(
119 struct efi_driver_binding_protocol *this,
120 efi_handle_t controller_handle,
121 struct efi_device_path *remaining_device_path)
122{
123 efi_status_t r, ret;
124 void *interface = NULL;
125 struct efi_driver_binding_extended_protocol *bp =
126 (struct efi_driver_binding_extended_protocol *)this;
127
Heinrich Schuchardt64dc7af2020-01-10 12:33:16 +0100128 EFI_ENTRY("%p, %p, %ls", this, controller_handle,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100129 efi_dp_str(remaining_device_path));
130
131 /* Attach driver to controller */
132 ret = EFI_CALL(systab.boottime->open_protocol(
133 controller_handle, bp->ops->protocol,
134 &interface, this->driver_binding_handle,
135 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
136 switch (ret) {
137 case EFI_ACCESS_DENIED:
138 case EFI_ALREADY_STARTED:
139 goto out;
140 case EFI_SUCCESS:
141 break;
142 default:
143 ret = EFI_UNSUPPORTED;
144 goto out;
145 }
146 ret = check_node_type(controller_handle);
147 if (ret != EFI_SUCCESS) {
148 r = EFI_CALL(systab.boottime->close_protocol(
149 controller_handle, bp->ops->protocol,
150 this->driver_binding_handle,
151 controller_handle));
152 if (r != EFI_SUCCESS)
153 EFI_PRINT("Failure to close handle\n");
154 goto out;
155 }
156
157 /* TODO: driver specific stuff */
158 bp->ops->bind(controller_handle, interface);
159
160out:
161 return EFI_EXIT(ret);
162}
163
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200164/**
165 * disconnect_child() - remove a single child controller from the parent
166 * controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100167 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200168 * @controller_handle: parent controller
169 * @child_handle: child controller
170 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100171 */
172static efi_status_t disconnect_child(efi_handle_t controller_handle,
173 efi_handle_t child_handle)
174{
175 efi_status_t ret;
176 efi_guid_t *guid_controller = NULL;
177 efi_guid_t *guid_child_controller = NULL;
178
179 ret = EFI_CALL(systab.boottime->close_protocol(
180 controller_handle, guid_controller,
181 child_handle, child_handle));
182 if (ret != EFI_SUCCESS) {
183 EFI_PRINT("Cannot close protocol\n");
184 return ret;
185 }
186 ret = EFI_CALL(systab.boottime->uninstall_protocol_interface(
187 child_handle, guid_child_controller, NULL));
188 if (ret != EFI_SUCCESS) {
189 EFI_PRINT("Cannot uninstall protocol interface\n");
190 return ret;
191 }
192 return ret;
193}
194
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200195/**
196 * efi_uc_stop() - Remove child controllers and disconnect the controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100197 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200198 * @this: driver binding protocol
199 * @controller_handle: handle of the controller
200 * @number_of_children: number of child controllers to remove
201 * @child_handle_buffer: handles of the child controllers to remove
202 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100203 */
204static efi_status_t EFIAPI efi_uc_stop(
205 struct efi_driver_binding_protocol *this,
206 efi_handle_t controller_handle,
207 size_t number_of_children,
208 efi_handle_t *child_handle_buffer)
209{
210 efi_status_t ret;
211 efi_uintn_t count;
212 struct efi_open_protocol_info_entry *entry_buffer;
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100213 struct efi_driver_binding_extended_protocol *bp =
214 (struct efi_driver_binding_extended_protocol *)this;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100215
Heinrich Schuchardt64dc7af2020-01-10 12:33:16 +0100216 EFI_ENTRY("%p, %p, %zu, %p", this, controller_handle,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100217 number_of_children, child_handle_buffer);
218
219 /* Destroy provided child controllers */
220 if (number_of_children) {
221 efi_uintn_t i;
222
223 for (i = 0; i < number_of_children; ++i) {
224 ret = disconnect_child(controller_handle,
225 child_handle_buffer[i]);
226 if (ret != EFI_SUCCESS)
227 return ret;
228 }
229 return EFI_SUCCESS;
230 }
231
232 /* Destroy all children */
233 ret = EFI_CALL(systab.boottime->open_protocol_information(
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100234 controller_handle, bp->ops->protocol,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100235 &entry_buffer, &count));
236 if (ret != EFI_SUCCESS)
237 goto out;
238 while (count) {
239 if (entry_buffer[--count].attributes &
240 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
241 ret = disconnect_child(
242 controller_handle,
243 entry_buffer[count].agent_handle);
244 if (ret != EFI_SUCCESS)
245 goto out;
246 }
247 }
248 ret = EFI_CALL(systab.boottime->free_pool(entry_buffer));
249 if (ret != EFI_SUCCESS)
Heinrich Schuchardt42c8ba22020-12-01 09:06:29 +0100250 log_err("Cannot free EFI memory pool\n");
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100251
252 /* Detach driver from controller */
253 ret = EFI_CALL(systab.boottime->close_protocol(
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100254 controller_handle, bp->ops->protocol,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100255 this->driver_binding_handle, controller_handle));
256out:
257 return EFI_EXIT(ret);
258}
259
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200260/**
261 * efi_add_driver() - add driver
262 *
263 * @drv: driver to add
264 * Return: status code
265 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100266static efi_status_t efi_add_driver(struct driver *drv)
267{
268 efi_status_t ret;
269 const struct efi_driver_ops *ops = drv->ops;
270 struct efi_driver_binding_extended_protocol *bp;
271
Heinrich Schuchardt42c8ba22020-12-01 09:06:29 +0100272 log_debug("Adding EFI driver '%s'\n", drv->name);
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100273 if (!ops->protocol) {
Heinrich Schuchardt42c8ba22020-12-01 09:06:29 +0100274 log_err("EFI protocol GUID missing for driver '%s'\n",
275 drv->name);
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100276 return EFI_INVALID_PARAMETER;
277 }
278 bp = calloc(1, sizeof(struct efi_driver_binding_extended_protocol));
279 if (!bp)
280 return EFI_OUT_OF_RESOURCES;
281
282 bp->bp.supported = efi_uc_supported;
283 bp->bp.start = efi_uc_start;
284 bp->bp.stop = efi_uc_stop;
285 bp->bp.version = 0xffffffff;
286 bp->ops = drv->ops;
287
288 ret = efi_create_handle(&bp->bp.driver_binding_handle);
289 if (ret != EFI_SUCCESS) {
290 free(bp);
291 goto out;
292 }
293 bp->bp.image_handle = bp->bp.driver_binding_handle;
294 ret = efi_add_protocol(bp->bp.driver_binding_handle,
295 &efi_guid_driver_binding_protocol, bp);
296 if (ret != EFI_SUCCESS) {
297 efi_delete_handle(bp->bp.driver_binding_handle);
298 free(bp);
299 goto out;
300 }
301out:
302 return ret;
303}
304
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200305/**
306 * efi_driver_init() - initialize the EFI drivers
307 *
308 * Called by efi_init_obj_list().
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100309 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200310 * Return: 0 = success, any other value will stop further execution
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100311 */
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100312efi_status_t efi_driver_init(void)
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100313{
314 struct driver *drv;
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100315 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100316
Heinrich Schuchardt42c8ba22020-12-01 09:06:29 +0100317 log_debug("Initializing EFI driver framework\n");
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100318 for (drv = ll_entry_start(struct driver, driver);
319 drv < ll_entry_end(struct driver, driver); ++drv) {
Simon Glass15c4d672021-12-04 08:56:30 -0700320 if (drv->id == UCLASS_EFI_LOADER) {
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100321 ret = efi_add_driver(drv);
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100322 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt42c8ba22020-12-01 09:06:29 +0100323 log_err("Failed to add EFI driver %s\n",
324 drv->name);
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100325 break;
326 }
327 }
328 }
329 return ret;
330}
331
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200332/**
333 * efi_uc_init() - initialize the EFI uclass
334 *
335 * @class: the EFI uclass
336 * Return: 0 = success
337 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100338static int efi_uc_init(struct uclass *class)
339{
Simon Glass15c4d672021-12-04 08:56:30 -0700340 log_debug("Initializing UCLASS_EFI_LOADER\n");
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100341 return 0;
342}
343
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200344/**
345 * efi_uc_destroy() - destroy the EFI uclass
346 *
347 * @class: the EFI uclass
348 * Return: 0 = success
349 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100350static int efi_uc_destroy(struct uclass *class)
351{
Simon Glass15c4d672021-12-04 08:56:30 -0700352 log_debug("Destroying UCLASS_EFI_LOADER\n");
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100353 return 0;
354}
355
356UCLASS_DRIVER(efi) = {
357 .name = "efi",
Simon Glass15c4d672021-12-04 08:56:30 -0700358 .id = UCLASS_EFI_LOADER,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100359 .init = efi_uc_init,
360 .destroy = efi_uc_destroy,
361};