blob: ba28989f36ac098b1e4e5694d5e53e1612b485d4 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020030
Alexander Grafc15d9212016-03-04 01:09:59 +010031/* This list contains all the EFI objects our payload has access to */
32LIST_HEAD(efi_obj_list);
33
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010036
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020037/* List of queued events */
38LIST_HEAD(efi_event_queue);
39
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
47LIST_HEAD(efi_register_notify_events);
48
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010053/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010055 * fixed when compiling U-Boot. However, the payload does not know about that
56 * restriction so we need to manually swap its and our view of that register on
57 * EFI callback entry/exit.
58 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060060#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010061
Heinrich Schuchardt72506232019-02-09 14:10:39 +010062/* 1 if inside U-Boot code, 0 if inside EFI payload code */
63static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040064static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010065/* GUID of the device tree table */
66const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-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 Clark86789d52017-07-27 08:04:18 -040070
Heinrich Schuchardt672d99e2018-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 Schuchardt44772c42021-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 Schuchardt672d99e2018-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 Schuchardt485956d2020-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 Kojimacd1fe7d2021-10-26 17:27:24 +090092/* GUID of the SMBIOS table */
93const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010094
Heinrich Schuchardtb7cb8b42018-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 Schuchardte9943282018-01-11 08:16:04 +010099
Rob Clark86789d52017-07-27 08:04:18 -0400100/* Called on every callback entry */
101int __efi_entry_check(void)
102{
103 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200104#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400105 assert(efi_gd);
106 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200107 set_gd(efi_gd);
Rob Clark86789d52017-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 Schuchardtf277d942020-09-10 12:22:54 +0200116#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200117 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400118#endif
119 return ret;
120}
121
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200122/**
123 * efi_save_gd() - save global data register
124 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200125 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-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 Grafc15d9212016-03-04 01:09:59 +0100131void efi_save_gd(void)
132{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200133#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100134 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600135#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100136}
137
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200138/**
139 * efi_restore_gd() - restore global data register
140 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200141 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200142 * Restore it after returning from the UEFI world to the value saved via
143 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400144 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100145void efi_restore_gd(void)
146{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200147#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100148 /* Only restore if we're already in EFI context */
149 if (!efi_gd)
150 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200151 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600152#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100153}
154
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200155/**
Mario Six8fac2912018-07-10 08:40:17 +0200156 * indent_string() - returns a string for indenting with two spaces per level
157 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100158 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200159 * A maximum of ten indent levels is supported. Higher indent levels will be
160 * truncated.
161 *
Mario Six8fac2912018-07-10 08:40:17 +0200162 * Return: A string for indenting with two spaces per level is
163 * returned.
Rob Clarke7896c32017-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 Schuchardt91064592018-02-18 15:17:49 +0100169
Rob Clarke7896c32017-07-27 08:04:19 -0400170 level = min(max, level * 2);
171 return &indent[max - level];
172}
173
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200174const char *__efi_nesting(void)
175{
176 return indent_string(nesting_level);
177}
178
Rob Clarke7896c32017-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 Schuchardt59999172018-05-11 18:15:41 +0200189/**
Heinrich Schuchardt35c0cf62019-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 Six8fac2912018-07-10 08:40:17 +0200228 * efi_queue_event() - queue an EFI event
229 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200230 *
231 * This function queues the notification function of the event for future
232 * execution.
233 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200234 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200235static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200236{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100237 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-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 Schuchardt3ef4c852020-12-28 00:25:34 +0100257 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200258 }
259}
260
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200261/**
Heinrich Schuchardt7f0f0052018-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 Schuchardtcf70a732018-09-03 19:12:24 +0200265 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200266 */
267efi_status_t is_valid_tpl(efi_uintn_t tpl)
268{
269 switch (tpl) {
270 case TPL_APPLICATION:
271 case TPL_CALLBACK:
272 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200273 return EFI_SUCCESS;
274 default:
275 return EFI_INVALID_PARAMETER;
276 }
277}
278
279/**
Mario Six8fac2912018-07-10 08:40:17 +0200280 * efi_signal_event() - signal an EFI event
281 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100282 *
Heinrich Schuchardt9de0fb72020-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 Schuchardtbf7f1692018-02-18 15:17:52 +0100285 * their notification function is queued.
286 *
287 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100288 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200289void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100290{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200291 if (event->is_signaled)
292 return;
Heinrich Schuchardtbf7f1692018-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 Schuchardtbf7f1692018-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 Schuchardt35c0cf62019-06-05 21:00:39 +0200310 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100311 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200312 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100313 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200314 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100315 }
316}
317
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200318/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200321 *
322 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200323 *
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200326 *
Mario Six8fac2912018-07-10 08:40:17 +0200327 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200328 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100329static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100330{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100331 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200332
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200333 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200334
335 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200336 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-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 Grafc15d9212016-03-04 01:09:59 +0100343}
344
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200345/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200348 *
349 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200350 *
Mario Six8fac2912018-07-10 08:40:17 +0200351 * See the Unified Extensible Firmware Interface (UEFI) specification for
352 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200353 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100354static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100355{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200356 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200357
358 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200359 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-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 Schuchardt59b20952018-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 Schuchardt0c150ca2017-09-15 10:06:16 +0200369 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100370}
371
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200372/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200378 *
379 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200380 *
Mario Six8fac2912018-07-10 08:40:17 +0200381 * See the Unified Extensible Firmware Interface (UEFI) specification for
382 * details.
383 *
384 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200385 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900386static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100387 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900388 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100389{
390 efi_status_t r;
391
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100392 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100393 r = efi_allocate_pages(type, memory_type, pages, memory);
394 return EFI_EXIT(r);
395}
396
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200397/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200401 *
402 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200403 *
404 * See the Unified Extensible Firmware Interface (UEFI) specification for
405 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200406 *
Mario Six8fac2912018-07-10 08:40:17 +0200407 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200408 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900409static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100410 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100411{
412 efi_status_t r;
413
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900414 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100415 r = efi_free_pages(memory, pages);
416 return EFI_EXIT(r);
417}
418
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200419/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200427 *
428 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200429 *
Mario Six8fac2912018-07-10 08:40:17 +0200430 * See the Unified Extensible Firmware Interface (UEFI) specification for
431 * details.
432 *
433 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200434 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900435static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100436 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900437 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100438 efi_uintn_t *map_key,
439 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900440 uint32_t *descriptor_version)
Alexander Grafc15d9212016-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 Schuchardt59999172018-05-11 18:15:41 +0200451/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200456 *
457 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200458 *
459 * See the Unified Extensible Firmware Interface (UEFI) specification for
460 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200461 *
Mario Six8fac2912018-07-10 08:40:17 +0200462 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200463 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200464static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100465 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200466 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100467{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100468 efi_status_t r;
469
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100470 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200471 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100472 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100473}
474
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200475/**
Mario Six8fac2912018-07-10 08:40:17 +0200476 * efi_free_pool_ext() - free memory from pool
477 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200478 *
479 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200480 *
Mario Six8fac2912018-07-10 08:40:17 +0200481 * See the Unified Extensible Firmware Interface (UEFI) specification for
482 * details.
483 *
484 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200485 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200486static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100487{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100488 efi_status_t r;
489
490 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200491 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100492 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100493}
494
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200495/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200496 * efi_add_handle() - add a new handle to the object list
497 *
498 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100499 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200500 * The protocols list is initialized. The handle is added to the list of known
501 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100502 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200503void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100504{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200505 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100506 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200507 INIT_LIST_HEAD(&handle->protocols);
508 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100509}
510
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200511/**
Mario Six8fac2912018-07-10 08:40:17 +0200512 * efi_create_handle() - create handle
513 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200514 *
Mario Six8fac2912018-07-10 08:40:17 +0200515 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200516 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100517efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200518{
519 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200520
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200521 obj = calloc(1, sizeof(struct efi_object));
522 if (!obj)
523 return EFI_OUT_OF_RESOURCES;
524
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100525 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200526 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200527
528 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200529}
530
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200531/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100536 *
Mario Six8fac2912018-07-10 08:40:17 +0200537 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100538 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100539efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-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 Schuchardt2a22db92022-03-09 19:56:23 +0100555 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-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 Schuchardt59999172018-05-11 18:15:41 +0200564/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100569 *
Mario Six8fac2912018-07-10 08:40:17 +0200570 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100571 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100572efi_status_t efi_remove_protocol(const efi_handle_t handle,
573 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-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 Schuchardtb27594d2018-05-11 12:09:21 +0200582 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200583 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100584 list_del(&handler->link);
585 free(handler);
586 return EFI_SUCCESS;
587}
588
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200589/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100592 *
Mario Six8fac2912018-07-10 08:40:17 +0200593 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100595efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100596{
597 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100598 struct efi_handler *protocol;
599 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600
601 efiobj = efi_search_obj(handle);
602 if (!efiobj)
603 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100604 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100605 efi_status_t ret;
606
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100607 ret = efi_remove_protocol(handle, &protocol->guid,
Heinrich Schuchardt7d135462017-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 Schuchardt59999172018-05-11 18:15:41 +0200615/**
Mario Six8fac2912018-07-10 08:40:17 +0200616 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100617 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200618 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100619 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200620void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100621{
Etienne Carriereb20a42b2022-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 Schuchardt7d135462017-12-04 18:03:02 +0100627 return;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200628 }
629
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200630 list_del(&handle->link);
631 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100632}
633
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200634/**
Mario Six8fac2912018-07-10 08:40:17 +0200635 * efi_is_event() - check if a pointer is a valid event
636 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100637 *
Mario Six8fac2912018-07-10 08:40:17 +0200638 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100639 */
Heinrich Schuchardt370c7a82018-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 Grafc15d9212016-03-04 01:09:59 +0100652
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200653/**
Mario Six8fac2912018-07-10 08:40:17 +0200654 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200655 *
Mario Six8fac2912018-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 Schuchardta40db6e2017-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 Six8fac2912018-07-10 08:40:17 +0200668 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200669 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100670efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200671 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200672 struct efi_event *event,
673 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100674 void *notify_context, efi_guid_t *group,
675 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100676{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100677 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200678 efi_status_t ret;
679 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200680
Jonathan Gray7758b212017-03-12 19:26:07 +1100681 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200682 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100683
Heinrich Schuchardt7f0f0052018-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 Schuchardt4429d872019-07-11 20:15:09 +0200692 pool_type = EFI_BOOT_SERVICES_DATA;
693 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200694 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200695 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200696 break;
697 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200698 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200699 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100700
Heinrich Schuchardt3d83b732021-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 Takahiro3f3835d2018-08-10 15:36:32 +0900707 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100708 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
709 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200710 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100711
Heinrich Schuchardt4429d872019-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 Schuchardt370c7a82018-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 Schuchardtbf7f1692018-02-18 15:17:52 +0100721 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200722 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100723 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100724 list_add_tail(&evt->link, &efi_events);
725 *event = evt;
726 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100727}
728
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200729/*
Mario Six8fac2912018-07-10 08:40:17 +0200730 * efi_create_event_ex() - create an event in a group
731 * @type: type of the event to create
732 * @notify_tpl: task priority level of the event
733 * @notify_function: notification function of the event
734 * @notify_context: pointer passed to the notification function
735 * @event: created event
736 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100737 *
738 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100739 *
Mario Six8fac2912018-07-10 08:40:17 +0200740 * See the Unified Extensible Firmware Interface (UEFI) specification for
741 * details.
742 *
743 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100744 */
745efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
746 void (EFIAPI *notify_function) (
747 struct efi_event *event,
748 void *context),
749 void *notify_context,
750 efi_guid_t *event_group,
751 struct efi_event **event)
752{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200753 efi_status_t ret;
754
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100755 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100756 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200757
758 /*
759 * The allowable input parameters are the same as in CreateEvent()
760 * except for the following two disallowed event types.
761 */
762 switch (type) {
763 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
764 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
765 ret = EFI_INVALID_PARAMETER;
766 goto out;
767 }
768
769 ret = efi_create_event(type, notify_tpl, notify_function,
770 notify_context, event_group, event);
771out:
772 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100773}
774
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200775/**
Mario Six8fac2912018-07-10 08:40:17 +0200776 * efi_create_event_ext() - create an event
777 * @type: type of the event to create
778 * @notify_tpl: task priority level of the event
779 * @notify_function: notification function of the event
780 * @notify_context: pointer passed to the notification function
781 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200782 *
783 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200784 *
785 * See the Unified Extensible Firmware Interface (UEFI) specification for
786 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200787 *
Mario Six8fac2912018-07-10 08:40:17 +0200788 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200789 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200790static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100791 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200792 void (EFIAPI *notify_function) (
793 struct efi_event *event,
794 void *context),
795 void *notify_context, struct efi_event **event)
796{
797 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
798 notify_context);
799 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100800 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200801}
802
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200803/**
Mario Six8fac2912018-07-10 08:40:17 +0200804 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200805 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200806 * Check if a timer event has occurred or a queued notification function should
807 * be called.
808 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100809 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200810 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100811 */
812void efi_timer_check(void)
813{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100814 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100815 u64 now = timer_get_us();
816
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100817 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200818 if (!timers_enabled)
819 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100820 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200821 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100822 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200823 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100824 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200825 break;
826 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100827 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200828 break;
829 default:
830 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200831 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100832 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200833 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100834 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200835 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200836 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100837}
838
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200839/**
Mario Six8fac2912018-07-10 08:40:17 +0200840 * efi_set_timer() - set the trigger time for a timer event or stop the event
841 * @event: event for which the timer is set
842 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200843 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200844 *
845 * This is the function for internal usage in U-Boot. For the API function
846 * implementing the SetTimer service see efi_set_timer_ext.
847 *
Mario Six8fac2912018-07-10 08:40:17 +0200848 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200849 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200850efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200851 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100852{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100853 /* Check that the event is valid */
854 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
855 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100856
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200857 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200858 * The parameter defines a multiple of 100 ns.
859 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200860 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200861 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100862
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100863 switch (type) {
864 case EFI_TIMER_STOP:
865 event->trigger_next = -1ULL;
866 break;
867 case EFI_TIMER_PERIODIC:
868 case EFI_TIMER_RELATIVE:
869 event->trigger_next = timer_get_us() + trigger_time;
870 break;
871 default:
872 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100873 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100874 event->trigger_type = type;
875 event->trigger_time = trigger_time;
876 event->is_signaled = false;
877 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100878}
879
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200880/**
Mario Six8fac2912018-07-10 08:40:17 +0200881 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
882 * event
883 * @event: event for which the timer is set
884 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200885 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 *
887 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200888 *
Mario Six8fac2912018-07-10 08:40:17 +0200889 * See the Unified Extensible Firmware Interface (UEFI) specification for
890 * details.
891 *
892 *
893 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200894 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200895static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
896 enum efi_timer_delay type,
897 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200898{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900899 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200900 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
901}
902
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200903/**
Mario Six8fac2912018-07-10 08:40:17 +0200904 * efi_wait_for_event() - wait for events to be signaled
905 * @num_events: number of events to be waited for
906 * @event: events to be waited for
907 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200908 *
909 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 *
Mario Six8fac2912018-07-10 08:40:17 +0200911 * See the Unified Extensible Firmware Interface (UEFI) specification for
912 * details.
913 *
914 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200915 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100916static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200917 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100918 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100919{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100921
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100922 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100923
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200924 /* Check parameters */
925 if (!num_events || !event)
926 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200927 /* Check TPL */
928 if (efi_tpl != TPL_APPLICATION)
929 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200930 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100931 if (efi_is_event(event[i]) != EFI_SUCCESS)
932 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200933 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
934 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200935 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200936 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200937 }
938
939 /* Wait for signal */
940 for (;;) {
941 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200942 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200943 goto out;
944 }
945 /* Allow events to occur. */
946 efi_timer_check();
947 }
948
949out:
950 /*
951 * Reset the signal which is passed to the caller to allow periodic
952 * events to occur.
953 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200954 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200955 if (index)
956 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100957
958 return EFI_EXIT(EFI_SUCCESS);
959}
960
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200961/**
Mario Six8fac2912018-07-10 08:40:17 +0200962 * efi_signal_event_ext() - signal an EFI event
963 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200964 *
965 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200966 *
967 * See the Unified Extensible Firmware Interface (UEFI) specification for
968 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200969 *
970 * This functions sets the signaled state of the event and queues the
971 * notification function for execution.
972 *
Mario Six8fac2912018-07-10 08:40:17 +0200973 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200974 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200975static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100976{
977 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100978 if (efi_is_event(event) != EFI_SUCCESS)
979 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200980 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100981 return EFI_EXIT(EFI_SUCCESS);
982}
983
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200984/**
Mario Six8fac2912018-07-10 08:40:17 +0200985 * efi_close_event() - close an EFI event
986 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200987 *
988 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200989 *
Mario Six8fac2912018-07-10 08:40:17 +0200990 * See the Unified Extensible Firmware Interface (UEFI) specification for
991 * details.
992 *
993 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200994 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200995static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100996{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200997 struct efi_register_notify_event *item, *next;
998
Alexander Grafc15d9212016-03-04 01:09:59 +0100999 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001000 if (efi_is_event(event) != EFI_SUCCESS)
1001 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001002
1003 /* Remove protocol notify registrations for the event */
1004 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1005 link) {
1006 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001007 struct efi_protocol_notification *hitem, *hnext;
1008
1009 /* Remove signaled handles */
1010 list_for_each_entry_safe(hitem, hnext, &item->handles,
1011 link) {
1012 list_del(&hitem->link);
1013 free(hitem);
1014 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001015 list_del(&item->link);
1016 free(item);
1017 }
1018 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001019 /* Remove event from queue */
1020 if (efi_event_is_queued(event))
1021 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001022
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001023 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001024 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001025 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001026}
1027
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001028/**
Mario Six8fac2912018-07-10 08:40:17 +02001029 * efi_check_event() - check if an event is signaled
1030 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001031 *
1032 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001033 *
1034 * See the Unified Extensible Firmware Interface (UEFI) specification for
1035 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001036 *
Mario Six8fac2912018-07-10 08:40:17 +02001037 * If an event is not signaled yet, the notification function is queued. The
1038 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001039 *
Mario Six8fac2912018-07-10 08:40:17 +02001040 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001041 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001042static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001043{
1044 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001045 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001046 if (efi_is_event(event) != EFI_SUCCESS ||
1047 event->type & EVT_NOTIFY_SIGNAL)
1048 return EFI_EXIT(EFI_INVALID_PARAMETER);
1049 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001050 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001051 if (event->is_signaled) {
1052 event->is_signaled = false;
1053 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001054 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001055 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001056}
1057
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001058/**
Mario Six8fac2912018-07-10 08:40:17 +02001059 * efi_search_obj() - find the internal EFI object for a handle
1060 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001061 *
Mario Six8fac2912018-07-10 08:40:17 +02001062 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001063 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001064struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001065{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001066 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001067
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001068 if (!handle)
1069 return NULL;
1070
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001071 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001072 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001073 return efiobj;
1074 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001075 return NULL;
1076}
1077
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001078/**
Mario Six8fac2912018-07-10 08:40:17 +02001079 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1080 * to a protocol
1081 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001082 *
Mario Six8fac2912018-07-10 08:40:17 +02001083 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001084 */
1085static struct efi_open_protocol_info_entry *efi_create_open_info(
1086 struct efi_handler *handler)
1087{
1088 struct efi_open_protocol_info_item *item;
1089
1090 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1091 if (!item)
1092 return NULL;
1093 /* Append the item to the open protocol info list. */
1094 list_add_tail(&item->link, &handler->open_infos);
1095
1096 return &item->info;
1097}
1098
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001099/**
Mario Six8fac2912018-07-10 08:40:17 +02001100 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1101 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001102 *
Mario Six8fac2912018-07-10 08:40:17 +02001103 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001104 */
1105static efi_status_t efi_delete_open_info(
1106 struct efi_open_protocol_info_item *item)
1107{
1108 list_del(&item->link);
1109 free(item);
1110 return EFI_SUCCESS;
1111}
1112
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001113/**
Mario Six8fac2912018-07-10 08:40:17 +02001114 * efi_add_protocol() - install new protocol on a handle
1115 * @handle: handle on which the protocol shall be installed
1116 * @protocol: GUID of the protocol to be installed
1117 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001118 *
Mario Six8fac2912018-07-10 08:40:17 +02001119 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001120 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001121efi_status_t efi_add_protocol(const efi_handle_t handle,
1122 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001123 void *protocol_interface)
1124{
1125 struct efi_object *efiobj;
1126 struct efi_handler *handler;
1127 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001128 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001129
1130 efiobj = efi_search_obj(handle);
1131 if (!efiobj)
1132 return EFI_INVALID_PARAMETER;
1133 ret = efi_search_protocol(handle, protocol, NULL);
1134 if (ret != EFI_NOT_FOUND)
1135 return EFI_INVALID_PARAMETER;
1136 handler = calloc(1, sizeof(struct efi_handler));
1137 if (!handler)
1138 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001139 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001140 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001141 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001142 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001143
1144 /* Notify registered events */
1145 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001146 if (!guidcmp(protocol, &event->protocol)) {
1147 struct efi_protocol_notification *notif;
1148
1149 notif = calloc(1, sizeof(*notif));
1150 if (!notif) {
1151 list_del(&handler->link);
1152 free(handler);
1153 return EFI_OUT_OF_RESOURCES;
1154 }
1155 notif->handle = handle;
1156 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001157 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001158 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001159 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001160 }
1161
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001162 if (!guidcmp(&efi_guid_device_path, protocol))
1163 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001164 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001165}
1166
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001167/**
Mario Six8fac2912018-07-10 08:40:17 +02001168 * efi_install_protocol_interface() - install protocol interface
1169 * @handle: handle on which the protocol shall be installed
1170 * @protocol: GUID of the protocol to be installed
1171 * @protocol_interface_type: type of the interface to be installed,
1172 * always EFI_NATIVE_INTERFACE
1173 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001174 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001175 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001176 *
Mario Six8fac2912018-07-10 08:40:17 +02001177 * See the Unified Extensible Firmware Interface (UEFI) specification for
1178 * details.
1179 *
1180 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001181 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001182static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001183 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001184 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001185{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001186 efi_status_t r;
1187
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001188 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001189 protocol_interface);
1190
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001191 if (!handle || !protocol ||
1192 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1193 r = EFI_INVALID_PARAMETER;
1194 goto out;
1195 }
1196
1197 /* Create new handle if requested. */
1198 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001199 r = efi_create_handle(handle);
1200 if (r != EFI_SUCCESS)
1201 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001202 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001203 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001204 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001205 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001206 /* Add new protocol */
1207 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001208out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001209 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001210}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001211
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001212/**
Mario Six8fac2912018-07-10 08:40:17 +02001213 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001214 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001215 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001216 * @number_of_drivers: number of child controllers
1217 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001218 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001219 * The allocated buffer has to be freed with free().
1220 *
Mario Six8fac2912018-07-10 08:40:17 +02001221 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001222 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001223static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001224 const efi_guid_t *protocol,
1225 efi_uintn_t *number_of_drivers,
1226 efi_handle_t **driver_handle_buffer)
1227{
1228 struct efi_handler *handler;
1229 struct efi_open_protocol_info_item *item;
1230 efi_uintn_t count = 0, i;
1231 bool duplicate;
1232
1233 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001234 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001235 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001236 continue;
1237 list_for_each_entry(item, &handler->open_infos, link) {
1238 if (item->info.attributes &
1239 EFI_OPEN_PROTOCOL_BY_DRIVER)
1240 ++count;
1241 }
1242 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001243 *number_of_drivers = 0;
1244 if (!count) {
1245 *driver_handle_buffer = NULL;
1246 return EFI_SUCCESS;
1247 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001248 /*
1249 * Create buffer. In case of duplicate driver assignments the buffer
1250 * will be too large. But that does not harm.
1251 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001252 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1253 if (!*driver_handle_buffer)
1254 return EFI_OUT_OF_RESOURCES;
1255 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001256 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001257 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001258 continue;
1259 list_for_each_entry(item, &handler->open_infos, link) {
1260 if (item->info.attributes &
1261 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1262 /* Check this is a new driver */
1263 duplicate = false;
1264 for (i = 0; i < *number_of_drivers; ++i) {
1265 if ((*driver_handle_buffer)[i] ==
1266 item->info.agent_handle)
1267 duplicate = true;
1268 }
1269 /* Copy handle to buffer */
1270 if (!duplicate) {
1271 i = (*number_of_drivers)++;
1272 (*driver_handle_buffer)[i] =
1273 item->info.agent_handle;
1274 }
1275 }
1276 }
1277 }
1278 return EFI_SUCCESS;
1279}
1280
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001281/**
Mario Six8fac2912018-07-10 08:40:17 +02001282 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001283 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001284 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001285 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001286 *
1287 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001288 *
1289 * See the Unified Extensible Firmware Interface (UEFI) specification for
1290 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001291 *
Mario Six8fac2912018-07-10 08:40:17 +02001292 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001293 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001294static efi_status_t efi_disconnect_all_drivers
1295 (efi_handle_t handle,
1296 const efi_guid_t *protocol,
1297 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001298{
1299 efi_uintn_t number_of_drivers;
1300 efi_handle_t *driver_handle_buffer;
1301 efi_status_t r, ret;
1302
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001303 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001304 &driver_handle_buffer);
1305 if (ret != EFI_SUCCESS)
1306 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001307 if (!number_of_drivers)
1308 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001309 ret = EFI_NOT_FOUND;
1310 while (number_of_drivers) {
1311 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001312 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001313 driver_handle_buffer[--number_of_drivers],
1314 child_handle));
1315 if (r == EFI_SUCCESS)
1316 ret = r;
1317 }
1318 free(driver_handle_buffer);
1319 return ret;
1320}
1321
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001322/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001323 * efi_uninstall_protocol() - uninstall protocol interface
1324 *
Mario Six8fac2912018-07-10 08:40:17 +02001325 * @handle: handle from which the protocol shall be removed
1326 * @protocol: GUID of the protocol to be removed
1327 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001328 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001329 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001330 *
1331 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001332 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001333static efi_status_t efi_uninstall_protocol
1334 (efi_handle_t handle, const efi_guid_t *protocol,
1335 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001336{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001337 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001338 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001339 struct efi_open_protocol_info_item *item;
1340 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001341 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001342
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001343 /* Check handle */
1344 efiobj = efi_search_obj(handle);
1345 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001346 r = EFI_INVALID_PARAMETER;
1347 goto out;
1348 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001349 /* Find the protocol on the handle */
1350 r = efi_search_protocol(handle, protocol, &handler);
1351 if (r != EFI_SUCCESS)
1352 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001353 /* Disconnect controllers */
1354 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001355 /* Close protocol */
1356 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1357 if (item->info.attributes ==
1358 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1359 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1360 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1361 list_del(&item->link);
1362 }
1363 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001364 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001365 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001366 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001367 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001368out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001369 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001370}
1371
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001372/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001373 * efi_uninstall_protocol_interface() - uninstall protocol interface
1374 * @handle: handle from which the protocol shall be removed
1375 * @protocol: GUID of the protocol to be removed
1376 * @protocol_interface: interface to be removed
1377 *
1378 * This function implements the UninstallProtocolInterface service.
1379 *
1380 * See the Unified Extensible Firmware Interface (UEFI) specification for
1381 * details.
1382 *
1383 * Return: status code
1384 */
1385static efi_status_t EFIAPI efi_uninstall_protocol_interface
1386 (efi_handle_t handle, const efi_guid_t *protocol,
1387 void *protocol_interface)
1388{
1389 efi_status_t ret;
1390
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001391 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001392
1393 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1394 if (ret != EFI_SUCCESS)
1395 goto out;
1396
1397 /* If the last protocol has been removed, delete the handle. */
1398 if (list_empty(&handle->protocols)) {
1399 list_del(&handle->link);
1400 free(handle);
1401 }
1402out:
1403 return EFI_EXIT(ret);
1404}
1405
1406/**
Mario Six8fac2912018-07-10 08:40:17 +02001407 * efi_register_protocol_notify() - register an event for notification when a
1408 * protocol is installed.
1409 * @protocol: GUID of the protocol whose installation shall be notified
1410 * @event: event to be signaled upon installation of the protocol
1411 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001412 *
1413 * This function implements the RegisterProtocolNotify service.
1414 * See the Unified Extensible Firmware Interface (UEFI) specification
1415 * for details.
1416 *
Mario Six8fac2912018-07-10 08:40:17 +02001417 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001418 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001419efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1420 struct efi_event *event,
1421 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001422{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001423 struct efi_register_notify_event *item;
1424 efi_status_t ret = EFI_SUCCESS;
1425
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001426 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001427
1428 if (!protocol || !event || !registration) {
1429 ret = EFI_INVALID_PARAMETER;
1430 goto out;
1431 }
1432
1433 item = calloc(1, sizeof(struct efi_register_notify_event));
1434 if (!item) {
1435 ret = EFI_OUT_OF_RESOURCES;
1436 goto out;
1437 }
1438
1439 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301440 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001441 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001442
1443 list_add_tail(&item->link, &efi_register_notify_events);
1444
1445 *registration = item;
1446out:
1447 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001448}
1449
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001450/**
Mario Six8fac2912018-07-10 08:40:17 +02001451 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001452 *
Mario Six8fac2912018-07-10 08:40:17 +02001453 * @search_type: selection criterion
1454 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001455 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001456 *
1457 * See the documentation of the LocateHandle service in the UEFI specification.
1458 *
Mario Six8fac2912018-07-10 08:40:17 +02001459 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001460 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001461static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001462 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001463{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001464 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001465
1466 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001467 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001468 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001469 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001470 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001471 return (ret != EFI_SUCCESS);
1472 default:
1473 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001474 return -1;
1475 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001476}
1477
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001478/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001479 * efi_check_register_notify_event() - check if registration key is valid
1480 *
1481 * Check that a pointer is a valid registration key as returned by
1482 * RegisterProtocolNotify().
1483 *
1484 * @key: registration key
1485 * Return: valid registration key or NULL
1486 */
1487static struct efi_register_notify_event *efi_check_register_notify_event
1488 (void *key)
1489{
1490 struct efi_register_notify_event *event;
1491
1492 list_for_each_entry(event, &efi_register_notify_events, link) {
1493 if (event == (struct efi_register_notify_event *)key)
1494 return event;
1495 }
1496 return NULL;
1497}
1498
1499/**
Mario Six8fac2912018-07-10 08:40:17 +02001500 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001501 *
1502 * @search_type: selection criterion
1503 * @protocol: GUID of the protocol
1504 * @search_key: registration key
1505 * @buffer_size: size of the buffer to receive the handles in bytes
1506 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001507 *
1508 * This function is meant for U-Boot internal calls. For the API implementation
1509 * of the LocateHandle service see efi_locate_handle_ext.
1510 *
Mario Six8fac2912018-07-10 08:40:17 +02001511 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001512 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001513static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001514 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001515 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001516 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001517{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001518 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001519 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001520 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001521 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001522
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001523 /* Check parameters */
1524 switch (search_type) {
1525 case ALL_HANDLES:
1526 break;
1527 case BY_REGISTER_NOTIFY:
1528 if (!search_key)
1529 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001530 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001531 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001532 if (!event)
1533 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001534 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001535 case BY_PROTOCOL:
1536 if (!protocol)
1537 return EFI_INVALID_PARAMETER;
1538 break;
1539 default:
1540 return EFI_INVALID_PARAMETER;
1541 }
1542
Alexander Grafc15d9212016-03-04 01:09:59 +01001543 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001544 if (search_type == BY_REGISTER_NOTIFY) {
1545 if (list_empty(&event->handles))
1546 return EFI_NOT_FOUND;
1547 handle = list_first_entry(&event->handles,
1548 struct efi_protocol_notification,
1549 link);
1550 efiobj = handle->handle;
1551 size += sizeof(void *);
1552 } else {
1553 list_for_each_entry(efiobj, &efi_obj_list, link) {
1554 if (!efi_search(search_type, protocol, efiobj))
1555 size += sizeof(void *);
1556 }
1557 if (size == 0)
1558 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001559 }
1560
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001561 if (!buffer_size)
1562 return EFI_INVALID_PARAMETER;
1563
Alexander Grafc15d9212016-03-04 01:09:59 +01001564 if (*buffer_size < size) {
1565 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001566 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001567 }
1568
Rob Clarkcdee3372017-08-06 14:10:07 -04001569 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001570
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001571 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001572 if (!buffer)
1573 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001574
Alexander Grafc15d9212016-03-04 01:09:59 +01001575 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001576 if (search_type == BY_REGISTER_NOTIFY) {
1577 *buffer = efiobj;
1578 list_del(&handle->link);
1579 } else {
1580 list_for_each_entry(efiobj, &efi_obj_list, link) {
1581 if (!efi_search(search_type, protocol, efiobj))
1582 *buffer++ = efiobj;
1583 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001584 }
1585
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001586 return EFI_SUCCESS;
1587}
1588
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001589/**
Mario Six8fac2912018-07-10 08:40:17 +02001590 * efi_locate_handle_ext() - locate handles implementing a protocol.
1591 * @search_type: selection criterion
1592 * @protocol: GUID of the protocol
1593 * @search_key: registration key
1594 * @buffer_size: size of the buffer to receive the handles in bytes
1595 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001596 *
1597 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001598 *
Mario Six8fac2912018-07-10 08:40:17 +02001599 * See the Unified Extensible Firmware Interface (UEFI) specification for
1600 * details.
1601 *
1602 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001603 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001604static efi_status_t EFIAPI efi_locate_handle_ext(
1605 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001606 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001607 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001608{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001609 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001610 buffer_size, buffer);
1611
1612 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1613 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001614}
1615
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001616/**
Mario Six8fac2912018-07-10 08:40:17 +02001617 * efi_remove_configuration_table() - collapses configuration table entries,
1618 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001619 *
Mario Six8fac2912018-07-10 08:40:17 +02001620 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001621 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001622static void efi_remove_configuration_table(int i)
1623{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001624 struct efi_configuration_table *this = &systab.tables[i];
1625 struct efi_configuration_table *next = &systab.tables[i + 1];
1626 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001627
1628 memmove(this, next, (ulong)end - (ulong)next);
1629 systab.nr_tables--;
1630}
1631
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001632/**
Mario Six8fac2912018-07-10 08:40:17 +02001633 * efi_install_configuration_table() - adds, updates, or removes a
1634 * configuration table
1635 * @guid: GUID of the installed table
1636 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001637 *
1638 * This function is used for internal calls. For the API implementation of the
1639 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1640 *
Mario Six8fac2912018-07-10 08:40:17 +02001641 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001642 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001643efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1644 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001645{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001646 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001647 int i;
1648
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001649 if (!guid)
1650 return EFI_INVALID_PARAMETER;
1651
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001652 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001653 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001654 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001655 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001656 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001657 else
1658 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001659 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001660 }
1661 }
1662
Alexander Graffe3366f2017-07-26 13:41:04 +02001663 if (!table)
1664 return EFI_NOT_FOUND;
1665
Alexander Grafc15d9212016-03-04 01:09:59 +01001666 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001667 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001668 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001669
1670 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301671 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001672 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001673 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001674
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001675out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001676 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001677 efi_update_table_header_crc32(&systab.hdr);
1678
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001679 /* Notify that the configuration table was changed */
1680 list_for_each_entry(evt, &efi_events, link) {
1681 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001682 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001683 break;
1684 }
1685 }
1686
Alexander Grafc5c11632016-08-19 01:23:24 +02001687 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001688}
1689
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001690/**
Mario Six8fac2912018-07-10 08:40:17 +02001691 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1692 * configuration table.
1693 * @guid: GUID of the installed table
1694 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001695 *
1696 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001697 *
Mario Six8fac2912018-07-10 08:40:17 +02001698 * See the Unified Extensible Firmware Interface (UEFI) specification for
1699 * details.
1700 *
1701 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001702 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001703static efi_status_t
1704EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1705 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001706{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001707 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001708 return EFI_EXIT(efi_install_configuration_table(guid, table));
1709}
1710
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001711/**
Mario Six8fac2912018-07-10 08:40:17 +02001712 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001713 *
1714 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001715 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001716 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001717 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001718 * code is returned.
1719 *
1720 * @device_path: device path of the loaded image
1721 * @file_path: file path of the loaded image
1722 * @handle_ptr: handle of the loaded image
1723 * @info_ptr: loaded image protocol
1724 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001725 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001726efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1727 struct efi_device_path *file_path,
1728 struct efi_loaded_image_obj **handle_ptr,
1729 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001730{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001731 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001732 struct efi_loaded_image *info = NULL;
1733 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001734 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001735
1736 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1737 *handle_ptr = NULL;
1738 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001739
1740 info = calloc(1, sizeof(*info));
1741 if (!info)
1742 return EFI_OUT_OF_RESOURCES;
1743 obj = calloc(1, sizeof(*obj));
1744 if (!obj) {
1745 free(info);
1746 return EFI_OUT_OF_RESOURCES;
1747 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001748 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001749
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001750 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001751 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001752
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001753 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001754 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001755 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001756
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001757 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001758 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001759
1760 dp = efi_dp_append(device_path, file_path);
1761 if (!dp) {
1762 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001763 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001764 }
1765 } else {
1766 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001767 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001768 ret = efi_add_protocol(&obj->header,
1769 &efi_guid_loaded_image_device_path, dp);
1770 if (ret != EFI_SUCCESS)
1771 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001772
1773 /*
1774 * When asking for the loaded_image interface, just
1775 * return handle which points to loaded_image_info
1776 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001777 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001778 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001779 if (ret != EFI_SUCCESS)
1780 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001781
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001782 *info_ptr = info;
1783 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001784
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001785 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001786failure:
1787 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001788 efi_delete_handle(&obj->header);
1789 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001790 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001791}
1792
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001793/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001794 * efi_locate_device_path() - Get the device path and handle of an device
1795 * implementing a protocol
1796 * @protocol: GUID of the protocol
1797 * @device_path: device path
1798 * @device: handle of the device
1799 *
1800 * This function implements the LocateDevicePath service.
1801 *
1802 * See the Unified Extensible Firmware Interface (UEFI) specification for
1803 * details.
1804 *
1805 * Return: status code
1806 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001807efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1808 struct efi_device_path **device_path,
1809 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001810{
1811 struct efi_device_path *dp;
1812 size_t i;
1813 struct efi_handler *handler;
1814 efi_handle_t *handles;
1815 size_t len, len_dp;
1816 size_t len_best = 0;
1817 efi_uintn_t no_handles;
1818 u8 *remainder;
1819 efi_status_t ret;
1820
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001821 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001822
1823 if (!protocol || !device_path || !*device_path) {
1824 ret = EFI_INVALID_PARAMETER;
1825 goto out;
1826 }
1827
1828 /* Find end of device path */
1829 len = efi_dp_instance_size(*device_path);
1830
1831 /* Get all handles implementing the protocol */
1832 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1833 &no_handles, &handles));
1834 if (ret != EFI_SUCCESS)
1835 goto out;
1836
1837 for (i = 0; i < no_handles; ++i) {
1838 /* Find the device path protocol */
1839 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1840 &handler);
1841 if (ret != EFI_SUCCESS)
1842 continue;
1843 dp = (struct efi_device_path *)handler->protocol_interface;
1844 len_dp = efi_dp_instance_size(dp);
1845 /*
1846 * This handle can only be a better fit
1847 * if its device path length is longer than the best fit and
1848 * if its device path length is shorter of equal the searched
1849 * device path.
1850 */
1851 if (len_dp <= len_best || len_dp > len)
1852 continue;
1853 /* Check if dp is a subpath of device_path */
1854 if (memcmp(*device_path, dp, len_dp))
1855 continue;
1856 if (!device) {
1857 ret = EFI_INVALID_PARAMETER;
1858 goto out;
1859 }
1860 *device = handles[i];
1861 len_best = len_dp;
1862 }
1863 if (len_best) {
1864 remainder = (u8 *)*device_path + len_best;
1865 *device_path = (struct efi_device_path *)remainder;
1866 ret = EFI_SUCCESS;
1867 } else {
1868 ret = EFI_NOT_FOUND;
1869 }
1870out:
1871 return EFI_EXIT(ret);
1872}
1873
1874/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001875 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001876 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001877 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1878 * callers obligation to update the memory type as needed.
1879 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001880 * @file_path: the path of the image to load
1881 * @buffer: buffer containing the loaded image
1882 * @size: size of the loaded image
1883 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001884 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001885static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001886efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001887 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001888{
Rob Clark857a1222017-09-13 18:05:35 -04001889 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001890 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001891 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001892 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001893
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001894 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001895 f = efi_file_from_path(file_path);
1896 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001897 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001898
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001899 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001900 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001901 goto error;
1902
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001903 /*
1904 * When reading the file we do not yet know if it contains an
1905 * application, a boottime driver, or a runtime driver. So here we
1906 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1907 * update the reservation according to the image type.
1908 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001909 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1910 EFI_BOOT_SERVICES_DATA,
1911 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001912 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001913 ret = EFI_OUT_OF_RESOURCES;
1914 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001915 }
1916
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001917 /* Read file */
1918 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1919 if (ret != EFI_SUCCESS)
1920 efi_free_pages(addr, efi_size_in_pages(bs));
1921 *buffer = (void *)(uintptr_t)addr;
1922 *size = bs;
1923error:
1924 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001925 return ret;
1926}
1927
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001928/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001929 * efi_load_image_from_path() - load an image using a file path
1930 *
1931 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1932 * callers obligation to update the memory type as needed.
1933 *
1934 * @boot_policy: true for request originating from the boot manager
1935 * @file_path: the path of the image to load
1936 * @buffer: buffer containing the loaded image
1937 * @size: size of the loaded image
1938 * Return: status code
1939 */
1940static
1941efi_status_t efi_load_image_from_path(bool boot_policy,
1942 struct efi_device_path *file_path,
1943 void **buffer, efi_uintn_t *size)
1944{
1945 efi_handle_t device;
1946 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001947 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001948 struct efi_load_file_protocol *load_file_protocol = NULL;
1949 efi_uintn_t buffer_size;
1950 uint64_t addr, pages;
1951 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001952 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001953
1954 /* In case of failure nothing is returned */
1955 *buffer = NULL;
1956 *size = 0;
1957
1958 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001959 device = efi_dp_find_obj(dp, NULL, &rem);
1960 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1961 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001962 if (ret == EFI_SUCCESS)
1963 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001964
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001965 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001966 if (ret == EFI_SUCCESS) {
1967 guid = &efi_guid_load_file_protocol;
1968 } else if (!boot_policy) {
1969 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001970 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001971 }
1972 if (ret != EFI_SUCCESS)
1973 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001974 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001975 if (ret != EFI_SUCCESS)
1976 return EFI_NOT_FOUND;
1977 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001978 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001979 ret = EFI_CALL(load_file_protocol->load_file(
1980 load_file_protocol, rem, boot_policy,
1981 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001982 if (ret != EFI_BUFFER_TOO_SMALL)
1983 goto out;
1984 pages = efi_size_in_pages(buffer_size);
1985 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1986 pages, &addr);
1987 if (ret != EFI_SUCCESS) {
1988 ret = EFI_OUT_OF_RESOURCES;
1989 goto out;
1990 }
1991 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001992 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001993 &buffer_size, (void *)(uintptr_t)addr));
1994 if (ret != EFI_SUCCESS)
1995 efi_free_pages(addr, pages);
1996out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02001997 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001998 if (ret == EFI_SUCCESS) {
1999 *buffer = (void *)(uintptr_t)addr;
2000 *size = buffer_size;
2001 }
2002
2003 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002004}
2005
2006/**
Mario Six8fac2912018-07-10 08:40:17 +02002007 * efi_load_image() - load an EFI image into memory
2008 * @boot_policy: true for request originating from the boot manager
2009 * @parent_image: the caller's image handle
2010 * @file_path: the path of the image to load
2011 * @source_buffer: memory location from which the image is installed
2012 * @source_size: size of the memory area from which the image is installed
2013 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002014 *
2015 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002016 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002017 * See the Unified Extensible Firmware Interface (UEFI) specification
2018 * for details.
2019 *
Mario Six8fac2912018-07-10 08:40:17 +02002020 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002021 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002022efi_status_t EFIAPI efi_load_image(bool boot_policy,
2023 efi_handle_t parent_image,
2024 struct efi_device_path *file_path,
2025 void *source_buffer,
2026 efi_uintn_t source_size,
2027 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002028{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002029 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002030 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002031 struct efi_loaded_image_obj **image_obj =
2032 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002033 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002034 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002035
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002036 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002037 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002038
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002039 if (!image_handle || (!source_buffer && !file_path) ||
2040 !efi_search_obj(parent_image) ||
2041 /* The parent image handle must refer to a loaded image */
2042 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002043 ret = EFI_INVALID_PARAMETER;
2044 goto error;
2045 }
Rob Clark857a1222017-09-13 18:05:35 -04002046
2047 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002048 ret = efi_load_image_from_path(boot_policy, file_path,
2049 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002050 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002051 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002052 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002053 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002054 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002055 /* split file_path which contains both the device and file parts */
2056 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002057 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002058 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002059 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002060 if (!source_buffer)
2061 /* Release buffer to which file was loaded */
2062 efi_free_pages((uintptr_t)dest_buffer,
2063 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002064 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002065 info->system_table = &systab;
2066 info->parent_handle = parent_image;
2067 } else {
2068 /* The image is invalid. Release all associated resources. */
2069 efi_delete_handle(*image_handle);
2070 *image_handle = NULL;
2071 free(info);
2072 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002073error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002074 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002075}
2076
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002077/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002078 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2079 */
2080static void efi_exit_caches(void)
2081{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002082#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002083 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002084 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2085 * caches are enabled.
2086 *
2087 * TODO:
2088 * According to the UEFI spec caches that can be managed via CP15
2089 * operations should be enabled. Caches requiring platform information
2090 * to manage should be disabled. This should not happen in
2091 * ExitBootServices() but before invoking any UEFI binary is invoked.
2092 *
2093 * We want to keep the current workaround while GRUB prior to version
2094 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002095 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002096 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002097#endif
2098}
2099
2100/**
Mario Six8fac2912018-07-10 08:40:17 +02002101 * efi_exit_boot_services() - stop all boot services
2102 * @image_handle: handle of the loaded image
2103 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002104 *
2105 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002106 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002107 * See the Unified Extensible Firmware Interface (UEFI) specification
2108 * for details.
2109 *
Mario Six8fac2912018-07-10 08:40:17 +02002110 * All timer events are disabled. For exit boot services events the
2111 * notification function is called. The boot services are disabled in the
2112 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002113 *
Mario Six8fac2912018-07-10 08:40:17 +02002114 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002115 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002116static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002117 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002118{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002119 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002120 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002121
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002122 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002123
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002124 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002125 if (map_key != efi_memory_map_key) {
2126 ret = EFI_INVALID_PARAMETER;
2127 goto out;
2128 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002129
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002130 /* Check if ExitBootServices has already been called */
2131 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002132 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002133
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002134 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2135 list_for_each_entry(evt, &efi_events, link) {
2136 if (evt->group &&
2137 !guidcmp(evt->group,
2138 &efi_guid_event_group_before_exit_boot_services)) {
2139 efi_signal_event(evt);
2140 break;
2141 }
2142 }
2143
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002144 /* Stop all timer related activities */
2145 timers_enabled = false;
2146
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002147 /* Add related events to the event group */
2148 list_for_each_entry(evt, &efi_events, link) {
2149 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2150 evt->group = &efi_guid_event_group_exit_boot_services;
2151 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002152 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002153 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002154 if (evt->group &&
2155 !guidcmp(evt->group,
2156 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002157 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002158 break;
2159 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002160 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002161
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002162 /* Make sure that notification functions are not called anymore */
2163 efi_tpl = TPL_HIGH_LEVEL;
2164
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002165 /* Notify variable services */
2166 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002167
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002168 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2169 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2170 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2171 list_del(&evt->link);
2172 }
2173
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002174 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002175 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002176 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002177 udc_disconnect();
2178 board_quiesce_devices();
2179 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2180 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002181
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002182 /* Patch out unsupported runtime function */
2183 efi_runtime_detach();
2184
Alexander Graffa5246b2018-11-15 21:23:47 +01002185 /* Fix up caches for EFI payloads if necessary */
2186 efi_exit_caches();
2187
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002188 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002189 systab.con_in_handle = NULL;
2190 systab.con_in = NULL;
2191 systab.con_out_handle = NULL;
2192 systab.con_out = NULL;
2193 systab.stderr_handle = NULL;
2194 systab.std_err = NULL;
2195 systab.boottime = NULL;
2196
2197 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002198 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002199
Alexander Grafc15d9212016-03-04 01:09:59 +01002200 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002201 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002202 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002203out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002204 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2205 if (ret != EFI_SUCCESS)
2206 efi_tcg2_notify_exit_boot_services_failed();
2207 }
2208
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002209 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002210}
2211
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002212/**
Mario Six8fac2912018-07-10 08:40:17 +02002213 * efi_get_next_monotonic_count() - get next value of the counter
2214 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002215 *
2216 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002217 *
2218 * See the Unified Extensible Firmware Interface (UEFI) specification for
2219 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002220 *
Mario Six8fac2912018-07-10 08:40:17 +02002221 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002222 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002223static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2224{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002225 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002226 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002227
Alexander Grafc15d9212016-03-04 01:09:59 +01002228 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002229 if (!count) {
2230 ret = EFI_INVALID_PARAMETER;
2231 goto out;
2232 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002233 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002234 ret = EFI_SUCCESS;
2235out:
2236 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002237}
2238
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002239/**
Mario Six8fac2912018-07-10 08:40:17 +02002240 * efi_stall() - sleep
2241 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002242 *
Mario Six8fac2912018-07-10 08:40:17 +02002243 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002244 *
Mario Six8fac2912018-07-10 08:40:17 +02002245 * See the Unified Extensible Firmware Interface (UEFI) specification for
2246 * details.
2247 *
2248 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002249 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002250static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2251{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002252 u64 end_tick;
2253
Alexander Grafc15d9212016-03-04 01:09:59 +01002254 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002255
2256 end_tick = get_ticks() + usec_to_tick(microseconds);
2257 while (get_ticks() < end_tick)
2258 efi_timer_check();
2259
Alexander Grafc15d9212016-03-04 01:09:59 +01002260 return EFI_EXIT(EFI_SUCCESS);
2261}
2262
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002263/**
Mario Six8fac2912018-07-10 08:40:17 +02002264 * efi_set_watchdog_timer() - reset the watchdog timer
2265 * @timeout: seconds before reset by watchdog
2266 * @watchdog_code: code to be logged when resetting
2267 * @data_size: size of buffer in bytes
2268 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002269 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002270 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002271 *
Mario Six8fac2912018-07-10 08:40:17 +02002272 * See the Unified Extensible Firmware Interface (UEFI) specification for
2273 * details.
2274 *
2275 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002276 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002277static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2278 uint64_t watchdog_code,
2279 unsigned long data_size,
2280 uint16_t *watchdog_data)
2281{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002282 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002283 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002284 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002285}
2286
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002287/**
Mario Six8fac2912018-07-10 08:40:17 +02002288 * efi_close_protocol() - close a protocol
2289 * @handle: handle on which the protocol shall be closed
2290 * @protocol: GUID of the protocol to close
2291 * @agent_handle: handle of the driver
2292 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002293 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002294 * This is the function implementing the CloseProtocol service is for internal
2295 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002296 *
Mario Six8fac2912018-07-10 08:40:17 +02002297 * See the Unified Extensible Firmware Interface (UEFI) specification for
2298 * details.
2299 *
2300 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002301 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002302efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2303 efi_handle_t agent_handle,
2304 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002305{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002306 struct efi_handler *handler;
2307 struct efi_open_protocol_info_item *item;
2308 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002309 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002310
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002311 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002312 (controller_handle && !efi_search_obj(controller_handle)))
2313 return EFI_INVALID_PARAMETER;
2314 ret = efi_search_protocol(handle, protocol, &handler);
2315 if (ret != EFI_SUCCESS)
2316 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002317
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002318 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002319 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2320 if (item->info.agent_handle == agent_handle &&
2321 item->info.controller_handle == controller_handle) {
2322 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002323 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002324 }
2325 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002326
2327 return ret;
2328}
2329
2330/**
2331 * efi_close_protocol_ext() - close a protocol
2332 * @handle: handle on which the protocol shall be closed
2333 * @protocol: GUID of the protocol to close
2334 * @agent_handle: handle of the driver
2335 * @controller_handle: handle of the controller
2336 *
2337 * This function implements the CloseProtocol service.
2338 *
2339 * See the Unified Extensible Firmware Interface (UEFI) specification for
2340 * details.
2341 *
2342 * Return: status code
2343 */
2344static efi_status_t EFIAPI
2345efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2346 efi_handle_t agent_handle,
2347 efi_handle_t controller_handle)
2348{
2349 efi_status_t ret;
2350
2351 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2352 controller_handle);
2353
2354 ret = efi_close_protocol(handle, protocol,
2355 agent_handle, controller_handle);
2356
2357 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002358}
2359
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002360/**
Mario Six8fac2912018-07-10 08:40:17 +02002361 * efi_open_protocol_information() - provide information about then open status
2362 * of a protocol on a handle
2363 * @handle: handle for which the information shall be retrieved
2364 * @protocol: GUID of the protocol
2365 * @entry_buffer: buffer to receive the open protocol information
2366 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002367 *
2368 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002369 *
2370 * See the Unified Extensible Firmware Interface (UEFI) specification for
2371 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002372 *
Mario Six8fac2912018-07-10 08:40:17 +02002373 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002374 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002375static efi_status_t EFIAPI efi_open_protocol_information(
2376 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002377 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002378 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002379{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002380 unsigned long buffer_size;
2381 unsigned long count;
2382 struct efi_handler *handler;
2383 struct efi_open_protocol_info_item *item;
2384 efi_status_t r;
2385
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002386 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002387 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002388
2389 /* Check parameters */
2390 if (!entry_buffer) {
2391 r = EFI_INVALID_PARAMETER;
2392 goto out;
2393 }
2394 r = efi_search_protocol(handle, protocol, &handler);
2395 if (r != EFI_SUCCESS)
2396 goto out;
2397
2398 /* Count entries */
2399 count = 0;
2400 list_for_each_entry(item, &handler->open_infos, link) {
2401 if (item->info.open_count)
2402 ++count;
2403 }
2404 *entry_count = count;
2405 *entry_buffer = NULL;
2406 if (!count) {
2407 r = EFI_SUCCESS;
2408 goto out;
2409 }
2410
2411 /* Copy entries */
2412 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002413 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002414 (void **)entry_buffer);
2415 if (r != EFI_SUCCESS)
2416 goto out;
2417 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2418 if (item->info.open_count)
2419 (*entry_buffer)[--count] = item->info;
2420 }
2421out:
2422 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002423}
2424
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002425/**
Mario Six8fac2912018-07-10 08:40:17 +02002426 * efi_protocols_per_handle() - get protocols installed on a handle
2427 * @handle: handle for which the information is retrieved
2428 * @protocol_buffer: buffer with protocol GUIDs
2429 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002430 *
2431 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002432 *
Mario Six8fac2912018-07-10 08:40:17 +02002433 * See the Unified Extensible Firmware Interface (UEFI) specification for
2434 * details.
2435 *
2436 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002437 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002438static efi_status_t EFIAPI efi_protocols_per_handle(
2439 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002440 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002441{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002442 unsigned long buffer_size;
2443 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002444 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002445 efi_status_t r;
2446
Alexander Grafc15d9212016-03-04 01:09:59 +01002447 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2448 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002449
2450 if (!handle || !protocol_buffer || !protocol_buffer_count)
2451 return EFI_EXIT(EFI_INVALID_PARAMETER);
2452
2453 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002454 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002455
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002456 efiobj = efi_search_obj(handle);
2457 if (!efiobj)
2458 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002459
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002460 /* Count protocols */
2461 list_for_each(protocol_handle, &efiobj->protocols) {
2462 ++*protocol_buffer_count;
2463 }
2464
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002465 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002466 if (*protocol_buffer_count) {
2467 size_t j = 0;
2468
2469 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002470 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002471 (void **)protocol_buffer);
2472 if (r != EFI_SUCCESS)
2473 return EFI_EXIT(r);
2474 list_for_each(protocol_handle, &efiobj->protocols) {
2475 struct efi_handler *protocol;
2476
2477 protocol = list_entry(protocol_handle,
2478 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002479 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002480 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002481 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002482 }
2483
2484 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002485}
2486
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002487efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2488 const efi_guid_t *protocol, void *search_key,
2489 efi_uintn_t *no_handles, efi_handle_t **buffer)
2490{
2491 efi_status_t r;
2492 efi_uintn_t buffer_size = 0;
2493
2494 if (!no_handles || !buffer) {
2495 r = EFI_INVALID_PARAMETER;
2496 goto out;
2497 }
2498 *no_handles = 0;
2499 *buffer = NULL;
2500 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2501 *buffer);
2502 if (r != EFI_BUFFER_TOO_SMALL)
2503 goto out;
2504 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2505 (void **)buffer);
2506 if (r != EFI_SUCCESS)
2507 goto out;
2508 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2509 *buffer);
2510 if (r == EFI_SUCCESS)
2511 *no_handles = buffer_size / sizeof(efi_handle_t);
2512out:
2513 return r;
2514}
2515
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002516/**
Mario Six8fac2912018-07-10 08:40:17 +02002517 * efi_locate_handle_buffer() - locate handles implementing a protocol
2518 * @search_type: selection criterion
2519 * @protocol: GUID of the protocol
2520 * @search_key: registration key
2521 * @no_handles: number of returned handles
2522 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002523 *
2524 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002525 *
Mario Six8fac2912018-07-10 08:40:17 +02002526 * See the Unified Extensible Firmware Interface (UEFI) specification for
2527 * details.
2528 *
2529 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002530 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002531efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002532 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002533 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002534 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002535{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002536 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002537
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002538 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002539 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002540
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002541 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2542 no_handles, buffer);
2543
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002544 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002545}
2546
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002547/**
Mario Six8fac2912018-07-10 08:40:17 +02002548 * efi_locate_protocol() - find an interface implementing a protocol
2549 * @protocol: GUID of the protocol
2550 * @registration: registration key passed to the notification function
2551 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002552 *
2553 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002554 *
2555 * See the Unified Extensible Firmware Interface (UEFI) specification for
2556 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002557 *
Mario Six8fac2912018-07-10 08:40:17 +02002558 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002559 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002560static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002561 void *registration,
2562 void **protocol_interface)
2563{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002564 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002565 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002566 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002567
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002568 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002569
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002570 /*
2571 * The UEFI spec explicitly requires a protocol even if a registration
2572 * key is provided. This differs from the logic in LocateHandle().
2573 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002574 if (!protocol || !protocol_interface)
2575 return EFI_EXIT(EFI_INVALID_PARAMETER);
2576
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002577 if (registration) {
2578 struct efi_register_notify_event *event;
2579 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002580
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002581 event = efi_check_register_notify_event(registration);
2582 if (!event)
2583 return EFI_EXIT(EFI_INVALID_PARAMETER);
2584 /*
2585 * The UEFI spec requires to return EFI_NOT_FOUND if no
2586 * protocol instance matches protocol and registration.
2587 * So let's do the same for a mismatch between protocol and
2588 * registration.
2589 */
2590 if (guidcmp(&event->protocol, protocol))
2591 goto not_found;
2592 if (list_empty(&event->handles))
2593 goto not_found;
2594 handle = list_first_entry(&event->handles,
2595 struct efi_protocol_notification,
2596 link);
2597 efiobj = handle->handle;
2598 list_del(&handle->link);
2599 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002600 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002601 if (ret == EFI_SUCCESS)
2602 goto found;
2603 } else {
2604 list_for_each_entry(efiobj, &efi_obj_list, link) {
2605 ret = efi_search_protocol(efiobj, protocol, &handler);
2606 if (ret == EFI_SUCCESS)
2607 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002608 }
2609 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002610not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002611 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002612 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002613found:
2614 *protocol_interface = handler->protocol_interface;
2615 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002616}
2617
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002618/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002619 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002620 * interfaces
2621 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002622 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002623 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002624 * Core functionality of efi_install_multiple_protocol_interfaces
2625 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002626 *
Mario Six8fac2912018-07-10 08:40:17 +02002627 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002628 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002629static efi_status_t EFIAPI
2630efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2631 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002632{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002633 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002634 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002635 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002636 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002637 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002638 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002639
2640 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002641 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002642
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002643 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002644 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002645 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002646 if (!protocol)
2647 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002648 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002649 /* Check that a device path has not been installed before */
2650 if (!guidcmp(protocol, &efi_guid_device_path)) {
2651 struct efi_device_path *dp = protocol_interface;
2652
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002653 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2654 &old_handle));
2655 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002656 dp->type == DEVICE_PATH_TYPE_END) {
2657 EFI_PRINT("Path %pD already installed\n",
2658 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002659 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002660 break;
2661 }
2662 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002663 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2664 EFI_NATIVE_INTERFACE,
2665 protocol_interface));
2666 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002667 break;
2668 i++;
2669 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002670 if (ret == EFI_SUCCESS)
2671 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002672
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002673 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002674 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002675 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2676 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002677 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002678 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002679 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002680
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002681out:
2682 efi_va_end(argptr_copy);
2683 return ret;
2684
Alexander Grafc15d9212016-03-04 01:09:59 +01002685}
2686
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002687/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002688 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2689 * interfaces
2690 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002691 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2692 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002693 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002694 *
2695 * This is the function for internal usage in U-Boot. For the API function
2696 * implementing the InstallMultipleProtocol service see
2697 * efi_install_multiple_protocol_interfaces_ext()
2698 *
2699 * Return: status code
2700 */
2701efi_status_t EFIAPI
2702efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2703{
2704 efi_status_t ret;
2705 efi_va_list argptr;
2706
2707 efi_va_start(argptr, handle);
2708 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2709 efi_va_end(argptr);
2710 return ret;
2711}
2712
2713/**
2714 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2715 * interfaces
2716 * @handle: handle on which the protocol interfaces shall be installed
2717 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2718 * interfaces
2719 *
2720 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002721 *
Mario Six8fac2912018-07-10 08:40:17 +02002722 * See the Unified Extensible Firmware Interface (UEFI) specification for
2723 * details.
2724 *
2725 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002726 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002727static efi_status_t EFIAPI
2728efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002729{
2730 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002731 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002732 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002733
2734 efi_va_start(argptr, handle);
2735 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2736 efi_va_end(argptr);
2737 return EFI_EXIT(ret);
2738}
2739
2740/**
2741 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2742 * multiple protocol
2743 * interfaces
2744 * @handle: handle from which the protocol interfaces shall be removed
2745 * @argptr: va_list of args
2746 *
2747 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2748 * Must not be called directly
2749 *
2750 * Return: status code
2751 */
2752static efi_status_t EFIAPI
2753efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2754 efi_va_list argptr)
2755{
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002756 const efi_guid_t *protocol;
2757 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002758 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002759 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002760 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002761
2762 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002763 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002764
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002765 efi_va_copy(argptr_copy, argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002766 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002767 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002768 if (!protocol)
2769 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002770 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002771 ret = efi_uninstall_protocol(handle, protocol,
2772 protocol_interface);
2773 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002774 break;
2775 i++;
2776 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002777 if (ret == EFI_SUCCESS) {
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002778 /* If the last protocol has been removed, delete the handle. */
2779 if (list_empty(&handle->protocols)) {
2780 list_del(&handle->link);
2781 free(handle);
2782 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002783 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002784 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002785
2786 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002787 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002788 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2789 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002790 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2791 EFI_NATIVE_INTERFACE,
2792 protocol_interface));
2793 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002794 /*
2795 * If any errors are generated while the protocol interfaces are being
2796 * uninstalled, then the protocols uninstalled prior to the error will
2797 * be reinstalled using InstallProtocolInterface() and the status code
2798 * EFI_INVALID_PARAMETER is returned.
2799 */
2800 ret = EFI_INVALID_PARAMETER;
2801
2802out:
2803 efi_va_end(argptr_copy);
2804 return ret;
2805}
2806
2807/**
2808 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2809 * interfaces
2810 * @handle: handle from which the protocol interfaces shall be removed
2811 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2812 * interfaces
2813 *
2814 * This function implements the UninstallMultipleProtocolInterfaces service.
2815 *
2816 * This is the function for internal usage in U-Boot. For the API function
2817 * implementing the UninstallMultipleProtocolInterfaces service see
2818 * efi_uninstall_multiple_protocol_interfaces_ext()
2819 *
2820 * Return: status code
2821 */
2822efi_status_t EFIAPI
2823efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2824{
2825 efi_status_t ret;
2826 efi_va_list argptr;
2827
2828 efi_va_start(argptr, handle);
2829 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002830 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002831 return ret;
2832}
2833
2834/**
2835 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2836 * interfaces
2837 * @handle: handle from which the protocol interfaces shall be removed
2838 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2839 * interfaces
2840 *
2841 * This function implements the UninstallMultipleProtocolInterfaces service.
2842 *
2843 * See the Unified Extensible Firmware Interface (UEFI) specification for
2844 * details.
2845 *
2846 * Return: status code
2847 */
2848static efi_status_t EFIAPI
2849efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2850{
2851 EFI_ENTRY("%p", handle);
2852 efi_status_t ret;
2853 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002854
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002855 efi_va_start(argptr, handle);
2856 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2857 efi_va_end(argptr);
2858 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002859}
2860
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002861/**
Mario Six8fac2912018-07-10 08:40:17 +02002862 * efi_calculate_crc32() - calculate cyclic redundancy code
2863 * @data: buffer with data
2864 * @data_size: size of buffer in bytes
2865 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002866 *
2867 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002868 *
2869 * See the Unified Extensible Firmware Interface (UEFI) specification for
2870 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002871 *
Mario Six8fac2912018-07-10 08:40:17 +02002872 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002873 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002874static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2875 efi_uintn_t data_size,
2876 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002877{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002878 efi_status_t ret = EFI_SUCCESS;
2879
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002880 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002881 if (!data || !data_size || !crc32_p) {
2882 ret = EFI_INVALID_PARAMETER;
2883 goto out;
2884 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002885 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002886out:
2887 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002888}
2889
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002890/**
Mario Six8fac2912018-07-10 08:40:17 +02002891 * efi_copy_mem() - copy memory
2892 * @destination: destination of the copy operation
2893 * @source: source of the copy operation
2894 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002895 *
2896 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002897 *
Mario Six8fac2912018-07-10 08:40:17 +02002898 * See the Unified Extensible Firmware Interface (UEFI) specification for
2899 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002900 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002901static void EFIAPI efi_copy_mem(void *destination, const void *source,
2902 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002903{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002904 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002905 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002906 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002907}
2908
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002909/**
Mario Six8fac2912018-07-10 08:40:17 +02002910 * efi_set_mem() - Fill memory with a byte value.
2911 * @buffer: buffer to fill
2912 * @size: size of buffer in bytes
2913 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002914 *
2915 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002916 *
Mario Six8fac2912018-07-10 08:40:17 +02002917 * See the Unified Extensible Firmware Interface (UEFI) specification for
2918 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002919 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002920static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002921{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002922 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002923 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002924 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002925}
2926
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002927/**
Mario Six8fac2912018-07-10 08:40:17 +02002928 * efi_protocol_open() - open protocol interface on a handle
2929 * @handler: handler of a protocol
2930 * @protocol_interface: interface implementing the protocol
2931 * @agent_handle: handle of the driver
2932 * @controller_handle: handle of the controller
2933 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002934 *
Mario Six8fac2912018-07-10 08:40:17 +02002935 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002936 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002937efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002938 struct efi_handler *handler,
2939 void **protocol_interface, void *agent_handle,
2940 void *controller_handle, uint32_t attributes)
2941{
2942 struct efi_open_protocol_info_item *item;
2943 struct efi_open_protocol_info_entry *match = NULL;
2944 bool opened_by_driver = false;
2945 bool opened_exclusive = false;
2946
2947 /* If there is no agent, only return the interface */
2948 if (!agent_handle)
2949 goto out;
2950
2951 /* For TEST_PROTOCOL ignore interface attribute */
2952 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2953 *protocol_interface = NULL;
2954
2955 /*
2956 * Check if the protocol is already opened by a driver with the same
2957 * attributes or opened exclusively
2958 */
2959 list_for_each_entry(item, &handler->open_infos, link) {
2960 if (item->info.agent_handle == agent_handle) {
2961 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2962 (item->info.attributes == attributes))
2963 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002964 } else {
2965 if (item->info.attributes &
2966 EFI_OPEN_PROTOCOL_BY_DRIVER)
2967 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002968 }
2969 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2970 opened_exclusive = true;
2971 }
2972
2973 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002974 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2975 if (opened_exclusive)
2976 return EFI_ACCESS_DENIED;
2977 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2978 if (opened_exclusive || opened_by_driver)
2979 return EFI_ACCESS_DENIED;
2980 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002981
2982 /* Prepare exclusive opening */
2983 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2984 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002985disconnect_next:
2986 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002987 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002988 efi_status_t ret;
2989
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002990 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002991 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2992 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002993 item->info.controller_handle,
2994 item->info.agent_handle,
2995 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002996 if (ret == EFI_SUCCESS)
2997 /*
2998 * Child controllers may have been
2999 * removed from the open_infos list. So
3000 * let's restart the loop.
3001 */
3002 goto disconnect_next;
3003 else
3004 opened_by_driver = true;
3005 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003006 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003007 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003008 if (opened_by_driver)
3009 return EFI_ACCESS_DENIED;
3010 }
3011
3012 /* Find existing entry */
3013 list_for_each_entry(item, &handler->open_infos, link) {
3014 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003015 item->info.controller_handle == controller_handle &&
3016 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003017 match = &item->info;
3018 }
3019 /* None found, create one */
3020 if (!match) {
3021 match = efi_create_open_info(handler);
3022 if (!match)
3023 return EFI_OUT_OF_RESOURCES;
3024 }
3025
3026 match->agent_handle = agent_handle;
3027 match->controller_handle = controller_handle;
3028 match->attributes = attributes;
3029 match->open_count++;
3030
3031out:
3032 /* For TEST_PROTOCOL ignore interface attribute. */
3033 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3034 *protocol_interface = handler->protocol_interface;
3035
3036 return EFI_SUCCESS;
3037}
3038
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003039/**
Mario Six8fac2912018-07-10 08:40:17 +02003040 * efi_open_protocol() - open protocol interface on a handle
3041 * @handle: handle on which the protocol shall be opened
3042 * @protocol: GUID of the protocol
3043 * @protocol_interface: interface implementing the protocol
3044 * @agent_handle: handle of the driver
3045 * @controller_handle: handle of the controller
3046 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003047 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003048 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003049 *
Mario Six8fac2912018-07-10 08:40:17 +02003050 * See the Unified Extensible Firmware Interface (UEFI) specification for
3051 * details.
3052 *
3053 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003054 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003055static efi_status_t EFIAPI efi_open_protocol
3056 (efi_handle_t handle, const efi_guid_t *protocol,
3057 void **protocol_interface, efi_handle_t agent_handle,
3058 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003059{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003060 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003061 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003062
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003063 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003064 protocol_interface, agent_handle, controller_handle,
3065 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003066
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003067 if (!handle || !protocol ||
3068 (!protocol_interface && attributes !=
3069 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003070 goto out;
3071 }
3072
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003073 switch (attributes) {
3074 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3075 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3076 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3077 break;
3078 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3079 if (controller_handle == handle)
3080 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003081 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003082 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3083 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003084 /* Check that the controller handle is valid */
3085 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003086 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003087 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003088 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003089 /* Check that the agent handle is valid */
3090 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003091 goto out;
3092 break;
3093 default:
3094 goto out;
3095 }
3096
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003097 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003098 switch (r) {
3099 case EFI_SUCCESS:
3100 break;
3101 case EFI_NOT_FOUND:
3102 r = EFI_UNSUPPORTED;
3103 goto out;
3104 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003105 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003106 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003107
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003108 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3109 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003110out:
3111 return EFI_EXIT(r);
3112}
3113
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003114/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003115 * efi_start_image() - call the entry point of an image
3116 * @image_handle: handle of the image
3117 * @exit_data_size: size of the buffer
3118 * @exit_data: buffer to receive the exit data of the called image
3119 *
3120 * This function implements the StartImage service.
3121 *
3122 * See the Unified Extensible Firmware Interface (UEFI) specification for
3123 * details.
3124 *
3125 * Return: status code
3126 */
3127efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3128 efi_uintn_t *exit_data_size,
3129 u16 **exit_data)
3130{
3131 struct efi_loaded_image_obj *image_obj =
3132 (struct efi_loaded_image_obj *)image_handle;
3133 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003134 void *info;
3135 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003136 efi_status_t exit_status;
3137 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003138
3139 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3140
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003141 if (!efi_search_obj(image_handle))
3142 return EFI_EXIT(EFI_INVALID_PARAMETER);
3143
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003144 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003145 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3146 return EFI_EXIT(EFI_INVALID_PARAMETER);
3147
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003148 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3149 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3150
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003151 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3152 &info, NULL, NULL,
3153 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3154 if (ret != EFI_SUCCESS)
3155 return EFI_EXIT(EFI_INVALID_PARAMETER);
3156
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003157 image_obj->exit_data_size = exit_data_size;
3158 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003159 image_obj->exit_status = &exit_status;
3160 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003161
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003162 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3163 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003164 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003165 if (ret == EFI_SECURITY_VIOLATION) {
3166 /*
3167 * TCG2 Protocol is installed but no TPM device found,
3168 * this is not expected.
3169 */
3170 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003171 }
3172 }
3173 }
3174
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003175 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003176 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003177 /*
3178 * We called the entry point of the child image with EFI_CALL
3179 * in the lines below. The child image called the Exit() boot
3180 * service efi_exit() which executed the long jump that brought
3181 * us to the current line. This implies that the second half
3182 * of the EFI_CALL macro has not been executed.
3183 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003184#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003185 /*
3186 * efi_exit() called efi_restore_gd(). We have to undo this
3187 * otherwise __efi_entry_check() will put the wrong value into
3188 * app_gd.
3189 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003190 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003191#endif
3192 /*
3193 * To get ready to call EFI_EXIT below we have to execute the
3194 * missed out steps of EFI_CALL.
3195 */
3196 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003197 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003198 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003199 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003200 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003201 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003202 }
3203
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003204 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003205 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003206 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003207 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3208
3209 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003210 * Control is returned from a started UEFI image either by calling
3211 * Exit() (where exit data can be provided) or by simply returning from
3212 * the entry point. In the latter case call Exit() on behalf of the
3213 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003214 */
3215 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3216}
3217
3218/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003219 * efi_delete_image() - delete loaded image from memory)
3220 *
3221 * @image_obj: handle of the loaded image
3222 * @loaded_image_protocol: loaded image protocol
3223 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003224static efi_status_t efi_delete_image
3225 (struct efi_loaded_image_obj *image_obj,
3226 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003227{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003228 struct efi_object *efiobj;
3229 efi_status_t r, ret = EFI_SUCCESS;
3230
3231close_next:
3232 list_for_each_entry(efiobj, &efi_obj_list, link) {
3233 struct efi_handler *protocol;
3234
3235 list_for_each_entry(protocol, &efiobj->protocols, link) {
3236 struct efi_open_protocol_info_item *info;
3237
3238 list_for_each_entry(info, &protocol->open_infos, link) {
3239 if (info->info.agent_handle !=
3240 (efi_handle_t)image_obj)
3241 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003242 r = efi_close_protocol(
3243 efiobj, &protocol->guid,
3244 info->info.agent_handle,
3245 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003246 if (r != EFI_SUCCESS)
3247 ret = r;
3248 /*
3249 * Closing protocols may results in further
3250 * items being deleted. To play it safe loop
3251 * over all elements again.
3252 */
3253 goto close_next;
3254 }
3255 }
3256 }
3257
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003258 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3259 efi_size_in_pages(loaded_image_protocol->image_size));
3260 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003261
3262 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003263}
3264
3265/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003266 * efi_unload_image() - unload an EFI image
3267 * @image_handle: handle of the image to be unloaded
3268 *
3269 * This function implements the UnloadImage service.
3270 *
3271 * See the Unified Extensible Firmware Interface (UEFI) specification for
3272 * details.
3273 *
3274 * Return: status code
3275 */
3276efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3277{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003278 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003279 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003280 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003281
3282 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003283
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003284 efiobj = efi_search_obj(image_handle);
3285 if (!efiobj) {
3286 ret = EFI_INVALID_PARAMETER;
3287 goto out;
3288 }
3289 /* Find the loaded image protocol */
3290 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3291 (void **)&loaded_image_protocol,
3292 NULL, NULL,
3293 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3294 if (ret != EFI_SUCCESS) {
3295 ret = EFI_INVALID_PARAMETER;
3296 goto out;
3297 }
3298 switch (efiobj->type) {
3299 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3300 /* Call the unload function */
3301 if (!loaded_image_protocol->unload) {
3302 ret = EFI_UNSUPPORTED;
3303 goto out;
3304 }
3305 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3306 if (ret != EFI_SUCCESS)
3307 goto out;
3308 break;
3309 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3310 break;
3311 default:
3312 ret = EFI_INVALID_PARAMETER;
3313 goto out;
3314 }
3315 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3316 loaded_image_protocol);
3317out:
3318 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003319}
3320
3321/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003322 * efi_update_exit_data() - fill exit data parameters of StartImage()
3323 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003324 * @image_obj: image handle
3325 * @exit_data_size: size of the exit data buffer
3326 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003327 * Return: status code
3328 */
3329static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3330 efi_uintn_t exit_data_size,
3331 u16 *exit_data)
3332{
3333 efi_status_t ret;
3334
3335 /*
3336 * If exit_data is not provided to StartImage(), exit_data_size must be
3337 * ignored.
3338 */
3339 if (!image_obj->exit_data)
3340 return EFI_SUCCESS;
3341 if (image_obj->exit_data_size)
3342 *image_obj->exit_data_size = exit_data_size;
3343 if (exit_data_size && exit_data) {
3344 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3345 exit_data_size,
3346 (void **)image_obj->exit_data);
3347 if (ret != EFI_SUCCESS)
3348 return ret;
3349 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3350 } else {
3351 image_obj->exit_data = NULL;
3352 }
3353 return EFI_SUCCESS;
3354}
3355
3356/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003357 * efi_exit() - leave an EFI application or driver
3358 * @image_handle: handle of the application or driver that is exiting
3359 * @exit_status: status code
3360 * @exit_data_size: size of the buffer in bytes
3361 * @exit_data: buffer with data describing an error
3362 *
3363 * This function implements the Exit service.
3364 *
3365 * See the Unified Extensible Firmware Interface (UEFI) specification for
3366 * details.
3367 *
3368 * Return: status code
3369 */
3370static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3371 efi_status_t exit_status,
3372 efi_uintn_t exit_data_size,
3373 u16 *exit_data)
3374{
3375 /*
3376 * TODO: We should call the unload procedure of the loaded
3377 * image protocol.
3378 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003379 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003380 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003381 struct efi_loaded_image_obj *image_obj =
3382 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003383 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003384
3385 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3386 exit_data_size, exit_data);
3387
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003388 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003389 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003390 (void **)&loaded_image_protocol,
3391 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003392 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003393 if (ret != EFI_SUCCESS) {
3394 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003395 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003396 }
3397
3398 /* Unloading of unstarted images */
3399 switch (image_obj->header.type) {
3400 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3401 break;
3402 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3403 efi_delete_image(image_obj, loaded_image_protocol);
3404 ret = EFI_SUCCESS;
3405 goto out;
3406 default:
3407 /* Handle does not refer to loaded image */
3408 ret = EFI_INVALID_PARAMETER;
3409 goto out;
3410 }
3411 /* A started image can only be unloaded it is the last one started. */
3412 if (image_handle != current_image) {
3413 ret = EFI_INVALID_PARAMETER;
3414 goto out;
3415 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003416
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003417 /* Exit data is only foreseen in case of failure. */
3418 if (exit_status != EFI_SUCCESS) {
3419 ret = efi_update_exit_data(image_obj, exit_data_size,
3420 exit_data);
3421 /* Exiting has priority. Don't return error to caller. */
3422 if (ret != EFI_SUCCESS)
3423 EFI_PRINT("%s: out of memory\n", __func__);
3424 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003425 /* efi_delete_image() frees image_obj. Copy before the call. */
3426 exit_jmp = image_obj->exit_jmp;
3427 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003428 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3429 exit_status != EFI_SUCCESS)
3430 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003431
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003432 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3433 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3434 ret = efi_tcg2_measure_efi_app_exit();
3435 if (ret != EFI_SUCCESS) {
3436 log_warning("tcg2 measurement fails(0x%lx)\n",
3437 ret);
3438 }
3439 }
3440 }
3441
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003442 /* Make sure entry/exit counts for EFI world cross-overs match */
3443 EFI_EXIT(exit_status);
3444
3445 /*
3446 * But longjmp out with the U-Boot gd, not the application's, as
3447 * the other end is a setjmp call inside EFI context.
3448 */
3449 efi_restore_gd();
3450
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003451 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003452
3453 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003454out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003455 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003456}
3457
3458/**
Mario Six8fac2912018-07-10 08:40:17 +02003459 * efi_handle_protocol() - get interface of a protocol on a handle
3460 * @handle: handle on which the protocol shall be opened
3461 * @protocol: GUID of the protocol
3462 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003463 *
3464 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003465 *
3466 * See the Unified Extensible Firmware Interface (UEFI) specification for
3467 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003468 *
Mario Six8fac2912018-07-10 08:40:17 +02003469 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003470 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003471efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3472 const efi_guid_t *protocol,
3473 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003474{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003475 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003476 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003477}
3478
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003479/**
Mario Six8fac2912018-07-10 08:40:17 +02003480 * efi_bind_controller() - bind a single driver to a controller
3481 * @controller_handle: controller handle
3482 * @driver_image_handle: driver handle
3483 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003484 *
Mario Six8fac2912018-07-10 08:40:17 +02003485 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003486 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003487static efi_status_t efi_bind_controller(
3488 efi_handle_t controller_handle,
3489 efi_handle_t driver_image_handle,
3490 struct efi_device_path *remain_device_path)
3491{
3492 struct efi_driver_binding_protocol *binding_protocol;
3493 efi_status_t r;
3494
3495 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3496 &efi_guid_driver_binding_protocol,
3497 (void **)&binding_protocol,
3498 driver_image_handle, NULL,
3499 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3500 if (r != EFI_SUCCESS)
3501 return r;
3502 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3503 controller_handle,
3504 remain_device_path));
3505 if (r == EFI_SUCCESS)
3506 r = EFI_CALL(binding_protocol->start(binding_protocol,
3507 controller_handle,
3508 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003509 efi_close_protocol(driver_image_handle,
3510 &efi_guid_driver_binding_protocol,
3511 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003512 return r;
3513}
3514
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003515/**
Mario Six8fac2912018-07-10 08:40:17 +02003516 * efi_connect_single_controller() - connect a single driver to a controller
3517 * @controller_handle: controller
3518 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003519 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003520 *
Mario Six8fac2912018-07-10 08:40:17 +02003521 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003522 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003523static efi_status_t efi_connect_single_controller(
3524 efi_handle_t controller_handle,
3525 efi_handle_t *driver_image_handle,
3526 struct efi_device_path *remain_device_path)
3527{
3528 efi_handle_t *buffer;
3529 size_t count;
3530 size_t i;
3531 efi_status_t r;
3532 size_t connected = 0;
3533
3534 /* Get buffer with all handles with driver binding protocol */
3535 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3536 &efi_guid_driver_binding_protocol,
3537 NULL, &count, &buffer));
3538 if (r != EFI_SUCCESS)
3539 return r;
3540
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003541 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003542 if (driver_image_handle) {
3543 for (; *driver_image_handle; ++driver_image_handle) {
3544 for (i = 0; i < count; ++i) {
3545 if (buffer[i] == *driver_image_handle) {
3546 buffer[i] = NULL;
3547 r = efi_bind_controller(
3548 controller_handle,
3549 *driver_image_handle,
3550 remain_device_path);
3551 /*
3552 * For drivers that do not support the
3553 * controller or are already connected
3554 * we receive an error code here.
3555 */
3556 if (r == EFI_SUCCESS)
3557 ++connected;
3558 }
3559 }
3560 }
3561 }
3562
3563 /*
3564 * TODO: Some overrides are not yet implemented:
3565 * - Platform Driver Override
3566 * - Driver Family Override Search
3567 * - Bus Specific Driver Override
3568 */
3569
3570 /* Driver Binding Search */
3571 for (i = 0; i < count; ++i) {
3572 if (buffer[i]) {
3573 r = efi_bind_controller(controller_handle,
3574 buffer[i],
3575 remain_device_path);
3576 if (r == EFI_SUCCESS)
3577 ++connected;
3578 }
3579 }
3580
3581 efi_free_pool(buffer);
3582 if (!connected)
3583 return EFI_NOT_FOUND;
3584 return EFI_SUCCESS;
3585}
3586
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003587/**
Mario Six8fac2912018-07-10 08:40:17 +02003588 * efi_connect_controller() - connect a controller to a driver
3589 * @controller_handle: handle of the controller
3590 * @driver_image_handle: handle of the driver
3591 * @remain_device_path: device path of a child controller
3592 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003593 *
3594 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003595 *
3596 * See the Unified Extensible Firmware Interface (UEFI) specification for
3597 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003598 *
3599 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003600 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003601 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3602 *
Mario Six8fac2912018-07-10 08:40:17 +02003603 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003604 */
3605static efi_status_t EFIAPI efi_connect_controller(
3606 efi_handle_t controller_handle,
3607 efi_handle_t *driver_image_handle,
3608 struct efi_device_path *remain_device_path,
3609 bool recursive)
3610{
3611 efi_status_t r;
3612 efi_status_t ret = EFI_NOT_FOUND;
3613 struct efi_object *efiobj;
3614
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003615 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003616 remain_device_path, recursive);
3617
3618 efiobj = efi_search_obj(controller_handle);
3619 if (!efiobj) {
3620 ret = EFI_INVALID_PARAMETER;
3621 goto out;
3622 }
3623
3624 r = efi_connect_single_controller(controller_handle,
3625 driver_image_handle,
3626 remain_device_path);
3627 if (r == EFI_SUCCESS)
3628 ret = EFI_SUCCESS;
3629 if (recursive) {
3630 struct efi_handler *handler;
3631 struct efi_open_protocol_info_item *item;
3632
3633 list_for_each_entry(handler, &efiobj->protocols, link) {
3634 list_for_each_entry(item, &handler->open_infos, link) {
3635 if (item->info.attributes &
3636 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3637 r = EFI_CALL(efi_connect_controller(
3638 item->info.controller_handle,
3639 driver_image_handle,
3640 remain_device_path,
3641 recursive));
3642 if (r == EFI_SUCCESS)
3643 ret = EFI_SUCCESS;
3644 }
3645 }
3646 }
3647 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003648 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003649 if (ret != EFI_SUCCESS && remain_device_path &&
3650 remain_device_path->type == DEVICE_PATH_TYPE_END)
3651 ret = EFI_SUCCESS;
3652out:
3653 return EFI_EXIT(ret);
3654}
3655
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003656/**
Mario Six8fac2912018-07-10 08:40:17 +02003657 * efi_reinstall_protocol_interface() - reinstall protocol interface
3658 * @handle: handle on which the protocol shall be reinstalled
3659 * @protocol: GUID of the protocol to be installed
3660 * @old_interface: interface to be removed
3661 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003662 *
3663 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003664 *
3665 * See the Unified Extensible Firmware Interface (UEFI) specification for
3666 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003667 *
3668 * The old interface is uninstalled. The new interface is installed.
3669 * Drivers are connected.
3670 *
Mario Six8fac2912018-07-10 08:40:17 +02003671 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003672 */
3673static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3674 efi_handle_t handle, const efi_guid_t *protocol,
3675 void *old_interface, void *new_interface)
3676{
3677 efi_status_t ret;
3678
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003679 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003680 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003681
3682 /* Uninstall protocol but do not delete handle */
3683 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003684 if (ret != EFI_SUCCESS)
3685 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003686
3687 /* Install the new protocol */
3688 ret = efi_add_protocol(handle, protocol, new_interface);
3689 /*
3690 * The UEFI spec does not specify what should happen to the handle
3691 * if in case of an error no protocol interface remains on the handle.
3692 * So let's do nothing here.
3693 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003694 if (ret != EFI_SUCCESS)
3695 goto out;
3696 /*
3697 * The returned status code has to be ignored.
3698 * Do not create an error if no suitable driver for the handle exists.
3699 */
3700 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3701out:
3702 return EFI_EXIT(ret);
3703}
3704
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003705/**
Mario Six8fac2912018-07-10 08:40:17 +02003706 * efi_get_child_controllers() - get all child controllers associated to a driver
3707 * @efiobj: handle of the controller
3708 * @driver_handle: handle of the driver
3709 * @number_of_children: number of child controllers
3710 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003711 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003712 * The allocated buffer has to be freed with free().
3713 *
Mario Six8fac2912018-07-10 08:40:17 +02003714 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003715 */
3716static efi_status_t efi_get_child_controllers(
3717 struct efi_object *efiobj,
3718 efi_handle_t driver_handle,
3719 efi_uintn_t *number_of_children,
3720 efi_handle_t **child_handle_buffer)
3721{
3722 struct efi_handler *handler;
3723 struct efi_open_protocol_info_item *item;
3724 efi_uintn_t count = 0, i;
3725 bool duplicate;
3726
3727 /* Count all child controller associations */
3728 list_for_each_entry(handler, &efiobj->protocols, link) {
3729 list_for_each_entry(item, &handler->open_infos, link) {
3730 if (item->info.agent_handle == driver_handle &&
3731 item->info.attributes &
3732 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3733 ++count;
3734 }
3735 }
3736 /*
3737 * Create buffer. In case of duplicate child controller assignments
3738 * the buffer will be too large. But that does not harm.
3739 */
3740 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003741 if (!count)
3742 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003743 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3744 if (!*child_handle_buffer)
3745 return EFI_OUT_OF_RESOURCES;
3746 /* Copy unique child handles */
3747 list_for_each_entry(handler, &efiobj->protocols, link) {
3748 list_for_each_entry(item, &handler->open_infos, link) {
3749 if (item->info.agent_handle == driver_handle &&
3750 item->info.attributes &
3751 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3752 /* Check this is a new child controller */
3753 duplicate = false;
3754 for (i = 0; i < *number_of_children; ++i) {
3755 if ((*child_handle_buffer)[i] ==
3756 item->info.controller_handle)
3757 duplicate = true;
3758 }
3759 /* Copy handle to buffer */
3760 if (!duplicate) {
3761 i = (*number_of_children)++;
3762 (*child_handle_buffer)[i] =
3763 item->info.controller_handle;
3764 }
3765 }
3766 }
3767 }
3768 return EFI_SUCCESS;
3769}
3770
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003771/**
Mario Six8fac2912018-07-10 08:40:17 +02003772 * efi_disconnect_controller() - disconnect a controller from a driver
3773 * @controller_handle: handle of the controller
3774 * @driver_image_handle: handle of the driver
3775 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003776 *
3777 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003778 *
3779 * See the Unified Extensible Firmware Interface (UEFI) specification for
3780 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003781 *
Mario Six8fac2912018-07-10 08:40:17 +02003782 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003783 */
3784static efi_status_t EFIAPI efi_disconnect_controller(
3785 efi_handle_t controller_handle,
3786 efi_handle_t driver_image_handle,
3787 efi_handle_t child_handle)
3788{
3789 struct efi_driver_binding_protocol *binding_protocol;
3790 efi_handle_t *child_handle_buffer = NULL;
3791 size_t number_of_children = 0;
3792 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003793 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003794 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003795
3796 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3797 child_handle);
3798
3799 efiobj = efi_search_obj(controller_handle);
3800 if (!efiobj) {
3801 r = EFI_INVALID_PARAMETER;
3802 goto out;
3803 }
3804
3805 if (child_handle && !efi_search_obj(child_handle)) {
3806 r = EFI_INVALID_PARAMETER;
3807 goto out;
3808 }
3809
3810 /* If no driver handle is supplied, disconnect all drivers */
3811 if (!driver_image_handle) {
3812 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3813 goto out;
3814 }
3815
3816 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003817 r = efi_get_child_controllers(efiobj,
3818 driver_image_handle,
3819 &number_of_children,
3820 &child_handle_buffer);
3821 if (r != EFI_SUCCESS)
3822 return r;
3823 sole_child = (number_of_children == 1);
3824
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003825 if (child_handle) {
3826 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003827 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003828 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003829 }
3830
3831 /* Get the driver binding protocol */
3832 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3833 &efi_guid_driver_binding_protocol,
3834 (void **)&binding_protocol,
3835 driver_image_handle, NULL,
3836 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003837 if (r != EFI_SUCCESS) {
3838 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003839 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003840 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003841 /* Remove the children */
3842 if (number_of_children) {
3843 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3844 controller_handle,
3845 number_of_children,
3846 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003847 if (r != EFI_SUCCESS) {
3848 r = EFI_DEVICE_ERROR;
3849 goto out;
3850 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003851 }
3852 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003853 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003854 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3855 controller_handle,
3856 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003857 if (r != EFI_SUCCESS) {
3858 r = EFI_DEVICE_ERROR;
3859 goto out;
3860 }
3861 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003862 efi_close_protocol(driver_image_handle,
3863 &efi_guid_driver_binding_protocol,
3864 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003865 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003866out:
3867 if (!child_handle)
3868 free(child_handle_buffer);
3869 return EFI_EXIT(r);
3870}
3871
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003872static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003873 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003874 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3875 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003876 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003877 },
3878 .raise_tpl = efi_raise_tpl,
3879 .restore_tpl = efi_restore_tpl,
3880 .allocate_pages = efi_allocate_pages_ext,
3881 .free_pages = efi_free_pages_ext,
3882 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003883 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003884 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003885 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003886 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003887 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003888 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003889 .close_event = efi_close_event,
3890 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003891 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003892 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003893 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003894 .handle_protocol = efi_handle_protocol,
3895 .reserved = NULL,
3896 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003897 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003898 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003899 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003900 .load_image = efi_load_image,
3901 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003902 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003903 .unload_image = efi_unload_image,
3904 .exit_boot_services = efi_exit_boot_services,
3905 .get_next_monotonic_count = efi_get_next_monotonic_count,
3906 .stall = efi_stall,
3907 .set_watchdog_timer = efi_set_watchdog_timer,
3908 .connect_controller = efi_connect_controller,
3909 .disconnect_controller = efi_disconnect_controller,
3910 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003911 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003912 .open_protocol_information = efi_open_protocol_information,
3913 .protocols_per_handle = efi_protocols_per_handle,
3914 .locate_handle_buffer = efi_locate_handle_buffer,
3915 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003916 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003917 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003918 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003919 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003920 .calculate_crc32 = efi_calculate_crc32,
3921 .copy_mem = efi_copy_mem,
3922 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003923 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003924};
3925
Simon Glass90975372022-01-23 12:55:12 -07003926static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003927
Alexander Graf393dd912016-10-14 13:45:30 +02003928struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003929 .hdr = {
3930 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003931 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003932 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003933 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003934 .fw_vendor = firmware_vendor,
3935 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003936 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003937 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003938 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003939};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003940
3941/**
3942 * efi_initialize_system_table() - Initialize system table
3943 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003944 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003945 */
3946efi_status_t efi_initialize_system_table(void)
3947{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003948 efi_status_t ret;
3949
3950 /* Allocate configuration table array */
3951 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3952 EFI_MAX_CONFIGURATION_TABLES *
3953 sizeof(struct efi_configuration_table),
3954 (void **)&systab.tables);
3955
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003956 /*
3957 * These entries will be set to NULL in ExitBootServices(). To avoid
3958 * relocation in SetVirtualAddressMap(), set them dynamically.
3959 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01003960 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003961 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003962 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003963 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003964 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003965 systab.std_err = &efi_con_out;
3966 systab.boottime = &efi_boot_services;
3967
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003968 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003969 efi_update_table_header_crc32(&systab.hdr);
3970 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3971 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003972
3973 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003974}