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