blob: b26ac9fbfc797965fad56f3cfe2150ceae29ca0e [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>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030022#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010023
24DECLARE_GLOBAL_DATA_PTR;
25
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020026/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010027static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028
Alexander Grafc15d9212016-03-04 01:09:59 +010029/* This list contains all the EFI objects our payload has access to */
30LIST_HEAD(efi_obj_list);
31
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010032/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020033__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020035/* List of queued events */
36LIST_HEAD(efi_event_queue);
37
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020038/* Flag to disable timer activity in ExitBootServices() */
39static bool timers_enabled = true;
40
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020041/* List of all events registered by RegisterProtocolNotify() */
42LIST_HEAD(efi_register_notify_events);
43
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010044/* Handle of the currently executing image */
45static efi_handle_t current_image;
46
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020047#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010048/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020049 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010050 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020054static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060055#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010056
Heinrich Schuchardt72506232019-02-09 14:10:39 +010057/* 1 if inside U-Boot code, 0 if inside EFI payload code */
58static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040059static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010060/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010062/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040065
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010066/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010082static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010086
Rob Clark86789d52017-07-27 08:04:18 -040087/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020091#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -040092 assert(efi_gd);
93 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020094 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -040095#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200103#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200104 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400105#endif
106 return ret;
107}
108
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200109/**
110 * efi_save_gd() - save global data register
111 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200112 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200113 * As this register may be overwritten by an EFI payload we save it here
114 * and restore it on every callback entered.
115 *
116 * This function is called after relocation from initr_reloc_global_data().
117 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100118void efi_save_gd(void)
119{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200120#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100121 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600122#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100123}
124
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200125/**
126 * efi_restore_gd() - restore global data register
127 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200128 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200129 * Restore it after returning from the UEFI world to the value saved via
130 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400131 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100132void efi_restore_gd(void)
133{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200134#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100135 /* Only restore if we're already in EFI context */
136 if (!efi_gd)
137 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200138 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600139#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100140}
141
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200142/**
Mario Six8fac2912018-07-10 08:40:17 +0200143 * indent_string() - returns a string for indenting with two spaces per level
144 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100145 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200146 * A maximum of ten indent levels is supported. Higher indent levels will be
147 * truncated.
148 *
Mario Six8fac2912018-07-10 08:40:17 +0200149 * Return: A string for indenting with two spaces per level is
150 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400151 */
152static const char *indent_string(int level)
153{
154 const char *indent = " ";
155 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100156
Rob Clarke7896c32017-07-27 08:04:19 -0400157 level = min(max, level * 2);
158 return &indent[max - level];
159}
160
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200161const char *__efi_nesting(void)
162{
163 return indent_string(nesting_level);
164}
165
Rob Clarke7896c32017-07-27 08:04:19 -0400166const char *__efi_nesting_inc(void)
167{
168 return indent_string(nesting_level++);
169}
170
171const char *__efi_nesting_dec(void)
172{
173 return indent_string(--nesting_level);
174}
175
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200176/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200177 * efi_event_is_queued() - check if an event is queued
178 *
179 * @event: event
180 * Return: true if event is queued
181 */
182static bool efi_event_is_queued(struct efi_event *event)
183{
184 return !!event->queue_link.next;
185}
186
187/**
188 * efi_process_event_queue() - process event queue
189 */
190static void efi_process_event_queue(void)
191{
192 while (!list_empty(&efi_event_queue)) {
193 struct efi_event *event;
194 efi_uintn_t old_tpl;
195
196 event = list_first_entry(&efi_event_queue, struct efi_event,
197 queue_link);
198 if (efi_tpl >= event->notify_tpl)
199 return;
200 list_del(&event->queue_link);
201 event->queue_link.next = NULL;
202 event->queue_link.prev = NULL;
203 /* Events must be executed at the event's TPL */
204 old_tpl = efi_tpl;
205 efi_tpl = event->notify_tpl;
206 EFI_CALL_VOID(event->notify_function(event,
207 event->notify_context));
208 efi_tpl = old_tpl;
209 if (event->type == EVT_NOTIFY_SIGNAL)
210 event->is_signaled = 0;
211 }
212}
213
214/**
Mario Six8fac2912018-07-10 08:40:17 +0200215 * efi_queue_event() - queue an EFI event
216 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200217 *
218 * This function queues the notification function of the event for future
219 * execution.
220 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200221 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200222static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200223{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100224 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200225
226 if (!event->notify_function)
227 return;
228
229 if (!efi_event_is_queued(event)) {
230 /*
231 * Events must be notified in order of decreasing task priority
232 * level. Insert the new event accordingly.
233 */
234 list_for_each_entry(item, &efi_event_queue, queue_link) {
235 if (item->notify_tpl < event->notify_tpl) {
236 list_add_tail(&event->queue_link,
237 &item->queue_link);
238 event = NULL;
239 break;
240 }
241 }
242 if (event)
243 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200244 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200245 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200246}
247
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200248/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200249 * is_valid_tpl() - check if the task priority level is valid
250 *
251 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200252 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200253 */
254efi_status_t is_valid_tpl(efi_uintn_t tpl)
255{
256 switch (tpl) {
257 case TPL_APPLICATION:
258 case TPL_CALLBACK:
259 case TPL_NOTIFY:
260 case TPL_HIGH_LEVEL:
261 return EFI_SUCCESS;
262 default:
263 return EFI_INVALID_PARAMETER;
264 }
265}
266
267/**
Mario Six8fac2912018-07-10 08:40:17 +0200268 * efi_signal_event() - signal an EFI event
269 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100270 *
Mario Six8fac2912018-07-10 08:40:17 +0200271 * This function signals an event. If the event belongs to an event group all
272 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100273 * their notification function is queued.
274 *
275 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100276 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200277void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100278{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200279 if (event->is_signaled)
280 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100281 if (event->group) {
282 struct efi_event *evt;
283
284 /*
285 * The signaled state has to set before executing any
286 * notification function
287 */
288 list_for_each_entry(evt, &efi_events, link) {
289 if (!evt->group || guidcmp(evt->group, event->group))
290 continue;
291 if (evt->is_signaled)
292 continue;
293 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100294 }
295 list_for_each_entry(evt, &efi_events, link) {
296 if (!evt->group || guidcmp(evt->group, event->group))
297 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200298 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100299 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200300 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100301 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200302 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100303 }
304}
305
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200306/**
Mario Six8fac2912018-07-10 08:40:17 +0200307 * efi_raise_tpl() - raise the task priority level
308 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200309 *
310 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200311 *
312 * See the Unified Extensible Firmware Interface (UEFI) specification for
313 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200314 *
Mario Six8fac2912018-07-10 08:40:17 +0200315 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200316 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100317static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100318{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100319 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200320
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200321 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200322
323 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200324 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200325 efi_tpl = new_tpl;
326 if (efi_tpl > TPL_HIGH_LEVEL)
327 efi_tpl = TPL_HIGH_LEVEL;
328
329 EFI_EXIT(EFI_SUCCESS);
330 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100331}
332
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200333/**
Mario Six8fac2912018-07-10 08:40:17 +0200334 * efi_restore_tpl() - lower the task priority level
335 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200336 *
337 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200338 *
Mario Six8fac2912018-07-10 08:40:17 +0200339 * See the Unified Extensible Firmware Interface (UEFI) specification for
340 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200341 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100342static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100343{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200344 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200345
346 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200347 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200348 efi_tpl = old_tpl;
349 if (efi_tpl > TPL_HIGH_LEVEL)
350 efi_tpl = TPL_HIGH_LEVEL;
351
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100352 /*
353 * Lowering the TPL may have made queued events eligible for execution.
354 */
355 efi_timer_check();
356
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200357 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100358}
359
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200360/**
Mario Six8fac2912018-07-10 08:40:17 +0200361 * efi_allocate_pages_ext() - allocate memory pages
362 * @type: type of allocation to be performed
363 * @memory_type: usage type of the allocated memory
364 * @pages: number of pages to be allocated
365 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 *
367 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200368 *
Mario Six8fac2912018-07-10 08:40:17 +0200369 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * details.
371 *
372 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900374static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100375 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900376 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100377{
378 efi_status_t r;
379
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100380 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100381 r = efi_allocate_pages(type, memory_type, pages, memory);
382 return EFI_EXIT(r);
383}
384
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200385/**
Mario Six8fac2912018-07-10 08:40:17 +0200386 * efi_free_pages_ext() - Free memory pages.
387 * @memory: start of the memory area to be freed
388 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200389 *
390 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200391 *
392 * See the Unified Extensible Firmware Interface (UEFI) specification for
393 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200394 *
Mario Six8fac2912018-07-10 08:40:17 +0200395 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900397static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100398 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100399{
400 efi_status_t r;
401
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900402 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100403 r = efi_free_pages(memory, pages);
404 return EFI_EXIT(r);
405}
406
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200407/**
Mario Six8fac2912018-07-10 08:40:17 +0200408 * efi_get_memory_map_ext() - get map describing memory usage
409 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
410 * on exit the size of the copied memory map
411 * @memory_map: buffer to which the memory map is written
412 * @map_key: key for the memory map
413 * @descriptor_size: size of an individual memory descriptor
414 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200415 *
416 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200417 *
Mario Six8fac2912018-07-10 08:40:17 +0200418 * See the Unified Extensible Firmware Interface (UEFI) specification for
419 * details.
420 *
421 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200422 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900423static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100424 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900425 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100426 efi_uintn_t *map_key,
427 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900428 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100429{
430 efi_status_t r;
431
432 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
433 map_key, descriptor_size, descriptor_version);
434 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
435 descriptor_size, descriptor_version);
436 return EFI_EXIT(r);
437}
438
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200439/**
Mario Six8fac2912018-07-10 08:40:17 +0200440 * efi_allocate_pool_ext() - allocate memory from pool
441 * @pool_type: type of the pool from which memory is to be allocated
442 * @size: number of bytes to be allocated
443 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200444 *
445 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200446 *
447 * See the Unified Extensible Firmware Interface (UEFI) specification for
448 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200449 *
Mario Six8fac2912018-07-10 08:40:17 +0200450 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200451 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200452static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100453 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200454 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100455{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100456 efi_status_t r;
457
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100458 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200459 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100460 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100461}
462
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200463/**
Mario Six8fac2912018-07-10 08:40:17 +0200464 * efi_free_pool_ext() - free memory from pool
465 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200466 *
467 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200468 *
Mario Six8fac2912018-07-10 08:40:17 +0200469 * See the Unified Extensible Firmware Interface (UEFI) specification for
470 * details.
471 *
472 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200473 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200474static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100475{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100476 efi_status_t r;
477
478 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200479 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100480 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100481}
482
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200483/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200484 * efi_add_handle() - add a new handle to the object list
485 *
486 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100487 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200488 * The protocols list is initialized. The handle is added to the list of known
489 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100490 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200491void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100492{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200493 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100494 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200495 INIT_LIST_HEAD(&handle->protocols);
496 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100497}
498
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200499/**
Mario Six8fac2912018-07-10 08:40:17 +0200500 * efi_create_handle() - create handle
501 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200502 *
Mario Six8fac2912018-07-10 08:40:17 +0200503 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200504 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100505efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200506{
507 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200508
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200509 obj = calloc(1, sizeof(struct efi_object));
510 if (!obj)
511 return EFI_OUT_OF_RESOURCES;
512
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100513 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200514 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200515
516 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200517}
518
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200519/**
Mario Six8fac2912018-07-10 08:40:17 +0200520 * efi_search_protocol() - find a protocol on a handle.
521 * @handle: handle
522 * @protocol_guid: GUID of the protocol
523 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 *
Mario Six8fac2912018-07-10 08:40:17 +0200525 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100527efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100528 const efi_guid_t *protocol_guid,
529 struct efi_handler **handler)
530{
531 struct efi_object *efiobj;
532 struct list_head *lhandle;
533
534 if (!handle || !protocol_guid)
535 return EFI_INVALID_PARAMETER;
536 efiobj = efi_search_obj(handle);
537 if (!efiobj)
538 return EFI_INVALID_PARAMETER;
539 list_for_each(lhandle, &efiobj->protocols) {
540 struct efi_handler *protocol;
541
542 protocol = list_entry(lhandle, struct efi_handler, link);
543 if (!guidcmp(protocol->guid, protocol_guid)) {
544 if (handler)
545 *handler = protocol;
546 return EFI_SUCCESS;
547 }
548 }
549 return EFI_NOT_FOUND;
550}
551
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200552/**
Mario Six8fac2912018-07-10 08:40:17 +0200553 * efi_remove_protocol() - delete protocol from a handle
554 * @handle: handle from which the protocol shall be deleted
555 * @protocol: GUID of the protocol to be deleted
556 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557 *
Mario Six8fac2912018-07-10 08:40:17 +0200558 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100559 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100560efi_status_t efi_remove_protocol(const efi_handle_t handle,
561 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100562 void *protocol_interface)
563{
564 struct efi_handler *handler;
565 efi_status_t ret;
566
567 ret = efi_search_protocol(handle, protocol, &handler);
568 if (ret != EFI_SUCCESS)
569 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200570 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200571 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100572 list_del(&handler->link);
573 free(handler);
574 return EFI_SUCCESS;
575}
576
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200577/**
Mario Six8fac2912018-07-10 08:40:17 +0200578 * efi_remove_all_protocols() - delete all protocols from a handle
579 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100580 *
Mario Six8fac2912018-07-10 08:40:17 +0200581 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100582 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100583efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100584{
585 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100586 struct efi_handler *protocol;
587 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100588
589 efiobj = efi_search_obj(handle);
590 if (!efiobj)
591 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100592 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100593 efi_status_t ret;
594
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100595 ret = efi_remove_protocol(handle, protocol->guid,
596 protocol->protocol_interface);
597 if (ret != EFI_SUCCESS)
598 return ret;
599 }
600 return EFI_SUCCESS;
601}
602
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200603/**
Mario Six8fac2912018-07-10 08:40:17 +0200604 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100605 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200606 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100607 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200608void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100609{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200610 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100611 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200612 efi_remove_all_protocols(handle);
613 list_del(&handle->link);
614 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100615}
616
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200617/**
Mario Six8fac2912018-07-10 08:40:17 +0200618 * efi_is_event() - check if a pointer is a valid event
619 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100620 *
Mario Six8fac2912018-07-10 08:40:17 +0200621 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100622 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100623static efi_status_t efi_is_event(const struct efi_event *event)
624{
625 const struct efi_event *evt;
626
627 if (!event)
628 return EFI_INVALID_PARAMETER;
629 list_for_each_entry(evt, &efi_events, link) {
630 if (evt == event)
631 return EFI_SUCCESS;
632 }
633 return EFI_INVALID_PARAMETER;
634}
Alexander Grafc15d9212016-03-04 01:09:59 +0100635
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200636/**
Mario Six8fac2912018-07-10 08:40:17 +0200637 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200638 *
Mario Six8fac2912018-07-10 08:40:17 +0200639 * @type: type of the event to create
640 * @notify_tpl: task priority level of the event
641 * @notify_function: notification function of the event
642 * @notify_context: pointer passed to the notification function
643 * @group: event group
644 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200645 *
646 * This function is used inside U-Boot code to create an event.
647 *
648 * For the API function implementing the CreateEvent service see
649 * efi_create_event_ext.
650 *
Mario Six8fac2912018-07-10 08:40:17 +0200651 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200652 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100653efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200654 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200655 struct efi_event *event,
656 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100657 void *notify_context, efi_guid_t *group,
658 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100659{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100660 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200661 efi_status_t ret;
662 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200663
Jonathan Gray7758b212017-03-12 19:26:07 +1100664 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200665 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100666
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200667 switch (type) {
668 case 0:
669 case EVT_TIMER:
670 case EVT_NOTIFY_SIGNAL:
671 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
672 case EVT_NOTIFY_WAIT:
673 case EVT_TIMER | EVT_NOTIFY_WAIT:
674 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200675 pool_type = EFI_BOOT_SERVICES_DATA;
676 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200677 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200678 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200679 break;
680 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200681 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200682 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100683
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900684 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200685 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200686 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100687
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200688 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
689 (void **)&evt);
690 if (ret != EFI_SUCCESS)
691 return ret;
692 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100693 evt->type = type;
694 evt->notify_tpl = notify_tpl;
695 evt->notify_function = notify_function;
696 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100697 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200698 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100699 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100700 list_add_tail(&evt->link, &efi_events);
701 *event = evt;
702 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100703}
704
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200705/*
Mario Six8fac2912018-07-10 08:40:17 +0200706 * efi_create_event_ex() - create an event in a group
707 * @type: type of the event to create
708 * @notify_tpl: task priority level of the event
709 * @notify_function: notification function of the event
710 * @notify_context: pointer passed to the notification function
711 * @event: created event
712 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100713 *
714 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100715 *
Mario Six8fac2912018-07-10 08:40:17 +0200716 * See the Unified Extensible Firmware Interface (UEFI) specification for
717 * details.
718 *
719 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100720 */
721efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
722 void (EFIAPI *notify_function) (
723 struct efi_event *event,
724 void *context),
725 void *notify_context,
726 efi_guid_t *event_group,
727 struct efi_event **event)
728{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200729 efi_status_t ret;
730
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100731 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
732 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200733
734 /*
735 * The allowable input parameters are the same as in CreateEvent()
736 * except for the following two disallowed event types.
737 */
738 switch (type) {
739 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
740 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
741 ret = EFI_INVALID_PARAMETER;
742 goto out;
743 }
744
745 ret = efi_create_event(type, notify_tpl, notify_function,
746 notify_context, event_group, event);
747out:
748 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100749}
750
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200751/**
Mario Six8fac2912018-07-10 08:40:17 +0200752 * efi_create_event_ext() - create an event
753 * @type: type of the event to create
754 * @notify_tpl: task priority level of the event
755 * @notify_function: notification function of the event
756 * @notify_context: pointer passed to the notification function
757 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200758 *
759 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200760 *
761 * See the Unified Extensible Firmware Interface (UEFI) specification for
762 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200763 *
Mario Six8fac2912018-07-10 08:40:17 +0200764 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200765 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200766static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100767 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200768 void (EFIAPI *notify_function) (
769 struct efi_event *event,
770 void *context),
771 void *notify_context, struct efi_event **event)
772{
773 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
774 notify_context);
775 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100776 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200777}
778
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200779/**
Mario Six8fac2912018-07-10 08:40:17 +0200780 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200781 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200782 * Check if a timer event has occurred or a queued notification function should
783 * be called.
784 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100785 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200786 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100787 */
788void efi_timer_check(void)
789{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100790 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100791 u64 now = timer_get_us();
792
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100793 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200794 if (!timers_enabled)
795 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100796 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200797 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100798 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200799 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100800 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200801 break;
802 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100803 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200804 break;
805 default:
806 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200807 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100808 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200809 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100810 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200811 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100812 WATCHDOG_RESET();
813}
814
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200815/**
Mario Six8fac2912018-07-10 08:40:17 +0200816 * efi_set_timer() - set the trigger time for a timer event or stop the event
817 * @event: event for which the timer is set
818 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200819 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200820 *
821 * This is the function for internal usage in U-Boot. For the API function
822 * implementing the SetTimer service see efi_set_timer_ext.
823 *
Mario Six8fac2912018-07-10 08:40:17 +0200824 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200825 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200826efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200827 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100828{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100829 /* Check that the event is valid */
830 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
831 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100832
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200833 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200834 * The parameter defines a multiple of 100 ns.
835 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200836 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200837 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100838
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100839 switch (type) {
840 case EFI_TIMER_STOP:
841 event->trigger_next = -1ULL;
842 break;
843 case EFI_TIMER_PERIODIC:
844 case EFI_TIMER_RELATIVE:
845 event->trigger_next = timer_get_us() + trigger_time;
846 break;
847 default:
848 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100849 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100850 event->trigger_type = type;
851 event->trigger_time = trigger_time;
852 event->is_signaled = false;
853 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100854}
855
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200856/**
Mario Six8fac2912018-07-10 08:40:17 +0200857 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
858 * event
859 * @event: event for which the timer is set
860 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200861 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200862 *
863 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200864 *
Mario Six8fac2912018-07-10 08:40:17 +0200865 * See the Unified Extensible Firmware Interface (UEFI) specification for
866 * details.
867 *
868 *
869 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200870 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200871static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
872 enum efi_timer_delay type,
873 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200874{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900875 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200876 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
877}
878
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200879/**
Mario Six8fac2912018-07-10 08:40:17 +0200880 * efi_wait_for_event() - wait for events to be signaled
881 * @num_events: number of events to be waited for
882 * @event: events to be waited for
883 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 *
885 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 *
Mario Six8fac2912018-07-10 08:40:17 +0200887 * See the Unified Extensible Firmware Interface (UEFI) specification for
888 * details.
889 *
890 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100892static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200893 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100894 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100895{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100896 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100897
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100898 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100899
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200900 /* Check parameters */
901 if (!num_events || !event)
902 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200903 /* Check TPL */
904 if (efi_tpl != TPL_APPLICATION)
905 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200906 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100907 if (efi_is_event(event[i]) != EFI_SUCCESS)
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200909 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
910 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200911 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200912 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200913 }
914
915 /* Wait for signal */
916 for (;;) {
917 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200918 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 goto out;
920 }
921 /* Allow events to occur. */
922 efi_timer_check();
923 }
924
925out:
926 /*
927 * Reset the signal which is passed to the caller to allow periodic
928 * events to occur.
929 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200930 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200931 if (index)
932 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100933
934 return EFI_EXIT(EFI_SUCCESS);
935}
936
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200937/**
Mario Six8fac2912018-07-10 08:40:17 +0200938 * efi_signal_event_ext() - signal an EFI event
939 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200940 *
941 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200942 *
943 * See the Unified Extensible Firmware Interface (UEFI) specification for
944 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200945 *
946 * This functions sets the signaled state of the event and queues the
947 * notification function for execution.
948 *
Mario Six8fac2912018-07-10 08:40:17 +0200949 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200950 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200951static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100952{
953 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100954 if (efi_is_event(event) != EFI_SUCCESS)
955 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200956 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100957 return EFI_EXIT(EFI_SUCCESS);
958}
959
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200960/**
Mario Six8fac2912018-07-10 08:40:17 +0200961 * efi_close_event() - close an EFI event
962 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200963 *
964 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200965 *
Mario Six8fac2912018-07-10 08:40:17 +0200966 * See the Unified Extensible Firmware Interface (UEFI) specification for
967 * details.
968 *
969 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200970 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200971static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100972{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200973 struct efi_register_notify_event *item, *next;
974
Alexander Grafc15d9212016-03-04 01:09:59 +0100975 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100976 if (efi_is_event(event) != EFI_SUCCESS)
977 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200978
979 /* Remove protocol notify registrations for the event */
980 list_for_each_entry_safe(item, next, &efi_register_notify_events,
981 link) {
982 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200983 struct efi_protocol_notification *hitem, *hnext;
984
985 /* Remove signaled handles */
986 list_for_each_entry_safe(hitem, hnext, &item->handles,
987 link) {
988 list_del(&hitem->link);
989 free(hitem);
990 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200991 list_del(&item->link);
992 free(item);
993 }
994 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200995 /* Remove event from queue */
996 if (efi_event_is_queued(event))
997 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200998
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100999 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001000 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001001 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001002}
1003
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001004/**
Mario Six8fac2912018-07-10 08:40:17 +02001005 * efi_check_event() - check if an event is signaled
1006 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001007 *
1008 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001009 *
1010 * See the Unified Extensible Firmware Interface (UEFI) specification for
1011 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001012 *
Mario Six8fac2912018-07-10 08:40:17 +02001013 * If an event is not signaled yet, the notification function is queued. The
1014 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001015 *
Mario Six8fac2912018-07-10 08:40:17 +02001016 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001017 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001018static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001019{
1020 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001021 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001022 if (efi_is_event(event) != EFI_SUCCESS ||
1023 event->type & EVT_NOTIFY_SIGNAL)
1024 return EFI_EXIT(EFI_INVALID_PARAMETER);
1025 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001026 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001027 if (event->is_signaled) {
1028 event->is_signaled = false;
1029 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001030 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001031 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001032}
1033
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001034/**
Mario Six8fac2912018-07-10 08:40:17 +02001035 * efi_search_obj() - find the internal EFI object for a handle
1036 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001037 *
Mario Six8fac2912018-07-10 08:40:17 +02001038 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001039 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001040struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001041{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001042 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001043
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001044 if (!handle)
1045 return NULL;
1046
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001047 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001048 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001049 return efiobj;
1050 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001051 return NULL;
1052}
1053
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001054/**
Mario Six8fac2912018-07-10 08:40:17 +02001055 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1056 * to a protocol
1057 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001058 *
Mario Six8fac2912018-07-10 08:40:17 +02001059 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001060 */
1061static struct efi_open_protocol_info_entry *efi_create_open_info(
1062 struct efi_handler *handler)
1063{
1064 struct efi_open_protocol_info_item *item;
1065
1066 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1067 if (!item)
1068 return NULL;
1069 /* Append the item to the open protocol info list. */
1070 list_add_tail(&item->link, &handler->open_infos);
1071
1072 return &item->info;
1073}
1074
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001075/**
Mario Six8fac2912018-07-10 08:40:17 +02001076 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1077 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001078 *
Mario Six8fac2912018-07-10 08:40:17 +02001079 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001080 */
1081static efi_status_t efi_delete_open_info(
1082 struct efi_open_protocol_info_item *item)
1083{
1084 list_del(&item->link);
1085 free(item);
1086 return EFI_SUCCESS;
1087}
1088
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001089/**
Mario Six8fac2912018-07-10 08:40:17 +02001090 * efi_add_protocol() - install new protocol on a handle
1091 * @handle: handle on which the protocol shall be installed
1092 * @protocol: GUID of the protocol to be installed
1093 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001094 *
Mario Six8fac2912018-07-10 08:40:17 +02001095 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001096 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001097efi_status_t efi_add_protocol(const efi_handle_t handle,
1098 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001099 void *protocol_interface)
1100{
1101 struct efi_object *efiobj;
1102 struct efi_handler *handler;
1103 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001104 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001105
1106 efiobj = efi_search_obj(handle);
1107 if (!efiobj)
1108 return EFI_INVALID_PARAMETER;
1109 ret = efi_search_protocol(handle, protocol, NULL);
1110 if (ret != EFI_NOT_FOUND)
1111 return EFI_INVALID_PARAMETER;
1112 handler = calloc(1, sizeof(struct efi_handler));
1113 if (!handler)
1114 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001115 handler->guid = protocol;
1116 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001117 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001118 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001119
1120 /* Notify registered events */
1121 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001122 if (!guidcmp(protocol, &event->protocol)) {
1123 struct efi_protocol_notification *notif;
1124
1125 notif = calloc(1, sizeof(*notif));
1126 if (!notif) {
1127 list_del(&handler->link);
1128 free(handler);
1129 return EFI_OUT_OF_RESOURCES;
1130 }
1131 notif->handle = handle;
1132 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001133 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001134 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001135 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001136 }
1137
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001138 if (!guidcmp(&efi_guid_device_path, protocol))
1139 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001140 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001141}
1142
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001143/**
Mario Six8fac2912018-07-10 08:40:17 +02001144 * efi_install_protocol_interface() - install protocol interface
1145 * @handle: handle on which the protocol shall be installed
1146 * @protocol: GUID of the protocol to be installed
1147 * @protocol_interface_type: type of the interface to be installed,
1148 * always EFI_NATIVE_INTERFACE
1149 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001150 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001151 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001152 *
Mario Six8fac2912018-07-10 08:40:17 +02001153 * See the Unified Extensible Firmware Interface (UEFI) specification for
1154 * details.
1155 *
1156 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001157 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001158static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001159 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001160 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001161{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001162 efi_status_t r;
1163
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001164 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1165 protocol_interface);
1166
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001167 if (!handle || !protocol ||
1168 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1169 r = EFI_INVALID_PARAMETER;
1170 goto out;
1171 }
1172
1173 /* Create new handle if requested. */
1174 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001175 r = efi_create_handle(handle);
1176 if (r != EFI_SUCCESS)
1177 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001178 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001179 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001180 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001181 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001182 /* Add new protocol */
1183 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001184out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001185 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001186}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001187
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001188/**
Mario Six8fac2912018-07-10 08:40:17 +02001189 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001190 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001191 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001192 * @number_of_drivers: number of child controllers
1193 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001194 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001195 * The allocated buffer has to be freed with free().
1196 *
Mario Six8fac2912018-07-10 08:40:17 +02001197 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001198 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001199static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001200 const efi_guid_t *protocol,
1201 efi_uintn_t *number_of_drivers,
1202 efi_handle_t **driver_handle_buffer)
1203{
1204 struct efi_handler *handler;
1205 struct efi_open_protocol_info_item *item;
1206 efi_uintn_t count = 0, i;
1207 bool duplicate;
1208
1209 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001210 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001211 if (protocol && guidcmp(handler->guid, protocol))
1212 continue;
1213 list_for_each_entry(item, &handler->open_infos, link) {
1214 if (item->info.attributes &
1215 EFI_OPEN_PROTOCOL_BY_DRIVER)
1216 ++count;
1217 }
1218 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001219 *number_of_drivers = 0;
1220 if (!count) {
1221 *driver_handle_buffer = NULL;
1222 return EFI_SUCCESS;
1223 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001224 /*
1225 * Create buffer. In case of duplicate driver assignments the buffer
1226 * will be too large. But that does not harm.
1227 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001228 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1229 if (!*driver_handle_buffer)
1230 return EFI_OUT_OF_RESOURCES;
1231 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001232 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001233 if (protocol && guidcmp(handler->guid, protocol))
1234 continue;
1235 list_for_each_entry(item, &handler->open_infos, link) {
1236 if (item->info.attributes &
1237 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1238 /* Check this is a new driver */
1239 duplicate = false;
1240 for (i = 0; i < *number_of_drivers; ++i) {
1241 if ((*driver_handle_buffer)[i] ==
1242 item->info.agent_handle)
1243 duplicate = true;
1244 }
1245 /* Copy handle to buffer */
1246 if (!duplicate) {
1247 i = (*number_of_drivers)++;
1248 (*driver_handle_buffer)[i] =
1249 item->info.agent_handle;
1250 }
1251 }
1252 }
1253 }
1254 return EFI_SUCCESS;
1255}
1256
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001257/**
Mario Six8fac2912018-07-10 08:40:17 +02001258 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001259 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001260 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001261 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001262 *
1263 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001264 *
1265 * See the Unified Extensible Firmware Interface (UEFI) specification for
1266 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001267 *
Mario Six8fac2912018-07-10 08:40:17 +02001268 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001269 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001270static efi_status_t efi_disconnect_all_drivers
1271 (efi_handle_t handle,
1272 const efi_guid_t *protocol,
1273 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001274{
1275 efi_uintn_t number_of_drivers;
1276 efi_handle_t *driver_handle_buffer;
1277 efi_status_t r, ret;
1278
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001279 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001280 &driver_handle_buffer);
1281 if (ret != EFI_SUCCESS)
1282 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001283 if (!number_of_drivers)
1284 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001285 ret = EFI_NOT_FOUND;
1286 while (number_of_drivers) {
1287 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001288 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001289 driver_handle_buffer[--number_of_drivers],
1290 child_handle));
1291 if (r == EFI_SUCCESS)
1292 ret = r;
1293 }
1294 free(driver_handle_buffer);
1295 return ret;
1296}
1297
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001298/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001299 * efi_uninstall_protocol() - uninstall protocol interface
1300 *
Mario Six8fac2912018-07-10 08:40:17 +02001301 * @handle: handle from which the protocol shall be removed
1302 * @protocol: GUID of the protocol to be removed
1303 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001304 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001305 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001306 *
1307 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001308 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001309static efi_status_t efi_uninstall_protocol
1310 (efi_handle_t handle, const efi_guid_t *protocol,
1311 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001312{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001313 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001314 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001315 struct efi_open_protocol_info_item *item;
1316 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001317 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001318
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001319 /* Check handle */
1320 efiobj = efi_search_obj(handle);
1321 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001322 r = EFI_INVALID_PARAMETER;
1323 goto out;
1324 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001325 /* Find the protocol on the handle */
1326 r = efi_search_protocol(handle, protocol, &handler);
1327 if (r != EFI_SUCCESS)
1328 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001329 /* Disconnect controllers */
1330 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001331 /* Close protocol */
1332 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1333 if (item->info.attributes ==
1334 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1335 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1336 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1337 list_del(&item->link);
1338 }
1339 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001340 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001341 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001342 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001343 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001344out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001345 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001346}
1347
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001348/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001349 * efi_uninstall_protocol_interface() - uninstall protocol interface
1350 * @handle: handle from which the protocol shall be removed
1351 * @protocol: GUID of the protocol to be removed
1352 * @protocol_interface: interface to be removed
1353 *
1354 * This function implements the UninstallProtocolInterface service.
1355 *
1356 * See the Unified Extensible Firmware Interface (UEFI) specification for
1357 * details.
1358 *
1359 * Return: status code
1360 */
1361static efi_status_t EFIAPI efi_uninstall_protocol_interface
1362 (efi_handle_t handle, const efi_guid_t *protocol,
1363 void *protocol_interface)
1364{
1365 efi_status_t ret;
1366
1367 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1368
1369 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1370 if (ret != EFI_SUCCESS)
1371 goto out;
1372
1373 /* If the last protocol has been removed, delete the handle. */
1374 if (list_empty(&handle->protocols)) {
1375 list_del(&handle->link);
1376 free(handle);
1377 }
1378out:
1379 return EFI_EXIT(ret);
1380}
1381
1382/**
Mario Six8fac2912018-07-10 08:40:17 +02001383 * efi_register_protocol_notify() - register an event for notification when a
1384 * protocol is installed.
1385 * @protocol: GUID of the protocol whose installation shall be notified
1386 * @event: event to be signaled upon installation of the protocol
1387 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001388 *
1389 * This function implements the RegisterProtocolNotify service.
1390 * See the Unified Extensible Firmware Interface (UEFI) specification
1391 * for details.
1392 *
Mario Six8fac2912018-07-10 08:40:17 +02001393 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001394 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001395static efi_status_t EFIAPI efi_register_protocol_notify(
1396 const efi_guid_t *protocol,
1397 struct efi_event *event,
1398 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001399{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001400 struct efi_register_notify_event *item;
1401 efi_status_t ret = EFI_SUCCESS;
1402
Rob Clark238f88c2017-09-13 18:05:41 -04001403 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001404
1405 if (!protocol || !event || !registration) {
1406 ret = EFI_INVALID_PARAMETER;
1407 goto out;
1408 }
1409
1410 item = calloc(1, sizeof(struct efi_register_notify_event));
1411 if (!item) {
1412 ret = EFI_OUT_OF_RESOURCES;
1413 goto out;
1414 }
1415
1416 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301417 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001418 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001419
1420 list_add_tail(&item->link, &efi_register_notify_events);
1421
1422 *registration = item;
1423out:
1424 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001425}
1426
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001427/**
Mario Six8fac2912018-07-10 08:40:17 +02001428 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001429 *
Mario Six8fac2912018-07-10 08:40:17 +02001430 * @search_type: selection criterion
1431 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001432 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001433 *
1434 * See the documentation of the LocateHandle service in the UEFI specification.
1435 *
Mario Six8fac2912018-07-10 08:40:17 +02001436 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001437 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001438static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001439 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001440{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001441 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001442
1443 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001444 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001445 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001446 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001447 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001448 return (ret != EFI_SUCCESS);
1449 default:
1450 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001451 return -1;
1452 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001453}
1454
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001455/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001456 * efi_check_register_notify_event() - check if registration key is valid
1457 *
1458 * Check that a pointer is a valid registration key as returned by
1459 * RegisterProtocolNotify().
1460 *
1461 * @key: registration key
1462 * Return: valid registration key or NULL
1463 */
1464static struct efi_register_notify_event *efi_check_register_notify_event
1465 (void *key)
1466{
1467 struct efi_register_notify_event *event;
1468
1469 list_for_each_entry(event, &efi_register_notify_events, link) {
1470 if (event == (struct efi_register_notify_event *)key)
1471 return event;
1472 }
1473 return NULL;
1474}
1475
1476/**
Mario Six8fac2912018-07-10 08:40:17 +02001477 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001478 *
1479 * @search_type: selection criterion
1480 * @protocol: GUID of the protocol
1481 * @search_key: registration key
1482 * @buffer_size: size of the buffer to receive the handles in bytes
1483 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001484 *
1485 * This function is meant for U-Boot internal calls. For the API implementation
1486 * of the LocateHandle service see efi_locate_handle_ext.
1487 *
Mario Six8fac2912018-07-10 08:40:17 +02001488 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001489 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001490static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001491 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001492 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001493 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001494{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001495 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001496 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001497 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001498 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001499
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001500 /* Check parameters */
1501 switch (search_type) {
1502 case ALL_HANDLES:
1503 break;
1504 case BY_REGISTER_NOTIFY:
1505 if (!search_key)
1506 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001507 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001508 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001509 if (!event)
1510 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001511 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001512 case BY_PROTOCOL:
1513 if (!protocol)
1514 return EFI_INVALID_PARAMETER;
1515 break;
1516 default:
1517 return EFI_INVALID_PARAMETER;
1518 }
1519
Alexander Grafc15d9212016-03-04 01:09:59 +01001520 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001521 if (search_type == BY_REGISTER_NOTIFY) {
1522 if (list_empty(&event->handles))
1523 return EFI_NOT_FOUND;
1524 handle = list_first_entry(&event->handles,
1525 struct efi_protocol_notification,
1526 link);
1527 efiobj = handle->handle;
1528 size += sizeof(void *);
1529 } else {
1530 list_for_each_entry(efiobj, &efi_obj_list, link) {
1531 if (!efi_search(search_type, protocol, efiobj))
1532 size += sizeof(void *);
1533 }
1534 if (size == 0)
1535 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001536 }
1537
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001538 if (!buffer_size)
1539 return EFI_INVALID_PARAMETER;
1540
Alexander Grafc15d9212016-03-04 01:09:59 +01001541 if (*buffer_size < size) {
1542 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001543 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001544 }
1545
Rob Clarkcdee3372017-08-06 14:10:07 -04001546 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001547
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001548 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001549 if (!buffer)
1550 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001551
Alexander Grafc15d9212016-03-04 01:09:59 +01001552 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001553 if (search_type == BY_REGISTER_NOTIFY) {
1554 *buffer = efiobj;
1555 list_del(&handle->link);
1556 } else {
1557 list_for_each_entry(efiobj, &efi_obj_list, link) {
1558 if (!efi_search(search_type, protocol, efiobj))
1559 *buffer++ = efiobj;
1560 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001561 }
1562
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001563 return EFI_SUCCESS;
1564}
1565
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001566/**
Mario Six8fac2912018-07-10 08:40:17 +02001567 * efi_locate_handle_ext() - locate handles implementing a protocol.
1568 * @search_type: selection criterion
1569 * @protocol: GUID of the protocol
1570 * @search_key: registration key
1571 * @buffer_size: size of the buffer to receive the handles in bytes
1572 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001573 *
1574 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001575 *
Mario Six8fac2912018-07-10 08:40:17 +02001576 * See the Unified Extensible Firmware Interface (UEFI) specification for
1577 * details.
1578 *
1579 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001580 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001581static efi_status_t EFIAPI efi_locate_handle_ext(
1582 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001583 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001584 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001585{
Rob Clark238f88c2017-09-13 18:05:41 -04001586 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001587 buffer_size, buffer);
1588
1589 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1590 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001591}
1592
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001593/**
Mario Six8fac2912018-07-10 08:40:17 +02001594 * efi_remove_configuration_table() - collapses configuration table entries,
1595 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001596 *
Mario Six8fac2912018-07-10 08:40:17 +02001597 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001598 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001599static void efi_remove_configuration_table(int i)
1600{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001601 struct efi_configuration_table *this = &systab.tables[i];
1602 struct efi_configuration_table *next = &systab.tables[i + 1];
1603 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001604
1605 memmove(this, next, (ulong)end - (ulong)next);
1606 systab.nr_tables--;
1607}
1608
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001609/**
Mario Six8fac2912018-07-10 08:40:17 +02001610 * efi_install_configuration_table() - adds, updates, or removes a
1611 * configuration table
1612 * @guid: GUID of the installed table
1613 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001614 *
1615 * This function is used for internal calls. For the API implementation of the
1616 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1617 *
Mario Six8fac2912018-07-10 08:40:17 +02001618 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001619 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001620efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1621 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001622{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001623 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001624 int i;
1625
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001626 if (!guid)
1627 return EFI_INVALID_PARAMETER;
1628
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001629 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001630 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001631 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001632 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001633 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001634 else
1635 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001636 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001637 }
1638 }
1639
Alexander Graffe3366f2017-07-26 13:41:04 +02001640 if (!table)
1641 return EFI_NOT_FOUND;
1642
Alexander Grafc15d9212016-03-04 01:09:59 +01001643 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001644 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001645 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001646
1647 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301648 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001649 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001650 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001651
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001652out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001653 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001654 efi_update_table_header_crc32(&systab.hdr);
1655
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001656 /* Notify that the configuration table was changed */
1657 list_for_each_entry(evt, &efi_events, link) {
1658 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001659 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001660 break;
1661 }
1662 }
1663
Alexander Grafc5c11632016-08-19 01:23:24 +02001664 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001665}
1666
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001667/**
Mario Six8fac2912018-07-10 08:40:17 +02001668 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1669 * configuration table.
1670 * @guid: GUID of the installed table
1671 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001672 *
1673 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001674 *
Mario Six8fac2912018-07-10 08:40:17 +02001675 * See the Unified Extensible Firmware Interface (UEFI) specification for
1676 * details.
1677 *
1678 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001679 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001680static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1681 void *table)
1682{
Rob Clark238f88c2017-09-13 18:05:41 -04001683 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001684 return EFI_EXIT(efi_install_configuration_table(guid, table));
1685}
1686
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001687/**
Mario Six8fac2912018-07-10 08:40:17 +02001688 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001689 *
1690 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001691 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001692 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001693 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001694 * code is returned.
1695 *
1696 * @device_path: device path of the loaded image
1697 * @file_path: file path of the loaded image
1698 * @handle_ptr: handle of the loaded image
1699 * @info_ptr: loaded image protocol
1700 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001701 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001702efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1703 struct efi_device_path *file_path,
1704 struct efi_loaded_image_obj **handle_ptr,
1705 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001706{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001707 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001708 struct efi_loaded_image *info = NULL;
1709 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001710 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001711
1712 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1713 *handle_ptr = NULL;
1714 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001715
1716 info = calloc(1, sizeof(*info));
1717 if (!info)
1718 return EFI_OUT_OF_RESOURCES;
1719 obj = calloc(1, sizeof(*obj));
1720 if (!obj) {
1721 free(info);
1722 return EFI_OUT_OF_RESOURCES;
1723 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001724 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001725
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001726 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001727 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001728
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001729 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001730 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001731 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001732
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001733 if (device_path) {
1734 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001735
1736 dp = efi_dp_append(device_path, file_path);
1737 if (!dp) {
1738 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001739 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001740 }
1741 } else {
1742 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001743 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001744 ret = efi_add_protocol(&obj->header,
1745 &efi_guid_loaded_image_device_path, dp);
1746 if (ret != EFI_SUCCESS)
1747 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001748
1749 /*
1750 * When asking for the loaded_image interface, just
1751 * return handle which points to loaded_image_info
1752 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001753 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001754 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001755 if (ret != EFI_SUCCESS)
1756 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001757
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001758 *info_ptr = info;
1759 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001760
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001761 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001762failure:
1763 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001764 efi_delete_handle(&obj->header);
1765 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001766 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001767}
1768
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001769/**
Mario Six8fac2912018-07-10 08:40:17 +02001770 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001771 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001772 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1773 * callers obligation to update the memory type as needed.
1774 *
1775 * @file_path: the path of the image to load
1776 * @buffer: buffer containing the loaded image
1777 * @size: size of the loaded image
1778 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001779 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001780static
Rob Clarkc84c1102017-09-13 18:05:38 -04001781efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001782 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001783{
1784 struct efi_file_info *info = NULL;
1785 struct efi_file_handle *f;
1786 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001787 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001788 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001789
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001790 /* In case of failure nothing is returned */
1791 *buffer = NULL;
1792 *size = 0;
1793
1794 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001795 f = efi_file_from_path(file_path);
1796 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001797 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001798
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001799 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001800 bs = 0;
1801 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1802 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001803 if (ret != EFI_BUFFER_TOO_SMALL) {
1804 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001805 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001806 }
Rob Clark857a1222017-09-13 18:05:35 -04001807
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001808 info = malloc(bs);
1809 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1810 info));
1811 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001812 goto error;
1813
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001814 /*
1815 * When reading the file we do not yet know if it contains an
1816 * application, a boottime driver, or a runtime driver. So here we
1817 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1818 * update the reservation according to the image type.
1819 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001820 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001821 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1822 EFI_BOOT_SERVICES_DATA,
1823 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001824 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001825 ret = EFI_OUT_OF_RESOURCES;
1826 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001827 }
1828
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001829 /* Read file */
1830 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1831 if (ret != EFI_SUCCESS)
1832 efi_free_pages(addr, efi_size_in_pages(bs));
1833 *buffer = (void *)(uintptr_t)addr;
1834 *size = bs;
1835error:
1836 EFI_CALL(f->close(f));
1837 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001838 return ret;
1839}
1840
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001841/**
Mario Six8fac2912018-07-10 08:40:17 +02001842 * efi_load_image() - load an EFI image into memory
1843 * @boot_policy: true for request originating from the boot manager
1844 * @parent_image: the caller's image handle
1845 * @file_path: the path of the image to load
1846 * @source_buffer: memory location from which the image is installed
1847 * @source_size: size of the memory area from which the image is installed
1848 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001849 *
1850 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001851 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001852 * See the Unified Extensible Firmware Interface (UEFI) specification
1853 * for details.
1854 *
Mario Six8fac2912018-07-10 08:40:17 +02001855 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001856 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001857efi_status_t EFIAPI efi_load_image(bool boot_policy,
1858 efi_handle_t parent_image,
1859 struct efi_device_path *file_path,
1860 void *source_buffer,
1861 efi_uintn_t source_size,
1862 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001863{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001864 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001865 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001866 struct efi_loaded_image_obj **image_obj =
1867 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001868 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001869 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001870
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001871 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001872 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001873
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001874 if (!image_handle || (!source_buffer && !file_path) ||
1875 !efi_search_obj(parent_image) ||
1876 /* The parent image handle must refer to a loaded image */
1877 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001878 ret = EFI_INVALID_PARAMETER;
1879 goto error;
1880 }
Rob Clark857a1222017-09-13 18:05:35 -04001881
1882 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001883 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001884 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001885 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001886 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001887 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001888 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001889 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001890 /* split file_path which contains both the device and file parts */
1891 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001892 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001893 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001894 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001895 if (!source_buffer)
1896 /* Release buffer to which file was loaded */
1897 efi_free_pages((uintptr_t)dest_buffer,
1898 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001899 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001900 info->system_table = &systab;
1901 info->parent_handle = parent_image;
1902 } else {
1903 /* The image is invalid. Release all associated resources. */
1904 efi_delete_handle(*image_handle);
1905 *image_handle = NULL;
1906 free(info);
1907 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001908error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001909 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001910}
1911
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001912/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001913 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1914 */
1915static void efi_exit_caches(void)
1916{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001917#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01001918 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001919 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1920 * caches are enabled.
1921 *
1922 * TODO:
1923 * According to the UEFI spec caches that can be managed via CP15
1924 * operations should be enabled. Caches requiring platform information
1925 * to manage should be disabled. This should not happen in
1926 * ExitBootServices() but before invoking any UEFI binary is invoked.
1927 *
1928 * We want to keep the current workaround while GRUB prior to version
1929 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01001930 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001931 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01001932#endif
1933}
1934
1935/**
Mario Six8fac2912018-07-10 08:40:17 +02001936 * efi_exit_boot_services() - stop all boot services
1937 * @image_handle: handle of the loaded image
1938 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001939 *
1940 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001941 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001942 * See the Unified Extensible Firmware Interface (UEFI) specification
1943 * for details.
1944 *
Mario Six8fac2912018-07-10 08:40:17 +02001945 * All timer events are disabled. For exit boot services events the
1946 * notification function is called. The boot services are disabled in the
1947 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001948 *
Mario Six8fac2912018-07-10 08:40:17 +02001949 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001950 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001951static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001952 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001953{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001954 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001955 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001956
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001957 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001958
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001959 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001960 if (map_key != efi_memory_map_key) {
1961 ret = EFI_INVALID_PARAMETER;
1962 goto out;
1963 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001964
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001965 /* Check if ExitBootServices has already been called */
1966 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001967 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001968
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001969 /* Stop all timer related activities */
1970 timers_enabled = false;
1971
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001972 /* Add related events to the event group */
1973 list_for_each_entry(evt, &efi_events, link) {
1974 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1975 evt->group = &efi_guid_event_group_exit_boot_services;
1976 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001977 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001978 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001979 if (evt->group &&
1980 !guidcmp(evt->group,
1981 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001982 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001983 break;
1984 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001985 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001986
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001987 /* Make sure that notification functions are not called anymore */
1988 efi_tpl = TPL_HIGH_LEVEL;
1989
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001990 /* Notify variable services */
1991 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001992
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001993 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1994 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1995 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1996 list_del(&evt->link);
1997 }
1998
Ilias Apalodimasc539fb82020-10-22 01:04:21 +03001999 if IS_ENABLED(CONFIG_USB_DEVICE)
2000 udc_disconnect();
Alexander Graf2ebeb442016-11-17 01:02:57 +01002001 board_quiesce_devices();
Ilias Apalodimasc539fb82020-10-22 01:04:21 +03002002 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
Alexander Graf2ebeb442016-11-17 01:02:57 +01002003
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002004 /* Patch out unsupported runtime function */
2005 efi_runtime_detach();
2006
Alexander Graffa5246b2018-11-15 21:23:47 +01002007 /* Fix up caches for EFI payloads if necessary */
2008 efi_exit_caches();
2009
Alexander Grafc15d9212016-03-04 01:09:59 +01002010 /* This stops all lingering devices */
2011 bootm_disable_interrupts();
2012
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002013 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002014 systab.con_in_handle = NULL;
2015 systab.con_in = NULL;
2016 systab.con_out_handle = NULL;
2017 systab.con_out = NULL;
2018 systab.stderr_handle = NULL;
2019 systab.std_err = NULL;
2020 systab.boottime = NULL;
2021
2022 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002023 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002024
Alexander Grafc15d9212016-03-04 01:09:59 +01002025 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002026 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002027 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002028out:
2029 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002030}
2031
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002032/**
Mario Six8fac2912018-07-10 08:40:17 +02002033 * efi_get_next_monotonic_count() - get next value of the counter
2034 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002035 *
2036 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002037 *
2038 * See the Unified Extensible Firmware Interface (UEFI) specification for
2039 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002040 *
Mario Six8fac2912018-07-10 08:40:17 +02002041 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002042 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002043static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2044{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002045 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002046 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002047
Alexander Grafc15d9212016-03-04 01:09:59 +01002048 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002049 if (!count) {
2050 ret = EFI_INVALID_PARAMETER;
2051 goto out;
2052 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002053 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002054 ret = EFI_SUCCESS;
2055out:
2056 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002057}
2058
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002059/**
Mario Six8fac2912018-07-10 08:40:17 +02002060 * efi_stall() - sleep
2061 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002062 *
Mario Six8fac2912018-07-10 08:40:17 +02002063 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002064 *
Mario Six8fac2912018-07-10 08:40:17 +02002065 * See the Unified Extensible Firmware Interface (UEFI) specification for
2066 * details.
2067 *
2068 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002069 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002070static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2071{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002072 u64 end_tick;
2073
Alexander Grafc15d9212016-03-04 01:09:59 +01002074 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002075
2076 end_tick = get_ticks() + usec_to_tick(microseconds);
2077 while (get_ticks() < end_tick)
2078 efi_timer_check();
2079
Alexander Grafc15d9212016-03-04 01:09:59 +01002080 return EFI_EXIT(EFI_SUCCESS);
2081}
2082
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002083/**
Mario Six8fac2912018-07-10 08:40:17 +02002084 * efi_set_watchdog_timer() - reset the watchdog timer
2085 * @timeout: seconds before reset by watchdog
2086 * @watchdog_code: code to be logged when resetting
2087 * @data_size: size of buffer in bytes
2088 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002089 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002090 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002091 *
Mario Six8fac2912018-07-10 08:40:17 +02002092 * See the Unified Extensible Firmware Interface (UEFI) specification for
2093 * details.
2094 *
2095 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002096 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002097static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2098 uint64_t watchdog_code,
2099 unsigned long data_size,
2100 uint16_t *watchdog_data)
2101{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002102 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002103 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002104 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002105}
2106
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002107/**
Mario Six8fac2912018-07-10 08:40:17 +02002108 * efi_close_protocol() - close a protocol
2109 * @handle: handle on which the protocol shall be closed
2110 * @protocol: GUID of the protocol to close
2111 * @agent_handle: handle of the driver
2112 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002113 *
2114 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002115 *
Mario Six8fac2912018-07-10 08:40:17 +02002116 * See the Unified Extensible Firmware Interface (UEFI) specification for
2117 * details.
2118 *
2119 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002120 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002121efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2122 const efi_guid_t *protocol,
2123 efi_handle_t agent_handle,
2124 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002125{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002126 struct efi_handler *handler;
2127 struct efi_open_protocol_info_item *item;
2128 struct efi_open_protocol_info_item *pos;
2129 efi_status_t r;
2130
Rob Clark238f88c2017-09-13 18:05:41 -04002131 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002132 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002133
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002134 if (!efi_search_obj(agent_handle) ||
2135 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002136 r = EFI_INVALID_PARAMETER;
2137 goto out;
2138 }
2139 r = efi_search_protocol(handle, protocol, &handler);
2140 if (r != EFI_SUCCESS)
2141 goto out;
2142
2143 r = EFI_NOT_FOUND;
2144 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2145 if (item->info.agent_handle == agent_handle &&
2146 item->info.controller_handle == controller_handle) {
2147 efi_delete_open_info(item);
2148 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002149 }
2150 }
2151out:
2152 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002153}
2154
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002155/**
Mario Six8fac2912018-07-10 08:40:17 +02002156 * efi_open_protocol_information() - provide information about then open status
2157 * of a protocol on a handle
2158 * @handle: handle for which the information shall be retrieved
2159 * @protocol: GUID of the protocol
2160 * @entry_buffer: buffer to receive the open protocol information
2161 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002162 *
2163 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002164 *
2165 * See the Unified Extensible Firmware Interface (UEFI) specification for
2166 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002167 *
Mario Six8fac2912018-07-10 08:40:17 +02002168 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002169 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002170static efi_status_t EFIAPI efi_open_protocol_information(
2171 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002172 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002173 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002174{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002175 unsigned long buffer_size;
2176 unsigned long count;
2177 struct efi_handler *handler;
2178 struct efi_open_protocol_info_item *item;
2179 efi_status_t r;
2180
Rob Clark238f88c2017-09-13 18:05:41 -04002181 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002182 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002183
2184 /* Check parameters */
2185 if (!entry_buffer) {
2186 r = EFI_INVALID_PARAMETER;
2187 goto out;
2188 }
2189 r = efi_search_protocol(handle, protocol, &handler);
2190 if (r != EFI_SUCCESS)
2191 goto out;
2192
2193 /* Count entries */
2194 count = 0;
2195 list_for_each_entry(item, &handler->open_infos, link) {
2196 if (item->info.open_count)
2197 ++count;
2198 }
2199 *entry_count = count;
2200 *entry_buffer = NULL;
2201 if (!count) {
2202 r = EFI_SUCCESS;
2203 goto out;
2204 }
2205
2206 /* Copy entries */
2207 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002208 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002209 (void **)entry_buffer);
2210 if (r != EFI_SUCCESS)
2211 goto out;
2212 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2213 if (item->info.open_count)
2214 (*entry_buffer)[--count] = item->info;
2215 }
2216out:
2217 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002218}
2219
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002220/**
Mario Six8fac2912018-07-10 08:40:17 +02002221 * efi_protocols_per_handle() - get protocols installed on a handle
2222 * @handle: handle for which the information is retrieved
2223 * @protocol_buffer: buffer with protocol GUIDs
2224 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002225 *
2226 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002227 *
Mario Six8fac2912018-07-10 08:40:17 +02002228 * See the Unified Extensible Firmware Interface (UEFI) specification for
2229 * details.
2230 *
2231 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002232 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002233static efi_status_t EFIAPI efi_protocols_per_handle(
2234 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002235 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002236{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002237 unsigned long buffer_size;
2238 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002239 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002240 efi_status_t r;
2241
Alexander Grafc15d9212016-03-04 01:09:59 +01002242 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2243 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002244
2245 if (!handle || !protocol_buffer || !protocol_buffer_count)
2246 return EFI_EXIT(EFI_INVALID_PARAMETER);
2247
2248 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002249 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002250
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002251 efiobj = efi_search_obj(handle);
2252 if (!efiobj)
2253 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002254
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002255 /* Count protocols */
2256 list_for_each(protocol_handle, &efiobj->protocols) {
2257 ++*protocol_buffer_count;
2258 }
2259
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002260 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002261 if (*protocol_buffer_count) {
2262 size_t j = 0;
2263
2264 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002265 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002266 (void **)protocol_buffer);
2267 if (r != EFI_SUCCESS)
2268 return EFI_EXIT(r);
2269 list_for_each(protocol_handle, &efiobj->protocols) {
2270 struct efi_handler *protocol;
2271
2272 protocol = list_entry(protocol_handle,
2273 struct efi_handler, link);
2274 (*protocol_buffer)[j] = (void *)protocol->guid;
2275 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002276 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002277 }
2278
2279 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002280}
2281
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002282/**
Mario Six8fac2912018-07-10 08:40:17 +02002283 * efi_locate_handle_buffer() - locate handles implementing a protocol
2284 * @search_type: selection criterion
2285 * @protocol: GUID of the protocol
2286 * @search_key: registration key
2287 * @no_handles: number of returned handles
2288 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002289 *
2290 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002291 *
Mario Six8fac2912018-07-10 08:40:17 +02002292 * See the Unified Extensible Firmware Interface (UEFI) specification for
2293 * details.
2294 *
2295 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002296 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002297efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002298 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002299 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002300 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002301{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002302 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002303 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002304
Rob Clark238f88c2017-09-13 18:05:41 -04002305 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002306 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002307
2308 if (!no_handles || !buffer) {
2309 r = EFI_INVALID_PARAMETER;
2310 goto out;
2311 }
2312 *no_handles = 0;
2313 *buffer = NULL;
2314 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2315 *buffer);
2316 if (r != EFI_BUFFER_TOO_SMALL)
2317 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002318 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002319 (void **)buffer);
2320 if (r != EFI_SUCCESS)
2321 goto out;
2322 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2323 *buffer);
2324 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002325 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002326out:
2327 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002328}
2329
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002330/**
Mario Six8fac2912018-07-10 08:40:17 +02002331 * efi_locate_protocol() - find an interface implementing a protocol
2332 * @protocol: GUID of the protocol
2333 * @registration: registration key passed to the notification function
2334 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002335 *
2336 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002337 *
2338 * See the Unified Extensible Firmware Interface (UEFI) specification for
2339 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002340 *
Mario Six8fac2912018-07-10 08:40:17 +02002341 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002342 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002343static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002344 void *registration,
2345 void **protocol_interface)
2346{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002347 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002348 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002349 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002350
Rob Clark238f88c2017-09-13 18:05:41 -04002351 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002352
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002353 /*
2354 * The UEFI spec explicitly requires a protocol even if a registration
2355 * key is provided. This differs from the logic in LocateHandle().
2356 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002357 if (!protocol || !protocol_interface)
2358 return EFI_EXIT(EFI_INVALID_PARAMETER);
2359
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002360 if (registration) {
2361 struct efi_register_notify_event *event;
2362 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002363
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002364 event = efi_check_register_notify_event(registration);
2365 if (!event)
2366 return EFI_EXIT(EFI_INVALID_PARAMETER);
2367 /*
2368 * The UEFI spec requires to return EFI_NOT_FOUND if no
2369 * protocol instance matches protocol and registration.
2370 * So let's do the same for a mismatch between protocol and
2371 * registration.
2372 */
2373 if (guidcmp(&event->protocol, protocol))
2374 goto not_found;
2375 if (list_empty(&event->handles))
2376 goto not_found;
2377 handle = list_first_entry(&event->handles,
2378 struct efi_protocol_notification,
2379 link);
2380 efiobj = handle->handle;
2381 list_del(&handle->link);
2382 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002383 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002384 if (ret == EFI_SUCCESS)
2385 goto found;
2386 } else {
2387 list_for_each_entry(efiobj, &efi_obj_list, link) {
2388 ret = efi_search_protocol(efiobj, protocol, &handler);
2389 if (ret == EFI_SUCCESS)
2390 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002391 }
2392 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002393not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002394 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002395 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002396found:
2397 *protocol_interface = handler->protocol_interface;
2398 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002399}
2400
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002401/**
Mario Six8fac2912018-07-10 08:40:17 +02002402 * efi_locate_device_path() - Get the device path and handle of an device
2403 * implementing a protocol
2404 * @protocol: GUID of the protocol
2405 * @device_path: device path
2406 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002407 *
2408 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002409 *
Mario Six8fac2912018-07-10 08:40:17 +02002410 * See the Unified Extensible Firmware Interface (UEFI) specification for
2411 * details.
2412 *
2413 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002414 */
2415static efi_status_t EFIAPI efi_locate_device_path(
2416 const efi_guid_t *protocol,
2417 struct efi_device_path **device_path,
2418 efi_handle_t *device)
2419{
2420 struct efi_device_path *dp;
2421 size_t i;
2422 struct efi_handler *handler;
2423 efi_handle_t *handles;
2424 size_t len, len_dp;
2425 size_t len_best = 0;
2426 efi_uintn_t no_handles;
2427 u8 *remainder;
2428 efi_status_t ret;
2429
2430 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2431
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002432 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002433 ret = EFI_INVALID_PARAMETER;
2434 goto out;
2435 }
2436
2437 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002438 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002439
2440 /* Get all handles implementing the protocol */
2441 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2442 &no_handles, &handles));
2443 if (ret != EFI_SUCCESS)
2444 goto out;
2445
2446 for (i = 0; i < no_handles; ++i) {
2447 /* Find the device path protocol */
2448 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2449 &handler);
2450 if (ret != EFI_SUCCESS)
2451 continue;
2452 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002453 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002454 /*
2455 * This handle can only be a better fit
2456 * if its device path length is longer than the best fit and
2457 * if its device path length is shorter of equal the searched
2458 * device path.
2459 */
2460 if (len_dp <= len_best || len_dp > len)
2461 continue;
2462 /* Check if dp is a subpath of device_path */
2463 if (memcmp(*device_path, dp, len_dp))
2464 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002465 if (!device) {
2466 ret = EFI_INVALID_PARAMETER;
2467 goto out;
2468 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002469 *device = handles[i];
2470 len_best = len_dp;
2471 }
2472 if (len_best) {
2473 remainder = (u8 *)*device_path + len_best;
2474 *device_path = (struct efi_device_path *)remainder;
2475 ret = EFI_SUCCESS;
2476 } else {
2477 ret = EFI_NOT_FOUND;
2478 }
2479out:
2480 return EFI_EXIT(ret);
2481}
2482
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002483/**
Mario Six8fac2912018-07-10 08:40:17 +02002484 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2485 * interfaces
2486 * @handle: handle on which the protocol interfaces shall be installed
2487 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2488 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002489 *
2490 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002491 *
2492 * See the Unified Extensible Firmware Interface (UEFI) specification for
2493 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002494 *
Mario Six8fac2912018-07-10 08:40:17 +02002495 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002496 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002497efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002498 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002499{
2500 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002501
Alexander Graf404a7c82018-06-18 17:23:05 +02002502 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002503 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002504 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002505 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002506 efi_status_t r = EFI_SUCCESS;
2507 int i = 0;
2508
2509 if (!handle)
2510 return EFI_EXIT(EFI_INVALID_PARAMETER);
2511
Alexander Graf404a7c82018-06-18 17:23:05 +02002512 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002513 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002514 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002515 if (!protocol)
2516 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002517 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002518 /* Check that a device path has not been installed before */
2519 if (!guidcmp(protocol, &efi_guid_device_path)) {
2520 struct efi_device_path *dp = protocol_interface;
2521
2522 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2523 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002524 if (r == EFI_SUCCESS &&
2525 dp->type == DEVICE_PATH_TYPE_END) {
2526 EFI_PRINT("Path %pD already installed\n",
2527 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002528 r = EFI_ALREADY_STARTED;
2529 break;
2530 }
2531 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002532 r = EFI_CALL(efi_install_protocol_interface(
2533 handle, protocol,
2534 EFI_NATIVE_INTERFACE,
2535 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002536 if (r != EFI_SUCCESS)
2537 break;
2538 i++;
2539 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002540 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002541 if (r == EFI_SUCCESS)
2542 return EFI_EXIT(r);
2543
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002544 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002545 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002546 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002547 protocol = efi_va_arg(argptr, efi_guid_t*);
2548 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002549 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002550 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002551 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002552 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002553
2554 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002555}
2556
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002557/**
Mario Six8fac2912018-07-10 08:40:17 +02002558 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2559 * interfaces
2560 * @handle: handle from which the protocol interfaces shall be removed
2561 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2562 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002563 *
2564 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002565 *
Mario Six8fac2912018-07-10 08:40:17 +02002566 * See the Unified Extensible Firmware Interface (UEFI) specification for
2567 * details.
2568 *
2569 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002570 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002571static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002572 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002573{
2574 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002575
Alexander Graf404a7c82018-06-18 17:23:05 +02002576 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002577 const efi_guid_t *protocol;
2578 void *protocol_interface;
2579 efi_status_t r = EFI_SUCCESS;
2580 size_t i = 0;
2581
2582 if (!handle)
2583 return EFI_EXIT(EFI_INVALID_PARAMETER);
2584
Alexander Graf404a7c82018-06-18 17:23:05 +02002585 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002586 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002587 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002588 if (!protocol)
2589 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002590 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002591 r = efi_uninstall_protocol(handle, protocol,
2592 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002593 if (r != EFI_SUCCESS)
2594 break;
2595 i++;
2596 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002597 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002598 if (r == EFI_SUCCESS) {
2599 /* If the last protocol has been removed, delete the handle. */
2600 if (list_empty(&handle->protocols)) {
2601 list_del(&handle->link);
2602 free(handle);
2603 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002604 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002605 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002606
2607 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002608 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002609 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002610 protocol = efi_va_arg(argptr, efi_guid_t*);
2611 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002612 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2613 EFI_NATIVE_INTERFACE,
2614 protocol_interface));
2615 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002616 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002617
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002618 /* In case of an error always return EFI_INVALID_PARAMETER */
2619 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002620}
2621
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002622/**
Mario Six8fac2912018-07-10 08:40:17 +02002623 * efi_calculate_crc32() - calculate cyclic redundancy code
2624 * @data: buffer with data
2625 * @data_size: size of buffer in bytes
2626 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002627 *
2628 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002629 *
2630 * See the Unified Extensible Firmware Interface (UEFI) specification for
2631 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002632 *
Mario Six8fac2912018-07-10 08:40:17 +02002633 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002634 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002635static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2636 efi_uintn_t data_size,
2637 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002638{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002639 efi_status_t ret = EFI_SUCCESS;
2640
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002641 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002642 if (!data || !data_size || !crc32_p) {
2643 ret = EFI_INVALID_PARAMETER;
2644 goto out;
2645 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002646 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002647out:
2648 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002649}
2650
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002651/**
Mario Six8fac2912018-07-10 08:40:17 +02002652 * efi_copy_mem() - copy memory
2653 * @destination: destination of the copy operation
2654 * @source: source of the copy operation
2655 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002656 *
2657 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002658 *
Mario Six8fac2912018-07-10 08:40:17 +02002659 * See the Unified Extensible Firmware Interface (UEFI) specification for
2660 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002661 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002662static void EFIAPI efi_copy_mem(void *destination, const void *source,
2663 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002664{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002665 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002666 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002667 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002668}
2669
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002670/**
Mario Six8fac2912018-07-10 08:40:17 +02002671 * efi_set_mem() - Fill memory with a byte value.
2672 * @buffer: buffer to fill
2673 * @size: size of buffer in bytes
2674 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002675 *
2676 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002677 *
Mario Six8fac2912018-07-10 08:40:17 +02002678 * See the Unified Extensible Firmware Interface (UEFI) specification for
2679 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002680 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002681static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002682{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002683 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002684 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002685 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002686}
2687
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002688/**
Mario Six8fac2912018-07-10 08:40:17 +02002689 * efi_protocol_open() - open protocol interface on a handle
2690 * @handler: handler of a protocol
2691 * @protocol_interface: interface implementing the protocol
2692 * @agent_handle: handle of the driver
2693 * @controller_handle: handle of the controller
2694 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002695 *
Mario Six8fac2912018-07-10 08:40:17 +02002696 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002697 */
2698static efi_status_t efi_protocol_open(
2699 struct efi_handler *handler,
2700 void **protocol_interface, void *agent_handle,
2701 void *controller_handle, uint32_t attributes)
2702{
2703 struct efi_open_protocol_info_item *item;
2704 struct efi_open_protocol_info_entry *match = NULL;
2705 bool opened_by_driver = false;
2706 bool opened_exclusive = false;
2707
2708 /* If there is no agent, only return the interface */
2709 if (!agent_handle)
2710 goto out;
2711
2712 /* For TEST_PROTOCOL ignore interface attribute */
2713 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2714 *protocol_interface = NULL;
2715
2716 /*
2717 * Check if the protocol is already opened by a driver with the same
2718 * attributes or opened exclusively
2719 */
2720 list_for_each_entry(item, &handler->open_infos, link) {
2721 if (item->info.agent_handle == agent_handle) {
2722 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2723 (item->info.attributes == attributes))
2724 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002725 } else {
2726 if (item->info.attributes &
2727 EFI_OPEN_PROTOCOL_BY_DRIVER)
2728 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002729 }
2730 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2731 opened_exclusive = true;
2732 }
2733
2734 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002735 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2736 if (opened_exclusive)
2737 return EFI_ACCESS_DENIED;
2738 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2739 if (opened_exclusive || opened_by_driver)
2740 return EFI_ACCESS_DENIED;
2741 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002742
2743 /* Prepare exclusive opening */
2744 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2745 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002746disconnect_next:
2747 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002748 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002749 efi_status_t ret;
2750
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002751 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002752 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2753 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002754 item->info.controller_handle,
2755 item->info.agent_handle,
2756 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002757 if (ret == EFI_SUCCESS)
2758 /*
2759 * Child controllers may have been
2760 * removed from the open_infos list. So
2761 * let's restart the loop.
2762 */
2763 goto disconnect_next;
2764 else
2765 opened_by_driver = true;
2766 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002767 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002768 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002769 if (opened_by_driver)
2770 return EFI_ACCESS_DENIED;
2771 }
2772
2773 /* Find existing entry */
2774 list_for_each_entry(item, &handler->open_infos, link) {
2775 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002776 item->info.controller_handle == controller_handle &&
2777 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002778 match = &item->info;
2779 }
2780 /* None found, create one */
2781 if (!match) {
2782 match = efi_create_open_info(handler);
2783 if (!match)
2784 return EFI_OUT_OF_RESOURCES;
2785 }
2786
2787 match->agent_handle = agent_handle;
2788 match->controller_handle = controller_handle;
2789 match->attributes = attributes;
2790 match->open_count++;
2791
2792out:
2793 /* For TEST_PROTOCOL ignore interface attribute. */
2794 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2795 *protocol_interface = handler->protocol_interface;
2796
2797 return EFI_SUCCESS;
2798}
2799
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002800/**
Mario Six8fac2912018-07-10 08:40:17 +02002801 * efi_open_protocol() - open protocol interface on a handle
2802 * @handle: handle on which the protocol shall be opened
2803 * @protocol: GUID of the protocol
2804 * @protocol_interface: interface implementing the protocol
2805 * @agent_handle: handle of the driver
2806 * @controller_handle: handle of the controller
2807 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002808 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002809 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002810 *
Mario Six8fac2912018-07-10 08:40:17 +02002811 * See the Unified Extensible Firmware Interface (UEFI) specification for
2812 * details.
2813 *
2814 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002815 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002816static efi_status_t EFIAPI efi_open_protocol
2817 (efi_handle_t handle, const efi_guid_t *protocol,
2818 void **protocol_interface, efi_handle_t agent_handle,
2819 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002820{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002821 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002822 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002823
Rob Clark238f88c2017-09-13 18:05:41 -04002824 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002825 protocol_interface, agent_handle, controller_handle,
2826 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002827
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002828 if (!handle || !protocol ||
2829 (!protocol_interface && attributes !=
2830 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002831 goto out;
2832 }
2833
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002834 switch (attributes) {
2835 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2836 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2837 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2838 break;
2839 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2840 if (controller_handle == handle)
2841 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002842 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002843 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2844 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002845 /* Check that the controller handle is valid */
2846 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002847 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002848 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002849 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002850 /* Check that the agent handle is valid */
2851 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002852 goto out;
2853 break;
2854 default:
2855 goto out;
2856 }
2857
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002858 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002859 switch (r) {
2860 case EFI_SUCCESS:
2861 break;
2862 case EFI_NOT_FOUND:
2863 r = EFI_UNSUPPORTED;
2864 goto out;
2865 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002866 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002867 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002868
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002869 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2870 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002871out:
2872 return EFI_EXIT(r);
2873}
2874
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002875/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002876 * efi_start_image() - call the entry point of an image
2877 * @image_handle: handle of the image
2878 * @exit_data_size: size of the buffer
2879 * @exit_data: buffer to receive the exit data of the called image
2880 *
2881 * This function implements the StartImage service.
2882 *
2883 * See the Unified Extensible Firmware Interface (UEFI) specification for
2884 * details.
2885 *
2886 * Return: status code
2887 */
2888efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2889 efi_uintn_t *exit_data_size,
2890 u16 **exit_data)
2891{
2892 struct efi_loaded_image_obj *image_obj =
2893 (struct efi_loaded_image_obj *)image_handle;
2894 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002895 void *info;
2896 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002897
2898 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2899
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002900 if (!efi_search_obj(image_handle))
2901 return EFI_EXIT(EFI_INVALID_PARAMETER);
2902
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002903 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002904 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2905 return EFI_EXIT(EFI_INVALID_PARAMETER);
2906
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002907 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2908 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2909
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002910 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2911 &info, NULL, NULL,
2912 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2913 if (ret != EFI_SUCCESS)
2914 return EFI_EXIT(EFI_INVALID_PARAMETER);
2915
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002916 image_obj->exit_data_size = exit_data_size;
2917 image_obj->exit_data = exit_data;
2918
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002919 /* call the image! */
2920 if (setjmp(&image_obj->exit_jmp)) {
2921 /*
2922 * We called the entry point of the child image with EFI_CALL
2923 * in the lines below. The child image called the Exit() boot
2924 * service efi_exit() which executed the long jump that brought
2925 * us to the current line. This implies that the second half
2926 * of the EFI_CALL macro has not been executed.
2927 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02002928#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002929 /*
2930 * efi_exit() called efi_restore_gd(). We have to undo this
2931 * otherwise __efi_entry_check() will put the wrong value into
2932 * app_gd.
2933 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02002934 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002935#endif
2936 /*
2937 * To get ready to call EFI_EXIT below we have to execute the
2938 * missed out steps of EFI_CALL.
2939 */
2940 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002941 EFI_PRINT("%lu returned by started image\n",
2942 (unsigned long)((uintptr_t)image_obj->exit_status &
2943 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002944 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002945 return EFI_EXIT(image_obj->exit_status);
2946 }
2947
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002948 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002949 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002950 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002951 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2952
2953 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01002954 * Control is returned from a started UEFI image either by calling
2955 * Exit() (where exit data can be provided) or by simply returning from
2956 * the entry point. In the latter case call Exit() on behalf of the
2957 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002958 */
2959 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2960}
2961
2962/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002963 * efi_delete_image() - delete loaded image from memory)
2964 *
2965 * @image_obj: handle of the loaded image
2966 * @loaded_image_protocol: loaded image protocol
2967 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002968static efi_status_t efi_delete_image
2969 (struct efi_loaded_image_obj *image_obj,
2970 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002971{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002972 struct efi_object *efiobj;
2973 efi_status_t r, ret = EFI_SUCCESS;
2974
2975close_next:
2976 list_for_each_entry(efiobj, &efi_obj_list, link) {
2977 struct efi_handler *protocol;
2978
2979 list_for_each_entry(protocol, &efiobj->protocols, link) {
2980 struct efi_open_protocol_info_item *info;
2981
2982 list_for_each_entry(info, &protocol->open_infos, link) {
2983 if (info->info.agent_handle !=
2984 (efi_handle_t)image_obj)
2985 continue;
2986 r = EFI_CALL(efi_close_protocol
2987 (efiobj, protocol->guid,
2988 info->info.agent_handle,
2989 info->info.controller_handle
2990 ));
2991 if (r != EFI_SUCCESS)
2992 ret = r;
2993 /*
2994 * Closing protocols may results in further
2995 * items being deleted. To play it safe loop
2996 * over all elements again.
2997 */
2998 goto close_next;
2999 }
3000 }
3001 }
3002
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003003 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3004 efi_size_in_pages(loaded_image_protocol->image_size));
3005 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003006
3007 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003008}
3009
3010/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003011 * efi_unload_image() - unload an EFI image
3012 * @image_handle: handle of the image to be unloaded
3013 *
3014 * This function implements the UnloadImage service.
3015 *
3016 * See the Unified Extensible Firmware Interface (UEFI) specification for
3017 * details.
3018 *
3019 * Return: status code
3020 */
3021efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3022{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003023 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003024 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003025 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003026
3027 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003028
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003029 efiobj = efi_search_obj(image_handle);
3030 if (!efiobj) {
3031 ret = EFI_INVALID_PARAMETER;
3032 goto out;
3033 }
3034 /* Find the loaded image protocol */
3035 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3036 (void **)&loaded_image_protocol,
3037 NULL, NULL,
3038 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3039 if (ret != EFI_SUCCESS) {
3040 ret = EFI_INVALID_PARAMETER;
3041 goto out;
3042 }
3043 switch (efiobj->type) {
3044 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3045 /* Call the unload function */
3046 if (!loaded_image_protocol->unload) {
3047 ret = EFI_UNSUPPORTED;
3048 goto out;
3049 }
3050 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3051 if (ret != EFI_SUCCESS)
3052 goto out;
3053 break;
3054 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3055 break;
3056 default:
3057 ret = EFI_INVALID_PARAMETER;
3058 goto out;
3059 }
3060 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3061 loaded_image_protocol);
3062out:
3063 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003064}
3065
3066/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003067 * efi_update_exit_data() - fill exit data parameters of StartImage()
3068 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003069 * @image_obj: image handle
3070 * @exit_data_size: size of the exit data buffer
3071 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003072 * Return: status code
3073 */
3074static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3075 efi_uintn_t exit_data_size,
3076 u16 *exit_data)
3077{
3078 efi_status_t ret;
3079
3080 /*
3081 * If exit_data is not provided to StartImage(), exit_data_size must be
3082 * ignored.
3083 */
3084 if (!image_obj->exit_data)
3085 return EFI_SUCCESS;
3086 if (image_obj->exit_data_size)
3087 *image_obj->exit_data_size = exit_data_size;
3088 if (exit_data_size && exit_data) {
3089 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3090 exit_data_size,
3091 (void **)image_obj->exit_data);
3092 if (ret != EFI_SUCCESS)
3093 return ret;
3094 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3095 } else {
3096 image_obj->exit_data = NULL;
3097 }
3098 return EFI_SUCCESS;
3099}
3100
3101/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003102 * efi_exit() - leave an EFI application or driver
3103 * @image_handle: handle of the application or driver that is exiting
3104 * @exit_status: status code
3105 * @exit_data_size: size of the buffer in bytes
3106 * @exit_data: buffer with data describing an error
3107 *
3108 * This function implements the Exit service.
3109 *
3110 * See the Unified Extensible Firmware Interface (UEFI) specification for
3111 * details.
3112 *
3113 * Return: status code
3114 */
3115static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3116 efi_status_t exit_status,
3117 efi_uintn_t exit_data_size,
3118 u16 *exit_data)
3119{
3120 /*
3121 * TODO: We should call the unload procedure of the loaded
3122 * image protocol.
3123 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003124 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003125 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003126 struct efi_loaded_image_obj *image_obj =
3127 (struct efi_loaded_image_obj *)image_handle;
3128
3129 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3130 exit_data_size, exit_data);
3131
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003132 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003133 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003134 (void **)&loaded_image_protocol,
3135 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003136 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003137 if (ret != EFI_SUCCESS) {
3138 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003139 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003140 }
3141
3142 /* Unloading of unstarted images */
3143 switch (image_obj->header.type) {
3144 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3145 break;
3146 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3147 efi_delete_image(image_obj, loaded_image_protocol);
3148 ret = EFI_SUCCESS;
3149 goto out;
3150 default:
3151 /* Handle does not refer to loaded image */
3152 ret = EFI_INVALID_PARAMETER;
3153 goto out;
3154 }
3155 /* A started image can only be unloaded it is the last one started. */
3156 if (image_handle != current_image) {
3157 ret = EFI_INVALID_PARAMETER;
3158 goto out;
3159 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003160
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003161 /* Exit data is only foreseen in case of failure. */
3162 if (exit_status != EFI_SUCCESS) {
3163 ret = efi_update_exit_data(image_obj, exit_data_size,
3164 exit_data);
3165 /* Exiting has priority. Don't return error to caller. */
3166 if (ret != EFI_SUCCESS)
3167 EFI_PRINT("%s: out of memory\n", __func__);
3168 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003169 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3170 exit_status != EFI_SUCCESS)
3171 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003172
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003173 /* Make sure entry/exit counts for EFI world cross-overs match */
3174 EFI_EXIT(exit_status);
3175
3176 /*
3177 * But longjmp out with the U-Boot gd, not the application's, as
3178 * the other end is a setjmp call inside EFI context.
3179 */
3180 efi_restore_gd();
3181
3182 image_obj->exit_status = exit_status;
3183 longjmp(&image_obj->exit_jmp, 1);
3184
3185 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003186out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003187 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003188}
3189
3190/**
Mario Six8fac2912018-07-10 08:40:17 +02003191 * efi_handle_protocol() - get interface of a protocol on a handle
3192 * @handle: handle on which the protocol shall be opened
3193 * @protocol: GUID of the protocol
3194 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003195 *
3196 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003197 *
3198 * See the Unified Extensible Firmware Interface (UEFI) specification for
3199 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003200 *
Mario Six8fac2912018-07-10 08:40:17 +02003201 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003202 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003203efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3204 const efi_guid_t *protocol,
3205 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003206{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003207 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003208 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003209}
3210
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003211/**
Mario Six8fac2912018-07-10 08:40:17 +02003212 * efi_bind_controller() - bind a single driver to a controller
3213 * @controller_handle: controller handle
3214 * @driver_image_handle: driver handle
3215 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003216 *
Mario Six8fac2912018-07-10 08:40:17 +02003217 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003218 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003219static efi_status_t efi_bind_controller(
3220 efi_handle_t controller_handle,
3221 efi_handle_t driver_image_handle,
3222 struct efi_device_path *remain_device_path)
3223{
3224 struct efi_driver_binding_protocol *binding_protocol;
3225 efi_status_t r;
3226
3227 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3228 &efi_guid_driver_binding_protocol,
3229 (void **)&binding_protocol,
3230 driver_image_handle, NULL,
3231 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3232 if (r != EFI_SUCCESS)
3233 return r;
3234 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3235 controller_handle,
3236 remain_device_path));
3237 if (r == EFI_SUCCESS)
3238 r = EFI_CALL(binding_protocol->start(binding_protocol,
3239 controller_handle,
3240 remain_device_path));
3241 EFI_CALL(efi_close_protocol(driver_image_handle,
3242 &efi_guid_driver_binding_protocol,
3243 driver_image_handle, NULL));
3244 return r;
3245}
3246
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003247/**
Mario Six8fac2912018-07-10 08:40:17 +02003248 * efi_connect_single_controller() - connect a single driver to a controller
3249 * @controller_handle: controller
3250 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003251 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003252 *
Mario Six8fac2912018-07-10 08:40:17 +02003253 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003254 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003255static efi_status_t efi_connect_single_controller(
3256 efi_handle_t controller_handle,
3257 efi_handle_t *driver_image_handle,
3258 struct efi_device_path *remain_device_path)
3259{
3260 efi_handle_t *buffer;
3261 size_t count;
3262 size_t i;
3263 efi_status_t r;
3264 size_t connected = 0;
3265
3266 /* Get buffer with all handles with driver binding protocol */
3267 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3268 &efi_guid_driver_binding_protocol,
3269 NULL, &count, &buffer));
3270 if (r != EFI_SUCCESS)
3271 return r;
3272
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003273 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003274 if (driver_image_handle) {
3275 for (; *driver_image_handle; ++driver_image_handle) {
3276 for (i = 0; i < count; ++i) {
3277 if (buffer[i] == *driver_image_handle) {
3278 buffer[i] = NULL;
3279 r = efi_bind_controller(
3280 controller_handle,
3281 *driver_image_handle,
3282 remain_device_path);
3283 /*
3284 * For drivers that do not support the
3285 * controller or are already connected
3286 * we receive an error code here.
3287 */
3288 if (r == EFI_SUCCESS)
3289 ++connected;
3290 }
3291 }
3292 }
3293 }
3294
3295 /*
3296 * TODO: Some overrides are not yet implemented:
3297 * - Platform Driver Override
3298 * - Driver Family Override Search
3299 * - Bus Specific Driver Override
3300 */
3301
3302 /* Driver Binding Search */
3303 for (i = 0; i < count; ++i) {
3304 if (buffer[i]) {
3305 r = efi_bind_controller(controller_handle,
3306 buffer[i],
3307 remain_device_path);
3308 if (r == EFI_SUCCESS)
3309 ++connected;
3310 }
3311 }
3312
3313 efi_free_pool(buffer);
3314 if (!connected)
3315 return EFI_NOT_FOUND;
3316 return EFI_SUCCESS;
3317}
3318
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003319/**
Mario Six8fac2912018-07-10 08:40:17 +02003320 * efi_connect_controller() - connect a controller to a driver
3321 * @controller_handle: handle of the controller
3322 * @driver_image_handle: handle of the driver
3323 * @remain_device_path: device path of a child controller
3324 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003325 *
3326 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003327 *
3328 * See the Unified Extensible Firmware Interface (UEFI) specification for
3329 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003330 *
3331 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003332 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003333 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3334 *
Mario Six8fac2912018-07-10 08:40:17 +02003335 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003336 */
3337static efi_status_t EFIAPI efi_connect_controller(
3338 efi_handle_t controller_handle,
3339 efi_handle_t *driver_image_handle,
3340 struct efi_device_path *remain_device_path,
3341 bool recursive)
3342{
3343 efi_status_t r;
3344 efi_status_t ret = EFI_NOT_FOUND;
3345 struct efi_object *efiobj;
3346
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003347 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003348 remain_device_path, recursive);
3349
3350 efiobj = efi_search_obj(controller_handle);
3351 if (!efiobj) {
3352 ret = EFI_INVALID_PARAMETER;
3353 goto out;
3354 }
3355
3356 r = efi_connect_single_controller(controller_handle,
3357 driver_image_handle,
3358 remain_device_path);
3359 if (r == EFI_SUCCESS)
3360 ret = EFI_SUCCESS;
3361 if (recursive) {
3362 struct efi_handler *handler;
3363 struct efi_open_protocol_info_item *item;
3364
3365 list_for_each_entry(handler, &efiobj->protocols, link) {
3366 list_for_each_entry(item, &handler->open_infos, link) {
3367 if (item->info.attributes &
3368 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3369 r = EFI_CALL(efi_connect_controller(
3370 item->info.controller_handle,
3371 driver_image_handle,
3372 remain_device_path,
3373 recursive));
3374 if (r == EFI_SUCCESS)
3375 ret = EFI_SUCCESS;
3376 }
3377 }
3378 }
3379 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003380 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003381 if (ret != EFI_SUCCESS && remain_device_path &&
3382 remain_device_path->type == DEVICE_PATH_TYPE_END)
3383 ret = EFI_SUCCESS;
3384out:
3385 return EFI_EXIT(ret);
3386}
3387
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003388/**
Mario Six8fac2912018-07-10 08:40:17 +02003389 * efi_reinstall_protocol_interface() - reinstall protocol interface
3390 * @handle: handle on which the protocol shall be reinstalled
3391 * @protocol: GUID of the protocol to be installed
3392 * @old_interface: interface to be removed
3393 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003394 *
3395 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003396 *
3397 * See the Unified Extensible Firmware Interface (UEFI) specification for
3398 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003399 *
3400 * The old interface is uninstalled. The new interface is installed.
3401 * Drivers are connected.
3402 *
Mario Six8fac2912018-07-10 08:40:17 +02003403 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003404 */
3405static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3406 efi_handle_t handle, const efi_guid_t *protocol,
3407 void *old_interface, void *new_interface)
3408{
3409 efi_status_t ret;
3410
3411 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3412 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003413
3414 /* Uninstall protocol but do not delete handle */
3415 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003416 if (ret != EFI_SUCCESS)
3417 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003418
3419 /* Install the new protocol */
3420 ret = efi_add_protocol(handle, protocol, new_interface);
3421 /*
3422 * The UEFI spec does not specify what should happen to the handle
3423 * if in case of an error no protocol interface remains on the handle.
3424 * So let's do nothing here.
3425 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003426 if (ret != EFI_SUCCESS)
3427 goto out;
3428 /*
3429 * The returned status code has to be ignored.
3430 * Do not create an error if no suitable driver for the handle exists.
3431 */
3432 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3433out:
3434 return EFI_EXIT(ret);
3435}
3436
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003437/**
Mario Six8fac2912018-07-10 08:40:17 +02003438 * efi_get_child_controllers() - get all child controllers associated to a driver
3439 * @efiobj: handle of the controller
3440 * @driver_handle: handle of the driver
3441 * @number_of_children: number of child controllers
3442 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003443 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003444 * The allocated buffer has to be freed with free().
3445 *
Mario Six8fac2912018-07-10 08:40:17 +02003446 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003447 */
3448static efi_status_t efi_get_child_controllers(
3449 struct efi_object *efiobj,
3450 efi_handle_t driver_handle,
3451 efi_uintn_t *number_of_children,
3452 efi_handle_t **child_handle_buffer)
3453{
3454 struct efi_handler *handler;
3455 struct efi_open_protocol_info_item *item;
3456 efi_uintn_t count = 0, i;
3457 bool duplicate;
3458
3459 /* Count all child controller associations */
3460 list_for_each_entry(handler, &efiobj->protocols, link) {
3461 list_for_each_entry(item, &handler->open_infos, link) {
3462 if (item->info.agent_handle == driver_handle &&
3463 item->info.attributes &
3464 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3465 ++count;
3466 }
3467 }
3468 /*
3469 * Create buffer. In case of duplicate child controller assignments
3470 * the buffer will be too large. But that does not harm.
3471 */
3472 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003473 if (!count)
3474 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003475 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3476 if (!*child_handle_buffer)
3477 return EFI_OUT_OF_RESOURCES;
3478 /* Copy unique child handles */
3479 list_for_each_entry(handler, &efiobj->protocols, link) {
3480 list_for_each_entry(item, &handler->open_infos, link) {
3481 if (item->info.agent_handle == driver_handle &&
3482 item->info.attributes &
3483 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3484 /* Check this is a new child controller */
3485 duplicate = false;
3486 for (i = 0; i < *number_of_children; ++i) {
3487 if ((*child_handle_buffer)[i] ==
3488 item->info.controller_handle)
3489 duplicate = true;
3490 }
3491 /* Copy handle to buffer */
3492 if (!duplicate) {
3493 i = (*number_of_children)++;
3494 (*child_handle_buffer)[i] =
3495 item->info.controller_handle;
3496 }
3497 }
3498 }
3499 }
3500 return EFI_SUCCESS;
3501}
3502
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003503/**
Mario Six8fac2912018-07-10 08:40:17 +02003504 * efi_disconnect_controller() - disconnect a controller from a driver
3505 * @controller_handle: handle of the controller
3506 * @driver_image_handle: handle of the driver
3507 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003508 *
3509 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003510 *
3511 * See the Unified Extensible Firmware Interface (UEFI) specification for
3512 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003513 *
Mario Six8fac2912018-07-10 08:40:17 +02003514 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003515 */
3516static efi_status_t EFIAPI efi_disconnect_controller(
3517 efi_handle_t controller_handle,
3518 efi_handle_t driver_image_handle,
3519 efi_handle_t child_handle)
3520{
3521 struct efi_driver_binding_protocol *binding_protocol;
3522 efi_handle_t *child_handle_buffer = NULL;
3523 size_t number_of_children = 0;
3524 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003525 struct efi_object *efiobj;
3526
3527 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3528 child_handle);
3529
3530 efiobj = efi_search_obj(controller_handle);
3531 if (!efiobj) {
3532 r = EFI_INVALID_PARAMETER;
3533 goto out;
3534 }
3535
3536 if (child_handle && !efi_search_obj(child_handle)) {
3537 r = EFI_INVALID_PARAMETER;
3538 goto out;
3539 }
3540
3541 /* If no driver handle is supplied, disconnect all drivers */
3542 if (!driver_image_handle) {
3543 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3544 goto out;
3545 }
3546
3547 /* Create list of child handles */
3548 if (child_handle) {
3549 number_of_children = 1;
3550 child_handle_buffer = &child_handle;
3551 } else {
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003552 r = efi_get_child_controllers(efiobj,
3553 driver_image_handle,
3554 &number_of_children,
3555 &child_handle_buffer);
3556 if (r != EFI_SUCCESS)
3557 return r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003558 }
3559
3560 /* Get the driver binding protocol */
3561 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3562 &efi_guid_driver_binding_protocol,
3563 (void **)&binding_protocol,
3564 driver_image_handle, NULL,
3565 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003566 if (r != EFI_SUCCESS) {
3567 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003568 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003569 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003570 /* Remove the children */
3571 if (number_of_children) {
3572 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3573 controller_handle,
3574 number_of_children,
3575 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003576 if (r != EFI_SUCCESS) {
3577 r = EFI_DEVICE_ERROR;
3578 goto out;
3579 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003580 }
3581 /* Remove the driver */
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003582 if (!child_handle) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003583 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3584 controller_handle,
3585 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003586 if (r != EFI_SUCCESS) {
3587 r = EFI_DEVICE_ERROR;
3588 goto out;
3589 }
3590 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003591 EFI_CALL(efi_close_protocol(driver_image_handle,
3592 &efi_guid_driver_binding_protocol,
3593 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003594 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003595out:
3596 if (!child_handle)
3597 free(child_handle_buffer);
3598 return EFI_EXIT(r);
3599}
3600
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003601static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003602 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003603 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3604 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003605 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003606 },
3607 .raise_tpl = efi_raise_tpl,
3608 .restore_tpl = efi_restore_tpl,
3609 .allocate_pages = efi_allocate_pages_ext,
3610 .free_pages = efi_free_pages_ext,
3611 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003612 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003613 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003614 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003615 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003616 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003617 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003618 .close_event = efi_close_event,
3619 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003620 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003621 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003622 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003623 .handle_protocol = efi_handle_protocol,
3624 .reserved = NULL,
3625 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003626 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003627 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003628 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003629 .load_image = efi_load_image,
3630 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003631 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003632 .unload_image = efi_unload_image,
3633 .exit_boot_services = efi_exit_boot_services,
3634 .get_next_monotonic_count = efi_get_next_monotonic_count,
3635 .stall = efi_stall,
3636 .set_watchdog_timer = efi_set_watchdog_timer,
3637 .connect_controller = efi_connect_controller,
3638 .disconnect_controller = efi_disconnect_controller,
3639 .open_protocol = efi_open_protocol,
3640 .close_protocol = efi_close_protocol,
3641 .open_protocol_information = efi_open_protocol_information,
3642 .protocols_per_handle = efi_protocols_per_handle,
3643 .locate_handle_buffer = efi_locate_handle_buffer,
3644 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003645 .install_multiple_protocol_interfaces =
3646 efi_install_multiple_protocol_interfaces,
3647 .uninstall_multiple_protocol_interfaces =
3648 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003649 .calculate_crc32 = efi_calculate_crc32,
3650 .copy_mem = efi_copy_mem,
3651 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003652 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003653};
3654
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003655static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003656
Alexander Graf393dd912016-10-14 13:45:30 +02003657struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003658 .hdr = {
3659 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003660 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003661 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003662 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003663 .fw_vendor = firmware_vendor,
3664 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003665 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003666 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003667 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003668};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003669
3670/**
3671 * efi_initialize_system_table() - Initialize system table
3672 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003673 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003674 */
3675efi_status_t efi_initialize_system_table(void)
3676{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003677 efi_status_t ret;
3678
3679 /* Allocate configuration table array */
3680 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3681 EFI_MAX_CONFIGURATION_TABLES *
3682 sizeof(struct efi_configuration_table),
3683 (void **)&systab.tables);
3684
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003685 /*
3686 * These entries will be set to NULL in ExitBootServices(). To avoid
3687 * relocation in SetVirtualAddressMap(), set them dynamically.
3688 */
3689 systab.con_in = &efi_con_in;
3690 systab.con_out = &efi_con_out;
3691 systab.std_err = &efi_con_out;
3692 systab.boottime = &efi_boot_services;
3693
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003694 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003695 efi_update_table_header_crc32(&systab.hdr);
3696 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3697 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003698
3699 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003700}