blob: caaab685ee0c379dbb579bad904d6c5210d524a0 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020030
Alexander Grafc15d9212016-03-04 01:09:59 +010031/* This list contains all the EFI objects our payload has access to */
32LIST_HEAD(efi_obj_list);
33
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010036
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020037/* List of queued events */
38LIST_HEAD(efi_event_queue);
39
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
47LIST_HEAD(efi_register_notify_events);
48
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010053/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010055 * fixed when compiling U-Boot. However, the payload does not know about that
56 * restriction so we need to manually swap its and our view of that register on
57 * EFI callback entry/exit.
58 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060060#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010061
Heinrich Schuchardt72506232019-02-09 14:10:39 +010062/* 1 if inside U-Boot code, 0 if inside EFI payload code */
63static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040064static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010065/* GUID of the device tree table */
66const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010067/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
68const efi_guid_t efi_guid_driver_binding_protocol =
69 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040070
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010071/* event group ExitBootServices() invoked */
72const efi_guid_t efi_guid_event_group_exit_boot_services =
73 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt44772c42021-11-16 18:46:27 +010074/* event group before ExitBootServices() invoked */
75const efi_guid_t efi_guid_event_group_before_exit_boot_services =
76 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010077/* event group SetVirtualAddressMap() invoked */
78const efi_guid_t efi_guid_event_group_virtual_address_change =
79 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
80/* event group memory map changed */
81const efi_guid_t efi_guid_event_group_memory_map_change =
82 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
83/* event group boot manager about to boot */
84const efi_guid_t efi_guid_event_group_ready_to_boot =
85 EFI_EVENT_GROUP_READY_TO_BOOT;
86/* event group ResetSystem() invoked (before ExitBootServices) */
87const efi_guid_t efi_guid_event_group_reset_system =
88 EFI_EVENT_GROUP_RESET_SYSTEM;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010089/* GUIDs of the Load File and Load File2 protocols */
90const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
91const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090092/* GUID of the SMBIOS table */
93const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010094
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010095static efi_status_t EFIAPI efi_disconnect_controller(
96 efi_handle_t controller_handle,
97 efi_handle_t driver_image_handle,
98 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010099
Rob Clark86789d52017-07-27 08:04:18 -0400100/* Called on every callback entry */
101int __efi_entry_check(void)
102{
103 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200104#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400105 assert(efi_gd);
106 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200107 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400108#endif
109 return ret;
110}
111
112/* Called on every callback exit */
113int __efi_exit_check(void)
114{
115 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200116#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200117 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400118#endif
119 return ret;
120}
121
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200122/**
123 * efi_save_gd() - save global data register
124 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200125 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200126 * As this register may be overwritten by an EFI payload we save it here
127 * and restore it on every callback entered.
128 *
129 * This function is called after relocation from initr_reloc_global_data().
130 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100131void efi_save_gd(void)
132{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200133#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100134 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600135#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100136}
137
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200138/**
139 * efi_restore_gd() - restore global data register
140 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200141 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200142 * Restore it after returning from the UEFI world to the value saved via
143 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400144 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100145void efi_restore_gd(void)
146{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200147#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100148 /* Only restore if we're already in EFI context */
149 if (!efi_gd)
150 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200151 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600152#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100153}
154
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200155/**
Mario Six8fac2912018-07-10 08:40:17 +0200156 * indent_string() - returns a string for indenting with two spaces per level
157 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100158 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200159 * A maximum of ten indent levels is supported. Higher indent levels will be
160 * truncated.
161 *
Mario Six8fac2912018-07-10 08:40:17 +0200162 * Return: A string for indenting with two spaces per level is
163 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400164 */
165static const char *indent_string(int level)
166{
167 const char *indent = " ";
168 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100169
Rob Clarke7896c32017-07-27 08:04:19 -0400170 level = min(max, level * 2);
171 return &indent[max - level];
172}
173
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200174const char *__efi_nesting(void)
175{
176 return indent_string(nesting_level);
177}
178
Rob Clarke7896c32017-07-27 08:04:19 -0400179const char *__efi_nesting_inc(void)
180{
181 return indent_string(nesting_level++);
182}
183
184const char *__efi_nesting_dec(void)
185{
186 return indent_string(--nesting_level);
187}
188
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200189/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200190 * efi_event_is_queued() - check if an event is queued
191 *
192 * @event: event
193 * Return: true if event is queued
194 */
195static bool efi_event_is_queued(struct efi_event *event)
196{
197 return !!event->queue_link.next;
198}
199
200/**
201 * efi_process_event_queue() - process event queue
202 */
203static void efi_process_event_queue(void)
204{
205 while (!list_empty(&efi_event_queue)) {
206 struct efi_event *event;
207 efi_uintn_t old_tpl;
208
209 event = list_first_entry(&efi_event_queue, struct efi_event,
210 queue_link);
211 if (efi_tpl >= event->notify_tpl)
212 return;
213 list_del(&event->queue_link);
214 event->queue_link.next = NULL;
215 event->queue_link.prev = NULL;
216 /* Events must be executed at the event's TPL */
217 old_tpl = efi_tpl;
218 efi_tpl = event->notify_tpl;
219 EFI_CALL_VOID(event->notify_function(event,
220 event->notify_context));
221 efi_tpl = old_tpl;
222 if (event->type == EVT_NOTIFY_SIGNAL)
223 event->is_signaled = 0;
224 }
225}
226
227/**
Mario Six8fac2912018-07-10 08:40:17 +0200228 * efi_queue_event() - queue an EFI event
229 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200230 *
231 * This function queues the notification function of the event for future
232 * execution.
233 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200234 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200235static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200236{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100237 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200238
239 if (!event->notify_function)
240 return;
241
242 if (!efi_event_is_queued(event)) {
243 /*
244 * Events must be notified in order of decreasing task priority
245 * level. Insert the new event accordingly.
246 */
247 list_for_each_entry(item, &efi_event_queue, queue_link) {
248 if (item->notify_tpl < event->notify_tpl) {
249 list_add_tail(&event->queue_link,
250 &item->queue_link);
251 event = NULL;
252 break;
253 }
254 }
255 if (event)
256 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100257 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200258 }
259}
260
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200261/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200262 * is_valid_tpl() - check if the task priority level is valid
263 *
264 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200265 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200266 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100267static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200268{
269 switch (tpl) {
270 case TPL_APPLICATION:
271 case TPL_CALLBACK:
272 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200273 return EFI_SUCCESS;
274 default:
275 return EFI_INVALID_PARAMETER;
276 }
277}
278
279/**
Mario Six8fac2912018-07-10 08:40:17 +0200280 * efi_signal_event() - signal an EFI event
281 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100282 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100283 * This function signals an event. If the event belongs to an event group, all
284 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100285 * their notification function is queued.
286 *
287 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100288 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200289void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100290{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200291 if (event->is_signaled)
292 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100293 if (event->group) {
294 struct efi_event *evt;
295
296 /*
297 * The signaled state has to set before executing any
298 * notification function
299 */
300 list_for_each_entry(evt, &efi_events, link) {
301 if (!evt->group || guidcmp(evt->group, event->group))
302 continue;
303 if (evt->is_signaled)
304 continue;
305 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100306 }
307 list_for_each_entry(evt, &efi_events, link) {
308 if (!evt->group || guidcmp(evt->group, event->group))
309 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200310 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100311 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200312 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100313 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200314 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100315 }
316}
317
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200318/**
Mario Six8fac2912018-07-10 08:40:17 +0200319 * efi_raise_tpl() - raise the task priority level
320 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200321 *
322 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200323 *
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200326 *
Mario Six8fac2912018-07-10 08:40:17 +0200327 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200328 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100329static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100330{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100331 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200332
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200333 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200334
335 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200336 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200337 efi_tpl = new_tpl;
338 if (efi_tpl > TPL_HIGH_LEVEL)
339 efi_tpl = TPL_HIGH_LEVEL;
340
341 EFI_EXIT(EFI_SUCCESS);
342 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100343}
344
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200345/**
Mario Six8fac2912018-07-10 08:40:17 +0200346 * efi_restore_tpl() - lower the task priority level
347 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200348 *
349 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200350 *
Mario Six8fac2912018-07-10 08:40:17 +0200351 * See the Unified Extensible Firmware Interface (UEFI) specification for
352 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200353 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100354static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100355{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200356 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200357
358 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200359 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200360 efi_tpl = old_tpl;
361 if (efi_tpl > TPL_HIGH_LEVEL)
362 efi_tpl = TPL_HIGH_LEVEL;
363
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100364 /*
365 * Lowering the TPL may have made queued events eligible for execution.
366 */
367 efi_timer_check();
368
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200369 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100370}
371
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200372/**
Mario Six8fac2912018-07-10 08:40:17 +0200373 * efi_allocate_pages_ext() - allocate memory pages
374 * @type: type of allocation to be performed
375 * @memory_type: usage type of the allocated memory
376 * @pages: number of pages to be allocated
377 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200378 *
379 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200380 *
Mario Six8fac2912018-07-10 08:40:17 +0200381 * See the Unified Extensible Firmware Interface (UEFI) specification for
382 * details.
383 *
384 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200385 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900386static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100387 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900388 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100389{
390 efi_status_t r;
391
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100392 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100393 r = efi_allocate_pages(type, memory_type, pages, memory);
394 return EFI_EXIT(r);
395}
396
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200397/**
Mario Six8fac2912018-07-10 08:40:17 +0200398 * efi_free_pages_ext() - Free memory pages.
399 * @memory: start of the memory area to be freed
400 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200401 *
402 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200403 *
404 * See the Unified Extensible Firmware Interface (UEFI) specification for
405 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200406 *
Mario Six8fac2912018-07-10 08:40:17 +0200407 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200408 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900409static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100410 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100411{
412 efi_status_t r;
413
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900414 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100415 r = efi_free_pages(memory, pages);
416 return EFI_EXIT(r);
417}
418
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200419/**
Mario Six8fac2912018-07-10 08:40:17 +0200420 * efi_get_memory_map_ext() - get map describing memory usage
421 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
422 * on exit the size of the copied memory map
423 * @memory_map: buffer to which the memory map is written
424 * @map_key: key for the memory map
425 * @descriptor_size: size of an individual memory descriptor
426 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200427 *
428 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200429 *
Mario Six8fac2912018-07-10 08:40:17 +0200430 * See the Unified Extensible Firmware Interface (UEFI) specification for
431 * details.
432 *
433 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200434 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900435static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100436 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900437 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100438 efi_uintn_t *map_key,
439 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900440 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100441{
442 efi_status_t r;
443
444 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
445 map_key, descriptor_size, descriptor_version);
446 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
447 descriptor_size, descriptor_version);
448 return EFI_EXIT(r);
449}
450
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200451/**
Mario Six8fac2912018-07-10 08:40:17 +0200452 * efi_allocate_pool_ext() - allocate memory from pool
453 * @pool_type: type of the pool from which memory is to be allocated
454 * @size: number of bytes to be allocated
455 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200456 *
457 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200458 *
459 * See the Unified Extensible Firmware Interface (UEFI) specification for
460 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200461 *
Mario Six8fac2912018-07-10 08:40:17 +0200462 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200463 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200464static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100465 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200466 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100467{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100468 efi_status_t r;
469
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100470 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200471 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100472 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100473}
474
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200475/**
Mario Six8fac2912018-07-10 08:40:17 +0200476 * efi_free_pool_ext() - free memory from pool
477 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200478 *
479 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200480 *
Mario Six8fac2912018-07-10 08:40:17 +0200481 * See the Unified Extensible Firmware Interface (UEFI) specification for
482 * details.
483 *
484 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200485 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200486static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100487{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100488 efi_status_t r;
489
490 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200491 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100492 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100493}
494
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200495/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200496 * efi_add_handle() - add a new handle to the object list
497 *
498 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100499 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200500 * The protocols list is initialized. The handle is added to the list of known
501 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100502 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200503void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100504{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200505 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100506 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200507 INIT_LIST_HEAD(&handle->protocols);
508 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100509}
510
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200511/**
Mario Six8fac2912018-07-10 08:40:17 +0200512 * efi_create_handle() - create handle
513 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200514 *
Mario Six8fac2912018-07-10 08:40:17 +0200515 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200516 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100517efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200518{
519 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200520
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200521 obj = calloc(1, sizeof(struct efi_object));
522 if (!obj)
523 return EFI_OUT_OF_RESOURCES;
524
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100525 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200526 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200527
528 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200529}
530
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200531/**
Mario Six8fac2912018-07-10 08:40:17 +0200532 * efi_search_protocol() - find a protocol on a handle.
533 * @handle: handle
534 * @protocol_guid: GUID of the protocol
535 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100536 *
Mario Six8fac2912018-07-10 08:40:17 +0200537 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100538 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100539efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100540 const efi_guid_t *protocol_guid,
541 struct efi_handler **handler)
542{
543 struct efi_object *efiobj;
544 struct list_head *lhandle;
545
546 if (!handle || !protocol_guid)
547 return EFI_INVALID_PARAMETER;
548 efiobj = efi_search_obj(handle);
549 if (!efiobj)
550 return EFI_INVALID_PARAMETER;
551 list_for_each(lhandle, &efiobj->protocols) {
552 struct efi_handler *protocol;
553
554 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100555 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100556 if (handler)
557 *handler = protocol;
558 return EFI_SUCCESS;
559 }
560 }
561 return EFI_NOT_FOUND;
562}
563
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200564/**
Mario Six8fac2912018-07-10 08:40:17 +0200565 * efi_remove_protocol() - delete protocol from a handle
566 * @handle: handle from which the protocol shall be deleted
567 * @protocol: GUID of the protocol to be deleted
568 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100569 *
Mario Six8fac2912018-07-10 08:40:17 +0200570 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100571 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100572efi_status_t efi_remove_protocol(const efi_handle_t handle,
573 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100574 void *protocol_interface)
575{
576 struct efi_handler *handler;
577 efi_status_t ret;
578
579 ret = efi_search_protocol(handle, protocol, &handler);
580 if (ret != EFI_SUCCESS)
581 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200582 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200583 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100584 list_del(&handler->link);
585 free(handler);
586 return EFI_SUCCESS;
587}
588
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200589/**
Mario Six8fac2912018-07-10 08:40:17 +0200590 * efi_remove_all_protocols() - delete all protocols from a handle
591 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100592 *
Mario Six8fac2912018-07-10 08:40:17 +0200593 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100595static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100596{
597 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100598 struct efi_handler *protocol;
599 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600
601 efiobj = efi_search_obj(handle);
602 if (!efiobj)
603 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100604 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100605 efi_status_t ret;
606
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100607 ret = efi_remove_protocol(handle, &protocol->guid,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100608 protocol->protocol_interface);
609 if (ret != EFI_SUCCESS)
610 return ret;
611 }
612 return EFI_SUCCESS;
613}
614
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200615/**
Mario Six8fac2912018-07-10 08:40:17 +0200616 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100617 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200618 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100619 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200620void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100621{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200622 efi_status_t ret;
623
624 ret = efi_remove_all_protocols(handle);
625 if (ret == EFI_INVALID_PARAMETER) {
626 log_err("Can't remove invalid handle %p\n", handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627 return;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200628 }
629
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200630 list_del(&handle->link);
631 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100632}
633
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200634/**
Mario Six8fac2912018-07-10 08:40:17 +0200635 * efi_is_event() - check if a pointer is a valid event
636 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100637 *
Mario Six8fac2912018-07-10 08:40:17 +0200638 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100639 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100640static efi_status_t efi_is_event(const struct efi_event *event)
641{
642 const struct efi_event *evt;
643
644 if (!event)
645 return EFI_INVALID_PARAMETER;
646 list_for_each_entry(evt, &efi_events, link) {
647 if (evt == event)
648 return EFI_SUCCESS;
649 }
650 return EFI_INVALID_PARAMETER;
651}
Alexander Grafc15d9212016-03-04 01:09:59 +0100652
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200653/**
Mario Six8fac2912018-07-10 08:40:17 +0200654 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200655 *
Mario Six8fac2912018-07-10 08:40:17 +0200656 * @type: type of the event to create
657 * @notify_tpl: task priority level of the event
658 * @notify_function: notification function of the event
659 * @notify_context: pointer passed to the notification function
660 * @group: event group
661 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200662 *
663 * This function is used inside U-Boot code to create an event.
664 *
665 * For the API function implementing the CreateEvent service see
666 * efi_create_event_ext.
667 *
Mario Six8fac2912018-07-10 08:40:17 +0200668 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200669 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100670efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200671 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200672 struct efi_event *event,
673 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100674 void *notify_context, efi_guid_t *group,
675 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100676{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100677 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200678 efi_status_t ret;
679 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200680
Jonathan Gray7758b212017-03-12 19:26:07 +1100681 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200682 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100683
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200684 switch (type) {
685 case 0:
686 case EVT_TIMER:
687 case EVT_NOTIFY_SIGNAL:
688 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
689 case EVT_NOTIFY_WAIT:
690 case EVT_TIMER | EVT_NOTIFY_WAIT:
691 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200692 pool_type = EFI_BOOT_SERVICES_DATA;
693 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200694 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200695 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200696 break;
697 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200698 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200699 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100700
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100701 /*
702 * The UEFI specification requires event notification levels to be
703 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
704 *
705 * Parameter NotifyTpl should not be checked if it is not used.
706 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900707 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100708 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
709 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200710 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100711
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200712 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
713 (void **)&evt);
714 if (ret != EFI_SUCCESS)
715 return ret;
716 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100717 evt->type = type;
718 evt->notify_tpl = notify_tpl;
719 evt->notify_function = notify_function;
720 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100721 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200722 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100723 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100724 list_add_tail(&evt->link, &efi_events);
725 *event = evt;
726 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100727}
728
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200729/*
Mario Six8fac2912018-07-10 08:40:17 +0200730 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100731 *
Mario Six8fac2912018-07-10 08:40:17 +0200732 * @type: type of the event to create
733 * @notify_tpl: task priority level of the event
734 * @notify_function: notification function of the event
735 * @notify_context: pointer passed to the notification function
736 * @event: created event
737 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100738 *
739 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100740 *
Mario Six8fac2912018-07-10 08:40:17 +0200741 * See the Unified Extensible Firmware Interface (UEFI) specification for
742 * details.
743 *
744 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100745 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100746static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100747efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
748 void (EFIAPI *notify_function) (
749 struct efi_event *event,
750 void *context),
751 void *notify_context,
752 efi_guid_t *event_group,
753 struct efi_event **event)
754{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200755 efi_status_t ret;
756
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100757 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100758 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200759
760 /*
761 * The allowable input parameters are the same as in CreateEvent()
762 * except for the following two disallowed event types.
763 */
764 switch (type) {
765 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
766 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
767 ret = EFI_INVALID_PARAMETER;
768 goto out;
769 }
770
771 ret = efi_create_event(type, notify_tpl, notify_function,
772 notify_context, event_group, event);
773out:
774 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100775}
776
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200777/**
Mario Six8fac2912018-07-10 08:40:17 +0200778 * efi_create_event_ext() - create an event
779 * @type: type of the event to create
780 * @notify_tpl: task priority level of the event
781 * @notify_function: notification function of the event
782 * @notify_context: pointer passed to the notification function
783 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200784 *
785 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200786 *
787 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200789 *
Mario Six8fac2912018-07-10 08:40:17 +0200790 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200791 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200792static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100793 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200794 void (EFIAPI *notify_function) (
795 struct efi_event *event,
796 void *context),
797 void *notify_context, struct efi_event **event)
798{
799 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
800 notify_context);
801 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100802 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200803}
804
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200805/**
Mario Six8fac2912018-07-10 08:40:17 +0200806 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200807 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200808 * Check if a timer event has occurred or a queued notification function should
809 * be called.
810 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100811 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200812 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100813 */
814void efi_timer_check(void)
815{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100816 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100817 u64 now = timer_get_us();
818
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100819 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200820 if (!timers_enabled)
821 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100822 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200823 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100824 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200825 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100826 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200827 break;
828 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100829 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200830 break;
831 default:
832 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200833 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100834 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200835 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100836 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200837 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200838 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100839}
840
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200841/**
Mario Six8fac2912018-07-10 08:40:17 +0200842 * efi_set_timer() - set the trigger time for a timer event or stop the event
843 * @event: event for which the timer is set
844 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200845 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200846 *
847 * This is the function for internal usage in U-Boot. For the API function
848 * implementing the SetTimer service see efi_set_timer_ext.
849 *
Mario Six8fac2912018-07-10 08:40:17 +0200850 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200851 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200852efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200853 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100854{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100855 /* Check that the event is valid */
856 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
857 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100858
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200859 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200860 * The parameter defines a multiple of 100 ns.
861 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200862 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200863 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100864
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100865 switch (type) {
866 case EFI_TIMER_STOP:
867 event->trigger_next = -1ULL;
868 break;
869 case EFI_TIMER_PERIODIC:
870 case EFI_TIMER_RELATIVE:
871 event->trigger_next = timer_get_us() + trigger_time;
872 break;
873 default:
874 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100875 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100876 event->trigger_type = type;
877 event->trigger_time = trigger_time;
878 event->is_signaled = false;
879 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100880}
881
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200882/**
Mario Six8fac2912018-07-10 08:40:17 +0200883 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
884 * event
885 * @event: event for which the timer is set
886 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200887 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200888 *
889 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200890 *
Mario Six8fac2912018-07-10 08:40:17 +0200891 * See the Unified Extensible Firmware Interface (UEFI) specification for
892 * details.
893 *
894 *
895 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200896 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200897static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
898 enum efi_timer_delay type,
899 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200900{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900901 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200902 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
903}
904
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200905/**
Mario Six8fac2912018-07-10 08:40:17 +0200906 * efi_wait_for_event() - wait for events to be signaled
907 * @num_events: number of events to be waited for
908 * @event: events to be waited for
909 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 *
911 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200912 *
Mario Six8fac2912018-07-10 08:40:17 +0200913 * See the Unified Extensible Firmware Interface (UEFI) specification for
914 * details.
915 *
916 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200917 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100918static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200919 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100920 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100921{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100922 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100923
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100924 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100925
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200926 /* Check parameters */
927 if (!num_events || !event)
928 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200929 /* Check TPL */
930 if (efi_tpl != TPL_APPLICATION)
931 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200932 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100933 if (efi_is_event(event[i]) != EFI_SUCCESS)
934 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200935 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
936 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200937 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200938 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200939 }
940
941 /* Wait for signal */
942 for (;;) {
943 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200944 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200945 goto out;
946 }
947 /* Allow events to occur. */
948 efi_timer_check();
949 }
950
951out:
952 /*
953 * Reset the signal which is passed to the caller to allow periodic
954 * events to occur.
955 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200956 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200957 if (index)
958 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100959
960 return EFI_EXIT(EFI_SUCCESS);
961}
962
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200963/**
Mario Six8fac2912018-07-10 08:40:17 +0200964 * efi_signal_event_ext() - signal an EFI event
965 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200966 *
967 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200968 *
969 * See the Unified Extensible Firmware Interface (UEFI) specification for
970 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200971 *
972 * This functions sets the signaled state of the event and queues the
973 * notification function for execution.
974 *
Mario Six8fac2912018-07-10 08:40:17 +0200975 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200976 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200977static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100978{
979 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100980 if (efi_is_event(event) != EFI_SUCCESS)
981 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200982 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100983 return EFI_EXIT(EFI_SUCCESS);
984}
985
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200986/**
Mario Six8fac2912018-07-10 08:40:17 +0200987 * efi_close_event() - close an EFI event
988 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200989 *
990 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200991 *
Mario Six8fac2912018-07-10 08:40:17 +0200992 * See the Unified Extensible Firmware Interface (UEFI) specification for
993 * details.
994 *
995 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200996 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200997static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100998{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200999 struct efi_register_notify_event *item, *next;
1000
Alexander Grafc15d9212016-03-04 01:09:59 +01001001 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001002 if (efi_is_event(event) != EFI_SUCCESS)
1003 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001004
1005 /* Remove protocol notify registrations for the event */
1006 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1007 link) {
1008 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001009 struct efi_protocol_notification *hitem, *hnext;
1010
1011 /* Remove signaled handles */
1012 list_for_each_entry_safe(hitem, hnext, &item->handles,
1013 link) {
1014 list_del(&hitem->link);
1015 free(hitem);
1016 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001017 list_del(&item->link);
1018 free(item);
1019 }
1020 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001021 /* Remove event from queue */
1022 if (efi_event_is_queued(event))
1023 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001024
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001025 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001026 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001027 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001028}
1029
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001030/**
Mario Six8fac2912018-07-10 08:40:17 +02001031 * efi_check_event() - check if an event is signaled
1032 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001033 *
1034 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001035 *
1036 * See the Unified Extensible Firmware Interface (UEFI) specification for
1037 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001038 *
Mario Six8fac2912018-07-10 08:40:17 +02001039 * If an event is not signaled yet, the notification function is queued. The
1040 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001041 *
Mario Six8fac2912018-07-10 08:40:17 +02001042 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001043 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001044static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001045{
1046 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001047 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001048 if (efi_is_event(event) != EFI_SUCCESS ||
1049 event->type & EVT_NOTIFY_SIGNAL)
1050 return EFI_EXIT(EFI_INVALID_PARAMETER);
1051 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001052 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001053 if (event->is_signaled) {
1054 event->is_signaled = false;
1055 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001056 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001057 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001058}
1059
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001060/**
Mario Six8fac2912018-07-10 08:40:17 +02001061 * efi_search_obj() - find the internal EFI object for a handle
1062 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001063 *
Mario Six8fac2912018-07-10 08:40:17 +02001064 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001065 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001066struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001067{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001068 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001069
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001070 if (!handle)
1071 return NULL;
1072
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001073 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001074 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001075 return efiobj;
1076 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001077 return NULL;
1078}
1079
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001080/**
Mario Six8fac2912018-07-10 08:40:17 +02001081 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1082 * to a protocol
1083 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001084 *
Mario Six8fac2912018-07-10 08:40:17 +02001085 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001086 */
1087static struct efi_open_protocol_info_entry *efi_create_open_info(
1088 struct efi_handler *handler)
1089{
1090 struct efi_open_protocol_info_item *item;
1091
1092 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1093 if (!item)
1094 return NULL;
1095 /* Append the item to the open protocol info list. */
1096 list_add_tail(&item->link, &handler->open_infos);
1097
1098 return &item->info;
1099}
1100
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001101/**
Mario Six8fac2912018-07-10 08:40:17 +02001102 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1103 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001104 *
Mario Six8fac2912018-07-10 08:40:17 +02001105 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001106 */
1107static efi_status_t efi_delete_open_info(
1108 struct efi_open_protocol_info_item *item)
1109{
1110 list_del(&item->link);
1111 free(item);
1112 return EFI_SUCCESS;
1113}
1114
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001115/**
Mario Six8fac2912018-07-10 08:40:17 +02001116 * efi_add_protocol() - install new protocol on a handle
1117 * @handle: handle on which the protocol shall be installed
1118 * @protocol: GUID of the protocol to be installed
1119 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001120 *
Mario Six8fac2912018-07-10 08:40:17 +02001121 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001122 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001123efi_status_t efi_add_protocol(const efi_handle_t handle,
1124 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001125 void *protocol_interface)
1126{
1127 struct efi_object *efiobj;
1128 struct efi_handler *handler;
1129 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001130 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001131
1132 efiobj = efi_search_obj(handle);
1133 if (!efiobj)
1134 return EFI_INVALID_PARAMETER;
1135 ret = efi_search_protocol(handle, protocol, NULL);
1136 if (ret != EFI_NOT_FOUND)
1137 return EFI_INVALID_PARAMETER;
1138 handler = calloc(1, sizeof(struct efi_handler));
1139 if (!handler)
1140 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001141 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001142 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001143 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001144 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001145
1146 /* Notify registered events */
1147 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001148 if (!guidcmp(protocol, &event->protocol)) {
1149 struct efi_protocol_notification *notif;
1150
1151 notif = calloc(1, sizeof(*notif));
1152 if (!notif) {
1153 list_del(&handler->link);
1154 free(handler);
1155 return EFI_OUT_OF_RESOURCES;
1156 }
1157 notif->handle = handle;
1158 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001159 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001160 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001161 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001162 }
1163
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001164 if (!guidcmp(&efi_guid_device_path, protocol))
1165 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001166 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001167}
1168
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001169/**
Mario Six8fac2912018-07-10 08:40:17 +02001170 * efi_install_protocol_interface() - install protocol interface
1171 * @handle: handle on which the protocol shall be installed
1172 * @protocol: GUID of the protocol to be installed
1173 * @protocol_interface_type: type of the interface to be installed,
1174 * always EFI_NATIVE_INTERFACE
1175 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001176 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001177 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001178 *
Mario Six8fac2912018-07-10 08:40:17 +02001179 * See the Unified Extensible Firmware Interface (UEFI) specification for
1180 * details.
1181 *
1182 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001183 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001184static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001185 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001186 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001187{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001188 efi_status_t r;
1189
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001190 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001191 protocol_interface);
1192
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001193 if (!handle || !protocol ||
1194 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1195 r = EFI_INVALID_PARAMETER;
1196 goto out;
1197 }
1198
1199 /* Create new handle if requested. */
1200 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001201 r = efi_create_handle(handle);
1202 if (r != EFI_SUCCESS)
1203 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001204 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001205 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001206 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001207 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001208 /* Add new protocol */
1209 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001210out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001211 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001212}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001213
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001214/**
Mario Six8fac2912018-07-10 08:40:17 +02001215 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001216 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001217 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001218 * @number_of_drivers: number of child controllers
1219 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001220 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001221 * The allocated buffer has to be freed with free().
1222 *
Mario Six8fac2912018-07-10 08:40:17 +02001223 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001224 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001225static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001226 const efi_guid_t *protocol,
1227 efi_uintn_t *number_of_drivers,
1228 efi_handle_t **driver_handle_buffer)
1229{
1230 struct efi_handler *handler;
1231 struct efi_open_protocol_info_item *item;
1232 efi_uintn_t count = 0, i;
1233 bool duplicate;
1234
1235 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001236 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001237 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001238 continue;
1239 list_for_each_entry(item, &handler->open_infos, link) {
1240 if (item->info.attributes &
1241 EFI_OPEN_PROTOCOL_BY_DRIVER)
1242 ++count;
1243 }
1244 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001245 *number_of_drivers = 0;
1246 if (!count) {
1247 *driver_handle_buffer = NULL;
1248 return EFI_SUCCESS;
1249 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001250 /*
1251 * Create buffer. In case of duplicate driver assignments the buffer
1252 * will be too large. But that does not harm.
1253 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001254 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1255 if (!*driver_handle_buffer)
1256 return EFI_OUT_OF_RESOURCES;
1257 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001258 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001259 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001260 continue;
1261 list_for_each_entry(item, &handler->open_infos, link) {
1262 if (item->info.attributes &
1263 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1264 /* Check this is a new driver */
1265 duplicate = false;
1266 for (i = 0; i < *number_of_drivers; ++i) {
1267 if ((*driver_handle_buffer)[i] ==
1268 item->info.agent_handle)
1269 duplicate = true;
1270 }
1271 /* Copy handle to buffer */
1272 if (!duplicate) {
1273 i = (*number_of_drivers)++;
1274 (*driver_handle_buffer)[i] =
1275 item->info.agent_handle;
1276 }
1277 }
1278 }
1279 }
1280 return EFI_SUCCESS;
1281}
1282
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001283/**
Mario Six8fac2912018-07-10 08:40:17 +02001284 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001285 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001286 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001287 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001288 *
1289 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001290 *
1291 * See the Unified Extensible Firmware Interface (UEFI) specification for
1292 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001293 *
Mario Six8fac2912018-07-10 08:40:17 +02001294 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001295 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001296static efi_status_t efi_disconnect_all_drivers
1297 (efi_handle_t handle,
1298 const efi_guid_t *protocol,
1299 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001300{
1301 efi_uintn_t number_of_drivers;
1302 efi_handle_t *driver_handle_buffer;
1303 efi_status_t r, ret;
1304
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001305 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001306 &driver_handle_buffer);
1307 if (ret != EFI_SUCCESS)
1308 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001309 if (!number_of_drivers)
1310 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001311 ret = EFI_NOT_FOUND;
1312 while (number_of_drivers) {
1313 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001314 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001315 driver_handle_buffer[--number_of_drivers],
1316 child_handle));
1317 if (r == EFI_SUCCESS)
1318 ret = r;
1319 }
1320 free(driver_handle_buffer);
1321 return ret;
1322}
1323
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001324/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001325 * efi_uninstall_protocol() - uninstall protocol interface
1326 *
Mario Six8fac2912018-07-10 08:40:17 +02001327 * @handle: handle from which the protocol shall be removed
1328 * @protocol: GUID of the protocol to be removed
1329 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001330 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001331 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001332 *
1333 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001334 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001335static efi_status_t efi_uninstall_protocol
1336 (efi_handle_t handle, const efi_guid_t *protocol,
1337 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001338{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001339 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001340 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001341 struct efi_open_protocol_info_item *item;
1342 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001343 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001344
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001345 /* Check handle */
1346 efiobj = efi_search_obj(handle);
1347 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001348 r = EFI_INVALID_PARAMETER;
1349 goto out;
1350 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001351 /* Find the protocol on the handle */
1352 r = efi_search_protocol(handle, protocol, &handler);
1353 if (r != EFI_SUCCESS)
1354 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001355 /* Disconnect controllers */
1356 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001357 /* Close protocol */
1358 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1359 if (item->info.attributes ==
1360 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1361 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1362 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1363 list_del(&item->link);
1364 }
1365 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001366 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001367 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001368 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001369 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001370out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001371 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001372}
1373
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001374/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001375 * efi_uninstall_protocol_interface() - uninstall protocol interface
1376 * @handle: handle from which the protocol shall be removed
1377 * @protocol: GUID of the protocol to be removed
1378 * @protocol_interface: interface to be removed
1379 *
1380 * This function implements the UninstallProtocolInterface service.
1381 *
1382 * See the Unified Extensible Firmware Interface (UEFI) specification for
1383 * details.
1384 *
1385 * Return: status code
1386 */
1387static efi_status_t EFIAPI efi_uninstall_protocol_interface
1388 (efi_handle_t handle, const efi_guid_t *protocol,
1389 void *protocol_interface)
1390{
1391 efi_status_t ret;
1392
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001393 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001394
1395 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1396 if (ret != EFI_SUCCESS)
1397 goto out;
1398
1399 /* If the last protocol has been removed, delete the handle. */
1400 if (list_empty(&handle->protocols)) {
1401 list_del(&handle->link);
1402 free(handle);
1403 }
1404out:
1405 return EFI_EXIT(ret);
1406}
1407
1408/**
Mario Six8fac2912018-07-10 08:40:17 +02001409 * efi_register_protocol_notify() - register an event for notification when a
1410 * protocol is installed.
1411 * @protocol: GUID of the protocol whose installation shall be notified
1412 * @event: event to be signaled upon installation of the protocol
1413 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001414 *
1415 * This function implements the RegisterProtocolNotify service.
1416 * See the Unified Extensible Firmware Interface (UEFI) specification
1417 * for details.
1418 *
Mario Six8fac2912018-07-10 08:40:17 +02001419 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001420 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001421efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1422 struct efi_event *event,
1423 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001424{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001425 struct efi_register_notify_event *item;
1426 efi_status_t ret = EFI_SUCCESS;
1427
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001428 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001429
1430 if (!protocol || !event || !registration) {
1431 ret = EFI_INVALID_PARAMETER;
1432 goto out;
1433 }
1434
1435 item = calloc(1, sizeof(struct efi_register_notify_event));
1436 if (!item) {
1437 ret = EFI_OUT_OF_RESOURCES;
1438 goto out;
1439 }
1440
1441 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301442 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001443 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001444
1445 list_add_tail(&item->link, &efi_register_notify_events);
1446
1447 *registration = item;
1448out:
1449 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001450}
1451
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001452/**
Mario Six8fac2912018-07-10 08:40:17 +02001453 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001454 *
Mario Six8fac2912018-07-10 08:40:17 +02001455 * @search_type: selection criterion
1456 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001457 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001458 *
1459 * See the documentation of the LocateHandle service in the UEFI specification.
1460 *
Mario Six8fac2912018-07-10 08:40:17 +02001461 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001462 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001463static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001464 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001465{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001466 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001467
1468 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001469 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001470 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001471 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001472 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001473 return (ret != EFI_SUCCESS);
1474 default:
1475 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001476 return -1;
1477 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001478}
1479
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001480/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001481 * efi_check_register_notify_event() - check if registration key is valid
1482 *
1483 * Check that a pointer is a valid registration key as returned by
1484 * RegisterProtocolNotify().
1485 *
1486 * @key: registration key
1487 * Return: valid registration key or NULL
1488 */
1489static struct efi_register_notify_event *efi_check_register_notify_event
1490 (void *key)
1491{
1492 struct efi_register_notify_event *event;
1493
1494 list_for_each_entry(event, &efi_register_notify_events, link) {
1495 if (event == (struct efi_register_notify_event *)key)
1496 return event;
1497 }
1498 return NULL;
1499}
1500
1501/**
Mario Six8fac2912018-07-10 08:40:17 +02001502 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001503 *
1504 * @search_type: selection criterion
1505 * @protocol: GUID of the protocol
1506 * @search_key: registration key
1507 * @buffer_size: size of the buffer to receive the handles in bytes
1508 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001509 *
1510 * This function is meant for U-Boot internal calls. For the API implementation
1511 * of the LocateHandle service see efi_locate_handle_ext.
1512 *
Mario Six8fac2912018-07-10 08:40:17 +02001513 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001514 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001515static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001516 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001517 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001518 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001519{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001520 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001521 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001522 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001523 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001524
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001525 /* Check parameters */
1526 switch (search_type) {
1527 case ALL_HANDLES:
1528 break;
1529 case BY_REGISTER_NOTIFY:
1530 if (!search_key)
1531 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001532 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001533 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001534 if (!event)
1535 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001536 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001537 case BY_PROTOCOL:
1538 if (!protocol)
1539 return EFI_INVALID_PARAMETER;
1540 break;
1541 default:
1542 return EFI_INVALID_PARAMETER;
1543 }
1544
Alexander Grafc15d9212016-03-04 01:09:59 +01001545 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001546 if (search_type == BY_REGISTER_NOTIFY) {
1547 if (list_empty(&event->handles))
1548 return EFI_NOT_FOUND;
1549 handle = list_first_entry(&event->handles,
1550 struct efi_protocol_notification,
1551 link);
1552 efiobj = handle->handle;
1553 size += sizeof(void *);
1554 } else {
1555 list_for_each_entry(efiobj, &efi_obj_list, link) {
1556 if (!efi_search(search_type, protocol, efiobj))
1557 size += sizeof(void *);
1558 }
1559 if (size == 0)
1560 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001561 }
1562
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001563 if (!buffer_size)
1564 return EFI_INVALID_PARAMETER;
1565
Alexander Grafc15d9212016-03-04 01:09:59 +01001566 if (*buffer_size < size) {
1567 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001568 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001569 }
1570
Rob Clarkcdee3372017-08-06 14:10:07 -04001571 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001572
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001573 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001574 if (!buffer)
1575 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001576
Alexander Grafc15d9212016-03-04 01:09:59 +01001577 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001578 if (search_type == BY_REGISTER_NOTIFY) {
1579 *buffer = efiobj;
1580 list_del(&handle->link);
1581 } else {
1582 list_for_each_entry(efiobj, &efi_obj_list, link) {
1583 if (!efi_search(search_type, protocol, efiobj))
1584 *buffer++ = efiobj;
1585 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001586 }
1587
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001588 return EFI_SUCCESS;
1589}
1590
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001591/**
Mario Six8fac2912018-07-10 08:40:17 +02001592 * efi_locate_handle_ext() - locate handles implementing a protocol.
1593 * @search_type: selection criterion
1594 * @protocol: GUID of the protocol
1595 * @search_key: registration key
1596 * @buffer_size: size of the buffer to receive the handles in bytes
1597 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001598 *
1599 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001600 *
Mario Six8fac2912018-07-10 08:40:17 +02001601 * See the Unified Extensible Firmware Interface (UEFI) specification for
1602 * details.
1603 *
1604 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001605 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001606static efi_status_t EFIAPI efi_locate_handle_ext(
1607 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001608 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001609 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001610{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001611 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001612 buffer_size, buffer);
1613
1614 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1615 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001616}
1617
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001618/**
Mario Six8fac2912018-07-10 08:40:17 +02001619 * efi_remove_configuration_table() - collapses configuration table entries,
1620 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001621 *
Mario Six8fac2912018-07-10 08:40:17 +02001622 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001623 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001624static void efi_remove_configuration_table(int i)
1625{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001626 struct efi_configuration_table *this = &systab.tables[i];
1627 struct efi_configuration_table *next = &systab.tables[i + 1];
1628 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001629
1630 memmove(this, next, (ulong)end - (ulong)next);
1631 systab.nr_tables--;
1632}
1633
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001634/**
Mario Six8fac2912018-07-10 08:40:17 +02001635 * efi_install_configuration_table() - adds, updates, or removes a
1636 * configuration table
1637 * @guid: GUID of the installed table
1638 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001639 *
1640 * This function is used for internal calls. For the API implementation of the
1641 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1642 *
Mario Six8fac2912018-07-10 08:40:17 +02001643 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001644 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001645efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1646 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001647{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001648 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001649 int i;
1650
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001651 if (!guid)
1652 return EFI_INVALID_PARAMETER;
1653
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001654 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001655 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001656 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001657 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001658 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001659 else
1660 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001661 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001662 }
1663 }
1664
Alexander Graffe3366f2017-07-26 13:41:04 +02001665 if (!table)
1666 return EFI_NOT_FOUND;
1667
Alexander Grafc15d9212016-03-04 01:09:59 +01001668 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001669 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001670 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001671
1672 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301673 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001674 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001675 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001676
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001677out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001678 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001679 efi_update_table_header_crc32(&systab.hdr);
1680
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001681 /* Notify that the configuration table was changed */
1682 list_for_each_entry(evt, &efi_events, link) {
1683 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001684 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001685 break;
1686 }
1687 }
1688
Alexander Grafc5c11632016-08-19 01:23:24 +02001689 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001690}
1691
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001692/**
Mario Six8fac2912018-07-10 08:40:17 +02001693 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1694 * configuration table.
1695 * @guid: GUID of the installed table
1696 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001697 *
1698 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001699 *
Mario Six8fac2912018-07-10 08:40:17 +02001700 * See the Unified Extensible Firmware Interface (UEFI) specification for
1701 * details.
1702 *
1703 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001704 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001705static efi_status_t
1706EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1707 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001708{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001709 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001710 return EFI_EXIT(efi_install_configuration_table(guid, table));
1711}
1712
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001713/**
Mario Six8fac2912018-07-10 08:40:17 +02001714 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001715 *
1716 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001717 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001718 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001719 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001720 * code is returned.
1721 *
1722 * @device_path: device path of the loaded image
1723 * @file_path: file path of the loaded image
1724 * @handle_ptr: handle of the loaded image
1725 * @info_ptr: loaded image protocol
1726 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001727 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001728efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1729 struct efi_device_path *file_path,
1730 struct efi_loaded_image_obj **handle_ptr,
1731 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001732{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001733 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001734 struct efi_loaded_image *info = NULL;
1735 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001736 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001737
1738 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1739 *handle_ptr = NULL;
1740 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001741
1742 info = calloc(1, sizeof(*info));
1743 if (!info)
1744 return EFI_OUT_OF_RESOURCES;
1745 obj = calloc(1, sizeof(*obj));
1746 if (!obj) {
1747 free(info);
1748 return EFI_OUT_OF_RESOURCES;
1749 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001750 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001751
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001752 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001753 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001754
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001755 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001756 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001757 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001758
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001759 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001760 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001761
1762 dp = efi_dp_append(device_path, file_path);
1763 if (!dp) {
1764 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001765 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001766 }
1767 } else {
1768 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001769 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001770 ret = efi_add_protocol(&obj->header,
1771 &efi_guid_loaded_image_device_path, dp);
1772 if (ret != EFI_SUCCESS)
1773 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001774
1775 /*
1776 * When asking for the loaded_image interface, just
1777 * return handle which points to loaded_image_info
1778 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001779 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001780 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001781 if (ret != EFI_SUCCESS)
1782 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001783
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001784 *info_ptr = info;
1785 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001786
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001787 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001788failure:
1789 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001790 efi_delete_handle(&obj->header);
1791 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001792 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001793}
1794
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001795/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001796 * efi_locate_device_path() - Get the device path and handle of an device
1797 * implementing a protocol
1798 * @protocol: GUID of the protocol
1799 * @device_path: device path
1800 * @device: handle of the device
1801 *
1802 * This function implements the LocateDevicePath service.
1803 *
1804 * See the Unified Extensible Firmware Interface (UEFI) specification for
1805 * details.
1806 *
1807 * Return: status code
1808 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001809efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1810 struct efi_device_path **device_path,
1811 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001812{
1813 struct efi_device_path *dp;
1814 size_t i;
1815 struct efi_handler *handler;
1816 efi_handle_t *handles;
1817 size_t len, len_dp;
1818 size_t len_best = 0;
1819 efi_uintn_t no_handles;
1820 u8 *remainder;
1821 efi_status_t ret;
1822
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001823 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001824
1825 if (!protocol || !device_path || !*device_path) {
1826 ret = EFI_INVALID_PARAMETER;
1827 goto out;
1828 }
1829
1830 /* Find end of device path */
1831 len = efi_dp_instance_size(*device_path);
1832
1833 /* Get all handles implementing the protocol */
1834 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1835 &no_handles, &handles));
1836 if (ret != EFI_SUCCESS)
1837 goto out;
1838
1839 for (i = 0; i < no_handles; ++i) {
1840 /* Find the device path protocol */
1841 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1842 &handler);
1843 if (ret != EFI_SUCCESS)
1844 continue;
1845 dp = (struct efi_device_path *)handler->protocol_interface;
1846 len_dp = efi_dp_instance_size(dp);
1847 /*
1848 * This handle can only be a better fit
1849 * if its device path length is longer than the best fit and
1850 * if its device path length is shorter of equal the searched
1851 * device path.
1852 */
1853 if (len_dp <= len_best || len_dp > len)
1854 continue;
1855 /* Check if dp is a subpath of device_path */
1856 if (memcmp(*device_path, dp, len_dp))
1857 continue;
1858 if (!device) {
1859 ret = EFI_INVALID_PARAMETER;
1860 goto out;
1861 }
1862 *device = handles[i];
1863 len_best = len_dp;
1864 }
1865 if (len_best) {
1866 remainder = (u8 *)*device_path + len_best;
1867 *device_path = (struct efi_device_path *)remainder;
1868 ret = EFI_SUCCESS;
1869 } else {
1870 ret = EFI_NOT_FOUND;
1871 }
1872out:
1873 return EFI_EXIT(ret);
1874}
1875
1876/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001877 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001878 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001879 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1880 * callers obligation to update the memory type as needed.
1881 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001882 * @file_path: the path of the image to load
1883 * @buffer: buffer containing the loaded image
1884 * @size: size of the loaded image
1885 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001886 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001887static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001888efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001889 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001890{
Rob Clark857a1222017-09-13 18:05:35 -04001891 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001892 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001893 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001894 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001895
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001896 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001897 f = efi_file_from_path(file_path);
1898 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001899 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001900
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001901 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001902 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001903 goto error;
1904
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001905 /*
1906 * When reading the file we do not yet know if it contains an
1907 * application, a boottime driver, or a runtime driver. So here we
1908 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1909 * update the reservation according to the image type.
1910 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001911 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1912 EFI_BOOT_SERVICES_DATA,
1913 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001914 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001915 ret = EFI_OUT_OF_RESOURCES;
1916 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001917 }
1918
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001919 /* Read file */
1920 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1921 if (ret != EFI_SUCCESS)
1922 efi_free_pages(addr, efi_size_in_pages(bs));
1923 *buffer = (void *)(uintptr_t)addr;
1924 *size = bs;
1925error:
1926 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001927 return ret;
1928}
1929
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001930/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001931 * efi_load_image_from_path() - load an image using a file path
1932 *
1933 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1934 * callers obligation to update the memory type as needed.
1935 *
1936 * @boot_policy: true for request originating from the boot manager
1937 * @file_path: the path of the image to load
1938 * @buffer: buffer containing the loaded image
1939 * @size: size of the loaded image
1940 * Return: status code
1941 */
1942static
1943efi_status_t efi_load_image_from_path(bool boot_policy,
1944 struct efi_device_path *file_path,
1945 void **buffer, efi_uintn_t *size)
1946{
1947 efi_handle_t device;
1948 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001949 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001950 struct efi_load_file_protocol *load_file_protocol = NULL;
1951 efi_uintn_t buffer_size;
1952 uint64_t addr, pages;
1953 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001954 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001955
1956 /* In case of failure nothing is returned */
1957 *buffer = NULL;
1958 *size = 0;
1959
1960 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001961 device = efi_dp_find_obj(dp, NULL, &rem);
1962 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1963 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001964 if (ret == EFI_SUCCESS)
1965 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001966
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001967 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001968 if (ret == EFI_SUCCESS) {
1969 guid = &efi_guid_load_file_protocol;
1970 } else if (!boot_policy) {
1971 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001972 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001973 }
1974 if (ret != EFI_SUCCESS)
1975 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001976 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001977 if (ret != EFI_SUCCESS)
1978 return EFI_NOT_FOUND;
1979 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001980 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001981 ret = EFI_CALL(load_file_protocol->load_file(
1982 load_file_protocol, rem, boot_policy,
1983 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001984 if (ret != EFI_BUFFER_TOO_SMALL)
1985 goto out;
1986 pages = efi_size_in_pages(buffer_size);
1987 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1988 pages, &addr);
1989 if (ret != EFI_SUCCESS) {
1990 ret = EFI_OUT_OF_RESOURCES;
1991 goto out;
1992 }
1993 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001994 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001995 &buffer_size, (void *)(uintptr_t)addr));
1996 if (ret != EFI_SUCCESS)
1997 efi_free_pages(addr, pages);
1998out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02001999 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002000 if (ret == EFI_SUCCESS) {
2001 *buffer = (void *)(uintptr_t)addr;
2002 *size = buffer_size;
2003 }
2004
2005 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002006}
2007
2008/**
Mario Six8fac2912018-07-10 08:40:17 +02002009 * efi_load_image() - load an EFI image into memory
2010 * @boot_policy: true for request originating from the boot manager
2011 * @parent_image: the caller's image handle
2012 * @file_path: the path of the image to load
2013 * @source_buffer: memory location from which the image is installed
2014 * @source_size: size of the memory area from which the image is installed
2015 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002016 *
2017 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002018 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002019 * See the Unified Extensible Firmware Interface (UEFI) specification
2020 * for details.
2021 *
Mario Six8fac2912018-07-10 08:40:17 +02002022 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002023 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002024efi_status_t EFIAPI efi_load_image(bool boot_policy,
2025 efi_handle_t parent_image,
2026 struct efi_device_path *file_path,
2027 void *source_buffer,
2028 efi_uintn_t source_size,
2029 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002030{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002031 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002032 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002033 struct efi_loaded_image_obj **image_obj =
2034 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002035 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002036 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002037
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002038 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002039 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002040
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002041 if (!image_handle || (!source_buffer && !file_path) ||
2042 !efi_search_obj(parent_image) ||
2043 /* The parent image handle must refer to a loaded image */
2044 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002045 ret = EFI_INVALID_PARAMETER;
2046 goto error;
2047 }
Rob Clark857a1222017-09-13 18:05:35 -04002048
2049 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002050 ret = efi_load_image_from_path(boot_policy, file_path,
2051 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002052 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002053 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002054 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002055 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002056 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002057 /* split file_path which contains both the device and file parts */
2058 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002059 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002060 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002061 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002062 if (!source_buffer)
2063 /* Release buffer to which file was loaded */
2064 efi_free_pages((uintptr_t)dest_buffer,
2065 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002066 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002067 info->system_table = &systab;
2068 info->parent_handle = parent_image;
2069 } else {
2070 /* The image is invalid. Release all associated resources. */
2071 efi_delete_handle(*image_handle);
2072 *image_handle = NULL;
2073 free(info);
2074 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002075error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002076 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002077}
2078
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002079/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002080 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2081 */
2082static void efi_exit_caches(void)
2083{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002084#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002085 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002086 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2087 * caches are enabled.
2088 *
2089 * TODO:
2090 * According to the UEFI spec caches that can be managed via CP15
2091 * operations should be enabled. Caches requiring platform information
2092 * to manage should be disabled. This should not happen in
2093 * ExitBootServices() but before invoking any UEFI binary is invoked.
2094 *
2095 * We want to keep the current workaround while GRUB prior to version
2096 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002097 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002098 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002099#endif
2100}
2101
2102/**
Mario Six8fac2912018-07-10 08:40:17 +02002103 * efi_exit_boot_services() - stop all boot services
2104 * @image_handle: handle of the loaded image
2105 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002106 *
2107 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002108 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002109 * See the Unified Extensible Firmware Interface (UEFI) specification
2110 * for details.
2111 *
Mario Six8fac2912018-07-10 08:40:17 +02002112 * All timer events are disabled. For exit boot services events the
2113 * notification function is called. The boot services are disabled in the
2114 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002115 *
Mario Six8fac2912018-07-10 08:40:17 +02002116 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002117 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002118static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002119 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002120{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002121 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002122 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002123
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002124 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002125
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002126 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002127 if (map_key != efi_memory_map_key) {
2128 ret = EFI_INVALID_PARAMETER;
2129 goto out;
2130 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002131
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002132 /* Check if ExitBootServices has already been called */
2133 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002134 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002135
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002136 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2137 list_for_each_entry(evt, &efi_events, link) {
2138 if (evt->group &&
2139 !guidcmp(evt->group,
2140 &efi_guid_event_group_before_exit_boot_services)) {
2141 efi_signal_event(evt);
2142 break;
2143 }
2144 }
2145
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002146 /* Stop all timer related activities */
2147 timers_enabled = false;
2148
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002149 /* Add related events to the event group */
2150 list_for_each_entry(evt, &efi_events, link) {
2151 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2152 evt->group = &efi_guid_event_group_exit_boot_services;
2153 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002154 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002155 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002156 if (evt->group &&
2157 !guidcmp(evt->group,
2158 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002159 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002160 break;
2161 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002162 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002163
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002164 /* Make sure that notification functions are not called anymore */
2165 efi_tpl = TPL_HIGH_LEVEL;
2166
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002167 /* Notify variable services */
2168 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002169
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002170 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2171 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2172 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2173 list_del(&evt->link);
2174 }
2175
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002176 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002177 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002178 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002179 udc_disconnect();
2180 board_quiesce_devices();
2181 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2182 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002183
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002184 /* Patch out unsupported runtime function */
2185 efi_runtime_detach();
2186
Alexander Graffa5246b2018-11-15 21:23:47 +01002187 /* Fix up caches for EFI payloads if necessary */
2188 efi_exit_caches();
2189
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002190 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002191 systab.con_in_handle = NULL;
2192 systab.con_in = NULL;
2193 systab.con_out_handle = NULL;
2194 systab.con_out = NULL;
2195 systab.stderr_handle = NULL;
2196 systab.std_err = NULL;
2197 systab.boottime = NULL;
2198
2199 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002200 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002201
Alexander Grafc15d9212016-03-04 01:09:59 +01002202 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002203 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002204 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002205out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002206 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2207 if (ret != EFI_SUCCESS)
2208 efi_tcg2_notify_exit_boot_services_failed();
2209 }
2210
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002211 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002212}
2213
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002214/**
Mario Six8fac2912018-07-10 08:40:17 +02002215 * efi_get_next_monotonic_count() - get next value of the counter
2216 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002217 *
2218 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002219 *
2220 * See the Unified Extensible Firmware Interface (UEFI) specification for
2221 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002222 *
Mario Six8fac2912018-07-10 08:40:17 +02002223 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002224 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002225static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2226{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002227 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002228 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002229
Alexander Grafc15d9212016-03-04 01:09:59 +01002230 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002231 if (!count) {
2232 ret = EFI_INVALID_PARAMETER;
2233 goto out;
2234 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002235 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002236 ret = EFI_SUCCESS;
2237out:
2238 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002239}
2240
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002241/**
Mario Six8fac2912018-07-10 08:40:17 +02002242 * efi_stall() - sleep
2243 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002244 *
Mario Six8fac2912018-07-10 08:40:17 +02002245 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002246 *
Mario Six8fac2912018-07-10 08:40:17 +02002247 * See the Unified Extensible Firmware Interface (UEFI) specification for
2248 * details.
2249 *
2250 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002251 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002252static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2253{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002254 u64 end_tick;
2255
Alexander Grafc15d9212016-03-04 01:09:59 +01002256 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002257
2258 end_tick = get_ticks() + usec_to_tick(microseconds);
2259 while (get_ticks() < end_tick)
2260 efi_timer_check();
2261
Alexander Grafc15d9212016-03-04 01:09:59 +01002262 return EFI_EXIT(EFI_SUCCESS);
2263}
2264
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002265/**
Mario Six8fac2912018-07-10 08:40:17 +02002266 * efi_set_watchdog_timer() - reset the watchdog timer
2267 * @timeout: seconds before reset by watchdog
2268 * @watchdog_code: code to be logged when resetting
2269 * @data_size: size of buffer in bytes
2270 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002271 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002272 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002273 *
Mario Six8fac2912018-07-10 08:40:17 +02002274 * See the Unified Extensible Firmware Interface (UEFI) specification for
2275 * details.
2276 *
2277 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002278 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002279static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2280 uint64_t watchdog_code,
2281 unsigned long data_size,
2282 uint16_t *watchdog_data)
2283{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002284 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002285 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002286 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002287}
2288
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002289/**
Mario Six8fac2912018-07-10 08:40:17 +02002290 * efi_close_protocol() - close a protocol
2291 * @handle: handle on which the protocol shall be closed
2292 * @protocol: GUID of the protocol to close
2293 * @agent_handle: handle of the driver
2294 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002295 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002296 * This is the function implementing the CloseProtocol service is for internal
2297 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002298 *
Mario Six8fac2912018-07-10 08:40:17 +02002299 * See the Unified Extensible Firmware Interface (UEFI) specification for
2300 * details.
2301 *
2302 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002303 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002304efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2305 efi_handle_t agent_handle,
2306 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002307{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002308 struct efi_handler *handler;
2309 struct efi_open_protocol_info_item *item;
2310 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002311 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002312
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002313 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002314 (controller_handle && !efi_search_obj(controller_handle)))
2315 return EFI_INVALID_PARAMETER;
2316 ret = efi_search_protocol(handle, protocol, &handler);
2317 if (ret != EFI_SUCCESS)
2318 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002319
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002320 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002321 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2322 if (item->info.agent_handle == agent_handle &&
2323 item->info.controller_handle == controller_handle) {
2324 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002325 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002326 }
2327 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002328
2329 return ret;
2330}
2331
2332/**
2333 * efi_close_protocol_ext() - close a protocol
2334 * @handle: handle on which the protocol shall be closed
2335 * @protocol: GUID of the protocol to close
2336 * @agent_handle: handle of the driver
2337 * @controller_handle: handle of the controller
2338 *
2339 * This function implements the CloseProtocol service.
2340 *
2341 * See the Unified Extensible Firmware Interface (UEFI) specification for
2342 * details.
2343 *
2344 * Return: status code
2345 */
2346static efi_status_t EFIAPI
2347efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2348 efi_handle_t agent_handle,
2349 efi_handle_t controller_handle)
2350{
2351 efi_status_t ret;
2352
2353 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2354 controller_handle);
2355
2356 ret = efi_close_protocol(handle, protocol,
2357 agent_handle, controller_handle);
2358
2359 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002360}
2361
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002362/**
Mario Six8fac2912018-07-10 08:40:17 +02002363 * efi_open_protocol_information() - provide information about then open status
2364 * of a protocol on a handle
2365 * @handle: handle for which the information shall be retrieved
2366 * @protocol: GUID of the protocol
2367 * @entry_buffer: buffer to receive the open protocol information
2368 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002369 *
2370 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002371 *
2372 * See the Unified Extensible Firmware Interface (UEFI) specification for
2373 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002374 *
Mario Six8fac2912018-07-10 08:40:17 +02002375 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002376 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002377static efi_status_t EFIAPI efi_open_protocol_information(
2378 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002379 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002380 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002381{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002382 unsigned long buffer_size;
2383 unsigned long count;
2384 struct efi_handler *handler;
2385 struct efi_open_protocol_info_item *item;
2386 efi_status_t r;
2387
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002388 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002389 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002390
2391 /* Check parameters */
2392 if (!entry_buffer) {
2393 r = EFI_INVALID_PARAMETER;
2394 goto out;
2395 }
2396 r = efi_search_protocol(handle, protocol, &handler);
2397 if (r != EFI_SUCCESS)
2398 goto out;
2399
2400 /* Count entries */
2401 count = 0;
2402 list_for_each_entry(item, &handler->open_infos, link) {
2403 if (item->info.open_count)
2404 ++count;
2405 }
2406 *entry_count = count;
2407 *entry_buffer = NULL;
2408 if (!count) {
2409 r = EFI_SUCCESS;
2410 goto out;
2411 }
2412
2413 /* Copy entries */
2414 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002415 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002416 (void **)entry_buffer);
2417 if (r != EFI_SUCCESS)
2418 goto out;
2419 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2420 if (item->info.open_count)
2421 (*entry_buffer)[--count] = item->info;
2422 }
2423out:
2424 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002425}
2426
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002427/**
Mario Six8fac2912018-07-10 08:40:17 +02002428 * efi_protocols_per_handle() - get protocols installed on a handle
2429 * @handle: handle for which the information is retrieved
2430 * @protocol_buffer: buffer with protocol GUIDs
2431 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002432 *
2433 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002434 *
Mario Six8fac2912018-07-10 08:40:17 +02002435 * See the Unified Extensible Firmware Interface (UEFI) specification for
2436 * details.
2437 *
2438 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002439 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002440static efi_status_t EFIAPI efi_protocols_per_handle(
2441 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002442 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002443{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002444 unsigned long buffer_size;
2445 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002446 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002447 efi_status_t r;
2448
Alexander Grafc15d9212016-03-04 01:09:59 +01002449 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2450 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002451
2452 if (!handle || !protocol_buffer || !protocol_buffer_count)
2453 return EFI_EXIT(EFI_INVALID_PARAMETER);
2454
2455 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002456 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002457
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002458 efiobj = efi_search_obj(handle);
2459 if (!efiobj)
2460 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002461
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002462 /* Count protocols */
2463 list_for_each(protocol_handle, &efiobj->protocols) {
2464 ++*protocol_buffer_count;
2465 }
2466
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002467 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002468 if (*protocol_buffer_count) {
2469 size_t j = 0;
2470
2471 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002472 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002473 (void **)protocol_buffer);
2474 if (r != EFI_SUCCESS)
2475 return EFI_EXIT(r);
2476 list_for_each(protocol_handle, &efiobj->protocols) {
2477 struct efi_handler *protocol;
2478
2479 protocol = list_entry(protocol_handle,
2480 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002481 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002482 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002483 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002484 }
2485
2486 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002487}
2488
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002489efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2490 const efi_guid_t *protocol, void *search_key,
2491 efi_uintn_t *no_handles, efi_handle_t **buffer)
2492{
2493 efi_status_t r;
2494 efi_uintn_t buffer_size = 0;
2495
2496 if (!no_handles || !buffer) {
2497 r = EFI_INVALID_PARAMETER;
2498 goto out;
2499 }
2500 *no_handles = 0;
2501 *buffer = NULL;
2502 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2503 *buffer);
2504 if (r != EFI_BUFFER_TOO_SMALL)
2505 goto out;
2506 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2507 (void **)buffer);
2508 if (r != EFI_SUCCESS)
2509 goto out;
2510 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2511 *buffer);
2512 if (r == EFI_SUCCESS)
2513 *no_handles = buffer_size / sizeof(efi_handle_t);
2514out:
2515 return r;
2516}
2517
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002518/**
Mario Six8fac2912018-07-10 08:40:17 +02002519 * efi_locate_handle_buffer() - locate handles implementing a protocol
2520 * @search_type: selection criterion
2521 * @protocol: GUID of the protocol
2522 * @search_key: registration key
2523 * @no_handles: number of returned handles
2524 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002525 *
2526 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002527 *
Mario Six8fac2912018-07-10 08:40:17 +02002528 * See the Unified Extensible Firmware Interface (UEFI) specification for
2529 * details.
2530 *
2531 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002532 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002533efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002534 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002535 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002536 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002537{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002538 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002539
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002540 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002541 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002542
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002543 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2544 no_handles, buffer);
2545
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002546 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002547}
2548
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002549/**
Mario Six8fac2912018-07-10 08:40:17 +02002550 * efi_locate_protocol() - find an interface implementing a protocol
2551 * @protocol: GUID of the protocol
2552 * @registration: registration key passed to the notification function
2553 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002554 *
2555 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002556 *
2557 * See the Unified Extensible Firmware Interface (UEFI) specification for
2558 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002559 *
Mario Six8fac2912018-07-10 08:40:17 +02002560 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002561 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002562static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002563 void *registration,
2564 void **protocol_interface)
2565{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002566 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002567 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002568 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002569
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002570 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002571
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002572 /*
2573 * The UEFI spec explicitly requires a protocol even if a registration
2574 * key is provided. This differs from the logic in LocateHandle().
2575 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002576 if (!protocol || !protocol_interface)
2577 return EFI_EXIT(EFI_INVALID_PARAMETER);
2578
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002579 if (registration) {
2580 struct efi_register_notify_event *event;
2581 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002582
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002583 event = efi_check_register_notify_event(registration);
2584 if (!event)
2585 return EFI_EXIT(EFI_INVALID_PARAMETER);
2586 /*
2587 * The UEFI spec requires to return EFI_NOT_FOUND if no
2588 * protocol instance matches protocol and registration.
2589 * So let's do the same for a mismatch between protocol and
2590 * registration.
2591 */
2592 if (guidcmp(&event->protocol, protocol))
2593 goto not_found;
2594 if (list_empty(&event->handles))
2595 goto not_found;
2596 handle = list_first_entry(&event->handles,
2597 struct efi_protocol_notification,
2598 link);
2599 efiobj = handle->handle;
2600 list_del(&handle->link);
2601 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002602 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002603 if (ret == EFI_SUCCESS)
2604 goto found;
2605 } else {
2606 list_for_each_entry(efiobj, &efi_obj_list, link) {
2607 ret = efi_search_protocol(efiobj, protocol, &handler);
2608 if (ret == EFI_SUCCESS)
2609 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002610 }
2611 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002612not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002613 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002614 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002615found:
2616 *protocol_interface = handler->protocol_interface;
2617 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002618}
2619
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002620/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002621 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002622 * interfaces
2623 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002624 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002625 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002626 * Core functionality of efi_install_multiple_protocol_interfaces
2627 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002628 *
Mario Six8fac2912018-07-10 08:40:17 +02002629 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002630 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002631static efi_status_t EFIAPI
2632efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2633 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002634{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002635 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002636 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002637 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002638 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002639 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002640 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002641
2642 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002643 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002644
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002645 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002646 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002647 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002648 if (!protocol)
2649 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002650 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002651 /* Check that a device path has not been installed before */
2652 if (!guidcmp(protocol, &efi_guid_device_path)) {
2653 struct efi_device_path *dp = protocol_interface;
2654
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002655 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2656 &old_handle));
2657 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002658 dp->type == DEVICE_PATH_TYPE_END) {
2659 EFI_PRINT("Path %pD already installed\n",
2660 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002661 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002662 break;
2663 }
2664 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002665 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2666 EFI_NATIVE_INTERFACE,
2667 protocol_interface));
2668 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002669 break;
2670 i++;
2671 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002672 if (ret == EFI_SUCCESS)
2673 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002674
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002675 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002676 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002677 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2678 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002679 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002680 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002681 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002682
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002683out:
2684 efi_va_end(argptr_copy);
2685 return ret;
2686
Alexander Grafc15d9212016-03-04 01:09:59 +01002687}
2688
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002689/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002690 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2691 * interfaces
2692 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002693 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2694 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002695 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002696 *
2697 * This is the function for internal usage in U-Boot. For the API function
2698 * implementing the InstallMultipleProtocol service see
2699 * efi_install_multiple_protocol_interfaces_ext()
2700 *
2701 * Return: status code
2702 */
2703efi_status_t EFIAPI
2704efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2705{
2706 efi_status_t ret;
2707 efi_va_list argptr;
2708
2709 efi_va_start(argptr, handle);
2710 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2711 efi_va_end(argptr);
2712 return ret;
2713}
2714
2715/**
2716 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2717 * interfaces
2718 * @handle: handle on which the protocol interfaces shall be installed
2719 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2720 * interfaces
2721 *
2722 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002723 *
Mario Six8fac2912018-07-10 08:40:17 +02002724 * See the Unified Extensible Firmware Interface (UEFI) specification for
2725 * details.
2726 *
2727 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002728 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002729static efi_status_t EFIAPI
2730efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002731{
2732 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002733 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002734 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002735
2736 efi_va_start(argptr, handle);
2737 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2738 efi_va_end(argptr);
2739 return EFI_EXIT(ret);
2740}
2741
2742/**
2743 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2744 * multiple protocol
2745 * interfaces
2746 * @handle: handle from which the protocol interfaces shall be removed
2747 * @argptr: va_list of args
2748 *
2749 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2750 * Must not be called directly
2751 *
2752 * Return: status code
2753 */
2754static efi_status_t EFIAPI
2755efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2756 efi_va_list argptr)
2757{
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002758 const efi_guid_t *protocol;
2759 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002760 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002761 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002762 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002763
2764 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002765 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002766
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002767 efi_va_copy(argptr_copy, argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002768 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002769 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002770 if (!protocol)
2771 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002772 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002773 ret = efi_uninstall_protocol(handle, protocol,
2774 protocol_interface);
2775 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002776 break;
2777 i++;
2778 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002779 if (ret == EFI_SUCCESS) {
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002780 /* If the last protocol has been removed, delete the handle. */
2781 if (list_empty(&handle->protocols)) {
2782 list_del(&handle->link);
2783 free(handle);
2784 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002785 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002786 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002787
2788 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002789 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002790 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2791 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002792 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2793 EFI_NATIVE_INTERFACE,
2794 protocol_interface));
2795 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002796 /*
2797 * If any errors are generated while the protocol interfaces are being
2798 * uninstalled, then the protocols uninstalled prior to the error will
2799 * be reinstalled using InstallProtocolInterface() and the status code
2800 * EFI_INVALID_PARAMETER is returned.
2801 */
2802 ret = EFI_INVALID_PARAMETER;
2803
2804out:
2805 efi_va_end(argptr_copy);
2806 return ret;
2807}
2808
2809/**
2810 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2811 * interfaces
2812 * @handle: handle from which the protocol interfaces shall be removed
2813 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2814 * interfaces
2815 *
2816 * This function implements the UninstallMultipleProtocolInterfaces service.
2817 *
2818 * This is the function for internal usage in U-Boot. For the API function
2819 * implementing the UninstallMultipleProtocolInterfaces service see
2820 * efi_uninstall_multiple_protocol_interfaces_ext()
2821 *
2822 * Return: status code
2823 */
2824efi_status_t EFIAPI
2825efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2826{
2827 efi_status_t ret;
2828 efi_va_list argptr;
2829
2830 efi_va_start(argptr, handle);
2831 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002832 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002833 return ret;
2834}
2835
2836/**
2837 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2838 * interfaces
2839 * @handle: handle from which the protocol interfaces shall be removed
2840 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2841 * interfaces
2842 *
2843 * This function implements the UninstallMultipleProtocolInterfaces service.
2844 *
2845 * See the Unified Extensible Firmware Interface (UEFI) specification for
2846 * details.
2847 *
2848 * Return: status code
2849 */
2850static efi_status_t EFIAPI
2851efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2852{
2853 EFI_ENTRY("%p", handle);
2854 efi_status_t ret;
2855 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002856
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002857 efi_va_start(argptr, handle);
2858 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2859 efi_va_end(argptr);
2860 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002861}
2862
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002863/**
Mario Six8fac2912018-07-10 08:40:17 +02002864 * efi_calculate_crc32() - calculate cyclic redundancy code
2865 * @data: buffer with data
2866 * @data_size: size of buffer in bytes
2867 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002868 *
2869 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002870 *
2871 * See the Unified Extensible Firmware Interface (UEFI) specification for
2872 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002873 *
Mario Six8fac2912018-07-10 08:40:17 +02002874 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002875 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002876static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2877 efi_uintn_t data_size,
2878 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002879{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002880 efi_status_t ret = EFI_SUCCESS;
2881
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002882 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002883 if (!data || !data_size || !crc32_p) {
2884 ret = EFI_INVALID_PARAMETER;
2885 goto out;
2886 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002887 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002888out:
2889 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002890}
2891
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002892/**
Mario Six8fac2912018-07-10 08:40:17 +02002893 * efi_copy_mem() - copy memory
2894 * @destination: destination of the copy operation
2895 * @source: source of the copy operation
2896 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002897 *
2898 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002899 *
Mario Six8fac2912018-07-10 08:40:17 +02002900 * See the Unified Extensible Firmware Interface (UEFI) specification for
2901 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002902 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002903static void EFIAPI efi_copy_mem(void *destination, const void *source,
2904 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002905{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002906 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002907 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002908 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002909}
2910
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002911/**
Mario Six8fac2912018-07-10 08:40:17 +02002912 * efi_set_mem() - Fill memory with a byte value.
2913 * @buffer: buffer to fill
2914 * @size: size of buffer in bytes
2915 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002916 *
2917 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002918 *
Mario Six8fac2912018-07-10 08:40:17 +02002919 * See the Unified Extensible Firmware Interface (UEFI) specification for
2920 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002921 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002922static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002923{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002924 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002925 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002926 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002927}
2928
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002929/**
Mario Six8fac2912018-07-10 08:40:17 +02002930 * efi_protocol_open() - open protocol interface on a handle
2931 * @handler: handler of a protocol
2932 * @protocol_interface: interface implementing the protocol
2933 * @agent_handle: handle of the driver
2934 * @controller_handle: handle of the controller
2935 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002936 *
Mario Six8fac2912018-07-10 08:40:17 +02002937 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002938 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002939efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002940 struct efi_handler *handler,
2941 void **protocol_interface, void *agent_handle,
2942 void *controller_handle, uint32_t attributes)
2943{
2944 struct efi_open_protocol_info_item *item;
2945 struct efi_open_protocol_info_entry *match = NULL;
2946 bool opened_by_driver = false;
2947 bool opened_exclusive = false;
2948
2949 /* If there is no agent, only return the interface */
2950 if (!agent_handle)
2951 goto out;
2952
2953 /* For TEST_PROTOCOL ignore interface attribute */
2954 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2955 *protocol_interface = NULL;
2956
2957 /*
2958 * Check if the protocol is already opened by a driver with the same
2959 * attributes or opened exclusively
2960 */
2961 list_for_each_entry(item, &handler->open_infos, link) {
2962 if (item->info.agent_handle == agent_handle) {
2963 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2964 (item->info.attributes == attributes))
2965 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002966 } else {
2967 if (item->info.attributes &
2968 EFI_OPEN_PROTOCOL_BY_DRIVER)
2969 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002970 }
2971 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2972 opened_exclusive = true;
2973 }
2974
2975 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002976 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2977 if (opened_exclusive)
2978 return EFI_ACCESS_DENIED;
2979 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2980 if (opened_exclusive || opened_by_driver)
2981 return EFI_ACCESS_DENIED;
2982 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002983
2984 /* Prepare exclusive opening */
2985 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2986 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002987disconnect_next:
2988 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002989 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002990 efi_status_t ret;
2991
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002992 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002993 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2994 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002995 item->info.controller_handle,
2996 item->info.agent_handle,
2997 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002998 if (ret == EFI_SUCCESS)
2999 /*
3000 * Child controllers may have been
3001 * removed from the open_infos list. So
3002 * let's restart the loop.
3003 */
3004 goto disconnect_next;
3005 else
3006 opened_by_driver = true;
3007 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003008 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003009 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003010 if (opened_by_driver)
3011 return EFI_ACCESS_DENIED;
3012 }
3013
3014 /* Find existing entry */
3015 list_for_each_entry(item, &handler->open_infos, link) {
3016 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003017 item->info.controller_handle == controller_handle &&
3018 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003019 match = &item->info;
3020 }
3021 /* None found, create one */
3022 if (!match) {
3023 match = efi_create_open_info(handler);
3024 if (!match)
3025 return EFI_OUT_OF_RESOURCES;
3026 }
3027
3028 match->agent_handle = agent_handle;
3029 match->controller_handle = controller_handle;
3030 match->attributes = attributes;
3031 match->open_count++;
3032
3033out:
3034 /* For TEST_PROTOCOL ignore interface attribute. */
3035 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3036 *protocol_interface = handler->protocol_interface;
3037
3038 return EFI_SUCCESS;
3039}
3040
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003041/**
Mario Six8fac2912018-07-10 08:40:17 +02003042 * efi_open_protocol() - open protocol interface on a handle
3043 * @handle: handle on which the protocol shall be opened
3044 * @protocol: GUID of the protocol
3045 * @protocol_interface: interface implementing the protocol
3046 * @agent_handle: handle of the driver
3047 * @controller_handle: handle of the controller
3048 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003049 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003050 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003051 *
Mario Six8fac2912018-07-10 08:40:17 +02003052 * See the Unified Extensible Firmware Interface (UEFI) specification for
3053 * details.
3054 *
3055 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003056 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003057static efi_status_t EFIAPI efi_open_protocol
3058 (efi_handle_t handle, const efi_guid_t *protocol,
3059 void **protocol_interface, efi_handle_t agent_handle,
3060 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003061{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003062 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003063 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003064
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003065 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003066 protocol_interface, agent_handle, controller_handle,
3067 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003068
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003069 if (!handle || !protocol ||
3070 (!protocol_interface && attributes !=
3071 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003072 goto out;
3073 }
3074
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003075 switch (attributes) {
3076 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3077 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3078 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3079 break;
3080 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3081 if (controller_handle == handle)
3082 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003083 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003084 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3085 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003086 /* Check that the controller handle is valid */
3087 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003088 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003089 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003090 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003091 /* Check that the agent handle is valid */
3092 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003093 goto out;
3094 break;
3095 default:
3096 goto out;
3097 }
3098
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003099 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003100 switch (r) {
3101 case EFI_SUCCESS:
3102 break;
3103 case EFI_NOT_FOUND:
3104 r = EFI_UNSUPPORTED;
3105 goto out;
3106 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003107 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003108 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003109
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003110 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3111 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003112out:
3113 return EFI_EXIT(r);
3114}
3115
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003116/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003117 * efi_start_image() - call the entry point of an image
3118 * @image_handle: handle of the image
3119 * @exit_data_size: size of the buffer
3120 * @exit_data: buffer to receive the exit data of the called image
3121 *
3122 * This function implements the StartImage service.
3123 *
3124 * See the Unified Extensible Firmware Interface (UEFI) specification for
3125 * details.
3126 *
3127 * Return: status code
3128 */
3129efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3130 efi_uintn_t *exit_data_size,
3131 u16 **exit_data)
3132{
3133 struct efi_loaded_image_obj *image_obj =
3134 (struct efi_loaded_image_obj *)image_handle;
3135 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003136 void *info;
3137 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003138 efi_status_t exit_status;
3139 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003140
3141 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3142
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003143 if (!efi_search_obj(image_handle))
3144 return EFI_EXIT(EFI_INVALID_PARAMETER);
3145
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003146 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003147 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3148 return EFI_EXIT(EFI_INVALID_PARAMETER);
3149
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003150 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3151 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3152
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003153 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3154 &info, NULL, NULL,
3155 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3156 if (ret != EFI_SUCCESS)
3157 return EFI_EXIT(EFI_INVALID_PARAMETER);
3158
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003159 image_obj->exit_data_size = exit_data_size;
3160 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003161 image_obj->exit_status = &exit_status;
3162 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003163
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003164 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3165 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003166 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003167 if (ret == EFI_SECURITY_VIOLATION) {
3168 /*
3169 * TCG2 Protocol is installed but no TPM device found,
3170 * this is not expected.
3171 */
3172 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003173 }
3174 }
3175 }
3176
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003177 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003178 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003179 /*
3180 * We called the entry point of the child image with EFI_CALL
3181 * in the lines below. The child image called the Exit() boot
3182 * service efi_exit() which executed the long jump that brought
3183 * us to the current line. This implies that the second half
3184 * of the EFI_CALL macro has not been executed.
3185 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003186#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003187 /*
3188 * efi_exit() called efi_restore_gd(). We have to undo this
3189 * otherwise __efi_entry_check() will put the wrong value into
3190 * app_gd.
3191 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003192 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003193#endif
3194 /*
3195 * To get ready to call EFI_EXIT below we have to execute the
3196 * missed out steps of EFI_CALL.
3197 */
3198 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003199 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003200 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003201 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003202 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003203 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003204 }
3205
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003206 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003207 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003208 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003209 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3210
3211 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003212 * Control is returned from a started UEFI image either by calling
3213 * Exit() (where exit data can be provided) or by simply returning from
3214 * the entry point. In the latter case call Exit() on behalf of the
3215 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003216 */
3217 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3218}
3219
3220/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003221 * efi_delete_image() - delete loaded image from memory)
3222 *
3223 * @image_obj: handle of the loaded image
3224 * @loaded_image_protocol: loaded image protocol
3225 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003226static efi_status_t efi_delete_image
3227 (struct efi_loaded_image_obj *image_obj,
3228 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003229{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003230 struct efi_object *efiobj;
3231 efi_status_t r, ret = EFI_SUCCESS;
3232
3233close_next:
3234 list_for_each_entry(efiobj, &efi_obj_list, link) {
3235 struct efi_handler *protocol;
3236
3237 list_for_each_entry(protocol, &efiobj->protocols, link) {
3238 struct efi_open_protocol_info_item *info;
3239
3240 list_for_each_entry(info, &protocol->open_infos, link) {
3241 if (info->info.agent_handle !=
3242 (efi_handle_t)image_obj)
3243 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003244 r = efi_close_protocol(
3245 efiobj, &protocol->guid,
3246 info->info.agent_handle,
3247 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003248 if (r != EFI_SUCCESS)
3249 ret = r;
3250 /*
3251 * Closing protocols may results in further
3252 * items being deleted. To play it safe loop
3253 * over all elements again.
3254 */
3255 goto close_next;
3256 }
3257 }
3258 }
3259
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003260 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3261 efi_size_in_pages(loaded_image_protocol->image_size));
3262 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003263
3264 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003265}
3266
3267/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003268 * efi_unload_image() - unload an EFI image
3269 * @image_handle: handle of the image to be unloaded
3270 *
3271 * This function implements the UnloadImage service.
3272 *
3273 * See the Unified Extensible Firmware Interface (UEFI) specification for
3274 * details.
3275 *
3276 * Return: status code
3277 */
3278efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3279{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003280 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003281 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003282 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003283
3284 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003285
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003286 efiobj = efi_search_obj(image_handle);
3287 if (!efiobj) {
3288 ret = EFI_INVALID_PARAMETER;
3289 goto out;
3290 }
3291 /* Find the loaded image protocol */
3292 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3293 (void **)&loaded_image_protocol,
3294 NULL, NULL,
3295 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3296 if (ret != EFI_SUCCESS) {
3297 ret = EFI_INVALID_PARAMETER;
3298 goto out;
3299 }
3300 switch (efiobj->type) {
3301 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3302 /* Call the unload function */
3303 if (!loaded_image_protocol->unload) {
3304 ret = EFI_UNSUPPORTED;
3305 goto out;
3306 }
3307 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3308 if (ret != EFI_SUCCESS)
3309 goto out;
3310 break;
3311 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3312 break;
3313 default:
3314 ret = EFI_INVALID_PARAMETER;
3315 goto out;
3316 }
3317 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3318 loaded_image_protocol);
3319out:
3320 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003321}
3322
3323/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003324 * efi_update_exit_data() - fill exit data parameters of StartImage()
3325 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003326 * @image_obj: image handle
3327 * @exit_data_size: size of the exit data buffer
3328 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003329 * Return: status code
3330 */
3331static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3332 efi_uintn_t exit_data_size,
3333 u16 *exit_data)
3334{
3335 efi_status_t ret;
3336
3337 /*
3338 * If exit_data is not provided to StartImage(), exit_data_size must be
3339 * ignored.
3340 */
3341 if (!image_obj->exit_data)
3342 return EFI_SUCCESS;
3343 if (image_obj->exit_data_size)
3344 *image_obj->exit_data_size = exit_data_size;
3345 if (exit_data_size && exit_data) {
3346 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3347 exit_data_size,
3348 (void **)image_obj->exit_data);
3349 if (ret != EFI_SUCCESS)
3350 return ret;
3351 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3352 } else {
3353 image_obj->exit_data = NULL;
3354 }
3355 return EFI_SUCCESS;
3356}
3357
3358/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003359 * efi_exit() - leave an EFI application or driver
3360 * @image_handle: handle of the application or driver that is exiting
3361 * @exit_status: status code
3362 * @exit_data_size: size of the buffer in bytes
3363 * @exit_data: buffer with data describing an error
3364 *
3365 * This function implements the Exit service.
3366 *
3367 * See the Unified Extensible Firmware Interface (UEFI) specification for
3368 * details.
3369 *
3370 * Return: status code
3371 */
3372static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3373 efi_status_t exit_status,
3374 efi_uintn_t exit_data_size,
3375 u16 *exit_data)
3376{
3377 /*
3378 * TODO: We should call the unload procedure of the loaded
3379 * image protocol.
3380 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003381 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003382 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003383 struct efi_loaded_image_obj *image_obj =
3384 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003385 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003386
3387 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3388 exit_data_size, exit_data);
3389
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003390 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003391 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003392 (void **)&loaded_image_protocol,
3393 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003394 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003395 if (ret != EFI_SUCCESS) {
3396 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003397 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003398 }
3399
3400 /* Unloading of unstarted images */
3401 switch (image_obj->header.type) {
3402 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3403 break;
3404 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3405 efi_delete_image(image_obj, loaded_image_protocol);
3406 ret = EFI_SUCCESS;
3407 goto out;
3408 default:
3409 /* Handle does not refer to loaded image */
3410 ret = EFI_INVALID_PARAMETER;
3411 goto out;
3412 }
3413 /* A started image can only be unloaded it is the last one started. */
3414 if (image_handle != current_image) {
3415 ret = EFI_INVALID_PARAMETER;
3416 goto out;
3417 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003418
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003419 /* Exit data is only foreseen in case of failure. */
3420 if (exit_status != EFI_SUCCESS) {
3421 ret = efi_update_exit_data(image_obj, exit_data_size,
3422 exit_data);
3423 /* Exiting has priority. Don't return error to caller. */
3424 if (ret != EFI_SUCCESS)
3425 EFI_PRINT("%s: out of memory\n", __func__);
3426 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003427 /* efi_delete_image() frees image_obj. Copy before the call. */
3428 exit_jmp = image_obj->exit_jmp;
3429 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003430 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3431 exit_status != EFI_SUCCESS)
3432 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003433
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003434 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3435 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3436 ret = efi_tcg2_measure_efi_app_exit();
3437 if (ret != EFI_SUCCESS) {
3438 log_warning("tcg2 measurement fails(0x%lx)\n",
3439 ret);
3440 }
3441 }
3442 }
3443
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003444 /* Make sure entry/exit counts for EFI world cross-overs match */
3445 EFI_EXIT(exit_status);
3446
3447 /*
3448 * But longjmp out with the U-Boot gd, not the application's, as
3449 * the other end is a setjmp call inside EFI context.
3450 */
3451 efi_restore_gd();
3452
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003453 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003454
3455 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003456out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003457 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003458}
3459
3460/**
Mario Six8fac2912018-07-10 08:40:17 +02003461 * efi_handle_protocol() - get interface of a protocol on a handle
3462 * @handle: handle on which the protocol shall be opened
3463 * @protocol: GUID of the protocol
3464 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003465 *
3466 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003467 *
3468 * See the Unified Extensible Firmware Interface (UEFI) specification for
3469 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003470 *
Mario Six8fac2912018-07-10 08:40:17 +02003471 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003472 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003473efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3474 const efi_guid_t *protocol,
3475 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003476{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003477 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003478 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003479}
3480
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003481/**
Mario Six8fac2912018-07-10 08:40:17 +02003482 * efi_bind_controller() - bind a single driver to a controller
3483 * @controller_handle: controller handle
3484 * @driver_image_handle: driver handle
3485 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003486 *
Mario Six8fac2912018-07-10 08:40:17 +02003487 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003488 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003489static efi_status_t efi_bind_controller(
3490 efi_handle_t controller_handle,
3491 efi_handle_t driver_image_handle,
3492 struct efi_device_path *remain_device_path)
3493{
3494 struct efi_driver_binding_protocol *binding_protocol;
3495 efi_status_t r;
3496
3497 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3498 &efi_guid_driver_binding_protocol,
3499 (void **)&binding_protocol,
3500 driver_image_handle, NULL,
3501 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3502 if (r != EFI_SUCCESS)
3503 return r;
3504 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3505 controller_handle,
3506 remain_device_path));
3507 if (r == EFI_SUCCESS)
3508 r = EFI_CALL(binding_protocol->start(binding_protocol,
3509 controller_handle,
3510 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003511 efi_close_protocol(driver_image_handle,
3512 &efi_guid_driver_binding_protocol,
3513 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003514 return r;
3515}
3516
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003517/**
Mario Six8fac2912018-07-10 08:40:17 +02003518 * efi_connect_single_controller() - connect a single driver to a controller
3519 * @controller_handle: controller
3520 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003521 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003522 *
Mario Six8fac2912018-07-10 08:40:17 +02003523 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003524 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003525static efi_status_t efi_connect_single_controller(
3526 efi_handle_t controller_handle,
3527 efi_handle_t *driver_image_handle,
3528 struct efi_device_path *remain_device_path)
3529{
3530 efi_handle_t *buffer;
3531 size_t count;
3532 size_t i;
3533 efi_status_t r;
3534 size_t connected = 0;
3535
3536 /* Get buffer with all handles with driver binding protocol */
3537 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3538 &efi_guid_driver_binding_protocol,
3539 NULL, &count, &buffer));
3540 if (r != EFI_SUCCESS)
3541 return r;
3542
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003543 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003544 if (driver_image_handle) {
3545 for (; *driver_image_handle; ++driver_image_handle) {
3546 for (i = 0; i < count; ++i) {
3547 if (buffer[i] == *driver_image_handle) {
3548 buffer[i] = NULL;
3549 r = efi_bind_controller(
3550 controller_handle,
3551 *driver_image_handle,
3552 remain_device_path);
3553 /*
3554 * For drivers that do not support the
3555 * controller or are already connected
3556 * we receive an error code here.
3557 */
3558 if (r == EFI_SUCCESS)
3559 ++connected;
3560 }
3561 }
3562 }
3563 }
3564
3565 /*
3566 * TODO: Some overrides are not yet implemented:
3567 * - Platform Driver Override
3568 * - Driver Family Override Search
3569 * - Bus Specific Driver Override
3570 */
3571
3572 /* Driver Binding Search */
3573 for (i = 0; i < count; ++i) {
3574 if (buffer[i]) {
3575 r = efi_bind_controller(controller_handle,
3576 buffer[i],
3577 remain_device_path);
3578 if (r == EFI_SUCCESS)
3579 ++connected;
3580 }
3581 }
3582
3583 efi_free_pool(buffer);
3584 if (!connected)
3585 return EFI_NOT_FOUND;
3586 return EFI_SUCCESS;
3587}
3588
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003589/**
Mario Six8fac2912018-07-10 08:40:17 +02003590 * efi_connect_controller() - connect a controller to a driver
3591 * @controller_handle: handle of the controller
3592 * @driver_image_handle: handle of the driver
3593 * @remain_device_path: device path of a child controller
3594 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003595 *
3596 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003597 *
3598 * See the Unified Extensible Firmware Interface (UEFI) specification for
3599 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003600 *
3601 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003602 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003603 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3604 *
Mario Six8fac2912018-07-10 08:40:17 +02003605 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003606 */
3607static efi_status_t EFIAPI efi_connect_controller(
3608 efi_handle_t controller_handle,
3609 efi_handle_t *driver_image_handle,
3610 struct efi_device_path *remain_device_path,
3611 bool recursive)
3612{
3613 efi_status_t r;
3614 efi_status_t ret = EFI_NOT_FOUND;
3615 struct efi_object *efiobj;
3616
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003617 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003618 remain_device_path, recursive);
3619
3620 efiobj = efi_search_obj(controller_handle);
3621 if (!efiobj) {
3622 ret = EFI_INVALID_PARAMETER;
3623 goto out;
3624 }
3625
3626 r = efi_connect_single_controller(controller_handle,
3627 driver_image_handle,
3628 remain_device_path);
3629 if (r == EFI_SUCCESS)
3630 ret = EFI_SUCCESS;
3631 if (recursive) {
3632 struct efi_handler *handler;
3633 struct efi_open_protocol_info_item *item;
3634
3635 list_for_each_entry(handler, &efiobj->protocols, link) {
3636 list_for_each_entry(item, &handler->open_infos, link) {
3637 if (item->info.attributes &
3638 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3639 r = EFI_CALL(efi_connect_controller(
3640 item->info.controller_handle,
3641 driver_image_handle,
3642 remain_device_path,
3643 recursive));
3644 if (r == EFI_SUCCESS)
3645 ret = EFI_SUCCESS;
3646 }
3647 }
3648 }
3649 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003650 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003651 if (ret != EFI_SUCCESS && remain_device_path &&
3652 remain_device_path->type == DEVICE_PATH_TYPE_END)
3653 ret = EFI_SUCCESS;
3654out:
3655 return EFI_EXIT(ret);
3656}
3657
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003658/**
Mario Six8fac2912018-07-10 08:40:17 +02003659 * efi_reinstall_protocol_interface() - reinstall protocol interface
3660 * @handle: handle on which the protocol shall be reinstalled
3661 * @protocol: GUID of the protocol to be installed
3662 * @old_interface: interface to be removed
3663 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003664 *
3665 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003666 *
3667 * See the Unified Extensible Firmware Interface (UEFI) specification for
3668 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003669 *
3670 * The old interface is uninstalled. The new interface is installed.
3671 * Drivers are connected.
3672 *
Mario Six8fac2912018-07-10 08:40:17 +02003673 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003674 */
3675static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3676 efi_handle_t handle, const efi_guid_t *protocol,
3677 void *old_interface, void *new_interface)
3678{
3679 efi_status_t ret;
3680
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003681 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003682 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003683
3684 /* Uninstall protocol but do not delete handle */
3685 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003686 if (ret != EFI_SUCCESS)
3687 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003688
3689 /* Install the new protocol */
3690 ret = efi_add_protocol(handle, protocol, new_interface);
3691 /*
3692 * The UEFI spec does not specify what should happen to the handle
3693 * if in case of an error no protocol interface remains on the handle.
3694 * So let's do nothing here.
3695 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003696 if (ret != EFI_SUCCESS)
3697 goto out;
3698 /*
3699 * The returned status code has to be ignored.
3700 * Do not create an error if no suitable driver for the handle exists.
3701 */
3702 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3703out:
3704 return EFI_EXIT(ret);
3705}
3706
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003707/**
Mario Six8fac2912018-07-10 08:40:17 +02003708 * efi_get_child_controllers() - get all child controllers associated to a driver
3709 * @efiobj: handle of the controller
3710 * @driver_handle: handle of the driver
3711 * @number_of_children: number of child controllers
3712 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003713 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003714 * The allocated buffer has to be freed with free().
3715 *
Mario Six8fac2912018-07-10 08:40:17 +02003716 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003717 */
3718static efi_status_t efi_get_child_controllers(
3719 struct efi_object *efiobj,
3720 efi_handle_t driver_handle,
3721 efi_uintn_t *number_of_children,
3722 efi_handle_t **child_handle_buffer)
3723{
3724 struct efi_handler *handler;
3725 struct efi_open_protocol_info_item *item;
3726 efi_uintn_t count = 0, i;
3727 bool duplicate;
3728
3729 /* Count all child controller associations */
3730 list_for_each_entry(handler, &efiobj->protocols, link) {
3731 list_for_each_entry(item, &handler->open_infos, link) {
3732 if (item->info.agent_handle == driver_handle &&
3733 item->info.attributes &
3734 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3735 ++count;
3736 }
3737 }
3738 /*
3739 * Create buffer. In case of duplicate child controller assignments
3740 * the buffer will be too large. But that does not harm.
3741 */
3742 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003743 if (!count)
3744 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003745 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3746 if (!*child_handle_buffer)
3747 return EFI_OUT_OF_RESOURCES;
3748 /* Copy unique child handles */
3749 list_for_each_entry(handler, &efiobj->protocols, link) {
3750 list_for_each_entry(item, &handler->open_infos, link) {
3751 if (item->info.agent_handle == driver_handle &&
3752 item->info.attributes &
3753 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3754 /* Check this is a new child controller */
3755 duplicate = false;
3756 for (i = 0; i < *number_of_children; ++i) {
3757 if ((*child_handle_buffer)[i] ==
3758 item->info.controller_handle)
3759 duplicate = true;
3760 }
3761 /* Copy handle to buffer */
3762 if (!duplicate) {
3763 i = (*number_of_children)++;
3764 (*child_handle_buffer)[i] =
3765 item->info.controller_handle;
3766 }
3767 }
3768 }
3769 }
3770 return EFI_SUCCESS;
3771}
3772
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003773/**
Mario Six8fac2912018-07-10 08:40:17 +02003774 * efi_disconnect_controller() - disconnect a controller from a driver
3775 * @controller_handle: handle of the controller
3776 * @driver_image_handle: handle of the driver
3777 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003778 *
3779 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003780 *
3781 * See the Unified Extensible Firmware Interface (UEFI) specification for
3782 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003783 *
Mario Six8fac2912018-07-10 08:40:17 +02003784 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003785 */
3786static efi_status_t EFIAPI efi_disconnect_controller(
3787 efi_handle_t controller_handle,
3788 efi_handle_t driver_image_handle,
3789 efi_handle_t child_handle)
3790{
3791 struct efi_driver_binding_protocol *binding_protocol;
3792 efi_handle_t *child_handle_buffer = NULL;
3793 size_t number_of_children = 0;
3794 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003795 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003796 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003797
3798 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3799 child_handle);
3800
3801 efiobj = efi_search_obj(controller_handle);
3802 if (!efiobj) {
3803 r = EFI_INVALID_PARAMETER;
3804 goto out;
3805 }
3806
3807 if (child_handle && !efi_search_obj(child_handle)) {
3808 r = EFI_INVALID_PARAMETER;
3809 goto out;
3810 }
3811
3812 /* If no driver handle is supplied, disconnect all drivers */
3813 if (!driver_image_handle) {
3814 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3815 goto out;
3816 }
3817
3818 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003819 r = efi_get_child_controllers(efiobj,
3820 driver_image_handle,
3821 &number_of_children,
3822 &child_handle_buffer);
3823 if (r != EFI_SUCCESS)
3824 return r;
3825 sole_child = (number_of_children == 1);
3826
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003827 if (child_handle) {
3828 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003829 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003830 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003831 }
3832
3833 /* Get the driver binding protocol */
3834 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3835 &efi_guid_driver_binding_protocol,
3836 (void **)&binding_protocol,
3837 driver_image_handle, NULL,
3838 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003839 if (r != EFI_SUCCESS) {
3840 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003841 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003842 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003843 /* Remove the children */
3844 if (number_of_children) {
3845 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3846 controller_handle,
3847 number_of_children,
3848 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003849 if (r != EFI_SUCCESS) {
3850 r = EFI_DEVICE_ERROR;
3851 goto out;
3852 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003853 }
3854 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003855 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003856 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3857 controller_handle,
3858 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003859 if (r != EFI_SUCCESS) {
3860 r = EFI_DEVICE_ERROR;
3861 goto out;
3862 }
3863 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003864 efi_close_protocol(driver_image_handle,
3865 &efi_guid_driver_binding_protocol,
3866 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003867 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003868out:
3869 if (!child_handle)
3870 free(child_handle_buffer);
3871 return EFI_EXIT(r);
3872}
3873
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003874static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003875 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003876 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3877 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003878 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003879 },
3880 .raise_tpl = efi_raise_tpl,
3881 .restore_tpl = efi_restore_tpl,
3882 .allocate_pages = efi_allocate_pages_ext,
3883 .free_pages = efi_free_pages_ext,
3884 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003885 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003886 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003887 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003888 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003889 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003890 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003891 .close_event = efi_close_event,
3892 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003893 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003894 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003895 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003896 .handle_protocol = efi_handle_protocol,
3897 .reserved = NULL,
3898 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003899 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003900 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003901 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003902 .load_image = efi_load_image,
3903 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003904 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003905 .unload_image = efi_unload_image,
3906 .exit_boot_services = efi_exit_boot_services,
3907 .get_next_monotonic_count = efi_get_next_monotonic_count,
3908 .stall = efi_stall,
3909 .set_watchdog_timer = efi_set_watchdog_timer,
3910 .connect_controller = efi_connect_controller,
3911 .disconnect_controller = efi_disconnect_controller,
3912 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003913 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003914 .open_protocol_information = efi_open_protocol_information,
3915 .protocols_per_handle = efi_protocols_per_handle,
3916 .locate_handle_buffer = efi_locate_handle_buffer,
3917 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003918 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003919 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003920 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003921 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003922 .calculate_crc32 = efi_calculate_crc32,
3923 .copy_mem = efi_copy_mem,
3924 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003925 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003926};
3927
Simon Glass90975372022-01-23 12:55:12 -07003928static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003929
Alexander Graf393dd912016-10-14 13:45:30 +02003930struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003931 .hdr = {
3932 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003933 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003934 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003935 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003936 .fw_vendor = firmware_vendor,
3937 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003938 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003939 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003940 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003941};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003942
3943/**
3944 * efi_initialize_system_table() - Initialize system table
3945 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003946 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003947 */
3948efi_status_t efi_initialize_system_table(void)
3949{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003950 efi_status_t ret;
3951
3952 /* Allocate configuration table array */
3953 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3954 EFI_MAX_CONFIGURATION_TABLES *
3955 sizeof(struct efi_configuration_table),
3956 (void **)&systab.tables);
3957
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003958 /*
3959 * These entries will be set to NULL in ExitBootServices(). To avoid
3960 * relocation in SetVirtualAddressMap(), set them dynamically.
3961 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01003962 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003963 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003964 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003965 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003966 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003967 systab.std_err = &efi_con_out;
3968 systab.boottime = &efi_boot_services;
3969
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003970 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003971 efi_update_table_header_crc32(&systab.hdr);
3972 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3973 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003974
3975 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003976}