blob: 5164cb15986c1a5f587000c0ca40d288bbba7133 [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 Schuchardt37587522019-05-01 20:07:04 +020018#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030019#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070020#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030021#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010022#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090024#include <asm/setjmp.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
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030063static efi_status_t efi_uninstall_protocol
64 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +030065 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
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100103static efi_status_t EFIAPI efi_disconnect_controller(
104 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 +0300108static
109efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
110 efi_handle_t *driver_image_handle,
111 struct efi_device_path *remain_device_path,
112 bool recursive);
113
Rob Clark86789d52017-07-27 08:04:18 -0400114/* Called on every callback entry */
115int __efi_entry_check(void)
116{
117 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200118#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400119 assert(efi_gd);
120 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200121 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400122#endif
123 return ret;
124}
125
126/* Called on every callback exit */
127int __efi_exit_check(void)
128{
129 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200130#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200131 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400132#endif
133 return ret;
134}
135
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200136/**
137 * efi_save_gd() - save global data register
138 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200139 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200140 * As this register may be overwritten by an EFI payload we save it here
141 * and restore it on every callback entered.
142 *
143 * This function is called after relocation from initr_reloc_global_data().
144 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100145void efi_save_gd(void)
146{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200147#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100148 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600149#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100150}
151
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200152/**
153 * efi_restore_gd() - restore global data register
154 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200155 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200156 * Restore it after returning from the UEFI world to the value saved via
157 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400158 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100159void efi_restore_gd(void)
160{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200161#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100162 /* Only restore if we're already in EFI context */
163 if (!efi_gd)
164 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200165 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600166#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100167}
168
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200169/**
Mario Six8fac2912018-07-10 08:40:17 +0200170 * indent_string() - returns a string for indenting with two spaces per level
171 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100172 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200173 * A maximum of ten indent levels is supported. Higher indent levels will be
174 * truncated.
175 *
Mario Six8fac2912018-07-10 08:40:17 +0200176 * Return: A string for indenting with two spaces per level is
177 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400178 */
179static const char *indent_string(int level)
180{
181 const char *indent = " ";
182 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100183
Rob Clarke7896c32017-07-27 08:04:19 -0400184 level = min(max, level * 2);
185 return &indent[max - level];
186}
187
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200188const char *__efi_nesting(void)
189{
190 return indent_string(nesting_level);
191}
192
Rob Clarke7896c32017-07-27 08:04:19 -0400193const char *__efi_nesting_inc(void)
194{
195 return indent_string(nesting_level++);
196}
197
198const char *__efi_nesting_dec(void)
199{
200 return indent_string(--nesting_level);
201}
202
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200203/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200204 * efi_event_is_queued() - check if an event is queued
205 *
206 * @event: event
207 * Return: true if event is queued
208 */
209static bool efi_event_is_queued(struct efi_event *event)
210{
211 return !!event->queue_link.next;
212}
213
214/**
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300215 * efi_purge_handle() - Clean the deleted handle from the various lists
216 * @handle: handle to remove
217 *
218 * Return: status code
219 */
220static efi_status_t efi_purge_handle(efi_handle_t handle)
221{
222 struct efi_register_notify_event *item;
223
224 if (!list_empty(&handle->protocols))
225 return EFI_ACCESS_DENIED;
226 /* The handle is about to be freed. Remove it from events */
227 list_for_each_entry(item, &efi_register_notify_events, link) {
228 struct efi_protocol_notification *hitem, *hnext;
229
230 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
231 if (handle == hitem->handle) {
232 list_del(&hitem->link);
233 free(hitem);
234 }
235 }
236 }
237 /* The last protocol has been removed, delete the handle. */
238 list_del(&handle->link);
239 free(handle);
240
241 return EFI_SUCCESS;
242}
243
244/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200245 * efi_process_event_queue() - process event queue
246 */
247static void efi_process_event_queue(void)
248{
249 while (!list_empty(&efi_event_queue)) {
250 struct efi_event *event;
251 efi_uintn_t old_tpl;
252
253 event = list_first_entry(&efi_event_queue, struct efi_event,
254 queue_link);
255 if (efi_tpl >= event->notify_tpl)
256 return;
257 list_del(&event->queue_link);
258 event->queue_link.next = NULL;
259 event->queue_link.prev = NULL;
260 /* Events must be executed at the event's TPL */
261 old_tpl = efi_tpl;
262 efi_tpl = event->notify_tpl;
263 EFI_CALL_VOID(event->notify_function(event,
264 event->notify_context));
265 efi_tpl = old_tpl;
266 if (event->type == EVT_NOTIFY_SIGNAL)
267 event->is_signaled = 0;
268 }
269}
270
271/**
Mario Six8fac2912018-07-10 08:40:17 +0200272 * efi_queue_event() - queue an EFI event
273 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200274 *
275 * This function queues the notification function of the event for future
276 * execution.
277 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200278 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200279static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200280{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100281 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200282
283 if (!event->notify_function)
284 return;
285
286 if (!efi_event_is_queued(event)) {
287 /*
288 * Events must be notified in order of decreasing task priority
289 * level. Insert the new event accordingly.
290 */
291 list_for_each_entry(item, &efi_event_queue, queue_link) {
292 if (item->notify_tpl < event->notify_tpl) {
293 list_add_tail(&event->queue_link,
294 &item->queue_link);
295 event = NULL;
296 break;
297 }
298 }
299 if (event)
300 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100301 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200302 }
303}
304
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200305/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200306 * is_valid_tpl() - check if the task priority level is valid
307 *
308 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200309 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200310 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100311static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200312{
313 switch (tpl) {
314 case TPL_APPLICATION:
315 case TPL_CALLBACK:
316 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200317 return EFI_SUCCESS;
318 default:
319 return EFI_INVALID_PARAMETER;
320 }
321}
322
323/**
Mario Six8fac2912018-07-10 08:40:17 +0200324 * efi_signal_event() - signal an EFI event
325 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100326 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100327 * This function signals an event. If the event belongs to an event group, all
328 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100329 * their notification function is queued.
330 *
331 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100332 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200333void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100334{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200335 if (event->is_signaled)
336 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100337 if (event->group) {
338 struct efi_event *evt;
339
340 /*
341 * The signaled state has to set before executing any
342 * notification function
343 */
344 list_for_each_entry(evt, &efi_events, link) {
345 if (!evt->group || guidcmp(evt->group, event->group))
346 continue;
347 if (evt->is_signaled)
348 continue;
349 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100350 }
351 list_for_each_entry(evt, &efi_events, link) {
352 if (!evt->group || guidcmp(evt->group, event->group))
353 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200354 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100355 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200356 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100357 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200358 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100359 }
360}
361
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200362/**
Mario Six8fac2912018-07-10 08:40:17 +0200363 * efi_raise_tpl() - raise the task priority level
364 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200365 *
366 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200367 *
368 * See the Unified Extensible Firmware Interface (UEFI) specification for
369 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200370 *
Mario Six8fac2912018-07-10 08:40:17 +0200371 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200372 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100373static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100374{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100375 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200376
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200377 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200378
379 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200380 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200381 efi_tpl = new_tpl;
382 if (efi_tpl > TPL_HIGH_LEVEL)
383 efi_tpl = TPL_HIGH_LEVEL;
384
385 EFI_EXIT(EFI_SUCCESS);
386 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100387}
388
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200389/**
Mario Six8fac2912018-07-10 08:40:17 +0200390 * efi_restore_tpl() - lower the task priority level
391 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200392 *
393 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200394 *
Mario Six8fac2912018-07-10 08:40:17 +0200395 * See the Unified Extensible Firmware Interface (UEFI) specification for
396 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200397 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100398static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100399{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200400 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200401
402 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200403 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200404 efi_tpl = old_tpl;
405 if (efi_tpl > TPL_HIGH_LEVEL)
406 efi_tpl = TPL_HIGH_LEVEL;
407
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100408 /*
409 * Lowering the TPL may have made queued events eligible for execution.
410 */
411 efi_timer_check();
412
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200413 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100414}
415
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200416/**
Mario Six8fac2912018-07-10 08:40:17 +0200417 * efi_allocate_pages_ext() - allocate memory pages
418 * @type: type of allocation to be performed
419 * @memory_type: usage type of the allocated memory
420 * @pages: number of pages to be allocated
421 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200422 *
423 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200424 *
Mario Six8fac2912018-07-10 08:40:17 +0200425 * See the Unified Extensible Firmware Interface (UEFI) specification for
426 * details.
427 *
428 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200429 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900430static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100431 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900432 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100433{
434 efi_status_t r;
435
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100436 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100437 r = efi_allocate_pages(type, memory_type, pages, memory);
438 return EFI_EXIT(r);
439}
440
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200441/**
Mario Six8fac2912018-07-10 08:40:17 +0200442 * efi_free_pages_ext() - Free memory pages.
443 * @memory: start of the memory area to be freed
444 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200445 *
446 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200447 *
448 * See the Unified Extensible Firmware Interface (UEFI) specification for
449 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200450 *
Mario Six8fac2912018-07-10 08:40:17 +0200451 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200452 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900453static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100454 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100455{
456 efi_status_t r;
457
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900458 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100459 r = efi_free_pages(memory, pages);
460 return EFI_EXIT(r);
461}
462
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200463/**
Mario Six8fac2912018-07-10 08:40:17 +0200464 * efi_get_memory_map_ext() - get map describing memory usage
465 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
466 * on exit the size of the copied memory map
467 * @memory_map: buffer to which the memory map is written
468 * @map_key: key for the memory map
469 * @descriptor_size: size of an individual memory descriptor
470 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200471 *
472 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200473 *
Mario Six8fac2912018-07-10 08:40:17 +0200474 * See the Unified Extensible Firmware Interface (UEFI) specification for
475 * details.
476 *
477 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200478 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900479static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100480 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900481 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100482 efi_uintn_t *map_key,
483 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900484 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100485{
486 efi_status_t r;
487
488 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
489 map_key, descriptor_size, descriptor_version);
490 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
491 descriptor_size, descriptor_version);
492 return EFI_EXIT(r);
493}
494
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200495/**
Mario Six8fac2912018-07-10 08:40:17 +0200496 * efi_allocate_pool_ext() - allocate memory from pool
497 * @pool_type: type of the pool from which memory is to be allocated
498 * @size: number of bytes to be allocated
499 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200500 *
501 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200502 *
503 * See the Unified Extensible Firmware Interface (UEFI) specification for
504 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200505 *
Mario Six8fac2912018-07-10 08:40:17 +0200506 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200507 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200508static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100509 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200510 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100511{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100512 efi_status_t r;
513
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100514 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200515 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100516 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100517}
518
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200519/**
Mario Six8fac2912018-07-10 08:40:17 +0200520 * efi_free_pool_ext() - free memory from pool
521 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200522 *
523 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200524 *
Mario Six8fac2912018-07-10 08:40:17 +0200525 * See the Unified Extensible Firmware Interface (UEFI) specification for
526 * details.
527 *
528 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200529 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200530static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100531{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100532 efi_status_t r;
533
534 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200535 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100536 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100537}
538
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200539/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200540 * efi_add_handle() - add a new handle to the object list
541 *
542 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100543 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200544 * The protocols list is initialized. The handle is added to the list of known
545 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100546 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200547void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100548{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200549 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100550 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200551 INIT_LIST_HEAD(&handle->protocols);
552 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100553}
554
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200555/**
Mario Six8fac2912018-07-10 08:40:17 +0200556 * efi_create_handle() - create handle
557 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200558 *
Mario Six8fac2912018-07-10 08:40:17 +0200559 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200560 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100561efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200562{
563 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200564
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200565 obj = calloc(1, sizeof(struct efi_object));
566 if (!obj)
567 return EFI_OUT_OF_RESOURCES;
568
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100569 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200570 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200571
572 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200573}
574
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200575/**
Mario Six8fac2912018-07-10 08:40:17 +0200576 * efi_search_protocol() - find a protocol on a handle.
577 * @handle: handle
578 * @protocol_guid: GUID of the protocol
579 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100580 *
Mario Six8fac2912018-07-10 08:40:17 +0200581 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100582 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100583efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100584 const efi_guid_t *protocol_guid,
585 struct efi_handler **handler)
586{
587 struct efi_object *efiobj;
588 struct list_head *lhandle;
589
590 if (!handle || !protocol_guid)
591 return EFI_INVALID_PARAMETER;
592 efiobj = efi_search_obj(handle);
593 if (!efiobj)
594 return EFI_INVALID_PARAMETER;
595 list_for_each(lhandle, &efiobj->protocols) {
596 struct efi_handler *protocol;
597
598 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100599 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600 if (handler)
601 *handler = protocol;
602 return EFI_SUCCESS;
603 }
604 }
605 return EFI_NOT_FOUND;
606}
607
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200608/**
Mario Six8fac2912018-07-10 08:40:17 +0200609 * efi_remove_protocol() - delete protocol from a handle
610 * @handle: handle from which the protocol shall be deleted
611 * @protocol: GUID of the protocol to be deleted
612 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100613 *
Mario Six8fac2912018-07-10 08:40:17 +0200614 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100615 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300616static efi_status_t efi_remove_protocol(const efi_handle_t handle,
617 const efi_guid_t *protocol,
618 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100619{
620 struct efi_handler *handler;
621 efi_status_t ret;
622
623 ret = efi_search_protocol(handle, protocol, &handler);
624 if (ret != EFI_SUCCESS)
625 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200626 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200627 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100628 list_del(&handler->link);
629 free(handler);
630 return EFI_SUCCESS;
631}
632
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200633/**
Mario Six8fac2912018-07-10 08:40:17 +0200634 * efi_remove_all_protocols() - delete all protocols from a handle
635 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100636 *
Mario Six8fac2912018-07-10 08:40:17 +0200637 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100638 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100639static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100640{
641 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100642 struct efi_handler *protocol;
643 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100644
645 efiobj = efi_search_obj(handle);
646 if (!efiobj)
647 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100648 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100649 efi_status_t ret;
650
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300651 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300652 protocol->protocol_interface, true);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100653 if (ret != EFI_SUCCESS)
654 return ret;
655 }
656 return EFI_SUCCESS;
657}
658
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200659/**
Mario Six8fac2912018-07-10 08:40:17 +0200660 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100661 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200662 * @handle: handle to delete
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300663 *
664 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100665 */
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300666efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100667{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200668 efi_status_t ret;
669
670 ret = efi_remove_all_protocols(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300671 if (ret != EFI_SUCCESS) {
672 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
673 return ret;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200674 }
675
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300676 return efi_purge_handle(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100677}
678
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200679/**
Mario Six8fac2912018-07-10 08:40:17 +0200680 * efi_is_event() - check if a pointer is a valid event
681 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100682 *
Mario Six8fac2912018-07-10 08:40:17 +0200683 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100684 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100685static efi_status_t efi_is_event(const struct efi_event *event)
686{
687 const struct efi_event *evt;
688
689 if (!event)
690 return EFI_INVALID_PARAMETER;
691 list_for_each_entry(evt, &efi_events, link) {
692 if (evt == event)
693 return EFI_SUCCESS;
694 }
695 return EFI_INVALID_PARAMETER;
696}
Alexander Grafc15d9212016-03-04 01:09:59 +0100697
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200698/**
Mario Six8fac2912018-07-10 08:40:17 +0200699 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200700 *
Mario Six8fac2912018-07-10 08:40:17 +0200701 * @type: type of the event to create
702 * @notify_tpl: task priority level of the event
703 * @notify_function: notification function of the event
704 * @notify_context: pointer passed to the notification function
705 * @group: event group
706 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200707 *
708 * This function is used inside U-Boot code to create an event.
709 *
710 * For the API function implementing the CreateEvent service see
711 * efi_create_event_ext.
712 *
Mario Six8fac2912018-07-10 08:40:17 +0200713 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200714 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100715efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200716 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200717 struct efi_event *event,
718 void *context),
Masahisa Kojima787774a2023-11-10 13:25:38 +0900719 void *notify_context, const efi_guid_t *group,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100720 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100721{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100722 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200723 efi_status_t ret;
724 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200725
Jonathan Gray7758b212017-03-12 19:26:07 +1100726 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200727 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100728
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200729 switch (type) {
730 case 0:
731 case EVT_TIMER:
732 case EVT_NOTIFY_SIGNAL:
733 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
734 case EVT_NOTIFY_WAIT:
735 case EVT_TIMER | EVT_NOTIFY_WAIT:
736 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200737 pool_type = EFI_BOOT_SERVICES_DATA;
738 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200739 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200740 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200741 break;
742 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200743 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200744 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100745
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100746 /*
747 * The UEFI specification requires event notification levels to be
748 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
749 *
750 * Parameter NotifyTpl should not be checked if it is not used.
751 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900752 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100753 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
754 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200755 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100756
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200757 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
758 (void **)&evt);
759 if (ret != EFI_SUCCESS)
760 return ret;
761 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100762 evt->type = type;
763 evt->notify_tpl = notify_tpl;
764 evt->notify_function = notify_function;
765 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100766 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200767 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100768 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100769 list_add_tail(&evt->link, &efi_events);
770 *event = evt;
771 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100772}
773
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200774/*
Mario Six8fac2912018-07-10 08:40:17 +0200775 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100776 *
Mario Six8fac2912018-07-10 08:40:17 +0200777 * @type: type of the event to create
778 * @notify_tpl: task priority level of the event
779 * @notify_function: notification function of the event
780 * @notify_context: pointer passed to the notification function
781 * @event: created event
782 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100783 *
784 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100785 *
Mario Six8fac2912018-07-10 08:40:17 +0200786 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 * details.
788 *
789 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100790 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100791static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100792efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
793 void (EFIAPI *notify_function) (
794 struct efi_event *event,
795 void *context),
796 void *notify_context,
Masahisa Kojima787774a2023-11-10 13:25:38 +0900797 const efi_guid_t *event_group,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100798 struct efi_event **event)
799{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200800 efi_status_t ret;
801
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100802 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100803 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200804
805 /*
806 * The allowable input parameters are the same as in CreateEvent()
807 * except for the following two disallowed event types.
808 */
809 switch (type) {
810 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
811 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
812 ret = EFI_INVALID_PARAMETER;
813 goto out;
814 }
815
816 ret = efi_create_event(type, notify_tpl, notify_function,
817 notify_context, event_group, event);
818out:
819 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100820}
821
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200822/**
Mario Six8fac2912018-07-10 08:40:17 +0200823 * efi_create_event_ext() - create an event
824 * @type: type of the event to create
825 * @notify_tpl: task priority level of the event
826 * @notify_function: notification function of the event
827 * @notify_context: pointer passed to the notification function
828 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200829 *
830 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200831 *
832 * See the Unified Extensible Firmware Interface (UEFI) specification for
833 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200834 *
Mario Six8fac2912018-07-10 08:40:17 +0200835 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200836 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200837static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100838 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200839 void (EFIAPI *notify_function) (
840 struct efi_event *event,
841 void *context),
842 void *notify_context, struct efi_event **event)
843{
844 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
845 notify_context);
846 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100847 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200848}
849
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200850/**
Mario Six8fac2912018-07-10 08:40:17 +0200851 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200852 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200853 * Check if a timer event has occurred or a queued notification function should
854 * be called.
855 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100856 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200857 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100858 */
859void efi_timer_check(void)
860{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100861 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100862 u64 now = timer_get_us();
863
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100864 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200865 if (!timers_enabled)
866 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100867 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200868 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100869 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200870 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100871 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200872 break;
873 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100874 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200875 break;
876 default:
877 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200878 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100879 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200880 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100881 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200882 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200883 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100884}
885
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200886/**
Mario Six8fac2912018-07-10 08:40:17 +0200887 * efi_set_timer() - set the trigger time for a timer event or stop the event
888 * @event: event for which the timer is set
889 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200890 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 *
892 * This is the function for internal usage in U-Boot. For the API function
893 * implementing the SetTimer service see efi_set_timer_ext.
894 *
Mario Six8fac2912018-07-10 08:40:17 +0200895 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200896 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200897efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200898 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100899{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100900 /* Check that the event is valid */
901 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
902 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100903
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200904 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200905 * The parameter defines a multiple of 100 ns.
906 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200907 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200908 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100909
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100910 switch (type) {
911 case EFI_TIMER_STOP:
912 event->trigger_next = -1ULL;
913 break;
914 case EFI_TIMER_PERIODIC:
915 case EFI_TIMER_RELATIVE:
916 event->trigger_next = timer_get_us() + trigger_time;
917 break;
918 default:
919 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100920 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100921 event->trigger_type = type;
922 event->trigger_time = trigger_time;
923 event->is_signaled = false;
924 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100925}
926
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200927/**
Mario Six8fac2912018-07-10 08:40:17 +0200928 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
929 * event
930 * @event: event for which the timer is set
931 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200932 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200933 *
934 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200935 *
Mario Six8fac2912018-07-10 08:40:17 +0200936 * See the Unified Extensible Firmware Interface (UEFI) specification for
937 * details.
938 *
939 *
940 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200941 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200942static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
943 enum efi_timer_delay type,
944 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200945{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900946 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200947 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
948}
949
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200950/**
Mario Six8fac2912018-07-10 08:40:17 +0200951 * efi_wait_for_event() - wait for events to be signaled
952 * @num_events: number of events to be waited for
953 * @event: events to be waited for
954 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200955 *
956 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200957 *
Mario Six8fac2912018-07-10 08:40:17 +0200958 * See the Unified Extensible Firmware Interface (UEFI) specification for
959 * details.
960 *
961 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200962 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100963static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200964 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100965 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100966{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100967 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100968
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100969 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100970
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200971 /* Check parameters */
972 if (!num_events || !event)
973 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200974 /* Check TPL */
975 if (efi_tpl != TPL_APPLICATION)
976 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200977 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100978 if (efi_is_event(event[i]) != EFI_SUCCESS)
979 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200980 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
981 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200982 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200983 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200984 }
985
986 /* Wait for signal */
987 for (;;) {
988 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200989 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200990 goto out;
991 }
992 /* Allow events to occur. */
993 efi_timer_check();
994 }
995
996out:
997 /*
998 * Reset the signal which is passed to the caller to allow periodic
999 * events to occur.
1000 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +02001001 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001002 if (index)
1003 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +01001004
1005 return EFI_EXIT(EFI_SUCCESS);
1006}
1007
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001008/**
Mario Six8fac2912018-07-10 08:40:17 +02001009 * efi_signal_event_ext() - signal an EFI event
1010 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001011 *
1012 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001013 *
1014 * See the Unified Extensible Firmware Interface (UEFI) specification for
1015 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001016 *
1017 * This functions sets the signaled state of the event and queues the
1018 * notification function for execution.
1019 *
Mario Six8fac2912018-07-10 08:40:17 +02001020 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001021 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001022static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001023{
1024 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001025 if (efi_is_event(event) != EFI_SUCCESS)
1026 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001027 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +01001028 return EFI_EXIT(EFI_SUCCESS);
1029}
1030
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001031/**
Mario Six8fac2912018-07-10 08:40:17 +02001032 * efi_close_event() - close an EFI event
1033 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001034 *
1035 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001036 *
Mario Six8fac2912018-07-10 08:40:17 +02001037 * See the Unified Extensible Firmware Interface (UEFI) specification for
1038 * details.
1039 *
1040 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001041 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001042static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001043{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001044 struct efi_register_notify_event *item, *next;
1045
Alexander Grafc15d9212016-03-04 01:09:59 +01001046 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001047 if (efi_is_event(event) != EFI_SUCCESS)
1048 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001049
1050 /* Remove protocol notify registrations for the event */
1051 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1052 link) {
1053 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001054 struct efi_protocol_notification *hitem, *hnext;
1055
1056 /* Remove signaled handles */
1057 list_for_each_entry_safe(hitem, hnext, &item->handles,
1058 link) {
1059 list_del(&hitem->link);
1060 free(hitem);
1061 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001062 list_del(&item->link);
1063 free(item);
1064 }
1065 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001066 /* Remove event from queue */
1067 if (efi_event_is_queued(event))
1068 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001069
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001070 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001071 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001072 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001073}
1074
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001075/**
Mario Six8fac2912018-07-10 08:40:17 +02001076 * efi_check_event() - check if an event is signaled
1077 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001078 *
1079 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001080 *
1081 * See the Unified Extensible Firmware Interface (UEFI) specification for
1082 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001083 *
Mario Six8fac2912018-07-10 08:40:17 +02001084 * If an event is not signaled yet, the notification function is queued. The
1085 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001086 *
Mario Six8fac2912018-07-10 08:40:17 +02001087 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001088 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001089static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001090{
1091 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001092 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001093 if (efi_is_event(event) != EFI_SUCCESS ||
1094 event->type & EVT_NOTIFY_SIGNAL)
1095 return EFI_EXIT(EFI_INVALID_PARAMETER);
1096 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001097 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001098 if (event->is_signaled) {
1099 event->is_signaled = false;
1100 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001101 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001102 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001103}
1104
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001105/**
Mario Six8fac2912018-07-10 08:40:17 +02001106 * efi_search_obj() - find the internal EFI object for a handle
1107 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001108 *
Mario Six8fac2912018-07-10 08:40:17 +02001109 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001110 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001111struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001112{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001113 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001114
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001115 if (!handle)
1116 return NULL;
1117
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001118 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001119 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001120 return efiobj;
1121 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001122 return NULL;
1123}
1124
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001125/**
Mario Six8fac2912018-07-10 08:40:17 +02001126 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1127 * to a protocol
1128 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001129 *
Mario Six8fac2912018-07-10 08:40:17 +02001130 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001131 */
1132static struct efi_open_protocol_info_entry *efi_create_open_info(
1133 struct efi_handler *handler)
1134{
1135 struct efi_open_protocol_info_item *item;
1136
1137 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1138 if (!item)
1139 return NULL;
1140 /* Append the item to the open protocol info list. */
1141 list_add_tail(&item->link, &handler->open_infos);
1142
1143 return &item->info;
1144}
1145
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001146/**
Mario Six8fac2912018-07-10 08:40:17 +02001147 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1148 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001149 *
Mario Six8fac2912018-07-10 08:40:17 +02001150 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001151 */
1152static efi_status_t efi_delete_open_info(
1153 struct efi_open_protocol_info_item *item)
1154{
1155 list_del(&item->link);
1156 free(item);
1157 return EFI_SUCCESS;
1158}
1159
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001160/**
Mario Six8fac2912018-07-10 08:40:17 +02001161 * efi_add_protocol() - install new protocol on a handle
1162 * @handle: handle on which the protocol shall be installed
1163 * @protocol: GUID of the protocol to be installed
1164 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001165 *
Mario Six8fac2912018-07-10 08:40:17 +02001166 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001167 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001168efi_status_t efi_add_protocol(const efi_handle_t handle,
1169 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001170 void *protocol_interface)
1171{
1172 struct efi_object *efiobj;
1173 struct efi_handler *handler;
1174 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001175 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001176
1177 efiobj = efi_search_obj(handle);
1178 if (!efiobj)
1179 return EFI_INVALID_PARAMETER;
1180 ret = efi_search_protocol(handle, protocol, NULL);
1181 if (ret != EFI_NOT_FOUND)
1182 return EFI_INVALID_PARAMETER;
1183 handler = calloc(1, sizeof(struct efi_handler));
1184 if (!handler)
1185 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001186 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001187 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001188 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001189 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001190
1191 /* Notify registered events */
1192 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001193 if (!guidcmp(protocol, &event->protocol)) {
1194 struct efi_protocol_notification *notif;
1195
1196 notif = calloc(1, sizeof(*notif));
1197 if (!notif) {
1198 list_del(&handler->link);
1199 free(handler);
1200 return EFI_OUT_OF_RESOURCES;
1201 }
1202 notif->handle = handle;
1203 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001204 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001205 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001206 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001207 }
1208
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001209 if (!guidcmp(&efi_guid_device_path, protocol))
1210 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001211 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001212}
1213
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001214/**
Mario Six8fac2912018-07-10 08:40:17 +02001215 * efi_install_protocol_interface() - install protocol interface
1216 * @handle: handle on which the protocol shall be installed
1217 * @protocol: GUID of the protocol to be installed
1218 * @protocol_interface_type: type of the interface to be installed,
1219 * always EFI_NATIVE_INTERFACE
1220 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001221 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001222 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001223 *
Mario Six8fac2912018-07-10 08:40:17 +02001224 * See the Unified Extensible Firmware Interface (UEFI) specification for
1225 * details.
1226 *
1227 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001228 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001229static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001230 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001231 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001232{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001233 efi_status_t r;
1234
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001235 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001236 protocol_interface);
1237
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001238 if (!handle || !protocol ||
1239 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1240 r = EFI_INVALID_PARAMETER;
1241 goto out;
1242 }
1243
1244 /* Create new handle if requested. */
1245 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001246 r = efi_create_handle(handle);
1247 if (r != EFI_SUCCESS)
1248 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001249 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001250 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001251 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001252 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001253 /* Add new protocol */
1254 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001255out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001256 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001257}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001258
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001259/**
Mario Six8fac2912018-07-10 08:40:17 +02001260 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001261 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001262 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001263 * @number_of_drivers: number of child controllers
1264 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001265 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001266 * The allocated buffer has to be freed with free().
1267 *
Mario Six8fac2912018-07-10 08:40:17 +02001268 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001269 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001270static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001271 const efi_guid_t *protocol,
1272 efi_uintn_t *number_of_drivers,
1273 efi_handle_t **driver_handle_buffer)
1274{
1275 struct efi_handler *handler;
1276 struct efi_open_protocol_info_item *item;
1277 efi_uintn_t count = 0, i;
1278 bool duplicate;
1279
1280 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001281 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001282 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001283 continue;
1284 list_for_each_entry(item, &handler->open_infos, link) {
1285 if (item->info.attributes &
1286 EFI_OPEN_PROTOCOL_BY_DRIVER)
1287 ++count;
1288 }
1289 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001290 *number_of_drivers = 0;
1291 if (!count) {
1292 *driver_handle_buffer = NULL;
1293 return EFI_SUCCESS;
1294 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001295 /*
1296 * Create buffer. In case of duplicate driver assignments the buffer
1297 * will be too large. But that does not harm.
1298 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001299 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1300 if (!*driver_handle_buffer)
1301 return EFI_OUT_OF_RESOURCES;
1302 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001303 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001304 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001305 continue;
1306 list_for_each_entry(item, &handler->open_infos, link) {
1307 if (item->info.attributes &
1308 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1309 /* Check this is a new driver */
1310 duplicate = false;
1311 for (i = 0; i < *number_of_drivers; ++i) {
1312 if ((*driver_handle_buffer)[i] ==
1313 item->info.agent_handle)
1314 duplicate = true;
1315 }
1316 /* Copy handle to buffer */
1317 if (!duplicate) {
1318 i = (*number_of_drivers)++;
1319 (*driver_handle_buffer)[i] =
1320 item->info.agent_handle;
1321 }
1322 }
1323 }
1324 }
1325 return EFI_SUCCESS;
1326}
1327
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001328/**
Mario Six8fac2912018-07-10 08:40:17 +02001329 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001330 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001331 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001332 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001333 *
1334 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001335 *
1336 * See the Unified Extensible Firmware Interface (UEFI) specification for
1337 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001338 *
Mario Six8fac2912018-07-10 08:40:17 +02001339 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001340 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001341static efi_status_t efi_disconnect_all_drivers
1342 (efi_handle_t handle,
1343 const efi_guid_t *protocol,
1344 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001345{
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001346 efi_uintn_t number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001347 efi_handle_t *driver_handle_buffer;
1348 efi_status_t r, ret;
1349
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001350 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001351 &driver_handle_buffer);
1352 if (ret != EFI_SUCCESS)
1353 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001354 if (!number_of_drivers)
1355 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001356
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001357 while (number_of_drivers) {
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001358 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001359 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001360 driver_handle_buffer[--number_of_drivers],
1361 child_handle));
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001362 if (r != EFI_SUCCESS)
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001363 ret = r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001364 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001365
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001366 free(driver_handle_buffer);
1367 return ret;
1368}
1369
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001370/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001371 * efi_uninstall_protocol() - uninstall protocol interface
1372 *
Mario Six8fac2912018-07-10 08:40:17 +02001373 * @handle: handle from which the protocol shall be removed
1374 * @protocol: GUID of the protocol to be removed
1375 * @protocol_interface: interface to be removed
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001376 * @preserve: preserve or delete the handle and remove it from any
1377 * list it participates if no protocols remain
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001378 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001379 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001380 *
1381 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001382 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001383static efi_status_t efi_uninstall_protocol
1384 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001385 void *protocol_interface, bool preserve)
Alexander Grafc15d9212016-03-04 01:09:59 +01001386{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001387 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001388 struct efi_open_protocol_info_item *item;
1389 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001390 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001391
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001392 /* Find the protocol on the handle */
1393 r = efi_search_protocol(handle, protocol, &handler);
1394 if (r != EFI_SUCCESS)
1395 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001396 if (handler->protocol_interface != protocol_interface)
1397 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001398 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001399 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001400 if (r != EFI_SUCCESS) {
1401 r = EFI_ACCESS_DENIED;
Ilias Apalodimas8e315e92023-11-28 21:10:31 +02001402 /*
1403 * This will reconnect all controllers of the handle, even ones
1404 * that were not connected before. This can be done better
1405 * but we are following the EDKII implementation on this for
1406 * now
1407 */
1408 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001409 goto out;
1410 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001411 /* Close protocol */
1412 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1413 if (item->info.attributes ==
1414 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1415 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1416 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001417 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001418 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001419 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001420 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001421 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001422 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001423 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001424 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001425 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001426 if (r != EFI_SUCCESS)
1427 return r;
1428 /*
1429 * We don't care about the return value here since the
1430 * handle might have more protocols installed
1431 */
1432 if (!preserve)
1433 efi_purge_handle(handle);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001434out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001435 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001436}
1437
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001438/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001439 * efi_uninstall_protocol_interface() - uninstall protocol interface
1440 * @handle: handle from which the protocol shall be removed
1441 * @protocol: GUID of the protocol to be removed
1442 * @protocol_interface: interface to be removed
1443 *
1444 * This function implements the UninstallProtocolInterface service.
1445 *
1446 * See the Unified Extensible Firmware Interface (UEFI) specification for
1447 * details.
1448 *
1449 * Return: status code
1450 */
1451static efi_status_t EFIAPI efi_uninstall_protocol_interface
1452 (efi_handle_t handle, const efi_guid_t *protocol,
1453 void *protocol_interface)
1454{
1455 efi_status_t ret;
1456
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001457 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001458
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001459 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001460 if (ret != EFI_SUCCESS)
1461 goto out;
1462
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001463out:
1464 return EFI_EXIT(ret);
1465}
1466
1467/**
Mario Six8fac2912018-07-10 08:40:17 +02001468 * efi_register_protocol_notify() - register an event for notification when a
1469 * protocol is installed.
1470 * @protocol: GUID of the protocol whose installation shall be notified
1471 * @event: event to be signaled upon installation of the protocol
1472 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001473 *
1474 * This function implements the RegisterProtocolNotify service.
1475 * See the Unified Extensible Firmware Interface (UEFI) specification
1476 * for details.
1477 *
Mario Six8fac2912018-07-10 08:40:17 +02001478 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001479 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001480efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1481 struct efi_event *event,
1482 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001483{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001484 struct efi_register_notify_event *item;
1485 efi_status_t ret = EFI_SUCCESS;
1486
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001487 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001488
1489 if (!protocol || !event || !registration) {
1490 ret = EFI_INVALID_PARAMETER;
1491 goto out;
1492 }
1493
1494 item = calloc(1, sizeof(struct efi_register_notify_event));
1495 if (!item) {
1496 ret = EFI_OUT_OF_RESOURCES;
1497 goto out;
1498 }
1499
1500 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301501 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001502 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001503
1504 list_add_tail(&item->link, &efi_register_notify_events);
1505
1506 *registration = item;
1507out:
1508 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001509}
1510
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001511/**
Mario Six8fac2912018-07-10 08:40:17 +02001512 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001513 *
Mario Six8fac2912018-07-10 08:40:17 +02001514 * @search_type: selection criterion
1515 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001516 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001517 *
1518 * See the documentation of the LocateHandle service in the UEFI specification.
1519 *
Mario Six8fac2912018-07-10 08:40:17 +02001520 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001521 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001522static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001523 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001524{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001525 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001526
1527 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001528 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001529 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001530 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001531 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001532 return (ret != EFI_SUCCESS);
1533 default:
1534 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001535 return -1;
1536 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001537}
1538
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001539/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001540 * efi_check_register_notify_event() - check if registration key is valid
1541 *
1542 * Check that a pointer is a valid registration key as returned by
1543 * RegisterProtocolNotify().
1544 *
1545 * @key: registration key
1546 * Return: valid registration key or NULL
1547 */
1548static struct efi_register_notify_event *efi_check_register_notify_event
1549 (void *key)
1550{
1551 struct efi_register_notify_event *event;
1552
1553 list_for_each_entry(event, &efi_register_notify_events, link) {
1554 if (event == (struct efi_register_notify_event *)key)
1555 return event;
1556 }
1557 return NULL;
1558}
1559
1560/**
Mario Six8fac2912018-07-10 08:40:17 +02001561 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001562 *
1563 * @search_type: selection criterion
1564 * @protocol: GUID of the protocol
1565 * @search_key: registration key
1566 * @buffer_size: size of the buffer to receive the handles in bytes
1567 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001568 *
1569 * This function is meant for U-Boot internal calls. For the API implementation
1570 * of the LocateHandle service see efi_locate_handle_ext.
1571 *
Mario Six8fac2912018-07-10 08:40:17 +02001572 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001573 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001574static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001575 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001576 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001577 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001578{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001579 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001580 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001581 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001582 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001583
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001584 /* Check parameters */
1585 switch (search_type) {
1586 case ALL_HANDLES:
1587 break;
1588 case BY_REGISTER_NOTIFY:
1589 if (!search_key)
1590 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001591 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001592 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001593 if (!event)
1594 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001595 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001596 case BY_PROTOCOL:
1597 if (!protocol)
1598 return EFI_INVALID_PARAMETER;
1599 break;
1600 default:
1601 return EFI_INVALID_PARAMETER;
1602 }
1603
Alexander Grafc15d9212016-03-04 01:09:59 +01001604 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001605 if (search_type == BY_REGISTER_NOTIFY) {
1606 if (list_empty(&event->handles))
1607 return EFI_NOT_FOUND;
1608 handle = list_first_entry(&event->handles,
1609 struct efi_protocol_notification,
1610 link);
1611 efiobj = handle->handle;
1612 size += sizeof(void *);
1613 } else {
1614 list_for_each_entry(efiobj, &efi_obj_list, link) {
1615 if (!efi_search(search_type, protocol, efiobj))
1616 size += sizeof(void *);
1617 }
1618 if (size == 0)
1619 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001620 }
1621
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001622 if (!buffer_size)
1623 return EFI_INVALID_PARAMETER;
1624
Alexander Grafc15d9212016-03-04 01:09:59 +01001625 if (*buffer_size < size) {
1626 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001627 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001628 }
1629
Rob Clarkcdee3372017-08-06 14:10:07 -04001630 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001631
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001632 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001633 if (!buffer)
1634 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001635
Alexander Grafc15d9212016-03-04 01:09:59 +01001636 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001637 if (search_type == BY_REGISTER_NOTIFY) {
1638 *buffer = efiobj;
1639 list_del(&handle->link);
1640 } else {
1641 list_for_each_entry(efiobj, &efi_obj_list, link) {
1642 if (!efi_search(search_type, protocol, efiobj))
1643 *buffer++ = efiobj;
1644 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001645 }
1646
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001647 return EFI_SUCCESS;
1648}
1649
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001650/**
Mario Six8fac2912018-07-10 08:40:17 +02001651 * efi_locate_handle_ext() - locate handles implementing a protocol.
1652 * @search_type: selection criterion
1653 * @protocol: GUID of the protocol
1654 * @search_key: registration key
1655 * @buffer_size: size of the buffer to receive the handles in bytes
1656 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001657 *
1658 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001659 *
Mario Six8fac2912018-07-10 08:40:17 +02001660 * See the Unified Extensible Firmware Interface (UEFI) specification for
1661 * details.
1662 *
1663 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001664 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001665static efi_status_t EFIAPI efi_locate_handle_ext(
1666 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001667 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001668 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001669{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001670 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001671 buffer_size, buffer);
1672
1673 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1674 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001675}
1676
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001677/**
Mario Six8fac2912018-07-10 08:40:17 +02001678 * efi_remove_configuration_table() - collapses configuration table entries,
1679 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001680 *
Mario Six8fac2912018-07-10 08:40:17 +02001681 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001682 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001683static void efi_remove_configuration_table(int i)
1684{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001685 struct efi_configuration_table *this = &systab.tables[i];
1686 struct efi_configuration_table *next = &systab.tables[i + 1];
1687 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001688
1689 memmove(this, next, (ulong)end - (ulong)next);
1690 systab.nr_tables--;
1691}
1692
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001693/**
Mario Six8fac2912018-07-10 08:40:17 +02001694 * efi_install_configuration_table() - adds, updates, or removes a
1695 * configuration table
1696 * @guid: GUID of the installed table
1697 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001698 *
1699 * This function is used for internal calls. For the API implementation of the
1700 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1701 *
Mario Six8fac2912018-07-10 08:40:17 +02001702 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001703 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001704efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1705 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001706{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001707 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001708 int i;
1709
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001710 if (!guid)
1711 return EFI_INVALID_PARAMETER;
1712
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001713 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001714 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001715 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001716 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001717 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001718 else
1719 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001720 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001721 }
1722 }
1723
Alexander Graffe3366f2017-07-26 13:41:04 +02001724 if (!table)
1725 return EFI_NOT_FOUND;
1726
Alexander Grafc15d9212016-03-04 01:09:59 +01001727 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001728 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001729 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001730
1731 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301732 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001733 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001734 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001735
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001736out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001737 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001738 efi_update_table_header_crc32(&systab.hdr);
1739
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001740 /* Notify that the configuration table was changed */
1741 list_for_each_entry(evt, &efi_events, link) {
1742 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001743 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001744 break;
1745 }
1746 }
1747
Alexander Grafc5c11632016-08-19 01:23:24 +02001748 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001749}
1750
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001751/**
Mario Six8fac2912018-07-10 08:40:17 +02001752 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1753 * configuration table.
1754 * @guid: GUID of the installed table
1755 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001756 *
1757 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001758 *
Mario Six8fac2912018-07-10 08:40:17 +02001759 * See the Unified Extensible Firmware Interface (UEFI) specification for
1760 * details.
1761 *
1762 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001763 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001764static efi_status_t
1765EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1766 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001767{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001768 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001769 return EFI_EXIT(efi_install_configuration_table(guid, table));
1770}
1771
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001772/**
Mario Six8fac2912018-07-10 08:40:17 +02001773 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001774 *
1775 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001776 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001777 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001778 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001779 * code is returned.
1780 *
1781 * @device_path: device path of the loaded image
1782 * @file_path: file path of the loaded image
1783 * @handle_ptr: handle of the loaded image
1784 * @info_ptr: loaded image protocol
1785 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001786 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001787efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1788 struct efi_device_path *file_path,
1789 struct efi_loaded_image_obj **handle_ptr,
1790 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001791{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001792 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001793 struct efi_loaded_image *info = NULL;
1794 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001795 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001796
1797 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1798 *handle_ptr = NULL;
1799 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001800
1801 info = calloc(1, sizeof(*info));
1802 if (!info)
1803 return EFI_OUT_OF_RESOURCES;
1804 obj = calloc(1, sizeof(*obj));
1805 if (!obj) {
1806 free(info);
1807 return EFI_OUT_OF_RESOURCES;
1808 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001809 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001810
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001811 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001812 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001813
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001814 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001815 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001816 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001817
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001818 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001819 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001820
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +02001821 dp = efi_dp_concat(device_path, file_path, 0);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001822 if (!dp) {
1823 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001824 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001825 }
1826 } else {
1827 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001828 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001829 ret = efi_add_protocol(&obj->header,
1830 &efi_guid_loaded_image_device_path, dp);
1831 if (ret != EFI_SUCCESS)
1832 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001833
1834 /*
1835 * When asking for the loaded_image interface, just
1836 * return handle which points to loaded_image_info
1837 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001838 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001839 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001840 if (ret != EFI_SUCCESS)
1841 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001842
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001843 *info_ptr = info;
1844 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001845
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001846 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001847failure:
1848 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001849 efi_delete_handle(&obj->header);
1850 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001851 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001852}
1853
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001854/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001855 * efi_locate_device_path() - Get the device path and handle of an device
1856 * implementing a protocol
1857 * @protocol: GUID of the protocol
1858 * @device_path: device path
1859 * @device: handle of the device
1860 *
1861 * This function implements the LocateDevicePath service.
1862 *
1863 * See the Unified Extensible Firmware Interface (UEFI) specification for
1864 * details.
1865 *
1866 * Return: status code
1867 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001868efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1869 struct efi_device_path **device_path,
1870 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001871{
1872 struct efi_device_path *dp;
1873 size_t i;
1874 struct efi_handler *handler;
1875 efi_handle_t *handles;
1876 size_t len, len_dp;
1877 size_t len_best = 0;
1878 efi_uintn_t no_handles;
1879 u8 *remainder;
1880 efi_status_t ret;
1881
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001882 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001883
1884 if (!protocol || !device_path || !*device_path) {
1885 ret = EFI_INVALID_PARAMETER;
1886 goto out;
1887 }
1888
1889 /* Find end of device path */
1890 len = efi_dp_instance_size(*device_path);
1891
1892 /* Get all handles implementing the protocol */
1893 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1894 &no_handles, &handles));
1895 if (ret != EFI_SUCCESS)
1896 goto out;
1897
1898 for (i = 0; i < no_handles; ++i) {
1899 /* Find the device path protocol */
1900 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1901 &handler);
1902 if (ret != EFI_SUCCESS)
1903 continue;
1904 dp = (struct efi_device_path *)handler->protocol_interface;
1905 len_dp = efi_dp_instance_size(dp);
1906 /*
1907 * This handle can only be a better fit
1908 * if its device path length is longer than the best fit and
1909 * if its device path length is shorter of equal the searched
1910 * device path.
1911 */
1912 if (len_dp <= len_best || len_dp > len)
1913 continue;
1914 /* Check if dp is a subpath of device_path */
1915 if (memcmp(*device_path, dp, len_dp))
1916 continue;
1917 if (!device) {
1918 ret = EFI_INVALID_PARAMETER;
1919 goto out;
1920 }
1921 *device = handles[i];
1922 len_best = len_dp;
1923 }
1924 if (len_best) {
1925 remainder = (u8 *)*device_path + len_best;
1926 *device_path = (struct efi_device_path *)remainder;
1927 ret = EFI_SUCCESS;
1928 } else {
1929 ret = EFI_NOT_FOUND;
1930 }
1931out:
1932 return EFI_EXIT(ret);
1933}
1934
1935/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001936 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001937 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001938 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1939 * callers obligation to update the memory type as needed.
1940 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001941 * @file_path: the path of the image to load
1942 * @buffer: buffer containing the loaded image
1943 * @size: size of the loaded image
1944 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001945 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001946static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001947efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001948 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001949{
Rob Clark857a1222017-09-13 18:05:35 -04001950 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001951 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001952 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001953 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001954
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001955 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001956 f = efi_file_from_path(file_path);
1957 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001958 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001959
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001960 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001961 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001962 goto error;
1963
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001964 /*
1965 * When reading the file we do not yet know if it contains an
1966 * application, a boottime driver, or a runtime driver. So here we
1967 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1968 * update the reservation according to the image type.
1969 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001970 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1971 EFI_BOOT_SERVICES_DATA,
1972 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001973 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001974 ret = EFI_OUT_OF_RESOURCES;
1975 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001976 }
1977
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001978 /* Read file */
1979 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1980 if (ret != EFI_SUCCESS)
1981 efi_free_pages(addr, efi_size_in_pages(bs));
1982 *buffer = (void *)(uintptr_t)addr;
1983 *size = bs;
1984error:
1985 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001986 return ret;
1987}
1988
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001989/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001990 * efi_load_image_from_path() - load an image using a file path
1991 *
1992 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1993 * callers obligation to update the memory type as needed.
1994 *
1995 * @boot_policy: true for request originating from the boot manager
1996 * @file_path: the path of the image to load
1997 * @buffer: buffer containing the loaded image
1998 * @size: size of the loaded image
1999 * Return: status code
2000 */
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002001efi_status_t efi_load_image_from_path(bool boot_policy,
2002 struct efi_device_path *file_path,
2003 void **buffer, efi_uintn_t *size)
2004{
2005 efi_handle_t device;
2006 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002007 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002008 struct efi_load_file_protocol *load_file_protocol = NULL;
2009 efi_uintn_t buffer_size;
2010 uint64_t addr, pages;
2011 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002012 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002013
2014 /* In case of failure nothing is returned */
2015 *buffer = NULL;
2016 *size = 0;
2017
2018 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002019 device = efi_dp_find_obj(dp, NULL, &rem);
2020 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2021 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002022 if (ret == EFI_SUCCESS)
2023 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002024
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002025 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002026 if (ret == EFI_SUCCESS) {
2027 guid = &efi_guid_load_file_protocol;
2028 } else if (!boot_policy) {
2029 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002030 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002031 }
2032 if (ret != EFI_SUCCESS)
2033 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002034 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002035 if (ret != EFI_SUCCESS)
2036 return EFI_NOT_FOUND;
2037 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002038 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002039 ret = EFI_CALL(load_file_protocol->load_file(
2040 load_file_protocol, rem, boot_policy,
2041 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002042 if (ret != EFI_BUFFER_TOO_SMALL)
2043 goto out;
2044 pages = efi_size_in_pages(buffer_size);
2045 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2046 pages, &addr);
2047 if (ret != EFI_SUCCESS) {
2048 ret = EFI_OUT_OF_RESOURCES;
2049 goto out;
2050 }
2051 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002052 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002053 &buffer_size, (void *)(uintptr_t)addr));
2054 if (ret != EFI_SUCCESS)
2055 efi_free_pages(addr, pages);
2056out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002057 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002058 if (ret == EFI_SUCCESS) {
2059 *buffer = (void *)(uintptr_t)addr;
2060 *size = buffer_size;
2061 }
2062
2063 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002064}
2065
2066/**
Mario Six8fac2912018-07-10 08:40:17 +02002067 * efi_load_image() - load an EFI image into memory
2068 * @boot_policy: true for request originating from the boot manager
2069 * @parent_image: the caller's image handle
2070 * @file_path: the path of the image to load
2071 * @source_buffer: memory location from which the image is installed
2072 * @source_size: size of the memory area from which the image is installed
2073 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002074 *
2075 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002076 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002077 * See the Unified Extensible Firmware Interface (UEFI) specification
2078 * for details.
2079 *
Mario Six8fac2912018-07-10 08:40:17 +02002080 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002081 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002082efi_status_t EFIAPI efi_load_image(bool boot_policy,
2083 efi_handle_t parent_image,
2084 struct efi_device_path *file_path,
2085 void *source_buffer,
2086 efi_uintn_t source_size,
2087 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002088{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002089 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002090 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002091 struct efi_loaded_image_obj **image_obj =
2092 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002093 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002094 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002095
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002096 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002097 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002098
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002099 if (!image_handle || (!source_buffer && !file_path) ||
2100 !efi_search_obj(parent_image) ||
2101 /* The parent image handle must refer to a loaded image */
2102 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002103 ret = EFI_INVALID_PARAMETER;
2104 goto error;
2105 }
Rob Clark857a1222017-09-13 18:05:35 -04002106
2107 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002108 ret = efi_load_image_from_path(boot_policy, file_path,
2109 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002110 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002111 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002112 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002113 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002114 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002115 /* split file_path which contains both the device and file parts */
2116 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002117 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002118 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002119 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002120 if (!source_buffer)
2121 /* Release buffer to which file was loaded */
2122 efi_free_pages((uintptr_t)dest_buffer,
2123 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002124 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002125 info->system_table = &systab;
2126 info->parent_handle = parent_image;
2127 } else {
2128 /* The image is invalid. Release all associated resources. */
2129 efi_delete_handle(*image_handle);
2130 *image_handle = NULL;
2131 free(info);
2132 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002133error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002134 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002135}
2136
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002137/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002138 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2139 */
2140static void efi_exit_caches(void)
2141{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002142#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002143 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002144 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2145 * caches are enabled.
2146 *
2147 * TODO:
2148 * According to the UEFI spec caches that can be managed via CP15
2149 * operations should be enabled. Caches requiring platform information
2150 * to manage should be disabled. This should not happen in
2151 * ExitBootServices() but before invoking any UEFI binary is invoked.
2152 *
2153 * We want to keep the current workaround while GRUB prior to version
2154 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002155 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002156 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002157#endif
2158}
2159
2160/**
Mario Six8fac2912018-07-10 08:40:17 +02002161 * efi_exit_boot_services() - stop all boot services
2162 * @image_handle: handle of the loaded image
2163 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002164 *
2165 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002166 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002167 * See the Unified Extensible Firmware Interface (UEFI) specification
2168 * for details.
2169 *
Mario Six8fac2912018-07-10 08:40:17 +02002170 * All timer events are disabled. For exit boot services events the
2171 * notification function is called. The boot services are disabled in the
2172 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002173 *
Mario Six8fac2912018-07-10 08:40:17 +02002174 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002175 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002176static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002177 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002178{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002179 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002180 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002181
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002182 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002183
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002184 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002185 if (map_key != efi_memory_map_key) {
2186 ret = EFI_INVALID_PARAMETER;
2187 goto out;
2188 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002189
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002190 /* Check if ExitBootServices has already been called */
2191 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002192 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002193
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002194 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2195 list_for_each_entry(evt, &efi_events, link) {
2196 if (evt->group &&
2197 !guidcmp(evt->group,
2198 &efi_guid_event_group_before_exit_boot_services)) {
2199 efi_signal_event(evt);
2200 break;
2201 }
2202 }
2203
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002204 /* Stop all timer related activities */
2205 timers_enabled = false;
2206
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002207 /* Add related events to the event group */
2208 list_for_each_entry(evt, &efi_events, link) {
2209 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2210 evt->group = &efi_guid_event_group_exit_boot_services;
2211 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002212 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002213 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002214 if (evt->group &&
2215 !guidcmp(evt->group,
2216 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002217 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002218 break;
2219 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002220 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002221
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002222 /* Make sure that notification functions are not called anymore */
2223 efi_tpl = TPL_HIGH_LEVEL;
2224
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002225 /* Notify variable services */
2226 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002227
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002228 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2229 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2230 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2231 list_del(&evt->link);
2232 }
2233
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002234 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002235 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002236 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002237 udc_disconnect();
2238 board_quiesce_devices();
Janne Grunau64609732024-11-23 22:44:05 +01002239 dm_remove_devices_active();
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002240 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002241
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002242 /* Patch out unsupported runtime function */
2243 efi_runtime_detach();
2244
Alexander Graffa5246b2018-11-15 21:23:47 +01002245 /* Fix up caches for EFI payloads if necessary */
2246 efi_exit_caches();
2247
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002248 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002249 systab.con_in_handle = NULL;
2250 systab.con_in = NULL;
2251 systab.con_out_handle = NULL;
2252 systab.con_out = NULL;
2253 systab.stderr_handle = NULL;
2254 systab.std_err = NULL;
2255 systab.boottime = NULL;
2256
2257 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002258 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002259
Alexander Grafc15d9212016-03-04 01:09:59 +01002260 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002261 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002262 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002263out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002264 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2265 if (ret != EFI_SUCCESS)
2266 efi_tcg2_notify_exit_boot_services_failed();
2267 }
2268
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002269 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002270}
2271
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002272/**
Mario Six8fac2912018-07-10 08:40:17 +02002273 * efi_get_next_monotonic_count() - get next value of the counter
2274 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002275 *
2276 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002277 *
2278 * See the Unified Extensible Firmware Interface (UEFI) specification for
2279 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002280 *
Mario Six8fac2912018-07-10 08:40:17 +02002281 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002282 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002283static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2284{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002285 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002286 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002287
Alexander Grafc15d9212016-03-04 01:09:59 +01002288 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002289 if (!count) {
2290 ret = EFI_INVALID_PARAMETER;
2291 goto out;
2292 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002293 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002294 ret = EFI_SUCCESS;
2295out:
2296 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002297}
2298
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002299/**
Mario Six8fac2912018-07-10 08:40:17 +02002300 * efi_stall() - sleep
2301 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002302 *
Mario Six8fac2912018-07-10 08:40:17 +02002303 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002304 *
Mario Six8fac2912018-07-10 08:40:17 +02002305 * See the Unified Extensible Firmware Interface (UEFI) specification for
2306 * details.
2307 *
2308 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002309 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002310static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2311{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002312 u64 end_tick;
2313
Alexander Grafc15d9212016-03-04 01:09:59 +01002314 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002315
2316 end_tick = get_ticks() + usec_to_tick(microseconds);
2317 while (get_ticks() < end_tick)
2318 efi_timer_check();
2319
Alexander Grafc15d9212016-03-04 01:09:59 +01002320 return EFI_EXIT(EFI_SUCCESS);
2321}
2322
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002323/**
Mario Six8fac2912018-07-10 08:40:17 +02002324 * efi_set_watchdog_timer() - reset the watchdog timer
2325 * @timeout: seconds before reset by watchdog
2326 * @watchdog_code: code to be logged when resetting
2327 * @data_size: size of buffer in bytes
2328 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002329 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002330 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002331 *
Mario Six8fac2912018-07-10 08:40:17 +02002332 * See the Unified Extensible Firmware Interface (UEFI) specification for
2333 * details.
2334 *
2335 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002336 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002337static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2338 uint64_t watchdog_code,
2339 unsigned long data_size,
2340 uint16_t *watchdog_data)
2341{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002342 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002343 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002344 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002345}
2346
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002347/**
Mario Six8fac2912018-07-10 08:40:17 +02002348 * efi_close_protocol() - close a protocol
2349 * @handle: handle on which the protocol shall be closed
2350 * @protocol: GUID of the protocol to close
2351 * @agent_handle: handle of the driver
2352 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002353 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002354 * This is the function implementing the CloseProtocol service is for internal
2355 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002356 *
Mario Six8fac2912018-07-10 08:40:17 +02002357 * See the Unified Extensible Firmware Interface (UEFI) specification for
2358 * details.
2359 *
2360 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002361 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002362efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2363 efi_handle_t agent_handle,
2364 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002365{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002366 struct efi_handler *handler;
2367 struct efi_open_protocol_info_item *item;
2368 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002369 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002370
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002371 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002372 (controller_handle && !efi_search_obj(controller_handle)))
2373 return EFI_INVALID_PARAMETER;
2374 ret = efi_search_protocol(handle, protocol, &handler);
2375 if (ret != EFI_SUCCESS)
2376 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002377
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002378 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002379 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2380 if (item->info.agent_handle == agent_handle &&
2381 item->info.controller_handle == controller_handle) {
2382 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002383 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002384 }
2385 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002386
2387 return ret;
2388}
2389
2390/**
2391 * efi_close_protocol_ext() - close a protocol
2392 * @handle: handle on which the protocol shall be closed
2393 * @protocol: GUID of the protocol to close
2394 * @agent_handle: handle of the driver
2395 * @controller_handle: handle of the controller
2396 *
2397 * This function implements the CloseProtocol service.
2398 *
2399 * See the Unified Extensible Firmware Interface (UEFI) specification for
2400 * details.
2401 *
2402 * Return: status code
2403 */
2404static efi_status_t EFIAPI
2405efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2406 efi_handle_t agent_handle,
2407 efi_handle_t controller_handle)
2408{
2409 efi_status_t ret;
2410
2411 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2412 controller_handle);
2413
2414 ret = efi_close_protocol(handle, protocol,
2415 agent_handle, controller_handle);
2416
2417 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002418}
2419
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002420/**
Mario Six8fac2912018-07-10 08:40:17 +02002421 * efi_open_protocol_information() - provide information about then open status
2422 * of a protocol on a handle
2423 * @handle: handle for which the information shall be retrieved
2424 * @protocol: GUID of the protocol
2425 * @entry_buffer: buffer to receive the open protocol information
2426 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002427 *
2428 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002429 *
2430 * See the Unified Extensible Firmware Interface (UEFI) specification for
2431 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002432 *
Mario Six8fac2912018-07-10 08:40:17 +02002433 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002434 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002435static efi_status_t EFIAPI efi_open_protocol_information(
2436 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002437 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002438 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002439{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002440 unsigned long buffer_size;
2441 unsigned long count;
2442 struct efi_handler *handler;
2443 struct efi_open_protocol_info_item *item;
2444 efi_status_t r;
2445
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002446 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002447 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002448
2449 /* Check parameters */
2450 if (!entry_buffer) {
2451 r = EFI_INVALID_PARAMETER;
2452 goto out;
2453 }
2454 r = efi_search_protocol(handle, protocol, &handler);
2455 if (r != EFI_SUCCESS)
2456 goto out;
2457
2458 /* Count entries */
2459 count = 0;
2460 list_for_each_entry(item, &handler->open_infos, link) {
2461 if (item->info.open_count)
2462 ++count;
2463 }
2464 *entry_count = count;
2465 *entry_buffer = NULL;
2466 if (!count) {
2467 r = EFI_SUCCESS;
2468 goto out;
2469 }
2470
2471 /* Copy entries */
2472 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002473 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002474 (void **)entry_buffer);
2475 if (r != EFI_SUCCESS)
2476 goto out;
2477 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2478 if (item->info.open_count)
2479 (*entry_buffer)[--count] = item->info;
2480 }
2481out:
2482 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002483}
2484
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002485/**
Mario Six8fac2912018-07-10 08:40:17 +02002486 * efi_protocols_per_handle() - get protocols installed on a handle
2487 * @handle: handle for which the information is retrieved
2488 * @protocol_buffer: buffer with protocol GUIDs
2489 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002490 *
2491 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002492 *
Mario Six8fac2912018-07-10 08:40:17 +02002493 * See the Unified Extensible Firmware Interface (UEFI) specification for
2494 * details.
2495 *
2496 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002497 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002498static efi_status_t EFIAPI efi_protocols_per_handle(
2499 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002500 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002501{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002502 unsigned long buffer_size;
2503 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002504 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002505 efi_status_t r;
2506
Alexander Grafc15d9212016-03-04 01:09:59 +01002507 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2508 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002509
2510 if (!handle || !protocol_buffer || !protocol_buffer_count)
2511 return EFI_EXIT(EFI_INVALID_PARAMETER);
2512
2513 *protocol_buffer = NULL;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002514
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002515 efiobj = efi_search_obj(handle);
2516 if (!efiobj)
2517 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002518
Heinrich Schuchardt5f6806d2024-07-31 10:13:04 +02002519 *protocol_buffer_count = list_count_nodes(&efiobj->protocols);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002520
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002521 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002522 if (*protocol_buffer_count) {
2523 size_t j = 0;
2524
2525 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002526 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002527 (void **)protocol_buffer);
2528 if (r != EFI_SUCCESS)
2529 return EFI_EXIT(r);
2530 list_for_each(protocol_handle, &efiobj->protocols) {
2531 struct efi_handler *protocol;
2532
2533 protocol = list_entry(protocol_handle,
2534 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002535 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002536 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002537 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002538 }
2539
2540 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002541}
2542
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002543efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2544 const efi_guid_t *protocol, void *search_key,
2545 efi_uintn_t *no_handles, efi_handle_t **buffer)
2546{
2547 efi_status_t r;
2548 efi_uintn_t buffer_size = 0;
2549
2550 if (!no_handles || !buffer) {
2551 r = EFI_INVALID_PARAMETER;
2552 goto out;
2553 }
2554 *no_handles = 0;
2555 *buffer = NULL;
2556 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2557 *buffer);
2558 if (r != EFI_BUFFER_TOO_SMALL)
2559 goto out;
2560 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2561 (void **)buffer);
2562 if (r != EFI_SUCCESS)
2563 goto out;
2564 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2565 *buffer);
2566 if (r == EFI_SUCCESS)
2567 *no_handles = buffer_size / sizeof(efi_handle_t);
2568out:
2569 return r;
2570}
2571
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002572/**
Mario Six8fac2912018-07-10 08:40:17 +02002573 * efi_locate_handle_buffer() - locate handles implementing a protocol
2574 * @search_type: selection criterion
2575 * @protocol: GUID of the protocol
2576 * @search_key: registration key
2577 * @no_handles: number of returned handles
2578 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002579 *
2580 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002581 *
Mario Six8fac2912018-07-10 08:40:17 +02002582 * See the Unified Extensible Firmware Interface (UEFI) specification for
2583 * details.
2584 *
2585 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002586 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002587efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002588 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002589 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002590 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002591{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002592 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002593
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002594 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002595 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002596
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002597 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2598 no_handles, buffer);
2599
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002600 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002601}
2602
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002603/**
Mario Six8fac2912018-07-10 08:40:17 +02002604 * efi_locate_protocol() - find an interface implementing a protocol
2605 * @protocol: GUID of the protocol
2606 * @registration: registration key passed to the notification function
2607 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002608 *
2609 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002610 *
2611 * See the Unified Extensible Firmware Interface (UEFI) specification for
2612 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002613 *
Mario Six8fac2912018-07-10 08:40:17 +02002614 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002615 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002616static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002617 void *registration,
2618 void **protocol_interface)
2619{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002620 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002621 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002622 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002623
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002624 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002625
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002626 /*
2627 * The UEFI spec explicitly requires a protocol even if a registration
2628 * key is provided. This differs from the logic in LocateHandle().
2629 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002630 if (!protocol || !protocol_interface)
2631 return EFI_EXIT(EFI_INVALID_PARAMETER);
2632
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002633 if (registration) {
2634 struct efi_register_notify_event *event;
2635 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002636
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002637 event = efi_check_register_notify_event(registration);
2638 if (!event)
2639 return EFI_EXIT(EFI_INVALID_PARAMETER);
2640 /*
2641 * The UEFI spec requires to return EFI_NOT_FOUND if no
2642 * protocol instance matches protocol and registration.
2643 * So let's do the same for a mismatch between protocol and
2644 * registration.
2645 */
2646 if (guidcmp(&event->protocol, protocol))
2647 goto not_found;
2648 if (list_empty(&event->handles))
2649 goto not_found;
2650 handle = list_first_entry(&event->handles,
2651 struct efi_protocol_notification,
2652 link);
2653 efiobj = handle->handle;
2654 list_del(&handle->link);
2655 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002656 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002657 if (ret == EFI_SUCCESS)
2658 goto found;
2659 } else {
2660 list_for_each_entry(efiobj, &efi_obj_list, link) {
2661 ret = efi_search_protocol(efiobj, protocol, &handler);
2662 if (ret == EFI_SUCCESS)
2663 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002664 }
2665 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002666not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002667 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002668 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002669found:
2670 *protocol_interface = handler->protocol_interface;
2671 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002672}
2673
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002674/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002675 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002676 * interfaces
2677 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002678 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002679 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002680 * Core functionality of efi_install_multiple_protocol_interfaces
2681 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002682 *
Mario Six8fac2912018-07-10 08:40:17 +02002683 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002684 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002685static efi_status_t EFIAPI
2686efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2687 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002688{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002689 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002690 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002691 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002692 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002693 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002694 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002695
2696 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002697 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002698
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002699 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002700 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002701 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002702 if (!protocol)
2703 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002704 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002705 /* Check that a device path has not been installed before */
2706 if (!guidcmp(protocol, &efi_guid_device_path)) {
2707 struct efi_device_path *dp = protocol_interface;
2708
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002709 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2710 &old_handle));
2711 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002712 dp->type == DEVICE_PATH_TYPE_END) {
2713 EFI_PRINT("Path %pD already installed\n",
2714 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002715 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002716 break;
2717 }
2718 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002719 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2720 EFI_NATIVE_INTERFACE,
2721 protocol_interface));
2722 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002723 break;
2724 i++;
2725 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002726 if (ret == EFI_SUCCESS)
2727 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002728
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002729 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002730 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002731 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2732 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002733 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002734 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002735 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002736
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002737out:
2738 efi_va_end(argptr_copy);
2739 return ret;
2740
Alexander Grafc15d9212016-03-04 01:09:59 +01002741}
2742
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002743/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002744 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2745 * interfaces
2746 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002747 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2748 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002749 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002750 *
2751 * This is the function for internal usage in U-Boot. For the API function
2752 * implementing the InstallMultipleProtocol service see
2753 * efi_install_multiple_protocol_interfaces_ext()
2754 *
2755 * Return: status code
2756 */
2757efi_status_t EFIAPI
2758efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2759{
2760 efi_status_t ret;
2761 efi_va_list argptr;
2762
2763 efi_va_start(argptr, handle);
2764 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2765 efi_va_end(argptr);
2766 return ret;
2767}
2768
2769/**
2770 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2771 * interfaces
2772 * @handle: handle on which the protocol interfaces shall be installed
2773 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2774 * interfaces
2775 *
2776 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002777 *
Mario Six8fac2912018-07-10 08:40:17 +02002778 * See the Unified Extensible Firmware Interface (UEFI) specification for
2779 * details.
2780 *
2781 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002782 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002783static efi_status_t EFIAPI
2784efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002785{
2786 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002787 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002788 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002789
2790 efi_va_start(argptr, handle);
2791 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2792 efi_va_end(argptr);
2793 return EFI_EXIT(ret);
2794}
2795
2796/**
2797 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2798 * multiple protocol
2799 * interfaces
2800 * @handle: handle from which the protocol interfaces shall be removed
2801 * @argptr: va_list of args
2802 *
2803 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2804 * Must not be called directly
2805 *
2806 * Return: status code
2807 */
2808static efi_status_t EFIAPI
2809efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2810 efi_va_list argptr)
2811{
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002812 const efi_guid_t *protocol, *next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002813 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002814 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002815 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002816 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002817
2818 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002819 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002820
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002821 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002822 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002823 for (;;) {
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002824 /*
2825 * If efi_uninstall_protocol() fails we need to be able to
2826 * reinstall the previously uninstalled protocols on the same
2827 * handle.
2828 * Instead of calling efi_uninstall_protocol(...,..., false)
2829 * and potentially removing the handle, only allow the handle
2830 * removal on the last protocol that we requested to uninstall.
2831 * That way we can preserve the handle in case the latter fails
2832 */
2833 bool preserve = true;
2834
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002835 if (!protocol)
2836 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002837 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002838 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2839 if (!next_protocol)
2840 preserve = false;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002841 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002842 protocol_interface, preserve);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002843 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002844 break;
2845 i++;
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002846 protocol = next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002847 }
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002848 if (ret == EFI_SUCCESS)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002849 goto out;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002850
2851 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002852 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002853 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2854 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002855 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2856 EFI_NATIVE_INTERFACE,
2857 protocol_interface));
2858 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002859 /*
2860 * If any errors are generated while the protocol interfaces are being
2861 * uninstalled, then the protocols uninstalled prior to the error will
2862 * be reinstalled using InstallProtocolInterface() and the status code
2863 * EFI_INVALID_PARAMETER is returned.
2864 */
2865 ret = EFI_INVALID_PARAMETER;
2866
2867out:
2868 efi_va_end(argptr_copy);
2869 return ret;
2870}
2871
2872/**
2873 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2874 * interfaces
2875 * @handle: handle from which the protocol interfaces shall be removed
2876 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2877 * interfaces
2878 *
2879 * This function implements the UninstallMultipleProtocolInterfaces service.
2880 *
2881 * This is the function for internal usage in U-Boot. For the API function
2882 * implementing the UninstallMultipleProtocolInterfaces service see
2883 * efi_uninstall_multiple_protocol_interfaces_ext()
2884 *
2885 * Return: status code
2886 */
2887efi_status_t EFIAPI
2888efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2889{
2890 efi_status_t ret;
2891 efi_va_list argptr;
2892
2893 efi_va_start(argptr, handle);
2894 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002895 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002896 return ret;
2897}
2898
2899/**
2900 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2901 * interfaces
2902 * @handle: handle from which the protocol interfaces shall be removed
2903 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2904 * interfaces
2905 *
2906 * This function implements the UninstallMultipleProtocolInterfaces service.
2907 *
2908 * See the Unified Extensible Firmware Interface (UEFI) specification for
2909 * details.
2910 *
2911 * Return: status code
2912 */
2913static efi_status_t EFIAPI
2914efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2915{
2916 EFI_ENTRY("%p", handle);
2917 efi_status_t ret;
2918 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002919
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002920 efi_va_start(argptr, handle);
2921 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2922 efi_va_end(argptr);
2923 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002924}
2925
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002926/**
Mario Six8fac2912018-07-10 08:40:17 +02002927 * efi_calculate_crc32() - calculate cyclic redundancy code
2928 * @data: buffer with data
2929 * @data_size: size of buffer in bytes
2930 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002931 *
2932 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002933 *
2934 * See the Unified Extensible Firmware Interface (UEFI) specification for
2935 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002936 *
Mario Six8fac2912018-07-10 08:40:17 +02002937 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002938 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002939static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2940 efi_uintn_t data_size,
2941 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002942{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002943 efi_status_t ret = EFI_SUCCESS;
2944
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002945 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002946 if (!data || !data_size || !crc32_p) {
2947 ret = EFI_INVALID_PARAMETER;
2948 goto out;
2949 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002950 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002951out:
2952 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002953}
2954
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002955/**
Mario Six8fac2912018-07-10 08:40:17 +02002956 * efi_copy_mem() - copy memory
2957 * @destination: destination of the copy operation
2958 * @source: source of the copy operation
2959 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002960 *
2961 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002962 *
Mario Six8fac2912018-07-10 08:40:17 +02002963 * See the Unified Extensible Firmware Interface (UEFI) specification for
2964 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002965 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002966static void EFIAPI efi_copy_mem(void *destination, const void *source,
2967 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002968{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002969 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002970 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002971 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002972}
2973
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002974/**
Mario Six8fac2912018-07-10 08:40:17 +02002975 * efi_set_mem() - Fill memory with a byte value.
2976 * @buffer: buffer to fill
2977 * @size: size of buffer in bytes
2978 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002979 *
2980 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002981 *
Mario Six8fac2912018-07-10 08:40:17 +02002982 * See the Unified Extensible Firmware Interface (UEFI) specification for
2983 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002984 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002985static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002986{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002987 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002988 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002989 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002990}
2991
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002992/**
Mario Six8fac2912018-07-10 08:40:17 +02002993 * efi_protocol_open() - open protocol interface on a handle
2994 * @handler: handler of a protocol
2995 * @protocol_interface: interface implementing the protocol
2996 * @agent_handle: handle of the driver
2997 * @controller_handle: handle of the controller
2998 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002999 *
Mario Six8fac2912018-07-10 08:40:17 +02003000 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003001 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01003002efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003003 struct efi_handler *handler,
3004 void **protocol_interface, void *agent_handle,
3005 void *controller_handle, uint32_t attributes)
3006{
3007 struct efi_open_protocol_info_item *item;
3008 struct efi_open_protocol_info_entry *match = NULL;
3009 bool opened_by_driver = false;
3010 bool opened_exclusive = false;
3011
3012 /* If there is no agent, only return the interface */
3013 if (!agent_handle)
3014 goto out;
3015
3016 /* For TEST_PROTOCOL ignore interface attribute */
3017 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3018 *protocol_interface = NULL;
3019
3020 /*
3021 * Check if the protocol is already opened by a driver with the same
3022 * attributes or opened exclusively
3023 */
3024 list_for_each_entry(item, &handler->open_infos, link) {
3025 if (item->info.agent_handle == agent_handle) {
3026 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3027 (item->info.attributes == attributes))
3028 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003029 } else {
3030 if (item->info.attributes &
3031 EFI_OPEN_PROTOCOL_BY_DRIVER)
3032 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003033 }
3034 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3035 opened_exclusive = true;
3036 }
3037
3038 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003039 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3040 if (opened_exclusive)
3041 return EFI_ACCESS_DENIED;
3042 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3043 if (opened_exclusive || opened_by_driver)
3044 return EFI_ACCESS_DENIED;
3045 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003046
3047 /* Prepare exclusive opening */
3048 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3049 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003050disconnect_next:
3051 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003052 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003053 efi_status_t ret;
3054
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003055 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003056 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3057 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003058 item->info.controller_handle,
3059 item->info.agent_handle,
3060 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003061 if (ret == EFI_SUCCESS)
3062 /*
3063 * Child controllers may have been
3064 * removed from the open_infos list. So
3065 * let's restart the loop.
3066 */
3067 goto disconnect_next;
3068 else
3069 opened_by_driver = true;
3070 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003071 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003072 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003073 if (opened_by_driver)
3074 return EFI_ACCESS_DENIED;
3075 }
3076
3077 /* Find existing entry */
3078 list_for_each_entry(item, &handler->open_infos, link) {
3079 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003080 item->info.controller_handle == controller_handle &&
3081 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003082 match = &item->info;
3083 }
3084 /* None found, create one */
3085 if (!match) {
3086 match = efi_create_open_info(handler);
3087 if (!match)
3088 return EFI_OUT_OF_RESOURCES;
3089 }
3090
3091 match->agent_handle = agent_handle;
3092 match->controller_handle = controller_handle;
3093 match->attributes = attributes;
3094 match->open_count++;
3095
3096out:
3097 /* For TEST_PROTOCOL ignore interface attribute. */
3098 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3099 *protocol_interface = handler->protocol_interface;
3100
3101 return EFI_SUCCESS;
3102}
3103
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003104/**
Mario Six8fac2912018-07-10 08:40:17 +02003105 * efi_open_protocol() - open protocol interface on a handle
3106 * @handle: handle on which the protocol shall be opened
3107 * @protocol: GUID of the protocol
3108 * @protocol_interface: interface implementing the protocol
3109 * @agent_handle: handle of the driver
3110 * @controller_handle: handle of the controller
3111 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003112 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003113 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003114 *
Mario Six8fac2912018-07-10 08:40:17 +02003115 * See the Unified Extensible Firmware Interface (UEFI) specification for
3116 * details.
3117 *
3118 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003119 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003120static efi_status_t EFIAPI efi_open_protocol
3121 (efi_handle_t handle, const efi_guid_t *protocol,
3122 void **protocol_interface, efi_handle_t agent_handle,
3123 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003124{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003125 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003126 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003127
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003128 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003129 protocol_interface, agent_handle, controller_handle,
3130 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003131
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003132 if (!handle || !protocol ||
3133 (!protocol_interface && attributes !=
3134 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003135 goto out;
3136 }
3137
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003138 switch (attributes) {
3139 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3140 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3141 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3142 break;
3143 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3144 if (controller_handle == handle)
3145 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003146 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003147 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3148 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003149 /* Check that the controller handle is valid */
3150 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003151 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003152 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003153 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003154 /* Check that the agent handle is valid */
3155 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003156 goto out;
3157 break;
3158 default:
3159 goto out;
3160 }
3161
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003162 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003163 switch (r) {
3164 case EFI_SUCCESS:
3165 break;
3166 case EFI_NOT_FOUND:
3167 r = EFI_UNSUPPORTED;
3168 goto out;
3169 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003170 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003171 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003172
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003173 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3174 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003175out:
3176 return EFI_EXIT(r);
3177}
3178
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003179/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003180 * efi_start_image() - call the entry point of an image
3181 * @image_handle: handle of the image
3182 * @exit_data_size: size of the buffer
3183 * @exit_data: buffer to receive the exit data of the called image
3184 *
3185 * This function implements the StartImage service.
3186 *
3187 * See the Unified Extensible Firmware Interface (UEFI) specification for
3188 * details.
3189 *
3190 * Return: status code
3191 */
3192efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3193 efi_uintn_t *exit_data_size,
3194 u16 **exit_data)
3195{
3196 struct efi_loaded_image_obj *image_obj =
3197 (struct efi_loaded_image_obj *)image_handle;
3198 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003199 void *info;
3200 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003201 efi_status_t exit_status;
3202 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003203
3204 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3205
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003206 if (!efi_search_obj(image_handle))
3207 return EFI_EXIT(EFI_INVALID_PARAMETER);
3208
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003209 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003210 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3211 return EFI_EXIT(EFI_INVALID_PARAMETER);
3212
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003213 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3214 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3215
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003216 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3217 &info, NULL, NULL,
3218 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3219 if (ret != EFI_SUCCESS)
3220 return EFI_EXIT(EFI_INVALID_PARAMETER);
3221
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003222 image_obj->exit_data_size = exit_data_size;
3223 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003224 image_obj->exit_status = &exit_status;
3225 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003226
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003227 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3228 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003229 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003230 if (ret == EFI_SECURITY_VIOLATION) {
3231 /*
3232 * TCG2 Protocol is installed but no TPM device found,
3233 * this is not expected.
3234 */
3235 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003236 }
3237 }
3238 }
3239
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003240 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003241 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003242 /*
3243 * We called the entry point of the child image with EFI_CALL
3244 * in the lines below. The child image called the Exit() boot
3245 * service efi_exit() which executed the long jump that brought
3246 * us to the current line. This implies that the second half
3247 * of the EFI_CALL macro has not been executed.
3248 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003249#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003250 /*
3251 * efi_exit() called efi_restore_gd(). We have to undo this
3252 * otherwise __efi_entry_check() will put the wrong value into
3253 * app_gd.
3254 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003255 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003256#endif
3257 /*
3258 * To get ready to call EFI_EXIT below we have to execute the
3259 * missed out steps of EFI_CALL.
3260 */
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003261 EFI_RETURN(exit_status);
3262
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003263 current_image = parent_image;
Heinrich Schuchardt0ad71bc2025-01-17 01:09:51 +01003264
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003265 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003266 }
3267
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003268 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003269 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003270 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003271 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3272
3273 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003274 * Control is returned from a started UEFI image either by calling
3275 * Exit() (where exit data can be provided) or by simply returning from
3276 * the entry point. In the latter case call Exit() on behalf of the
3277 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003278 */
3279 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3280}
3281
3282/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003283 * efi_delete_image() - delete loaded image from memory)
3284 *
3285 * @image_obj: handle of the loaded image
3286 * @loaded_image_protocol: loaded image protocol
3287 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003288static efi_status_t efi_delete_image
3289 (struct efi_loaded_image_obj *image_obj,
3290 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003291{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003292 struct efi_object *efiobj;
3293 efi_status_t r, ret = EFI_SUCCESS;
3294
3295close_next:
3296 list_for_each_entry(efiobj, &efi_obj_list, link) {
3297 struct efi_handler *protocol;
3298
3299 list_for_each_entry(protocol, &efiobj->protocols, link) {
3300 struct efi_open_protocol_info_item *info;
3301
3302 list_for_each_entry(info, &protocol->open_infos, link) {
3303 if (info->info.agent_handle !=
3304 (efi_handle_t)image_obj)
3305 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003306 r = efi_close_protocol(
3307 efiobj, &protocol->guid,
3308 info->info.agent_handle,
3309 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003310 if (r != EFI_SUCCESS)
3311 ret = r;
3312 /*
3313 * Closing protocols may results in further
3314 * items being deleted. To play it safe loop
3315 * over all elements again.
3316 */
3317 goto close_next;
3318 }
3319 }
3320 }
3321
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003322 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3323 efi_size_in_pages(loaded_image_protocol->image_size));
3324 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003325
3326 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003327}
3328
3329/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003330 * efi_unload_image() - unload an EFI image
3331 * @image_handle: handle of the image to be unloaded
3332 *
3333 * This function implements the UnloadImage service.
3334 *
3335 * See the Unified Extensible Firmware Interface (UEFI) specification for
3336 * details.
3337 *
3338 * Return: status code
3339 */
3340efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3341{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003342 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003343 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003344 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003345
3346 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003347
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003348 efiobj = efi_search_obj(image_handle);
3349 if (!efiobj) {
3350 ret = EFI_INVALID_PARAMETER;
3351 goto out;
3352 }
3353 /* Find the loaded image protocol */
3354 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3355 (void **)&loaded_image_protocol,
3356 NULL, NULL,
3357 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3358 if (ret != EFI_SUCCESS) {
3359 ret = EFI_INVALID_PARAMETER;
3360 goto out;
3361 }
3362 switch (efiobj->type) {
3363 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3364 /* Call the unload function */
3365 if (!loaded_image_protocol->unload) {
3366 ret = EFI_UNSUPPORTED;
3367 goto out;
3368 }
3369 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3370 if (ret != EFI_SUCCESS)
3371 goto out;
3372 break;
3373 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3374 break;
3375 default:
3376 ret = EFI_INVALID_PARAMETER;
3377 goto out;
3378 }
3379 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3380 loaded_image_protocol);
3381out:
3382 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003383}
3384
3385/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003386 * efi_update_exit_data() - fill exit data parameters of StartImage()
3387 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003388 * @image_obj: image handle
3389 * @exit_data_size: size of the exit data buffer
3390 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003391 * Return: status code
3392 */
3393static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3394 efi_uintn_t exit_data_size,
3395 u16 *exit_data)
3396{
3397 efi_status_t ret;
3398
3399 /*
3400 * If exit_data is not provided to StartImage(), exit_data_size must be
3401 * ignored.
3402 */
3403 if (!image_obj->exit_data)
3404 return EFI_SUCCESS;
3405 if (image_obj->exit_data_size)
3406 *image_obj->exit_data_size = exit_data_size;
3407 if (exit_data_size && exit_data) {
3408 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3409 exit_data_size,
3410 (void **)image_obj->exit_data);
3411 if (ret != EFI_SUCCESS)
3412 return ret;
3413 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3414 } else {
3415 image_obj->exit_data = NULL;
3416 }
3417 return EFI_SUCCESS;
3418}
3419
3420/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003421 * efi_exit() - leave an EFI application or driver
3422 * @image_handle: handle of the application or driver that is exiting
3423 * @exit_status: status code
3424 * @exit_data_size: size of the buffer in bytes
3425 * @exit_data: buffer with data describing an error
3426 *
3427 * This function implements the Exit service.
3428 *
3429 * See the Unified Extensible Firmware Interface (UEFI) specification for
3430 * details.
3431 *
3432 * Return: status code
3433 */
3434static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3435 efi_status_t exit_status,
3436 efi_uintn_t exit_data_size,
3437 u16 *exit_data)
3438{
3439 /*
3440 * TODO: We should call the unload procedure of the loaded
3441 * image protocol.
3442 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003443 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003444 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003445 struct efi_loaded_image_obj *image_obj =
3446 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003447 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003448
3449 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3450 exit_data_size, exit_data);
3451
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003452 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003453 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003454 (void **)&loaded_image_protocol,
3455 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003456 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003457 if (ret != EFI_SUCCESS) {
3458 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003459 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003460 }
3461
3462 /* Unloading of unstarted images */
3463 switch (image_obj->header.type) {
3464 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3465 break;
3466 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3467 efi_delete_image(image_obj, loaded_image_protocol);
3468 ret = EFI_SUCCESS;
3469 goto out;
3470 default:
3471 /* Handle does not refer to loaded image */
3472 ret = EFI_INVALID_PARAMETER;
3473 goto out;
3474 }
3475 /* A started image can only be unloaded it is the last one started. */
3476 if (image_handle != current_image) {
3477 ret = EFI_INVALID_PARAMETER;
3478 goto out;
3479 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003480
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003481 /* Exit data is only foreseen in case of failure. */
3482 if (exit_status != EFI_SUCCESS) {
3483 ret = efi_update_exit_data(image_obj, exit_data_size,
3484 exit_data);
3485 /* Exiting has priority. Don't return error to caller. */
3486 if (ret != EFI_SUCCESS)
3487 EFI_PRINT("%s: out of memory\n", __func__);
3488 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003489 /* efi_delete_image() frees image_obj. Copy before the call. */
3490 exit_jmp = image_obj->exit_jmp;
3491 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003492 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3493 exit_status != EFI_SUCCESS)
3494 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003495
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003496 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3497 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3498 ret = efi_tcg2_measure_efi_app_exit();
Heinrich Schuchardt265bfab2024-11-27 00:40:17 +01003499 if (ret != EFI_SUCCESS)
3500 log_debug("tcg2 measurement fails (0x%lx)\n",
3501 ret);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003502 }
3503 }
3504
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003505 /* Make sure entry/exit counts for EFI world cross-overs match */
3506 EFI_EXIT(exit_status);
3507
3508 /*
3509 * But longjmp out with the U-Boot gd, not the application's, as
3510 * the other end is a setjmp call inside EFI context.
3511 */
3512 efi_restore_gd();
3513
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003514 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003515
3516 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003517out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003518 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003519}
3520
3521/**
Mario Six8fac2912018-07-10 08:40:17 +02003522 * efi_handle_protocol() - get interface of a protocol on a handle
3523 * @handle: handle on which the protocol shall be opened
3524 * @protocol: GUID of the protocol
3525 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003526 *
3527 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003528 *
3529 * See the Unified Extensible Firmware Interface (UEFI) specification for
3530 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003531 *
Mario Six8fac2912018-07-10 08:40:17 +02003532 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003533 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003534efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3535 const efi_guid_t *protocol,
3536 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003537{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003538 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003539 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003540}
3541
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003542/**
Mario Six8fac2912018-07-10 08:40:17 +02003543 * efi_bind_controller() - bind a single driver to a controller
3544 * @controller_handle: controller handle
3545 * @driver_image_handle: driver handle
3546 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003547 *
Mario Six8fac2912018-07-10 08:40:17 +02003548 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003549 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003550static efi_status_t efi_bind_controller(
3551 efi_handle_t controller_handle,
3552 efi_handle_t driver_image_handle,
3553 struct efi_device_path *remain_device_path)
3554{
3555 struct efi_driver_binding_protocol *binding_protocol;
3556 efi_status_t r;
3557
3558 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3559 &efi_guid_driver_binding_protocol,
3560 (void **)&binding_protocol,
3561 driver_image_handle, NULL,
3562 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3563 if (r != EFI_SUCCESS)
3564 return r;
3565 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3566 controller_handle,
3567 remain_device_path));
3568 if (r == EFI_SUCCESS)
3569 r = EFI_CALL(binding_protocol->start(binding_protocol,
3570 controller_handle,
3571 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003572 efi_close_protocol(driver_image_handle,
3573 &efi_guid_driver_binding_protocol,
3574 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003575 return r;
3576}
3577
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003578/**
Mario Six8fac2912018-07-10 08:40:17 +02003579 * efi_connect_single_controller() - connect a single driver to a controller
3580 * @controller_handle: controller
3581 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003582 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003583 *
Mario Six8fac2912018-07-10 08:40:17 +02003584 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003585 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003586static efi_status_t efi_connect_single_controller(
3587 efi_handle_t controller_handle,
3588 efi_handle_t *driver_image_handle,
3589 struct efi_device_path *remain_device_path)
3590{
3591 efi_handle_t *buffer;
3592 size_t count;
3593 size_t i;
3594 efi_status_t r;
3595 size_t connected = 0;
3596
3597 /* Get buffer with all handles with driver binding protocol */
3598 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3599 &efi_guid_driver_binding_protocol,
3600 NULL, &count, &buffer));
3601 if (r != EFI_SUCCESS)
3602 return r;
3603
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003604 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003605 if (driver_image_handle) {
3606 for (; *driver_image_handle; ++driver_image_handle) {
3607 for (i = 0; i < count; ++i) {
3608 if (buffer[i] == *driver_image_handle) {
3609 buffer[i] = NULL;
3610 r = efi_bind_controller(
3611 controller_handle,
3612 *driver_image_handle,
3613 remain_device_path);
3614 /*
3615 * For drivers that do not support the
3616 * controller or are already connected
3617 * we receive an error code here.
3618 */
3619 if (r == EFI_SUCCESS)
3620 ++connected;
3621 }
3622 }
3623 }
3624 }
3625
3626 /*
3627 * TODO: Some overrides are not yet implemented:
3628 * - Platform Driver Override
3629 * - Driver Family Override Search
3630 * - Bus Specific Driver Override
3631 */
3632
3633 /* Driver Binding Search */
3634 for (i = 0; i < count; ++i) {
3635 if (buffer[i]) {
3636 r = efi_bind_controller(controller_handle,
3637 buffer[i],
3638 remain_device_path);
3639 if (r == EFI_SUCCESS)
3640 ++connected;
3641 }
3642 }
3643
3644 efi_free_pool(buffer);
3645 if (!connected)
3646 return EFI_NOT_FOUND;
3647 return EFI_SUCCESS;
3648}
3649
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003650/**
Mario Six8fac2912018-07-10 08:40:17 +02003651 * efi_connect_controller() - connect a controller to a driver
3652 * @controller_handle: handle of the controller
3653 * @driver_image_handle: handle of the driver
3654 * @remain_device_path: device path of a child controller
3655 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003656 *
3657 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003658 *
3659 * See the Unified Extensible Firmware Interface (UEFI) specification for
3660 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003661 *
3662 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003663 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003664 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3665 *
Mario Six8fac2912018-07-10 08:40:17 +02003666 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003667 */
3668static efi_status_t EFIAPI efi_connect_controller(
3669 efi_handle_t controller_handle,
3670 efi_handle_t *driver_image_handle,
3671 struct efi_device_path *remain_device_path,
3672 bool recursive)
3673{
3674 efi_status_t r;
3675 efi_status_t ret = EFI_NOT_FOUND;
3676 struct efi_object *efiobj;
3677
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003678 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003679 remain_device_path, recursive);
3680
3681 efiobj = efi_search_obj(controller_handle);
3682 if (!efiobj) {
3683 ret = EFI_INVALID_PARAMETER;
3684 goto out;
3685 }
3686
3687 r = efi_connect_single_controller(controller_handle,
3688 driver_image_handle,
3689 remain_device_path);
3690 if (r == EFI_SUCCESS)
3691 ret = EFI_SUCCESS;
3692 if (recursive) {
3693 struct efi_handler *handler;
3694 struct efi_open_protocol_info_item *item;
3695
3696 list_for_each_entry(handler, &efiobj->protocols, link) {
3697 list_for_each_entry(item, &handler->open_infos, link) {
3698 if (item->info.attributes &
3699 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3700 r = EFI_CALL(efi_connect_controller(
3701 item->info.controller_handle,
3702 driver_image_handle,
3703 remain_device_path,
3704 recursive));
3705 if (r == EFI_SUCCESS)
3706 ret = EFI_SUCCESS;
3707 }
3708 }
3709 }
3710 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003711 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003712 if (ret != EFI_SUCCESS && remain_device_path &&
3713 remain_device_path->type == DEVICE_PATH_TYPE_END)
3714 ret = EFI_SUCCESS;
3715out:
3716 return EFI_EXIT(ret);
3717}
3718
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003719/**
Mario Six8fac2912018-07-10 08:40:17 +02003720 * efi_reinstall_protocol_interface() - reinstall protocol interface
3721 * @handle: handle on which the protocol shall be reinstalled
3722 * @protocol: GUID of the protocol to be installed
3723 * @old_interface: interface to be removed
3724 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003725 *
3726 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003727 *
3728 * See the Unified Extensible Firmware Interface (UEFI) specification for
3729 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003730 *
3731 * The old interface is uninstalled. The new interface is installed.
3732 * Drivers are connected.
3733 *
Mario Six8fac2912018-07-10 08:40:17 +02003734 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003735 */
Adriano Cordova3d4b09a2024-12-06 14:18:34 -03003736efi_status_t EFIAPI efi_reinstall_protocol_interface(
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003737 efi_handle_t handle, const efi_guid_t *protocol,
3738 void *old_interface, void *new_interface)
3739{
3740 efi_status_t ret;
3741
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003742 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003743 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003744
3745 /* Uninstall protocol but do not delete handle */
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03003746 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003747 if (ret != EFI_SUCCESS)
3748 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003749
3750 /* Install the new protocol */
3751 ret = efi_add_protocol(handle, protocol, new_interface);
3752 /*
3753 * The UEFI spec does not specify what should happen to the handle
3754 * if in case of an error no protocol interface remains on the handle.
3755 * So let's do nothing here.
3756 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003757 if (ret != EFI_SUCCESS)
3758 goto out;
3759 /*
3760 * The returned status code has to be ignored.
3761 * Do not create an error if no suitable driver for the handle exists.
3762 */
3763 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3764out:
3765 return EFI_EXIT(ret);
3766}
3767
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003768/**
Mario Six8fac2912018-07-10 08:40:17 +02003769 * efi_get_child_controllers() - get all child controllers associated to a driver
3770 * @efiobj: handle of the controller
3771 * @driver_handle: handle of the driver
3772 * @number_of_children: number of child controllers
3773 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003774 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003775 * The allocated buffer has to be freed with free().
3776 *
Mario Six8fac2912018-07-10 08:40:17 +02003777 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003778 */
3779static efi_status_t efi_get_child_controllers(
3780 struct efi_object *efiobj,
3781 efi_handle_t driver_handle,
3782 efi_uintn_t *number_of_children,
3783 efi_handle_t **child_handle_buffer)
3784{
3785 struct efi_handler *handler;
3786 struct efi_open_protocol_info_item *item;
3787 efi_uintn_t count = 0, i;
3788 bool duplicate;
3789
3790 /* Count all child controller associations */
3791 list_for_each_entry(handler, &efiobj->protocols, link) {
3792 list_for_each_entry(item, &handler->open_infos, link) {
3793 if (item->info.agent_handle == driver_handle &&
3794 item->info.attributes &
3795 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3796 ++count;
3797 }
3798 }
3799 /*
3800 * Create buffer. In case of duplicate child controller assignments
3801 * the buffer will be too large. But that does not harm.
3802 */
3803 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003804 if (!count)
3805 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003806 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3807 if (!*child_handle_buffer)
3808 return EFI_OUT_OF_RESOURCES;
3809 /* Copy unique child handles */
3810 list_for_each_entry(handler, &efiobj->protocols, link) {
3811 list_for_each_entry(item, &handler->open_infos, link) {
3812 if (item->info.agent_handle == driver_handle &&
3813 item->info.attributes &
3814 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3815 /* Check this is a new child controller */
3816 duplicate = false;
3817 for (i = 0; i < *number_of_children; ++i) {
3818 if ((*child_handle_buffer)[i] ==
3819 item->info.controller_handle)
3820 duplicate = true;
3821 }
3822 /* Copy handle to buffer */
3823 if (!duplicate) {
3824 i = (*number_of_children)++;
3825 (*child_handle_buffer)[i] =
3826 item->info.controller_handle;
3827 }
3828 }
3829 }
3830 }
3831 return EFI_SUCCESS;
3832}
3833
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003834/**
Mario Six8fac2912018-07-10 08:40:17 +02003835 * efi_disconnect_controller() - disconnect a controller from a driver
3836 * @controller_handle: handle of the controller
3837 * @driver_image_handle: handle of the driver
3838 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003839 *
3840 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003841 *
3842 * See the Unified Extensible Firmware Interface (UEFI) specification for
3843 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003844 *
Mario Six8fac2912018-07-10 08:40:17 +02003845 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003846 */
3847static efi_status_t EFIAPI efi_disconnect_controller(
3848 efi_handle_t controller_handle,
3849 efi_handle_t driver_image_handle,
3850 efi_handle_t child_handle)
3851{
3852 struct efi_driver_binding_protocol *binding_protocol;
3853 efi_handle_t *child_handle_buffer = NULL;
3854 size_t number_of_children = 0;
3855 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003856 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003857 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003858
3859 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3860 child_handle);
3861
3862 efiobj = efi_search_obj(controller_handle);
3863 if (!efiobj) {
3864 r = EFI_INVALID_PARAMETER;
3865 goto out;
3866 }
3867
3868 if (child_handle && !efi_search_obj(child_handle)) {
3869 r = EFI_INVALID_PARAMETER;
3870 goto out;
3871 }
3872
3873 /* If no driver handle is supplied, disconnect all drivers */
3874 if (!driver_image_handle) {
3875 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3876 goto out;
3877 }
3878
3879 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003880 r = efi_get_child_controllers(efiobj,
3881 driver_image_handle,
3882 &number_of_children,
3883 &child_handle_buffer);
3884 if (r != EFI_SUCCESS)
3885 return r;
3886 sole_child = (number_of_children == 1);
3887
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003888 if (child_handle) {
3889 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003890 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003891 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003892 }
3893
3894 /* Get the driver binding protocol */
3895 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3896 &efi_guid_driver_binding_protocol,
3897 (void **)&binding_protocol,
3898 driver_image_handle, NULL,
3899 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003900 if (r != EFI_SUCCESS) {
3901 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003902 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003903 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003904 /* Remove the children */
3905 if (number_of_children) {
3906 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3907 controller_handle,
3908 number_of_children,
3909 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003910 if (r != EFI_SUCCESS) {
3911 r = EFI_DEVICE_ERROR;
3912 goto out;
3913 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003914 }
3915 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003916 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003917 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3918 controller_handle,
3919 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003920 if (r != EFI_SUCCESS) {
3921 r = EFI_DEVICE_ERROR;
3922 goto out;
3923 }
3924 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003925 efi_close_protocol(driver_image_handle,
3926 &efi_guid_driver_binding_protocol,
3927 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003928 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003929out:
3930 if (!child_handle)
3931 free(child_handle_buffer);
3932 return EFI_EXIT(r);
3933}
3934
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003935static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003936 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003937 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3938 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003939 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003940 },
3941 .raise_tpl = efi_raise_tpl,
3942 .restore_tpl = efi_restore_tpl,
3943 .allocate_pages = efi_allocate_pages_ext,
3944 .free_pages = efi_free_pages_ext,
3945 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003946 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003947 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003948 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003949 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003950 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003951 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003952 .close_event = efi_close_event,
3953 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003954 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003955 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003956 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003957 .handle_protocol = efi_handle_protocol,
3958 .reserved = NULL,
3959 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003960 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003961 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003962 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003963 .load_image = efi_load_image,
3964 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003965 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003966 .unload_image = efi_unload_image,
3967 .exit_boot_services = efi_exit_boot_services,
3968 .get_next_monotonic_count = efi_get_next_monotonic_count,
3969 .stall = efi_stall,
3970 .set_watchdog_timer = efi_set_watchdog_timer,
3971 .connect_controller = efi_connect_controller,
3972 .disconnect_controller = efi_disconnect_controller,
3973 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003974 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003975 .open_protocol_information = efi_open_protocol_information,
3976 .protocols_per_handle = efi_protocols_per_handle,
3977 .locate_handle_buffer = efi_locate_handle_buffer,
3978 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003979 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003980 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003981 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003982 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003983 .calculate_crc32 = efi_calculate_crc32,
3984 .copy_mem = efi_copy_mem,
3985 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003986 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003987};
3988
Simon Glass90975372022-01-23 12:55:12 -07003989static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003990
Alexander Graf393dd912016-10-14 13:45:30 +02003991struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003992 .hdr = {
3993 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003994 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003995 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003996 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003997 .fw_vendor = firmware_vendor,
3998 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003999 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01004000 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004001 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01004002};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004003
4004/**
4005 * efi_initialize_system_table() - Initialize system table
4006 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02004007 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004008 */
4009efi_status_t efi_initialize_system_table(void)
4010{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004011 efi_status_t ret;
4012
4013 /* Allocate configuration table array */
4014 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4015 EFI_MAX_CONFIGURATION_TABLES *
4016 sizeof(struct efi_configuration_table),
4017 (void **)&systab.tables);
4018
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004019 /*
4020 * These entries will be set to NULL in ExitBootServices(). To avoid
4021 * relocation in SetVirtualAddressMap(), set them dynamically.
4022 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01004023 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004024 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004025 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004026 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004027 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004028 systab.std_err = &efi_con_out;
4029 systab.boottime = &efi_boot_services;
4030
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02004031 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004032 efi_update_table_header_crc32(&systab.hdr);
4033 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4034 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004035
4036 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004037}