blob: caaab685ee0c379dbb579bad904d6c5210d524a0 [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 */
38LIST_HEAD(efi_event_queue);
39
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() */
47LIST_HEAD(efi_register_notify_events);
48
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
Heinrich Schuchardt914df752019-02-09 14:10:39 +010062/* 1 if inside U-Boot code, 0 if inside EFI payload code */
63static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040064static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010065/* GUID of the device tree table */
66const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010067/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
68const efi_guid_t efi_guid_driver_binding_protocol =
69 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040070
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010071/* event group ExitBootServices() invoked */
72const efi_guid_t efi_guid_event_group_exit_boot_services =
73 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt43eaf5b2021-11-16 18:46:27 +010074/* event group before ExitBootServices() invoked */
75const efi_guid_t efi_guid_event_group_before_exit_boot_services =
76 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010077/* event group SetVirtualAddressMap() invoked */
78const efi_guid_t efi_guid_event_group_virtual_address_change =
79 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
80/* event group memory map changed */
81const efi_guid_t efi_guid_event_group_memory_map_change =
82 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
83/* event group boot manager about to boot */
84const efi_guid_t efi_guid_event_group_ready_to_boot =
85 EFI_EVENT_GROUP_READY_TO_BOOT;
86/* event group ResetSystem() invoked (before ExitBootServices) */
87const efi_guid_t efi_guid_event_group_reset_system =
88 EFI_EVENT_GROUP_RESET_SYSTEM;
Heinrich Schuchardtb6f11092020-12-04 03:33:41 +010089/* GUIDs of the Load File and Load File2 protocols */
90const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
91const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090092/* GUID of the SMBIOS table */
93const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010094
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010095static efi_status_t EFIAPI efi_disconnect_controller(
96 efi_handle_t controller_handle,
97 efi_handle_t driver_image_handle,
98 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010099
Rob Clarkc160d2f2017-07-27 08:04:18 -0400100/* Called on every callback entry */
101int __efi_entry_check(void)
102{
103 int ret = entry_count++ == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200104#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clarkc160d2f2017-07-27 08:04:18 -0400105 assert(efi_gd);
106 app_gd = gd;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200107 set_gd(efi_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400108#endif
109 return ret;
110}
111
112/* Called on every callback exit */
113int __efi_exit_check(void)
114{
115 int ret = --entry_count == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200116#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200117 set_gd(app_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400118#endif
119 return ret;
120}
121
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200122/**
123 * efi_save_gd() - save global data register
124 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200125 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200126 * As this register may be overwritten by an EFI payload we save it here
127 * and restore it on every callback entered.
128 *
129 * This function is called after relocation from initr_reloc_global_data().
130 */
Alexander Grafbee91162016-03-04 01:09:59 +0100131void efi_save_gd(void)
132{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200133#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100134 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600135#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100136}
137
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200138/**
139 * efi_restore_gd() - restore global data register
140 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200141 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200142 * Restore it after returning from the UEFI world to the value saved via
143 * efi_save_gd().
Rob Clarkc160d2f2017-07-27 08:04:18 -0400144 */
Alexander Grafbee91162016-03-04 01:09:59 +0100145void efi_restore_gd(void)
146{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200147#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100148 /* Only restore if we're already in EFI context */
149 if (!efi_gd)
150 return;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200151 set_gd(efi_gd);
Simon Glass65e4c0b2016-09-25 15:27:35 -0600152#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100153}
154
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200155/**
Mario Six78a88f72018-07-10 08:40:17 +0200156 * indent_string() - returns a string for indenting with two spaces per level
157 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100158 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200159 * A maximum of ten indent levels is supported. Higher indent levels will be
160 * truncated.
161 *
Mario Six78a88f72018-07-10 08:40:17 +0200162 * Return: A string for indenting with two spaces per level is
163 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400164 */
165static const char *indent_string(int level)
166{
167 const char *indent = " ";
168 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100169
Rob Clarkaf65db82017-07-27 08:04:19 -0400170 level = min(max, level * 2);
171 return &indent[max - level];
172}
173
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200174const char *__efi_nesting(void)
175{
176 return indent_string(nesting_level);
177}
178
Rob Clarkaf65db82017-07-27 08:04:19 -0400179const char *__efi_nesting_inc(void)
180{
181 return indent_string(nesting_level++);
182}
183
184const char *__efi_nesting_dec(void)
185{
186 return indent_string(--nesting_level);
187}
188
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200189/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200190 * efi_event_is_queued() - check if an event is queued
191 *
192 * @event: event
193 * Return: true if event is queued
194 */
195static bool efi_event_is_queued(struct efi_event *event)
196{
197 return !!event->queue_link.next;
198}
199
200/**
201 * efi_process_event_queue() - process event queue
202 */
203static void efi_process_event_queue(void)
204{
205 while (!list_empty(&efi_event_queue)) {
206 struct efi_event *event;
207 efi_uintn_t old_tpl;
208
209 event = list_first_entry(&efi_event_queue, struct efi_event,
210 queue_link);
211 if (efi_tpl >= event->notify_tpl)
212 return;
213 list_del(&event->queue_link);
214 event->queue_link.next = NULL;
215 event->queue_link.prev = NULL;
216 /* Events must be executed at the event's TPL */
217 old_tpl = efi_tpl;
218 efi_tpl = event->notify_tpl;
219 EFI_CALL_VOID(event->notify_function(event,
220 event->notify_context));
221 efi_tpl = old_tpl;
222 if (event->type == EVT_NOTIFY_SIGNAL)
223 event->is_signaled = 0;
224 }
225}
226
227/**
Mario Six78a88f72018-07-10 08:40:17 +0200228 * efi_queue_event() - queue an EFI event
229 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200230 *
231 * This function queues the notification function of the event for future
232 * execution.
233 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200234 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200235static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200236{
Heinrich Schuchardt2b8568f2020-03-06 21:56:10 +0100237 struct efi_event *item;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200238
239 if (!event->notify_function)
240 return;
241
242 if (!efi_event_is_queued(event)) {
243 /*
244 * Events must be notified in order of decreasing task priority
245 * level. Insert the new event accordingly.
246 */
247 list_for_each_entry(item, &efi_event_queue, queue_link) {
248 if (item->notify_tpl < event->notify_tpl) {
249 list_add_tail(&event->queue_link,
250 &item->queue_link);
251 event = NULL;
252 break;
253 }
254 }
255 if (event)
256 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardtb7d186f2020-12-28 00:25:34 +0100257 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200258 }
259}
260
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200261/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200262 * is_valid_tpl() - check if the task priority level is valid
263 *
264 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200265 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200266 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100267static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200268{
269 switch (tpl) {
270 case TPL_APPLICATION:
271 case TPL_CALLBACK:
272 case TPL_NOTIFY:
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200273 return EFI_SUCCESS;
274 default:
275 return EFI_INVALID_PARAMETER;
276 }
277}
278
279/**
Mario Six78a88f72018-07-10 08:40:17 +0200280 * efi_signal_event() - signal an EFI event
281 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100282 *
Heinrich Schuchardt2a0f80f2020-12-28 00:59:09 +0100283 * This function signals an event. If the event belongs to an event group, all
284 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100285 * their notification function is queued.
286 *
287 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100288 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200289void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100290{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200291 if (event->is_signaled)
292 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100293 if (event->group) {
294 struct efi_event *evt;
295
296 /*
297 * The signaled state has to set before executing any
298 * notification function
299 */
300 list_for_each_entry(evt, &efi_events, link) {
301 if (!evt->group || guidcmp(evt->group, event->group))
302 continue;
303 if (evt->is_signaled)
304 continue;
305 evt->is_signaled = true;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100306 }
307 list_for_each_entry(evt, &efi_events, link) {
308 if (!evt->group || guidcmp(evt->group, event->group))
309 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200310 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100311 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200312 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100313 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200314 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100315 }
316}
317
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200318/**
Mario Six78a88f72018-07-10 08:40:17 +0200319 * efi_raise_tpl() - raise the task priority level
320 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200321 *
322 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200323 *
Mario Six78a88f72018-07-10 08:40:17 +0200324 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * details.
326 *
327 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200328 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100329static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100330{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100331 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200332
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200333 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200334
335 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200336 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200337 efi_tpl = new_tpl;
338 if (efi_tpl > TPL_HIGH_LEVEL)
339 efi_tpl = TPL_HIGH_LEVEL;
340
341 EFI_EXIT(EFI_SUCCESS);
342 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100343}
344
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200345/**
Mario Six78a88f72018-07-10 08:40:17 +0200346 * efi_restore_tpl() - lower the task priority level
347 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200348 *
349 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200350 *
Mario Six78a88f72018-07-10 08:40:17 +0200351 * See the Unified Extensible Firmware Interface (UEFI) specification for
352 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200353 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100354static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100355{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200356 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200357
358 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200359 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200360 efi_tpl = old_tpl;
361 if (efi_tpl > TPL_HIGH_LEVEL)
362 efi_tpl = TPL_HIGH_LEVEL;
363
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100364 /*
365 * Lowering the TPL may have made queued events eligible for execution.
366 */
367 efi_timer_check();
368
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200369 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100370}
371
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200372/**
Mario Six78a88f72018-07-10 08:40:17 +0200373 * efi_allocate_pages_ext() - allocate memory pages
374 * @type: type of allocation to be performed
375 * @memory_type: usage type of the allocated memory
376 * @pages: number of pages to be allocated
377 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200378 *
379 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200380 *
Mario Six78a88f72018-07-10 08:40:17 +0200381 * See the Unified Extensible Firmware Interface (UEFI) specification for
382 * details.
383 *
384 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200385 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900386static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100387 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900388 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100389{
390 efi_status_t r;
391
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100392 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100393 r = efi_allocate_pages(type, memory_type, pages, memory);
394 return EFI_EXIT(r);
395}
396
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200397/**
Mario Six78a88f72018-07-10 08:40:17 +0200398 * efi_free_pages_ext() - Free memory pages.
399 * @memory: start of the memory area to be freed
400 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200401 *
402 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200403 *
Mario Six78a88f72018-07-10 08:40:17 +0200404 * See the Unified Extensible Firmware Interface (UEFI) specification for
405 * details.
406 *
407 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200408 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900409static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100410 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100411{
412 efi_status_t r;
413
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900414 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100415 r = efi_free_pages(memory, pages);
416 return EFI_EXIT(r);
417}
418
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200419/**
Mario Six78a88f72018-07-10 08:40:17 +0200420 * efi_get_memory_map_ext() - get map describing memory usage
421 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
422 * on exit the size of the copied memory map
423 * @memory_map: buffer to which the memory map is written
424 * @map_key: key for the memory map
425 * @descriptor_size: size of an individual memory descriptor
426 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200427 *
428 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200429 *
Mario Six78a88f72018-07-10 08:40:17 +0200430 * See the Unified Extensible Firmware Interface (UEFI) specification for
431 * details.
432 *
433 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200434 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900435static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100436 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900437 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100438 efi_uintn_t *map_key,
439 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900440 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100441{
442 efi_status_t r;
443
444 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
445 map_key, descriptor_size, descriptor_version);
446 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
447 descriptor_size, descriptor_version);
448 return EFI_EXIT(r);
449}
450
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200451/**
Mario Six78a88f72018-07-10 08:40:17 +0200452 * efi_allocate_pool_ext() - allocate memory from pool
453 * @pool_type: type of the pool from which memory is to be allocated
454 * @size: number of bytes to be allocated
455 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200456 *
457 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200458 *
Mario Six78a88f72018-07-10 08:40:17 +0200459 * See the Unified Extensible Firmware Interface (UEFI) specification for
460 * details.
461 *
462 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200463 */
Stefan Brünsead12742016-10-09 22:17:18 +0200464static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100465 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200466 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100467{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100468 efi_status_t r;
469
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100470 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200471 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100472 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100473}
474
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200475/**
Mario Six78a88f72018-07-10 08:40:17 +0200476 * efi_free_pool_ext() - free memory from pool
477 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200478 *
479 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200480 *
Mario Six78a88f72018-07-10 08:40:17 +0200481 * See the Unified Extensible Firmware Interface (UEFI) specification for
482 * details.
483 *
484 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200485 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200486static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100487{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100488 efi_status_t r;
489
490 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200491 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100492 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100493}
494
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200495/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200496 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100497 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200498 * @handle: handle to be added
499 *
500 * The protocols list is initialized. The handle is added to the list of known
501 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100502 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200503void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100504{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200505 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100506 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200507 INIT_LIST_HEAD(&handle->protocols);
508 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100509}
510
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200511/**
Mario Six78a88f72018-07-10 08:40:17 +0200512 * efi_create_handle() - create handle
513 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200514 *
Mario Six78a88f72018-07-10 08:40:17 +0200515 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200516 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100517efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200518{
519 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200520
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200521 obj = calloc(1, sizeof(struct efi_object));
522 if (!obj)
523 return EFI_OUT_OF_RESOURCES;
524
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100525 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200526 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200527
528 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200529}
530
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200531/**
Mario Six78a88f72018-07-10 08:40:17 +0200532 * efi_search_protocol() - find a protocol on a handle.
533 * @handle: handle
534 * @protocol_guid: GUID of the protocol
535 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100536 *
Mario Six78a88f72018-07-10 08:40:17 +0200537 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100538 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100539efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100540 const efi_guid_t *protocol_guid,
541 struct efi_handler **handler)
542{
543 struct efi_object *efiobj;
544 struct list_head *lhandle;
545
546 if (!handle || !protocol_guid)
547 return EFI_INVALID_PARAMETER;
548 efiobj = efi_search_obj(handle);
549 if (!efiobj)
550 return EFI_INVALID_PARAMETER;
551 list_for_each(lhandle, &efiobj->protocols) {
552 struct efi_handler *protocol;
553
554 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +0100555 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100556 if (handler)
557 *handler = protocol;
558 return EFI_SUCCESS;
559 }
560 }
561 return EFI_NOT_FOUND;
562}
563
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200564/**
Mario Six78a88f72018-07-10 08:40:17 +0200565 * efi_remove_protocol() - delete protocol from a handle
566 * @handle: handle from which the protocol shall be deleted
567 * @protocol: GUID of the protocol to be deleted
568 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100569 *
Mario Six78a88f72018-07-10 08:40:17 +0200570 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100571 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100572efi_status_t efi_remove_protocol(const efi_handle_t handle,
573 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100574 void *protocol_interface)
575{
576 struct efi_handler *handler;
577 efi_status_t ret;
578
579 ret = efi_search_protocol(handle, protocol, &handler);
580 if (ret != EFI_SUCCESS)
581 return ret;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200582 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200583 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100584 list_del(&handler->link);
585 free(handler);
586 return EFI_SUCCESS;
587}
588
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200589/**
Mario Six78a88f72018-07-10 08:40:17 +0200590 * efi_remove_all_protocols() - delete all protocols from a handle
591 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100592 *
Mario Six78a88f72018-07-10 08:40:17 +0200593 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100594 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100595static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100596{
597 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100598 struct efi_handler *protocol;
599 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100600
601 efiobj = efi_search_obj(handle);
602 if (!efiobj)
603 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100604 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100605 efi_status_t ret;
606
Heinrich Schuchardt66028932022-03-09 19:56:23 +0100607 ret = efi_remove_protocol(handle, &protocol->guid,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100608 protocol->protocol_interface);
609 if (ret != EFI_SUCCESS)
610 return ret;
611 }
612 return EFI_SUCCESS;
613}
614
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200615/**
Mario Six78a88f72018-07-10 08:40:17 +0200616 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100617 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200618 * @handle: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100619 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200620void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100621{
Etienne Carriere79325482022-09-07 10:20:13 +0200622 efi_status_t ret;
623
624 ret = efi_remove_all_protocols(handle);
625 if (ret == EFI_INVALID_PARAMETER) {
626 log_err("Can't remove invalid handle %p\n", handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100627 return;
Etienne Carriere79325482022-09-07 10:20:13 +0200628 }
629
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200630 list_del(&handle->link);
631 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100632}
633
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200634/**
Mario Six78a88f72018-07-10 08:40:17 +0200635 * efi_is_event() - check if a pointer is a valid event
636 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100637 *
Mario Six78a88f72018-07-10 08:40:17 +0200638 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100639 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100640static efi_status_t efi_is_event(const struct efi_event *event)
641{
642 const struct efi_event *evt;
643
644 if (!event)
645 return EFI_INVALID_PARAMETER;
646 list_for_each_entry(evt, &efi_events, link) {
647 if (evt == event)
648 return EFI_SUCCESS;
649 }
650 return EFI_INVALID_PARAMETER;
651}
Alexander Grafbee91162016-03-04 01:09:59 +0100652
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200653/**
Mario Six78a88f72018-07-10 08:40:17 +0200654 * efi_create_event() - create an event
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200655 *
Mario Six78a88f72018-07-10 08:40:17 +0200656 * @type: type of the event to create
657 * @notify_tpl: task priority level of the event
658 * @notify_function: notification function of the event
659 * @notify_context: pointer passed to the notification function
660 * @group: event group
661 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200662 *
663 * This function is used inside U-Boot code to create an event.
664 *
665 * For the API function implementing the CreateEvent service see
666 * efi_create_event_ext.
667 *
Mario Six78a88f72018-07-10 08:40:17 +0200668 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200669 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100670efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200671 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200672 struct efi_event *event,
673 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100674 void *notify_context, efi_guid_t *group,
675 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100676{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100677 struct efi_event *evt;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200678 efi_status_t ret;
679 int pool_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200680
Jonathan Graya95343b2017-03-12 19:26:07 +1100681 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200682 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100683
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200684 switch (type) {
685 case 0:
686 case EVT_TIMER:
687 case EVT_NOTIFY_SIGNAL:
688 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
689 case EVT_NOTIFY_WAIT:
690 case EVT_TIMER | EVT_NOTIFY_WAIT:
691 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200692 pool_type = EFI_BOOT_SERVICES_DATA;
693 break;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200694 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200695 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200696 break;
697 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200698 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200699 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100700
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100701 /*
702 * The UEFI specification requires event notification levels to be
703 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
704 *
705 * Parameter NotifyTpl should not be checked if it is not used.
706 */
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900707 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100708 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
709 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200710 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100711
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200712 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
713 (void **)&evt);
714 if (ret != EFI_SUCCESS)
715 return ret;
716 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100717 evt->type = type;
718 evt->notify_tpl = notify_tpl;
719 evt->notify_function = notify_function;
720 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100721 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200722 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100723 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100724 list_add_tail(&evt->link, &efi_events);
725 *event = evt;
726 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100727}
728
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200729/*
Mario Six78a88f72018-07-10 08:40:17 +0200730 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100731 *
Mario Six78a88f72018-07-10 08:40:17 +0200732 * @type: type of the event to create
733 * @notify_tpl: task priority level of the event
734 * @notify_function: notification function of the event
735 * @notify_context: pointer passed to the notification function
736 * @event: created event
737 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100738 *
739 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100740 *
Mario Six78a88f72018-07-10 08:40:17 +0200741 * See the Unified Extensible Firmware Interface (UEFI) specification for
742 * details.
743 *
744 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100745 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100746static
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100747efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
748 void (EFIAPI *notify_function) (
749 struct efi_event *event,
750 void *context),
751 void *notify_context,
752 efi_guid_t *event_group,
753 struct efi_event **event)
754{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200755 efi_status_t ret;
756
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100757 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100758 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200759
760 /*
761 * The allowable input parameters are the same as in CreateEvent()
762 * except for the following two disallowed event types.
763 */
764 switch (type) {
765 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
766 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
767 ret = EFI_INVALID_PARAMETER;
768 goto out;
769 }
770
771 ret = efi_create_event(type, notify_tpl, notify_function,
772 notify_context, event_group, event);
773out:
774 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100775}
776
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200777/**
Mario Six78a88f72018-07-10 08:40:17 +0200778 * efi_create_event_ext() - create an event
779 * @type: type of the event to create
780 * @notify_tpl: task priority level of the event
781 * @notify_function: notification function of the event
782 * @notify_context: pointer passed to the notification function
783 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200784 *
785 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200786 *
Mario Six78a88f72018-07-10 08:40:17 +0200787 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * details.
789 *
790 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200791 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200792static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100793 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200794 void (EFIAPI *notify_function) (
795 struct efi_event *event,
796 void *context),
797 void *notify_context, struct efi_event **event)
798{
799 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
800 notify_context);
801 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100802 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200803}
804
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200805/**
Mario Six78a88f72018-07-10 08:40:17 +0200806 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200807 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200808 * Check if a timer event has occurred or a queued notification function should
809 * be called.
810 *
Alexander Grafbee91162016-03-04 01:09:59 +0100811 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200812 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100813 */
814void efi_timer_check(void)
815{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100816 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100817 u64 now = timer_get_us();
818
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100819 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200820 if (!timers_enabled)
821 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100822 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200823 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100824 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200825 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100826 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200827 break;
828 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100829 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200830 break;
831 default:
832 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200833 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100834 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200835 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100836 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200837 efi_process_event_queue();
Stefan Roese29caf932022-09-02 14:10:46 +0200838 schedule();
Alexander Grafbee91162016-03-04 01:09:59 +0100839}
840
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200841/**
Mario Six78a88f72018-07-10 08:40:17 +0200842 * efi_set_timer() - set the trigger time for a timer event or stop the event
843 * @event: event for which the timer is set
844 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200845 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200846 *
847 * This is the function for internal usage in U-Boot. For the API function
848 * implementing the SetTimer service see efi_set_timer_ext.
849 *
Mario Six78a88f72018-07-10 08:40:17 +0200850 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200851 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200852efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200853 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100854{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100855 /* Check that the event is valid */
856 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
857 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100858
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200859 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200860 * The parameter defines a multiple of 100 ns.
861 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200862 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200863 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100864
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100865 switch (type) {
866 case EFI_TIMER_STOP:
867 event->trigger_next = -1ULL;
868 break;
869 case EFI_TIMER_PERIODIC:
870 case EFI_TIMER_RELATIVE:
871 event->trigger_next = timer_get_us() + trigger_time;
872 break;
873 default:
874 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100875 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100876 event->trigger_type = type;
877 event->trigger_time = trigger_time;
878 event->is_signaled = false;
879 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200880}
881
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200882/**
Mario Six78a88f72018-07-10 08:40:17 +0200883 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
884 * event
885 * @event: event for which the timer is set
886 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200887 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200888 *
889 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200890 *
Mario Six78a88f72018-07-10 08:40:17 +0200891 * See the Unified Extensible Firmware Interface (UEFI) specification for
892 * details.
893 *
894 *
895 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200896 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200897static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
898 enum efi_timer_delay type,
899 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200900{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900901 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200902 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100903}
904
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200905/**
Mario Six78a88f72018-07-10 08:40:17 +0200906 * efi_wait_for_event() - wait for events to be signaled
907 * @num_events: number of events to be waited for
908 * @event: events to be waited for
909 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200910 *
911 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200912 *
Mario Six78a88f72018-07-10 08:40:17 +0200913 * See the Unified Extensible Firmware Interface (UEFI) specification for
914 * details.
915 *
916 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200917 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100918static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200919 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100920 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100921{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100922 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100923
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100924 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100925
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200926 /* Check parameters */
927 if (!num_events || !event)
928 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200929 /* Check TPL */
930 if (efi_tpl != TPL_APPLICATION)
931 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200932 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100933 if (efi_is_event(event[i]) != EFI_SUCCESS)
934 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200935 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
936 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200937 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200938 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200939 }
940
941 /* Wait for signal */
942 for (;;) {
943 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200944 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200945 goto out;
946 }
947 /* Allow events to occur. */
948 efi_timer_check();
949 }
950
951out:
952 /*
953 * Reset the signal which is passed to the caller to allow periodic
954 * events to occur.
955 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200956 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200957 if (index)
958 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100959
960 return EFI_EXIT(EFI_SUCCESS);
961}
962
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200963/**
Mario Six78a88f72018-07-10 08:40:17 +0200964 * efi_signal_event_ext() - signal an EFI event
965 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200966 *
967 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200968 *
969 * See the Unified Extensible Firmware Interface (UEFI) specification for
970 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200971 *
972 * This functions sets the signaled state of the event and queues the
973 * notification function for execution.
974 *
Mario Six78a88f72018-07-10 08:40:17 +0200975 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200976 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200977static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100978{
979 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100980 if (efi_is_event(event) != EFI_SUCCESS)
981 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200982 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +0100983 return EFI_EXIT(EFI_SUCCESS);
984}
985
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200986/**
Mario Six78a88f72018-07-10 08:40:17 +0200987 * efi_close_event() - close an EFI event
988 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200989 *
990 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200991 *
Mario Six78a88f72018-07-10 08:40:17 +0200992 * See the Unified Extensible Firmware Interface (UEFI) specification for
993 * details.
994 *
995 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200996 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200997static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100998{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200999 struct efi_register_notify_event *item, *next;
1000
Alexander Grafbee91162016-03-04 01:09:59 +01001001 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001002 if (efi_is_event(event) != EFI_SUCCESS)
1003 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001004
1005 /* Remove protocol notify registrations for the event */
1006 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1007 link) {
1008 if (event == item->event) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001009 struct efi_protocol_notification *hitem, *hnext;
1010
1011 /* Remove signaled handles */
1012 list_for_each_entry_safe(hitem, hnext, &item->handles,
1013 link) {
1014 list_del(&hitem->link);
1015 free(hitem);
1016 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001017 list_del(&item->link);
1018 free(item);
1019 }
1020 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +02001021 /* Remove event from queue */
1022 if (efi_event_is_queued(event))
1023 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001024
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001025 list_del(&event->link);
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001026 efi_free_pool(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001027 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001028}
1029
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001030/**
Mario Six78a88f72018-07-10 08:40:17 +02001031 * efi_check_event() - check if an event is signaled
1032 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001033 *
1034 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001035 *
Mario Six78a88f72018-07-10 08:40:17 +02001036 * See the Unified Extensible Firmware Interface (UEFI) specification for
1037 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001038 *
Mario Six78a88f72018-07-10 08:40:17 +02001039 * If an event is not signaled yet, the notification function is queued. The
1040 * signaled state is cleared.
1041 *
1042 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001043 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001044static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001045{
1046 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001047 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001048 if (efi_is_event(event) != EFI_SUCCESS ||
1049 event->type & EVT_NOTIFY_SIGNAL)
1050 return EFI_EXIT(EFI_INVALID_PARAMETER);
1051 if (!event->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001052 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001053 if (event->is_signaled) {
1054 event->is_signaled = false;
1055 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001056 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001057 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001058}
1059
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001060/**
Mario Six78a88f72018-07-10 08:40:17 +02001061 * efi_search_obj() - find the internal EFI object for a handle
1062 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001063 *
Mario Six78a88f72018-07-10 08:40:17 +02001064 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001065 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001066struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001067{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001068 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001069
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001070 if (!handle)
1071 return NULL;
1072
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001073 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001074 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001075 return efiobj;
1076 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001077 return NULL;
1078}
1079
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001080/**
Mario Six78a88f72018-07-10 08:40:17 +02001081 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1082 * to a protocol
1083 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001084 *
Mario Six78a88f72018-07-10 08:40:17 +02001085 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001086 */
1087static struct efi_open_protocol_info_entry *efi_create_open_info(
1088 struct efi_handler *handler)
1089{
1090 struct efi_open_protocol_info_item *item;
1091
1092 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1093 if (!item)
1094 return NULL;
1095 /* Append the item to the open protocol info list. */
1096 list_add_tail(&item->link, &handler->open_infos);
1097
1098 return &item->info;
1099}
1100
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001101/**
Mario Six78a88f72018-07-10 08:40:17 +02001102 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1103 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001104 *
Mario Six78a88f72018-07-10 08:40:17 +02001105 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001106 */
1107static efi_status_t efi_delete_open_info(
1108 struct efi_open_protocol_info_item *item)
1109{
1110 list_del(&item->link);
1111 free(item);
1112 return EFI_SUCCESS;
1113}
1114
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001115/**
Mario Six78a88f72018-07-10 08:40:17 +02001116 * efi_add_protocol() - install new protocol on a handle
1117 * @handle: handle on which the protocol shall be installed
1118 * @protocol: GUID of the protocol to be installed
1119 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001120 *
Mario Six78a88f72018-07-10 08:40:17 +02001121 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001122 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001123efi_status_t efi_add_protocol(const efi_handle_t handle,
1124 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001125 void *protocol_interface)
1126{
1127 struct efi_object *efiobj;
1128 struct efi_handler *handler;
1129 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001130 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001131
1132 efiobj = efi_search_obj(handle);
1133 if (!efiobj)
1134 return EFI_INVALID_PARAMETER;
1135 ret = efi_search_protocol(handle, protocol, NULL);
1136 if (ret != EFI_NOT_FOUND)
1137 return EFI_INVALID_PARAMETER;
1138 handler = calloc(1, sizeof(struct efi_handler));
1139 if (!handler)
1140 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001141 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001142 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001143 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001144 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001145
1146 /* Notify registered events */
1147 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001148 if (!guidcmp(protocol, &event->protocol)) {
1149 struct efi_protocol_notification *notif;
1150
1151 notif = calloc(1, sizeof(*notif));
1152 if (!notif) {
1153 list_del(&handler->link);
1154 free(handler);
1155 return EFI_OUT_OF_RESOURCES;
1156 }
1157 notif->handle = handle;
1158 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001159 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001160 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001161 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001162 }
1163
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001164 if (!guidcmp(&efi_guid_device_path, protocol))
1165 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001166 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001167}
1168
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001169/**
Mario Six78a88f72018-07-10 08:40:17 +02001170 * efi_install_protocol_interface() - install protocol interface
1171 * @handle: handle on which the protocol shall be installed
1172 * @protocol: GUID of the protocol to be installed
1173 * @protocol_interface_type: type of the interface to be installed,
1174 * always EFI_NATIVE_INTERFACE
1175 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001176 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001177 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001178 *
Mario Six78a88f72018-07-10 08:40:17 +02001179 * See the Unified Extensible Firmware Interface (UEFI) specification for
1180 * details.
1181 *
1182 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001183 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001184static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001185 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001186 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001187{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001188 efi_status_t r;
1189
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001190 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001191 protocol_interface);
1192
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001193 if (!handle || !protocol ||
1194 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1195 r = EFI_INVALID_PARAMETER;
1196 goto out;
1197 }
1198
1199 /* Create new handle if requested. */
1200 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001201 r = efi_create_handle(handle);
1202 if (r != EFI_SUCCESS)
1203 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001204 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001205 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001206 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001207 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001208 /* Add new protocol */
1209 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001210out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001211 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001212}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001213
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001214/**
Mario Six78a88f72018-07-10 08:40:17 +02001215 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001216 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001217 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001218 * @number_of_drivers: number of child controllers
1219 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001220 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001221 * The allocated buffer has to be freed with free().
1222 *
Mario Six78a88f72018-07-10 08:40:17 +02001223 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001224 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001225static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001226 const efi_guid_t *protocol,
1227 efi_uintn_t *number_of_drivers,
1228 efi_handle_t **driver_handle_buffer)
1229{
1230 struct efi_handler *handler;
1231 struct efi_open_protocol_info_item *item;
1232 efi_uintn_t count = 0, i;
1233 bool duplicate;
1234
1235 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001236 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001237 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001238 continue;
1239 list_for_each_entry(item, &handler->open_infos, link) {
1240 if (item->info.attributes &
1241 EFI_OPEN_PROTOCOL_BY_DRIVER)
1242 ++count;
1243 }
1244 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001245 *number_of_drivers = 0;
1246 if (!count) {
1247 *driver_handle_buffer = NULL;
1248 return EFI_SUCCESS;
1249 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001250 /*
1251 * Create buffer. In case of duplicate driver assignments the buffer
1252 * will be too large. But that does not harm.
1253 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001254 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1255 if (!*driver_handle_buffer)
1256 return EFI_OUT_OF_RESOURCES;
1257 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001258 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001259 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001260 continue;
1261 list_for_each_entry(item, &handler->open_infos, link) {
1262 if (item->info.attributes &
1263 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1264 /* Check this is a new driver */
1265 duplicate = false;
1266 for (i = 0; i < *number_of_drivers; ++i) {
1267 if ((*driver_handle_buffer)[i] ==
1268 item->info.agent_handle)
1269 duplicate = true;
1270 }
1271 /* Copy handle to buffer */
1272 if (!duplicate) {
1273 i = (*number_of_drivers)++;
1274 (*driver_handle_buffer)[i] =
1275 item->info.agent_handle;
1276 }
1277 }
1278 }
1279 }
1280 return EFI_SUCCESS;
1281}
1282
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001283/**
Mario Six78a88f72018-07-10 08:40:17 +02001284 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001285 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001286 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001287 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001288 *
1289 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001290 *
Mario Six78a88f72018-07-10 08:40:17 +02001291 * See the Unified Extensible Firmware Interface (UEFI) specification for
1292 * details.
1293 *
1294 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001295 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001296static efi_status_t efi_disconnect_all_drivers
1297 (efi_handle_t handle,
1298 const efi_guid_t *protocol,
1299 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001300{
1301 efi_uintn_t number_of_drivers;
1302 efi_handle_t *driver_handle_buffer;
1303 efi_status_t r, ret;
1304
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001305 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001306 &driver_handle_buffer);
1307 if (ret != EFI_SUCCESS)
1308 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001309 if (!number_of_drivers)
1310 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001311 ret = EFI_NOT_FOUND;
1312 while (number_of_drivers) {
1313 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001314 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001315 driver_handle_buffer[--number_of_drivers],
1316 child_handle));
1317 if (r == EFI_SUCCESS)
1318 ret = r;
1319 }
1320 free(driver_handle_buffer);
1321 return ret;
1322}
1323
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001324/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001325 * efi_uninstall_protocol() - uninstall protocol interface
1326 *
Mario Six78a88f72018-07-10 08:40:17 +02001327 * @handle: handle from which the protocol shall be removed
1328 * @protocol: GUID of the protocol to be removed
1329 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001330 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001331 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001332 *
1333 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001334 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001335static efi_status_t efi_uninstall_protocol
1336 (efi_handle_t handle, const efi_guid_t *protocol,
1337 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001338{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001339 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001340 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001341 struct efi_open_protocol_info_item *item;
1342 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001343 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001344
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001345 /* Check handle */
1346 efiobj = efi_search_obj(handle);
1347 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001348 r = EFI_INVALID_PARAMETER;
1349 goto out;
1350 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001351 /* Find the protocol on the handle */
1352 r = efi_search_protocol(handle, protocol, &handler);
1353 if (r != EFI_SUCCESS)
1354 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001355 /* Disconnect controllers */
1356 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001357 /* Close protocol */
1358 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1359 if (item->info.attributes ==
1360 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1361 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1362 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1363 list_del(&item->link);
1364 }
1365 if (!list_empty(&handler->open_infos)) {
1366 r = EFI_ACCESS_DENIED;
1367 goto out;
1368 }
1369 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001370out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001371 return r;
1372}
1373
1374/**
1375 * efi_uninstall_protocol_interface() - uninstall protocol interface
1376 * @handle: handle from which the protocol shall be removed
1377 * @protocol: GUID of the protocol to be removed
1378 * @protocol_interface: interface to be removed
1379 *
1380 * This function implements the UninstallProtocolInterface service.
1381 *
1382 * See the Unified Extensible Firmware Interface (UEFI) specification for
1383 * details.
1384 *
1385 * Return: status code
1386 */
1387static efi_status_t EFIAPI efi_uninstall_protocol_interface
1388 (efi_handle_t handle, const efi_guid_t *protocol,
1389 void *protocol_interface)
1390{
1391 efi_status_t ret;
1392
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001393 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001394
1395 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1396 if (ret != EFI_SUCCESS)
1397 goto out;
1398
1399 /* If the last protocol has been removed, delete the handle. */
1400 if (list_empty(&handle->protocols)) {
1401 list_del(&handle->link);
1402 free(handle);
1403 }
1404out:
1405 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001406}
1407
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001408/**
Mario Six78a88f72018-07-10 08:40:17 +02001409 * efi_register_protocol_notify() - register an event for notification when a
1410 * protocol is installed.
1411 * @protocol: GUID of the protocol whose installation shall be notified
1412 * @event: event to be signaled upon installation of the protocol
1413 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001414 *
1415 * This function implements the RegisterProtocolNotify service.
1416 * See the Unified Extensible Firmware Interface (UEFI) specification
1417 * for details.
1418 *
Mario Six78a88f72018-07-10 08:40:17 +02001419 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001420 */
Jose Marinho64a8aae2021-03-02 17:26:38 +00001421efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1422 struct efi_event *event,
1423 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001424{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001425 struct efi_register_notify_event *item;
1426 efi_status_t ret = EFI_SUCCESS;
1427
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001428 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001429
1430 if (!protocol || !event || !registration) {
1431 ret = EFI_INVALID_PARAMETER;
1432 goto out;
1433 }
1434
1435 item = calloc(1, sizeof(struct efi_register_notify_event));
1436 if (!item) {
1437 ret = EFI_OUT_OF_RESOURCES;
1438 goto out;
1439 }
1440
1441 item->event = event;
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301442 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001443 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001444
1445 list_add_tail(&item->link, &efi_register_notify_events);
1446
1447 *registration = item;
1448out:
1449 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001450}
1451
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001452/**
Mario Six78a88f72018-07-10 08:40:17 +02001453 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001454 *
Mario Six78a88f72018-07-10 08:40:17 +02001455 * @search_type: selection criterion
1456 * @protocol: GUID of the protocol
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001457 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001458 *
1459 * See the documentation of the LocateHandle service in the UEFI specification.
1460 *
Mario Six78a88f72018-07-10 08:40:17 +02001461 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001462 */
Alexander Grafbee91162016-03-04 01:09:59 +01001463static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001464 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001465{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001466 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001467
1468 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001469 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001470 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001471 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001472 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001473 return (ret != EFI_SUCCESS);
1474 default:
1475 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001476 return -1;
1477 }
Alexander Grafbee91162016-03-04 01:09:59 +01001478}
1479
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001480/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001481 * efi_check_register_notify_event() - check if registration key is valid
1482 *
1483 * Check that a pointer is a valid registration key as returned by
1484 * RegisterProtocolNotify().
1485 *
1486 * @key: registration key
1487 * Return: valid registration key or NULL
1488 */
1489static struct efi_register_notify_event *efi_check_register_notify_event
1490 (void *key)
1491{
1492 struct efi_register_notify_event *event;
1493
1494 list_for_each_entry(event, &efi_register_notify_events, link) {
1495 if (event == (struct efi_register_notify_event *)key)
1496 return event;
1497 }
1498 return NULL;
1499}
1500
1501/**
Mario Six78a88f72018-07-10 08:40:17 +02001502 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001503 *
1504 * @search_type: selection criterion
1505 * @protocol: GUID of the protocol
1506 * @search_key: registration key
1507 * @buffer_size: size of the buffer to receive the handles in bytes
1508 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001509 *
1510 * This function is meant for U-Boot internal calls. For the API implementation
1511 * of the LocateHandle service see efi_locate_handle_ext.
1512 *
Mario Six78a88f72018-07-10 08:40:17 +02001513 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001514 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001515static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001516 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001517 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001518 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001519{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001520 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001521 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001522 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001523 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001524
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001525 /* Check parameters */
1526 switch (search_type) {
1527 case ALL_HANDLES:
1528 break;
1529 case BY_REGISTER_NOTIFY:
1530 if (!search_key)
1531 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001532 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001533 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001534 if (!event)
1535 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001536 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001537 case BY_PROTOCOL:
1538 if (!protocol)
1539 return EFI_INVALID_PARAMETER;
1540 break;
1541 default:
1542 return EFI_INVALID_PARAMETER;
1543 }
1544
Alexander Grafbee91162016-03-04 01:09:59 +01001545 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001546 if (search_type == BY_REGISTER_NOTIFY) {
1547 if (list_empty(&event->handles))
1548 return EFI_NOT_FOUND;
1549 handle = list_first_entry(&event->handles,
1550 struct efi_protocol_notification,
1551 link);
1552 efiobj = handle->handle;
1553 size += sizeof(void *);
1554 } else {
1555 list_for_each_entry(efiobj, &efi_obj_list, link) {
1556 if (!efi_search(search_type, protocol, efiobj))
1557 size += sizeof(void *);
1558 }
1559 if (size == 0)
1560 return EFI_NOT_FOUND;
Alexander Grafbee91162016-03-04 01:09:59 +01001561 }
1562
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001563 if (!buffer_size)
1564 return EFI_INVALID_PARAMETER;
1565
Alexander Grafbee91162016-03-04 01:09:59 +01001566 if (*buffer_size < size) {
1567 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001568 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001569 }
1570
Rob Clark796a78c2017-08-06 14:10:07 -04001571 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001572
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001573 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001574 if (!buffer)
1575 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001576
Alexander Grafbee91162016-03-04 01:09:59 +01001577 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001578 if (search_type == BY_REGISTER_NOTIFY) {
1579 *buffer = efiobj;
1580 list_del(&handle->link);
1581 } else {
1582 list_for_each_entry(efiobj, &efi_obj_list, link) {
1583 if (!efi_search(search_type, protocol, efiobj))
1584 *buffer++ = efiobj;
1585 }
Alexander Grafbee91162016-03-04 01:09:59 +01001586 }
1587
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001588 return EFI_SUCCESS;
1589}
1590
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001591/**
Mario Six78a88f72018-07-10 08:40:17 +02001592 * efi_locate_handle_ext() - locate handles implementing a protocol.
1593 * @search_type: selection criterion
1594 * @protocol: GUID of the protocol
1595 * @search_key: registration key
1596 * @buffer_size: size of the buffer to receive the handles in bytes
1597 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001598 *
1599 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001600 *
Mario Six78a88f72018-07-10 08:40:17 +02001601 * See the Unified Extensible Firmware Interface (UEFI) specification for
1602 * details.
1603 *
1604 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001605 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001606static efi_status_t EFIAPI efi_locate_handle_ext(
1607 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001608 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001609 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001610{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001611 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001612 buffer_size, buffer);
1613
1614 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1615 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001616}
1617
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001618/**
Mario Six78a88f72018-07-10 08:40:17 +02001619 * efi_remove_configuration_table() - collapses configuration table entries,
1620 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001621 *
Mario Six78a88f72018-07-10 08:40:17 +02001622 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001623 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001624static void efi_remove_configuration_table(int i)
1625{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001626 struct efi_configuration_table *this = &systab.tables[i];
1627 struct efi_configuration_table *next = &systab.tables[i + 1];
1628 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001629
1630 memmove(this, next, (ulong)end - (ulong)next);
1631 systab.nr_tables--;
1632}
1633
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001634/**
Mario Six78a88f72018-07-10 08:40:17 +02001635 * efi_install_configuration_table() - adds, updates, or removes a
1636 * configuration table
1637 * @guid: GUID of the installed table
1638 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001639 *
1640 * This function is used for internal calls. For the API implementation of the
1641 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1642 *
Mario Six78a88f72018-07-10 08:40:17 +02001643 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001644 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001645efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1646 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001647{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001648 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001649 int i;
1650
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001651 if (!guid)
1652 return EFI_INVALID_PARAMETER;
1653
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001654 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001655 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001656 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001657 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001658 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001659 else
1660 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001661 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001662 }
1663 }
1664
Alexander Grafd98cdf62017-07-26 13:41:04 +02001665 if (!table)
1666 return EFI_NOT_FOUND;
1667
Alexander Grafbee91162016-03-04 01:09:59 +01001668 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001669 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001670 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001671
1672 /* Add a new entry */
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301673 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001674 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001675 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001676
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001677out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001678 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001679 efi_update_table_header_crc32(&systab.hdr);
1680
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001681 /* Notify that the configuration table was changed */
1682 list_for_each_entry(evt, &efi_events, link) {
1683 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001684 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001685 break;
1686 }
1687 }
1688
Alexander Graf488bf122016-08-19 01:23:24 +02001689 return EFI_SUCCESS;
1690}
1691
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001692/**
Mario Six78a88f72018-07-10 08:40:17 +02001693 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1694 * configuration table.
1695 * @guid: GUID of the installed table
1696 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001697 *
1698 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001699 *
Mario Six78a88f72018-07-10 08:40:17 +02001700 * See the Unified Extensible Firmware Interface (UEFI) specification for
1701 * details.
1702 *
1703 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001704 */
Masahisa Kojimaf86352e2021-10-22 20:24:24 +09001705static efi_status_t
1706EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1707 void *table)
Alexander Graf488bf122016-08-19 01:23:24 +02001708{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001709 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001710 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001711}
1712
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001713/**
Mario Six78a88f72018-07-10 08:40:17 +02001714 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001715 *
1716 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001717 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001718 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001719 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001720 * code is returned.
1721 *
1722 * @device_path: device path of the loaded image
1723 * @file_path: file path of the loaded image
1724 * @handle_ptr: handle of the loaded image
1725 * @info_ptr: loaded image protocol
1726 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001727 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001728efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1729 struct efi_device_path *file_path,
1730 struct efi_loaded_image_obj **handle_ptr,
1731 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001732{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001733 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001734 struct efi_loaded_image *info = NULL;
1735 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001736 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001737
1738 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1739 *handle_ptr = NULL;
1740 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001741
1742 info = calloc(1, sizeof(*info));
1743 if (!info)
1744 return EFI_OUT_OF_RESOURCES;
1745 obj = calloc(1, sizeof(*obj));
1746 if (!obj) {
1747 free(info);
1748 return EFI_OUT_OF_RESOURCES;
1749 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001750 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001751
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001752 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001753 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001754
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001755 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001756 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001757 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001758
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001759 if (device_path) {
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +01001760 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001761
1762 dp = efi_dp_append(device_path, file_path);
1763 if (!dp) {
1764 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001765 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001766 }
1767 } else {
1768 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001769 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001770 ret = efi_add_protocol(&obj->header,
1771 &efi_guid_loaded_image_device_path, dp);
1772 if (ret != EFI_SUCCESS)
1773 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001774
1775 /*
1776 * When asking for the loaded_image interface, just
1777 * return handle which points to loaded_image_info
1778 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001779 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001780 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001781 if (ret != EFI_SUCCESS)
1782 goto failure;
1783
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001784 *info_ptr = info;
1785 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001786
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001787 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001788failure:
1789 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001790 efi_delete_handle(&obj->header);
1791 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001792 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001793}
1794
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001795/**
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001796 * efi_locate_device_path() - Get the device path and handle of an device
1797 * implementing a protocol
1798 * @protocol: GUID of the protocol
1799 * @device_path: device path
1800 * @device: handle of the device
1801 *
1802 * This function implements the LocateDevicePath service.
1803 *
1804 * See the Unified Extensible Firmware Interface (UEFI) specification for
1805 * details.
1806 *
1807 * Return: status code
1808 */
AKASHI Takahirod8465ff2022-04-28 17:09:38 +09001809efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1810 struct efi_device_path **device_path,
1811 efi_handle_t *device)
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001812{
1813 struct efi_device_path *dp;
1814 size_t i;
1815 struct efi_handler *handler;
1816 efi_handle_t *handles;
1817 size_t len, len_dp;
1818 size_t len_best = 0;
1819 efi_uintn_t no_handles;
1820 u8 *remainder;
1821 efi_status_t ret;
1822
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001823 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001824
1825 if (!protocol || !device_path || !*device_path) {
1826 ret = EFI_INVALID_PARAMETER;
1827 goto out;
1828 }
1829
1830 /* Find end of device path */
1831 len = efi_dp_instance_size(*device_path);
1832
1833 /* Get all handles implementing the protocol */
1834 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1835 &no_handles, &handles));
1836 if (ret != EFI_SUCCESS)
1837 goto out;
1838
1839 for (i = 0; i < no_handles; ++i) {
1840 /* Find the device path protocol */
1841 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1842 &handler);
1843 if (ret != EFI_SUCCESS)
1844 continue;
1845 dp = (struct efi_device_path *)handler->protocol_interface;
1846 len_dp = efi_dp_instance_size(dp);
1847 /*
1848 * This handle can only be a better fit
1849 * if its device path length is longer than the best fit and
1850 * if its device path length is shorter of equal the searched
1851 * device path.
1852 */
1853 if (len_dp <= len_best || len_dp > len)
1854 continue;
1855 /* Check if dp is a subpath of device_path */
1856 if (memcmp(*device_path, dp, len_dp))
1857 continue;
1858 if (!device) {
1859 ret = EFI_INVALID_PARAMETER;
1860 goto out;
1861 }
1862 *device = handles[i];
1863 len_best = len_dp;
1864 }
1865 if (len_best) {
1866 remainder = (u8 *)*device_path + len_best;
1867 *device_path = (struct efi_device_path *)remainder;
1868 ret = EFI_SUCCESS;
1869 } else {
1870 ret = EFI_NOT_FOUND;
1871 }
1872out:
1873 return EFI_EXIT(ret);
1874}
1875
1876/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001877 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001878 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001879 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1880 * callers obligation to update the memory type as needed.
1881 *
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01001882 * @file_path: the path of the image to load
1883 * @buffer: buffer containing the loaded image
1884 * @size: size of the loaded image
1885 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001886 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001887static
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001888efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001889 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001890{
Rob Clark838ee4b2017-09-13 18:05:35 -04001891 struct efi_file_handle *f;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001892 efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001893 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001894 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001895
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001896 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001897 f = efi_file_from_path(file_path);
1898 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001899 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001900
Ilias Apalodimasac30aad2021-03-25 21:55:16 +02001901 ret = efi_file_size(f, &bs);
Rob Clark838ee4b2017-09-13 18:05:35 -04001902 if (ret != EFI_SUCCESS)
1903 goto error;
1904
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001905 /*
1906 * When reading the file we do not yet know if it contains an
1907 * application, a boottime driver, or a runtime driver. So here we
1908 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1909 * update the reservation according to the image type.
1910 */
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001911 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1912 EFI_BOOT_SERVICES_DATA,
1913 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001914 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001915 ret = EFI_OUT_OF_RESOURCES;
1916 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001917 }
1918
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001919 /* Read file */
1920 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1921 if (ret != EFI_SUCCESS)
1922 efi_free_pages(addr, efi_size_in_pages(bs));
1923 *buffer = (void *)(uintptr_t)addr;
1924 *size = bs;
1925error:
1926 EFI_CALL(f->close(f));
Rob Clark838ee4b2017-09-13 18:05:35 -04001927 return ret;
1928}
1929
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001930/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001931 * efi_load_image_from_path() - load an image using a file path
1932 *
1933 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1934 * callers obligation to update the memory type as needed.
1935 *
1936 * @boot_policy: true for request originating from the boot manager
1937 * @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
1941 */
1942static
1943efi_status_t efi_load_image_from_path(bool boot_policy,
1944 struct efi_device_path *file_path,
1945 void **buffer, efi_uintn_t *size)
1946{
1947 efi_handle_t device;
1948 efi_status_t ret;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001949 struct efi_device_path *dp, *rem;
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001950 struct efi_load_file_protocol *load_file_protocol = NULL;
1951 efi_uintn_t buffer_size;
1952 uint64_t addr, pages;
1953 const efi_guid_t *guid;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01001954 struct efi_handler *handler;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001955
1956 /* In case of failure nothing is returned */
1957 *buffer = NULL;
1958 *size = 0;
1959
1960 dp = file_path;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001961 device = efi_dp_find_obj(dp, NULL, &rem);
1962 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1963 NULL);
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001964 if (ret == EFI_SUCCESS)
1965 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001966
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001967 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001968 if (ret == EFI_SUCCESS) {
1969 guid = &efi_guid_load_file_protocol;
1970 } else if (!boot_policy) {
1971 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001972 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001973 }
1974 if (ret != EFI_SUCCESS)
1975 return EFI_NOT_FOUND;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01001976 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001977 if (ret != EFI_SUCCESS)
1978 return EFI_NOT_FOUND;
1979 buffer_size = 0;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01001980 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001981 ret = EFI_CALL(load_file_protocol->load_file(
1982 load_file_protocol, rem, boot_policy,
1983 &buffer_size, NULL));
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001984 if (ret != EFI_BUFFER_TOO_SMALL)
1985 goto out;
1986 pages = efi_size_in_pages(buffer_size);
1987 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1988 pages, &addr);
1989 if (ret != EFI_SUCCESS) {
1990 ret = EFI_OUT_OF_RESOURCES;
1991 goto out;
1992 }
1993 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01001994 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001995 &buffer_size, (void *)(uintptr_t)addr));
1996 if (ret != EFI_SUCCESS)
1997 efi_free_pages(addr, pages);
1998out:
Heinrich Schuchardtef185762022-10-07 15:18:15 +02001999 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002000 if (ret == EFI_SUCCESS) {
2001 *buffer = (void *)(uintptr_t)addr;
2002 *size = buffer_size;
2003 }
2004
2005 return ret;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002006}
2007
2008/**
Mario Six78a88f72018-07-10 08:40:17 +02002009 * efi_load_image() - load an EFI image into memory
2010 * @boot_policy: true for request originating from the boot manager
2011 * @parent_image: the caller's image handle
2012 * @file_path: the path of the image to load
2013 * @source_buffer: memory location from which the image is installed
2014 * @source_size: size of the memory area from which the image is installed
2015 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002016 *
2017 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02002018 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002019 * See the Unified Extensible Firmware Interface (UEFI) specification
2020 * for details.
2021 *
Mario Six78a88f72018-07-10 08:40:17 +02002022 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002023 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09002024efi_status_t EFIAPI efi_load_image(bool boot_policy,
2025 efi_handle_t parent_image,
2026 struct efi_device_path *file_path,
2027 void *source_buffer,
2028 efi_uintn_t source_size,
2029 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002030{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002031 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04002032 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02002033 struct efi_loaded_image_obj **image_obj =
2034 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002035 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002036 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01002037
Heinrich Schuchardte9df5492022-02-03 22:21:51 +01002038 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01002039 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04002040
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02002041 if (!image_handle || (!source_buffer && !file_path) ||
2042 !efi_search_obj(parent_image) ||
2043 /* The parent image handle must refer to a loaded image */
2044 !parent_image->type) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02002045 ret = EFI_INVALID_PARAMETER;
2046 goto error;
2047 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002048
Rob Clark838ee4b2017-09-13 18:05:35 -04002049 if (!source_buffer) {
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01002050 ret = efi_load_image_from_path(boot_policy, file_path,
2051 &dest_buffer, &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002052 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002053 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04002054 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002055 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04002056 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00002057 /* split file_path which contains both the device and file parts */
2058 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002059 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002060 if (ret == EFI_SUCCESS)
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002061 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002062 if (!source_buffer)
2063 /* Release buffer to which file was loaded */
2064 efi_free_pages((uintptr_t)dest_buffer,
2065 efi_size_in_pages(source_size));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002066 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002067 info->system_table = &systab;
2068 info->parent_handle = parent_image;
2069 } else {
2070 /* The image is invalid. Release all associated resources. */
2071 efi_delete_handle(*image_handle);
2072 *image_handle = NULL;
2073 free(info);
2074 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002075error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002076 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002077}
2078
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002079/**
Alexander Graff31239a2018-11-15 21:23:47 +01002080 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2081 */
2082static void efi_exit_caches(void)
2083{
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002084#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graff31239a2018-11-15 21:23:47 +01002085 /*
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002086 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2087 * caches are enabled.
2088 *
2089 * TODO:
2090 * According to the UEFI spec caches that can be managed via CP15
2091 * operations should be enabled. Caches requiring platform information
2092 * to manage should be disabled. This should not happen in
2093 * ExitBootServices() but before invoking any UEFI binary is invoked.
2094 *
2095 * We want to keep the current workaround while GRUB prior to version
2096 * 2.04 is still in use.
Alexander Graff31239a2018-11-15 21:23:47 +01002097 */
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002098 cleanup_before_linux();
Alexander Graff31239a2018-11-15 21:23:47 +01002099#endif
2100}
2101
2102/**
Mario Six78a88f72018-07-10 08:40:17 +02002103 * efi_exit_boot_services() - stop all boot services
2104 * @image_handle: handle of the loaded image
2105 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002106 *
2107 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02002108 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002109 * See the Unified Extensible Firmware Interface (UEFI) specification
2110 * for details.
2111 *
Mario Six78a88f72018-07-10 08:40:17 +02002112 * All timer events are disabled. For exit boot services events the
2113 * notification function is called. The boot services are disabled in the
2114 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002115 *
Mario Six78a88f72018-07-10 08:40:17 +02002116 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002117 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002118static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002119 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01002120{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002121 struct efi_event *evt, *next_event;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002122 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002123
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002124 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01002125
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002126 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002127 if (map_key != efi_memory_map_key) {
2128 ret = EFI_INVALID_PARAMETER;
2129 goto out;
2130 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002131
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002132 /* Check if ExitBootServices has already been called */
2133 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002134 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002135
Heinrich Schuchardt43eaf5b2021-11-16 18:46:27 +01002136 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2137 list_for_each_entry(evt, &efi_events, link) {
2138 if (evt->group &&
2139 !guidcmp(evt->group,
2140 &efi_guid_event_group_before_exit_boot_services)) {
2141 efi_signal_event(evt);
2142 break;
2143 }
2144 }
2145
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002146 /* Stop all timer related activities */
2147 timers_enabled = false;
2148
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002149 /* Add related events to the event group */
2150 list_for_each_entry(evt, &efi_events, link) {
2151 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2152 evt->group = &efi_guid_event_group_exit_boot_services;
2153 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002154 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01002155 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002156 if (evt->group &&
2157 !guidcmp(evt->group,
2158 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002159 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002160 break;
2161 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002162 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002163
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002164 /* Make sure that notification functions are not called anymore */
2165 efi_tpl = TPL_HIGH_LEVEL;
2166
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02002167 /* Notify variable services */
2168 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04002169
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002170 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2171 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2172 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2173 list_del(&evt->link);
2174 }
2175
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002176 if (!efi_st_keep_devices) {
Tom Rini3f73e792021-11-19 16:33:04 -05002177 bootm_disable_interrupts();
Heinrich Schuchardtdb6288d2020-12-27 15:33:09 +01002178 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002179 udc_disconnect();
2180 board_quiesce_devices();
2181 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2182 }
Alexander Grafb7b84102016-11-17 01:02:57 +01002183
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02002184 /* Patch out unsupported runtime function */
2185 efi_runtime_detach();
2186
Alexander Graff31239a2018-11-15 21:23:47 +01002187 /* Fix up caches for EFI payloads if necessary */
2188 efi_exit_caches();
2189
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002190 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002191 systab.con_in_handle = NULL;
2192 systab.con_in = NULL;
2193 systab.con_out_handle = NULL;
2194 systab.con_out = NULL;
2195 systab.stderr_handle = NULL;
2196 systab.std_err = NULL;
2197 systab.boottime = NULL;
2198
2199 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02002200 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002201
Alexander Grafbee91162016-03-04 01:09:59 +01002202 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002203 efi_set_watchdog(0);
Stefan Roese29caf932022-09-02 14:10:46 +02002204 schedule();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002205out:
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002206 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2207 if (ret != EFI_SUCCESS)
2208 efi_tcg2_notify_exit_boot_services_failed();
2209 }
2210
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002211 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002212}
2213
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002214/**
Mario Six78a88f72018-07-10 08:40:17 +02002215 * efi_get_next_monotonic_count() - get next value of the counter
2216 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002217 *
2218 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002219 *
Mario Six78a88f72018-07-10 08:40:17 +02002220 * See the Unified Extensible Firmware Interface (UEFI) specification for
2221 * details.
2222 *
2223 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002224 */
Alexander Grafbee91162016-03-04 01:09:59 +01002225static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2226{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002227 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002228 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002229
Alexander Grafbee91162016-03-04 01:09:59 +01002230 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002231 if (!count) {
2232 ret = EFI_INVALID_PARAMETER;
2233 goto out;
2234 }
Alexander Grafbee91162016-03-04 01:09:59 +01002235 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002236 ret = EFI_SUCCESS;
2237out:
2238 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002239}
2240
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002241/**
Mario Six78a88f72018-07-10 08:40:17 +02002242 * efi_stall() - sleep
2243 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002244 *
Mario Six78a88f72018-07-10 08:40:17 +02002245 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002246 *
Mario Six78a88f72018-07-10 08:40:17 +02002247 * See the Unified Extensible Firmware Interface (UEFI) specification for
2248 * details.
2249 *
2250 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002251 */
Alexander Grafbee91162016-03-04 01:09:59 +01002252static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2253{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002254 u64 end_tick;
2255
Alexander Grafbee91162016-03-04 01:09:59 +01002256 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002257
2258 end_tick = get_ticks() + usec_to_tick(microseconds);
2259 while (get_ticks() < end_tick)
2260 efi_timer_check();
2261
Alexander Grafbee91162016-03-04 01:09:59 +01002262 return EFI_EXIT(EFI_SUCCESS);
2263}
2264
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002265/**
Mario Six78a88f72018-07-10 08:40:17 +02002266 * efi_set_watchdog_timer() - reset the watchdog timer
2267 * @timeout: seconds before reset by watchdog
2268 * @watchdog_code: code to be logged when resetting
2269 * @data_size: size of buffer in bytes
2270 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002271 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002272 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002273 *
Mario Six78a88f72018-07-10 08:40:17 +02002274 * See the Unified Extensible Firmware Interface (UEFI) specification for
2275 * details.
2276 *
2277 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002278 */
Alexander Grafbee91162016-03-04 01:09:59 +01002279static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2280 uint64_t watchdog_code,
2281 unsigned long data_size,
2282 uint16_t *watchdog_data)
2283{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002284 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002285 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002286 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002287}
2288
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002289/**
Mario Six78a88f72018-07-10 08:40:17 +02002290 * efi_close_protocol() - close a protocol
2291 * @handle: handle on which the protocol shall be closed
2292 * @protocol: GUID of the protocol to close
2293 * @agent_handle: handle of the driver
2294 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002295 *
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002296 * This is the function implementing the CloseProtocol service is for internal
2297 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
2298 *
2299 * See the Unified Extensible Firmware Interface (UEFI) specification for
2300 * details.
2301 *
2302 * Return: status code
2303 */
2304efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2305 efi_handle_t agent_handle,
2306 efi_handle_t controller_handle)
2307{
2308 struct efi_handler *handler;
2309 struct efi_open_protocol_info_item *item;
2310 struct efi_open_protocol_info_item *pos;
2311 efi_status_t ret;
2312
2313 if (!efi_search_obj(agent_handle) ||
2314 (controller_handle && !efi_search_obj(controller_handle)))
2315 return EFI_INVALID_PARAMETER;
2316 ret = efi_search_protocol(handle, protocol, &handler);
2317 if (ret != EFI_SUCCESS)
2318 return ret;
2319
2320 ret = EFI_NOT_FOUND;
2321 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2322 if (item->info.agent_handle == agent_handle &&
2323 item->info.controller_handle == controller_handle) {
2324 efi_delete_open_info(item);
2325 ret = EFI_SUCCESS;
2326 }
2327 }
2328
2329 return ret;
2330}
2331
2332/**
2333 * efi_close_protocol_ext() - close a protocol
2334 * @handle: handle on which the protocol shall be closed
2335 * @protocol: GUID of the protocol to close
2336 * @agent_handle: handle of the driver
2337 * @controller_handle: handle of the controller
2338 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002339 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002340 *
Mario Six78a88f72018-07-10 08:40:17 +02002341 * See the Unified Extensible Firmware Interface (UEFI) specification for
2342 * details.
2343 *
2344 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002345 */
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002346static efi_status_t EFIAPI
2347efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2348 efi_handle_t agent_handle,
2349 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002350{
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002351 efi_status_t ret;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002352
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002353 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002354 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002355
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002356 ret = efi_close_protocol(handle, protocol,
2357 agent_handle, controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002358
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002359 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002360}
2361
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002362/**
Mario Six78a88f72018-07-10 08:40:17 +02002363 * efi_open_protocol_information() - provide information about then open status
2364 * of a protocol on a handle
2365 * @handle: handle for which the information shall be retrieved
2366 * @protocol: GUID of the protocol
2367 * @entry_buffer: buffer to receive the open protocol information
2368 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002369 *
2370 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002371 *
Mario Six78a88f72018-07-10 08:40:17 +02002372 * See the Unified Extensible Firmware Interface (UEFI) specification for
2373 * details.
2374 *
2375 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002376 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002377static efi_status_t EFIAPI efi_open_protocol_information(
2378 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002379 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002380 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002381{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002382 unsigned long buffer_size;
2383 unsigned long count;
2384 struct efi_handler *handler;
2385 struct efi_open_protocol_info_item *item;
2386 efi_status_t r;
2387
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002388 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002389 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002390
2391 /* Check parameters */
2392 if (!entry_buffer) {
2393 r = EFI_INVALID_PARAMETER;
2394 goto out;
2395 }
2396 r = efi_search_protocol(handle, protocol, &handler);
2397 if (r != EFI_SUCCESS)
2398 goto out;
2399
2400 /* Count entries */
2401 count = 0;
2402 list_for_each_entry(item, &handler->open_infos, link) {
2403 if (item->info.open_count)
2404 ++count;
2405 }
2406 *entry_count = count;
2407 *entry_buffer = NULL;
2408 if (!count) {
2409 r = EFI_SUCCESS;
2410 goto out;
2411 }
2412
2413 /* Copy entries */
2414 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002415 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002416 (void **)entry_buffer);
2417 if (r != EFI_SUCCESS)
2418 goto out;
2419 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2420 if (item->info.open_count)
2421 (*entry_buffer)[--count] = item->info;
2422 }
2423out:
2424 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002425}
2426
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002427/**
Mario Six78a88f72018-07-10 08:40:17 +02002428 * efi_protocols_per_handle() - get protocols installed on a handle
2429 * @handle: handle for which the information is retrieved
2430 * @protocol_buffer: buffer with protocol GUIDs
2431 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002432 *
2433 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002434 *
Mario Six78a88f72018-07-10 08:40:17 +02002435 * See the Unified Extensible Firmware Interface (UEFI) specification for
2436 * details.
2437 *
2438 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002439 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002440static efi_status_t EFIAPI efi_protocols_per_handle(
2441 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002442 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002443{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002444 unsigned long buffer_size;
2445 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002446 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002447 efi_status_t r;
2448
Alexander Grafbee91162016-03-04 01:09:59 +01002449 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2450 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002451
2452 if (!handle || !protocol_buffer || !protocol_buffer_count)
2453 return EFI_EXIT(EFI_INVALID_PARAMETER);
2454
2455 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002456 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002457
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002458 efiobj = efi_search_obj(handle);
2459 if (!efiobj)
2460 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002461
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002462 /* Count protocols */
2463 list_for_each(protocol_handle, &efiobj->protocols) {
2464 ++*protocol_buffer_count;
2465 }
2466
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002467 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002468 if (*protocol_buffer_count) {
2469 size_t j = 0;
2470
2471 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002472 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002473 (void **)protocol_buffer);
2474 if (r != EFI_SUCCESS)
2475 return EFI_EXIT(r);
2476 list_for_each(protocol_handle, &efiobj->protocols) {
2477 struct efi_handler *protocol;
2478
2479 protocol = list_entry(protocol_handle,
2480 struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +01002481 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002482 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002483 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002484 }
2485
2486 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002487}
2488
Masahisa Kojima87d79142022-09-12 17:33:50 +09002489efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2490 const efi_guid_t *protocol, void *search_key,
2491 efi_uintn_t *no_handles, efi_handle_t **buffer)
2492{
2493 efi_status_t r;
2494 efi_uintn_t buffer_size = 0;
2495
2496 if (!no_handles || !buffer) {
2497 r = EFI_INVALID_PARAMETER;
2498 goto out;
2499 }
2500 *no_handles = 0;
2501 *buffer = NULL;
2502 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2503 *buffer);
2504 if (r != EFI_BUFFER_TOO_SMALL)
2505 goto out;
2506 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2507 (void **)buffer);
2508 if (r != EFI_SUCCESS)
2509 goto out;
2510 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2511 *buffer);
2512 if (r == EFI_SUCCESS)
2513 *no_handles = buffer_size / sizeof(efi_handle_t);
2514out:
2515 return r;
2516}
2517
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002518/**
Mario Six78a88f72018-07-10 08:40:17 +02002519 * efi_locate_handle_buffer() - locate handles implementing a protocol
2520 * @search_type: selection criterion
2521 * @protocol: GUID of the protocol
2522 * @search_key: registration key
2523 * @no_handles: number of returned handles
2524 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002525 *
2526 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002527 *
Mario Six78a88f72018-07-10 08:40:17 +02002528 * See the Unified Extensible Firmware Interface (UEFI) specification for
2529 * details.
2530 *
2531 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002532 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002533efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafbee91162016-03-04 01:09:59 +01002534 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002535 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002536 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002537{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002538 efi_status_t r;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002539
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002540 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002541 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002542
Masahisa Kojima87d79142022-09-12 17:33:50 +09002543 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2544 no_handles, buffer);
2545
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002546 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002547}
2548
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002549/**
Mario Six78a88f72018-07-10 08:40:17 +02002550 * efi_locate_protocol() - find an interface implementing a protocol
2551 * @protocol: GUID of the protocol
2552 * @registration: registration key passed to the notification function
2553 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002554 *
2555 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002556 *
Mario Six78a88f72018-07-10 08:40:17 +02002557 * See the Unified Extensible Firmware Interface (UEFI) specification for
2558 * details.
2559 *
2560 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002561 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002562static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002563 void *registration,
2564 void **protocol_interface)
2565{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002566 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002567 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002568 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002569
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002570 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002571
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002572 /*
2573 * The UEFI spec explicitly requires a protocol even if a registration
2574 * key is provided. This differs from the logic in LocateHandle().
2575 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002576 if (!protocol || !protocol_interface)
2577 return EFI_EXIT(EFI_INVALID_PARAMETER);
2578
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002579 if (registration) {
2580 struct efi_register_notify_event *event;
2581 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002582
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002583 event = efi_check_register_notify_event(registration);
2584 if (!event)
2585 return EFI_EXIT(EFI_INVALID_PARAMETER);
2586 /*
2587 * The UEFI spec requires to return EFI_NOT_FOUND if no
2588 * protocol instance matches protocol and registration.
2589 * So let's do the same for a mismatch between protocol and
2590 * registration.
2591 */
2592 if (guidcmp(&event->protocol, protocol))
2593 goto not_found;
2594 if (list_empty(&event->handles))
2595 goto not_found;
2596 handle = list_first_entry(&event->handles,
2597 struct efi_protocol_notification,
2598 link);
2599 efiobj = handle->handle;
2600 list_del(&handle->link);
2601 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002602 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002603 if (ret == EFI_SUCCESS)
2604 goto found;
2605 } else {
2606 list_for_each_entry(efiobj, &efi_obj_list, link) {
2607 ret = efi_search_protocol(efiobj, protocol, &handler);
2608 if (ret == EFI_SUCCESS)
2609 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002610 }
2611 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002612not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002613 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002614 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002615found:
2616 *protocol_interface = handler->protocol_interface;
2617 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002618}
2619
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002620/**
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002621 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
2622 * interfaces
2623 * @handle: handle on which the protocol interfaces shall be installed
2624 * @argptr: va_list of args
2625 *
2626 * Core functionality of efi_install_multiple_protocol_interfaces
2627 * Must not be called directly
2628 *
2629 * Return: status code
2630 */
2631static efi_status_t EFIAPI
2632efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2633 efi_va_list argptr)
2634{
2635 const efi_guid_t *protocol;
2636 void *protocol_interface;
2637 efi_handle_t old_handle;
2638 efi_status_t ret = EFI_SUCCESS;
2639 int i = 0;
2640 efi_va_list argptr_copy;
2641
2642 if (!handle)
2643 return EFI_INVALID_PARAMETER;
2644
2645 efi_va_copy(argptr_copy, argptr);
2646 for (;;) {
2647 protocol = efi_va_arg(argptr, efi_guid_t*);
2648 if (!protocol)
2649 break;
2650 protocol_interface = efi_va_arg(argptr, void*);
2651 /* Check that a device path has not been installed before */
2652 if (!guidcmp(protocol, &efi_guid_device_path)) {
2653 struct efi_device_path *dp = protocol_interface;
2654
2655 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2656 &old_handle));
2657 if (ret == EFI_SUCCESS &&
2658 dp->type == DEVICE_PATH_TYPE_END) {
2659 EFI_PRINT("Path %pD already installed\n",
2660 protocol_interface);
2661 ret = EFI_ALREADY_STARTED;
2662 break;
2663 }
2664 }
2665 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2666 EFI_NATIVE_INTERFACE,
2667 protocol_interface));
2668 if (ret != EFI_SUCCESS)
2669 break;
2670 i++;
2671 }
2672 if (ret == EFI_SUCCESS)
2673 goto out;
2674
2675 /* If an error occurred undo all changes. */
2676 for (; i; --i) {
2677 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2678 protocol_interface = efi_va_arg(argptr_copy, void*);
2679 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2680 protocol_interface));
2681 }
2682
2683out:
2684 efi_va_end(argptr_copy);
2685 return ret;
2686
2687}
2688
2689/**
Mario Six78a88f72018-07-10 08:40:17 +02002690 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2691 * interfaces
2692 * @handle: handle on which the protocol interfaces shall be installed
2693 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2694 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002695 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002696 *
2697 * This is the function for internal usage in U-Boot. For the API function
2698 * implementing the InstallMultipleProtocol service see
2699 * efi_install_multiple_protocol_interfaces_ext()
2700 *
2701 * Return: status code
2702 */
2703efi_status_t EFIAPI
2704efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2705{
2706 efi_status_t ret;
2707 efi_va_list argptr;
2708
2709 efi_va_start(argptr, handle);
2710 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2711 efi_va_end(argptr);
2712 return ret;
2713}
2714
2715/**
2716 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2717 * interfaces
2718 * @handle: handle on which the protocol interfaces shall be installed
2719 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2720 * interfaces
2721 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002722 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002723 *
Mario Six78a88f72018-07-10 08:40:17 +02002724 * See the Unified Extensible Firmware Interface (UEFI) specification for
2725 * details.
2726 *
2727 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002728 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002729static efi_status_t EFIAPI
2730efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002731{
2732 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002733 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002734 efi_va_list argptr;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002735
Alexander Grafbeb077a2018-06-18 17:23:05 +02002736 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002737 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2738 efi_va_end(argptr);
2739 return EFI_EXIT(ret);
2740}
2741
2742/**
2743 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2744 * multiple protocol
2745 * interfaces
2746 * @handle: handle from which the protocol interfaces shall be removed
2747 * @argptr: va_list of args
2748 *
2749 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2750 * Must not be called directly
2751 *
2752 * Return: status code
2753 */
2754static efi_status_t EFIAPI
2755efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2756 efi_va_list argptr)
2757{
2758 const efi_guid_t *protocol;
2759 void *protocol_interface;
Ilias Apalodimas9fb32692022-11-10 10:21:24 +02002760 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002761 size_t i = 0;
2762 efi_va_list argptr_copy;
2763
2764 if (!handle)
2765 return EFI_INVALID_PARAMETER;
2766
2767 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002768 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002769 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002770 if (!protocol)
2771 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002772 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002773 ret = efi_uninstall_protocol(handle, protocol,
2774 protocol_interface);
2775 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002776 break;
2777 i++;
2778 }
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002779 if (ret == EFI_SUCCESS) {
2780 /* If the last protocol has been removed, delete the handle. */
2781 if (list_empty(&handle->protocols)) {
2782 list_del(&handle->link);
2783 free(handle);
2784 }
2785 goto out;
2786 }
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002787
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002788 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002789 for (; i; --i) {
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002790 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2791 protocol_interface = efi_va_arg(argptr_copy, void*);
2792 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2793 EFI_NATIVE_INTERFACE,
2794 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002795 }
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002796 /*
2797 * If any errors are generated while the protocol interfaces are being
2798 * uninstalled, then the protocols uninstalled prior to the error will
2799 * be reinstalled using InstallProtocolInterface() and the status code
2800 * EFI_INVALID_PARAMETER is returned.
2801 */
2802 ret = EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002803
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002804out:
2805 efi_va_end(argptr_copy);
2806 return ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002807}
2808
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002809/**
Mario Six78a88f72018-07-10 08:40:17 +02002810 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2811 * interfaces
2812 * @handle: handle from which the protocol interfaces shall be removed
2813 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2814 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002815 *
2816 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002817 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002818 * This is the function for internal usage in U-Boot. For the API function
2819 * implementing the UninstallMultipleProtocolInterfaces service see
2820 * efi_uninstall_multiple_protocol_interfaces_ext()
2821 *
2822 * Return: status code
2823 */
2824efi_status_t EFIAPI
2825efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2826{
2827 efi_status_t ret;
2828 efi_va_list argptr;
2829
2830 efi_va_start(argptr, handle);
2831 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2832 efi_va_end(argptr);
2833 return ret;
2834}
2835
2836/**
2837 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2838 * interfaces
2839 * @handle: handle from which the protocol interfaces shall be removed
2840 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2841 * interfaces
2842 *
2843 * This function implements the UninstallMultipleProtocolInterfaces service.
2844 *
Mario Six78a88f72018-07-10 08:40:17 +02002845 * See the Unified Extensible Firmware Interface (UEFI) specification for
2846 * details.
2847 *
2848 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002849 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002850static efi_status_t EFIAPI
2851efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002852{
2853 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002854 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002855 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002856
Alexander Grafbeb077a2018-06-18 17:23:05 +02002857 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002858 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Grafbeb077a2018-06-18 17:23:05 +02002859 efi_va_end(argptr);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002860 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002861}
2862
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002863/**
Mario Six78a88f72018-07-10 08:40:17 +02002864 * efi_calculate_crc32() - calculate cyclic redundancy code
2865 * @data: buffer with data
2866 * @data_size: size of buffer in bytes
2867 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002868 *
2869 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002870 *
Mario Six78a88f72018-07-10 08:40:17 +02002871 * See the Unified Extensible Firmware Interface (UEFI) specification for
2872 * details.
2873 *
2874 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002875 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002876static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2877 efi_uintn_t data_size,
2878 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002879{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002880 efi_status_t ret = EFI_SUCCESS;
2881
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002882 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002883 if (!data || !data_size || !crc32_p) {
2884 ret = EFI_INVALID_PARAMETER;
2885 goto out;
2886 }
Alexander Grafbee91162016-03-04 01:09:59 +01002887 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002888out:
2889 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002890}
2891
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002892/**
Mario Six78a88f72018-07-10 08:40:17 +02002893 * efi_copy_mem() - copy memory
2894 * @destination: destination of the copy operation
2895 * @source: source of the copy operation
2896 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002897 *
2898 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002899 *
Mario Six78a88f72018-07-10 08:40:17 +02002900 * See the Unified Extensible Firmware Interface (UEFI) specification for
2901 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002902 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002903static void EFIAPI efi_copy_mem(void *destination, const void *source,
2904 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002905{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002906 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002907 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002908 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002909}
2910
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002911/**
Mario Six78a88f72018-07-10 08:40:17 +02002912 * efi_set_mem() - Fill memory with a byte value.
2913 * @buffer: buffer to fill
2914 * @size: size of buffer in bytes
2915 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002916 *
2917 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002918 *
Mario Six78a88f72018-07-10 08:40:17 +02002919 * See the Unified Extensible Firmware Interface (UEFI) specification for
2920 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002921 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002922static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002923{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002924 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002925 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002926 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002927}
2928
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002929/**
Mario Six78a88f72018-07-10 08:40:17 +02002930 * efi_protocol_open() - open protocol interface on a handle
2931 * @handler: handler of a protocol
2932 * @protocol_interface: interface implementing the protocol
2933 * @agent_handle: handle of the driver
2934 * @controller_handle: handle of the controller
2935 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002936 *
Mario Six78a88f72018-07-10 08:40:17 +02002937 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002938 */
Heinrich Schuchardtf9ad2402020-01-10 12:33:59 +01002939efi_status_t efi_protocol_open(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002940 struct efi_handler *handler,
2941 void **protocol_interface, void *agent_handle,
2942 void *controller_handle, uint32_t attributes)
2943{
2944 struct efi_open_protocol_info_item *item;
2945 struct efi_open_protocol_info_entry *match = NULL;
2946 bool opened_by_driver = false;
2947 bool opened_exclusive = false;
2948
2949 /* If there is no agent, only return the interface */
2950 if (!agent_handle)
2951 goto out;
2952
2953 /* For TEST_PROTOCOL ignore interface attribute */
2954 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2955 *protocol_interface = NULL;
2956
2957 /*
2958 * Check if the protocol is already opened by a driver with the same
2959 * attributes or opened exclusively
2960 */
2961 list_for_each_entry(item, &handler->open_infos, link) {
2962 if (item->info.agent_handle == agent_handle) {
2963 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2964 (item->info.attributes == attributes))
2965 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002966 } else {
2967 if (item->info.attributes &
2968 EFI_OPEN_PROTOCOL_BY_DRIVER)
2969 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002970 }
2971 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2972 opened_exclusive = true;
2973 }
2974
2975 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002976 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2977 if (opened_exclusive)
2978 return EFI_ACCESS_DENIED;
2979 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2980 if (opened_exclusive || opened_by_driver)
2981 return EFI_ACCESS_DENIED;
2982 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002983
2984 /* Prepare exclusive opening */
2985 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2986 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002987disconnect_next:
2988 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002989 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002990 efi_status_t ret;
2991
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002992 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002993 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2994 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002995 item->info.controller_handle,
2996 item->info.agent_handle,
2997 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002998 if (ret == EFI_SUCCESS)
2999 /*
3000 * Child controllers may have been
3001 * removed from the open_infos list. So
3002 * let's restart the loop.
3003 */
3004 goto disconnect_next;
3005 else
3006 opened_by_driver = true;
3007 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003008 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003009 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003010 if (opened_by_driver)
3011 return EFI_ACCESS_DENIED;
3012 }
3013
3014 /* Find existing entry */
3015 list_for_each_entry(item, &handler->open_infos, link) {
3016 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02003017 item->info.controller_handle == controller_handle &&
3018 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003019 match = &item->info;
3020 }
3021 /* None found, create one */
3022 if (!match) {
3023 match = efi_create_open_info(handler);
3024 if (!match)
3025 return EFI_OUT_OF_RESOURCES;
3026 }
3027
3028 match->agent_handle = agent_handle;
3029 match->controller_handle = controller_handle;
3030 match->attributes = attributes;
3031 match->open_count++;
3032
3033out:
3034 /* For TEST_PROTOCOL ignore interface attribute. */
3035 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3036 *protocol_interface = handler->protocol_interface;
3037
3038 return EFI_SUCCESS;
3039}
3040
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003041/**
Mario Six78a88f72018-07-10 08:40:17 +02003042 * efi_open_protocol() - open protocol interface on a handle
3043 * @handle: handle on which the protocol shall be opened
3044 * @protocol: GUID of the protocol
3045 * @protocol_interface: interface implementing the protocol
3046 * @agent_handle: handle of the driver
3047 * @controller_handle: handle of the controller
3048 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003049 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003050 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003051 *
Mario Six78a88f72018-07-10 08:40:17 +02003052 * See the Unified Extensible Firmware Interface (UEFI) specification for
3053 * details.
3054 *
3055 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003056 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02003057static efi_status_t EFIAPI efi_open_protocol
3058 (efi_handle_t handle, const efi_guid_t *protocol,
3059 void **protocol_interface, efi_handle_t agent_handle,
3060 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01003061{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003062 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003063 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01003064
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003065 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01003066 protocol_interface, agent_handle, controller_handle,
3067 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003068
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003069 if (!handle || !protocol ||
3070 (!protocol_interface && attributes !=
3071 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
3072 goto out;
3073 }
3074
3075 switch (attributes) {
3076 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3077 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3078 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3079 break;
3080 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3081 if (controller_handle == handle)
3082 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003083 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003084 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3085 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003086 /* Check that the controller handle is valid */
3087 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003088 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003089 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003090 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003091 /* Check that the agent handle is valid */
3092 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003093 goto out;
3094 break;
3095 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003096 goto out;
3097 }
3098
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003099 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003100 switch (r) {
3101 case EFI_SUCCESS:
3102 break;
3103 case EFI_NOT_FOUND:
3104 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003105 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003106 default:
3107 goto out;
3108 }
Alexander Grafbee91162016-03-04 01:09:59 +01003109
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003110 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3111 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01003112out:
3113 return EFI_EXIT(r);
3114}
3115
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003116/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003117 * efi_start_image() - call the entry point of an image
3118 * @image_handle: handle of the image
3119 * @exit_data_size: size of the buffer
3120 * @exit_data: buffer to receive the exit data of the called image
3121 *
3122 * This function implements the StartImage service.
3123 *
3124 * See the Unified Extensible Firmware Interface (UEFI) specification for
3125 * details.
3126 *
3127 * Return: status code
3128 */
3129efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3130 efi_uintn_t *exit_data_size,
3131 u16 **exit_data)
3132{
3133 struct efi_loaded_image_obj *image_obj =
3134 (struct efi_loaded_image_obj *)image_handle;
3135 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003136 void *info;
3137 efi_handle_t parent_image = current_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003138 efi_status_t exit_status;
3139 struct jmp_buf_data exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003140
3141 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3142
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003143 if (!efi_search_obj(image_handle))
3144 return EFI_EXIT(EFI_INVALID_PARAMETER);
3145
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003146 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02003147 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3148 return EFI_EXIT(EFI_INVALID_PARAMETER);
3149
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003150 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3151 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3152
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003153 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3154 &info, NULL, NULL,
3155 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3156 if (ret != EFI_SUCCESS)
3157 return EFI_EXIT(EFI_INVALID_PARAMETER);
3158
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003159 image_obj->exit_data_size = exit_data_size;
3160 image_obj->exit_data = exit_data;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003161 image_obj->exit_status = &exit_status;
3162 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003163
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003164 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3165 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09003166 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojimaf9b51dc2021-12-07 14:15:33 +09003167 if (ret == EFI_SECURITY_VIOLATION) {
3168 /*
3169 * TCG2 Protocol is installed but no TPM device found,
3170 * this is not expected.
3171 */
3172 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003173 }
3174 }
3175 }
3176
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003177 /* call the image! */
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003178 if (setjmp(&exit_jmp)) {
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003179 /*
3180 * We called the entry point of the child image with EFI_CALL
3181 * in the lines below. The child image called the Exit() boot
3182 * service efi_exit() which executed the long jump that brought
3183 * us to the current line. This implies that the second half
3184 * of the EFI_CALL macro has not been executed.
3185 */
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +02003186#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003187 /*
3188 * efi_exit() called efi_restore_gd(). We have to undo this
3189 * otherwise __efi_entry_check() will put the wrong value into
3190 * app_gd.
3191 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +02003192 set_gd(app_gd);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003193#endif
3194 /*
3195 * To get ready to call EFI_EXIT below we have to execute the
3196 * missed out steps of EFI_CALL.
3197 */
3198 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003199 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003200 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003201 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003202 current_image = parent_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003203 return EFI_EXIT(exit_status);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003204 }
3205
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003206 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02003207 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09003208 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003209 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3210
3211 /*
Heinrich Schuchardt55111c52020-01-10 22:06:54 +01003212 * Control is returned from a started UEFI image either by calling
3213 * Exit() (where exit data can be provided) or by simply returning from
3214 * the entry point. In the latter case call Exit() on behalf of the
3215 * image.
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003216 */
3217 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3218}
3219
3220/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003221 * efi_delete_image() - delete loaded image from memory)
3222 *
3223 * @image_obj: handle of the loaded image
3224 * @loaded_image_protocol: loaded image protocol
3225 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003226static efi_status_t efi_delete_image
3227 (struct efi_loaded_image_obj *image_obj,
3228 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003229{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003230 struct efi_object *efiobj;
3231 efi_status_t r, ret = EFI_SUCCESS;
3232
3233close_next:
3234 list_for_each_entry(efiobj, &efi_obj_list, link) {
3235 struct efi_handler *protocol;
3236
3237 list_for_each_entry(protocol, &efiobj->protocols, link) {
3238 struct efi_open_protocol_info_item *info;
3239
3240 list_for_each_entry(info, &protocol->open_infos, link) {
3241 if (info->info.agent_handle !=
3242 (efi_handle_t)image_obj)
3243 continue;
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003244 r = efi_close_protocol(
3245 efiobj, &protocol->guid,
3246 info->info.agent_handle,
3247 info->info.controller_handle);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003248 if (r != EFI_SUCCESS)
3249 ret = r;
3250 /*
3251 * Closing protocols may results in further
3252 * items being deleted. To play it safe loop
3253 * over all elements again.
3254 */
3255 goto close_next;
3256 }
3257 }
3258 }
3259
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003260 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3261 efi_size_in_pages(loaded_image_protocol->image_size));
3262 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003263
3264 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003265}
3266
3267/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003268 * efi_unload_image() - unload an EFI image
3269 * @image_handle: handle of the image to be unloaded
3270 *
3271 * This function implements the UnloadImage service.
3272 *
3273 * See the Unified Extensible Firmware Interface (UEFI) specification for
3274 * details.
3275 *
3276 * Return: status code
3277 */
3278efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3279{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003280 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003281 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003282 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003283
3284 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003285
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003286 efiobj = efi_search_obj(image_handle);
3287 if (!efiobj) {
3288 ret = EFI_INVALID_PARAMETER;
3289 goto out;
3290 }
3291 /* Find the loaded image protocol */
3292 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3293 (void **)&loaded_image_protocol,
3294 NULL, NULL,
3295 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3296 if (ret != EFI_SUCCESS) {
3297 ret = EFI_INVALID_PARAMETER;
3298 goto out;
3299 }
3300 switch (efiobj->type) {
3301 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3302 /* Call the unload function */
3303 if (!loaded_image_protocol->unload) {
3304 ret = EFI_UNSUPPORTED;
3305 goto out;
3306 }
3307 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3308 if (ret != EFI_SUCCESS)
3309 goto out;
3310 break;
3311 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3312 break;
3313 default:
3314 ret = EFI_INVALID_PARAMETER;
3315 goto out;
3316 }
3317 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3318 loaded_image_protocol);
3319out:
3320 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003321}
3322
3323/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003324 * efi_update_exit_data() - fill exit data parameters of StartImage()
3325 *
Heinrich Schuchardt9ce91272019-07-14 11:25:06 +02003326 * @image_obj: image handle
3327 * @exit_data_size: size of the exit data buffer
3328 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003329 * Return: status code
3330 */
3331static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3332 efi_uintn_t exit_data_size,
3333 u16 *exit_data)
3334{
3335 efi_status_t ret;
3336
3337 /*
3338 * If exit_data is not provided to StartImage(), exit_data_size must be
3339 * ignored.
3340 */
3341 if (!image_obj->exit_data)
3342 return EFI_SUCCESS;
3343 if (image_obj->exit_data_size)
3344 *image_obj->exit_data_size = exit_data_size;
3345 if (exit_data_size && exit_data) {
3346 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3347 exit_data_size,
3348 (void **)image_obj->exit_data);
3349 if (ret != EFI_SUCCESS)
3350 return ret;
3351 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3352 } else {
3353 image_obj->exit_data = NULL;
3354 }
3355 return EFI_SUCCESS;
3356}
3357
3358/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003359 * efi_exit() - leave an EFI application or driver
3360 * @image_handle: handle of the application or driver that is exiting
3361 * @exit_status: status code
3362 * @exit_data_size: size of the buffer in bytes
3363 * @exit_data: buffer with data describing an error
3364 *
3365 * This function implements the Exit service.
3366 *
3367 * See the Unified Extensible Firmware Interface (UEFI) specification for
3368 * details.
3369 *
3370 * Return: status code
3371 */
3372static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3373 efi_status_t exit_status,
3374 efi_uintn_t exit_data_size,
3375 u16 *exit_data)
3376{
3377 /*
3378 * TODO: We should call the unload procedure of the loaded
3379 * image protocol.
3380 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003381 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003382 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003383 struct efi_loaded_image_obj *image_obj =
3384 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003385 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003386
3387 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3388 exit_data_size, exit_data);
3389
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003390 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003391 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003392 (void **)&loaded_image_protocol,
3393 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003394 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003395 if (ret != EFI_SUCCESS) {
3396 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003397 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003398 }
3399
3400 /* Unloading of unstarted images */
3401 switch (image_obj->header.type) {
3402 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3403 break;
3404 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3405 efi_delete_image(image_obj, loaded_image_protocol);
3406 ret = EFI_SUCCESS;
3407 goto out;
3408 default:
3409 /* Handle does not refer to loaded image */
3410 ret = EFI_INVALID_PARAMETER;
3411 goto out;
3412 }
3413 /* A started image can only be unloaded it is the last one started. */
3414 if (image_handle != current_image) {
3415 ret = EFI_INVALID_PARAMETER;
3416 goto out;
3417 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003418
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003419 /* Exit data is only foreseen in case of failure. */
3420 if (exit_status != EFI_SUCCESS) {
3421 ret = efi_update_exit_data(image_obj, exit_data_size,
3422 exit_data);
3423 /* Exiting has priority. Don't return error to caller. */
3424 if (ret != EFI_SUCCESS)
3425 EFI_PRINT("%s: out of memory\n", __func__);
3426 }
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003427 /* efi_delete_image() frees image_obj. Copy before the call. */
3428 exit_jmp = image_obj->exit_jmp;
3429 *image_obj->exit_status = exit_status;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003430 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3431 exit_status != EFI_SUCCESS)
3432 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003433
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003434 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3435 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3436 ret = efi_tcg2_measure_efi_app_exit();
3437 if (ret != EFI_SUCCESS) {
3438 log_warning("tcg2 measurement fails(0x%lx)\n",
3439 ret);
3440 }
3441 }
3442 }
3443
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003444 /* Make sure entry/exit counts for EFI world cross-overs match */
3445 EFI_EXIT(exit_status);
3446
3447 /*
3448 * But longjmp out with the U-Boot gd, not the application's, as
3449 * the other end is a setjmp call inside EFI context.
3450 */
3451 efi_restore_gd();
3452
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003453 longjmp(exit_jmp, 1);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003454
3455 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003456out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003457 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003458}
3459
3460/**
Mario Six78a88f72018-07-10 08:40:17 +02003461 * efi_handle_protocol() - get interface of a protocol on a handle
3462 * @handle: handle on which the protocol shall be opened
3463 * @protocol: GUID of the protocol
3464 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003465 *
3466 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003467 *
Mario Six78a88f72018-07-10 08:40:17 +02003468 * See the Unified Extensible Firmware Interface (UEFI) specification for
3469 * details.
3470 *
3471 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003472 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09003473efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3474 const efi_guid_t *protocol,
3475 void **protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01003476{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003477 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003478 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003479}
3480
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003481/**
Mario Six78a88f72018-07-10 08:40:17 +02003482 * efi_bind_controller() - bind a single driver to a controller
3483 * @controller_handle: controller handle
3484 * @driver_image_handle: driver handle
3485 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003486 *
Mario Six78a88f72018-07-10 08:40:17 +02003487 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003488 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003489static efi_status_t efi_bind_controller(
3490 efi_handle_t controller_handle,
3491 efi_handle_t driver_image_handle,
3492 struct efi_device_path *remain_device_path)
3493{
3494 struct efi_driver_binding_protocol *binding_protocol;
3495 efi_status_t r;
3496
3497 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3498 &efi_guid_driver_binding_protocol,
3499 (void **)&binding_protocol,
3500 driver_image_handle, NULL,
3501 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3502 if (r != EFI_SUCCESS)
3503 return r;
3504 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3505 controller_handle,
3506 remain_device_path));
3507 if (r == EFI_SUCCESS)
3508 r = EFI_CALL(binding_protocol->start(binding_protocol,
3509 controller_handle,
3510 remain_device_path));
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003511 efi_close_protocol(driver_image_handle,
3512 &efi_guid_driver_binding_protocol,
3513 driver_image_handle, NULL);
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003514 return r;
3515}
3516
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003517/**
Mario Six78a88f72018-07-10 08:40:17 +02003518 * efi_connect_single_controller() - connect a single driver to a controller
3519 * @controller_handle: controller
3520 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003521 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003522 *
Mario Six78a88f72018-07-10 08:40:17 +02003523 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003524 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003525static efi_status_t efi_connect_single_controller(
3526 efi_handle_t controller_handle,
3527 efi_handle_t *driver_image_handle,
3528 struct efi_device_path *remain_device_path)
3529{
3530 efi_handle_t *buffer;
3531 size_t count;
3532 size_t i;
3533 efi_status_t r;
3534 size_t connected = 0;
3535
3536 /* Get buffer with all handles with driver binding protocol */
3537 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3538 &efi_guid_driver_binding_protocol,
3539 NULL, &count, &buffer));
3540 if (r != EFI_SUCCESS)
3541 return r;
3542
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003543 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003544 if (driver_image_handle) {
3545 for (; *driver_image_handle; ++driver_image_handle) {
3546 for (i = 0; i < count; ++i) {
3547 if (buffer[i] == *driver_image_handle) {
3548 buffer[i] = NULL;
3549 r = efi_bind_controller(
3550 controller_handle,
3551 *driver_image_handle,
3552 remain_device_path);
3553 /*
3554 * For drivers that do not support the
3555 * controller or are already connected
3556 * we receive an error code here.
3557 */
3558 if (r == EFI_SUCCESS)
3559 ++connected;
3560 }
3561 }
3562 }
3563 }
3564
3565 /*
3566 * TODO: Some overrides are not yet implemented:
3567 * - Platform Driver Override
3568 * - Driver Family Override Search
3569 * - Bus Specific Driver Override
3570 */
3571
3572 /* Driver Binding Search */
3573 for (i = 0; i < count; ++i) {
3574 if (buffer[i]) {
3575 r = efi_bind_controller(controller_handle,
3576 buffer[i],
3577 remain_device_path);
3578 if (r == EFI_SUCCESS)
3579 ++connected;
3580 }
3581 }
3582
3583 efi_free_pool(buffer);
3584 if (!connected)
3585 return EFI_NOT_FOUND;
3586 return EFI_SUCCESS;
3587}
3588
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003589/**
Mario Six78a88f72018-07-10 08:40:17 +02003590 * efi_connect_controller() - connect a controller to a driver
3591 * @controller_handle: handle of the controller
3592 * @driver_image_handle: handle of the driver
3593 * @remain_device_path: device path of a child controller
3594 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003595 *
3596 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003597 *
3598 * See the Unified Extensible Firmware Interface (UEFI) specification for
3599 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003600 *
3601 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003602 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003603 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3604 *
Mario Six78a88f72018-07-10 08:40:17 +02003605 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003606 */
3607static efi_status_t EFIAPI efi_connect_controller(
3608 efi_handle_t controller_handle,
3609 efi_handle_t *driver_image_handle,
3610 struct efi_device_path *remain_device_path,
3611 bool recursive)
3612{
3613 efi_status_t r;
3614 efi_status_t ret = EFI_NOT_FOUND;
3615 struct efi_object *efiobj;
3616
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003617 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003618 remain_device_path, recursive);
3619
3620 efiobj = efi_search_obj(controller_handle);
3621 if (!efiobj) {
3622 ret = EFI_INVALID_PARAMETER;
3623 goto out;
3624 }
3625
3626 r = efi_connect_single_controller(controller_handle,
3627 driver_image_handle,
3628 remain_device_path);
3629 if (r == EFI_SUCCESS)
3630 ret = EFI_SUCCESS;
3631 if (recursive) {
3632 struct efi_handler *handler;
3633 struct efi_open_protocol_info_item *item;
3634
3635 list_for_each_entry(handler, &efiobj->protocols, link) {
3636 list_for_each_entry(item, &handler->open_infos, link) {
3637 if (item->info.attributes &
3638 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3639 r = EFI_CALL(efi_connect_controller(
3640 item->info.controller_handle,
3641 driver_image_handle,
3642 remain_device_path,
3643 recursive));
3644 if (r == EFI_SUCCESS)
3645 ret = EFI_SUCCESS;
3646 }
3647 }
3648 }
3649 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003650 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003651 if (ret != EFI_SUCCESS && remain_device_path &&
3652 remain_device_path->type == DEVICE_PATH_TYPE_END)
3653 ret = EFI_SUCCESS;
3654out:
3655 return EFI_EXIT(ret);
3656}
3657
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003658/**
Mario Six78a88f72018-07-10 08:40:17 +02003659 * efi_reinstall_protocol_interface() - reinstall protocol interface
3660 * @handle: handle on which the protocol shall be reinstalled
3661 * @protocol: GUID of the protocol to be installed
3662 * @old_interface: interface to be removed
3663 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003664 *
3665 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003666 *
3667 * See the Unified Extensible Firmware Interface (UEFI) specification for
3668 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003669 *
3670 * The old interface is uninstalled. The new interface is installed.
3671 * Drivers are connected.
3672 *
Mario Six78a88f72018-07-10 08:40:17 +02003673 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003674 */
3675static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3676 efi_handle_t handle, const efi_guid_t *protocol,
3677 void *old_interface, void *new_interface)
3678{
3679 efi_status_t ret;
3680
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003681 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003682 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003683
3684 /* Uninstall protocol but do not delete handle */
3685 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003686 if (ret != EFI_SUCCESS)
3687 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003688
3689 /* Install the new protocol */
3690 ret = efi_add_protocol(handle, protocol, new_interface);
3691 /*
3692 * The UEFI spec does not specify what should happen to the handle
3693 * if in case of an error no protocol interface remains on the handle.
3694 * So let's do nothing here.
3695 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003696 if (ret != EFI_SUCCESS)
3697 goto out;
3698 /*
3699 * The returned status code has to be ignored.
3700 * Do not create an error if no suitable driver for the handle exists.
3701 */
3702 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3703out:
3704 return EFI_EXIT(ret);
3705}
3706
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003707/**
Mario Six78a88f72018-07-10 08:40:17 +02003708 * efi_get_child_controllers() - get all child controllers associated to a driver
3709 * @efiobj: handle of the controller
3710 * @driver_handle: handle of the driver
3711 * @number_of_children: number of child controllers
3712 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003713 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003714 * The allocated buffer has to be freed with free().
3715 *
Mario Six78a88f72018-07-10 08:40:17 +02003716 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003717 */
3718static efi_status_t efi_get_child_controllers(
3719 struct efi_object *efiobj,
3720 efi_handle_t driver_handle,
3721 efi_uintn_t *number_of_children,
3722 efi_handle_t **child_handle_buffer)
3723{
3724 struct efi_handler *handler;
3725 struct efi_open_protocol_info_item *item;
3726 efi_uintn_t count = 0, i;
3727 bool duplicate;
3728
3729 /* Count all child controller associations */
3730 list_for_each_entry(handler, &efiobj->protocols, link) {
3731 list_for_each_entry(item, &handler->open_infos, link) {
3732 if (item->info.agent_handle == driver_handle &&
3733 item->info.attributes &
3734 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3735 ++count;
3736 }
3737 }
3738 /*
3739 * Create buffer. In case of duplicate child controller assignments
3740 * the buffer will be too large. But that does not harm.
3741 */
3742 *number_of_children = 0;
Heinrich Schuchardt1047c6e2020-07-07 04:21:26 +02003743 if (!count)
3744 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003745 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3746 if (!*child_handle_buffer)
3747 return EFI_OUT_OF_RESOURCES;
3748 /* Copy unique child handles */
3749 list_for_each_entry(handler, &efiobj->protocols, link) {
3750 list_for_each_entry(item, &handler->open_infos, link) {
3751 if (item->info.agent_handle == driver_handle &&
3752 item->info.attributes &
3753 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3754 /* Check this is a new child controller */
3755 duplicate = false;
3756 for (i = 0; i < *number_of_children; ++i) {
3757 if ((*child_handle_buffer)[i] ==
3758 item->info.controller_handle)
3759 duplicate = true;
3760 }
3761 /* Copy handle to buffer */
3762 if (!duplicate) {
3763 i = (*number_of_children)++;
3764 (*child_handle_buffer)[i] =
3765 item->info.controller_handle;
3766 }
3767 }
3768 }
3769 }
3770 return EFI_SUCCESS;
3771}
3772
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003773/**
Mario Six78a88f72018-07-10 08:40:17 +02003774 * efi_disconnect_controller() - disconnect a controller from a driver
3775 * @controller_handle: handle of the controller
3776 * @driver_image_handle: handle of the driver
3777 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003778 *
3779 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003780 *
Mario Six78a88f72018-07-10 08:40:17 +02003781 * See the Unified Extensible Firmware Interface (UEFI) specification for
3782 * details.
3783 *
3784 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003785 */
3786static efi_status_t EFIAPI efi_disconnect_controller(
3787 efi_handle_t controller_handle,
3788 efi_handle_t driver_image_handle,
3789 efi_handle_t child_handle)
3790{
3791 struct efi_driver_binding_protocol *binding_protocol;
3792 efi_handle_t *child_handle_buffer = NULL;
3793 size_t number_of_children = 0;
3794 efi_status_t r;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003795 struct efi_object *efiobj;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003796 bool sole_child;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003797
3798 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3799 child_handle);
3800
3801 efiobj = efi_search_obj(controller_handle);
3802 if (!efiobj) {
3803 r = EFI_INVALID_PARAMETER;
3804 goto out;
3805 }
3806
3807 if (child_handle && !efi_search_obj(child_handle)) {
3808 r = EFI_INVALID_PARAMETER;
3809 goto out;
3810 }
3811
3812 /* If no driver handle is supplied, disconnect all drivers */
3813 if (!driver_image_handle) {
3814 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3815 goto out;
3816 }
3817
3818 /* Create list of child handles */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003819 r = efi_get_child_controllers(efiobj,
3820 driver_image_handle,
3821 &number_of_children,
3822 &child_handle_buffer);
3823 if (r != EFI_SUCCESS)
3824 return r;
3825 sole_child = (number_of_children == 1);
3826
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003827 if (child_handle) {
3828 number_of_children = 1;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003829 free(child_handle_buffer);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003830 child_handle_buffer = &child_handle;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003831 }
3832
3833 /* Get the driver binding protocol */
3834 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3835 &efi_guid_driver_binding_protocol,
3836 (void **)&binding_protocol,
3837 driver_image_handle, NULL,
3838 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003839 if (r != EFI_SUCCESS) {
3840 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003841 goto out;
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003842 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003843 /* Remove the children */
3844 if (number_of_children) {
3845 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3846 controller_handle,
3847 number_of_children,
3848 child_handle_buffer));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003849 if (r != EFI_SUCCESS) {
3850 r = EFI_DEVICE_ERROR;
3851 goto out;
3852 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003853 }
3854 /* Remove the driver */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003855 if (!child_handle || sole_child) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003856 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3857 controller_handle,
3858 0, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003859 if (r != EFI_SUCCESS) {
3860 r = EFI_DEVICE_ERROR;
3861 goto out;
3862 }
3863 }
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003864 efi_close_protocol(driver_image_handle,
3865 &efi_guid_driver_binding_protocol,
3866 driver_image_handle, NULL);
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003867 r = EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003868out:
3869 if (!child_handle)
3870 free(child_handle_buffer);
3871 return EFI_EXIT(r);
3872}
3873
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003874static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003875 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003876 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3877 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003878 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003879 },
3880 .raise_tpl = efi_raise_tpl,
3881 .restore_tpl = efi_restore_tpl,
3882 .allocate_pages = efi_allocate_pages_ext,
3883 .free_pages = efi_free_pages_ext,
3884 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003885 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003886 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003887 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003888 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003889 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003890 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003891 .close_event = efi_close_event,
3892 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003893 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003894 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003895 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003896 .handle_protocol = efi_handle_protocol,
3897 .reserved = NULL,
3898 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003899 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003900 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003901 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003902 .load_image = efi_load_image,
3903 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003904 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003905 .unload_image = efi_unload_image,
3906 .exit_boot_services = efi_exit_boot_services,
3907 .get_next_monotonic_count = efi_get_next_monotonic_count,
3908 .stall = efi_stall,
3909 .set_watchdog_timer = efi_set_watchdog_timer,
3910 .connect_controller = efi_connect_controller,
3911 .disconnect_controller = efi_disconnect_controller,
3912 .open_protocol = efi_open_protocol,
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003913 .close_protocol = efi_close_protocol_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003914 .open_protocol_information = efi_open_protocol_information,
3915 .protocols_per_handle = efi_protocols_per_handle,
3916 .locate_handle_buffer = efi_locate_handle_buffer,
3917 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003918 .install_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003919 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003920 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003921 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003922 .calculate_crc32 = efi_calculate_crc32,
3923 .copy_mem = efi_copy_mem,
3924 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003925 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003926};
3927
Simon Glass156ccbc2022-01-23 12:55:12 -07003928static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003929
Alexander Graf3c63db92016-10-14 13:45:30 +02003930struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003931 .hdr = {
3932 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003933 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003934 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003935 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003936 .fw_vendor = firmware_vendor,
3937 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02003938 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003939 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003940 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003941};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003942
3943/**
3944 * efi_initialize_system_table() - Initialize system table
3945 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003946 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003947 */
3948efi_status_t efi_initialize_system_table(void)
3949{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003950 efi_status_t ret;
3951
3952 /* Allocate configuration table array */
3953 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3954 EFI_MAX_CONFIGURATION_TABLES *
3955 sizeof(struct efi_configuration_table),
3956 (void **)&systab.tables);
3957
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003958 /*
3959 * These entries will be set to NULL in ExitBootServices(). To avoid
3960 * relocation in SetVirtualAddressMap(), set them dynamically.
3961 */
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01003962 systab.con_in_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003963 systab.con_in = &efi_con_in;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01003964 systab.con_out_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003965 systab.con_out = &efi_con_out;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01003966 systab.stderr_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003967 systab.std_err = &efi_con_out;
3968 systab.boottime = &efi_boot_services;
3969
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003970 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003971 efi_update_table_header_crc32(&systab.hdr);
3972 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3973 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003974
3975 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003976}