blob: 2ca7359f3e16df86caee19b5ff4954f98539e91a [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro4cbba2b2021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020030
Alexander Grafc15d9212016-03-04 01:09:59 +010031/* This list contains all the EFI objects our payload has access to */
32LIST_HEAD(efi_obj_list);
33
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010036
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020037/* List of queued events */
Bin Mengd89dea32023-04-05 20:15:17 +080038static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020039
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
Bin Mengd89dea32023-04-05 20:15:17 +080047static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020048
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010053/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010055 * fixed when compiling U-Boot. However, the payload does not know about that
56 * restriction so we need to manually swap its and our view of that register on
57 * EFI callback entry/exit.
58 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060060#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010061
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
Ilias Apalodimas1e10d622023-06-20 09:19:28 +0300100static
101efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
102 efi_handle_t *driver_image_handle,
103 struct efi_device_path *remain_device_path,
104 bool recursive);
105
Rob Clark86789d52017-07-27 08:04:18 -0400106/* Called on every callback entry */
107int __efi_entry_check(void)
108{
109 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200110#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400111 assert(efi_gd);
112 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200113 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400114#endif
115 return ret;
116}
117
118/* Called on every callback exit */
119int __efi_exit_check(void)
120{
121 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200122#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200123 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400124#endif
125 return ret;
126}
127
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200128/**
129 * efi_save_gd() - save global data register
130 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200131 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200132 * As this register may be overwritten by an EFI payload we save it here
133 * and restore it on every callback entered.
134 *
135 * This function is called after relocation from initr_reloc_global_data().
136 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100137void efi_save_gd(void)
138{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200139#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100140 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600141#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100142}
143
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200144/**
145 * efi_restore_gd() - restore global data register
146 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200147 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200148 * Restore it after returning from the UEFI world to the value saved via
149 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400150 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100151void efi_restore_gd(void)
152{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200153#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100154 /* Only restore if we're already in EFI context */
155 if (!efi_gd)
156 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200157 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600158#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100159}
160
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200161/**
Mario Six8fac2912018-07-10 08:40:17 +0200162 * indent_string() - returns a string for indenting with two spaces per level
163 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100164 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200165 * A maximum of ten indent levels is supported. Higher indent levels will be
166 * truncated.
167 *
Mario Six8fac2912018-07-10 08:40:17 +0200168 * Return: A string for indenting with two spaces per level is
169 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400170 */
171static const char *indent_string(int level)
172{
173 const char *indent = " ";
174 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100175
Rob Clarke7896c32017-07-27 08:04:19 -0400176 level = min(max, level * 2);
177 return &indent[max - level];
178}
179
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200180const char *__efi_nesting(void)
181{
182 return indent_string(nesting_level);
183}
184
Rob Clarke7896c32017-07-27 08:04:19 -0400185const char *__efi_nesting_inc(void)
186{
187 return indent_string(nesting_level++);
188}
189
190const char *__efi_nesting_dec(void)
191{
192 return indent_string(--nesting_level);
193}
194
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200195/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200196 * efi_event_is_queued() - check if an event is queued
197 *
198 * @event: event
199 * Return: true if event is queued
200 */
201static bool efi_event_is_queued(struct efi_event *event)
202{
203 return !!event->queue_link.next;
204}
205
206/**
207 * efi_process_event_queue() - process event queue
208 */
209static void efi_process_event_queue(void)
210{
211 while (!list_empty(&efi_event_queue)) {
212 struct efi_event *event;
213 efi_uintn_t old_tpl;
214
215 event = list_first_entry(&efi_event_queue, struct efi_event,
216 queue_link);
217 if (efi_tpl >= event->notify_tpl)
218 return;
219 list_del(&event->queue_link);
220 event->queue_link.next = NULL;
221 event->queue_link.prev = NULL;
222 /* Events must be executed at the event's TPL */
223 old_tpl = efi_tpl;
224 efi_tpl = event->notify_tpl;
225 EFI_CALL_VOID(event->notify_function(event,
226 event->notify_context));
227 efi_tpl = old_tpl;
228 if (event->type == EVT_NOTIFY_SIGNAL)
229 event->is_signaled = 0;
230 }
231}
232
233/**
Mario Six8fac2912018-07-10 08:40:17 +0200234 * efi_queue_event() - queue an EFI event
235 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200236 *
237 * This function queues the notification function of the event for future
238 * execution.
239 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200240 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200241static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200242{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100243 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200244
245 if (!event->notify_function)
246 return;
247
248 if (!efi_event_is_queued(event)) {
249 /*
250 * Events must be notified in order of decreasing task priority
251 * level. Insert the new event accordingly.
252 */
253 list_for_each_entry(item, &efi_event_queue, queue_link) {
254 if (item->notify_tpl < event->notify_tpl) {
255 list_add_tail(&event->queue_link,
256 &item->queue_link);
257 event = NULL;
258 break;
259 }
260 }
261 if (event)
262 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100263 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200264 }
265}
266
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200267/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200268 * is_valid_tpl() - check if the task priority level is valid
269 *
270 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200271 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200272 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100273static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200274{
275 switch (tpl) {
276 case TPL_APPLICATION:
277 case TPL_CALLBACK:
278 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200279 return EFI_SUCCESS;
280 default:
281 return EFI_INVALID_PARAMETER;
282 }
283}
284
285/**
Mario Six8fac2912018-07-10 08:40:17 +0200286 * efi_signal_event() - signal an EFI event
287 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100288 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100289 * This function signals an event. If the event belongs to an event group, all
290 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100291 * their notification function is queued.
292 *
293 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100294 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200295void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100296{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200297 if (event->is_signaled)
298 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100299 if (event->group) {
300 struct efi_event *evt;
301
302 /*
303 * The signaled state has to set before executing any
304 * notification function
305 */
306 list_for_each_entry(evt, &efi_events, link) {
307 if (!evt->group || guidcmp(evt->group, event->group))
308 continue;
309 if (evt->is_signaled)
310 continue;
311 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100312 }
313 list_for_each_entry(evt, &efi_events, link) {
314 if (!evt->group || guidcmp(evt->group, event->group))
315 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200316 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100317 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200318 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100319 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200320 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100321 }
322}
323
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200324/**
Mario Six8fac2912018-07-10 08:40:17 +0200325 * efi_raise_tpl() - raise the task priority level
326 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200327 *
328 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200329 *
330 * See the Unified Extensible Firmware Interface (UEFI) specification for
331 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200332 *
Mario Six8fac2912018-07-10 08:40:17 +0200333 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200334 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100335static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100336{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100337 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200338
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200339 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200340
341 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200342 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200343 efi_tpl = new_tpl;
344 if (efi_tpl > TPL_HIGH_LEVEL)
345 efi_tpl = TPL_HIGH_LEVEL;
346
347 EFI_EXIT(EFI_SUCCESS);
348 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100349}
350
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200351/**
Mario Six8fac2912018-07-10 08:40:17 +0200352 * efi_restore_tpl() - lower the task priority level
353 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200354 *
355 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200356 *
Mario Six8fac2912018-07-10 08:40:17 +0200357 * See the Unified Extensible Firmware Interface (UEFI) specification for
358 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200359 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100360static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100361{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200362 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200363
364 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200365 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200366 efi_tpl = old_tpl;
367 if (efi_tpl > TPL_HIGH_LEVEL)
368 efi_tpl = TPL_HIGH_LEVEL;
369
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100370 /*
371 * Lowering the TPL may have made queued events eligible for execution.
372 */
373 efi_timer_check();
374
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200375 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100376}
377
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200378/**
Mario Six8fac2912018-07-10 08:40:17 +0200379 * efi_allocate_pages_ext() - allocate memory pages
380 * @type: type of allocation to be performed
381 * @memory_type: usage type of the allocated memory
382 * @pages: number of pages to be allocated
383 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200384 *
385 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200386 *
Mario Six8fac2912018-07-10 08:40:17 +0200387 * See the Unified Extensible Firmware Interface (UEFI) specification for
388 * details.
389 *
390 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900392static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100393 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900394 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100395{
396 efi_status_t r;
397
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100398 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100399 r = efi_allocate_pages(type, memory_type, pages, memory);
400 return EFI_EXIT(r);
401}
402
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200403/**
Mario Six8fac2912018-07-10 08:40:17 +0200404 * efi_free_pages_ext() - Free memory pages.
405 * @memory: start of the memory area to be freed
406 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200407 *
408 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200409 *
410 * See the Unified Extensible Firmware Interface (UEFI) specification for
411 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200412 *
Mario Six8fac2912018-07-10 08:40:17 +0200413 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200414 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900415static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100416 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100417{
418 efi_status_t r;
419
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900420 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100421 r = efi_free_pages(memory, pages);
422 return EFI_EXIT(r);
423}
424
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200425/**
Mario Six8fac2912018-07-10 08:40:17 +0200426 * efi_get_memory_map_ext() - get map describing memory usage
427 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
428 * on exit the size of the copied memory map
429 * @memory_map: buffer to which the memory map is written
430 * @map_key: key for the memory map
431 * @descriptor_size: size of an individual memory descriptor
432 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200433 *
434 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200435 *
Mario Six8fac2912018-07-10 08:40:17 +0200436 * See the Unified Extensible Firmware Interface (UEFI) specification for
437 * details.
438 *
439 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200440 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900441static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100442 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900443 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100444 efi_uintn_t *map_key,
445 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900446 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100447{
448 efi_status_t r;
449
450 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
451 map_key, descriptor_size, descriptor_version);
452 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
453 descriptor_size, descriptor_version);
454 return EFI_EXIT(r);
455}
456
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200457/**
Mario Six8fac2912018-07-10 08:40:17 +0200458 * efi_allocate_pool_ext() - allocate memory from pool
459 * @pool_type: type of the pool from which memory is to be allocated
460 * @size: number of bytes to be allocated
461 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200462 *
463 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200464 *
465 * See the Unified Extensible Firmware Interface (UEFI) specification for
466 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200467 *
Mario Six8fac2912018-07-10 08:40:17 +0200468 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200469 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200470static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100471 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200472 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100473{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100474 efi_status_t r;
475
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100476 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200477 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100478 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100479}
480
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200481/**
Mario Six8fac2912018-07-10 08:40:17 +0200482 * efi_free_pool_ext() - free memory from pool
483 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200484 *
485 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200486 *
Mario Six8fac2912018-07-10 08:40:17 +0200487 * See the Unified Extensible Firmware Interface (UEFI) specification for
488 * details.
489 *
490 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200491 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200492static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100493{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100494 efi_status_t r;
495
496 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200497 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100498 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100499}
500
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200501/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200502 * efi_add_handle() - add a new handle to the object list
503 *
504 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100505 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200506 * The protocols list is initialized. The handle is added to the list of known
507 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100508 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200509void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100510{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200511 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100512 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200513 INIT_LIST_HEAD(&handle->protocols);
514 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100515}
516
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200517/**
Mario Six8fac2912018-07-10 08:40:17 +0200518 * efi_create_handle() - create handle
519 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200520 *
Mario Six8fac2912018-07-10 08:40:17 +0200521 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200522 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100523efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200524{
525 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200526
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200527 obj = calloc(1, sizeof(struct efi_object));
528 if (!obj)
529 return EFI_OUT_OF_RESOURCES;
530
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100531 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200532 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200533
534 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200535}
536
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200537/**
Mario Six8fac2912018-07-10 08:40:17 +0200538 * efi_search_protocol() - find a protocol on a handle.
539 * @handle: handle
540 * @protocol_guid: GUID of the protocol
541 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100542 *
Mario Six8fac2912018-07-10 08:40:17 +0200543 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100544 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100545efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 const efi_guid_t *protocol_guid,
547 struct efi_handler **handler)
548{
549 struct efi_object *efiobj;
550 struct list_head *lhandle;
551
552 if (!handle || !protocol_guid)
553 return EFI_INVALID_PARAMETER;
554 efiobj = efi_search_obj(handle);
555 if (!efiobj)
556 return EFI_INVALID_PARAMETER;
557 list_for_each(lhandle, &efiobj->protocols) {
558 struct efi_handler *protocol;
559
560 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100561 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100562 if (handler)
563 *handler = protocol;
564 return EFI_SUCCESS;
565 }
566 }
567 return EFI_NOT_FOUND;
568}
569
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200570/**
Mario Six8fac2912018-07-10 08:40:17 +0200571 * efi_remove_protocol() - delete protocol from a handle
572 * @handle: handle from which the protocol shall be deleted
573 * @protocol: GUID of the protocol to be deleted
574 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100575 *
Mario Six8fac2912018-07-10 08:40:17 +0200576 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100577 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100578efi_status_t efi_remove_protocol(const efi_handle_t handle,
579 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100580 void *protocol_interface)
581{
582 struct efi_handler *handler;
583 efi_status_t ret;
584
585 ret = efi_search_protocol(handle, protocol, &handler);
586 if (ret != EFI_SUCCESS)
587 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200588 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200589 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100590 list_del(&handler->link);
591 free(handler);
592 return EFI_SUCCESS;
593}
594
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200595/**
Mario Six8fac2912018-07-10 08:40:17 +0200596 * efi_remove_all_protocols() - delete all protocols from a handle
597 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100598 *
Mario Six8fac2912018-07-10 08:40:17 +0200599 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100601static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100602{
603 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100604 struct efi_handler *protocol;
605 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100606
607 efiobj = efi_search_obj(handle);
608 if (!efiobj)
609 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100610 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100611 efi_status_t ret;
612
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100613 ret = efi_remove_protocol(handle, &protocol->guid,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100614 protocol->protocol_interface);
615 if (ret != EFI_SUCCESS)
616 return ret;
617 }
618 return EFI_SUCCESS;
619}
620
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200621/**
Mario Six8fac2912018-07-10 08:40:17 +0200622 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100623 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200624 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100625 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200626void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200628 efi_status_t ret;
629
630 ret = efi_remove_all_protocols(handle);
631 if (ret == EFI_INVALID_PARAMETER) {
632 log_err("Can't remove invalid handle %p\n", handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100633 return;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200634 }
635
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200636 list_del(&handle->link);
637 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100638}
639
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200640/**
Mario Six8fac2912018-07-10 08:40:17 +0200641 * efi_is_event() - check if a pointer is a valid event
642 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100643 *
Mario Six8fac2912018-07-10 08:40:17 +0200644 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100645 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100646static efi_status_t efi_is_event(const struct efi_event *event)
647{
648 const struct efi_event *evt;
649
650 if (!event)
651 return EFI_INVALID_PARAMETER;
652 list_for_each_entry(evt, &efi_events, link) {
653 if (evt == event)
654 return EFI_SUCCESS;
655 }
656 return EFI_INVALID_PARAMETER;
657}
Alexander Grafc15d9212016-03-04 01:09:59 +0100658
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200659/**
Mario Six8fac2912018-07-10 08:40:17 +0200660 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200661 *
Mario Six8fac2912018-07-10 08:40:17 +0200662 * @type: type of the event to create
663 * @notify_tpl: task priority level of the event
664 * @notify_function: notification function of the event
665 * @notify_context: pointer passed to the notification function
666 * @group: event group
667 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200668 *
669 * This function is used inside U-Boot code to create an event.
670 *
671 * For the API function implementing the CreateEvent service see
672 * efi_create_event_ext.
673 *
Mario Six8fac2912018-07-10 08:40:17 +0200674 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200675 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100676efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200677 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200678 struct efi_event *event,
679 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100680 void *notify_context, efi_guid_t *group,
681 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100682{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100683 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200684 efi_status_t ret;
685 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200686
Jonathan Gray7758b212017-03-12 19:26:07 +1100687 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200688 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100689
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200690 switch (type) {
691 case 0:
692 case EVT_TIMER:
693 case EVT_NOTIFY_SIGNAL:
694 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
695 case EVT_NOTIFY_WAIT:
696 case EVT_TIMER | EVT_NOTIFY_WAIT:
697 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200698 pool_type = EFI_BOOT_SERVICES_DATA;
699 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200700 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200701 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200702 break;
703 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200704 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200705 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100706
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100707 /*
708 * The UEFI specification requires event notification levels to be
709 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
710 *
711 * Parameter NotifyTpl should not be checked if it is not used.
712 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900713 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100714 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
715 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200716 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100717
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200718 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
719 (void **)&evt);
720 if (ret != EFI_SUCCESS)
721 return ret;
722 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100723 evt->type = type;
724 evt->notify_tpl = notify_tpl;
725 evt->notify_function = notify_function;
726 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100727 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200728 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100729 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100730 list_add_tail(&evt->link, &efi_events);
731 *event = evt;
732 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100733}
734
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200735/*
Mario Six8fac2912018-07-10 08:40:17 +0200736 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100737 *
Mario Six8fac2912018-07-10 08:40:17 +0200738 * @type: type of the event to create
739 * @notify_tpl: task priority level of the event
740 * @notify_function: notification function of the event
741 * @notify_context: pointer passed to the notification function
742 * @event: created event
743 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100744 *
745 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100746 *
Mario Six8fac2912018-07-10 08:40:17 +0200747 * See the Unified Extensible Firmware Interface (UEFI) specification for
748 * details.
749 *
750 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100751 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100752static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100753efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
754 void (EFIAPI *notify_function) (
755 struct efi_event *event,
756 void *context),
757 void *notify_context,
758 efi_guid_t *event_group,
759 struct efi_event **event)
760{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200761 efi_status_t ret;
762
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100763 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100764 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200765
766 /*
767 * The allowable input parameters are the same as in CreateEvent()
768 * except for the following two disallowed event types.
769 */
770 switch (type) {
771 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
772 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
773 ret = EFI_INVALID_PARAMETER;
774 goto out;
775 }
776
777 ret = efi_create_event(type, notify_tpl, notify_function,
778 notify_context, event_group, event);
779out:
780 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100781}
782
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200783/**
Mario Six8fac2912018-07-10 08:40:17 +0200784 * efi_create_event_ext() - create an event
785 * @type: type of the event to create
786 * @notify_tpl: task priority level of the event
787 * @notify_function: notification function of the event
788 * @notify_context: pointer passed to the notification function
789 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200790 *
791 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200792 *
793 * See the Unified Extensible Firmware Interface (UEFI) specification for
794 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200795 *
Mario Six8fac2912018-07-10 08:40:17 +0200796 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200797 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200798static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100799 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200800 void (EFIAPI *notify_function) (
801 struct efi_event *event,
802 void *context),
803 void *notify_context, struct efi_event **event)
804{
805 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
806 notify_context);
807 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100808 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200809}
810
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200811/**
Mario Six8fac2912018-07-10 08:40:17 +0200812 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200813 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200814 * Check if a timer event has occurred or a queued notification function should
815 * be called.
816 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100817 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200818 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100819 */
820void efi_timer_check(void)
821{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100822 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100823 u64 now = timer_get_us();
824
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100825 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200826 if (!timers_enabled)
827 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100828 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200829 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100830 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200831 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100832 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200833 break;
834 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100835 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200836 break;
837 default:
838 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200839 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100840 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200841 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100842 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200843 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200844 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100845}
846
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200847/**
Mario Six8fac2912018-07-10 08:40:17 +0200848 * efi_set_timer() - set the trigger time for a timer event or stop the event
849 * @event: event for which the timer is set
850 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200851 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200852 *
853 * This is the function for internal usage in U-Boot. For the API function
854 * implementing the SetTimer service see efi_set_timer_ext.
855 *
Mario Six8fac2912018-07-10 08:40:17 +0200856 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200857 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200858efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200859 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100860{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100861 /* Check that the event is valid */
862 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
863 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100864
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200865 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200866 * The parameter defines a multiple of 100 ns.
867 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200868 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200869 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100870
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100871 switch (type) {
872 case EFI_TIMER_STOP:
873 event->trigger_next = -1ULL;
874 break;
875 case EFI_TIMER_PERIODIC:
876 case EFI_TIMER_RELATIVE:
877 event->trigger_next = timer_get_us() + trigger_time;
878 break;
879 default:
880 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100881 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100882 event->trigger_type = type;
883 event->trigger_time = trigger_time;
884 event->is_signaled = false;
885 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100886}
887
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200888/**
Mario Six8fac2912018-07-10 08:40:17 +0200889 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
890 * event
891 * @event: event for which the timer is set
892 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200893 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200894 *
895 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200896 *
Mario Six8fac2912018-07-10 08:40:17 +0200897 * See the Unified Extensible Firmware Interface (UEFI) specification for
898 * details.
899 *
900 *
901 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200902 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200903static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
904 enum efi_timer_delay type,
905 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200906{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900907 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200908 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
909}
910
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200911/**
Mario Six8fac2912018-07-10 08:40:17 +0200912 * efi_wait_for_event() - wait for events to be signaled
913 * @num_events: number of events to be waited for
914 * @event: events to be waited for
915 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200916 *
917 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200918 *
Mario Six8fac2912018-07-10 08:40:17 +0200919 * See the Unified Extensible Firmware Interface (UEFI) specification for
920 * details.
921 *
922 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200923 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100924static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200925 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100926 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100927{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100928 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100929
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100930 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100931
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200932 /* Check parameters */
933 if (!num_events || !event)
934 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200935 /* Check TPL */
936 if (efi_tpl != TPL_APPLICATION)
937 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200938 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100939 if (efi_is_event(event[i]) != EFI_SUCCESS)
940 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200941 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
942 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200943 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200944 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200945 }
946
947 /* Wait for signal */
948 for (;;) {
949 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200950 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200951 goto out;
952 }
953 /* Allow events to occur. */
954 efi_timer_check();
955 }
956
957out:
958 /*
959 * Reset the signal which is passed to the caller to allow periodic
960 * events to occur.
961 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200962 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200963 if (index)
964 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100965
966 return EFI_EXIT(EFI_SUCCESS);
967}
968
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200969/**
Mario Six8fac2912018-07-10 08:40:17 +0200970 * efi_signal_event_ext() - signal an EFI event
971 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200972 *
973 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200974 *
975 * See the Unified Extensible Firmware Interface (UEFI) specification for
976 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200977 *
978 * This functions sets the signaled state of the event and queues the
979 * notification function for execution.
980 *
Mario Six8fac2912018-07-10 08:40:17 +0200981 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200982 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200983static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100984{
985 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100986 if (efi_is_event(event) != EFI_SUCCESS)
987 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200988 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100989 return EFI_EXIT(EFI_SUCCESS);
990}
991
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200992/**
Mario Six8fac2912018-07-10 08:40:17 +0200993 * efi_close_event() - close an EFI event
994 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200995 *
996 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200997 *
Mario Six8fac2912018-07-10 08:40:17 +0200998 * See the Unified Extensible Firmware Interface (UEFI) specification for
999 * details.
1000 *
1001 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001002 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001003static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001004{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001005 struct efi_register_notify_event *item, *next;
1006
Alexander Grafc15d9212016-03-04 01:09:59 +01001007 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001008 if (efi_is_event(event) != EFI_SUCCESS)
1009 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001010
1011 /* Remove protocol notify registrations for the event */
1012 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1013 link) {
1014 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001015 struct efi_protocol_notification *hitem, *hnext;
1016
1017 /* Remove signaled handles */
1018 list_for_each_entry_safe(hitem, hnext, &item->handles,
1019 link) {
1020 list_del(&hitem->link);
1021 free(hitem);
1022 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001023 list_del(&item->link);
1024 free(item);
1025 }
1026 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001027 /* Remove event from queue */
1028 if (efi_event_is_queued(event))
1029 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001030
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001031 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001032 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001033 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001034}
1035
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001036/**
Mario Six8fac2912018-07-10 08:40:17 +02001037 * efi_check_event() - check if an event is signaled
1038 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001039 *
1040 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001041 *
1042 * See the Unified Extensible Firmware Interface (UEFI) specification for
1043 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001044 *
Mario Six8fac2912018-07-10 08:40:17 +02001045 * If an event is not signaled yet, the notification function is queued. The
1046 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001047 *
Mario Six8fac2912018-07-10 08:40:17 +02001048 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001049 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001050static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001051{
1052 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001053 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001054 if (efi_is_event(event) != EFI_SUCCESS ||
1055 event->type & EVT_NOTIFY_SIGNAL)
1056 return EFI_EXIT(EFI_INVALID_PARAMETER);
1057 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001058 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001059 if (event->is_signaled) {
1060 event->is_signaled = false;
1061 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001062 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001063 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001064}
1065
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001066/**
Mario Six8fac2912018-07-10 08:40:17 +02001067 * efi_search_obj() - find the internal EFI object for a handle
1068 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001069 *
Mario Six8fac2912018-07-10 08:40:17 +02001070 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001071 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001072struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001073{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001074 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001075
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001076 if (!handle)
1077 return NULL;
1078
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001079 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001080 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001081 return efiobj;
1082 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001083 return NULL;
1084}
1085
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001086/**
Mario Six8fac2912018-07-10 08:40:17 +02001087 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1088 * to a protocol
1089 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001090 *
Mario Six8fac2912018-07-10 08:40:17 +02001091 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001092 */
1093static struct efi_open_protocol_info_entry *efi_create_open_info(
1094 struct efi_handler *handler)
1095{
1096 struct efi_open_protocol_info_item *item;
1097
1098 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1099 if (!item)
1100 return NULL;
1101 /* Append the item to the open protocol info list. */
1102 list_add_tail(&item->link, &handler->open_infos);
1103
1104 return &item->info;
1105}
1106
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001107/**
Mario Six8fac2912018-07-10 08:40:17 +02001108 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1109 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001110 *
Mario Six8fac2912018-07-10 08:40:17 +02001111 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001112 */
1113static efi_status_t efi_delete_open_info(
1114 struct efi_open_protocol_info_item *item)
1115{
1116 list_del(&item->link);
1117 free(item);
1118 return EFI_SUCCESS;
1119}
1120
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001121/**
Mario Six8fac2912018-07-10 08:40:17 +02001122 * efi_add_protocol() - install new protocol on a handle
1123 * @handle: handle on which the protocol shall be installed
1124 * @protocol: GUID of the protocol to be installed
1125 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001126 *
Mario Six8fac2912018-07-10 08:40:17 +02001127 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001128 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001129efi_status_t efi_add_protocol(const efi_handle_t handle,
1130 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001131 void *protocol_interface)
1132{
1133 struct efi_object *efiobj;
1134 struct efi_handler *handler;
1135 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001136 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001137
1138 efiobj = efi_search_obj(handle);
1139 if (!efiobj)
1140 return EFI_INVALID_PARAMETER;
1141 ret = efi_search_protocol(handle, protocol, NULL);
1142 if (ret != EFI_NOT_FOUND)
1143 return EFI_INVALID_PARAMETER;
1144 handler = calloc(1, sizeof(struct efi_handler));
1145 if (!handler)
1146 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001147 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001148 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001149 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001150 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001151
1152 /* Notify registered events */
1153 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001154 if (!guidcmp(protocol, &event->protocol)) {
1155 struct efi_protocol_notification *notif;
1156
1157 notif = calloc(1, sizeof(*notif));
1158 if (!notif) {
1159 list_del(&handler->link);
1160 free(handler);
1161 return EFI_OUT_OF_RESOURCES;
1162 }
1163 notif->handle = handle;
1164 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001165 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001166 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001167 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001168 }
1169
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001170 if (!guidcmp(&efi_guid_device_path, protocol))
1171 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001172 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001173}
1174
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001175/**
Mario Six8fac2912018-07-10 08:40:17 +02001176 * efi_install_protocol_interface() - install protocol interface
1177 * @handle: handle on which the protocol shall be installed
1178 * @protocol: GUID of the protocol to be installed
1179 * @protocol_interface_type: type of the interface to be installed,
1180 * always EFI_NATIVE_INTERFACE
1181 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001182 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001183 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001184 *
Mario Six8fac2912018-07-10 08:40:17 +02001185 * See the Unified Extensible Firmware Interface (UEFI) specification for
1186 * details.
1187 *
1188 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001189 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001190static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001191 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001192 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001193{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001194 efi_status_t r;
1195
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001196 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001197 protocol_interface);
1198
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001199 if (!handle || !protocol ||
1200 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1201 r = EFI_INVALID_PARAMETER;
1202 goto out;
1203 }
1204
1205 /* Create new handle if requested. */
1206 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001207 r = efi_create_handle(handle);
1208 if (r != EFI_SUCCESS)
1209 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001210 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001211 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001212 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001213 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001214 /* Add new protocol */
1215 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001216out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001217 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001218}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001219
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001220/**
Mario Six8fac2912018-07-10 08:40:17 +02001221 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001222 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001223 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001224 * @number_of_drivers: number of child controllers
1225 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001226 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001227 * The allocated buffer has to be freed with free().
1228 *
Mario Six8fac2912018-07-10 08:40:17 +02001229 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001230 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001231static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001232 const efi_guid_t *protocol,
1233 efi_uintn_t *number_of_drivers,
1234 efi_handle_t **driver_handle_buffer)
1235{
1236 struct efi_handler *handler;
1237 struct efi_open_protocol_info_item *item;
1238 efi_uintn_t count = 0, i;
1239 bool duplicate;
1240
1241 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001242 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001243 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001244 continue;
1245 list_for_each_entry(item, &handler->open_infos, link) {
1246 if (item->info.attributes &
1247 EFI_OPEN_PROTOCOL_BY_DRIVER)
1248 ++count;
1249 }
1250 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001251 *number_of_drivers = 0;
1252 if (!count) {
1253 *driver_handle_buffer = NULL;
1254 return EFI_SUCCESS;
1255 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001256 /*
1257 * Create buffer. In case of duplicate driver assignments the buffer
1258 * will be too large. But that does not harm.
1259 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001260 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1261 if (!*driver_handle_buffer)
1262 return EFI_OUT_OF_RESOURCES;
1263 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001264 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001265 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001266 continue;
1267 list_for_each_entry(item, &handler->open_infos, link) {
1268 if (item->info.attributes &
1269 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1270 /* Check this is a new driver */
1271 duplicate = false;
1272 for (i = 0; i < *number_of_drivers; ++i) {
1273 if ((*driver_handle_buffer)[i] ==
1274 item->info.agent_handle)
1275 duplicate = true;
1276 }
1277 /* Copy handle to buffer */
1278 if (!duplicate) {
1279 i = (*number_of_drivers)++;
1280 (*driver_handle_buffer)[i] =
1281 item->info.agent_handle;
1282 }
1283 }
1284 }
1285 }
1286 return EFI_SUCCESS;
1287}
1288
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001289/**
Mario Six8fac2912018-07-10 08:40:17 +02001290 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001291 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001292 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001293 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001294 *
1295 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001296 *
1297 * See the Unified Extensible Firmware Interface (UEFI) specification for
1298 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001299 *
Mario Six8fac2912018-07-10 08:40:17 +02001300 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001301 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001302static efi_status_t efi_disconnect_all_drivers
1303 (efi_handle_t handle,
1304 const efi_guid_t *protocol,
1305 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001306{
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001307 efi_uintn_t number_of_drivers, tmp;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001308 efi_handle_t *driver_handle_buffer;
1309 efi_status_t r, ret;
1310
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001311 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001312 &driver_handle_buffer);
1313 if (ret != EFI_SUCCESS)
1314 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001315 if (!number_of_drivers)
1316 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001317
1318 tmp = number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001319 while (number_of_drivers) {
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001320 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001321 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001322 driver_handle_buffer[--number_of_drivers],
1323 child_handle));
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001324 if (ret != EFI_SUCCESS)
1325 goto reconnect;
1326 }
1327
1328 free(driver_handle_buffer);
1329 return ret;
1330
1331reconnect:
1332 /* Reconnect all disconnected drivers */
1333 for (; number_of_drivers < tmp; number_of_drivers++) {
1334 r = EFI_CALL(efi_connect_controller(handle,
1335 &driver_handle_buffer[number_of_drivers],
1336 NULL, true));
1337 if (r != EFI_SUCCESS)
1338 EFI_PRINT("Failed to reconnect controller\n");
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001339 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001340
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001341 free(driver_handle_buffer);
1342 return ret;
1343}
1344
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001345/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001346 * efi_uninstall_protocol() - uninstall protocol interface
1347 *
Mario Six8fac2912018-07-10 08:40:17 +02001348 * @handle: handle from which the protocol shall be removed
1349 * @protocol: GUID of the protocol to be removed
1350 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001351 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001352 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001353 *
1354 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001355 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001356static efi_status_t efi_uninstall_protocol
1357 (efi_handle_t handle, const efi_guid_t *protocol,
1358 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001359{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001360 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001361 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001362 struct efi_open_protocol_info_item *item;
1363 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001364 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001365
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001366 /* Check handle */
1367 efiobj = efi_search_obj(handle);
1368 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001369 r = EFI_INVALID_PARAMETER;
1370 goto out;
1371 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001372 /* Find the protocol on the handle */
1373 r = efi_search_protocol(handle, protocol, &handler);
1374 if (r != EFI_SUCCESS)
1375 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001376 if (handler->protocol_interface != protocol_interface)
1377 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001378 /* Disconnect controllers */
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001379 r = efi_disconnect_all_drivers(efiobj, protocol, NULL);
1380 if (r != EFI_SUCCESS) {
1381 r = EFI_ACCESS_DENIED;
1382 goto out;
1383 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001384 /* Close protocol */
1385 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1386 if (item->info.attributes ==
1387 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1388 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1389 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001390 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001391 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001392 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001393 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001394 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001395 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001396 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001397 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001398 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001399out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001400 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001401}
1402
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001403/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001404 * efi_uninstall_protocol_interface() - uninstall protocol interface
1405 * @handle: handle from which the protocol shall be removed
1406 * @protocol: GUID of the protocol to be removed
1407 * @protocol_interface: interface to be removed
1408 *
1409 * This function implements the UninstallProtocolInterface service.
1410 *
1411 * See the Unified Extensible Firmware Interface (UEFI) specification for
1412 * details.
1413 *
1414 * Return: status code
1415 */
1416static efi_status_t EFIAPI efi_uninstall_protocol_interface
1417 (efi_handle_t handle, const efi_guid_t *protocol,
1418 void *protocol_interface)
1419{
1420 efi_status_t ret;
1421
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001422 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001423
1424 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1425 if (ret != EFI_SUCCESS)
1426 goto out;
1427
1428 /* If the last protocol has been removed, delete the handle. */
1429 if (list_empty(&handle->protocols)) {
1430 list_del(&handle->link);
1431 free(handle);
1432 }
1433out:
1434 return EFI_EXIT(ret);
1435}
1436
1437/**
Mario Six8fac2912018-07-10 08:40:17 +02001438 * efi_register_protocol_notify() - register an event for notification when a
1439 * protocol is installed.
1440 * @protocol: GUID of the protocol whose installation shall be notified
1441 * @event: event to be signaled upon installation of the protocol
1442 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001443 *
1444 * This function implements the RegisterProtocolNotify service.
1445 * See the Unified Extensible Firmware Interface (UEFI) specification
1446 * for details.
1447 *
Mario Six8fac2912018-07-10 08:40:17 +02001448 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001449 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001450efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1451 struct efi_event *event,
1452 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001453{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001454 struct efi_register_notify_event *item;
1455 efi_status_t ret = EFI_SUCCESS;
1456
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001457 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001458
1459 if (!protocol || !event || !registration) {
1460 ret = EFI_INVALID_PARAMETER;
1461 goto out;
1462 }
1463
1464 item = calloc(1, sizeof(struct efi_register_notify_event));
1465 if (!item) {
1466 ret = EFI_OUT_OF_RESOURCES;
1467 goto out;
1468 }
1469
1470 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301471 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001472 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001473
1474 list_add_tail(&item->link, &efi_register_notify_events);
1475
1476 *registration = item;
1477out:
1478 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001479}
1480
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001481/**
Mario Six8fac2912018-07-10 08:40:17 +02001482 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001483 *
Mario Six8fac2912018-07-10 08:40:17 +02001484 * @search_type: selection criterion
1485 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001486 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001487 *
1488 * See the documentation of the LocateHandle service in the UEFI specification.
1489 *
Mario Six8fac2912018-07-10 08:40:17 +02001490 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001491 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001492static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001493 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001494{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001495 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001496
1497 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001498 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001499 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001500 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001501 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001502 return (ret != EFI_SUCCESS);
1503 default:
1504 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001505 return -1;
1506 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001507}
1508
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001509/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001510 * efi_check_register_notify_event() - check if registration key is valid
1511 *
1512 * Check that a pointer is a valid registration key as returned by
1513 * RegisterProtocolNotify().
1514 *
1515 * @key: registration key
1516 * Return: valid registration key or NULL
1517 */
1518static struct efi_register_notify_event *efi_check_register_notify_event
1519 (void *key)
1520{
1521 struct efi_register_notify_event *event;
1522
1523 list_for_each_entry(event, &efi_register_notify_events, link) {
1524 if (event == (struct efi_register_notify_event *)key)
1525 return event;
1526 }
1527 return NULL;
1528}
1529
1530/**
Mario Six8fac2912018-07-10 08:40:17 +02001531 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001532 *
1533 * @search_type: selection criterion
1534 * @protocol: GUID of the protocol
1535 * @search_key: registration key
1536 * @buffer_size: size of the buffer to receive the handles in bytes
1537 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001538 *
1539 * This function is meant for U-Boot internal calls. For the API implementation
1540 * of the LocateHandle service see efi_locate_handle_ext.
1541 *
Mario Six8fac2912018-07-10 08:40:17 +02001542 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001543 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001544static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001545 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001546 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001547 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001548{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001549 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001550 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001551 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001552 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001553
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001554 /* Check parameters */
1555 switch (search_type) {
1556 case ALL_HANDLES:
1557 break;
1558 case BY_REGISTER_NOTIFY:
1559 if (!search_key)
1560 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001561 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001562 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001563 if (!event)
1564 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001565 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001566 case BY_PROTOCOL:
1567 if (!protocol)
1568 return EFI_INVALID_PARAMETER;
1569 break;
1570 default:
1571 return EFI_INVALID_PARAMETER;
1572 }
1573
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001575 if (search_type == BY_REGISTER_NOTIFY) {
1576 if (list_empty(&event->handles))
1577 return EFI_NOT_FOUND;
1578 handle = list_first_entry(&event->handles,
1579 struct efi_protocol_notification,
1580 link);
1581 efiobj = handle->handle;
1582 size += sizeof(void *);
1583 } else {
1584 list_for_each_entry(efiobj, &efi_obj_list, link) {
1585 if (!efi_search(search_type, protocol, efiobj))
1586 size += sizeof(void *);
1587 }
1588 if (size == 0)
1589 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001590 }
1591
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001592 if (!buffer_size)
1593 return EFI_INVALID_PARAMETER;
1594
Alexander Grafc15d9212016-03-04 01:09:59 +01001595 if (*buffer_size < size) {
1596 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001597 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001598 }
1599
Rob Clarkcdee3372017-08-06 14:10:07 -04001600 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001601
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001602 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001603 if (!buffer)
1604 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001605
Alexander Grafc15d9212016-03-04 01:09:59 +01001606 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001607 if (search_type == BY_REGISTER_NOTIFY) {
1608 *buffer = efiobj;
1609 list_del(&handle->link);
1610 } else {
1611 list_for_each_entry(efiobj, &efi_obj_list, link) {
1612 if (!efi_search(search_type, protocol, efiobj))
1613 *buffer++ = efiobj;
1614 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001615 }
1616
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001617 return EFI_SUCCESS;
1618}
1619
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001620/**
Mario Six8fac2912018-07-10 08:40:17 +02001621 * efi_locate_handle_ext() - locate handles implementing a protocol.
1622 * @search_type: selection criterion
1623 * @protocol: GUID of the protocol
1624 * @search_key: registration key
1625 * @buffer_size: size of the buffer to receive the handles in bytes
1626 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001627 *
1628 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001629 *
Mario Six8fac2912018-07-10 08:40:17 +02001630 * See the Unified Extensible Firmware Interface (UEFI) specification for
1631 * details.
1632 *
1633 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001634 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001635static efi_status_t EFIAPI efi_locate_handle_ext(
1636 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001637 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001638 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001639{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001640 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001641 buffer_size, buffer);
1642
1643 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1644 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001645}
1646
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001647/**
Mario Six8fac2912018-07-10 08:40:17 +02001648 * efi_remove_configuration_table() - collapses configuration table entries,
1649 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001650 *
Mario Six8fac2912018-07-10 08:40:17 +02001651 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001652 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001653static void efi_remove_configuration_table(int i)
1654{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001655 struct efi_configuration_table *this = &systab.tables[i];
1656 struct efi_configuration_table *next = &systab.tables[i + 1];
1657 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001658
1659 memmove(this, next, (ulong)end - (ulong)next);
1660 systab.nr_tables--;
1661}
1662
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001663/**
Mario Six8fac2912018-07-10 08:40:17 +02001664 * efi_install_configuration_table() - adds, updates, or removes a
1665 * configuration table
1666 * @guid: GUID of the installed table
1667 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001668 *
1669 * This function is used for internal calls. For the API implementation of the
1670 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1671 *
Mario Six8fac2912018-07-10 08:40:17 +02001672 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001673 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001674efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1675 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001676{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001677 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001678 int i;
1679
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001680 if (!guid)
1681 return EFI_INVALID_PARAMETER;
1682
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001683 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001684 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001685 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001686 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001687 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001688 else
1689 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001690 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001691 }
1692 }
1693
Alexander Graffe3366f2017-07-26 13:41:04 +02001694 if (!table)
1695 return EFI_NOT_FOUND;
1696
Alexander Grafc15d9212016-03-04 01:09:59 +01001697 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001698 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001699 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001700
1701 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301702 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001703 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001704 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001705
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001706out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001707 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001708 efi_update_table_header_crc32(&systab.hdr);
1709
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001710 /* Notify that the configuration table was changed */
1711 list_for_each_entry(evt, &efi_events, link) {
1712 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001713 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001714 break;
1715 }
1716 }
1717
Alexander Grafc5c11632016-08-19 01:23:24 +02001718 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001719}
1720
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001721/**
Mario Six8fac2912018-07-10 08:40:17 +02001722 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1723 * configuration table.
1724 * @guid: GUID of the installed table
1725 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001726 *
1727 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001728 *
Mario Six8fac2912018-07-10 08:40:17 +02001729 * See the Unified Extensible Firmware Interface (UEFI) specification for
1730 * details.
1731 *
1732 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001733 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001734static efi_status_t
1735EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1736 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001737{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001738 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001739 return EFI_EXIT(efi_install_configuration_table(guid, table));
1740}
1741
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001742/**
Mario Six8fac2912018-07-10 08:40:17 +02001743 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001744 *
1745 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001746 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001747 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001748 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001749 * code is returned.
1750 *
1751 * @device_path: device path of the loaded image
1752 * @file_path: file path of the loaded image
1753 * @handle_ptr: handle of the loaded image
1754 * @info_ptr: loaded image protocol
1755 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001756 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001757efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1758 struct efi_device_path *file_path,
1759 struct efi_loaded_image_obj **handle_ptr,
1760 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001761{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001762 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001763 struct efi_loaded_image *info = NULL;
1764 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001765 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001766
1767 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1768 *handle_ptr = NULL;
1769 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001770
1771 info = calloc(1, sizeof(*info));
1772 if (!info)
1773 return EFI_OUT_OF_RESOURCES;
1774 obj = calloc(1, sizeof(*obj));
1775 if (!obj) {
1776 free(info);
1777 return EFI_OUT_OF_RESOURCES;
1778 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001779 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001780
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001781 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001782 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001783
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001784 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001785 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001786 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001787
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001788 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001789 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001790
1791 dp = efi_dp_append(device_path, file_path);
1792 if (!dp) {
1793 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001794 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001795 }
1796 } else {
1797 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001798 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001799 ret = efi_add_protocol(&obj->header,
1800 &efi_guid_loaded_image_device_path, dp);
1801 if (ret != EFI_SUCCESS)
1802 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001803
1804 /*
1805 * When asking for the loaded_image interface, just
1806 * return handle which points to loaded_image_info
1807 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001808 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001809 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001810 if (ret != EFI_SUCCESS)
1811 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001812
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001813 *info_ptr = info;
1814 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001815
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001816 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001817failure:
1818 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001819 efi_delete_handle(&obj->header);
1820 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001821 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001822}
1823
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001824/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001825 * efi_locate_device_path() - Get the device path and handle of an device
1826 * implementing a protocol
1827 * @protocol: GUID of the protocol
1828 * @device_path: device path
1829 * @device: handle of the device
1830 *
1831 * This function implements the LocateDevicePath service.
1832 *
1833 * See the Unified Extensible Firmware Interface (UEFI) specification for
1834 * details.
1835 *
1836 * Return: status code
1837 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001838efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1839 struct efi_device_path **device_path,
1840 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001841{
1842 struct efi_device_path *dp;
1843 size_t i;
1844 struct efi_handler *handler;
1845 efi_handle_t *handles;
1846 size_t len, len_dp;
1847 size_t len_best = 0;
1848 efi_uintn_t no_handles;
1849 u8 *remainder;
1850 efi_status_t ret;
1851
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001852 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001853
1854 if (!protocol || !device_path || !*device_path) {
1855 ret = EFI_INVALID_PARAMETER;
1856 goto out;
1857 }
1858
1859 /* Find end of device path */
1860 len = efi_dp_instance_size(*device_path);
1861
1862 /* Get all handles implementing the protocol */
1863 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1864 &no_handles, &handles));
1865 if (ret != EFI_SUCCESS)
1866 goto out;
1867
1868 for (i = 0; i < no_handles; ++i) {
1869 /* Find the device path protocol */
1870 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1871 &handler);
1872 if (ret != EFI_SUCCESS)
1873 continue;
1874 dp = (struct efi_device_path *)handler->protocol_interface;
1875 len_dp = efi_dp_instance_size(dp);
1876 /*
1877 * This handle can only be a better fit
1878 * if its device path length is longer than the best fit and
1879 * if its device path length is shorter of equal the searched
1880 * device path.
1881 */
1882 if (len_dp <= len_best || len_dp > len)
1883 continue;
1884 /* Check if dp is a subpath of device_path */
1885 if (memcmp(*device_path, dp, len_dp))
1886 continue;
1887 if (!device) {
1888 ret = EFI_INVALID_PARAMETER;
1889 goto out;
1890 }
1891 *device = handles[i];
1892 len_best = len_dp;
1893 }
1894 if (len_best) {
1895 remainder = (u8 *)*device_path + len_best;
1896 *device_path = (struct efi_device_path *)remainder;
1897 ret = EFI_SUCCESS;
1898 } else {
1899 ret = EFI_NOT_FOUND;
1900 }
1901out:
1902 return EFI_EXIT(ret);
1903}
1904
1905/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001906 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001907 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001908 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1909 * callers obligation to update the memory type as needed.
1910 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001911 * @file_path: the path of the image to load
1912 * @buffer: buffer containing the loaded image
1913 * @size: size of the loaded image
1914 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001915 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001916static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001917efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001918 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001919{
Rob Clark857a1222017-09-13 18:05:35 -04001920 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001921 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001922 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001923 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001924
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001925 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001926 f = efi_file_from_path(file_path);
1927 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001928 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001929
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001930 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001931 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001932 goto error;
1933
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001934 /*
1935 * When reading the file we do not yet know if it contains an
1936 * application, a boottime driver, or a runtime driver. So here we
1937 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1938 * update the reservation according to the image type.
1939 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001940 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1941 EFI_BOOT_SERVICES_DATA,
1942 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001943 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001944 ret = EFI_OUT_OF_RESOURCES;
1945 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001946 }
1947
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001948 /* Read file */
1949 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1950 if (ret != EFI_SUCCESS)
1951 efi_free_pages(addr, efi_size_in_pages(bs));
1952 *buffer = (void *)(uintptr_t)addr;
1953 *size = bs;
1954error:
1955 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001956 return ret;
1957}
1958
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001959/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001960 * efi_load_image_from_path() - load an image using a file path
1961 *
1962 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1963 * callers obligation to update the memory type as needed.
1964 *
1965 * @boot_policy: true for request originating from the boot manager
1966 * @file_path: the path of the image to load
1967 * @buffer: buffer containing the loaded image
1968 * @size: size of the loaded image
1969 * Return: status code
1970 */
1971static
1972efi_status_t efi_load_image_from_path(bool boot_policy,
1973 struct efi_device_path *file_path,
1974 void **buffer, efi_uintn_t *size)
1975{
1976 efi_handle_t device;
1977 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001978 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001979 struct efi_load_file_protocol *load_file_protocol = NULL;
1980 efi_uintn_t buffer_size;
1981 uint64_t addr, pages;
1982 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001983 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001984
1985 /* In case of failure nothing is returned */
1986 *buffer = NULL;
1987 *size = 0;
1988
1989 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001990 device = efi_dp_find_obj(dp, NULL, &rem);
1991 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1992 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001993 if (ret == EFI_SUCCESS)
1994 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001995
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001996 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001997 if (ret == EFI_SUCCESS) {
1998 guid = &efi_guid_load_file_protocol;
1999 } else if (!boot_policy) {
2000 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002001 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002002 }
2003 if (ret != EFI_SUCCESS)
2004 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002005 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002006 if (ret != EFI_SUCCESS)
2007 return EFI_NOT_FOUND;
2008 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002009 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002010 ret = EFI_CALL(load_file_protocol->load_file(
2011 load_file_protocol, rem, boot_policy,
2012 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002013 if (ret != EFI_BUFFER_TOO_SMALL)
2014 goto out;
2015 pages = efi_size_in_pages(buffer_size);
2016 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2017 pages, &addr);
2018 if (ret != EFI_SUCCESS) {
2019 ret = EFI_OUT_OF_RESOURCES;
2020 goto out;
2021 }
2022 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002023 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002024 &buffer_size, (void *)(uintptr_t)addr));
2025 if (ret != EFI_SUCCESS)
2026 efi_free_pages(addr, pages);
2027out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002028 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002029 if (ret == EFI_SUCCESS) {
2030 *buffer = (void *)(uintptr_t)addr;
2031 *size = buffer_size;
2032 }
2033
2034 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002035}
2036
2037/**
Mario Six8fac2912018-07-10 08:40:17 +02002038 * efi_load_image() - load an EFI image into memory
2039 * @boot_policy: true for request originating from the boot manager
2040 * @parent_image: the caller's image handle
2041 * @file_path: the path of the image to load
2042 * @source_buffer: memory location from which the image is installed
2043 * @source_size: size of the memory area from which the image is installed
2044 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002045 *
2046 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002047 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002048 * See the Unified Extensible Firmware Interface (UEFI) specification
2049 * for details.
2050 *
Mario Six8fac2912018-07-10 08:40:17 +02002051 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002052 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002053efi_status_t EFIAPI efi_load_image(bool boot_policy,
2054 efi_handle_t parent_image,
2055 struct efi_device_path *file_path,
2056 void *source_buffer,
2057 efi_uintn_t source_size,
2058 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002059{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002060 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002061 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002062 struct efi_loaded_image_obj **image_obj =
2063 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002064 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002065 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002066
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002067 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002068 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002069
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002070 if (!image_handle || (!source_buffer && !file_path) ||
2071 !efi_search_obj(parent_image) ||
2072 /* The parent image handle must refer to a loaded image */
2073 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002074 ret = EFI_INVALID_PARAMETER;
2075 goto error;
2076 }
Rob Clark857a1222017-09-13 18:05:35 -04002077
2078 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002079 ret = efi_load_image_from_path(boot_policy, file_path,
2080 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002081 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002082 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002083 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002084 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002085 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002086 /* split file_path which contains both the device and file parts */
2087 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002088 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002089 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002090 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002091 if (!source_buffer)
2092 /* Release buffer to which file was loaded */
2093 efi_free_pages((uintptr_t)dest_buffer,
2094 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002095 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002096 info->system_table = &systab;
2097 info->parent_handle = parent_image;
2098 } else {
2099 /* The image is invalid. Release all associated resources. */
2100 efi_delete_handle(*image_handle);
2101 *image_handle = NULL;
2102 free(info);
2103 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002104error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002105 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002106}
2107
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002108/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002109 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2110 */
2111static void efi_exit_caches(void)
2112{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002113#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002114 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002115 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2116 * caches are enabled.
2117 *
2118 * TODO:
2119 * According to the UEFI spec caches that can be managed via CP15
2120 * operations should be enabled. Caches requiring platform information
2121 * to manage should be disabled. This should not happen in
2122 * ExitBootServices() but before invoking any UEFI binary is invoked.
2123 *
2124 * We want to keep the current workaround while GRUB prior to version
2125 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002126 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002127 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002128#endif
2129}
2130
2131/**
Mario Six8fac2912018-07-10 08:40:17 +02002132 * efi_exit_boot_services() - stop all boot services
2133 * @image_handle: handle of the loaded image
2134 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002135 *
2136 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002137 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002138 * See the Unified Extensible Firmware Interface (UEFI) specification
2139 * for details.
2140 *
Mario Six8fac2912018-07-10 08:40:17 +02002141 * All timer events are disabled. For exit boot services events the
2142 * notification function is called. The boot services are disabled in the
2143 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002144 *
Mario Six8fac2912018-07-10 08:40:17 +02002145 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002146 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002147static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002148 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002149{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002150 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002151 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002152
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002153 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002154
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002155 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002156 if (map_key != efi_memory_map_key) {
2157 ret = EFI_INVALID_PARAMETER;
2158 goto out;
2159 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002160
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002161 /* Check if ExitBootServices has already been called */
2162 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002163 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002164
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002165 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2166 list_for_each_entry(evt, &efi_events, link) {
2167 if (evt->group &&
2168 !guidcmp(evt->group,
2169 &efi_guid_event_group_before_exit_boot_services)) {
2170 efi_signal_event(evt);
2171 break;
2172 }
2173 }
2174
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002175 /* Stop all timer related activities */
2176 timers_enabled = false;
2177
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002178 /* Add related events to the event group */
2179 list_for_each_entry(evt, &efi_events, link) {
2180 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2181 evt->group = &efi_guid_event_group_exit_boot_services;
2182 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002183 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002184 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002185 if (evt->group &&
2186 !guidcmp(evt->group,
2187 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002188 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002189 break;
2190 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002191 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002192
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002193 /* Make sure that notification functions are not called anymore */
2194 efi_tpl = TPL_HIGH_LEVEL;
2195
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002196 /* Notify variable services */
2197 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002198
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002199 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2200 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2201 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2202 list_del(&evt->link);
2203 }
2204
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002205 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002206 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002207 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002208 udc_disconnect();
2209 board_quiesce_devices();
2210 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2211 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002212
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002213 /* Patch out unsupported runtime function */
2214 efi_runtime_detach();
2215
Alexander Graffa5246b2018-11-15 21:23:47 +01002216 /* Fix up caches for EFI payloads if necessary */
2217 efi_exit_caches();
2218
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002219 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002220 systab.con_in_handle = NULL;
2221 systab.con_in = NULL;
2222 systab.con_out_handle = NULL;
2223 systab.con_out = NULL;
2224 systab.stderr_handle = NULL;
2225 systab.std_err = NULL;
2226 systab.boottime = NULL;
2227
2228 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002229 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002230
Alexander Grafc15d9212016-03-04 01:09:59 +01002231 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002232 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002233 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002234out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002235 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2236 if (ret != EFI_SUCCESS)
2237 efi_tcg2_notify_exit_boot_services_failed();
2238 }
2239
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002240 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002241}
2242
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002243/**
Mario Six8fac2912018-07-10 08:40:17 +02002244 * efi_get_next_monotonic_count() - get next value of the counter
2245 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002246 *
2247 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002248 *
2249 * See the Unified Extensible Firmware Interface (UEFI) specification for
2250 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002251 *
Mario Six8fac2912018-07-10 08:40:17 +02002252 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002253 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002254static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2255{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002256 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002257 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002258
Alexander Grafc15d9212016-03-04 01:09:59 +01002259 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002260 if (!count) {
2261 ret = EFI_INVALID_PARAMETER;
2262 goto out;
2263 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002264 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002265 ret = EFI_SUCCESS;
2266out:
2267 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002268}
2269
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002270/**
Mario Six8fac2912018-07-10 08:40:17 +02002271 * efi_stall() - sleep
2272 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002273 *
Mario Six8fac2912018-07-10 08:40:17 +02002274 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002275 *
Mario Six8fac2912018-07-10 08:40:17 +02002276 * See the Unified Extensible Firmware Interface (UEFI) specification for
2277 * details.
2278 *
2279 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002280 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002281static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2282{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002283 u64 end_tick;
2284
Alexander Grafc15d9212016-03-04 01:09:59 +01002285 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002286
2287 end_tick = get_ticks() + usec_to_tick(microseconds);
2288 while (get_ticks() < end_tick)
2289 efi_timer_check();
2290
Alexander Grafc15d9212016-03-04 01:09:59 +01002291 return EFI_EXIT(EFI_SUCCESS);
2292}
2293
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002294/**
Mario Six8fac2912018-07-10 08:40:17 +02002295 * efi_set_watchdog_timer() - reset the watchdog timer
2296 * @timeout: seconds before reset by watchdog
2297 * @watchdog_code: code to be logged when resetting
2298 * @data_size: size of buffer in bytes
2299 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002300 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002301 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002302 *
Mario Six8fac2912018-07-10 08:40:17 +02002303 * See the Unified Extensible Firmware Interface (UEFI) specification for
2304 * details.
2305 *
2306 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002307 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002308static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2309 uint64_t watchdog_code,
2310 unsigned long data_size,
2311 uint16_t *watchdog_data)
2312{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002313 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002314 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002315 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002316}
2317
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002318/**
Mario Six8fac2912018-07-10 08:40:17 +02002319 * efi_close_protocol() - close a protocol
2320 * @handle: handle on which the protocol shall be closed
2321 * @protocol: GUID of the protocol to close
2322 * @agent_handle: handle of the driver
2323 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002324 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002325 * This is the function implementing the CloseProtocol service is for internal
2326 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002327 *
Mario Six8fac2912018-07-10 08:40:17 +02002328 * See the Unified Extensible Firmware Interface (UEFI) specification for
2329 * details.
2330 *
2331 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002332 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002333efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2334 efi_handle_t agent_handle,
2335 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002336{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002337 struct efi_handler *handler;
2338 struct efi_open_protocol_info_item *item;
2339 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002340 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002341
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002342 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002343 (controller_handle && !efi_search_obj(controller_handle)))
2344 return EFI_INVALID_PARAMETER;
2345 ret = efi_search_protocol(handle, protocol, &handler);
2346 if (ret != EFI_SUCCESS)
2347 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002348
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002349 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002350 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2351 if (item->info.agent_handle == agent_handle &&
2352 item->info.controller_handle == controller_handle) {
2353 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002354 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002355 }
2356 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002357
2358 return ret;
2359}
2360
2361/**
2362 * efi_close_protocol_ext() - close a protocol
2363 * @handle: handle on which the protocol shall be closed
2364 * @protocol: GUID of the protocol to close
2365 * @agent_handle: handle of the driver
2366 * @controller_handle: handle of the controller
2367 *
2368 * This function implements the CloseProtocol service.
2369 *
2370 * See the Unified Extensible Firmware Interface (UEFI) specification for
2371 * details.
2372 *
2373 * Return: status code
2374 */
2375static efi_status_t EFIAPI
2376efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2377 efi_handle_t agent_handle,
2378 efi_handle_t controller_handle)
2379{
2380 efi_status_t ret;
2381
2382 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2383 controller_handle);
2384
2385 ret = efi_close_protocol(handle, protocol,
2386 agent_handle, controller_handle);
2387
2388 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002389}
2390
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002391/**
Mario Six8fac2912018-07-10 08:40:17 +02002392 * efi_open_protocol_information() - provide information about then open status
2393 * of a protocol on a handle
2394 * @handle: handle for which the information shall be retrieved
2395 * @protocol: GUID of the protocol
2396 * @entry_buffer: buffer to receive the open protocol information
2397 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002398 *
2399 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002400 *
2401 * See the Unified Extensible Firmware Interface (UEFI) specification for
2402 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002403 *
Mario Six8fac2912018-07-10 08:40:17 +02002404 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002405 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002406static efi_status_t EFIAPI efi_open_protocol_information(
2407 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002408 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002409 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002410{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002411 unsigned long buffer_size;
2412 unsigned long count;
2413 struct efi_handler *handler;
2414 struct efi_open_protocol_info_item *item;
2415 efi_status_t r;
2416
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002417 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002418 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002419
2420 /* Check parameters */
2421 if (!entry_buffer) {
2422 r = EFI_INVALID_PARAMETER;
2423 goto out;
2424 }
2425 r = efi_search_protocol(handle, protocol, &handler);
2426 if (r != EFI_SUCCESS)
2427 goto out;
2428
2429 /* Count entries */
2430 count = 0;
2431 list_for_each_entry(item, &handler->open_infos, link) {
2432 if (item->info.open_count)
2433 ++count;
2434 }
2435 *entry_count = count;
2436 *entry_buffer = NULL;
2437 if (!count) {
2438 r = EFI_SUCCESS;
2439 goto out;
2440 }
2441
2442 /* Copy entries */
2443 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002444 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002445 (void **)entry_buffer);
2446 if (r != EFI_SUCCESS)
2447 goto out;
2448 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2449 if (item->info.open_count)
2450 (*entry_buffer)[--count] = item->info;
2451 }
2452out:
2453 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002454}
2455
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002456/**
Mario Six8fac2912018-07-10 08:40:17 +02002457 * efi_protocols_per_handle() - get protocols installed on a handle
2458 * @handle: handle for which the information is retrieved
2459 * @protocol_buffer: buffer with protocol GUIDs
2460 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002461 *
2462 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002463 *
Mario Six8fac2912018-07-10 08:40:17 +02002464 * See the Unified Extensible Firmware Interface (UEFI) specification for
2465 * details.
2466 *
2467 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002468 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002469static efi_status_t EFIAPI efi_protocols_per_handle(
2470 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002471 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002472{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002473 unsigned long buffer_size;
2474 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002475 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002476 efi_status_t r;
2477
Alexander Grafc15d9212016-03-04 01:09:59 +01002478 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2479 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002480
2481 if (!handle || !protocol_buffer || !protocol_buffer_count)
2482 return EFI_EXIT(EFI_INVALID_PARAMETER);
2483
2484 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002485 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002486
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002487 efiobj = efi_search_obj(handle);
2488 if (!efiobj)
2489 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002490
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002491 /* Count protocols */
2492 list_for_each(protocol_handle, &efiobj->protocols) {
2493 ++*protocol_buffer_count;
2494 }
2495
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002496 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002497 if (*protocol_buffer_count) {
2498 size_t j = 0;
2499
2500 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002501 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002502 (void **)protocol_buffer);
2503 if (r != EFI_SUCCESS)
2504 return EFI_EXIT(r);
2505 list_for_each(protocol_handle, &efiobj->protocols) {
2506 struct efi_handler *protocol;
2507
2508 protocol = list_entry(protocol_handle,
2509 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002510 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002511 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002512 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002513 }
2514
2515 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002516}
2517
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002518efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2519 const efi_guid_t *protocol, void *search_key,
2520 efi_uintn_t *no_handles, efi_handle_t **buffer)
2521{
2522 efi_status_t r;
2523 efi_uintn_t buffer_size = 0;
2524
2525 if (!no_handles || !buffer) {
2526 r = EFI_INVALID_PARAMETER;
2527 goto out;
2528 }
2529 *no_handles = 0;
2530 *buffer = NULL;
2531 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2532 *buffer);
2533 if (r != EFI_BUFFER_TOO_SMALL)
2534 goto out;
2535 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2536 (void **)buffer);
2537 if (r != EFI_SUCCESS)
2538 goto out;
2539 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2540 *buffer);
2541 if (r == EFI_SUCCESS)
2542 *no_handles = buffer_size / sizeof(efi_handle_t);
2543out:
2544 return r;
2545}
2546
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002547/**
Mario Six8fac2912018-07-10 08:40:17 +02002548 * efi_locate_handle_buffer() - locate handles implementing a protocol
2549 * @search_type: selection criterion
2550 * @protocol: GUID of the protocol
2551 * @search_key: registration key
2552 * @no_handles: number of returned handles
2553 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002554 *
2555 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002556 *
Mario Six8fac2912018-07-10 08:40:17 +02002557 * See the Unified Extensible Firmware Interface (UEFI) specification for
2558 * details.
2559 *
2560 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002561 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002562efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002563 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002564 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002565 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002566{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002567 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002568
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002569 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002570 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002571
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002572 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2573 no_handles, buffer);
2574
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002575 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002576}
2577
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002578/**
Mario Six8fac2912018-07-10 08:40:17 +02002579 * efi_locate_protocol() - find an interface implementing a protocol
2580 * @protocol: GUID of the protocol
2581 * @registration: registration key passed to the notification function
2582 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002583 *
2584 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002585 *
2586 * See the Unified Extensible Firmware Interface (UEFI) specification for
2587 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002588 *
Mario Six8fac2912018-07-10 08:40:17 +02002589 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002590 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002591static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002592 void *registration,
2593 void **protocol_interface)
2594{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002595 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002596 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002597 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002598
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002599 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002600
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002601 /*
2602 * The UEFI spec explicitly requires a protocol even if a registration
2603 * key is provided. This differs from the logic in LocateHandle().
2604 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002605 if (!protocol || !protocol_interface)
2606 return EFI_EXIT(EFI_INVALID_PARAMETER);
2607
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002608 if (registration) {
2609 struct efi_register_notify_event *event;
2610 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002611
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002612 event = efi_check_register_notify_event(registration);
2613 if (!event)
2614 return EFI_EXIT(EFI_INVALID_PARAMETER);
2615 /*
2616 * The UEFI spec requires to return EFI_NOT_FOUND if no
2617 * protocol instance matches protocol and registration.
2618 * So let's do the same for a mismatch between protocol and
2619 * registration.
2620 */
2621 if (guidcmp(&event->protocol, protocol))
2622 goto not_found;
2623 if (list_empty(&event->handles))
2624 goto not_found;
2625 handle = list_first_entry(&event->handles,
2626 struct efi_protocol_notification,
2627 link);
2628 efiobj = handle->handle;
2629 list_del(&handle->link);
2630 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002631 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002632 if (ret == EFI_SUCCESS)
2633 goto found;
2634 } else {
2635 list_for_each_entry(efiobj, &efi_obj_list, link) {
2636 ret = efi_search_protocol(efiobj, protocol, &handler);
2637 if (ret == EFI_SUCCESS)
2638 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002639 }
2640 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002641not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002642 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002643 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002644found:
2645 *protocol_interface = handler->protocol_interface;
2646 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002647}
2648
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002649/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002650 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002651 * interfaces
2652 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002653 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002654 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002655 * Core functionality of efi_install_multiple_protocol_interfaces
2656 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002657 *
Mario Six8fac2912018-07-10 08:40:17 +02002658 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002659 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002660static efi_status_t EFIAPI
2661efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2662 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002663{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002664 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002665 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002666 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002667 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002668 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002669 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002670
2671 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002672 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002673
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002674 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002675 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002676 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002677 if (!protocol)
2678 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002679 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002680 /* Check that a device path has not been installed before */
2681 if (!guidcmp(protocol, &efi_guid_device_path)) {
2682 struct efi_device_path *dp = protocol_interface;
2683
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002684 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2685 &old_handle));
2686 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002687 dp->type == DEVICE_PATH_TYPE_END) {
2688 EFI_PRINT("Path %pD already installed\n",
2689 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002690 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002691 break;
2692 }
2693 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002694 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2695 EFI_NATIVE_INTERFACE,
2696 protocol_interface));
2697 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002698 break;
2699 i++;
2700 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002701 if (ret == EFI_SUCCESS)
2702 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002703
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002704 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002705 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002706 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2707 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002708 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002709 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002710 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002711
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002712out:
2713 efi_va_end(argptr_copy);
2714 return ret;
2715
Alexander Grafc15d9212016-03-04 01:09:59 +01002716}
2717
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002718/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002719 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2720 * interfaces
2721 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002722 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2723 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002724 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002725 *
2726 * This is the function for internal usage in U-Boot. For the API function
2727 * implementing the InstallMultipleProtocol service see
2728 * efi_install_multiple_protocol_interfaces_ext()
2729 *
2730 * Return: status code
2731 */
2732efi_status_t EFIAPI
2733efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2734{
2735 efi_status_t ret;
2736 efi_va_list argptr;
2737
2738 efi_va_start(argptr, handle);
2739 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2740 efi_va_end(argptr);
2741 return ret;
2742}
2743
2744/**
2745 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2746 * interfaces
2747 * @handle: handle on which the protocol interfaces shall be installed
2748 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2749 * interfaces
2750 *
2751 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002752 *
Mario Six8fac2912018-07-10 08:40:17 +02002753 * See the Unified Extensible Firmware Interface (UEFI) specification for
2754 * details.
2755 *
2756 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002757 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002758static efi_status_t EFIAPI
2759efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002760{
2761 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002762 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002763 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002764
2765 efi_va_start(argptr, handle);
2766 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2767 efi_va_end(argptr);
2768 return EFI_EXIT(ret);
2769}
2770
2771/**
2772 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2773 * multiple protocol
2774 * interfaces
2775 * @handle: handle from which the protocol interfaces shall be removed
2776 * @argptr: va_list of args
2777 *
2778 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2779 * Must not be called directly
2780 *
2781 * Return: status code
2782 */
2783static efi_status_t EFIAPI
2784efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2785 efi_va_list argptr)
2786{
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002787 const efi_guid_t *protocol;
2788 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002789 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002790 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002791 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002792
2793 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002794 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002795
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002796 efi_va_copy(argptr_copy, argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002797 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002798 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002799 if (!protocol)
2800 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002801 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002802 ret = efi_uninstall_protocol(handle, protocol,
2803 protocol_interface);
2804 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002805 break;
2806 i++;
2807 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002808 if (ret == EFI_SUCCESS) {
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002809 /* If the last protocol has been removed, delete the handle. */
2810 if (list_empty(&handle->protocols)) {
2811 list_del(&handle->link);
2812 free(handle);
2813 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002814 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002815 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002816
2817 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002818 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002819 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2820 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002821 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2822 EFI_NATIVE_INTERFACE,
2823 protocol_interface));
2824 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002825 /*
2826 * If any errors are generated while the protocol interfaces are being
2827 * uninstalled, then the protocols uninstalled prior to the error will
2828 * be reinstalled using InstallProtocolInterface() and the status code
2829 * EFI_INVALID_PARAMETER is returned.
2830 */
2831 ret = EFI_INVALID_PARAMETER;
2832
2833out:
2834 efi_va_end(argptr_copy);
2835 return ret;
2836}
2837
2838/**
2839 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2840 * interfaces
2841 * @handle: handle from which the protocol interfaces shall be removed
2842 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2843 * interfaces
2844 *
2845 * This function implements the UninstallMultipleProtocolInterfaces service.
2846 *
2847 * This is the function for internal usage in U-Boot. For the API function
2848 * implementing the UninstallMultipleProtocolInterfaces service see
2849 * efi_uninstall_multiple_protocol_interfaces_ext()
2850 *
2851 * Return: status code
2852 */
2853efi_status_t EFIAPI
2854efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2855{
2856 efi_status_t ret;
2857 efi_va_list argptr;
2858
2859 efi_va_start(argptr, handle);
2860 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002861 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002862 return ret;
2863}
2864
2865/**
2866 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2867 * interfaces
2868 * @handle: handle from which the protocol interfaces shall be removed
2869 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2870 * interfaces
2871 *
2872 * This function implements the UninstallMultipleProtocolInterfaces service.
2873 *
2874 * See the Unified Extensible Firmware Interface (UEFI) specification for
2875 * details.
2876 *
2877 * Return: status code
2878 */
2879static efi_status_t EFIAPI
2880efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2881{
2882 EFI_ENTRY("%p", handle);
2883 efi_status_t ret;
2884 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002885
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002886 efi_va_start(argptr, handle);
2887 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2888 efi_va_end(argptr);
2889 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002890}
2891
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002892/**
Mario Six8fac2912018-07-10 08:40:17 +02002893 * efi_calculate_crc32() - calculate cyclic redundancy code
2894 * @data: buffer with data
2895 * @data_size: size of buffer in bytes
2896 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002897 *
2898 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002899 *
2900 * See the Unified Extensible Firmware Interface (UEFI) specification for
2901 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002902 *
Mario Six8fac2912018-07-10 08:40:17 +02002903 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002904 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002905static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2906 efi_uintn_t data_size,
2907 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002908{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002909 efi_status_t ret = EFI_SUCCESS;
2910
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002911 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002912 if (!data || !data_size || !crc32_p) {
2913 ret = EFI_INVALID_PARAMETER;
2914 goto out;
2915 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002916 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002917out:
2918 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002919}
2920
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002921/**
Mario Six8fac2912018-07-10 08:40:17 +02002922 * efi_copy_mem() - copy memory
2923 * @destination: destination of the copy operation
2924 * @source: source of the copy operation
2925 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002926 *
2927 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002928 *
Mario Six8fac2912018-07-10 08:40:17 +02002929 * See the Unified Extensible Firmware Interface (UEFI) specification for
2930 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002931 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002932static void EFIAPI efi_copy_mem(void *destination, const void *source,
2933 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002934{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002935 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002936 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002937 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002938}
2939
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002940/**
Mario Six8fac2912018-07-10 08:40:17 +02002941 * efi_set_mem() - Fill memory with a byte value.
2942 * @buffer: buffer to fill
2943 * @size: size of buffer in bytes
2944 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002945 *
2946 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002947 *
Mario Six8fac2912018-07-10 08:40:17 +02002948 * See the Unified Extensible Firmware Interface (UEFI) specification for
2949 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002950 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002951static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002952{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002953 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002954 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002955 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002956}
2957
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002958/**
Mario Six8fac2912018-07-10 08:40:17 +02002959 * efi_protocol_open() - open protocol interface on a handle
2960 * @handler: handler of a protocol
2961 * @protocol_interface: interface implementing the protocol
2962 * @agent_handle: handle of the driver
2963 * @controller_handle: handle of the controller
2964 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002965 *
Mario Six8fac2912018-07-10 08:40:17 +02002966 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002967 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002968efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002969 struct efi_handler *handler,
2970 void **protocol_interface, void *agent_handle,
2971 void *controller_handle, uint32_t attributes)
2972{
2973 struct efi_open_protocol_info_item *item;
2974 struct efi_open_protocol_info_entry *match = NULL;
2975 bool opened_by_driver = false;
2976 bool opened_exclusive = false;
2977
2978 /* If there is no agent, only return the interface */
2979 if (!agent_handle)
2980 goto out;
2981
2982 /* For TEST_PROTOCOL ignore interface attribute */
2983 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2984 *protocol_interface = NULL;
2985
2986 /*
2987 * Check if the protocol is already opened by a driver with the same
2988 * attributes or opened exclusively
2989 */
2990 list_for_each_entry(item, &handler->open_infos, link) {
2991 if (item->info.agent_handle == agent_handle) {
2992 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2993 (item->info.attributes == attributes))
2994 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002995 } else {
2996 if (item->info.attributes &
2997 EFI_OPEN_PROTOCOL_BY_DRIVER)
2998 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002999 }
3000 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3001 opened_exclusive = true;
3002 }
3003
3004 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003005 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3006 if (opened_exclusive)
3007 return EFI_ACCESS_DENIED;
3008 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3009 if (opened_exclusive || opened_by_driver)
3010 return EFI_ACCESS_DENIED;
3011 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003012
3013 /* Prepare exclusive opening */
3014 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3015 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003016disconnect_next:
3017 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003018 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003019 efi_status_t ret;
3020
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003021 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003022 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3023 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003024 item->info.controller_handle,
3025 item->info.agent_handle,
3026 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003027 if (ret == EFI_SUCCESS)
3028 /*
3029 * Child controllers may have been
3030 * removed from the open_infos list. So
3031 * let's restart the loop.
3032 */
3033 goto disconnect_next;
3034 else
3035 opened_by_driver = true;
3036 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003037 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003038 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003039 if (opened_by_driver)
3040 return EFI_ACCESS_DENIED;
3041 }
3042
3043 /* Find existing entry */
3044 list_for_each_entry(item, &handler->open_infos, link) {
3045 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003046 item->info.controller_handle == controller_handle &&
3047 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003048 match = &item->info;
3049 }
3050 /* None found, create one */
3051 if (!match) {
3052 match = efi_create_open_info(handler);
3053 if (!match)
3054 return EFI_OUT_OF_RESOURCES;
3055 }
3056
3057 match->agent_handle = agent_handle;
3058 match->controller_handle = controller_handle;
3059 match->attributes = attributes;
3060 match->open_count++;
3061
3062out:
3063 /* For TEST_PROTOCOL ignore interface attribute. */
3064 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3065 *protocol_interface = handler->protocol_interface;
3066
3067 return EFI_SUCCESS;
3068}
3069
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003070/**
Mario Six8fac2912018-07-10 08:40:17 +02003071 * efi_open_protocol() - open protocol interface on a handle
3072 * @handle: handle on which the protocol shall be opened
3073 * @protocol: GUID of the protocol
3074 * @protocol_interface: interface implementing the protocol
3075 * @agent_handle: handle of the driver
3076 * @controller_handle: handle of the controller
3077 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003078 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003079 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003080 *
Mario Six8fac2912018-07-10 08:40:17 +02003081 * See the Unified Extensible Firmware Interface (UEFI) specification for
3082 * details.
3083 *
3084 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003085 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003086static efi_status_t EFIAPI efi_open_protocol
3087 (efi_handle_t handle, const efi_guid_t *protocol,
3088 void **protocol_interface, efi_handle_t agent_handle,
3089 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003090{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003091 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003092 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003093
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003094 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003095 protocol_interface, agent_handle, controller_handle,
3096 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003097
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003098 if (!handle || !protocol ||
3099 (!protocol_interface && attributes !=
3100 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003101 goto out;
3102 }
3103
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003104 switch (attributes) {
3105 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3106 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3107 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3108 break;
3109 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3110 if (controller_handle == handle)
3111 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003112 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003113 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3114 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003115 /* Check that the controller handle is valid */
3116 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003117 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003118 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003119 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003120 /* Check that the agent handle is valid */
3121 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003122 goto out;
3123 break;
3124 default:
3125 goto out;
3126 }
3127
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003128 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003129 switch (r) {
3130 case EFI_SUCCESS:
3131 break;
3132 case EFI_NOT_FOUND:
3133 r = EFI_UNSUPPORTED;
3134 goto out;
3135 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003136 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003137 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003138
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003139 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3140 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003141out:
3142 return EFI_EXIT(r);
3143}
3144
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003145/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003146 * efi_start_image() - call the entry point of an image
3147 * @image_handle: handle of the image
3148 * @exit_data_size: size of the buffer
3149 * @exit_data: buffer to receive the exit data of the called image
3150 *
3151 * This function implements the StartImage service.
3152 *
3153 * See the Unified Extensible Firmware Interface (UEFI) specification for
3154 * details.
3155 *
3156 * Return: status code
3157 */
3158efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3159 efi_uintn_t *exit_data_size,
3160 u16 **exit_data)
3161{
3162 struct efi_loaded_image_obj *image_obj =
3163 (struct efi_loaded_image_obj *)image_handle;
3164 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003165 void *info;
3166 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003167 efi_status_t exit_status;
3168 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003169
3170 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3171
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003172 if (!efi_search_obj(image_handle))
3173 return EFI_EXIT(EFI_INVALID_PARAMETER);
3174
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003175 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003176 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3177 return EFI_EXIT(EFI_INVALID_PARAMETER);
3178
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003179 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3180 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3181
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003182 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3183 &info, NULL, NULL,
3184 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3185 if (ret != EFI_SUCCESS)
3186 return EFI_EXIT(EFI_INVALID_PARAMETER);
3187
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003188 image_obj->exit_data_size = exit_data_size;
3189 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003190 image_obj->exit_status = &exit_status;
3191 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003192
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003193 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3194 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003195 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003196 if (ret == EFI_SECURITY_VIOLATION) {
3197 /*
3198 * TCG2 Protocol is installed but no TPM device found,
3199 * this is not expected.
3200 */
3201 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003202 }
3203 }
3204 }
3205
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003206 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003207 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003208 /*
3209 * We called the entry point of the child image with EFI_CALL
3210 * in the lines below. The child image called the Exit() boot
3211 * service efi_exit() which executed the long jump that brought
3212 * us to the current line. This implies that the second half
3213 * of the EFI_CALL macro has not been executed.
3214 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003215#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003216 /*
3217 * efi_exit() called efi_restore_gd(). We have to undo this
3218 * otherwise __efi_entry_check() will put the wrong value into
3219 * app_gd.
3220 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003221 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003222#endif
3223 /*
3224 * To get ready to call EFI_EXIT below we have to execute the
3225 * missed out steps of EFI_CALL.
3226 */
3227 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003228 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003229 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003230 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003231 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003232 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003233 }
3234
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003235 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003236 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003237 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003238 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3239
3240 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003241 * Control is returned from a started UEFI image either by calling
3242 * Exit() (where exit data can be provided) or by simply returning from
3243 * the entry point. In the latter case call Exit() on behalf of the
3244 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003245 */
3246 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3247}
3248
3249/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003250 * efi_delete_image() - delete loaded image from memory)
3251 *
3252 * @image_obj: handle of the loaded image
3253 * @loaded_image_protocol: loaded image protocol
3254 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003255static efi_status_t efi_delete_image
3256 (struct efi_loaded_image_obj *image_obj,
3257 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003258{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003259 struct efi_object *efiobj;
3260 efi_status_t r, ret = EFI_SUCCESS;
3261
3262close_next:
3263 list_for_each_entry(efiobj, &efi_obj_list, link) {
3264 struct efi_handler *protocol;
3265
3266 list_for_each_entry(protocol, &efiobj->protocols, link) {
3267 struct efi_open_protocol_info_item *info;
3268
3269 list_for_each_entry(info, &protocol->open_infos, link) {
3270 if (info->info.agent_handle !=
3271 (efi_handle_t)image_obj)
3272 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003273 r = efi_close_protocol(
3274 efiobj, &protocol->guid,
3275 info->info.agent_handle,
3276 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003277 if (r != EFI_SUCCESS)
3278 ret = r;
3279 /*
3280 * Closing protocols may results in further
3281 * items being deleted. To play it safe loop
3282 * over all elements again.
3283 */
3284 goto close_next;
3285 }
3286 }
3287 }
3288
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003289 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3290 efi_size_in_pages(loaded_image_protocol->image_size));
3291 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003292
3293 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003294}
3295
3296/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003297 * efi_unload_image() - unload an EFI image
3298 * @image_handle: handle of the image to be unloaded
3299 *
3300 * This function implements the UnloadImage service.
3301 *
3302 * See the Unified Extensible Firmware Interface (UEFI) specification for
3303 * details.
3304 *
3305 * Return: status code
3306 */
3307efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3308{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003309 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003310 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003311 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003312
3313 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003314
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003315 efiobj = efi_search_obj(image_handle);
3316 if (!efiobj) {
3317 ret = EFI_INVALID_PARAMETER;
3318 goto out;
3319 }
3320 /* Find the loaded image protocol */
3321 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3322 (void **)&loaded_image_protocol,
3323 NULL, NULL,
3324 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3325 if (ret != EFI_SUCCESS) {
3326 ret = EFI_INVALID_PARAMETER;
3327 goto out;
3328 }
3329 switch (efiobj->type) {
3330 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3331 /* Call the unload function */
3332 if (!loaded_image_protocol->unload) {
3333 ret = EFI_UNSUPPORTED;
3334 goto out;
3335 }
3336 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3337 if (ret != EFI_SUCCESS)
3338 goto out;
3339 break;
3340 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3341 break;
3342 default:
3343 ret = EFI_INVALID_PARAMETER;
3344 goto out;
3345 }
3346 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3347 loaded_image_protocol);
3348out:
3349 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003350}
3351
3352/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003353 * efi_update_exit_data() - fill exit data parameters of StartImage()
3354 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003355 * @image_obj: image handle
3356 * @exit_data_size: size of the exit data buffer
3357 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003358 * Return: status code
3359 */
3360static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3361 efi_uintn_t exit_data_size,
3362 u16 *exit_data)
3363{
3364 efi_status_t ret;
3365
3366 /*
3367 * If exit_data is not provided to StartImage(), exit_data_size must be
3368 * ignored.
3369 */
3370 if (!image_obj->exit_data)
3371 return EFI_SUCCESS;
3372 if (image_obj->exit_data_size)
3373 *image_obj->exit_data_size = exit_data_size;
3374 if (exit_data_size && exit_data) {
3375 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3376 exit_data_size,
3377 (void **)image_obj->exit_data);
3378 if (ret != EFI_SUCCESS)
3379 return ret;
3380 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3381 } else {
3382 image_obj->exit_data = NULL;
3383 }
3384 return EFI_SUCCESS;
3385}
3386
3387/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003388 * efi_exit() - leave an EFI application or driver
3389 * @image_handle: handle of the application or driver that is exiting
3390 * @exit_status: status code
3391 * @exit_data_size: size of the buffer in bytes
3392 * @exit_data: buffer with data describing an error
3393 *
3394 * This function implements the Exit service.
3395 *
3396 * See the Unified Extensible Firmware Interface (UEFI) specification for
3397 * details.
3398 *
3399 * Return: status code
3400 */
3401static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3402 efi_status_t exit_status,
3403 efi_uintn_t exit_data_size,
3404 u16 *exit_data)
3405{
3406 /*
3407 * TODO: We should call the unload procedure of the loaded
3408 * image protocol.
3409 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003410 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003411 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003412 struct efi_loaded_image_obj *image_obj =
3413 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003414 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003415
3416 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3417 exit_data_size, exit_data);
3418
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003419 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003420 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003421 (void **)&loaded_image_protocol,
3422 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003423 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003424 if (ret != EFI_SUCCESS) {
3425 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003426 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003427 }
3428
3429 /* Unloading of unstarted images */
3430 switch (image_obj->header.type) {
3431 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3432 break;
3433 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3434 efi_delete_image(image_obj, loaded_image_protocol);
3435 ret = EFI_SUCCESS;
3436 goto out;
3437 default:
3438 /* Handle does not refer to loaded image */
3439 ret = EFI_INVALID_PARAMETER;
3440 goto out;
3441 }
3442 /* A started image can only be unloaded it is the last one started. */
3443 if (image_handle != current_image) {
3444 ret = EFI_INVALID_PARAMETER;
3445 goto out;
3446 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003447
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003448 /* Exit data is only foreseen in case of failure. */
3449 if (exit_status != EFI_SUCCESS) {
3450 ret = efi_update_exit_data(image_obj, exit_data_size,
3451 exit_data);
3452 /* Exiting has priority. Don't return error to caller. */
3453 if (ret != EFI_SUCCESS)
3454 EFI_PRINT("%s: out of memory\n", __func__);
3455 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003456 /* efi_delete_image() frees image_obj. Copy before the call. */
3457 exit_jmp = image_obj->exit_jmp;
3458 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003459 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3460 exit_status != EFI_SUCCESS)
3461 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003462
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003463 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3464 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3465 ret = efi_tcg2_measure_efi_app_exit();
3466 if (ret != EFI_SUCCESS) {
3467 log_warning("tcg2 measurement fails(0x%lx)\n",
3468 ret);
3469 }
3470 }
3471 }
3472
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003473 /* Make sure entry/exit counts for EFI world cross-overs match */
3474 EFI_EXIT(exit_status);
3475
3476 /*
3477 * But longjmp out with the U-Boot gd, not the application's, as
3478 * the other end is a setjmp call inside EFI context.
3479 */
3480 efi_restore_gd();
3481
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003482 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003483
3484 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003485out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003486 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003487}
3488
3489/**
Mario Six8fac2912018-07-10 08:40:17 +02003490 * efi_handle_protocol() - get interface of a protocol on a handle
3491 * @handle: handle on which the protocol shall be opened
3492 * @protocol: GUID of the protocol
3493 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003494 *
3495 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003496 *
3497 * See the Unified Extensible Firmware Interface (UEFI) specification for
3498 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003499 *
Mario Six8fac2912018-07-10 08:40:17 +02003500 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003501 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003502efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3503 const efi_guid_t *protocol,
3504 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003505{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003506 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003507 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003508}
3509
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003510/**
Mario Six8fac2912018-07-10 08:40:17 +02003511 * efi_bind_controller() - bind a single driver to a controller
3512 * @controller_handle: controller handle
3513 * @driver_image_handle: driver handle
3514 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003515 *
Mario Six8fac2912018-07-10 08:40:17 +02003516 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003517 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003518static efi_status_t efi_bind_controller(
3519 efi_handle_t controller_handle,
3520 efi_handle_t driver_image_handle,
3521 struct efi_device_path *remain_device_path)
3522{
3523 struct efi_driver_binding_protocol *binding_protocol;
3524 efi_status_t r;
3525
3526 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3527 &efi_guid_driver_binding_protocol,
3528 (void **)&binding_protocol,
3529 driver_image_handle, NULL,
3530 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3531 if (r != EFI_SUCCESS)
3532 return r;
3533 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3534 controller_handle,
3535 remain_device_path));
3536 if (r == EFI_SUCCESS)
3537 r = EFI_CALL(binding_protocol->start(binding_protocol,
3538 controller_handle,
3539 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003540 efi_close_protocol(driver_image_handle,
3541 &efi_guid_driver_binding_protocol,
3542 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003543 return r;
3544}
3545
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003546/**
Mario Six8fac2912018-07-10 08:40:17 +02003547 * efi_connect_single_controller() - connect a single driver to a controller
3548 * @controller_handle: controller
3549 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003550 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003551 *
Mario Six8fac2912018-07-10 08:40:17 +02003552 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003553 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003554static efi_status_t efi_connect_single_controller(
3555 efi_handle_t controller_handle,
3556 efi_handle_t *driver_image_handle,
3557 struct efi_device_path *remain_device_path)
3558{
3559 efi_handle_t *buffer;
3560 size_t count;
3561 size_t i;
3562 efi_status_t r;
3563 size_t connected = 0;
3564
3565 /* Get buffer with all handles with driver binding protocol */
3566 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3567 &efi_guid_driver_binding_protocol,
3568 NULL, &count, &buffer));
3569 if (r != EFI_SUCCESS)
3570 return r;
3571
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003572 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003573 if (driver_image_handle) {
3574 for (; *driver_image_handle; ++driver_image_handle) {
3575 for (i = 0; i < count; ++i) {
3576 if (buffer[i] == *driver_image_handle) {
3577 buffer[i] = NULL;
3578 r = efi_bind_controller(
3579 controller_handle,
3580 *driver_image_handle,
3581 remain_device_path);
3582 /*
3583 * For drivers that do not support the
3584 * controller or are already connected
3585 * we receive an error code here.
3586 */
3587 if (r == EFI_SUCCESS)
3588 ++connected;
3589 }
3590 }
3591 }
3592 }
3593
3594 /*
3595 * TODO: Some overrides are not yet implemented:
3596 * - Platform Driver Override
3597 * - Driver Family Override Search
3598 * - Bus Specific Driver Override
3599 */
3600
3601 /* Driver Binding Search */
3602 for (i = 0; i < count; ++i) {
3603 if (buffer[i]) {
3604 r = efi_bind_controller(controller_handle,
3605 buffer[i],
3606 remain_device_path);
3607 if (r == EFI_SUCCESS)
3608 ++connected;
3609 }
3610 }
3611
3612 efi_free_pool(buffer);
3613 if (!connected)
3614 return EFI_NOT_FOUND;
3615 return EFI_SUCCESS;
3616}
3617
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003618/**
Mario Six8fac2912018-07-10 08:40:17 +02003619 * efi_connect_controller() - connect a controller to a driver
3620 * @controller_handle: handle of the controller
3621 * @driver_image_handle: handle of the driver
3622 * @remain_device_path: device path of a child controller
3623 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003624 *
3625 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003626 *
3627 * See the Unified Extensible Firmware Interface (UEFI) specification for
3628 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003629 *
3630 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003631 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003632 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3633 *
Mario Six8fac2912018-07-10 08:40:17 +02003634 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003635 */
3636static efi_status_t EFIAPI efi_connect_controller(
3637 efi_handle_t controller_handle,
3638 efi_handle_t *driver_image_handle,
3639 struct efi_device_path *remain_device_path,
3640 bool recursive)
3641{
3642 efi_status_t r;
3643 efi_status_t ret = EFI_NOT_FOUND;
3644 struct efi_object *efiobj;
3645
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003646 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003647 remain_device_path, recursive);
3648
3649 efiobj = efi_search_obj(controller_handle);
3650 if (!efiobj) {
3651 ret = EFI_INVALID_PARAMETER;
3652 goto out;
3653 }
3654
3655 r = efi_connect_single_controller(controller_handle,
3656 driver_image_handle,
3657 remain_device_path);
3658 if (r == EFI_SUCCESS)
3659 ret = EFI_SUCCESS;
3660 if (recursive) {
3661 struct efi_handler *handler;
3662 struct efi_open_protocol_info_item *item;
3663
3664 list_for_each_entry(handler, &efiobj->protocols, link) {
3665 list_for_each_entry(item, &handler->open_infos, link) {
3666 if (item->info.attributes &
3667 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3668 r = EFI_CALL(efi_connect_controller(
3669 item->info.controller_handle,
3670 driver_image_handle,
3671 remain_device_path,
3672 recursive));
3673 if (r == EFI_SUCCESS)
3674 ret = EFI_SUCCESS;
3675 }
3676 }
3677 }
3678 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003679 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003680 if (ret != EFI_SUCCESS && remain_device_path &&
3681 remain_device_path->type == DEVICE_PATH_TYPE_END)
3682 ret = EFI_SUCCESS;
3683out:
3684 return EFI_EXIT(ret);
3685}
3686
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003687/**
Mario Six8fac2912018-07-10 08:40:17 +02003688 * efi_reinstall_protocol_interface() - reinstall protocol interface
3689 * @handle: handle on which the protocol shall be reinstalled
3690 * @protocol: GUID of the protocol to be installed
3691 * @old_interface: interface to be removed
3692 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003693 *
3694 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003695 *
3696 * See the Unified Extensible Firmware Interface (UEFI) specification for
3697 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003698 *
3699 * The old interface is uninstalled. The new interface is installed.
3700 * Drivers are connected.
3701 *
Mario Six8fac2912018-07-10 08:40:17 +02003702 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003703 */
3704static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3705 efi_handle_t handle, const efi_guid_t *protocol,
3706 void *old_interface, void *new_interface)
3707{
3708 efi_status_t ret;
3709
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003710 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003711 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003712
3713 /* Uninstall protocol but do not delete handle */
3714 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003715 if (ret != EFI_SUCCESS)
3716 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003717
3718 /* Install the new protocol */
3719 ret = efi_add_protocol(handle, protocol, new_interface);
3720 /*
3721 * The UEFI spec does not specify what should happen to the handle
3722 * if in case of an error no protocol interface remains on the handle.
3723 * So let's do nothing here.
3724 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003725 if (ret != EFI_SUCCESS)
3726 goto out;
3727 /*
3728 * The returned status code has to be ignored.
3729 * Do not create an error if no suitable driver for the handle exists.
3730 */
3731 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3732out:
3733 return EFI_EXIT(ret);
3734}
3735
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003736/**
Mario Six8fac2912018-07-10 08:40:17 +02003737 * efi_get_child_controllers() - get all child controllers associated to a driver
3738 * @efiobj: handle of the controller
3739 * @driver_handle: handle of the driver
3740 * @number_of_children: number of child controllers
3741 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003742 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003743 * The allocated buffer has to be freed with free().
3744 *
Mario Six8fac2912018-07-10 08:40:17 +02003745 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003746 */
3747static efi_status_t efi_get_child_controllers(
3748 struct efi_object *efiobj,
3749 efi_handle_t driver_handle,
3750 efi_uintn_t *number_of_children,
3751 efi_handle_t **child_handle_buffer)
3752{
3753 struct efi_handler *handler;
3754 struct efi_open_protocol_info_item *item;
3755 efi_uintn_t count = 0, i;
3756 bool duplicate;
3757
3758 /* Count all child controller associations */
3759 list_for_each_entry(handler, &efiobj->protocols, link) {
3760 list_for_each_entry(item, &handler->open_infos, link) {
3761 if (item->info.agent_handle == driver_handle &&
3762 item->info.attributes &
3763 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3764 ++count;
3765 }
3766 }
3767 /*
3768 * Create buffer. In case of duplicate child controller assignments
3769 * the buffer will be too large. But that does not harm.
3770 */
3771 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003772 if (!count)
3773 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003774 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3775 if (!*child_handle_buffer)
3776 return EFI_OUT_OF_RESOURCES;
3777 /* Copy unique child handles */
3778 list_for_each_entry(handler, &efiobj->protocols, link) {
3779 list_for_each_entry(item, &handler->open_infos, link) {
3780 if (item->info.agent_handle == driver_handle &&
3781 item->info.attributes &
3782 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3783 /* Check this is a new child controller */
3784 duplicate = false;
3785 for (i = 0; i < *number_of_children; ++i) {
3786 if ((*child_handle_buffer)[i] ==
3787 item->info.controller_handle)
3788 duplicate = true;
3789 }
3790 /* Copy handle to buffer */
3791 if (!duplicate) {
3792 i = (*number_of_children)++;
3793 (*child_handle_buffer)[i] =
3794 item->info.controller_handle;
3795 }
3796 }
3797 }
3798 }
3799 return EFI_SUCCESS;
3800}
3801
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003802/**
Mario Six8fac2912018-07-10 08:40:17 +02003803 * efi_disconnect_controller() - disconnect a controller from a driver
3804 * @controller_handle: handle of the controller
3805 * @driver_image_handle: handle of the driver
3806 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003807 *
3808 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003809 *
3810 * See the Unified Extensible Firmware Interface (UEFI) specification for
3811 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003812 *
Mario Six8fac2912018-07-10 08:40:17 +02003813 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003814 */
3815static efi_status_t EFIAPI efi_disconnect_controller(
3816 efi_handle_t controller_handle,
3817 efi_handle_t driver_image_handle,
3818 efi_handle_t child_handle)
3819{
3820 struct efi_driver_binding_protocol *binding_protocol;
3821 efi_handle_t *child_handle_buffer = NULL;
3822 size_t number_of_children = 0;
3823 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003824 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003825 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003826
3827 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3828 child_handle);
3829
3830 efiobj = efi_search_obj(controller_handle);
3831 if (!efiobj) {
3832 r = EFI_INVALID_PARAMETER;
3833 goto out;
3834 }
3835
3836 if (child_handle && !efi_search_obj(child_handle)) {
3837 r = EFI_INVALID_PARAMETER;
3838 goto out;
3839 }
3840
3841 /* If no driver handle is supplied, disconnect all drivers */
3842 if (!driver_image_handle) {
3843 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3844 goto out;
3845 }
3846
3847 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003848 r = efi_get_child_controllers(efiobj,
3849 driver_image_handle,
3850 &number_of_children,
3851 &child_handle_buffer);
3852 if (r != EFI_SUCCESS)
3853 return r;
3854 sole_child = (number_of_children == 1);
3855
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003856 if (child_handle) {
3857 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003858 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003859 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003860 }
3861
3862 /* Get the driver binding protocol */
3863 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3864 &efi_guid_driver_binding_protocol,
3865 (void **)&binding_protocol,
3866 driver_image_handle, NULL,
3867 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003868 if (r != EFI_SUCCESS) {
3869 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003870 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003871 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003872 /* Remove the children */
3873 if (number_of_children) {
3874 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3875 controller_handle,
3876 number_of_children,
3877 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003878 if (r != EFI_SUCCESS) {
3879 r = EFI_DEVICE_ERROR;
3880 goto out;
3881 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003882 }
3883 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003884 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003885 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3886 controller_handle,
3887 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003888 if (r != EFI_SUCCESS) {
3889 r = EFI_DEVICE_ERROR;
3890 goto out;
3891 }
3892 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003893 efi_close_protocol(driver_image_handle,
3894 &efi_guid_driver_binding_protocol,
3895 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003896 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003897out:
3898 if (!child_handle)
3899 free(child_handle_buffer);
3900 return EFI_EXIT(r);
3901}
3902
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003903static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003904 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003905 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3906 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003907 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003908 },
3909 .raise_tpl = efi_raise_tpl,
3910 .restore_tpl = efi_restore_tpl,
3911 .allocate_pages = efi_allocate_pages_ext,
3912 .free_pages = efi_free_pages_ext,
3913 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003914 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003915 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003916 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003917 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003918 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003919 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003920 .close_event = efi_close_event,
3921 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003922 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003923 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003924 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003925 .handle_protocol = efi_handle_protocol,
3926 .reserved = NULL,
3927 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003928 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003929 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003930 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003931 .load_image = efi_load_image,
3932 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003933 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003934 .unload_image = efi_unload_image,
3935 .exit_boot_services = efi_exit_boot_services,
3936 .get_next_monotonic_count = efi_get_next_monotonic_count,
3937 .stall = efi_stall,
3938 .set_watchdog_timer = efi_set_watchdog_timer,
3939 .connect_controller = efi_connect_controller,
3940 .disconnect_controller = efi_disconnect_controller,
3941 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003942 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003943 .open_protocol_information = efi_open_protocol_information,
3944 .protocols_per_handle = efi_protocols_per_handle,
3945 .locate_handle_buffer = efi_locate_handle_buffer,
3946 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003947 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003948 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003949 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003950 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003951 .calculate_crc32 = efi_calculate_crc32,
3952 .copy_mem = efi_copy_mem,
3953 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003954 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003955};
3956
Simon Glass90975372022-01-23 12:55:12 -07003957static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003958
Alexander Graf393dd912016-10-14 13:45:30 +02003959struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003960 .hdr = {
3961 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003962 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003963 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003964 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003965 .fw_vendor = firmware_vendor,
3966 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003967 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003968 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003969 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003970};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003971
3972/**
3973 * efi_initialize_system_table() - Initialize system table
3974 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003975 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003976 */
3977efi_status_t efi_initialize_system_table(void)
3978{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003979 efi_status_t ret;
3980
3981 /* Allocate configuration table array */
3982 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3983 EFI_MAX_CONFIGURATION_TABLES *
3984 sizeof(struct efi_configuration_table),
3985 (void **)&systab.tables);
3986
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003987 /*
3988 * These entries will be set to NULL in ExitBootServices(). To avoid
3989 * relocation in SetVirtualAddressMap(), set them dynamically.
3990 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01003991 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003992 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003993 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003994 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003995 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003996 systab.std_err = &efi_con_out;
3997 systab.boottime = &efi_boot_services;
3998
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003999 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004000 efi_update_table_header_crc32(&systab.hdr);
4001 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4002 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004003
4004 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004005}