blob: f220daa048f4a065f884092a8b0defe48f137a9f [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>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030025#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010026
27DECLARE_GLOBAL_DATA_PTR;
28
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020029/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010030static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020031
Alexander Grafc15d9212016-03-04 01:09:59 +010032/* This list contains all the EFI objects our payload has access to */
33LIST_HEAD(efi_obj_list);
34
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010035/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020036__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010037
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020038/* List of queued events */
Bin Mengd89dea32023-04-05 20:15:17 +080039static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020040
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020041/* Flag to disable timer activity in ExitBootServices() */
42static bool timers_enabled = true;
43
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010044/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
45bool efi_st_keep_devices;
46
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020047/* List of all events registered by RegisterProtocolNotify() */
Bin Mengd89dea32023-04-05 20:15:17 +080048static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020049
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010050/* Handle of the currently executing image */
51static efi_handle_t current_image;
52
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020053#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010054/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020055 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010056 * fixed when compiling U-Boot. However, the payload does not know about that
57 * restriction so we need to manually swap its and our view of that register on
58 * EFI callback entry/exit.
59 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020060static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060061#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010062
Adriano Cordova7f2bcd42025-03-03 11:13:11 -030063efi_status_t efi_uninstall_protocol
64 (efi_handle_t handle, const efi_guid_t *protocol,
65 void *protocol_interface, bool preserve);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030066
Heinrich Schuchardt72506232019-02-09 14:10:39 +010067/* 1 if inside U-Boot code, 0 if inside EFI payload code */
68static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040069static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010070/* GUID of the device tree table */
71const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010072/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
73const efi_guid_t efi_guid_driver_binding_protocol =
74 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040075
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010076/* event group ExitBootServices() invoked */
77const efi_guid_t efi_guid_event_group_exit_boot_services =
78 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt44772c42021-11-16 18:46:27 +010079/* event group before ExitBootServices() invoked */
80const efi_guid_t efi_guid_event_group_before_exit_boot_services =
81 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010082/* event group SetVirtualAddressMap() invoked */
83const efi_guid_t efi_guid_event_group_virtual_address_change =
84 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
85/* event group memory map changed */
86const efi_guid_t efi_guid_event_group_memory_map_change =
87 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
88/* event group boot manager about to boot */
89const efi_guid_t efi_guid_event_group_ready_to_boot =
90 EFI_EVENT_GROUP_READY_TO_BOOT;
91/* event group ResetSystem() invoked (before ExitBootServices) */
92const efi_guid_t efi_guid_event_group_reset_system =
93 EFI_EVENT_GROUP_RESET_SYSTEM;
Masahisa Kojima1923f362023-11-10 13:25:39 +090094/* event group return to efibootmgr */
95const efi_guid_t efi_guid_event_group_return_to_efibootmgr =
96 EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010097/* GUIDs of the Load File and Load File2 protocols */
98const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
99const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900100/* GUID of the SMBIOS table */
101const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +0100102
Adriano Cordova7f2bcd42025-03-03 11:13:11 -0300103efi_status_t EFIAPI efi_disconnect_controller(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100104 efi_handle_t controller_handle,
105 efi_handle_t driver_image_handle,
106 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +0100107
Ilias Apalodimas1e10d622023-06-20 09:19:28 +0300108efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
109 efi_handle_t *driver_image_handle,
110 struct efi_device_path *remain_device_path,
111 bool recursive);
112
Rob Clark86789d52017-07-27 08:04:18 -0400113/* Called on every callback entry */
114int __efi_entry_check(void)
115{
116 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200117#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400118 assert(efi_gd);
119 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200120 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400121#endif
122 return ret;
123}
124
125/* Called on every callback exit */
126int __efi_exit_check(void)
127{
128 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200129#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200130 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400131#endif
132 return ret;
133}
134
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200135/**
136 * efi_save_gd() - save global data register
137 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200138 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200139 * As this register may be overwritten by an EFI payload we save it here
140 * and restore it on every callback entered.
141 *
142 * This function is called after relocation from initr_reloc_global_data().
143 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100144void efi_save_gd(void)
145{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200146#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100147 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600148#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100149}
150
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200151/**
152 * efi_restore_gd() - restore global data register
153 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200154 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200155 * Restore it after returning from the UEFI world to the value saved via
156 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400157 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100158void efi_restore_gd(void)
159{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200160#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100161 /* Only restore if we're already in EFI context */
162 if (!efi_gd)
163 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200164 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600165#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100166}
167
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200168/**
Mario Six8fac2912018-07-10 08:40:17 +0200169 * indent_string() - returns a string for indenting with two spaces per level
170 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100171 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200172 * A maximum of ten indent levels is supported. Higher indent levels will be
173 * truncated.
174 *
Mario Six8fac2912018-07-10 08:40:17 +0200175 * Return: A string for indenting with two spaces per level is
176 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400177 */
178static const char *indent_string(int level)
179{
180 const char *indent = " ";
181 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100182
Rob Clarke7896c32017-07-27 08:04:19 -0400183 level = min(max, level * 2);
184 return &indent[max - level];
185}
186
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200187const char *__efi_nesting(void)
188{
189 return indent_string(nesting_level);
190}
191
Rob Clarke7896c32017-07-27 08:04:19 -0400192const char *__efi_nesting_inc(void)
193{
194 return indent_string(nesting_level++);
195}
196
197const char *__efi_nesting_dec(void)
198{
199 return indent_string(--nesting_level);
200}
201
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200202/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200203 * efi_event_is_queued() - check if an event is queued
204 *
205 * @event: event
206 * Return: true if event is queued
207 */
208static bool efi_event_is_queued(struct efi_event *event)
209{
210 return !!event->queue_link.next;
211}
212
213/**
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300214 * efi_purge_handle() - Clean the deleted handle from the various lists
215 * @handle: handle to remove
216 *
217 * Return: status code
218 */
219static efi_status_t efi_purge_handle(efi_handle_t handle)
220{
221 struct efi_register_notify_event *item;
222
223 if (!list_empty(&handle->protocols))
224 return EFI_ACCESS_DENIED;
225 /* The handle is about to be freed. Remove it from events */
226 list_for_each_entry(item, &efi_register_notify_events, link) {
227 struct efi_protocol_notification *hitem, *hnext;
228
229 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
230 if (handle == hitem->handle) {
231 list_del(&hitem->link);
232 free(hitem);
233 }
234 }
235 }
236 /* The last protocol has been removed, delete the handle. */
237 list_del(&handle->link);
238 free(handle);
239
240 return EFI_SUCCESS;
241}
242
243/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200244 * efi_process_event_queue() - process event queue
245 */
246static void efi_process_event_queue(void)
247{
248 while (!list_empty(&efi_event_queue)) {
249 struct efi_event *event;
250 efi_uintn_t old_tpl;
251
252 event = list_first_entry(&efi_event_queue, struct efi_event,
253 queue_link);
254 if (efi_tpl >= event->notify_tpl)
255 return;
256 list_del(&event->queue_link);
257 event->queue_link.next = NULL;
258 event->queue_link.prev = NULL;
259 /* Events must be executed at the event's TPL */
260 old_tpl = efi_tpl;
261 efi_tpl = event->notify_tpl;
262 EFI_CALL_VOID(event->notify_function(event,
263 event->notify_context));
264 efi_tpl = old_tpl;
265 if (event->type == EVT_NOTIFY_SIGNAL)
266 event->is_signaled = 0;
267 }
268}
269
270/**
Mario Six8fac2912018-07-10 08:40:17 +0200271 * efi_queue_event() - queue an EFI event
272 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200273 *
274 * This function queues the notification function of the event for future
275 * execution.
276 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200277 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200278static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200279{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100280 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200281
282 if (!event->notify_function)
283 return;
284
285 if (!efi_event_is_queued(event)) {
286 /*
287 * Events must be notified in order of decreasing task priority
288 * level. Insert the new event accordingly.
289 */
290 list_for_each_entry(item, &efi_event_queue, queue_link) {
291 if (item->notify_tpl < event->notify_tpl) {
292 list_add_tail(&event->queue_link,
293 &item->queue_link);
294 event = NULL;
295 break;
296 }
297 }
298 if (event)
299 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100300 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200301 }
302}
303
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200304/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200305 * is_valid_tpl() - check if the task priority level is valid
306 *
307 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200308 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200309 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100310static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200311{
312 switch (tpl) {
313 case TPL_APPLICATION:
314 case TPL_CALLBACK:
315 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200316 return EFI_SUCCESS;
317 default:
318 return EFI_INVALID_PARAMETER;
319 }
320}
321
322/**
Mario Six8fac2912018-07-10 08:40:17 +0200323 * efi_signal_event() - signal an EFI event
324 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100325 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100326 * This function signals an event. If the event belongs to an event group, all
327 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100328 * their notification function is queued.
329 *
330 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100331 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200332void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100333{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200334 if (event->is_signaled)
335 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100336 if (event->group) {
337 struct efi_event *evt;
338
339 /*
340 * The signaled state has to set before executing any
341 * notification function
342 */
343 list_for_each_entry(evt, &efi_events, link) {
344 if (!evt->group || guidcmp(evt->group, event->group))
345 continue;
346 if (evt->is_signaled)
347 continue;
348 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100349 }
350 list_for_each_entry(evt, &efi_events, link) {
351 if (!evt->group || guidcmp(evt->group, event->group))
352 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200353 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100354 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200355 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100356 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200357 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100358 }
359}
360
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200361/**
Mario Six8fac2912018-07-10 08:40:17 +0200362 * efi_raise_tpl() - raise the task priority level
363 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200364 *
365 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200366 *
367 * See the Unified Extensible Firmware Interface (UEFI) specification for
368 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200369 *
Mario Six8fac2912018-07-10 08:40:17 +0200370 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200371 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100372static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100373{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100374 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200375
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200376 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200377
378 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200379 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200380 efi_tpl = new_tpl;
381 if (efi_tpl > TPL_HIGH_LEVEL)
382 efi_tpl = TPL_HIGH_LEVEL;
383
384 EFI_EXIT(EFI_SUCCESS);
385 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100386}
387
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200388/**
Mario Six8fac2912018-07-10 08:40:17 +0200389 * efi_restore_tpl() - lower the task priority level
390 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 *
392 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200393 *
Mario Six8fac2912018-07-10 08:40:17 +0200394 * See the Unified Extensible Firmware Interface (UEFI) specification for
395 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100397static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100398{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200399 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200400
401 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200402 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200403 efi_tpl = old_tpl;
404 if (efi_tpl > TPL_HIGH_LEVEL)
405 efi_tpl = TPL_HIGH_LEVEL;
406
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100407 /*
408 * Lowering the TPL may have made queued events eligible for execution.
409 */
410 efi_timer_check();
411
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200412 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100413}
414
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200415/**
Mario Six8fac2912018-07-10 08:40:17 +0200416 * efi_allocate_pages_ext() - allocate memory pages
417 * @type: type of allocation to be performed
418 * @memory_type: usage type of the allocated memory
419 * @pages: number of pages to be allocated
420 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200421 *
422 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 *
Mario Six8fac2912018-07-10 08:40:17 +0200424 * See the Unified Extensible Firmware Interface (UEFI) specification for
425 * details.
426 *
427 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200428 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900429static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100430 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900431 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100432{
433 efi_status_t r;
434
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100435 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100436 r = efi_allocate_pages(type, memory_type, pages, memory);
437 return EFI_EXIT(r);
438}
439
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200440/**
Mario Six8fac2912018-07-10 08:40:17 +0200441 * efi_free_pages_ext() - Free memory pages.
442 * @memory: start of the memory area to be freed
443 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200444 *
445 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200446 *
447 * See the Unified Extensible Firmware Interface (UEFI) specification for
448 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200449 *
Mario Six8fac2912018-07-10 08:40:17 +0200450 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200451 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900452static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100453 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100454{
455 efi_status_t r;
456
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900457 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100458 r = efi_free_pages(memory, pages);
459 return EFI_EXIT(r);
460}
461
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200462/**
Mario Six8fac2912018-07-10 08:40:17 +0200463 * efi_get_memory_map_ext() - get map describing memory usage
464 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
465 * on exit the size of the copied memory map
466 * @memory_map: buffer to which the memory map is written
467 * @map_key: key for the memory map
468 * @descriptor_size: size of an individual memory descriptor
469 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200470 *
471 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200472 *
Mario Six8fac2912018-07-10 08:40:17 +0200473 * See the Unified Extensible Firmware Interface (UEFI) specification for
474 * details.
475 *
476 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200477 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900478static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100479 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900480 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100481 efi_uintn_t *map_key,
482 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900483 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100484{
485 efi_status_t r;
486
487 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
488 map_key, descriptor_size, descriptor_version);
489 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
490 descriptor_size, descriptor_version);
491 return EFI_EXIT(r);
492}
493
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200494/**
Mario Six8fac2912018-07-10 08:40:17 +0200495 * efi_allocate_pool_ext() - allocate memory from pool
496 * @pool_type: type of the pool from which memory is to be allocated
497 * @size: number of bytes to be allocated
498 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200499 *
500 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200501 *
502 * See the Unified Extensible Firmware Interface (UEFI) specification for
503 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200504 *
Mario Six8fac2912018-07-10 08:40:17 +0200505 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200506 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200507static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100508 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200509 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100510{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100511 efi_status_t r;
512
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100513 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200514 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100515 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100516}
517
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200518/**
Mario Six8fac2912018-07-10 08:40:17 +0200519 * efi_free_pool_ext() - free memory from pool
520 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200521 *
522 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200523 *
Mario Six8fac2912018-07-10 08:40:17 +0200524 * See the Unified Extensible Firmware Interface (UEFI) specification for
525 * details.
526 *
527 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200528 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200529static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100530{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100531 efi_status_t r;
532
533 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200534 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100535 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100536}
537
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200538/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200539 * efi_add_handle() - add a new handle to the object list
540 *
541 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100542 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200543 * The protocols list is initialized. The handle is added to the list of known
544 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100545 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200546void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100547{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200548 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100549 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200550 INIT_LIST_HEAD(&handle->protocols);
551 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100552}
553
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200554/**
Mario Six8fac2912018-07-10 08:40:17 +0200555 * efi_create_handle() - create handle
556 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200557 *
Mario Six8fac2912018-07-10 08:40:17 +0200558 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200559 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100560efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200561{
562 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200563
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200564 obj = calloc(1, sizeof(struct efi_object));
565 if (!obj)
566 return EFI_OUT_OF_RESOURCES;
567
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100568 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200569 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200570
571 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200572}
573
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200574/**
Mario Six8fac2912018-07-10 08:40:17 +0200575 * efi_search_protocol() - find a protocol on a handle.
576 * @handle: handle
577 * @protocol_guid: GUID of the protocol
578 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100579 *
Mario Six8fac2912018-07-10 08:40:17 +0200580 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100582efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100583 const efi_guid_t *protocol_guid,
584 struct efi_handler **handler)
585{
586 struct efi_object *efiobj;
587 struct list_head *lhandle;
588
589 if (!handle || !protocol_guid)
590 return EFI_INVALID_PARAMETER;
591 efiobj = efi_search_obj(handle);
592 if (!efiobj)
593 return EFI_INVALID_PARAMETER;
594 list_for_each(lhandle, &efiobj->protocols) {
595 struct efi_handler *protocol;
596
597 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100598 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599 if (handler)
600 *handler = protocol;
601 return EFI_SUCCESS;
602 }
603 }
604 return EFI_NOT_FOUND;
605}
606
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200607/**
Mario Six8fac2912018-07-10 08:40:17 +0200608 * efi_remove_protocol() - delete protocol from a handle
609 * @handle: handle from which the protocol shall be deleted
610 * @protocol: GUID of the protocol to be deleted
611 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100612 *
Mario Six8fac2912018-07-10 08:40:17 +0200613 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100614 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300615static efi_status_t efi_remove_protocol(const efi_handle_t handle,
616 const efi_guid_t *protocol,
617 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100618{
619 struct efi_handler *handler;
620 efi_status_t ret;
621
622 ret = efi_search_protocol(handle, protocol, &handler);
623 if (ret != EFI_SUCCESS)
624 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200625 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200626 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627 list_del(&handler->link);
628 free(handler);
629 return EFI_SUCCESS;
630}
631
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200632/**
Mario Six8fac2912018-07-10 08:40:17 +0200633 * efi_remove_all_protocols() - delete all protocols from a handle
634 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100635 *
Mario Six8fac2912018-07-10 08:40:17 +0200636 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100637 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100638static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100639{
640 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100641 struct efi_handler *protocol;
642 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100643
644 efiobj = efi_search_obj(handle);
645 if (!efiobj)
646 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100647 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100648 efi_status_t ret;
649
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300650 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300651 protocol->protocol_interface, true);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100652 if (ret != EFI_SUCCESS)
653 return ret;
654 }
655 return EFI_SUCCESS;
656}
657
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200658/**
Mario Six8fac2912018-07-10 08:40:17 +0200659 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100660 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200661 * @handle: handle to delete
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300662 *
663 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100664 */
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300665efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100666{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200667 efi_status_t ret;
668
669 ret = efi_remove_all_protocols(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300670 if (ret != EFI_SUCCESS) {
671 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
672 return ret;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200673 }
674
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300675 return efi_purge_handle(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100676}
677
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200678/**
Mario Six8fac2912018-07-10 08:40:17 +0200679 * efi_is_event() - check if a pointer is a valid event
680 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100681 *
Mario Six8fac2912018-07-10 08:40:17 +0200682 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100683 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100684static efi_status_t efi_is_event(const struct efi_event *event)
685{
686 const struct efi_event *evt;
687
688 if (!event)
689 return EFI_INVALID_PARAMETER;
690 list_for_each_entry(evt, &efi_events, link) {
691 if (evt == event)
692 return EFI_SUCCESS;
693 }
694 return EFI_INVALID_PARAMETER;
695}
Alexander Grafc15d9212016-03-04 01:09:59 +0100696
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200697/**
Mario Six8fac2912018-07-10 08:40:17 +0200698 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200699 *
Mario Six8fac2912018-07-10 08:40:17 +0200700 * @type: type of the event to create
701 * @notify_tpl: task priority level of the event
702 * @notify_function: notification function of the event
703 * @notify_context: pointer passed to the notification function
704 * @group: event group
705 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200706 *
707 * This function is used inside U-Boot code to create an event.
708 *
709 * For the API function implementing the CreateEvent service see
710 * efi_create_event_ext.
711 *
Mario Six8fac2912018-07-10 08:40:17 +0200712 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200713 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100714efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200715 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200716 struct efi_event *event,
717 void *context),
Masahisa Kojima787774a2023-11-10 13:25:38 +0900718 void *notify_context, const efi_guid_t *group,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100719 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100720{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100721 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200722 efi_status_t ret;
723 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200724
Jonathan Gray7758b212017-03-12 19:26:07 +1100725 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200726 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100727
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200728 switch (type) {
729 case 0:
730 case EVT_TIMER:
731 case EVT_NOTIFY_SIGNAL:
732 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
733 case EVT_NOTIFY_WAIT:
734 case EVT_TIMER | EVT_NOTIFY_WAIT:
735 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200736 pool_type = EFI_BOOT_SERVICES_DATA;
737 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200738 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200739 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200740 break;
741 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200742 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200743 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100744
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100745 /*
746 * The UEFI specification requires event notification levels to be
747 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
748 *
749 * Parameter NotifyTpl should not be checked if it is not used.
750 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900751 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100752 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
753 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200754 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100755
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200756 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
757 (void **)&evt);
758 if (ret != EFI_SUCCESS)
759 return ret;
760 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100761 evt->type = type;
762 evt->notify_tpl = notify_tpl;
763 evt->notify_function = notify_function;
764 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100765 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200766 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100767 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100768 list_add_tail(&evt->link, &efi_events);
769 *event = evt;
770 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100771}
772
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200773/*
Mario Six8fac2912018-07-10 08:40:17 +0200774 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100775 *
Mario Six8fac2912018-07-10 08:40:17 +0200776 * @type: type of the event to create
777 * @notify_tpl: task priority level of the event
778 * @notify_function: notification function of the event
779 * @notify_context: pointer passed to the notification function
780 * @event: created event
781 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100782 *
783 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100784 *
Mario Six8fac2912018-07-10 08:40:17 +0200785 * See the Unified Extensible Firmware Interface (UEFI) specification for
786 * details.
787 *
788 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100789 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100790static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100791efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
792 void (EFIAPI *notify_function) (
793 struct efi_event *event,
794 void *context),
795 void *notify_context,
Masahisa Kojima787774a2023-11-10 13:25:38 +0900796 const efi_guid_t *event_group,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100797 struct efi_event **event)
798{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200799 efi_status_t ret;
800
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100801 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100802 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200803
804 /*
805 * The allowable input parameters are the same as in CreateEvent()
806 * except for the following two disallowed event types.
807 */
808 switch (type) {
809 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
810 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
811 ret = EFI_INVALID_PARAMETER;
812 goto out;
813 }
814
815 ret = efi_create_event(type, notify_tpl, notify_function,
816 notify_context, event_group, event);
817out:
818 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100819}
820
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200821/**
Mario Six8fac2912018-07-10 08:40:17 +0200822 * efi_create_event_ext() - create an event
823 * @type: type of the event to create
824 * @notify_tpl: task priority level of the event
825 * @notify_function: notification function of the event
826 * @notify_context: pointer passed to the notification function
827 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200828 *
829 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200830 *
831 * See the Unified Extensible Firmware Interface (UEFI) specification for
832 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200833 *
Mario Six8fac2912018-07-10 08:40:17 +0200834 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200835 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200836static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100837 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200838 void (EFIAPI *notify_function) (
839 struct efi_event *event,
840 void *context),
841 void *notify_context, struct efi_event **event)
842{
843 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
844 notify_context);
845 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100846 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200847}
848
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200849/**
Mario Six8fac2912018-07-10 08:40:17 +0200850 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200851 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200852 * Check if a timer event has occurred or a queued notification function should
853 * be called.
854 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100855 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200856 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100857 */
858void efi_timer_check(void)
859{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100860 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100861 u64 now = timer_get_us();
862
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100863 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200864 if (!timers_enabled)
865 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100866 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200867 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100868 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200869 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100870 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200871 break;
872 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100873 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200874 break;
875 default:
876 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200877 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100878 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200879 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100880 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200881 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200882 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100883}
884
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200885/**
Mario Six8fac2912018-07-10 08:40:17 +0200886 * efi_set_timer() - set the trigger time for a timer event or stop the event
887 * @event: event for which the timer is set
888 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200889 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200890 *
891 * This is the function for internal usage in U-Boot. For the API function
892 * implementing the SetTimer service see efi_set_timer_ext.
893 *
Mario Six8fac2912018-07-10 08:40:17 +0200894 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200895 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200896efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200897 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100898{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100899 /* Check that the event is valid */
900 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
901 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100902
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200903 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200904 * The parameter defines a multiple of 100 ns.
905 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200906 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200907 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100908
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100909 switch (type) {
910 case EFI_TIMER_STOP:
911 event->trigger_next = -1ULL;
912 break;
913 case EFI_TIMER_PERIODIC:
914 case EFI_TIMER_RELATIVE:
915 event->trigger_next = timer_get_us() + trigger_time;
916 break;
917 default:
918 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100919 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 event->trigger_type = type;
921 event->trigger_time = trigger_time;
922 event->is_signaled = false;
923 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100924}
925
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200926/**
Mario Six8fac2912018-07-10 08:40:17 +0200927 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
928 * event
929 * @event: event for which the timer is set
930 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200931 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200932 *
933 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200934 *
Mario Six8fac2912018-07-10 08:40:17 +0200935 * See the Unified Extensible Firmware Interface (UEFI) specification for
936 * details.
937 *
938 *
939 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200940 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200941static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
942 enum efi_timer_delay type,
943 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200944{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900945 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200946 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
947}
948
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200949/**
Mario Six8fac2912018-07-10 08:40:17 +0200950 * efi_wait_for_event() - wait for events to be signaled
951 * @num_events: number of events to be waited for
952 * @event: events to be waited for
953 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200954 *
955 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200956 *
Mario Six8fac2912018-07-10 08:40:17 +0200957 * See the Unified Extensible Firmware Interface (UEFI) specification for
958 * details.
959 *
960 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200961 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100962static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200963 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100964 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100965{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100966 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100967
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100968 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100969
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200970 /* Check parameters */
971 if (!num_events || !event)
972 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200973 /* Check TPL */
974 if (efi_tpl != TPL_APPLICATION)
975 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200976 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100977 if (efi_is_event(event[i]) != EFI_SUCCESS)
978 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200979 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
980 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200981 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200982 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200983 }
984
985 /* Wait for signal */
986 for (;;) {
987 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200988 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200989 goto out;
990 }
991 /* Allow events to occur. */
992 efi_timer_check();
993 }
994
995out:
996 /*
997 * Reset the signal which is passed to the caller to allow periodic
998 * events to occur.
999 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +02001000 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001001 if (index)
1002 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +01001003
1004 return EFI_EXIT(EFI_SUCCESS);
1005}
1006
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001007/**
Mario Six8fac2912018-07-10 08:40:17 +02001008 * efi_signal_event_ext() - signal an EFI event
1009 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001010 *
1011 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001012 *
1013 * See the Unified Extensible Firmware Interface (UEFI) specification for
1014 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001015 *
1016 * This functions sets the signaled state of the event and queues the
1017 * notification function for execution.
1018 *
Mario Six8fac2912018-07-10 08:40:17 +02001019 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001020 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001021static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001022{
1023 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001024 if (efi_is_event(event) != EFI_SUCCESS)
1025 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001026 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +01001027 return EFI_EXIT(EFI_SUCCESS);
1028}
1029
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001030/**
Mario Six8fac2912018-07-10 08:40:17 +02001031 * efi_close_event() - close an EFI event
1032 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001033 *
1034 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001035 *
Mario Six8fac2912018-07-10 08:40:17 +02001036 * See the Unified Extensible Firmware Interface (UEFI) specification for
1037 * details.
1038 *
1039 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001040 */
Adriano Cordova7f2bcd42025-03-03 11:13:11 -03001041efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001042{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001043 struct efi_register_notify_event *item, *next;
1044
Alexander Grafc15d9212016-03-04 01:09:59 +01001045 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001046 if (efi_is_event(event) != EFI_SUCCESS)
1047 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001048
1049 /* Remove protocol notify registrations for the event */
1050 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1051 link) {
1052 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001053 struct efi_protocol_notification *hitem, *hnext;
1054
1055 /* Remove signaled handles */
1056 list_for_each_entry_safe(hitem, hnext, &item->handles,
1057 link) {
1058 list_del(&hitem->link);
1059 free(hitem);
1060 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001061 list_del(&item->link);
1062 free(item);
1063 }
1064 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001065 /* Remove event from queue */
1066 if (efi_event_is_queued(event))
1067 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001068
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001069 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001070 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001071 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001072}
1073
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001074/**
Mario Six8fac2912018-07-10 08:40:17 +02001075 * efi_check_event() - check if an event is signaled
1076 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001077 *
1078 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001079 *
1080 * See the Unified Extensible Firmware Interface (UEFI) specification for
1081 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001082 *
Mario Six8fac2912018-07-10 08:40:17 +02001083 * If an event is not signaled yet, the notification function is queued. The
1084 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001085 *
Mario Six8fac2912018-07-10 08:40:17 +02001086 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001087 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001088static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001089{
1090 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001091 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001092 if (efi_is_event(event) != EFI_SUCCESS ||
1093 event->type & EVT_NOTIFY_SIGNAL)
1094 return EFI_EXIT(EFI_INVALID_PARAMETER);
1095 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001096 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001097 if (event->is_signaled) {
1098 event->is_signaled = false;
1099 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001100 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001101 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001102}
1103
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001104/**
Mario Six8fac2912018-07-10 08:40:17 +02001105 * efi_search_obj() - find the internal EFI object for a handle
1106 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001107 *
Mario Six8fac2912018-07-10 08:40:17 +02001108 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001109 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001110struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001111{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001112 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001113
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001114 if (!handle)
1115 return NULL;
1116
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001117 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001118 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001119 return efiobj;
1120 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001121 return NULL;
1122}
1123
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001124/**
Mario Six8fac2912018-07-10 08:40:17 +02001125 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1126 * to a protocol
1127 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001128 *
Mario Six8fac2912018-07-10 08:40:17 +02001129 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001130 */
1131static struct efi_open_protocol_info_entry *efi_create_open_info(
1132 struct efi_handler *handler)
1133{
1134 struct efi_open_protocol_info_item *item;
1135
1136 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1137 if (!item)
1138 return NULL;
1139 /* Append the item to the open protocol info list. */
1140 list_add_tail(&item->link, &handler->open_infos);
1141
1142 return &item->info;
1143}
1144
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001145/**
Mario Six8fac2912018-07-10 08:40:17 +02001146 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1147 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001148 *
Mario Six8fac2912018-07-10 08:40:17 +02001149 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001150 */
1151static efi_status_t efi_delete_open_info(
1152 struct efi_open_protocol_info_item *item)
1153{
1154 list_del(&item->link);
1155 free(item);
1156 return EFI_SUCCESS;
1157}
1158
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001159/**
Mario Six8fac2912018-07-10 08:40:17 +02001160 * efi_add_protocol() - install new protocol on a handle
1161 * @handle: handle on which the protocol shall be installed
1162 * @protocol: GUID of the protocol to be installed
1163 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001164 *
Mario Six8fac2912018-07-10 08:40:17 +02001165 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001166 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001167efi_status_t efi_add_protocol(const efi_handle_t handle,
1168 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001169 void *protocol_interface)
1170{
1171 struct efi_object *efiobj;
1172 struct efi_handler *handler;
1173 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001174 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001175
1176 efiobj = efi_search_obj(handle);
1177 if (!efiobj)
1178 return EFI_INVALID_PARAMETER;
1179 ret = efi_search_protocol(handle, protocol, NULL);
1180 if (ret != EFI_NOT_FOUND)
1181 return EFI_INVALID_PARAMETER;
1182 handler = calloc(1, sizeof(struct efi_handler));
1183 if (!handler)
1184 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001185 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001186 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001187 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001188 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001189
1190 /* Notify registered events */
1191 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001192 if (!guidcmp(protocol, &event->protocol)) {
1193 struct efi_protocol_notification *notif;
1194
1195 notif = calloc(1, sizeof(*notif));
1196 if (!notif) {
1197 list_del(&handler->link);
1198 free(handler);
1199 return EFI_OUT_OF_RESOURCES;
1200 }
1201 notif->handle = handle;
1202 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001203 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001204 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001205 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001206 }
1207
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001208 if (!guidcmp(&efi_guid_device_path, protocol))
1209 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001210 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001211}
1212
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001213/**
Mario Six8fac2912018-07-10 08:40:17 +02001214 * efi_install_protocol_interface() - install protocol interface
1215 * @handle: handle on which the protocol shall be installed
1216 * @protocol: GUID of the protocol to be installed
1217 * @protocol_interface_type: type of the interface to be installed,
1218 * always EFI_NATIVE_INTERFACE
1219 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001220 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001221 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001222 *
Mario Six8fac2912018-07-10 08:40:17 +02001223 * See the Unified Extensible Firmware Interface (UEFI) specification for
1224 * details.
1225 *
1226 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001227 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001228static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001229 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001230 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001231{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001232 efi_status_t r;
1233
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001234 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001235 protocol_interface);
1236
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001237 if (!handle || !protocol ||
1238 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1239 r = EFI_INVALID_PARAMETER;
1240 goto out;
1241 }
1242
1243 /* Create new handle if requested. */
1244 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001245 r = efi_create_handle(handle);
1246 if (r != EFI_SUCCESS)
1247 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001248 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001249 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001250 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001251 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001252 /* Add new protocol */
1253 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001254out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001255 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001256}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001257
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001258/**
Mario Six8fac2912018-07-10 08:40:17 +02001259 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001260 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001261 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001262 * @number_of_drivers: number of child controllers
1263 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001264 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001265 * The allocated buffer has to be freed with free().
1266 *
Mario Six8fac2912018-07-10 08:40:17 +02001267 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001268 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001269static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001270 const efi_guid_t *protocol,
1271 efi_uintn_t *number_of_drivers,
1272 efi_handle_t **driver_handle_buffer)
1273{
1274 struct efi_handler *handler;
1275 struct efi_open_protocol_info_item *item;
1276 efi_uintn_t count = 0, i;
1277 bool duplicate;
1278
1279 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001280 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001281 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001282 continue;
1283 list_for_each_entry(item, &handler->open_infos, link) {
1284 if (item->info.attributes &
1285 EFI_OPEN_PROTOCOL_BY_DRIVER)
1286 ++count;
1287 }
1288 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001289 *number_of_drivers = 0;
1290 if (!count) {
1291 *driver_handle_buffer = NULL;
1292 return EFI_SUCCESS;
1293 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001294 /*
1295 * Create buffer. In case of duplicate driver assignments the buffer
1296 * will be too large. But that does not harm.
1297 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001298 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1299 if (!*driver_handle_buffer)
1300 return EFI_OUT_OF_RESOURCES;
1301 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001302 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001303 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001304 continue;
1305 list_for_each_entry(item, &handler->open_infos, link) {
1306 if (item->info.attributes &
1307 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1308 /* Check this is a new driver */
1309 duplicate = false;
1310 for (i = 0; i < *number_of_drivers; ++i) {
1311 if ((*driver_handle_buffer)[i] ==
1312 item->info.agent_handle)
1313 duplicate = true;
1314 }
1315 /* Copy handle to buffer */
1316 if (!duplicate) {
1317 i = (*number_of_drivers)++;
1318 (*driver_handle_buffer)[i] =
1319 item->info.agent_handle;
1320 }
1321 }
1322 }
1323 }
1324 return EFI_SUCCESS;
1325}
1326
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001327/**
Mario Six8fac2912018-07-10 08:40:17 +02001328 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001329 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001330 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001331 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001332 *
1333 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001334 *
1335 * See the Unified Extensible Firmware Interface (UEFI) specification for
1336 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001337 *
Mario Six8fac2912018-07-10 08:40:17 +02001338 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001339 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001340static efi_status_t efi_disconnect_all_drivers
1341 (efi_handle_t handle,
1342 const efi_guid_t *protocol,
1343 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001344{
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001345 efi_uintn_t number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001346 efi_handle_t *driver_handle_buffer;
1347 efi_status_t r, ret;
1348
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001349 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001350 &driver_handle_buffer);
1351 if (ret != EFI_SUCCESS)
1352 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001353 if (!number_of_drivers)
1354 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001355
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001356 while (number_of_drivers) {
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001357 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001358 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001359 driver_handle_buffer[--number_of_drivers],
1360 child_handle));
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001361 if (r != EFI_SUCCESS)
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001362 ret = r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001363 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001364
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001365 free(driver_handle_buffer);
1366 return ret;
1367}
1368
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001369/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001370 * efi_uninstall_protocol() - uninstall protocol interface
1371 *
Mario Six8fac2912018-07-10 08:40:17 +02001372 * @handle: handle from which the protocol shall be removed
1373 * @protocol: GUID of the protocol to be removed
1374 * @protocol_interface: interface to be removed
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001375 * @preserve: preserve or delete the handle and remove it from any
1376 * list it participates if no protocols remain
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001377 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001378 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001379 *
1380 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001381 */
Adriano Cordova7f2bcd42025-03-03 11:13:11 -03001382efi_status_t efi_uninstall_protocol
1383 (efi_handle_t handle, const efi_guid_t *protocol,
1384 void *protocol_interface, bool preserve)
Alexander Grafc15d9212016-03-04 01:09:59 +01001385{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001386 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001387 struct efi_open_protocol_info_item *item;
1388 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001389 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001390
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001391 /* Find the protocol on the handle */
1392 r = efi_search_protocol(handle, protocol, &handler);
1393 if (r != EFI_SUCCESS)
1394 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001395 if (handler->protocol_interface != protocol_interface)
1396 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001397 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001398 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001399 if (r != EFI_SUCCESS) {
1400 r = EFI_ACCESS_DENIED;
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001401 /*
1402 * This will reconnect all controllers of the handle, even ones
1403 * that were not connected before. This can be done better
1404 * but we are following the EDKII implementation on this for
1405 * now
1406 */
1407 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001408 goto out;
1409 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001410 /* Close protocol */
1411 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1412 if (item->info.attributes ==
1413 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1414 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1415 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001416 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001417 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001418 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001419 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001420 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001421 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001422 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001423 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001424 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001425 if (r != EFI_SUCCESS)
1426 return r;
1427 /*
1428 * We don't care about the return value here since the
1429 * handle might have more protocols installed
1430 */
1431 if (!preserve)
1432 efi_purge_handle(handle);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001433out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001434 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001435}
1436
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001437/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001438 * efi_uninstall_protocol_interface() - uninstall protocol interface
1439 * @handle: handle from which the protocol shall be removed
1440 * @protocol: GUID of the protocol to be removed
1441 * @protocol_interface: interface to be removed
1442 *
1443 * This function implements the UninstallProtocolInterface service.
1444 *
1445 * See the Unified Extensible Firmware Interface (UEFI) specification for
1446 * details.
1447 *
1448 * Return: status code
1449 */
1450static efi_status_t EFIAPI efi_uninstall_protocol_interface
1451 (efi_handle_t handle, const efi_guid_t *protocol,
1452 void *protocol_interface)
1453{
1454 efi_status_t ret;
1455
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001456 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001457
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001458 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001459 if (ret != EFI_SUCCESS)
1460 goto out;
1461
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001462out:
1463 return EFI_EXIT(ret);
1464}
1465
1466/**
Mario Six8fac2912018-07-10 08:40:17 +02001467 * efi_register_protocol_notify() - register an event for notification when a
1468 * protocol is installed.
1469 * @protocol: GUID of the protocol whose installation shall be notified
1470 * @event: event to be signaled upon installation of the protocol
1471 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001472 *
1473 * This function implements the RegisterProtocolNotify service.
1474 * See the Unified Extensible Firmware Interface (UEFI) specification
1475 * for details.
1476 *
Mario Six8fac2912018-07-10 08:40:17 +02001477 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001478 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001479efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1480 struct efi_event *event,
1481 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001482{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001483 struct efi_register_notify_event *item;
1484 efi_status_t ret = EFI_SUCCESS;
1485
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001486 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001487
1488 if (!protocol || !event || !registration) {
1489 ret = EFI_INVALID_PARAMETER;
1490 goto out;
1491 }
1492
1493 item = calloc(1, sizeof(struct efi_register_notify_event));
1494 if (!item) {
1495 ret = EFI_OUT_OF_RESOURCES;
1496 goto out;
1497 }
1498
1499 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301500 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001501 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001502
1503 list_add_tail(&item->link, &efi_register_notify_events);
1504
1505 *registration = item;
1506out:
1507 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001508}
1509
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001510/**
Mario Six8fac2912018-07-10 08:40:17 +02001511 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001512 *
Mario Six8fac2912018-07-10 08:40:17 +02001513 * @search_type: selection criterion
1514 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001515 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001516 *
1517 * See the documentation of the LocateHandle service in the UEFI specification.
1518 *
Mario Six8fac2912018-07-10 08:40:17 +02001519 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001520 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001521static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001522 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001523{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001524 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001525
1526 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001527 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001528 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001529 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001530 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001531 return (ret != EFI_SUCCESS);
1532 default:
1533 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001534 return -1;
1535 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001536}
1537
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001538/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001539 * efi_check_register_notify_event() - check if registration key is valid
1540 *
1541 * Check that a pointer is a valid registration key as returned by
1542 * RegisterProtocolNotify().
1543 *
1544 * @key: registration key
1545 * Return: valid registration key or NULL
1546 */
1547static struct efi_register_notify_event *efi_check_register_notify_event
1548 (void *key)
1549{
1550 struct efi_register_notify_event *event;
1551
1552 list_for_each_entry(event, &efi_register_notify_events, link) {
1553 if (event == (struct efi_register_notify_event *)key)
1554 return event;
1555 }
1556 return NULL;
1557}
1558
1559/**
Mario Six8fac2912018-07-10 08:40:17 +02001560 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001561 *
1562 * @search_type: selection criterion
1563 * @protocol: GUID of the protocol
1564 * @search_key: registration key
1565 * @buffer_size: size of the buffer to receive the handles in bytes
1566 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001567 *
1568 * This function is meant for U-Boot internal calls. For the API implementation
1569 * of the LocateHandle service see efi_locate_handle_ext.
1570 *
Mario Six8fac2912018-07-10 08:40:17 +02001571 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001572 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001573static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001575 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001576 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001577{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001578 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001579 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001580 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001581 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001582
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001583 /* Check parameters */
1584 switch (search_type) {
1585 case ALL_HANDLES:
1586 break;
1587 case BY_REGISTER_NOTIFY:
1588 if (!search_key)
1589 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001590 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001591 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001592 if (!event)
1593 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001594 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001595 case BY_PROTOCOL:
1596 if (!protocol)
1597 return EFI_INVALID_PARAMETER;
1598 break;
1599 default:
1600 return EFI_INVALID_PARAMETER;
1601 }
1602
Alexander Grafc15d9212016-03-04 01:09:59 +01001603 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001604 if (search_type == BY_REGISTER_NOTIFY) {
1605 if (list_empty(&event->handles))
1606 return EFI_NOT_FOUND;
1607 handle = list_first_entry(&event->handles,
1608 struct efi_protocol_notification,
1609 link);
1610 efiobj = handle->handle;
1611 size += sizeof(void *);
1612 } else {
1613 list_for_each_entry(efiobj, &efi_obj_list, link) {
1614 if (!efi_search(search_type, protocol, efiobj))
1615 size += sizeof(void *);
1616 }
1617 if (size == 0)
1618 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001619 }
1620
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001621 if (!buffer_size)
1622 return EFI_INVALID_PARAMETER;
1623
Alexander Grafc15d9212016-03-04 01:09:59 +01001624 if (*buffer_size < size) {
1625 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001626 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001627 }
1628
Rob Clarkcdee3372017-08-06 14:10:07 -04001629 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001630
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001631 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001632 if (!buffer)
1633 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001634
Alexander Grafc15d9212016-03-04 01:09:59 +01001635 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001636 if (search_type == BY_REGISTER_NOTIFY) {
1637 *buffer = efiobj;
1638 list_del(&handle->link);
1639 } else {
1640 list_for_each_entry(efiobj, &efi_obj_list, link) {
1641 if (!efi_search(search_type, protocol, efiobj))
1642 *buffer++ = efiobj;
1643 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001644 }
1645
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001646 return EFI_SUCCESS;
1647}
1648
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001649/**
Mario Six8fac2912018-07-10 08:40:17 +02001650 * efi_locate_handle_ext() - locate handles implementing a protocol.
1651 * @search_type: selection criterion
1652 * @protocol: GUID of the protocol
1653 * @search_key: registration key
1654 * @buffer_size: size of the buffer to receive the handles in bytes
1655 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001656 *
1657 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001658 *
Mario Six8fac2912018-07-10 08:40:17 +02001659 * See the Unified Extensible Firmware Interface (UEFI) specification for
1660 * details.
1661 *
1662 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001663 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001664static efi_status_t EFIAPI efi_locate_handle_ext(
1665 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001666 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001667 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001668{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001669 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001670 buffer_size, buffer);
1671
1672 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1673 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001674}
1675
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001676/**
Mario Six8fac2912018-07-10 08:40:17 +02001677 * efi_remove_configuration_table() - collapses configuration table entries,
1678 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001679 *
Mario Six8fac2912018-07-10 08:40:17 +02001680 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001681 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001682static void efi_remove_configuration_table(int i)
1683{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001684 struct efi_configuration_table *this = &systab.tables[i];
1685 struct efi_configuration_table *next = &systab.tables[i + 1];
1686 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001687
1688 memmove(this, next, (ulong)end - (ulong)next);
1689 systab.nr_tables--;
1690}
1691
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001692/**
Mario Six8fac2912018-07-10 08:40:17 +02001693 * efi_install_configuration_table() - adds, updates, or removes a
1694 * configuration table
1695 * @guid: GUID of the installed table
1696 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001697 *
1698 * This function is used for internal calls. For the API implementation of the
1699 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1700 *
Mario Six8fac2912018-07-10 08:40:17 +02001701 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001702 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001703efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1704 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001705{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001706 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001707 int i;
1708
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001709 if (!guid)
1710 return EFI_INVALID_PARAMETER;
1711
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001712 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001713 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001714 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001715 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001716 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001717 else
1718 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001719 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001720 }
1721 }
1722
Alexander Graffe3366f2017-07-26 13:41:04 +02001723 if (!table)
1724 return EFI_NOT_FOUND;
1725
Alexander Grafc15d9212016-03-04 01:09:59 +01001726 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001727 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001728 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001729
1730 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301731 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001732 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001733 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001734
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001735out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001736 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001737 efi_update_table_header_crc32(&systab.hdr);
1738
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001739 /* Notify that the configuration table was changed */
1740 list_for_each_entry(evt, &efi_events, link) {
1741 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001742 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001743 break;
1744 }
1745 }
1746
Alexander Grafc5c11632016-08-19 01:23:24 +02001747 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001748}
1749
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001750/**
Mario Six8fac2912018-07-10 08:40:17 +02001751 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1752 * configuration table.
1753 * @guid: GUID of the installed table
1754 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001755 *
1756 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001757 *
Mario Six8fac2912018-07-10 08:40:17 +02001758 * See the Unified Extensible Firmware Interface (UEFI) specification for
1759 * details.
1760 *
1761 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001762 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001763static efi_status_t
1764EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1765 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001766{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001767 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001768 return EFI_EXIT(efi_install_configuration_table(guid, table));
1769}
1770
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001771/**
Mario Six8fac2912018-07-10 08:40:17 +02001772 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001773 *
1774 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001775 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001776 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001777 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001778 * code is returned.
1779 *
1780 * @device_path: device path of the loaded image
1781 * @file_path: file path of the loaded image
1782 * @handle_ptr: handle of the loaded image
1783 * @info_ptr: loaded image protocol
1784 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001785 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001786efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1787 struct efi_device_path *file_path,
1788 struct efi_loaded_image_obj **handle_ptr,
1789 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001790{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001791 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001792 struct efi_loaded_image *info = NULL;
1793 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001794 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001795
1796 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1797 *handle_ptr = NULL;
1798 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001799
1800 info = calloc(1, sizeof(*info));
1801 if (!info)
1802 return EFI_OUT_OF_RESOURCES;
1803 obj = calloc(1, sizeof(*obj));
1804 if (!obj) {
1805 free(info);
1806 return EFI_OUT_OF_RESOURCES;
1807 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001808 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001809
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001810 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001811 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001812
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001813 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001814 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001815 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001816
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001817 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001818 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001819
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +02001820 dp = efi_dp_concat(device_path, file_path, 0);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001821 if (!dp) {
1822 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001823 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001824 }
1825 } else {
1826 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001827 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001828 ret = efi_add_protocol(&obj->header,
1829 &efi_guid_loaded_image_device_path, dp);
1830 if (ret != EFI_SUCCESS)
1831 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001832
1833 /*
1834 * When asking for the loaded_image interface, just
1835 * return handle which points to loaded_image_info
1836 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001837 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001838 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001839 if (ret != EFI_SUCCESS)
1840 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001841
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001842 *info_ptr = info;
1843 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001844
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001845 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001846failure:
1847 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001848 efi_delete_handle(&obj->header);
1849 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001850 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001851}
1852
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001853/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001854 * efi_locate_device_path() - Get the device path and handle of an device
1855 * implementing a protocol
1856 * @protocol: GUID of the protocol
1857 * @device_path: device path
1858 * @device: handle of the device
1859 *
1860 * This function implements the LocateDevicePath service.
1861 *
1862 * See the Unified Extensible Firmware Interface (UEFI) specification for
1863 * details.
1864 *
1865 * Return: status code
1866 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001867efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1868 struct efi_device_path **device_path,
1869 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001870{
1871 struct efi_device_path *dp;
1872 size_t i;
1873 struct efi_handler *handler;
1874 efi_handle_t *handles;
1875 size_t len, len_dp;
1876 size_t len_best = 0;
1877 efi_uintn_t no_handles;
1878 u8 *remainder;
1879 efi_status_t ret;
1880
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001881 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001882
1883 if (!protocol || !device_path || !*device_path) {
1884 ret = EFI_INVALID_PARAMETER;
1885 goto out;
1886 }
1887
1888 /* Find end of device path */
1889 len = efi_dp_instance_size(*device_path);
1890
1891 /* Get all handles implementing the protocol */
1892 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1893 &no_handles, &handles));
1894 if (ret != EFI_SUCCESS)
1895 goto out;
1896
1897 for (i = 0; i < no_handles; ++i) {
1898 /* Find the device path protocol */
1899 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1900 &handler);
1901 if (ret != EFI_SUCCESS)
1902 continue;
1903 dp = (struct efi_device_path *)handler->protocol_interface;
1904 len_dp = efi_dp_instance_size(dp);
1905 /*
1906 * This handle can only be a better fit
1907 * if its device path length is longer than the best fit and
1908 * if its device path length is shorter of equal the searched
1909 * device path.
1910 */
1911 if (len_dp <= len_best || len_dp > len)
1912 continue;
1913 /* Check if dp is a subpath of device_path */
1914 if (memcmp(*device_path, dp, len_dp))
1915 continue;
1916 if (!device) {
1917 ret = EFI_INVALID_PARAMETER;
1918 goto out;
1919 }
1920 *device = handles[i];
1921 len_best = len_dp;
1922 }
1923 if (len_best) {
1924 remainder = (u8 *)*device_path + len_best;
1925 *device_path = (struct efi_device_path *)remainder;
1926 ret = EFI_SUCCESS;
1927 } else {
1928 ret = EFI_NOT_FOUND;
1929 }
1930out:
1931 return EFI_EXIT(ret);
1932}
1933
1934/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001935 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001936 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001937 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1938 * callers obligation to update the memory type as needed.
1939 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001940 * @file_path: the path of the image to load
1941 * @buffer: buffer containing the loaded image
1942 * @size: size of the loaded image
1943 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001944 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001945static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001946efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001947 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001948{
Rob Clark857a1222017-09-13 18:05:35 -04001949 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001950 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001951 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001952 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001953
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001954 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001955 f = efi_file_from_path(file_path);
1956 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001957 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001958
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001959 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001960 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001961 goto error;
1962
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001963 /*
1964 * When reading the file we do not yet know if it contains an
1965 * application, a boottime driver, or a runtime driver. So here we
1966 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1967 * update the reservation according to the image type.
1968 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001969 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1970 EFI_BOOT_SERVICES_DATA,
1971 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001972 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001973 ret = EFI_OUT_OF_RESOURCES;
1974 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001975 }
1976
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001977 /* Read file */
1978 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1979 if (ret != EFI_SUCCESS)
1980 efi_free_pages(addr, efi_size_in_pages(bs));
1981 *buffer = (void *)(uintptr_t)addr;
1982 *size = bs;
1983error:
1984 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001985 return ret;
1986}
1987
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001988/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001989 * efi_load_image_from_path() - load an image using a file path
1990 *
1991 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1992 * callers obligation to update the memory type as needed.
1993 *
1994 * @boot_policy: true for request originating from the boot manager
1995 * @file_path: the path of the image to load
1996 * @buffer: buffer containing the loaded image
1997 * @size: size of the loaded image
1998 * Return: status code
1999 */
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002000efi_status_t efi_load_image_from_path(bool boot_policy,
2001 struct efi_device_path *file_path,
2002 void **buffer, efi_uintn_t *size)
2003{
2004 efi_handle_t device;
2005 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002006 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002007 struct efi_load_file_protocol *load_file_protocol = NULL;
2008 efi_uintn_t buffer_size;
2009 uint64_t addr, pages;
2010 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002011 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002012
2013 /* In case of failure nothing is returned */
2014 *buffer = NULL;
2015 *size = 0;
2016
2017 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002018 device = efi_dp_find_obj(dp, NULL, &rem);
2019 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2020 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002021 if (ret == EFI_SUCCESS)
2022 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002023
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002024 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002025 if (ret == EFI_SUCCESS) {
2026 guid = &efi_guid_load_file_protocol;
2027 } else if (!boot_policy) {
2028 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002029 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002030 }
2031 if (ret != EFI_SUCCESS)
2032 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002033 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002034 if (ret != EFI_SUCCESS)
2035 return EFI_NOT_FOUND;
2036 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002037 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002038 ret = EFI_CALL(load_file_protocol->load_file(
2039 load_file_protocol, rem, boot_policy,
2040 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002041 if (ret != EFI_BUFFER_TOO_SMALL)
2042 goto out;
2043 pages = efi_size_in_pages(buffer_size);
2044 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2045 pages, &addr);
2046 if (ret != EFI_SUCCESS) {
2047 ret = EFI_OUT_OF_RESOURCES;
2048 goto out;
2049 }
2050 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002051 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002052 &buffer_size, (void *)(uintptr_t)addr));
2053 if (ret != EFI_SUCCESS)
2054 efi_free_pages(addr, pages);
2055out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002056 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002057 if (ret == EFI_SUCCESS) {
2058 *buffer = (void *)(uintptr_t)addr;
2059 *size = buffer_size;
2060 }
2061
2062 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002063}
2064
2065/**
Mario Six8fac2912018-07-10 08:40:17 +02002066 * efi_load_image() - load an EFI image into memory
2067 * @boot_policy: true for request originating from the boot manager
2068 * @parent_image: the caller's image handle
2069 * @file_path: the path of the image to load
2070 * @source_buffer: memory location from which the image is installed
2071 * @source_size: size of the memory area from which the image is installed
2072 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002073 *
2074 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002075 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002076 * See the Unified Extensible Firmware Interface (UEFI) specification
2077 * for details.
2078 *
Mario Six8fac2912018-07-10 08:40:17 +02002079 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002080 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002081efi_status_t EFIAPI efi_load_image(bool boot_policy,
2082 efi_handle_t parent_image,
2083 struct efi_device_path *file_path,
2084 void *source_buffer,
2085 efi_uintn_t source_size,
2086 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002087{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002088 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002089 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002090 struct efi_loaded_image_obj **image_obj =
2091 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002092 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002093 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002094
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002095 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002096 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002097
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002098 if (!image_handle || (!source_buffer && !file_path) ||
2099 !efi_search_obj(parent_image) ||
2100 /* The parent image handle must refer to a loaded image */
2101 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002102 ret = EFI_INVALID_PARAMETER;
2103 goto error;
2104 }
Rob Clark857a1222017-09-13 18:05:35 -04002105
2106 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002107 ret = efi_load_image_from_path(boot_policy, file_path,
2108 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002109 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002110 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002111 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002112 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002113 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002114 /* split file_path which contains both the device and file parts */
2115 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002116 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002117 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002118 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002119 if (!source_buffer)
2120 /* Release buffer to which file was loaded */
2121 efi_free_pages((uintptr_t)dest_buffer,
2122 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002123 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002124 info->system_table = &systab;
2125 info->parent_handle = parent_image;
2126 } else {
2127 /* The image is invalid. Release all associated resources. */
2128 efi_delete_handle(*image_handle);
2129 *image_handle = NULL;
2130 free(info);
2131 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002132error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002133 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002134}
2135
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002136/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002137 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2138 */
2139static void efi_exit_caches(void)
2140{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002141#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002142 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002143 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2144 * caches are enabled.
2145 *
2146 * TODO:
2147 * According to the UEFI spec caches that can be managed via CP15
2148 * operations should be enabled. Caches requiring platform information
2149 * to manage should be disabled. This should not happen in
2150 * ExitBootServices() but before invoking any UEFI binary is invoked.
2151 *
2152 * We want to keep the current workaround while GRUB prior to version
2153 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002154 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002155 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002156#endif
2157}
2158
2159/**
Mario Six8fac2912018-07-10 08:40:17 +02002160 * efi_exit_boot_services() - stop all boot services
2161 * @image_handle: handle of the loaded image
2162 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002163 *
2164 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002165 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002166 * See the Unified Extensible Firmware Interface (UEFI) specification
2167 * for details.
2168 *
Mario Six8fac2912018-07-10 08:40:17 +02002169 * All timer events are disabled. For exit boot services events the
2170 * notification function is called. The boot services are disabled in the
2171 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002172 *
Mario Six8fac2912018-07-10 08:40:17 +02002173 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002174 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002175static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002176 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002177{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002178 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002179 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002180
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002181 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002182
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002183 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002184 if (map_key != efi_memory_map_key) {
2185 ret = EFI_INVALID_PARAMETER;
2186 goto out;
2187 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002188
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002189 /* Check if ExitBootServices has already been called */
2190 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002191 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002192
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002193 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2194 list_for_each_entry(evt, &efi_events, link) {
2195 if (evt->group &&
2196 !guidcmp(evt->group,
2197 &efi_guid_event_group_before_exit_boot_services)) {
2198 efi_signal_event(evt);
2199 break;
2200 }
2201 }
2202
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002203 /* Stop all timer related activities */
2204 timers_enabled = false;
2205
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002206 /* Add related events to the event group */
2207 list_for_each_entry(evt, &efi_events, link) {
2208 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2209 evt->group = &efi_guid_event_group_exit_boot_services;
2210 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002211 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002212 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002213 if (evt->group &&
2214 !guidcmp(evt->group,
2215 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002216 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002217 break;
2218 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002219 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002220
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002221 /* Make sure that notification functions are not called anymore */
2222 efi_tpl = TPL_HIGH_LEVEL;
2223
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002224 /* Notify variable services */
2225 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002226
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002227 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2228 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2229 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2230 list_del(&evt->link);
2231 }
2232
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002233 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002234 bootm_disable_interrupts();
Heinrich Schuchardt20047fd2025-04-05 08:58:12 +02002235 if (IS_ENABLED(CONFIG_DM_ETH))
2236 eth_halt();
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002237 board_quiesce_devices();
Janne Grunau64609732024-11-23 22:44:05 +01002238 dm_remove_devices_active();
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002239 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002240
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002241 /* Patch out unsupported runtime function */
2242 efi_runtime_detach();
2243
Alexander Graffa5246b2018-11-15 21:23:47 +01002244 /* Fix up caches for EFI payloads if necessary */
2245 efi_exit_caches();
2246
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002247 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002248 systab.con_in_handle = NULL;
2249 systab.con_in = NULL;
2250 systab.con_out_handle = NULL;
2251 systab.con_out = NULL;
2252 systab.stderr_handle = NULL;
2253 systab.std_err = NULL;
2254 systab.boottime = NULL;
2255
2256 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002257 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002258
Alexander Grafc15d9212016-03-04 01:09:59 +01002259 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002260 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002261 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002262out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002263 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2264 if (ret != EFI_SUCCESS)
2265 efi_tcg2_notify_exit_boot_services_failed();
2266 }
2267
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002268 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002269}
2270
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002271/**
Mario Six8fac2912018-07-10 08:40:17 +02002272 * efi_get_next_monotonic_count() - get next value of the counter
2273 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002274 *
2275 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002276 *
2277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2278 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002279 *
Mario Six8fac2912018-07-10 08:40:17 +02002280 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002281 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002282static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2283{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002284 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002285 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002286
Alexander Grafc15d9212016-03-04 01:09:59 +01002287 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002288 if (!count) {
2289 ret = EFI_INVALID_PARAMETER;
2290 goto out;
2291 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002292 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002293 ret = EFI_SUCCESS;
2294out:
2295 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002296}
2297
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002298/**
Mario Six8fac2912018-07-10 08:40:17 +02002299 * efi_stall() - sleep
2300 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002301 *
Mario Six8fac2912018-07-10 08:40:17 +02002302 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002303 *
Mario Six8fac2912018-07-10 08:40:17 +02002304 * See the Unified Extensible Firmware Interface (UEFI) specification for
2305 * details.
2306 *
2307 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002308 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002309static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2310{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002311 u64 end_tick;
2312
Alexander Grafc15d9212016-03-04 01:09:59 +01002313 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002314
2315 end_tick = get_ticks() + usec_to_tick(microseconds);
2316 while (get_ticks() < end_tick)
2317 efi_timer_check();
2318
Alexander Grafc15d9212016-03-04 01:09:59 +01002319 return EFI_EXIT(EFI_SUCCESS);
2320}
2321
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002322/**
Mario Six8fac2912018-07-10 08:40:17 +02002323 * efi_set_watchdog_timer() - reset the watchdog timer
2324 * @timeout: seconds before reset by watchdog
2325 * @watchdog_code: code to be logged when resetting
2326 * @data_size: size of buffer in bytes
2327 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002328 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002329 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002330 *
Mario Six8fac2912018-07-10 08:40:17 +02002331 * See the Unified Extensible Firmware Interface (UEFI) specification for
2332 * details.
2333 *
2334 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002335 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002336static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2337 uint64_t watchdog_code,
2338 unsigned long data_size,
2339 uint16_t *watchdog_data)
2340{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002341 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002342 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002343 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002344}
2345
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002346/**
Mario Six8fac2912018-07-10 08:40:17 +02002347 * efi_close_protocol() - close a protocol
2348 * @handle: handle on which the protocol shall be closed
2349 * @protocol: GUID of the protocol to close
2350 * @agent_handle: handle of the driver
2351 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002352 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002353 * This is the function implementing the CloseProtocol service is for internal
2354 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002355 *
Mario Six8fac2912018-07-10 08:40:17 +02002356 * See the Unified Extensible Firmware Interface (UEFI) specification for
2357 * details.
2358 *
2359 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002360 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002361efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2362 efi_handle_t agent_handle,
2363 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002364{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002365 struct efi_handler *handler;
2366 struct efi_open_protocol_info_item *item;
2367 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002368 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002369
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002370 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002371 (controller_handle && !efi_search_obj(controller_handle)))
2372 return EFI_INVALID_PARAMETER;
2373 ret = efi_search_protocol(handle, protocol, &handler);
2374 if (ret != EFI_SUCCESS)
2375 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002376
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002377 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002378 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2379 if (item->info.agent_handle == agent_handle &&
2380 item->info.controller_handle == controller_handle) {
2381 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002382 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002383 }
2384 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002385
2386 return ret;
2387}
2388
2389/**
2390 * efi_close_protocol_ext() - close a protocol
2391 * @handle: handle on which the protocol shall be closed
2392 * @protocol: GUID of the protocol to close
2393 * @agent_handle: handle of the driver
2394 * @controller_handle: handle of the controller
2395 *
2396 * This function implements the CloseProtocol service.
2397 *
2398 * See the Unified Extensible Firmware Interface (UEFI) specification for
2399 * details.
2400 *
2401 * Return: status code
2402 */
2403static efi_status_t EFIAPI
2404efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2405 efi_handle_t agent_handle,
2406 efi_handle_t controller_handle)
2407{
2408 efi_status_t ret;
2409
2410 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2411 controller_handle);
2412
2413 ret = efi_close_protocol(handle, protocol,
2414 agent_handle, controller_handle);
2415
2416 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002417}
2418
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002419/**
Mario Six8fac2912018-07-10 08:40:17 +02002420 * efi_open_protocol_information() - provide information about then open status
2421 * of a protocol on a handle
2422 * @handle: handle for which the information shall be retrieved
2423 * @protocol: GUID of the protocol
2424 * @entry_buffer: buffer to receive the open protocol information
2425 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002426 *
2427 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002428 *
2429 * See the Unified Extensible Firmware Interface (UEFI) specification for
2430 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002431 *
Mario Six8fac2912018-07-10 08:40:17 +02002432 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002433 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002434static efi_status_t EFIAPI efi_open_protocol_information(
2435 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002436 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002437 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002438{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002439 unsigned long buffer_size;
2440 unsigned long count;
2441 struct efi_handler *handler;
2442 struct efi_open_protocol_info_item *item;
2443 efi_status_t r;
2444
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002445 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002446 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002447
2448 /* Check parameters */
2449 if (!entry_buffer) {
2450 r = EFI_INVALID_PARAMETER;
2451 goto out;
2452 }
2453 r = efi_search_protocol(handle, protocol, &handler);
2454 if (r != EFI_SUCCESS)
2455 goto out;
2456
2457 /* Count entries */
2458 count = 0;
2459 list_for_each_entry(item, &handler->open_infos, link) {
2460 if (item->info.open_count)
2461 ++count;
2462 }
2463 *entry_count = count;
2464 *entry_buffer = NULL;
2465 if (!count) {
2466 r = EFI_SUCCESS;
2467 goto out;
2468 }
2469
2470 /* Copy entries */
2471 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002472 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002473 (void **)entry_buffer);
2474 if (r != EFI_SUCCESS)
2475 goto out;
2476 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2477 if (item->info.open_count)
2478 (*entry_buffer)[--count] = item->info;
2479 }
2480out:
2481 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002482}
2483
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002484/**
Mario Six8fac2912018-07-10 08:40:17 +02002485 * efi_protocols_per_handle() - get protocols installed on a handle
2486 * @handle: handle for which the information is retrieved
2487 * @protocol_buffer: buffer with protocol GUIDs
2488 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002489 *
2490 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002491 *
Mario Six8fac2912018-07-10 08:40:17 +02002492 * See the Unified Extensible Firmware Interface (UEFI) specification for
2493 * details.
2494 *
2495 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002496 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002497static efi_status_t EFIAPI efi_protocols_per_handle(
2498 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002499 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002500{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002501 unsigned long buffer_size;
2502 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002503 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002504 efi_status_t r;
2505
Alexander Grafc15d9212016-03-04 01:09:59 +01002506 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2507 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002508
2509 if (!handle || !protocol_buffer || !protocol_buffer_count)
2510 return EFI_EXIT(EFI_INVALID_PARAMETER);
2511
2512 *protocol_buffer = NULL;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002513
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002514 efiobj = efi_search_obj(handle);
2515 if (!efiobj)
2516 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002517
Heinrich Schuchardt5f6806d2024-07-31 10:13:04 +02002518 *protocol_buffer_count = list_count_nodes(&efiobj->protocols);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002519
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002520 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002521 if (*protocol_buffer_count) {
2522 size_t j = 0;
2523
2524 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002525 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002526 (void **)protocol_buffer);
2527 if (r != EFI_SUCCESS)
2528 return EFI_EXIT(r);
2529 list_for_each(protocol_handle, &efiobj->protocols) {
2530 struct efi_handler *protocol;
2531
2532 protocol = list_entry(protocol_handle,
2533 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002534 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002535 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002536 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002537 }
2538
2539 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002540}
2541
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002542efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2543 const efi_guid_t *protocol, void *search_key,
2544 efi_uintn_t *no_handles, efi_handle_t **buffer)
2545{
2546 efi_status_t r;
2547 efi_uintn_t buffer_size = 0;
2548
2549 if (!no_handles || !buffer) {
2550 r = EFI_INVALID_PARAMETER;
2551 goto out;
2552 }
2553 *no_handles = 0;
2554 *buffer = NULL;
2555 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2556 *buffer);
2557 if (r != EFI_BUFFER_TOO_SMALL)
2558 goto out;
2559 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2560 (void **)buffer);
2561 if (r != EFI_SUCCESS)
2562 goto out;
2563 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2564 *buffer);
2565 if (r == EFI_SUCCESS)
2566 *no_handles = buffer_size / sizeof(efi_handle_t);
2567out:
2568 return r;
2569}
2570
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002571/**
Mario Six8fac2912018-07-10 08:40:17 +02002572 * efi_locate_handle_buffer() - locate handles implementing a protocol
2573 * @search_type: selection criterion
2574 * @protocol: GUID of the protocol
2575 * @search_key: registration key
2576 * @no_handles: number of returned handles
2577 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002578 *
2579 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002580 *
Mario Six8fac2912018-07-10 08:40:17 +02002581 * See the Unified Extensible Firmware Interface (UEFI) specification for
2582 * details.
2583 *
2584 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002585 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002586efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002587 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002588 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002589 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002590{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002591 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002592
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002593 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002594 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002595
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002596 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2597 no_handles, buffer);
2598
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002599 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002600}
2601
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002602/**
Mario Six8fac2912018-07-10 08:40:17 +02002603 * efi_locate_protocol() - find an interface implementing a protocol
2604 * @protocol: GUID of the protocol
2605 * @registration: registration key passed to the notification function
2606 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002607 *
2608 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002609 *
2610 * See the Unified Extensible Firmware Interface (UEFI) specification for
2611 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002612 *
Mario Six8fac2912018-07-10 08:40:17 +02002613 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002614 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002615static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002616 void *registration,
2617 void **protocol_interface)
2618{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002619 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002620 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002621 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002622
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002623 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002624
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002625 /*
2626 * The UEFI spec explicitly requires a protocol even if a registration
2627 * key is provided. This differs from the logic in LocateHandle().
2628 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002629 if (!protocol || !protocol_interface)
2630 return EFI_EXIT(EFI_INVALID_PARAMETER);
2631
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002632 if (registration) {
2633 struct efi_register_notify_event *event;
2634 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002635
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002636 event = efi_check_register_notify_event(registration);
2637 if (!event)
2638 return EFI_EXIT(EFI_INVALID_PARAMETER);
2639 /*
2640 * The UEFI spec requires to return EFI_NOT_FOUND if no
2641 * protocol instance matches protocol and registration.
2642 * So let's do the same for a mismatch between protocol and
2643 * registration.
2644 */
2645 if (guidcmp(&event->protocol, protocol))
2646 goto not_found;
2647 if (list_empty(&event->handles))
2648 goto not_found;
2649 handle = list_first_entry(&event->handles,
2650 struct efi_protocol_notification,
2651 link);
2652 efiobj = handle->handle;
2653 list_del(&handle->link);
2654 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002655 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002656 if (ret == EFI_SUCCESS)
2657 goto found;
2658 } else {
2659 list_for_each_entry(efiobj, &efi_obj_list, link) {
2660 ret = efi_search_protocol(efiobj, protocol, &handler);
2661 if (ret == EFI_SUCCESS)
2662 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002663 }
2664 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002665not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002666 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002667 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002668found:
2669 *protocol_interface = handler->protocol_interface;
2670 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002671}
2672
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002673/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002674 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002675 * interfaces
2676 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002677 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002678 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002679 * Core functionality of efi_install_multiple_protocol_interfaces
2680 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002681 *
Mario Six8fac2912018-07-10 08:40:17 +02002682 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002683 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002684static efi_status_t EFIAPI
2685efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2686 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002687{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002688 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002689 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002690 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002691 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002692 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002693 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002694
2695 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002696 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002697
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002698 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002699 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002700 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002701 if (!protocol)
2702 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002703 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002704 /* Check that a device path has not been installed before */
2705 if (!guidcmp(protocol, &efi_guid_device_path)) {
2706 struct efi_device_path *dp = protocol_interface;
2707
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002708 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2709 &old_handle));
2710 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002711 dp->type == DEVICE_PATH_TYPE_END) {
2712 EFI_PRINT("Path %pD already installed\n",
2713 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002714 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002715 break;
2716 }
2717 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002718 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2719 EFI_NATIVE_INTERFACE,
2720 protocol_interface));
2721 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002722 break;
2723 i++;
2724 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002725 if (ret == EFI_SUCCESS)
2726 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002727
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002728 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002729 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002730 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2731 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002732 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002733 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002734 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002735
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002736out:
2737 efi_va_end(argptr_copy);
2738 return ret;
2739
Alexander Grafc15d9212016-03-04 01:09:59 +01002740}
2741
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002742/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002743 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2744 * interfaces
2745 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002746 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2747 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002748 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002749 *
2750 * This is the function for internal usage in U-Boot. For the API function
2751 * implementing the InstallMultipleProtocol service see
2752 * efi_install_multiple_protocol_interfaces_ext()
2753 *
2754 * Return: status code
2755 */
2756efi_status_t EFIAPI
2757efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2758{
2759 efi_status_t ret;
2760 efi_va_list argptr;
2761
2762 efi_va_start(argptr, handle);
2763 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2764 efi_va_end(argptr);
2765 return ret;
2766}
2767
2768/**
2769 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2770 * interfaces
2771 * @handle: handle on which the protocol interfaces shall be installed
2772 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2773 * interfaces
2774 *
2775 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002776 *
Mario Six8fac2912018-07-10 08:40:17 +02002777 * See the Unified Extensible Firmware Interface (UEFI) specification for
2778 * details.
2779 *
2780 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002781 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002782static efi_status_t EFIAPI
2783efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002784{
2785 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002786 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002787 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002788
2789 efi_va_start(argptr, handle);
2790 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2791 efi_va_end(argptr);
2792 return EFI_EXIT(ret);
2793}
2794
2795/**
2796 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2797 * multiple protocol
2798 * interfaces
2799 * @handle: handle from which the protocol interfaces shall be removed
2800 * @argptr: va_list of args
2801 *
2802 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2803 * Must not be called directly
2804 *
2805 * Return: status code
2806 */
2807static efi_status_t EFIAPI
2808efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2809 efi_va_list argptr)
2810{
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002811 const efi_guid_t *protocol, *next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002812 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002813 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002814 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002815 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002816
2817 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002818 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002819
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002820 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002821 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002822 for (;;) {
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002823 /*
2824 * If efi_uninstall_protocol() fails we need to be able to
2825 * reinstall the previously uninstalled protocols on the same
2826 * handle.
2827 * Instead of calling efi_uninstall_protocol(...,..., false)
2828 * and potentially removing the handle, only allow the handle
2829 * removal on the last protocol that we requested to uninstall.
2830 * That way we can preserve the handle in case the latter fails
2831 */
2832 bool preserve = true;
2833
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002834 if (!protocol)
2835 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002836 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002837 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2838 if (!next_protocol)
2839 preserve = false;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002840 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002841 protocol_interface, preserve);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002842 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002843 break;
2844 i++;
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002845 protocol = next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002846 }
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002847 if (ret == EFI_SUCCESS)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002848 goto out;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002849
2850 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002851 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002852 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2853 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002854 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2855 EFI_NATIVE_INTERFACE,
2856 protocol_interface));
2857 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002858 /*
2859 * If any errors are generated while the protocol interfaces are being
2860 * uninstalled, then the protocols uninstalled prior to the error will
2861 * be reinstalled using InstallProtocolInterface() and the status code
2862 * EFI_INVALID_PARAMETER is returned.
2863 */
2864 ret = EFI_INVALID_PARAMETER;
2865
2866out:
2867 efi_va_end(argptr_copy);
2868 return ret;
2869}
2870
2871/**
2872 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2873 * interfaces
2874 * @handle: handle from which the protocol interfaces shall be removed
2875 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2876 * interfaces
2877 *
2878 * This function implements the UninstallMultipleProtocolInterfaces service.
2879 *
2880 * This is the function for internal usage in U-Boot. For the API function
2881 * implementing the UninstallMultipleProtocolInterfaces service see
2882 * efi_uninstall_multiple_protocol_interfaces_ext()
2883 *
2884 * Return: status code
2885 */
2886efi_status_t EFIAPI
2887efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2888{
2889 efi_status_t ret;
2890 efi_va_list argptr;
2891
2892 efi_va_start(argptr, handle);
2893 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002894 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002895 return ret;
2896}
2897
2898/**
2899 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2900 * interfaces
2901 * @handle: handle from which the protocol interfaces shall be removed
2902 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2903 * interfaces
2904 *
2905 * This function implements the UninstallMultipleProtocolInterfaces service.
2906 *
2907 * See the Unified Extensible Firmware Interface (UEFI) specification for
2908 * details.
2909 *
2910 * Return: status code
2911 */
2912static efi_status_t EFIAPI
2913efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2914{
2915 EFI_ENTRY("%p", handle);
2916 efi_status_t ret;
2917 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002918
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002919 efi_va_start(argptr, handle);
2920 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2921 efi_va_end(argptr);
2922 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002923}
2924
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002925/**
Mario Six8fac2912018-07-10 08:40:17 +02002926 * efi_calculate_crc32() - calculate cyclic redundancy code
2927 * @data: buffer with data
2928 * @data_size: size of buffer in bytes
2929 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002930 *
2931 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002932 *
2933 * See the Unified Extensible Firmware Interface (UEFI) specification for
2934 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002935 *
Mario Six8fac2912018-07-10 08:40:17 +02002936 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002937 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002938static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2939 efi_uintn_t data_size,
2940 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002941{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002942 efi_status_t ret = EFI_SUCCESS;
2943
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002944 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002945 if (!data || !data_size || !crc32_p) {
2946 ret = EFI_INVALID_PARAMETER;
2947 goto out;
2948 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002949 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002950out:
2951 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002952}
2953
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002954/**
Mario Six8fac2912018-07-10 08:40:17 +02002955 * efi_copy_mem() - copy memory
2956 * @destination: destination of the copy operation
2957 * @source: source of the copy operation
2958 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002959 *
2960 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002961 *
Mario Six8fac2912018-07-10 08:40:17 +02002962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2963 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002964 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002965static void EFIAPI efi_copy_mem(void *destination, const void *source,
2966 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002967{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002968 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002969 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002970 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002971}
2972
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002973/**
Mario Six8fac2912018-07-10 08:40:17 +02002974 * efi_set_mem() - Fill memory with a byte value.
2975 * @buffer: buffer to fill
2976 * @size: size of buffer in bytes
2977 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002978 *
2979 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002980 *
Mario Six8fac2912018-07-10 08:40:17 +02002981 * See the Unified Extensible Firmware Interface (UEFI) specification for
2982 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002983 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002984static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002985{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002986 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002987 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002988 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002989}
2990
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002991/**
Mario Six8fac2912018-07-10 08:40:17 +02002992 * efi_protocol_open() - open protocol interface on a handle
2993 * @handler: handler of a protocol
2994 * @protocol_interface: interface implementing the protocol
2995 * @agent_handle: handle of the driver
2996 * @controller_handle: handle of the controller
2997 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002998 *
Mario Six8fac2912018-07-10 08:40:17 +02002999 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003000 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01003001efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003002 struct efi_handler *handler,
3003 void **protocol_interface, void *agent_handle,
3004 void *controller_handle, uint32_t attributes)
3005{
3006 struct efi_open_protocol_info_item *item;
3007 struct efi_open_protocol_info_entry *match = NULL;
3008 bool opened_by_driver = false;
3009 bool opened_exclusive = false;
3010
3011 /* If there is no agent, only return the interface */
3012 if (!agent_handle)
3013 goto out;
3014
3015 /* For TEST_PROTOCOL ignore interface attribute */
3016 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3017 *protocol_interface = NULL;
3018
3019 /*
3020 * Check if the protocol is already opened by a driver with the same
3021 * attributes or opened exclusively
3022 */
3023 list_for_each_entry(item, &handler->open_infos, link) {
3024 if (item->info.agent_handle == agent_handle) {
3025 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3026 (item->info.attributes == attributes))
3027 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003028 } else {
3029 if (item->info.attributes &
3030 EFI_OPEN_PROTOCOL_BY_DRIVER)
3031 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003032 }
3033 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3034 opened_exclusive = true;
3035 }
3036
3037 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003038 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3039 if (opened_exclusive)
3040 return EFI_ACCESS_DENIED;
3041 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3042 if (opened_exclusive || opened_by_driver)
3043 return EFI_ACCESS_DENIED;
3044 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003045
3046 /* Prepare exclusive opening */
3047 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3048 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003049disconnect_next:
3050 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003051 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003052 efi_status_t ret;
3053
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003054 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003055 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3056 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003057 item->info.controller_handle,
3058 item->info.agent_handle,
3059 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003060 if (ret == EFI_SUCCESS)
3061 /*
3062 * Child controllers may have been
3063 * removed from the open_infos list. So
3064 * let's restart the loop.
3065 */
3066 goto disconnect_next;
3067 else
3068 opened_by_driver = true;
3069 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003070 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003071 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003072 if (opened_by_driver)
3073 return EFI_ACCESS_DENIED;
3074 }
3075
3076 /* Find existing entry */
3077 list_for_each_entry(item, &handler->open_infos, link) {
3078 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003079 item->info.controller_handle == controller_handle &&
3080 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003081 match = &item->info;
3082 }
3083 /* None found, create one */
3084 if (!match) {
3085 match = efi_create_open_info(handler);
3086 if (!match)
3087 return EFI_OUT_OF_RESOURCES;
3088 }
3089
3090 match->agent_handle = agent_handle;
3091 match->controller_handle = controller_handle;
3092 match->attributes = attributes;
3093 match->open_count++;
3094
3095out:
3096 /* For TEST_PROTOCOL ignore interface attribute. */
3097 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3098 *protocol_interface = handler->protocol_interface;
3099
3100 return EFI_SUCCESS;
3101}
3102
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003103/**
Mario Six8fac2912018-07-10 08:40:17 +02003104 * efi_open_protocol() - open protocol interface on a handle
3105 * @handle: handle on which the protocol shall be opened
3106 * @protocol: GUID of the protocol
3107 * @protocol_interface: interface implementing the protocol
3108 * @agent_handle: handle of the driver
3109 * @controller_handle: handle of the controller
3110 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003111 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003112 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003113 *
Mario Six8fac2912018-07-10 08:40:17 +02003114 * See the Unified Extensible Firmware Interface (UEFI) specification for
3115 * details.
3116 *
3117 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003118 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003119static efi_status_t EFIAPI efi_open_protocol
3120 (efi_handle_t handle, const efi_guid_t *protocol,
3121 void **protocol_interface, efi_handle_t agent_handle,
3122 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003123{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003124 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003125 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003126
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003127 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003128 protocol_interface, agent_handle, controller_handle,
3129 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003130
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003131 if (!handle || !protocol ||
3132 (!protocol_interface && attributes !=
3133 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003134 goto out;
3135 }
3136
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003137 switch (attributes) {
3138 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3139 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3140 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3141 break;
3142 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3143 if (controller_handle == handle)
3144 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003145 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003146 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3147 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003148 /* Check that the controller handle is valid */
3149 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003150 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003151 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003152 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003153 /* Check that the agent handle is valid */
3154 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003155 goto out;
3156 break;
3157 default:
3158 goto out;
3159 }
3160
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003161 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003162 switch (r) {
3163 case EFI_SUCCESS:
3164 break;
3165 case EFI_NOT_FOUND:
3166 r = EFI_UNSUPPORTED;
3167 goto out;
3168 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003169 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003170 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003171
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003172 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3173 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003174out:
3175 return EFI_EXIT(r);
3176}
3177
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003178/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003179 * efi_start_image() - call the entry point of an image
3180 * @image_handle: handle of the image
3181 * @exit_data_size: size of the buffer
3182 * @exit_data: buffer to receive the exit data of the called image
3183 *
3184 * This function implements the StartImage service.
3185 *
3186 * See the Unified Extensible Firmware Interface (UEFI) specification for
3187 * details.
3188 *
3189 * Return: status code
3190 */
3191efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3192 efi_uintn_t *exit_data_size,
3193 u16 **exit_data)
3194{
3195 struct efi_loaded_image_obj *image_obj =
3196 (struct efi_loaded_image_obj *)image_handle;
3197 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003198 void *info;
3199 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003200 efi_status_t exit_status;
Yao Zi11c9cee2025-03-02 15:21:20 +01003201 jmp_buf exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003202
3203 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3204
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003205 if (!efi_search_obj(image_handle))
3206 return EFI_EXIT(EFI_INVALID_PARAMETER);
3207
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003208 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003209 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3210 return EFI_EXIT(EFI_INVALID_PARAMETER);
3211
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003212 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3213 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3214
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003215 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3216 &info, NULL, NULL,
3217 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3218 if (ret != EFI_SUCCESS)
3219 return EFI_EXIT(EFI_INVALID_PARAMETER);
3220
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003221 image_obj->exit_data_size = exit_data_size;
3222 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003223 image_obj->exit_status = &exit_status;
3224 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003225
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003226 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3227 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003228 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003229 if (ret == EFI_SECURITY_VIOLATION) {
3230 /*
3231 * TCG2 Protocol is installed but no TPM device found,
3232 * this is not expected.
3233 */
3234 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003235 }
3236 }
3237 }
3238
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003239 /* call the image! */
Yao Zi11c9cee2025-03-02 15:21:20 +01003240 if (setjmp(exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003241 /*
3242 * We called the entry point of the child image with EFI_CALL
3243 * in the lines below. The child image called the Exit() boot
3244 * service efi_exit() which executed the long jump that brought
3245 * us to the current line. This implies that the second half
3246 * of the EFI_CALL macro has not been executed.
3247 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003248#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003249 /*
3250 * efi_exit() called efi_restore_gd(). We have to undo this
3251 * otherwise __efi_entry_check() will put the wrong value into
3252 * app_gd.
3253 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003254 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003255#endif
3256 /*
3257 * To get ready to call EFI_EXIT below we have to execute the
3258 * missed out steps of EFI_CALL.
3259 */
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003260 EFI_RETURN(exit_status);
3261
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003262 current_image = parent_image;
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003263
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003264 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003265 }
3266
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003267 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003268 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003269 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003270 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3271
3272 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003273 * Control is returned from a started UEFI image either by calling
3274 * Exit() (where exit data can be provided) or by simply returning from
3275 * the entry point. In the latter case call Exit() on behalf of the
3276 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003277 */
3278 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3279}
3280
3281/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003282 * efi_delete_image() - delete loaded image from memory)
3283 *
3284 * @image_obj: handle of the loaded image
3285 * @loaded_image_protocol: loaded image protocol
3286 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003287static efi_status_t efi_delete_image
3288 (struct efi_loaded_image_obj *image_obj,
3289 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003290{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003291 struct efi_object *efiobj;
3292 efi_status_t r, ret = EFI_SUCCESS;
3293
3294close_next:
3295 list_for_each_entry(efiobj, &efi_obj_list, link) {
3296 struct efi_handler *protocol;
3297
3298 list_for_each_entry(protocol, &efiobj->protocols, link) {
3299 struct efi_open_protocol_info_item *info;
3300
3301 list_for_each_entry(info, &protocol->open_infos, link) {
3302 if (info->info.agent_handle !=
3303 (efi_handle_t)image_obj)
3304 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003305 r = efi_close_protocol(
3306 efiobj, &protocol->guid,
3307 info->info.agent_handle,
3308 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003309 if (r != EFI_SUCCESS)
3310 ret = r;
3311 /*
3312 * Closing protocols may results in further
3313 * items being deleted. To play it safe loop
3314 * over all elements again.
3315 */
3316 goto close_next;
3317 }
3318 }
3319 }
3320
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003321 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3322 efi_size_in_pages(loaded_image_protocol->image_size));
3323 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003324
3325 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003326}
3327
3328/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003329 * efi_unload_image() - unload an EFI image
3330 * @image_handle: handle of the image to be unloaded
3331 *
3332 * This function implements the UnloadImage service.
3333 *
3334 * See the Unified Extensible Firmware Interface (UEFI) specification for
3335 * details.
3336 *
3337 * Return: status code
3338 */
3339efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3340{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003341 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003342 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003343 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003344
3345 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003346
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003347 efiobj = efi_search_obj(image_handle);
3348 if (!efiobj) {
3349 ret = EFI_INVALID_PARAMETER;
3350 goto out;
3351 }
3352 /* Find the loaded image protocol */
3353 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3354 (void **)&loaded_image_protocol,
3355 NULL, NULL,
3356 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3357 if (ret != EFI_SUCCESS) {
3358 ret = EFI_INVALID_PARAMETER;
3359 goto out;
3360 }
3361 switch (efiobj->type) {
3362 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3363 /* Call the unload function */
3364 if (!loaded_image_protocol->unload) {
3365 ret = EFI_UNSUPPORTED;
3366 goto out;
3367 }
3368 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3369 if (ret != EFI_SUCCESS)
3370 goto out;
3371 break;
3372 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3373 break;
3374 default:
3375 ret = EFI_INVALID_PARAMETER;
3376 goto out;
3377 }
3378 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3379 loaded_image_protocol);
3380out:
3381 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003382}
3383
3384/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003385 * efi_update_exit_data() - fill exit data parameters of StartImage()
3386 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003387 * @image_obj: image handle
3388 * @exit_data_size: size of the exit data buffer
3389 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003390 * Return: status code
3391 */
3392static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3393 efi_uintn_t exit_data_size,
3394 u16 *exit_data)
3395{
3396 efi_status_t ret;
3397
3398 /*
3399 * If exit_data is not provided to StartImage(), exit_data_size must be
3400 * ignored.
3401 */
3402 if (!image_obj->exit_data)
3403 return EFI_SUCCESS;
3404 if (image_obj->exit_data_size)
3405 *image_obj->exit_data_size = exit_data_size;
3406 if (exit_data_size && exit_data) {
3407 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3408 exit_data_size,
3409 (void **)image_obj->exit_data);
3410 if (ret != EFI_SUCCESS)
3411 return ret;
3412 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3413 } else {
3414 image_obj->exit_data = NULL;
3415 }
3416 return EFI_SUCCESS;
3417}
3418
3419/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003420 * efi_exit() - leave an EFI application or driver
3421 * @image_handle: handle of the application or driver that is exiting
3422 * @exit_status: status code
3423 * @exit_data_size: size of the buffer in bytes
3424 * @exit_data: buffer with data describing an error
3425 *
3426 * This function implements the Exit service.
3427 *
3428 * See the Unified Extensible Firmware Interface (UEFI) specification for
3429 * details.
3430 *
3431 * Return: status code
3432 */
3433static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3434 efi_status_t exit_status,
3435 efi_uintn_t exit_data_size,
3436 u16 *exit_data)
3437{
3438 /*
3439 * TODO: We should call the unload procedure of the loaded
3440 * image protocol.
3441 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003442 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003443 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003444 struct efi_loaded_image_obj *image_obj =
3445 (struct efi_loaded_image_obj *)image_handle;
Yao Zi11c9cee2025-03-02 15:21:20 +01003446 jmp_buf *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003447
3448 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3449 exit_data_size, exit_data);
3450
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003451 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003452 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003453 (void **)&loaded_image_protocol,
3454 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003455 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003456 if (ret != EFI_SUCCESS) {
3457 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003458 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003459 }
3460
3461 /* Unloading of unstarted images */
3462 switch (image_obj->header.type) {
3463 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3464 break;
3465 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3466 efi_delete_image(image_obj, loaded_image_protocol);
3467 ret = EFI_SUCCESS;
3468 goto out;
3469 default:
3470 /* Handle does not refer to loaded image */
3471 ret = EFI_INVALID_PARAMETER;
3472 goto out;
3473 }
3474 /* A started image can only be unloaded it is the last one started. */
3475 if (image_handle != current_image) {
3476 ret = EFI_INVALID_PARAMETER;
3477 goto out;
3478 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003479
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003480 /* Exit data is only foreseen in case of failure. */
3481 if (exit_status != EFI_SUCCESS) {
3482 ret = efi_update_exit_data(image_obj, exit_data_size,
3483 exit_data);
3484 /* Exiting has priority. Don't return error to caller. */
3485 if (ret != EFI_SUCCESS)
3486 EFI_PRINT("%s: out of memory\n", __func__);
3487 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003488 /* efi_delete_image() frees image_obj. Copy before the call. */
3489 exit_jmp = image_obj->exit_jmp;
3490 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003491 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3492 exit_status != EFI_SUCCESS)
3493 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003494
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003495 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3496 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3497 ret = efi_tcg2_measure_efi_app_exit();
Heinrich Schuchardt265bfab2024-11-27 00:40:17 +01003498 if (ret != EFI_SUCCESS)
3499 log_debug("tcg2 measurement fails (0x%lx)\n",
3500 ret);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003501 }
3502 }
3503
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003504 /* Make sure entry/exit counts for EFI world cross-overs match */
3505 EFI_EXIT(exit_status);
3506
3507 /*
3508 * But longjmp out with the U-Boot gd, not the application's, as
3509 * the other end is a setjmp call inside EFI context.
3510 */
3511 efi_restore_gd();
3512
Yao Zi11c9cee2025-03-02 15:21:20 +01003513 longjmp(*exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003514
3515 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003516out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003517 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003518}
3519
3520/**
Mario Six8fac2912018-07-10 08:40:17 +02003521 * efi_handle_protocol() - get interface of a protocol on a handle
3522 * @handle: handle on which the protocol shall be opened
3523 * @protocol: GUID of the protocol
3524 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003525 *
3526 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003527 *
3528 * See the Unified Extensible Firmware Interface (UEFI) specification for
3529 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003530 *
Mario Six8fac2912018-07-10 08:40:17 +02003531 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003532 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003533efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3534 const efi_guid_t *protocol,
3535 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003536{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003537 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003538 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003539}
3540
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003541/**
Mario Six8fac2912018-07-10 08:40:17 +02003542 * efi_bind_controller() - bind a single driver to a controller
3543 * @controller_handle: controller handle
3544 * @driver_image_handle: driver handle
3545 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003546 *
Mario Six8fac2912018-07-10 08:40:17 +02003547 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003548 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003549static efi_status_t efi_bind_controller(
3550 efi_handle_t controller_handle,
3551 efi_handle_t driver_image_handle,
3552 struct efi_device_path *remain_device_path)
3553{
3554 struct efi_driver_binding_protocol *binding_protocol;
3555 efi_status_t r;
3556
3557 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3558 &efi_guid_driver_binding_protocol,
3559 (void **)&binding_protocol,
3560 driver_image_handle, NULL,
3561 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3562 if (r != EFI_SUCCESS)
3563 return r;
3564 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3565 controller_handle,
3566 remain_device_path));
3567 if (r == EFI_SUCCESS)
3568 r = EFI_CALL(binding_protocol->start(binding_protocol,
3569 controller_handle,
3570 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003571 efi_close_protocol(driver_image_handle,
3572 &efi_guid_driver_binding_protocol,
3573 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003574 return r;
3575}
3576
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003577/**
Mario Six8fac2912018-07-10 08:40:17 +02003578 * efi_connect_single_controller() - connect a single driver to a controller
3579 * @controller_handle: controller
3580 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003581 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003582 *
Mario Six8fac2912018-07-10 08:40:17 +02003583 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003584 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003585static efi_status_t efi_connect_single_controller(
3586 efi_handle_t controller_handle,
3587 efi_handle_t *driver_image_handle,
3588 struct efi_device_path *remain_device_path)
3589{
3590 efi_handle_t *buffer;
3591 size_t count;
3592 size_t i;
3593 efi_status_t r;
3594 size_t connected = 0;
3595
3596 /* Get buffer with all handles with driver binding protocol */
3597 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3598 &efi_guid_driver_binding_protocol,
3599 NULL, &count, &buffer));
3600 if (r != EFI_SUCCESS)
3601 return r;
3602
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003603 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003604 if (driver_image_handle) {
3605 for (; *driver_image_handle; ++driver_image_handle) {
3606 for (i = 0; i < count; ++i) {
3607 if (buffer[i] == *driver_image_handle) {
3608 buffer[i] = NULL;
3609 r = efi_bind_controller(
3610 controller_handle,
3611 *driver_image_handle,
3612 remain_device_path);
3613 /*
3614 * For drivers that do not support the
3615 * controller or are already connected
3616 * we receive an error code here.
3617 */
3618 if (r == EFI_SUCCESS)
3619 ++connected;
3620 }
3621 }
3622 }
3623 }
3624
3625 /*
3626 * TODO: Some overrides are not yet implemented:
3627 * - Platform Driver Override
3628 * - Driver Family Override Search
3629 * - Bus Specific Driver Override
3630 */
3631
3632 /* Driver Binding Search */
3633 for (i = 0; i < count; ++i) {
3634 if (buffer[i]) {
3635 r = efi_bind_controller(controller_handle,
3636 buffer[i],
3637 remain_device_path);
3638 if (r == EFI_SUCCESS)
3639 ++connected;
3640 }
3641 }
3642
3643 efi_free_pool(buffer);
3644 if (!connected)
3645 return EFI_NOT_FOUND;
3646 return EFI_SUCCESS;
3647}
3648
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003649/**
Mario Six8fac2912018-07-10 08:40:17 +02003650 * efi_connect_controller() - connect a controller to a driver
3651 * @controller_handle: handle of the controller
3652 * @driver_image_handle: handle of the driver
3653 * @remain_device_path: device path of a child controller
3654 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003655 *
3656 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003657 *
3658 * See the Unified Extensible Firmware Interface (UEFI) specification for
3659 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003660 *
3661 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003662 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003663 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3664 *
Mario Six8fac2912018-07-10 08:40:17 +02003665 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003666 */
Adriano Cordova7f2bcd42025-03-03 11:13:11 -03003667efi_status_t EFIAPI efi_connect_controller(
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003668 efi_handle_t controller_handle,
3669 efi_handle_t *driver_image_handle,
3670 struct efi_device_path *remain_device_path,
3671 bool recursive)
3672{
3673 efi_status_t r;
3674 efi_status_t ret = EFI_NOT_FOUND;
3675 struct efi_object *efiobj;
3676
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003677 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003678 remain_device_path, recursive);
3679
3680 efiobj = efi_search_obj(controller_handle);
3681 if (!efiobj) {
3682 ret = EFI_INVALID_PARAMETER;
3683 goto out;
3684 }
3685
3686 r = efi_connect_single_controller(controller_handle,
3687 driver_image_handle,
3688 remain_device_path);
3689 if (r == EFI_SUCCESS)
3690 ret = EFI_SUCCESS;
3691 if (recursive) {
3692 struct efi_handler *handler;
3693 struct efi_open_protocol_info_item *item;
3694
3695 list_for_each_entry(handler, &efiobj->protocols, link) {
3696 list_for_each_entry(item, &handler->open_infos, link) {
3697 if (item->info.attributes &
3698 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3699 r = EFI_CALL(efi_connect_controller(
3700 item->info.controller_handle,
3701 driver_image_handle,
3702 remain_device_path,
3703 recursive));
3704 if (r == EFI_SUCCESS)
3705 ret = EFI_SUCCESS;
3706 }
3707 }
3708 }
3709 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003710 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003711 if (ret != EFI_SUCCESS && remain_device_path &&
3712 remain_device_path->type == DEVICE_PATH_TYPE_END)
3713 ret = EFI_SUCCESS;
3714out:
3715 return EFI_EXIT(ret);
3716}
3717
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003718/**
Mario Six8fac2912018-07-10 08:40:17 +02003719 * efi_reinstall_protocol_interface() - reinstall protocol interface
3720 * @handle: handle on which the protocol shall be reinstalled
3721 * @protocol: GUID of the protocol to be installed
3722 * @old_interface: interface to be removed
3723 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003724 *
3725 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003726 *
3727 * See the Unified Extensible Firmware Interface (UEFI) specification for
3728 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003729 *
3730 * The old interface is uninstalled. The new interface is installed.
3731 * Drivers are connected.
3732 *
Mario Six8fac2912018-07-10 08:40:17 +02003733 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003734 */
Adriano Cordova3d4b09a2024-12-06 14:18:34 -03003735efi_status_t EFIAPI efi_reinstall_protocol_interface(
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003736 efi_handle_t handle, const efi_guid_t *protocol,
3737 void *old_interface, void *new_interface)
3738{
3739 efi_status_t ret;
3740
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003741 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003742 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003743
3744 /* Uninstall protocol but do not delete handle */
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03003745 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003746 if (ret != EFI_SUCCESS)
3747 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003748
3749 /* Install the new protocol */
3750 ret = efi_add_protocol(handle, protocol, new_interface);
3751 /*
3752 * The UEFI spec does not specify what should happen to the handle
3753 * if in case of an error no protocol interface remains on the handle.
3754 * So let's do nothing here.
3755 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003756 if (ret != EFI_SUCCESS)
3757 goto out;
3758 /*
3759 * The returned status code has to be ignored.
3760 * Do not create an error if no suitable driver for the handle exists.
3761 */
3762 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3763out:
3764 return EFI_EXIT(ret);
3765}
3766
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003767/**
Mario Six8fac2912018-07-10 08:40:17 +02003768 * efi_get_child_controllers() - get all child controllers associated to a driver
3769 * @efiobj: handle of the controller
3770 * @driver_handle: handle of the driver
3771 * @number_of_children: number of child controllers
3772 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003773 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003774 * The allocated buffer has to be freed with free().
3775 *
Mario Six8fac2912018-07-10 08:40:17 +02003776 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003777 */
3778static efi_status_t efi_get_child_controllers(
3779 struct efi_object *efiobj,
3780 efi_handle_t driver_handle,
3781 efi_uintn_t *number_of_children,
3782 efi_handle_t **child_handle_buffer)
3783{
3784 struct efi_handler *handler;
3785 struct efi_open_protocol_info_item *item;
3786 efi_uintn_t count = 0, i;
3787 bool duplicate;
3788
3789 /* Count all child controller associations */
3790 list_for_each_entry(handler, &efiobj->protocols, link) {
3791 list_for_each_entry(item, &handler->open_infos, link) {
3792 if (item->info.agent_handle == driver_handle &&
3793 item->info.attributes &
3794 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3795 ++count;
3796 }
3797 }
3798 /*
3799 * Create buffer. In case of duplicate child controller assignments
3800 * the buffer will be too large. But that does not harm.
3801 */
3802 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003803 if (!count)
3804 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003805 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3806 if (!*child_handle_buffer)
3807 return EFI_OUT_OF_RESOURCES;
3808 /* Copy unique child handles */
3809 list_for_each_entry(handler, &efiobj->protocols, link) {
3810 list_for_each_entry(item, &handler->open_infos, link) {
3811 if (item->info.agent_handle == driver_handle &&
3812 item->info.attributes &
3813 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3814 /* Check this is a new child controller */
3815 duplicate = false;
3816 for (i = 0; i < *number_of_children; ++i) {
3817 if ((*child_handle_buffer)[i] ==
3818 item->info.controller_handle)
3819 duplicate = true;
3820 }
3821 /* Copy handle to buffer */
3822 if (!duplicate) {
3823 i = (*number_of_children)++;
3824 (*child_handle_buffer)[i] =
3825 item->info.controller_handle;
3826 }
3827 }
3828 }
3829 }
3830 return EFI_SUCCESS;
3831}
3832
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003833/**
Mario Six8fac2912018-07-10 08:40:17 +02003834 * efi_disconnect_controller() - disconnect a controller from a driver
3835 * @controller_handle: handle of the controller
3836 * @driver_image_handle: handle of the driver
3837 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003838 *
3839 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003840 *
3841 * See the Unified Extensible Firmware Interface (UEFI) specification for
3842 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003843 *
Mario Six8fac2912018-07-10 08:40:17 +02003844 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003845 */
Adriano Cordova7f2bcd42025-03-03 11:13:11 -03003846efi_status_t EFIAPI efi_disconnect_controller(
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003847 efi_handle_t controller_handle,
3848 efi_handle_t driver_image_handle,
3849 efi_handle_t child_handle)
3850{
3851 struct efi_driver_binding_protocol *binding_protocol;
3852 efi_handle_t *child_handle_buffer = NULL;
3853 size_t number_of_children = 0;
3854 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003855 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003856 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003857
3858 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3859 child_handle);
3860
3861 efiobj = efi_search_obj(controller_handle);
3862 if (!efiobj) {
3863 r = EFI_INVALID_PARAMETER;
3864 goto out;
3865 }
3866
3867 if (child_handle && !efi_search_obj(child_handle)) {
3868 r = EFI_INVALID_PARAMETER;
3869 goto out;
3870 }
3871
3872 /* If no driver handle is supplied, disconnect all drivers */
3873 if (!driver_image_handle) {
3874 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3875 goto out;
3876 }
3877
3878 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003879 r = efi_get_child_controllers(efiobj,
3880 driver_image_handle,
3881 &number_of_children,
3882 &child_handle_buffer);
3883 if (r != EFI_SUCCESS)
3884 return r;
3885 sole_child = (number_of_children == 1);
3886
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003887 if (child_handle) {
3888 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003889 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003890 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003891 }
3892
3893 /* Get the driver binding protocol */
3894 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3895 &efi_guid_driver_binding_protocol,
3896 (void **)&binding_protocol,
3897 driver_image_handle, NULL,
3898 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003899 if (r != EFI_SUCCESS) {
3900 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003901 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003902 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003903 /* Remove the children */
3904 if (number_of_children) {
3905 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3906 controller_handle,
3907 number_of_children,
3908 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003909 if (r != EFI_SUCCESS) {
3910 r = EFI_DEVICE_ERROR;
3911 goto out;
3912 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003913 }
3914 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003915 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003916 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3917 controller_handle,
3918 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003919 if (r != EFI_SUCCESS) {
3920 r = EFI_DEVICE_ERROR;
3921 goto out;
3922 }
3923 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003924 efi_close_protocol(driver_image_handle,
3925 &efi_guid_driver_binding_protocol,
3926 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003927 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003928out:
3929 if (!child_handle)
3930 free(child_handle_buffer);
3931 return EFI_EXIT(r);
3932}
3933
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003934static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003935 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003936 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3937 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003938 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003939 },
3940 .raise_tpl = efi_raise_tpl,
3941 .restore_tpl = efi_restore_tpl,
3942 .allocate_pages = efi_allocate_pages_ext,
3943 .free_pages = efi_free_pages_ext,
3944 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003945 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003946 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003947 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003948 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003949 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003950 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003951 .close_event = efi_close_event,
3952 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003953 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003954 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003955 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003956 .handle_protocol = efi_handle_protocol,
3957 .reserved = NULL,
3958 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003959 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003960 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003961 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003962 .load_image = efi_load_image,
3963 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003964 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003965 .unload_image = efi_unload_image,
3966 .exit_boot_services = efi_exit_boot_services,
3967 .get_next_monotonic_count = efi_get_next_monotonic_count,
3968 .stall = efi_stall,
3969 .set_watchdog_timer = efi_set_watchdog_timer,
3970 .connect_controller = efi_connect_controller,
3971 .disconnect_controller = efi_disconnect_controller,
3972 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003973 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003974 .open_protocol_information = efi_open_protocol_information,
3975 .protocols_per_handle = efi_protocols_per_handle,
3976 .locate_handle_buffer = efi_locate_handle_buffer,
3977 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003978 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003979 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003980 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003981 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003982 .calculate_crc32 = efi_calculate_crc32,
3983 .copy_mem = efi_copy_mem,
3984 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003985 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003986};
3987
Simon Glass90975372022-01-23 12:55:12 -07003988static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003989
Alexander Graf393dd912016-10-14 13:45:30 +02003990struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003991 .hdr = {
3992 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003993 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003994 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003995 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003996 .fw_vendor = firmware_vendor,
3997 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003998 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003999 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004000 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01004001};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004002
4003/**
4004 * efi_initialize_system_table() - Initialize system table
4005 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02004006 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004007 */
4008efi_status_t efi_initialize_system_table(void)
4009{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004010 efi_status_t ret;
4011
4012 /* Allocate configuration table array */
4013 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4014 EFI_MAX_CONFIGURATION_TABLES *
4015 sizeof(struct efi_configuration_table),
4016 (void **)&systab.tables);
4017
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004018 /*
4019 * These entries will be set to NULL in ExitBootServices(). To avoid
4020 * relocation in SetVirtualAddressMap(), set them dynamically.
4021 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01004022 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004023 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004024 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004025 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004026 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004027 systab.std_err = &efi_con_out;
4028 systab.boottime = &efi_boot_services;
4029
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02004030 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004031 efi_update_table_header_crc32(&systab.hdr);
4032 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4033 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004034
4035 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004036}