blob: 4f6e8d1679afde1ad99466e9b5abeec40138de78 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020016#include <pe.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010017#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020028__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020030/* List of queued events */
31LIST_HEAD(efi_event_queue);
32
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020033/* Flag to disable timer activity in ExitBootServices() */
34static bool timers_enabled = true;
35
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020036/* List of all events registered by RegisterProtocolNotify() */
37LIST_HEAD(efi_register_notify_events);
38
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010039/* Handle of the currently executing image */
40static efi_handle_t current_image;
41
Alexander Graffa5246b2018-11-15 21:23:47 +010042/*
43 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
44 * we need to do trickery with caches. Since we don't want to break the EFI
45 * aware boot path, only apply hacks when loading exiting directly (breaking
46 * direct Linux EFI booting along the way - oh well).
47 */
48static bool efi_is_direct_boot = true;
49
Simon Glasscdfe6962016-09-25 15:27:35 -060050#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010051/*
52 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
53 * fixed when compiling U-Boot. However, the payload does not know about that
54 * restriction so we need to manually swap its and our view of that register on
55 * EFI callback entry/exit.
56 */
57static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060058#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010059
Heinrich Schuchardt72506232019-02-09 14:10:39 +010060/* 1 if inside U-Boot code, 0 if inside EFI payload code */
61static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040062static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010063/* GUID of the device tree table */
64const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010065/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
66const efi_guid_t efi_guid_driver_binding_protocol =
67 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040068
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010069/* event group ExitBootServices() invoked */
70const efi_guid_t efi_guid_event_group_exit_boot_services =
71 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
72/* event group SetVirtualAddressMap() invoked */
73const efi_guid_t efi_guid_event_group_virtual_address_change =
74 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
75/* event group memory map changed */
76const efi_guid_t efi_guid_event_group_memory_map_change =
77 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
78/* event group boot manager about to boot */
79const efi_guid_t efi_guid_event_group_ready_to_boot =
80 EFI_EVENT_GROUP_READY_TO_BOOT;
81/* event group ResetSystem() invoked (before ExitBootServices) */
82const efi_guid_t efi_guid_event_group_reset_system =
83 EFI_EVENT_GROUP_RESET_SYSTEM;
84
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010085static efi_status_t EFIAPI efi_disconnect_controller(
86 efi_handle_t controller_handle,
87 efi_handle_t driver_image_handle,
88 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010089
Rob Clark86789d52017-07-27 08:04:18 -040090/* Called on every callback entry */
91int __efi_entry_check(void)
92{
93 int ret = entry_count++ == 0;
94#ifdef CONFIG_ARM
95 assert(efi_gd);
96 app_gd = gd;
97 gd = efi_gd;
98#endif
99 return ret;
100}
101
102/* Called on every callback exit */
103int __efi_exit_check(void)
104{
105 int ret = --entry_count == 0;
106#ifdef CONFIG_ARM
107 gd = app_gd;
108#endif
109 return ret;
110}
111
Alexander Grafc15d9212016-03-04 01:09:59 +0100112/* Called from do_bootefi_exec() */
113void efi_save_gd(void)
114{
Simon Glasscdfe6962016-09-25 15:27:35 -0600115#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100116 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600117#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100118}
119
Rob Clark86789d52017-07-27 08:04:18 -0400120/*
Mario Six8fac2912018-07-10 08:40:17 +0200121 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200122 * world so we can dump out an abort message, without any care about returning
123 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400124 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100125void efi_restore_gd(void)
126{
Simon Glasscdfe6962016-09-25 15:27:35 -0600127#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100128 /* Only restore if we're already in EFI context */
129 if (!efi_gd)
130 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100131 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600132#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100133}
134
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200135/**
Mario Six8fac2912018-07-10 08:40:17 +0200136 * indent_string() - returns a string for indenting with two spaces per level
137 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100138 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200139 * A maximum of ten indent levels is supported. Higher indent levels will be
140 * truncated.
141 *
Mario Six8fac2912018-07-10 08:40:17 +0200142 * Return: A string for indenting with two spaces per level is
143 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400144 */
145static const char *indent_string(int level)
146{
147 const char *indent = " ";
148 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100149
Rob Clarke7896c32017-07-27 08:04:19 -0400150 level = min(max, level * 2);
151 return &indent[max - level];
152}
153
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200154const char *__efi_nesting(void)
155{
156 return indent_string(nesting_level);
157}
158
Rob Clarke7896c32017-07-27 08:04:19 -0400159const char *__efi_nesting_inc(void)
160{
161 return indent_string(nesting_level++);
162}
163
164const char *__efi_nesting_dec(void)
165{
166 return indent_string(--nesting_level);
167}
168
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200169/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200170 * efi_event_is_queued() - check if an event is queued
171 *
172 * @event: event
173 * Return: true if event is queued
174 */
175static bool efi_event_is_queued(struct efi_event *event)
176{
177 return !!event->queue_link.next;
178}
179
180/**
181 * efi_process_event_queue() - process event queue
182 */
183static void efi_process_event_queue(void)
184{
185 while (!list_empty(&efi_event_queue)) {
186 struct efi_event *event;
187 efi_uintn_t old_tpl;
188
189 event = list_first_entry(&efi_event_queue, struct efi_event,
190 queue_link);
191 if (efi_tpl >= event->notify_tpl)
192 return;
193 list_del(&event->queue_link);
194 event->queue_link.next = NULL;
195 event->queue_link.prev = NULL;
196 /* Events must be executed at the event's TPL */
197 old_tpl = efi_tpl;
198 efi_tpl = event->notify_tpl;
199 EFI_CALL_VOID(event->notify_function(event,
200 event->notify_context));
201 efi_tpl = old_tpl;
202 if (event->type == EVT_NOTIFY_SIGNAL)
203 event->is_signaled = 0;
204 }
205}
206
207/**
Mario Six8fac2912018-07-10 08:40:17 +0200208 * efi_queue_event() - queue an EFI event
209 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200210 *
211 * This function queues the notification function of the event for future
212 * execution.
213 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200214 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200215static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200216{
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200217 struct efi_event *item = NULL;
218
219 if (!event->notify_function)
220 return;
221
222 if (!efi_event_is_queued(event)) {
223 /*
224 * Events must be notified in order of decreasing task priority
225 * level. Insert the new event accordingly.
226 */
227 list_for_each_entry(item, &efi_event_queue, queue_link) {
228 if (item->notify_tpl < event->notify_tpl) {
229 list_add_tail(&event->queue_link,
230 &item->queue_link);
231 event = NULL;
232 break;
233 }
234 }
235 if (event)
236 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200237 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200238 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200239}
240
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200241/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200242 * is_valid_tpl() - check if the task priority level is valid
243 *
244 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200245 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200246 */
247efi_status_t is_valid_tpl(efi_uintn_t tpl)
248{
249 switch (tpl) {
250 case TPL_APPLICATION:
251 case TPL_CALLBACK:
252 case TPL_NOTIFY:
253 case TPL_HIGH_LEVEL:
254 return EFI_SUCCESS;
255 default:
256 return EFI_INVALID_PARAMETER;
257 }
258}
259
260/**
Mario Six8fac2912018-07-10 08:40:17 +0200261 * efi_signal_event() - signal an EFI event
262 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100263 *
Mario Six8fac2912018-07-10 08:40:17 +0200264 * This function signals an event. If the event belongs to an event group all
265 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100266 * their notification function is queued.
267 *
268 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100269 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200270void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100271{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200272 if (event->is_signaled)
273 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100274 if (event->group) {
275 struct efi_event *evt;
276
277 /*
278 * The signaled state has to set before executing any
279 * notification function
280 */
281 list_for_each_entry(evt, &efi_events, link) {
282 if (!evt->group || guidcmp(evt->group, event->group))
283 continue;
284 if (evt->is_signaled)
285 continue;
286 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100287 }
288 list_for_each_entry(evt, &efi_events, link) {
289 if (!evt->group || guidcmp(evt->group, event->group))
290 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200291 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100292 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200293 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100294 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200295 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100296 }
297}
298
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200299/**
Mario Six8fac2912018-07-10 08:40:17 +0200300 * efi_raise_tpl() - raise the task priority level
301 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200302 *
303 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200304 *
305 * See the Unified Extensible Firmware Interface (UEFI) specification for
306 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200307 *
Mario Six8fac2912018-07-10 08:40:17 +0200308 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200309 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100310static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100311{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100312 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200313
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200314 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200315
316 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200317 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200318 efi_tpl = new_tpl;
319 if (efi_tpl > TPL_HIGH_LEVEL)
320 efi_tpl = TPL_HIGH_LEVEL;
321
322 EFI_EXIT(EFI_SUCCESS);
323 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100324}
325
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200326/**
Mario Six8fac2912018-07-10 08:40:17 +0200327 * efi_restore_tpl() - lower the task priority level
328 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200329 *
330 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200331 *
Mario Six8fac2912018-07-10 08:40:17 +0200332 * See the Unified Extensible Firmware Interface (UEFI) specification for
333 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200334 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100335static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100336{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200337 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200338
339 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200340 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200341 efi_tpl = old_tpl;
342 if (efi_tpl > TPL_HIGH_LEVEL)
343 efi_tpl = TPL_HIGH_LEVEL;
344
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100345 /*
346 * Lowering the TPL may have made queued events eligible for execution.
347 */
348 efi_timer_check();
349
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200350 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100351}
352
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200353/**
Mario Six8fac2912018-07-10 08:40:17 +0200354 * efi_allocate_pages_ext() - allocate memory pages
355 * @type: type of allocation to be performed
356 * @memory_type: usage type of the allocated memory
357 * @pages: number of pages to be allocated
358 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200359 *
360 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200361 *
Mario Six8fac2912018-07-10 08:40:17 +0200362 * See the Unified Extensible Firmware Interface (UEFI) specification for
363 * details.
364 *
365 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900367static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100368 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900369 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100370{
371 efi_status_t r;
372
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100373 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100374 r = efi_allocate_pages(type, memory_type, pages, memory);
375 return EFI_EXIT(r);
376}
377
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200378/**
Mario Six8fac2912018-07-10 08:40:17 +0200379 * efi_free_pages_ext() - Free memory pages.
380 * @memory: start of the memory area to be freed
381 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200382 *
383 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200384 *
385 * See the Unified Extensible Firmware Interface (UEFI) specification for
386 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200387 *
Mario Six8fac2912018-07-10 08:40:17 +0200388 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200389 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900390static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100391 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100392{
393 efi_status_t r;
394
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900395 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100396 r = efi_free_pages(memory, pages);
397 return EFI_EXIT(r);
398}
399
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200400/**
Mario Six8fac2912018-07-10 08:40:17 +0200401 * efi_get_memory_map_ext() - get map describing memory usage
402 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
403 * on exit the size of the copied memory map
404 * @memory_map: buffer to which the memory map is written
405 * @map_key: key for the memory map
406 * @descriptor_size: size of an individual memory descriptor
407 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200408 *
409 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200410 *
Mario Six8fac2912018-07-10 08:40:17 +0200411 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * details.
413 *
414 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200415 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900416static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100417 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900418 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100419 efi_uintn_t *map_key,
420 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900421 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100422{
423 efi_status_t r;
424
425 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
426 map_key, descriptor_size, descriptor_version);
427 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
428 descriptor_size, descriptor_version);
429 return EFI_EXIT(r);
430}
431
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200432/**
Mario Six8fac2912018-07-10 08:40:17 +0200433 * efi_allocate_pool_ext() - allocate memory from pool
434 * @pool_type: type of the pool from which memory is to be allocated
435 * @size: number of bytes to be allocated
436 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200437 *
438 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200439 *
440 * See the Unified Extensible Firmware Interface (UEFI) specification for
441 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200442 *
Mario Six8fac2912018-07-10 08:40:17 +0200443 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200444 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200445static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100446 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200447 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100448{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100449 efi_status_t r;
450
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100451 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200452 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100453 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100454}
455
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200456/**
Mario Six8fac2912018-07-10 08:40:17 +0200457 * efi_free_pool_ext() - free memory from pool
458 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200459 *
460 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200461 *
Mario Six8fac2912018-07-10 08:40:17 +0200462 * See the Unified Extensible Firmware Interface (UEFI) specification for
463 * details.
464 *
465 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200466 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200467static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100468{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100469 efi_status_t r;
470
471 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200472 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100473 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100474}
475
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200476/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200477 * efi_add_handle() - add a new handle to the object list
478 *
479 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100480 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200481 * The protocols list is initialized. The handle is added to the list of known
482 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100483 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200484void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100485{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200486 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100487 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200488 INIT_LIST_HEAD(&handle->protocols);
489 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100490}
491
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200492/**
Mario Six8fac2912018-07-10 08:40:17 +0200493 * efi_create_handle() - create handle
494 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200495 *
Mario Six8fac2912018-07-10 08:40:17 +0200496 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200497 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100498efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200499{
500 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200501
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200502 obj = calloc(1, sizeof(struct efi_object));
503 if (!obj)
504 return EFI_OUT_OF_RESOURCES;
505
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100506 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200507 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200508
509 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200510}
511
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200512/**
Mario Six8fac2912018-07-10 08:40:17 +0200513 * efi_search_protocol() - find a protocol on a handle.
514 * @handle: handle
515 * @protocol_guid: GUID of the protocol
516 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100517 *
Mario Six8fac2912018-07-10 08:40:17 +0200518 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100519 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100520efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100521 const efi_guid_t *protocol_guid,
522 struct efi_handler **handler)
523{
524 struct efi_object *efiobj;
525 struct list_head *lhandle;
526
527 if (!handle || !protocol_guid)
528 return EFI_INVALID_PARAMETER;
529 efiobj = efi_search_obj(handle);
530 if (!efiobj)
531 return EFI_INVALID_PARAMETER;
532 list_for_each(lhandle, &efiobj->protocols) {
533 struct efi_handler *protocol;
534
535 protocol = list_entry(lhandle, struct efi_handler, link);
536 if (!guidcmp(protocol->guid, protocol_guid)) {
537 if (handler)
538 *handler = protocol;
539 return EFI_SUCCESS;
540 }
541 }
542 return EFI_NOT_FOUND;
543}
544
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200545/**
Mario Six8fac2912018-07-10 08:40:17 +0200546 * efi_remove_protocol() - delete protocol from a handle
547 * @handle: handle from which the protocol shall be deleted
548 * @protocol: GUID of the protocol to be deleted
549 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100550 *
Mario Six8fac2912018-07-10 08:40:17 +0200551 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100552 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100553efi_status_t efi_remove_protocol(const efi_handle_t handle,
554 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100555 void *protocol_interface)
556{
557 struct efi_handler *handler;
558 efi_status_t ret;
559
560 ret = efi_search_protocol(handle, protocol, &handler);
561 if (ret != EFI_SUCCESS)
562 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200563 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200564 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100565 list_del(&handler->link);
566 free(handler);
567 return EFI_SUCCESS;
568}
569
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200570/**
Mario Six8fac2912018-07-10 08:40:17 +0200571 * efi_remove_all_protocols() - delete all protocols from a handle
572 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100573 *
Mario Six8fac2912018-07-10 08:40:17 +0200574 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100575 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100576efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100577{
578 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100579 struct efi_handler *protocol;
580 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581
582 efiobj = efi_search_obj(handle);
583 if (!efiobj)
584 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100585 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100586 efi_status_t ret;
587
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100588 ret = efi_remove_protocol(handle, protocol->guid,
589 protocol->protocol_interface);
590 if (ret != EFI_SUCCESS)
591 return ret;
592 }
593 return EFI_SUCCESS;
594}
595
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200596/**
Mario Six8fac2912018-07-10 08:40:17 +0200597 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100598 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200599 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200601void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100602{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200603 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100604 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200605 efi_remove_all_protocols(handle);
606 list_del(&handle->link);
607 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100608}
609
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200610/**
Mario Six8fac2912018-07-10 08:40:17 +0200611 * efi_is_event() - check if a pointer is a valid event
612 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100613 *
Mario Six8fac2912018-07-10 08:40:17 +0200614 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100615 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100616static efi_status_t efi_is_event(const struct efi_event *event)
617{
618 const struct efi_event *evt;
619
620 if (!event)
621 return EFI_INVALID_PARAMETER;
622 list_for_each_entry(evt, &efi_events, link) {
623 if (evt == event)
624 return EFI_SUCCESS;
625 }
626 return EFI_INVALID_PARAMETER;
627}
Alexander Grafc15d9212016-03-04 01:09:59 +0100628
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200629/**
Mario Six8fac2912018-07-10 08:40:17 +0200630 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200631 *
Mario Six8fac2912018-07-10 08:40:17 +0200632 * @type: type of the event to create
633 * @notify_tpl: task priority level of the event
634 * @notify_function: notification function of the event
635 * @notify_context: pointer passed to the notification function
636 * @group: event group
637 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200638 *
639 * This function is used inside U-Boot code to create an event.
640 *
641 * For the API function implementing the CreateEvent service see
642 * efi_create_event_ext.
643 *
Mario Six8fac2912018-07-10 08:40:17 +0200644 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200645 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100646efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200647 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200648 struct efi_event *event,
649 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100650 void *notify_context, efi_guid_t *group,
651 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100652{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100653 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200654 efi_status_t ret;
655 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200656
Jonathan Gray7758b212017-03-12 19:26:07 +1100657 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200658 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100659
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200660 switch (type) {
661 case 0:
662 case EVT_TIMER:
663 case EVT_NOTIFY_SIGNAL:
664 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
665 case EVT_NOTIFY_WAIT:
666 case EVT_TIMER | EVT_NOTIFY_WAIT:
667 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200668 pool_type = EFI_BOOT_SERVICES_DATA;
669 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200670 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200671 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200672 break;
673 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200674 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200675 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100676
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900677 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200678 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200679 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100680
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200681 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
682 (void **)&evt);
683 if (ret != EFI_SUCCESS)
684 return ret;
685 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100686 evt->type = type;
687 evt->notify_tpl = notify_tpl;
688 evt->notify_function = notify_function;
689 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100690 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200691 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100692 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100693 list_add_tail(&evt->link, &efi_events);
694 *event = evt;
695 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100696}
697
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200698/*
Mario Six8fac2912018-07-10 08:40:17 +0200699 * efi_create_event_ex() - create an event in a group
700 * @type: type of the event to create
701 * @notify_tpl: task priority level of the event
702 * @notify_function: notification function of the event
703 * @notify_context: pointer passed to the notification function
704 * @event: created event
705 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100706 *
707 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100708 *
Mario Six8fac2912018-07-10 08:40:17 +0200709 * See the Unified Extensible Firmware Interface (UEFI) specification for
710 * details.
711 *
712 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100713 */
714efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
715 void (EFIAPI *notify_function) (
716 struct efi_event *event,
717 void *context),
718 void *notify_context,
719 efi_guid_t *event_group,
720 struct efi_event **event)
721{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200722 efi_status_t ret;
723
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100724 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
725 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200726
727 /*
728 * The allowable input parameters are the same as in CreateEvent()
729 * except for the following two disallowed event types.
730 */
731 switch (type) {
732 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
733 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
734 ret = EFI_INVALID_PARAMETER;
735 goto out;
736 }
737
738 ret = efi_create_event(type, notify_tpl, notify_function,
739 notify_context, event_group, event);
740out:
741 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100742}
743
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200744/**
Mario Six8fac2912018-07-10 08:40:17 +0200745 * efi_create_event_ext() - create an event
746 * @type: type of the event to create
747 * @notify_tpl: task priority level of the event
748 * @notify_function: notification function of the event
749 * @notify_context: pointer passed to the notification function
750 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200751 *
752 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200753 *
754 * See the Unified Extensible Firmware Interface (UEFI) specification for
755 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200756 *
Mario Six8fac2912018-07-10 08:40:17 +0200757 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200758 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200759static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100760 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200761 void (EFIAPI *notify_function) (
762 struct efi_event *event,
763 void *context),
764 void *notify_context, struct efi_event **event)
765{
766 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
767 notify_context);
768 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100769 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200770}
771
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200772/**
Mario Six8fac2912018-07-10 08:40:17 +0200773 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200774 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200775 * Check if a timer event has occurred or a queued notification function should
776 * be called.
777 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100778 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200779 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100780 */
781void efi_timer_check(void)
782{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100783 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100784 u64 now = timer_get_us();
785
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100786 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200787 if (!timers_enabled)
788 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100789 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200790 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100791 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200792 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100793 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200794 break;
795 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100796 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200797 break;
798 default:
799 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200800 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100801 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200802 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100803 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200804 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100805 WATCHDOG_RESET();
806}
807
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200808/**
Mario Six8fac2912018-07-10 08:40:17 +0200809 * efi_set_timer() - set the trigger time for a timer event or stop the event
810 * @event: event for which the timer is set
811 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200812 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200813 *
814 * This is the function for internal usage in U-Boot. For the API function
815 * implementing the SetTimer service see efi_set_timer_ext.
816 *
Mario Six8fac2912018-07-10 08:40:17 +0200817 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200818 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200819efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200820 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100821{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100822 /* Check that the event is valid */
823 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
824 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100825
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200826 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200827 * The parameter defines a multiple of 100 ns.
828 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200829 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200830 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100831
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100832 switch (type) {
833 case EFI_TIMER_STOP:
834 event->trigger_next = -1ULL;
835 break;
836 case EFI_TIMER_PERIODIC:
837 case EFI_TIMER_RELATIVE:
838 event->trigger_next = timer_get_us() + trigger_time;
839 break;
840 default:
841 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100842 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100843 event->trigger_type = type;
844 event->trigger_time = trigger_time;
845 event->is_signaled = false;
846 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100847}
848
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200849/**
Mario Six8fac2912018-07-10 08:40:17 +0200850 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
851 * event
852 * @event: event for which the timer is set
853 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200854 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200855 *
856 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200857 *
Mario Six8fac2912018-07-10 08:40:17 +0200858 * See the Unified Extensible Firmware Interface (UEFI) specification for
859 * details.
860 *
861 *
862 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200863 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200864static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
865 enum efi_timer_delay type,
866 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200867{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900868 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200869 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
870}
871
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200872/**
Mario Six8fac2912018-07-10 08:40:17 +0200873 * efi_wait_for_event() - wait for events to be signaled
874 * @num_events: number of events to be waited for
875 * @event: events to be waited for
876 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200877 *
878 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200879 *
Mario Six8fac2912018-07-10 08:40:17 +0200880 * See the Unified Extensible Firmware Interface (UEFI) specification for
881 * details.
882 *
883 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100885static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200886 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100887 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100888{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100889 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100890
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100891 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100892
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200893 /* Check parameters */
894 if (!num_events || !event)
895 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200896 /* Check TPL */
897 if (efi_tpl != TPL_APPLICATION)
898 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200899 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100900 if (efi_is_event(event[i]) != EFI_SUCCESS)
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200902 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
903 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200904 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200905 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200906 }
907
908 /* Wait for signal */
909 for (;;) {
910 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200911 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200912 goto out;
913 }
914 /* Allow events to occur. */
915 efi_timer_check();
916 }
917
918out:
919 /*
920 * Reset the signal which is passed to the caller to allow periodic
921 * events to occur.
922 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200923 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200924 if (index)
925 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100926
927 return EFI_EXIT(EFI_SUCCESS);
928}
929
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200930/**
Mario Six8fac2912018-07-10 08:40:17 +0200931 * efi_signal_event_ext() - signal an EFI event
932 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200933 *
934 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200935 *
936 * See the Unified Extensible Firmware Interface (UEFI) specification for
937 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200938 *
939 * This functions sets the signaled state of the event and queues the
940 * notification function for execution.
941 *
Mario Six8fac2912018-07-10 08:40:17 +0200942 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200943 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200944static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100945{
946 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100947 if (efi_is_event(event) != EFI_SUCCESS)
948 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200949 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100950 return EFI_EXIT(EFI_SUCCESS);
951}
952
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200953/**
Mario Six8fac2912018-07-10 08:40:17 +0200954 * efi_close_event() - close an EFI event
955 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200956 *
957 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200958 *
Mario Six8fac2912018-07-10 08:40:17 +0200959 * See the Unified Extensible Firmware Interface (UEFI) specification for
960 * details.
961 *
962 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200963 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200964static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100965{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200966 struct efi_register_notify_event *item, *next;
967
Alexander Grafc15d9212016-03-04 01:09:59 +0100968 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100969 if (efi_is_event(event) != EFI_SUCCESS)
970 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200971
972 /* Remove protocol notify registrations for the event */
973 list_for_each_entry_safe(item, next, &efi_register_notify_events,
974 link) {
975 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200976 struct efi_protocol_notification *hitem, *hnext;
977
978 /* Remove signaled handles */
979 list_for_each_entry_safe(hitem, hnext, &item->handles,
980 link) {
981 list_del(&hitem->link);
982 free(hitem);
983 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200984 list_del(&item->link);
985 free(item);
986 }
987 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200988 /* Remove event from queue */
989 if (efi_event_is_queued(event))
990 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200991
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100992 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200993 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100994 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100995}
996
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200997/**
Mario Six8fac2912018-07-10 08:40:17 +0200998 * efi_check_event() - check if an event is signaled
999 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001000 *
1001 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001002 *
1003 * See the Unified Extensible Firmware Interface (UEFI) specification for
1004 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001005 *
Mario Six8fac2912018-07-10 08:40:17 +02001006 * If an event is not signaled yet, the notification function is queued. The
1007 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001008 *
Mario Six8fac2912018-07-10 08:40:17 +02001009 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001010 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001011static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001012{
1013 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001014 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001015 if (efi_is_event(event) != EFI_SUCCESS ||
1016 event->type & EVT_NOTIFY_SIGNAL)
1017 return EFI_EXIT(EFI_INVALID_PARAMETER);
1018 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001019 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001020 if (event->is_signaled) {
1021 event->is_signaled = false;
1022 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001023 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001024 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001025}
1026
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001027/**
Mario Six8fac2912018-07-10 08:40:17 +02001028 * efi_search_obj() - find the internal EFI object for a handle
1029 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001030 *
Mario Six8fac2912018-07-10 08:40:17 +02001031 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001032 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001033struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001034{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001035 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001036
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001037 if (!handle)
1038 return NULL;
1039
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001040 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001041 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001042 return efiobj;
1043 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001044 return NULL;
1045}
1046
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001047/**
Mario Six8fac2912018-07-10 08:40:17 +02001048 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1049 * to a protocol
1050 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001051 *
Mario Six8fac2912018-07-10 08:40:17 +02001052 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001053 */
1054static struct efi_open_protocol_info_entry *efi_create_open_info(
1055 struct efi_handler *handler)
1056{
1057 struct efi_open_protocol_info_item *item;
1058
1059 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1060 if (!item)
1061 return NULL;
1062 /* Append the item to the open protocol info list. */
1063 list_add_tail(&item->link, &handler->open_infos);
1064
1065 return &item->info;
1066}
1067
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001068/**
Mario Six8fac2912018-07-10 08:40:17 +02001069 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1070 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001071 *
Mario Six8fac2912018-07-10 08:40:17 +02001072 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001073 */
1074static efi_status_t efi_delete_open_info(
1075 struct efi_open_protocol_info_item *item)
1076{
1077 list_del(&item->link);
1078 free(item);
1079 return EFI_SUCCESS;
1080}
1081
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001082/**
Mario Six8fac2912018-07-10 08:40:17 +02001083 * efi_add_protocol() - install new protocol on a handle
1084 * @handle: handle on which the protocol shall be installed
1085 * @protocol: GUID of the protocol to be installed
1086 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001087 *
Mario Six8fac2912018-07-10 08:40:17 +02001088 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001089 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001090efi_status_t efi_add_protocol(const efi_handle_t handle,
1091 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001092 void *protocol_interface)
1093{
1094 struct efi_object *efiobj;
1095 struct efi_handler *handler;
1096 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001097 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001098
1099 efiobj = efi_search_obj(handle);
1100 if (!efiobj)
1101 return EFI_INVALID_PARAMETER;
1102 ret = efi_search_protocol(handle, protocol, NULL);
1103 if (ret != EFI_NOT_FOUND)
1104 return EFI_INVALID_PARAMETER;
1105 handler = calloc(1, sizeof(struct efi_handler));
1106 if (!handler)
1107 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001108 handler->guid = protocol;
1109 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001110 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001111 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001112
1113 /* Notify registered events */
1114 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001115 if (!guidcmp(protocol, &event->protocol)) {
1116 struct efi_protocol_notification *notif;
1117
1118 notif = calloc(1, sizeof(*notif));
1119 if (!notif) {
1120 list_del(&handler->link);
1121 free(handler);
1122 return EFI_OUT_OF_RESOURCES;
1123 }
1124 notif->handle = handle;
1125 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001126 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001127 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001128 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001129 }
1130
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001131 if (!guidcmp(&efi_guid_device_path, protocol))
1132 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001133 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001134}
1135
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001136/**
Mario Six8fac2912018-07-10 08:40:17 +02001137 * efi_install_protocol_interface() - install protocol interface
1138 * @handle: handle on which the protocol shall be installed
1139 * @protocol: GUID of the protocol to be installed
1140 * @protocol_interface_type: type of the interface to be installed,
1141 * always EFI_NATIVE_INTERFACE
1142 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001143 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001144 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001145 *
Mario Six8fac2912018-07-10 08:40:17 +02001146 * See the Unified Extensible Firmware Interface (UEFI) specification for
1147 * details.
1148 *
1149 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001150 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001151static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001152 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001153 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001154{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001155 efi_status_t r;
1156
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001157 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1158 protocol_interface);
1159
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001160 if (!handle || !protocol ||
1161 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1162 r = EFI_INVALID_PARAMETER;
1163 goto out;
1164 }
1165
1166 /* Create new handle if requested. */
1167 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001168 r = efi_create_handle(handle);
1169 if (r != EFI_SUCCESS)
1170 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001171 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001172 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001173 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001174 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001175 /* Add new protocol */
1176 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001177out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001178 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001179}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001180
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001181/**
Mario Six8fac2912018-07-10 08:40:17 +02001182 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001183 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001184 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001185 * @number_of_drivers: number of child controllers
1186 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001187 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001188 * The allocated buffer has to be freed with free().
1189 *
Mario Six8fac2912018-07-10 08:40:17 +02001190 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001191 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001192static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001193 const efi_guid_t *protocol,
1194 efi_uintn_t *number_of_drivers,
1195 efi_handle_t **driver_handle_buffer)
1196{
1197 struct efi_handler *handler;
1198 struct efi_open_protocol_info_item *item;
1199 efi_uintn_t count = 0, i;
1200 bool duplicate;
1201
1202 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001203 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001204 if (protocol && guidcmp(handler->guid, protocol))
1205 continue;
1206 list_for_each_entry(item, &handler->open_infos, link) {
1207 if (item->info.attributes &
1208 EFI_OPEN_PROTOCOL_BY_DRIVER)
1209 ++count;
1210 }
1211 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001212 *number_of_drivers = 0;
1213 if (!count) {
1214 *driver_handle_buffer = NULL;
1215 return EFI_SUCCESS;
1216 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001217 /*
1218 * Create buffer. In case of duplicate driver assignments the buffer
1219 * will be too large. But that does not harm.
1220 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001221 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1222 if (!*driver_handle_buffer)
1223 return EFI_OUT_OF_RESOURCES;
1224 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001225 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001226 if (protocol && guidcmp(handler->guid, protocol))
1227 continue;
1228 list_for_each_entry(item, &handler->open_infos, link) {
1229 if (item->info.attributes &
1230 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1231 /* Check this is a new driver */
1232 duplicate = false;
1233 for (i = 0; i < *number_of_drivers; ++i) {
1234 if ((*driver_handle_buffer)[i] ==
1235 item->info.agent_handle)
1236 duplicate = true;
1237 }
1238 /* Copy handle to buffer */
1239 if (!duplicate) {
1240 i = (*number_of_drivers)++;
1241 (*driver_handle_buffer)[i] =
1242 item->info.agent_handle;
1243 }
1244 }
1245 }
1246 }
1247 return EFI_SUCCESS;
1248}
1249
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001250/**
Mario Six8fac2912018-07-10 08:40:17 +02001251 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001252 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001253 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001254 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001255 *
1256 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001257 *
1258 * See the Unified Extensible Firmware Interface (UEFI) specification for
1259 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001260 *
Mario Six8fac2912018-07-10 08:40:17 +02001261 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001262 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001263static efi_status_t efi_disconnect_all_drivers
1264 (efi_handle_t handle,
1265 const efi_guid_t *protocol,
1266 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001267{
1268 efi_uintn_t number_of_drivers;
1269 efi_handle_t *driver_handle_buffer;
1270 efi_status_t r, ret;
1271
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001272 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001273 &driver_handle_buffer);
1274 if (ret != EFI_SUCCESS)
1275 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001276 if (!number_of_drivers)
1277 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001278 ret = EFI_NOT_FOUND;
1279 while (number_of_drivers) {
1280 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001281 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001282 driver_handle_buffer[--number_of_drivers],
1283 child_handle));
1284 if (r == EFI_SUCCESS)
1285 ret = r;
1286 }
1287 free(driver_handle_buffer);
1288 return ret;
1289}
1290
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001291/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001292 * efi_uninstall_protocol() - uninstall protocol interface
1293 *
Mario Six8fac2912018-07-10 08:40:17 +02001294 * @handle: handle from which the protocol shall be removed
1295 * @protocol: GUID of the protocol to be removed
1296 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001297 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001298 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001299 *
1300 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001301 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001302static efi_status_t efi_uninstall_protocol
1303 (efi_handle_t handle, const efi_guid_t *protocol,
1304 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001305{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001306 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001307 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001308 struct efi_open_protocol_info_item *item;
1309 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001310 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001311
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001312 /* Check handle */
1313 efiobj = efi_search_obj(handle);
1314 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001315 r = EFI_INVALID_PARAMETER;
1316 goto out;
1317 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001318 /* Find the protocol on the handle */
1319 r = efi_search_protocol(handle, protocol, &handler);
1320 if (r != EFI_SUCCESS)
1321 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001322 /* Disconnect controllers */
1323 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001324 /* Close protocol */
1325 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1326 if (item->info.attributes ==
1327 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1328 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1329 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1330 list_del(&item->link);
1331 }
1332 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001333 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001334 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001335 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001336 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001337out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001338 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001339}
1340
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001341/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001342 * efi_uninstall_protocol_interface() - uninstall protocol interface
1343 * @handle: handle from which the protocol shall be removed
1344 * @protocol: GUID of the protocol to be removed
1345 * @protocol_interface: interface to be removed
1346 *
1347 * This function implements the UninstallProtocolInterface service.
1348 *
1349 * See the Unified Extensible Firmware Interface (UEFI) specification for
1350 * details.
1351 *
1352 * Return: status code
1353 */
1354static efi_status_t EFIAPI efi_uninstall_protocol_interface
1355 (efi_handle_t handle, const efi_guid_t *protocol,
1356 void *protocol_interface)
1357{
1358 efi_status_t ret;
1359
1360 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1361
1362 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1363 if (ret != EFI_SUCCESS)
1364 goto out;
1365
1366 /* If the last protocol has been removed, delete the handle. */
1367 if (list_empty(&handle->protocols)) {
1368 list_del(&handle->link);
1369 free(handle);
1370 }
1371out:
1372 return EFI_EXIT(ret);
1373}
1374
1375/**
Mario Six8fac2912018-07-10 08:40:17 +02001376 * efi_register_protocol_notify() - register an event for notification when a
1377 * protocol is installed.
1378 * @protocol: GUID of the protocol whose installation shall be notified
1379 * @event: event to be signaled upon installation of the protocol
1380 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001381 *
1382 * This function implements the RegisterProtocolNotify service.
1383 * See the Unified Extensible Firmware Interface (UEFI) specification
1384 * for details.
1385 *
Mario Six8fac2912018-07-10 08:40:17 +02001386 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001387 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001388static efi_status_t EFIAPI efi_register_protocol_notify(
1389 const efi_guid_t *protocol,
1390 struct efi_event *event,
1391 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001392{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001393 struct efi_register_notify_event *item;
1394 efi_status_t ret = EFI_SUCCESS;
1395
Rob Clark238f88c2017-09-13 18:05:41 -04001396 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001397
1398 if (!protocol || !event || !registration) {
1399 ret = EFI_INVALID_PARAMETER;
1400 goto out;
1401 }
1402
1403 item = calloc(1, sizeof(struct efi_register_notify_event));
1404 if (!item) {
1405 ret = EFI_OUT_OF_RESOURCES;
1406 goto out;
1407 }
1408
1409 item->event = event;
1410 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001411 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001412
1413 list_add_tail(&item->link, &efi_register_notify_events);
1414
1415 *registration = item;
1416out:
1417 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001418}
1419
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001420/**
Mario Six8fac2912018-07-10 08:40:17 +02001421 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001422 *
Mario Six8fac2912018-07-10 08:40:17 +02001423 * @search_type: selection criterion
1424 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001425 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001426 *
1427 * See the documentation of the LocateHandle service in the UEFI specification.
1428 *
Mario Six8fac2912018-07-10 08:40:17 +02001429 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001430 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001431static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001432 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001433{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001434 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001435
1436 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001437 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001438 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001439 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001440 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001441 return (ret != EFI_SUCCESS);
1442 default:
1443 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001444 return -1;
1445 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001446}
1447
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001448/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001449 * efi_check_register_notify_event() - check if registration key is valid
1450 *
1451 * Check that a pointer is a valid registration key as returned by
1452 * RegisterProtocolNotify().
1453 *
1454 * @key: registration key
1455 * Return: valid registration key or NULL
1456 */
1457static struct efi_register_notify_event *efi_check_register_notify_event
1458 (void *key)
1459{
1460 struct efi_register_notify_event *event;
1461
1462 list_for_each_entry(event, &efi_register_notify_events, link) {
1463 if (event == (struct efi_register_notify_event *)key)
1464 return event;
1465 }
1466 return NULL;
1467}
1468
1469/**
Mario Six8fac2912018-07-10 08:40:17 +02001470 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001471 *
1472 * @search_type: selection criterion
1473 * @protocol: GUID of the protocol
1474 * @search_key: registration key
1475 * @buffer_size: size of the buffer to receive the handles in bytes
1476 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001477 *
1478 * This function is meant for U-Boot internal calls. For the API implementation
1479 * of the LocateHandle service see efi_locate_handle_ext.
1480 *
Mario Six8fac2912018-07-10 08:40:17 +02001481 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001482 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001483static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001484 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001485 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001486 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001487{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001488 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001489 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001490 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001491 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001492
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001493 /* Check parameters */
1494 switch (search_type) {
1495 case ALL_HANDLES:
1496 break;
1497 case BY_REGISTER_NOTIFY:
1498 if (!search_key)
1499 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001500 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001501 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001502 if (!event)
1503 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001504 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001505 case BY_PROTOCOL:
1506 if (!protocol)
1507 return EFI_INVALID_PARAMETER;
1508 break;
1509 default:
1510 return EFI_INVALID_PARAMETER;
1511 }
1512
Alexander Grafc15d9212016-03-04 01:09:59 +01001513 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001514 if (search_type == BY_REGISTER_NOTIFY) {
1515 if (list_empty(&event->handles))
1516 return EFI_NOT_FOUND;
1517 handle = list_first_entry(&event->handles,
1518 struct efi_protocol_notification,
1519 link);
1520 efiobj = handle->handle;
1521 size += sizeof(void *);
1522 } else {
1523 list_for_each_entry(efiobj, &efi_obj_list, link) {
1524 if (!efi_search(search_type, protocol, efiobj))
1525 size += sizeof(void *);
1526 }
1527 if (size == 0)
1528 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001529 }
1530
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001531 if (!buffer_size)
1532 return EFI_INVALID_PARAMETER;
1533
Alexander Grafc15d9212016-03-04 01:09:59 +01001534 if (*buffer_size < size) {
1535 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001536 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001537 }
1538
Rob Clarkcdee3372017-08-06 14:10:07 -04001539 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001540
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001541 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001542 if (!buffer)
1543 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001544
Alexander Grafc15d9212016-03-04 01:09:59 +01001545 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001546 if (search_type == BY_REGISTER_NOTIFY) {
1547 *buffer = efiobj;
1548 list_del(&handle->link);
1549 } else {
1550 list_for_each_entry(efiobj, &efi_obj_list, link) {
1551 if (!efi_search(search_type, protocol, efiobj))
1552 *buffer++ = efiobj;
1553 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001554 }
1555
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001556 return EFI_SUCCESS;
1557}
1558
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001559/**
Mario Six8fac2912018-07-10 08:40:17 +02001560 * efi_locate_handle_ext() - locate handles implementing a protocol.
1561 * @search_type: selection criterion
1562 * @protocol: GUID of the protocol
1563 * @search_key: registration key
1564 * @buffer_size: size of the buffer to receive the handles in bytes
1565 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001566 *
1567 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001568 *
Mario Six8fac2912018-07-10 08:40:17 +02001569 * See the Unified Extensible Firmware Interface (UEFI) specification for
1570 * details.
1571 *
1572 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001573 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001574static efi_status_t EFIAPI efi_locate_handle_ext(
1575 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001576 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001577 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001578{
Rob Clark238f88c2017-09-13 18:05:41 -04001579 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001580 buffer_size, buffer);
1581
1582 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1583 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001584}
1585
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001586/**
Mario Six8fac2912018-07-10 08:40:17 +02001587 * efi_remove_configuration_table() - collapses configuration table entries,
1588 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001589 *
Mario Six8fac2912018-07-10 08:40:17 +02001590 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001591 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001592static void efi_remove_configuration_table(int i)
1593{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001594 struct efi_configuration_table *this = &systab.tables[i];
1595 struct efi_configuration_table *next = &systab.tables[i + 1];
1596 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001597
1598 memmove(this, next, (ulong)end - (ulong)next);
1599 systab.nr_tables--;
1600}
1601
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001602/**
Mario Six8fac2912018-07-10 08:40:17 +02001603 * efi_install_configuration_table() - adds, updates, or removes a
1604 * configuration table
1605 * @guid: GUID of the installed table
1606 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001607 *
1608 * This function is used for internal calls. For the API implementation of the
1609 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1610 *
Mario Six8fac2912018-07-10 08:40:17 +02001611 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001612 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001613efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1614 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001615{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001616 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001617 int i;
1618
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001619 if (!guid)
1620 return EFI_INVALID_PARAMETER;
1621
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001622 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001623 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001624 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001625 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001626 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001627 else
1628 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001629 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001630 }
1631 }
1632
Alexander Graffe3366f2017-07-26 13:41:04 +02001633 if (!table)
1634 return EFI_NOT_FOUND;
1635
Alexander Grafc15d9212016-03-04 01:09:59 +01001636 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001637 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001638 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001639
1640 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001641 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1642 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001643 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001644
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001645out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001646 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001647 efi_update_table_header_crc32(&systab.hdr);
1648
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001649 /* Notify that the configuration table was changed */
1650 list_for_each_entry(evt, &efi_events, link) {
1651 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001652 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001653 break;
1654 }
1655 }
1656
Alexander Grafc5c11632016-08-19 01:23:24 +02001657 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001658}
1659
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001660/**
Mario Six8fac2912018-07-10 08:40:17 +02001661 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1662 * configuration table.
1663 * @guid: GUID of the installed table
1664 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001665 *
1666 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001667 *
Mario Six8fac2912018-07-10 08:40:17 +02001668 * See the Unified Extensible Firmware Interface (UEFI) specification for
1669 * details.
1670 *
1671 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001672 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001673static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1674 void *table)
1675{
Rob Clark238f88c2017-09-13 18:05:41 -04001676 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001677 return EFI_EXIT(efi_install_configuration_table(guid, table));
1678}
1679
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001680/**
Mario Six8fac2912018-07-10 08:40:17 +02001681 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001682 *
1683 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001684 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001685 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001686 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001687 * code is returned.
1688 *
1689 * @device_path: device path of the loaded image
1690 * @file_path: file path of the loaded image
1691 * @handle_ptr: handle of the loaded image
1692 * @info_ptr: loaded image protocol
1693 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001694 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001695efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1696 struct efi_device_path *file_path,
1697 struct efi_loaded_image_obj **handle_ptr,
1698 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001699{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001700 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001701 struct efi_loaded_image *info = NULL;
1702 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001703 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001704
1705 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1706 *handle_ptr = NULL;
1707 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001708
1709 info = calloc(1, sizeof(*info));
1710 if (!info)
1711 return EFI_OUT_OF_RESOURCES;
1712 obj = calloc(1, sizeof(*obj));
1713 if (!obj) {
1714 free(info);
1715 return EFI_OUT_OF_RESOURCES;
1716 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001717 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001718
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001719 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001720 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001721
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001722 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001723 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001724 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001725
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001726 if (device_path) {
1727 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001728
1729 dp = efi_dp_append(device_path, file_path);
1730 if (!dp) {
1731 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001732 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001733 }
1734 } else {
1735 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001736 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001737 ret = efi_add_protocol(&obj->header,
1738 &efi_guid_loaded_image_device_path, dp);
1739 if (ret != EFI_SUCCESS)
1740 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001741
1742 /*
1743 * When asking for the loaded_image interface, just
1744 * return handle which points to loaded_image_info
1745 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001746 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001747 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001748 if (ret != EFI_SUCCESS)
1749 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001750
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001751 *info_ptr = info;
1752 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001753
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001754 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001755failure:
1756 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001757 efi_delete_handle(&obj->header);
1758 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001759 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001760}
1761
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001762/**
Mario Six8fac2912018-07-10 08:40:17 +02001763 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001764 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001765 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1766 * callers obligation to update the memory type as needed.
1767 *
1768 * @file_path: the path of the image to load
1769 * @buffer: buffer containing the loaded image
1770 * @size: size of the loaded image
1771 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001772 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001773static
Rob Clarkc84c1102017-09-13 18:05:38 -04001774efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001775 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001776{
1777 struct efi_file_info *info = NULL;
1778 struct efi_file_handle *f;
1779 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001780 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001781 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001782
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001783 /* In case of failure nothing is returned */
1784 *buffer = NULL;
1785 *size = 0;
1786
1787 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001788 f = efi_file_from_path(file_path);
1789 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001790 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001791
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001792 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001793 bs = 0;
1794 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1795 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001796 if (ret != EFI_BUFFER_TOO_SMALL) {
1797 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001798 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001799 }
Rob Clark857a1222017-09-13 18:05:35 -04001800
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001801 info = malloc(bs);
1802 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1803 info));
1804 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001805 goto error;
1806
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001807 /*
1808 * When reading the file we do not yet know if it contains an
1809 * application, a boottime driver, or a runtime driver. So here we
1810 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1811 * update the reservation according to the image type.
1812 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001813 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001814 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1815 EFI_BOOT_SERVICES_DATA,
1816 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001817 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001818 ret = EFI_OUT_OF_RESOURCES;
1819 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001820 }
1821
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001822 /* Read file */
1823 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1824 if (ret != EFI_SUCCESS)
1825 efi_free_pages(addr, efi_size_in_pages(bs));
1826 *buffer = (void *)(uintptr_t)addr;
1827 *size = bs;
1828error:
1829 EFI_CALL(f->close(f));
1830 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001831 return ret;
1832}
1833
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001834/**
Mario Six8fac2912018-07-10 08:40:17 +02001835 * efi_load_image() - load an EFI image into memory
1836 * @boot_policy: true for request originating from the boot manager
1837 * @parent_image: the caller's image handle
1838 * @file_path: the path of the image to load
1839 * @source_buffer: memory location from which the image is installed
1840 * @source_size: size of the memory area from which the image is installed
1841 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001842 *
1843 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001844 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001845 * See the Unified Extensible Firmware Interface (UEFI) specification
1846 * for details.
1847 *
Mario Six8fac2912018-07-10 08:40:17 +02001848 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001849 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001850efi_status_t EFIAPI efi_load_image(bool boot_policy,
1851 efi_handle_t parent_image,
1852 struct efi_device_path *file_path,
1853 void *source_buffer,
1854 efi_uintn_t source_size,
1855 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001856{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001857 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001858 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001859 struct efi_loaded_image_obj **image_obj =
1860 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001861 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001862 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001863
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001864 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001865 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001866
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001867 if (!image_handle || (!source_buffer && !file_path) ||
1868 !efi_search_obj(parent_image) ||
1869 /* The parent image handle must refer to a loaded image */
1870 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001871 ret = EFI_INVALID_PARAMETER;
1872 goto error;
1873 }
Rob Clark857a1222017-09-13 18:05:35 -04001874
1875 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001876 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001877 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001878 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001879 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001880 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001881 if (!source_size) {
1882 ret = EFI_LOAD_ERROR;
1883 goto error;
1884 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001885 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001886 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001887 /* split file_path which contains both the device and file parts */
1888 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001889 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001890 if (ret == EFI_SUCCESS)
1891 ret = efi_load_pe(*image_obj, dest_buffer, info);
1892 if (!source_buffer)
1893 /* Release buffer to which file was loaded */
1894 efi_free_pages((uintptr_t)dest_buffer,
1895 efi_size_in_pages(source_size));
1896 if (ret == EFI_SUCCESS) {
1897 info->system_table = &systab;
1898 info->parent_handle = parent_image;
1899 } else {
1900 /* The image is invalid. Release all associated resources. */
1901 efi_delete_handle(*image_handle);
1902 *image_handle = NULL;
1903 free(info);
1904 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001905error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001906 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001907}
1908
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001909/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001910 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1911 */
1912static void efi_exit_caches(void)
1913{
1914#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1915 /*
1916 * Grub on 32bit ARM needs to have caches disabled before jumping into
1917 * a zImage, but does not know of all cache layers. Give it a hand.
1918 */
1919 if (efi_is_direct_boot)
1920 cleanup_before_linux();
1921#endif
1922}
1923
1924/**
Mario Six8fac2912018-07-10 08:40:17 +02001925 * efi_exit_boot_services() - stop all boot services
1926 * @image_handle: handle of the loaded image
1927 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001928 *
1929 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001930 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001931 * See the Unified Extensible Firmware Interface (UEFI) specification
1932 * for details.
1933 *
Mario Six8fac2912018-07-10 08:40:17 +02001934 * All timer events are disabled. For exit boot services events the
1935 * notification function is called. The boot services are disabled in the
1936 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001937 *
Mario Six8fac2912018-07-10 08:40:17 +02001938 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001939 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001940static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001941 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001942{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001943 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001944 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001945
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001946 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001947
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001948 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001949 if (map_key != efi_memory_map_key) {
1950 ret = EFI_INVALID_PARAMETER;
1951 goto out;
1952 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001953
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001954 /* Check if ExitBootServices has already been called */
1955 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001956 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001957
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001958 /* Stop all timer related activities */
1959 timers_enabled = false;
1960
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001961 /* Add related events to the event group */
1962 list_for_each_entry(evt, &efi_events, link) {
1963 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1964 evt->group = &efi_guid_event_group_exit_boot_services;
1965 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001966 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001967 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001968 if (evt->group &&
1969 !guidcmp(evt->group,
1970 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001971 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001972 break;
1973 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001974 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001975
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001976 /* Make sure that notification functions are not called anymore */
1977 efi_tpl = TPL_HIGH_LEVEL;
1978
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001979 /* Notify variable services */
1980 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001981
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001982 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1983 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1984 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1985 list_del(&evt->link);
1986 }
1987
Alexander Graf2ebeb442016-11-17 01:02:57 +01001988 board_quiesce_devices();
1989
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02001990 /* Patch out unsupported runtime function */
1991 efi_runtime_detach();
1992
Alexander Graffa5246b2018-11-15 21:23:47 +01001993 /* Fix up caches for EFI payloads if necessary */
1994 efi_exit_caches();
1995
Alexander Grafc15d9212016-03-04 01:09:59 +01001996 /* This stops all lingering devices */
1997 bootm_disable_interrupts();
1998
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001999 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002000 systab.con_in_handle = NULL;
2001 systab.con_in = NULL;
2002 systab.con_out_handle = NULL;
2003 systab.con_out = NULL;
2004 systab.stderr_handle = NULL;
2005 systab.std_err = NULL;
2006 systab.boottime = NULL;
2007
2008 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002009 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002010
Alexander Grafc15d9212016-03-04 01:09:59 +01002011 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002012 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002013 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002014out:
2015 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002016}
2017
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002018/**
Mario Six8fac2912018-07-10 08:40:17 +02002019 * efi_get_next_monotonic_count() - get next value of the counter
2020 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002021 *
2022 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002023 *
2024 * See the Unified Extensible Firmware Interface (UEFI) specification for
2025 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002026 *
Mario Six8fac2912018-07-10 08:40:17 +02002027 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002028 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002029static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2030{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002031 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002032 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002033
Alexander Grafc15d9212016-03-04 01:09:59 +01002034 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002035 if (!count) {
2036 ret = EFI_INVALID_PARAMETER;
2037 goto out;
2038 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002039 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002040 ret = EFI_SUCCESS;
2041out:
2042 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002043}
2044
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002045/**
Mario Six8fac2912018-07-10 08:40:17 +02002046 * efi_stall() - sleep
2047 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002048 *
Mario Six8fac2912018-07-10 08:40:17 +02002049 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002050 *
Mario Six8fac2912018-07-10 08:40:17 +02002051 * See the Unified Extensible Firmware Interface (UEFI) specification for
2052 * details.
2053 *
2054 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002055 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002056static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2057{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002058 u64 end_tick;
2059
Alexander Grafc15d9212016-03-04 01:09:59 +01002060 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002061
2062 end_tick = get_ticks() + usec_to_tick(microseconds);
2063 while (get_ticks() < end_tick)
2064 efi_timer_check();
2065
Alexander Grafc15d9212016-03-04 01:09:59 +01002066 return EFI_EXIT(EFI_SUCCESS);
2067}
2068
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002069/**
Mario Six8fac2912018-07-10 08:40:17 +02002070 * efi_set_watchdog_timer() - reset the watchdog timer
2071 * @timeout: seconds before reset by watchdog
2072 * @watchdog_code: code to be logged when resetting
2073 * @data_size: size of buffer in bytes
2074 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002075 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002076 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002077 *
Mario Six8fac2912018-07-10 08:40:17 +02002078 * See the Unified Extensible Firmware Interface (UEFI) specification for
2079 * details.
2080 *
2081 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002082 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002083static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2084 uint64_t watchdog_code,
2085 unsigned long data_size,
2086 uint16_t *watchdog_data)
2087{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002088 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002089 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002090 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002091}
2092
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002093/**
Mario Six8fac2912018-07-10 08:40:17 +02002094 * efi_close_protocol() - close a protocol
2095 * @handle: handle on which the protocol shall be closed
2096 * @protocol: GUID of the protocol to close
2097 * @agent_handle: handle of the driver
2098 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002099 *
2100 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002101 *
Mario Six8fac2912018-07-10 08:40:17 +02002102 * See the Unified Extensible Firmware Interface (UEFI) specification for
2103 * details.
2104 *
2105 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002106 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002107static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002108 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002109 efi_handle_t agent_handle,
2110 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002111{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002112 struct efi_handler *handler;
2113 struct efi_open_protocol_info_item *item;
2114 struct efi_open_protocol_info_item *pos;
2115 efi_status_t r;
2116
Rob Clark238f88c2017-09-13 18:05:41 -04002117 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002118 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002119
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002120 if (!efi_search_obj(agent_handle) ||
2121 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002122 r = EFI_INVALID_PARAMETER;
2123 goto out;
2124 }
2125 r = efi_search_protocol(handle, protocol, &handler);
2126 if (r != EFI_SUCCESS)
2127 goto out;
2128
2129 r = EFI_NOT_FOUND;
2130 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2131 if (item->info.agent_handle == agent_handle &&
2132 item->info.controller_handle == controller_handle) {
2133 efi_delete_open_info(item);
2134 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002135 }
2136 }
2137out:
2138 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002139}
2140
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002141/**
Mario Six8fac2912018-07-10 08:40:17 +02002142 * efi_open_protocol_information() - provide information about then open status
2143 * of a protocol on a handle
2144 * @handle: handle for which the information shall be retrieved
2145 * @protocol: GUID of the protocol
2146 * @entry_buffer: buffer to receive the open protocol information
2147 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002148 *
2149 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002150 *
2151 * See the Unified Extensible Firmware Interface (UEFI) specification for
2152 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002153 *
Mario Six8fac2912018-07-10 08:40:17 +02002154 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002155 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002156static efi_status_t EFIAPI efi_open_protocol_information(
2157 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002158 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002159 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002160{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002161 unsigned long buffer_size;
2162 unsigned long count;
2163 struct efi_handler *handler;
2164 struct efi_open_protocol_info_item *item;
2165 efi_status_t r;
2166
Rob Clark238f88c2017-09-13 18:05:41 -04002167 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002168 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002169
2170 /* Check parameters */
2171 if (!entry_buffer) {
2172 r = EFI_INVALID_PARAMETER;
2173 goto out;
2174 }
2175 r = efi_search_protocol(handle, protocol, &handler);
2176 if (r != EFI_SUCCESS)
2177 goto out;
2178
2179 /* Count entries */
2180 count = 0;
2181 list_for_each_entry(item, &handler->open_infos, link) {
2182 if (item->info.open_count)
2183 ++count;
2184 }
2185 *entry_count = count;
2186 *entry_buffer = NULL;
2187 if (!count) {
2188 r = EFI_SUCCESS;
2189 goto out;
2190 }
2191
2192 /* Copy entries */
2193 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002194 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002195 (void **)entry_buffer);
2196 if (r != EFI_SUCCESS)
2197 goto out;
2198 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2199 if (item->info.open_count)
2200 (*entry_buffer)[--count] = item->info;
2201 }
2202out:
2203 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002204}
2205
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002206/**
Mario Six8fac2912018-07-10 08:40:17 +02002207 * efi_protocols_per_handle() - get protocols installed on a handle
2208 * @handle: handle for which the information is retrieved
2209 * @protocol_buffer: buffer with protocol GUIDs
2210 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002211 *
2212 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002213 *
Mario Six8fac2912018-07-10 08:40:17 +02002214 * See the Unified Extensible Firmware Interface (UEFI) specification for
2215 * details.
2216 *
2217 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002218 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002219static efi_status_t EFIAPI efi_protocols_per_handle(
2220 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002221 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002222{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002223 unsigned long buffer_size;
2224 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002225 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002226 efi_status_t r;
2227
Alexander Grafc15d9212016-03-04 01:09:59 +01002228 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2229 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002230
2231 if (!handle || !protocol_buffer || !protocol_buffer_count)
2232 return EFI_EXIT(EFI_INVALID_PARAMETER);
2233
2234 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002235 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002236
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002237 efiobj = efi_search_obj(handle);
2238 if (!efiobj)
2239 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002240
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002241 /* Count protocols */
2242 list_for_each(protocol_handle, &efiobj->protocols) {
2243 ++*protocol_buffer_count;
2244 }
2245
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002246 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002247 if (*protocol_buffer_count) {
2248 size_t j = 0;
2249
2250 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002251 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002252 (void **)protocol_buffer);
2253 if (r != EFI_SUCCESS)
2254 return EFI_EXIT(r);
2255 list_for_each(protocol_handle, &efiobj->protocols) {
2256 struct efi_handler *protocol;
2257
2258 protocol = list_entry(protocol_handle,
2259 struct efi_handler, link);
2260 (*protocol_buffer)[j] = (void *)protocol->guid;
2261 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002262 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002263 }
2264
2265 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002266}
2267
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002268/**
Mario Six8fac2912018-07-10 08:40:17 +02002269 * efi_locate_handle_buffer() - locate handles implementing a protocol
2270 * @search_type: selection criterion
2271 * @protocol: GUID of the protocol
2272 * @search_key: registration key
2273 * @no_handles: number of returned handles
2274 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002275 *
2276 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002277 *
Mario Six8fac2912018-07-10 08:40:17 +02002278 * See the Unified Extensible Firmware Interface (UEFI) specification for
2279 * details.
2280 *
2281 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002282 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002283static efi_status_t EFIAPI efi_locate_handle_buffer(
2284 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002285 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002286 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002287{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002288 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002289 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002290
Rob Clark238f88c2017-09-13 18:05:41 -04002291 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002292 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002293
2294 if (!no_handles || !buffer) {
2295 r = EFI_INVALID_PARAMETER;
2296 goto out;
2297 }
2298 *no_handles = 0;
2299 *buffer = NULL;
2300 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2301 *buffer);
2302 if (r != EFI_BUFFER_TOO_SMALL)
2303 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002304 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002305 (void **)buffer);
2306 if (r != EFI_SUCCESS)
2307 goto out;
2308 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2309 *buffer);
2310 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002311 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002312out:
2313 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002314}
2315
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002316/**
Mario Six8fac2912018-07-10 08:40:17 +02002317 * efi_locate_protocol() - find an interface implementing a protocol
2318 * @protocol: GUID of the protocol
2319 * @registration: registration key passed to the notification function
2320 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002321 *
2322 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002323 *
2324 * See the Unified Extensible Firmware Interface (UEFI) specification for
2325 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002326 *
Mario Six8fac2912018-07-10 08:40:17 +02002327 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002328 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002329static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002330 void *registration,
2331 void **protocol_interface)
2332{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002333 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002334 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002335 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002336
Rob Clark238f88c2017-09-13 18:05:41 -04002337 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002338
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002339 /*
2340 * The UEFI spec explicitly requires a protocol even if a registration
2341 * key is provided. This differs from the logic in LocateHandle().
2342 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002343 if (!protocol || !protocol_interface)
2344 return EFI_EXIT(EFI_INVALID_PARAMETER);
2345
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002346 if (registration) {
2347 struct efi_register_notify_event *event;
2348 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002349
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002350 event = efi_check_register_notify_event(registration);
2351 if (!event)
2352 return EFI_EXIT(EFI_INVALID_PARAMETER);
2353 /*
2354 * The UEFI spec requires to return EFI_NOT_FOUND if no
2355 * protocol instance matches protocol and registration.
2356 * So let's do the same for a mismatch between protocol and
2357 * registration.
2358 */
2359 if (guidcmp(&event->protocol, protocol))
2360 goto not_found;
2361 if (list_empty(&event->handles))
2362 goto not_found;
2363 handle = list_first_entry(&event->handles,
2364 struct efi_protocol_notification,
2365 link);
2366 efiobj = handle->handle;
2367 list_del(&handle->link);
2368 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002369 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002370 if (ret == EFI_SUCCESS)
2371 goto found;
2372 } else {
2373 list_for_each_entry(efiobj, &efi_obj_list, link) {
2374 ret = efi_search_protocol(efiobj, protocol, &handler);
2375 if (ret == EFI_SUCCESS)
2376 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002377 }
2378 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002379not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002380 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002381 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002382found:
2383 *protocol_interface = handler->protocol_interface;
2384 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002385}
2386
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002387/**
Mario Six8fac2912018-07-10 08:40:17 +02002388 * efi_locate_device_path() - Get the device path and handle of an device
2389 * implementing a protocol
2390 * @protocol: GUID of the protocol
2391 * @device_path: device path
2392 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002393 *
2394 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002395 *
Mario Six8fac2912018-07-10 08:40:17 +02002396 * See the Unified Extensible Firmware Interface (UEFI) specification for
2397 * details.
2398 *
2399 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002400 */
2401static efi_status_t EFIAPI efi_locate_device_path(
2402 const efi_guid_t *protocol,
2403 struct efi_device_path **device_path,
2404 efi_handle_t *device)
2405{
2406 struct efi_device_path *dp;
2407 size_t i;
2408 struct efi_handler *handler;
2409 efi_handle_t *handles;
2410 size_t len, len_dp;
2411 size_t len_best = 0;
2412 efi_uintn_t no_handles;
2413 u8 *remainder;
2414 efi_status_t ret;
2415
2416 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2417
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002418 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002419 ret = EFI_INVALID_PARAMETER;
2420 goto out;
2421 }
2422
2423 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002424 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002425
2426 /* Get all handles implementing the protocol */
2427 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2428 &no_handles, &handles));
2429 if (ret != EFI_SUCCESS)
2430 goto out;
2431
2432 for (i = 0; i < no_handles; ++i) {
2433 /* Find the device path protocol */
2434 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2435 &handler);
2436 if (ret != EFI_SUCCESS)
2437 continue;
2438 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002439 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002440 /*
2441 * This handle can only be a better fit
2442 * if its device path length is longer than the best fit and
2443 * if its device path length is shorter of equal the searched
2444 * device path.
2445 */
2446 if (len_dp <= len_best || len_dp > len)
2447 continue;
2448 /* Check if dp is a subpath of device_path */
2449 if (memcmp(*device_path, dp, len_dp))
2450 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002451 if (!device) {
2452 ret = EFI_INVALID_PARAMETER;
2453 goto out;
2454 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002455 *device = handles[i];
2456 len_best = len_dp;
2457 }
2458 if (len_best) {
2459 remainder = (u8 *)*device_path + len_best;
2460 *device_path = (struct efi_device_path *)remainder;
2461 ret = EFI_SUCCESS;
2462 } else {
2463 ret = EFI_NOT_FOUND;
2464 }
2465out:
2466 return EFI_EXIT(ret);
2467}
2468
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002469/**
Mario Six8fac2912018-07-10 08:40:17 +02002470 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2471 * interfaces
2472 * @handle: handle on which the protocol interfaces shall be installed
2473 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2474 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002475 *
2476 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002477 *
2478 * See the Unified Extensible Firmware Interface (UEFI) specification for
2479 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002480 *
Mario Six8fac2912018-07-10 08:40:17 +02002481 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002482 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002483efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002484 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002485{
2486 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002487
Alexander Graf404a7c82018-06-18 17:23:05 +02002488 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002489 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002490 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002491 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002492 efi_status_t r = EFI_SUCCESS;
2493 int i = 0;
2494
2495 if (!handle)
2496 return EFI_EXIT(EFI_INVALID_PARAMETER);
2497
Alexander Graf404a7c82018-06-18 17:23:05 +02002498 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002499 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002500 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002501 if (!protocol)
2502 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002503 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002504 /* Check that a device path has not been installed before */
2505 if (!guidcmp(protocol, &efi_guid_device_path)) {
2506 struct efi_device_path *dp = protocol_interface;
2507
2508 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2509 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002510 if (r == EFI_SUCCESS &&
2511 dp->type == DEVICE_PATH_TYPE_END) {
2512 EFI_PRINT("Path %pD already installed\n",
2513 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002514 r = EFI_ALREADY_STARTED;
2515 break;
2516 }
2517 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002518 r = EFI_CALL(efi_install_protocol_interface(
2519 handle, protocol,
2520 EFI_NATIVE_INTERFACE,
2521 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002522 if (r != EFI_SUCCESS)
2523 break;
2524 i++;
2525 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002526 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002527 if (r == EFI_SUCCESS)
2528 return EFI_EXIT(r);
2529
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002530 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002531 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002532 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002533 protocol = efi_va_arg(argptr, efi_guid_t*);
2534 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002535 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002536 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002537 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002538 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002539
2540 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002541}
2542
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002543/**
Mario Six8fac2912018-07-10 08:40:17 +02002544 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2545 * interfaces
2546 * @handle: handle from which the protocol interfaces shall be removed
2547 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2548 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002549 *
2550 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002551 *
Mario Six8fac2912018-07-10 08:40:17 +02002552 * See the Unified Extensible Firmware Interface (UEFI) specification for
2553 * details.
2554 *
2555 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002556 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002557static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002558 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002559{
2560 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002561
Alexander Graf404a7c82018-06-18 17:23:05 +02002562 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002563 const efi_guid_t *protocol;
2564 void *protocol_interface;
2565 efi_status_t r = EFI_SUCCESS;
2566 size_t i = 0;
2567
2568 if (!handle)
2569 return EFI_EXIT(EFI_INVALID_PARAMETER);
2570
Alexander Graf404a7c82018-06-18 17:23:05 +02002571 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002572 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002573 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002574 if (!protocol)
2575 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002576 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002577 r = efi_uninstall_protocol(handle, protocol,
2578 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002579 if (r != EFI_SUCCESS)
2580 break;
2581 i++;
2582 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002583 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002584 if (r == EFI_SUCCESS) {
2585 /* If the last protocol has been removed, delete the handle. */
2586 if (list_empty(&handle->protocols)) {
2587 list_del(&handle->link);
2588 free(handle);
2589 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002590 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002591 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002592
2593 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002594 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002595 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002596 protocol = efi_va_arg(argptr, efi_guid_t*);
2597 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002598 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2599 EFI_NATIVE_INTERFACE,
2600 protocol_interface));
2601 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002602 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002603
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002604 /* In case of an error always return EFI_INVALID_PARAMETER */
2605 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002606}
2607
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002608/**
Mario Six8fac2912018-07-10 08:40:17 +02002609 * efi_calculate_crc32() - calculate cyclic redundancy code
2610 * @data: buffer with data
2611 * @data_size: size of buffer in bytes
2612 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002613 *
2614 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002615 *
2616 * See the Unified Extensible Firmware Interface (UEFI) specification for
2617 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002618 *
Mario Six8fac2912018-07-10 08:40:17 +02002619 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002620 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002621static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2622 efi_uintn_t data_size,
2623 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002624{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002625 efi_status_t ret = EFI_SUCCESS;
2626
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002627 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002628 if (!data || !data_size || !crc32_p) {
2629 ret = EFI_INVALID_PARAMETER;
2630 goto out;
2631 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002632 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002633out:
2634 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002635}
2636
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002637/**
Mario Six8fac2912018-07-10 08:40:17 +02002638 * efi_copy_mem() - copy memory
2639 * @destination: destination of the copy operation
2640 * @source: source of the copy operation
2641 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002642 *
2643 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002644 *
Mario Six8fac2912018-07-10 08:40:17 +02002645 * See the Unified Extensible Firmware Interface (UEFI) specification for
2646 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002647 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002648static void EFIAPI efi_copy_mem(void *destination, const void *source,
2649 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002650{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002651 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002652 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002653 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002654}
2655
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002656/**
Mario Six8fac2912018-07-10 08:40:17 +02002657 * efi_set_mem() - Fill memory with a byte value.
2658 * @buffer: buffer to fill
2659 * @size: size of buffer in bytes
2660 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002661 *
2662 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002663 *
Mario Six8fac2912018-07-10 08:40:17 +02002664 * See the Unified Extensible Firmware Interface (UEFI) specification for
2665 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002666 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002667static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002668{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002669 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002670 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002671 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002672}
2673
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002674/**
Mario Six8fac2912018-07-10 08:40:17 +02002675 * efi_protocol_open() - open protocol interface on a handle
2676 * @handler: handler of a protocol
2677 * @protocol_interface: interface implementing the protocol
2678 * @agent_handle: handle of the driver
2679 * @controller_handle: handle of the controller
2680 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002681 *
Mario Six8fac2912018-07-10 08:40:17 +02002682 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002683 */
2684static efi_status_t efi_protocol_open(
2685 struct efi_handler *handler,
2686 void **protocol_interface, void *agent_handle,
2687 void *controller_handle, uint32_t attributes)
2688{
2689 struct efi_open_protocol_info_item *item;
2690 struct efi_open_protocol_info_entry *match = NULL;
2691 bool opened_by_driver = false;
2692 bool opened_exclusive = false;
2693
2694 /* If there is no agent, only return the interface */
2695 if (!agent_handle)
2696 goto out;
2697
2698 /* For TEST_PROTOCOL ignore interface attribute */
2699 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2700 *protocol_interface = NULL;
2701
2702 /*
2703 * Check if the protocol is already opened by a driver with the same
2704 * attributes or opened exclusively
2705 */
2706 list_for_each_entry(item, &handler->open_infos, link) {
2707 if (item->info.agent_handle == agent_handle) {
2708 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2709 (item->info.attributes == attributes))
2710 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002711 } else {
2712 if (item->info.attributes &
2713 EFI_OPEN_PROTOCOL_BY_DRIVER)
2714 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002715 }
2716 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2717 opened_exclusive = true;
2718 }
2719
2720 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002721 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2722 if (opened_exclusive)
2723 return EFI_ACCESS_DENIED;
2724 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2725 if (opened_exclusive || opened_by_driver)
2726 return EFI_ACCESS_DENIED;
2727 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002728
2729 /* Prepare exclusive opening */
2730 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2731 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002732disconnect_next:
2733 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002734 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002735 efi_status_t ret;
2736
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002737 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002738 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2739 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002740 item->info.controller_handle,
2741 item->info.agent_handle,
2742 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002743 if (ret == EFI_SUCCESS)
2744 /*
2745 * Child controllers may have been
2746 * removed from the open_infos list. So
2747 * let's restart the loop.
2748 */
2749 goto disconnect_next;
2750 else
2751 opened_by_driver = true;
2752 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002753 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002754 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002755 if (opened_by_driver)
2756 return EFI_ACCESS_DENIED;
2757 }
2758
2759 /* Find existing entry */
2760 list_for_each_entry(item, &handler->open_infos, link) {
2761 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002762 item->info.controller_handle == controller_handle &&
2763 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002764 match = &item->info;
2765 }
2766 /* None found, create one */
2767 if (!match) {
2768 match = efi_create_open_info(handler);
2769 if (!match)
2770 return EFI_OUT_OF_RESOURCES;
2771 }
2772
2773 match->agent_handle = agent_handle;
2774 match->controller_handle = controller_handle;
2775 match->attributes = attributes;
2776 match->open_count++;
2777
2778out:
2779 /* For TEST_PROTOCOL ignore interface attribute. */
2780 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2781 *protocol_interface = handler->protocol_interface;
2782
2783 return EFI_SUCCESS;
2784}
2785
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002786/**
Mario Six8fac2912018-07-10 08:40:17 +02002787 * efi_open_protocol() - open protocol interface on a handle
2788 * @handle: handle on which the protocol shall be opened
2789 * @protocol: GUID of the protocol
2790 * @protocol_interface: interface implementing the protocol
2791 * @agent_handle: handle of the driver
2792 * @controller_handle: handle of the controller
2793 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002794 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002795 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002796 *
Mario Six8fac2912018-07-10 08:40:17 +02002797 * See the Unified Extensible Firmware Interface (UEFI) specification for
2798 * details.
2799 *
2800 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002801 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002802static efi_status_t EFIAPI efi_open_protocol
2803 (efi_handle_t handle, const efi_guid_t *protocol,
2804 void **protocol_interface, efi_handle_t agent_handle,
2805 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002806{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002807 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002808 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002809
Rob Clark238f88c2017-09-13 18:05:41 -04002810 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002811 protocol_interface, agent_handle, controller_handle,
2812 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002813
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002814 if (!handle || !protocol ||
2815 (!protocol_interface && attributes !=
2816 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002817 goto out;
2818 }
2819
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002820 switch (attributes) {
2821 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2822 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2823 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2824 break;
2825 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2826 if (controller_handle == handle)
2827 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002828 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002829 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2830 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002831 /* Check that the controller handle is valid */
2832 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002833 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002834 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002835 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002836 /* Check that the agent handle is valid */
2837 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002838 goto out;
2839 break;
2840 default:
2841 goto out;
2842 }
2843
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002844 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002845 switch (r) {
2846 case EFI_SUCCESS:
2847 break;
2848 case EFI_NOT_FOUND:
2849 r = EFI_UNSUPPORTED;
2850 goto out;
2851 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002852 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002853 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002854
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002855 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2856 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002857out:
2858 return EFI_EXIT(r);
2859}
2860
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002861/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002862 * efi_start_image() - call the entry point of an image
2863 * @image_handle: handle of the image
2864 * @exit_data_size: size of the buffer
2865 * @exit_data: buffer to receive the exit data of the called image
2866 *
2867 * This function implements the StartImage service.
2868 *
2869 * See the Unified Extensible Firmware Interface (UEFI) specification for
2870 * details.
2871 *
2872 * Return: status code
2873 */
2874efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2875 efi_uintn_t *exit_data_size,
2876 u16 **exit_data)
2877{
2878 struct efi_loaded_image_obj *image_obj =
2879 (struct efi_loaded_image_obj *)image_handle;
2880 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002881 void *info;
2882 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002883
2884 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2885
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002886 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002887 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2888 return EFI_EXIT(EFI_INVALID_PARAMETER);
2889
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002890 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2891 &info, NULL, NULL,
2892 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2893 if (ret != EFI_SUCCESS)
2894 return EFI_EXIT(EFI_INVALID_PARAMETER);
2895
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002896 efi_is_direct_boot = false;
2897
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002898 image_obj->exit_data_size = exit_data_size;
2899 image_obj->exit_data = exit_data;
2900
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002901 /* call the image! */
2902 if (setjmp(&image_obj->exit_jmp)) {
2903 /*
2904 * We called the entry point of the child image with EFI_CALL
2905 * in the lines below. The child image called the Exit() boot
2906 * service efi_exit() which executed the long jump that brought
2907 * us to the current line. This implies that the second half
2908 * of the EFI_CALL macro has not been executed.
2909 */
2910#ifdef CONFIG_ARM
2911 /*
2912 * efi_exit() called efi_restore_gd(). We have to undo this
2913 * otherwise __efi_entry_check() will put the wrong value into
2914 * app_gd.
2915 */
2916 gd = app_gd;
2917#endif
2918 /*
2919 * To get ready to call EFI_EXIT below we have to execute the
2920 * missed out steps of EFI_CALL.
2921 */
2922 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002923 EFI_PRINT("%lu returned by started image\n",
2924 (unsigned long)((uintptr_t)image_obj->exit_status &
2925 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002926 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002927 return EFI_EXIT(image_obj->exit_status);
2928 }
2929
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002930 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002931 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002932 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002933 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2934
2935 /*
2936 * Usually UEFI applications call Exit() instead of returning.
2937 * But because the world doesn't consist of ponies and unicorns,
2938 * we're happy to emulate that behavior on behalf of a payload
2939 * that forgot.
2940 */
2941 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2942}
2943
2944/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002945 * efi_delete_image() - delete loaded image from memory)
2946 *
2947 * @image_obj: handle of the loaded image
2948 * @loaded_image_protocol: loaded image protocol
2949 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002950static efi_status_t efi_delete_image
2951 (struct efi_loaded_image_obj *image_obj,
2952 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002953{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002954 struct efi_object *efiobj;
2955 efi_status_t r, ret = EFI_SUCCESS;
2956
2957close_next:
2958 list_for_each_entry(efiobj, &efi_obj_list, link) {
2959 struct efi_handler *protocol;
2960
2961 list_for_each_entry(protocol, &efiobj->protocols, link) {
2962 struct efi_open_protocol_info_item *info;
2963
2964 list_for_each_entry(info, &protocol->open_infos, link) {
2965 if (info->info.agent_handle !=
2966 (efi_handle_t)image_obj)
2967 continue;
2968 r = EFI_CALL(efi_close_protocol
2969 (efiobj, protocol->guid,
2970 info->info.agent_handle,
2971 info->info.controller_handle
2972 ));
2973 if (r != EFI_SUCCESS)
2974 ret = r;
2975 /*
2976 * Closing protocols may results in further
2977 * items being deleted. To play it safe loop
2978 * over all elements again.
2979 */
2980 goto close_next;
2981 }
2982 }
2983 }
2984
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002985 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2986 efi_size_in_pages(loaded_image_protocol->image_size));
2987 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002988
2989 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002990}
2991
2992/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002993 * efi_unload_image() - unload an EFI image
2994 * @image_handle: handle of the image to be unloaded
2995 *
2996 * This function implements the UnloadImage service.
2997 *
2998 * See the Unified Extensible Firmware Interface (UEFI) specification for
2999 * details.
3000 *
3001 * Return: status code
3002 */
3003efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3004{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003005 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003006 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003007 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003008
3009 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003010
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003011 efiobj = efi_search_obj(image_handle);
3012 if (!efiobj) {
3013 ret = EFI_INVALID_PARAMETER;
3014 goto out;
3015 }
3016 /* Find the loaded image protocol */
3017 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3018 (void **)&loaded_image_protocol,
3019 NULL, NULL,
3020 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3021 if (ret != EFI_SUCCESS) {
3022 ret = EFI_INVALID_PARAMETER;
3023 goto out;
3024 }
3025 switch (efiobj->type) {
3026 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3027 /* Call the unload function */
3028 if (!loaded_image_protocol->unload) {
3029 ret = EFI_UNSUPPORTED;
3030 goto out;
3031 }
3032 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3033 if (ret != EFI_SUCCESS)
3034 goto out;
3035 break;
3036 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3037 break;
3038 default:
3039 ret = EFI_INVALID_PARAMETER;
3040 goto out;
3041 }
3042 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3043 loaded_image_protocol);
3044out:
3045 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003046}
3047
3048/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003049 * efi_update_exit_data() - fill exit data parameters of StartImage()
3050 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003051 * @image_obj: image handle
3052 * @exit_data_size: size of the exit data buffer
3053 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003054 * Return: status code
3055 */
3056static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3057 efi_uintn_t exit_data_size,
3058 u16 *exit_data)
3059{
3060 efi_status_t ret;
3061
3062 /*
3063 * If exit_data is not provided to StartImage(), exit_data_size must be
3064 * ignored.
3065 */
3066 if (!image_obj->exit_data)
3067 return EFI_SUCCESS;
3068 if (image_obj->exit_data_size)
3069 *image_obj->exit_data_size = exit_data_size;
3070 if (exit_data_size && exit_data) {
3071 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3072 exit_data_size,
3073 (void **)image_obj->exit_data);
3074 if (ret != EFI_SUCCESS)
3075 return ret;
3076 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3077 } else {
3078 image_obj->exit_data = NULL;
3079 }
3080 return EFI_SUCCESS;
3081}
3082
3083/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003084 * efi_exit() - leave an EFI application or driver
3085 * @image_handle: handle of the application or driver that is exiting
3086 * @exit_status: status code
3087 * @exit_data_size: size of the buffer in bytes
3088 * @exit_data: buffer with data describing an error
3089 *
3090 * This function implements the Exit service.
3091 *
3092 * See the Unified Extensible Firmware Interface (UEFI) specification for
3093 * details.
3094 *
3095 * Return: status code
3096 */
3097static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3098 efi_status_t exit_status,
3099 efi_uintn_t exit_data_size,
3100 u16 *exit_data)
3101{
3102 /*
3103 * TODO: We should call the unload procedure of the loaded
3104 * image protocol.
3105 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003106 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003107 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003108 struct efi_loaded_image_obj *image_obj =
3109 (struct efi_loaded_image_obj *)image_handle;
3110
3111 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3112 exit_data_size, exit_data);
3113
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003114 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003115 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003116 (void **)&loaded_image_protocol,
3117 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003118 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003119 if (ret != EFI_SUCCESS) {
3120 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003121 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003122 }
3123
3124 /* Unloading of unstarted images */
3125 switch (image_obj->header.type) {
3126 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3127 break;
3128 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3129 efi_delete_image(image_obj, loaded_image_protocol);
3130 ret = EFI_SUCCESS;
3131 goto out;
3132 default:
3133 /* Handle does not refer to loaded image */
3134 ret = EFI_INVALID_PARAMETER;
3135 goto out;
3136 }
3137 /* A started image can only be unloaded it is the last one started. */
3138 if (image_handle != current_image) {
3139 ret = EFI_INVALID_PARAMETER;
3140 goto out;
3141 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003142
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003143 /* Exit data is only foreseen in case of failure. */
3144 if (exit_status != EFI_SUCCESS) {
3145 ret = efi_update_exit_data(image_obj, exit_data_size,
3146 exit_data);
3147 /* Exiting has priority. Don't return error to caller. */
3148 if (ret != EFI_SUCCESS)
3149 EFI_PRINT("%s: out of memory\n", __func__);
3150 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003151 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3152 exit_status != EFI_SUCCESS)
3153 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003154
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003155 /* Make sure entry/exit counts for EFI world cross-overs match */
3156 EFI_EXIT(exit_status);
3157
3158 /*
3159 * But longjmp out with the U-Boot gd, not the application's, as
3160 * the other end is a setjmp call inside EFI context.
3161 */
3162 efi_restore_gd();
3163
3164 image_obj->exit_status = exit_status;
3165 longjmp(&image_obj->exit_jmp, 1);
3166
3167 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003168out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003169 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003170}
3171
3172/**
Mario Six8fac2912018-07-10 08:40:17 +02003173 * efi_handle_protocol() - get interface of a protocol on a handle
3174 * @handle: handle on which the protocol shall be opened
3175 * @protocol: GUID of the protocol
3176 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003177 *
3178 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003179 *
3180 * See the Unified Extensible Firmware Interface (UEFI) specification for
3181 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003182 *
Mario Six8fac2912018-07-10 08:40:17 +02003183 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003184 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01003185static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02003186 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003187 void **protocol_interface)
3188{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003189 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003190 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003191}
3192
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003193/**
Mario Six8fac2912018-07-10 08:40:17 +02003194 * efi_bind_controller() - bind a single driver to a controller
3195 * @controller_handle: controller handle
3196 * @driver_image_handle: driver handle
3197 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003198 *
Mario Six8fac2912018-07-10 08:40:17 +02003199 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003200 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003201static efi_status_t efi_bind_controller(
3202 efi_handle_t controller_handle,
3203 efi_handle_t driver_image_handle,
3204 struct efi_device_path *remain_device_path)
3205{
3206 struct efi_driver_binding_protocol *binding_protocol;
3207 efi_status_t r;
3208
3209 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3210 &efi_guid_driver_binding_protocol,
3211 (void **)&binding_protocol,
3212 driver_image_handle, NULL,
3213 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3214 if (r != EFI_SUCCESS)
3215 return r;
3216 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3217 controller_handle,
3218 remain_device_path));
3219 if (r == EFI_SUCCESS)
3220 r = EFI_CALL(binding_protocol->start(binding_protocol,
3221 controller_handle,
3222 remain_device_path));
3223 EFI_CALL(efi_close_protocol(driver_image_handle,
3224 &efi_guid_driver_binding_protocol,
3225 driver_image_handle, NULL));
3226 return r;
3227}
3228
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003229/**
Mario Six8fac2912018-07-10 08:40:17 +02003230 * efi_connect_single_controller() - connect a single driver to a controller
3231 * @controller_handle: controller
3232 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003233 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003234 *
Mario Six8fac2912018-07-10 08:40:17 +02003235 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003236 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003237static efi_status_t efi_connect_single_controller(
3238 efi_handle_t controller_handle,
3239 efi_handle_t *driver_image_handle,
3240 struct efi_device_path *remain_device_path)
3241{
3242 efi_handle_t *buffer;
3243 size_t count;
3244 size_t i;
3245 efi_status_t r;
3246 size_t connected = 0;
3247
3248 /* Get buffer with all handles with driver binding protocol */
3249 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3250 &efi_guid_driver_binding_protocol,
3251 NULL, &count, &buffer));
3252 if (r != EFI_SUCCESS)
3253 return r;
3254
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003255 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003256 if (driver_image_handle) {
3257 for (; *driver_image_handle; ++driver_image_handle) {
3258 for (i = 0; i < count; ++i) {
3259 if (buffer[i] == *driver_image_handle) {
3260 buffer[i] = NULL;
3261 r = efi_bind_controller(
3262 controller_handle,
3263 *driver_image_handle,
3264 remain_device_path);
3265 /*
3266 * For drivers that do not support the
3267 * controller or are already connected
3268 * we receive an error code here.
3269 */
3270 if (r == EFI_SUCCESS)
3271 ++connected;
3272 }
3273 }
3274 }
3275 }
3276
3277 /*
3278 * TODO: Some overrides are not yet implemented:
3279 * - Platform Driver Override
3280 * - Driver Family Override Search
3281 * - Bus Specific Driver Override
3282 */
3283
3284 /* Driver Binding Search */
3285 for (i = 0; i < count; ++i) {
3286 if (buffer[i]) {
3287 r = efi_bind_controller(controller_handle,
3288 buffer[i],
3289 remain_device_path);
3290 if (r == EFI_SUCCESS)
3291 ++connected;
3292 }
3293 }
3294
3295 efi_free_pool(buffer);
3296 if (!connected)
3297 return EFI_NOT_FOUND;
3298 return EFI_SUCCESS;
3299}
3300
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003301/**
Mario Six8fac2912018-07-10 08:40:17 +02003302 * efi_connect_controller() - connect a controller to a driver
3303 * @controller_handle: handle of the controller
3304 * @driver_image_handle: handle of the driver
3305 * @remain_device_path: device path of a child controller
3306 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003307 *
3308 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003309 *
3310 * See the Unified Extensible Firmware Interface (UEFI) specification for
3311 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003312 *
3313 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003314 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003315 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3316 *
Mario Six8fac2912018-07-10 08:40:17 +02003317 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003318 */
3319static efi_status_t EFIAPI efi_connect_controller(
3320 efi_handle_t controller_handle,
3321 efi_handle_t *driver_image_handle,
3322 struct efi_device_path *remain_device_path,
3323 bool recursive)
3324{
3325 efi_status_t r;
3326 efi_status_t ret = EFI_NOT_FOUND;
3327 struct efi_object *efiobj;
3328
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003329 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003330 remain_device_path, recursive);
3331
3332 efiobj = efi_search_obj(controller_handle);
3333 if (!efiobj) {
3334 ret = EFI_INVALID_PARAMETER;
3335 goto out;
3336 }
3337
3338 r = efi_connect_single_controller(controller_handle,
3339 driver_image_handle,
3340 remain_device_path);
3341 if (r == EFI_SUCCESS)
3342 ret = EFI_SUCCESS;
3343 if (recursive) {
3344 struct efi_handler *handler;
3345 struct efi_open_protocol_info_item *item;
3346
3347 list_for_each_entry(handler, &efiobj->protocols, link) {
3348 list_for_each_entry(item, &handler->open_infos, link) {
3349 if (item->info.attributes &
3350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3351 r = EFI_CALL(efi_connect_controller(
3352 item->info.controller_handle,
3353 driver_image_handle,
3354 remain_device_path,
3355 recursive));
3356 if (r == EFI_SUCCESS)
3357 ret = EFI_SUCCESS;
3358 }
3359 }
3360 }
3361 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003362 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003363 if (ret != EFI_SUCCESS && remain_device_path &&
3364 remain_device_path->type == DEVICE_PATH_TYPE_END)
3365 ret = EFI_SUCCESS;
3366out:
3367 return EFI_EXIT(ret);
3368}
3369
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003370/**
Mario Six8fac2912018-07-10 08:40:17 +02003371 * efi_reinstall_protocol_interface() - reinstall protocol interface
3372 * @handle: handle on which the protocol shall be reinstalled
3373 * @protocol: GUID of the protocol to be installed
3374 * @old_interface: interface to be removed
3375 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003376 *
3377 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003378 *
3379 * See the Unified Extensible Firmware Interface (UEFI) specification for
3380 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003381 *
3382 * The old interface is uninstalled. The new interface is installed.
3383 * Drivers are connected.
3384 *
Mario Six8fac2912018-07-10 08:40:17 +02003385 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003386 */
3387static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3388 efi_handle_t handle, const efi_guid_t *protocol,
3389 void *old_interface, void *new_interface)
3390{
3391 efi_status_t ret;
3392
3393 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3394 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003395
3396 /* Uninstall protocol but do not delete handle */
3397 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003398 if (ret != EFI_SUCCESS)
3399 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003400
3401 /* Install the new protocol */
3402 ret = efi_add_protocol(handle, protocol, new_interface);
3403 /*
3404 * The UEFI spec does not specify what should happen to the handle
3405 * if in case of an error no protocol interface remains on the handle.
3406 * So let's do nothing here.
3407 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003408 if (ret != EFI_SUCCESS)
3409 goto out;
3410 /*
3411 * The returned status code has to be ignored.
3412 * Do not create an error if no suitable driver for the handle exists.
3413 */
3414 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3415out:
3416 return EFI_EXIT(ret);
3417}
3418
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003419/**
Mario Six8fac2912018-07-10 08:40:17 +02003420 * efi_get_child_controllers() - get all child controllers associated to a driver
3421 * @efiobj: handle of the controller
3422 * @driver_handle: handle of the driver
3423 * @number_of_children: number of child controllers
3424 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003425 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003426 * The allocated buffer has to be freed with free().
3427 *
Mario Six8fac2912018-07-10 08:40:17 +02003428 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003429 */
3430static efi_status_t efi_get_child_controllers(
3431 struct efi_object *efiobj,
3432 efi_handle_t driver_handle,
3433 efi_uintn_t *number_of_children,
3434 efi_handle_t **child_handle_buffer)
3435{
3436 struct efi_handler *handler;
3437 struct efi_open_protocol_info_item *item;
3438 efi_uintn_t count = 0, i;
3439 bool duplicate;
3440
3441 /* Count all child controller associations */
3442 list_for_each_entry(handler, &efiobj->protocols, link) {
3443 list_for_each_entry(item, &handler->open_infos, link) {
3444 if (item->info.agent_handle == driver_handle &&
3445 item->info.attributes &
3446 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3447 ++count;
3448 }
3449 }
3450 /*
3451 * Create buffer. In case of duplicate child controller assignments
3452 * the buffer will be too large. But that does not harm.
3453 */
3454 *number_of_children = 0;
3455 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3456 if (!*child_handle_buffer)
3457 return EFI_OUT_OF_RESOURCES;
3458 /* Copy unique child handles */
3459 list_for_each_entry(handler, &efiobj->protocols, link) {
3460 list_for_each_entry(item, &handler->open_infos, link) {
3461 if (item->info.agent_handle == driver_handle &&
3462 item->info.attributes &
3463 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3464 /* Check this is a new child controller */
3465 duplicate = false;
3466 for (i = 0; i < *number_of_children; ++i) {
3467 if ((*child_handle_buffer)[i] ==
3468 item->info.controller_handle)
3469 duplicate = true;
3470 }
3471 /* Copy handle to buffer */
3472 if (!duplicate) {
3473 i = (*number_of_children)++;
3474 (*child_handle_buffer)[i] =
3475 item->info.controller_handle;
3476 }
3477 }
3478 }
3479 }
3480 return EFI_SUCCESS;
3481}
3482
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003483/**
Mario Six8fac2912018-07-10 08:40:17 +02003484 * efi_disconnect_controller() - disconnect a controller from a driver
3485 * @controller_handle: handle of the controller
3486 * @driver_image_handle: handle of the driver
3487 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003488 *
3489 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003490 *
3491 * See the Unified Extensible Firmware Interface (UEFI) specification for
3492 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003493 *
Mario Six8fac2912018-07-10 08:40:17 +02003494 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003495 */
3496static efi_status_t EFIAPI efi_disconnect_controller(
3497 efi_handle_t controller_handle,
3498 efi_handle_t driver_image_handle,
3499 efi_handle_t child_handle)
3500{
3501 struct efi_driver_binding_protocol *binding_protocol;
3502 efi_handle_t *child_handle_buffer = NULL;
3503 size_t number_of_children = 0;
3504 efi_status_t r;
3505 size_t stop_count = 0;
3506 struct efi_object *efiobj;
3507
3508 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3509 child_handle);
3510
3511 efiobj = efi_search_obj(controller_handle);
3512 if (!efiobj) {
3513 r = EFI_INVALID_PARAMETER;
3514 goto out;
3515 }
3516
3517 if (child_handle && !efi_search_obj(child_handle)) {
3518 r = EFI_INVALID_PARAMETER;
3519 goto out;
3520 }
3521
3522 /* If no driver handle is supplied, disconnect all drivers */
3523 if (!driver_image_handle) {
3524 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3525 goto out;
3526 }
3527
3528 /* Create list of child handles */
3529 if (child_handle) {
3530 number_of_children = 1;
3531 child_handle_buffer = &child_handle;
3532 } else {
3533 efi_get_child_controllers(efiobj,
3534 driver_image_handle,
3535 &number_of_children,
3536 &child_handle_buffer);
3537 }
3538
3539 /* Get the driver binding protocol */
3540 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3541 &efi_guid_driver_binding_protocol,
3542 (void **)&binding_protocol,
3543 driver_image_handle, NULL,
3544 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3545 if (r != EFI_SUCCESS)
3546 goto out;
3547 /* Remove the children */
3548 if (number_of_children) {
3549 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3550 controller_handle,
3551 number_of_children,
3552 child_handle_buffer));
3553 if (r == EFI_SUCCESS)
3554 ++stop_count;
3555 }
3556 /* Remove the driver */
3557 if (!child_handle)
3558 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3559 controller_handle,
3560 0, NULL));
3561 if (r == EFI_SUCCESS)
3562 ++stop_count;
3563 EFI_CALL(efi_close_protocol(driver_image_handle,
3564 &efi_guid_driver_binding_protocol,
3565 driver_image_handle, NULL));
3566
3567 if (stop_count)
3568 r = EFI_SUCCESS;
3569 else
3570 r = EFI_NOT_FOUND;
3571out:
3572 if (!child_handle)
3573 free(child_handle_buffer);
3574 return EFI_EXIT(r);
3575}
3576
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003577static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003578 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003579 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3580 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003581 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003582 },
3583 .raise_tpl = efi_raise_tpl,
3584 .restore_tpl = efi_restore_tpl,
3585 .allocate_pages = efi_allocate_pages_ext,
3586 .free_pages = efi_free_pages_ext,
3587 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003588 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003589 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003590 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003591 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003592 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003593 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003594 .close_event = efi_close_event,
3595 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003596 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003597 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003598 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003599 .handle_protocol = efi_handle_protocol,
3600 .reserved = NULL,
3601 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003602 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003603 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003604 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003605 .load_image = efi_load_image,
3606 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003607 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003608 .unload_image = efi_unload_image,
3609 .exit_boot_services = efi_exit_boot_services,
3610 .get_next_monotonic_count = efi_get_next_monotonic_count,
3611 .stall = efi_stall,
3612 .set_watchdog_timer = efi_set_watchdog_timer,
3613 .connect_controller = efi_connect_controller,
3614 .disconnect_controller = efi_disconnect_controller,
3615 .open_protocol = efi_open_protocol,
3616 .close_protocol = efi_close_protocol,
3617 .open_protocol_information = efi_open_protocol_information,
3618 .protocols_per_handle = efi_protocols_per_handle,
3619 .locate_handle_buffer = efi_locate_handle_buffer,
3620 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003621 .install_multiple_protocol_interfaces =
3622 efi_install_multiple_protocol_interfaces,
3623 .uninstall_multiple_protocol_interfaces =
3624 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003625 .calculate_crc32 = efi_calculate_crc32,
3626 .copy_mem = efi_copy_mem,
3627 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003628 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003629};
3630
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003631static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003632
Alexander Graf393dd912016-10-14 13:45:30 +02003633struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003634 .hdr = {
3635 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003636 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003637 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003638 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003639 .fw_vendor = firmware_vendor,
3640 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003641 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003642 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003643 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003644};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003645
3646/**
3647 * efi_initialize_system_table() - Initialize system table
3648 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003649 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003650 */
3651efi_status_t efi_initialize_system_table(void)
3652{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003653 efi_status_t ret;
3654
3655 /* Allocate configuration table array */
3656 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3657 EFI_MAX_CONFIGURATION_TABLES *
3658 sizeof(struct efi_configuration_table),
3659 (void **)&systab.tables);
3660
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003661 /*
3662 * These entries will be set to NULL in ExitBootServices(). To avoid
3663 * relocation in SetVirtualAddressMap(), set them dynamically.
3664 */
3665 systab.con_in = &efi_con_in;
3666 systab.con_out = &efi_con_out;
3667 systab.std_err = &efi_con_out;
3668 systab.boottime = &efi_boot_services;
3669
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003670 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003671 efi_update_table_header_crc32(&systab.hdr);
3672 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3673 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003674
3675 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003676}