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