blob: eaa6464fa393a5a6520d52b71eeb8ca81eb9dee3 [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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030010#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020011#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030012#include <dm/device.h>
13#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070015#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010017#include <malloc.h>
Heinrich Schuchardt20047fd2025-04-05 08:58:12 +020018#include <net-common.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020019#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030020#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030022#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010023#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090025#include <asm/setjmp.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030026#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010027
28DECLARE_GLOBAL_DATA_PTR;
29
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020030/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010031static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020032
Alexander Grafc15d9212016-03-04 01:09:59 +010033/* This list contains all the EFI objects our payload has access to */
34LIST_HEAD(efi_obj_list);
35
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010036/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020037__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010038
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020039/* List of queued events */
Bin Mengd89dea32023-04-05 20:15:17 +080040static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020041
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020042/* Flag to disable timer activity in ExitBootServices() */
43static bool timers_enabled = true;
44
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010045/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
46bool efi_st_keep_devices;
47
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020048/* List of all events registered by RegisterProtocolNotify() */
Bin Mengd89dea32023-04-05 20:15:17 +080049static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020050
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010051/* Handle of the currently executing image */
52static efi_handle_t current_image;
53
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020054#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010055/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020056 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010057 * fixed when compiling U-Boot. However, the payload does not know about that
58 * restriction so we need to manually swap its and our view of that register on
59 * EFI callback entry/exit.
60 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020061static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060062#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010063
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030064static efi_status_t efi_uninstall_protocol
65 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +030066 void *protocol_interface, bool preserve);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030067
Heinrich Schuchardt72506232019-02-09 14:10:39 +010068/* 1 if inside U-Boot code, 0 if inside EFI payload code */
69static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040070static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010071/* GUID of the device tree table */
72const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010073/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
74const efi_guid_t efi_guid_driver_binding_protocol =
75 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040076
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010077/* event group ExitBootServices() invoked */
78const efi_guid_t efi_guid_event_group_exit_boot_services =
79 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt44772c42021-11-16 18:46:27 +010080/* event group before ExitBootServices() invoked */
81const efi_guid_t efi_guid_event_group_before_exit_boot_services =
82 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010083/* event group SetVirtualAddressMap() invoked */
84const efi_guid_t efi_guid_event_group_virtual_address_change =
85 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
86/* event group memory map changed */
87const efi_guid_t efi_guid_event_group_memory_map_change =
88 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
89/* event group boot manager about to boot */
90const efi_guid_t efi_guid_event_group_ready_to_boot =
91 EFI_EVENT_GROUP_READY_TO_BOOT;
92/* event group ResetSystem() invoked (before ExitBootServices) */
93const efi_guid_t efi_guid_event_group_reset_system =
94 EFI_EVENT_GROUP_RESET_SYSTEM;
Masahisa Kojima1923f362023-11-10 13:25:39 +090095/* event group return to efibootmgr */
96const efi_guid_t efi_guid_event_group_return_to_efibootmgr =
97 EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010098/* GUIDs of the Load File and Load File2 protocols */
99const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
100const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900101/* GUID of the SMBIOS table */
102const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +0100103
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100104static efi_status_t EFIAPI efi_disconnect_controller(
105 efi_handle_t controller_handle,
106 efi_handle_t driver_image_handle,
107 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +0100108
Ilias Apalodimas1e10d622023-06-20 09:19:28 +0300109static
110efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
111 efi_handle_t *driver_image_handle,
112 struct efi_device_path *remain_device_path,
113 bool recursive);
114
Rob Clark86789d52017-07-27 08:04:18 -0400115/* Called on every callback entry */
116int __efi_entry_check(void)
117{
118 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200119#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400120 assert(efi_gd);
121 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200122 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400123#endif
124 return ret;
125}
126
127/* Called on every callback exit */
128int __efi_exit_check(void)
129{
130 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200131#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200132 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400133#endif
134 return ret;
135}
136
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200137/**
138 * efi_save_gd() - save global data register
139 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200140 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200141 * As this register may be overwritten by an EFI payload we save it here
142 * and restore it on every callback entered.
143 *
144 * This function is called after relocation from initr_reloc_global_data().
145 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100146void efi_save_gd(void)
147{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200148#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100149 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600150#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100151}
152
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200153/**
154 * efi_restore_gd() - restore global data register
155 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200156 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200157 * Restore it after returning from the UEFI world to the value saved via
158 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400159 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100160void efi_restore_gd(void)
161{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200162#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100163 /* Only restore if we're already in EFI context */
164 if (!efi_gd)
165 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200166 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600167#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100168}
169
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200170/**
Mario Six8fac2912018-07-10 08:40:17 +0200171 * indent_string() - returns a string for indenting with two spaces per level
172 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100173 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200174 * A maximum of ten indent levels is supported. Higher indent levels will be
175 * truncated.
176 *
Mario Six8fac2912018-07-10 08:40:17 +0200177 * Return: A string for indenting with two spaces per level is
178 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400179 */
180static const char *indent_string(int level)
181{
182 const char *indent = " ";
183 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100184
Rob Clarke7896c32017-07-27 08:04:19 -0400185 level = min(max, level * 2);
186 return &indent[max - level];
187}
188
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200189const char *__efi_nesting(void)
190{
191 return indent_string(nesting_level);
192}
193
Rob Clarke7896c32017-07-27 08:04:19 -0400194const char *__efi_nesting_inc(void)
195{
196 return indent_string(nesting_level++);
197}
198
199const char *__efi_nesting_dec(void)
200{
201 return indent_string(--nesting_level);
202}
203
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200204/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200205 * efi_event_is_queued() - check if an event is queued
206 *
207 * @event: event
208 * Return: true if event is queued
209 */
210static bool efi_event_is_queued(struct efi_event *event)
211{
212 return !!event->queue_link.next;
213}
214
215/**
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300216 * efi_purge_handle() - Clean the deleted handle from the various lists
217 * @handle: handle to remove
218 *
219 * Return: status code
220 */
221static efi_status_t efi_purge_handle(efi_handle_t handle)
222{
223 struct efi_register_notify_event *item;
224
225 if (!list_empty(&handle->protocols))
226 return EFI_ACCESS_DENIED;
227 /* The handle is about to be freed. Remove it from events */
228 list_for_each_entry(item, &efi_register_notify_events, link) {
229 struct efi_protocol_notification *hitem, *hnext;
230
231 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
232 if (handle == hitem->handle) {
233 list_del(&hitem->link);
234 free(hitem);
235 }
236 }
237 }
238 /* The last protocol has been removed, delete the handle. */
239 list_del(&handle->link);
240 free(handle);
241
242 return EFI_SUCCESS;
243}
244
245/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200246 * efi_process_event_queue() - process event queue
247 */
248static void efi_process_event_queue(void)
249{
250 while (!list_empty(&efi_event_queue)) {
251 struct efi_event *event;
252 efi_uintn_t old_tpl;
253
254 event = list_first_entry(&efi_event_queue, struct efi_event,
255 queue_link);
256 if (efi_tpl >= event->notify_tpl)
257 return;
258 list_del(&event->queue_link);
259 event->queue_link.next = NULL;
260 event->queue_link.prev = NULL;
261 /* Events must be executed at the event's TPL */
262 old_tpl = efi_tpl;
263 efi_tpl = event->notify_tpl;
264 EFI_CALL_VOID(event->notify_function(event,
265 event->notify_context));
266 efi_tpl = old_tpl;
267 if (event->type == EVT_NOTIFY_SIGNAL)
268 event->is_signaled = 0;
269 }
270}
271
272/**
Mario Six8fac2912018-07-10 08:40:17 +0200273 * efi_queue_event() - queue an EFI event
274 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200275 *
276 * This function queues the notification function of the event for future
277 * execution.
278 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200279 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200280static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200281{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100282 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200283
284 if (!event->notify_function)
285 return;
286
287 if (!efi_event_is_queued(event)) {
288 /*
289 * Events must be notified in order of decreasing task priority
290 * level. Insert the new event accordingly.
291 */
292 list_for_each_entry(item, &efi_event_queue, queue_link) {
293 if (item->notify_tpl < event->notify_tpl) {
294 list_add_tail(&event->queue_link,
295 &item->queue_link);
296 event = NULL;
297 break;
298 }
299 }
300 if (event)
301 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100302 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200303 }
304}
305
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200306/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200307 * is_valid_tpl() - check if the task priority level is valid
308 *
309 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200310 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200311 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100312static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200313{
314 switch (tpl) {
315 case TPL_APPLICATION:
316 case TPL_CALLBACK:
317 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200318 return EFI_SUCCESS;
319 default:
320 return EFI_INVALID_PARAMETER;
321 }
322}
323
324/**
Mario Six8fac2912018-07-10 08:40:17 +0200325 * efi_signal_event() - signal an EFI event
326 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100327 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100328 * This function signals an event. If the event belongs to an event group, all
329 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100330 * their notification function is queued.
331 *
332 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100333 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200334void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100335{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200336 if (event->is_signaled)
337 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100338 if (event->group) {
339 struct efi_event *evt;
340
341 /*
342 * The signaled state has to set before executing any
343 * notification function
344 */
345 list_for_each_entry(evt, &efi_events, link) {
346 if (!evt->group || guidcmp(evt->group, event->group))
347 continue;
348 if (evt->is_signaled)
349 continue;
350 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100351 }
352 list_for_each_entry(evt, &efi_events, link) {
353 if (!evt->group || guidcmp(evt->group, event->group))
354 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200355 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100356 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200357 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100358 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200359 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100360 }
361}
362
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200363/**
Mario Six8fac2912018-07-10 08:40:17 +0200364 * efi_raise_tpl() - raise the task priority level
365 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 *
367 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200368 *
369 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200371 *
Mario Six8fac2912018-07-10 08:40:17 +0200372 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100374static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100375{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100376 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200377
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200378 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200379
380 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200381 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200382 efi_tpl = new_tpl;
383 if (efi_tpl > TPL_HIGH_LEVEL)
384 efi_tpl = TPL_HIGH_LEVEL;
385
386 EFI_EXIT(EFI_SUCCESS);
387 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100388}
389
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200390/**
Mario Six8fac2912018-07-10 08:40:17 +0200391 * efi_restore_tpl() - lower the task priority level
392 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200393 *
394 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
Mario Six8fac2912018-07-10 08:40:17 +0200396 * See the Unified Extensible Firmware Interface (UEFI) specification for
397 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200398 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100399static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100400{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200401 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200402
403 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200404 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200405 efi_tpl = old_tpl;
406 if (efi_tpl > TPL_HIGH_LEVEL)
407 efi_tpl = TPL_HIGH_LEVEL;
408
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100409 /*
410 * Lowering the TPL may have made queued events eligible for execution.
411 */
412 efi_timer_check();
413
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200414 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100415}
416
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200417/**
Mario Six8fac2912018-07-10 08:40:17 +0200418 * efi_allocate_pages_ext() - allocate memory pages
419 * @type: type of allocation to be performed
420 * @memory_type: usage type of the allocated memory
421 * @pages: number of pages to be allocated
422 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 *
424 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200425 *
Mario Six8fac2912018-07-10 08:40:17 +0200426 * See the Unified Extensible Firmware Interface (UEFI) specification for
427 * details.
428 *
429 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200430 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900431static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100432 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900433 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100434{
435 efi_status_t r;
436
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100437 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100438 r = efi_allocate_pages(type, memory_type, pages, memory);
439 return EFI_EXIT(r);
440}
441
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200442/**
Mario Six8fac2912018-07-10 08:40:17 +0200443 * efi_free_pages_ext() - Free memory pages.
444 * @memory: start of the memory area to be freed
445 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200446 *
447 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200448 *
449 * See the Unified Extensible Firmware Interface (UEFI) specification for
450 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200451 *
Mario Six8fac2912018-07-10 08:40:17 +0200452 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200453 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900454static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100455 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100456{
457 efi_status_t r;
458
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900459 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100460 r = efi_free_pages(memory, pages);
461 return EFI_EXIT(r);
462}
463
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200464/**
Mario Six8fac2912018-07-10 08:40:17 +0200465 * efi_get_memory_map_ext() - get map describing memory usage
466 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
467 * on exit the size of the copied memory map
468 * @memory_map: buffer to which the memory map is written
469 * @map_key: key for the memory map
470 * @descriptor_size: size of an individual memory descriptor
471 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200472 *
473 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200474 *
Mario Six8fac2912018-07-10 08:40:17 +0200475 * See the Unified Extensible Firmware Interface (UEFI) specification for
476 * details.
477 *
478 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200479 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900480static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100481 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900482 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100483 efi_uintn_t *map_key,
484 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900485 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100486{
487 efi_status_t r;
488
489 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
490 map_key, descriptor_size, descriptor_version);
491 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
492 descriptor_size, descriptor_version);
493 return EFI_EXIT(r);
494}
495
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200496/**
Mario Six8fac2912018-07-10 08:40:17 +0200497 * efi_allocate_pool_ext() - allocate memory from pool
498 * @pool_type: type of the pool from which memory is to be allocated
499 * @size: number of bytes to be allocated
500 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200501 *
502 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200503 *
504 * See the Unified Extensible Firmware Interface (UEFI) specification for
505 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200506 *
Mario Six8fac2912018-07-10 08:40:17 +0200507 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200508 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200509static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100510 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200511 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100512{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100513 efi_status_t r;
514
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100515 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200516 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100517 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100518}
519
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200520/**
Mario Six8fac2912018-07-10 08:40:17 +0200521 * efi_free_pool_ext() - free memory from pool
522 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200523 *
524 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200525 *
Mario Six8fac2912018-07-10 08:40:17 +0200526 * See the Unified Extensible Firmware Interface (UEFI) specification for
527 * details.
528 *
529 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200530 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200531static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100532{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100533 efi_status_t r;
534
535 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200536 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100537 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100538}
539
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200540/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200541 * efi_add_handle() - add a new handle to the object list
542 *
543 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100544 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200545 * The protocols list is initialized. The handle is added to the list of known
546 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100547 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200548void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100549{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200550 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100551 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200552 INIT_LIST_HEAD(&handle->protocols);
553 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100554}
555
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200556/**
Mario Six8fac2912018-07-10 08:40:17 +0200557 * efi_create_handle() - create handle
558 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200559 *
Mario Six8fac2912018-07-10 08:40:17 +0200560 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200561 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100562efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200563{
564 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200565
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200566 obj = calloc(1, sizeof(struct efi_object));
567 if (!obj)
568 return EFI_OUT_OF_RESOURCES;
569
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100570 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200571 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200572
573 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200574}
575
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200576/**
Mario Six8fac2912018-07-10 08:40:17 +0200577 * efi_search_protocol() - find a protocol on a handle.
578 * @handle: handle
579 * @protocol_guid: GUID of the protocol
580 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581 *
Mario Six8fac2912018-07-10 08:40:17 +0200582 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100583 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100584efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100585 const efi_guid_t *protocol_guid,
586 struct efi_handler **handler)
587{
588 struct efi_object *efiobj;
589 struct list_head *lhandle;
590
591 if (!handle || !protocol_guid)
592 return EFI_INVALID_PARAMETER;
593 efiobj = efi_search_obj(handle);
594 if (!efiobj)
595 return EFI_INVALID_PARAMETER;
596 list_for_each(lhandle, &efiobj->protocols) {
597 struct efi_handler *protocol;
598
599 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100600 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100601 if (handler)
602 *handler = protocol;
603 return EFI_SUCCESS;
604 }
605 }
606 return EFI_NOT_FOUND;
607}
608
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200609/**
Mario Six8fac2912018-07-10 08:40:17 +0200610 * efi_remove_protocol() - delete protocol from a handle
611 * @handle: handle from which the protocol shall be deleted
612 * @protocol: GUID of the protocol to be deleted
613 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100614 *
Mario Six8fac2912018-07-10 08:40:17 +0200615 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100616 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300617static efi_status_t efi_remove_protocol(const efi_handle_t handle,
618 const efi_guid_t *protocol,
619 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100620{
621 struct efi_handler *handler;
622 efi_status_t ret;
623
624 ret = efi_search_protocol(handle, protocol, &handler);
625 if (ret != EFI_SUCCESS)
626 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200627 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200628 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100629 list_del(&handler->link);
630 free(handler);
631 return EFI_SUCCESS;
632}
633
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200634/**
Mario Six8fac2912018-07-10 08:40:17 +0200635 * efi_remove_all_protocols() - delete all protocols from a handle
636 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100637 *
Mario Six8fac2912018-07-10 08:40:17 +0200638 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100639 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100640static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100641{
642 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100643 struct efi_handler *protocol;
644 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100645
646 efiobj = efi_search_obj(handle);
647 if (!efiobj)
648 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100649 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100650 efi_status_t ret;
651
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300652 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300653 protocol->protocol_interface, true);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100654 if (ret != EFI_SUCCESS)
655 return ret;
656 }
657 return EFI_SUCCESS;
658}
659
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200660/**
Mario Six8fac2912018-07-10 08:40:17 +0200661 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100662 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200663 * @handle: handle to delete
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300664 *
665 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100666 */
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300667efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100668{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200669 efi_status_t ret;
670
671 ret = efi_remove_all_protocols(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300672 if (ret != EFI_SUCCESS) {
673 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
674 return ret;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200675 }
676
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300677 return efi_purge_handle(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100678}
679
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200680/**
Mario Six8fac2912018-07-10 08:40:17 +0200681 * efi_is_event() - check if a pointer is a valid event
682 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100683 *
Mario Six8fac2912018-07-10 08:40:17 +0200684 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100685 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100686static efi_status_t efi_is_event(const struct efi_event *event)
687{
688 const struct efi_event *evt;
689
690 if (!event)
691 return EFI_INVALID_PARAMETER;
692 list_for_each_entry(evt, &efi_events, link) {
693 if (evt == event)
694 return EFI_SUCCESS;
695 }
696 return EFI_INVALID_PARAMETER;
697}
Alexander Grafc15d9212016-03-04 01:09:59 +0100698
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200699/**
Mario Six8fac2912018-07-10 08:40:17 +0200700 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200701 *
Mario Six8fac2912018-07-10 08:40:17 +0200702 * @type: type of the event to create
703 * @notify_tpl: task priority level of the event
704 * @notify_function: notification function of the event
705 * @notify_context: pointer passed to the notification function
706 * @group: event group
707 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200708 *
709 * This function is used inside U-Boot code to create an event.
710 *
711 * For the API function implementing the CreateEvent service see
712 * efi_create_event_ext.
713 *
Mario Six8fac2912018-07-10 08:40:17 +0200714 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200715 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100716efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200717 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200718 struct efi_event *event,
719 void *context),
Masahisa Kojima787774a2023-11-10 13:25:38 +0900720 void *notify_context, const efi_guid_t *group,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100721 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100722{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100723 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200724 efi_status_t ret;
725 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200726
Jonathan Gray7758b212017-03-12 19:26:07 +1100727 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200728 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100729
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200730 switch (type) {
731 case 0:
732 case EVT_TIMER:
733 case EVT_NOTIFY_SIGNAL:
734 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
735 case EVT_NOTIFY_WAIT:
736 case EVT_TIMER | EVT_NOTIFY_WAIT:
737 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200738 pool_type = EFI_BOOT_SERVICES_DATA;
739 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200740 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200741 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200742 break;
743 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200744 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200745 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100746
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100747 /*
748 * The UEFI specification requires event notification levels to be
749 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
750 *
751 * Parameter NotifyTpl should not be checked if it is not used.
752 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900753 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100754 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
755 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200756 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100757
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200758 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
759 (void **)&evt);
760 if (ret != EFI_SUCCESS)
761 return ret;
762 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100763 evt->type = type;
764 evt->notify_tpl = notify_tpl;
765 evt->notify_function = notify_function;
766 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100767 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200768 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100769 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100770 list_add_tail(&evt->link, &efi_events);
771 *event = evt;
772 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100773}
774
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200775/*
Mario Six8fac2912018-07-10 08:40:17 +0200776 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100777 *
Mario Six8fac2912018-07-10 08:40:17 +0200778 * @type: type of the event to create
779 * @notify_tpl: task priority level of the event
780 * @notify_function: notification function of the event
781 * @notify_context: pointer passed to the notification function
782 * @event: created event
783 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100784 *
785 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100786 *
Mario Six8fac2912018-07-10 08:40:17 +0200787 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * details.
789 *
790 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100791 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100792static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100793efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
794 void (EFIAPI *notify_function) (
795 struct efi_event *event,
796 void *context),
797 void *notify_context,
Masahisa Kojima787774a2023-11-10 13:25:38 +0900798 const efi_guid_t *event_group,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100799 struct efi_event **event)
800{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200801 efi_status_t ret;
802
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100803 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100804 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200805
806 /*
807 * The allowable input parameters are the same as in CreateEvent()
808 * except for the following two disallowed event types.
809 */
810 switch (type) {
811 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
812 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
813 ret = EFI_INVALID_PARAMETER;
814 goto out;
815 }
816
817 ret = efi_create_event(type, notify_tpl, notify_function,
818 notify_context, event_group, event);
819out:
820 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100821}
822
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200823/**
Mario Six8fac2912018-07-10 08:40:17 +0200824 * efi_create_event_ext() - create an event
825 * @type: type of the event to create
826 * @notify_tpl: task priority level of the event
827 * @notify_function: notification function of the event
828 * @notify_context: pointer passed to the notification function
829 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200830 *
831 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200832 *
833 * See the Unified Extensible Firmware Interface (UEFI) specification for
834 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200835 *
Mario Six8fac2912018-07-10 08:40:17 +0200836 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200837 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200838static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100839 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200840 void (EFIAPI *notify_function) (
841 struct efi_event *event,
842 void *context),
843 void *notify_context, struct efi_event **event)
844{
845 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
846 notify_context);
847 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100848 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200849}
850
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200851/**
Mario Six8fac2912018-07-10 08:40:17 +0200852 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200853 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200854 * Check if a timer event has occurred or a queued notification function should
855 * be called.
856 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100857 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200858 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100859 */
860void efi_timer_check(void)
861{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100862 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100863 u64 now = timer_get_us();
864
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100865 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200866 if (!timers_enabled)
867 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100868 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200869 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100870 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200871 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100872 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200873 break;
874 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100875 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200876 break;
877 default:
878 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200879 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100880 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200881 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100882 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200883 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200884 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100885}
886
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200887/**
Mario Six8fac2912018-07-10 08:40:17 +0200888 * efi_set_timer() - set the trigger time for a timer event or stop the event
889 * @event: event for which the timer is set
890 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200891 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200892 *
893 * This is the function for internal usage in U-Boot. For the API function
894 * implementing the SetTimer service see efi_set_timer_ext.
895 *
Mario Six8fac2912018-07-10 08:40:17 +0200896 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200897 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200898efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200899 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100900{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100901 /* Check that the event is valid */
902 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
903 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100904
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200905 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200906 * The parameter defines a multiple of 100 ns.
907 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200908 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200909 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100910
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100911 switch (type) {
912 case EFI_TIMER_STOP:
913 event->trigger_next = -1ULL;
914 break;
915 case EFI_TIMER_PERIODIC:
916 case EFI_TIMER_RELATIVE:
917 event->trigger_next = timer_get_us() + trigger_time;
918 break;
919 default:
920 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100921 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100922 event->trigger_type = type;
923 event->trigger_time = trigger_time;
924 event->is_signaled = false;
925 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100926}
927
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200928/**
Mario Six8fac2912018-07-10 08:40:17 +0200929 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
930 * event
931 * @event: event for which the timer is set
932 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200933 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200934 *
935 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200936 *
Mario Six8fac2912018-07-10 08:40:17 +0200937 * See the Unified Extensible Firmware Interface (UEFI) specification for
938 * details.
939 *
940 *
941 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200942 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200943static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
944 enum efi_timer_delay type,
945 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200946{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900947 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200948 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
949}
950
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200951/**
Mario Six8fac2912018-07-10 08:40:17 +0200952 * efi_wait_for_event() - wait for events to be signaled
953 * @num_events: number of events to be waited for
954 * @event: events to be waited for
955 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200956 *
957 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200958 *
Mario Six8fac2912018-07-10 08:40:17 +0200959 * See the Unified Extensible Firmware Interface (UEFI) specification for
960 * details.
961 *
962 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200963 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100964static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200965 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100966 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100967{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100968 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100969
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100970 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100971
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200972 /* Check parameters */
973 if (!num_events || !event)
974 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200975 /* Check TPL */
976 if (efi_tpl != TPL_APPLICATION)
977 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200978 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100979 if (efi_is_event(event[i]) != EFI_SUCCESS)
980 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200981 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
982 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200983 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200984 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200985 }
986
987 /* Wait for signal */
988 for (;;) {
989 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200990 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200991 goto out;
992 }
993 /* Allow events to occur. */
994 efi_timer_check();
995 }
996
997out:
998 /*
999 * Reset the signal which is passed to the caller to allow periodic
1000 * events to occur.
1001 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +02001002 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001003 if (index)
1004 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +01001005
1006 return EFI_EXIT(EFI_SUCCESS);
1007}
1008
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001009/**
Mario Six8fac2912018-07-10 08:40:17 +02001010 * efi_signal_event_ext() - signal an EFI event
1011 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001012 *
1013 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001014 *
1015 * See the Unified Extensible Firmware Interface (UEFI) specification for
1016 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001017 *
1018 * This functions sets the signaled state of the event and queues the
1019 * notification function for execution.
1020 *
Mario Six8fac2912018-07-10 08:40:17 +02001021 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001022 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001023static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001024{
1025 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001026 if (efi_is_event(event) != EFI_SUCCESS)
1027 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001028 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +01001029 return EFI_EXIT(EFI_SUCCESS);
1030}
1031
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001032/**
Mario Six8fac2912018-07-10 08:40:17 +02001033 * efi_close_event() - close an EFI event
1034 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001035 *
1036 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001037 *
Mario Six8fac2912018-07-10 08:40:17 +02001038 * See the Unified Extensible Firmware Interface (UEFI) specification for
1039 * details.
1040 *
1041 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001042 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001043static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001044{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001045 struct efi_register_notify_event *item, *next;
1046
Alexander Grafc15d9212016-03-04 01:09:59 +01001047 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001048 if (efi_is_event(event) != EFI_SUCCESS)
1049 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001050
1051 /* Remove protocol notify registrations for the event */
1052 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1053 link) {
1054 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001055 struct efi_protocol_notification *hitem, *hnext;
1056
1057 /* Remove signaled handles */
1058 list_for_each_entry_safe(hitem, hnext, &item->handles,
1059 link) {
1060 list_del(&hitem->link);
1061 free(hitem);
1062 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001063 list_del(&item->link);
1064 free(item);
1065 }
1066 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001067 /* Remove event from queue */
1068 if (efi_event_is_queued(event))
1069 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001070
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001071 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001072 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001073 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001074}
1075
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001076/**
Mario Six8fac2912018-07-10 08:40:17 +02001077 * efi_check_event() - check if an event is signaled
1078 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001079 *
1080 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001081 *
1082 * See the Unified Extensible Firmware Interface (UEFI) specification for
1083 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001084 *
Mario Six8fac2912018-07-10 08:40:17 +02001085 * If an event is not signaled yet, the notification function is queued. The
1086 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001087 *
Mario Six8fac2912018-07-10 08:40:17 +02001088 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001089 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001090static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001091{
1092 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001093 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001094 if (efi_is_event(event) != EFI_SUCCESS ||
1095 event->type & EVT_NOTIFY_SIGNAL)
1096 return EFI_EXIT(EFI_INVALID_PARAMETER);
1097 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001098 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001099 if (event->is_signaled) {
1100 event->is_signaled = false;
1101 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001102 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001103 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001104}
1105
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001106/**
Mario Six8fac2912018-07-10 08:40:17 +02001107 * efi_search_obj() - find the internal EFI object for a handle
1108 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001109 *
Mario Six8fac2912018-07-10 08:40:17 +02001110 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001111 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001112struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001113{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001114 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001115
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001116 if (!handle)
1117 return NULL;
1118
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001119 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001120 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001121 return efiobj;
1122 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001123 return NULL;
1124}
1125
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001126/**
Mario Six8fac2912018-07-10 08:40:17 +02001127 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1128 * to a protocol
1129 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001130 *
Mario Six8fac2912018-07-10 08:40:17 +02001131 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001132 */
1133static struct efi_open_protocol_info_entry *efi_create_open_info(
1134 struct efi_handler *handler)
1135{
1136 struct efi_open_protocol_info_item *item;
1137
1138 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1139 if (!item)
1140 return NULL;
1141 /* Append the item to the open protocol info list. */
1142 list_add_tail(&item->link, &handler->open_infos);
1143
1144 return &item->info;
1145}
1146
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001147/**
Mario Six8fac2912018-07-10 08:40:17 +02001148 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1149 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001150 *
Mario Six8fac2912018-07-10 08:40:17 +02001151 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001152 */
1153static efi_status_t efi_delete_open_info(
1154 struct efi_open_protocol_info_item *item)
1155{
1156 list_del(&item->link);
1157 free(item);
1158 return EFI_SUCCESS;
1159}
1160
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001161/**
Mario Six8fac2912018-07-10 08:40:17 +02001162 * efi_add_protocol() - install new protocol on a handle
1163 * @handle: handle on which the protocol shall be installed
1164 * @protocol: GUID of the protocol to be installed
1165 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001166 *
Mario Six8fac2912018-07-10 08:40:17 +02001167 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001168 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001169efi_status_t efi_add_protocol(const efi_handle_t handle,
1170 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001171 void *protocol_interface)
1172{
1173 struct efi_object *efiobj;
1174 struct efi_handler *handler;
1175 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001176 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001177
1178 efiobj = efi_search_obj(handle);
1179 if (!efiobj)
1180 return EFI_INVALID_PARAMETER;
1181 ret = efi_search_protocol(handle, protocol, NULL);
1182 if (ret != EFI_NOT_FOUND)
1183 return EFI_INVALID_PARAMETER;
1184 handler = calloc(1, sizeof(struct efi_handler));
1185 if (!handler)
1186 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001187 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001188 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001189 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001190 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001191
1192 /* Notify registered events */
1193 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001194 if (!guidcmp(protocol, &event->protocol)) {
1195 struct efi_protocol_notification *notif;
1196
1197 notif = calloc(1, sizeof(*notif));
1198 if (!notif) {
1199 list_del(&handler->link);
1200 free(handler);
1201 return EFI_OUT_OF_RESOURCES;
1202 }
1203 notif->handle = handle;
1204 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001205 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001206 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001207 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001208 }
1209
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001210 if (!guidcmp(&efi_guid_device_path, protocol))
1211 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001212 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001213}
1214
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001215/**
Mario Six8fac2912018-07-10 08:40:17 +02001216 * efi_install_protocol_interface() - install protocol interface
1217 * @handle: handle on which the protocol shall be installed
1218 * @protocol: GUID of the protocol to be installed
1219 * @protocol_interface_type: type of the interface to be installed,
1220 * always EFI_NATIVE_INTERFACE
1221 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001222 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001223 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001224 *
Mario Six8fac2912018-07-10 08:40:17 +02001225 * See the Unified Extensible Firmware Interface (UEFI) specification for
1226 * details.
1227 *
1228 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001229 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001230static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001231 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001232 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001233{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001234 efi_status_t r;
1235
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001236 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001237 protocol_interface);
1238
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001239 if (!handle || !protocol ||
1240 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1241 r = EFI_INVALID_PARAMETER;
1242 goto out;
1243 }
1244
1245 /* Create new handle if requested. */
1246 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001247 r = efi_create_handle(handle);
1248 if (r != EFI_SUCCESS)
1249 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001250 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001251 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001252 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001253 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001254 /* Add new protocol */
1255 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001256out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001257 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001258}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001259
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001260/**
Mario Six8fac2912018-07-10 08:40:17 +02001261 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001262 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001263 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001264 * @number_of_drivers: number of child controllers
1265 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001266 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001267 * The allocated buffer has to be freed with free().
1268 *
Mario Six8fac2912018-07-10 08:40:17 +02001269 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001270 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001271static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001272 const efi_guid_t *protocol,
1273 efi_uintn_t *number_of_drivers,
1274 efi_handle_t **driver_handle_buffer)
1275{
1276 struct efi_handler *handler;
1277 struct efi_open_protocol_info_item *item;
1278 efi_uintn_t count = 0, i;
1279 bool duplicate;
1280
1281 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001282 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001283 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001284 continue;
1285 list_for_each_entry(item, &handler->open_infos, link) {
1286 if (item->info.attributes &
1287 EFI_OPEN_PROTOCOL_BY_DRIVER)
1288 ++count;
1289 }
1290 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001291 *number_of_drivers = 0;
1292 if (!count) {
1293 *driver_handle_buffer = NULL;
1294 return EFI_SUCCESS;
1295 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001296 /*
1297 * Create buffer. In case of duplicate driver assignments the buffer
1298 * will be too large. But that does not harm.
1299 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001300 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1301 if (!*driver_handle_buffer)
1302 return EFI_OUT_OF_RESOURCES;
1303 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001304 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001305 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001306 continue;
1307 list_for_each_entry(item, &handler->open_infos, link) {
1308 if (item->info.attributes &
1309 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1310 /* Check this is a new driver */
1311 duplicate = false;
1312 for (i = 0; i < *number_of_drivers; ++i) {
1313 if ((*driver_handle_buffer)[i] ==
1314 item->info.agent_handle)
1315 duplicate = true;
1316 }
1317 /* Copy handle to buffer */
1318 if (!duplicate) {
1319 i = (*number_of_drivers)++;
1320 (*driver_handle_buffer)[i] =
1321 item->info.agent_handle;
1322 }
1323 }
1324 }
1325 }
1326 return EFI_SUCCESS;
1327}
1328
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001329/**
Mario Six8fac2912018-07-10 08:40:17 +02001330 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001331 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001332 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001333 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001334 *
1335 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001336 *
1337 * See the Unified Extensible Firmware Interface (UEFI) specification for
1338 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001339 *
Mario Six8fac2912018-07-10 08:40:17 +02001340 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001341 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001342static efi_status_t efi_disconnect_all_drivers
1343 (efi_handle_t handle,
1344 const efi_guid_t *protocol,
1345 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001346{
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001347 efi_uintn_t number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001348 efi_handle_t *driver_handle_buffer;
1349 efi_status_t r, ret;
1350
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001351 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001352 &driver_handle_buffer);
1353 if (ret != EFI_SUCCESS)
1354 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001355 if (!number_of_drivers)
1356 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001357
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001358 while (number_of_drivers) {
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001359 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001360 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001361 driver_handle_buffer[--number_of_drivers],
1362 child_handle));
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001363 if (r != EFI_SUCCESS)
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001364 ret = r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001365 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001366
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001367 free(driver_handle_buffer);
1368 return ret;
1369}
1370
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001371/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001372 * efi_uninstall_protocol() - uninstall protocol interface
1373 *
Mario Six8fac2912018-07-10 08:40:17 +02001374 * @handle: handle from which the protocol shall be removed
1375 * @protocol: GUID of the protocol to be removed
1376 * @protocol_interface: interface to be removed
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001377 * @preserve: preserve or delete the handle and remove it from any
1378 * list it participates if no protocols remain
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001379 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001380 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001381 *
1382 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001383 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001384static efi_status_t efi_uninstall_protocol
1385 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001386 void *protocol_interface, bool preserve)
Alexander Grafc15d9212016-03-04 01:09:59 +01001387{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001388 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001389 struct efi_open_protocol_info_item *item;
1390 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001391 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001392
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001393 /* Find the protocol on the handle */
1394 r = efi_search_protocol(handle, protocol, &handler);
1395 if (r != EFI_SUCCESS)
1396 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001397 if (handler->protocol_interface != protocol_interface)
1398 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001399 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001400 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001401 if (r != EFI_SUCCESS) {
1402 r = EFI_ACCESS_DENIED;
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001403 /*
1404 * This will reconnect all controllers of the handle, even ones
1405 * that were not connected before. This can be done better
1406 * but we are following the EDKII implementation on this for
1407 * now
1408 */
1409 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001410 goto out;
1411 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001412 /* Close protocol */
1413 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1414 if (item->info.attributes ==
1415 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1416 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1417 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001418 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001419 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001420 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001421 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001422 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001423 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001424 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001425 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001426 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001427 if (r != EFI_SUCCESS)
1428 return r;
1429 /*
1430 * We don't care about the return value here since the
1431 * handle might have more protocols installed
1432 */
1433 if (!preserve)
1434 efi_purge_handle(handle);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001435out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001436 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001437}
1438
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001439/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001440 * efi_uninstall_protocol_interface() - uninstall protocol interface
1441 * @handle: handle from which the protocol shall be removed
1442 * @protocol: GUID of the protocol to be removed
1443 * @protocol_interface: interface to be removed
1444 *
1445 * This function implements the UninstallProtocolInterface service.
1446 *
1447 * See the Unified Extensible Firmware Interface (UEFI) specification for
1448 * details.
1449 *
1450 * Return: status code
1451 */
1452static efi_status_t EFIAPI efi_uninstall_protocol_interface
1453 (efi_handle_t handle, const efi_guid_t *protocol,
1454 void *protocol_interface)
1455{
1456 efi_status_t ret;
1457
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001458 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001459
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001460 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001461 if (ret != EFI_SUCCESS)
1462 goto out;
1463
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001464out:
1465 return EFI_EXIT(ret);
1466}
1467
1468/**
Mario Six8fac2912018-07-10 08:40:17 +02001469 * efi_register_protocol_notify() - register an event for notification when a
1470 * protocol is installed.
1471 * @protocol: GUID of the protocol whose installation shall be notified
1472 * @event: event to be signaled upon installation of the protocol
1473 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001474 *
1475 * This function implements the RegisterProtocolNotify service.
1476 * See the Unified Extensible Firmware Interface (UEFI) specification
1477 * for details.
1478 *
Mario Six8fac2912018-07-10 08:40:17 +02001479 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001480 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001481efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1482 struct efi_event *event,
1483 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001484{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001485 struct efi_register_notify_event *item;
1486 efi_status_t ret = EFI_SUCCESS;
1487
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001488 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001489
1490 if (!protocol || !event || !registration) {
1491 ret = EFI_INVALID_PARAMETER;
1492 goto out;
1493 }
1494
1495 item = calloc(1, sizeof(struct efi_register_notify_event));
1496 if (!item) {
1497 ret = EFI_OUT_OF_RESOURCES;
1498 goto out;
1499 }
1500
1501 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301502 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001503 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001504
1505 list_add_tail(&item->link, &efi_register_notify_events);
1506
1507 *registration = item;
1508out:
1509 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001510}
1511
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001512/**
Mario Six8fac2912018-07-10 08:40:17 +02001513 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001514 *
Mario Six8fac2912018-07-10 08:40:17 +02001515 * @search_type: selection criterion
1516 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001517 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001518 *
1519 * See the documentation of the LocateHandle service in the UEFI specification.
1520 *
Mario Six8fac2912018-07-10 08:40:17 +02001521 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001522 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001523static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001524 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001525{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001526 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001527
1528 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001529 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001530 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001531 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001532 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001533 return (ret != EFI_SUCCESS);
1534 default:
1535 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001536 return -1;
1537 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001538}
1539
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001540/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001541 * efi_check_register_notify_event() - check if registration key is valid
1542 *
1543 * Check that a pointer is a valid registration key as returned by
1544 * RegisterProtocolNotify().
1545 *
1546 * @key: registration key
1547 * Return: valid registration key or NULL
1548 */
1549static struct efi_register_notify_event *efi_check_register_notify_event
1550 (void *key)
1551{
1552 struct efi_register_notify_event *event;
1553
1554 list_for_each_entry(event, &efi_register_notify_events, link) {
1555 if (event == (struct efi_register_notify_event *)key)
1556 return event;
1557 }
1558 return NULL;
1559}
1560
1561/**
Mario Six8fac2912018-07-10 08:40:17 +02001562 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001563 *
1564 * @search_type: selection criterion
1565 * @protocol: GUID of the protocol
1566 * @search_key: registration key
1567 * @buffer_size: size of the buffer to receive the handles in bytes
1568 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001569 *
1570 * This function is meant for U-Boot internal calls. For the API implementation
1571 * of the LocateHandle service see efi_locate_handle_ext.
1572 *
Mario Six8fac2912018-07-10 08:40:17 +02001573 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001574 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001575static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001576 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001577 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001578 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001579{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001580 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001581 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001582 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001583 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001584
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001585 /* Check parameters */
1586 switch (search_type) {
1587 case ALL_HANDLES:
1588 break;
1589 case BY_REGISTER_NOTIFY:
1590 if (!search_key)
1591 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001592 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001593 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001594 if (!event)
1595 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001596 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001597 case BY_PROTOCOL:
1598 if (!protocol)
1599 return EFI_INVALID_PARAMETER;
1600 break;
1601 default:
1602 return EFI_INVALID_PARAMETER;
1603 }
1604
Alexander Grafc15d9212016-03-04 01:09:59 +01001605 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001606 if (search_type == BY_REGISTER_NOTIFY) {
1607 if (list_empty(&event->handles))
1608 return EFI_NOT_FOUND;
1609 handle = list_first_entry(&event->handles,
1610 struct efi_protocol_notification,
1611 link);
1612 efiobj = handle->handle;
1613 size += sizeof(void *);
1614 } else {
1615 list_for_each_entry(efiobj, &efi_obj_list, link) {
1616 if (!efi_search(search_type, protocol, efiobj))
1617 size += sizeof(void *);
1618 }
1619 if (size == 0)
1620 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001621 }
1622
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001623 if (!buffer_size)
1624 return EFI_INVALID_PARAMETER;
1625
Alexander Grafc15d9212016-03-04 01:09:59 +01001626 if (*buffer_size < size) {
1627 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001628 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001629 }
1630
Rob Clarkcdee3372017-08-06 14:10:07 -04001631 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001632
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001633 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001634 if (!buffer)
1635 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001636
Alexander Grafc15d9212016-03-04 01:09:59 +01001637 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001638 if (search_type == BY_REGISTER_NOTIFY) {
1639 *buffer = efiobj;
1640 list_del(&handle->link);
1641 } else {
1642 list_for_each_entry(efiobj, &efi_obj_list, link) {
1643 if (!efi_search(search_type, protocol, efiobj))
1644 *buffer++ = efiobj;
1645 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001646 }
1647
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001648 return EFI_SUCCESS;
1649}
1650
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001651/**
Mario Six8fac2912018-07-10 08:40:17 +02001652 * efi_locate_handle_ext() - locate handles implementing a protocol.
1653 * @search_type: selection criterion
1654 * @protocol: GUID of the protocol
1655 * @search_key: registration key
1656 * @buffer_size: size of the buffer to receive the handles in bytes
1657 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001658 *
1659 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001660 *
Mario Six8fac2912018-07-10 08:40:17 +02001661 * See the Unified Extensible Firmware Interface (UEFI) specification for
1662 * details.
1663 *
1664 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001665 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001666static efi_status_t EFIAPI efi_locate_handle_ext(
1667 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001668 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001669 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001670{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001671 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001672 buffer_size, buffer);
1673
1674 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1675 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001676}
1677
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001678/**
Mario Six8fac2912018-07-10 08:40:17 +02001679 * efi_remove_configuration_table() - collapses configuration table entries,
1680 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001681 *
Mario Six8fac2912018-07-10 08:40:17 +02001682 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001683 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001684static void efi_remove_configuration_table(int i)
1685{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001686 struct efi_configuration_table *this = &systab.tables[i];
1687 struct efi_configuration_table *next = &systab.tables[i + 1];
1688 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001689
1690 memmove(this, next, (ulong)end - (ulong)next);
1691 systab.nr_tables--;
1692}
1693
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001694/**
Mario Six8fac2912018-07-10 08:40:17 +02001695 * efi_install_configuration_table() - adds, updates, or removes a
1696 * configuration table
1697 * @guid: GUID of the installed table
1698 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001699 *
1700 * This function is used for internal calls. For the API implementation of the
1701 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1702 *
Mario Six8fac2912018-07-10 08:40:17 +02001703 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001704 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001705efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1706 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001707{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001708 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001709 int i;
1710
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001711 if (!guid)
1712 return EFI_INVALID_PARAMETER;
1713
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001714 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001715 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001716 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001717 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001718 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001719 else
1720 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001721 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001722 }
1723 }
1724
Alexander Graffe3366f2017-07-26 13:41:04 +02001725 if (!table)
1726 return EFI_NOT_FOUND;
1727
Alexander Grafc15d9212016-03-04 01:09:59 +01001728 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001729 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001730 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001731
1732 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301733 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001734 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001735 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001736
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001737out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001738 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001739 efi_update_table_header_crc32(&systab.hdr);
1740
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001741 /* Notify that the configuration table was changed */
1742 list_for_each_entry(evt, &efi_events, link) {
1743 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001744 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001745 break;
1746 }
1747 }
1748
Alexander Grafc5c11632016-08-19 01:23:24 +02001749 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001750}
1751
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001752/**
Mario Six8fac2912018-07-10 08:40:17 +02001753 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1754 * configuration table.
1755 * @guid: GUID of the installed table
1756 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001757 *
1758 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001759 *
Mario Six8fac2912018-07-10 08:40:17 +02001760 * See the Unified Extensible Firmware Interface (UEFI) specification for
1761 * details.
1762 *
1763 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001764 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001765static efi_status_t
1766EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1767 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001768{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001769 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001770 return EFI_EXIT(efi_install_configuration_table(guid, table));
1771}
1772
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001773/**
Mario Six8fac2912018-07-10 08:40:17 +02001774 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001775 *
1776 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001777 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001778 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001779 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001780 * code is returned.
1781 *
1782 * @device_path: device path of the loaded image
1783 * @file_path: file path of the loaded image
1784 * @handle_ptr: handle of the loaded image
1785 * @info_ptr: loaded image protocol
1786 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001787 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001788efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1789 struct efi_device_path *file_path,
1790 struct efi_loaded_image_obj **handle_ptr,
1791 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001792{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001793 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001794 struct efi_loaded_image *info = NULL;
1795 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001796 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001797
1798 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1799 *handle_ptr = NULL;
1800 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001801
1802 info = calloc(1, sizeof(*info));
1803 if (!info)
1804 return EFI_OUT_OF_RESOURCES;
1805 obj = calloc(1, sizeof(*obj));
1806 if (!obj) {
1807 free(info);
1808 return EFI_OUT_OF_RESOURCES;
1809 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001810 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001811
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001812 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001813 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001814
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001815 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001816 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001817 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001818
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001819 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001820 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001821
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +02001822 dp = efi_dp_concat(device_path, file_path, 0);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001823 if (!dp) {
1824 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001825 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001826 }
1827 } else {
1828 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001829 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001830 ret = efi_add_protocol(&obj->header,
1831 &efi_guid_loaded_image_device_path, dp);
1832 if (ret != EFI_SUCCESS)
1833 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001834
1835 /*
1836 * When asking for the loaded_image interface, just
1837 * return handle which points to loaded_image_info
1838 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001839 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001840 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001841 if (ret != EFI_SUCCESS)
1842 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001843
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001844 *info_ptr = info;
1845 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001846
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001847 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001848failure:
1849 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001850 efi_delete_handle(&obj->header);
1851 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001852 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001853}
1854
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001855/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001856 * efi_locate_device_path() - Get the device path and handle of an device
1857 * implementing a protocol
1858 * @protocol: GUID of the protocol
1859 * @device_path: device path
1860 * @device: handle of the device
1861 *
1862 * This function implements the LocateDevicePath service.
1863 *
1864 * See the Unified Extensible Firmware Interface (UEFI) specification for
1865 * details.
1866 *
1867 * Return: status code
1868 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001869efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1870 struct efi_device_path **device_path,
1871 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001872{
1873 struct efi_device_path *dp;
1874 size_t i;
1875 struct efi_handler *handler;
1876 efi_handle_t *handles;
1877 size_t len, len_dp;
1878 size_t len_best = 0;
1879 efi_uintn_t no_handles;
1880 u8 *remainder;
1881 efi_status_t ret;
1882
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001883 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001884
1885 if (!protocol || !device_path || !*device_path) {
1886 ret = EFI_INVALID_PARAMETER;
1887 goto out;
1888 }
1889
1890 /* Find end of device path */
1891 len = efi_dp_instance_size(*device_path);
1892
1893 /* Get all handles implementing the protocol */
1894 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1895 &no_handles, &handles));
1896 if (ret != EFI_SUCCESS)
1897 goto out;
1898
1899 for (i = 0; i < no_handles; ++i) {
1900 /* Find the device path protocol */
1901 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1902 &handler);
1903 if (ret != EFI_SUCCESS)
1904 continue;
1905 dp = (struct efi_device_path *)handler->protocol_interface;
1906 len_dp = efi_dp_instance_size(dp);
1907 /*
1908 * This handle can only be a better fit
1909 * if its device path length is longer than the best fit and
1910 * if its device path length is shorter of equal the searched
1911 * device path.
1912 */
1913 if (len_dp <= len_best || len_dp > len)
1914 continue;
1915 /* Check if dp is a subpath of device_path */
1916 if (memcmp(*device_path, dp, len_dp))
1917 continue;
1918 if (!device) {
1919 ret = EFI_INVALID_PARAMETER;
1920 goto out;
1921 }
1922 *device = handles[i];
1923 len_best = len_dp;
1924 }
1925 if (len_best) {
1926 remainder = (u8 *)*device_path + len_best;
1927 *device_path = (struct efi_device_path *)remainder;
1928 ret = EFI_SUCCESS;
1929 } else {
1930 ret = EFI_NOT_FOUND;
1931 }
1932out:
1933 return EFI_EXIT(ret);
1934}
1935
1936/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001937 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001938 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001939 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1940 * callers obligation to update the memory type as needed.
1941 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001942 * @file_path: the path of the image to load
1943 * @buffer: buffer containing the loaded image
1944 * @size: size of the loaded image
1945 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001946 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001947static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001948efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001949 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001950{
Rob Clark857a1222017-09-13 18:05:35 -04001951 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001952 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001953 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001954 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001955
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001956 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001957 f = efi_file_from_path(file_path);
1958 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001959 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001960
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001961 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001962 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001963 goto error;
1964
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001965 /*
1966 * When reading the file we do not yet know if it contains an
1967 * application, a boottime driver, or a runtime driver. So here we
1968 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1969 * update the reservation according to the image type.
1970 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001971 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1972 EFI_BOOT_SERVICES_DATA,
1973 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001974 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001975 ret = EFI_OUT_OF_RESOURCES;
1976 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001977 }
1978
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001979 /* Read file */
1980 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1981 if (ret != EFI_SUCCESS)
1982 efi_free_pages(addr, efi_size_in_pages(bs));
1983 *buffer = (void *)(uintptr_t)addr;
1984 *size = bs;
1985error:
1986 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001987 return ret;
1988}
1989
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001990/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001991 * efi_load_image_from_path() - load an image using a file path
1992 *
1993 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1994 * callers obligation to update the memory type as needed.
1995 *
1996 * @boot_policy: true for request originating from the boot manager
1997 * @file_path: the path of the image to load
1998 * @buffer: buffer containing the loaded image
1999 * @size: size of the loaded image
2000 * Return: status code
2001 */
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002002efi_status_t efi_load_image_from_path(bool boot_policy,
2003 struct efi_device_path *file_path,
2004 void **buffer, efi_uintn_t *size)
2005{
2006 efi_handle_t device;
2007 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002008 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002009 struct efi_load_file_protocol *load_file_protocol = NULL;
2010 efi_uintn_t buffer_size;
2011 uint64_t addr, pages;
2012 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002013 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002014
2015 /* In case of failure nothing is returned */
2016 *buffer = NULL;
2017 *size = 0;
2018
2019 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002020 device = efi_dp_find_obj(dp, NULL, &rem);
2021 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2022 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002023 if (ret == EFI_SUCCESS)
2024 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002025
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002026 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002027 if (ret == EFI_SUCCESS) {
2028 guid = &efi_guid_load_file_protocol;
2029 } else if (!boot_policy) {
2030 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002031 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002032 }
2033 if (ret != EFI_SUCCESS)
2034 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002035 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002036 if (ret != EFI_SUCCESS)
2037 return EFI_NOT_FOUND;
2038 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002039 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002040 ret = EFI_CALL(load_file_protocol->load_file(
2041 load_file_protocol, rem, boot_policy,
2042 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002043 if (ret != EFI_BUFFER_TOO_SMALL)
2044 goto out;
2045 pages = efi_size_in_pages(buffer_size);
2046 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2047 pages, &addr);
2048 if (ret != EFI_SUCCESS) {
2049 ret = EFI_OUT_OF_RESOURCES;
2050 goto out;
2051 }
2052 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002053 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002054 &buffer_size, (void *)(uintptr_t)addr));
2055 if (ret != EFI_SUCCESS)
2056 efi_free_pages(addr, pages);
2057out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002058 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002059 if (ret == EFI_SUCCESS) {
2060 *buffer = (void *)(uintptr_t)addr;
2061 *size = buffer_size;
2062 }
2063
2064 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002065}
2066
2067/**
Mario Six8fac2912018-07-10 08:40:17 +02002068 * efi_load_image() - load an EFI image into memory
2069 * @boot_policy: true for request originating from the boot manager
2070 * @parent_image: the caller's image handle
2071 * @file_path: the path of the image to load
2072 * @source_buffer: memory location from which the image is installed
2073 * @source_size: size of the memory area from which the image is installed
2074 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002075 *
2076 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002077 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002078 * See the Unified Extensible Firmware Interface (UEFI) specification
2079 * for details.
2080 *
Mario Six8fac2912018-07-10 08:40:17 +02002081 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002082 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002083efi_status_t EFIAPI efi_load_image(bool boot_policy,
2084 efi_handle_t parent_image,
2085 struct efi_device_path *file_path,
2086 void *source_buffer,
2087 efi_uintn_t source_size,
2088 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002089{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002090 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002091 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002092 struct efi_loaded_image_obj **image_obj =
2093 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002094 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002095 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002096
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002097 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002098 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002099
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002100 if (!image_handle || (!source_buffer && !file_path) ||
2101 !efi_search_obj(parent_image) ||
2102 /* The parent image handle must refer to a loaded image */
2103 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002104 ret = EFI_INVALID_PARAMETER;
2105 goto error;
2106 }
Rob Clark857a1222017-09-13 18:05:35 -04002107
2108 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002109 ret = efi_load_image_from_path(boot_policy, file_path,
2110 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002111 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002112 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002113 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002114 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002115 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002116 /* split file_path which contains both the device and file parts */
2117 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002118 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002119 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002120 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002121 if (!source_buffer)
2122 /* Release buffer to which file was loaded */
2123 efi_free_pages((uintptr_t)dest_buffer,
2124 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002125 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002126 info->system_table = &systab;
2127 info->parent_handle = parent_image;
2128 } else {
2129 /* The image is invalid. Release all associated resources. */
2130 efi_delete_handle(*image_handle);
2131 *image_handle = NULL;
2132 free(info);
2133 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002134error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002135 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002136}
2137
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002138/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002139 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2140 */
2141static void efi_exit_caches(void)
2142{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002143#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002144 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002145 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2146 * caches are enabled.
2147 *
2148 * TODO:
2149 * According to the UEFI spec caches that can be managed via CP15
2150 * operations should be enabled. Caches requiring platform information
2151 * to manage should be disabled. This should not happen in
2152 * ExitBootServices() but before invoking any UEFI binary is invoked.
2153 *
2154 * We want to keep the current workaround while GRUB prior to version
2155 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002156 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002157 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002158#endif
2159}
2160
2161/**
Mario Six8fac2912018-07-10 08:40:17 +02002162 * efi_exit_boot_services() - stop all boot services
2163 * @image_handle: handle of the loaded image
2164 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002165 *
2166 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002167 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002168 * See the Unified Extensible Firmware Interface (UEFI) specification
2169 * for details.
2170 *
Mario Six8fac2912018-07-10 08:40:17 +02002171 * All timer events are disabled. For exit boot services events the
2172 * notification function is called. The boot services are disabled in the
2173 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002174 *
Mario Six8fac2912018-07-10 08:40:17 +02002175 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002176 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002177static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002178 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002179{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002180 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002181 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002182
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002183 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002184
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002185 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002186 if (map_key != efi_memory_map_key) {
2187 ret = EFI_INVALID_PARAMETER;
2188 goto out;
2189 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002190
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002191 /* Check if ExitBootServices has already been called */
2192 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002193 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002194
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002195 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2196 list_for_each_entry(evt, &efi_events, link) {
2197 if (evt->group &&
2198 !guidcmp(evt->group,
2199 &efi_guid_event_group_before_exit_boot_services)) {
2200 efi_signal_event(evt);
2201 break;
2202 }
2203 }
2204
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002205 /* Stop all timer related activities */
2206 timers_enabled = false;
2207
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002208 /* Add related events to the event group */
2209 list_for_each_entry(evt, &efi_events, link) {
2210 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2211 evt->group = &efi_guid_event_group_exit_boot_services;
2212 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002213 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002214 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002215 if (evt->group &&
2216 !guidcmp(evt->group,
2217 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002218 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002219 break;
2220 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002221 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002222
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002223 /* Make sure that notification functions are not called anymore */
2224 efi_tpl = TPL_HIGH_LEVEL;
2225
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002226 /* Notify variable services */
2227 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002228
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002229 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2230 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2231 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2232 list_del(&evt->link);
2233 }
2234
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002235 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002236 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002237 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002238 udc_disconnect();
Heinrich Schuchardt20047fd2025-04-05 08:58:12 +02002239 if (IS_ENABLED(CONFIG_DM_ETH))
2240 eth_halt();
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002241 board_quiesce_devices();
Janne Grunau64609732024-11-23 22:44:05 +01002242 dm_remove_devices_active();
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002243 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002244
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002245 /* Patch out unsupported runtime function */
2246 efi_runtime_detach();
2247
Alexander Graffa5246b2018-11-15 21:23:47 +01002248 /* Fix up caches for EFI payloads if necessary */
2249 efi_exit_caches();
2250
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002251 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002252 systab.con_in_handle = NULL;
2253 systab.con_in = NULL;
2254 systab.con_out_handle = NULL;
2255 systab.con_out = NULL;
2256 systab.stderr_handle = NULL;
2257 systab.std_err = NULL;
2258 systab.boottime = NULL;
2259
2260 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002261 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002262
Alexander Grafc15d9212016-03-04 01:09:59 +01002263 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002264 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002265 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002266out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002267 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2268 if (ret != EFI_SUCCESS)
2269 efi_tcg2_notify_exit_boot_services_failed();
2270 }
2271
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002272 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002273}
2274
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002275/**
Mario Six8fac2912018-07-10 08:40:17 +02002276 * efi_get_next_monotonic_count() - get next value of the counter
2277 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002278 *
2279 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002280 *
2281 * See the Unified Extensible Firmware Interface (UEFI) specification for
2282 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002283 *
Mario Six8fac2912018-07-10 08:40:17 +02002284 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002285 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002286static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2287{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002288 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002289 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002290
Alexander Grafc15d9212016-03-04 01:09:59 +01002291 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002292 if (!count) {
2293 ret = EFI_INVALID_PARAMETER;
2294 goto out;
2295 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002296 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002297 ret = EFI_SUCCESS;
2298out:
2299 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002300}
2301
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002302/**
Mario Six8fac2912018-07-10 08:40:17 +02002303 * efi_stall() - sleep
2304 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002305 *
Mario Six8fac2912018-07-10 08:40:17 +02002306 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002307 *
Mario Six8fac2912018-07-10 08:40:17 +02002308 * See the Unified Extensible Firmware Interface (UEFI) specification for
2309 * details.
2310 *
2311 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002312 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002313static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2314{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002315 u64 end_tick;
2316
Alexander Grafc15d9212016-03-04 01:09:59 +01002317 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002318
2319 end_tick = get_ticks() + usec_to_tick(microseconds);
2320 while (get_ticks() < end_tick)
2321 efi_timer_check();
2322
Alexander Grafc15d9212016-03-04 01:09:59 +01002323 return EFI_EXIT(EFI_SUCCESS);
2324}
2325
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002326/**
Mario Six8fac2912018-07-10 08:40:17 +02002327 * efi_set_watchdog_timer() - reset the watchdog timer
2328 * @timeout: seconds before reset by watchdog
2329 * @watchdog_code: code to be logged when resetting
2330 * @data_size: size of buffer in bytes
2331 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002332 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002333 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002334 *
Mario Six8fac2912018-07-10 08:40:17 +02002335 * See the Unified Extensible Firmware Interface (UEFI) specification for
2336 * details.
2337 *
2338 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002339 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002340static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2341 uint64_t watchdog_code,
2342 unsigned long data_size,
2343 uint16_t *watchdog_data)
2344{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002345 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002346 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002347 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002348}
2349
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002350/**
Mario Six8fac2912018-07-10 08:40:17 +02002351 * efi_close_protocol() - close a protocol
2352 * @handle: handle on which the protocol shall be closed
2353 * @protocol: GUID of the protocol to close
2354 * @agent_handle: handle of the driver
2355 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002356 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002357 * This is the function implementing the CloseProtocol service is for internal
2358 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002359 *
Mario Six8fac2912018-07-10 08:40:17 +02002360 * See the Unified Extensible Firmware Interface (UEFI) specification for
2361 * details.
2362 *
2363 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002364 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002365efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2366 efi_handle_t agent_handle,
2367 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002368{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002369 struct efi_handler *handler;
2370 struct efi_open_protocol_info_item *item;
2371 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002372 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002373
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002374 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002375 (controller_handle && !efi_search_obj(controller_handle)))
2376 return EFI_INVALID_PARAMETER;
2377 ret = efi_search_protocol(handle, protocol, &handler);
2378 if (ret != EFI_SUCCESS)
2379 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002380
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002381 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002382 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2383 if (item->info.agent_handle == agent_handle &&
2384 item->info.controller_handle == controller_handle) {
2385 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002386 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002387 }
2388 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002389
2390 return ret;
2391}
2392
2393/**
2394 * efi_close_protocol_ext() - close a protocol
2395 * @handle: handle on which the protocol shall be closed
2396 * @protocol: GUID of the protocol to close
2397 * @agent_handle: handle of the driver
2398 * @controller_handle: handle of the controller
2399 *
2400 * This function implements the CloseProtocol service.
2401 *
2402 * See the Unified Extensible Firmware Interface (UEFI) specification for
2403 * details.
2404 *
2405 * Return: status code
2406 */
2407static efi_status_t EFIAPI
2408efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2409 efi_handle_t agent_handle,
2410 efi_handle_t controller_handle)
2411{
2412 efi_status_t ret;
2413
2414 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2415 controller_handle);
2416
2417 ret = efi_close_protocol(handle, protocol,
2418 agent_handle, controller_handle);
2419
2420 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002421}
2422
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002423/**
Mario Six8fac2912018-07-10 08:40:17 +02002424 * efi_open_protocol_information() - provide information about then open status
2425 * of a protocol on a handle
2426 * @handle: handle for which the information shall be retrieved
2427 * @protocol: GUID of the protocol
2428 * @entry_buffer: buffer to receive the open protocol information
2429 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002430 *
2431 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002432 *
2433 * See the Unified Extensible Firmware Interface (UEFI) specification for
2434 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002435 *
Mario Six8fac2912018-07-10 08:40:17 +02002436 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002437 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002438static efi_status_t EFIAPI efi_open_protocol_information(
2439 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002440 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002441 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002442{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002443 unsigned long buffer_size;
2444 unsigned long count;
2445 struct efi_handler *handler;
2446 struct efi_open_protocol_info_item *item;
2447 efi_status_t r;
2448
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002449 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002450 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002451
2452 /* Check parameters */
2453 if (!entry_buffer) {
2454 r = EFI_INVALID_PARAMETER;
2455 goto out;
2456 }
2457 r = efi_search_protocol(handle, protocol, &handler);
2458 if (r != EFI_SUCCESS)
2459 goto out;
2460
2461 /* Count entries */
2462 count = 0;
2463 list_for_each_entry(item, &handler->open_infos, link) {
2464 if (item->info.open_count)
2465 ++count;
2466 }
2467 *entry_count = count;
2468 *entry_buffer = NULL;
2469 if (!count) {
2470 r = EFI_SUCCESS;
2471 goto out;
2472 }
2473
2474 /* Copy entries */
2475 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002476 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002477 (void **)entry_buffer);
2478 if (r != EFI_SUCCESS)
2479 goto out;
2480 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2481 if (item->info.open_count)
2482 (*entry_buffer)[--count] = item->info;
2483 }
2484out:
2485 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002486}
2487
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002488/**
Mario Six8fac2912018-07-10 08:40:17 +02002489 * efi_protocols_per_handle() - get protocols installed on a handle
2490 * @handle: handle for which the information is retrieved
2491 * @protocol_buffer: buffer with protocol GUIDs
2492 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002493 *
2494 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002495 *
Mario Six8fac2912018-07-10 08:40:17 +02002496 * See the Unified Extensible Firmware Interface (UEFI) specification for
2497 * details.
2498 *
2499 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002500 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002501static efi_status_t EFIAPI efi_protocols_per_handle(
2502 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002503 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002504{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002505 unsigned long buffer_size;
2506 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002507 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002508 efi_status_t r;
2509
Alexander Grafc15d9212016-03-04 01:09:59 +01002510 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2511 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002512
2513 if (!handle || !protocol_buffer || !protocol_buffer_count)
2514 return EFI_EXIT(EFI_INVALID_PARAMETER);
2515
2516 *protocol_buffer = NULL;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002517
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002518 efiobj = efi_search_obj(handle);
2519 if (!efiobj)
2520 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002521
Heinrich Schuchardt5f6806d2024-07-31 10:13:04 +02002522 *protocol_buffer_count = list_count_nodes(&efiobj->protocols);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002523
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002524 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002525 if (*protocol_buffer_count) {
2526 size_t j = 0;
2527
2528 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002529 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002530 (void **)protocol_buffer);
2531 if (r != EFI_SUCCESS)
2532 return EFI_EXIT(r);
2533 list_for_each(protocol_handle, &efiobj->protocols) {
2534 struct efi_handler *protocol;
2535
2536 protocol = list_entry(protocol_handle,
2537 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002538 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002539 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002540 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002541 }
2542
2543 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002544}
2545
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002546efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2547 const efi_guid_t *protocol, void *search_key,
2548 efi_uintn_t *no_handles, efi_handle_t **buffer)
2549{
2550 efi_status_t r;
2551 efi_uintn_t buffer_size = 0;
2552
2553 if (!no_handles || !buffer) {
2554 r = EFI_INVALID_PARAMETER;
2555 goto out;
2556 }
2557 *no_handles = 0;
2558 *buffer = NULL;
2559 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2560 *buffer);
2561 if (r != EFI_BUFFER_TOO_SMALL)
2562 goto out;
2563 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2564 (void **)buffer);
2565 if (r != EFI_SUCCESS)
2566 goto out;
2567 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2568 *buffer);
2569 if (r == EFI_SUCCESS)
2570 *no_handles = buffer_size / sizeof(efi_handle_t);
2571out:
2572 return r;
2573}
2574
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002575/**
Mario Six8fac2912018-07-10 08:40:17 +02002576 * efi_locate_handle_buffer() - locate handles implementing a protocol
2577 * @search_type: selection criterion
2578 * @protocol: GUID of the protocol
2579 * @search_key: registration key
2580 * @no_handles: number of returned handles
2581 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002582 *
2583 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002584 *
Mario Six8fac2912018-07-10 08:40:17 +02002585 * See the Unified Extensible Firmware Interface (UEFI) specification for
2586 * details.
2587 *
2588 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002589 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002590efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002591 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002592 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002593 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002594{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002595 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002596
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002597 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002598 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002599
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002600 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2601 no_handles, buffer);
2602
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002603 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002604}
2605
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002606/**
Mario Six8fac2912018-07-10 08:40:17 +02002607 * efi_locate_protocol() - find an interface implementing a protocol
2608 * @protocol: GUID of the protocol
2609 * @registration: registration key passed to the notification function
2610 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002611 *
2612 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002613 *
2614 * See the Unified Extensible Firmware Interface (UEFI) specification for
2615 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002616 *
Mario Six8fac2912018-07-10 08:40:17 +02002617 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002618 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002619static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002620 void *registration,
2621 void **protocol_interface)
2622{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002623 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002624 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002625 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002626
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002627 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002628
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002629 /*
2630 * The UEFI spec explicitly requires a protocol even if a registration
2631 * key is provided. This differs from the logic in LocateHandle().
2632 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002633 if (!protocol || !protocol_interface)
2634 return EFI_EXIT(EFI_INVALID_PARAMETER);
2635
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002636 if (registration) {
2637 struct efi_register_notify_event *event;
2638 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002639
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002640 event = efi_check_register_notify_event(registration);
2641 if (!event)
2642 return EFI_EXIT(EFI_INVALID_PARAMETER);
2643 /*
2644 * The UEFI spec requires to return EFI_NOT_FOUND if no
2645 * protocol instance matches protocol and registration.
2646 * So let's do the same for a mismatch between protocol and
2647 * registration.
2648 */
2649 if (guidcmp(&event->protocol, protocol))
2650 goto not_found;
2651 if (list_empty(&event->handles))
2652 goto not_found;
2653 handle = list_first_entry(&event->handles,
2654 struct efi_protocol_notification,
2655 link);
2656 efiobj = handle->handle;
2657 list_del(&handle->link);
2658 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002659 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002660 if (ret == EFI_SUCCESS)
2661 goto found;
2662 } else {
2663 list_for_each_entry(efiobj, &efi_obj_list, link) {
2664 ret = efi_search_protocol(efiobj, protocol, &handler);
2665 if (ret == EFI_SUCCESS)
2666 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002667 }
2668 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002669not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002670 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002671 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002672found:
2673 *protocol_interface = handler->protocol_interface;
2674 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002675}
2676
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002677/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002678 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002679 * interfaces
2680 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002681 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002682 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002683 * Core functionality of efi_install_multiple_protocol_interfaces
2684 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002685 *
Mario Six8fac2912018-07-10 08:40:17 +02002686 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002687 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002688static efi_status_t EFIAPI
2689efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2690 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002691{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002692 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002693 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002694 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002695 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002696 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002697 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002698
2699 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002700 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002701
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002702 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002703 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002704 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002705 if (!protocol)
2706 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002707 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002708 /* Check that a device path has not been installed before */
2709 if (!guidcmp(protocol, &efi_guid_device_path)) {
2710 struct efi_device_path *dp = protocol_interface;
2711
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002712 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2713 &old_handle));
2714 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002715 dp->type == DEVICE_PATH_TYPE_END) {
2716 EFI_PRINT("Path %pD already installed\n",
2717 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002718 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002719 break;
2720 }
2721 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002722 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2723 EFI_NATIVE_INTERFACE,
2724 protocol_interface));
2725 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002726 break;
2727 i++;
2728 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002729 if (ret == EFI_SUCCESS)
2730 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002731
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002732 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002733 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002734 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2735 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002736 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002737 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002738 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002739
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002740out:
2741 efi_va_end(argptr_copy);
2742 return ret;
2743
Alexander Grafc15d9212016-03-04 01:09:59 +01002744}
2745
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002746/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002747 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2748 * interfaces
2749 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002750 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2751 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002752 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002753 *
2754 * This is the function for internal usage in U-Boot. For the API function
2755 * implementing the InstallMultipleProtocol service see
2756 * efi_install_multiple_protocol_interfaces_ext()
2757 *
2758 * Return: status code
2759 */
2760efi_status_t EFIAPI
2761efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2762{
2763 efi_status_t ret;
2764 efi_va_list argptr;
2765
2766 efi_va_start(argptr, handle);
2767 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2768 efi_va_end(argptr);
2769 return ret;
2770}
2771
2772/**
2773 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2774 * interfaces
2775 * @handle: handle on which the protocol interfaces shall be installed
2776 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2777 * interfaces
2778 *
2779 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002780 *
Mario Six8fac2912018-07-10 08:40:17 +02002781 * See the Unified Extensible Firmware Interface (UEFI) specification for
2782 * details.
2783 *
2784 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002785 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002786static efi_status_t EFIAPI
2787efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002788{
2789 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002790 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002791 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002792
2793 efi_va_start(argptr, handle);
2794 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2795 efi_va_end(argptr);
2796 return EFI_EXIT(ret);
2797}
2798
2799/**
2800 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2801 * multiple protocol
2802 * interfaces
2803 * @handle: handle from which the protocol interfaces shall be removed
2804 * @argptr: va_list of args
2805 *
2806 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2807 * Must not be called directly
2808 *
2809 * Return: status code
2810 */
2811static efi_status_t EFIAPI
2812efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2813 efi_va_list argptr)
2814{
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002815 const efi_guid_t *protocol, *next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002816 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002817 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002818 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002819 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002820
2821 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002822 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002823
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002824 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002825 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002826 for (;;) {
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002827 /*
2828 * If efi_uninstall_protocol() fails we need to be able to
2829 * reinstall the previously uninstalled protocols on the same
2830 * handle.
2831 * Instead of calling efi_uninstall_protocol(...,..., false)
2832 * and potentially removing the handle, only allow the handle
2833 * removal on the last protocol that we requested to uninstall.
2834 * That way we can preserve the handle in case the latter fails
2835 */
2836 bool preserve = true;
2837
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002838 if (!protocol)
2839 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002840 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002841 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2842 if (!next_protocol)
2843 preserve = false;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002844 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002845 protocol_interface, preserve);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002846 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002847 break;
2848 i++;
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002849 protocol = next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002850 }
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002851 if (ret == EFI_SUCCESS)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002852 goto out;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002853
2854 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002855 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002856 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2857 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002858 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2859 EFI_NATIVE_INTERFACE,
2860 protocol_interface));
2861 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002862 /*
2863 * If any errors are generated while the protocol interfaces are being
2864 * uninstalled, then the protocols uninstalled prior to the error will
2865 * be reinstalled using InstallProtocolInterface() and the status code
2866 * EFI_INVALID_PARAMETER is returned.
2867 */
2868 ret = EFI_INVALID_PARAMETER;
2869
2870out:
2871 efi_va_end(argptr_copy);
2872 return ret;
2873}
2874
2875/**
2876 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2877 * interfaces
2878 * @handle: handle from which the protocol interfaces shall be removed
2879 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2880 * interfaces
2881 *
2882 * This function implements the UninstallMultipleProtocolInterfaces service.
2883 *
2884 * This is the function for internal usage in U-Boot. For the API function
2885 * implementing the UninstallMultipleProtocolInterfaces service see
2886 * efi_uninstall_multiple_protocol_interfaces_ext()
2887 *
2888 * Return: status code
2889 */
2890efi_status_t EFIAPI
2891efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2892{
2893 efi_status_t ret;
2894 efi_va_list argptr;
2895
2896 efi_va_start(argptr, handle);
2897 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002898 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002899 return ret;
2900}
2901
2902/**
2903 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2904 * interfaces
2905 * @handle: handle from which the protocol interfaces shall be removed
2906 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2907 * interfaces
2908 *
2909 * This function implements the UninstallMultipleProtocolInterfaces service.
2910 *
2911 * See the Unified Extensible Firmware Interface (UEFI) specification for
2912 * details.
2913 *
2914 * Return: status code
2915 */
2916static efi_status_t EFIAPI
2917efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2918{
2919 EFI_ENTRY("%p", handle);
2920 efi_status_t ret;
2921 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002922
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002923 efi_va_start(argptr, handle);
2924 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2925 efi_va_end(argptr);
2926 return EFI_EXIT(ret);
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_calculate_crc32() - calculate cyclic redundancy code
2931 * @data: buffer with data
2932 * @data_size: size of buffer in bytes
2933 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002934 *
2935 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002936 *
2937 * See the Unified Extensible Firmware Interface (UEFI) specification for
2938 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002939 *
Mario Six8fac2912018-07-10 08:40:17 +02002940 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002941 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002942static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2943 efi_uintn_t data_size,
2944 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002945{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002946 efi_status_t ret = EFI_SUCCESS;
2947
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002948 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002949 if (!data || !data_size || !crc32_p) {
2950 ret = EFI_INVALID_PARAMETER;
2951 goto out;
2952 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002953 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002954out:
2955 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002956}
2957
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002958/**
Mario Six8fac2912018-07-10 08:40:17 +02002959 * efi_copy_mem() - copy memory
2960 * @destination: destination of the copy operation
2961 * @source: source of the copy operation
2962 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002963 *
2964 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002965 *
Mario Six8fac2912018-07-10 08:40:17 +02002966 * See the Unified Extensible Firmware Interface (UEFI) specification for
2967 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002968 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002969static void EFIAPI efi_copy_mem(void *destination, const void *source,
2970 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002971{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002972 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002973 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002974 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002975}
2976
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002977/**
Mario Six8fac2912018-07-10 08:40:17 +02002978 * efi_set_mem() - Fill memory with a byte value.
2979 * @buffer: buffer to fill
2980 * @size: size of buffer in bytes
2981 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002982 *
2983 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002984 *
Mario Six8fac2912018-07-10 08:40:17 +02002985 * See the Unified Extensible Firmware Interface (UEFI) specification for
2986 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002987 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002988static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002989{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002990 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002991 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002992 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002993}
2994
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002995/**
Mario Six8fac2912018-07-10 08:40:17 +02002996 * efi_protocol_open() - open protocol interface on a handle
2997 * @handler: handler of a protocol
2998 * @protocol_interface: interface implementing the protocol
2999 * @agent_handle: handle of the driver
3000 * @controller_handle: handle of the controller
3001 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003002 *
Mario Six8fac2912018-07-10 08:40:17 +02003003 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003004 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01003005efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003006 struct efi_handler *handler,
3007 void **protocol_interface, void *agent_handle,
3008 void *controller_handle, uint32_t attributes)
3009{
3010 struct efi_open_protocol_info_item *item;
3011 struct efi_open_protocol_info_entry *match = NULL;
3012 bool opened_by_driver = false;
3013 bool opened_exclusive = false;
3014
3015 /* If there is no agent, only return the interface */
3016 if (!agent_handle)
3017 goto out;
3018
3019 /* For TEST_PROTOCOL ignore interface attribute */
3020 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3021 *protocol_interface = NULL;
3022
3023 /*
3024 * Check if the protocol is already opened by a driver with the same
3025 * attributes or opened exclusively
3026 */
3027 list_for_each_entry(item, &handler->open_infos, link) {
3028 if (item->info.agent_handle == agent_handle) {
3029 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3030 (item->info.attributes == attributes))
3031 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003032 } else {
3033 if (item->info.attributes &
3034 EFI_OPEN_PROTOCOL_BY_DRIVER)
3035 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003036 }
3037 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3038 opened_exclusive = true;
3039 }
3040
3041 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003042 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3043 if (opened_exclusive)
3044 return EFI_ACCESS_DENIED;
3045 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3046 if (opened_exclusive || opened_by_driver)
3047 return EFI_ACCESS_DENIED;
3048 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003049
3050 /* Prepare exclusive opening */
3051 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3052 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003053disconnect_next:
3054 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003055 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003056 efi_status_t ret;
3057
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003058 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003059 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3060 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003061 item->info.controller_handle,
3062 item->info.agent_handle,
3063 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003064 if (ret == EFI_SUCCESS)
3065 /*
3066 * Child controllers may have been
3067 * removed from the open_infos list. So
3068 * let's restart the loop.
3069 */
3070 goto disconnect_next;
3071 else
3072 opened_by_driver = true;
3073 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003074 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003075 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003076 if (opened_by_driver)
3077 return EFI_ACCESS_DENIED;
3078 }
3079
3080 /* Find existing entry */
3081 list_for_each_entry(item, &handler->open_infos, link) {
3082 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003083 item->info.controller_handle == controller_handle &&
3084 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003085 match = &item->info;
3086 }
3087 /* None found, create one */
3088 if (!match) {
3089 match = efi_create_open_info(handler);
3090 if (!match)
3091 return EFI_OUT_OF_RESOURCES;
3092 }
3093
3094 match->agent_handle = agent_handle;
3095 match->controller_handle = controller_handle;
3096 match->attributes = attributes;
3097 match->open_count++;
3098
3099out:
3100 /* For TEST_PROTOCOL ignore interface attribute. */
3101 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3102 *protocol_interface = handler->protocol_interface;
3103
3104 return EFI_SUCCESS;
3105}
3106
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003107/**
Mario Six8fac2912018-07-10 08:40:17 +02003108 * efi_open_protocol() - open protocol interface on a handle
3109 * @handle: handle on which the protocol shall be opened
3110 * @protocol: GUID of the protocol
3111 * @protocol_interface: interface implementing the protocol
3112 * @agent_handle: handle of the driver
3113 * @controller_handle: handle of the controller
3114 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003115 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003116 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003117 *
Mario Six8fac2912018-07-10 08:40:17 +02003118 * See the Unified Extensible Firmware Interface (UEFI) specification for
3119 * details.
3120 *
3121 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003122 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003123static efi_status_t EFIAPI efi_open_protocol
3124 (efi_handle_t handle, const efi_guid_t *protocol,
3125 void **protocol_interface, efi_handle_t agent_handle,
3126 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003127{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003128 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003129 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003130
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003131 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003132 protocol_interface, agent_handle, controller_handle,
3133 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003134
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003135 if (!handle || !protocol ||
3136 (!protocol_interface && attributes !=
3137 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003138 goto out;
3139 }
3140
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003141 switch (attributes) {
3142 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3143 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3144 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3145 break;
3146 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3147 if (controller_handle == handle)
3148 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003149 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003150 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3151 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003152 /* Check that the controller handle is valid */
3153 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003154 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003155 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003156 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003157 /* Check that the agent handle is valid */
3158 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003159 goto out;
3160 break;
3161 default:
3162 goto out;
3163 }
3164
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003165 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003166 switch (r) {
3167 case EFI_SUCCESS:
3168 break;
3169 case EFI_NOT_FOUND:
3170 r = EFI_UNSUPPORTED;
3171 goto out;
3172 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003173 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003174 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003175
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003176 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3177 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003178out:
3179 return EFI_EXIT(r);
3180}
3181
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003182/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003183 * efi_start_image() - call the entry point of an image
3184 * @image_handle: handle of the image
3185 * @exit_data_size: size of the buffer
3186 * @exit_data: buffer to receive the exit data of the called image
3187 *
3188 * This function implements the StartImage service.
3189 *
3190 * See the Unified Extensible Firmware Interface (UEFI) specification for
3191 * details.
3192 *
3193 * Return: status code
3194 */
3195efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3196 efi_uintn_t *exit_data_size,
3197 u16 **exit_data)
3198{
3199 struct efi_loaded_image_obj *image_obj =
3200 (struct efi_loaded_image_obj *)image_handle;
3201 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003202 void *info;
3203 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003204 efi_status_t exit_status;
3205 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003206
3207 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3208
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003209 if (!efi_search_obj(image_handle))
3210 return EFI_EXIT(EFI_INVALID_PARAMETER);
3211
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003212 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003213 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3214 return EFI_EXIT(EFI_INVALID_PARAMETER);
3215
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003216 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3217 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3218
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003219 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3220 &info, NULL, NULL,
3221 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3222 if (ret != EFI_SUCCESS)
3223 return EFI_EXIT(EFI_INVALID_PARAMETER);
3224
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003225 image_obj->exit_data_size = exit_data_size;
3226 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003227 image_obj->exit_status = &exit_status;
3228 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003229
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003230 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3231 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003232 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003233 if (ret == EFI_SECURITY_VIOLATION) {
3234 /*
3235 * TCG2 Protocol is installed but no TPM device found,
3236 * this is not expected.
3237 */
3238 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003239 }
3240 }
3241 }
3242
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003243 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003244 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003245 /*
3246 * We called the entry point of the child image with EFI_CALL
3247 * in the lines below. The child image called the Exit() boot
3248 * service efi_exit() which executed the long jump that brought
3249 * us to the current line. This implies that the second half
3250 * of the EFI_CALL macro has not been executed.
3251 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003252#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003253 /*
3254 * efi_exit() called efi_restore_gd(). We have to undo this
3255 * otherwise __efi_entry_check() will put the wrong value into
3256 * app_gd.
3257 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003258 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003259#endif
3260 /*
3261 * To get ready to call EFI_EXIT below we have to execute the
3262 * missed out steps of EFI_CALL.
3263 */
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003264 EFI_RETURN(exit_status);
3265
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003266 current_image = parent_image;
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003267
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003268 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003269 }
3270
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003271 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003272 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003273 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003274 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3275
3276 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003277 * Control is returned from a started UEFI image either by calling
3278 * Exit() (where exit data can be provided) or by simply returning from
3279 * the entry point. In the latter case call Exit() on behalf of the
3280 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003281 */
3282 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3283}
3284
3285/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003286 * efi_delete_image() - delete loaded image from memory)
3287 *
3288 * @image_obj: handle of the loaded image
3289 * @loaded_image_protocol: loaded image protocol
3290 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003291static efi_status_t efi_delete_image
3292 (struct efi_loaded_image_obj *image_obj,
3293 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003294{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003295 struct efi_object *efiobj;
3296 efi_status_t r, ret = EFI_SUCCESS;
3297
3298close_next:
3299 list_for_each_entry(efiobj, &efi_obj_list, link) {
3300 struct efi_handler *protocol;
3301
3302 list_for_each_entry(protocol, &efiobj->protocols, link) {
3303 struct efi_open_protocol_info_item *info;
3304
3305 list_for_each_entry(info, &protocol->open_infos, link) {
3306 if (info->info.agent_handle !=
3307 (efi_handle_t)image_obj)
3308 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003309 r = efi_close_protocol(
3310 efiobj, &protocol->guid,
3311 info->info.agent_handle,
3312 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003313 if (r != EFI_SUCCESS)
3314 ret = r;
3315 /*
3316 * Closing protocols may results in further
3317 * items being deleted. To play it safe loop
3318 * over all elements again.
3319 */
3320 goto close_next;
3321 }
3322 }
3323 }
3324
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003325 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3326 efi_size_in_pages(loaded_image_protocol->image_size));
3327 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003328
3329 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003330}
3331
3332/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003333 * efi_unload_image() - unload an EFI image
3334 * @image_handle: handle of the image to be unloaded
3335 *
3336 * This function implements the UnloadImage service.
3337 *
3338 * See the Unified Extensible Firmware Interface (UEFI) specification for
3339 * details.
3340 *
3341 * Return: status code
3342 */
3343efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3344{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003345 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003346 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003347 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003348
3349 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003350
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003351 efiobj = efi_search_obj(image_handle);
3352 if (!efiobj) {
3353 ret = EFI_INVALID_PARAMETER;
3354 goto out;
3355 }
3356 /* Find the loaded image protocol */
3357 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3358 (void **)&loaded_image_protocol,
3359 NULL, NULL,
3360 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3361 if (ret != EFI_SUCCESS) {
3362 ret = EFI_INVALID_PARAMETER;
3363 goto out;
3364 }
3365 switch (efiobj->type) {
3366 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3367 /* Call the unload function */
3368 if (!loaded_image_protocol->unload) {
3369 ret = EFI_UNSUPPORTED;
3370 goto out;
3371 }
3372 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3373 if (ret != EFI_SUCCESS)
3374 goto out;
3375 break;
3376 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3377 break;
3378 default:
3379 ret = EFI_INVALID_PARAMETER;
3380 goto out;
3381 }
3382 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3383 loaded_image_protocol);
3384out:
3385 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003386}
3387
3388/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003389 * efi_update_exit_data() - fill exit data parameters of StartImage()
3390 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003391 * @image_obj: image handle
3392 * @exit_data_size: size of the exit data buffer
3393 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003394 * Return: status code
3395 */
3396static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3397 efi_uintn_t exit_data_size,
3398 u16 *exit_data)
3399{
3400 efi_status_t ret;
3401
3402 /*
3403 * If exit_data is not provided to StartImage(), exit_data_size must be
3404 * ignored.
3405 */
3406 if (!image_obj->exit_data)
3407 return EFI_SUCCESS;
3408 if (image_obj->exit_data_size)
3409 *image_obj->exit_data_size = exit_data_size;
3410 if (exit_data_size && exit_data) {
3411 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3412 exit_data_size,
3413 (void **)image_obj->exit_data);
3414 if (ret != EFI_SUCCESS)
3415 return ret;
3416 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3417 } else {
3418 image_obj->exit_data = NULL;
3419 }
3420 return EFI_SUCCESS;
3421}
3422
3423/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003424 * efi_exit() - leave an EFI application or driver
3425 * @image_handle: handle of the application or driver that is exiting
3426 * @exit_status: status code
3427 * @exit_data_size: size of the buffer in bytes
3428 * @exit_data: buffer with data describing an error
3429 *
3430 * This function implements the Exit service.
3431 *
3432 * See the Unified Extensible Firmware Interface (UEFI) specification for
3433 * details.
3434 *
3435 * Return: status code
3436 */
3437static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3438 efi_status_t exit_status,
3439 efi_uintn_t exit_data_size,
3440 u16 *exit_data)
3441{
3442 /*
3443 * TODO: We should call the unload procedure of the loaded
3444 * image protocol.
3445 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003446 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003447 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003448 struct efi_loaded_image_obj *image_obj =
3449 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003450 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003451
3452 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3453 exit_data_size, exit_data);
3454
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003455 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003456 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003457 (void **)&loaded_image_protocol,
3458 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003459 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003460 if (ret != EFI_SUCCESS) {
3461 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003462 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003463 }
3464
3465 /* Unloading of unstarted images */
3466 switch (image_obj->header.type) {
3467 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3468 break;
3469 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3470 efi_delete_image(image_obj, loaded_image_protocol);
3471 ret = EFI_SUCCESS;
3472 goto out;
3473 default:
3474 /* Handle does not refer to loaded image */
3475 ret = EFI_INVALID_PARAMETER;
3476 goto out;
3477 }
3478 /* A started image can only be unloaded it is the last one started. */
3479 if (image_handle != current_image) {
3480 ret = EFI_INVALID_PARAMETER;
3481 goto out;
3482 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003483
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003484 /* Exit data is only foreseen in case of failure. */
3485 if (exit_status != EFI_SUCCESS) {
3486 ret = efi_update_exit_data(image_obj, exit_data_size,
3487 exit_data);
3488 /* Exiting has priority. Don't return error to caller. */
3489 if (ret != EFI_SUCCESS)
3490 EFI_PRINT("%s: out of memory\n", __func__);
3491 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003492 /* efi_delete_image() frees image_obj. Copy before the call. */
3493 exit_jmp = image_obj->exit_jmp;
3494 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003495 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3496 exit_status != EFI_SUCCESS)
3497 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003498
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003499 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3500 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3501 ret = efi_tcg2_measure_efi_app_exit();
Heinrich Schuchardt265bfab2024-11-27 00:40:17 +01003502 if (ret != EFI_SUCCESS)
3503 log_debug("tcg2 measurement fails (0x%lx)\n",
3504 ret);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003505 }
3506 }
3507
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003508 /* Make sure entry/exit counts for EFI world cross-overs match */
3509 EFI_EXIT(exit_status);
3510
3511 /*
3512 * But longjmp out with the U-Boot gd, not the application's, as
3513 * the other end is a setjmp call inside EFI context.
3514 */
3515 efi_restore_gd();
3516
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003517 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003518
3519 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003520out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003521 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003522}
3523
3524/**
Mario Six8fac2912018-07-10 08:40:17 +02003525 * efi_handle_protocol() - get interface of a protocol on a handle
3526 * @handle: handle on which the protocol shall be opened
3527 * @protocol: GUID of the protocol
3528 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003529 *
3530 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003531 *
3532 * See the Unified Extensible Firmware Interface (UEFI) specification for
3533 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003534 *
Mario Six8fac2912018-07-10 08:40:17 +02003535 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003536 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003537efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3538 const efi_guid_t *protocol,
3539 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003540{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003541 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003542 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003543}
3544
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003545/**
Mario Six8fac2912018-07-10 08:40:17 +02003546 * efi_bind_controller() - bind a single driver to a controller
3547 * @controller_handle: controller handle
3548 * @driver_image_handle: driver handle
3549 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003550 *
Mario Six8fac2912018-07-10 08:40:17 +02003551 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003552 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003553static efi_status_t efi_bind_controller(
3554 efi_handle_t controller_handle,
3555 efi_handle_t driver_image_handle,
3556 struct efi_device_path *remain_device_path)
3557{
3558 struct efi_driver_binding_protocol *binding_protocol;
3559 efi_status_t r;
3560
3561 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3562 &efi_guid_driver_binding_protocol,
3563 (void **)&binding_protocol,
3564 driver_image_handle, NULL,
3565 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3566 if (r != EFI_SUCCESS)
3567 return r;
3568 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3569 controller_handle,
3570 remain_device_path));
3571 if (r == EFI_SUCCESS)
3572 r = EFI_CALL(binding_protocol->start(binding_protocol,
3573 controller_handle,
3574 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003575 efi_close_protocol(driver_image_handle,
3576 &efi_guid_driver_binding_protocol,
3577 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003578 return r;
3579}
3580
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003581/**
Mario Six8fac2912018-07-10 08:40:17 +02003582 * efi_connect_single_controller() - connect a single driver to a controller
3583 * @controller_handle: controller
3584 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003585 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003586 *
Mario Six8fac2912018-07-10 08:40:17 +02003587 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003588 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003589static efi_status_t efi_connect_single_controller(
3590 efi_handle_t controller_handle,
3591 efi_handle_t *driver_image_handle,
3592 struct efi_device_path *remain_device_path)
3593{
3594 efi_handle_t *buffer;
3595 size_t count;
3596 size_t i;
3597 efi_status_t r;
3598 size_t connected = 0;
3599
3600 /* Get buffer with all handles with driver binding protocol */
3601 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3602 &efi_guid_driver_binding_protocol,
3603 NULL, &count, &buffer));
3604 if (r != EFI_SUCCESS)
3605 return r;
3606
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003607 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003608 if (driver_image_handle) {
3609 for (; *driver_image_handle; ++driver_image_handle) {
3610 for (i = 0; i < count; ++i) {
3611 if (buffer[i] == *driver_image_handle) {
3612 buffer[i] = NULL;
3613 r = efi_bind_controller(
3614 controller_handle,
3615 *driver_image_handle,
3616 remain_device_path);
3617 /*
3618 * For drivers that do not support the
3619 * controller or are already connected
3620 * we receive an error code here.
3621 */
3622 if (r == EFI_SUCCESS)
3623 ++connected;
3624 }
3625 }
3626 }
3627 }
3628
3629 /*
3630 * TODO: Some overrides are not yet implemented:
3631 * - Platform Driver Override
3632 * - Driver Family Override Search
3633 * - Bus Specific Driver Override
3634 */
3635
3636 /* Driver Binding Search */
3637 for (i = 0; i < count; ++i) {
3638 if (buffer[i]) {
3639 r = efi_bind_controller(controller_handle,
3640 buffer[i],
3641 remain_device_path);
3642 if (r == EFI_SUCCESS)
3643 ++connected;
3644 }
3645 }
3646
3647 efi_free_pool(buffer);
3648 if (!connected)
3649 return EFI_NOT_FOUND;
3650 return EFI_SUCCESS;
3651}
3652
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003653/**
Mario Six8fac2912018-07-10 08:40:17 +02003654 * efi_connect_controller() - connect a controller to a driver
3655 * @controller_handle: handle of the controller
3656 * @driver_image_handle: handle of the driver
3657 * @remain_device_path: device path of a child controller
3658 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003659 *
3660 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003661 *
3662 * See the Unified Extensible Firmware Interface (UEFI) specification for
3663 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003664 *
3665 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003666 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003667 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3668 *
Mario Six8fac2912018-07-10 08:40:17 +02003669 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003670 */
3671static efi_status_t EFIAPI efi_connect_controller(
3672 efi_handle_t controller_handle,
3673 efi_handle_t *driver_image_handle,
3674 struct efi_device_path *remain_device_path,
3675 bool recursive)
3676{
3677 efi_status_t r;
3678 efi_status_t ret = EFI_NOT_FOUND;
3679 struct efi_object *efiobj;
3680
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003681 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003682 remain_device_path, recursive);
3683
3684 efiobj = efi_search_obj(controller_handle);
3685 if (!efiobj) {
3686 ret = EFI_INVALID_PARAMETER;
3687 goto out;
3688 }
3689
3690 r = efi_connect_single_controller(controller_handle,
3691 driver_image_handle,
3692 remain_device_path);
3693 if (r == EFI_SUCCESS)
3694 ret = EFI_SUCCESS;
3695 if (recursive) {
3696 struct efi_handler *handler;
3697 struct efi_open_protocol_info_item *item;
3698
3699 list_for_each_entry(handler, &efiobj->protocols, link) {
3700 list_for_each_entry(item, &handler->open_infos, link) {
3701 if (item->info.attributes &
3702 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3703 r = EFI_CALL(efi_connect_controller(
3704 item->info.controller_handle,
3705 driver_image_handle,
3706 remain_device_path,
3707 recursive));
3708 if (r == EFI_SUCCESS)
3709 ret = EFI_SUCCESS;
3710 }
3711 }
3712 }
3713 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003714 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003715 if (ret != EFI_SUCCESS && remain_device_path &&
3716 remain_device_path->type == DEVICE_PATH_TYPE_END)
3717 ret = EFI_SUCCESS;
3718out:
3719 return EFI_EXIT(ret);
3720}
3721
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003722/**
Mario Six8fac2912018-07-10 08:40:17 +02003723 * efi_reinstall_protocol_interface() - reinstall protocol interface
3724 * @handle: handle on which the protocol shall be reinstalled
3725 * @protocol: GUID of the protocol to be installed
3726 * @old_interface: interface to be removed
3727 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003728 *
3729 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003730 *
3731 * See the Unified Extensible Firmware Interface (UEFI) specification for
3732 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003733 *
3734 * The old interface is uninstalled. The new interface is installed.
3735 * Drivers are connected.
3736 *
Mario Six8fac2912018-07-10 08:40:17 +02003737 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003738 */
Adriano Cordova3d4b09a2024-12-06 14:18:34 -03003739efi_status_t EFIAPI efi_reinstall_protocol_interface(
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003740 efi_handle_t handle, const efi_guid_t *protocol,
3741 void *old_interface, void *new_interface)
3742{
3743 efi_status_t ret;
3744
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003745 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003746 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003747
3748 /* Uninstall protocol but do not delete handle */
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03003749 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003750 if (ret != EFI_SUCCESS)
3751 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003752
3753 /* Install the new protocol */
3754 ret = efi_add_protocol(handle, protocol, new_interface);
3755 /*
3756 * The UEFI spec does not specify what should happen to the handle
3757 * if in case of an error no protocol interface remains on the handle.
3758 * So let's do nothing here.
3759 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003760 if (ret != EFI_SUCCESS)
3761 goto out;
3762 /*
3763 * The returned status code has to be ignored.
3764 * Do not create an error if no suitable driver for the handle exists.
3765 */
3766 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3767out:
3768 return EFI_EXIT(ret);
3769}
3770
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003771/**
Mario Six8fac2912018-07-10 08:40:17 +02003772 * efi_get_child_controllers() - get all child controllers associated to a driver
3773 * @efiobj: handle of the controller
3774 * @driver_handle: handle of the driver
3775 * @number_of_children: number of child controllers
3776 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003777 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003778 * The allocated buffer has to be freed with free().
3779 *
Mario Six8fac2912018-07-10 08:40:17 +02003780 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003781 */
3782static efi_status_t efi_get_child_controllers(
3783 struct efi_object *efiobj,
3784 efi_handle_t driver_handle,
3785 efi_uintn_t *number_of_children,
3786 efi_handle_t **child_handle_buffer)
3787{
3788 struct efi_handler *handler;
3789 struct efi_open_protocol_info_item *item;
3790 efi_uintn_t count = 0, i;
3791 bool duplicate;
3792
3793 /* Count all child controller associations */
3794 list_for_each_entry(handler, &efiobj->protocols, link) {
3795 list_for_each_entry(item, &handler->open_infos, link) {
3796 if (item->info.agent_handle == driver_handle &&
3797 item->info.attributes &
3798 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3799 ++count;
3800 }
3801 }
3802 /*
3803 * Create buffer. In case of duplicate child controller assignments
3804 * the buffer will be too large. But that does not harm.
3805 */
3806 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003807 if (!count)
3808 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003809 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3810 if (!*child_handle_buffer)
3811 return EFI_OUT_OF_RESOURCES;
3812 /* Copy unique child handles */
3813 list_for_each_entry(handler, &efiobj->protocols, link) {
3814 list_for_each_entry(item, &handler->open_infos, link) {
3815 if (item->info.agent_handle == driver_handle &&
3816 item->info.attributes &
3817 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3818 /* Check this is a new child controller */
3819 duplicate = false;
3820 for (i = 0; i < *number_of_children; ++i) {
3821 if ((*child_handle_buffer)[i] ==
3822 item->info.controller_handle)
3823 duplicate = true;
3824 }
3825 /* Copy handle to buffer */
3826 if (!duplicate) {
3827 i = (*number_of_children)++;
3828 (*child_handle_buffer)[i] =
3829 item->info.controller_handle;
3830 }
3831 }
3832 }
3833 }
3834 return EFI_SUCCESS;
3835}
3836
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003837/**
Mario Six8fac2912018-07-10 08:40:17 +02003838 * efi_disconnect_controller() - disconnect a controller from a driver
3839 * @controller_handle: handle of the controller
3840 * @driver_image_handle: handle of the driver
3841 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003842 *
3843 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003844 *
3845 * See the Unified Extensible Firmware Interface (UEFI) specification for
3846 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003847 *
Mario Six8fac2912018-07-10 08:40:17 +02003848 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003849 */
3850static efi_status_t EFIAPI efi_disconnect_controller(
3851 efi_handle_t controller_handle,
3852 efi_handle_t driver_image_handle,
3853 efi_handle_t child_handle)
3854{
3855 struct efi_driver_binding_protocol *binding_protocol;
3856 efi_handle_t *child_handle_buffer = NULL;
3857 size_t number_of_children = 0;
3858 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003859 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003860 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003861
3862 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3863 child_handle);
3864
3865 efiobj = efi_search_obj(controller_handle);
3866 if (!efiobj) {
3867 r = EFI_INVALID_PARAMETER;
3868 goto out;
3869 }
3870
3871 if (child_handle && !efi_search_obj(child_handle)) {
3872 r = EFI_INVALID_PARAMETER;
3873 goto out;
3874 }
3875
3876 /* If no driver handle is supplied, disconnect all drivers */
3877 if (!driver_image_handle) {
3878 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3879 goto out;
3880 }
3881
3882 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003883 r = efi_get_child_controllers(efiobj,
3884 driver_image_handle,
3885 &number_of_children,
3886 &child_handle_buffer);
3887 if (r != EFI_SUCCESS)
3888 return r;
3889 sole_child = (number_of_children == 1);
3890
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003891 if (child_handle) {
3892 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003893 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003894 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003895 }
3896
3897 /* Get the driver binding protocol */
3898 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3899 &efi_guid_driver_binding_protocol,
3900 (void **)&binding_protocol,
3901 driver_image_handle, NULL,
3902 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003903 if (r != EFI_SUCCESS) {
3904 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003905 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003906 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003907 /* Remove the children */
3908 if (number_of_children) {
3909 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3910 controller_handle,
3911 number_of_children,
3912 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003913 if (r != EFI_SUCCESS) {
3914 r = EFI_DEVICE_ERROR;
3915 goto out;
3916 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003917 }
3918 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003919 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003920 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3921 controller_handle,
3922 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003923 if (r != EFI_SUCCESS) {
3924 r = EFI_DEVICE_ERROR;
3925 goto out;
3926 }
3927 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003928 efi_close_protocol(driver_image_handle,
3929 &efi_guid_driver_binding_protocol,
3930 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003931 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003932out:
3933 if (!child_handle)
3934 free(child_handle_buffer);
3935 return EFI_EXIT(r);
3936}
3937
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003938static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003939 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003940 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3941 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003942 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003943 },
3944 .raise_tpl = efi_raise_tpl,
3945 .restore_tpl = efi_restore_tpl,
3946 .allocate_pages = efi_allocate_pages_ext,
3947 .free_pages = efi_free_pages_ext,
3948 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003949 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003950 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003951 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003952 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003953 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003954 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003955 .close_event = efi_close_event,
3956 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003957 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003958 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003959 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003960 .handle_protocol = efi_handle_protocol,
3961 .reserved = NULL,
3962 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003963 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003964 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003965 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003966 .load_image = efi_load_image,
3967 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003968 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003969 .unload_image = efi_unload_image,
3970 .exit_boot_services = efi_exit_boot_services,
3971 .get_next_monotonic_count = efi_get_next_monotonic_count,
3972 .stall = efi_stall,
3973 .set_watchdog_timer = efi_set_watchdog_timer,
3974 .connect_controller = efi_connect_controller,
3975 .disconnect_controller = efi_disconnect_controller,
3976 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003977 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003978 .open_protocol_information = efi_open_protocol_information,
3979 .protocols_per_handle = efi_protocols_per_handle,
3980 .locate_handle_buffer = efi_locate_handle_buffer,
3981 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003982 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003983 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003984 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003985 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003986 .calculate_crc32 = efi_calculate_crc32,
3987 .copy_mem = efi_copy_mem,
3988 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003989 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003990};
3991
Simon Glass90975372022-01-23 12:55:12 -07003992static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003993
Alexander Graf393dd912016-10-14 13:45:30 +02003994struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003995 .hdr = {
3996 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003997 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003998 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003999 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02004000 .fw_vendor = firmware_vendor,
4001 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02004002 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01004003 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004004 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01004005};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004006
4007/**
4008 * efi_initialize_system_table() - Initialize system table
4009 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02004010 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004011 */
4012efi_status_t efi_initialize_system_table(void)
4013{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004014 efi_status_t ret;
4015
4016 /* Allocate configuration table array */
4017 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4018 EFI_MAX_CONFIGURATION_TABLES *
4019 sizeof(struct efi_configuration_table),
4020 (void **)&systab.tables);
4021
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004022 /*
4023 * These entries will be set to NULL in ExitBootServices(). To avoid
4024 * relocation in SetVirtualAddressMap(), set them dynamically.
4025 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01004026 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004027 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004028 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004029 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004030 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004031 systab.std_err = &efi_con_out;
4032 systab.boottime = &efi_boot_services;
4033
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02004034 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004035 efi_update_table_header_crc32(&systab.hdr);
4036 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4037 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004038
4039 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004040}