blob: 1233418e77381b34259ac8d38697ec2ab0eb07fe [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{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200622 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100623 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200624 efi_remove_all_protocols(handle);
625 list_del(&handle->link);
626 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627}
628
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200629/**
Mario Six8fac2912018-07-10 08:40:17 +0200630 * efi_is_event() - check if a pointer is a valid event
631 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100632 *
Mario Six8fac2912018-07-10 08:40:17 +0200633 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100634 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100635static efi_status_t efi_is_event(const struct efi_event *event)
636{
637 const struct efi_event *evt;
638
639 if (!event)
640 return EFI_INVALID_PARAMETER;
641 list_for_each_entry(evt, &efi_events, link) {
642 if (evt == event)
643 return EFI_SUCCESS;
644 }
645 return EFI_INVALID_PARAMETER;
646}
Alexander Grafc15d9212016-03-04 01:09:59 +0100647
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200648/**
Mario Six8fac2912018-07-10 08:40:17 +0200649 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200650 *
Mario Six8fac2912018-07-10 08:40:17 +0200651 * @type: type of the event to create
652 * @notify_tpl: task priority level of the event
653 * @notify_function: notification function of the event
654 * @notify_context: pointer passed to the notification function
655 * @group: event group
656 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200657 *
658 * This function is used inside U-Boot code to create an event.
659 *
660 * For the API function implementing the CreateEvent service see
661 * efi_create_event_ext.
662 *
Mario Six8fac2912018-07-10 08:40:17 +0200663 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200664 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100665efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200666 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200667 struct efi_event *event,
668 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100669 void *notify_context, efi_guid_t *group,
670 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100671{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100672 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200673 efi_status_t ret;
674 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200675
Jonathan Gray7758b212017-03-12 19:26:07 +1100676 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200677 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100678
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200679 switch (type) {
680 case 0:
681 case EVT_TIMER:
682 case EVT_NOTIFY_SIGNAL:
683 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
684 case EVT_NOTIFY_WAIT:
685 case EVT_TIMER | EVT_NOTIFY_WAIT:
686 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200687 pool_type = EFI_BOOT_SERVICES_DATA;
688 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200689 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200690 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200691 break;
692 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200693 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200694 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100695
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100696 /*
697 * The UEFI specification requires event notification levels to be
698 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
699 *
700 * Parameter NotifyTpl should not be checked if it is not used.
701 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900702 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100703 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
704 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200705 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100706
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200707 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
708 (void **)&evt);
709 if (ret != EFI_SUCCESS)
710 return ret;
711 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100712 evt->type = type;
713 evt->notify_tpl = notify_tpl;
714 evt->notify_function = notify_function;
715 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100716 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200717 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100718 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100719 list_add_tail(&evt->link, &efi_events);
720 *event = evt;
721 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100722}
723
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200724/*
Mario Six8fac2912018-07-10 08:40:17 +0200725 * efi_create_event_ex() - create an event in a group
726 * @type: type of the event to create
727 * @notify_tpl: task priority level of the event
728 * @notify_function: notification function of the event
729 * @notify_context: pointer passed to the notification function
730 * @event: created event
731 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100732 *
733 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100734 *
Mario Six8fac2912018-07-10 08:40:17 +0200735 * See the Unified Extensible Firmware Interface (UEFI) specification for
736 * details.
737 *
738 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100739 */
740efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
741 void (EFIAPI *notify_function) (
742 struct efi_event *event,
743 void *context),
744 void *notify_context,
745 efi_guid_t *event_group,
746 struct efi_event **event)
747{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200748 efi_status_t ret;
749
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100750 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100751 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200752
753 /*
754 * The allowable input parameters are the same as in CreateEvent()
755 * except for the following two disallowed event types.
756 */
757 switch (type) {
758 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
759 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
760 ret = EFI_INVALID_PARAMETER;
761 goto out;
762 }
763
764 ret = efi_create_event(type, notify_tpl, notify_function,
765 notify_context, event_group, event);
766out:
767 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100768}
769
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200770/**
Mario Six8fac2912018-07-10 08:40:17 +0200771 * efi_create_event_ext() - create an event
772 * @type: type of the event to create
773 * @notify_tpl: task priority level of the event
774 * @notify_function: notification function of the event
775 * @notify_context: pointer passed to the notification function
776 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200777 *
778 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200779 *
780 * See the Unified Extensible Firmware Interface (UEFI) specification for
781 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200782 *
Mario Six8fac2912018-07-10 08:40:17 +0200783 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200784 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200785static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100786 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200787 void (EFIAPI *notify_function) (
788 struct efi_event *event,
789 void *context),
790 void *notify_context, struct efi_event **event)
791{
792 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
793 notify_context);
794 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100795 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200796}
797
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200798/**
Mario Six8fac2912018-07-10 08:40:17 +0200799 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200800 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200801 * Check if a timer event has occurred or a queued notification function should
802 * be called.
803 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100804 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200805 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100806 */
807void efi_timer_check(void)
808{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100809 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100810 u64 now = timer_get_us();
811
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100812 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200813 if (!timers_enabled)
814 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100815 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200816 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100817 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200818 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100819 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200820 break;
821 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100822 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200823 break;
824 default:
825 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200826 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100827 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200828 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100829 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200830 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100831 WATCHDOG_RESET();
832}
833
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200834/**
Mario Six8fac2912018-07-10 08:40:17 +0200835 * efi_set_timer() - set the trigger time for a timer event or stop the event
836 * @event: event for which the timer is set
837 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200838 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200839 *
840 * This is the function for internal usage in U-Boot. For the API function
841 * implementing the SetTimer service see efi_set_timer_ext.
842 *
Mario Six8fac2912018-07-10 08:40:17 +0200843 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200844 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200845efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200846 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100847{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100848 /* Check that the event is valid */
849 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
850 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100851
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200852 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200853 * The parameter defines a multiple of 100 ns.
854 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200855 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200856 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100857
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100858 switch (type) {
859 case EFI_TIMER_STOP:
860 event->trigger_next = -1ULL;
861 break;
862 case EFI_TIMER_PERIODIC:
863 case EFI_TIMER_RELATIVE:
864 event->trigger_next = timer_get_us() + trigger_time;
865 break;
866 default:
867 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100868 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100869 event->trigger_type = type;
870 event->trigger_time = trigger_time;
871 event->is_signaled = false;
872 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100873}
874
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200875/**
Mario Six8fac2912018-07-10 08:40:17 +0200876 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
877 * event
878 * @event: event for which the timer is set
879 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200880 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200881 *
882 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200883 *
Mario Six8fac2912018-07-10 08:40:17 +0200884 * See the Unified Extensible Firmware Interface (UEFI) specification for
885 * details.
886 *
887 *
888 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200889 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200890static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
891 enum efi_timer_delay type,
892 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200893{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900894 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200895 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
896}
897
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200898/**
Mario Six8fac2912018-07-10 08:40:17 +0200899 * efi_wait_for_event() - wait for events to be signaled
900 * @num_events: number of events to be waited for
901 * @event: events to be waited for
902 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200903 *
904 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200905 *
Mario Six8fac2912018-07-10 08:40:17 +0200906 * See the Unified Extensible Firmware Interface (UEFI) specification for
907 * details.
908 *
909 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100911static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200912 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100913 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100914{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100915 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100916
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100917 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100918
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 /* Check parameters */
920 if (!num_events || !event)
921 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200922 /* Check TPL */
923 if (efi_tpl != TPL_APPLICATION)
924 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200925 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100926 if (efi_is_event(event[i]) != EFI_SUCCESS)
927 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200928 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
929 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200930 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200931 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200932 }
933
934 /* Wait for signal */
935 for (;;) {
936 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200937 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200938 goto out;
939 }
940 /* Allow events to occur. */
941 efi_timer_check();
942 }
943
944out:
945 /*
946 * Reset the signal which is passed to the caller to allow periodic
947 * events to occur.
948 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200949 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200950 if (index)
951 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100952
953 return EFI_EXIT(EFI_SUCCESS);
954}
955
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200956/**
Mario Six8fac2912018-07-10 08:40:17 +0200957 * efi_signal_event_ext() - signal an EFI event
958 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200959 *
960 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200961 *
962 * See the Unified Extensible Firmware Interface (UEFI) specification for
963 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200964 *
965 * This functions sets the signaled state of the event and queues the
966 * notification function for execution.
967 *
Mario Six8fac2912018-07-10 08:40:17 +0200968 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200969 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200970static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100971{
972 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100973 if (efi_is_event(event) != EFI_SUCCESS)
974 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200975 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100976 return EFI_EXIT(EFI_SUCCESS);
977}
978
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200979/**
Mario Six8fac2912018-07-10 08:40:17 +0200980 * efi_close_event() - close an EFI event
981 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200982 *
983 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200984 *
Mario Six8fac2912018-07-10 08:40:17 +0200985 * See the Unified Extensible Firmware Interface (UEFI) specification for
986 * details.
987 *
988 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200989 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200990static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100991{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200992 struct efi_register_notify_event *item, *next;
993
Alexander Grafc15d9212016-03-04 01:09:59 +0100994 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100995 if (efi_is_event(event) != EFI_SUCCESS)
996 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200997
998 /* Remove protocol notify registrations for the event */
999 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1000 link) {
1001 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001002 struct efi_protocol_notification *hitem, *hnext;
1003
1004 /* Remove signaled handles */
1005 list_for_each_entry_safe(hitem, hnext, &item->handles,
1006 link) {
1007 list_del(&hitem->link);
1008 free(hitem);
1009 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001010 list_del(&item->link);
1011 free(item);
1012 }
1013 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001014 /* Remove event from queue */
1015 if (efi_event_is_queued(event))
1016 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001017
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001018 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001019 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001020 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001021}
1022
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001023/**
Mario Six8fac2912018-07-10 08:40:17 +02001024 * efi_check_event() - check if an event is signaled
1025 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001026 *
1027 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001028 *
1029 * See the Unified Extensible Firmware Interface (UEFI) specification for
1030 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001031 *
Mario Six8fac2912018-07-10 08:40:17 +02001032 * If an event is not signaled yet, the notification function is queued. The
1033 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001034 *
Mario Six8fac2912018-07-10 08:40:17 +02001035 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001036 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001037static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001038{
1039 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001040 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001041 if (efi_is_event(event) != EFI_SUCCESS ||
1042 event->type & EVT_NOTIFY_SIGNAL)
1043 return EFI_EXIT(EFI_INVALID_PARAMETER);
1044 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001045 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001046 if (event->is_signaled) {
1047 event->is_signaled = false;
1048 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001049 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001050 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001051}
1052
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001053/**
Mario Six8fac2912018-07-10 08:40:17 +02001054 * efi_search_obj() - find the internal EFI object for a handle
1055 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001056 *
Mario Six8fac2912018-07-10 08:40:17 +02001057 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001058 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001059struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001060{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001061 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001062
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001063 if (!handle)
1064 return NULL;
1065
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001066 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001067 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001068 return efiobj;
1069 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001070 return NULL;
1071}
1072
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001073/**
Mario Six8fac2912018-07-10 08:40:17 +02001074 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1075 * to a protocol
1076 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001077 *
Mario Six8fac2912018-07-10 08:40:17 +02001078 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001079 */
1080static struct efi_open_protocol_info_entry *efi_create_open_info(
1081 struct efi_handler *handler)
1082{
1083 struct efi_open_protocol_info_item *item;
1084
1085 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1086 if (!item)
1087 return NULL;
1088 /* Append the item to the open protocol info list. */
1089 list_add_tail(&item->link, &handler->open_infos);
1090
1091 return &item->info;
1092}
1093
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001094/**
Mario Six8fac2912018-07-10 08:40:17 +02001095 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1096 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001097 *
Mario Six8fac2912018-07-10 08:40:17 +02001098 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001099 */
1100static efi_status_t efi_delete_open_info(
1101 struct efi_open_protocol_info_item *item)
1102{
1103 list_del(&item->link);
1104 free(item);
1105 return EFI_SUCCESS;
1106}
1107
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001108/**
Mario Six8fac2912018-07-10 08:40:17 +02001109 * efi_add_protocol() - install new protocol on a handle
1110 * @handle: handle on which the protocol shall be installed
1111 * @protocol: GUID of the protocol to be installed
1112 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001113 *
Mario Six8fac2912018-07-10 08:40:17 +02001114 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001115 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001116efi_status_t efi_add_protocol(const efi_handle_t handle,
1117 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001118 void *protocol_interface)
1119{
1120 struct efi_object *efiobj;
1121 struct efi_handler *handler;
1122 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001123 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001124
1125 efiobj = efi_search_obj(handle);
1126 if (!efiobj)
1127 return EFI_INVALID_PARAMETER;
1128 ret = efi_search_protocol(handle, protocol, NULL);
1129 if (ret != EFI_NOT_FOUND)
1130 return EFI_INVALID_PARAMETER;
1131 handler = calloc(1, sizeof(struct efi_handler));
1132 if (!handler)
1133 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001134 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001135 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001136 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001137 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001138
1139 /* Notify registered events */
1140 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001141 if (!guidcmp(protocol, &event->protocol)) {
1142 struct efi_protocol_notification *notif;
1143
1144 notif = calloc(1, sizeof(*notif));
1145 if (!notif) {
1146 list_del(&handler->link);
1147 free(handler);
1148 return EFI_OUT_OF_RESOURCES;
1149 }
1150 notif->handle = handle;
1151 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001152 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001153 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001154 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001155 }
1156
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001157 if (!guidcmp(&efi_guid_device_path, protocol))
1158 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001159 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001160}
1161
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001162/**
Mario Six8fac2912018-07-10 08:40:17 +02001163 * efi_install_protocol_interface() - install protocol interface
1164 * @handle: handle on which the protocol shall be installed
1165 * @protocol: GUID of the protocol to be installed
1166 * @protocol_interface_type: type of the interface to be installed,
1167 * always EFI_NATIVE_INTERFACE
1168 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001169 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001170 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001171 *
Mario Six8fac2912018-07-10 08:40:17 +02001172 * See the Unified Extensible Firmware Interface (UEFI) specification for
1173 * details.
1174 *
1175 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001176 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001177static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001178 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001179 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001180{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001181 efi_status_t r;
1182
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001183 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001184 protocol_interface);
1185
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001186 if (!handle || !protocol ||
1187 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1188 r = EFI_INVALID_PARAMETER;
1189 goto out;
1190 }
1191
1192 /* Create new handle if requested. */
1193 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001194 r = efi_create_handle(handle);
1195 if (r != EFI_SUCCESS)
1196 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001197 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001198 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001199 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001200 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001201 /* Add new protocol */
1202 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001203out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001204 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001205}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001206
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001207/**
Mario Six8fac2912018-07-10 08:40:17 +02001208 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001209 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001210 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001211 * @number_of_drivers: number of child controllers
1212 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001213 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001214 * The allocated buffer has to be freed with free().
1215 *
Mario Six8fac2912018-07-10 08:40:17 +02001216 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001217 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001218static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001219 const efi_guid_t *protocol,
1220 efi_uintn_t *number_of_drivers,
1221 efi_handle_t **driver_handle_buffer)
1222{
1223 struct efi_handler *handler;
1224 struct efi_open_protocol_info_item *item;
1225 efi_uintn_t count = 0, i;
1226 bool duplicate;
1227
1228 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001229 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001230 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001231 continue;
1232 list_for_each_entry(item, &handler->open_infos, link) {
1233 if (item->info.attributes &
1234 EFI_OPEN_PROTOCOL_BY_DRIVER)
1235 ++count;
1236 }
1237 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001238 *number_of_drivers = 0;
1239 if (!count) {
1240 *driver_handle_buffer = NULL;
1241 return EFI_SUCCESS;
1242 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001243 /*
1244 * Create buffer. In case of duplicate driver assignments the buffer
1245 * will be too large. But that does not harm.
1246 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001247 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1248 if (!*driver_handle_buffer)
1249 return EFI_OUT_OF_RESOURCES;
1250 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001251 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001252 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001253 continue;
1254 list_for_each_entry(item, &handler->open_infos, link) {
1255 if (item->info.attributes &
1256 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1257 /* Check this is a new driver */
1258 duplicate = false;
1259 for (i = 0; i < *number_of_drivers; ++i) {
1260 if ((*driver_handle_buffer)[i] ==
1261 item->info.agent_handle)
1262 duplicate = true;
1263 }
1264 /* Copy handle to buffer */
1265 if (!duplicate) {
1266 i = (*number_of_drivers)++;
1267 (*driver_handle_buffer)[i] =
1268 item->info.agent_handle;
1269 }
1270 }
1271 }
1272 }
1273 return EFI_SUCCESS;
1274}
1275
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001276/**
Mario Six8fac2912018-07-10 08:40:17 +02001277 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001278 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001279 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001280 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001281 *
1282 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001283 *
1284 * See the Unified Extensible Firmware Interface (UEFI) specification for
1285 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001286 *
Mario Six8fac2912018-07-10 08:40:17 +02001287 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001288 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001289static efi_status_t efi_disconnect_all_drivers
1290 (efi_handle_t handle,
1291 const efi_guid_t *protocol,
1292 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001293{
1294 efi_uintn_t number_of_drivers;
1295 efi_handle_t *driver_handle_buffer;
1296 efi_status_t r, ret;
1297
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001298 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001299 &driver_handle_buffer);
1300 if (ret != EFI_SUCCESS)
1301 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001302 if (!number_of_drivers)
1303 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001304 ret = EFI_NOT_FOUND;
1305 while (number_of_drivers) {
1306 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001307 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001308 driver_handle_buffer[--number_of_drivers],
1309 child_handle));
1310 if (r == EFI_SUCCESS)
1311 ret = r;
1312 }
1313 free(driver_handle_buffer);
1314 return ret;
1315}
1316
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001317/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001318 * efi_uninstall_protocol() - uninstall protocol interface
1319 *
Mario Six8fac2912018-07-10 08:40:17 +02001320 * @handle: handle from which the protocol shall be removed
1321 * @protocol: GUID of the protocol to be removed
1322 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001323 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001324 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001325 *
1326 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001327 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001328static efi_status_t efi_uninstall_protocol
1329 (efi_handle_t handle, const efi_guid_t *protocol,
1330 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001331{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001332 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001333 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001334 struct efi_open_protocol_info_item *item;
1335 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001336 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001337
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001338 /* Check handle */
1339 efiobj = efi_search_obj(handle);
1340 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001341 r = EFI_INVALID_PARAMETER;
1342 goto out;
1343 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001344 /* Find the protocol on the handle */
1345 r = efi_search_protocol(handle, protocol, &handler);
1346 if (r != EFI_SUCCESS)
1347 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001348 /* Disconnect controllers */
1349 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001350 /* Close protocol */
1351 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1352 if (item->info.attributes ==
1353 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1354 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1355 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1356 list_del(&item->link);
1357 }
1358 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001359 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001360 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001361 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001362 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001363out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001364 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001365}
1366
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001367/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001368 * efi_uninstall_protocol_interface() - uninstall protocol interface
1369 * @handle: handle from which the protocol shall be removed
1370 * @protocol: GUID of the protocol to be removed
1371 * @protocol_interface: interface to be removed
1372 *
1373 * This function implements the UninstallProtocolInterface service.
1374 *
1375 * See the Unified Extensible Firmware Interface (UEFI) specification for
1376 * details.
1377 *
1378 * Return: status code
1379 */
1380static efi_status_t EFIAPI efi_uninstall_protocol_interface
1381 (efi_handle_t handle, const efi_guid_t *protocol,
1382 void *protocol_interface)
1383{
1384 efi_status_t ret;
1385
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001386 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001387
1388 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1389 if (ret != EFI_SUCCESS)
1390 goto out;
1391
1392 /* If the last protocol has been removed, delete the handle. */
1393 if (list_empty(&handle->protocols)) {
1394 list_del(&handle->link);
1395 free(handle);
1396 }
1397out:
1398 return EFI_EXIT(ret);
1399}
1400
1401/**
Mario Six8fac2912018-07-10 08:40:17 +02001402 * efi_register_protocol_notify() - register an event for notification when a
1403 * protocol is installed.
1404 * @protocol: GUID of the protocol whose installation shall be notified
1405 * @event: event to be signaled upon installation of the protocol
1406 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001407 *
1408 * This function implements the RegisterProtocolNotify service.
1409 * See the Unified Extensible Firmware Interface (UEFI) specification
1410 * for details.
1411 *
Mario Six8fac2912018-07-10 08:40:17 +02001412 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001413 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001414efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1415 struct efi_event *event,
1416 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001417{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001418 struct efi_register_notify_event *item;
1419 efi_status_t ret = EFI_SUCCESS;
1420
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001421 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001422
1423 if (!protocol || !event || !registration) {
1424 ret = EFI_INVALID_PARAMETER;
1425 goto out;
1426 }
1427
1428 item = calloc(1, sizeof(struct efi_register_notify_event));
1429 if (!item) {
1430 ret = EFI_OUT_OF_RESOURCES;
1431 goto out;
1432 }
1433
1434 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301435 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001436 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001437
1438 list_add_tail(&item->link, &efi_register_notify_events);
1439
1440 *registration = item;
1441out:
1442 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001443}
1444
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001445/**
Mario Six8fac2912018-07-10 08:40:17 +02001446 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001447 *
Mario Six8fac2912018-07-10 08:40:17 +02001448 * @search_type: selection criterion
1449 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001450 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001451 *
1452 * See the documentation of the LocateHandle service in the UEFI specification.
1453 *
Mario Six8fac2912018-07-10 08:40:17 +02001454 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001455 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001456static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001457 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001458{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001459 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001460
1461 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001462 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001463 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001464 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001465 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001466 return (ret != EFI_SUCCESS);
1467 default:
1468 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001469 return -1;
1470 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001471}
1472
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001473/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001474 * efi_check_register_notify_event() - check if registration key is valid
1475 *
1476 * Check that a pointer is a valid registration key as returned by
1477 * RegisterProtocolNotify().
1478 *
1479 * @key: registration key
1480 * Return: valid registration key or NULL
1481 */
1482static struct efi_register_notify_event *efi_check_register_notify_event
1483 (void *key)
1484{
1485 struct efi_register_notify_event *event;
1486
1487 list_for_each_entry(event, &efi_register_notify_events, link) {
1488 if (event == (struct efi_register_notify_event *)key)
1489 return event;
1490 }
1491 return NULL;
1492}
1493
1494/**
Mario Six8fac2912018-07-10 08:40:17 +02001495 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001496 *
1497 * @search_type: selection criterion
1498 * @protocol: GUID of the protocol
1499 * @search_key: registration key
1500 * @buffer_size: size of the buffer to receive the handles in bytes
1501 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001502 *
1503 * This function is meant for U-Boot internal calls. For the API implementation
1504 * of the LocateHandle service see efi_locate_handle_ext.
1505 *
Mario Six8fac2912018-07-10 08:40:17 +02001506 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001507 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001508static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001509 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001510 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001511 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001512{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001513 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001514 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001515 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001516 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001517
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001518 /* Check parameters */
1519 switch (search_type) {
1520 case ALL_HANDLES:
1521 break;
1522 case BY_REGISTER_NOTIFY:
1523 if (!search_key)
1524 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001525 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001526 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001527 if (!event)
1528 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001529 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001530 case BY_PROTOCOL:
1531 if (!protocol)
1532 return EFI_INVALID_PARAMETER;
1533 break;
1534 default:
1535 return EFI_INVALID_PARAMETER;
1536 }
1537
Alexander Grafc15d9212016-03-04 01:09:59 +01001538 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001539 if (search_type == BY_REGISTER_NOTIFY) {
1540 if (list_empty(&event->handles))
1541 return EFI_NOT_FOUND;
1542 handle = list_first_entry(&event->handles,
1543 struct efi_protocol_notification,
1544 link);
1545 efiobj = handle->handle;
1546 size += sizeof(void *);
1547 } else {
1548 list_for_each_entry(efiobj, &efi_obj_list, link) {
1549 if (!efi_search(search_type, protocol, efiobj))
1550 size += sizeof(void *);
1551 }
1552 if (size == 0)
1553 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001554 }
1555
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001556 if (!buffer_size)
1557 return EFI_INVALID_PARAMETER;
1558
Alexander Grafc15d9212016-03-04 01:09:59 +01001559 if (*buffer_size < size) {
1560 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001561 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001562 }
1563
Rob Clarkcdee3372017-08-06 14:10:07 -04001564 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001565
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001566 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001567 if (!buffer)
1568 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001569
Alexander Grafc15d9212016-03-04 01:09:59 +01001570 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001571 if (search_type == BY_REGISTER_NOTIFY) {
1572 *buffer = efiobj;
1573 list_del(&handle->link);
1574 } else {
1575 list_for_each_entry(efiobj, &efi_obj_list, link) {
1576 if (!efi_search(search_type, protocol, efiobj))
1577 *buffer++ = efiobj;
1578 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001579 }
1580
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001581 return EFI_SUCCESS;
1582}
1583
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001584/**
Mario Six8fac2912018-07-10 08:40:17 +02001585 * efi_locate_handle_ext() - locate handles implementing a protocol.
1586 * @search_type: selection criterion
1587 * @protocol: GUID of the protocol
1588 * @search_key: registration key
1589 * @buffer_size: size of the buffer to receive the handles in bytes
1590 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001591 *
1592 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001593 *
Mario Six8fac2912018-07-10 08:40:17 +02001594 * See the Unified Extensible Firmware Interface (UEFI) specification for
1595 * details.
1596 *
1597 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001598 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001599static efi_status_t EFIAPI efi_locate_handle_ext(
1600 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001601 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001602 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001603{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001604 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001605 buffer_size, buffer);
1606
1607 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1608 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001609}
1610
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001611/**
Mario Six8fac2912018-07-10 08:40:17 +02001612 * efi_remove_configuration_table() - collapses configuration table entries,
1613 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001614 *
Mario Six8fac2912018-07-10 08:40:17 +02001615 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001616 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001617static void efi_remove_configuration_table(int i)
1618{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001619 struct efi_configuration_table *this = &systab.tables[i];
1620 struct efi_configuration_table *next = &systab.tables[i + 1];
1621 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001622
1623 memmove(this, next, (ulong)end - (ulong)next);
1624 systab.nr_tables--;
1625}
1626
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001627/**
Mario Six8fac2912018-07-10 08:40:17 +02001628 * efi_install_configuration_table() - adds, updates, or removes a
1629 * configuration table
1630 * @guid: GUID of the installed table
1631 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001632 *
1633 * This function is used for internal calls. For the API implementation of the
1634 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1635 *
Mario Six8fac2912018-07-10 08:40:17 +02001636 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001637 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001638efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1639 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001640{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001641 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001642 int i;
1643
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001644 if (!guid)
1645 return EFI_INVALID_PARAMETER;
1646
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001647 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001648 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001649 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001650 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001651 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001652 else
1653 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001654 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001655 }
1656 }
1657
Alexander Graffe3366f2017-07-26 13:41:04 +02001658 if (!table)
1659 return EFI_NOT_FOUND;
1660
Alexander Grafc15d9212016-03-04 01:09:59 +01001661 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001662 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001663 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001664
1665 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301666 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001667 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001668 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001669
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001670out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001671 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001672 efi_update_table_header_crc32(&systab.hdr);
1673
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001674 /* Notify that the configuration table was changed */
1675 list_for_each_entry(evt, &efi_events, link) {
1676 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001677 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001678 break;
1679 }
1680 }
1681
Alexander Grafc5c11632016-08-19 01:23:24 +02001682 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001683}
1684
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001685/**
Mario Six8fac2912018-07-10 08:40:17 +02001686 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1687 * configuration table.
1688 * @guid: GUID of the installed table
1689 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001690 *
1691 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001692 *
Mario Six8fac2912018-07-10 08:40:17 +02001693 * See the Unified Extensible Firmware Interface (UEFI) specification for
1694 * details.
1695 *
1696 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001697 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001698static efi_status_t
1699EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1700 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001701{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001702 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001703 return EFI_EXIT(efi_install_configuration_table(guid, table));
1704}
1705
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001706/**
Mario Six8fac2912018-07-10 08:40:17 +02001707 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001708 *
1709 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001710 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001711 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001712 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001713 * code is returned.
1714 *
1715 * @device_path: device path of the loaded image
1716 * @file_path: file path of the loaded image
1717 * @handle_ptr: handle of the loaded image
1718 * @info_ptr: loaded image protocol
1719 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001720 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001721efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1722 struct efi_device_path *file_path,
1723 struct efi_loaded_image_obj **handle_ptr,
1724 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001725{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001726 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001727 struct efi_loaded_image *info = NULL;
1728 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001729 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001730
1731 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1732 *handle_ptr = NULL;
1733 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001734
1735 info = calloc(1, sizeof(*info));
1736 if (!info)
1737 return EFI_OUT_OF_RESOURCES;
1738 obj = calloc(1, sizeof(*obj));
1739 if (!obj) {
1740 free(info);
1741 return EFI_OUT_OF_RESOURCES;
1742 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001743 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001744
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001745 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001746 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001747
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001748 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001749 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001750 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001751
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001752 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001753 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001754
1755 dp = efi_dp_append(device_path, file_path);
1756 if (!dp) {
1757 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001758 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001759 }
1760 } else {
1761 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001762 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001763 ret = efi_add_protocol(&obj->header,
1764 &efi_guid_loaded_image_device_path, dp);
1765 if (ret != EFI_SUCCESS)
1766 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001767
1768 /*
1769 * When asking for the loaded_image interface, just
1770 * return handle which points to loaded_image_info
1771 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001772 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001773 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001774 if (ret != EFI_SUCCESS)
1775 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001776
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001777 *info_ptr = info;
1778 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001779
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001780 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001781failure:
1782 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001783 efi_delete_handle(&obj->header);
1784 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001785 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001786}
1787
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001788/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001789 * efi_locate_device_path() - Get the device path and handle of an device
1790 * implementing a protocol
1791 * @protocol: GUID of the protocol
1792 * @device_path: device path
1793 * @device: handle of the device
1794 *
1795 * This function implements the LocateDevicePath service.
1796 *
1797 * See the Unified Extensible Firmware Interface (UEFI) specification for
1798 * details.
1799 *
1800 * Return: status code
1801 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001802efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1803 struct efi_device_path **device_path,
1804 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001805{
1806 struct efi_device_path *dp;
1807 size_t i;
1808 struct efi_handler *handler;
1809 efi_handle_t *handles;
1810 size_t len, len_dp;
1811 size_t len_best = 0;
1812 efi_uintn_t no_handles;
1813 u8 *remainder;
1814 efi_status_t ret;
1815
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001816 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001817
1818 if (!protocol || !device_path || !*device_path) {
1819 ret = EFI_INVALID_PARAMETER;
1820 goto out;
1821 }
1822
1823 /* Find end of device path */
1824 len = efi_dp_instance_size(*device_path);
1825
1826 /* Get all handles implementing the protocol */
1827 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1828 &no_handles, &handles));
1829 if (ret != EFI_SUCCESS)
1830 goto out;
1831
1832 for (i = 0; i < no_handles; ++i) {
1833 /* Find the device path protocol */
1834 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1835 &handler);
1836 if (ret != EFI_SUCCESS)
1837 continue;
1838 dp = (struct efi_device_path *)handler->protocol_interface;
1839 len_dp = efi_dp_instance_size(dp);
1840 /*
1841 * This handle can only be a better fit
1842 * if its device path length is longer than the best fit and
1843 * if its device path length is shorter of equal the searched
1844 * device path.
1845 */
1846 if (len_dp <= len_best || len_dp > len)
1847 continue;
1848 /* Check if dp is a subpath of device_path */
1849 if (memcmp(*device_path, dp, len_dp))
1850 continue;
1851 if (!device) {
1852 ret = EFI_INVALID_PARAMETER;
1853 goto out;
1854 }
1855 *device = handles[i];
1856 len_best = len_dp;
1857 }
1858 if (len_best) {
1859 remainder = (u8 *)*device_path + len_best;
1860 *device_path = (struct efi_device_path *)remainder;
1861 ret = EFI_SUCCESS;
1862 } else {
1863 ret = EFI_NOT_FOUND;
1864 }
1865out:
1866 return EFI_EXIT(ret);
1867}
1868
1869/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001870 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001871 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001872 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1873 * callers obligation to update the memory type as needed.
1874 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001875 * @file_path: the path of the image to load
1876 * @buffer: buffer containing the loaded image
1877 * @size: size of the loaded image
1878 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001879 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001880static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001881efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001882 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001883{
Rob Clark857a1222017-09-13 18:05:35 -04001884 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001885 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001886 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001887 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001888
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001889 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001890 f = efi_file_from_path(file_path);
1891 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001892 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001893
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001894 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001895 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001896 goto error;
1897
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001898 /*
1899 * When reading the file we do not yet know if it contains an
1900 * application, a boottime driver, or a runtime driver. So here we
1901 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1902 * update the reservation according to the image type.
1903 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001904 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1905 EFI_BOOT_SERVICES_DATA,
1906 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001907 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001908 ret = EFI_OUT_OF_RESOURCES;
1909 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001910 }
1911
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001912 /* Read file */
1913 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1914 if (ret != EFI_SUCCESS)
1915 efi_free_pages(addr, efi_size_in_pages(bs));
1916 *buffer = (void *)(uintptr_t)addr;
1917 *size = bs;
1918error:
1919 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001920 return ret;
1921}
1922
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001923/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001924 * efi_load_image_from_path() - load an image using a file path
1925 *
1926 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1927 * callers obligation to update the memory type as needed.
1928 *
1929 * @boot_policy: true for request originating from the boot manager
1930 * @file_path: the path of the image to load
1931 * @buffer: buffer containing the loaded image
1932 * @size: size of the loaded image
1933 * Return: status code
1934 */
1935static
1936efi_status_t efi_load_image_from_path(bool boot_policy,
1937 struct efi_device_path *file_path,
1938 void **buffer, efi_uintn_t *size)
1939{
1940 efi_handle_t device;
1941 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001942 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001943 struct efi_load_file_protocol *load_file_protocol = NULL;
1944 efi_uintn_t buffer_size;
1945 uint64_t addr, pages;
1946 const efi_guid_t *guid;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001947
1948 /* In case of failure nothing is returned */
1949 *buffer = NULL;
1950 *size = 0;
1951
1952 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001953 device = efi_dp_find_obj(dp, NULL, &rem);
1954 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1955 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001956 if (ret == EFI_SUCCESS)
1957 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001958
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001959 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001960 if (ret == EFI_SUCCESS) {
1961 guid = &efi_guid_load_file_protocol;
1962 } else if (!boot_policy) {
1963 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001964 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001965 }
1966 if (ret != EFI_SUCCESS)
1967 return EFI_NOT_FOUND;
1968 ret = EFI_CALL(efi_handle_protocol(device, guid,
1969 (void **)&load_file_protocol));
1970 if (ret != EFI_SUCCESS)
1971 return EFI_NOT_FOUND;
1972 buffer_size = 0;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001973 ret = EFI_CALL(load_file_protocol->load_file(
1974 load_file_protocol, rem, boot_policy,
1975 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001976 if (ret != EFI_BUFFER_TOO_SMALL)
1977 goto out;
1978 pages = efi_size_in_pages(buffer_size);
1979 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1980 pages, &addr);
1981 if (ret != EFI_SUCCESS) {
1982 ret = EFI_OUT_OF_RESOURCES;
1983 goto out;
1984 }
1985 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001986 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001987 &buffer_size, (void *)(uintptr_t)addr));
1988 if (ret != EFI_SUCCESS)
1989 efi_free_pages(addr, pages);
1990out:
Heinrich Schuchardt20d88c72021-01-20 22:57:46 +01001991 EFI_CALL(efi_close_protocol(device, guid, efi_root, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001992 if (ret == EFI_SUCCESS) {
1993 *buffer = (void *)(uintptr_t)addr;
1994 *size = buffer_size;
1995 }
1996
1997 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001998}
1999
2000/**
Mario Six8fac2912018-07-10 08:40:17 +02002001 * efi_load_image() - load an EFI image into memory
2002 * @boot_policy: true for request originating from the boot manager
2003 * @parent_image: the caller's image handle
2004 * @file_path: the path of the image to load
2005 * @source_buffer: memory location from which the image is installed
2006 * @source_size: size of the memory area from which the image is installed
2007 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002008 *
2009 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002010 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002011 * See the Unified Extensible Firmware Interface (UEFI) specification
2012 * for details.
2013 *
Mario Six8fac2912018-07-10 08:40:17 +02002014 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002015 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002016efi_status_t EFIAPI efi_load_image(bool boot_policy,
2017 efi_handle_t parent_image,
2018 struct efi_device_path *file_path,
2019 void *source_buffer,
2020 efi_uintn_t source_size,
2021 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002022{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002023 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002024 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002025 struct efi_loaded_image_obj **image_obj =
2026 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002027 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002028 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002029
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002030 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002031 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002032
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002033 if (!image_handle || (!source_buffer && !file_path) ||
2034 !efi_search_obj(parent_image) ||
2035 /* The parent image handle must refer to a loaded image */
2036 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002037 ret = EFI_INVALID_PARAMETER;
2038 goto error;
2039 }
Rob Clark857a1222017-09-13 18:05:35 -04002040
2041 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002042 ret = efi_load_image_from_path(boot_policy, file_path,
2043 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002044 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002045 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002046 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002047 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002048 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002049 /* split file_path which contains both the device and file parts */
2050 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002051 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002052 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002053 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002054 if (!source_buffer)
2055 /* Release buffer to which file was loaded */
2056 efi_free_pages((uintptr_t)dest_buffer,
2057 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002058 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002059 info->system_table = &systab;
2060 info->parent_handle = parent_image;
2061 } else {
2062 /* The image is invalid. Release all associated resources. */
2063 efi_delete_handle(*image_handle);
2064 *image_handle = NULL;
2065 free(info);
2066 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002067error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002068 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002069}
2070
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002071/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002072 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2073 */
2074static void efi_exit_caches(void)
2075{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002076#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002077 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002078 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2079 * caches are enabled.
2080 *
2081 * TODO:
2082 * According to the UEFI spec caches that can be managed via CP15
2083 * operations should be enabled. Caches requiring platform information
2084 * to manage should be disabled. This should not happen in
2085 * ExitBootServices() but before invoking any UEFI binary is invoked.
2086 *
2087 * We want to keep the current workaround while GRUB prior to version
2088 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002089 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002090 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002091#endif
2092}
2093
2094/**
Mario Six8fac2912018-07-10 08:40:17 +02002095 * efi_exit_boot_services() - stop all boot services
2096 * @image_handle: handle of the loaded image
2097 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002098 *
2099 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002100 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002101 * See the Unified Extensible Firmware Interface (UEFI) specification
2102 * for details.
2103 *
Mario Six8fac2912018-07-10 08:40:17 +02002104 * All timer events are disabled. For exit boot services events the
2105 * notification function is called. The boot services are disabled in the
2106 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002107 *
Mario Six8fac2912018-07-10 08:40:17 +02002108 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002109 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002110static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002111 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002112{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002113 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002114 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002115
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002116 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002117
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002118 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002119 if (map_key != efi_memory_map_key) {
2120 ret = EFI_INVALID_PARAMETER;
2121 goto out;
2122 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002123
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002124 /* Check if ExitBootServices has already been called */
2125 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002126 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002127
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002128 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2129 list_for_each_entry(evt, &efi_events, link) {
2130 if (evt->group &&
2131 !guidcmp(evt->group,
2132 &efi_guid_event_group_before_exit_boot_services)) {
2133 efi_signal_event(evt);
2134 break;
2135 }
2136 }
2137
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002138 /* Stop all timer related activities */
2139 timers_enabled = false;
2140
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002141 /* Add related events to the event group */
2142 list_for_each_entry(evt, &efi_events, link) {
2143 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2144 evt->group = &efi_guid_event_group_exit_boot_services;
2145 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002146 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002147 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002148 if (evt->group &&
2149 !guidcmp(evt->group,
2150 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002151 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002152 break;
2153 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002154 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002155
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002156 /* Make sure that notification functions are not called anymore */
2157 efi_tpl = TPL_HIGH_LEVEL;
2158
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002159 /* Notify variable services */
2160 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002161
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002162 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2163 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2164 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2165 list_del(&evt->link);
2166 }
2167
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002168 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002169 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002170 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002171 udc_disconnect();
2172 board_quiesce_devices();
2173 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2174 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002175
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002176 /* Patch out unsupported runtime function */
2177 efi_runtime_detach();
2178
Alexander Graffa5246b2018-11-15 21:23:47 +01002179 /* Fix up caches for EFI payloads if necessary */
2180 efi_exit_caches();
2181
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002182 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002183 systab.con_in_handle = NULL;
2184 systab.con_in = NULL;
2185 systab.con_out_handle = NULL;
2186 systab.con_out = NULL;
2187 systab.stderr_handle = NULL;
2188 systab.std_err = NULL;
2189 systab.boottime = NULL;
2190
2191 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002192 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002193
Alexander Grafc15d9212016-03-04 01:09:59 +01002194 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002195 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002196 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002197out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002198 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2199 if (ret != EFI_SUCCESS)
2200 efi_tcg2_notify_exit_boot_services_failed();
2201 }
2202
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002203 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002204}
2205
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002206/**
Mario Six8fac2912018-07-10 08:40:17 +02002207 * efi_get_next_monotonic_count() - get next value of the counter
2208 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002209 *
2210 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002211 *
2212 * See the Unified Extensible Firmware Interface (UEFI) specification for
2213 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002214 *
Mario Six8fac2912018-07-10 08:40:17 +02002215 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002216 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002217static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2218{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002219 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002220 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002221
Alexander Grafc15d9212016-03-04 01:09:59 +01002222 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002223 if (!count) {
2224 ret = EFI_INVALID_PARAMETER;
2225 goto out;
2226 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002227 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002228 ret = EFI_SUCCESS;
2229out:
2230 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002231}
2232
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002233/**
Mario Six8fac2912018-07-10 08:40:17 +02002234 * efi_stall() - sleep
2235 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002236 *
Mario Six8fac2912018-07-10 08:40:17 +02002237 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002238 *
Mario Six8fac2912018-07-10 08:40:17 +02002239 * See the Unified Extensible Firmware Interface (UEFI) specification for
2240 * details.
2241 *
2242 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002243 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002244static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2245{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002246 u64 end_tick;
2247
Alexander Grafc15d9212016-03-04 01:09:59 +01002248 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002249
2250 end_tick = get_ticks() + usec_to_tick(microseconds);
2251 while (get_ticks() < end_tick)
2252 efi_timer_check();
2253
Alexander Grafc15d9212016-03-04 01:09:59 +01002254 return EFI_EXIT(EFI_SUCCESS);
2255}
2256
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002257/**
Mario Six8fac2912018-07-10 08:40:17 +02002258 * efi_set_watchdog_timer() - reset the watchdog timer
2259 * @timeout: seconds before reset by watchdog
2260 * @watchdog_code: code to be logged when resetting
2261 * @data_size: size of buffer in bytes
2262 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002263 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002264 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002265 *
Mario Six8fac2912018-07-10 08:40:17 +02002266 * See the Unified Extensible Firmware Interface (UEFI) specification for
2267 * details.
2268 *
2269 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002270 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002271static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2272 uint64_t watchdog_code,
2273 unsigned long data_size,
2274 uint16_t *watchdog_data)
2275{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002276 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002277 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002278 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002279}
2280
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002281/**
Mario Six8fac2912018-07-10 08:40:17 +02002282 * efi_close_protocol() - close a protocol
2283 * @handle: handle on which the protocol shall be closed
2284 * @protocol: GUID of the protocol to close
2285 * @agent_handle: handle of the driver
2286 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002287 *
2288 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002289 *
Mario Six8fac2912018-07-10 08:40:17 +02002290 * See the Unified Extensible Firmware Interface (UEFI) specification for
2291 * details.
2292 *
2293 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002294 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002295efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2296 const efi_guid_t *protocol,
2297 efi_handle_t agent_handle,
2298 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002299{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002300 struct efi_handler *handler;
2301 struct efi_open_protocol_info_item *item;
2302 struct efi_open_protocol_info_item *pos;
2303 efi_status_t r;
2304
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002305 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002306 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002307
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002308 if (!efi_search_obj(agent_handle) ||
2309 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002310 r = EFI_INVALID_PARAMETER;
2311 goto out;
2312 }
2313 r = efi_search_protocol(handle, protocol, &handler);
2314 if (r != EFI_SUCCESS)
2315 goto out;
2316
2317 r = EFI_NOT_FOUND;
2318 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2319 if (item->info.agent_handle == agent_handle &&
2320 item->info.controller_handle == controller_handle) {
2321 efi_delete_open_info(item);
2322 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002323 }
2324 }
2325out:
2326 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002327}
2328
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002329/**
Mario Six8fac2912018-07-10 08:40:17 +02002330 * efi_open_protocol_information() - provide information about then open status
2331 * of a protocol on a handle
2332 * @handle: handle for which the information shall be retrieved
2333 * @protocol: GUID of the protocol
2334 * @entry_buffer: buffer to receive the open protocol information
2335 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002336 *
2337 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002338 *
2339 * See the Unified Extensible Firmware Interface (UEFI) specification for
2340 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002341 *
Mario Six8fac2912018-07-10 08:40:17 +02002342 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002343 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002344static efi_status_t EFIAPI efi_open_protocol_information(
2345 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002346 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002347 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002348{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002349 unsigned long buffer_size;
2350 unsigned long count;
2351 struct efi_handler *handler;
2352 struct efi_open_protocol_info_item *item;
2353 efi_status_t r;
2354
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002355 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002356 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002357
2358 /* Check parameters */
2359 if (!entry_buffer) {
2360 r = EFI_INVALID_PARAMETER;
2361 goto out;
2362 }
2363 r = efi_search_protocol(handle, protocol, &handler);
2364 if (r != EFI_SUCCESS)
2365 goto out;
2366
2367 /* Count entries */
2368 count = 0;
2369 list_for_each_entry(item, &handler->open_infos, link) {
2370 if (item->info.open_count)
2371 ++count;
2372 }
2373 *entry_count = count;
2374 *entry_buffer = NULL;
2375 if (!count) {
2376 r = EFI_SUCCESS;
2377 goto out;
2378 }
2379
2380 /* Copy entries */
2381 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002382 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002383 (void **)entry_buffer);
2384 if (r != EFI_SUCCESS)
2385 goto out;
2386 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2387 if (item->info.open_count)
2388 (*entry_buffer)[--count] = item->info;
2389 }
2390out:
2391 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002392}
2393
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002394/**
Mario Six8fac2912018-07-10 08:40:17 +02002395 * efi_protocols_per_handle() - get protocols installed on a handle
2396 * @handle: handle for which the information is retrieved
2397 * @protocol_buffer: buffer with protocol GUIDs
2398 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002399 *
2400 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002401 *
Mario Six8fac2912018-07-10 08:40:17 +02002402 * See the Unified Extensible Firmware Interface (UEFI) specification for
2403 * details.
2404 *
2405 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002406 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002407static efi_status_t EFIAPI efi_protocols_per_handle(
2408 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002409 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002410{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002411 unsigned long buffer_size;
2412 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002413 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002414 efi_status_t r;
2415
Alexander Grafc15d9212016-03-04 01:09:59 +01002416 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2417 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002418
2419 if (!handle || !protocol_buffer || !protocol_buffer_count)
2420 return EFI_EXIT(EFI_INVALID_PARAMETER);
2421
2422 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002423 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002424
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002425 efiobj = efi_search_obj(handle);
2426 if (!efiobj)
2427 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002428
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002429 /* Count protocols */
2430 list_for_each(protocol_handle, &efiobj->protocols) {
2431 ++*protocol_buffer_count;
2432 }
2433
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002434 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002435 if (*protocol_buffer_count) {
2436 size_t j = 0;
2437
2438 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002439 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002440 (void **)protocol_buffer);
2441 if (r != EFI_SUCCESS)
2442 return EFI_EXIT(r);
2443 list_for_each(protocol_handle, &efiobj->protocols) {
2444 struct efi_handler *protocol;
2445
2446 protocol = list_entry(protocol_handle,
2447 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002448 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002449 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002450 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002451 }
2452
2453 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002454}
2455
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002456efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2457 const efi_guid_t *protocol, void *search_key,
2458 efi_uintn_t *no_handles, efi_handle_t **buffer)
2459{
2460 efi_status_t r;
2461 efi_uintn_t buffer_size = 0;
2462
2463 if (!no_handles || !buffer) {
2464 r = EFI_INVALID_PARAMETER;
2465 goto out;
2466 }
2467 *no_handles = 0;
2468 *buffer = NULL;
2469 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2470 *buffer);
2471 if (r != EFI_BUFFER_TOO_SMALL)
2472 goto out;
2473 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2474 (void **)buffer);
2475 if (r != EFI_SUCCESS)
2476 goto out;
2477 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2478 *buffer);
2479 if (r == EFI_SUCCESS)
2480 *no_handles = buffer_size / sizeof(efi_handle_t);
2481out:
2482 return r;
2483}
2484
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002485/**
Mario Six8fac2912018-07-10 08:40:17 +02002486 * efi_locate_handle_buffer() - locate handles implementing a protocol
2487 * @search_type: selection criterion
2488 * @protocol: GUID of the protocol
2489 * @search_key: registration key
2490 * @no_handles: number of returned handles
2491 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002492 *
2493 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002494 *
Mario Six8fac2912018-07-10 08:40:17 +02002495 * See the Unified Extensible Firmware Interface (UEFI) specification for
2496 * details.
2497 *
2498 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002499 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002500efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002501 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002502 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002503 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002504{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002505 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002506
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002507 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002508 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002509
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002510 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2511 no_handles, buffer);
2512
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002513 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002514}
2515
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002516/**
Mario Six8fac2912018-07-10 08:40:17 +02002517 * efi_locate_protocol() - find an interface implementing a protocol
2518 * @protocol: GUID of the protocol
2519 * @registration: registration key passed to the notification function
2520 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002521 *
2522 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002523 *
2524 * See the Unified Extensible Firmware Interface (UEFI) specification for
2525 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002526 *
Mario Six8fac2912018-07-10 08:40:17 +02002527 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002528 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002529static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002530 void *registration,
2531 void **protocol_interface)
2532{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002533 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002534 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002535 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002536
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002537 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002538
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002539 /*
2540 * The UEFI spec explicitly requires a protocol even if a registration
2541 * key is provided. This differs from the logic in LocateHandle().
2542 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002543 if (!protocol || !protocol_interface)
2544 return EFI_EXIT(EFI_INVALID_PARAMETER);
2545
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002546 if (registration) {
2547 struct efi_register_notify_event *event;
2548 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002549
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002550 event = efi_check_register_notify_event(registration);
2551 if (!event)
2552 return EFI_EXIT(EFI_INVALID_PARAMETER);
2553 /*
2554 * The UEFI spec requires to return EFI_NOT_FOUND if no
2555 * protocol instance matches protocol and registration.
2556 * So let's do the same for a mismatch between protocol and
2557 * registration.
2558 */
2559 if (guidcmp(&event->protocol, protocol))
2560 goto not_found;
2561 if (list_empty(&event->handles))
2562 goto not_found;
2563 handle = list_first_entry(&event->handles,
2564 struct efi_protocol_notification,
2565 link);
2566 efiobj = handle->handle;
2567 list_del(&handle->link);
2568 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002569 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002570 if (ret == EFI_SUCCESS)
2571 goto found;
2572 } else {
2573 list_for_each_entry(efiobj, &efi_obj_list, link) {
2574 ret = efi_search_protocol(efiobj, protocol, &handler);
2575 if (ret == EFI_SUCCESS)
2576 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002577 }
2578 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002579not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002580 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002581 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002582found:
2583 *protocol_interface = handler->protocol_interface;
2584 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002585}
2586
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002587/**
Mario Six8fac2912018-07-10 08:40:17 +02002588 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2589 * interfaces
2590 * @handle: handle on which the protocol interfaces shall be installed
2591 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2592 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002593 *
2594 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002595 *
2596 * See the Unified Extensible Firmware Interface (UEFI) specification for
2597 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002598 *
Mario Six8fac2912018-07-10 08:40:17 +02002599 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002600 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002601efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002602 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002603{
2604 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002605
Alexander Graf404a7c82018-06-18 17:23:05 +02002606 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002607 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002608 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002609 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002610 efi_status_t r = EFI_SUCCESS;
2611 int i = 0;
2612
2613 if (!handle)
2614 return EFI_EXIT(EFI_INVALID_PARAMETER);
2615
Alexander Graf404a7c82018-06-18 17:23:05 +02002616 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002617 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002618 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002619 if (!protocol)
2620 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002621 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002622 /* Check that a device path has not been installed before */
2623 if (!guidcmp(protocol, &efi_guid_device_path)) {
2624 struct efi_device_path *dp = protocol_interface;
2625
2626 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2627 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002628 if (r == EFI_SUCCESS &&
2629 dp->type == DEVICE_PATH_TYPE_END) {
2630 EFI_PRINT("Path %pD already installed\n",
2631 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002632 r = EFI_ALREADY_STARTED;
2633 break;
2634 }
2635 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002636 r = EFI_CALL(efi_install_protocol_interface(
2637 handle, protocol,
2638 EFI_NATIVE_INTERFACE,
2639 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002640 if (r != EFI_SUCCESS)
2641 break;
2642 i++;
2643 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002644 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002645 if (r == EFI_SUCCESS)
2646 return EFI_EXIT(r);
2647
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002648 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002649 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002650 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002651 protocol = efi_va_arg(argptr, efi_guid_t*);
2652 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002653 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002654 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002655 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002656 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002657
2658 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002659}
2660
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002661/**
Mario Six8fac2912018-07-10 08:40:17 +02002662 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2663 * interfaces
2664 * @handle: handle from which the protocol interfaces shall be removed
2665 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2666 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002667 *
2668 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002669 *
Mario Six8fac2912018-07-10 08:40:17 +02002670 * See the Unified Extensible Firmware Interface (UEFI) specification for
2671 * details.
2672 *
2673 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002674 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002675static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002676 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002677{
2678 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002679
Alexander Graf404a7c82018-06-18 17:23:05 +02002680 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002681 const efi_guid_t *protocol;
2682 void *protocol_interface;
2683 efi_status_t r = EFI_SUCCESS;
2684 size_t i = 0;
2685
2686 if (!handle)
2687 return EFI_EXIT(EFI_INVALID_PARAMETER);
2688
Alexander Graf404a7c82018-06-18 17:23:05 +02002689 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002690 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002691 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002692 if (!protocol)
2693 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002694 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002695 r = efi_uninstall_protocol(handle, protocol,
2696 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002697 if (r != EFI_SUCCESS)
2698 break;
2699 i++;
2700 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002701 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002702 if (r == EFI_SUCCESS) {
2703 /* If the last protocol has been removed, delete the handle. */
2704 if (list_empty(&handle->protocols)) {
2705 list_del(&handle->link);
2706 free(handle);
2707 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002708 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002709 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002710
2711 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002712 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002713 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002714 protocol = efi_va_arg(argptr, efi_guid_t*);
2715 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002716 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2717 EFI_NATIVE_INTERFACE,
2718 protocol_interface));
2719 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002720 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002721
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002722 /* In case of an error always return EFI_INVALID_PARAMETER */
2723 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002724}
2725
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002726/**
Mario Six8fac2912018-07-10 08:40:17 +02002727 * efi_calculate_crc32() - calculate cyclic redundancy code
2728 * @data: buffer with data
2729 * @data_size: size of buffer in bytes
2730 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002731 *
2732 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002733 *
2734 * See the Unified Extensible Firmware Interface (UEFI) specification for
2735 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002736 *
Mario Six8fac2912018-07-10 08:40:17 +02002737 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002738 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002739static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2740 efi_uintn_t data_size,
2741 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002742{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002743 efi_status_t ret = EFI_SUCCESS;
2744
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002745 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002746 if (!data || !data_size || !crc32_p) {
2747 ret = EFI_INVALID_PARAMETER;
2748 goto out;
2749 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002750 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002751out:
2752 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002753}
2754
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002755/**
Mario Six8fac2912018-07-10 08:40:17 +02002756 * efi_copy_mem() - copy memory
2757 * @destination: destination of the copy operation
2758 * @source: source of the copy operation
2759 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002760 *
2761 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002762 *
Mario Six8fac2912018-07-10 08:40:17 +02002763 * See the Unified Extensible Firmware Interface (UEFI) specification for
2764 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002765 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002766static void EFIAPI efi_copy_mem(void *destination, const void *source,
2767 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002768{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002769 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002770 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002771 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002772}
2773
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002774/**
Mario Six8fac2912018-07-10 08:40:17 +02002775 * efi_set_mem() - Fill memory with a byte value.
2776 * @buffer: buffer to fill
2777 * @size: size of buffer in bytes
2778 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002779 *
2780 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002781 *
Mario Six8fac2912018-07-10 08:40:17 +02002782 * See the Unified Extensible Firmware Interface (UEFI) specification for
2783 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002784 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002785static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002786{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002787 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002788 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002789 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002790}
2791
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002792/**
Mario Six8fac2912018-07-10 08:40:17 +02002793 * efi_protocol_open() - open protocol interface on a handle
2794 * @handler: handler of a protocol
2795 * @protocol_interface: interface implementing the protocol
2796 * @agent_handle: handle of the driver
2797 * @controller_handle: handle of the controller
2798 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002799 *
Mario Six8fac2912018-07-10 08:40:17 +02002800 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002801 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002802efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002803 struct efi_handler *handler,
2804 void **protocol_interface, void *agent_handle,
2805 void *controller_handle, uint32_t attributes)
2806{
2807 struct efi_open_protocol_info_item *item;
2808 struct efi_open_protocol_info_entry *match = NULL;
2809 bool opened_by_driver = false;
2810 bool opened_exclusive = false;
2811
2812 /* If there is no agent, only return the interface */
2813 if (!agent_handle)
2814 goto out;
2815
2816 /* For TEST_PROTOCOL ignore interface attribute */
2817 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2818 *protocol_interface = NULL;
2819
2820 /*
2821 * Check if the protocol is already opened by a driver with the same
2822 * attributes or opened exclusively
2823 */
2824 list_for_each_entry(item, &handler->open_infos, link) {
2825 if (item->info.agent_handle == agent_handle) {
2826 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2827 (item->info.attributes == attributes))
2828 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002829 } else {
2830 if (item->info.attributes &
2831 EFI_OPEN_PROTOCOL_BY_DRIVER)
2832 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002833 }
2834 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2835 opened_exclusive = true;
2836 }
2837
2838 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002839 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2840 if (opened_exclusive)
2841 return EFI_ACCESS_DENIED;
2842 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2843 if (opened_exclusive || opened_by_driver)
2844 return EFI_ACCESS_DENIED;
2845 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002846
2847 /* Prepare exclusive opening */
2848 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2849 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002850disconnect_next:
2851 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002852 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002853 efi_status_t ret;
2854
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002855 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002856 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2857 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002858 item->info.controller_handle,
2859 item->info.agent_handle,
2860 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002861 if (ret == EFI_SUCCESS)
2862 /*
2863 * Child controllers may have been
2864 * removed from the open_infos list. So
2865 * let's restart the loop.
2866 */
2867 goto disconnect_next;
2868 else
2869 opened_by_driver = true;
2870 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002871 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002872 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002873 if (opened_by_driver)
2874 return EFI_ACCESS_DENIED;
2875 }
2876
2877 /* Find existing entry */
2878 list_for_each_entry(item, &handler->open_infos, link) {
2879 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002880 item->info.controller_handle == controller_handle &&
2881 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002882 match = &item->info;
2883 }
2884 /* None found, create one */
2885 if (!match) {
2886 match = efi_create_open_info(handler);
2887 if (!match)
2888 return EFI_OUT_OF_RESOURCES;
2889 }
2890
2891 match->agent_handle = agent_handle;
2892 match->controller_handle = controller_handle;
2893 match->attributes = attributes;
2894 match->open_count++;
2895
2896out:
2897 /* For TEST_PROTOCOL ignore interface attribute. */
2898 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2899 *protocol_interface = handler->protocol_interface;
2900
2901 return EFI_SUCCESS;
2902}
2903
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002904/**
Mario Six8fac2912018-07-10 08:40:17 +02002905 * efi_open_protocol() - open protocol interface on a handle
2906 * @handle: handle on which the protocol shall be opened
2907 * @protocol: GUID of the protocol
2908 * @protocol_interface: interface implementing the protocol
2909 * @agent_handle: handle of the driver
2910 * @controller_handle: handle of the controller
2911 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002912 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002913 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002914 *
Mario Six8fac2912018-07-10 08:40:17 +02002915 * See the Unified Extensible Firmware Interface (UEFI) specification for
2916 * details.
2917 *
2918 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002919 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002920static efi_status_t EFIAPI efi_open_protocol
2921 (efi_handle_t handle, const efi_guid_t *protocol,
2922 void **protocol_interface, efi_handle_t agent_handle,
2923 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002924{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002925 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002926 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002927
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002928 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002929 protocol_interface, agent_handle, controller_handle,
2930 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002931
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002932 if (!handle || !protocol ||
2933 (!protocol_interface && attributes !=
2934 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002935 goto out;
2936 }
2937
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002938 switch (attributes) {
2939 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2940 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2941 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2942 break;
2943 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2944 if (controller_handle == handle)
2945 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002946 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002947 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2948 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002949 /* Check that the controller handle is valid */
2950 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002951 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002952 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002953 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002954 /* Check that the agent handle is valid */
2955 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002956 goto out;
2957 break;
2958 default:
2959 goto out;
2960 }
2961
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002962 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002963 switch (r) {
2964 case EFI_SUCCESS:
2965 break;
2966 case EFI_NOT_FOUND:
2967 r = EFI_UNSUPPORTED;
2968 goto out;
2969 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002970 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002971 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002972
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002973 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2974 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002975out:
2976 return EFI_EXIT(r);
2977}
2978
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002979/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002980 * efi_start_image() - call the entry point of an image
2981 * @image_handle: handle of the image
2982 * @exit_data_size: size of the buffer
2983 * @exit_data: buffer to receive the exit data of the called image
2984 *
2985 * This function implements the StartImage service.
2986 *
2987 * See the Unified Extensible Firmware Interface (UEFI) specification for
2988 * details.
2989 *
2990 * Return: status code
2991 */
2992efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2993 efi_uintn_t *exit_data_size,
2994 u16 **exit_data)
2995{
2996 struct efi_loaded_image_obj *image_obj =
2997 (struct efi_loaded_image_obj *)image_handle;
2998 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002999 void *info;
3000 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003001 efi_status_t exit_status;
3002 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003003
3004 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3005
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003006 if (!efi_search_obj(image_handle))
3007 return EFI_EXIT(EFI_INVALID_PARAMETER);
3008
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003009 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003010 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3011 return EFI_EXIT(EFI_INVALID_PARAMETER);
3012
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003013 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3014 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3015
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003016 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3017 &info, NULL, NULL,
3018 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3019 if (ret != EFI_SUCCESS)
3020 return EFI_EXIT(EFI_INVALID_PARAMETER);
3021
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003022 image_obj->exit_data_size = exit_data_size;
3023 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003024 image_obj->exit_status = &exit_status;
3025 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003026
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003027 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3028 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003029 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003030 if (ret == EFI_SECURITY_VIOLATION) {
3031 /*
3032 * TCG2 Protocol is installed but no TPM device found,
3033 * this is not expected.
3034 */
3035 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003036 }
3037 }
3038 }
3039
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003040 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003041 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003042 /*
3043 * We called the entry point of the child image with EFI_CALL
3044 * in the lines below. The child image called the Exit() boot
3045 * service efi_exit() which executed the long jump that brought
3046 * us to the current line. This implies that the second half
3047 * of the EFI_CALL macro has not been executed.
3048 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003049#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003050 /*
3051 * efi_exit() called efi_restore_gd(). We have to undo this
3052 * otherwise __efi_entry_check() will put the wrong value into
3053 * app_gd.
3054 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003055 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003056#endif
3057 /*
3058 * To get ready to call EFI_EXIT below we have to execute the
3059 * missed out steps of EFI_CALL.
3060 */
3061 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003062 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003063 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003064 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003065 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003066 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003067 }
3068
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003069 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003070 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003071 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003072 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3073
3074 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003075 * Control is returned from a started UEFI image either by calling
3076 * Exit() (where exit data can be provided) or by simply returning from
3077 * the entry point. In the latter case call Exit() on behalf of the
3078 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003079 */
3080 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3081}
3082
3083/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003084 * efi_delete_image() - delete loaded image from memory)
3085 *
3086 * @image_obj: handle of the loaded image
3087 * @loaded_image_protocol: loaded image protocol
3088 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003089static efi_status_t efi_delete_image
3090 (struct efi_loaded_image_obj *image_obj,
3091 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003092{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003093 struct efi_object *efiobj;
3094 efi_status_t r, ret = EFI_SUCCESS;
3095
3096close_next:
3097 list_for_each_entry(efiobj, &efi_obj_list, link) {
3098 struct efi_handler *protocol;
3099
3100 list_for_each_entry(protocol, &efiobj->protocols, link) {
3101 struct efi_open_protocol_info_item *info;
3102
3103 list_for_each_entry(info, &protocol->open_infos, link) {
3104 if (info->info.agent_handle !=
3105 (efi_handle_t)image_obj)
3106 continue;
3107 r = EFI_CALL(efi_close_protocol
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01003108 (efiobj, &protocol->guid,
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003109 info->info.agent_handle,
3110 info->info.controller_handle
3111 ));
3112 if (r != EFI_SUCCESS)
3113 ret = r;
3114 /*
3115 * Closing protocols may results in further
3116 * items being deleted. To play it safe loop
3117 * over all elements again.
3118 */
3119 goto close_next;
3120 }
3121 }
3122 }
3123
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003124 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3125 efi_size_in_pages(loaded_image_protocol->image_size));
3126 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003127
3128 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003129}
3130
3131/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003132 * efi_unload_image() - unload an EFI image
3133 * @image_handle: handle of the image to be unloaded
3134 *
3135 * This function implements the UnloadImage service.
3136 *
3137 * See the Unified Extensible Firmware Interface (UEFI) specification for
3138 * details.
3139 *
3140 * Return: status code
3141 */
3142efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3143{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003144 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003145 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003146 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003147
3148 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003149
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003150 efiobj = efi_search_obj(image_handle);
3151 if (!efiobj) {
3152 ret = EFI_INVALID_PARAMETER;
3153 goto out;
3154 }
3155 /* Find the loaded image protocol */
3156 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3157 (void **)&loaded_image_protocol,
3158 NULL, NULL,
3159 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3160 if (ret != EFI_SUCCESS) {
3161 ret = EFI_INVALID_PARAMETER;
3162 goto out;
3163 }
3164 switch (efiobj->type) {
3165 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3166 /* Call the unload function */
3167 if (!loaded_image_protocol->unload) {
3168 ret = EFI_UNSUPPORTED;
3169 goto out;
3170 }
3171 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3172 if (ret != EFI_SUCCESS)
3173 goto out;
3174 break;
3175 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3176 break;
3177 default:
3178 ret = EFI_INVALID_PARAMETER;
3179 goto out;
3180 }
3181 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3182 loaded_image_protocol);
3183out:
3184 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003185}
3186
3187/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003188 * efi_update_exit_data() - fill exit data parameters of StartImage()
3189 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003190 * @image_obj: image handle
3191 * @exit_data_size: size of the exit data buffer
3192 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003193 * Return: status code
3194 */
3195static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3196 efi_uintn_t exit_data_size,
3197 u16 *exit_data)
3198{
3199 efi_status_t ret;
3200
3201 /*
3202 * If exit_data is not provided to StartImage(), exit_data_size must be
3203 * ignored.
3204 */
3205 if (!image_obj->exit_data)
3206 return EFI_SUCCESS;
3207 if (image_obj->exit_data_size)
3208 *image_obj->exit_data_size = exit_data_size;
3209 if (exit_data_size && exit_data) {
3210 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3211 exit_data_size,
3212 (void **)image_obj->exit_data);
3213 if (ret != EFI_SUCCESS)
3214 return ret;
3215 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3216 } else {
3217 image_obj->exit_data = NULL;
3218 }
3219 return EFI_SUCCESS;
3220}
3221
3222/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003223 * efi_exit() - leave an EFI application or driver
3224 * @image_handle: handle of the application or driver that is exiting
3225 * @exit_status: status code
3226 * @exit_data_size: size of the buffer in bytes
3227 * @exit_data: buffer with data describing an error
3228 *
3229 * This function implements the Exit service.
3230 *
3231 * See the Unified Extensible Firmware Interface (UEFI) specification for
3232 * details.
3233 *
3234 * Return: status code
3235 */
3236static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3237 efi_status_t exit_status,
3238 efi_uintn_t exit_data_size,
3239 u16 *exit_data)
3240{
3241 /*
3242 * TODO: We should call the unload procedure of the loaded
3243 * image protocol.
3244 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003245 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003246 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003247 struct efi_loaded_image_obj *image_obj =
3248 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003249 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003250
3251 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3252 exit_data_size, exit_data);
3253
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003254 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003255 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003256 (void **)&loaded_image_protocol,
3257 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003258 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003259 if (ret != EFI_SUCCESS) {
3260 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003261 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003262 }
3263
3264 /* Unloading of unstarted images */
3265 switch (image_obj->header.type) {
3266 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3267 break;
3268 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3269 efi_delete_image(image_obj, loaded_image_protocol);
3270 ret = EFI_SUCCESS;
3271 goto out;
3272 default:
3273 /* Handle does not refer to loaded image */
3274 ret = EFI_INVALID_PARAMETER;
3275 goto out;
3276 }
3277 /* A started image can only be unloaded it is the last one started. */
3278 if (image_handle != current_image) {
3279 ret = EFI_INVALID_PARAMETER;
3280 goto out;
3281 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003282
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003283 /* Exit data is only foreseen in case of failure. */
3284 if (exit_status != EFI_SUCCESS) {
3285 ret = efi_update_exit_data(image_obj, exit_data_size,
3286 exit_data);
3287 /* Exiting has priority. Don't return error to caller. */
3288 if (ret != EFI_SUCCESS)
3289 EFI_PRINT("%s: out of memory\n", __func__);
3290 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003291 /* efi_delete_image() frees image_obj. Copy before the call. */
3292 exit_jmp = image_obj->exit_jmp;
3293 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003294 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3295 exit_status != EFI_SUCCESS)
3296 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003297
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003298 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3299 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3300 ret = efi_tcg2_measure_efi_app_exit();
3301 if (ret != EFI_SUCCESS) {
3302 log_warning("tcg2 measurement fails(0x%lx)\n",
3303 ret);
3304 }
3305 }
3306 }
3307
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003308 /* Make sure entry/exit counts for EFI world cross-overs match */
3309 EFI_EXIT(exit_status);
3310
3311 /*
3312 * But longjmp out with the U-Boot gd, not the application's, as
3313 * the other end is a setjmp call inside EFI context.
3314 */
3315 efi_restore_gd();
3316
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003317 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003318
3319 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003320out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003321 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003322}
3323
3324/**
Mario Six8fac2912018-07-10 08:40:17 +02003325 * efi_handle_protocol() - get interface of a protocol on a handle
3326 * @handle: handle on which the protocol shall be opened
3327 * @protocol: GUID of the protocol
3328 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003329 *
3330 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003331 *
3332 * See the Unified Extensible Firmware Interface (UEFI) specification for
3333 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003334 *
Mario Six8fac2912018-07-10 08:40:17 +02003335 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003336 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003337efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3338 const efi_guid_t *protocol,
3339 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003340{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003341 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003342 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003343}
3344
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003345/**
Mario Six8fac2912018-07-10 08:40:17 +02003346 * efi_bind_controller() - bind a single driver to a controller
3347 * @controller_handle: controller handle
3348 * @driver_image_handle: driver handle
3349 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003350 *
Mario Six8fac2912018-07-10 08:40:17 +02003351 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003352 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003353static efi_status_t efi_bind_controller(
3354 efi_handle_t controller_handle,
3355 efi_handle_t driver_image_handle,
3356 struct efi_device_path *remain_device_path)
3357{
3358 struct efi_driver_binding_protocol *binding_protocol;
3359 efi_status_t r;
3360
3361 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3362 &efi_guid_driver_binding_protocol,
3363 (void **)&binding_protocol,
3364 driver_image_handle, NULL,
3365 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3366 if (r != EFI_SUCCESS)
3367 return r;
3368 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3369 controller_handle,
3370 remain_device_path));
3371 if (r == EFI_SUCCESS)
3372 r = EFI_CALL(binding_protocol->start(binding_protocol,
3373 controller_handle,
3374 remain_device_path));
3375 EFI_CALL(efi_close_protocol(driver_image_handle,
3376 &efi_guid_driver_binding_protocol,
3377 driver_image_handle, NULL));
3378 return r;
3379}
3380
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003381/**
Mario Six8fac2912018-07-10 08:40:17 +02003382 * efi_connect_single_controller() - connect a single driver to a controller
3383 * @controller_handle: controller
3384 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003385 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003386 *
Mario Six8fac2912018-07-10 08:40:17 +02003387 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003388 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003389static efi_status_t efi_connect_single_controller(
3390 efi_handle_t controller_handle,
3391 efi_handle_t *driver_image_handle,
3392 struct efi_device_path *remain_device_path)
3393{
3394 efi_handle_t *buffer;
3395 size_t count;
3396 size_t i;
3397 efi_status_t r;
3398 size_t connected = 0;
3399
3400 /* Get buffer with all handles with driver binding protocol */
3401 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3402 &efi_guid_driver_binding_protocol,
3403 NULL, &count, &buffer));
3404 if (r != EFI_SUCCESS)
3405 return r;
3406
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003407 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003408 if (driver_image_handle) {
3409 for (; *driver_image_handle; ++driver_image_handle) {
3410 for (i = 0; i < count; ++i) {
3411 if (buffer[i] == *driver_image_handle) {
3412 buffer[i] = NULL;
3413 r = efi_bind_controller(
3414 controller_handle,
3415 *driver_image_handle,
3416 remain_device_path);
3417 /*
3418 * For drivers that do not support the
3419 * controller or are already connected
3420 * we receive an error code here.
3421 */
3422 if (r == EFI_SUCCESS)
3423 ++connected;
3424 }
3425 }
3426 }
3427 }
3428
3429 /*
3430 * TODO: Some overrides are not yet implemented:
3431 * - Platform Driver Override
3432 * - Driver Family Override Search
3433 * - Bus Specific Driver Override
3434 */
3435
3436 /* Driver Binding Search */
3437 for (i = 0; i < count; ++i) {
3438 if (buffer[i]) {
3439 r = efi_bind_controller(controller_handle,
3440 buffer[i],
3441 remain_device_path);
3442 if (r == EFI_SUCCESS)
3443 ++connected;
3444 }
3445 }
3446
3447 efi_free_pool(buffer);
3448 if (!connected)
3449 return EFI_NOT_FOUND;
3450 return EFI_SUCCESS;
3451}
3452
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003453/**
Mario Six8fac2912018-07-10 08:40:17 +02003454 * efi_connect_controller() - connect a controller to a driver
3455 * @controller_handle: handle of the controller
3456 * @driver_image_handle: handle of the driver
3457 * @remain_device_path: device path of a child controller
3458 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003459 *
3460 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003461 *
3462 * See the Unified Extensible Firmware Interface (UEFI) specification for
3463 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003464 *
3465 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003466 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003467 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3468 *
Mario Six8fac2912018-07-10 08:40:17 +02003469 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003470 */
3471static efi_status_t EFIAPI efi_connect_controller(
3472 efi_handle_t controller_handle,
3473 efi_handle_t *driver_image_handle,
3474 struct efi_device_path *remain_device_path,
3475 bool recursive)
3476{
3477 efi_status_t r;
3478 efi_status_t ret = EFI_NOT_FOUND;
3479 struct efi_object *efiobj;
3480
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003481 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003482 remain_device_path, recursive);
3483
3484 efiobj = efi_search_obj(controller_handle);
3485 if (!efiobj) {
3486 ret = EFI_INVALID_PARAMETER;
3487 goto out;
3488 }
3489
3490 r = efi_connect_single_controller(controller_handle,
3491 driver_image_handle,
3492 remain_device_path);
3493 if (r == EFI_SUCCESS)
3494 ret = EFI_SUCCESS;
3495 if (recursive) {
3496 struct efi_handler *handler;
3497 struct efi_open_protocol_info_item *item;
3498
3499 list_for_each_entry(handler, &efiobj->protocols, link) {
3500 list_for_each_entry(item, &handler->open_infos, link) {
3501 if (item->info.attributes &
3502 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3503 r = EFI_CALL(efi_connect_controller(
3504 item->info.controller_handle,
3505 driver_image_handle,
3506 remain_device_path,
3507 recursive));
3508 if (r == EFI_SUCCESS)
3509 ret = EFI_SUCCESS;
3510 }
3511 }
3512 }
3513 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003514 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003515 if (ret != EFI_SUCCESS && remain_device_path &&
3516 remain_device_path->type == DEVICE_PATH_TYPE_END)
3517 ret = EFI_SUCCESS;
3518out:
3519 return EFI_EXIT(ret);
3520}
3521
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003522/**
Mario Six8fac2912018-07-10 08:40:17 +02003523 * efi_reinstall_protocol_interface() - reinstall protocol interface
3524 * @handle: handle on which the protocol shall be reinstalled
3525 * @protocol: GUID of the protocol to be installed
3526 * @old_interface: interface to be removed
3527 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003528 *
3529 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003530 *
3531 * See the Unified Extensible Firmware Interface (UEFI) specification for
3532 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003533 *
3534 * The old interface is uninstalled. The new interface is installed.
3535 * Drivers are connected.
3536 *
Mario Six8fac2912018-07-10 08:40:17 +02003537 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003538 */
3539static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3540 efi_handle_t handle, const efi_guid_t *protocol,
3541 void *old_interface, void *new_interface)
3542{
3543 efi_status_t ret;
3544
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003545 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003546 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003547
3548 /* Uninstall protocol but do not delete handle */
3549 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003550 if (ret != EFI_SUCCESS)
3551 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003552
3553 /* Install the new protocol */
3554 ret = efi_add_protocol(handle, protocol, new_interface);
3555 /*
3556 * The UEFI spec does not specify what should happen to the handle
3557 * if in case of an error no protocol interface remains on the handle.
3558 * So let's do nothing here.
3559 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003560 if (ret != EFI_SUCCESS)
3561 goto out;
3562 /*
3563 * The returned status code has to be ignored.
3564 * Do not create an error if no suitable driver for the handle exists.
3565 */
3566 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3567out:
3568 return EFI_EXIT(ret);
3569}
3570
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003571/**
Mario Six8fac2912018-07-10 08:40:17 +02003572 * efi_get_child_controllers() - get all child controllers associated to a driver
3573 * @efiobj: handle of the controller
3574 * @driver_handle: handle of the driver
3575 * @number_of_children: number of child controllers
3576 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003577 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003578 * The allocated buffer has to be freed with free().
3579 *
Mario Six8fac2912018-07-10 08:40:17 +02003580 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003581 */
3582static efi_status_t efi_get_child_controllers(
3583 struct efi_object *efiobj,
3584 efi_handle_t driver_handle,
3585 efi_uintn_t *number_of_children,
3586 efi_handle_t **child_handle_buffer)
3587{
3588 struct efi_handler *handler;
3589 struct efi_open_protocol_info_item *item;
3590 efi_uintn_t count = 0, i;
3591 bool duplicate;
3592
3593 /* Count all child controller associations */
3594 list_for_each_entry(handler, &efiobj->protocols, link) {
3595 list_for_each_entry(item, &handler->open_infos, link) {
3596 if (item->info.agent_handle == driver_handle &&
3597 item->info.attributes &
3598 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3599 ++count;
3600 }
3601 }
3602 /*
3603 * Create buffer. In case of duplicate child controller assignments
3604 * the buffer will be too large. But that does not harm.
3605 */
3606 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003607 if (!count)
3608 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003609 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3610 if (!*child_handle_buffer)
3611 return EFI_OUT_OF_RESOURCES;
3612 /* Copy unique child handles */
3613 list_for_each_entry(handler, &efiobj->protocols, link) {
3614 list_for_each_entry(item, &handler->open_infos, link) {
3615 if (item->info.agent_handle == driver_handle &&
3616 item->info.attributes &
3617 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3618 /* Check this is a new child controller */
3619 duplicate = false;
3620 for (i = 0; i < *number_of_children; ++i) {
3621 if ((*child_handle_buffer)[i] ==
3622 item->info.controller_handle)
3623 duplicate = true;
3624 }
3625 /* Copy handle to buffer */
3626 if (!duplicate) {
3627 i = (*number_of_children)++;
3628 (*child_handle_buffer)[i] =
3629 item->info.controller_handle;
3630 }
3631 }
3632 }
3633 }
3634 return EFI_SUCCESS;
3635}
3636
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003637/**
Mario Six8fac2912018-07-10 08:40:17 +02003638 * efi_disconnect_controller() - disconnect a controller from a driver
3639 * @controller_handle: handle of the controller
3640 * @driver_image_handle: handle of the driver
3641 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003642 *
3643 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003644 *
3645 * See the Unified Extensible Firmware Interface (UEFI) specification for
3646 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003647 *
Mario Six8fac2912018-07-10 08:40:17 +02003648 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003649 */
3650static efi_status_t EFIAPI efi_disconnect_controller(
3651 efi_handle_t controller_handle,
3652 efi_handle_t driver_image_handle,
3653 efi_handle_t child_handle)
3654{
3655 struct efi_driver_binding_protocol *binding_protocol;
3656 efi_handle_t *child_handle_buffer = NULL;
3657 size_t number_of_children = 0;
3658 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003659 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003660 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003661
3662 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3663 child_handle);
3664
3665 efiobj = efi_search_obj(controller_handle);
3666 if (!efiobj) {
3667 r = EFI_INVALID_PARAMETER;
3668 goto out;
3669 }
3670
3671 if (child_handle && !efi_search_obj(child_handle)) {
3672 r = EFI_INVALID_PARAMETER;
3673 goto out;
3674 }
3675
3676 /* If no driver handle is supplied, disconnect all drivers */
3677 if (!driver_image_handle) {
3678 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3679 goto out;
3680 }
3681
3682 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003683 r = efi_get_child_controllers(efiobj,
3684 driver_image_handle,
3685 &number_of_children,
3686 &child_handle_buffer);
3687 if (r != EFI_SUCCESS)
3688 return r;
3689 sole_child = (number_of_children == 1);
3690
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003691 if (child_handle) {
3692 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003693 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003694 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003695 }
3696
3697 /* Get the driver binding protocol */
3698 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3699 &efi_guid_driver_binding_protocol,
3700 (void **)&binding_protocol,
3701 driver_image_handle, NULL,
3702 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003703 if (r != EFI_SUCCESS) {
3704 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003705 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003706 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003707 /* Remove the children */
3708 if (number_of_children) {
3709 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3710 controller_handle,
3711 number_of_children,
3712 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003713 if (r != EFI_SUCCESS) {
3714 r = EFI_DEVICE_ERROR;
3715 goto out;
3716 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003717 }
3718 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003719 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003720 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3721 controller_handle,
3722 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003723 if (r != EFI_SUCCESS) {
3724 r = EFI_DEVICE_ERROR;
3725 goto out;
3726 }
3727 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003728 EFI_CALL(efi_close_protocol(driver_image_handle,
3729 &efi_guid_driver_binding_protocol,
3730 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003731 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003732out:
3733 if (!child_handle)
3734 free(child_handle_buffer);
3735 return EFI_EXIT(r);
3736}
3737
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003738static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003739 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003740 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3741 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003742 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003743 },
3744 .raise_tpl = efi_raise_tpl,
3745 .restore_tpl = efi_restore_tpl,
3746 .allocate_pages = efi_allocate_pages_ext,
3747 .free_pages = efi_free_pages_ext,
3748 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003749 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003750 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003751 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003752 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003753 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003754 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003755 .close_event = efi_close_event,
3756 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003757 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003758 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003759 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003760 .handle_protocol = efi_handle_protocol,
3761 .reserved = NULL,
3762 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003763 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003764 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003765 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003766 .load_image = efi_load_image,
3767 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003768 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003769 .unload_image = efi_unload_image,
3770 .exit_boot_services = efi_exit_boot_services,
3771 .get_next_monotonic_count = efi_get_next_monotonic_count,
3772 .stall = efi_stall,
3773 .set_watchdog_timer = efi_set_watchdog_timer,
3774 .connect_controller = efi_connect_controller,
3775 .disconnect_controller = efi_disconnect_controller,
3776 .open_protocol = efi_open_protocol,
3777 .close_protocol = efi_close_protocol,
3778 .open_protocol_information = efi_open_protocol_information,
3779 .protocols_per_handle = efi_protocols_per_handle,
3780 .locate_handle_buffer = efi_locate_handle_buffer,
3781 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003782 .install_multiple_protocol_interfaces =
3783 efi_install_multiple_protocol_interfaces,
3784 .uninstall_multiple_protocol_interfaces =
3785 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003786 .calculate_crc32 = efi_calculate_crc32,
3787 .copy_mem = efi_copy_mem,
3788 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003789 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003790};
3791
Simon Glass90975372022-01-23 12:55:12 -07003792static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003793
Alexander Graf393dd912016-10-14 13:45:30 +02003794struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003795 .hdr = {
3796 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003797 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003798 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003799 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003800 .fw_vendor = firmware_vendor,
3801 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003802 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003803 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003804 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003805};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003806
3807/**
3808 * efi_initialize_system_table() - Initialize system table
3809 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003810 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003811 */
3812efi_status_t efi_initialize_system_table(void)
3813{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003814 efi_status_t ret;
3815
3816 /* Allocate configuration table array */
3817 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3818 EFI_MAX_CONFIGURATION_TABLES *
3819 sizeof(struct efi_configuration_table),
3820 (void **)&systab.tables);
3821
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003822 /*
3823 * These entries will be set to NULL in ExitBootServices(). To avoid
3824 * relocation in SetVirtualAddressMap(), set them dynamically.
3825 */
3826 systab.con_in = &efi_con_in;
3827 systab.con_out = &efi_con_out;
3828 systab.std_err = &efi_con_out;
3829 systab.boottime = &efi_boot_services;
3830
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003831 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003832 efi_update_table_header_crc32(&systab.hdr);
3833 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3834 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003835
3836 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003837}