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