blob: 3767fa2db429a5cf3f53ebc74511ed7980e50b5b [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020030
Alexander Grafc15d9212016-03-04 01:09:59 +010031/* This list contains all the EFI objects our payload has access to */
32LIST_HEAD(efi_obj_list);
33
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010036
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020037/* List of queued events */
Bin Mengd89dea32023-04-05 20:15:17 +080038static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020039
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
Bin Mengd89dea32023-04-05 20:15:17 +080047static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020048
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010053/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010055 * fixed when compiling U-Boot. However, the payload does not know about that
56 * restriction so we need to manually swap its and our view of that register on
57 * EFI callback entry/exit.
58 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060060#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010061
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030062static efi_status_t efi_uninstall_protocol
63 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +030064 void *protocol_interface, bool preserve);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030065
Heinrich Schuchardt72506232019-02-09 14:10:39 +010066/* 1 if inside U-Boot code, 0 if inside EFI payload code */
67static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040068static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010069/* GUID of the device tree table */
70const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010071/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
72const efi_guid_t efi_guid_driver_binding_protocol =
73 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040074
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010075/* event group ExitBootServices() invoked */
76const efi_guid_t efi_guid_event_group_exit_boot_services =
77 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt44772c42021-11-16 18:46:27 +010078/* event group before ExitBootServices() invoked */
79const efi_guid_t efi_guid_event_group_before_exit_boot_services =
80 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010081/* event group SetVirtualAddressMap() invoked */
82const efi_guid_t efi_guid_event_group_virtual_address_change =
83 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
84/* event group memory map changed */
85const efi_guid_t efi_guid_event_group_memory_map_change =
86 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
87/* event group boot manager about to boot */
88const efi_guid_t efi_guid_event_group_ready_to_boot =
89 EFI_EVENT_GROUP_READY_TO_BOOT;
90/* event group ResetSystem() invoked (before ExitBootServices) */
91const efi_guid_t efi_guid_event_group_reset_system =
92 EFI_EVENT_GROUP_RESET_SYSTEM;
Masahisa Kojima1923f362023-11-10 13:25:39 +090093/* event group return to efibootmgr */
94const efi_guid_t efi_guid_event_group_return_to_efibootmgr =
95 EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010096/* GUIDs of the Load File and Load File2 protocols */
97const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
98const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090099/* GUID of the SMBIOS table */
100const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +0100101
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100102static efi_status_t EFIAPI efi_disconnect_controller(
103 efi_handle_t controller_handle,
104 efi_handle_t driver_image_handle,
105 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +0100106
Ilias Apalodimas1e10d622023-06-20 09:19:28 +0300107static
108efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
109 efi_handle_t *driver_image_handle,
110 struct efi_device_path *remain_device_path,
111 bool recursive);
112
Rob Clark86789d52017-07-27 08:04:18 -0400113/* Called on every callback entry */
114int __efi_entry_check(void)
115{
116 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200117#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400118 assert(efi_gd);
119 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200120 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400121#endif
122 return ret;
123}
124
125/* Called on every callback exit */
126int __efi_exit_check(void)
127{
128 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200129#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200130 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400131#endif
132 return ret;
133}
134
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200135/**
136 * efi_save_gd() - save global data register
137 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200138 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200139 * As this register may be overwritten by an EFI payload we save it here
140 * and restore it on every callback entered.
141 *
142 * This function is called after relocation from initr_reloc_global_data().
143 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100144void efi_save_gd(void)
145{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200146#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100147 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600148#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100149}
150
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200151/**
152 * efi_restore_gd() - restore global data register
153 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200154 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200155 * Restore it after returning from the UEFI world to the value saved via
156 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400157 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100158void efi_restore_gd(void)
159{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200160#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100161 /* Only restore if we're already in EFI context */
162 if (!efi_gd)
163 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200164 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600165#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100166}
167
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200168/**
Mario Six8fac2912018-07-10 08:40:17 +0200169 * indent_string() - returns a string for indenting with two spaces per level
170 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100171 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200172 * A maximum of ten indent levels is supported. Higher indent levels will be
173 * truncated.
174 *
Mario Six8fac2912018-07-10 08:40:17 +0200175 * Return: A string for indenting with two spaces per level is
176 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400177 */
178static const char *indent_string(int level)
179{
180 const char *indent = " ";
181 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100182
Rob Clarke7896c32017-07-27 08:04:19 -0400183 level = min(max, level * 2);
184 return &indent[max - level];
185}
186
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200187const char *__efi_nesting(void)
188{
189 return indent_string(nesting_level);
190}
191
Rob Clarke7896c32017-07-27 08:04:19 -0400192const char *__efi_nesting_inc(void)
193{
194 return indent_string(nesting_level++);
195}
196
197const char *__efi_nesting_dec(void)
198{
199 return indent_string(--nesting_level);
200}
201
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200202/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200203 * efi_event_is_queued() - check if an event is queued
204 *
205 * @event: event
206 * Return: true if event is queued
207 */
208static bool efi_event_is_queued(struct efi_event *event)
209{
210 return !!event->queue_link.next;
211}
212
213/**
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300214 * efi_purge_handle() - Clean the deleted handle from the various lists
215 * @handle: handle to remove
216 *
217 * Return: status code
218 */
219static efi_status_t efi_purge_handle(efi_handle_t handle)
220{
221 struct efi_register_notify_event *item;
222
223 if (!list_empty(&handle->protocols))
224 return EFI_ACCESS_DENIED;
225 /* The handle is about to be freed. Remove it from events */
226 list_for_each_entry(item, &efi_register_notify_events, link) {
227 struct efi_protocol_notification *hitem, *hnext;
228
229 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
230 if (handle == hitem->handle) {
231 list_del(&hitem->link);
232 free(hitem);
233 }
234 }
235 }
236 /* The last protocol has been removed, delete the handle. */
237 list_del(&handle->link);
238 free(handle);
239
240 return EFI_SUCCESS;
241}
242
243/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200244 * efi_process_event_queue() - process event queue
245 */
246static void efi_process_event_queue(void)
247{
248 while (!list_empty(&efi_event_queue)) {
249 struct efi_event *event;
250 efi_uintn_t old_tpl;
251
252 event = list_first_entry(&efi_event_queue, struct efi_event,
253 queue_link);
254 if (efi_tpl >= event->notify_tpl)
255 return;
256 list_del(&event->queue_link);
257 event->queue_link.next = NULL;
258 event->queue_link.prev = NULL;
259 /* Events must be executed at the event's TPL */
260 old_tpl = efi_tpl;
261 efi_tpl = event->notify_tpl;
262 EFI_CALL_VOID(event->notify_function(event,
263 event->notify_context));
264 efi_tpl = old_tpl;
265 if (event->type == EVT_NOTIFY_SIGNAL)
266 event->is_signaled = 0;
267 }
268}
269
270/**
Mario Six8fac2912018-07-10 08:40:17 +0200271 * efi_queue_event() - queue an EFI event
272 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200273 *
274 * This function queues the notification function of the event for future
275 * execution.
276 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200277 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200278static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200279{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100280 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200281
282 if (!event->notify_function)
283 return;
284
285 if (!efi_event_is_queued(event)) {
286 /*
287 * Events must be notified in order of decreasing task priority
288 * level. Insert the new event accordingly.
289 */
290 list_for_each_entry(item, &efi_event_queue, queue_link) {
291 if (item->notify_tpl < event->notify_tpl) {
292 list_add_tail(&event->queue_link,
293 &item->queue_link);
294 event = NULL;
295 break;
296 }
297 }
298 if (event)
299 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100300 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200301 }
302}
303
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200304/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200305 * is_valid_tpl() - check if the task priority level is valid
306 *
307 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200308 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200309 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100310static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200311{
312 switch (tpl) {
313 case TPL_APPLICATION:
314 case TPL_CALLBACK:
315 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200316 return EFI_SUCCESS;
317 default:
318 return EFI_INVALID_PARAMETER;
319 }
320}
321
322/**
Mario Six8fac2912018-07-10 08:40:17 +0200323 * efi_signal_event() - signal an EFI event
324 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100325 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100326 * This function signals an event. If the event belongs to an event group, all
327 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100328 * their notification function is queued.
329 *
330 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100331 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200332void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100333{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200334 if (event->is_signaled)
335 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100336 if (event->group) {
337 struct efi_event *evt;
338
339 /*
340 * The signaled state has to set before executing any
341 * notification function
342 */
343 list_for_each_entry(evt, &efi_events, link) {
344 if (!evt->group || guidcmp(evt->group, event->group))
345 continue;
346 if (evt->is_signaled)
347 continue;
348 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100349 }
350 list_for_each_entry(evt, &efi_events, link) {
351 if (!evt->group || guidcmp(evt->group, event->group))
352 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200353 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100354 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200355 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100356 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200357 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100358 }
359}
360
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200361/**
Mario Six8fac2912018-07-10 08:40:17 +0200362 * efi_raise_tpl() - raise the task priority level
363 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200364 *
365 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200366 *
367 * See the Unified Extensible Firmware Interface (UEFI) specification for
368 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200369 *
Mario Six8fac2912018-07-10 08:40:17 +0200370 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200371 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100372static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100373{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100374 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200375
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200376 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200377
378 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200379 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200380 efi_tpl = new_tpl;
381 if (efi_tpl > TPL_HIGH_LEVEL)
382 efi_tpl = TPL_HIGH_LEVEL;
383
384 EFI_EXIT(EFI_SUCCESS);
385 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100386}
387
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200388/**
Mario Six8fac2912018-07-10 08:40:17 +0200389 * efi_restore_tpl() - lower the task priority level
390 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 *
392 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200393 *
Mario Six8fac2912018-07-10 08:40:17 +0200394 * See the Unified Extensible Firmware Interface (UEFI) specification for
395 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100397static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100398{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200399 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200400
401 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200402 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200403 efi_tpl = old_tpl;
404 if (efi_tpl > TPL_HIGH_LEVEL)
405 efi_tpl = TPL_HIGH_LEVEL;
406
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100407 /*
408 * Lowering the TPL may have made queued events eligible for execution.
409 */
410 efi_timer_check();
411
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200412 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100413}
414
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200415/**
Mario Six8fac2912018-07-10 08:40:17 +0200416 * efi_allocate_pages_ext() - allocate memory pages
417 * @type: type of allocation to be performed
418 * @memory_type: usage type of the allocated memory
419 * @pages: number of pages to be allocated
420 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200421 *
422 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 *
Mario Six8fac2912018-07-10 08:40:17 +0200424 * See the Unified Extensible Firmware Interface (UEFI) specification for
425 * details.
426 *
427 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200428 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900429static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100430 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900431 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100432{
433 efi_status_t r;
434
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100435 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100436 r = efi_allocate_pages(type, memory_type, pages, memory);
437 return EFI_EXIT(r);
438}
439
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200440/**
Mario Six8fac2912018-07-10 08:40:17 +0200441 * efi_free_pages_ext() - Free memory pages.
442 * @memory: start of the memory area to be freed
443 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200444 *
445 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200446 *
447 * See the Unified Extensible Firmware Interface (UEFI) specification for
448 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200449 *
Mario Six8fac2912018-07-10 08:40:17 +0200450 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200451 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900452static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100453 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100454{
455 efi_status_t r;
456
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900457 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100458 r = efi_free_pages(memory, pages);
459 return EFI_EXIT(r);
460}
461
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200462/**
Mario Six8fac2912018-07-10 08:40:17 +0200463 * efi_get_memory_map_ext() - get map describing memory usage
464 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
465 * on exit the size of the copied memory map
466 * @memory_map: buffer to which the memory map is written
467 * @map_key: key for the memory map
468 * @descriptor_size: size of an individual memory descriptor
469 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200470 *
471 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200472 *
Mario Six8fac2912018-07-10 08:40:17 +0200473 * See the Unified Extensible Firmware Interface (UEFI) specification for
474 * details.
475 *
476 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200477 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900478static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100479 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900480 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100481 efi_uintn_t *map_key,
482 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900483 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100484{
485 efi_status_t r;
486
487 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
488 map_key, descriptor_size, descriptor_version);
489 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
490 descriptor_size, descriptor_version);
491 return EFI_EXIT(r);
492}
493
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200494/**
Mario Six8fac2912018-07-10 08:40:17 +0200495 * efi_allocate_pool_ext() - allocate memory from pool
496 * @pool_type: type of the pool from which memory is to be allocated
497 * @size: number of bytes to be allocated
498 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200499 *
500 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200501 *
502 * See the Unified Extensible Firmware Interface (UEFI) specification for
503 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200504 *
Mario Six8fac2912018-07-10 08:40:17 +0200505 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200506 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200507static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100508 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200509 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100510{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100511 efi_status_t r;
512
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100513 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200514 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100515 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100516}
517
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200518/**
Mario Six8fac2912018-07-10 08:40:17 +0200519 * efi_free_pool_ext() - free memory from pool
520 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200521 *
522 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200523 *
Mario Six8fac2912018-07-10 08:40:17 +0200524 * See the Unified Extensible Firmware Interface (UEFI) specification for
525 * details.
526 *
527 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200528 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200529static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100530{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100531 efi_status_t r;
532
533 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200534 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100535 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100536}
537
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200538/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200539 * efi_add_handle() - add a new handle to the object list
540 *
541 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100542 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200543 * The protocols list is initialized. The handle is added to the list of known
544 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100545 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200546void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100547{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200548 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100549 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200550 INIT_LIST_HEAD(&handle->protocols);
551 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100552}
553
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200554/**
Mario Six8fac2912018-07-10 08:40:17 +0200555 * efi_create_handle() - create handle
556 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200557 *
Mario Six8fac2912018-07-10 08:40:17 +0200558 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200559 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100560efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200561{
562 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200563
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200564 obj = calloc(1, sizeof(struct efi_object));
565 if (!obj)
566 return EFI_OUT_OF_RESOURCES;
567
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100568 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200569 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200570
571 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200572}
573
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200574/**
Mario Six8fac2912018-07-10 08:40:17 +0200575 * efi_search_protocol() - find a protocol on a handle.
576 * @handle: handle
577 * @protocol_guid: GUID of the protocol
578 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100579 *
Mario Six8fac2912018-07-10 08:40:17 +0200580 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100582efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100583 const efi_guid_t *protocol_guid,
584 struct efi_handler **handler)
585{
586 struct efi_object *efiobj;
587 struct list_head *lhandle;
588
589 if (!handle || !protocol_guid)
590 return EFI_INVALID_PARAMETER;
591 efiobj = efi_search_obj(handle);
592 if (!efiobj)
593 return EFI_INVALID_PARAMETER;
594 list_for_each(lhandle, &efiobj->protocols) {
595 struct efi_handler *protocol;
596
597 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100598 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599 if (handler)
600 *handler = protocol;
601 return EFI_SUCCESS;
602 }
603 }
604 return EFI_NOT_FOUND;
605}
606
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200607/**
Mario Six8fac2912018-07-10 08:40:17 +0200608 * efi_remove_protocol() - delete protocol from a handle
609 * @handle: handle from which the protocol shall be deleted
610 * @protocol: GUID of the protocol to be deleted
611 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100612 *
Mario Six8fac2912018-07-10 08:40:17 +0200613 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100614 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300615static efi_status_t efi_remove_protocol(const efi_handle_t handle,
616 const efi_guid_t *protocol,
617 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100618{
619 struct efi_handler *handler;
620 efi_status_t ret;
621
622 ret = efi_search_protocol(handle, protocol, &handler);
623 if (ret != EFI_SUCCESS)
624 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200625 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200626 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627 list_del(&handler->link);
628 free(handler);
629 return EFI_SUCCESS;
630}
631
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200632/**
Mario Six8fac2912018-07-10 08:40:17 +0200633 * efi_remove_all_protocols() - delete all protocols from a handle
634 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100635 *
Mario Six8fac2912018-07-10 08:40:17 +0200636 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100637 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100638static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100639{
640 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100641 struct efi_handler *protocol;
642 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100643
644 efiobj = efi_search_obj(handle);
645 if (!efiobj)
646 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100647 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100648 efi_status_t ret;
649
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300650 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300651 protocol->protocol_interface, true);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100652 if (ret != EFI_SUCCESS)
653 return ret;
654 }
655 return EFI_SUCCESS;
656}
657
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200658/**
Mario Six8fac2912018-07-10 08:40:17 +0200659 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100660 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200661 * @handle: handle to delete
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300662 *
663 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100664 */
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300665efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100666{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200667 efi_status_t ret;
668
669 ret = efi_remove_all_protocols(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300670 if (ret != EFI_SUCCESS) {
671 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
672 return ret;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200673 }
674
Ilias Apalodimasc76eade2023-08-24 17:21:09 +0300675 return efi_purge_handle(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100676}
677
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200678/**
Mario Six8fac2912018-07-10 08:40:17 +0200679 * efi_is_event() - check if a pointer is a valid event
680 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100681 *
Mario Six8fac2912018-07-10 08:40:17 +0200682 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100683 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100684static efi_status_t efi_is_event(const struct efi_event *event)
685{
686 const struct efi_event *evt;
687
688 if (!event)
689 return EFI_INVALID_PARAMETER;
690 list_for_each_entry(evt, &efi_events, link) {
691 if (evt == event)
692 return EFI_SUCCESS;
693 }
694 return EFI_INVALID_PARAMETER;
695}
Alexander Grafc15d9212016-03-04 01:09:59 +0100696
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200697/**
Mario Six8fac2912018-07-10 08:40:17 +0200698 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200699 *
Mario Six8fac2912018-07-10 08:40:17 +0200700 * @type: type of the event to create
701 * @notify_tpl: task priority level of the event
702 * @notify_function: notification function of the event
703 * @notify_context: pointer passed to the notification function
704 * @group: event group
705 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200706 *
707 * This function is used inside U-Boot code to create an event.
708 *
709 * For the API function implementing the CreateEvent service see
710 * efi_create_event_ext.
711 *
Mario Six8fac2912018-07-10 08:40:17 +0200712 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200713 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100714efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200715 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200716 struct efi_event *event,
717 void *context),
Masahisa Kojima787774a2023-11-10 13:25:38 +0900718 void *notify_context, const efi_guid_t *group,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100719 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100720{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100721 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200722 efi_status_t ret;
723 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200724
Jonathan Gray7758b212017-03-12 19:26:07 +1100725 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200726 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100727
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200728 switch (type) {
729 case 0:
730 case EVT_TIMER:
731 case EVT_NOTIFY_SIGNAL:
732 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
733 case EVT_NOTIFY_WAIT:
734 case EVT_TIMER | EVT_NOTIFY_WAIT:
735 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200736 pool_type = EFI_BOOT_SERVICES_DATA;
737 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200738 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200739 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200740 break;
741 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200742 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200743 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100744
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100745 /*
746 * The UEFI specification requires event notification levels to be
747 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
748 *
749 * Parameter NotifyTpl should not be checked if it is not used.
750 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900751 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100752 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
753 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200754 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100755
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200756 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
757 (void **)&evt);
758 if (ret != EFI_SUCCESS)
759 return ret;
760 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100761 evt->type = type;
762 evt->notify_tpl = notify_tpl;
763 evt->notify_function = notify_function;
764 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100765 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200766 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100767 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100768 list_add_tail(&evt->link, &efi_events);
769 *event = evt;
770 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100771}
772
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200773/*
Mario Six8fac2912018-07-10 08:40:17 +0200774 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100775 *
Mario Six8fac2912018-07-10 08:40:17 +0200776 * @type: type of the event to create
777 * @notify_tpl: task priority level of the event
778 * @notify_function: notification function of the event
779 * @notify_context: pointer passed to the notification function
780 * @event: created event
781 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100782 *
783 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100784 *
Mario Six8fac2912018-07-10 08:40:17 +0200785 * See the Unified Extensible Firmware Interface (UEFI) specification for
786 * details.
787 *
788 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100789 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100790static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100791efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
792 void (EFIAPI *notify_function) (
793 struct efi_event *event,
794 void *context),
795 void *notify_context,
Masahisa Kojima787774a2023-11-10 13:25:38 +0900796 const efi_guid_t *event_group,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100797 struct efi_event **event)
798{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200799 efi_status_t ret;
800
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100801 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100802 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200803
804 /*
805 * The allowable input parameters are the same as in CreateEvent()
806 * except for the following two disallowed event types.
807 */
808 switch (type) {
809 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
810 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
811 ret = EFI_INVALID_PARAMETER;
812 goto out;
813 }
814
815 ret = efi_create_event(type, notify_tpl, notify_function,
816 notify_context, event_group, event);
817out:
818 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100819}
820
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200821/**
Mario Six8fac2912018-07-10 08:40:17 +0200822 * efi_create_event_ext() - create an event
823 * @type: type of the event to create
824 * @notify_tpl: task priority level of the event
825 * @notify_function: notification function of the event
826 * @notify_context: pointer passed to the notification function
827 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200828 *
829 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200830 *
831 * See the Unified Extensible Firmware Interface (UEFI) specification for
832 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200833 *
Mario Six8fac2912018-07-10 08:40:17 +0200834 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200835 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200836static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100837 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200838 void (EFIAPI *notify_function) (
839 struct efi_event *event,
840 void *context),
841 void *notify_context, struct efi_event **event)
842{
843 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
844 notify_context);
845 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100846 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200847}
848
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200849/**
Mario Six8fac2912018-07-10 08:40:17 +0200850 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200851 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200852 * Check if a timer event has occurred or a queued notification function should
853 * be called.
854 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100855 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200856 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100857 */
858void efi_timer_check(void)
859{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100860 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100861 u64 now = timer_get_us();
862
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100863 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200864 if (!timers_enabled)
865 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100866 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200867 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100868 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200869 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100870 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200871 break;
872 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100873 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200874 break;
875 default:
876 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200877 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100878 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200879 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100880 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200881 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200882 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100883}
884
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200885/**
Mario Six8fac2912018-07-10 08:40:17 +0200886 * efi_set_timer() - set the trigger time for a timer event or stop the event
887 * @event: event for which the timer is set
888 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200889 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200890 *
891 * This is the function for internal usage in U-Boot. For the API function
892 * implementing the SetTimer service see efi_set_timer_ext.
893 *
Mario Six8fac2912018-07-10 08:40:17 +0200894 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200895 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200896efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200897 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100898{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100899 /* Check that the event is valid */
900 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
901 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100902
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200903 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200904 * The parameter defines a multiple of 100 ns.
905 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200906 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200907 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100908
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100909 switch (type) {
910 case EFI_TIMER_STOP:
911 event->trigger_next = -1ULL;
912 break;
913 case EFI_TIMER_PERIODIC:
914 case EFI_TIMER_RELATIVE:
915 event->trigger_next = timer_get_us() + trigger_time;
916 break;
917 default:
918 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100919 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 event->trigger_type = type;
921 event->trigger_time = trigger_time;
922 event->is_signaled = false;
923 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100924}
925
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200926/**
Mario Six8fac2912018-07-10 08:40:17 +0200927 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
928 * event
929 * @event: event for which the timer is set
930 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200931 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200932 *
933 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200934 *
Mario Six8fac2912018-07-10 08:40:17 +0200935 * See the Unified Extensible Firmware Interface (UEFI) specification for
936 * details.
937 *
938 *
939 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200940 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200941static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
942 enum efi_timer_delay type,
943 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200944{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900945 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200946 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
947}
948
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200949/**
Mario Six8fac2912018-07-10 08:40:17 +0200950 * efi_wait_for_event() - wait for events to be signaled
951 * @num_events: number of events to be waited for
952 * @event: events to be waited for
953 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200954 *
955 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200956 *
Mario Six8fac2912018-07-10 08:40:17 +0200957 * See the Unified Extensible Firmware Interface (UEFI) specification for
958 * details.
959 *
960 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200961 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100962static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200963 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100964 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100965{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100966 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100967
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100968 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100969
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200970 /* Check parameters */
971 if (!num_events || !event)
972 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200973 /* Check TPL */
974 if (efi_tpl != TPL_APPLICATION)
975 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200976 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100977 if (efi_is_event(event[i]) != EFI_SUCCESS)
978 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200979 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
980 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200981 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200982 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200983 }
984
985 /* Wait for signal */
986 for (;;) {
987 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200988 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200989 goto out;
990 }
991 /* Allow events to occur. */
992 efi_timer_check();
993 }
994
995out:
996 /*
997 * Reset the signal which is passed to the caller to allow periodic
998 * events to occur.
999 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +02001000 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001001 if (index)
1002 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +01001003
1004 return EFI_EXIT(EFI_SUCCESS);
1005}
1006
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001007/**
Mario Six8fac2912018-07-10 08:40:17 +02001008 * efi_signal_event_ext() - signal an EFI event
1009 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001010 *
1011 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001012 *
1013 * See the Unified Extensible Firmware Interface (UEFI) specification for
1014 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001015 *
1016 * This functions sets the signaled state of the event and queues the
1017 * notification function for execution.
1018 *
Mario Six8fac2912018-07-10 08:40:17 +02001019 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001020 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001021static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001022{
1023 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001024 if (efi_is_event(event) != EFI_SUCCESS)
1025 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001026 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +01001027 return EFI_EXIT(EFI_SUCCESS);
1028}
1029
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001030/**
Mario Six8fac2912018-07-10 08:40:17 +02001031 * efi_close_event() - close an EFI event
1032 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001033 *
1034 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001035 *
Mario Six8fac2912018-07-10 08:40:17 +02001036 * See the Unified Extensible Firmware Interface (UEFI) specification for
1037 * details.
1038 *
1039 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001040 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001041static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001042{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001043 struct efi_register_notify_event *item, *next;
1044
Alexander Grafc15d9212016-03-04 01:09:59 +01001045 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001046 if (efi_is_event(event) != EFI_SUCCESS)
1047 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001048
1049 /* Remove protocol notify registrations for the event */
1050 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1051 link) {
1052 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001053 struct efi_protocol_notification *hitem, *hnext;
1054
1055 /* Remove signaled handles */
1056 list_for_each_entry_safe(hitem, hnext, &item->handles,
1057 link) {
1058 list_del(&hitem->link);
1059 free(hitem);
1060 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001061 list_del(&item->link);
1062 free(item);
1063 }
1064 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001065 /* Remove event from queue */
1066 if (efi_event_is_queued(event))
1067 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001068
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001069 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001070 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001071 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001072}
1073
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001074/**
Mario Six8fac2912018-07-10 08:40:17 +02001075 * efi_check_event() - check if an event is signaled
1076 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001077 *
1078 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001079 *
1080 * See the Unified Extensible Firmware Interface (UEFI) specification for
1081 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001082 *
Mario Six8fac2912018-07-10 08:40:17 +02001083 * If an event is not signaled yet, the notification function is queued. The
1084 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001085 *
Mario Six8fac2912018-07-10 08:40:17 +02001086 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001087 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001088static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001089{
1090 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001091 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001092 if (efi_is_event(event) != EFI_SUCCESS ||
1093 event->type & EVT_NOTIFY_SIGNAL)
1094 return EFI_EXIT(EFI_INVALID_PARAMETER);
1095 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001096 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001097 if (event->is_signaled) {
1098 event->is_signaled = false;
1099 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001100 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001101 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001102}
1103
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001104/**
Mario Six8fac2912018-07-10 08:40:17 +02001105 * efi_search_obj() - find the internal EFI object for a handle
1106 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001107 *
Mario Six8fac2912018-07-10 08:40:17 +02001108 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001109 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001110struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001111{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001112 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001113
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001114 if (!handle)
1115 return NULL;
1116
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001117 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001118 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001119 return efiobj;
1120 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001121 return NULL;
1122}
1123
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001124/**
Mario Six8fac2912018-07-10 08:40:17 +02001125 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1126 * to a protocol
1127 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001128 *
Mario Six8fac2912018-07-10 08:40:17 +02001129 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001130 */
1131static struct efi_open_protocol_info_entry *efi_create_open_info(
1132 struct efi_handler *handler)
1133{
1134 struct efi_open_protocol_info_item *item;
1135
1136 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1137 if (!item)
1138 return NULL;
1139 /* Append the item to the open protocol info list. */
1140 list_add_tail(&item->link, &handler->open_infos);
1141
1142 return &item->info;
1143}
1144
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001145/**
Mario Six8fac2912018-07-10 08:40:17 +02001146 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1147 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001148 *
Mario Six8fac2912018-07-10 08:40:17 +02001149 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001150 */
1151static efi_status_t efi_delete_open_info(
1152 struct efi_open_protocol_info_item *item)
1153{
1154 list_del(&item->link);
1155 free(item);
1156 return EFI_SUCCESS;
1157}
1158
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001159/**
Mario Six8fac2912018-07-10 08:40:17 +02001160 * efi_add_protocol() - install new protocol on a handle
1161 * @handle: handle on which the protocol shall be installed
1162 * @protocol: GUID of the protocol to be installed
1163 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001164 *
Mario Six8fac2912018-07-10 08:40:17 +02001165 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001166 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001167efi_status_t efi_add_protocol(const efi_handle_t handle,
1168 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001169 void *protocol_interface)
1170{
1171 struct efi_object *efiobj;
1172 struct efi_handler *handler;
1173 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001174 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001175
1176 efiobj = efi_search_obj(handle);
1177 if (!efiobj)
1178 return EFI_INVALID_PARAMETER;
1179 ret = efi_search_protocol(handle, protocol, NULL);
1180 if (ret != EFI_NOT_FOUND)
1181 return EFI_INVALID_PARAMETER;
1182 handler = calloc(1, sizeof(struct efi_handler));
1183 if (!handler)
1184 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001185 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001186 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001187 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001188 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001189
1190 /* Notify registered events */
1191 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001192 if (!guidcmp(protocol, &event->protocol)) {
1193 struct efi_protocol_notification *notif;
1194
1195 notif = calloc(1, sizeof(*notif));
1196 if (!notif) {
1197 list_del(&handler->link);
1198 free(handler);
1199 return EFI_OUT_OF_RESOURCES;
1200 }
1201 notif->handle = handle;
1202 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001203 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001204 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001205 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001206 }
1207
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001208 if (!guidcmp(&efi_guid_device_path, protocol))
1209 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001210 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001211}
1212
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001213/**
Mario Six8fac2912018-07-10 08:40:17 +02001214 * efi_install_protocol_interface() - install protocol interface
1215 * @handle: handle on which the protocol shall be installed
1216 * @protocol: GUID of the protocol to be installed
1217 * @protocol_interface_type: type of the interface to be installed,
1218 * always EFI_NATIVE_INTERFACE
1219 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001220 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001221 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001222 *
Mario Six8fac2912018-07-10 08:40:17 +02001223 * See the Unified Extensible Firmware Interface (UEFI) specification for
1224 * details.
1225 *
1226 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001227 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001228static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001229 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001230 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001231{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001232 efi_status_t r;
1233
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001234 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001235 protocol_interface);
1236
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001237 if (!handle || !protocol ||
1238 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1239 r = EFI_INVALID_PARAMETER;
1240 goto out;
1241 }
1242
1243 /* Create new handle if requested. */
1244 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001245 r = efi_create_handle(handle);
1246 if (r != EFI_SUCCESS)
1247 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001248 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001249 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001250 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001251 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001252 /* Add new protocol */
1253 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001254out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001255 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001256}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001257
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001258/**
Mario Six8fac2912018-07-10 08:40:17 +02001259 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001260 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001261 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001262 * @number_of_drivers: number of child controllers
1263 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001264 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001265 * The allocated buffer has to be freed with free().
1266 *
Mario Six8fac2912018-07-10 08:40:17 +02001267 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001268 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001269static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001270 const efi_guid_t *protocol,
1271 efi_uintn_t *number_of_drivers,
1272 efi_handle_t **driver_handle_buffer)
1273{
1274 struct efi_handler *handler;
1275 struct efi_open_protocol_info_item *item;
1276 efi_uintn_t count = 0, i;
1277 bool duplicate;
1278
1279 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001280 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001281 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001282 continue;
1283 list_for_each_entry(item, &handler->open_infos, link) {
1284 if (item->info.attributes &
1285 EFI_OPEN_PROTOCOL_BY_DRIVER)
1286 ++count;
1287 }
1288 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001289 *number_of_drivers = 0;
1290 if (!count) {
1291 *driver_handle_buffer = NULL;
1292 return EFI_SUCCESS;
1293 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001294 /*
1295 * Create buffer. In case of duplicate driver assignments the buffer
1296 * will be too large. But that does not harm.
1297 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001298 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1299 if (!*driver_handle_buffer)
1300 return EFI_OUT_OF_RESOURCES;
1301 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001302 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001303 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001304 continue;
1305 list_for_each_entry(item, &handler->open_infos, link) {
1306 if (item->info.attributes &
1307 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1308 /* Check this is a new driver */
1309 duplicate = false;
1310 for (i = 0; i < *number_of_drivers; ++i) {
1311 if ((*driver_handle_buffer)[i] ==
1312 item->info.agent_handle)
1313 duplicate = true;
1314 }
1315 /* Copy handle to buffer */
1316 if (!duplicate) {
1317 i = (*number_of_drivers)++;
1318 (*driver_handle_buffer)[i] =
1319 item->info.agent_handle;
1320 }
1321 }
1322 }
1323 }
1324 return EFI_SUCCESS;
1325}
1326
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001327/**
Mario Six8fac2912018-07-10 08:40:17 +02001328 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001329 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001330 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001331 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001332 *
1333 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001334 *
1335 * See the Unified Extensible Firmware Interface (UEFI) specification for
1336 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001337 *
Mario Six8fac2912018-07-10 08:40:17 +02001338 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001339 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001340static efi_status_t efi_disconnect_all_drivers
1341 (efi_handle_t handle,
1342 const efi_guid_t *protocol,
1343 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001344{
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001345 efi_uintn_t number_of_drivers, tmp;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001346 efi_handle_t *driver_handle_buffer;
1347 efi_status_t r, ret;
1348
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001349 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001350 &driver_handle_buffer);
1351 if (ret != EFI_SUCCESS)
1352 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001353 if (!number_of_drivers)
1354 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001355
1356 tmp = number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001357 while (number_of_drivers) {
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001358 ret = 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 (ret != EFI_SUCCESS)
1363 goto reconnect;
1364 }
1365
1366 free(driver_handle_buffer);
1367 return ret;
1368
1369reconnect:
1370 /* Reconnect all disconnected drivers */
1371 for (; number_of_drivers < tmp; number_of_drivers++) {
1372 r = EFI_CALL(efi_connect_controller(handle,
1373 &driver_handle_buffer[number_of_drivers],
1374 NULL, true));
1375 if (r != EFI_SUCCESS)
1376 EFI_PRINT("Failed to reconnect controller\n");
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001377 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001378
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001379 free(driver_handle_buffer);
1380 return ret;
1381}
1382
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001383/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001384 * efi_uninstall_protocol() - uninstall protocol interface
1385 *
Mario Six8fac2912018-07-10 08:40:17 +02001386 * @handle: handle from which the protocol shall be removed
1387 * @protocol: GUID of the protocol to be removed
1388 * @protocol_interface: interface to be removed
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001389 * @preserve: preserve or delete the handle and remove it from any
1390 * list it participates if no protocols remain
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001391 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001392 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001393 *
1394 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001395 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001396static efi_status_t efi_uninstall_protocol
1397 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001398 void *protocol_interface, bool preserve)
Alexander Grafc15d9212016-03-04 01:09:59 +01001399{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001400 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001401 struct efi_open_protocol_info_item *item;
1402 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001403 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001404
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001405 /* Find the protocol on the handle */
1406 r = efi_search_protocol(handle, protocol, &handler);
1407 if (r != EFI_SUCCESS)
1408 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001409 if (handler->protocol_interface != protocol_interface)
1410 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001411 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001412 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001413 if (r != EFI_SUCCESS) {
1414 r = EFI_ACCESS_DENIED;
1415 goto out;
1416 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001417 /* Close protocol */
1418 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1419 if (item->info.attributes ==
1420 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1421 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1422 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001423 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001424 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001425 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001426 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001427 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001428 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001429 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001430 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001431 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001432 if (r != EFI_SUCCESS)
1433 return r;
1434 /*
1435 * We don't care about the return value here since the
1436 * handle might have more protocols installed
1437 */
1438 if (!preserve)
1439 efi_purge_handle(handle);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001440out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001441 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001442}
1443
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001444/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001445 * efi_uninstall_protocol_interface() - uninstall protocol interface
1446 * @handle: handle from which the protocol shall be removed
1447 * @protocol: GUID of the protocol to be removed
1448 * @protocol_interface: interface to be removed
1449 *
1450 * This function implements the UninstallProtocolInterface service.
1451 *
1452 * See the Unified Extensible Firmware Interface (UEFI) specification for
1453 * details.
1454 *
1455 * Return: status code
1456 */
1457static efi_status_t EFIAPI efi_uninstall_protocol_interface
1458 (efi_handle_t handle, const efi_guid_t *protocol,
1459 void *protocol_interface)
1460{
1461 efi_status_t ret;
1462
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001463 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001464
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03001465 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001466 if (ret != EFI_SUCCESS)
1467 goto out;
1468
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001469out:
1470 return EFI_EXIT(ret);
1471}
1472
1473/**
Mario Six8fac2912018-07-10 08:40:17 +02001474 * efi_register_protocol_notify() - register an event for notification when a
1475 * protocol is installed.
1476 * @protocol: GUID of the protocol whose installation shall be notified
1477 * @event: event to be signaled upon installation of the protocol
1478 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001479 *
1480 * This function implements the RegisterProtocolNotify service.
1481 * See the Unified Extensible Firmware Interface (UEFI) specification
1482 * for details.
1483 *
Mario Six8fac2912018-07-10 08:40:17 +02001484 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001485 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001486efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1487 struct efi_event *event,
1488 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001489{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001490 struct efi_register_notify_event *item;
1491 efi_status_t ret = EFI_SUCCESS;
1492
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001493 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001494
1495 if (!protocol || !event || !registration) {
1496 ret = EFI_INVALID_PARAMETER;
1497 goto out;
1498 }
1499
1500 item = calloc(1, sizeof(struct efi_register_notify_event));
1501 if (!item) {
1502 ret = EFI_OUT_OF_RESOURCES;
1503 goto out;
1504 }
1505
1506 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301507 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001508 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001509
1510 list_add_tail(&item->link, &efi_register_notify_events);
1511
1512 *registration = item;
1513out:
1514 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001515}
1516
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001517/**
Mario Six8fac2912018-07-10 08:40:17 +02001518 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001519 *
Mario Six8fac2912018-07-10 08:40:17 +02001520 * @search_type: selection criterion
1521 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001522 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001523 *
1524 * See the documentation of the LocateHandle service in the UEFI specification.
1525 *
Mario Six8fac2912018-07-10 08:40:17 +02001526 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001527 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001528static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001529 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001530{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001531 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001532
1533 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001534 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001535 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001536 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001537 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001538 return (ret != EFI_SUCCESS);
1539 default:
1540 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001541 return -1;
1542 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001543}
1544
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001545/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001546 * efi_check_register_notify_event() - check if registration key is valid
1547 *
1548 * Check that a pointer is a valid registration key as returned by
1549 * RegisterProtocolNotify().
1550 *
1551 * @key: registration key
1552 * Return: valid registration key or NULL
1553 */
1554static struct efi_register_notify_event *efi_check_register_notify_event
1555 (void *key)
1556{
1557 struct efi_register_notify_event *event;
1558
1559 list_for_each_entry(event, &efi_register_notify_events, link) {
1560 if (event == (struct efi_register_notify_event *)key)
1561 return event;
1562 }
1563 return NULL;
1564}
1565
1566/**
Mario Six8fac2912018-07-10 08:40:17 +02001567 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001568 *
1569 * @search_type: selection criterion
1570 * @protocol: GUID of the protocol
1571 * @search_key: registration key
1572 * @buffer_size: size of the buffer to receive the handles in bytes
1573 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001574 *
1575 * This function is meant for U-Boot internal calls. For the API implementation
1576 * of the LocateHandle service see efi_locate_handle_ext.
1577 *
Mario Six8fac2912018-07-10 08:40:17 +02001578 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001579 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001580static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001581 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001582 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001583 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001584{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001585 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001586 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001587 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001588 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001589
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001590 /* Check parameters */
1591 switch (search_type) {
1592 case ALL_HANDLES:
1593 break;
1594 case BY_REGISTER_NOTIFY:
1595 if (!search_key)
1596 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001597 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001598 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001599 if (!event)
1600 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001601 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001602 case BY_PROTOCOL:
1603 if (!protocol)
1604 return EFI_INVALID_PARAMETER;
1605 break;
1606 default:
1607 return EFI_INVALID_PARAMETER;
1608 }
1609
Alexander Grafc15d9212016-03-04 01:09:59 +01001610 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001611 if (search_type == BY_REGISTER_NOTIFY) {
1612 if (list_empty(&event->handles))
1613 return EFI_NOT_FOUND;
1614 handle = list_first_entry(&event->handles,
1615 struct efi_protocol_notification,
1616 link);
1617 efiobj = handle->handle;
1618 size += sizeof(void *);
1619 } else {
1620 list_for_each_entry(efiobj, &efi_obj_list, link) {
1621 if (!efi_search(search_type, protocol, efiobj))
1622 size += sizeof(void *);
1623 }
1624 if (size == 0)
1625 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001626 }
1627
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001628 if (!buffer_size)
1629 return EFI_INVALID_PARAMETER;
1630
Alexander Grafc15d9212016-03-04 01:09:59 +01001631 if (*buffer_size < size) {
1632 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001633 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001634 }
1635
Rob Clarkcdee3372017-08-06 14:10:07 -04001636 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001637
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001638 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001639 if (!buffer)
1640 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001641
Alexander Grafc15d9212016-03-04 01:09:59 +01001642 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001643 if (search_type == BY_REGISTER_NOTIFY) {
1644 *buffer = efiobj;
1645 list_del(&handle->link);
1646 } else {
1647 list_for_each_entry(efiobj, &efi_obj_list, link) {
1648 if (!efi_search(search_type, protocol, efiobj))
1649 *buffer++ = efiobj;
1650 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001651 }
1652
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001653 return EFI_SUCCESS;
1654}
1655
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001656/**
Mario Six8fac2912018-07-10 08:40:17 +02001657 * efi_locate_handle_ext() - locate handles implementing a protocol.
1658 * @search_type: selection criterion
1659 * @protocol: GUID of the protocol
1660 * @search_key: registration key
1661 * @buffer_size: size of the buffer to receive the handles in bytes
1662 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001663 *
1664 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001665 *
Mario Six8fac2912018-07-10 08:40:17 +02001666 * See the Unified Extensible Firmware Interface (UEFI) specification for
1667 * details.
1668 *
1669 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001670 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001671static efi_status_t EFIAPI efi_locate_handle_ext(
1672 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001673 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001674 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001675{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001676 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001677 buffer_size, buffer);
1678
1679 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1680 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001681}
1682
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001683/**
Mario Six8fac2912018-07-10 08:40:17 +02001684 * efi_remove_configuration_table() - collapses configuration table entries,
1685 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001686 *
Mario Six8fac2912018-07-10 08:40:17 +02001687 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001688 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001689static void efi_remove_configuration_table(int i)
1690{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001691 struct efi_configuration_table *this = &systab.tables[i];
1692 struct efi_configuration_table *next = &systab.tables[i + 1];
1693 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001694
1695 memmove(this, next, (ulong)end - (ulong)next);
1696 systab.nr_tables--;
1697}
1698
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001699/**
Mario Six8fac2912018-07-10 08:40:17 +02001700 * efi_install_configuration_table() - adds, updates, or removes a
1701 * configuration table
1702 * @guid: GUID of the installed table
1703 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001704 *
1705 * This function is used for internal calls. For the API implementation of the
1706 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1707 *
Mario Six8fac2912018-07-10 08:40:17 +02001708 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001709 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001710efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1711 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001712{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001713 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001714 int i;
1715
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001716 if (!guid)
1717 return EFI_INVALID_PARAMETER;
1718
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001719 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001720 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001721 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001722 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001723 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001724 else
1725 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001726 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001727 }
1728 }
1729
Alexander Graffe3366f2017-07-26 13:41:04 +02001730 if (!table)
1731 return EFI_NOT_FOUND;
1732
Alexander Grafc15d9212016-03-04 01:09:59 +01001733 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001734 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001735 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001736
1737 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301738 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001739 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001740 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001741
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001742out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001743 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001744 efi_update_table_header_crc32(&systab.hdr);
1745
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001746 /* Notify that the configuration table was changed */
1747 list_for_each_entry(evt, &efi_events, link) {
1748 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001749 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001750 break;
1751 }
1752 }
1753
Alexander Grafc5c11632016-08-19 01:23:24 +02001754 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001755}
1756
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001757/**
Mario Six8fac2912018-07-10 08:40:17 +02001758 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1759 * configuration table.
1760 * @guid: GUID of the installed table
1761 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001762 *
1763 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001764 *
Mario Six8fac2912018-07-10 08:40:17 +02001765 * See the Unified Extensible Firmware Interface (UEFI) specification for
1766 * details.
1767 *
1768 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001769 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001770static efi_status_t
1771EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1772 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001773{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001774 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001775 return EFI_EXIT(efi_install_configuration_table(guid, table));
1776}
1777
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001778/**
Mario Six8fac2912018-07-10 08:40:17 +02001779 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001780 *
1781 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001782 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001783 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001784 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001785 * code is returned.
1786 *
1787 * @device_path: device path of the loaded image
1788 * @file_path: file path of the loaded image
1789 * @handle_ptr: handle of the loaded image
1790 * @info_ptr: loaded image protocol
1791 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001792 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001793efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1794 struct efi_device_path *file_path,
1795 struct efi_loaded_image_obj **handle_ptr,
1796 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001797{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001798 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001799 struct efi_loaded_image *info = NULL;
1800 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001801 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001802
1803 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1804 *handle_ptr = NULL;
1805 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001806
1807 info = calloc(1, sizeof(*info));
1808 if (!info)
1809 return EFI_OUT_OF_RESOURCES;
1810 obj = calloc(1, sizeof(*obj));
1811 if (!obj) {
1812 free(info);
1813 return EFI_OUT_OF_RESOURCES;
1814 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001815 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001816
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001817 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001818 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001819
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001820 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001821 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001822 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001823
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001824 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001825 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001826
1827 dp = efi_dp_append(device_path, file_path);
1828 if (!dp) {
1829 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001830 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001831 }
1832 } else {
1833 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001834 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001835 ret = efi_add_protocol(&obj->header,
1836 &efi_guid_loaded_image_device_path, dp);
1837 if (ret != EFI_SUCCESS)
1838 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001839
1840 /*
1841 * When asking for the loaded_image interface, just
1842 * return handle which points to loaded_image_info
1843 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001844 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001845 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001846 if (ret != EFI_SUCCESS)
1847 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001848
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001849 *info_ptr = info;
1850 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001851
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001852 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001853failure:
1854 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001855 efi_delete_handle(&obj->header);
1856 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001857 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001858}
1859
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001860/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001861 * efi_locate_device_path() - Get the device path and handle of an device
1862 * implementing a protocol
1863 * @protocol: GUID of the protocol
1864 * @device_path: device path
1865 * @device: handle of the device
1866 *
1867 * This function implements the LocateDevicePath service.
1868 *
1869 * See the Unified Extensible Firmware Interface (UEFI) specification for
1870 * details.
1871 *
1872 * Return: status code
1873 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001874efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1875 struct efi_device_path **device_path,
1876 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001877{
1878 struct efi_device_path *dp;
1879 size_t i;
1880 struct efi_handler *handler;
1881 efi_handle_t *handles;
1882 size_t len, len_dp;
1883 size_t len_best = 0;
1884 efi_uintn_t no_handles;
1885 u8 *remainder;
1886 efi_status_t ret;
1887
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001888 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001889
1890 if (!protocol || !device_path || !*device_path) {
1891 ret = EFI_INVALID_PARAMETER;
1892 goto out;
1893 }
1894
1895 /* Find end of device path */
1896 len = efi_dp_instance_size(*device_path);
1897
1898 /* Get all handles implementing the protocol */
1899 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1900 &no_handles, &handles));
1901 if (ret != EFI_SUCCESS)
1902 goto out;
1903
1904 for (i = 0; i < no_handles; ++i) {
1905 /* Find the device path protocol */
1906 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1907 &handler);
1908 if (ret != EFI_SUCCESS)
1909 continue;
1910 dp = (struct efi_device_path *)handler->protocol_interface;
1911 len_dp = efi_dp_instance_size(dp);
1912 /*
1913 * This handle can only be a better fit
1914 * if its device path length is longer than the best fit and
1915 * if its device path length is shorter of equal the searched
1916 * device path.
1917 */
1918 if (len_dp <= len_best || len_dp > len)
1919 continue;
1920 /* Check if dp is a subpath of device_path */
1921 if (memcmp(*device_path, dp, len_dp))
1922 continue;
1923 if (!device) {
1924 ret = EFI_INVALID_PARAMETER;
1925 goto out;
1926 }
1927 *device = handles[i];
1928 len_best = len_dp;
1929 }
1930 if (len_best) {
1931 remainder = (u8 *)*device_path + len_best;
1932 *device_path = (struct efi_device_path *)remainder;
1933 ret = EFI_SUCCESS;
1934 } else {
1935 ret = EFI_NOT_FOUND;
1936 }
1937out:
1938 return EFI_EXIT(ret);
1939}
1940
1941/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001942 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001943 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001944 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1945 * callers obligation to update the memory type as needed.
1946 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001947 * @file_path: the path of the image to load
1948 * @buffer: buffer containing the loaded image
1949 * @size: size of the loaded image
1950 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001951 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001952static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001953efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001954 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001955{
Rob Clark857a1222017-09-13 18:05:35 -04001956 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001957 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001958 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001959 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001960
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001961 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001962 f = efi_file_from_path(file_path);
1963 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001964 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001965
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001966 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001967 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001968 goto error;
1969
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001970 /*
1971 * When reading the file we do not yet know if it contains an
1972 * application, a boottime driver, or a runtime driver. So here we
1973 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1974 * update the reservation according to the image type.
1975 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001976 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1977 EFI_BOOT_SERVICES_DATA,
1978 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001979 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001980 ret = EFI_OUT_OF_RESOURCES;
1981 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001982 }
1983
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001984 /* Read file */
1985 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1986 if (ret != EFI_SUCCESS)
1987 efi_free_pages(addr, efi_size_in_pages(bs));
1988 *buffer = (void *)(uintptr_t)addr;
1989 *size = bs;
1990error:
1991 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001992 return ret;
1993}
1994
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001995/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001996 * efi_load_image_from_path() - load an image using a file path
1997 *
1998 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1999 * callers obligation to update the memory type as needed.
2000 *
2001 * @boot_policy: true for request originating from the boot manager
2002 * @file_path: the path of the image to load
2003 * @buffer: buffer containing the loaded image
2004 * @size: size of the loaded image
2005 * Return: status code
2006 */
2007static
2008efi_status_t efi_load_image_from_path(bool boot_policy,
2009 struct efi_device_path *file_path,
2010 void **buffer, efi_uintn_t *size)
2011{
2012 efi_handle_t device;
2013 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002014 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002015 struct efi_load_file_protocol *load_file_protocol = NULL;
2016 efi_uintn_t buffer_size;
2017 uint64_t addr, pages;
2018 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002019 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002020
2021 /* In case of failure nothing is returned */
2022 *buffer = NULL;
2023 *size = 0;
2024
2025 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002026 device = efi_dp_find_obj(dp, NULL, &rem);
2027 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2028 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002029 if (ret == EFI_SUCCESS)
2030 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002031
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002032 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002033 if (ret == EFI_SUCCESS) {
2034 guid = &efi_guid_load_file_protocol;
2035 } else if (!boot_policy) {
2036 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002037 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002038 }
2039 if (ret != EFI_SUCCESS)
2040 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002041 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002042 if (ret != EFI_SUCCESS)
2043 return EFI_NOT_FOUND;
2044 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002045 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002046 ret = EFI_CALL(load_file_protocol->load_file(
2047 load_file_protocol, rem, boot_policy,
2048 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002049 if (ret != EFI_BUFFER_TOO_SMALL)
2050 goto out;
2051 pages = efi_size_in_pages(buffer_size);
2052 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2053 pages, &addr);
2054 if (ret != EFI_SUCCESS) {
2055 ret = EFI_OUT_OF_RESOURCES;
2056 goto out;
2057 }
2058 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002059 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002060 &buffer_size, (void *)(uintptr_t)addr));
2061 if (ret != EFI_SUCCESS)
2062 efi_free_pages(addr, pages);
2063out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002064 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002065 if (ret == EFI_SUCCESS) {
2066 *buffer = (void *)(uintptr_t)addr;
2067 *size = buffer_size;
2068 }
2069
2070 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002071}
2072
2073/**
Mario Six8fac2912018-07-10 08:40:17 +02002074 * efi_load_image() - load an EFI image into memory
2075 * @boot_policy: true for request originating from the boot manager
2076 * @parent_image: the caller's image handle
2077 * @file_path: the path of the image to load
2078 * @source_buffer: memory location from which the image is installed
2079 * @source_size: size of the memory area from which the image is installed
2080 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002081 *
2082 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002083 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002084 * See the Unified Extensible Firmware Interface (UEFI) specification
2085 * for details.
2086 *
Mario Six8fac2912018-07-10 08:40:17 +02002087 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002088 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002089efi_status_t EFIAPI efi_load_image(bool boot_policy,
2090 efi_handle_t parent_image,
2091 struct efi_device_path *file_path,
2092 void *source_buffer,
2093 efi_uintn_t source_size,
2094 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002095{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002096 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002097 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002098 struct efi_loaded_image_obj **image_obj =
2099 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002100 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002101 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002102
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002103 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002104 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002105
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002106 if (!image_handle || (!source_buffer && !file_path) ||
2107 !efi_search_obj(parent_image) ||
2108 /* The parent image handle must refer to a loaded image */
2109 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002110 ret = EFI_INVALID_PARAMETER;
2111 goto error;
2112 }
Rob Clark857a1222017-09-13 18:05:35 -04002113
2114 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002115 ret = efi_load_image_from_path(boot_policy, file_path,
2116 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002117 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002118 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002119 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002120 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002121 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002122 /* split file_path which contains both the device and file parts */
2123 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002124 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002125 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002126 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002127 if (!source_buffer)
2128 /* Release buffer to which file was loaded */
2129 efi_free_pages((uintptr_t)dest_buffer,
2130 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002131 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002132 info->system_table = &systab;
2133 info->parent_handle = parent_image;
2134 } else {
2135 /* The image is invalid. Release all associated resources. */
2136 efi_delete_handle(*image_handle);
2137 *image_handle = NULL;
2138 free(info);
2139 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002140error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002141 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002142}
2143
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002144/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002145 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2146 */
2147static void efi_exit_caches(void)
2148{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002149#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002150 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002151 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2152 * caches are enabled.
2153 *
2154 * TODO:
2155 * According to the UEFI spec caches that can be managed via CP15
2156 * operations should be enabled. Caches requiring platform information
2157 * to manage should be disabled. This should not happen in
2158 * ExitBootServices() but before invoking any UEFI binary is invoked.
2159 *
2160 * We want to keep the current workaround while GRUB prior to version
2161 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002162 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002163 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002164#endif
2165}
2166
2167/**
Mario Six8fac2912018-07-10 08:40:17 +02002168 * efi_exit_boot_services() - stop all boot services
2169 * @image_handle: handle of the loaded image
2170 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002171 *
2172 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002173 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002174 * See the Unified Extensible Firmware Interface (UEFI) specification
2175 * for details.
2176 *
Mario Six8fac2912018-07-10 08:40:17 +02002177 * All timer events are disabled. For exit boot services events the
2178 * notification function is called. The boot services are disabled in the
2179 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002180 *
Mario Six8fac2912018-07-10 08:40:17 +02002181 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002182 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002183static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002184 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002185{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002186 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002187 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002188
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002189 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002190
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002191 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002192 if (map_key != efi_memory_map_key) {
2193 ret = EFI_INVALID_PARAMETER;
2194 goto out;
2195 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002196
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002197 /* Check if ExitBootServices has already been called */
2198 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002199 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002200
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002201 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2202 list_for_each_entry(evt, &efi_events, link) {
2203 if (evt->group &&
2204 !guidcmp(evt->group,
2205 &efi_guid_event_group_before_exit_boot_services)) {
2206 efi_signal_event(evt);
2207 break;
2208 }
2209 }
2210
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002211 /* Stop all timer related activities */
2212 timers_enabled = false;
2213
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002214 /* Add related events to the event group */
2215 list_for_each_entry(evt, &efi_events, link) {
2216 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2217 evt->group = &efi_guid_event_group_exit_boot_services;
2218 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002219 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002220 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002221 if (evt->group &&
2222 !guidcmp(evt->group,
2223 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002224 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002225 break;
2226 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002227 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002228
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002229 /* Make sure that notification functions are not called anymore */
2230 efi_tpl = TPL_HIGH_LEVEL;
2231
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002232 /* Notify variable services */
2233 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002234
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002235 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2236 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2237 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2238 list_del(&evt->link);
2239 }
2240
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002241 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002242 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002243 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002244 udc_disconnect();
2245 board_quiesce_devices();
2246 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2247 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002248
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002249 /* Patch out unsupported runtime function */
2250 efi_runtime_detach();
2251
Alexander Graffa5246b2018-11-15 21:23:47 +01002252 /* Fix up caches for EFI payloads if necessary */
2253 efi_exit_caches();
2254
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002255 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002256 systab.con_in_handle = NULL;
2257 systab.con_in = NULL;
2258 systab.con_out_handle = NULL;
2259 systab.con_out = NULL;
2260 systab.stderr_handle = NULL;
2261 systab.std_err = NULL;
2262 systab.boottime = NULL;
2263
2264 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002265 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002266
Alexander Grafc15d9212016-03-04 01:09:59 +01002267 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002268 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002269 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002270out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002271 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2272 if (ret != EFI_SUCCESS)
2273 efi_tcg2_notify_exit_boot_services_failed();
2274 }
2275
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002276 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002277}
2278
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002279/**
Mario Six8fac2912018-07-10 08:40:17 +02002280 * efi_get_next_monotonic_count() - get next value of the counter
2281 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002282 *
2283 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002284 *
2285 * See the Unified Extensible Firmware Interface (UEFI) specification for
2286 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002287 *
Mario Six8fac2912018-07-10 08:40:17 +02002288 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002289 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002290static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2291{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002292 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002293 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002294
Alexander Grafc15d9212016-03-04 01:09:59 +01002295 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002296 if (!count) {
2297 ret = EFI_INVALID_PARAMETER;
2298 goto out;
2299 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002300 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002301 ret = EFI_SUCCESS;
2302out:
2303 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002304}
2305
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002306/**
Mario Six8fac2912018-07-10 08:40:17 +02002307 * efi_stall() - sleep
2308 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002309 *
Mario Six8fac2912018-07-10 08:40:17 +02002310 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002311 *
Mario Six8fac2912018-07-10 08:40:17 +02002312 * See the Unified Extensible Firmware Interface (UEFI) specification for
2313 * details.
2314 *
2315 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002316 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002317static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2318{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002319 u64 end_tick;
2320
Alexander Grafc15d9212016-03-04 01:09:59 +01002321 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002322
2323 end_tick = get_ticks() + usec_to_tick(microseconds);
2324 while (get_ticks() < end_tick)
2325 efi_timer_check();
2326
Alexander Grafc15d9212016-03-04 01:09:59 +01002327 return EFI_EXIT(EFI_SUCCESS);
2328}
2329
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002330/**
Mario Six8fac2912018-07-10 08:40:17 +02002331 * efi_set_watchdog_timer() - reset the watchdog timer
2332 * @timeout: seconds before reset by watchdog
2333 * @watchdog_code: code to be logged when resetting
2334 * @data_size: size of buffer in bytes
2335 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002336 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002337 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002338 *
Mario Six8fac2912018-07-10 08:40:17 +02002339 * See the Unified Extensible Firmware Interface (UEFI) specification for
2340 * details.
2341 *
2342 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002343 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002344static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2345 uint64_t watchdog_code,
2346 unsigned long data_size,
2347 uint16_t *watchdog_data)
2348{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002349 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002350 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002351 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002352}
2353
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002354/**
Mario Six8fac2912018-07-10 08:40:17 +02002355 * efi_close_protocol() - close a protocol
2356 * @handle: handle on which the protocol shall be closed
2357 * @protocol: GUID of the protocol to close
2358 * @agent_handle: handle of the driver
2359 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002360 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002361 * This is the function implementing the CloseProtocol service is for internal
2362 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002363 *
Mario Six8fac2912018-07-10 08:40:17 +02002364 * See the Unified Extensible Firmware Interface (UEFI) specification for
2365 * details.
2366 *
2367 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002368 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002369efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2370 efi_handle_t agent_handle,
2371 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002372{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002373 struct efi_handler *handler;
2374 struct efi_open_protocol_info_item *item;
2375 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002376 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002377
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002378 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002379 (controller_handle && !efi_search_obj(controller_handle)))
2380 return EFI_INVALID_PARAMETER;
2381 ret = efi_search_protocol(handle, protocol, &handler);
2382 if (ret != EFI_SUCCESS)
2383 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002384
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002385 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002386 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2387 if (item->info.agent_handle == agent_handle &&
2388 item->info.controller_handle == controller_handle) {
2389 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002390 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002391 }
2392 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002393
2394 return ret;
2395}
2396
2397/**
2398 * efi_close_protocol_ext() - close a protocol
2399 * @handle: handle on which the protocol shall be closed
2400 * @protocol: GUID of the protocol to close
2401 * @agent_handle: handle of the driver
2402 * @controller_handle: handle of the controller
2403 *
2404 * This function implements the CloseProtocol service.
2405 *
2406 * See the Unified Extensible Firmware Interface (UEFI) specification for
2407 * details.
2408 *
2409 * Return: status code
2410 */
2411static efi_status_t EFIAPI
2412efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2413 efi_handle_t agent_handle,
2414 efi_handle_t controller_handle)
2415{
2416 efi_status_t ret;
2417
2418 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2419 controller_handle);
2420
2421 ret = efi_close_protocol(handle, protocol,
2422 agent_handle, controller_handle);
2423
2424 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002425}
2426
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002427/**
Mario Six8fac2912018-07-10 08:40:17 +02002428 * efi_open_protocol_information() - provide information about then open status
2429 * of a protocol on a handle
2430 * @handle: handle for which the information shall be retrieved
2431 * @protocol: GUID of the protocol
2432 * @entry_buffer: buffer to receive the open protocol information
2433 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002434 *
2435 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002436 *
2437 * See the Unified Extensible Firmware Interface (UEFI) specification for
2438 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002439 *
Mario Six8fac2912018-07-10 08:40:17 +02002440 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002441 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002442static efi_status_t EFIAPI efi_open_protocol_information(
2443 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002444 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002445 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002446{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002447 unsigned long buffer_size;
2448 unsigned long count;
2449 struct efi_handler *handler;
2450 struct efi_open_protocol_info_item *item;
2451 efi_status_t r;
2452
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002453 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002454 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002455
2456 /* Check parameters */
2457 if (!entry_buffer) {
2458 r = EFI_INVALID_PARAMETER;
2459 goto out;
2460 }
2461 r = efi_search_protocol(handle, protocol, &handler);
2462 if (r != EFI_SUCCESS)
2463 goto out;
2464
2465 /* Count entries */
2466 count = 0;
2467 list_for_each_entry(item, &handler->open_infos, link) {
2468 if (item->info.open_count)
2469 ++count;
2470 }
2471 *entry_count = count;
2472 *entry_buffer = NULL;
2473 if (!count) {
2474 r = EFI_SUCCESS;
2475 goto out;
2476 }
2477
2478 /* Copy entries */
2479 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002480 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002481 (void **)entry_buffer);
2482 if (r != EFI_SUCCESS)
2483 goto out;
2484 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2485 if (item->info.open_count)
2486 (*entry_buffer)[--count] = item->info;
2487 }
2488out:
2489 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002490}
2491
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002492/**
Mario Six8fac2912018-07-10 08:40:17 +02002493 * efi_protocols_per_handle() - get protocols installed on a handle
2494 * @handle: handle for which the information is retrieved
2495 * @protocol_buffer: buffer with protocol GUIDs
2496 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002497 *
2498 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002499 *
Mario Six8fac2912018-07-10 08:40:17 +02002500 * See the Unified Extensible Firmware Interface (UEFI) specification for
2501 * details.
2502 *
2503 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002504 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002505static efi_status_t EFIAPI efi_protocols_per_handle(
2506 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002507 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002508{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002509 unsigned long buffer_size;
2510 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002511 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002512 efi_status_t r;
2513
Alexander Grafc15d9212016-03-04 01:09:59 +01002514 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2515 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002516
2517 if (!handle || !protocol_buffer || !protocol_buffer_count)
2518 return EFI_EXIT(EFI_INVALID_PARAMETER);
2519
2520 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002521 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002522
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002523 efiobj = efi_search_obj(handle);
2524 if (!efiobj)
2525 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002526
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002527 /* Count protocols */
2528 list_for_each(protocol_handle, &efiobj->protocols) {
2529 ++*protocol_buffer_count;
2530 }
2531
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002532 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002533 if (*protocol_buffer_count) {
2534 size_t j = 0;
2535
2536 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002537 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002538 (void **)protocol_buffer);
2539 if (r != EFI_SUCCESS)
2540 return EFI_EXIT(r);
2541 list_for_each(protocol_handle, &efiobj->protocols) {
2542 struct efi_handler *protocol;
2543
2544 protocol = list_entry(protocol_handle,
2545 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002546 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002547 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002548 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002549 }
2550
2551 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002552}
2553
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002554efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2555 const efi_guid_t *protocol, void *search_key,
2556 efi_uintn_t *no_handles, efi_handle_t **buffer)
2557{
2558 efi_status_t r;
2559 efi_uintn_t buffer_size = 0;
2560
2561 if (!no_handles || !buffer) {
2562 r = EFI_INVALID_PARAMETER;
2563 goto out;
2564 }
2565 *no_handles = 0;
2566 *buffer = NULL;
2567 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2568 *buffer);
2569 if (r != EFI_BUFFER_TOO_SMALL)
2570 goto out;
2571 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2572 (void **)buffer);
2573 if (r != EFI_SUCCESS)
2574 goto out;
2575 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2576 *buffer);
2577 if (r == EFI_SUCCESS)
2578 *no_handles = buffer_size / sizeof(efi_handle_t);
2579out:
2580 return r;
2581}
2582
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002583/**
Mario Six8fac2912018-07-10 08:40:17 +02002584 * efi_locate_handle_buffer() - locate handles implementing a protocol
2585 * @search_type: selection criterion
2586 * @protocol: GUID of the protocol
2587 * @search_key: registration key
2588 * @no_handles: number of returned handles
2589 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002590 *
2591 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002592 *
Mario Six8fac2912018-07-10 08:40:17 +02002593 * See the Unified Extensible Firmware Interface (UEFI) specification for
2594 * details.
2595 *
2596 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002597 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002598efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002599 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002600 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002601 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002602{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002603 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002604
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002605 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002606 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002607
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002608 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2609 no_handles, buffer);
2610
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002611 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002612}
2613
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002614/**
Mario Six8fac2912018-07-10 08:40:17 +02002615 * efi_locate_protocol() - find an interface implementing a protocol
2616 * @protocol: GUID of the protocol
2617 * @registration: registration key passed to the notification function
2618 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002619 *
2620 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002621 *
2622 * See the Unified Extensible Firmware Interface (UEFI) specification for
2623 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002624 *
Mario Six8fac2912018-07-10 08:40:17 +02002625 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002626 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002627static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002628 void *registration,
2629 void **protocol_interface)
2630{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002631 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002632 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002633 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002634
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002635 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002636
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002637 /*
2638 * The UEFI spec explicitly requires a protocol even if a registration
2639 * key is provided. This differs from the logic in LocateHandle().
2640 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002641 if (!protocol || !protocol_interface)
2642 return EFI_EXIT(EFI_INVALID_PARAMETER);
2643
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002644 if (registration) {
2645 struct efi_register_notify_event *event;
2646 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002647
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002648 event = efi_check_register_notify_event(registration);
2649 if (!event)
2650 return EFI_EXIT(EFI_INVALID_PARAMETER);
2651 /*
2652 * The UEFI spec requires to return EFI_NOT_FOUND if no
2653 * protocol instance matches protocol and registration.
2654 * So let's do the same for a mismatch between protocol and
2655 * registration.
2656 */
2657 if (guidcmp(&event->protocol, protocol))
2658 goto not_found;
2659 if (list_empty(&event->handles))
2660 goto not_found;
2661 handle = list_first_entry(&event->handles,
2662 struct efi_protocol_notification,
2663 link);
2664 efiobj = handle->handle;
2665 list_del(&handle->link);
2666 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002667 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002668 if (ret == EFI_SUCCESS)
2669 goto found;
2670 } else {
2671 list_for_each_entry(efiobj, &efi_obj_list, link) {
2672 ret = efi_search_protocol(efiobj, protocol, &handler);
2673 if (ret == EFI_SUCCESS)
2674 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002675 }
2676 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002677not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002678 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002679 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002680found:
2681 *protocol_interface = handler->protocol_interface;
2682 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002683}
2684
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002685/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002686 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002687 * interfaces
2688 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002689 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002690 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002691 * Core functionality of efi_install_multiple_protocol_interfaces
2692 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002693 *
Mario Six8fac2912018-07-10 08:40:17 +02002694 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002695 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002696static efi_status_t EFIAPI
2697efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2698 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002699{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002700 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002701 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002702 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002703 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002704 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002705 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002706
2707 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002708 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002709
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002710 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002711 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002712 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002713 if (!protocol)
2714 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002715 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002716 /* Check that a device path has not been installed before */
2717 if (!guidcmp(protocol, &efi_guid_device_path)) {
2718 struct efi_device_path *dp = protocol_interface;
2719
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002720 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2721 &old_handle));
2722 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002723 dp->type == DEVICE_PATH_TYPE_END) {
2724 EFI_PRINT("Path %pD already installed\n",
2725 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002726 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002727 break;
2728 }
2729 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002730 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2731 EFI_NATIVE_INTERFACE,
2732 protocol_interface));
2733 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002734 break;
2735 i++;
2736 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002737 if (ret == EFI_SUCCESS)
2738 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002739
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002740 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002741 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002742 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2743 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002744 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002745 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002746 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002747
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002748out:
2749 efi_va_end(argptr_copy);
2750 return ret;
2751
Alexander Grafc15d9212016-03-04 01:09:59 +01002752}
2753
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002754/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002755 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2756 * interfaces
2757 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002758 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2759 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002760 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002761 *
2762 * This is the function for internal usage in U-Boot. For the API function
2763 * implementing the InstallMultipleProtocol service see
2764 * efi_install_multiple_protocol_interfaces_ext()
2765 *
2766 * Return: status code
2767 */
2768efi_status_t EFIAPI
2769efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2770{
2771 efi_status_t ret;
2772 efi_va_list argptr;
2773
2774 efi_va_start(argptr, handle);
2775 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2776 efi_va_end(argptr);
2777 return ret;
2778}
2779
2780/**
2781 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2782 * interfaces
2783 * @handle: handle on which the protocol interfaces shall be installed
2784 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2785 * interfaces
2786 *
2787 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002788 *
Mario Six8fac2912018-07-10 08:40:17 +02002789 * See the Unified Extensible Firmware Interface (UEFI) specification for
2790 * details.
2791 *
2792 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002793 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002794static efi_status_t EFIAPI
2795efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002796{
2797 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002798 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002799 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002800
2801 efi_va_start(argptr, handle);
2802 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2803 efi_va_end(argptr);
2804 return EFI_EXIT(ret);
2805}
2806
2807/**
2808 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2809 * multiple protocol
2810 * interfaces
2811 * @handle: handle from which the protocol interfaces shall be removed
2812 * @argptr: va_list of args
2813 *
2814 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2815 * Must not be called directly
2816 *
2817 * Return: status code
2818 */
2819static efi_status_t EFIAPI
2820efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2821 efi_va_list argptr)
2822{
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002823 const efi_guid_t *protocol, *next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002824 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002825 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002826 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002827 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002828
2829 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002830 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002831
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002832 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002833 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002834 for (;;) {
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002835 /*
2836 * If efi_uninstall_protocol() fails we need to be able to
2837 * reinstall the previously uninstalled protocols on the same
2838 * handle.
2839 * Instead of calling efi_uninstall_protocol(...,..., false)
2840 * and potentially removing the handle, only allow the handle
2841 * removal on the last protocol that we requested to uninstall.
2842 * That way we can preserve the handle in case the latter fails
2843 */
2844 bool preserve = true;
2845
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002846 if (!protocol)
2847 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002848 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002849 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2850 if (!next_protocol)
2851 preserve = false;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002852 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002853 protocol_interface, preserve);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002854 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002855 break;
2856 i++;
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002857 protocol = next_protocol;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002858 }
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03002859 if (ret == EFI_SUCCESS)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002860 goto out;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002861
2862 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002863 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002864 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2865 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002866 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2867 EFI_NATIVE_INTERFACE,
2868 protocol_interface));
2869 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002870 /*
2871 * If any errors are generated while the protocol interfaces are being
2872 * uninstalled, then the protocols uninstalled prior to the error will
2873 * be reinstalled using InstallProtocolInterface() and the status code
2874 * EFI_INVALID_PARAMETER is returned.
2875 */
2876 ret = EFI_INVALID_PARAMETER;
2877
2878out:
2879 efi_va_end(argptr_copy);
2880 return ret;
2881}
2882
2883/**
2884 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2885 * interfaces
2886 * @handle: handle from which the protocol interfaces shall be removed
2887 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2888 * interfaces
2889 *
2890 * This function implements the UninstallMultipleProtocolInterfaces service.
2891 *
2892 * This is the function for internal usage in U-Boot. For the API function
2893 * implementing the UninstallMultipleProtocolInterfaces service see
2894 * efi_uninstall_multiple_protocol_interfaces_ext()
2895 *
2896 * Return: status code
2897 */
2898efi_status_t EFIAPI
2899efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2900{
2901 efi_status_t ret;
2902 efi_va_list argptr;
2903
2904 efi_va_start(argptr, handle);
2905 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002906 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002907 return ret;
2908}
2909
2910/**
2911 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2912 * interfaces
2913 * @handle: handle from which the protocol interfaces shall be removed
2914 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2915 * interfaces
2916 *
2917 * This function implements the UninstallMultipleProtocolInterfaces service.
2918 *
2919 * See the Unified Extensible Firmware Interface (UEFI) specification for
2920 * details.
2921 *
2922 * Return: status code
2923 */
2924static efi_status_t EFIAPI
2925efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2926{
2927 EFI_ENTRY("%p", handle);
2928 efi_status_t ret;
2929 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002930
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002931 efi_va_start(argptr, handle);
2932 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2933 efi_va_end(argptr);
2934 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002935}
2936
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002937/**
Mario Six8fac2912018-07-10 08:40:17 +02002938 * efi_calculate_crc32() - calculate cyclic redundancy code
2939 * @data: buffer with data
2940 * @data_size: size of buffer in bytes
2941 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002942 *
2943 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002944 *
2945 * See the Unified Extensible Firmware Interface (UEFI) specification for
2946 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002947 *
Mario Six8fac2912018-07-10 08:40:17 +02002948 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002949 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002950static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2951 efi_uintn_t data_size,
2952 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002953{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002954 efi_status_t ret = EFI_SUCCESS;
2955
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002956 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002957 if (!data || !data_size || !crc32_p) {
2958 ret = EFI_INVALID_PARAMETER;
2959 goto out;
2960 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002961 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002962out:
2963 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002964}
2965
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002966/**
Mario Six8fac2912018-07-10 08:40:17 +02002967 * efi_copy_mem() - copy memory
2968 * @destination: destination of the copy operation
2969 * @source: source of the copy operation
2970 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002971 *
2972 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002973 *
Mario Six8fac2912018-07-10 08:40:17 +02002974 * See the Unified Extensible Firmware Interface (UEFI) specification for
2975 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002976 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002977static void EFIAPI efi_copy_mem(void *destination, const void *source,
2978 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002979{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002980 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002981 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002982 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002983}
2984
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002985/**
Mario Six8fac2912018-07-10 08:40:17 +02002986 * efi_set_mem() - Fill memory with a byte value.
2987 * @buffer: buffer to fill
2988 * @size: size of buffer in bytes
2989 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002990 *
2991 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002992 *
Mario Six8fac2912018-07-10 08:40:17 +02002993 * See the Unified Extensible Firmware Interface (UEFI) specification for
2994 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002995 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002996static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002997{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002998 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002999 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02003000 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01003001}
3002
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003003/**
Mario Six8fac2912018-07-10 08:40:17 +02003004 * efi_protocol_open() - open protocol interface on a handle
3005 * @handler: handler of a protocol
3006 * @protocol_interface: interface implementing the protocol
3007 * @agent_handle: handle of the driver
3008 * @controller_handle: handle of the controller
3009 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003010 *
Mario Six8fac2912018-07-10 08:40:17 +02003011 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003012 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01003013efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003014 struct efi_handler *handler,
3015 void **protocol_interface, void *agent_handle,
3016 void *controller_handle, uint32_t attributes)
3017{
3018 struct efi_open_protocol_info_item *item;
3019 struct efi_open_protocol_info_entry *match = NULL;
3020 bool opened_by_driver = false;
3021 bool opened_exclusive = false;
3022
3023 /* If there is no agent, only return the interface */
3024 if (!agent_handle)
3025 goto out;
3026
3027 /* For TEST_PROTOCOL ignore interface attribute */
3028 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3029 *protocol_interface = NULL;
3030
3031 /*
3032 * Check if the protocol is already opened by a driver with the same
3033 * attributes or opened exclusively
3034 */
3035 list_for_each_entry(item, &handler->open_infos, link) {
3036 if (item->info.agent_handle == agent_handle) {
3037 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3038 (item->info.attributes == attributes))
3039 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003040 } else {
3041 if (item->info.attributes &
3042 EFI_OPEN_PROTOCOL_BY_DRIVER)
3043 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003044 }
3045 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3046 opened_exclusive = true;
3047 }
3048
3049 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003050 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3051 if (opened_exclusive)
3052 return EFI_ACCESS_DENIED;
3053 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3054 if (opened_exclusive || opened_by_driver)
3055 return EFI_ACCESS_DENIED;
3056 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003057
3058 /* Prepare exclusive opening */
3059 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3060 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003061disconnect_next:
3062 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003063 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003064 efi_status_t ret;
3065
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003066 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003067 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3068 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003069 item->info.controller_handle,
3070 item->info.agent_handle,
3071 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003072 if (ret == EFI_SUCCESS)
3073 /*
3074 * Child controllers may have been
3075 * removed from the open_infos list. So
3076 * let's restart the loop.
3077 */
3078 goto disconnect_next;
3079 else
3080 opened_by_driver = true;
3081 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003082 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003083 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003084 if (opened_by_driver)
3085 return EFI_ACCESS_DENIED;
3086 }
3087
3088 /* Find existing entry */
3089 list_for_each_entry(item, &handler->open_infos, link) {
3090 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003091 item->info.controller_handle == controller_handle &&
3092 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003093 match = &item->info;
3094 }
3095 /* None found, create one */
3096 if (!match) {
3097 match = efi_create_open_info(handler);
3098 if (!match)
3099 return EFI_OUT_OF_RESOURCES;
3100 }
3101
3102 match->agent_handle = agent_handle;
3103 match->controller_handle = controller_handle;
3104 match->attributes = attributes;
3105 match->open_count++;
3106
3107out:
3108 /* For TEST_PROTOCOL ignore interface attribute. */
3109 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3110 *protocol_interface = handler->protocol_interface;
3111
3112 return EFI_SUCCESS;
3113}
3114
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003115/**
Mario Six8fac2912018-07-10 08:40:17 +02003116 * efi_open_protocol() - open protocol interface on a handle
3117 * @handle: handle on which the protocol shall be opened
3118 * @protocol: GUID of the protocol
3119 * @protocol_interface: interface implementing the protocol
3120 * @agent_handle: handle of the driver
3121 * @controller_handle: handle of the controller
3122 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003123 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003124 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003125 *
Mario Six8fac2912018-07-10 08:40:17 +02003126 * See the Unified Extensible Firmware Interface (UEFI) specification for
3127 * details.
3128 *
3129 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003130 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003131static efi_status_t EFIAPI efi_open_protocol
3132 (efi_handle_t handle, const efi_guid_t *protocol,
3133 void **protocol_interface, efi_handle_t agent_handle,
3134 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003135{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003136 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003137 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003138
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003139 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003140 protocol_interface, agent_handle, controller_handle,
3141 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003142
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003143 if (!handle || !protocol ||
3144 (!protocol_interface && attributes !=
3145 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003146 goto out;
3147 }
3148
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003149 switch (attributes) {
3150 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3151 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3152 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3153 break;
3154 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3155 if (controller_handle == handle)
3156 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003157 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003158 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3159 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003160 /* Check that the controller handle is valid */
3161 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003162 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003163 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003164 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003165 /* Check that the agent handle is valid */
3166 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003167 goto out;
3168 break;
3169 default:
3170 goto out;
3171 }
3172
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003173 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003174 switch (r) {
3175 case EFI_SUCCESS:
3176 break;
3177 case EFI_NOT_FOUND:
3178 r = EFI_UNSUPPORTED;
3179 goto out;
3180 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003181 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003182 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003183
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003184 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3185 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003186out:
3187 return EFI_EXIT(r);
3188}
3189
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003190/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003191 * efi_start_image() - call the entry point of an image
3192 * @image_handle: handle of the image
3193 * @exit_data_size: size of the buffer
3194 * @exit_data: buffer to receive the exit data of the called image
3195 *
3196 * This function implements the StartImage service.
3197 *
3198 * See the Unified Extensible Firmware Interface (UEFI) specification for
3199 * details.
3200 *
3201 * Return: status code
3202 */
3203efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3204 efi_uintn_t *exit_data_size,
3205 u16 **exit_data)
3206{
3207 struct efi_loaded_image_obj *image_obj =
3208 (struct efi_loaded_image_obj *)image_handle;
3209 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003210 void *info;
3211 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003212 efi_status_t exit_status;
3213 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003214
3215 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3216
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003217 if (!efi_search_obj(image_handle))
3218 return EFI_EXIT(EFI_INVALID_PARAMETER);
3219
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003220 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003221 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3222 return EFI_EXIT(EFI_INVALID_PARAMETER);
3223
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003224 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3225 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3226
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003227 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3228 &info, NULL, NULL,
3229 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3230 if (ret != EFI_SUCCESS)
3231 return EFI_EXIT(EFI_INVALID_PARAMETER);
3232
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003233 image_obj->exit_data_size = exit_data_size;
3234 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003235 image_obj->exit_status = &exit_status;
3236 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003237
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003238 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3239 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003240 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003241 if (ret == EFI_SECURITY_VIOLATION) {
3242 /*
3243 * TCG2 Protocol is installed but no TPM device found,
3244 * this is not expected.
3245 */
3246 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003247 }
3248 }
3249 }
3250
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003251 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003252 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003253 /*
3254 * We called the entry point of the child image with EFI_CALL
3255 * in the lines below. The child image called the Exit() boot
3256 * service efi_exit() which executed the long jump that brought
3257 * us to the current line. This implies that the second half
3258 * of the EFI_CALL macro has not been executed.
3259 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003260#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003261 /*
3262 * efi_exit() called efi_restore_gd(). We have to undo this
3263 * otherwise __efi_entry_check() will put the wrong value into
3264 * app_gd.
3265 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003266 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003267#endif
3268 /*
3269 * To get ready to call EFI_EXIT below we have to execute the
3270 * missed out steps of EFI_CALL.
3271 */
3272 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003273 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003274 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003275 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003276 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003277 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003278 }
3279
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003280 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003281 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003282 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003283 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3284
3285 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003286 * Control is returned from a started UEFI image either by calling
3287 * Exit() (where exit data can be provided) or by simply returning from
3288 * the entry point. In the latter case call Exit() on behalf of the
3289 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003290 */
3291 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3292}
3293
3294/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003295 * efi_delete_image() - delete loaded image from memory)
3296 *
3297 * @image_obj: handle of the loaded image
3298 * @loaded_image_protocol: loaded image protocol
3299 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003300static efi_status_t efi_delete_image
3301 (struct efi_loaded_image_obj *image_obj,
3302 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003303{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003304 struct efi_object *efiobj;
3305 efi_status_t r, ret = EFI_SUCCESS;
3306
3307close_next:
3308 list_for_each_entry(efiobj, &efi_obj_list, link) {
3309 struct efi_handler *protocol;
3310
3311 list_for_each_entry(protocol, &efiobj->protocols, link) {
3312 struct efi_open_protocol_info_item *info;
3313
3314 list_for_each_entry(info, &protocol->open_infos, link) {
3315 if (info->info.agent_handle !=
3316 (efi_handle_t)image_obj)
3317 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003318 r = efi_close_protocol(
3319 efiobj, &protocol->guid,
3320 info->info.agent_handle,
3321 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003322 if (r != EFI_SUCCESS)
3323 ret = r;
3324 /*
3325 * Closing protocols may results in further
3326 * items being deleted. To play it safe loop
3327 * over all elements again.
3328 */
3329 goto close_next;
3330 }
3331 }
3332 }
3333
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003334 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3335 efi_size_in_pages(loaded_image_protocol->image_size));
3336 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003337
3338 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003339}
3340
3341/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003342 * efi_unload_image() - unload an EFI image
3343 * @image_handle: handle of the image to be unloaded
3344 *
3345 * This function implements the UnloadImage service.
3346 *
3347 * See the Unified Extensible Firmware Interface (UEFI) specification for
3348 * details.
3349 *
3350 * Return: status code
3351 */
3352efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3353{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003354 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003355 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003356 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003357
3358 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003359
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003360 efiobj = efi_search_obj(image_handle);
3361 if (!efiobj) {
3362 ret = EFI_INVALID_PARAMETER;
3363 goto out;
3364 }
3365 /* Find the loaded image protocol */
3366 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3367 (void **)&loaded_image_protocol,
3368 NULL, NULL,
3369 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3370 if (ret != EFI_SUCCESS) {
3371 ret = EFI_INVALID_PARAMETER;
3372 goto out;
3373 }
3374 switch (efiobj->type) {
3375 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3376 /* Call the unload function */
3377 if (!loaded_image_protocol->unload) {
3378 ret = EFI_UNSUPPORTED;
3379 goto out;
3380 }
3381 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3382 if (ret != EFI_SUCCESS)
3383 goto out;
3384 break;
3385 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3386 break;
3387 default:
3388 ret = EFI_INVALID_PARAMETER;
3389 goto out;
3390 }
3391 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3392 loaded_image_protocol);
3393out:
3394 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003395}
3396
3397/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003398 * efi_update_exit_data() - fill exit data parameters of StartImage()
3399 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003400 * @image_obj: image handle
3401 * @exit_data_size: size of the exit data buffer
3402 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003403 * Return: status code
3404 */
3405static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3406 efi_uintn_t exit_data_size,
3407 u16 *exit_data)
3408{
3409 efi_status_t ret;
3410
3411 /*
3412 * If exit_data is not provided to StartImage(), exit_data_size must be
3413 * ignored.
3414 */
3415 if (!image_obj->exit_data)
3416 return EFI_SUCCESS;
3417 if (image_obj->exit_data_size)
3418 *image_obj->exit_data_size = exit_data_size;
3419 if (exit_data_size && exit_data) {
3420 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3421 exit_data_size,
3422 (void **)image_obj->exit_data);
3423 if (ret != EFI_SUCCESS)
3424 return ret;
3425 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3426 } else {
3427 image_obj->exit_data = NULL;
3428 }
3429 return EFI_SUCCESS;
3430}
3431
3432/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003433 * efi_exit() - leave an EFI application or driver
3434 * @image_handle: handle of the application or driver that is exiting
3435 * @exit_status: status code
3436 * @exit_data_size: size of the buffer in bytes
3437 * @exit_data: buffer with data describing an error
3438 *
3439 * This function implements the Exit service.
3440 *
3441 * See the Unified Extensible Firmware Interface (UEFI) specification for
3442 * details.
3443 *
3444 * Return: status code
3445 */
3446static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3447 efi_status_t exit_status,
3448 efi_uintn_t exit_data_size,
3449 u16 *exit_data)
3450{
3451 /*
3452 * TODO: We should call the unload procedure of the loaded
3453 * image protocol.
3454 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003455 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003456 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003457 struct efi_loaded_image_obj *image_obj =
3458 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003459 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003460
3461 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3462 exit_data_size, exit_data);
3463
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003464 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003465 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003466 (void **)&loaded_image_protocol,
3467 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003468 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003469 if (ret != EFI_SUCCESS) {
3470 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003471 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003472 }
3473
3474 /* Unloading of unstarted images */
3475 switch (image_obj->header.type) {
3476 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3477 break;
3478 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3479 efi_delete_image(image_obj, loaded_image_protocol);
3480 ret = EFI_SUCCESS;
3481 goto out;
3482 default:
3483 /* Handle does not refer to loaded image */
3484 ret = EFI_INVALID_PARAMETER;
3485 goto out;
3486 }
3487 /* A started image can only be unloaded it is the last one started. */
3488 if (image_handle != current_image) {
3489 ret = EFI_INVALID_PARAMETER;
3490 goto out;
3491 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003492
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003493 /* Exit data is only foreseen in case of failure. */
3494 if (exit_status != EFI_SUCCESS) {
3495 ret = efi_update_exit_data(image_obj, exit_data_size,
3496 exit_data);
3497 /* Exiting has priority. Don't return error to caller. */
3498 if (ret != EFI_SUCCESS)
3499 EFI_PRINT("%s: out of memory\n", __func__);
3500 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003501 /* efi_delete_image() frees image_obj. Copy before the call. */
3502 exit_jmp = image_obj->exit_jmp;
3503 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003504 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3505 exit_status != EFI_SUCCESS)
3506 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003507
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003508 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3509 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3510 ret = efi_tcg2_measure_efi_app_exit();
3511 if (ret != EFI_SUCCESS) {
3512 log_warning("tcg2 measurement fails(0x%lx)\n",
3513 ret);
3514 }
3515 }
3516 }
3517
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003518 /* Make sure entry/exit counts for EFI world cross-overs match */
3519 EFI_EXIT(exit_status);
3520
3521 /*
3522 * But longjmp out with the U-Boot gd, not the application's, as
3523 * the other end is a setjmp call inside EFI context.
3524 */
3525 efi_restore_gd();
3526
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003527 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003528
3529 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003530out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003531 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003532}
3533
3534/**
Mario Six8fac2912018-07-10 08:40:17 +02003535 * efi_handle_protocol() - get interface of a protocol on a handle
3536 * @handle: handle on which the protocol shall be opened
3537 * @protocol: GUID of the protocol
3538 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003539 *
3540 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003541 *
3542 * See the Unified Extensible Firmware Interface (UEFI) specification for
3543 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003544 *
Mario Six8fac2912018-07-10 08:40:17 +02003545 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003546 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003547efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3548 const efi_guid_t *protocol,
3549 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003550{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003551 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003552 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003553}
3554
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003555/**
Mario Six8fac2912018-07-10 08:40:17 +02003556 * efi_bind_controller() - bind a single driver to a controller
3557 * @controller_handle: controller handle
3558 * @driver_image_handle: driver handle
3559 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003560 *
Mario Six8fac2912018-07-10 08:40:17 +02003561 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003562 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003563static efi_status_t efi_bind_controller(
3564 efi_handle_t controller_handle,
3565 efi_handle_t driver_image_handle,
3566 struct efi_device_path *remain_device_path)
3567{
3568 struct efi_driver_binding_protocol *binding_protocol;
3569 efi_status_t r;
3570
3571 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3572 &efi_guid_driver_binding_protocol,
3573 (void **)&binding_protocol,
3574 driver_image_handle, NULL,
3575 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3576 if (r != EFI_SUCCESS)
3577 return r;
3578 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3579 controller_handle,
3580 remain_device_path));
3581 if (r == EFI_SUCCESS)
3582 r = EFI_CALL(binding_protocol->start(binding_protocol,
3583 controller_handle,
3584 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003585 efi_close_protocol(driver_image_handle,
3586 &efi_guid_driver_binding_protocol,
3587 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003588 return r;
3589}
3590
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003591/**
Mario Six8fac2912018-07-10 08:40:17 +02003592 * efi_connect_single_controller() - connect a single driver to a controller
3593 * @controller_handle: controller
3594 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003595 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003596 *
Mario Six8fac2912018-07-10 08:40:17 +02003597 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003598 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003599static efi_status_t efi_connect_single_controller(
3600 efi_handle_t controller_handle,
3601 efi_handle_t *driver_image_handle,
3602 struct efi_device_path *remain_device_path)
3603{
3604 efi_handle_t *buffer;
3605 size_t count;
3606 size_t i;
3607 efi_status_t r;
3608 size_t connected = 0;
3609
3610 /* Get buffer with all handles with driver binding protocol */
3611 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3612 &efi_guid_driver_binding_protocol,
3613 NULL, &count, &buffer));
3614 if (r != EFI_SUCCESS)
3615 return r;
3616
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003617 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003618 if (driver_image_handle) {
3619 for (; *driver_image_handle; ++driver_image_handle) {
3620 for (i = 0; i < count; ++i) {
3621 if (buffer[i] == *driver_image_handle) {
3622 buffer[i] = NULL;
3623 r = efi_bind_controller(
3624 controller_handle,
3625 *driver_image_handle,
3626 remain_device_path);
3627 /*
3628 * For drivers that do not support the
3629 * controller or are already connected
3630 * we receive an error code here.
3631 */
3632 if (r == EFI_SUCCESS)
3633 ++connected;
3634 }
3635 }
3636 }
3637 }
3638
3639 /*
3640 * TODO: Some overrides are not yet implemented:
3641 * - Platform Driver Override
3642 * - Driver Family Override Search
3643 * - Bus Specific Driver Override
3644 */
3645
3646 /* Driver Binding Search */
3647 for (i = 0; i < count; ++i) {
3648 if (buffer[i]) {
3649 r = efi_bind_controller(controller_handle,
3650 buffer[i],
3651 remain_device_path);
3652 if (r == EFI_SUCCESS)
3653 ++connected;
3654 }
3655 }
3656
3657 efi_free_pool(buffer);
3658 if (!connected)
3659 return EFI_NOT_FOUND;
3660 return EFI_SUCCESS;
3661}
3662
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003663/**
Mario Six8fac2912018-07-10 08:40:17 +02003664 * efi_connect_controller() - connect a controller to a driver
3665 * @controller_handle: handle of the controller
3666 * @driver_image_handle: handle of the driver
3667 * @remain_device_path: device path of a child controller
3668 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003669 *
3670 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003671 *
3672 * See the Unified Extensible Firmware Interface (UEFI) specification for
3673 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003674 *
3675 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003676 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003677 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3678 *
Mario Six8fac2912018-07-10 08:40:17 +02003679 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003680 */
3681static efi_status_t EFIAPI efi_connect_controller(
3682 efi_handle_t controller_handle,
3683 efi_handle_t *driver_image_handle,
3684 struct efi_device_path *remain_device_path,
3685 bool recursive)
3686{
3687 efi_status_t r;
3688 efi_status_t ret = EFI_NOT_FOUND;
3689 struct efi_object *efiobj;
3690
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003691 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003692 remain_device_path, recursive);
3693
3694 efiobj = efi_search_obj(controller_handle);
3695 if (!efiobj) {
3696 ret = EFI_INVALID_PARAMETER;
3697 goto out;
3698 }
3699
3700 r = efi_connect_single_controller(controller_handle,
3701 driver_image_handle,
3702 remain_device_path);
3703 if (r == EFI_SUCCESS)
3704 ret = EFI_SUCCESS;
3705 if (recursive) {
3706 struct efi_handler *handler;
3707 struct efi_open_protocol_info_item *item;
3708
3709 list_for_each_entry(handler, &efiobj->protocols, link) {
3710 list_for_each_entry(item, &handler->open_infos, link) {
3711 if (item->info.attributes &
3712 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3713 r = EFI_CALL(efi_connect_controller(
3714 item->info.controller_handle,
3715 driver_image_handle,
3716 remain_device_path,
3717 recursive));
3718 if (r == EFI_SUCCESS)
3719 ret = EFI_SUCCESS;
3720 }
3721 }
3722 }
3723 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003724 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003725 if (ret != EFI_SUCCESS && remain_device_path &&
3726 remain_device_path->type == DEVICE_PATH_TYPE_END)
3727 ret = EFI_SUCCESS;
3728out:
3729 return EFI_EXIT(ret);
3730}
3731
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003732/**
Mario Six8fac2912018-07-10 08:40:17 +02003733 * efi_reinstall_protocol_interface() - reinstall protocol interface
3734 * @handle: handle on which the protocol shall be reinstalled
3735 * @protocol: GUID of the protocol to be installed
3736 * @old_interface: interface to be removed
3737 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003738 *
3739 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003740 *
3741 * See the Unified Extensible Firmware Interface (UEFI) specification for
3742 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003743 *
3744 * The old interface is uninstalled. The new interface is installed.
3745 * Drivers are connected.
3746 *
Mario Six8fac2912018-07-10 08:40:17 +02003747 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003748 */
3749static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3750 efi_handle_t handle, const efi_guid_t *protocol,
3751 void *old_interface, void *new_interface)
3752{
3753 efi_status_t ret;
3754
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003755 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003756 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003757
3758 /* Uninstall protocol but do not delete handle */
Ilias Apalodimasc76eade2023-08-24 17:21:09 +03003759 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003760 if (ret != EFI_SUCCESS)
3761 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003762
3763 /* Install the new protocol */
3764 ret = efi_add_protocol(handle, protocol, new_interface);
3765 /*
3766 * The UEFI spec does not specify what should happen to the handle
3767 * if in case of an error no protocol interface remains on the handle.
3768 * So let's do nothing here.
3769 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003770 if (ret != EFI_SUCCESS)
3771 goto out;
3772 /*
3773 * The returned status code has to be ignored.
3774 * Do not create an error if no suitable driver for the handle exists.
3775 */
3776 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3777out:
3778 return EFI_EXIT(ret);
3779}
3780
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003781/**
Mario Six8fac2912018-07-10 08:40:17 +02003782 * efi_get_child_controllers() - get all child controllers associated to a driver
3783 * @efiobj: handle of the controller
3784 * @driver_handle: handle of the driver
3785 * @number_of_children: number of child controllers
3786 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003787 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003788 * The allocated buffer has to be freed with free().
3789 *
Mario Six8fac2912018-07-10 08:40:17 +02003790 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003791 */
3792static efi_status_t efi_get_child_controllers(
3793 struct efi_object *efiobj,
3794 efi_handle_t driver_handle,
3795 efi_uintn_t *number_of_children,
3796 efi_handle_t **child_handle_buffer)
3797{
3798 struct efi_handler *handler;
3799 struct efi_open_protocol_info_item *item;
3800 efi_uintn_t count = 0, i;
3801 bool duplicate;
3802
3803 /* Count all child controller associations */
3804 list_for_each_entry(handler, &efiobj->protocols, link) {
3805 list_for_each_entry(item, &handler->open_infos, link) {
3806 if (item->info.agent_handle == driver_handle &&
3807 item->info.attributes &
3808 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3809 ++count;
3810 }
3811 }
3812 /*
3813 * Create buffer. In case of duplicate child controller assignments
3814 * the buffer will be too large. But that does not harm.
3815 */
3816 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003817 if (!count)
3818 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003819 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3820 if (!*child_handle_buffer)
3821 return EFI_OUT_OF_RESOURCES;
3822 /* Copy unique child handles */
3823 list_for_each_entry(handler, &efiobj->protocols, link) {
3824 list_for_each_entry(item, &handler->open_infos, link) {
3825 if (item->info.agent_handle == driver_handle &&
3826 item->info.attributes &
3827 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3828 /* Check this is a new child controller */
3829 duplicate = false;
3830 for (i = 0; i < *number_of_children; ++i) {
3831 if ((*child_handle_buffer)[i] ==
3832 item->info.controller_handle)
3833 duplicate = true;
3834 }
3835 /* Copy handle to buffer */
3836 if (!duplicate) {
3837 i = (*number_of_children)++;
3838 (*child_handle_buffer)[i] =
3839 item->info.controller_handle;
3840 }
3841 }
3842 }
3843 }
3844 return EFI_SUCCESS;
3845}
3846
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003847/**
Mario Six8fac2912018-07-10 08:40:17 +02003848 * efi_disconnect_controller() - disconnect a controller from a driver
3849 * @controller_handle: handle of the controller
3850 * @driver_image_handle: handle of the driver
3851 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003852 *
3853 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003854 *
3855 * See the Unified Extensible Firmware Interface (UEFI) specification for
3856 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003857 *
Mario Six8fac2912018-07-10 08:40:17 +02003858 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003859 */
3860static efi_status_t EFIAPI efi_disconnect_controller(
3861 efi_handle_t controller_handle,
3862 efi_handle_t driver_image_handle,
3863 efi_handle_t child_handle)
3864{
3865 struct efi_driver_binding_protocol *binding_protocol;
3866 efi_handle_t *child_handle_buffer = NULL;
3867 size_t number_of_children = 0;
3868 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003869 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003870 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003871
3872 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3873 child_handle);
3874
3875 efiobj = efi_search_obj(controller_handle);
3876 if (!efiobj) {
3877 r = EFI_INVALID_PARAMETER;
3878 goto out;
3879 }
3880
3881 if (child_handle && !efi_search_obj(child_handle)) {
3882 r = EFI_INVALID_PARAMETER;
3883 goto out;
3884 }
3885
3886 /* If no driver handle is supplied, disconnect all drivers */
3887 if (!driver_image_handle) {
3888 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3889 goto out;
3890 }
3891
3892 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003893 r = efi_get_child_controllers(efiobj,
3894 driver_image_handle,
3895 &number_of_children,
3896 &child_handle_buffer);
3897 if (r != EFI_SUCCESS)
3898 return r;
3899 sole_child = (number_of_children == 1);
3900
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003901 if (child_handle) {
3902 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003903 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003904 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003905 }
3906
3907 /* Get the driver binding protocol */
3908 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3909 &efi_guid_driver_binding_protocol,
3910 (void **)&binding_protocol,
3911 driver_image_handle, NULL,
3912 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003913 if (r != EFI_SUCCESS) {
3914 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003915 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003916 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003917 /* Remove the children */
3918 if (number_of_children) {
3919 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3920 controller_handle,
3921 number_of_children,
3922 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003923 if (r != EFI_SUCCESS) {
3924 r = EFI_DEVICE_ERROR;
3925 goto out;
3926 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003927 }
3928 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003929 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003930 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3931 controller_handle,
3932 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003933 if (r != EFI_SUCCESS) {
3934 r = EFI_DEVICE_ERROR;
3935 goto out;
3936 }
3937 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003938 efi_close_protocol(driver_image_handle,
3939 &efi_guid_driver_binding_protocol,
3940 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003941 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003942out:
3943 if (!child_handle)
3944 free(child_handle_buffer);
3945 return EFI_EXIT(r);
3946}
3947
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003948static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003949 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003950 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3951 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003952 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003953 },
3954 .raise_tpl = efi_raise_tpl,
3955 .restore_tpl = efi_restore_tpl,
3956 .allocate_pages = efi_allocate_pages_ext,
3957 .free_pages = efi_free_pages_ext,
3958 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003959 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003960 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003961 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003962 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003963 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003964 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003965 .close_event = efi_close_event,
3966 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003967 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003968 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003969 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003970 .handle_protocol = efi_handle_protocol,
3971 .reserved = NULL,
3972 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003973 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003974 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003975 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003976 .load_image = efi_load_image,
3977 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003978 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003979 .unload_image = efi_unload_image,
3980 .exit_boot_services = efi_exit_boot_services,
3981 .get_next_monotonic_count = efi_get_next_monotonic_count,
3982 .stall = efi_stall,
3983 .set_watchdog_timer = efi_set_watchdog_timer,
3984 .connect_controller = efi_connect_controller,
3985 .disconnect_controller = efi_disconnect_controller,
3986 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003987 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003988 .open_protocol_information = efi_open_protocol_information,
3989 .protocols_per_handle = efi_protocols_per_handle,
3990 .locate_handle_buffer = efi_locate_handle_buffer,
3991 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003992 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003993 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003994 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003995 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003996 .calculate_crc32 = efi_calculate_crc32,
3997 .copy_mem = efi_copy_mem,
3998 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003999 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01004000};
4001
Simon Glass90975372022-01-23 12:55:12 -07004002static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01004003
Alexander Graf393dd912016-10-14 13:45:30 +02004004struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01004005 .hdr = {
4006 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02004007 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02004008 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01004009 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02004010 .fw_vendor = firmware_vendor,
4011 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02004012 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01004013 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004014 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01004015};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004016
4017/**
4018 * efi_initialize_system_table() - Initialize system table
4019 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02004020 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004021 */
4022efi_status_t efi_initialize_system_table(void)
4023{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004024 efi_status_t ret;
4025
4026 /* Allocate configuration table array */
4027 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4028 EFI_MAX_CONFIGURATION_TABLES *
4029 sizeof(struct efi_configuration_table),
4030 (void **)&systab.tables);
4031
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004032 /*
4033 * These entries will be set to NULL in ExitBootServices(). To avoid
4034 * relocation in SetVirtualAddressMap(), set them dynamically.
4035 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01004036 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004037 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004038 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004039 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01004040 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02004041 systab.std_err = &efi_con_out;
4042 systab.boottime = &efi_boot_services;
4043
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02004044 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004045 efi_update_table_header_crc32(&systab.hdr);
4046 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4047 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004048
4049 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004050}