]>
Commit | Line | Data |
---|---|---|
bee91169 AG |
1 | /* |
2 | * EFI application boot time services | |
3 | * | |
4 | * Copyright (c) 2016 Alexander Graf | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0+ | |
7 | */ | |
8 | ||
bee91169 | 9 | #include <common.h> |
7d963323 | 10 | #include <div64.h> |
bee91169 | 11 | #include <efi_loader.h> |
ad644e7c | 12 | #include <environment.h> |
bee91169 AG |
13 | #include <malloc.h> |
14 | #include <asm/global_data.h> | |
b08c8c48 | 15 | #include <linux/libfdt_env.h> |
bee91169 AG |
16 | #include <u-boot/crc.h> |
17 | #include <bootm.h> | |
18 | #include <inttypes.h> | |
19 | #include <watchdog.h> | |
20 | ||
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
32f4b2f8 | 23 | /* Task priority level */ |
152cade3 | 24 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
32f4b2f8 | 25 | |
bee91169 AG |
26 | /* This list contains all the EFI objects our payload has access to */ |
27 | LIST_HEAD(efi_obj_list); | |
28 | ||
29 | /* | |
30 | * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) | |
31 | * we need to do trickery with caches. Since we don't want to break the EFI | |
32 | * aware boot path, only apply hacks when loading exiting directly (breaking | |
33 | * direct Linux EFI booting along the way - oh well). | |
34 | */ | |
35 | static bool efi_is_direct_boot = true; | |
36 | ||
37 | /* | |
38 | * EFI can pass arbitrary additional "tables" containing vendor specific | |
39 | * information to the payload. One such table is the FDT table which contains | |
40 | * a pointer to a flattened device tree blob. | |
41 | * | |
42 | * In most cases we want to pass an FDT to the payload, so reserve one slot of | |
43 | * config table space for it. The pointer gets populated by do_bootefi_exec(). | |
44 | */ | |
3c63db9c | 45 | static struct efi_configuration_table __efi_runtime_data efi_conf_table[2]; |
bee91169 | 46 | |
65e4c0b1 | 47 | #ifdef CONFIG_ARM |
bee91169 AG |
48 | /* |
49 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare | |
50 | * fixed when compiling U-Boot. However, the payload does not know about that | |
51 | * restriction so we need to manually swap its and our view of that register on | |
52 | * EFI callback entry/exit. | |
53 | */ | |
54 | static volatile void *efi_gd, *app_gd; | |
65e4c0b1 | 55 | #endif |
bee91169 | 56 | |
c160d2f5 | 57 | static int entry_count; |
af65db85 | 58 | static int nesting_level; |
bc4f9133 HS |
59 | /* GUID of the device tree table */ |
60 | const efi_guid_t efi_guid_fdt = EFI_FDT_GUID; | |
f0959dbe HS |
61 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
62 | const efi_guid_t efi_guid_driver_binding_protocol = | |
63 | EFI_DRIVER_BINDING_PROTOCOL_GUID; | |
c160d2f5 | 64 | |
2074f700 HS |
65 | static efi_status_t EFIAPI efi_disconnect_controller( |
66 | efi_handle_t controller_handle, | |
67 | efi_handle_t driver_image_handle, | |
68 | efi_handle_t child_handle); | |
3f9b0042 | 69 | |
c160d2f5 RC |
70 | /* Called on every callback entry */ |
71 | int __efi_entry_check(void) | |
72 | { | |
73 | int ret = entry_count++ == 0; | |
74 | #ifdef CONFIG_ARM | |
75 | assert(efi_gd); | |
76 | app_gd = gd; | |
77 | gd = efi_gd; | |
78 | #endif | |
79 | return ret; | |
80 | } | |
81 | ||
82 | /* Called on every callback exit */ | |
83 | int __efi_exit_check(void) | |
84 | { | |
85 | int ret = --entry_count == 0; | |
86 | #ifdef CONFIG_ARM | |
87 | gd = app_gd; | |
88 | #endif | |
89 | return ret; | |
90 | } | |
91 | ||
bee91169 AG |
92 | /* Called from do_bootefi_exec() */ |
93 | void efi_save_gd(void) | |
94 | { | |
65e4c0b1 | 95 | #ifdef CONFIG_ARM |
bee91169 | 96 | efi_gd = gd; |
65e4c0b1 | 97 | #endif |
bee91169 AG |
98 | } |
99 | ||
c160d2f5 RC |
100 | /* |
101 | * Special case handler for error/abort that just forces things back | |
102 | * to u-boot world so we can dump out an abort msg, without any care | |
103 | * about returning back to UEFI world. | |
104 | */ | |
bee91169 AG |
105 | void efi_restore_gd(void) |
106 | { | |
65e4c0b1 | 107 | #ifdef CONFIG_ARM |
bee91169 AG |
108 | /* Only restore if we're already in EFI context */ |
109 | if (!efi_gd) | |
110 | return; | |
bee91169 | 111 | gd = efi_gd; |
65e4c0b1 | 112 | #endif |
bee91169 AG |
113 | } |
114 | ||
af65db85 | 115 | /* |
c8df80c5 HS |
116 | * Return a string for indenting with two spaces per level. A maximum of ten |
117 | * indent levels is supported. Higher indent levels will be truncated. | |
118 | * | |
119 | * @level indent level | |
120 | * @return indent string | |
af65db85 RC |
121 | */ |
122 | static const char *indent_string(int level) | |
123 | { | |
124 | const char *indent = " "; | |
125 | const int max = strlen(indent); | |
126 | level = min(max, level * 2); | |
127 | return &indent[max - level]; | |
128 | } | |
129 | ||
ae0bd3a9 HS |
130 | const char *__efi_nesting(void) |
131 | { | |
132 | return indent_string(nesting_level); | |
133 | } | |
134 | ||
af65db85 RC |
135 | const char *__efi_nesting_inc(void) |
136 | { | |
137 | return indent_string(nesting_level++); | |
138 | } | |
139 | ||
140 | const char *__efi_nesting_dec(void) | |
141 | { | |
142 | return indent_string(--nesting_level); | |
143 | } | |
144 | ||
332468f7 HS |
145 | /* |
146 | * Queue an EFI event. | |
147 | * | |
148 | * This function queues the notification function of the event for future | |
149 | * execution. | |
150 | * | |
151 | * The notification function is called if the task priority level of the | |
152 | * event is higher than the current task priority level. | |
153 | * | |
154 | * For the SignalEvent service see efi_signal_event_ext. | |
155 | * | |
156 | * @event event to signal | |
9bc9664d | 157 | * @check_tpl check the TPL level |
332468f7 | 158 | */ |
9bc9664d | 159 | void efi_signal_event(struct efi_event *event, bool check_tpl) |
c6841592 | 160 | { |
ca62a4f5 | 161 | if (event->notify_function) { |
e190e897 | 162 | event->is_queued = true; |
32f4b2f8 | 163 | /* Check TPL */ |
9bc9664d | 164 | if (check_tpl && efi_tpl >= event->notify_tpl) |
32f4b2f8 | 165 | return; |
ea630ce9 HS |
166 | EFI_CALL_VOID(event->notify_function(event, |
167 | event->notify_context)); | |
c6841592 | 168 | } |
e190e897 | 169 | event->is_queued = false; |
c6841592 HS |
170 | } |
171 | ||
332468f7 HS |
172 | /* |
173 | * Raise the task priority level. | |
174 | * | |
175 | * This function implements the RaiseTpl service. | |
176 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
177 | * for details. | |
178 | * | |
179 | * @new_tpl new value of the task priority level | |
180 | * @return old value of the task priority level | |
181 | */ | |
152cade3 | 182 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
bee91169 | 183 | { |
152cade3 | 184 | efi_uintn_t old_tpl = efi_tpl; |
32f4b2f8 | 185 | |
503f2695 | 186 | EFI_ENTRY("0x%zx", new_tpl); |
32f4b2f8 HS |
187 | |
188 | if (new_tpl < efi_tpl) | |
189 | debug("WARNING: new_tpl < current_tpl in %s\n", __func__); | |
190 | efi_tpl = new_tpl; | |
191 | if (efi_tpl > TPL_HIGH_LEVEL) | |
192 | efi_tpl = TPL_HIGH_LEVEL; | |
193 | ||
194 | EFI_EXIT(EFI_SUCCESS); | |
195 | return old_tpl; | |
bee91169 AG |
196 | } |
197 | ||
332468f7 HS |
198 | /* |
199 | * Lower the task priority level. | |
200 | * | |
201 | * This function implements the RestoreTpl service. | |
202 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
203 | * for details. | |
204 | * | |
205 | * @old_tpl value of the task priority level to be restored | |
206 | */ | |
152cade3 | 207 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
bee91169 | 208 | { |
503f2695 | 209 | EFI_ENTRY("0x%zx", old_tpl); |
32f4b2f8 HS |
210 | |
211 | if (old_tpl > efi_tpl) | |
212 | debug("WARNING: old_tpl > current_tpl in %s\n", __func__); | |
213 | efi_tpl = old_tpl; | |
214 | if (efi_tpl > TPL_HIGH_LEVEL) | |
215 | efi_tpl = TPL_HIGH_LEVEL; | |
216 | ||
217 | EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
218 | } |
219 | ||
332468f7 HS |
220 | /* |
221 | * Allocate memory pages. | |
222 | * | |
223 | * This function implements the AllocatePages service. | |
224 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
225 | * for details. | |
226 | * | |
227 | * @type type of allocation to be performed | |
228 | * @memory_type usage type of the allocated memory | |
229 | * @pages number of pages to be allocated | |
230 | * @memory allocated memory | |
231 | * @return status code | |
232 | */ | |
6e0bf8d8 | 233 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
f5a2a938 | 234 | efi_uintn_t pages, |
6e0bf8d8 | 235 | uint64_t *memory) |
bee91169 AG |
236 | { |
237 | efi_status_t r; | |
238 | ||
f5a2a938 | 239 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
bee91169 AG |
240 | r = efi_allocate_pages(type, memory_type, pages, memory); |
241 | return EFI_EXIT(r); | |
242 | } | |
243 | ||
332468f7 HS |
244 | /* |
245 | * Free memory pages. | |
246 | * | |
247 | * This function implements the FreePages service. | |
248 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
249 | * for details. | |
250 | * | |
251 | * @memory start of the memory area to be freed | |
252 | * @pages number of pages to be freed | |
253 | * @return status code | |
254 | */ | |
6e0bf8d8 | 255 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
f5a2a938 | 256 | efi_uintn_t pages) |
bee91169 AG |
257 | { |
258 | efi_status_t r; | |
259 | ||
f5a2a938 | 260 | EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages); |
bee91169 AG |
261 | r = efi_free_pages(memory, pages); |
262 | return EFI_EXIT(r); | |
263 | } | |
264 | ||
332468f7 HS |
265 | /* |
266 | * Get map describing memory usage. | |
267 | * | |
268 | * This function implements the GetMemoryMap service. | |
269 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
270 | * for details. | |
271 | * | |
272 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, | |
273 | * on exit the size of the copied memory map | |
274 | * @memory_map buffer to which the memory map is written | |
275 | * @map_key key for the memory map | |
276 | * @descriptor_size size of an individual memory descriptor | |
277 | * @descriptor_version version number of the memory descriptor structure | |
278 | * @return status code | |
279 | */ | |
6e0bf8d8 | 280 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
f5a2a938 | 281 | efi_uintn_t *memory_map_size, |
6e0bf8d8 | 282 | struct efi_mem_desc *memory_map, |
f5a2a938 HS |
283 | efi_uintn_t *map_key, |
284 | efi_uintn_t *descriptor_size, | |
6e0bf8d8 | 285 | uint32_t *descriptor_version) |
bee91169 AG |
286 | { |
287 | efi_status_t r; | |
288 | ||
289 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, | |
290 | map_key, descriptor_size, descriptor_version); | |
291 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, | |
292 | descriptor_size, descriptor_version); | |
293 | return EFI_EXIT(r); | |
294 | } | |
295 | ||
332468f7 HS |
296 | /* |
297 | * Allocate memory from pool. | |
298 | * | |
299 | * This function implements the AllocatePool service. | |
300 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
301 | * for details. | |
302 | * | |
303 | * @pool_type type of the pool from which memory is to be allocated | |
304 | * @size number of bytes to be allocated | |
305 | * @buffer allocated memory | |
306 | * @return status code | |
307 | */ | |
ead1274b | 308 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
f5a2a938 | 309 | efi_uintn_t size, |
ead1274b | 310 | void **buffer) |
bee91169 | 311 | { |
1cd29f0a AG |
312 | efi_status_t r; |
313 | ||
f5a2a938 | 314 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
ead1274b | 315 | r = efi_allocate_pool(pool_type, size, buffer); |
1cd29f0a | 316 | return EFI_EXIT(r); |
bee91169 AG |
317 | } |
318 | ||
332468f7 HS |
319 | /* |
320 | * Free memory from pool. | |
321 | * | |
322 | * This function implements the FreePool service. | |
323 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
324 | * for details. | |
325 | * | |
326 | * @buffer start of memory to be freed | |
327 | * @return status code | |
328 | */ | |
42417bc8 | 329 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
bee91169 | 330 | { |
1cd29f0a AG |
331 | efi_status_t r; |
332 | ||
333 | EFI_ENTRY("%p", buffer); | |
42417bc8 | 334 | r = efi_free_pool(buffer); |
1cd29f0a | 335 | return EFI_EXIT(r); |
bee91169 AG |
336 | } |
337 | ||
44549d62 HS |
338 | /* |
339 | * Add a new object to the object list. | |
340 | * | |
341 | * The protocols list is initialized. | |
342 | * The object handle is set. | |
343 | * | |
344 | * @obj object to be added | |
345 | */ | |
346 | void efi_add_handle(struct efi_object *obj) | |
347 | { | |
348 | if (!obj) | |
349 | return; | |
350 | INIT_LIST_HEAD(&obj->protocols); | |
351 | obj->handle = obj; | |
352 | list_add_tail(&obj->link, &efi_obj_list); | |
353 | } | |
354 | ||
2edab5e2 HS |
355 | /* |
356 | * Create handle. | |
357 | * | |
358 | * @handle new handle | |
359 | * @return status code | |
360 | */ | |
2074f700 | 361 | efi_status_t efi_create_handle(efi_handle_t *handle) |
3cc6e3fe HS |
362 | { |
363 | struct efi_object *obj; | |
364 | efi_status_t r; | |
365 | ||
366 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, | |
367 | sizeof(struct efi_object), | |
368 | (void **)&obj); | |
369 | if (r != EFI_SUCCESS) | |
370 | return r; | |
44549d62 HS |
371 | efi_add_handle(obj); |
372 | *handle = obj->handle; | |
3cc6e3fe HS |
373 | return r; |
374 | } | |
375 | ||
678e03a0 HS |
376 | /* |
377 | * Find a protocol on a handle. | |
378 | * | |
379 | * @handle handle | |
380 | * @protocol_guid GUID of the protocol | |
381 | * @handler reference to the protocol | |
382 | * @return status code | |
383 | */ | |
2074f700 | 384 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
678e03a0 HS |
385 | const efi_guid_t *protocol_guid, |
386 | struct efi_handler **handler) | |
387 | { | |
388 | struct efi_object *efiobj; | |
389 | struct list_head *lhandle; | |
390 | ||
391 | if (!handle || !protocol_guid) | |
392 | return EFI_INVALID_PARAMETER; | |
393 | efiobj = efi_search_obj(handle); | |
394 | if (!efiobj) | |
395 | return EFI_INVALID_PARAMETER; | |
396 | list_for_each(lhandle, &efiobj->protocols) { | |
397 | struct efi_handler *protocol; | |
398 | ||
399 | protocol = list_entry(lhandle, struct efi_handler, link); | |
400 | if (!guidcmp(protocol->guid, protocol_guid)) { | |
401 | if (handler) | |
402 | *handler = protocol; | |
403 | return EFI_SUCCESS; | |
404 | } | |
405 | } | |
406 | return EFI_NOT_FOUND; | |
407 | } | |
408 | ||
409 | /* | |
410 | * Delete protocol from a handle. | |
411 | * | |
412 | * @handle handle from which the protocol shall be deleted | |
413 | * @protocol GUID of the protocol to be deleted | |
414 | * @protocol_interface interface of the protocol implementation | |
415 | * @return status code | |
416 | */ | |
2074f700 HS |
417 | efi_status_t efi_remove_protocol(const efi_handle_t handle, |
418 | const efi_guid_t *protocol, | |
678e03a0 HS |
419 | void *protocol_interface) |
420 | { | |
421 | struct efi_handler *handler; | |
422 | efi_status_t ret; | |
423 | ||
424 | ret = efi_search_protocol(handle, protocol, &handler); | |
425 | if (ret != EFI_SUCCESS) | |
426 | return ret; | |
427 | if (guidcmp(handler->guid, protocol)) | |
428 | return EFI_INVALID_PARAMETER; | |
429 | list_del(&handler->link); | |
430 | free(handler); | |
431 | return EFI_SUCCESS; | |
432 | } | |
433 | ||
434 | /* | |
435 | * Delete all protocols from a handle. | |
436 | * | |
437 | * @handle handle from which the protocols shall be deleted | |
438 | * @return status code | |
439 | */ | |
2074f700 | 440 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
678e03a0 HS |
441 | { |
442 | struct efi_object *efiobj; | |
32e6fed6 HS |
443 | struct efi_handler *protocol; |
444 | struct efi_handler *pos; | |
678e03a0 HS |
445 | |
446 | efiobj = efi_search_obj(handle); | |
447 | if (!efiobj) | |
448 | return EFI_INVALID_PARAMETER; | |
32e6fed6 | 449 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
678e03a0 HS |
450 | efi_status_t ret; |
451 | ||
678e03a0 HS |
452 | ret = efi_remove_protocol(handle, protocol->guid, |
453 | protocol->protocol_interface); | |
454 | if (ret != EFI_SUCCESS) | |
455 | return ret; | |
456 | } | |
457 | return EFI_SUCCESS; | |
458 | } | |
459 | ||
460 | /* | |
461 | * Delete handle. | |
462 | * | |
463 | * @handle handle to delete | |
464 | */ | |
465 | void efi_delete_handle(struct efi_object *obj) | |
466 | { | |
467 | if (!obj) | |
468 | return; | |
469 | efi_remove_all_protocols(obj->handle); | |
470 | list_del(&obj->link); | |
471 | free(obj); | |
472 | } | |
473 | ||
bee91169 | 474 | /* |
c6841592 HS |
475 | * Our event capabilities are very limited. Only a small limited |
476 | * number of events is allowed to coexist. | |
bee91169 | 477 | */ |
c6841592 | 478 | static struct efi_event efi_events[16]; |
bee91169 | 479 | |
332468f7 HS |
480 | /* |
481 | * Create an event. | |
482 | * | |
483 | * This function is used inside U-Boot code to create an event. | |
484 | * | |
485 | * For the API function implementing the CreateEvent service see | |
486 | * efi_create_event_ext. | |
487 | * | |
488 | * @type type of the event to create | |
489 | * @notify_tpl task priority level of the event | |
490 | * @notify_function notification function of the event | |
491 | * @notify_context pointer passed to the notification function | |
492 | * @event created event | |
493 | * @return status code | |
494 | */ | |
152cade3 | 495 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
49deb455 | 496 | void (EFIAPI *notify_function) ( |
2fd945fe HS |
497 | struct efi_event *event, |
498 | void *context), | |
49deb455 | 499 | void *notify_context, struct efi_event **event) |
bee91169 | 500 | { |
c6841592 HS |
501 | int i; |
502 | ||
a95343b8 | 503 | if (event == NULL) |
49deb455 | 504 | return EFI_INVALID_PARAMETER; |
a95343b8 JG |
505 | |
506 | if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT)) | |
49deb455 | 507 | return EFI_INVALID_PARAMETER; |
a95343b8 JG |
508 | |
509 | if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) && | |
510 | notify_function == NULL) | |
49deb455 | 511 | return EFI_INVALID_PARAMETER; |
a95343b8 | 512 | |
c6841592 HS |
513 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
514 | if (efi_events[i].type) | |
515 | continue; | |
516 | efi_events[i].type = type; | |
517 | efi_events[i].notify_tpl = notify_tpl; | |
518 | efi_events[i].notify_function = notify_function; | |
519 | efi_events[i].notify_context = notify_context; | |
520 | /* Disable timers on bootup */ | |
521 | efi_events[i].trigger_next = -1ULL; | |
e190e897 HS |
522 | efi_events[i].is_queued = false; |
523 | efi_events[i].is_signaled = false; | |
c6841592 | 524 | *event = &efi_events[i]; |
49deb455 | 525 | return EFI_SUCCESS; |
c6841592 | 526 | } |
49deb455 | 527 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
528 | } |
529 | ||
9f0930e5 HS |
530 | /* |
531 | * Create an event in a group. | |
532 | * | |
533 | * This function implements the CreateEventEx service. | |
534 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
535 | * for details. | |
536 | * TODO: Support event groups | |
537 | * | |
538 | * @type type of the event to create | |
539 | * @notify_tpl task priority level of the event | |
540 | * @notify_function notification function of the event | |
541 | * @notify_context pointer passed to the notification function | |
542 | * @event created event | |
543 | * @event_group event group | |
544 | * @return status code | |
545 | */ | |
546 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, | |
547 | void (EFIAPI *notify_function) ( | |
548 | struct efi_event *event, | |
549 | void *context), | |
550 | void *notify_context, | |
551 | efi_guid_t *event_group, | |
552 | struct efi_event **event) | |
553 | { | |
554 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function, | |
555 | notify_context, event_group); | |
556 | if (event_group) | |
557 | return EFI_EXIT(EFI_UNSUPPORTED); | |
558 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
559 | notify_context, event)); | |
560 | } | |
561 | ||
332468f7 HS |
562 | /* |
563 | * Create an event. | |
564 | * | |
565 | * This function implements the CreateEvent service. | |
566 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
567 | * for details. | |
568 | * | |
569 | * @type type of the event to create | |
570 | * @notify_tpl task priority level of the event | |
571 | * @notify_function notification function of the event | |
572 | * @notify_context pointer passed to the notification function | |
573 | * @event created event | |
574 | * @return status code | |
575 | */ | |
49deb455 | 576 | static efi_status_t EFIAPI efi_create_event_ext( |
152cade3 | 577 | uint32_t type, efi_uintn_t notify_tpl, |
49deb455 HS |
578 | void (EFIAPI *notify_function) ( |
579 | struct efi_event *event, | |
580 | void *context), | |
581 | void *notify_context, struct efi_event **event) | |
582 | { | |
583 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, | |
584 | notify_context); | |
585 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
586 | notify_context, event)); | |
587 | } | |
588 | ||
589 | ||
bee91169 | 590 | /* |
332468f7 HS |
591 | * Check if a timer event has occurred or a queued notification function should |
592 | * be called. | |
593 | * | |
bee91169 | 594 | * Our timers have to work without interrupts, so we check whenever keyboard |
332468f7 | 595 | * input or disk accesses happen if enough time elapsed for them to fire. |
bee91169 AG |
596 | */ |
597 | void efi_timer_check(void) | |
598 | { | |
c6841592 | 599 | int i; |
bee91169 AG |
600 | u64 now = timer_get_us(); |
601 | ||
c6841592 | 602 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
ca62a4f5 HS |
603 | if (!efi_events[i].type) |
604 | continue; | |
e190e897 | 605 | if (efi_events[i].is_queued) |
9bc9664d | 606 | efi_signal_event(&efi_events[i], true); |
ca62a4f5 | 607 | if (!(efi_events[i].type & EVT_TIMER) || |
c6841592 HS |
608 | now < efi_events[i].trigger_next) |
609 | continue; | |
ca62a4f5 HS |
610 | switch (efi_events[i].trigger_type) { |
611 | case EFI_TIMER_RELATIVE: | |
612 | efi_events[i].trigger_type = EFI_TIMER_STOP; | |
613 | break; | |
614 | case EFI_TIMER_PERIODIC: | |
c6841592 | 615 | efi_events[i].trigger_next += |
8787b02e | 616 | efi_events[i].trigger_time; |
ca62a4f5 HS |
617 | break; |
618 | default: | |
619 | continue; | |
c6841592 | 620 | } |
e190e897 | 621 | efi_events[i].is_signaled = true; |
9bc9664d | 622 | efi_signal_event(&efi_events[i], true); |
bee91169 | 623 | } |
bee91169 AG |
624 | WATCHDOG_RESET(); |
625 | } | |
626 | ||
332468f7 HS |
627 | /* |
628 | * Set the trigger time for a timer event or stop the event. | |
629 | * | |
630 | * This is the function for internal usage in U-Boot. For the API function | |
631 | * implementing the SetTimer service see efi_set_timer_ext. | |
632 | * | |
633 | * @event event for which the timer is set | |
634 | * @type type of the timer | |
635 | * @trigger_time trigger period in multiples of 100ns | |
636 | * @return status code | |
637 | */ | |
b521d29e | 638 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
bfc72462 | 639 | uint64_t trigger_time) |
bee91169 | 640 | { |
c6841592 | 641 | int i; |
bee91169 | 642 | |
8787b02e HS |
643 | /* |
644 | * The parameter defines a multiple of 100ns. | |
645 | * We use multiples of 1000ns. So divide by 10. | |
646 | */ | |
7d963323 | 647 | do_div(trigger_time, 10); |
bee91169 | 648 | |
c6841592 HS |
649 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
650 | if (event != &efi_events[i]) | |
651 | continue; | |
bee91169 | 652 | |
c6841592 HS |
653 | if (!(event->type & EVT_TIMER)) |
654 | break; | |
655 | switch (type) { | |
656 | case EFI_TIMER_STOP: | |
657 | event->trigger_next = -1ULL; | |
658 | break; | |
659 | case EFI_TIMER_PERIODIC: | |
660 | case EFI_TIMER_RELATIVE: | |
661 | event->trigger_next = | |
8787b02e | 662 | timer_get_us() + trigger_time; |
c6841592 HS |
663 | break; |
664 | default: | |
bfc72462 | 665 | return EFI_INVALID_PARAMETER; |
c6841592 HS |
666 | } |
667 | event->trigger_type = type; | |
668 | event->trigger_time = trigger_time; | |
e190e897 | 669 | event->is_signaled = false; |
bfc72462 | 670 | return EFI_SUCCESS; |
bee91169 | 671 | } |
bfc72462 HS |
672 | return EFI_INVALID_PARAMETER; |
673 | } | |
674 | ||
332468f7 HS |
675 | /* |
676 | * Set the trigger time for a timer event or stop the event. | |
677 | * | |
678 | * This function implements the SetTimer service. | |
679 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
680 | * for details. | |
681 | * | |
682 | * @event event for which the timer is set | |
683 | * @type type of the timer | |
684 | * @trigger_time trigger period in multiples of 100ns | |
685 | * @return status code | |
686 | */ | |
b521d29e HS |
687 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
688 | enum efi_timer_delay type, | |
689 | uint64_t trigger_time) | |
bfc72462 HS |
690 | { |
691 | EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); | |
692 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); | |
bee91169 AG |
693 | } |
694 | ||
332468f7 HS |
695 | /* |
696 | * Wait for events to be signaled. | |
697 | * | |
698 | * This function implements the WaitForEvent service. | |
699 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
700 | * for details. | |
701 | * | |
702 | * @num_events number of events to be waited for | |
703 | * @events events to be waited for | |
704 | * @index index of the event that was signaled | |
705 | * @return status code | |
706 | */ | |
f5a2a938 | 707 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
2fd945fe | 708 | struct efi_event **event, |
f5a2a938 | 709 | efi_uintn_t *index) |
bee91169 | 710 | { |
c6841592 | 711 | int i, j; |
bee91169 | 712 | |
f5a2a938 | 713 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
bee91169 | 714 | |
c6841592 HS |
715 | /* Check parameters */ |
716 | if (!num_events || !event) | |
717 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
32f4b2f8 HS |
718 | /* Check TPL */ |
719 | if (efi_tpl != TPL_APPLICATION) | |
720 | return EFI_EXIT(EFI_UNSUPPORTED); | |
c6841592 HS |
721 | for (i = 0; i < num_events; ++i) { |
722 | for (j = 0; j < ARRAY_SIZE(efi_events); ++j) { | |
723 | if (event[i] == &efi_events[j]) | |
724 | goto known_event; | |
725 | } | |
726 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
727 | known_event: | |
728 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) | |
729 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
e190e897 | 730 | if (!event[i]->is_signaled) |
9bc9664d | 731 | efi_signal_event(event[i], true); |
c6841592 HS |
732 | } |
733 | ||
734 | /* Wait for signal */ | |
735 | for (;;) { | |
736 | for (i = 0; i < num_events; ++i) { | |
e190e897 | 737 | if (event[i]->is_signaled) |
c6841592 HS |
738 | goto out; |
739 | } | |
740 | /* Allow events to occur. */ | |
741 | efi_timer_check(); | |
742 | } | |
743 | ||
744 | out: | |
745 | /* | |
746 | * Reset the signal which is passed to the caller to allow periodic | |
747 | * events to occur. | |
748 | */ | |
e190e897 | 749 | event[i]->is_signaled = false; |
c6841592 HS |
750 | if (index) |
751 | *index = i; | |
bee91169 AG |
752 | |
753 | return EFI_EXIT(EFI_SUCCESS); | |
754 | } | |
755 | ||
332468f7 HS |
756 | /* |
757 | * Signal an EFI event. | |
758 | * | |
759 | * This function implements the SignalEvent service. | |
760 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
761 | * for details. | |
762 | * | |
763 | * This functions sets the signaled state of the event and queues the | |
764 | * notification function for execution. | |
765 | * | |
766 | * @event event to signal | |
10a08c4e | 767 | * @return status code |
332468f7 | 768 | */ |
c6841592 | 769 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
bee91169 | 770 | { |
c6841592 HS |
771 | int i; |
772 | ||
bee91169 | 773 | EFI_ENTRY("%p", event); |
c6841592 HS |
774 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
775 | if (event != &efi_events[i]) | |
776 | continue; | |
e190e897 | 777 | if (event->is_signaled) |
ca62a4f5 | 778 | break; |
e190e897 | 779 | event->is_signaled = true; |
ca62a4f5 | 780 | if (event->type & EVT_NOTIFY_SIGNAL) |
9bc9664d | 781 | efi_signal_event(event, true); |
c6841592 HS |
782 | break; |
783 | } | |
bee91169 AG |
784 | return EFI_EXIT(EFI_SUCCESS); |
785 | } | |
786 | ||
332468f7 HS |
787 | /* |
788 | * Close an EFI event. | |
789 | * | |
790 | * This function implements the CloseEvent service. | |
791 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
792 | * for details. | |
793 | * | |
794 | * @event event to close | |
795 | * @return status code | |
796 | */ | |
2fd945fe | 797 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
bee91169 | 798 | { |
c6841592 HS |
799 | int i; |
800 | ||
bee91169 | 801 | EFI_ENTRY("%p", event); |
c6841592 HS |
802 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
803 | if (event == &efi_events[i]) { | |
804 | event->type = 0; | |
805 | event->trigger_next = -1ULL; | |
e190e897 HS |
806 | event->is_queued = false; |
807 | event->is_signaled = false; | |
c6841592 HS |
808 | return EFI_EXIT(EFI_SUCCESS); |
809 | } | |
810 | } | |
811 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
812 | } |
813 | ||
332468f7 HS |
814 | /* |
815 | * Check if an event is signaled. | |
816 | * | |
817 | * This function implements the CheckEvent service. | |
818 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
819 | * for details. | |
820 | * | |
7069515e HS |
821 | * If an event is not signaled yet, the notification function is queued. |
822 | * The signaled state is cleared. | |
332468f7 HS |
823 | * |
824 | * @event event to check | |
825 | * @return status code | |
826 | */ | |
2fd945fe | 827 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
bee91169 | 828 | { |
c6841592 HS |
829 | int i; |
830 | ||
bee91169 | 831 | EFI_ENTRY("%p", event); |
c6841592 HS |
832 | efi_timer_check(); |
833 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { | |
834 | if (event != &efi_events[i]) | |
835 | continue; | |
836 | if (!event->type || event->type & EVT_NOTIFY_SIGNAL) | |
837 | break; | |
e190e897 | 838 | if (!event->is_signaled) |
9bc9664d | 839 | efi_signal_event(event, true); |
7069515e HS |
840 | if (event->is_signaled) { |
841 | event->is_signaled = false; | |
c6841592 | 842 | return EFI_EXIT(EFI_SUCCESS); |
7069515e | 843 | } |
c6841592 HS |
844 | return EFI_EXIT(EFI_NOT_READY); |
845 | } | |
846 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
847 | } |
848 | ||
7b9f8ad7 HS |
849 | /* |
850 | * Find the internal EFI object for a handle. | |
851 | * | |
852 | * @handle handle to find | |
853 | * @return EFI object | |
854 | */ | |
2074f700 | 855 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
7b9f8ad7 | 856 | { |
1b68153a | 857 | struct efi_object *efiobj; |
7b9f8ad7 | 858 | |
1b68153a | 859 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
7b9f8ad7 HS |
860 | if (efiobj->handle == handle) |
861 | return efiobj; | |
862 | } | |
863 | ||
864 | return NULL; | |
865 | } | |
866 | ||
fe1599da HS |
867 | /* |
868 | * Create open protocol info entry and add it to a protocol. | |
869 | * | |
870 | * @handler handler of a protocol | |
871 | * @return open protocol info entry | |
872 | */ | |
873 | static struct efi_open_protocol_info_entry *efi_create_open_info( | |
874 | struct efi_handler *handler) | |
875 | { | |
876 | struct efi_open_protocol_info_item *item; | |
877 | ||
878 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); | |
879 | if (!item) | |
880 | return NULL; | |
881 | /* Append the item to the open protocol info list. */ | |
882 | list_add_tail(&item->link, &handler->open_infos); | |
883 | ||
884 | return &item->info; | |
885 | } | |
886 | ||
887 | /* | |
888 | * Remove an open protocol info entry from a protocol. | |
889 | * | |
890 | * @handler handler of a protocol | |
891 | * @return status code | |
892 | */ | |
893 | static efi_status_t efi_delete_open_info( | |
894 | struct efi_open_protocol_info_item *item) | |
895 | { | |
896 | list_del(&item->link); | |
897 | free(item); | |
898 | return EFI_SUCCESS; | |
899 | } | |
900 | ||
3f79a2b5 HS |
901 | /* |
902 | * Install new protocol on a handle. | |
903 | * | |
904 | * @handle handle on which the protocol shall be installed | |
905 | * @protocol GUID of the protocol to be installed | |
906 | * @protocol_interface interface of the protocol implementation | |
907 | * @return status code | |
908 | */ | |
2074f700 HS |
909 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
910 | const efi_guid_t *protocol, | |
3f79a2b5 HS |
911 | void *protocol_interface) |
912 | { | |
913 | struct efi_object *efiobj; | |
914 | struct efi_handler *handler; | |
915 | efi_status_t ret; | |
3f79a2b5 HS |
916 | |
917 | efiobj = efi_search_obj(handle); | |
918 | if (!efiobj) | |
919 | return EFI_INVALID_PARAMETER; | |
920 | ret = efi_search_protocol(handle, protocol, NULL); | |
921 | if (ret != EFI_NOT_FOUND) | |
922 | return EFI_INVALID_PARAMETER; | |
923 | handler = calloc(1, sizeof(struct efi_handler)); | |
924 | if (!handler) | |
925 | return EFI_OUT_OF_RESOURCES; | |
69fb6b1a HS |
926 | handler->guid = protocol; |
927 | handler->protocol_interface = protocol_interface; | |
fe1599da | 928 | INIT_LIST_HEAD(&handler->open_infos); |
69fb6b1a | 929 | list_add_tail(&handler->link, &efiobj->protocols); |
d5504144 HS |
930 | if (!guidcmp(&efi_guid_device_path, protocol)) |
931 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); | |
69fb6b1a | 932 | return EFI_SUCCESS; |
3f79a2b5 HS |
933 | } |
934 | ||
332468f7 HS |
935 | /* |
936 | * Install protocol interface. | |
937 | * | |
1760ef57 HS |
938 | * This function implements the InstallProtocolInterface service. |
939 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
940 | * for details. | |
332468f7 HS |
941 | * |
942 | * @handle handle on which the protocol shall be installed | |
943 | * @protocol GUID of the protocol to be installed | |
944 | * @protocol_interface_type type of the interface to be installed, | |
945 | * always EFI_NATIVE_INTERFACE | |
946 | * @protocol_interface interface of the protocol implementation | |
947 | * @return status code | |
948 | */ | |
1760ef57 HS |
949 | static efi_status_t EFIAPI efi_install_protocol_interface( |
950 | void **handle, const efi_guid_t *protocol, | |
951 | int protocol_interface_type, void *protocol_interface) | |
bee91169 | 952 | { |
e0549f8a HS |
953 | efi_status_t r; |
954 | ||
1760ef57 HS |
955 | EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type, |
956 | protocol_interface); | |
957 | ||
e0549f8a HS |
958 | if (!handle || !protocol || |
959 | protocol_interface_type != EFI_NATIVE_INTERFACE) { | |
960 | r = EFI_INVALID_PARAMETER; | |
961 | goto out; | |
962 | } | |
963 | ||
964 | /* Create new handle if requested. */ | |
965 | if (!*handle) { | |
3cc6e3fe HS |
966 | r = efi_create_handle(handle); |
967 | if (r != EFI_SUCCESS) | |
968 | goto out; | |
af1408e0 HS |
969 | debug("%sEFI: new handle %p\n", indent_string(nesting_level), |
970 | *handle); | |
971 | } else { | |
972 | debug("%sEFI: handle %p\n", indent_string(nesting_level), | |
973 | *handle); | |
e0549f8a | 974 | } |
1202530d HS |
975 | /* Add new protocol */ |
976 | r = efi_add_protocol(*handle, protocol, protocol_interface); | |
e0549f8a | 977 | out: |
1760ef57 | 978 | return EFI_EXIT(r); |
bee91169 | 979 | } |
e0549f8a | 980 | |
332468f7 HS |
981 | /* |
982 | * Reinstall protocol interface. | |
983 | * | |
984 | * This function implements the ReinstallProtocolInterface service. | |
985 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
986 | * for details. | |
987 | * | |
988 | * @handle handle on which the protocol shall be | |
989 | * reinstalled | |
990 | * @protocol GUID of the protocol to be installed | |
991 | * @old_interface interface to be removed | |
992 | * @new_interface interface to be installed | |
993 | * @return status code | |
994 | */ | |
2074f700 HS |
995 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( |
996 | efi_handle_t handle, const efi_guid_t *protocol, | |
997 | void *old_interface, void *new_interface) | |
bee91169 | 998 | { |
778e6af8 | 999 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface, |
bee91169 AG |
1000 | new_interface); |
1001 | return EFI_EXIT(EFI_ACCESS_DENIED); | |
1002 | } | |
1003 | ||
3f9b0042 HS |
1004 | /* |
1005 | * Get all drivers associated to a controller. | |
1006 | * The allocated buffer has to be freed with free(). | |
1007 | * | |
1008 | * @efiobj handle of the controller | |
1009 | * @protocol protocol guid (optional) | |
1010 | * @number_of_drivers number of child controllers | |
1011 | * @driver_handle_buffer handles of the the drivers | |
1012 | * @return status code | |
1013 | */ | |
1014 | static efi_status_t efi_get_drivers(struct efi_object *efiobj, | |
1015 | const efi_guid_t *protocol, | |
1016 | efi_uintn_t *number_of_drivers, | |
1017 | efi_handle_t **driver_handle_buffer) | |
1018 | { | |
1019 | struct efi_handler *handler; | |
1020 | struct efi_open_protocol_info_item *item; | |
1021 | efi_uintn_t count = 0, i; | |
1022 | bool duplicate; | |
1023 | ||
1024 | /* Count all driver associations */ | |
1025 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
1026 | if (protocol && guidcmp(handler->guid, protocol)) | |
1027 | continue; | |
1028 | list_for_each_entry(item, &handler->open_infos, link) { | |
1029 | if (item->info.attributes & | |
1030 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
1031 | ++count; | |
1032 | } | |
1033 | } | |
1034 | /* | |
1035 | * Create buffer. In case of duplicate driver assignments the buffer | |
1036 | * will be too large. But that does not harm. | |
1037 | */ | |
1038 | *number_of_drivers = 0; | |
1039 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); | |
1040 | if (!*driver_handle_buffer) | |
1041 | return EFI_OUT_OF_RESOURCES; | |
1042 | /* Collect unique driver handles */ | |
1043 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
1044 | if (protocol && guidcmp(handler->guid, protocol)) | |
1045 | continue; | |
1046 | list_for_each_entry(item, &handler->open_infos, link) { | |
1047 | if (item->info.attributes & | |
1048 | EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
1049 | /* Check this is a new driver */ | |
1050 | duplicate = false; | |
1051 | for (i = 0; i < *number_of_drivers; ++i) { | |
1052 | if ((*driver_handle_buffer)[i] == | |
1053 | item->info.agent_handle) | |
1054 | duplicate = true; | |
1055 | } | |
1056 | /* Copy handle to buffer */ | |
1057 | if (!duplicate) { | |
1058 | i = (*number_of_drivers)++; | |
1059 | (*driver_handle_buffer)[i] = | |
1060 | item->info.agent_handle; | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | } | |
1065 | return EFI_SUCCESS; | |
1066 | } | |
1067 | ||
1068 | /* | |
1069 | * Disconnect all drivers from a controller. | |
1070 | * | |
1071 | * This function implements the DisconnectController service. | |
1072 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1073 | * for details. | |
1074 | * | |
1075 | * @efiobj handle of the controller | |
1076 | * @protocol protocol guid (optional) | |
1077 | * @child_handle handle of the child to destroy | |
1078 | * @return status code | |
1079 | */ | |
1080 | static efi_status_t efi_disconnect_all_drivers( | |
1081 | struct efi_object *efiobj, | |
1082 | const efi_guid_t *protocol, | |
1083 | efi_handle_t child_handle) | |
1084 | { | |
1085 | efi_uintn_t number_of_drivers; | |
1086 | efi_handle_t *driver_handle_buffer; | |
1087 | efi_status_t r, ret; | |
1088 | ||
1089 | ret = efi_get_drivers(efiobj, protocol, &number_of_drivers, | |
1090 | &driver_handle_buffer); | |
1091 | if (ret != EFI_SUCCESS) | |
1092 | return ret; | |
1093 | ||
1094 | ret = EFI_NOT_FOUND; | |
1095 | while (number_of_drivers) { | |
1096 | r = EFI_CALL(efi_disconnect_controller( | |
1097 | efiobj->handle, | |
1098 | driver_handle_buffer[--number_of_drivers], | |
1099 | child_handle)); | |
1100 | if (r == EFI_SUCCESS) | |
1101 | ret = r; | |
1102 | } | |
1103 | free(driver_handle_buffer); | |
1104 | return ret; | |
1105 | } | |
1106 | ||
332468f7 HS |
1107 | /* |
1108 | * Uninstall protocol interface. | |
1109 | * | |
cd534083 HS |
1110 | * This function implements the UninstallProtocolInterface service. |
1111 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1112 | * for details. | |
332468f7 HS |
1113 | * |
1114 | * @handle handle from which the protocol shall be removed | |
1115 | * @protocol GUID of the protocol to be removed | |
1116 | * @protocol_interface interface to be removed | |
1117 | * @return status code | |
1118 | */ | |
cd534083 | 1119 | static efi_status_t EFIAPI efi_uninstall_protocol_interface( |
2074f700 | 1120 | efi_handle_t handle, const efi_guid_t *protocol, |
cd534083 | 1121 | void *protocol_interface) |
bee91169 | 1122 | { |
ad97373b | 1123 | struct efi_object *efiobj; |
5810511d | 1124 | struct efi_handler *handler; |
ad97373b HS |
1125 | struct efi_open_protocol_info_item *item; |
1126 | struct efi_open_protocol_info_item *pos; | |
5810511d | 1127 | efi_status_t r; |
4b6ed0d7 | 1128 | |
cd534083 HS |
1129 | EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface); |
1130 | ||
ad97373b HS |
1131 | /* Check handle */ |
1132 | efiobj = efi_search_obj(handle); | |
1133 | if (!efiobj) { | |
4b6ed0d7 HS |
1134 | r = EFI_INVALID_PARAMETER; |
1135 | goto out; | |
1136 | } | |
5810511d HS |
1137 | /* Find the protocol on the handle */ |
1138 | r = efi_search_protocol(handle, protocol, &handler); | |
1139 | if (r != EFI_SUCCESS) | |
1140 | goto out; | |
ad97373b HS |
1141 | /* Disconnect controllers */ |
1142 | efi_disconnect_all_drivers(efiobj, protocol, NULL); | |
1143 | if (!list_empty(&handler->open_infos)) { | |
5810511d | 1144 | r = EFI_ACCESS_DENIED; |
ad97373b HS |
1145 | goto out; |
1146 | } | |
1147 | /* Close protocol */ | |
1148 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1149 | if (item->info.attributes == | |
1150 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || | |
1151 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || | |
1152 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
1153 | list_del(&item->link); | |
1154 | } | |
1155 | if (!list_empty(&handler->open_infos)) { | |
1156 | r = EFI_ACCESS_DENIED; | |
1157 | goto out; | |
4b6ed0d7 | 1158 | } |
ad97373b | 1159 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
4b6ed0d7 | 1160 | out: |
cd534083 | 1161 | return EFI_EXIT(r); |
bee91169 AG |
1162 | } |
1163 | ||
332468f7 HS |
1164 | /* |
1165 | * Register an event for notification when a protocol is installed. | |
1166 | * | |
1167 | * This function implements the RegisterProtocolNotify service. | |
1168 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1169 | * for details. | |
1170 | * | |
1171 | * @protocol GUID of the protocol whose installation shall be | |
1172 | * notified | |
1173 | * @event event to be signaled upon installation of the protocol | |
1174 | * @registration key for retrieving the registration information | |
1175 | * @return status code | |
1176 | */ | |
5a9682d0 HS |
1177 | static efi_status_t EFIAPI efi_register_protocol_notify( |
1178 | const efi_guid_t *protocol, | |
1179 | struct efi_event *event, | |
1180 | void **registration) | |
bee91169 | 1181 | { |
778e6af8 | 1182 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
bee91169 AG |
1183 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
1184 | } | |
1185 | ||
332468f7 HS |
1186 | /* |
1187 | * Determine if an EFI handle implements a protocol. | |
1188 | * | |
1189 | * See the documentation of the LocateHandle service in the UEFI specification. | |
1190 | * | |
1191 | * @search_type selection criterion | |
1192 | * @protocol GUID of the protocol | |
1193 | * @search_key registration key | |
1194 | * @efiobj handle | |
1195 | * @return 0 if the handle implements the protocol | |
1196 | */ | |
bee91169 | 1197 | static int efi_search(enum efi_locate_search_type search_type, |
5a9682d0 | 1198 | const efi_guid_t *protocol, void *search_key, |
bee91169 AG |
1199 | struct efi_object *efiobj) |
1200 | { | |
42cf1242 | 1201 | efi_status_t ret; |
bee91169 AG |
1202 | |
1203 | switch (search_type) { | |
9f0770ff | 1204 | case ALL_HANDLES: |
bee91169 | 1205 | return 0; |
9f0770ff | 1206 | case BY_REGISTER_NOTIFY: |
42cf1242 | 1207 | /* TODO: RegisterProtocolNotify is not implemented yet */ |
bee91169 | 1208 | return -1; |
9f0770ff | 1209 | case BY_PROTOCOL: |
42cf1242 HS |
1210 | ret = efi_search_protocol(efiobj->handle, protocol, NULL); |
1211 | return (ret != EFI_SUCCESS); | |
1212 | default: | |
1213 | /* Invalid search type */ | |
bee91169 AG |
1214 | return -1; |
1215 | } | |
bee91169 AG |
1216 | } |
1217 | ||
332468f7 HS |
1218 | /* |
1219 | * Locate handles implementing a protocol. | |
1220 | * | |
1221 | * This function is meant for U-Boot internal calls. For the API implementation | |
1222 | * of the LocateHandle service see efi_locate_handle_ext. | |
1223 | * | |
1224 | * @search_type selection criterion | |
1225 | * @protocol GUID of the protocol | |
1226 | * @search_key registration key | |
1227 | * @buffer_size size of the buffer to receive the handles in bytes | |
1228 | * @buffer buffer to receive the relevant handles | |
1229 | * @return status code | |
1230 | */ | |
ebf199b9 | 1231 | static efi_status_t efi_locate_handle( |
bee91169 | 1232 | enum efi_locate_search_type search_type, |
5a9682d0 | 1233 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1234 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
bee91169 | 1235 | { |
caf864e4 | 1236 | struct efi_object *efiobj; |
f5a2a938 | 1237 | efi_uintn_t size = 0; |
bee91169 | 1238 | |
caf864e4 HS |
1239 | /* Check parameters */ |
1240 | switch (search_type) { | |
1241 | case ALL_HANDLES: | |
1242 | break; | |
1243 | case BY_REGISTER_NOTIFY: | |
1244 | if (!search_key) | |
1245 | return EFI_INVALID_PARAMETER; | |
1246 | /* RegisterProtocolNotify is not implemented yet */ | |
1247 | return EFI_UNSUPPORTED; | |
1248 | case BY_PROTOCOL: | |
1249 | if (!protocol) | |
1250 | return EFI_INVALID_PARAMETER; | |
1251 | break; | |
1252 | default: | |
1253 | return EFI_INVALID_PARAMETER; | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * efi_locate_handle_buffer uses this function for | |
1258 | * the calculation of the necessary buffer size. | |
1259 | * So do not require a buffer for buffersize == 0. | |
1260 | */ | |
1261 | if (!buffer_size || (*buffer_size && !buffer)) | |
1262 | return EFI_INVALID_PARAMETER; | |
1263 | ||
bee91169 | 1264 | /* Count how much space we need */ |
caf864e4 HS |
1265 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
1266 | if (!efi_search(search_type, protocol, search_key, efiobj)) | |
bee91169 | 1267 | size += sizeof(void*); |
bee91169 AG |
1268 | } |
1269 | ||
1270 | if (*buffer_size < size) { | |
1271 | *buffer_size = size; | |
26329584 | 1272 | return EFI_BUFFER_TOO_SMALL; |
bee91169 AG |
1273 | } |
1274 | ||
796a78cb RC |
1275 | *buffer_size = size; |
1276 | if (size == 0) | |
1277 | return EFI_NOT_FOUND; | |
1278 | ||
bee91169 | 1279 | /* Then fill the array */ |
caf864e4 HS |
1280 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
1281 | if (!efi_search(search_type, protocol, search_key, efiobj)) | |
1282 | *buffer++ = efiobj->handle; | |
bee91169 AG |
1283 | } |
1284 | ||
26329584 HS |
1285 | return EFI_SUCCESS; |
1286 | } | |
1287 | ||
332468f7 HS |
1288 | /* |
1289 | * Locate handles implementing a protocol. | |
1290 | * | |
1291 | * This function implements the LocateHandle service. | |
1292 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1293 | * for details. | |
1294 | * | |
1295 | * @search_type selection criterion | |
1296 | * @protocol GUID of the protocol | |
1297 | * @search_key registration key | |
1298 | * @buffer_size size of the buffer to receive the handles in bytes | |
1299 | * @buffer buffer to receive the relevant handles | |
1300 | * @return 0 if the handle implements the protocol | |
1301 | */ | |
26329584 HS |
1302 | static efi_status_t EFIAPI efi_locate_handle_ext( |
1303 | enum efi_locate_search_type search_type, | |
5a9682d0 | 1304 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1305 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
26329584 | 1306 | { |
778e6af8 | 1307 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
26329584 HS |
1308 | buffer_size, buffer); |
1309 | ||
1310 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, | |
1311 | buffer_size, buffer)); | |
bee91169 AG |
1312 | } |
1313 | ||
d98cdf6a AG |
1314 | /* Collapses configuration table entries, removing index i */ |
1315 | static void efi_remove_configuration_table(int i) | |
1316 | { | |
1317 | struct efi_configuration_table *this = &efi_conf_table[i]; | |
1318 | struct efi_configuration_table *next = &efi_conf_table[i+1]; | |
1319 | struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables]; | |
1320 | ||
1321 | memmove(this, next, (ulong)end - (ulong)next); | |
1322 | systab.nr_tables--; | |
1323 | } | |
1324 | ||
332468f7 HS |
1325 | /* |
1326 | * Adds, updates, or removes a configuration table. | |
1327 | * | |
1328 | * This function is used for internal calls. For the API implementation of the | |
1329 | * InstallConfigurationTable service see efi_install_configuration_table_ext. | |
1330 | * | |
1331 | * @guid GUID of the installed table | |
1332 | * @table table to be installed | |
1333 | * @return status code | |
1334 | */ | |
488bf12d | 1335 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table) |
bee91169 AG |
1336 | { |
1337 | int i; | |
1338 | ||
eb68b4ef HS |
1339 | if (!guid) |
1340 | return EFI_INVALID_PARAMETER; | |
1341 | ||
bee91169 AG |
1342 | /* Check for guid override */ |
1343 | for (i = 0; i < systab.nr_tables; i++) { | |
1344 | if (!guidcmp(guid, &efi_conf_table[i].guid)) { | |
d98cdf6a AG |
1345 | if (table) |
1346 | efi_conf_table[i].table = table; | |
1347 | else | |
1348 | efi_remove_configuration_table(i); | |
488bf12d | 1349 | return EFI_SUCCESS; |
bee91169 AG |
1350 | } |
1351 | } | |
1352 | ||
d98cdf6a AG |
1353 | if (!table) |
1354 | return EFI_NOT_FOUND; | |
1355 | ||
bee91169 AG |
1356 | /* No override, check for overflow */ |
1357 | if (i >= ARRAY_SIZE(efi_conf_table)) | |
488bf12d | 1358 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
1359 | |
1360 | /* Add a new entry */ | |
1361 | memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); | |
1362 | efi_conf_table[i].table = table; | |
aba5e919 | 1363 | systab.nr_tables = i + 1; |
bee91169 | 1364 | |
488bf12d AG |
1365 | return EFI_SUCCESS; |
1366 | } | |
1367 | ||
332468f7 HS |
1368 | /* |
1369 | * Adds, updates, or removes a configuration table. | |
1370 | * | |
1371 | * This function implements the InstallConfigurationTable service. | |
1372 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1373 | * for details. | |
1374 | * | |
1375 | * @guid GUID of the installed table | |
1376 | * @table table to be installed | |
1377 | * @return status code | |
1378 | */ | |
488bf12d AG |
1379 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
1380 | void *table) | |
1381 | { | |
778e6af8 | 1382 | EFI_ENTRY("%pUl, %p", guid, table); |
488bf12d | 1383 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
bee91169 AG |
1384 | } |
1385 | ||
332468f7 HS |
1386 | /* |
1387 | * Initialize a loaded_image_info + loaded_image_info object with correct | |
95c5553e | 1388 | * protocols, boot-device, etc. |
332468f7 | 1389 | * |
10a08c4e | 1390 | * @info loaded image info to be passed to the entry point of the |
332468f7 HS |
1391 | * image |
1392 | * @obj internal object associated with the loaded image | |
1393 | * @device_path device path of the loaded image | |
1394 | * @file_path file path of the loaded image | |
56d92888 | 1395 | * @return status code |
95c5553e | 1396 | */ |
56d92888 HS |
1397 | efi_status_t efi_setup_loaded_image( |
1398 | struct efi_loaded_image *info, struct efi_object *obj, | |
1399 | struct efi_device_path *device_path, | |
1400 | struct efi_device_path *file_path) | |
95c5553e | 1401 | { |
48b66230 HS |
1402 | efi_status_t ret; |
1403 | ||
44549d62 HS |
1404 | /* Add internal object to object list */ |
1405 | efi_add_handle(obj); | |
1406 | /* efi_exit() assumes that the handle points to the info */ | |
95c5553e RC |
1407 | obj->handle = info; |
1408 | ||
48b66230 | 1409 | info->file_path = file_path; |
48b66230 | 1410 | |
7df5af6f HS |
1411 | if (device_path) { |
1412 | info->device_handle = efi_dp_find_obj(device_path, NULL); | |
1413 | /* | |
1414 | * When asking for the device path interface, return | |
1415 | * bootefi_device_path | |
1416 | */ | |
1417 | ret = efi_add_protocol(obj->handle, &efi_guid_device_path, | |
1418 | device_path); | |
1419 | if (ret != EFI_SUCCESS) | |
1420 | goto failure; | |
1421 | } | |
95c5553e RC |
1422 | |
1423 | /* | |
1424 | * When asking for the loaded_image interface, just | |
1425 | * return handle which points to loaded_image_info | |
1426 | */ | |
48b66230 HS |
1427 | ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info); |
1428 | if (ret != EFI_SUCCESS) | |
1429 | goto failure; | |
95c5553e | 1430 | |
48b66230 HS |
1431 | ret = efi_add_protocol(obj->handle, |
1432 | &efi_guid_device_path_to_text_protocol, | |
1433 | (void *)&efi_device_path_to_text); | |
1434 | if (ret != EFI_SUCCESS) | |
1435 | goto failure; | |
95c5553e | 1436 | |
e70f8dfa LL |
1437 | ret = efi_add_protocol(obj->handle, |
1438 | &efi_guid_device_path_utilities_protocol, | |
1439 | (void *)&efi_device_path_utilities); | |
1440 | if (ret != EFI_SUCCESS) | |
1441 | goto failure; | |
1442 | ||
56d92888 | 1443 | return ret; |
48b66230 HS |
1444 | failure: |
1445 | printf("ERROR: Failure to install protocols for loaded image\n"); | |
56d92888 | 1446 | return ret; |
95c5553e RC |
1447 | } |
1448 | ||
332468f7 HS |
1449 | /* |
1450 | * Load an image using a file path. | |
1451 | * | |
1452 | * @file_path the path of the image to load | |
1453 | * @buffer buffer containing the loaded image | |
10a08c4e | 1454 | * @return status code |
332468f7 | 1455 | */ |
9975fe96 RC |
1456 | efi_status_t efi_load_image_from_path(struct efi_device_path *file_path, |
1457 | void **buffer) | |
838ee4b4 RC |
1458 | { |
1459 | struct efi_file_info *info = NULL; | |
1460 | struct efi_file_handle *f; | |
1461 | static efi_status_t ret; | |
1462 | uint64_t bs; | |
1463 | ||
1464 | f = efi_file_from_path(file_path); | |
1465 | if (!f) | |
1466 | return EFI_DEVICE_ERROR; | |
1467 | ||
1468 | bs = 0; | |
1469 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, | |
1470 | &bs, info)); | |
1471 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
1472 | info = malloc(bs); | |
1473 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, | |
1474 | &bs, info)); | |
1475 | } | |
1476 | if (ret != EFI_SUCCESS) | |
1477 | goto error; | |
1478 | ||
1479 | ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer); | |
1480 | if (ret) | |
1481 | goto error; | |
1482 | ||
1483 | EFI_CALL(ret = f->read(f, &info->file_size, *buffer)); | |
1484 | ||
1485 | error: | |
1486 | free(info); | |
1487 | EFI_CALL(f->close(f)); | |
1488 | ||
1489 | if (ret != EFI_SUCCESS) { | |
1490 | efi_free_pool(*buffer); | |
1491 | *buffer = NULL; | |
1492 | } | |
1493 | ||
1494 | return ret; | |
1495 | } | |
1496 | ||
332468f7 HS |
1497 | /* |
1498 | * Load an EFI image into memory. | |
1499 | * | |
1500 | * This function implements the LoadImage service. | |
1501 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1502 | * for details. | |
1503 | * | |
1504 | * @boot_policy true for request originating from the boot manager | |
c8df80c5 | 1505 | * @parent_image the caller's image handle |
332468f7 HS |
1506 | * @file_path the path of the image to load |
1507 | * @source_buffer memory location from which the image is installed | |
1508 | * @source_size size of the memory area from which the image is | |
1509 | * installed | |
1510 | * @image_handle handle for the newly installed image | |
1511 | * @return status code | |
1512 | */ | |
bee91169 AG |
1513 | static efi_status_t EFIAPI efi_load_image(bool boot_policy, |
1514 | efi_handle_t parent_image, | |
1515 | struct efi_device_path *file_path, | |
1516 | void *source_buffer, | |
1517 | unsigned long source_size, | |
1518 | efi_handle_t *image_handle) | |
1519 | { | |
bee91169 AG |
1520 | struct efi_loaded_image *info; |
1521 | struct efi_object *obj; | |
b9b17598 | 1522 | efi_status_t ret; |
bee91169 | 1523 | |
476ed96e | 1524 | EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image, |
bee91169 | 1525 | file_path, source_buffer, source_size, image_handle); |
838ee4b4 | 1526 | |
28a4fd46 HS |
1527 | if (!image_handle || !parent_image) { |
1528 | ret = EFI_INVALID_PARAMETER; | |
1529 | goto error; | |
1530 | } | |
1531 | ||
1532 | if (!source_buffer && !file_path) { | |
1533 | ret = EFI_NOT_FOUND; | |
1534 | goto error; | |
1535 | } | |
1536 | ||
838ee4b4 | 1537 | info = calloc(1, sizeof(*info)); |
28a4fd46 HS |
1538 | if (!info) { |
1539 | ret = EFI_OUT_OF_RESOURCES; | |
1540 | goto error; | |
1541 | } | |
838ee4b4 | 1542 | obj = calloc(1, sizeof(*obj)); |
28a4fd46 HS |
1543 | if (!obj) { |
1544 | free(info); | |
1545 | ret = EFI_OUT_OF_RESOURCES; | |
1546 | goto error; | |
1547 | } | |
838ee4b4 RC |
1548 | |
1549 | if (!source_buffer) { | |
1550 | struct efi_device_path *dp, *fp; | |
838ee4b4 | 1551 | |
9975fe96 | 1552 | ret = efi_load_image_from_path(file_path, &source_buffer); |
b9b17598 HS |
1553 | if (ret != EFI_SUCCESS) |
1554 | goto failure; | |
838ee4b4 RC |
1555 | /* |
1556 | * split file_path which contains both the device and | |
1557 | * file parts: | |
1558 | */ | |
1559 | efi_dp_split_file_path(file_path, &dp, &fp); | |
b9b17598 HS |
1560 | ret = efi_setup_loaded_image(info, obj, dp, fp); |
1561 | if (ret != EFI_SUCCESS) | |
1562 | goto failure; | |
838ee4b4 RC |
1563 | } else { |
1564 | /* In this case, file_path is the "device" path, ie. | |
1565 | * something like a HARDWARE_DEVICE:MEMORY_MAPPED | |
1566 | */ | |
b9b17598 HS |
1567 | ret = efi_setup_loaded_image(info, obj, file_path, NULL); |
1568 | if (ret != EFI_SUCCESS) | |
1569 | goto failure; | |
838ee4b4 | 1570 | } |
bee91169 AG |
1571 | info->reserved = efi_load_pe(source_buffer, info); |
1572 | if (!info->reserved) { | |
b9b17598 HS |
1573 | ret = EFI_UNSUPPORTED; |
1574 | goto failure; | |
bee91169 | 1575 | } |
32fc2ac3 HS |
1576 | info->system_table = &systab; |
1577 | info->parent_handle = parent_image; | |
ea54ad59 | 1578 | *image_handle = obj->handle; |
bee91169 | 1579 | return EFI_EXIT(EFI_SUCCESS); |
b9b17598 HS |
1580 | failure: |
1581 | free(info); | |
1582 | efi_delete_handle(obj); | |
28a4fd46 | 1583 | error: |
b9b17598 | 1584 | return EFI_EXIT(ret); |
bee91169 AG |
1585 | } |
1586 | ||
332468f7 HS |
1587 | /* |
1588 | * Call the entry point of an image. | |
1589 | * | |
1590 | * This function implements the StartImage service. | |
1591 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1592 | * for details. | |
1593 | * | |
1594 | * @image_handle handle of the image | |
1595 | * @exit_data_size size of the buffer | |
1596 | * @exit_data buffer to receive the exit data of the called image | |
10a08c4e | 1597 | * @return status code |
332468f7 | 1598 | */ |
bee91169 AG |
1599 | static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, |
1600 | unsigned long *exit_data_size, | |
1601 | s16 **exit_data) | |
1602 | { | |
c6fa5df6 AG |
1603 | EFIAPI efi_status_t (*entry)(efi_handle_t image_handle, |
1604 | struct efi_system_table *st); | |
bee91169 | 1605 | struct efi_loaded_image *info = image_handle; |
727a1afb | 1606 | efi_status_t ret; |
bee91169 AG |
1607 | |
1608 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); | |
1609 | entry = info->reserved; | |
1610 | ||
1611 | efi_is_direct_boot = false; | |
1612 | ||
1613 | /* call the image! */ | |
a86aeaf2 | 1614 | if (setjmp(&info->exit_jmp)) { |
727a1afb HS |
1615 | /* |
1616 | * We called the entry point of the child image with EFI_CALL | |
1617 | * in the lines below. The child image called the Exit() boot | |
1618 | * service efi_exit() which executed the long jump that brought | |
1619 | * us to the current line. This implies that the second half | |
1620 | * of the EFI_CALL macro has not been executed. | |
1621 | */ | |
1622 | #ifdef CONFIG_ARM | |
1623 | /* | |
1624 | * efi_exit() called efi_restore_gd(). We have to undo this | |
1625 | * otherwise __efi_entry_check() will put the wrong value into | |
1626 | * app_gd. | |
1627 | */ | |
1628 | gd = app_gd; | |
1629 | #endif | |
1630 | /* | |
1631 | * To get ready to call EFI_EXIT below we have to execute the | |
1632 | * missed out steps of EFI_CALL. | |
1633 | */ | |
1634 | assert(__efi_entry_check()); | |
1635 | debug("%sEFI: %lu returned by started image\n", | |
1636 | __efi_nesting_dec(), | |
1637 | (unsigned long)((uintptr_t)info->exit_status & | |
1638 | ~EFI_ERROR_MASK)); | |
a86aeaf2 AG |
1639 | return EFI_EXIT(info->exit_status); |
1640 | } | |
1641 | ||
727a1afb | 1642 | ret = EFI_CALL(entry(image_handle, &systab)); |
bee91169 | 1643 | |
56672bf5 AG |
1644 | /* |
1645 | * Usually UEFI applications call Exit() instead of returning. | |
1646 | * But because the world doesn not consist of ponies and unicorns, | |
1647 | * we're happy to emulate that behavior on behalf of a payload | |
1648 | * that forgot. | |
1649 | */ | |
1650 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); | |
bee91169 AG |
1651 | } |
1652 | ||
332468f7 HS |
1653 | /* |
1654 | * Leave an EFI application or driver. | |
1655 | * | |
1656 | * This function implements the Exit service. | |
1657 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1658 | * for details. | |
1659 | * | |
1660 | * @image_handle handle of the application or driver that is exiting | |
1661 | * @exit_status status code | |
1662 | * @exit_data_size size of the buffer in bytes | |
1663 | * @exit_data buffer with data describing an error | |
10a08c4e | 1664 | * @return status code |
332468f7 | 1665 | */ |
a86aeaf2 AG |
1666 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
1667 | efi_status_t exit_status, unsigned long exit_data_size, | |
1668 | int16_t *exit_data) | |
bee91169 | 1669 | { |
44549d62 HS |
1670 | /* |
1671 | * We require that the handle points to the original loaded | |
1672 | * image protocol interface. | |
1673 | * | |
1674 | * For getting the longjmp address this is safer than locating | |
1675 | * the protocol because the protocol may have been reinstalled | |
1676 | * pointing to another memory location. | |
1677 | * | |
1678 | * TODO: We should call the unload procedure of the loaded | |
1679 | * image protocol. | |
1680 | */ | |
a86aeaf2 AG |
1681 | struct efi_loaded_image *loaded_image_info = (void*)image_handle; |
1682 | ||
bee91169 AG |
1683 | EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, |
1684 | exit_data_size, exit_data); | |
a86aeaf2 | 1685 | |
a148920e | 1686 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
727a1afb | 1687 | EFI_EXIT(exit_status); |
da94073b | 1688 | |
a148920e AG |
1689 | /* |
1690 | * But longjmp out with the U-Boot gd, not the application's, as | |
1691 | * the other end is a setjmp call inside EFI context. | |
1692 | */ | |
1693 | efi_restore_gd(); | |
1694 | ||
a86aeaf2 | 1695 | loaded_image_info->exit_status = exit_status; |
692fcdd8 | 1696 | longjmp(&loaded_image_info->exit_jmp, 1); |
a86aeaf2 AG |
1697 | |
1698 | panic("EFI application exited"); | |
bee91169 AG |
1699 | } |
1700 | ||
332468f7 HS |
1701 | /* |
1702 | * Unload an EFI image. | |
1703 | * | |
1704 | * This function implements the UnloadImage service. | |
1705 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1706 | * for details. | |
1707 | * | |
1708 | * @image_handle handle of the image to be unloaded | |
1709 | * @return status code | |
1710 | */ | |
2074f700 | 1711 | static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) |
bee91169 AG |
1712 | { |
1713 | struct efi_object *efiobj; | |
1714 | ||
1715 | EFI_ENTRY("%p", image_handle); | |
1716 | efiobj = efi_search_obj(image_handle); | |
1717 | if (efiobj) | |
1718 | list_del(&efiobj->link); | |
1719 | ||
1720 | return EFI_EXIT(EFI_SUCCESS); | |
1721 | } | |
1722 | ||
332468f7 HS |
1723 | /* |
1724 | * Fix up caches for EFI payloads if necessary. | |
1725 | */ | |
bee91169 AG |
1726 | static void efi_exit_caches(void) |
1727 | { | |
1728 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) | |
1729 | /* | |
1730 | * Grub on 32bit ARM needs to have caches disabled before jumping into | |
1731 | * a zImage, but does not know of all cache layers. Give it a hand. | |
1732 | */ | |
1733 | if (efi_is_direct_boot) | |
1734 | cleanup_before_linux(); | |
1735 | #endif | |
1736 | } | |
1737 | ||
332468f7 | 1738 | /* |
cc20ed03 | 1739 | * Stop all boot services. |
332468f7 HS |
1740 | * |
1741 | * This function implements the ExitBootServices service. | |
1742 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1743 | * for details. | |
1744 | * | |
cc20ed03 HS |
1745 | * All timer events are disabled. |
1746 | * For exit boot services events the notification function is called. | |
1747 | * The boot services are disabled in the system table. | |
1748 | * | |
332468f7 HS |
1749 | * @image_handle handle of the loaded image |
1750 | * @map_key key of the memory map | |
1751 | * @return status code | |
1752 | */ | |
2074f700 | 1753 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
bee91169 AG |
1754 | unsigned long map_key) |
1755 | { | |
152a263c HS |
1756 | int i; |
1757 | ||
bee91169 AG |
1758 | EFI_ENTRY("%p, %ld", image_handle, map_key); |
1759 | ||
cc20ed03 HS |
1760 | /* Make sure that notification functions are not called anymore */ |
1761 | efi_tpl = TPL_HIGH_LEVEL; | |
1762 | ||
1763 | /* Check if ExitBootServices has already been called */ | |
1764 | if (!systab.boottime) | |
1765 | return EFI_EXIT(EFI_SUCCESS); | |
1766 | ||
152a263c HS |
1767 | /* Notify that ExitBootServices is invoked. */ |
1768 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { | |
1769 | if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES) | |
1770 | continue; | |
cc20ed03 HS |
1771 | efi_events[i].is_signaled = true; |
1772 | efi_signal_event(&efi_events[i], false); | |
152a263c | 1773 | } |
152a263c | 1774 | |
cc20ed03 | 1775 | /* TODO Should persist EFI variables here */ |
ad644e7c | 1776 | |
b7b8410a AG |
1777 | board_quiesce_devices(); |
1778 | ||
bee91169 AG |
1779 | /* Fix up caches for EFI payloads if necessary */ |
1780 | efi_exit_caches(); | |
1781 | ||
1782 | /* This stops all lingering devices */ | |
1783 | bootm_disable_interrupts(); | |
1784 | ||
cc20ed03 HS |
1785 | /* Disable boottime services */ |
1786 | systab.con_in_handle = NULL; | |
1787 | systab.con_in = NULL; | |
1788 | systab.con_out_handle = NULL; | |
1789 | systab.con_out = NULL; | |
1790 | systab.stderr_handle = NULL; | |
1791 | systab.std_err = NULL; | |
1792 | systab.boottime = NULL; | |
1793 | ||
1794 | /* Recalculate CRC32 */ | |
1795 | systab.hdr.crc32 = 0; | |
1796 | systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab, | |
1797 | sizeof(struct efi_system_table)); | |
1798 | ||
bee91169 | 1799 | /* Give the payload some time to boot */ |
b3d60900 | 1800 | efi_set_watchdog(0); |
bee91169 AG |
1801 | WATCHDOG_RESET(); |
1802 | ||
1803 | return EFI_EXIT(EFI_SUCCESS); | |
1804 | } | |
1805 | ||
332468f7 HS |
1806 | /* |
1807 | * Get next value of the counter. | |
1808 | * | |
1809 | * This function implements the NextMonotonicCount service. | |
1810 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1811 | * for details. | |
1812 | * | |
1813 | * @count returned value of the counter | |
1814 | * @return status code | |
1815 | */ | |
bee91169 AG |
1816 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
1817 | { | |
1818 | static uint64_t mono = 0; | |
1819 | EFI_ENTRY("%p", count); | |
1820 | *count = mono++; | |
1821 | return EFI_EXIT(EFI_SUCCESS); | |
1822 | } | |
1823 | ||
332468f7 HS |
1824 | /* |
1825 | * Sleep. | |
1826 | * | |
1827 | * This function implements the Stall sercive. | |
1828 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1829 | * for details. | |
1830 | * | |
1831 | * @microseconds period to sleep in microseconds | |
1832 | * @return status code | |
1833 | */ | |
bee91169 AG |
1834 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
1835 | { | |
1836 | EFI_ENTRY("%ld", microseconds); | |
1837 | udelay(microseconds); | |
1838 | return EFI_EXIT(EFI_SUCCESS); | |
1839 | } | |
1840 | ||
332468f7 HS |
1841 | /* |
1842 | * Reset the watchdog timer. | |
1843 | * | |
b3d60900 | 1844 | * This function implements the SetWatchdogTimer service. |
332468f7 HS |
1845 | * See the Unified Extensible Firmware Interface (UEFI) specification |
1846 | * for details. | |
1847 | * | |
1848 | * @timeout seconds before reset by watchdog | |
1849 | * @watchdog_code code to be logged when resetting | |
1850 | * @data_size size of buffer in bytes | |
1851 | * @watchdog_data buffer with data describing the reset reason | |
10a08c4e | 1852 | * @return status code |
332468f7 | 1853 | */ |
bee91169 AG |
1854 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
1855 | uint64_t watchdog_code, | |
1856 | unsigned long data_size, | |
1857 | uint16_t *watchdog_data) | |
1858 | { | |
1859 | EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code, | |
1860 | data_size, watchdog_data); | |
b3d60900 | 1861 | return EFI_EXIT(efi_set_watchdog(timeout)); |
bee91169 AG |
1862 | } |
1863 | ||
332468f7 HS |
1864 | /* |
1865 | * Close a protocol. | |
1866 | * | |
1867 | * This function implements the CloseProtocol service. | |
1868 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1869 | * for details. | |
1870 | * | |
1871 | * @handle handle on which the protocol shall be closed | |
1872 | * @protocol GUID of the protocol to close | |
1873 | * @agent_handle handle of the driver | |
1874 | * @controller_handle handle of the controller | |
1875 | * @return status code | |
1876 | */ | |
2074f700 | 1877 | static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
5a9682d0 | 1878 | const efi_guid_t *protocol, |
2074f700 HS |
1879 | efi_handle_t agent_handle, |
1880 | efi_handle_t controller_handle) | |
bee91169 | 1881 | { |
3b8a489c HS |
1882 | struct efi_handler *handler; |
1883 | struct efi_open_protocol_info_item *item; | |
1884 | struct efi_open_protocol_info_item *pos; | |
1885 | efi_status_t r; | |
1886 | ||
778e6af8 | 1887 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
bee91169 | 1888 | controller_handle); |
3b8a489c HS |
1889 | |
1890 | if (!agent_handle) { | |
1891 | r = EFI_INVALID_PARAMETER; | |
1892 | goto out; | |
1893 | } | |
1894 | r = efi_search_protocol(handle, protocol, &handler); | |
1895 | if (r != EFI_SUCCESS) | |
1896 | goto out; | |
1897 | ||
1898 | r = EFI_NOT_FOUND; | |
1899 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1900 | if (item->info.agent_handle == agent_handle && | |
1901 | item->info.controller_handle == controller_handle) { | |
1902 | efi_delete_open_info(item); | |
1903 | r = EFI_SUCCESS; | |
1904 | break; | |
1905 | } | |
1906 | } | |
1907 | out: | |
1908 | return EFI_EXIT(r); | |
bee91169 AG |
1909 | } |
1910 | ||
332468f7 HS |
1911 | /* |
1912 | * Provide information about then open status of a protocol on a handle | |
1913 | * | |
1914 | * This function implements the OpenProtocolInformation service. | |
1915 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1916 | * for details. | |
1917 | * | |
1918 | * @handle handle for which the information shall be retrieved | |
1919 | * @protocol GUID of the protocol | |
1920 | * @entry_buffer buffer to receive the open protocol information | |
1921 | * @entry_count number of entries available in the buffer | |
1922 | * @return status code | |
1923 | */ | |
bee91169 | 1924 | static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle, |
5a9682d0 | 1925 | const efi_guid_t *protocol, |
bee91169 | 1926 | struct efi_open_protocol_info_entry **entry_buffer, |
f5a2a938 | 1927 | efi_uintn_t *entry_count) |
bee91169 | 1928 | { |
e3fbbc36 HS |
1929 | unsigned long buffer_size; |
1930 | unsigned long count; | |
1931 | struct efi_handler *handler; | |
1932 | struct efi_open_protocol_info_item *item; | |
1933 | efi_status_t r; | |
1934 | ||
778e6af8 | 1935 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
bee91169 | 1936 | entry_count); |
e3fbbc36 HS |
1937 | |
1938 | /* Check parameters */ | |
1939 | if (!entry_buffer) { | |
1940 | r = EFI_INVALID_PARAMETER; | |
1941 | goto out; | |
1942 | } | |
1943 | r = efi_search_protocol(handle, protocol, &handler); | |
1944 | if (r != EFI_SUCCESS) | |
1945 | goto out; | |
1946 | ||
1947 | /* Count entries */ | |
1948 | count = 0; | |
1949 | list_for_each_entry(item, &handler->open_infos, link) { | |
1950 | if (item->info.open_count) | |
1951 | ++count; | |
1952 | } | |
1953 | *entry_count = count; | |
1954 | *entry_buffer = NULL; | |
1955 | if (!count) { | |
1956 | r = EFI_SUCCESS; | |
1957 | goto out; | |
1958 | } | |
1959 | ||
1960 | /* Copy entries */ | |
1961 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); | |
1962 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
1963 | (void **)entry_buffer); | |
1964 | if (r != EFI_SUCCESS) | |
1965 | goto out; | |
1966 | list_for_each_entry_reverse(item, &handler->open_infos, link) { | |
1967 | if (item->info.open_count) | |
1968 | (*entry_buffer)[--count] = item->info; | |
1969 | } | |
1970 | out: | |
1971 | return EFI_EXIT(r); | |
bee91169 AG |
1972 | } |
1973 | ||
332468f7 HS |
1974 | /* |
1975 | * Get protocols installed on a handle. | |
1976 | * | |
1977 | * This function implements the ProtocolsPerHandleService. | |
1978 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1979 | * for details. | |
1980 | * | |
1981 | * @handle handle for which the information is retrieved | |
1982 | * @protocol_buffer buffer with protocol GUIDs | |
1983 | * @protocol_buffer_count number of entries in the buffer | |
10a08c4e | 1984 | * @return status code |
332468f7 | 1985 | */ |
2074f700 HS |
1986 | static efi_status_t EFIAPI efi_protocols_per_handle( |
1987 | efi_handle_t handle, efi_guid_t ***protocol_buffer, | |
f5a2a938 | 1988 | efi_uintn_t *protocol_buffer_count) |
bee91169 | 1989 | { |
c0ebfc86 HS |
1990 | unsigned long buffer_size; |
1991 | struct efi_object *efiobj; | |
69fb6b1a | 1992 | struct list_head *protocol_handle; |
c0ebfc86 HS |
1993 | efi_status_t r; |
1994 | ||
bee91169 AG |
1995 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
1996 | protocol_buffer_count); | |
c0ebfc86 HS |
1997 | |
1998 | if (!handle || !protocol_buffer || !protocol_buffer_count) | |
1999 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2000 | ||
2001 | *protocol_buffer = NULL; | |
661c8327 | 2002 | *protocol_buffer_count = 0; |
c0ebfc86 | 2003 | |
69fb6b1a HS |
2004 | efiobj = efi_search_obj(handle); |
2005 | if (!efiobj) | |
2006 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c0ebfc86 | 2007 | |
69fb6b1a HS |
2008 | /* Count protocols */ |
2009 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2010 | ++*protocol_buffer_count; | |
2011 | } | |
2012 | ||
2013 | /* Copy guids */ | |
2014 | if (*protocol_buffer_count) { | |
2015 | size_t j = 0; | |
2016 | ||
2017 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; | |
2018 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
2019 | (void **)protocol_buffer); | |
2020 | if (r != EFI_SUCCESS) | |
2021 | return EFI_EXIT(r); | |
2022 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2023 | struct efi_handler *protocol; | |
2024 | ||
2025 | protocol = list_entry(protocol_handle, | |
2026 | struct efi_handler, link); | |
2027 | (*protocol_buffer)[j] = (void *)protocol->guid; | |
2028 | ++j; | |
c0ebfc86 | 2029 | } |
c0ebfc86 HS |
2030 | } |
2031 | ||
2032 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2033 | } |
2034 | ||
332468f7 HS |
2035 | /* |
2036 | * Locate handles implementing a protocol. | |
2037 | * | |
2038 | * This function implements the LocateHandleBuffer service. | |
2039 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2040 | * for details. | |
2041 | * | |
2042 | * @search_type selection criterion | |
2043 | * @protocol GUID of the protocol | |
2044 | * @search_key registration key | |
2045 | * @no_handles number of returned handles | |
2046 | * @buffer buffer with the returned handles | |
2047 | * @return status code | |
2048 | */ | |
bee91169 AG |
2049 | static efi_status_t EFIAPI efi_locate_handle_buffer( |
2050 | enum efi_locate_search_type search_type, | |
5a9682d0 | 2051 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 2052 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
bee91169 | 2053 | { |
c2e703f9 | 2054 | efi_status_t r; |
f5a2a938 | 2055 | efi_uintn_t buffer_size = 0; |
c2e703f9 | 2056 | |
778e6af8 | 2057 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
bee91169 | 2058 | no_handles, buffer); |
c2e703f9 HS |
2059 | |
2060 | if (!no_handles || !buffer) { | |
2061 | r = EFI_INVALID_PARAMETER; | |
2062 | goto out; | |
2063 | } | |
2064 | *no_handles = 0; | |
2065 | *buffer = NULL; | |
2066 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2067 | *buffer); | |
2068 | if (r != EFI_BUFFER_TOO_SMALL) | |
2069 | goto out; | |
2070 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
2071 | (void **)buffer); | |
2072 | if (r != EFI_SUCCESS) | |
2073 | goto out; | |
2074 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2075 | *buffer); | |
2076 | if (r == EFI_SUCCESS) | |
2074f700 | 2077 | *no_handles = buffer_size / sizeof(efi_handle_t); |
c2e703f9 HS |
2078 | out: |
2079 | return EFI_EXIT(r); | |
bee91169 AG |
2080 | } |
2081 | ||
332468f7 HS |
2082 | /* |
2083 | * Find an interface implementing a protocol. | |
2084 | * | |
2085 | * This function implements the LocateProtocol service. | |
2086 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2087 | * for details. | |
2088 | * | |
2089 | * @protocol GUID of the protocol | |
2090 | * @registration registration key passed to the notification function | |
2091 | * @protocol_interface interface implementing the protocol | |
10a08c4e | 2092 | * @return status code |
332468f7 | 2093 | */ |
5a9682d0 | 2094 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
bee91169 AG |
2095 | void *registration, |
2096 | void **protocol_interface) | |
2097 | { | |
88adae5e | 2098 | struct list_head *lhandle; |
9172cd91 | 2099 | efi_status_t ret; |
bee91169 | 2100 | |
778e6af8 | 2101 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
88adae5e HS |
2102 | |
2103 | if (!protocol || !protocol_interface) | |
2104 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2105 | ||
2106 | list_for_each(lhandle, &efi_obj_list) { | |
2107 | struct efi_object *efiobj; | |
9172cd91 | 2108 | struct efi_handler *handler; |
88adae5e HS |
2109 | |
2110 | efiobj = list_entry(lhandle, struct efi_object, link); | |
9172cd91 HS |
2111 | |
2112 | ret = efi_search_protocol(efiobj->handle, protocol, &handler); | |
2113 | if (ret == EFI_SUCCESS) { | |
2114 | *protocol_interface = handler->protocol_interface; | |
2115 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2116 | } |
2117 | } | |
88adae5e | 2118 | *protocol_interface = NULL; |
bee91169 AG |
2119 | |
2120 | return EFI_EXIT(EFI_NOT_FOUND); | |
2121 | } | |
2122 | ||
ae2c85c1 HS |
2123 | /* |
2124 | * Get the device path and handle of an device implementing a protocol. | |
2125 | * | |
2126 | * This function implements the LocateDevicePath service. | |
2127 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2128 | * for details. | |
2129 | * | |
2130 | * @protocol GUID of the protocol | |
2131 | * @device_path device path | |
2132 | * @device handle of the device | |
2133 | * @return status code | |
2134 | */ | |
2135 | static efi_status_t EFIAPI efi_locate_device_path( | |
2136 | const efi_guid_t *protocol, | |
2137 | struct efi_device_path **device_path, | |
2138 | efi_handle_t *device) | |
2139 | { | |
2140 | struct efi_device_path *dp; | |
2141 | size_t i; | |
2142 | struct efi_handler *handler; | |
2143 | efi_handle_t *handles; | |
2144 | size_t len, len_dp; | |
2145 | size_t len_best = 0; | |
2146 | efi_uintn_t no_handles; | |
2147 | u8 *remainder; | |
2148 | efi_status_t ret; | |
2149 | ||
2150 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); | |
2151 | ||
2152 | if (!protocol || !device_path || !*device_path || !device) { | |
2153 | ret = EFI_INVALID_PARAMETER; | |
2154 | goto out; | |
2155 | } | |
2156 | ||
2157 | /* Find end of device path */ | |
2158 | len = efi_dp_size(*device_path); | |
2159 | ||
2160 | /* Get all handles implementing the protocol */ | |
2161 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, | |
2162 | &no_handles, &handles)); | |
2163 | if (ret != EFI_SUCCESS) | |
2164 | goto out; | |
2165 | ||
2166 | for (i = 0; i < no_handles; ++i) { | |
2167 | /* Find the device path protocol */ | |
2168 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, | |
2169 | &handler); | |
2170 | if (ret != EFI_SUCCESS) | |
2171 | continue; | |
2172 | dp = (struct efi_device_path *)handler->protocol_interface; | |
2173 | len_dp = efi_dp_size(dp); | |
2174 | /* | |
2175 | * This handle can only be a better fit | |
2176 | * if its device path length is longer than the best fit and | |
2177 | * if its device path length is shorter of equal the searched | |
2178 | * device path. | |
2179 | */ | |
2180 | if (len_dp <= len_best || len_dp > len) | |
2181 | continue; | |
2182 | /* Check if dp is a subpath of device_path */ | |
2183 | if (memcmp(*device_path, dp, len_dp)) | |
2184 | continue; | |
2185 | *device = handles[i]; | |
2186 | len_best = len_dp; | |
2187 | } | |
2188 | if (len_best) { | |
2189 | remainder = (u8 *)*device_path + len_best; | |
2190 | *device_path = (struct efi_device_path *)remainder; | |
2191 | ret = EFI_SUCCESS; | |
2192 | } else { | |
2193 | ret = EFI_NOT_FOUND; | |
2194 | } | |
2195 | out: | |
2196 | return EFI_EXIT(ret); | |
2197 | } | |
2198 | ||
332468f7 HS |
2199 | /* |
2200 | * Install multiple protocol interfaces. | |
2201 | * | |
2202 | * This function implements the MultipleProtocolInterfaces service. | |
2203 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2204 | * for details. | |
2205 | * | |
2206 | * @handle handle on which the protocol interfaces shall be installed | |
2207 | * @... NULL terminated argument list with pairs of protocol GUIDS and | |
2208 | * interfaces | |
2209 | * @return status code | |
2210 | */ | |
bee91169 AG |
2211 | static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces( |
2212 | void **handle, ...) | |
2213 | { | |
2214 | EFI_ENTRY("%p", handle); | |
58b83586 HS |
2215 | |
2216 | va_list argptr; | |
5a9682d0 | 2217 | const efi_guid_t *protocol; |
58b83586 HS |
2218 | void *protocol_interface; |
2219 | efi_status_t r = EFI_SUCCESS; | |
2220 | int i = 0; | |
2221 | ||
2222 | if (!handle) | |
2223 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2224 | ||
2225 | va_start(argptr, handle); | |
2226 | for (;;) { | |
2227 | protocol = va_arg(argptr, efi_guid_t*); | |
2228 | if (!protocol) | |
2229 | break; | |
2230 | protocol_interface = va_arg(argptr, void*); | |
1760ef57 HS |
2231 | r = EFI_CALL(efi_install_protocol_interface( |
2232 | handle, protocol, | |
2233 | EFI_NATIVE_INTERFACE, | |
2234 | protocol_interface)); | |
58b83586 HS |
2235 | if (r != EFI_SUCCESS) |
2236 | break; | |
2237 | i++; | |
2238 | } | |
2239 | va_end(argptr); | |
2240 | if (r == EFI_SUCCESS) | |
2241 | return EFI_EXIT(r); | |
2242 | ||
62471e46 | 2243 | /* If an error occurred undo all changes. */ |
58b83586 HS |
2244 | va_start(argptr, handle); |
2245 | for (; i; --i) { | |
2246 | protocol = va_arg(argptr, efi_guid_t*); | |
2247 | protocol_interface = va_arg(argptr, void*); | |
cd534083 HS |
2248 | EFI_CALL(efi_uninstall_protocol_interface(handle, protocol, |
2249 | protocol_interface)); | |
58b83586 HS |
2250 | } |
2251 | va_end(argptr); | |
2252 | ||
2253 | return EFI_EXIT(r); | |
bee91169 AG |
2254 | } |
2255 | ||
332468f7 HS |
2256 | /* |
2257 | * Uninstall multiple protocol interfaces. | |
2258 | * | |
2259 | * This function implements the UninstallMultipleProtocolInterfaces service. | |
2260 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2261 | * for details. | |
2262 | * | |
2263 | * @handle handle from which the protocol interfaces shall be removed | |
2264 | * @... NULL terminated argument list with pairs of protocol GUIDS and | |
2265 | * interfaces | |
2266 | * @return status code | |
2267 | */ | |
bee91169 AG |
2268 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
2269 | void *handle, ...) | |
2270 | { | |
2271 | EFI_ENTRY("%p", handle); | |
843ce54c HS |
2272 | |
2273 | va_list argptr; | |
2274 | const efi_guid_t *protocol; | |
2275 | void *protocol_interface; | |
2276 | efi_status_t r = EFI_SUCCESS; | |
2277 | size_t i = 0; | |
2278 | ||
2279 | if (!handle) | |
2280 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2281 | ||
2282 | va_start(argptr, handle); | |
2283 | for (;;) { | |
2284 | protocol = va_arg(argptr, efi_guid_t*); | |
2285 | if (!protocol) | |
2286 | break; | |
2287 | protocol_interface = va_arg(argptr, void*); | |
2288 | r = EFI_CALL(efi_uninstall_protocol_interface( | |
2289 | handle, protocol, | |
2290 | protocol_interface)); | |
2291 | if (r != EFI_SUCCESS) | |
2292 | break; | |
2293 | i++; | |
2294 | } | |
2295 | va_end(argptr); | |
2296 | if (r == EFI_SUCCESS) | |
2297 | return EFI_EXIT(r); | |
2298 | ||
2299 | /* If an error occurred undo all changes. */ | |
2300 | va_start(argptr, handle); | |
2301 | for (; i; --i) { | |
2302 | protocol = va_arg(argptr, efi_guid_t*); | |
2303 | protocol_interface = va_arg(argptr, void*); | |
2304 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, | |
2305 | EFI_NATIVE_INTERFACE, | |
2306 | protocol_interface)); | |
2307 | } | |
2308 | va_end(argptr); | |
2309 | ||
2310 | return EFI_EXIT(r); | |
bee91169 AG |
2311 | } |
2312 | ||
332468f7 HS |
2313 | /* |
2314 | * Calculate cyclic redundancy code. | |
2315 | * | |
2316 | * This function implements the CalculateCrc32 service. | |
2317 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2318 | * for details. | |
2319 | * | |
2320 | * @data buffer with data | |
2321 | * @data_size size of buffer in bytes | |
2322 | * @crc32_p cyclic redundancy code | |
2323 | * @return status code | |
2324 | */ | |
bee91169 AG |
2325 | static efi_status_t EFIAPI efi_calculate_crc32(void *data, |
2326 | unsigned long data_size, | |
2327 | uint32_t *crc32_p) | |
2328 | { | |
2329 | EFI_ENTRY("%p, %ld", data, data_size); | |
2330 | *crc32_p = crc32(0, data, data_size); | |
2331 | return EFI_EXIT(EFI_SUCCESS); | |
2332 | } | |
2333 | ||
332468f7 HS |
2334 | /* |
2335 | * Copy memory. | |
2336 | * | |
2337 | * This function implements the CopyMem service. | |
2338 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2339 | * for details. | |
2340 | * | |
2341 | * @destination destination of the copy operation | |
2342 | * @source source of the copy operation | |
2343 | * @length number of bytes to copy | |
2344 | */ | |
fc05a959 HS |
2345 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
2346 | size_t length) | |
bee91169 | 2347 | { |
fc05a959 | 2348 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
bee91169 | 2349 | memcpy(destination, source, length); |
f7c78176 | 2350 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2351 | } |
2352 | ||
332468f7 HS |
2353 | /* |
2354 | * Fill memory with a byte value. | |
2355 | * | |
2356 | * This function implements the SetMem service. | |
2357 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2358 | * for details. | |
2359 | * | |
2360 | * @buffer buffer to fill | |
2361 | * @size size of buffer in bytes | |
2362 | * @value byte to copy to the buffer | |
2363 | */ | |
fc05a959 | 2364 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
bee91169 | 2365 | { |
fc05a959 | 2366 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
bee91169 | 2367 | memset(buffer, value, size); |
f7c78176 | 2368 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2369 | } |
2370 | ||
191a41cc HS |
2371 | /* |
2372 | * Open protocol interface on a handle. | |
2373 | * | |
2374 | * @handler handler of a protocol | |
2375 | * @protocol_interface interface implementing the protocol | |
2376 | * @agent_handle handle of the driver | |
2377 | * @controller_handle handle of the controller | |
2378 | * @attributes attributes indicating how to open the protocol | |
2379 | * @return status code | |
2380 | */ | |
2381 | static efi_status_t efi_protocol_open( | |
2382 | struct efi_handler *handler, | |
2383 | void **protocol_interface, void *agent_handle, | |
2384 | void *controller_handle, uint32_t attributes) | |
2385 | { | |
2386 | struct efi_open_protocol_info_item *item; | |
2387 | struct efi_open_protocol_info_entry *match = NULL; | |
2388 | bool opened_by_driver = false; | |
2389 | bool opened_exclusive = false; | |
2390 | ||
2391 | /* If there is no agent, only return the interface */ | |
2392 | if (!agent_handle) | |
2393 | goto out; | |
2394 | ||
2395 | /* For TEST_PROTOCOL ignore interface attribute */ | |
2396 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2397 | *protocol_interface = NULL; | |
2398 | ||
2399 | /* | |
2400 | * Check if the protocol is already opened by a driver with the same | |
2401 | * attributes or opened exclusively | |
2402 | */ | |
2403 | list_for_each_entry(item, &handler->open_infos, link) { | |
2404 | if (item->info.agent_handle == agent_handle) { | |
2405 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && | |
2406 | (item->info.attributes == attributes)) | |
2407 | return EFI_ALREADY_STARTED; | |
2408 | } | |
2409 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) | |
2410 | opened_exclusive = true; | |
2411 | } | |
2412 | ||
2413 | /* Only one controller can open the protocol exclusively */ | |
2414 | if (opened_exclusive && attributes & | |
2415 | (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER)) | |
2416 | return EFI_ACCESS_DENIED; | |
2417 | ||
2418 | /* Prepare exclusive opening */ | |
2419 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { | |
2420 | /* Try to disconnect controllers */ | |
2421 | list_for_each_entry(item, &handler->open_infos, link) { | |
2422 | if (item->info.attributes == | |
2423 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2424 | EFI_CALL(efi_disconnect_controller( | |
2425 | item->info.controller_handle, | |
2426 | item->info.agent_handle, | |
2427 | NULL)); | |
2428 | } | |
2429 | opened_by_driver = false; | |
2430 | /* Check if all controllers are disconnected */ | |
2431 | list_for_each_entry(item, &handler->open_infos, link) { | |
2432 | if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2433 | opened_by_driver = true; | |
2434 | } | |
2435 | /* Only one controller can be conncected */ | |
2436 | if (opened_by_driver) | |
2437 | return EFI_ACCESS_DENIED; | |
2438 | } | |
2439 | ||
2440 | /* Find existing entry */ | |
2441 | list_for_each_entry(item, &handler->open_infos, link) { | |
2442 | if (item->info.agent_handle == agent_handle && | |
2443 | item->info.controller_handle == controller_handle) | |
2444 | match = &item->info; | |
2445 | } | |
2446 | /* None found, create one */ | |
2447 | if (!match) { | |
2448 | match = efi_create_open_info(handler); | |
2449 | if (!match) | |
2450 | return EFI_OUT_OF_RESOURCES; | |
2451 | } | |
2452 | ||
2453 | match->agent_handle = agent_handle; | |
2454 | match->controller_handle = controller_handle; | |
2455 | match->attributes = attributes; | |
2456 | match->open_count++; | |
2457 | ||
2458 | out: | |
2459 | /* For TEST_PROTOCOL ignore interface attribute. */ | |
2460 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2461 | *protocol_interface = handler->protocol_interface; | |
2462 | ||
2463 | return EFI_SUCCESS; | |
2464 | } | |
2465 | ||
332468f7 HS |
2466 | /* |
2467 | * Open protocol interface on a handle. | |
2468 | * | |
2469 | * This function implements the OpenProtocol interface. | |
2470 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2471 | * for details. | |
2472 | * | |
2473 | * @handle handle on which the protocol shall be opened | |
2474 | * @protocol GUID of the protocol | |
2475 | * @protocol_interface interface implementing the protocol | |
2476 | * @agent_handle handle of the driver | |
2477 | * @controller_handle handle of the controller | |
2478 | * @attributes attributes indicating how to open the protocol | |
2479 | * @return status code | |
2480 | */ | |
bee91169 | 2481 | static efi_status_t EFIAPI efi_open_protocol( |
5a9682d0 | 2482 | void *handle, const efi_guid_t *protocol, |
bee91169 AG |
2483 | void **protocol_interface, void *agent_handle, |
2484 | void *controller_handle, uint32_t attributes) | |
2485 | { | |
80286e8f | 2486 | struct efi_handler *handler; |
69baec67 | 2487 | efi_status_t r = EFI_INVALID_PARAMETER; |
bee91169 | 2488 | |
778e6af8 | 2489 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
bee91169 AG |
2490 | protocol_interface, agent_handle, controller_handle, |
2491 | attributes); | |
b5349f74 | 2492 | |
69baec67 HS |
2493 | if (!handle || !protocol || |
2494 | (!protocol_interface && attributes != | |
2495 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { | |
2496 | goto out; | |
2497 | } | |
2498 | ||
2499 | switch (attributes) { | |
2500 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: | |
2501 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: | |
2502 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: | |
2503 | break; | |
2504 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: | |
2505 | if (controller_handle == handle) | |
2506 | goto out; | |
191a41cc | 2507 | /* fall-through */ |
69baec67 HS |
2508 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
2509 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
191a41cc HS |
2510 | /* Check that the controller handle is valid */ |
2511 | if (!efi_search_obj(controller_handle)) | |
69baec67 | 2512 | goto out; |
191a41cc | 2513 | /* fall-through */ |
69baec67 | 2514 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
191a41cc HS |
2515 | /* Check that the agent handle is valid */ |
2516 | if (!efi_search_obj(agent_handle)) | |
69baec67 HS |
2517 | goto out; |
2518 | break; | |
2519 | default: | |
b5349f74 HS |
2520 | goto out; |
2521 | } | |
2522 | ||
80286e8f HS |
2523 | r = efi_search_protocol(handle, protocol, &handler); |
2524 | if (r != EFI_SUCCESS) | |
2525 | goto out; | |
bee91169 | 2526 | |
191a41cc HS |
2527 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
2528 | controller_handle, attributes); | |
bee91169 AG |
2529 | out: |
2530 | return EFI_EXIT(r); | |
2531 | } | |
2532 | ||
332468f7 HS |
2533 | /* |
2534 | * Get interface of a protocol on a handle. | |
2535 | * | |
2536 | * This function implements the HandleProtocol service. | |
2537 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2538 | * for details. | |
2539 | * | |
2540 | * @handle handle on which the protocol shall be opened | |
2541 | * @protocol GUID of the protocol | |
2542 | * @protocol_interface interface implementing the protocol | |
2543 | * @return status code | |
2544 | */ | |
2074f700 | 2545 | static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
5a9682d0 | 2546 | const efi_guid_t *protocol, |
bee91169 AG |
2547 | void **protocol_interface) |
2548 | { | |
8e1d329f HS |
2549 | return efi_open_protocol(handle, protocol, protocol_interface, NULL, |
2550 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); | |
bee91169 AG |
2551 | } |
2552 | ||
f0959dbe HS |
2553 | static efi_status_t efi_bind_controller( |
2554 | efi_handle_t controller_handle, | |
2555 | efi_handle_t driver_image_handle, | |
2556 | struct efi_device_path *remain_device_path) | |
2557 | { | |
2558 | struct efi_driver_binding_protocol *binding_protocol; | |
2559 | efi_status_t r; | |
2560 | ||
2561 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
2562 | &efi_guid_driver_binding_protocol, | |
2563 | (void **)&binding_protocol, | |
2564 | driver_image_handle, NULL, | |
2565 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2566 | if (r != EFI_SUCCESS) | |
2567 | return r; | |
2568 | r = EFI_CALL(binding_protocol->supported(binding_protocol, | |
2569 | controller_handle, | |
2570 | remain_device_path)); | |
2571 | if (r == EFI_SUCCESS) | |
2572 | r = EFI_CALL(binding_protocol->start(binding_protocol, | |
2573 | controller_handle, | |
2574 | remain_device_path)); | |
2575 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
2576 | &efi_guid_driver_binding_protocol, | |
2577 | driver_image_handle, NULL)); | |
2578 | return r; | |
2579 | } | |
2580 | ||
2581 | static efi_status_t efi_connect_single_controller( | |
2582 | efi_handle_t controller_handle, | |
2583 | efi_handle_t *driver_image_handle, | |
2584 | struct efi_device_path *remain_device_path) | |
2585 | { | |
2586 | efi_handle_t *buffer; | |
2587 | size_t count; | |
2588 | size_t i; | |
2589 | efi_status_t r; | |
2590 | size_t connected = 0; | |
2591 | ||
2592 | /* Get buffer with all handles with driver binding protocol */ | |
2593 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, | |
2594 | &efi_guid_driver_binding_protocol, | |
2595 | NULL, &count, &buffer)); | |
2596 | if (r != EFI_SUCCESS) | |
2597 | return r; | |
2598 | ||
2599 | /* Context Override */ | |
2600 | if (driver_image_handle) { | |
2601 | for (; *driver_image_handle; ++driver_image_handle) { | |
2602 | for (i = 0; i < count; ++i) { | |
2603 | if (buffer[i] == *driver_image_handle) { | |
2604 | buffer[i] = NULL; | |
2605 | r = efi_bind_controller( | |
2606 | controller_handle, | |
2607 | *driver_image_handle, | |
2608 | remain_device_path); | |
2609 | /* | |
2610 | * For drivers that do not support the | |
2611 | * controller or are already connected | |
2612 | * we receive an error code here. | |
2613 | */ | |
2614 | if (r == EFI_SUCCESS) | |
2615 | ++connected; | |
2616 | } | |
2617 | } | |
2618 | } | |
2619 | } | |
2620 | ||
2621 | /* | |
2622 | * TODO: Some overrides are not yet implemented: | |
2623 | * - Platform Driver Override | |
2624 | * - Driver Family Override Search | |
2625 | * - Bus Specific Driver Override | |
2626 | */ | |
2627 | ||
2628 | /* Driver Binding Search */ | |
2629 | for (i = 0; i < count; ++i) { | |
2630 | if (buffer[i]) { | |
2631 | r = efi_bind_controller(controller_handle, | |
2632 | buffer[i], | |
2633 | remain_device_path); | |
2634 | if (r == EFI_SUCCESS) | |
2635 | ++connected; | |
2636 | } | |
2637 | } | |
2638 | ||
2639 | efi_free_pool(buffer); | |
2640 | if (!connected) | |
2641 | return EFI_NOT_FOUND; | |
2642 | return EFI_SUCCESS; | |
2643 | } | |
2644 | ||
2645 | /* | |
2646 | * Connect a controller to a driver. | |
2647 | * | |
2648 | * This function implements the ConnectController service. | |
2649 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2650 | * for details. | |
2651 | * | |
2652 | * First all driver binding protocol handles are tried for binding drivers. | |
2653 | * Afterwards all handles that have openened a protocol of the controller | |
2654 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. | |
2655 | * | |
2656 | * @controller_handle handle of the controller | |
2657 | * @driver_image_handle handle of the driver | |
2658 | * @remain_device_path device path of a child controller | |
2659 | * @recursive true to connect all child controllers | |
2660 | * @return status code | |
2661 | */ | |
2662 | static efi_status_t EFIAPI efi_connect_controller( | |
2663 | efi_handle_t controller_handle, | |
2664 | efi_handle_t *driver_image_handle, | |
2665 | struct efi_device_path *remain_device_path, | |
2666 | bool recursive) | |
2667 | { | |
2668 | efi_status_t r; | |
2669 | efi_status_t ret = EFI_NOT_FOUND; | |
2670 | struct efi_object *efiobj; | |
2671 | ||
2672 | EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle, | |
2673 | remain_device_path, recursive); | |
2674 | ||
2675 | efiobj = efi_search_obj(controller_handle); | |
2676 | if (!efiobj) { | |
2677 | ret = EFI_INVALID_PARAMETER; | |
2678 | goto out; | |
2679 | } | |
2680 | ||
2681 | r = efi_connect_single_controller(controller_handle, | |
2682 | driver_image_handle, | |
2683 | remain_device_path); | |
2684 | if (r == EFI_SUCCESS) | |
2685 | ret = EFI_SUCCESS; | |
2686 | if (recursive) { | |
2687 | struct efi_handler *handler; | |
2688 | struct efi_open_protocol_info_item *item; | |
2689 | ||
2690 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2691 | list_for_each_entry(item, &handler->open_infos, link) { | |
2692 | if (item->info.attributes & | |
2693 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
2694 | r = EFI_CALL(efi_connect_controller( | |
2695 | item->info.controller_handle, | |
2696 | driver_image_handle, | |
2697 | remain_device_path, | |
2698 | recursive)); | |
2699 | if (r == EFI_SUCCESS) | |
2700 | ret = EFI_SUCCESS; | |
2701 | } | |
2702 | } | |
2703 | } | |
2704 | } | |
2705 | /* Check for child controller specified by end node */ | |
2706 | if (ret != EFI_SUCCESS && remain_device_path && | |
2707 | remain_device_path->type == DEVICE_PATH_TYPE_END) | |
2708 | ret = EFI_SUCCESS; | |
2709 | out: | |
2710 | return EFI_EXIT(ret); | |
2711 | } | |
2712 | ||
3f9b0042 HS |
2713 | /* |
2714 | * Get all child controllers associated to a driver. | |
2715 | * The allocated buffer has to be freed with free(). | |
2716 | * | |
2717 | * @efiobj handle of the controller | |
2718 | * @driver_handle handle of the driver | |
2719 | * @number_of_children number of child controllers | |
2720 | * @child_handle_buffer handles of the the child controllers | |
2721 | */ | |
2722 | static efi_status_t efi_get_child_controllers( | |
2723 | struct efi_object *efiobj, | |
2724 | efi_handle_t driver_handle, | |
2725 | efi_uintn_t *number_of_children, | |
2726 | efi_handle_t **child_handle_buffer) | |
2727 | { | |
2728 | struct efi_handler *handler; | |
2729 | struct efi_open_protocol_info_item *item; | |
2730 | efi_uintn_t count = 0, i; | |
2731 | bool duplicate; | |
2732 | ||
2733 | /* Count all child controller associations */ | |
2734 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2735 | list_for_each_entry(item, &handler->open_infos, link) { | |
2736 | if (item->info.agent_handle == driver_handle && | |
2737 | item->info.attributes & | |
2738 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) | |
2739 | ++count; | |
2740 | } | |
2741 | } | |
2742 | /* | |
2743 | * Create buffer. In case of duplicate child controller assignments | |
2744 | * the buffer will be too large. But that does not harm. | |
2745 | */ | |
2746 | *number_of_children = 0; | |
2747 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); | |
2748 | if (!*child_handle_buffer) | |
2749 | return EFI_OUT_OF_RESOURCES; | |
2750 | /* Copy unique child handles */ | |
2751 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2752 | list_for_each_entry(item, &handler->open_infos, link) { | |
2753 | if (item->info.agent_handle == driver_handle && | |
2754 | item->info.attributes & | |
2755 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
2756 | /* Check this is a new child controller */ | |
2757 | duplicate = false; | |
2758 | for (i = 0; i < *number_of_children; ++i) { | |
2759 | if ((*child_handle_buffer)[i] == | |
2760 | item->info.controller_handle) | |
2761 | duplicate = true; | |
2762 | } | |
2763 | /* Copy handle to buffer */ | |
2764 | if (!duplicate) { | |
2765 | i = (*number_of_children)++; | |
2766 | (*child_handle_buffer)[i] = | |
2767 | item->info.controller_handle; | |
2768 | } | |
2769 | } | |
2770 | } | |
2771 | } | |
2772 | return EFI_SUCCESS; | |
2773 | } | |
2774 | ||
2775 | /* | |
2776 | * Disconnect a controller from a driver. | |
2777 | * | |
2778 | * This function implements the DisconnectController service. | |
2779 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2780 | * for details. | |
2781 | * | |
2782 | * @controller_handle handle of the controller | |
2783 | * @driver_image_handle handle of the driver | |
2784 | * @child_handle handle of the child to destroy | |
2785 | * @return status code | |
2786 | */ | |
2787 | static efi_status_t EFIAPI efi_disconnect_controller( | |
2788 | efi_handle_t controller_handle, | |
2789 | efi_handle_t driver_image_handle, | |
2790 | efi_handle_t child_handle) | |
2791 | { | |
2792 | struct efi_driver_binding_protocol *binding_protocol; | |
2793 | efi_handle_t *child_handle_buffer = NULL; | |
2794 | size_t number_of_children = 0; | |
2795 | efi_status_t r; | |
2796 | size_t stop_count = 0; | |
2797 | struct efi_object *efiobj; | |
2798 | ||
2799 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, | |
2800 | child_handle); | |
2801 | ||
2802 | efiobj = efi_search_obj(controller_handle); | |
2803 | if (!efiobj) { | |
2804 | r = EFI_INVALID_PARAMETER; | |
2805 | goto out; | |
2806 | } | |
2807 | ||
2808 | if (child_handle && !efi_search_obj(child_handle)) { | |
2809 | r = EFI_INVALID_PARAMETER; | |
2810 | goto out; | |
2811 | } | |
2812 | ||
2813 | /* If no driver handle is supplied, disconnect all drivers */ | |
2814 | if (!driver_image_handle) { | |
2815 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); | |
2816 | goto out; | |
2817 | } | |
2818 | ||
2819 | /* Create list of child handles */ | |
2820 | if (child_handle) { | |
2821 | number_of_children = 1; | |
2822 | child_handle_buffer = &child_handle; | |
2823 | } else { | |
2824 | efi_get_child_controllers(efiobj, | |
2825 | driver_image_handle, | |
2826 | &number_of_children, | |
2827 | &child_handle_buffer); | |
2828 | } | |
2829 | ||
2830 | /* Get the driver binding protocol */ | |
2831 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
2832 | &efi_guid_driver_binding_protocol, | |
2833 | (void **)&binding_protocol, | |
2834 | driver_image_handle, NULL, | |
2835 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2836 | if (r != EFI_SUCCESS) | |
2837 | goto out; | |
2838 | /* Remove the children */ | |
2839 | if (number_of_children) { | |
2840 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
2841 | controller_handle, | |
2842 | number_of_children, | |
2843 | child_handle_buffer)); | |
2844 | if (r == EFI_SUCCESS) | |
2845 | ++stop_count; | |
2846 | } | |
2847 | /* Remove the driver */ | |
2848 | if (!child_handle) | |
2849 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
2850 | controller_handle, | |
2851 | 0, NULL)); | |
2852 | if (r == EFI_SUCCESS) | |
2853 | ++stop_count; | |
2854 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
2855 | &efi_guid_driver_binding_protocol, | |
2856 | driver_image_handle, NULL)); | |
2857 | ||
2858 | if (stop_count) | |
2859 | r = EFI_SUCCESS; | |
2860 | else | |
2861 | r = EFI_NOT_FOUND; | |
2862 | out: | |
2863 | if (!child_handle) | |
2864 | free(child_handle_buffer); | |
2865 | return EFI_EXIT(r); | |
2866 | } | |
2867 | ||
bee91169 AG |
2868 | static const struct efi_boot_services efi_boot_services = { |
2869 | .hdr = { | |
2870 | .headersize = sizeof(struct efi_table_hdr), | |
2871 | }, | |
2872 | .raise_tpl = efi_raise_tpl, | |
2873 | .restore_tpl = efi_restore_tpl, | |
2874 | .allocate_pages = efi_allocate_pages_ext, | |
2875 | .free_pages = efi_free_pages_ext, | |
2876 | .get_memory_map = efi_get_memory_map_ext, | |
ead1274b | 2877 | .allocate_pool = efi_allocate_pool_ext, |
42417bc8 | 2878 | .free_pool = efi_free_pool_ext, |
49deb455 | 2879 | .create_event = efi_create_event_ext, |
bfc72462 | 2880 | .set_timer = efi_set_timer_ext, |
bee91169 | 2881 | .wait_for_event = efi_wait_for_event, |
c6841592 | 2882 | .signal_event = efi_signal_event_ext, |
bee91169 AG |
2883 | .close_event = efi_close_event, |
2884 | .check_event = efi_check_event, | |
1760ef57 | 2885 | .install_protocol_interface = efi_install_protocol_interface, |
bee91169 | 2886 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
cd534083 | 2887 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
bee91169 AG |
2888 | .handle_protocol = efi_handle_protocol, |
2889 | .reserved = NULL, | |
2890 | .register_protocol_notify = efi_register_protocol_notify, | |
26329584 | 2891 | .locate_handle = efi_locate_handle_ext, |
bee91169 | 2892 | .locate_device_path = efi_locate_device_path, |
488bf12d | 2893 | .install_configuration_table = efi_install_configuration_table_ext, |
bee91169 AG |
2894 | .load_image = efi_load_image, |
2895 | .start_image = efi_start_image, | |
a86aeaf2 | 2896 | .exit = efi_exit, |
bee91169 AG |
2897 | .unload_image = efi_unload_image, |
2898 | .exit_boot_services = efi_exit_boot_services, | |
2899 | .get_next_monotonic_count = efi_get_next_monotonic_count, | |
2900 | .stall = efi_stall, | |
2901 | .set_watchdog_timer = efi_set_watchdog_timer, | |
2902 | .connect_controller = efi_connect_controller, | |
2903 | .disconnect_controller = efi_disconnect_controller, | |
2904 | .open_protocol = efi_open_protocol, | |
2905 | .close_protocol = efi_close_protocol, | |
2906 | .open_protocol_information = efi_open_protocol_information, | |
2907 | .protocols_per_handle = efi_protocols_per_handle, | |
2908 | .locate_handle_buffer = efi_locate_handle_buffer, | |
2909 | .locate_protocol = efi_locate_protocol, | |
2910 | .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces, | |
2911 | .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces, | |
2912 | .calculate_crc32 = efi_calculate_crc32, | |
2913 | .copy_mem = efi_copy_mem, | |
2914 | .set_mem = efi_set_mem, | |
9f0930e5 | 2915 | .create_event_ex = efi_create_event_ex, |
bee91169 AG |
2916 | }; |
2917 | ||
2918 | ||
05b6f56e | 2919 | static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
bee91169 | 2920 | |
3c63db9c | 2921 | struct efi_system_table __efi_runtime_data systab = { |
bee91169 AG |
2922 | .hdr = { |
2923 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, | |
f19a95a4 | 2924 | .revision = 2 << 16 | 70, /* 2.7 */ |
bee91169 AG |
2925 | .headersize = sizeof(struct efi_table_hdr), |
2926 | }, | |
2927 | .fw_vendor = (long)firmware_vendor, | |
2928 | .con_in = (void*)&efi_con_in, | |
2929 | .con_out = (void*)&efi_con_out, | |
2930 | .std_err = (void*)&efi_con_out, | |
2931 | .runtime = (void*)&efi_runtime_services, | |
2932 | .boottime = (void*)&efi_boot_services, | |
2933 | .nr_tables = 0, | |
2934 | .tables = (void*)efi_conf_table, | |
2935 | }; |