blob: 382c2b477f4dbaa87022e0a777386b88edde333c [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +01002/*
3 * Uclass for EFI drivers
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
6 *
Heinrich Schuchardt05ef48a2018-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 Glasse1e10f22020-07-19 10:15:43 -060020#include <common.h>
21#include <dm.h>
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010022#include <efi_driver.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070024#include <malloc.h>
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010025
Heinrich Schuchardtea808852018-09-18 18:52:46 +020026/**
27 * check_node_type() - check node type
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010028 *
Heinrich Schuchardtea808852018-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 Schuchardt05ef48a2018-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 Schuchardtea808852018-09-18 18:52:46 +020053/**
54 * efi_uc_supported() - check if the driver supports the controller
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010055 *
Heinrich Schuchardtea808852018-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 Schuchardt05ef48a2018-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
74 ret = EFI_CALL(systab.boottime->open_protocol(
75 controller_handle, bp->ops->protocol,
76 &interface, this->driver_binding_handle,
77 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
78 switch (ret) {
79 case EFI_ACCESS_DENIED:
80 case EFI_ALREADY_STARTED:
81 goto out;
82 case EFI_SUCCESS:
83 break;
84 default:
85 ret = EFI_UNSUPPORTED;
86 goto out;
87 }
88
89 ret = check_node_type(controller_handle);
90
91 r = EFI_CALL(systab.boottime->close_protocol(
92 controller_handle, bp->ops->protocol,
93 this->driver_binding_handle,
94 controller_handle));
95 if (r != EFI_SUCCESS)
96 ret = EFI_UNSUPPORTED;
97out:
98 return EFI_EXIT(ret);
99}
100
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200101/**
102 * efi_uc_start() - create child controllers and attach driver
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100103 *
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200104 * @this: driver binding protocol
105 * @controller_handle: handle of the controller
106 * @remaining_device_path: path specifying the child controller
107 * Return: status code
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100108 */
109static efi_status_t EFIAPI efi_uc_start(
110 struct efi_driver_binding_protocol *this,
111 efi_handle_t controller_handle,
112 struct efi_device_path *remaining_device_path)
113{
114 efi_status_t r, ret;
115 void *interface = NULL;
116 struct efi_driver_binding_extended_protocol *bp =
117 (struct efi_driver_binding_extended_protocol *)this;
118
Heinrich Schuchardt30ed1d42020-01-10 12:33:16 +0100119 EFI_ENTRY("%p, %p, %ls", this, controller_handle,
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100120 efi_dp_str(remaining_device_path));
121
122 /* Attach driver to controller */
123 ret = EFI_CALL(systab.boottime->open_protocol(
124 controller_handle, bp->ops->protocol,
125 &interface, this->driver_binding_handle,
126 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
127 switch (ret) {
128 case EFI_ACCESS_DENIED:
129 case EFI_ALREADY_STARTED:
130 goto out;
131 case EFI_SUCCESS:
132 break;
133 default:
134 ret = EFI_UNSUPPORTED;
135 goto out;
136 }
137 ret = check_node_type(controller_handle);
138 if (ret != EFI_SUCCESS) {
139 r = EFI_CALL(systab.boottime->close_protocol(
140 controller_handle, bp->ops->protocol,
141 this->driver_binding_handle,
142 controller_handle));
143 if (r != EFI_SUCCESS)
144 EFI_PRINT("Failure to close handle\n");
145 goto out;
146 }
147
148 /* TODO: driver specific stuff */
149 bp->ops->bind(controller_handle, interface);
150
151out:
152 return EFI_EXIT(ret);
153}
154
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200155/**
156 * disconnect_child() - remove a single child controller from the parent
157 * controller
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100158 *
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200159 * @controller_handle: parent controller
160 * @child_handle: child controller
161 * Return: status code
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100162 */
163static efi_status_t disconnect_child(efi_handle_t controller_handle,
164 efi_handle_t child_handle)
165{
166 efi_status_t ret;
167 efi_guid_t *guid_controller = NULL;
168 efi_guid_t *guid_child_controller = NULL;
169
170 ret = EFI_CALL(systab.boottime->close_protocol(
171 controller_handle, guid_controller,
172 child_handle, child_handle));
173 if (ret != EFI_SUCCESS) {
174 EFI_PRINT("Cannot close protocol\n");
175 return ret;
176 }
177 ret = EFI_CALL(systab.boottime->uninstall_protocol_interface(
178 child_handle, guid_child_controller, NULL));
179 if (ret != EFI_SUCCESS) {
180 EFI_PRINT("Cannot uninstall protocol interface\n");
181 return ret;
182 }
183 return ret;
184}
185
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200186/**
187 * efi_uc_stop() - Remove child controllers and disconnect the controller
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100188 *
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200189 * @this: driver binding protocol
190 * @controller_handle: handle of the controller
191 * @number_of_children: number of child controllers to remove
192 * @child_handle_buffer: handles of the child controllers to remove
193 * Return: status code
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100194 */
195static efi_status_t EFIAPI efi_uc_stop(
196 struct efi_driver_binding_protocol *this,
197 efi_handle_t controller_handle,
198 size_t number_of_children,
199 efi_handle_t *child_handle_buffer)
200{
201 efi_status_t ret;
202 efi_uintn_t count;
203 struct efi_open_protocol_info_entry *entry_buffer;
Heinrich Schuchardtd7431042020-01-09 23:26:43 +0100204 struct efi_driver_binding_extended_protocol *bp =
205 (struct efi_driver_binding_extended_protocol *)this;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100206
Heinrich Schuchardt30ed1d42020-01-10 12:33:16 +0100207 EFI_ENTRY("%p, %p, %zu, %p", this, controller_handle,
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100208 number_of_children, child_handle_buffer);
209
210 /* Destroy provided child controllers */
211 if (number_of_children) {
212 efi_uintn_t i;
213
214 for (i = 0; i < number_of_children; ++i) {
215 ret = disconnect_child(controller_handle,
216 child_handle_buffer[i]);
217 if (ret != EFI_SUCCESS)
218 return ret;
219 }
220 return EFI_SUCCESS;
221 }
222
223 /* Destroy all children */
224 ret = EFI_CALL(systab.boottime->open_protocol_information(
Heinrich Schuchardtd7431042020-01-09 23:26:43 +0100225 controller_handle, bp->ops->protocol,
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100226 &entry_buffer, &count));
227 if (ret != EFI_SUCCESS)
228 goto out;
229 while (count) {
230 if (entry_buffer[--count].attributes &
231 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
232 ret = disconnect_child(
233 controller_handle,
234 entry_buffer[count].agent_handle);
235 if (ret != EFI_SUCCESS)
236 goto out;
237 }
238 }
239 ret = EFI_CALL(systab.boottime->free_pool(entry_buffer));
240 if (ret != EFI_SUCCESS)
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100241 log_err("Cannot free EFI memory pool\n");
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100242
243 /* Detach driver from controller */
244 ret = EFI_CALL(systab.boottime->close_protocol(
Heinrich Schuchardtd7431042020-01-09 23:26:43 +0100245 controller_handle, bp->ops->protocol,
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100246 this->driver_binding_handle, controller_handle));
247out:
248 return EFI_EXIT(ret);
249}
250
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200251/**
252 * efi_add_driver() - add driver
253 *
254 * @drv: driver to add
255 * Return: status code
256 */
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100257static efi_status_t efi_add_driver(struct driver *drv)
258{
259 efi_status_t ret;
260 const struct efi_driver_ops *ops = drv->ops;
261 struct efi_driver_binding_extended_protocol *bp;
262
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100263 log_debug("Adding EFI driver '%s'\n", drv->name);
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100264 if (!ops->protocol) {
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100265 log_err("EFI protocol GUID missing for driver '%s'\n",
266 drv->name);
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100267 return EFI_INVALID_PARAMETER;
268 }
269 bp = calloc(1, sizeof(struct efi_driver_binding_extended_protocol));
270 if (!bp)
271 return EFI_OUT_OF_RESOURCES;
272
273 bp->bp.supported = efi_uc_supported;
274 bp->bp.start = efi_uc_start;
275 bp->bp.stop = efi_uc_stop;
276 bp->bp.version = 0xffffffff;
277 bp->ops = drv->ops;
278
279 ret = efi_create_handle(&bp->bp.driver_binding_handle);
280 if (ret != EFI_SUCCESS) {
281 free(bp);
282 goto out;
283 }
284 bp->bp.image_handle = bp->bp.driver_binding_handle;
285 ret = efi_add_protocol(bp->bp.driver_binding_handle,
286 &efi_guid_driver_binding_protocol, bp);
287 if (ret != EFI_SUCCESS) {
288 efi_delete_handle(bp->bp.driver_binding_handle);
289 free(bp);
290 goto out;
291 }
292out:
293 return ret;
294}
295
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200296/**
297 * efi_driver_init() - initialize the EFI drivers
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100298 *
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200299 * Called by efi_init_obj_list().
300 *
301 * Return: 0 = success, any other value will stop further execution
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100302 */
Heinrich Schuchardt038782a2018-02-01 12:53:32 +0100303efi_status_t efi_driver_init(void)
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100304{
305 struct driver *drv;
Heinrich Schuchardt038782a2018-02-01 12:53:32 +0100306 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100307
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100308 log_debug("Initializing EFI driver framework\n");
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100309 for (drv = ll_entry_start(struct driver, driver);
310 drv < ll_entry_end(struct driver, driver); ++drv) {
311 if (drv->id == UCLASS_EFI) {
312 ret = efi_add_driver(drv);
Heinrich Schuchardt038782a2018-02-01 12:53:32 +0100313 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100314 log_err("Failed to add EFI driver %s\n",
315 drv->name);
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100316 break;
317 }
318 }
319 }
320 return ret;
321}
322
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200323/**
324 * efi_uc_init() - initialize the EFI uclass
325 *
326 * @class: the EFI uclass
327 * Return: 0 = success
328 */
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100329static int efi_uc_init(struct uclass *class)
330{
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100331 log_debug("Initializing UCLASS_EFI\n");
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100332 return 0;
333}
334
Heinrich Schuchardtea808852018-09-18 18:52:46 +0200335/**
336 * efi_uc_destroy() - destroy the EFI uclass
337 *
338 * @class: the EFI uclass
339 * Return: 0 = success
340 */
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100341static int efi_uc_destroy(struct uclass *class)
342{
Heinrich Schuchardtfc364422020-12-01 09:06:29 +0100343 log_debug("Destroying UCLASS_EFI\n");
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100344 return 0;
345}
346
347UCLASS_DRIVER(efi) = {
348 .name = "efi",
349 .id = UCLASS_EFI,
350 .init = efi_uc_init,
351 .destroy = efi_uc_destroy,
352};