blob: 052fe481e473a5067bb3c40b22e6592b096c4d5b [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 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300578static efi_status_t efi_remove_protocol(const efi_handle_t handle,
579 const efi_guid_t *protocol,
580 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581{
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 Schuchardt7538c272017-10-26 19:25:56 +02001360 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001361 struct efi_open_protocol_info_item *item;
1362 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001363 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001364
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001365 /* Find the protocol on the handle */
1366 r = efi_search_protocol(handle, protocol, &handler);
1367 if (r != EFI_SUCCESS)
1368 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001369 if (handler->protocol_interface != protocol_interface)
1370 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001371 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001372 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001373 if (r != EFI_SUCCESS) {
1374 r = EFI_ACCESS_DENIED;
1375 goto out;
1376 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001377 /* Close protocol */
1378 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1379 if (item->info.attributes ==
1380 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1381 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1382 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001383 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001384 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001385 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001386 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001387 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001388 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001389 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001390 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001391 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001392out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001393 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001394}
1395
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001396/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001397 * efi_uninstall_protocol_interface() - uninstall protocol interface
1398 * @handle: handle from which the protocol shall be removed
1399 * @protocol: GUID of the protocol to be removed
1400 * @protocol_interface: interface to be removed
1401 *
1402 * This function implements the UninstallProtocolInterface service.
1403 *
1404 * See the Unified Extensible Firmware Interface (UEFI) specification for
1405 * details.
1406 *
1407 * Return: status code
1408 */
1409static efi_status_t EFIAPI efi_uninstall_protocol_interface
1410 (efi_handle_t handle, const efi_guid_t *protocol,
1411 void *protocol_interface)
1412{
1413 efi_status_t ret;
1414
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001415 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001416
1417 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1418 if (ret != EFI_SUCCESS)
1419 goto out;
1420
1421 /* If the last protocol has been removed, delete the handle. */
1422 if (list_empty(&handle->protocols)) {
1423 list_del(&handle->link);
1424 free(handle);
1425 }
1426out:
1427 return EFI_EXIT(ret);
1428}
1429
1430/**
Mario Six8fac2912018-07-10 08:40:17 +02001431 * efi_register_protocol_notify() - register an event for notification when a
1432 * protocol is installed.
1433 * @protocol: GUID of the protocol whose installation shall be notified
1434 * @event: event to be signaled upon installation of the protocol
1435 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001436 *
1437 * This function implements the RegisterProtocolNotify service.
1438 * See the Unified Extensible Firmware Interface (UEFI) specification
1439 * for details.
1440 *
Mario Six8fac2912018-07-10 08:40:17 +02001441 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001442 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001443efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1444 struct efi_event *event,
1445 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001446{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001447 struct efi_register_notify_event *item;
1448 efi_status_t ret = EFI_SUCCESS;
1449
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001450 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001451
1452 if (!protocol || !event || !registration) {
1453 ret = EFI_INVALID_PARAMETER;
1454 goto out;
1455 }
1456
1457 item = calloc(1, sizeof(struct efi_register_notify_event));
1458 if (!item) {
1459 ret = EFI_OUT_OF_RESOURCES;
1460 goto out;
1461 }
1462
1463 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301464 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001465 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001466
1467 list_add_tail(&item->link, &efi_register_notify_events);
1468
1469 *registration = item;
1470out:
1471 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001472}
1473
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001474/**
Mario Six8fac2912018-07-10 08:40:17 +02001475 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001476 *
Mario Six8fac2912018-07-10 08:40:17 +02001477 * @search_type: selection criterion
1478 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001479 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001480 *
1481 * See the documentation of the LocateHandle service in the UEFI specification.
1482 *
Mario Six8fac2912018-07-10 08:40:17 +02001483 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001484 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001485static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001486 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001487{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001488 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001489
1490 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001491 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001492 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001493 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001494 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001495 return (ret != EFI_SUCCESS);
1496 default:
1497 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001498 return -1;
1499 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001500}
1501
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001502/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001503 * efi_check_register_notify_event() - check if registration key is valid
1504 *
1505 * Check that a pointer is a valid registration key as returned by
1506 * RegisterProtocolNotify().
1507 *
1508 * @key: registration key
1509 * Return: valid registration key or NULL
1510 */
1511static struct efi_register_notify_event *efi_check_register_notify_event
1512 (void *key)
1513{
1514 struct efi_register_notify_event *event;
1515
1516 list_for_each_entry(event, &efi_register_notify_events, link) {
1517 if (event == (struct efi_register_notify_event *)key)
1518 return event;
1519 }
1520 return NULL;
1521}
1522
1523/**
Mario Six8fac2912018-07-10 08:40:17 +02001524 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001525 *
1526 * @search_type: selection criterion
1527 * @protocol: GUID of the protocol
1528 * @search_key: registration key
1529 * @buffer_size: size of the buffer to receive the handles in bytes
1530 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001531 *
1532 * This function is meant for U-Boot internal calls. For the API implementation
1533 * of the LocateHandle service see efi_locate_handle_ext.
1534 *
Mario Six8fac2912018-07-10 08:40:17 +02001535 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001536 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001537static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001538 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001539 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001540 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001541{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001542 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001543 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001544 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001545 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001546
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001547 /* Check parameters */
1548 switch (search_type) {
1549 case ALL_HANDLES:
1550 break;
1551 case BY_REGISTER_NOTIFY:
1552 if (!search_key)
1553 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001554 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001555 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001556 if (!event)
1557 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001558 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001559 case BY_PROTOCOL:
1560 if (!protocol)
1561 return EFI_INVALID_PARAMETER;
1562 break;
1563 default:
1564 return EFI_INVALID_PARAMETER;
1565 }
1566
Alexander Grafc15d9212016-03-04 01:09:59 +01001567 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001568 if (search_type == BY_REGISTER_NOTIFY) {
1569 if (list_empty(&event->handles))
1570 return EFI_NOT_FOUND;
1571 handle = list_first_entry(&event->handles,
1572 struct efi_protocol_notification,
1573 link);
1574 efiobj = handle->handle;
1575 size += sizeof(void *);
1576 } else {
1577 list_for_each_entry(efiobj, &efi_obj_list, link) {
1578 if (!efi_search(search_type, protocol, efiobj))
1579 size += sizeof(void *);
1580 }
1581 if (size == 0)
1582 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001583 }
1584
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001585 if (!buffer_size)
1586 return EFI_INVALID_PARAMETER;
1587
Alexander Grafc15d9212016-03-04 01:09:59 +01001588 if (*buffer_size < size) {
1589 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001590 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001591 }
1592
Rob Clarkcdee3372017-08-06 14:10:07 -04001593 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001594
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001595 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001596 if (!buffer)
1597 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001598
Alexander Grafc15d9212016-03-04 01:09:59 +01001599 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001600 if (search_type == BY_REGISTER_NOTIFY) {
1601 *buffer = efiobj;
1602 list_del(&handle->link);
1603 } else {
1604 list_for_each_entry(efiobj, &efi_obj_list, link) {
1605 if (!efi_search(search_type, protocol, efiobj))
1606 *buffer++ = efiobj;
1607 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001608 }
1609
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001610 return EFI_SUCCESS;
1611}
1612
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001613/**
Mario Six8fac2912018-07-10 08:40:17 +02001614 * efi_locate_handle_ext() - locate handles implementing a protocol.
1615 * @search_type: selection criterion
1616 * @protocol: GUID of the protocol
1617 * @search_key: registration key
1618 * @buffer_size: size of the buffer to receive the handles in bytes
1619 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001620 *
1621 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001622 *
Mario Six8fac2912018-07-10 08:40:17 +02001623 * See the Unified Extensible Firmware Interface (UEFI) specification for
1624 * details.
1625 *
1626 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001627 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001628static efi_status_t EFIAPI efi_locate_handle_ext(
1629 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001630 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001631 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001632{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001633 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001634 buffer_size, buffer);
1635
1636 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1637 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001638}
1639
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001640/**
Mario Six8fac2912018-07-10 08:40:17 +02001641 * efi_remove_configuration_table() - collapses configuration table entries,
1642 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001643 *
Mario Six8fac2912018-07-10 08:40:17 +02001644 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001645 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001646static void efi_remove_configuration_table(int i)
1647{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001648 struct efi_configuration_table *this = &systab.tables[i];
1649 struct efi_configuration_table *next = &systab.tables[i + 1];
1650 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001651
1652 memmove(this, next, (ulong)end - (ulong)next);
1653 systab.nr_tables--;
1654}
1655
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001656/**
Mario Six8fac2912018-07-10 08:40:17 +02001657 * efi_install_configuration_table() - adds, updates, or removes a
1658 * configuration table
1659 * @guid: GUID of the installed table
1660 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001661 *
1662 * This function is used for internal calls. For the API implementation of the
1663 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1664 *
Mario Six8fac2912018-07-10 08:40:17 +02001665 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001666 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001667efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1668 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001669{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001670 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001671 int i;
1672
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001673 if (!guid)
1674 return EFI_INVALID_PARAMETER;
1675
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001676 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001677 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001678 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001679 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001680 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001681 else
1682 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001683 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001684 }
1685 }
1686
Alexander Graffe3366f2017-07-26 13:41:04 +02001687 if (!table)
1688 return EFI_NOT_FOUND;
1689
Alexander Grafc15d9212016-03-04 01:09:59 +01001690 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001691 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001692 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001693
1694 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301695 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001696 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001697 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001698
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001699out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001700 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001701 efi_update_table_header_crc32(&systab.hdr);
1702
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001703 /* Notify that the configuration table was changed */
1704 list_for_each_entry(evt, &efi_events, link) {
1705 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001706 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001707 break;
1708 }
1709 }
1710
Alexander Grafc5c11632016-08-19 01:23:24 +02001711 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001712}
1713
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001714/**
Mario Six8fac2912018-07-10 08:40:17 +02001715 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1716 * configuration table.
1717 * @guid: GUID of the installed table
1718 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001719 *
1720 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001721 *
Mario Six8fac2912018-07-10 08:40:17 +02001722 * See the Unified Extensible Firmware Interface (UEFI) specification for
1723 * details.
1724 *
1725 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001726 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001727static efi_status_t
1728EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1729 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001730{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001731 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001732 return EFI_EXIT(efi_install_configuration_table(guid, table));
1733}
1734
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001735/**
Mario Six8fac2912018-07-10 08:40:17 +02001736 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001737 *
1738 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001739 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001740 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001741 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001742 * code is returned.
1743 *
1744 * @device_path: device path of the loaded image
1745 * @file_path: file path of the loaded image
1746 * @handle_ptr: handle of the loaded image
1747 * @info_ptr: loaded image protocol
1748 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001749 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001750efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1751 struct efi_device_path *file_path,
1752 struct efi_loaded_image_obj **handle_ptr,
1753 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001754{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001755 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001756 struct efi_loaded_image *info = NULL;
1757 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001758 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001759
1760 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1761 *handle_ptr = NULL;
1762 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001763
1764 info = calloc(1, sizeof(*info));
1765 if (!info)
1766 return EFI_OUT_OF_RESOURCES;
1767 obj = calloc(1, sizeof(*obj));
1768 if (!obj) {
1769 free(info);
1770 return EFI_OUT_OF_RESOURCES;
1771 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001772 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001773
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001774 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001775 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001776
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001777 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001778 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001779 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001780
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001781 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001782 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001783
1784 dp = efi_dp_append(device_path, file_path);
1785 if (!dp) {
1786 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001787 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001788 }
1789 } else {
1790 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001791 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001792 ret = efi_add_protocol(&obj->header,
1793 &efi_guid_loaded_image_device_path, dp);
1794 if (ret != EFI_SUCCESS)
1795 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001796
1797 /*
1798 * When asking for the loaded_image interface, just
1799 * return handle which points to loaded_image_info
1800 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001801 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001802 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001803 if (ret != EFI_SUCCESS)
1804 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001805
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001806 *info_ptr = info;
1807 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001808
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001809 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001810failure:
1811 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001812 efi_delete_handle(&obj->header);
1813 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001814 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001815}
1816
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001817/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001818 * efi_locate_device_path() - Get the device path and handle of an device
1819 * implementing a protocol
1820 * @protocol: GUID of the protocol
1821 * @device_path: device path
1822 * @device: handle of the device
1823 *
1824 * This function implements the LocateDevicePath service.
1825 *
1826 * See the Unified Extensible Firmware Interface (UEFI) specification for
1827 * details.
1828 *
1829 * Return: status code
1830 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001831efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1832 struct efi_device_path **device_path,
1833 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001834{
1835 struct efi_device_path *dp;
1836 size_t i;
1837 struct efi_handler *handler;
1838 efi_handle_t *handles;
1839 size_t len, len_dp;
1840 size_t len_best = 0;
1841 efi_uintn_t no_handles;
1842 u8 *remainder;
1843 efi_status_t ret;
1844
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001845 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001846
1847 if (!protocol || !device_path || !*device_path) {
1848 ret = EFI_INVALID_PARAMETER;
1849 goto out;
1850 }
1851
1852 /* Find end of device path */
1853 len = efi_dp_instance_size(*device_path);
1854
1855 /* Get all handles implementing the protocol */
1856 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1857 &no_handles, &handles));
1858 if (ret != EFI_SUCCESS)
1859 goto out;
1860
1861 for (i = 0; i < no_handles; ++i) {
1862 /* Find the device path protocol */
1863 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1864 &handler);
1865 if (ret != EFI_SUCCESS)
1866 continue;
1867 dp = (struct efi_device_path *)handler->protocol_interface;
1868 len_dp = efi_dp_instance_size(dp);
1869 /*
1870 * This handle can only be a better fit
1871 * if its device path length is longer than the best fit and
1872 * if its device path length is shorter of equal the searched
1873 * device path.
1874 */
1875 if (len_dp <= len_best || len_dp > len)
1876 continue;
1877 /* Check if dp is a subpath of device_path */
1878 if (memcmp(*device_path, dp, len_dp))
1879 continue;
1880 if (!device) {
1881 ret = EFI_INVALID_PARAMETER;
1882 goto out;
1883 }
1884 *device = handles[i];
1885 len_best = len_dp;
1886 }
1887 if (len_best) {
1888 remainder = (u8 *)*device_path + len_best;
1889 *device_path = (struct efi_device_path *)remainder;
1890 ret = EFI_SUCCESS;
1891 } else {
1892 ret = EFI_NOT_FOUND;
1893 }
1894out:
1895 return EFI_EXIT(ret);
1896}
1897
1898/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001899 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001900 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001901 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1902 * callers obligation to update the memory type as needed.
1903 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001904 * @file_path: the path of the image to load
1905 * @buffer: buffer containing the loaded image
1906 * @size: size of the loaded image
1907 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001908 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001909static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001910efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001911 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001912{
Rob Clark857a1222017-09-13 18:05:35 -04001913 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001914 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001915 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001916 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001917
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001918 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001919 f = efi_file_from_path(file_path);
1920 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001921 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001922
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001923 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001924 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001925 goto error;
1926
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001927 /*
1928 * When reading the file we do not yet know if it contains an
1929 * application, a boottime driver, or a runtime driver. So here we
1930 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1931 * update the reservation according to the image type.
1932 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001933 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1934 EFI_BOOT_SERVICES_DATA,
1935 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001936 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001937 ret = EFI_OUT_OF_RESOURCES;
1938 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001939 }
1940
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001941 /* Read file */
1942 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1943 if (ret != EFI_SUCCESS)
1944 efi_free_pages(addr, efi_size_in_pages(bs));
1945 *buffer = (void *)(uintptr_t)addr;
1946 *size = bs;
1947error:
1948 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001949 return ret;
1950}
1951
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001952/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001953 * efi_load_image_from_path() - load an image using a file path
1954 *
1955 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1956 * callers obligation to update the memory type as needed.
1957 *
1958 * @boot_policy: true for request originating from the boot manager
1959 * @file_path: the path of the image to load
1960 * @buffer: buffer containing the loaded image
1961 * @size: size of the loaded image
1962 * Return: status code
1963 */
1964static
1965efi_status_t efi_load_image_from_path(bool boot_policy,
1966 struct efi_device_path *file_path,
1967 void **buffer, efi_uintn_t *size)
1968{
1969 efi_handle_t device;
1970 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001971 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001972 struct efi_load_file_protocol *load_file_protocol = NULL;
1973 efi_uintn_t buffer_size;
1974 uint64_t addr, pages;
1975 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001976 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001977
1978 /* In case of failure nothing is returned */
1979 *buffer = NULL;
1980 *size = 0;
1981
1982 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001983 device = efi_dp_find_obj(dp, NULL, &rem);
1984 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1985 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001986 if (ret == EFI_SUCCESS)
1987 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001988
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001989 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001990 if (ret == EFI_SUCCESS) {
1991 guid = &efi_guid_load_file_protocol;
1992 } else if (!boot_policy) {
1993 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001994 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001995 }
1996 if (ret != EFI_SUCCESS)
1997 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001998 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001999 if (ret != EFI_SUCCESS)
2000 return EFI_NOT_FOUND;
2001 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002002 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002003 ret = EFI_CALL(load_file_protocol->load_file(
2004 load_file_protocol, rem, boot_policy,
2005 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002006 if (ret != EFI_BUFFER_TOO_SMALL)
2007 goto out;
2008 pages = efi_size_in_pages(buffer_size);
2009 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2010 pages, &addr);
2011 if (ret != EFI_SUCCESS) {
2012 ret = EFI_OUT_OF_RESOURCES;
2013 goto out;
2014 }
2015 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002016 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002017 &buffer_size, (void *)(uintptr_t)addr));
2018 if (ret != EFI_SUCCESS)
2019 efi_free_pages(addr, pages);
2020out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002021 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002022 if (ret == EFI_SUCCESS) {
2023 *buffer = (void *)(uintptr_t)addr;
2024 *size = buffer_size;
2025 }
2026
2027 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002028}
2029
2030/**
Mario Six8fac2912018-07-10 08:40:17 +02002031 * efi_load_image() - load an EFI image into memory
2032 * @boot_policy: true for request originating from the boot manager
2033 * @parent_image: the caller's image handle
2034 * @file_path: the path of the image to load
2035 * @source_buffer: memory location from which the image is installed
2036 * @source_size: size of the memory area from which the image is installed
2037 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002038 *
2039 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002040 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002041 * See the Unified Extensible Firmware Interface (UEFI) specification
2042 * for details.
2043 *
Mario Six8fac2912018-07-10 08:40:17 +02002044 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002045 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002046efi_status_t EFIAPI efi_load_image(bool boot_policy,
2047 efi_handle_t parent_image,
2048 struct efi_device_path *file_path,
2049 void *source_buffer,
2050 efi_uintn_t source_size,
2051 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002052{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002053 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002054 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002055 struct efi_loaded_image_obj **image_obj =
2056 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002057 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002058 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002059
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002060 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002061 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002062
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002063 if (!image_handle || (!source_buffer && !file_path) ||
2064 !efi_search_obj(parent_image) ||
2065 /* The parent image handle must refer to a loaded image */
2066 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002067 ret = EFI_INVALID_PARAMETER;
2068 goto error;
2069 }
Rob Clark857a1222017-09-13 18:05:35 -04002070
2071 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002072 ret = efi_load_image_from_path(boot_policy, file_path,
2073 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002074 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002075 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002076 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002077 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002078 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002079 /* split file_path which contains both the device and file parts */
2080 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002081 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002082 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002083 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002084 if (!source_buffer)
2085 /* Release buffer to which file was loaded */
2086 efi_free_pages((uintptr_t)dest_buffer,
2087 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002088 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002089 info->system_table = &systab;
2090 info->parent_handle = parent_image;
2091 } else {
2092 /* The image is invalid. Release all associated resources. */
2093 efi_delete_handle(*image_handle);
2094 *image_handle = NULL;
2095 free(info);
2096 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002097error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002098 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002099}
2100
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002101/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002102 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2103 */
2104static void efi_exit_caches(void)
2105{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002106#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002107 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002108 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2109 * caches are enabled.
2110 *
2111 * TODO:
2112 * According to the UEFI spec caches that can be managed via CP15
2113 * operations should be enabled. Caches requiring platform information
2114 * to manage should be disabled. This should not happen in
2115 * ExitBootServices() but before invoking any UEFI binary is invoked.
2116 *
2117 * We want to keep the current workaround while GRUB prior to version
2118 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002119 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002120 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002121#endif
2122}
2123
2124/**
Mario Six8fac2912018-07-10 08:40:17 +02002125 * efi_exit_boot_services() - stop all boot services
2126 * @image_handle: handle of the loaded image
2127 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002128 *
2129 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002130 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002131 * See the Unified Extensible Firmware Interface (UEFI) specification
2132 * for details.
2133 *
Mario Six8fac2912018-07-10 08:40:17 +02002134 * All timer events are disabled. For exit boot services events the
2135 * notification function is called. The boot services are disabled in the
2136 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002137 *
Mario Six8fac2912018-07-10 08:40:17 +02002138 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002139 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002140static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002141 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002142{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002143 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002144 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002145
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002146 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002147
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002148 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002149 if (map_key != efi_memory_map_key) {
2150 ret = EFI_INVALID_PARAMETER;
2151 goto out;
2152 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002153
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002154 /* Check if ExitBootServices has already been called */
2155 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002156 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002157
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002158 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2159 list_for_each_entry(evt, &efi_events, link) {
2160 if (evt->group &&
2161 !guidcmp(evt->group,
2162 &efi_guid_event_group_before_exit_boot_services)) {
2163 efi_signal_event(evt);
2164 break;
2165 }
2166 }
2167
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002168 /* Stop all timer related activities */
2169 timers_enabled = false;
2170
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002171 /* Add related events to the event group */
2172 list_for_each_entry(evt, &efi_events, link) {
2173 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2174 evt->group = &efi_guid_event_group_exit_boot_services;
2175 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002176 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002177 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002178 if (evt->group &&
2179 !guidcmp(evt->group,
2180 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002181 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002182 break;
2183 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002184 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002185
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002186 /* Make sure that notification functions are not called anymore */
2187 efi_tpl = TPL_HIGH_LEVEL;
2188
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002189 /* Notify variable services */
2190 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002191
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002192 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2193 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2194 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2195 list_del(&evt->link);
2196 }
2197
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002198 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002199 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002200 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002201 udc_disconnect();
2202 board_quiesce_devices();
2203 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2204 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002205
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002206 /* Patch out unsupported runtime function */
2207 efi_runtime_detach();
2208
Alexander Graffa5246b2018-11-15 21:23:47 +01002209 /* Fix up caches for EFI payloads if necessary */
2210 efi_exit_caches();
2211
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002212 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002213 systab.con_in_handle = NULL;
2214 systab.con_in = NULL;
2215 systab.con_out_handle = NULL;
2216 systab.con_out = NULL;
2217 systab.stderr_handle = NULL;
2218 systab.std_err = NULL;
2219 systab.boottime = NULL;
2220
2221 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002222 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002223
Alexander Grafc15d9212016-03-04 01:09:59 +01002224 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002225 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002226 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002227out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002228 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2229 if (ret != EFI_SUCCESS)
2230 efi_tcg2_notify_exit_boot_services_failed();
2231 }
2232
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002233 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002234}
2235
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002236/**
Mario Six8fac2912018-07-10 08:40:17 +02002237 * efi_get_next_monotonic_count() - get next value of the counter
2238 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002239 *
2240 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002241 *
2242 * See the Unified Extensible Firmware Interface (UEFI) specification for
2243 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002244 *
Mario Six8fac2912018-07-10 08:40:17 +02002245 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002246 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002247static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2248{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002249 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002250 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002251
Alexander Grafc15d9212016-03-04 01:09:59 +01002252 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002253 if (!count) {
2254 ret = EFI_INVALID_PARAMETER;
2255 goto out;
2256 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002257 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002258 ret = EFI_SUCCESS;
2259out:
2260 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002261}
2262
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002263/**
Mario Six8fac2912018-07-10 08:40:17 +02002264 * efi_stall() - sleep
2265 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002266 *
Mario Six8fac2912018-07-10 08:40:17 +02002267 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002268 *
Mario Six8fac2912018-07-10 08:40:17 +02002269 * See the Unified Extensible Firmware Interface (UEFI) specification for
2270 * details.
2271 *
2272 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002273 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002274static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2275{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002276 u64 end_tick;
2277
Alexander Grafc15d9212016-03-04 01:09:59 +01002278 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002279
2280 end_tick = get_ticks() + usec_to_tick(microseconds);
2281 while (get_ticks() < end_tick)
2282 efi_timer_check();
2283
Alexander Grafc15d9212016-03-04 01:09:59 +01002284 return EFI_EXIT(EFI_SUCCESS);
2285}
2286
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002287/**
Mario Six8fac2912018-07-10 08:40:17 +02002288 * efi_set_watchdog_timer() - reset the watchdog timer
2289 * @timeout: seconds before reset by watchdog
2290 * @watchdog_code: code to be logged when resetting
2291 * @data_size: size of buffer in bytes
2292 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002293 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002294 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002295 *
Mario Six8fac2912018-07-10 08:40:17 +02002296 * See the Unified Extensible Firmware Interface (UEFI) specification for
2297 * details.
2298 *
2299 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002300 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002301static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2302 uint64_t watchdog_code,
2303 unsigned long data_size,
2304 uint16_t *watchdog_data)
2305{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002306 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002307 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002308 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002309}
2310
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002311/**
Mario Six8fac2912018-07-10 08:40:17 +02002312 * efi_close_protocol() - close a protocol
2313 * @handle: handle on which the protocol shall be closed
2314 * @protocol: GUID of the protocol to close
2315 * @agent_handle: handle of the driver
2316 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002317 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002318 * This is the function implementing the CloseProtocol service is for internal
2319 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002320 *
Mario Six8fac2912018-07-10 08:40:17 +02002321 * See the Unified Extensible Firmware Interface (UEFI) specification for
2322 * details.
2323 *
2324 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002325 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002326efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2327 efi_handle_t agent_handle,
2328 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002329{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002330 struct efi_handler *handler;
2331 struct efi_open_protocol_info_item *item;
2332 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002333 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002334
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002335 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002336 (controller_handle && !efi_search_obj(controller_handle)))
2337 return EFI_INVALID_PARAMETER;
2338 ret = efi_search_protocol(handle, protocol, &handler);
2339 if (ret != EFI_SUCCESS)
2340 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002341
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002342 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002343 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2344 if (item->info.agent_handle == agent_handle &&
2345 item->info.controller_handle == controller_handle) {
2346 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002347 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002348 }
2349 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002350
2351 return ret;
2352}
2353
2354/**
2355 * efi_close_protocol_ext() - close a protocol
2356 * @handle: handle on which the protocol shall be closed
2357 * @protocol: GUID of the protocol to close
2358 * @agent_handle: handle of the driver
2359 * @controller_handle: handle of the controller
2360 *
2361 * This function implements the CloseProtocol service.
2362 *
2363 * See the Unified Extensible Firmware Interface (UEFI) specification for
2364 * details.
2365 *
2366 * Return: status code
2367 */
2368static efi_status_t EFIAPI
2369efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2370 efi_handle_t agent_handle,
2371 efi_handle_t controller_handle)
2372{
2373 efi_status_t ret;
2374
2375 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2376 controller_handle);
2377
2378 ret = efi_close_protocol(handle, protocol,
2379 agent_handle, controller_handle);
2380
2381 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002382}
2383
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002384/**
Mario Six8fac2912018-07-10 08:40:17 +02002385 * efi_open_protocol_information() - provide information about then open status
2386 * of a protocol on a handle
2387 * @handle: handle for which the information shall be retrieved
2388 * @protocol: GUID of the protocol
2389 * @entry_buffer: buffer to receive the open protocol information
2390 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002391 *
2392 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002393 *
2394 * See the Unified Extensible Firmware Interface (UEFI) specification for
2395 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002396 *
Mario Six8fac2912018-07-10 08:40:17 +02002397 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002398 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002399static efi_status_t EFIAPI efi_open_protocol_information(
2400 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002401 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002402 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002403{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002404 unsigned long buffer_size;
2405 unsigned long count;
2406 struct efi_handler *handler;
2407 struct efi_open_protocol_info_item *item;
2408 efi_status_t r;
2409
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002410 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002411 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002412
2413 /* Check parameters */
2414 if (!entry_buffer) {
2415 r = EFI_INVALID_PARAMETER;
2416 goto out;
2417 }
2418 r = efi_search_protocol(handle, protocol, &handler);
2419 if (r != EFI_SUCCESS)
2420 goto out;
2421
2422 /* Count entries */
2423 count = 0;
2424 list_for_each_entry(item, &handler->open_infos, link) {
2425 if (item->info.open_count)
2426 ++count;
2427 }
2428 *entry_count = count;
2429 *entry_buffer = NULL;
2430 if (!count) {
2431 r = EFI_SUCCESS;
2432 goto out;
2433 }
2434
2435 /* Copy entries */
2436 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002437 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002438 (void **)entry_buffer);
2439 if (r != EFI_SUCCESS)
2440 goto out;
2441 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2442 if (item->info.open_count)
2443 (*entry_buffer)[--count] = item->info;
2444 }
2445out:
2446 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002447}
2448
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002449/**
Mario Six8fac2912018-07-10 08:40:17 +02002450 * efi_protocols_per_handle() - get protocols installed on a handle
2451 * @handle: handle for which the information is retrieved
2452 * @protocol_buffer: buffer with protocol GUIDs
2453 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002454 *
2455 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002456 *
Mario Six8fac2912018-07-10 08:40:17 +02002457 * See the Unified Extensible Firmware Interface (UEFI) specification for
2458 * details.
2459 *
2460 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002461 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002462static efi_status_t EFIAPI efi_protocols_per_handle(
2463 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002464 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002465{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002466 unsigned long buffer_size;
2467 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002468 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002469 efi_status_t r;
2470
Alexander Grafc15d9212016-03-04 01:09:59 +01002471 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2472 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002473
2474 if (!handle || !protocol_buffer || !protocol_buffer_count)
2475 return EFI_EXIT(EFI_INVALID_PARAMETER);
2476
2477 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002478 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002479
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002480 efiobj = efi_search_obj(handle);
2481 if (!efiobj)
2482 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002483
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002484 /* Count protocols */
2485 list_for_each(protocol_handle, &efiobj->protocols) {
2486 ++*protocol_buffer_count;
2487 }
2488
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002489 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002490 if (*protocol_buffer_count) {
2491 size_t j = 0;
2492
2493 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002494 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002495 (void **)protocol_buffer);
2496 if (r != EFI_SUCCESS)
2497 return EFI_EXIT(r);
2498 list_for_each(protocol_handle, &efiobj->protocols) {
2499 struct efi_handler *protocol;
2500
2501 protocol = list_entry(protocol_handle,
2502 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002503 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002504 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002505 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002506 }
2507
2508 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002509}
2510
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002511efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2512 const efi_guid_t *protocol, void *search_key,
2513 efi_uintn_t *no_handles, efi_handle_t **buffer)
2514{
2515 efi_status_t r;
2516 efi_uintn_t buffer_size = 0;
2517
2518 if (!no_handles || !buffer) {
2519 r = EFI_INVALID_PARAMETER;
2520 goto out;
2521 }
2522 *no_handles = 0;
2523 *buffer = NULL;
2524 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2525 *buffer);
2526 if (r != EFI_BUFFER_TOO_SMALL)
2527 goto out;
2528 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2529 (void **)buffer);
2530 if (r != EFI_SUCCESS)
2531 goto out;
2532 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2533 *buffer);
2534 if (r == EFI_SUCCESS)
2535 *no_handles = buffer_size / sizeof(efi_handle_t);
2536out:
2537 return r;
2538}
2539
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002540/**
Mario Six8fac2912018-07-10 08:40:17 +02002541 * efi_locate_handle_buffer() - locate handles implementing a protocol
2542 * @search_type: selection criterion
2543 * @protocol: GUID of the protocol
2544 * @search_key: registration key
2545 * @no_handles: number of returned handles
2546 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002547 *
2548 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002549 *
Mario Six8fac2912018-07-10 08:40:17 +02002550 * See the Unified Extensible Firmware Interface (UEFI) specification for
2551 * details.
2552 *
2553 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002554 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002555efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002556 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002557 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002558 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002559{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002560 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002561
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002562 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002563 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002564
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002565 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2566 no_handles, buffer);
2567
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002568 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002569}
2570
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002571/**
Mario Six8fac2912018-07-10 08:40:17 +02002572 * efi_locate_protocol() - find an interface implementing a protocol
2573 * @protocol: GUID of the protocol
2574 * @registration: registration key passed to the notification function
2575 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002576 *
2577 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002578 *
2579 * See the Unified Extensible Firmware Interface (UEFI) specification for
2580 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002581 *
Mario Six8fac2912018-07-10 08:40:17 +02002582 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002583 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002584static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002585 void *registration,
2586 void **protocol_interface)
2587{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002588 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002589 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002590 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002591
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002592 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002593
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002594 /*
2595 * The UEFI spec explicitly requires a protocol even if a registration
2596 * key is provided. This differs from the logic in LocateHandle().
2597 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002598 if (!protocol || !protocol_interface)
2599 return EFI_EXIT(EFI_INVALID_PARAMETER);
2600
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002601 if (registration) {
2602 struct efi_register_notify_event *event;
2603 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002604
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002605 event = efi_check_register_notify_event(registration);
2606 if (!event)
2607 return EFI_EXIT(EFI_INVALID_PARAMETER);
2608 /*
2609 * The UEFI spec requires to return EFI_NOT_FOUND if no
2610 * protocol instance matches protocol and registration.
2611 * So let's do the same for a mismatch between protocol and
2612 * registration.
2613 */
2614 if (guidcmp(&event->protocol, protocol))
2615 goto not_found;
2616 if (list_empty(&event->handles))
2617 goto not_found;
2618 handle = list_first_entry(&event->handles,
2619 struct efi_protocol_notification,
2620 link);
2621 efiobj = handle->handle;
2622 list_del(&handle->link);
2623 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002624 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002625 if (ret == EFI_SUCCESS)
2626 goto found;
2627 } else {
2628 list_for_each_entry(efiobj, &efi_obj_list, link) {
2629 ret = efi_search_protocol(efiobj, protocol, &handler);
2630 if (ret == EFI_SUCCESS)
2631 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002632 }
2633 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002634not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002635 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002636 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002637found:
2638 *protocol_interface = handler->protocol_interface;
2639 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002640}
2641
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002642/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002643 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002644 * interfaces
2645 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002646 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002647 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002648 * Core functionality of efi_install_multiple_protocol_interfaces
2649 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002650 *
Mario Six8fac2912018-07-10 08:40:17 +02002651 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002652 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002653static efi_status_t EFIAPI
2654efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2655 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002656{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002657 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002658 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002659 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002660 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002661 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002662 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002663
2664 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002665 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002666
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002667 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002668 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002669 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002670 if (!protocol)
2671 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002672 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002673 /* Check that a device path has not been installed before */
2674 if (!guidcmp(protocol, &efi_guid_device_path)) {
2675 struct efi_device_path *dp = protocol_interface;
2676
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002677 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2678 &old_handle));
2679 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002680 dp->type == DEVICE_PATH_TYPE_END) {
2681 EFI_PRINT("Path %pD already installed\n",
2682 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002683 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002684 break;
2685 }
2686 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002687 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2688 EFI_NATIVE_INTERFACE,
2689 protocol_interface));
2690 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002691 break;
2692 i++;
2693 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002694 if (ret == EFI_SUCCESS)
2695 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002696
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002697 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002698 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002699 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2700 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002701 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002702 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002703 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002704
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002705out:
2706 efi_va_end(argptr_copy);
2707 return ret;
2708
Alexander Grafc15d9212016-03-04 01:09:59 +01002709}
2710
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002711/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002712 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2713 * interfaces
2714 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002715 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2716 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002717 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002718 *
2719 * This is the function for internal usage in U-Boot. For the API function
2720 * implementing the InstallMultipleProtocol service see
2721 * efi_install_multiple_protocol_interfaces_ext()
2722 *
2723 * Return: status code
2724 */
2725efi_status_t EFIAPI
2726efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2727{
2728 efi_status_t ret;
2729 efi_va_list argptr;
2730
2731 efi_va_start(argptr, handle);
2732 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2733 efi_va_end(argptr);
2734 return ret;
2735}
2736
2737/**
2738 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2739 * interfaces
2740 * @handle: handle on which the protocol interfaces shall be installed
2741 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2742 * interfaces
2743 *
2744 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002745 *
Mario Six8fac2912018-07-10 08:40:17 +02002746 * See the Unified Extensible Firmware Interface (UEFI) specification for
2747 * details.
2748 *
2749 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002750 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002751static efi_status_t EFIAPI
2752efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002753{
2754 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002755 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002756 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002757
2758 efi_va_start(argptr, handle);
2759 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2760 efi_va_end(argptr);
2761 return EFI_EXIT(ret);
2762}
2763
2764/**
2765 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2766 * multiple protocol
2767 * interfaces
2768 * @handle: handle from which the protocol interfaces shall be removed
2769 * @argptr: va_list of args
2770 *
2771 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2772 * Must not be called directly
2773 *
2774 * Return: status code
2775 */
2776static efi_status_t EFIAPI
2777efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2778 efi_va_list argptr)
2779{
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002780 const efi_guid_t *protocol;
2781 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002782 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002783 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002784 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002785
2786 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002787 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002788
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002789 efi_va_copy(argptr_copy, argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002790 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002791 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002792 if (!protocol)
2793 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002794 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002795 ret = efi_uninstall_protocol(handle, protocol,
2796 protocol_interface);
2797 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002798 break;
2799 i++;
2800 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002801 if (ret == EFI_SUCCESS) {
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002802 /* If the last protocol has been removed, delete the handle. */
2803 if (list_empty(&handle->protocols)) {
2804 list_del(&handle->link);
2805 free(handle);
2806 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002807 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002808 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002809
2810 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002811 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002812 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2813 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002814 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2815 EFI_NATIVE_INTERFACE,
2816 protocol_interface));
2817 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002818 /*
2819 * If any errors are generated while the protocol interfaces are being
2820 * uninstalled, then the protocols uninstalled prior to the error will
2821 * be reinstalled using InstallProtocolInterface() and the status code
2822 * EFI_INVALID_PARAMETER is returned.
2823 */
2824 ret = EFI_INVALID_PARAMETER;
2825
2826out:
2827 efi_va_end(argptr_copy);
2828 return ret;
2829}
2830
2831/**
2832 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2833 * interfaces
2834 * @handle: handle from which the protocol interfaces shall be removed
2835 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2836 * interfaces
2837 *
2838 * This function implements the UninstallMultipleProtocolInterfaces service.
2839 *
2840 * This is the function for internal usage in U-Boot. For the API function
2841 * implementing the UninstallMultipleProtocolInterfaces service see
2842 * efi_uninstall_multiple_protocol_interfaces_ext()
2843 *
2844 * Return: status code
2845 */
2846efi_status_t EFIAPI
2847efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2848{
2849 efi_status_t ret;
2850 efi_va_list argptr;
2851
2852 efi_va_start(argptr, handle);
2853 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002854 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002855 return ret;
2856}
2857
2858/**
2859 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2860 * interfaces
2861 * @handle: handle from which the protocol interfaces shall be removed
2862 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2863 * interfaces
2864 *
2865 * This function implements the UninstallMultipleProtocolInterfaces service.
2866 *
2867 * See the Unified Extensible Firmware Interface (UEFI) specification for
2868 * details.
2869 *
2870 * Return: status code
2871 */
2872static efi_status_t EFIAPI
2873efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2874{
2875 EFI_ENTRY("%p", handle);
2876 efi_status_t ret;
2877 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002878
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002879 efi_va_start(argptr, handle);
2880 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2881 efi_va_end(argptr);
2882 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002883}
2884
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002885/**
Mario Six8fac2912018-07-10 08:40:17 +02002886 * efi_calculate_crc32() - calculate cyclic redundancy code
2887 * @data: buffer with data
2888 * @data_size: size of buffer in bytes
2889 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002890 *
2891 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002892 *
2893 * See the Unified Extensible Firmware Interface (UEFI) specification for
2894 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002895 *
Mario Six8fac2912018-07-10 08:40:17 +02002896 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002897 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002898static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2899 efi_uintn_t data_size,
2900 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002901{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002902 efi_status_t ret = EFI_SUCCESS;
2903
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002904 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002905 if (!data || !data_size || !crc32_p) {
2906 ret = EFI_INVALID_PARAMETER;
2907 goto out;
2908 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002909 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002910out:
2911 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002912}
2913
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002914/**
Mario Six8fac2912018-07-10 08:40:17 +02002915 * efi_copy_mem() - copy memory
2916 * @destination: destination of the copy operation
2917 * @source: source of the copy operation
2918 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002919 *
2920 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002921 *
Mario Six8fac2912018-07-10 08:40:17 +02002922 * See the Unified Extensible Firmware Interface (UEFI) specification for
2923 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002924 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002925static void EFIAPI efi_copy_mem(void *destination, const void *source,
2926 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002927{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002928 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002929 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002930 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002931}
2932
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002933/**
Mario Six8fac2912018-07-10 08:40:17 +02002934 * efi_set_mem() - Fill memory with a byte value.
2935 * @buffer: buffer to fill
2936 * @size: size of buffer in bytes
2937 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002938 *
2939 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002940 *
Mario Six8fac2912018-07-10 08:40:17 +02002941 * See the Unified Extensible Firmware Interface (UEFI) specification for
2942 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002943 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002944static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002945{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002946 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002947 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002948 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002949}
2950
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002951/**
Mario Six8fac2912018-07-10 08:40:17 +02002952 * efi_protocol_open() - open protocol interface on a handle
2953 * @handler: handler of a protocol
2954 * @protocol_interface: interface implementing the protocol
2955 * @agent_handle: handle of the driver
2956 * @controller_handle: handle of the controller
2957 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002958 *
Mario Six8fac2912018-07-10 08:40:17 +02002959 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002960 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002961efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002962 struct efi_handler *handler,
2963 void **protocol_interface, void *agent_handle,
2964 void *controller_handle, uint32_t attributes)
2965{
2966 struct efi_open_protocol_info_item *item;
2967 struct efi_open_protocol_info_entry *match = NULL;
2968 bool opened_by_driver = false;
2969 bool opened_exclusive = false;
2970
2971 /* If there is no agent, only return the interface */
2972 if (!agent_handle)
2973 goto out;
2974
2975 /* For TEST_PROTOCOL ignore interface attribute */
2976 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2977 *protocol_interface = NULL;
2978
2979 /*
2980 * Check if the protocol is already opened by a driver with the same
2981 * attributes or opened exclusively
2982 */
2983 list_for_each_entry(item, &handler->open_infos, link) {
2984 if (item->info.agent_handle == agent_handle) {
2985 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2986 (item->info.attributes == attributes))
2987 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002988 } else {
2989 if (item->info.attributes &
2990 EFI_OPEN_PROTOCOL_BY_DRIVER)
2991 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002992 }
2993 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2994 opened_exclusive = true;
2995 }
2996
2997 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002998 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2999 if (opened_exclusive)
3000 return EFI_ACCESS_DENIED;
3001 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3002 if (opened_exclusive || opened_by_driver)
3003 return EFI_ACCESS_DENIED;
3004 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003005
3006 /* Prepare exclusive opening */
3007 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3008 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003009disconnect_next:
3010 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003011 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003012 efi_status_t ret;
3013
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003014 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003015 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3016 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003017 item->info.controller_handle,
3018 item->info.agent_handle,
3019 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003020 if (ret == EFI_SUCCESS)
3021 /*
3022 * Child controllers may have been
3023 * removed from the open_infos list. So
3024 * let's restart the loop.
3025 */
3026 goto disconnect_next;
3027 else
3028 opened_by_driver = true;
3029 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003030 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003031 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003032 if (opened_by_driver)
3033 return EFI_ACCESS_DENIED;
3034 }
3035
3036 /* Find existing entry */
3037 list_for_each_entry(item, &handler->open_infos, link) {
3038 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003039 item->info.controller_handle == controller_handle &&
3040 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003041 match = &item->info;
3042 }
3043 /* None found, create one */
3044 if (!match) {
3045 match = efi_create_open_info(handler);
3046 if (!match)
3047 return EFI_OUT_OF_RESOURCES;
3048 }
3049
3050 match->agent_handle = agent_handle;
3051 match->controller_handle = controller_handle;
3052 match->attributes = attributes;
3053 match->open_count++;
3054
3055out:
3056 /* For TEST_PROTOCOL ignore interface attribute. */
3057 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3058 *protocol_interface = handler->protocol_interface;
3059
3060 return EFI_SUCCESS;
3061}
3062
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003063/**
Mario Six8fac2912018-07-10 08:40:17 +02003064 * efi_open_protocol() - open protocol interface on a handle
3065 * @handle: handle on which the protocol shall be opened
3066 * @protocol: GUID of the protocol
3067 * @protocol_interface: interface implementing the protocol
3068 * @agent_handle: handle of the driver
3069 * @controller_handle: handle of the controller
3070 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003071 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003072 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003073 *
Mario Six8fac2912018-07-10 08:40:17 +02003074 * See the Unified Extensible Firmware Interface (UEFI) specification for
3075 * details.
3076 *
3077 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003078 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003079static efi_status_t EFIAPI efi_open_protocol
3080 (efi_handle_t handle, const efi_guid_t *protocol,
3081 void **protocol_interface, efi_handle_t agent_handle,
3082 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003083{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003084 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003085 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003086
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003087 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003088 protocol_interface, agent_handle, controller_handle,
3089 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003090
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003091 if (!handle || !protocol ||
3092 (!protocol_interface && attributes !=
3093 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003094 goto out;
3095 }
3096
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003097 switch (attributes) {
3098 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3099 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3100 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3101 break;
3102 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3103 if (controller_handle == handle)
3104 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003105 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003106 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3107 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003108 /* Check that the controller handle is valid */
3109 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003110 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003111 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003112 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003113 /* Check that the agent handle is valid */
3114 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003115 goto out;
3116 break;
3117 default:
3118 goto out;
3119 }
3120
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003121 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003122 switch (r) {
3123 case EFI_SUCCESS:
3124 break;
3125 case EFI_NOT_FOUND:
3126 r = EFI_UNSUPPORTED;
3127 goto out;
3128 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003129 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003130 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003131
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003132 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3133 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003134out:
3135 return EFI_EXIT(r);
3136}
3137
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003138/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003139 * efi_start_image() - call the entry point of an image
3140 * @image_handle: handle of the image
3141 * @exit_data_size: size of the buffer
3142 * @exit_data: buffer to receive the exit data of the called image
3143 *
3144 * This function implements the StartImage service.
3145 *
3146 * See the Unified Extensible Firmware Interface (UEFI) specification for
3147 * details.
3148 *
3149 * Return: status code
3150 */
3151efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3152 efi_uintn_t *exit_data_size,
3153 u16 **exit_data)
3154{
3155 struct efi_loaded_image_obj *image_obj =
3156 (struct efi_loaded_image_obj *)image_handle;
3157 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003158 void *info;
3159 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003160 efi_status_t exit_status;
3161 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003162
3163 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3164
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003165 if (!efi_search_obj(image_handle))
3166 return EFI_EXIT(EFI_INVALID_PARAMETER);
3167
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003168 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003169 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3170 return EFI_EXIT(EFI_INVALID_PARAMETER);
3171
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003172 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3173 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3174
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003175 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3176 &info, NULL, NULL,
3177 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3178 if (ret != EFI_SUCCESS)
3179 return EFI_EXIT(EFI_INVALID_PARAMETER);
3180
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003181 image_obj->exit_data_size = exit_data_size;
3182 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003183 image_obj->exit_status = &exit_status;
3184 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003185
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003186 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3187 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003188 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003189 if (ret == EFI_SECURITY_VIOLATION) {
3190 /*
3191 * TCG2 Protocol is installed but no TPM device found,
3192 * this is not expected.
3193 */
3194 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003195 }
3196 }
3197 }
3198
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003199 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003200 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003201 /*
3202 * We called the entry point of the child image with EFI_CALL
3203 * in the lines below. The child image called the Exit() boot
3204 * service efi_exit() which executed the long jump that brought
3205 * us to the current line. This implies that the second half
3206 * of the EFI_CALL macro has not been executed.
3207 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003208#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003209 /*
3210 * efi_exit() called efi_restore_gd(). We have to undo this
3211 * otherwise __efi_entry_check() will put the wrong value into
3212 * app_gd.
3213 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003214 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003215#endif
3216 /*
3217 * To get ready to call EFI_EXIT below we have to execute the
3218 * missed out steps of EFI_CALL.
3219 */
3220 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003221 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003222 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003223 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003224 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003225 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003226 }
3227
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003228 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003229 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003230 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003231 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3232
3233 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003234 * Control is returned from a started UEFI image either by calling
3235 * Exit() (where exit data can be provided) or by simply returning from
3236 * the entry point. In the latter case call Exit() on behalf of the
3237 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003238 */
3239 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3240}
3241
3242/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003243 * efi_delete_image() - delete loaded image from memory)
3244 *
3245 * @image_obj: handle of the loaded image
3246 * @loaded_image_protocol: loaded image protocol
3247 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003248static efi_status_t efi_delete_image
3249 (struct efi_loaded_image_obj *image_obj,
3250 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003251{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003252 struct efi_object *efiobj;
3253 efi_status_t r, ret = EFI_SUCCESS;
3254
3255close_next:
3256 list_for_each_entry(efiobj, &efi_obj_list, link) {
3257 struct efi_handler *protocol;
3258
3259 list_for_each_entry(protocol, &efiobj->protocols, link) {
3260 struct efi_open_protocol_info_item *info;
3261
3262 list_for_each_entry(info, &protocol->open_infos, link) {
3263 if (info->info.agent_handle !=
3264 (efi_handle_t)image_obj)
3265 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003266 r = efi_close_protocol(
3267 efiobj, &protocol->guid,
3268 info->info.agent_handle,
3269 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003270 if (r != EFI_SUCCESS)
3271 ret = r;
3272 /*
3273 * Closing protocols may results in further
3274 * items being deleted. To play it safe loop
3275 * over all elements again.
3276 */
3277 goto close_next;
3278 }
3279 }
3280 }
3281
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003282 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3283 efi_size_in_pages(loaded_image_protocol->image_size));
3284 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003285
3286 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003287}
3288
3289/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003290 * efi_unload_image() - unload an EFI image
3291 * @image_handle: handle of the image to be unloaded
3292 *
3293 * This function implements the UnloadImage service.
3294 *
3295 * See the Unified Extensible Firmware Interface (UEFI) specification for
3296 * details.
3297 *
3298 * Return: status code
3299 */
3300efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3301{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003302 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003303 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003304 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003305
3306 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003307
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003308 efiobj = efi_search_obj(image_handle);
3309 if (!efiobj) {
3310 ret = EFI_INVALID_PARAMETER;
3311 goto out;
3312 }
3313 /* Find the loaded image protocol */
3314 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3315 (void **)&loaded_image_protocol,
3316 NULL, NULL,
3317 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3318 if (ret != EFI_SUCCESS) {
3319 ret = EFI_INVALID_PARAMETER;
3320 goto out;
3321 }
3322 switch (efiobj->type) {
3323 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3324 /* Call the unload function */
3325 if (!loaded_image_protocol->unload) {
3326 ret = EFI_UNSUPPORTED;
3327 goto out;
3328 }
3329 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3330 if (ret != EFI_SUCCESS)
3331 goto out;
3332 break;
3333 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3334 break;
3335 default:
3336 ret = EFI_INVALID_PARAMETER;
3337 goto out;
3338 }
3339 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3340 loaded_image_protocol);
3341out:
3342 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003343}
3344
3345/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003346 * efi_update_exit_data() - fill exit data parameters of StartImage()
3347 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003348 * @image_obj: image handle
3349 * @exit_data_size: size of the exit data buffer
3350 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003351 * Return: status code
3352 */
3353static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3354 efi_uintn_t exit_data_size,
3355 u16 *exit_data)
3356{
3357 efi_status_t ret;
3358
3359 /*
3360 * If exit_data is not provided to StartImage(), exit_data_size must be
3361 * ignored.
3362 */
3363 if (!image_obj->exit_data)
3364 return EFI_SUCCESS;
3365 if (image_obj->exit_data_size)
3366 *image_obj->exit_data_size = exit_data_size;
3367 if (exit_data_size && exit_data) {
3368 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3369 exit_data_size,
3370 (void **)image_obj->exit_data);
3371 if (ret != EFI_SUCCESS)
3372 return ret;
3373 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3374 } else {
3375 image_obj->exit_data = NULL;
3376 }
3377 return EFI_SUCCESS;
3378}
3379
3380/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003381 * efi_exit() - leave an EFI application or driver
3382 * @image_handle: handle of the application or driver that is exiting
3383 * @exit_status: status code
3384 * @exit_data_size: size of the buffer in bytes
3385 * @exit_data: buffer with data describing an error
3386 *
3387 * This function implements the Exit service.
3388 *
3389 * See the Unified Extensible Firmware Interface (UEFI) specification for
3390 * details.
3391 *
3392 * Return: status code
3393 */
3394static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3395 efi_status_t exit_status,
3396 efi_uintn_t exit_data_size,
3397 u16 *exit_data)
3398{
3399 /*
3400 * TODO: We should call the unload procedure of the loaded
3401 * image protocol.
3402 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003403 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003404 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003405 struct efi_loaded_image_obj *image_obj =
3406 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003407 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003408
3409 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3410 exit_data_size, exit_data);
3411
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003412 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003413 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003414 (void **)&loaded_image_protocol,
3415 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003416 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003417 if (ret != EFI_SUCCESS) {
3418 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003419 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003420 }
3421
3422 /* Unloading of unstarted images */
3423 switch (image_obj->header.type) {
3424 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3425 break;
3426 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3427 efi_delete_image(image_obj, loaded_image_protocol);
3428 ret = EFI_SUCCESS;
3429 goto out;
3430 default:
3431 /* Handle does not refer to loaded image */
3432 ret = EFI_INVALID_PARAMETER;
3433 goto out;
3434 }
3435 /* A started image can only be unloaded it is the last one started. */
3436 if (image_handle != current_image) {
3437 ret = EFI_INVALID_PARAMETER;
3438 goto out;
3439 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003440
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003441 /* Exit data is only foreseen in case of failure. */
3442 if (exit_status != EFI_SUCCESS) {
3443 ret = efi_update_exit_data(image_obj, exit_data_size,
3444 exit_data);
3445 /* Exiting has priority. Don't return error to caller. */
3446 if (ret != EFI_SUCCESS)
3447 EFI_PRINT("%s: out of memory\n", __func__);
3448 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003449 /* efi_delete_image() frees image_obj. Copy before the call. */
3450 exit_jmp = image_obj->exit_jmp;
3451 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003452 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3453 exit_status != EFI_SUCCESS)
3454 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003455
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003456 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3457 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3458 ret = efi_tcg2_measure_efi_app_exit();
3459 if (ret != EFI_SUCCESS) {
3460 log_warning("tcg2 measurement fails(0x%lx)\n",
3461 ret);
3462 }
3463 }
3464 }
3465
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003466 /* Make sure entry/exit counts for EFI world cross-overs match */
3467 EFI_EXIT(exit_status);
3468
3469 /*
3470 * But longjmp out with the U-Boot gd, not the application's, as
3471 * the other end is a setjmp call inside EFI context.
3472 */
3473 efi_restore_gd();
3474
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003475 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003476
3477 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003478out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003479 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003480}
3481
3482/**
Mario Six8fac2912018-07-10 08:40:17 +02003483 * efi_handle_protocol() - get interface of a protocol on a handle
3484 * @handle: handle on which the protocol shall be opened
3485 * @protocol: GUID of the protocol
3486 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003487 *
3488 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003489 *
3490 * See the Unified Extensible Firmware Interface (UEFI) specification for
3491 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003492 *
Mario Six8fac2912018-07-10 08:40:17 +02003493 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003494 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003495efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3496 const efi_guid_t *protocol,
3497 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003498{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003499 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003500 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003501}
3502
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003503/**
Mario Six8fac2912018-07-10 08:40:17 +02003504 * efi_bind_controller() - bind a single driver to a controller
3505 * @controller_handle: controller handle
3506 * @driver_image_handle: driver handle
3507 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003508 *
Mario Six8fac2912018-07-10 08:40:17 +02003509 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003510 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003511static efi_status_t efi_bind_controller(
3512 efi_handle_t controller_handle,
3513 efi_handle_t driver_image_handle,
3514 struct efi_device_path *remain_device_path)
3515{
3516 struct efi_driver_binding_protocol *binding_protocol;
3517 efi_status_t r;
3518
3519 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3520 &efi_guid_driver_binding_protocol,
3521 (void **)&binding_protocol,
3522 driver_image_handle, NULL,
3523 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3524 if (r != EFI_SUCCESS)
3525 return r;
3526 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3527 controller_handle,
3528 remain_device_path));
3529 if (r == EFI_SUCCESS)
3530 r = EFI_CALL(binding_protocol->start(binding_protocol,
3531 controller_handle,
3532 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003533 efi_close_protocol(driver_image_handle,
3534 &efi_guid_driver_binding_protocol,
3535 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003536 return r;
3537}
3538
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003539/**
Mario Six8fac2912018-07-10 08:40:17 +02003540 * efi_connect_single_controller() - connect a single driver to a controller
3541 * @controller_handle: controller
3542 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003543 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003544 *
Mario Six8fac2912018-07-10 08:40:17 +02003545 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003546 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003547static efi_status_t efi_connect_single_controller(
3548 efi_handle_t controller_handle,
3549 efi_handle_t *driver_image_handle,
3550 struct efi_device_path *remain_device_path)
3551{
3552 efi_handle_t *buffer;
3553 size_t count;
3554 size_t i;
3555 efi_status_t r;
3556 size_t connected = 0;
3557
3558 /* Get buffer with all handles with driver binding protocol */
3559 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3560 &efi_guid_driver_binding_protocol,
3561 NULL, &count, &buffer));
3562 if (r != EFI_SUCCESS)
3563 return r;
3564
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003565 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003566 if (driver_image_handle) {
3567 for (; *driver_image_handle; ++driver_image_handle) {
3568 for (i = 0; i < count; ++i) {
3569 if (buffer[i] == *driver_image_handle) {
3570 buffer[i] = NULL;
3571 r = efi_bind_controller(
3572 controller_handle,
3573 *driver_image_handle,
3574 remain_device_path);
3575 /*
3576 * For drivers that do not support the
3577 * controller or are already connected
3578 * we receive an error code here.
3579 */
3580 if (r == EFI_SUCCESS)
3581 ++connected;
3582 }
3583 }
3584 }
3585 }
3586
3587 /*
3588 * TODO: Some overrides are not yet implemented:
3589 * - Platform Driver Override
3590 * - Driver Family Override Search
3591 * - Bus Specific Driver Override
3592 */
3593
3594 /* Driver Binding Search */
3595 for (i = 0; i < count; ++i) {
3596 if (buffer[i]) {
3597 r = efi_bind_controller(controller_handle,
3598 buffer[i],
3599 remain_device_path);
3600 if (r == EFI_SUCCESS)
3601 ++connected;
3602 }
3603 }
3604
3605 efi_free_pool(buffer);
3606 if (!connected)
3607 return EFI_NOT_FOUND;
3608 return EFI_SUCCESS;
3609}
3610
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003611/**
Mario Six8fac2912018-07-10 08:40:17 +02003612 * efi_connect_controller() - connect a controller to a driver
3613 * @controller_handle: handle of the controller
3614 * @driver_image_handle: handle of the driver
3615 * @remain_device_path: device path of a child controller
3616 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003617 *
3618 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003619 *
3620 * See the Unified Extensible Firmware Interface (UEFI) specification for
3621 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003622 *
3623 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003624 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003625 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3626 *
Mario Six8fac2912018-07-10 08:40:17 +02003627 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003628 */
3629static efi_status_t EFIAPI efi_connect_controller(
3630 efi_handle_t controller_handle,
3631 efi_handle_t *driver_image_handle,
3632 struct efi_device_path *remain_device_path,
3633 bool recursive)
3634{
3635 efi_status_t r;
3636 efi_status_t ret = EFI_NOT_FOUND;
3637 struct efi_object *efiobj;
3638
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003639 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003640 remain_device_path, recursive);
3641
3642 efiobj = efi_search_obj(controller_handle);
3643 if (!efiobj) {
3644 ret = EFI_INVALID_PARAMETER;
3645 goto out;
3646 }
3647
3648 r = efi_connect_single_controller(controller_handle,
3649 driver_image_handle,
3650 remain_device_path);
3651 if (r == EFI_SUCCESS)
3652 ret = EFI_SUCCESS;
3653 if (recursive) {
3654 struct efi_handler *handler;
3655 struct efi_open_protocol_info_item *item;
3656
3657 list_for_each_entry(handler, &efiobj->protocols, link) {
3658 list_for_each_entry(item, &handler->open_infos, link) {
3659 if (item->info.attributes &
3660 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3661 r = EFI_CALL(efi_connect_controller(
3662 item->info.controller_handle,
3663 driver_image_handle,
3664 remain_device_path,
3665 recursive));
3666 if (r == EFI_SUCCESS)
3667 ret = EFI_SUCCESS;
3668 }
3669 }
3670 }
3671 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003672 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003673 if (ret != EFI_SUCCESS && remain_device_path &&
3674 remain_device_path->type == DEVICE_PATH_TYPE_END)
3675 ret = EFI_SUCCESS;
3676out:
3677 return EFI_EXIT(ret);
3678}
3679
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003680/**
Mario Six8fac2912018-07-10 08:40:17 +02003681 * efi_reinstall_protocol_interface() - reinstall protocol interface
3682 * @handle: handle on which the protocol shall be reinstalled
3683 * @protocol: GUID of the protocol to be installed
3684 * @old_interface: interface to be removed
3685 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003686 *
3687 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003688 *
3689 * See the Unified Extensible Firmware Interface (UEFI) specification for
3690 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003691 *
3692 * The old interface is uninstalled. The new interface is installed.
3693 * Drivers are connected.
3694 *
Mario Six8fac2912018-07-10 08:40:17 +02003695 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003696 */
3697static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3698 efi_handle_t handle, const efi_guid_t *protocol,
3699 void *old_interface, void *new_interface)
3700{
3701 efi_status_t ret;
3702
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003703 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003704 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003705
3706 /* Uninstall protocol but do not delete handle */
3707 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003708 if (ret != EFI_SUCCESS)
3709 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003710
3711 /* Install the new protocol */
3712 ret = efi_add_protocol(handle, protocol, new_interface);
3713 /*
3714 * The UEFI spec does not specify what should happen to the handle
3715 * if in case of an error no protocol interface remains on the handle.
3716 * So let's do nothing here.
3717 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003718 if (ret != EFI_SUCCESS)
3719 goto out;
3720 /*
3721 * The returned status code has to be ignored.
3722 * Do not create an error if no suitable driver for the handle exists.
3723 */
3724 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3725out:
3726 return EFI_EXIT(ret);
3727}
3728
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003729/**
Mario Six8fac2912018-07-10 08:40:17 +02003730 * efi_get_child_controllers() - get all child controllers associated to a driver
3731 * @efiobj: handle of the controller
3732 * @driver_handle: handle of the driver
3733 * @number_of_children: number of child controllers
3734 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003735 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003736 * The allocated buffer has to be freed with free().
3737 *
Mario Six8fac2912018-07-10 08:40:17 +02003738 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003739 */
3740static efi_status_t efi_get_child_controllers(
3741 struct efi_object *efiobj,
3742 efi_handle_t driver_handle,
3743 efi_uintn_t *number_of_children,
3744 efi_handle_t **child_handle_buffer)
3745{
3746 struct efi_handler *handler;
3747 struct efi_open_protocol_info_item *item;
3748 efi_uintn_t count = 0, i;
3749 bool duplicate;
3750
3751 /* Count all child controller associations */
3752 list_for_each_entry(handler, &efiobj->protocols, link) {
3753 list_for_each_entry(item, &handler->open_infos, link) {
3754 if (item->info.agent_handle == driver_handle &&
3755 item->info.attributes &
3756 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3757 ++count;
3758 }
3759 }
3760 /*
3761 * Create buffer. In case of duplicate child controller assignments
3762 * the buffer will be too large. But that does not harm.
3763 */
3764 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003765 if (!count)
3766 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003767 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3768 if (!*child_handle_buffer)
3769 return EFI_OUT_OF_RESOURCES;
3770 /* Copy unique child handles */
3771 list_for_each_entry(handler, &efiobj->protocols, link) {
3772 list_for_each_entry(item, &handler->open_infos, link) {
3773 if (item->info.agent_handle == driver_handle &&
3774 item->info.attributes &
3775 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3776 /* Check this is a new child controller */
3777 duplicate = false;
3778 for (i = 0; i < *number_of_children; ++i) {
3779 if ((*child_handle_buffer)[i] ==
3780 item->info.controller_handle)
3781 duplicate = true;
3782 }
3783 /* Copy handle to buffer */
3784 if (!duplicate) {
3785 i = (*number_of_children)++;
3786 (*child_handle_buffer)[i] =
3787 item->info.controller_handle;
3788 }
3789 }
3790 }
3791 }
3792 return EFI_SUCCESS;
3793}
3794
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003795/**
Mario Six8fac2912018-07-10 08:40:17 +02003796 * efi_disconnect_controller() - disconnect a controller from a driver
3797 * @controller_handle: handle of the controller
3798 * @driver_image_handle: handle of the driver
3799 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003800 *
3801 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003802 *
3803 * See the Unified Extensible Firmware Interface (UEFI) specification for
3804 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003805 *
Mario Six8fac2912018-07-10 08:40:17 +02003806 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003807 */
3808static efi_status_t EFIAPI efi_disconnect_controller(
3809 efi_handle_t controller_handle,
3810 efi_handle_t driver_image_handle,
3811 efi_handle_t child_handle)
3812{
3813 struct efi_driver_binding_protocol *binding_protocol;
3814 efi_handle_t *child_handle_buffer = NULL;
3815 size_t number_of_children = 0;
3816 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003817 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003818 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003819
3820 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3821 child_handle);
3822
3823 efiobj = efi_search_obj(controller_handle);
3824 if (!efiobj) {
3825 r = EFI_INVALID_PARAMETER;
3826 goto out;
3827 }
3828
3829 if (child_handle && !efi_search_obj(child_handle)) {
3830 r = EFI_INVALID_PARAMETER;
3831 goto out;
3832 }
3833
3834 /* If no driver handle is supplied, disconnect all drivers */
3835 if (!driver_image_handle) {
3836 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3837 goto out;
3838 }
3839
3840 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003841 r = efi_get_child_controllers(efiobj,
3842 driver_image_handle,
3843 &number_of_children,
3844 &child_handle_buffer);
3845 if (r != EFI_SUCCESS)
3846 return r;
3847 sole_child = (number_of_children == 1);
3848
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003849 if (child_handle) {
3850 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003851 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003852 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003853 }
3854
3855 /* Get the driver binding protocol */
3856 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3857 &efi_guid_driver_binding_protocol,
3858 (void **)&binding_protocol,
3859 driver_image_handle, NULL,
3860 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003861 if (r != EFI_SUCCESS) {
3862 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003863 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003864 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003865 /* Remove the children */
3866 if (number_of_children) {
3867 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3868 controller_handle,
3869 number_of_children,
3870 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003871 if (r != EFI_SUCCESS) {
3872 r = EFI_DEVICE_ERROR;
3873 goto out;
3874 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003875 }
3876 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003877 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003878 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3879 controller_handle,
3880 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003881 if (r != EFI_SUCCESS) {
3882 r = EFI_DEVICE_ERROR;
3883 goto out;
3884 }
3885 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003886 efi_close_protocol(driver_image_handle,
3887 &efi_guid_driver_binding_protocol,
3888 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003889 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003890out:
3891 if (!child_handle)
3892 free(child_handle_buffer);
3893 return EFI_EXIT(r);
3894}
3895
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003896static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003897 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003898 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3899 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003900 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003901 },
3902 .raise_tpl = efi_raise_tpl,
3903 .restore_tpl = efi_restore_tpl,
3904 .allocate_pages = efi_allocate_pages_ext,
3905 .free_pages = efi_free_pages_ext,
3906 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003907 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003908 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003909 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003910 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003911 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003912 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003913 .close_event = efi_close_event,
3914 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003915 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003916 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003917 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003918 .handle_protocol = efi_handle_protocol,
3919 .reserved = NULL,
3920 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003921 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003922 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003923 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003924 .load_image = efi_load_image,
3925 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003926 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003927 .unload_image = efi_unload_image,
3928 .exit_boot_services = efi_exit_boot_services,
3929 .get_next_monotonic_count = efi_get_next_monotonic_count,
3930 .stall = efi_stall,
3931 .set_watchdog_timer = efi_set_watchdog_timer,
3932 .connect_controller = efi_connect_controller,
3933 .disconnect_controller = efi_disconnect_controller,
3934 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003935 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003936 .open_protocol_information = efi_open_protocol_information,
3937 .protocols_per_handle = efi_protocols_per_handle,
3938 .locate_handle_buffer = efi_locate_handle_buffer,
3939 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003940 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003941 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003942 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003943 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003944 .calculate_crc32 = efi_calculate_crc32,
3945 .copy_mem = efi_copy_mem,
3946 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003947 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003948};
3949
Simon Glass90975372022-01-23 12:55:12 -07003950static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003951
Alexander Graf393dd912016-10-14 13:45:30 +02003952struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003953 .hdr = {
3954 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003955 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003956 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003957 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003958 .fw_vendor = firmware_vendor,
3959 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003960 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003961 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003962 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003963};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003964
3965/**
3966 * efi_initialize_system_table() - Initialize system table
3967 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003968 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003969 */
3970efi_status_t efi_initialize_system_table(void)
3971{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003972 efi_status_t ret;
3973
3974 /* Allocate configuration table array */
3975 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3976 EFI_MAX_CONFIGURATION_TABLES *
3977 sizeof(struct efi_configuration_table),
3978 (void **)&systab.tables);
3979
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003980 /*
3981 * These entries will be set to NULL in ExitBootServices(). To avoid
3982 * relocation in SetVirtualAddressMap(), set them dynamically.
3983 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01003984 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003985 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003986 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003987 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003988 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003989 systab.std_err = &efi_con_out;
3990 systab.boottime = &efi_boot_services;
3991
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003992 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003993 efi_update_table_header_crc32(&systab.hdr);
3994 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3995 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003996
3997 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003998}