blob: 04e4e457344afd307ebe36b8aa0049d4393fd692 [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
20#include <efi_driver.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <malloc.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010023
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020024/**
25 * check_node_type() - check node type
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010026 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020027 * We do not support partitions as controller handles.
28 *
29 * @handle: handle to be checked
30 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010031 */
32static efi_status_t check_node_type(efi_handle_t handle)
33{
34 efi_status_t r, ret = EFI_SUCCESS;
35 const struct efi_device_path *dp;
36
37 /* Open the device path protocol */
38 r = EFI_CALL(systab.boottime->open_protocol(
39 handle, &efi_guid_device_path, (void **)&dp,
40 NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL));
41 if (r == EFI_SUCCESS && dp) {
42 /* Get the last node */
43 const struct efi_device_path *node = efi_dp_last_node(dp);
44 /* We do not support partitions as controller */
45 if (!node || node->type == DEVICE_PATH_TYPE_MEDIA_DEVICE)
46 ret = EFI_UNSUPPORTED;
47 }
48 return ret;
49}
50
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020051/**
52 * efi_uc_supported() - check if the driver supports the controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010053 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020054 * @this: driver binding protocol
55 * @controller_handle: handle of the controller
56 * @remaining_device_path: path specifying the child controller
57 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010058 */
59static efi_status_t EFIAPI efi_uc_supported(
60 struct efi_driver_binding_protocol *this,
61 efi_handle_t controller_handle,
62 struct efi_device_path *remaining_device_path)
63{
64 efi_status_t r, ret;
65 void *interface;
66 struct efi_driver_binding_extended_protocol *bp =
67 (struct efi_driver_binding_extended_protocol *)this;
68
69 EFI_ENTRY("%p, %p, %ls", this, controller_handle,
70 efi_dp_str(remaining_device_path));
71
72 ret = EFI_CALL(systab.boottime->open_protocol(
73 controller_handle, bp->ops->protocol,
74 &interface, this->driver_binding_handle,
75 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
76 switch (ret) {
77 case EFI_ACCESS_DENIED:
78 case EFI_ALREADY_STARTED:
79 goto out;
80 case EFI_SUCCESS:
81 break;
82 default:
83 ret = EFI_UNSUPPORTED;
84 goto out;
85 }
86
87 ret = check_node_type(controller_handle);
88
89 r = EFI_CALL(systab.boottime->close_protocol(
90 controller_handle, bp->ops->protocol,
91 this->driver_binding_handle,
92 controller_handle));
93 if (r != EFI_SUCCESS)
94 ret = EFI_UNSUPPORTED;
95out:
96 return EFI_EXIT(ret);
97}
98
Heinrich Schuchardt55de1002018-09-18 18:52:46 +020099/**
100 * efi_uc_start() - create child controllers and attach driver
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100101 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200102 * @this: driver binding protocol
103 * @controller_handle: handle of the controller
104 * @remaining_device_path: path specifying the child controller
105 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100106 */
107static efi_status_t EFIAPI efi_uc_start(
108 struct efi_driver_binding_protocol *this,
109 efi_handle_t controller_handle,
110 struct efi_device_path *remaining_device_path)
111{
112 efi_status_t r, ret;
113 void *interface = NULL;
114 struct efi_driver_binding_extended_protocol *bp =
115 (struct efi_driver_binding_extended_protocol *)this;
116
Heinrich Schuchardt64dc7af2020-01-10 12:33:16 +0100117 EFI_ENTRY("%p, %p, %ls", this, controller_handle,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100118 efi_dp_str(remaining_device_path));
119
120 /* Attach driver to controller */
121 ret = EFI_CALL(systab.boottime->open_protocol(
122 controller_handle, bp->ops->protocol,
123 &interface, this->driver_binding_handle,
124 controller_handle, EFI_OPEN_PROTOCOL_BY_DRIVER));
125 switch (ret) {
126 case EFI_ACCESS_DENIED:
127 case EFI_ALREADY_STARTED:
128 goto out;
129 case EFI_SUCCESS:
130 break;
131 default:
132 ret = EFI_UNSUPPORTED;
133 goto out;
134 }
135 ret = check_node_type(controller_handle);
136 if (ret != EFI_SUCCESS) {
137 r = EFI_CALL(systab.boottime->close_protocol(
138 controller_handle, bp->ops->protocol,
139 this->driver_binding_handle,
140 controller_handle));
141 if (r != EFI_SUCCESS)
142 EFI_PRINT("Failure to close handle\n");
143 goto out;
144 }
145
146 /* TODO: driver specific stuff */
147 bp->ops->bind(controller_handle, interface);
148
149out:
150 return EFI_EXIT(ret);
151}
152
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200153/**
154 * disconnect_child() - remove a single child controller from the parent
155 * controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100156 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200157 * @controller_handle: parent controller
158 * @child_handle: child controller
159 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100160 */
161static efi_status_t disconnect_child(efi_handle_t controller_handle,
162 efi_handle_t child_handle)
163{
164 efi_status_t ret;
165 efi_guid_t *guid_controller = NULL;
166 efi_guid_t *guid_child_controller = NULL;
167
168 ret = EFI_CALL(systab.boottime->close_protocol(
169 controller_handle, guid_controller,
170 child_handle, child_handle));
171 if (ret != EFI_SUCCESS) {
172 EFI_PRINT("Cannot close protocol\n");
173 return ret;
174 }
175 ret = EFI_CALL(systab.boottime->uninstall_protocol_interface(
176 child_handle, guid_child_controller, NULL));
177 if (ret != EFI_SUCCESS) {
178 EFI_PRINT("Cannot uninstall protocol interface\n");
179 return ret;
180 }
181 return ret;
182}
183
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200184/**
185 * efi_uc_stop() - Remove child controllers and disconnect the controller
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100186 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200187 * @this: driver binding protocol
188 * @controller_handle: handle of the controller
189 * @number_of_children: number of child controllers to remove
190 * @child_handle_buffer: handles of the child controllers to remove
191 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100192 */
193static efi_status_t EFIAPI efi_uc_stop(
194 struct efi_driver_binding_protocol *this,
195 efi_handle_t controller_handle,
196 size_t number_of_children,
197 efi_handle_t *child_handle_buffer)
198{
199 efi_status_t ret;
200 efi_uintn_t count;
201 struct efi_open_protocol_info_entry *entry_buffer;
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100202 struct efi_driver_binding_extended_protocol *bp =
203 (struct efi_driver_binding_extended_protocol *)this;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100204
Heinrich Schuchardt64dc7af2020-01-10 12:33:16 +0100205 EFI_ENTRY("%p, %p, %zu, %p", this, controller_handle,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100206 number_of_children, child_handle_buffer);
207
208 /* Destroy provided child controllers */
209 if (number_of_children) {
210 efi_uintn_t i;
211
212 for (i = 0; i < number_of_children; ++i) {
213 ret = disconnect_child(controller_handle,
214 child_handle_buffer[i]);
215 if (ret != EFI_SUCCESS)
216 return ret;
217 }
218 return EFI_SUCCESS;
219 }
220
221 /* Destroy all children */
222 ret = EFI_CALL(systab.boottime->open_protocol_information(
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100223 controller_handle, bp->ops->protocol,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100224 &entry_buffer, &count));
225 if (ret != EFI_SUCCESS)
226 goto out;
227 while (count) {
228 if (entry_buffer[--count].attributes &
229 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
230 ret = disconnect_child(
231 controller_handle,
232 entry_buffer[count].agent_handle);
233 if (ret != EFI_SUCCESS)
234 goto out;
235 }
236 }
237 ret = EFI_CALL(systab.boottime->free_pool(entry_buffer));
238 if (ret != EFI_SUCCESS)
Heinrich Schuchardt924045e2019-01-06 17:12:43 +0100239 printf("%s: ERROR: Cannot free pool\n", __func__);
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100240
241 /* Detach driver from controller */
242 ret = EFI_CALL(systab.boottime->close_protocol(
Heinrich Schuchardta3a4a9c2020-01-09 23:26:43 +0100243 controller_handle, bp->ops->protocol,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100244 this->driver_binding_handle, controller_handle));
245out:
246 return EFI_EXIT(ret);
247}
248
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200249/**
250 * efi_add_driver() - add driver
251 *
252 * @drv: driver to add
253 * Return: status code
254 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100255static efi_status_t efi_add_driver(struct driver *drv)
256{
257 efi_status_t ret;
258 const struct efi_driver_ops *ops = drv->ops;
259 struct efi_driver_binding_extended_protocol *bp;
260
261 debug("EFI: Adding driver '%s'\n", drv->name);
262 if (!ops->protocol) {
263 printf("EFI: ERROR: protocol GUID missing for driver '%s'\n",
264 drv->name);
265 return EFI_INVALID_PARAMETER;
266 }
267 bp = calloc(1, sizeof(struct efi_driver_binding_extended_protocol));
268 if (!bp)
269 return EFI_OUT_OF_RESOURCES;
270
271 bp->bp.supported = efi_uc_supported;
272 bp->bp.start = efi_uc_start;
273 bp->bp.stop = efi_uc_stop;
274 bp->bp.version = 0xffffffff;
275 bp->ops = drv->ops;
276
277 ret = efi_create_handle(&bp->bp.driver_binding_handle);
278 if (ret != EFI_SUCCESS) {
279 free(bp);
280 goto out;
281 }
282 bp->bp.image_handle = bp->bp.driver_binding_handle;
283 ret = efi_add_protocol(bp->bp.driver_binding_handle,
284 &efi_guid_driver_binding_protocol, bp);
285 if (ret != EFI_SUCCESS) {
286 efi_delete_handle(bp->bp.driver_binding_handle);
287 free(bp);
288 goto out;
289 }
290out:
291 return ret;
292}
293
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200294/**
295 * efi_driver_init() - initialize the EFI drivers
296 *
297 * Called by efi_init_obj_list().
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100298 *
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200299 * Return: 0 = success, any other value will stop further execution
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100300 */
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100301efi_status_t efi_driver_init(void)
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100302{
303 struct driver *drv;
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100304 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100305
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100306 debug("EFI: Initializing EFI driver framework\n");
307 for (drv = ll_entry_start(struct driver, driver);
308 drv < ll_entry_end(struct driver, driver); ++drv) {
309 if (drv->id == UCLASS_EFI) {
310 ret = efi_add_driver(drv);
Heinrich Schuchardt8f3cc5b2018-02-01 12:53:32 +0100311 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100312 printf("EFI: ERROR: failed to add driver %s\n",
313 drv->name);
314 break;
315 }
316 }
317 }
318 return ret;
319}
320
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200321/**
322 * efi_uc_init() - initialize the EFI uclass
323 *
324 * @class: the EFI uclass
325 * Return: 0 = success
326 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100327static int efi_uc_init(struct uclass *class)
328{
329 printf("EFI: Initializing UCLASS_EFI\n");
330 return 0;
331}
332
Heinrich Schuchardt55de1002018-09-18 18:52:46 +0200333/**
334 * efi_uc_destroy() - destroy the EFI uclass
335 *
336 * @class: the EFI uclass
337 * Return: 0 = success
338 */
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100339static int efi_uc_destroy(struct uclass *class)
340{
341 printf("Destroying UCLASS_EFI\n");
342 return 0;
343}
344
345UCLASS_DRIVER(efi) = {
346 .name = "efi",
347 .id = UCLASS_EFI,
348 .init = efi_uc_init,
349 .destroy = efi_uc_destroy,
350};