blob: fad0476a88184005f7009f3f6fe3ca49c8cc8170 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafbee91162016-03-04 01:09:59 +01004 *
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass36bf4462019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030018#include <time.h>
Simon Glass3db71102019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafbee91162016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro86a3d432021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020030
Alexander Grafbee91162016-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 Schuchardt43bce442018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt14b40482019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010036
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020037/* List of queued events */
Bin Mengee238302023-04-05 20:15:17 +080038static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020039
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
Bin Mengee238302023-04-05 20:15:17 +080047static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020048
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +010053/*
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafbee91162016-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 Schuchardt4f7dc5f2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060060#endif
Alexander Grafbee91162016-03-04 01:09:59 +010061
Ilias Apalodimas54edc372023-07-24 13:17:36 +030062static efi_status_t efi_uninstall_protocol
63 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +030064 void *protocol_interface, bool preserve);
Ilias Apalodimas54edc372023-07-24 13:17:36 +030065
Heinrich Schuchardt914df752019-02-09 14:10:39 +010066/* 1 if inside U-Boot code, 0 if inside EFI payload code */
67static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040068static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010069/* GUID of the device tree table */
70const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-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 Clarkc160d2f2017-07-27 08:04:18 -040074
Heinrich Schuchardta3a28f52018-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 Schuchardt43eaf5b2021-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 Schuchardta3a28f52018-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;
Heinrich Schuchardtb6f11092020-12-04 03:33:41 +010093/* GUIDs of the Load File and Load File2 protocols */
94const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
95const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090096/* GUID of the SMBIOS table */
97const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010098
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010099static efi_status_t EFIAPI efi_disconnect_controller(
100 efi_handle_t controller_handle,
101 efi_handle_t driver_image_handle,
102 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +0100103
Ilias Apalodimas239d59a2023-06-20 09:19:28 +0300104static
105efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
106 efi_handle_t *driver_image_handle,
107 struct efi_device_path *remain_device_path,
108 bool recursive);
109
Rob Clarkc160d2f2017-07-27 08:04:18 -0400110/* Called on every callback entry */
111int __efi_entry_check(void)
112{
113 int ret = entry_count++ == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200114#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clarkc160d2f2017-07-27 08:04:18 -0400115 assert(efi_gd);
116 app_gd = gd;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200117 set_gd(efi_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400118#endif
119 return ret;
120}
121
122/* Called on every callback exit */
123int __efi_exit_check(void)
124{
125 int ret = --entry_count == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200126#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200127 set_gd(app_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400128#endif
129 return ret;
130}
131
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200132/**
133 * efi_save_gd() - save global data register
134 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200135 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200136 * As this register may be overwritten by an EFI payload we save it here
137 * and restore it on every callback entered.
138 *
139 * This function is called after relocation from initr_reloc_global_data().
140 */
Alexander Grafbee91162016-03-04 01:09:59 +0100141void efi_save_gd(void)
142{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200143#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100144 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600145#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100146}
147
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200148/**
149 * efi_restore_gd() - restore global data register
150 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200151 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200152 * Restore it after returning from the UEFI world to the value saved via
153 * efi_save_gd().
Rob Clarkc160d2f2017-07-27 08:04:18 -0400154 */
Alexander Grafbee91162016-03-04 01:09:59 +0100155void efi_restore_gd(void)
156{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200157#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100158 /* Only restore if we're already in EFI context */
159 if (!efi_gd)
160 return;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200161 set_gd(efi_gd);
Simon Glass65e4c0b2016-09-25 15:27:35 -0600162#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100163}
164
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200165/**
Mario Six78a88f72018-07-10 08:40:17 +0200166 * indent_string() - returns a string for indenting with two spaces per level
167 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100168 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200169 * A maximum of ten indent levels is supported. Higher indent levels will be
170 * truncated.
171 *
Mario Six78a88f72018-07-10 08:40:17 +0200172 * Return: A string for indenting with two spaces per level is
173 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400174 */
175static const char *indent_string(int level)
176{
177 const char *indent = " ";
178 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100179
Rob Clarkaf65db82017-07-27 08:04:19 -0400180 level = min(max, level * 2);
181 return &indent[max - level];
182}
183
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200184const char *__efi_nesting(void)
185{
186 return indent_string(nesting_level);
187}
188
Rob Clarkaf65db82017-07-27 08:04:19 -0400189const char *__efi_nesting_inc(void)
190{
191 return indent_string(nesting_level++);
192}
193
194const char *__efi_nesting_dec(void)
195{
196 return indent_string(--nesting_level);
197}
198
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200199/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200200 * efi_event_is_queued() - check if an event is queued
201 *
202 * @event: event
203 * Return: true if event is queued
204 */
205static bool efi_event_is_queued(struct efi_event *event)
206{
207 return !!event->queue_link.next;
208}
209
210/**
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300211 * efi_purge_handle() - Clean the deleted handle from the various lists
212 * @handle: handle to remove
213 *
214 * Return: status code
215 */
216static efi_status_t efi_purge_handle(efi_handle_t handle)
217{
218 struct efi_register_notify_event *item;
219
220 if (!list_empty(&handle->protocols))
221 return EFI_ACCESS_DENIED;
222 /* The handle is about to be freed. Remove it from events */
223 list_for_each_entry(item, &efi_register_notify_events, link) {
224 struct efi_protocol_notification *hitem, *hnext;
225
226 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
227 if (handle == hitem->handle) {
228 list_del(&hitem->link);
229 free(hitem);
230 }
231 }
232 }
233 /* The last protocol has been removed, delete the handle. */
234 list_del(&handle->link);
235 free(handle);
236
237 return EFI_SUCCESS;
238}
239
240/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200241 * efi_process_event_queue() - process event queue
242 */
243static void efi_process_event_queue(void)
244{
245 while (!list_empty(&efi_event_queue)) {
246 struct efi_event *event;
247 efi_uintn_t old_tpl;
248
249 event = list_first_entry(&efi_event_queue, struct efi_event,
250 queue_link);
251 if (efi_tpl >= event->notify_tpl)
252 return;
253 list_del(&event->queue_link);
254 event->queue_link.next = NULL;
255 event->queue_link.prev = NULL;
256 /* Events must be executed at the event's TPL */
257 old_tpl = efi_tpl;
258 efi_tpl = event->notify_tpl;
259 EFI_CALL_VOID(event->notify_function(event,
260 event->notify_context));
261 efi_tpl = old_tpl;
262 if (event->type == EVT_NOTIFY_SIGNAL)
263 event->is_signaled = 0;
264 }
265}
266
267/**
Mario Six78a88f72018-07-10 08:40:17 +0200268 * efi_queue_event() - queue an EFI event
269 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200270 *
271 * This function queues the notification function of the event for future
272 * execution.
273 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200274 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200275static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200276{
Heinrich Schuchardt2b8568f2020-03-06 21:56:10 +0100277 struct efi_event *item;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200278
279 if (!event->notify_function)
280 return;
281
282 if (!efi_event_is_queued(event)) {
283 /*
284 * Events must be notified in order of decreasing task priority
285 * level. Insert the new event accordingly.
286 */
287 list_for_each_entry(item, &efi_event_queue, queue_link) {
288 if (item->notify_tpl < event->notify_tpl) {
289 list_add_tail(&event->queue_link,
290 &item->queue_link);
291 event = NULL;
292 break;
293 }
294 }
295 if (event)
296 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardtb7d186f2020-12-28 00:25:34 +0100297 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200298 }
299}
300
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200301/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200302 * is_valid_tpl() - check if the task priority level is valid
303 *
304 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200305 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200306 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100307static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200308{
309 switch (tpl) {
310 case TPL_APPLICATION:
311 case TPL_CALLBACK:
312 case TPL_NOTIFY:
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200313 return EFI_SUCCESS;
314 default:
315 return EFI_INVALID_PARAMETER;
316 }
317}
318
319/**
Mario Six78a88f72018-07-10 08:40:17 +0200320 * efi_signal_event() - signal an EFI event
321 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100322 *
Heinrich Schuchardt2a0f80f2020-12-28 00:59:09 +0100323 * This function signals an event. If the event belongs to an event group, all
324 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100325 * their notification function is queued.
326 *
327 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100328 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200329void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100330{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200331 if (event->is_signaled)
332 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100333 if (event->group) {
334 struct efi_event *evt;
335
336 /*
337 * The signaled state has to set before executing any
338 * notification function
339 */
340 list_for_each_entry(evt, &efi_events, link) {
341 if (!evt->group || guidcmp(evt->group, event->group))
342 continue;
343 if (evt->is_signaled)
344 continue;
345 evt->is_signaled = true;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100346 }
347 list_for_each_entry(evt, &efi_events, link) {
348 if (!evt->group || guidcmp(evt->group, event->group))
349 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200350 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100351 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200352 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100353 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200354 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100355 }
356}
357
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200358/**
Mario Six78a88f72018-07-10 08:40:17 +0200359 * efi_raise_tpl() - raise the task priority level
360 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200361 *
362 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200363 *
Mario Six78a88f72018-07-10 08:40:17 +0200364 * See the Unified Extensible Firmware Interface (UEFI) specification for
365 * details.
366 *
367 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200368 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100369static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100370{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100371 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200372
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200373 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200374
375 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200376 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200377 efi_tpl = new_tpl;
378 if (efi_tpl > TPL_HIGH_LEVEL)
379 efi_tpl = TPL_HIGH_LEVEL;
380
381 EFI_EXIT(EFI_SUCCESS);
382 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100383}
384
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200385/**
Mario Six78a88f72018-07-10 08:40:17 +0200386 * efi_restore_tpl() - lower the task priority level
387 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200388 *
389 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200390 *
Mario Six78a88f72018-07-10 08:40:17 +0200391 * See the Unified Extensible Firmware Interface (UEFI) specification for
392 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200393 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100394static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100395{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200396 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200397
398 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200399 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200400 efi_tpl = old_tpl;
401 if (efi_tpl > TPL_HIGH_LEVEL)
402 efi_tpl = TPL_HIGH_LEVEL;
403
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100404 /*
405 * Lowering the TPL may have made queued events eligible for execution.
406 */
407 efi_timer_check();
408
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200409 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100410}
411
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200412/**
Mario Six78a88f72018-07-10 08:40:17 +0200413 * efi_allocate_pages_ext() - allocate memory pages
414 * @type: type of allocation to be performed
415 * @memory_type: usage type of the allocated memory
416 * @pages: number of pages to be allocated
417 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200418 *
419 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200420 *
Mario Six78a88f72018-07-10 08:40:17 +0200421 * See the Unified Extensible Firmware Interface (UEFI) specification for
422 * details.
423 *
424 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200425 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900426static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100427 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900428 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100429{
430 efi_status_t r;
431
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100432 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100433 r = efi_allocate_pages(type, memory_type, pages, memory);
434 return EFI_EXIT(r);
435}
436
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200437/**
Mario Six78a88f72018-07-10 08:40:17 +0200438 * efi_free_pages_ext() - Free memory pages.
439 * @memory: start of the memory area to be freed
440 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200441 *
442 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200443 *
Mario Six78a88f72018-07-10 08:40:17 +0200444 * See the Unified Extensible Firmware Interface (UEFI) specification for
445 * details.
446 *
447 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200448 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900449static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100450 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100451{
452 efi_status_t r;
453
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900454 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100455 r = efi_free_pages(memory, pages);
456 return EFI_EXIT(r);
457}
458
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200459/**
Mario Six78a88f72018-07-10 08:40:17 +0200460 * efi_get_memory_map_ext() - get map describing memory usage
461 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
462 * on exit the size of the copied memory map
463 * @memory_map: buffer to which the memory map is written
464 * @map_key: key for the memory map
465 * @descriptor_size: size of an individual memory descriptor
466 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200467 *
468 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200469 *
Mario Six78a88f72018-07-10 08:40:17 +0200470 * See the Unified Extensible Firmware Interface (UEFI) specification for
471 * details.
472 *
473 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200474 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900475static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100476 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900477 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100478 efi_uintn_t *map_key,
479 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900480 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100481{
482 efi_status_t r;
483
484 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
485 map_key, descriptor_size, descriptor_version);
486 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
487 descriptor_size, descriptor_version);
488 return EFI_EXIT(r);
489}
490
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200491/**
Mario Six78a88f72018-07-10 08:40:17 +0200492 * efi_allocate_pool_ext() - allocate memory from pool
493 * @pool_type: type of the pool from which memory is to be allocated
494 * @size: number of bytes to be allocated
495 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200496 *
497 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200498 *
Mario Six78a88f72018-07-10 08:40:17 +0200499 * See the Unified Extensible Firmware Interface (UEFI) specification for
500 * details.
501 *
502 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200503 */
Stefan Brünsead12742016-10-09 22:17:18 +0200504static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100505 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200506 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100507{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100508 efi_status_t r;
509
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100510 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200511 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100512 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100513}
514
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200515/**
Mario Six78a88f72018-07-10 08:40:17 +0200516 * efi_free_pool_ext() - free memory from pool
517 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200518 *
519 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200520 *
Mario Six78a88f72018-07-10 08:40:17 +0200521 * See the Unified Extensible Firmware Interface (UEFI) specification for
522 * details.
523 *
524 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200525 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200526static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100527{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100528 efi_status_t r;
529
530 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200531 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100532 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100533}
534
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200535/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200536 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100537 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200538 * @handle: handle to be added
539 *
540 * The protocols list is initialized. The handle is added to the list of known
541 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100542 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200543void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100544{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200545 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100546 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200547 INIT_LIST_HEAD(&handle->protocols);
548 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100549}
550
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200551/**
Mario Six78a88f72018-07-10 08:40:17 +0200552 * efi_create_handle() - create handle
553 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200554 *
Mario Six78a88f72018-07-10 08:40:17 +0200555 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200556 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100557efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200558{
559 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200560
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200561 obj = calloc(1, sizeof(struct efi_object));
562 if (!obj)
563 return EFI_OUT_OF_RESOURCES;
564
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100565 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200566 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200567
568 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200569}
570
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200571/**
Mario Six78a88f72018-07-10 08:40:17 +0200572 * efi_search_protocol() - find a protocol on a handle.
573 * @handle: handle
574 * @protocol_guid: GUID of the protocol
575 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100576 *
Mario Six78a88f72018-07-10 08:40:17 +0200577 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100578 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100579efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100580 const efi_guid_t *protocol_guid,
581 struct efi_handler **handler)
582{
583 struct efi_object *efiobj;
584 struct list_head *lhandle;
585
586 if (!handle || !protocol_guid)
587 return EFI_INVALID_PARAMETER;
588 efiobj = efi_search_obj(handle);
589 if (!efiobj)
590 return EFI_INVALID_PARAMETER;
591 list_for_each(lhandle, &efiobj->protocols) {
592 struct efi_handler *protocol;
593
594 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +0100595 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100596 if (handler)
597 *handler = protocol;
598 return EFI_SUCCESS;
599 }
600 }
601 return EFI_NOT_FOUND;
602}
603
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200604/**
Mario Six78a88f72018-07-10 08:40:17 +0200605 * efi_remove_protocol() - delete protocol from a handle
606 * @handle: handle from which the protocol shall be deleted
607 * @protocol: GUID of the protocol to be deleted
608 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100609 *
Mario Six78a88f72018-07-10 08:40:17 +0200610 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100611 */
Ilias Apalodimas21eb7c12023-06-19 14:14:03 +0300612static efi_status_t efi_remove_protocol(const efi_handle_t handle,
613 const efi_guid_t *protocol,
614 void *protocol_interface)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100615{
616 struct efi_handler *handler;
617 efi_status_t ret;
618
619 ret = efi_search_protocol(handle, protocol, &handler);
620 if (ret != EFI_SUCCESS)
621 return ret;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200622 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200623 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100624 list_del(&handler->link);
625 free(handler);
626 return EFI_SUCCESS;
627}
628
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200629/**
Mario Six78a88f72018-07-10 08:40:17 +0200630 * efi_remove_all_protocols() - delete all protocols from a handle
631 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100632 *
Mario Six78a88f72018-07-10 08:40:17 +0200633 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100634 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100635static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100636{
637 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100638 struct efi_handler *protocol;
639 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100640
641 efiobj = efi_search_obj(handle);
642 if (!efiobj)
643 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100644 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100645 efi_status_t ret;
646
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300647 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300648 protocol->protocol_interface, true);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100649 if (ret != EFI_SUCCESS)
650 return ret;
651 }
652 return EFI_SUCCESS;
653}
654
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200655/**
Mario Six78a88f72018-07-10 08:40:17 +0200656 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100657 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200658 * @handle: handle to delete
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300659 *
660 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100661 */
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300662efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100663{
Etienne Carriere79325482022-09-07 10:20:13 +0200664 efi_status_t ret;
665
666 ret = efi_remove_all_protocols(handle);
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300667 if (ret != EFI_SUCCESS) {
668 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
669 return ret;
Etienne Carriere79325482022-09-07 10:20:13 +0200670 }
671
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300672 return efi_purge_handle(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100673}
674
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200675/**
Mario Six78a88f72018-07-10 08:40:17 +0200676 * efi_is_event() - check if a pointer is a valid event
677 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100678 *
Mario Six78a88f72018-07-10 08:40:17 +0200679 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100680 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100681static efi_status_t efi_is_event(const struct efi_event *event)
682{
683 const struct efi_event *evt;
684
685 if (!event)
686 return EFI_INVALID_PARAMETER;
687 list_for_each_entry(evt, &efi_events, link) {
688 if (evt == event)
689 return EFI_SUCCESS;
690 }
691 return EFI_INVALID_PARAMETER;
692}
Alexander Grafbee91162016-03-04 01:09:59 +0100693
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200694/**
Mario Six78a88f72018-07-10 08:40:17 +0200695 * efi_create_event() - create an event
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200696 *
Mario Six78a88f72018-07-10 08:40:17 +0200697 * @type: type of the event to create
698 * @notify_tpl: task priority level of the event
699 * @notify_function: notification function of the event
700 * @notify_context: pointer passed to the notification function
701 * @group: event group
702 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200703 *
704 * This function is used inside U-Boot code to create an event.
705 *
706 * For the API function implementing the CreateEvent service see
707 * efi_create_event_ext.
708 *
Mario Six78a88f72018-07-10 08:40:17 +0200709 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200710 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100711efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200712 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200713 struct efi_event *event,
714 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100715 void *notify_context, efi_guid_t *group,
716 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100717{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100718 struct efi_event *evt;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200719 efi_status_t ret;
720 int pool_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200721
Jonathan Graya95343b2017-03-12 19:26:07 +1100722 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200723 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100724
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200725 switch (type) {
726 case 0:
727 case EVT_TIMER:
728 case EVT_NOTIFY_SIGNAL:
729 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
730 case EVT_NOTIFY_WAIT:
731 case EVT_TIMER | EVT_NOTIFY_WAIT:
732 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200733 pool_type = EFI_BOOT_SERVICES_DATA;
734 break;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200735 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200736 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200737 break;
738 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200739 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200740 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100741
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100742 /*
743 * The UEFI specification requires event notification levels to be
744 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
745 *
746 * Parameter NotifyTpl should not be checked if it is not used.
747 */
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900748 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100749 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
750 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200751 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100752
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200753 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
754 (void **)&evt);
755 if (ret != EFI_SUCCESS)
756 return ret;
757 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100758 evt->type = type;
759 evt->notify_tpl = notify_tpl;
760 evt->notify_function = notify_function;
761 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100762 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200763 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100764 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100765 list_add_tail(&evt->link, &efi_events);
766 *event = evt;
767 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100768}
769
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200770/*
Mario Six78a88f72018-07-10 08:40:17 +0200771 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100772 *
Mario Six78a88f72018-07-10 08:40:17 +0200773 * @type: type of the event to create
774 * @notify_tpl: task priority level of the event
775 * @notify_function: notification function of the event
776 * @notify_context: pointer passed to the notification function
777 * @event: created event
778 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100779 *
780 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100781 *
Mario Six78a88f72018-07-10 08:40:17 +0200782 * See the Unified Extensible Firmware Interface (UEFI) specification for
783 * details.
784 *
785 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100786 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100787static
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100788efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
789 void (EFIAPI *notify_function) (
790 struct efi_event *event,
791 void *context),
792 void *notify_context,
793 efi_guid_t *event_group,
794 struct efi_event **event)
795{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200796 efi_status_t ret;
797
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100798 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100799 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200800
801 /*
802 * The allowable input parameters are the same as in CreateEvent()
803 * except for the following two disallowed event types.
804 */
805 switch (type) {
806 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
807 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
808 ret = EFI_INVALID_PARAMETER;
809 goto out;
810 }
811
812 ret = efi_create_event(type, notify_tpl, notify_function,
813 notify_context, event_group, event);
814out:
815 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100816}
817
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200818/**
Mario Six78a88f72018-07-10 08:40:17 +0200819 * efi_create_event_ext() - create an event
820 * @type: type of the event to create
821 * @notify_tpl: task priority level of the event
822 * @notify_function: notification function of the event
823 * @notify_context: pointer passed to the notification function
824 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200825 *
826 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200827 *
Mario Six78a88f72018-07-10 08:40:17 +0200828 * See the Unified Extensible Firmware Interface (UEFI) specification for
829 * details.
830 *
831 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200832 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200833static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100834 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200835 void (EFIAPI *notify_function) (
836 struct efi_event *event,
837 void *context),
838 void *notify_context, struct efi_event **event)
839{
840 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
841 notify_context);
842 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100843 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200844}
845
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200846/**
Mario Six78a88f72018-07-10 08:40:17 +0200847 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200848 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200849 * Check if a timer event has occurred or a queued notification function should
850 * be called.
851 *
Alexander Grafbee91162016-03-04 01:09:59 +0100852 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200853 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100854 */
855void efi_timer_check(void)
856{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100857 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100858 u64 now = timer_get_us();
859
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100860 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200861 if (!timers_enabled)
862 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100863 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200864 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100865 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200866 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100867 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200868 break;
869 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100870 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200871 break;
872 default:
873 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200874 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100875 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200876 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100877 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200878 efi_process_event_queue();
Stefan Roese29caf932022-09-02 14:10:46 +0200879 schedule();
Alexander Grafbee91162016-03-04 01:09:59 +0100880}
881
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200882/**
Mario Six78a88f72018-07-10 08:40:17 +0200883 * efi_set_timer() - set the trigger time for a timer event or stop the event
884 * @event: event for which the timer is set
885 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200886 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200887 *
888 * This is the function for internal usage in U-Boot. For the API function
889 * implementing the SetTimer service see efi_set_timer_ext.
890 *
Mario Six78a88f72018-07-10 08:40:17 +0200891 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200892 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200893efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200894 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100895{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100896 /* Check that the event is valid */
897 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
898 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100899
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200900 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200901 * The parameter defines a multiple of 100 ns.
902 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200903 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200904 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100905
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100906 switch (type) {
907 case EFI_TIMER_STOP:
908 event->trigger_next = -1ULL;
909 break;
910 case EFI_TIMER_PERIODIC:
911 case EFI_TIMER_RELATIVE:
912 event->trigger_next = timer_get_us() + trigger_time;
913 break;
914 default:
915 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100916 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100917 event->trigger_type = type;
918 event->trigger_time = trigger_time;
919 event->is_signaled = false;
920 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200921}
922
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200923/**
Mario Six78a88f72018-07-10 08:40:17 +0200924 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
925 * event
926 * @event: event for which the timer is set
927 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200928 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200929 *
930 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200931 *
Mario Six78a88f72018-07-10 08:40:17 +0200932 * See the Unified Extensible Firmware Interface (UEFI) specification for
933 * details.
934 *
935 *
936 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200937 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200938static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
939 enum efi_timer_delay type,
940 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200941{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900942 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200943 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100944}
945
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200946/**
Mario Six78a88f72018-07-10 08:40:17 +0200947 * efi_wait_for_event() - wait for events to be signaled
948 * @num_events: number of events to be waited for
949 * @event: events to be waited for
950 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200951 *
952 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200953 *
Mario Six78a88f72018-07-10 08:40:17 +0200954 * See the Unified Extensible Firmware Interface (UEFI) specification for
955 * details.
956 *
957 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200958 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100959static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200960 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100961 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100962{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100963 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100964
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100965 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100966
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200967 /* Check parameters */
968 if (!num_events || !event)
969 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200970 /* Check TPL */
971 if (efi_tpl != TPL_APPLICATION)
972 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200973 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100974 if (efi_is_event(event[i]) != EFI_SUCCESS)
975 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200976 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
977 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200978 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200979 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200980 }
981
982 /* Wait for signal */
983 for (;;) {
984 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200985 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200986 goto out;
987 }
988 /* Allow events to occur. */
989 efi_timer_check();
990 }
991
992out:
993 /*
994 * Reset the signal which is passed to the caller to allow periodic
995 * events to occur.
996 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200997 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200998 if (index)
999 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +01001000
1001 return EFI_EXIT(EFI_SUCCESS);
1002}
1003
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001004/**
Mario Six78a88f72018-07-10 08:40:17 +02001005 * efi_signal_event_ext() - signal an EFI event
1006 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001007 *
1008 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +02001009 *
1010 * See the Unified Extensible Firmware Interface (UEFI) specification for
1011 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001012 *
1013 * This functions sets the signaled state of the event and queues the
1014 * notification function for execution.
1015 *
Mario Six78a88f72018-07-10 08:40:17 +02001016 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001017 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001018static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001019{
1020 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001021 if (efi_is_event(event) != EFI_SUCCESS)
1022 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001023 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +01001024 return EFI_EXIT(EFI_SUCCESS);
1025}
1026
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001027/**
Mario Six78a88f72018-07-10 08:40:17 +02001028 * efi_close_event() - close an EFI event
1029 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001030 *
1031 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001032 *
Mario Six78a88f72018-07-10 08:40:17 +02001033 * See the Unified Extensible Firmware Interface (UEFI) specification for
1034 * details.
1035 *
1036 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001037 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001038static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001039{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001040 struct efi_register_notify_event *item, *next;
1041
Alexander Grafbee91162016-03-04 01:09:59 +01001042 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001043 if (efi_is_event(event) != EFI_SUCCESS)
1044 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001045
1046 /* Remove protocol notify registrations for the event */
1047 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1048 link) {
1049 if (event == item->event) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001050 struct efi_protocol_notification *hitem, *hnext;
1051
1052 /* Remove signaled handles */
1053 list_for_each_entry_safe(hitem, hnext, &item->handles,
1054 link) {
1055 list_del(&hitem->link);
1056 free(hitem);
1057 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001058 list_del(&item->link);
1059 free(item);
1060 }
1061 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +02001062 /* Remove event from queue */
1063 if (efi_event_is_queued(event))
1064 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001065
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001066 list_del(&event->link);
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001067 efi_free_pool(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001068 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001069}
1070
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001071/**
Mario Six78a88f72018-07-10 08:40:17 +02001072 * efi_check_event() - check if an event is signaled
1073 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001074 *
1075 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001076 *
Mario Six78a88f72018-07-10 08:40:17 +02001077 * See the Unified Extensible Firmware Interface (UEFI) specification for
1078 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001079 *
Mario Six78a88f72018-07-10 08:40:17 +02001080 * If an event is not signaled yet, the notification function is queued. The
1081 * signaled state is cleared.
1082 *
1083 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001084 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001085static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001086{
1087 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001088 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001089 if (efi_is_event(event) != EFI_SUCCESS ||
1090 event->type & EVT_NOTIFY_SIGNAL)
1091 return EFI_EXIT(EFI_INVALID_PARAMETER);
1092 if (!event->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001093 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001094 if (event->is_signaled) {
1095 event->is_signaled = false;
1096 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001097 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001098 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001099}
1100
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001101/**
Mario Six78a88f72018-07-10 08:40:17 +02001102 * efi_search_obj() - find the internal EFI object for a handle
1103 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001104 *
Mario Six78a88f72018-07-10 08:40:17 +02001105 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001106 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001107struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001108{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001109 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001110
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001111 if (!handle)
1112 return NULL;
1113
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001114 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001115 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001116 return efiobj;
1117 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001118 return NULL;
1119}
1120
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001121/**
Mario Six78a88f72018-07-10 08:40:17 +02001122 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1123 * to a protocol
1124 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001125 *
Mario Six78a88f72018-07-10 08:40:17 +02001126 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001127 */
1128static struct efi_open_protocol_info_entry *efi_create_open_info(
1129 struct efi_handler *handler)
1130{
1131 struct efi_open_protocol_info_item *item;
1132
1133 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1134 if (!item)
1135 return NULL;
1136 /* Append the item to the open protocol info list. */
1137 list_add_tail(&item->link, &handler->open_infos);
1138
1139 return &item->info;
1140}
1141
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001142/**
Mario Six78a88f72018-07-10 08:40:17 +02001143 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1144 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001145 *
Mario Six78a88f72018-07-10 08:40:17 +02001146 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001147 */
1148static efi_status_t efi_delete_open_info(
1149 struct efi_open_protocol_info_item *item)
1150{
1151 list_del(&item->link);
1152 free(item);
1153 return EFI_SUCCESS;
1154}
1155
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001156/**
Mario Six78a88f72018-07-10 08:40:17 +02001157 * efi_add_protocol() - install new protocol on a handle
1158 * @handle: handle on which the protocol shall be installed
1159 * @protocol: GUID of the protocol to be installed
1160 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001161 *
Mario Six78a88f72018-07-10 08:40:17 +02001162 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001163 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001164efi_status_t efi_add_protocol(const efi_handle_t handle,
1165 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001166 void *protocol_interface)
1167{
1168 struct efi_object *efiobj;
1169 struct efi_handler *handler;
1170 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001171 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001172
1173 efiobj = efi_search_obj(handle);
1174 if (!efiobj)
1175 return EFI_INVALID_PARAMETER;
1176 ret = efi_search_protocol(handle, protocol, NULL);
1177 if (ret != EFI_NOT_FOUND)
1178 return EFI_INVALID_PARAMETER;
1179 handler = calloc(1, sizeof(struct efi_handler));
1180 if (!handler)
1181 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001182 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001183 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001184 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001185 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001186
1187 /* Notify registered events */
1188 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001189 if (!guidcmp(protocol, &event->protocol)) {
1190 struct efi_protocol_notification *notif;
1191
1192 notif = calloc(1, sizeof(*notif));
1193 if (!notif) {
1194 list_del(&handler->link);
1195 free(handler);
1196 return EFI_OUT_OF_RESOURCES;
1197 }
1198 notif->handle = handle;
1199 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001200 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001201 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001202 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001203 }
1204
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001205 if (!guidcmp(&efi_guid_device_path, protocol))
1206 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001207 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001208}
1209
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001210/**
Mario Six78a88f72018-07-10 08:40:17 +02001211 * efi_install_protocol_interface() - install protocol interface
1212 * @handle: handle on which the protocol shall be installed
1213 * @protocol: GUID of the protocol to be installed
1214 * @protocol_interface_type: type of the interface to be installed,
1215 * always EFI_NATIVE_INTERFACE
1216 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001217 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001218 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001219 *
Mario Six78a88f72018-07-10 08:40:17 +02001220 * See the Unified Extensible Firmware Interface (UEFI) specification for
1221 * details.
1222 *
1223 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001224 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001225static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001226 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001227 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001228{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001229 efi_status_t r;
1230
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001231 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001232 protocol_interface);
1233
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001234 if (!handle || !protocol ||
1235 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1236 r = EFI_INVALID_PARAMETER;
1237 goto out;
1238 }
1239
1240 /* Create new handle if requested. */
1241 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001242 r = efi_create_handle(handle);
1243 if (r != EFI_SUCCESS)
1244 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001245 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001246 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001247 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001248 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001249 /* Add new protocol */
1250 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001251out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001252 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001253}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001254
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001255/**
Mario Six78a88f72018-07-10 08:40:17 +02001256 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001257 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001258 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001259 * @number_of_drivers: number of child controllers
1260 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001261 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001262 * The allocated buffer has to be freed with free().
1263 *
Mario Six78a88f72018-07-10 08:40:17 +02001264 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001265 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001266static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001267 const efi_guid_t *protocol,
1268 efi_uintn_t *number_of_drivers,
1269 efi_handle_t **driver_handle_buffer)
1270{
1271 struct efi_handler *handler;
1272 struct efi_open_protocol_info_item *item;
1273 efi_uintn_t count = 0, i;
1274 bool duplicate;
1275
1276 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001277 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001278 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001279 continue;
1280 list_for_each_entry(item, &handler->open_infos, link) {
1281 if (item->info.attributes &
1282 EFI_OPEN_PROTOCOL_BY_DRIVER)
1283 ++count;
1284 }
1285 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001286 *number_of_drivers = 0;
1287 if (!count) {
1288 *driver_handle_buffer = NULL;
1289 return EFI_SUCCESS;
1290 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001291 /*
1292 * Create buffer. In case of duplicate driver assignments the buffer
1293 * will be too large. But that does not harm.
1294 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001295 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1296 if (!*driver_handle_buffer)
1297 return EFI_OUT_OF_RESOURCES;
1298 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001299 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001300 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001301 continue;
1302 list_for_each_entry(item, &handler->open_infos, link) {
1303 if (item->info.attributes &
1304 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1305 /* Check this is a new driver */
1306 duplicate = false;
1307 for (i = 0; i < *number_of_drivers; ++i) {
1308 if ((*driver_handle_buffer)[i] ==
1309 item->info.agent_handle)
1310 duplicate = true;
1311 }
1312 /* Copy handle to buffer */
1313 if (!duplicate) {
1314 i = (*number_of_drivers)++;
1315 (*driver_handle_buffer)[i] =
1316 item->info.agent_handle;
1317 }
1318 }
1319 }
1320 }
1321 return EFI_SUCCESS;
1322}
1323
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001324/**
Mario Six78a88f72018-07-10 08:40:17 +02001325 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001326 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001327 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001328 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001329 *
1330 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001331 *
Mario Six78a88f72018-07-10 08:40:17 +02001332 * See the Unified Extensible Firmware Interface (UEFI) specification for
1333 * details.
1334 *
1335 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001336 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001337static efi_status_t efi_disconnect_all_drivers
1338 (efi_handle_t handle,
1339 const efi_guid_t *protocol,
1340 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001341{
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001342 efi_uintn_t number_of_drivers;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001343 efi_handle_t *driver_handle_buffer;
1344 efi_status_t r, ret;
1345
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001346 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001347 &driver_handle_buffer);
1348 if (ret != EFI_SUCCESS)
1349 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001350 if (!number_of_drivers)
1351 return EFI_SUCCESS;
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001352
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001353 while (number_of_drivers) {
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001354 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001355 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001356 driver_handle_buffer[--number_of_drivers],
1357 child_handle));
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001358 if (r != EFI_SUCCESS)
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001359 ret = r;
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001360 }
1361
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001362 free(driver_handle_buffer);
1363 return ret;
1364}
1365
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001366/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001367 * efi_uninstall_protocol() - uninstall protocol interface
1368 *
Mario Six78a88f72018-07-10 08:40:17 +02001369 * @handle: handle from which the protocol shall be removed
1370 * @protocol: GUID of the protocol to be removed
1371 * @protocol_interface: interface to be removed
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001372 * @preserve: preserve or delete the handle and remove it from any
1373 * list it participates if no protocols remain
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001374 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001375 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001376 *
1377 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001378 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001379static efi_status_t efi_uninstall_protocol
1380 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001381 void *protocol_interface, bool preserve)
Alexander Grafbee91162016-03-04 01:09:59 +01001382{
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001383 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001384 struct efi_open_protocol_info_item *item;
1385 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001386 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001387
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001388 /* Find the protocol on the handle */
1389 r = efi_search_protocol(handle, protocol, &handler);
1390 if (r != EFI_SUCCESS)
1391 goto out;
Ilias Apalodimas748cb552023-06-20 09:19:30 +03001392 if (handler->protocol_interface != protocol_interface)
1393 return EFI_NOT_FOUND;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001394 /* Disconnect controllers */
Heinrich Schuchardt62870212023-06-18 09:00:45 +02001395 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001396 if (r != EFI_SUCCESS) {
1397 r = EFI_ACCESS_DENIED;
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001398 /*
1399 * This will reconnect all controllers of the handle, even ones
1400 * that were not connected before. This can be done better
1401 * but we are following the EDKII implementation on this for
1402 * now
1403 */
1404 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001405 goto out;
1406 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001407 /* Close protocol */
1408 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1409 if (item->info.attributes ==
1410 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1411 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1412 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001413 efi_delete_open_info(item);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001414 }
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001415 /* if agents didn't close the protocols properly */
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001416 if (!list_empty(&handler->open_infos)) {
1417 r = EFI_ACCESS_DENIED;
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001418 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001419 goto out;
1420 }
1421 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001422 if (r != EFI_SUCCESS)
1423 return r;
1424 /*
1425 * We don't care about the return value here since the
1426 * handle might have more protocols installed
1427 */
1428 if (!preserve)
1429 efi_purge_handle(handle);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001430out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001431 return r;
1432}
1433
1434/**
1435 * efi_uninstall_protocol_interface() - uninstall protocol interface
1436 * @handle: handle from which the protocol shall be removed
1437 * @protocol: GUID of the protocol to be removed
1438 * @protocol_interface: interface to be removed
1439 *
1440 * This function implements the UninstallProtocolInterface service.
1441 *
1442 * See the Unified Extensible Firmware Interface (UEFI) specification for
1443 * details.
1444 *
1445 * Return: status code
1446 */
1447static efi_status_t EFIAPI efi_uninstall_protocol_interface
1448 (efi_handle_t handle, const efi_guid_t *protocol,
1449 void *protocol_interface)
1450{
1451 efi_status_t ret;
1452
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001453 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001454
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001455 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001456 if (ret != EFI_SUCCESS)
1457 goto out;
1458
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001459out:
1460 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001461}
1462
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001463/**
Mario Six78a88f72018-07-10 08:40:17 +02001464 * efi_register_protocol_notify() - register an event for notification when a
1465 * protocol is installed.
1466 * @protocol: GUID of the protocol whose installation shall be notified
1467 * @event: event to be signaled upon installation of the protocol
1468 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001469 *
1470 * This function implements the RegisterProtocolNotify service.
1471 * See the Unified Extensible Firmware Interface (UEFI) specification
1472 * for details.
1473 *
Mario Six78a88f72018-07-10 08:40:17 +02001474 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001475 */
Jose Marinho64a8aae2021-03-02 17:26:38 +00001476efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1477 struct efi_event *event,
1478 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001479{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001480 struct efi_register_notify_event *item;
1481 efi_status_t ret = EFI_SUCCESS;
1482
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001483 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001484
1485 if (!protocol || !event || !registration) {
1486 ret = EFI_INVALID_PARAMETER;
1487 goto out;
1488 }
1489
1490 item = calloc(1, sizeof(struct efi_register_notify_event));
1491 if (!item) {
1492 ret = EFI_OUT_OF_RESOURCES;
1493 goto out;
1494 }
1495
1496 item->event = event;
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301497 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001498 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001499
1500 list_add_tail(&item->link, &efi_register_notify_events);
1501
1502 *registration = item;
1503out:
1504 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001505}
1506
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001507/**
Mario Six78a88f72018-07-10 08:40:17 +02001508 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001509 *
Mario Six78a88f72018-07-10 08:40:17 +02001510 * @search_type: selection criterion
1511 * @protocol: GUID of the protocol
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001512 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001513 *
1514 * See the documentation of the LocateHandle service in the UEFI specification.
1515 *
Mario Six78a88f72018-07-10 08:40:17 +02001516 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001517 */
Alexander Grafbee91162016-03-04 01:09:59 +01001518static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001519 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001520{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001521 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001522
1523 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001524 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001525 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001526 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001527 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001528 return (ret != EFI_SUCCESS);
1529 default:
1530 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001531 return -1;
1532 }
Alexander Grafbee91162016-03-04 01:09:59 +01001533}
1534
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001535/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001536 * efi_check_register_notify_event() - check if registration key is valid
1537 *
1538 * Check that a pointer is a valid registration key as returned by
1539 * RegisterProtocolNotify().
1540 *
1541 * @key: registration key
1542 * Return: valid registration key or NULL
1543 */
1544static struct efi_register_notify_event *efi_check_register_notify_event
1545 (void *key)
1546{
1547 struct efi_register_notify_event *event;
1548
1549 list_for_each_entry(event, &efi_register_notify_events, link) {
1550 if (event == (struct efi_register_notify_event *)key)
1551 return event;
1552 }
1553 return NULL;
1554}
1555
1556/**
Mario Six78a88f72018-07-10 08:40:17 +02001557 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001558 *
1559 * @search_type: selection criterion
1560 * @protocol: GUID of the protocol
1561 * @search_key: registration key
1562 * @buffer_size: size of the buffer to receive the handles in bytes
1563 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001564 *
1565 * This function is meant for U-Boot internal calls. For the API implementation
1566 * of the LocateHandle service see efi_locate_handle_ext.
1567 *
Mario Six78a88f72018-07-10 08:40:17 +02001568 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001569 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001570static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001571 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001572 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001573 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001574{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001575 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001576 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001577 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001578 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001579
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001580 /* Check parameters */
1581 switch (search_type) {
1582 case ALL_HANDLES:
1583 break;
1584 case BY_REGISTER_NOTIFY:
1585 if (!search_key)
1586 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001587 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001588 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001589 if (!event)
1590 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001591 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001592 case BY_PROTOCOL:
1593 if (!protocol)
1594 return EFI_INVALID_PARAMETER;
1595 break;
1596 default:
1597 return EFI_INVALID_PARAMETER;
1598 }
1599
Alexander Grafbee91162016-03-04 01:09:59 +01001600 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001601 if (search_type == BY_REGISTER_NOTIFY) {
1602 if (list_empty(&event->handles))
1603 return EFI_NOT_FOUND;
1604 handle = list_first_entry(&event->handles,
1605 struct efi_protocol_notification,
1606 link);
1607 efiobj = handle->handle;
1608 size += sizeof(void *);
1609 } else {
1610 list_for_each_entry(efiobj, &efi_obj_list, link) {
1611 if (!efi_search(search_type, protocol, efiobj))
1612 size += sizeof(void *);
1613 }
1614 if (size == 0)
1615 return EFI_NOT_FOUND;
Alexander Grafbee91162016-03-04 01:09:59 +01001616 }
1617
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001618 if (!buffer_size)
1619 return EFI_INVALID_PARAMETER;
1620
Alexander Grafbee91162016-03-04 01:09:59 +01001621 if (*buffer_size < size) {
1622 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001623 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001624 }
1625
Rob Clark796a78c2017-08-06 14:10:07 -04001626 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001627
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001628 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001629 if (!buffer)
1630 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001631
Alexander Grafbee91162016-03-04 01:09:59 +01001632 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001633 if (search_type == BY_REGISTER_NOTIFY) {
1634 *buffer = efiobj;
1635 list_del(&handle->link);
1636 } else {
1637 list_for_each_entry(efiobj, &efi_obj_list, link) {
1638 if (!efi_search(search_type, protocol, efiobj))
1639 *buffer++ = efiobj;
1640 }
Alexander Grafbee91162016-03-04 01:09:59 +01001641 }
1642
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001643 return EFI_SUCCESS;
1644}
1645
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001646/**
Mario Six78a88f72018-07-10 08:40:17 +02001647 * efi_locate_handle_ext() - locate handles implementing a protocol.
1648 * @search_type: selection criterion
1649 * @protocol: GUID of the protocol
1650 * @search_key: registration key
1651 * @buffer_size: size of the buffer to receive the handles in bytes
1652 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001653 *
1654 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001655 *
Mario Six78a88f72018-07-10 08:40:17 +02001656 * See the Unified Extensible Firmware Interface (UEFI) specification for
1657 * details.
1658 *
1659 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001660 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001661static efi_status_t EFIAPI efi_locate_handle_ext(
1662 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001663 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001664 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001665{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001666 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001667 buffer_size, buffer);
1668
1669 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1670 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001671}
1672
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001673/**
Mario Six78a88f72018-07-10 08:40:17 +02001674 * efi_remove_configuration_table() - collapses configuration table entries,
1675 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001676 *
Mario Six78a88f72018-07-10 08:40:17 +02001677 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001678 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001679static void efi_remove_configuration_table(int i)
1680{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001681 struct efi_configuration_table *this = &systab.tables[i];
1682 struct efi_configuration_table *next = &systab.tables[i + 1];
1683 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001684
1685 memmove(this, next, (ulong)end - (ulong)next);
1686 systab.nr_tables--;
1687}
1688
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001689/**
Mario Six78a88f72018-07-10 08:40:17 +02001690 * efi_install_configuration_table() - adds, updates, or removes a
1691 * configuration table
1692 * @guid: GUID of the installed table
1693 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001694 *
1695 * This function is used for internal calls. For the API implementation of the
1696 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1697 *
Mario Six78a88f72018-07-10 08:40:17 +02001698 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001699 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001700efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1701 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001702{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001703 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001704 int i;
1705
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001706 if (!guid)
1707 return EFI_INVALID_PARAMETER;
1708
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001709 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001710 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001711 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001712 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001713 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001714 else
1715 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001716 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001717 }
1718 }
1719
Alexander Grafd98cdf62017-07-26 13:41:04 +02001720 if (!table)
1721 return EFI_NOT_FOUND;
1722
Alexander Grafbee91162016-03-04 01:09:59 +01001723 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001724 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001725 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001726
1727 /* Add a new entry */
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301728 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001729 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001730 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001731
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001732out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001733 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001734 efi_update_table_header_crc32(&systab.hdr);
1735
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001736 /* Notify that the configuration table was changed */
1737 list_for_each_entry(evt, &efi_events, link) {
1738 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001739 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001740 break;
1741 }
1742 }
1743
Alexander Graf488bf122016-08-19 01:23:24 +02001744 return EFI_SUCCESS;
1745}
1746
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001747/**
Mario Six78a88f72018-07-10 08:40:17 +02001748 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1749 * configuration table.
1750 * @guid: GUID of the installed table
1751 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001752 *
1753 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001754 *
Mario Six78a88f72018-07-10 08:40:17 +02001755 * See the Unified Extensible Firmware Interface (UEFI) specification for
1756 * details.
1757 *
1758 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001759 */
Masahisa Kojimaf86352e2021-10-22 20:24:24 +09001760static efi_status_t
1761EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1762 void *table)
Alexander Graf488bf122016-08-19 01:23:24 +02001763{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001764 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001765 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001766}
1767
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001768/**
Mario Six78a88f72018-07-10 08:40:17 +02001769 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001770 *
1771 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001772 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001773 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001774 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001775 * code is returned.
1776 *
1777 * @device_path: device path of the loaded image
1778 * @file_path: file path of the loaded image
1779 * @handle_ptr: handle of the loaded image
1780 * @info_ptr: loaded image protocol
1781 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001782 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001783efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1784 struct efi_device_path *file_path,
1785 struct efi_loaded_image_obj **handle_ptr,
1786 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001787{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001788 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001789 struct efi_loaded_image *info = NULL;
1790 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001791 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001792
1793 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1794 *handle_ptr = NULL;
1795 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001796
1797 info = calloc(1, sizeof(*info));
1798 if (!info)
1799 return EFI_OUT_OF_RESOURCES;
1800 obj = calloc(1, sizeof(*obj));
1801 if (!obj) {
1802 free(info);
1803 return EFI_OUT_OF_RESOURCES;
1804 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001805 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001806
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001807 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001808 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001809
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001810 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001811 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001812 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001813
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001814 if (device_path) {
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +01001815 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001816
1817 dp = efi_dp_append(device_path, file_path);
1818 if (!dp) {
1819 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001820 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001821 }
1822 } else {
1823 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001824 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001825 ret = efi_add_protocol(&obj->header,
1826 &efi_guid_loaded_image_device_path, dp);
1827 if (ret != EFI_SUCCESS)
1828 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001829
1830 /*
1831 * When asking for the loaded_image interface, just
1832 * return handle which points to loaded_image_info
1833 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001834 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001835 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001836 if (ret != EFI_SUCCESS)
1837 goto failure;
1838
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001839 *info_ptr = info;
1840 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001841
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001842 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001843failure:
1844 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001845 efi_delete_handle(&obj->header);
1846 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001847 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001848}
1849
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001850/**
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001851 * efi_locate_device_path() - Get the device path and handle of an device
1852 * implementing a protocol
1853 * @protocol: GUID of the protocol
1854 * @device_path: device path
1855 * @device: handle of the device
1856 *
1857 * This function implements the LocateDevicePath service.
1858 *
1859 * See the Unified Extensible Firmware Interface (UEFI) specification for
1860 * details.
1861 *
1862 * Return: status code
1863 */
AKASHI Takahirod8465ff2022-04-28 17:09:38 +09001864efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1865 struct efi_device_path **device_path,
1866 efi_handle_t *device)
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001867{
1868 struct efi_device_path *dp;
1869 size_t i;
1870 struct efi_handler *handler;
1871 efi_handle_t *handles;
1872 size_t len, len_dp;
1873 size_t len_best = 0;
1874 efi_uintn_t no_handles;
1875 u8 *remainder;
1876 efi_status_t ret;
1877
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001878 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001879
1880 if (!protocol || !device_path || !*device_path) {
1881 ret = EFI_INVALID_PARAMETER;
1882 goto out;
1883 }
1884
1885 /* Find end of device path */
1886 len = efi_dp_instance_size(*device_path);
1887
1888 /* Get all handles implementing the protocol */
1889 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1890 &no_handles, &handles));
1891 if (ret != EFI_SUCCESS)
1892 goto out;
1893
1894 for (i = 0; i < no_handles; ++i) {
1895 /* Find the device path protocol */
1896 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1897 &handler);
1898 if (ret != EFI_SUCCESS)
1899 continue;
1900 dp = (struct efi_device_path *)handler->protocol_interface;
1901 len_dp = efi_dp_instance_size(dp);
1902 /*
1903 * This handle can only be a better fit
1904 * if its device path length is longer than the best fit and
1905 * if its device path length is shorter of equal the searched
1906 * device path.
1907 */
1908 if (len_dp <= len_best || len_dp > len)
1909 continue;
1910 /* Check if dp is a subpath of device_path */
1911 if (memcmp(*device_path, dp, len_dp))
1912 continue;
1913 if (!device) {
1914 ret = EFI_INVALID_PARAMETER;
1915 goto out;
1916 }
1917 *device = handles[i];
1918 len_best = len_dp;
1919 }
1920 if (len_best) {
1921 remainder = (u8 *)*device_path + len_best;
1922 *device_path = (struct efi_device_path *)remainder;
1923 ret = EFI_SUCCESS;
1924 } else {
1925 ret = EFI_NOT_FOUND;
1926 }
1927out:
1928 return EFI_EXIT(ret);
1929}
1930
1931/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001932 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001933 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001934 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1935 * callers obligation to update the memory type as needed.
1936 *
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01001937 * @file_path: the path of the image to load
1938 * @buffer: buffer containing the loaded image
1939 * @size: size of the loaded image
1940 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001941 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001942static
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001943efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001944 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001945{
Rob Clark838ee4b2017-09-13 18:05:35 -04001946 struct efi_file_handle *f;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001947 efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001948 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001949 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001950
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001951 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001952 f = efi_file_from_path(file_path);
1953 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001954 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001955
Ilias Apalodimasac30aad2021-03-25 21:55:16 +02001956 ret = efi_file_size(f, &bs);
Rob Clark838ee4b2017-09-13 18:05:35 -04001957 if (ret != EFI_SUCCESS)
1958 goto error;
1959
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001960 /*
1961 * When reading the file we do not yet know if it contains an
1962 * application, a boottime driver, or a runtime driver. So here we
1963 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1964 * update the reservation according to the image type.
1965 */
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001966 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1967 EFI_BOOT_SERVICES_DATA,
1968 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001969 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001970 ret = EFI_OUT_OF_RESOURCES;
1971 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001972 }
1973
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001974 /* Read file */
1975 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1976 if (ret != EFI_SUCCESS)
1977 efi_free_pages(addr, efi_size_in_pages(bs));
1978 *buffer = (void *)(uintptr_t)addr;
1979 *size = bs;
1980error:
1981 EFI_CALL(f->close(f));
Rob Clark838ee4b2017-09-13 18:05:35 -04001982 return ret;
1983}
1984
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001985/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001986 * efi_load_image_from_path() - load an image using a file path
1987 *
1988 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1989 * callers obligation to update the memory type as needed.
1990 *
1991 * @boot_policy: true for request originating from the boot manager
1992 * @file_path: the path of the image to load
1993 * @buffer: buffer containing the loaded image
1994 * @size: size of the loaded image
1995 * Return: status code
1996 */
1997static
1998efi_status_t efi_load_image_from_path(bool boot_policy,
1999 struct efi_device_path *file_path,
2000 void **buffer, efi_uintn_t *size)
2001{
2002 efi_handle_t device;
2003 efi_status_t ret;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002004 struct efi_device_path *dp, *rem;
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002005 struct efi_load_file_protocol *load_file_protocol = NULL;
2006 efi_uintn_t buffer_size;
2007 uint64_t addr, pages;
2008 const efi_guid_t *guid;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002009 struct efi_handler *handler;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002010
2011 /* In case of failure nothing is returned */
2012 *buffer = NULL;
2013 *size = 0;
2014
2015 dp = file_path;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002016 device = efi_dp_find_obj(dp, NULL, &rem);
2017 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2018 NULL);
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002019 if (ret == EFI_SUCCESS)
2020 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002021
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002022 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002023 if (ret == EFI_SUCCESS) {
2024 guid = &efi_guid_load_file_protocol;
2025 } else if (!boot_policy) {
2026 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002027 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002028 }
2029 if (ret != EFI_SUCCESS)
2030 return EFI_NOT_FOUND;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002031 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002032 if (ret != EFI_SUCCESS)
2033 return EFI_NOT_FOUND;
2034 buffer_size = 0;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002035 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002036 ret = EFI_CALL(load_file_protocol->load_file(
2037 load_file_protocol, rem, boot_policy,
2038 &buffer_size, NULL));
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002039 if (ret != EFI_BUFFER_TOO_SMALL)
2040 goto out;
2041 pages = efi_size_in_pages(buffer_size);
2042 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2043 pages, &addr);
2044 if (ret != EFI_SUCCESS) {
2045 ret = EFI_OUT_OF_RESOURCES;
2046 goto out;
2047 }
2048 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002049 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002050 &buffer_size, (void *)(uintptr_t)addr));
2051 if (ret != EFI_SUCCESS)
2052 efi_free_pages(addr, pages);
2053out:
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002054 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002055 if (ret == EFI_SUCCESS) {
2056 *buffer = (void *)(uintptr_t)addr;
2057 *size = buffer_size;
2058 }
2059
2060 return ret;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002061}
2062
2063/**
Mario Six78a88f72018-07-10 08:40:17 +02002064 * efi_load_image() - load an EFI image into memory
2065 * @boot_policy: true for request originating from the boot manager
2066 * @parent_image: the caller's image handle
2067 * @file_path: the path of the image to load
2068 * @source_buffer: memory location from which the image is installed
2069 * @source_size: size of the memory area from which the image is installed
2070 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002071 *
2072 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02002073 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002074 * See the Unified Extensible Firmware Interface (UEFI) specification
2075 * for details.
2076 *
Mario Six78a88f72018-07-10 08:40:17 +02002077 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002078 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09002079efi_status_t EFIAPI efi_load_image(bool boot_policy,
2080 efi_handle_t parent_image,
2081 struct efi_device_path *file_path,
2082 void *source_buffer,
2083 efi_uintn_t source_size,
2084 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002085{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002086 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04002087 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02002088 struct efi_loaded_image_obj **image_obj =
2089 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002090 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002091 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01002092
Heinrich Schuchardte9df5492022-02-03 22:21:51 +01002093 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01002094 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04002095
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02002096 if (!image_handle || (!source_buffer && !file_path) ||
2097 !efi_search_obj(parent_image) ||
2098 /* The parent image handle must refer to a loaded image */
2099 !parent_image->type) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02002100 ret = EFI_INVALID_PARAMETER;
2101 goto error;
2102 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002103
Rob Clark838ee4b2017-09-13 18:05:35 -04002104 if (!source_buffer) {
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01002105 ret = efi_load_image_from_path(boot_policy, file_path,
2106 &dest_buffer, &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002107 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002108 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04002109 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002110 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04002111 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00002112 /* split file_path which contains both the device and file parts */
2113 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002114 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002115 if (ret == EFI_SUCCESS)
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002116 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002117 if (!source_buffer)
2118 /* Release buffer to which file was loaded */
2119 efi_free_pages((uintptr_t)dest_buffer,
2120 efi_size_in_pages(source_size));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002121 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002122 info->system_table = &systab;
2123 info->parent_handle = parent_image;
2124 } else {
2125 /* The image is invalid. Release all associated resources. */
2126 efi_delete_handle(*image_handle);
2127 *image_handle = NULL;
2128 free(info);
2129 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002130error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002131 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002132}
2133
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002134/**
Alexander Graff31239a2018-11-15 21:23:47 +01002135 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2136 */
2137static void efi_exit_caches(void)
2138{
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002139#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graff31239a2018-11-15 21:23:47 +01002140 /*
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002141 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2142 * caches are enabled.
2143 *
2144 * TODO:
2145 * According to the UEFI spec caches that can be managed via CP15
2146 * operations should be enabled. Caches requiring platform information
2147 * to manage should be disabled. This should not happen in
2148 * ExitBootServices() but before invoking any UEFI binary is invoked.
2149 *
2150 * We want to keep the current workaround while GRUB prior to version
2151 * 2.04 is still in use.
Alexander Graff31239a2018-11-15 21:23:47 +01002152 */
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002153 cleanup_before_linux();
Alexander Graff31239a2018-11-15 21:23:47 +01002154#endif
2155}
2156
2157/**
Mario Six78a88f72018-07-10 08:40:17 +02002158 * efi_exit_boot_services() - stop all boot services
2159 * @image_handle: handle of the loaded image
2160 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002161 *
2162 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02002163 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002164 * See the Unified Extensible Firmware Interface (UEFI) specification
2165 * for details.
2166 *
Mario Six78a88f72018-07-10 08:40:17 +02002167 * All timer events are disabled. For exit boot services events the
2168 * notification function is called. The boot services are disabled in the
2169 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002170 *
Mario Six78a88f72018-07-10 08:40:17 +02002171 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002172 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002173static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002174 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01002175{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002176 struct efi_event *evt, *next_event;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002177 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002178
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002179 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01002180
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002181 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002182 if (map_key != efi_memory_map_key) {
2183 ret = EFI_INVALID_PARAMETER;
2184 goto out;
2185 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002186
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002187 /* Check if ExitBootServices has already been called */
2188 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002189 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002190
Heinrich Schuchardt43eaf5b2021-11-16 18:46:27 +01002191 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2192 list_for_each_entry(evt, &efi_events, link) {
2193 if (evt->group &&
2194 !guidcmp(evt->group,
2195 &efi_guid_event_group_before_exit_boot_services)) {
2196 efi_signal_event(evt);
2197 break;
2198 }
2199 }
2200
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002201 /* Stop all timer related activities */
2202 timers_enabled = false;
2203
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002204 /* Add related events to the event group */
2205 list_for_each_entry(evt, &efi_events, link) {
2206 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2207 evt->group = &efi_guid_event_group_exit_boot_services;
2208 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002209 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01002210 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002211 if (evt->group &&
2212 !guidcmp(evt->group,
2213 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002214 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002215 break;
2216 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002217 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002218
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002219 /* Make sure that notification functions are not called anymore */
2220 efi_tpl = TPL_HIGH_LEVEL;
2221
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02002222 /* Notify variable services */
2223 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04002224
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002225 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2226 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2227 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2228 list_del(&evt->link);
2229 }
2230
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002231 if (!efi_st_keep_devices) {
Tom Rini3f73e792021-11-19 16:33:04 -05002232 bootm_disable_interrupts();
Heinrich Schuchardtdb6288d2020-12-27 15:33:09 +01002233 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002234 udc_disconnect();
2235 board_quiesce_devices();
2236 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2237 }
Alexander Grafb7b84102016-11-17 01:02:57 +01002238
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02002239 /* Patch out unsupported runtime function */
2240 efi_runtime_detach();
2241
Alexander Graff31239a2018-11-15 21:23:47 +01002242 /* Fix up caches for EFI payloads if necessary */
2243 efi_exit_caches();
2244
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002245 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002246 systab.con_in_handle = NULL;
2247 systab.con_in = NULL;
2248 systab.con_out_handle = NULL;
2249 systab.con_out = NULL;
2250 systab.stderr_handle = NULL;
2251 systab.std_err = NULL;
2252 systab.boottime = NULL;
2253
2254 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02002255 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002256
Alexander Grafbee91162016-03-04 01:09:59 +01002257 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002258 efi_set_watchdog(0);
Stefan Roese29caf932022-09-02 14:10:46 +02002259 schedule();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002260out:
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002261 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2262 if (ret != EFI_SUCCESS)
2263 efi_tcg2_notify_exit_boot_services_failed();
2264 }
2265
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002266 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002267}
2268
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002269/**
Mario Six78a88f72018-07-10 08:40:17 +02002270 * efi_get_next_monotonic_count() - get next value of the counter
2271 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002272 *
2273 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002274 *
Mario Six78a88f72018-07-10 08:40:17 +02002275 * See the Unified Extensible Firmware Interface (UEFI) specification for
2276 * details.
2277 *
2278 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002279 */
Alexander Grafbee91162016-03-04 01:09:59 +01002280static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2281{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002282 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002283 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002284
Alexander Grafbee91162016-03-04 01:09:59 +01002285 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002286 if (!count) {
2287 ret = EFI_INVALID_PARAMETER;
2288 goto out;
2289 }
Alexander Grafbee91162016-03-04 01:09:59 +01002290 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002291 ret = EFI_SUCCESS;
2292out:
2293 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002294}
2295
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002296/**
Mario Six78a88f72018-07-10 08:40:17 +02002297 * efi_stall() - sleep
2298 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002299 *
Mario Six78a88f72018-07-10 08:40:17 +02002300 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002301 *
Mario Six78a88f72018-07-10 08:40:17 +02002302 * See the Unified Extensible Firmware Interface (UEFI) specification for
2303 * details.
2304 *
2305 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002306 */
Alexander Grafbee91162016-03-04 01:09:59 +01002307static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2308{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002309 u64 end_tick;
2310
Alexander Grafbee91162016-03-04 01:09:59 +01002311 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002312
2313 end_tick = get_ticks() + usec_to_tick(microseconds);
2314 while (get_ticks() < end_tick)
2315 efi_timer_check();
2316
Alexander Grafbee91162016-03-04 01:09:59 +01002317 return EFI_EXIT(EFI_SUCCESS);
2318}
2319
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002320/**
Mario Six78a88f72018-07-10 08:40:17 +02002321 * efi_set_watchdog_timer() - reset the watchdog timer
2322 * @timeout: seconds before reset by watchdog
2323 * @watchdog_code: code to be logged when resetting
2324 * @data_size: size of buffer in bytes
2325 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002326 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002327 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002328 *
Mario Six78a88f72018-07-10 08:40:17 +02002329 * See the Unified Extensible Firmware Interface (UEFI) specification for
2330 * details.
2331 *
2332 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002333 */
Alexander Grafbee91162016-03-04 01:09:59 +01002334static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2335 uint64_t watchdog_code,
2336 unsigned long data_size,
2337 uint16_t *watchdog_data)
2338{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002339 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002340 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002341 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002342}
2343
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002344/**
Mario Six78a88f72018-07-10 08:40:17 +02002345 * efi_close_protocol() - close a protocol
2346 * @handle: handle on which the protocol shall be closed
2347 * @protocol: GUID of the protocol to close
2348 * @agent_handle: handle of the driver
2349 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002350 *
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002351 * This is the function implementing the CloseProtocol service is for internal
2352 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
2353 *
2354 * See the Unified Extensible Firmware Interface (UEFI) specification for
2355 * details.
2356 *
2357 * Return: status code
2358 */
2359efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2360 efi_handle_t agent_handle,
2361 efi_handle_t controller_handle)
2362{
2363 struct efi_handler *handler;
2364 struct efi_open_protocol_info_item *item;
2365 struct efi_open_protocol_info_item *pos;
2366 efi_status_t ret;
2367
2368 if (!efi_search_obj(agent_handle) ||
2369 (controller_handle && !efi_search_obj(controller_handle)))
2370 return EFI_INVALID_PARAMETER;
2371 ret = efi_search_protocol(handle, protocol, &handler);
2372 if (ret != EFI_SUCCESS)
2373 return ret;
2374
2375 ret = EFI_NOT_FOUND;
2376 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2377 if (item->info.agent_handle == agent_handle &&
2378 item->info.controller_handle == controller_handle) {
2379 efi_delete_open_info(item);
2380 ret = EFI_SUCCESS;
2381 }
2382 }
2383
2384 return ret;
2385}
2386
2387/**
2388 * efi_close_protocol_ext() - close a protocol
2389 * @handle: handle on which the protocol shall be closed
2390 * @protocol: GUID of the protocol to close
2391 * @agent_handle: handle of the driver
2392 * @controller_handle: handle of the controller
2393 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002394 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002395 *
Mario Six78a88f72018-07-10 08:40:17 +02002396 * See the Unified Extensible Firmware Interface (UEFI) specification for
2397 * details.
2398 *
2399 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002400 */
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002401static efi_status_t EFIAPI
2402efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2403 efi_handle_t agent_handle,
2404 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002405{
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002406 efi_status_t ret;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002407
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002408 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002409 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002410
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002411 ret = efi_close_protocol(handle, protocol,
2412 agent_handle, controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002413
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002414 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002415}
2416
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002417/**
Mario Six78a88f72018-07-10 08:40:17 +02002418 * efi_open_protocol_information() - provide information about then open status
2419 * of a protocol on a handle
2420 * @handle: handle for which the information shall be retrieved
2421 * @protocol: GUID of the protocol
2422 * @entry_buffer: buffer to receive the open protocol information
2423 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002424 *
2425 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002426 *
Mario Six78a88f72018-07-10 08:40:17 +02002427 * See the Unified Extensible Firmware Interface (UEFI) specification for
2428 * details.
2429 *
2430 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002431 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002432static efi_status_t EFIAPI efi_open_protocol_information(
2433 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002434 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002435 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002436{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002437 unsigned long buffer_size;
2438 unsigned long count;
2439 struct efi_handler *handler;
2440 struct efi_open_protocol_info_item *item;
2441 efi_status_t r;
2442
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002443 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002444 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002445
2446 /* Check parameters */
2447 if (!entry_buffer) {
2448 r = EFI_INVALID_PARAMETER;
2449 goto out;
2450 }
2451 r = efi_search_protocol(handle, protocol, &handler);
2452 if (r != EFI_SUCCESS)
2453 goto out;
2454
2455 /* Count entries */
2456 count = 0;
2457 list_for_each_entry(item, &handler->open_infos, link) {
2458 if (item->info.open_count)
2459 ++count;
2460 }
2461 *entry_count = count;
2462 *entry_buffer = NULL;
2463 if (!count) {
2464 r = EFI_SUCCESS;
2465 goto out;
2466 }
2467
2468 /* Copy entries */
2469 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002470 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002471 (void **)entry_buffer);
2472 if (r != EFI_SUCCESS)
2473 goto out;
2474 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2475 if (item->info.open_count)
2476 (*entry_buffer)[--count] = item->info;
2477 }
2478out:
2479 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002480}
2481
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002482/**
Mario Six78a88f72018-07-10 08:40:17 +02002483 * efi_protocols_per_handle() - get protocols installed on a handle
2484 * @handle: handle for which the information is retrieved
2485 * @protocol_buffer: buffer with protocol GUIDs
2486 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002487 *
2488 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002489 *
Mario Six78a88f72018-07-10 08:40:17 +02002490 * See the Unified Extensible Firmware Interface (UEFI) specification for
2491 * details.
2492 *
2493 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002494 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002495static efi_status_t EFIAPI efi_protocols_per_handle(
2496 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002497 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002498{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002499 unsigned long buffer_size;
2500 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002501 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002502 efi_status_t r;
2503
Alexander Grafbee91162016-03-04 01:09:59 +01002504 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2505 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002506
2507 if (!handle || !protocol_buffer || !protocol_buffer_count)
2508 return EFI_EXIT(EFI_INVALID_PARAMETER);
2509
2510 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002511 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002512
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002513 efiobj = efi_search_obj(handle);
2514 if (!efiobj)
2515 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002516
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002517 /* Count protocols */
2518 list_for_each(protocol_handle, &efiobj->protocols) {
2519 ++*protocol_buffer_count;
2520 }
2521
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002522 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002523 if (*protocol_buffer_count) {
2524 size_t j = 0;
2525
2526 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002527 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002528 (void **)protocol_buffer);
2529 if (r != EFI_SUCCESS)
2530 return EFI_EXIT(r);
2531 list_for_each(protocol_handle, &efiobj->protocols) {
2532 struct efi_handler *protocol;
2533
2534 protocol = list_entry(protocol_handle,
2535 struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +01002536 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002537 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002538 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002539 }
2540
2541 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002542}
2543
Masahisa Kojima87d79142022-09-12 17:33:50 +09002544efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2545 const efi_guid_t *protocol, void *search_key,
2546 efi_uintn_t *no_handles, efi_handle_t **buffer)
2547{
2548 efi_status_t r;
2549 efi_uintn_t buffer_size = 0;
2550
2551 if (!no_handles || !buffer) {
2552 r = EFI_INVALID_PARAMETER;
2553 goto out;
2554 }
2555 *no_handles = 0;
2556 *buffer = NULL;
2557 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2558 *buffer);
2559 if (r != EFI_BUFFER_TOO_SMALL)
2560 goto out;
2561 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2562 (void **)buffer);
2563 if (r != EFI_SUCCESS)
2564 goto out;
2565 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2566 *buffer);
2567 if (r == EFI_SUCCESS)
2568 *no_handles = buffer_size / sizeof(efi_handle_t);
2569out:
2570 return r;
2571}
2572
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002573/**
Mario Six78a88f72018-07-10 08:40:17 +02002574 * efi_locate_handle_buffer() - locate handles implementing a protocol
2575 * @search_type: selection criterion
2576 * @protocol: GUID of the protocol
2577 * @search_key: registration key
2578 * @no_handles: number of returned handles
2579 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002580 *
2581 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002582 *
Mario Six78a88f72018-07-10 08:40:17 +02002583 * See the Unified Extensible Firmware Interface (UEFI) specification for
2584 * details.
2585 *
2586 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002587 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002588efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafbee91162016-03-04 01:09:59 +01002589 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002590 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002591 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002592{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002593 efi_status_t r;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002594
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002595 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002596 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002597
Masahisa Kojima87d79142022-09-12 17:33:50 +09002598 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2599 no_handles, buffer);
2600
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002601 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002602}
2603
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002604/**
Mario Six78a88f72018-07-10 08:40:17 +02002605 * efi_locate_protocol() - find an interface implementing a protocol
2606 * @protocol: GUID of the protocol
2607 * @registration: registration key passed to the notification function
2608 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002609 *
2610 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002611 *
Mario Six78a88f72018-07-10 08:40:17 +02002612 * See the Unified Extensible Firmware Interface (UEFI) specification for
2613 * details.
2614 *
2615 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002616 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002617static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002618 void *registration,
2619 void **protocol_interface)
2620{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002621 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002622 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002623 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002624
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002625 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002626
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002627 /*
2628 * The UEFI spec explicitly requires a protocol even if a registration
2629 * key is provided. This differs from the logic in LocateHandle().
2630 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002631 if (!protocol || !protocol_interface)
2632 return EFI_EXIT(EFI_INVALID_PARAMETER);
2633
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002634 if (registration) {
2635 struct efi_register_notify_event *event;
2636 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002637
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002638 event = efi_check_register_notify_event(registration);
2639 if (!event)
2640 return EFI_EXIT(EFI_INVALID_PARAMETER);
2641 /*
2642 * The UEFI spec requires to return EFI_NOT_FOUND if no
2643 * protocol instance matches protocol and registration.
2644 * So let's do the same for a mismatch between protocol and
2645 * registration.
2646 */
2647 if (guidcmp(&event->protocol, protocol))
2648 goto not_found;
2649 if (list_empty(&event->handles))
2650 goto not_found;
2651 handle = list_first_entry(&event->handles,
2652 struct efi_protocol_notification,
2653 link);
2654 efiobj = handle->handle;
2655 list_del(&handle->link);
2656 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002657 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002658 if (ret == EFI_SUCCESS)
2659 goto found;
2660 } else {
2661 list_for_each_entry(efiobj, &efi_obj_list, link) {
2662 ret = efi_search_protocol(efiobj, protocol, &handler);
2663 if (ret == EFI_SUCCESS)
2664 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002665 }
2666 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002667not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002668 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002669 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002670found:
2671 *protocol_interface = handler->protocol_interface;
2672 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002673}
2674
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002675/**
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002676 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
2677 * interfaces
2678 * @handle: handle on which the protocol interfaces shall be installed
2679 * @argptr: va_list of args
2680 *
2681 * Core functionality of efi_install_multiple_protocol_interfaces
2682 * Must not be called directly
2683 *
2684 * Return: status code
2685 */
2686static efi_status_t EFIAPI
2687efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2688 efi_va_list argptr)
2689{
2690 const efi_guid_t *protocol;
2691 void *protocol_interface;
2692 efi_handle_t old_handle;
2693 efi_status_t ret = EFI_SUCCESS;
2694 int i = 0;
2695 efi_va_list argptr_copy;
2696
2697 if (!handle)
2698 return EFI_INVALID_PARAMETER;
2699
2700 efi_va_copy(argptr_copy, argptr);
2701 for (;;) {
2702 protocol = efi_va_arg(argptr, efi_guid_t*);
2703 if (!protocol)
2704 break;
2705 protocol_interface = efi_va_arg(argptr, void*);
2706 /* Check that a device path has not been installed before */
2707 if (!guidcmp(protocol, &efi_guid_device_path)) {
2708 struct efi_device_path *dp = protocol_interface;
2709
2710 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2711 &old_handle));
2712 if (ret == EFI_SUCCESS &&
2713 dp->type == DEVICE_PATH_TYPE_END) {
2714 EFI_PRINT("Path %pD already installed\n",
2715 protocol_interface);
2716 ret = EFI_ALREADY_STARTED;
2717 break;
2718 }
2719 }
2720 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2721 EFI_NATIVE_INTERFACE,
2722 protocol_interface));
2723 if (ret != EFI_SUCCESS)
2724 break;
2725 i++;
2726 }
2727 if (ret == EFI_SUCCESS)
2728 goto out;
2729
2730 /* If an error occurred undo all changes. */
2731 for (; i; --i) {
2732 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2733 protocol_interface = efi_va_arg(argptr_copy, void*);
2734 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2735 protocol_interface));
2736 }
2737
2738out:
2739 efi_va_end(argptr_copy);
2740 return ret;
2741
2742}
2743
2744/**
Mario Six78a88f72018-07-10 08:40:17 +02002745 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2746 * interfaces
2747 * @handle: handle on which the protocol interfaces shall be installed
2748 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2749 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002750 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002751 *
2752 * This is the function for internal usage in U-Boot. For the API function
2753 * implementing the InstallMultipleProtocol service see
2754 * efi_install_multiple_protocol_interfaces_ext()
2755 *
2756 * Return: status code
2757 */
2758efi_status_t EFIAPI
2759efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2760{
2761 efi_status_t ret;
2762 efi_va_list argptr;
2763
2764 efi_va_start(argptr, handle);
2765 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2766 efi_va_end(argptr);
2767 return ret;
2768}
2769
2770/**
2771 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2772 * interfaces
2773 * @handle: handle on which the protocol interfaces shall be installed
2774 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2775 * interfaces
2776 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002777 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002778 *
Mario Six78a88f72018-07-10 08:40:17 +02002779 * See the Unified Extensible Firmware Interface (UEFI) specification for
2780 * details.
2781 *
2782 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002783 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002784static efi_status_t EFIAPI
2785efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002786{
2787 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002788 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002789 efi_va_list argptr;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002790
Alexander Grafbeb077a2018-06-18 17:23:05 +02002791 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002792 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2793 efi_va_end(argptr);
2794 return EFI_EXIT(ret);
2795}
2796
2797/**
2798 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2799 * multiple protocol
2800 * interfaces
2801 * @handle: handle from which the protocol interfaces shall be removed
2802 * @argptr: va_list of args
2803 *
2804 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2805 * Must not be called directly
2806 *
2807 * Return: status code
2808 */
2809static efi_status_t EFIAPI
2810efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2811 efi_va_list argptr)
2812{
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002813 const efi_guid_t *protocol, *next_protocol;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002814 void *protocol_interface;
Ilias Apalodimas9fb32692022-11-10 10:21:24 +02002815 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002816 size_t i = 0;
2817 efi_va_list argptr_copy;
2818
2819 if (!handle)
2820 return EFI_INVALID_PARAMETER;
2821
2822 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002823 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002824 for (;;) {
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002825 /*
2826 * If efi_uninstall_protocol() fails we need to be able to
2827 * reinstall the previously uninstalled protocols on the same
2828 * handle.
2829 * Instead of calling efi_uninstall_protocol(...,..., false)
2830 * and potentially removing the handle, only allow the handle
2831 * removal on the last protocol that we requested to uninstall.
2832 * That way we can preserve the handle in case the latter fails
2833 */
2834 bool preserve = true;
2835
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002836 if (!protocol)
2837 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002838 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002839 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2840 if (!next_protocol)
2841 preserve = false;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002842 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002843 protocol_interface, preserve);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002844 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002845 break;
2846 i++;
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002847 protocol = next_protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002848 }
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002849 if (ret == EFI_SUCCESS)
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002850 goto out;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002851
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002852 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002853 for (; i; --i) {
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002854 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2855 protocol_interface = efi_va_arg(argptr_copy, void*);
2856 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2857 EFI_NATIVE_INTERFACE,
2858 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002859 }
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002860 /*
2861 * If any errors are generated while the protocol interfaces are being
2862 * uninstalled, then the protocols uninstalled prior to the error will
2863 * be reinstalled using InstallProtocolInterface() and the status code
2864 * EFI_INVALID_PARAMETER is returned.
2865 */
2866 ret = EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002867
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002868out:
2869 efi_va_end(argptr_copy);
2870 return ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002871}
2872
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002873/**
Mario Six78a88f72018-07-10 08:40:17 +02002874 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2875 * interfaces
2876 * @handle: handle from which the protocol interfaces shall be removed
2877 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2878 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002879 *
2880 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002881 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002882 * This is the function for internal usage in U-Boot. For the API function
2883 * implementing the UninstallMultipleProtocolInterfaces service see
2884 * efi_uninstall_multiple_protocol_interfaces_ext()
2885 *
2886 * Return: status code
2887 */
2888efi_status_t EFIAPI
2889efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2890{
2891 efi_status_t ret;
2892 efi_va_list argptr;
2893
2894 efi_va_start(argptr, handle);
2895 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2896 efi_va_end(argptr);
2897 return ret;
2898}
2899
2900/**
2901 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2902 * interfaces
2903 * @handle: handle from which the protocol interfaces shall be removed
2904 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2905 * interfaces
2906 *
2907 * This function implements the UninstallMultipleProtocolInterfaces service.
2908 *
Mario Six78a88f72018-07-10 08:40:17 +02002909 * See the Unified Extensible Firmware Interface (UEFI) specification for
2910 * details.
2911 *
2912 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002913 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002914static efi_status_t EFIAPI
2915efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002916{
2917 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002918 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002919 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002920
Alexander Grafbeb077a2018-06-18 17:23:05 +02002921 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002922 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Grafbeb077a2018-06-18 17:23:05 +02002923 efi_va_end(argptr);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002924 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002925}
2926
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002927/**
Mario Six78a88f72018-07-10 08:40:17 +02002928 * efi_calculate_crc32() - calculate cyclic redundancy code
2929 * @data: buffer with data
2930 * @data_size: size of buffer in bytes
2931 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002932 *
2933 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002934 *
Mario Six78a88f72018-07-10 08:40:17 +02002935 * See the Unified Extensible Firmware Interface (UEFI) specification for
2936 * details.
2937 *
2938 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002939 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002940static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2941 efi_uintn_t data_size,
2942 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002943{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002944 efi_status_t ret = EFI_SUCCESS;
2945
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002946 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002947 if (!data || !data_size || !crc32_p) {
2948 ret = EFI_INVALID_PARAMETER;
2949 goto out;
2950 }
Alexander Grafbee91162016-03-04 01:09:59 +01002951 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002952out:
2953 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002954}
2955
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002956/**
Mario Six78a88f72018-07-10 08:40:17 +02002957 * efi_copy_mem() - copy memory
2958 * @destination: destination of the copy operation
2959 * @source: source of the copy operation
2960 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002961 *
2962 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002963 *
Mario Six78a88f72018-07-10 08:40:17 +02002964 * See the Unified Extensible Firmware Interface (UEFI) specification for
2965 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002966 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002967static void EFIAPI efi_copy_mem(void *destination, const void *source,
2968 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002969{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002970 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002971 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002972 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002973}
2974
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002975/**
Mario Six78a88f72018-07-10 08:40:17 +02002976 * efi_set_mem() - Fill memory with a byte value.
2977 * @buffer: buffer to fill
2978 * @size: size of buffer in bytes
2979 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002980 *
2981 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002982 *
Mario Six78a88f72018-07-10 08:40:17 +02002983 * See the Unified Extensible Firmware Interface (UEFI) specification for
2984 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002985 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002986static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002987{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002988 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002989 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002990 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002991}
2992
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002993/**
Mario Six78a88f72018-07-10 08:40:17 +02002994 * efi_protocol_open() - open protocol interface on a handle
2995 * @handler: handler of a protocol
2996 * @protocol_interface: interface implementing the protocol
2997 * @agent_handle: handle of the driver
2998 * @controller_handle: handle of the controller
2999 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003000 *
Mario Six78a88f72018-07-10 08:40:17 +02003001 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003002 */
Heinrich Schuchardtf9ad2402020-01-10 12:33:59 +01003003efi_status_t efi_protocol_open(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003004 struct efi_handler *handler,
3005 void **protocol_interface, void *agent_handle,
3006 void *controller_handle, uint32_t attributes)
3007{
3008 struct efi_open_protocol_info_item *item;
3009 struct efi_open_protocol_info_entry *match = NULL;
3010 bool opened_by_driver = false;
3011 bool opened_exclusive = false;
3012
3013 /* If there is no agent, only return the interface */
3014 if (!agent_handle)
3015 goto out;
3016
3017 /* For TEST_PROTOCOL ignore interface attribute */
3018 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3019 *protocol_interface = NULL;
3020
3021 /*
3022 * Check if the protocol is already opened by a driver with the same
3023 * attributes or opened exclusively
3024 */
3025 list_for_each_entry(item, &handler->open_infos, link) {
3026 if (item->info.agent_handle == agent_handle) {
3027 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3028 (item->info.attributes == attributes))
3029 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02003030 } else {
3031 if (item->info.attributes &
3032 EFI_OPEN_PROTOCOL_BY_DRIVER)
3033 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003034 }
3035 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3036 opened_exclusive = true;
3037 }
3038
3039 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02003040 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3041 if (opened_exclusive)
3042 return EFI_ACCESS_DENIED;
3043 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3044 if (opened_exclusive || opened_by_driver)
3045 return EFI_ACCESS_DENIED;
3046 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003047
3048 /* Prepare exclusive opening */
3049 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3050 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003051disconnect_next:
3052 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003053 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003054 efi_status_t ret;
3055
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003056 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003057 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3058 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003059 item->info.controller_handle,
3060 item->info.agent_handle,
3061 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003062 if (ret == EFI_SUCCESS)
3063 /*
3064 * Child controllers may have been
3065 * removed from the open_infos list. So
3066 * let's restart the loop.
3067 */
3068 goto disconnect_next;
3069 else
3070 opened_by_driver = true;
3071 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003072 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003073 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003074 if (opened_by_driver)
3075 return EFI_ACCESS_DENIED;
3076 }
3077
3078 /* Find existing entry */
3079 list_for_each_entry(item, &handler->open_infos, link) {
3080 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02003081 item->info.controller_handle == controller_handle &&
3082 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003083 match = &item->info;
3084 }
3085 /* None found, create one */
3086 if (!match) {
3087 match = efi_create_open_info(handler);
3088 if (!match)
3089 return EFI_OUT_OF_RESOURCES;
3090 }
3091
3092 match->agent_handle = agent_handle;
3093 match->controller_handle = controller_handle;
3094 match->attributes = attributes;
3095 match->open_count++;
3096
3097out:
3098 /* For TEST_PROTOCOL ignore interface attribute. */
3099 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3100 *protocol_interface = handler->protocol_interface;
3101
3102 return EFI_SUCCESS;
3103}
3104
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003105/**
Mario Six78a88f72018-07-10 08:40:17 +02003106 * efi_open_protocol() - open protocol interface on a handle
3107 * @handle: handle on which the protocol shall be opened
3108 * @protocol: GUID of the protocol
3109 * @protocol_interface: interface implementing the protocol
3110 * @agent_handle: handle of the driver
3111 * @controller_handle: handle of the controller
3112 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003113 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003114 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003115 *
Mario Six78a88f72018-07-10 08:40:17 +02003116 * See the Unified Extensible Firmware Interface (UEFI) specification for
3117 * details.
3118 *
3119 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003120 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02003121static efi_status_t EFIAPI efi_open_protocol
3122 (efi_handle_t handle, const efi_guid_t *protocol,
3123 void **protocol_interface, efi_handle_t agent_handle,
3124 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01003125{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003126 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003127 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01003128
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003129 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01003130 protocol_interface, agent_handle, controller_handle,
3131 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003132
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003133 if (!handle || !protocol ||
3134 (!protocol_interface && attributes !=
3135 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
3136 goto out;
3137 }
3138
3139 switch (attributes) {
3140 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3141 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3142 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3143 break;
3144 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3145 if (controller_handle == handle)
3146 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003147 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003148 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3149 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003150 /* Check that the controller handle is valid */
3151 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003152 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003153 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003154 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003155 /* Check that the agent handle is valid */
3156 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003157 goto out;
3158 break;
3159 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003160 goto out;
3161 }
3162
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003163 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003164 switch (r) {
3165 case EFI_SUCCESS:
3166 break;
3167 case EFI_NOT_FOUND:
3168 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003169 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003170 default:
3171 goto out;
3172 }
Alexander Grafbee91162016-03-04 01:09:59 +01003173
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003174 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3175 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01003176out:
3177 return EFI_EXIT(r);
3178}
3179
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003180/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003181 * efi_start_image() - call the entry point of an image
3182 * @image_handle: handle of the image
3183 * @exit_data_size: size of the buffer
3184 * @exit_data: buffer to receive the exit data of the called image
3185 *
3186 * This function implements the StartImage service.
3187 *
3188 * See the Unified Extensible Firmware Interface (UEFI) specification for
3189 * details.
3190 *
3191 * Return: status code
3192 */
3193efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3194 efi_uintn_t *exit_data_size,
3195 u16 **exit_data)
3196{
3197 struct efi_loaded_image_obj *image_obj =
3198 (struct efi_loaded_image_obj *)image_handle;
3199 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003200 void *info;
3201 efi_handle_t parent_image = current_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003202 efi_status_t exit_status;
3203 struct jmp_buf_data exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003204
3205 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3206
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003207 if (!efi_search_obj(image_handle))
3208 return EFI_EXIT(EFI_INVALID_PARAMETER);
3209
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003210 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02003211 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3212 return EFI_EXIT(EFI_INVALID_PARAMETER);
3213
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003214 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3215 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3216
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003217 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3218 &info, NULL, NULL,
3219 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3220 if (ret != EFI_SUCCESS)
3221 return EFI_EXIT(EFI_INVALID_PARAMETER);
3222
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003223 image_obj->exit_data_size = exit_data_size;
3224 image_obj->exit_data = exit_data;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003225 image_obj->exit_status = &exit_status;
3226 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003227
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003228 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3229 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09003230 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojimaf9b51dc2021-12-07 14:15:33 +09003231 if (ret == EFI_SECURITY_VIOLATION) {
3232 /*
3233 * TCG2 Protocol is installed but no TPM device found,
3234 * this is not expected.
3235 */
3236 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003237 }
3238 }
3239 }
3240
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003241 /* call the image! */
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003242 if (setjmp(&exit_jmp)) {
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003243 /*
3244 * We called the entry point of the child image with EFI_CALL
3245 * in the lines below. The child image called the Exit() boot
3246 * service efi_exit() which executed the long jump that brought
3247 * us to the current line. This implies that the second half
3248 * of the EFI_CALL macro has not been executed.
3249 */
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +02003250#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003251 /*
3252 * efi_exit() called efi_restore_gd(). We have to undo this
3253 * otherwise __efi_entry_check() will put the wrong value into
3254 * app_gd.
3255 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +02003256 set_gd(app_gd);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003257#endif
3258 /*
3259 * To get ready to call EFI_EXIT below we have to execute the
3260 * missed out steps of EFI_CALL.
3261 */
3262 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003263 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003264 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003265 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003266 current_image = parent_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003267 return EFI_EXIT(exit_status);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003268 }
3269
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003270 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02003271 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09003272 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003273 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3274
3275 /*
Heinrich Schuchardt55111c52020-01-10 22:06:54 +01003276 * Control is returned from a started UEFI image either by calling
3277 * Exit() (where exit data can be provided) or by simply returning from
3278 * the entry point. In the latter case call Exit() on behalf of the
3279 * image.
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003280 */
3281 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3282}
3283
3284/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003285 * efi_delete_image() - delete loaded image from memory)
3286 *
3287 * @image_obj: handle of the loaded image
3288 * @loaded_image_protocol: loaded image protocol
3289 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003290static efi_status_t efi_delete_image
3291 (struct efi_loaded_image_obj *image_obj,
3292 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003293{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003294 struct efi_object *efiobj;
3295 efi_status_t r, ret = EFI_SUCCESS;
3296
3297close_next:
3298 list_for_each_entry(efiobj, &efi_obj_list, link) {
3299 struct efi_handler *protocol;
3300
3301 list_for_each_entry(protocol, &efiobj->protocols, link) {
3302 struct efi_open_protocol_info_item *info;
3303
3304 list_for_each_entry(info, &protocol->open_infos, link) {
3305 if (info->info.agent_handle !=
3306 (efi_handle_t)image_obj)
3307 continue;
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003308 r = efi_close_protocol(
3309 efiobj, &protocol->guid,
3310 info->info.agent_handle,
3311 info->info.controller_handle);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003312 if (r != EFI_SUCCESS)
3313 ret = r;
3314 /*
3315 * Closing protocols may results in further
3316 * items being deleted. To play it safe loop
3317 * over all elements again.
3318 */
3319 goto close_next;
3320 }
3321 }
3322 }
3323
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003324 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3325 efi_size_in_pages(loaded_image_protocol->image_size));
3326 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003327
3328 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003329}
3330
3331/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003332 * efi_unload_image() - unload an EFI image
3333 * @image_handle: handle of the image to be unloaded
3334 *
3335 * This function implements the UnloadImage service.
3336 *
3337 * See the Unified Extensible Firmware Interface (UEFI) specification for
3338 * details.
3339 *
3340 * Return: status code
3341 */
3342efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3343{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003344 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003345 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003346 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003347
3348 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003349
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003350 efiobj = efi_search_obj(image_handle);
3351 if (!efiobj) {
3352 ret = EFI_INVALID_PARAMETER;
3353 goto out;
3354 }
3355 /* Find the loaded image protocol */
3356 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3357 (void **)&loaded_image_protocol,
3358 NULL, NULL,
3359 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3360 if (ret != EFI_SUCCESS) {
3361 ret = EFI_INVALID_PARAMETER;
3362 goto out;
3363 }
3364 switch (efiobj->type) {
3365 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3366 /* Call the unload function */
3367 if (!loaded_image_protocol->unload) {
3368 ret = EFI_UNSUPPORTED;
3369 goto out;
3370 }
3371 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3372 if (ret != EFI_SUCCESS)
3373 goto out;
3374 break;
3375 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3376 break;
3377 default:
3378 ret = EFI_INVALID_PARAMETER;
3379 goto out;
3380 }
3381 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3382 loaded_image_protocol);
3383out:
3384 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003385}
3386
3387/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003388 * efi_update_exit_data() - fill exit data parameters of StartImage()
3389 *
Heinrich Schuchardt9ce91272019-07-14 11:25:06 +02003390 * @image_obj: image handle
3391 * @exit_data_size: size of the exit data buffer
3392 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003393 * Return: status code
3394 */
3395static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3396 efi_uintn_t exit_data_size,
3397 u16 *exit_data)
3398{
3399 efi_status_t ret;
3400
3401 /*
3402 * If exit_data is not provided to StartImage(), exit_data_size must be
3403 * ignored.
3404 */
3405 if (!image_obj->exit_data)
3406 return EFI_SUCCESS;
3407 if (image_obj->exit_data_size)
3408 *image_obj->exit_data_size = exit_data_size;
3409 if (exit_data_size && exit_data) {
3410 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3411 exit_data_size,
3412 (void **)image_obj->exit_data);
3413 if (ret != EFI_SUCCESS)
3414 return ret;
3415 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3416 } else {
3417 image_obj->exit_data = NULL;
3418 }
3419 return EFI_SUCCESS;
3420}
3421
3422/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003423 * efi_exit() - leave an EFI application or driver
3424 * @image_handle: handle of the application or driver that is exiting
3425 * @exit_status: status code
3426 * @exit_data_size: size of the buffer in bytes
3427 * @exit_data: buffer with data describing an error
3428 *
3429 * This function implements the Exit service.
3430 *
3431 * See the Unified Extensible Firmware Interface (UEFI) specification for
3432 * details.
3433 *
3434 * Return: status code
3435 */
3436static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3437 efi_status_t exit_status,
3438 efi_uintn_t exit_data_size,
3439 u16 *exit_data)
3440{
3441 /*
3442 * TODO: We should call the unload procedure of the loaded
3443 * image protocol.
3444 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003445 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003446 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003447 struct efi_loaded_image_obj *image_obj =
3448 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003449 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003450
3451 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3452 exit_data_size, exit_data);
3453
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003454 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003455 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003456 (void **)&loaded_image_protocol,
3457 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003458 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003459 if (ret != EFI_SUCCESS) {
3460 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003461 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003462 }
3463
3464 /* Unloading of unstarted images */
3465 switch (image_obj->header.type) {
3466 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3467 break;
3468 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3469 efi_delete_image(image_obj, loaded_image_protocol);
3470 ret = EFI_SUCCESS;
3471 goto out;
3472 default:
3473 /* Handle does not refer to loaded image */
3474 ret = EFI_INVALID_PARAMETER;
3475 goto out;
3476 }
3477 /* A started image can only be unloaded it is the last one started. */
3478 if (image_handle != current_image) {
3479 ret = EFI_INVALID_PARAMETER;
3480 goto out;
3481 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003482
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003483 /* Exit data is only foreseen in case of failure. */
3484 if (exit_status != EFI_SUCCESS) {
3485 ret = efi_update_exit_data(image_obj, exit_data_size,
3486 exit_data);
3487 /* Exiting has priority. Don't return error to caller. */
3488 if (ret != EFI_SUCCESS)
3489 EFI_PRINT("%s: out of memory\n", __func__);
3490 }
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003491 /* efi_delete_image() frees image_obj. Copy before the call. */
3492 exit_jmp = image_obj->exit_jmp;
3493 *image_obj->exit_status = exit_status;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003494 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3495 exit_status != EFI_SUCCESS)
3496 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003497
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003498 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3499 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3500 ret = efi_tcg2_measure_efi_app_exit();
3501 if (ret != EFI_SUCCESS) {
3502 log_warning("tcg2 measurement fails(0x%lx)\n",
3503 ret);
3504 }
3505 }
3506 }
3507
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003508 /* Make sure entry/exit counts for EFI world cross-overs match */
3509 EFI_EXIT(exit_status);
3510
3511 /*
3512 * But longjmp out with the U-Boot gd, not the application's, as
3513 * the other end is a setjmp call inside EFI context.
3514 */
3515 efi_restore_gd();
3516
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003517 longjmp(exit_jmp, 1);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003518
3519 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003520out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003521 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003522}
3523
3524/**
Mario Six78a88f72018-07-10 08:40:17 +02003525 * efi_handle_protocol() - get interface of a protocol on a handle
3526 * @handle: handle on which the protocol shall be opened
3527 * @protocol: GUID of the protocol
3528 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003529 *
3530 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003531 *
Mario Six78a88f72018-07-10 08:40:17 +02003532 * See the Unified Extensible Firmware Interface (UEFI) specification for
3533 * details.
3534 *
3535 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003536 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09003537efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3538 const efi_guid_t *protocol,
3539 void **protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01003540{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003541 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003542 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003543}
3544
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003545/**
Mario Six78a88f72018-07-10 08:40:17 +02003546 * efi_bind_controller() - bind a single driver to a controller
3547 * @controller_handle: controller handle
3548 * @driver_image_handle: driver handle
3549 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003550 *
Mario Six78a88f72018-07-10 08:40:17 +02003551 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003552 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003553static efi_status_t efi_bind_controller(
3554 efi_handle_t controller_handle,
3555 efi_handle_t driver_image_handle,
3556 struct efi_device_path *remain_device_path)
3557{
3558 struct efi_driver_binding_protocol *binding_protocol;
3559 efi_status_t r;
3560
3561 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3562 &efi_guid_driver_binding_protocol,
3563 (void **)&binding_protocol,
3564 driver_image_handle, NULL,
3565 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3566 if (r != EFI_SUCCESS)
3567 return r;
3568 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3569 controller_handle,
3570 remain_device_path));
3571 if (r == EFI_SUCCESS)
3572 r = EFI_CALL(binding_protocol->start(binding_protocol,
3573 controller_handle,
3574 remain_device_path));
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003575 efi_close_protocol(driver_image_handle,
3576 &efi_guid_driver_binding_protocol,
3577 driver_image_handle, NULL);
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003578 return r;
3579}
3580
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003581/**
Mario Six78a88f72018-07-10 08:40:17 +02003582 * efi_connect_single_controller() - connect a single driver to a controller
3583 * @controller_handle: controller
3584 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003585 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003586 *
Mario Six78a88f72018-07-10 08:40:17 +02003587 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003588 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003589static efi_status_t efi_connect_single_controller(
3590 efi_handle_t controller_handle,
3591 efi_handle_t *driver_image_handle,
3592 struct efi_device_path *remain_device_path)
3593{
3594 efi_handle_t *buffer;
3595 size_t count;
3596 size_t i;
3597 efi_status_t r;
3598 size_t connected = 0;
3599
3600 /* Get buffer with all handles with driver binding protocol */
3601 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3602 &efi_guid_driver_binding_protocol,
3603 NULL, &count, &buffer));
3604 if (r != EFI_SUCCESS)
3605 return r;
3606
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003607 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003608 if (driver_image_handle) {
3609 for (; *driver_image_handle; ++driver_image_handle) {
3610 for (i = 0; i < count; ++i) {
3611 if (buffer[i] == *driver_image_handle) {
3612 buffer[i] = NULL;
3613 r = efi_bind_controller(
3614 controller_handle,
3615 *driver_image_handle,
3616 remain_device_path);
3617 /*
3618 * For drivers that do not support the
3619 * controller or are already connected
3620 * we receive an error code here.
3621 */
3622 if (r == EFI_SUCCESS)
3623 ++connected;
3624 }
3625 }
3626 }
3627 }
3628
3629 /*
3630 * TODO: Some overrides are not yet implemented:
3631 * - Platform Driver Override
3632 * - Driver Family Override Search
3633 * - Bus Specific Driver Override
3634 */
3635
3636 /* Driver Binding Search */
3637 for (i = 0; i < count; ++i) {
3638 if (buffer[i]) {
3639 r = efi_bind_controller(controller_handle,
3640 buffer[i],
3641 remain_device_path);
3642 if (r == EFI_SUCCESS)
3643 ++connected;
3644 }
3645 }
3646
3647 efi_free_pool(buffer);
3648 if (!connected)
3649 return EFI_NOT_FOUND;
3650 return EFI_SUCCESS;
3651}
3652
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003653/**
Mario Six78a88f72018-07-10 08:40:17 +02003654 * efi_connect_controller() - connect a controller to a driver
3655 * @controller_handle: handle of the controller
3656 * @driver_image_handle: handle of the driver
3657 * @remain_device_path: device path of a child controller
3658 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003659 *
3660 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003661 *
3662 * See the Unified Extensible Firmware Interface (UEFI) specification for
3663 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003664 *
3665 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003666 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003667 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3668 *
Mario Six78a88f72018-07-10 08:40:17 +02003669 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003670 */
3671static efi_status_t EFIAPI efi_connect_controller(
3672 efi_handle_t controller_handle,
3673 efi_handle_t *driver_image_handle,
3674 struct efi_device_path *remain_device_path,
3675 bool recursive)
3676{
3677 efi_status_t r;
3678 efi_status_t ret = EFI_NOT_FOUND;
3679 struct efi_object *efiobj;
3680
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003681 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003682 remain_device_path, recursive);
3683
3684 efiobj = efi_search_obj(controller_handle);
3685 if (!efiobj) {
3686 ret = EFI_INVALID_PARAMETER;
3687 goto out;
3688 }
3689
3690 r = efi_connect_single_controller(controller_handle,
3691 driver_image_handle,
3692 remain_device_path);
3693 if (r == EFI_SUCCESS)
3694 ret = EFI_SUCCESS;
3695 if (recursive) {
3696 struct efi_handler *handler;
3697 struct efi_open_protocol_info_item *item;
3698
3699 list_for_each_entry(handler, &efiobj->protocols, link) {
3700 list_for_each_entry(item, &handler->open_infos, link) {
3701 if (item->info.attributes &
3702 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3703 r = EFI_CALL(efi_connect_controller(
3704 item->info.controller_handle,
3705 driver_image_handle,
3706 remain_device_path,
3707 recursive));
3708 if (r == EFI_SUCCESS)
3709 ret = EFI_SUCCESS;
3710 }
3711 }
3712 }
3713 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003714 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003715 if (ret != EFI_SUCCESS && remain_device_path &&
3716 remain_device_path->type == DEVICE_PATH_TYPE_END)
3717 ret = EFI_SUCCESS;
3718out:
3719 return EFI_EXIT(ret);
3720}
3721
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003722/**
Mario Six78a88f72018-07-10 08:40:17 +02003723 * efi_reinstall_protocol_interface() - reinstall protocol interface
3724 * @handle: handle on which the protocol shall be reinstalled
3725 * @protocol: GUID of the protocol to be installed
3726 * @old_interface: interface to be removed
3727 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003728 *
3729 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003730 *
3731 * See the Unified Extensible Firmware Interface (UEFI) specification for
3732 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003733 *
3734 * The old interface is uninstalled. The new interface is installed.
3735 * Drivers are connected.
3736 *
Mario Six78a88f72018-07-10 08:40:17 +02003737 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003738 */
3739static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3740 efi_handle_t handle, const efi_guid_t *protocol,
3741 void *old_interface, void *new_interface)
3742{
3743 efi_status_t ret;
3744
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003745 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003746 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003747
3748 /* Uninstall protocol but do not delete handle */
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03003749 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003750 if (ret != EFI_SUCCESS)
3751 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003752
3753 /* Install the new protocol */
3754 ret = efi_add_protocol(handle, protocol, new_interface);
3755 /*
3756 * The UEFI spec does not specify what should happen to the handle
3757 * if in case of an error no protocol interface remains on the handle.
3758 * So let's do nothing here.
3759 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003760 if (ret != EFI_SUCCESS)
3761 goto out;
3762 /*
3763 * The returned status code has to be ignored.
3764 * Do not create an error if no suitable driver for the handle exists.
3765 */
3766 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3767out:
3768 return EFI_EXIT(ret);
3769}
3770
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003771/**
Mario Six78a88f72018-07-10 08:40:17 +02003772 * efi_get_child_controllers() - get all child controllers associated to a driver
3773 * @efiobj: handle of the controller
3774 * @driver_handle: handle of the driver
3775 * @number_of_children: number of child controllers
3776 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003777 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003778 * The allocated buffer has to be freed with free().
3779 *
Mario Six78a88f72018-07-10 08:40:17 +02003780 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003781 */
3782static efi_status_t efi_get_child_controllers(
3783 struct efi_object *efiobj,
3784 efi_handle_t driver_handle,
3785 efi_uintn_t *number_of_children,
3786 efi_handle_t **child_handle_buffer)
3787{
3788 struct efi_handler *handler;
3789 struct efi_open_protocol_info_item *item;
3790 efi_uintn_t count = 0, i;
3791 bool duplicate;
3792
3793 /* Count all child controller associations */
3794 list_for_each_entry(handler, &efiobj->protocols, link) {
3795 list_for_each_entry(item, &handler->open_infos, link) {
3796 if (item->info.agent_handle == driver_handle &&
3797 item->info.attributes &
3798 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3799 ++count;
3800 }
3801 }
3802 /*
3803 * Create buffer. In case of duplicate child controller assignments
3804 * the buffer will be too large. But that does not harm.
3805 */
3806 *number_of_children = 0;
Heinrich Schuchardt1047c6e2020-07-07 04:21:26 +02003807 if (!count)
3808 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003809 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3810 if (!*child_handle_buffer)
3811 return EFI_OUT_OF_RESOURCES;
3812 /* Copy unique child handles */
3813 list_for_each_entry(handler, &efiobj->protocols, link) {
3814 list_for_each_entry(item, &handler->open_infos, link) {
3815 if (item->info.agent_handle == driver_handle &&
3816 item->info.attributes &
3817 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3818 /* Check this is a new child controller */
3819 duplicate = false;
3820 for (i = 0; i < *number_of_children; ++i) {
3821 if ((*child_handle_buffer)[i] ==
3822 item->info.controller_handle)
3823 duplicate = true;
3824 }
3825 /* Copy handle to buffer */
3826 if (!duplicate) {
3827 i = (*number_of_children)++;
3828 (*child_handle_buffer)[i] =
3829 item->info.controller_handle;
3830 }
3831 }
3832 }
3833 }
3834 return EFI_SUCCESS;
3835}
3836
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003837/**
Mario Six78a88f72018-07-10 08:40:17 +02003838 * efi_disconnect_controller() - disconnect a controller from a driver
3839 * @controller_handle: handle of the controller
3840 * @driver_image_handle: handle of the driver
3841 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003842 *
3843 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003844 *
Mario Six78a88f72018-07-10 08:40:17 +02003845 * See the Unified Extensible Firmware Interface (UEFI) specification for
3846 * details.
3847 *
3848 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003849 */
3850static efi_status_t EFIAPI efi_disconnect_controller(
3851 efi_handle_t controller_handle,
3852 efi_handle_t driver_image_handle,
3853 efi_handle_t child_handle)
3854{
3855 struct efi_driver_binding_protocol *binding_protocol;
3856 efi_handle_t *child_handle_buffer = NULL;
3857 size_t number_of_children = 0;
3858 efi_status_t r;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003859 struct efi_object *efiobj;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003860 bool sole_child;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003861
3862 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3863 child_handle);
3864
3865 efiobj = efi_search_obj(controller_handle);
3866 if (!efiobj) {
3867 r = EFI_INVALID_PARAMETER;
3868 goto out;
3869 }
3870
3871 if (child_handle && !efi_search_obj(child_handle)) {
3872 r = EFI_INVALID_PARAMETER;
3873 goto out;
3874 }
3875
3876 /* If no driver handle is supplied, disconnect all drivers */
3877 if (!driver_image_handle) {
3878 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3879 goto out;
3880 }
3881
3882 /* Create list of child handles */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003883 r = efi_get_child_controllers(efiobj,
3884 driver_image_handle,
3885 &number_of_children,
3886 &child_handle_buffer);
3887 if (r != EFI_SUCCESS)
3888 return r;
3889 sole_child = (number_of_children == 1);
3890
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003891 if (child_handle) {
3892 number_of_children = 1;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003893 free(child_handle_buffer);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003894 child_handle_buffer = &child_handle;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003895 }
3896
3897 /* Get the driver binding protocol */
3898 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3899 &efi_guid_driver_binding_protocol,
3900 (void **)&binding_protocol,
3901 driver_image_handle, NULL,
3902 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003903 if (r != EFI_SUCCESS) {
3904 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003905 goto out;
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003906 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003907 /* Remove the children */
3908 if (number_of_children) {
3909 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3910 controller_handle,
3911 number_of_children,
3912 child_handle_buffer));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003913 if (r != EFI_SUCCESS) {
3914 r = EFI_DEVICE_ERROR;
3915 goto out;
3916 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003917 }
3918 /* Remove the driver */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003919 if (!child_handle || sole_child) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003920 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3921 controller_handle,
3922 0, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003923 if (r != EFI_SUCCESS) {
3924 r = EFI_DEVICE_ERROR;
3925 goto out;
3926 }
3927 }
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003928 efi_close_protocol(driver_image_handle,
3929 &efi_guid_driver_binding_protocol,
3930 driver_image_handle, NULL);
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003931 r = EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003932out:
3933 if (!child_handle)
3934 free(child_handle_buffer);
3935 return EFI_EXIT(r);
3936}
3937
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003938static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003939 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003940 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3941 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003942 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003943 },
3944 .raise_tpl = efi_raise_tpl,
3945 .restore_tpl = efi_restore_tpl,
3946 .allocate_pages = efi_allocate_pages_ext,
3947 .free_pages = efi_free_pages_ext,
3948 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003949 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003950 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003951 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003952 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003953 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003954 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003955 .close_event = efi_close_event,
3956 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003957 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003958 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003959 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003960 .handle_protocol = efi_handle_protocol,
3961 .reserved = NULL,
3962 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003963 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003964 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003965 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003966 .load_image = efi_load_image,
3967 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003968 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003969 .unload_image = efi_unload_image,
3970 .exit_boot_services = efi_exit_boot_services,
3971 .get_next_monotonic_count = efi_get_next_monotonic_count,
3972 .stall = efi_stall,
3973 .set_watchdog_timer = efi_set_watchdog_timer,
3974 .connect_controller = efi_connect_controller,
3975 .disconnect_controller = efi_disconnect_controller,
3976 .open_protocol = efi_open_protocol,
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003977 .close_protocol = efi_close_protocol_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003978 .open_protocol_information = efi_open_protocol_information,
3979 .protocols_per_handle = efi_protocols_per_handle,
3980 .locate_handle_buffer = efi_locate_handle_buffer,
3981 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003982 .install_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003983 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003984 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003985 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003986 .calculate_crc32 = efi_calculate_crc32,
3987 .copy_mem = efi_copy_mem,
3988 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003989 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003990};
3991
Simon Glass156ccbc2022-01-23 12:55:12 -07003992static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003993
Alexander Graf3c63db92016-10-14 13:45:30 +02003994struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003995 .hdr = {
3996 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003997 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003998 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003999 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02004000 .fw_vendor = firmware_vendor,
4001 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02004002 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01004003 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004004 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01004005};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004006
4007/**
4008 * efi_initialize_system_table() - Initialize system table
4009 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02004010 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004011 */
4012efi_status_t efi_initialize_system_table(void)
4013{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004014 efi_status_t ret;
4015
4016 /* Allocate configuration table array */
4017 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4018 EFI_MAX_CONFIGURATION_TABLES *
4019 sizeof(struct efi_configuration_table),
4020 (void **)&systab.tables);
4021
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004022 /*
4023 * These entries will be set to NULL in ExitBootServices(). To avoid
4024 * relocation in SetVirtualAddressMap(), set them dynamically.
4025 */
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004026 systab.con_in_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004027 systab.con_in = &efi_con_in;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004028 systab.con_out_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004029 systab.con_out = &efi_con_out;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004030 systab.stderr_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004031 systab.std_err = &efi_con_out;
4032 systab.boottime = &efi_boot_services;
4033
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02004034 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004035 efi_update_table_header_crc32(&systab.hdr);
4036 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4037 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004038
4039 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004040}