blob: 0e89c8505b11e8e4ad833c1bb7dae02f1eb7474d [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
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +030062static efi_status_t efi_uninstall_protocol
63 (efi_handle_t handle, const efi_guid_t *protocol,
64 void *protocol_interface);
65
Heinrich Schuchardt72506232019-02-09 14:10:39 +010066/* 1 if inside U-Boot code, 0 if inside EFI payload code */
67static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040068static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010069/* GUID of the device tree table */
70const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010071/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
72const efi_guid_t efi_guid_driver_binding_protocol =
73 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040074
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010075/* event group ExitBootServices() invoked */
76const efi_guid_t efi_guid_event_group_exit_boot_services =
77 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt44772c42021-11-16 18:46:27 +010078/* event group before ExitBootServices() invoked */
79const efi_guid_t efi_guid_event_group_before_exit_boot_services =
80 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010081/* event group SetVirtualAddressMap() invoked */
82const efi_guid_t efi_guid_event_group_virtual_address_change =
83 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
84/* event group memory map changed */
85const efi_guid_t efi_guid_event_group_memory_map_change =
86 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
87/* event group boot manager about to boot */
88const efi_guid_t efi_guid_event_group_ready_to_boot =
89 EFI_EVENT_GROUP_READY_TO_BOOT;
90/* event group ResetSystem() invoked (before ExitBootServices) */
91const efi_guid_t efi_guid_event_group_reset_system =
92 EFI_EVENT_GROUP_RESET_SYSTEM;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010093/* GUIDs of the Load File and Load File2 protocols */
94const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
95const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090096/* GUID of the SMBIOS table */
97const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010098
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010099static efi_status_t EFIAPI efi_disconnect_controller(
100 efi_handle_t controller_handle,
101 efi_handle_t driver_image_handle,
102 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +0100103
Ilias Apalodimas1e10d622023-06-20 09:19:28 +0300104static
105efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
106 efi_handle_t *driver_image_handle,
107 struct efi_device_path *remain_device_path,
108 bool recursive);
109
Rob Clark86789d52017-07-27 08:04:18 -0400110/* Called on every callback entry */
111int __efi_entry_check(void)
112{
113 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200114#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -0400115 assert(efi_gd);
116 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200117 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400118#endif
119 return ret;
120}
121
122/* Called on every callback exit */
123int __efi_exit_check(void)
124{
125 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200126#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200127 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400128#endif
129 return ret;
130}
131
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200132/**
133 * efi_save_gd() - save global data register
134 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200135 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200136 * As this register may be overwritten by an EFI payload we save it here
137 * and restore it on every callback entered.
138 *
139 * This function is called after relocation from initr_reloc_global_data().
140 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100141void efi_save_gd(void)
142{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200143#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100144 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600145#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100146}
147
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200148/**
149 * efi_restore_gd() - restore global data register
150 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200151 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200152 * Restore it after returning from the UEFI world to the value saved via
153 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400154 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100155void efi_restore_gd(void)
156{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200157#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100158 /* Only restore if we're already in EFI context */
159 if (!efi_gd)
160 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200161 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600162#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100163}
164
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200165/**
Mario Six8fac2912018-07-10 08:40:17 +0200166 * indent_string() - returns a string for indenting with two spaces per level
167 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100168 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200169 * A maximum of ten indent levels is supported. Higher indent levels will be
170 * truncated.
171 *
Mario Six8fac2912018-07-10 08:40:17 +0200172 * Return: A string for indenting with two spaces per level is
173 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400174 */
175static const char *indent_string(int level)
176{
177 const char *indent = " ";
178 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100179
Rob Clarke7896c32017-07-27 08:04:19 -0400180 level = min(max, level * 2);
181 return &indent[max - level];
182}
183
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200184const char *__efi_nesting(void)
185{
186 return indent_string(nesting_level);
187}
188
Rob Clarke7896c32017-07-27 08:04:19 -0400189const char *__efi_nesting_inc(void)
190{
191 return indent_string(nesting_level++);
192}
193
194const char *__efi_nesting_dec(void)
195{
196 return indent_string(--nesting_level);
197}
198
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200199/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200200 * efi_event_is_queued() - check if an event is queued
201 *
202 * @event: event
203 * Return: true if event is queued
204 */
205static bool efi_event_is_queued(struct efi_event *event)
206{
207 return !!event->queue_link.next;
208}
209
210/**
211 * efi_process_event_queue() - process event queue
212 */
213static void efi_process_event_queue(void)
214{
215 while (!list_empty(&efi_event_queue)) {
216 struct efi_event *event;
217 efi_uintn_t old_tpl;
218
219 event = list_first_entry(&efi_event_queue, struct efi_event,
220 queue_link);
221 if (efi_tpl >= event->notify_tpl)
222 return;
223 list_del(&event->queue_link);
224 event->queue_link.next = NULL;
225 event->queue_link.prev = NULL;
226 /* Events must be executed at the event's TPL */
227 old_tpl = efi_tpl;
228 efi_tpl = event->notify_tpl;
229 EFI_CALL_VOID(event->notify_function(event,
230 event->notify_context));
231 efi_tpl = old_tpl;
232 if (event->type == EVT_NOTIFY_SIGNAL)
233 event->is_signaled = 0;
234 }
235}
236
237/**
Mario Six8fac2912018-07-10 08:40:17 +0200238 * efi_queue_event() - queue an EFI event
239 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200240 *
241 * This function queues the notification function of the event for future
242 * execution.
243 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200244 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200245static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200246{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100247 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200248
249 if (!event->notify_function)
250 return;
251
252 if (!efi_event_is_queued(event)) {
253 /*
254 * Events must be notified in order of decreasing task priority
255 * level. Insert the new event accordingly.
256 */
257 list_for_each_entry(item, &efi_event_queue, queue_link) {
258 if (item->notify_tpl < event->notify_tpl) {
259 list_add_tail(&event->queue_link,
260 &item->queue_link);
261 event = NULL;
262 break;
263 }
264 }
265 if (event)
266 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100267 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200268 }
269}
270
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200271/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200272 * is_valid_tpl() - check if the task priority level is valid
273 *
274 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200275 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200276 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100277static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200278{
279 switch (tpl) {
280 case TPL_APPLICATION:
281 case TPL_CALLBACK:
282 case TPL_NOTIFY:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200283 return EFI_SUCCESS;
284 default:
285 return EFI_INVALID_PARAMETER;
286 }
287}
288
289/**
Mario Six8fac2912018-07-10 08:40:17 +0200290 * efi_signal_event() - signal an EFI event
291 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100292 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100293 * This function signals an event. If the event belongs to an event group, all
294 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100295 * their notification function is queued.
296 *
297 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100298 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200299void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100300{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200301 if (event->is_signaled)
302 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100303 if (event->group) {
304 struct efi_event *evt;
305
306 /*
307 * The signaled state has to set before executing any
308 * notification function
309 */
310 list_for_each_entry(evt, &efi_events, link) {
311 if (!evt->group || guidcmp(evt->group, event->group))
312 continue;
313 if (evt->is_signaled)
314 continue;
315 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100316 }
317 list_for_each_entry(evt, &efi_events, link) {
318 if (!evt->group || guidcmp(evt->group, event->group))
319 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200320 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100321 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200322 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100323 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200324 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100325 }
326}
327
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200328/**
Mario Six8fac2912018-07-10 08:40:17 +0200329 * efi_raise_tpl() - raise the task priority level
330 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200331 *
332 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200333 *
334 * See the Unified Extensible Firmware Interface (UEFI) specification for
335 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200336 *
Mario Six8fac2912018-07-10 08:40:17 +0200337 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200338 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100339static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100340{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100341 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200342
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200343 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200344
345 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200346 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200347 efi_tpl = new_tpl;
348 if (efi_tpl > TPL_HIGH_LEVEL)
349 efi_tpl = TPL_HIGH_LEVEL;
350
351 EFI_EXIT(EFI_SUCCESS);
352 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100353}
354
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200355/**
Mario Six8fac2912018-07-10 08:40:17 +0200356 * efi_restore_tpl() - lower the task priority level
357 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200358 *
359 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200360 *
Mario Six8fac2912018-07-10 08:40:17 +0200361 * See the Unified Extensible Firmware Interface (UEFI) specification for
362 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200363 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100364static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100365{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200366 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200367
368 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200369 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200370 efi_tpl = old_tpl;
371 if (efi_tpl > TPL_HIGH_LEVEL)
372 efi_tpl = TPL_HIGH_LEVEL;
373
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100374 /*
375 * Lowering the TPL may have made queued events eligible for execution.
376 */
377 efi_timer_check();
378
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200379 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100380}
381
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200382/**
Mario Six8fac2912018-07-10 08:40:17 +0200383 * efi_allocate_pages_ext() - allocate memory pages
384 * @type: type of allocation to be performed
385 * @memory_type: usage type of the allocated memory
386 * @pages: number of pages to be allocated
387 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200388 *
389 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200390 *
Mario Six8fac2912018-07-10 08:40:17 +0200391 * See the Unified Extensible Firmware Interface (UEFI) specification for
392 * details.
393 *
394 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900396static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100397 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900398 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100399{
400 efi_status_t r;
401
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100402 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100403 r = efi_allocate_pages(type, memory_type, pages, memory);
404 return EFI_EXIT(r);
405}
406
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200407/**
Mario Six8fac2912018-07-10 08:40:17 +0200408 * efi_free_pages_ext() - Free memory pages.
409 * @memory: start of the memory area to be freed
410 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200411 *
412 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200413 *
414 * See the Unified Extensible Firmware Interface (UEFI) specification for
415 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200416 *
Mario Six8fac2912018-07-10 08:40:17 +0200417 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200418 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900419static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100420 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100421{
422 efi_status_t r;
423
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900424 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100425 r = efi_free_pages(memory, pages);
426 return EFI_EXIT(r);
427}
428
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200429/**
Mario Six8fac2912018-07-10 08:40:17 +0200430 * efi_get_memory_map_ext() - get map describing memory usage
431 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
432 * on exit the size of the copied memory map
433 * @memory_map: buffer to which the memory map is written
434 * @map_key: key for the memory map
435 * @descriptor_size: size of an individual memory descriptor
436 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200437 *
438 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200439 *
Mario Six8fac2912018-07-10 08:40:17 +0200440 * See the Unified Extensible Firmware Interface (UEFI) specification for
441 * details.
442 *
443 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200444 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900445static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100446 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900447 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100448 efi_uintn_t *map_key,
449 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900450 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100451{
452 efi_status_t r;
453
454 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
455 map_key, descriptor_size, descriptor_version);
456 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
457 descriptor_size, descriptor_version);
458 return EFI_EXIT(r);
459}
460
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200461/**
Mario Six8fac2912018-07-10 08:40:17 +0200462 * efi_allocate_pool_ext() - allocate memory from pool
463 * @pool_type: type of the pool from which memory is to be allocated
464 * @size: number of bytes to be allocated
465 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200466 *
467 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200468 *
469 * See the Unified Extensible Firmware Interface (UEFI) specification for
470 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200471 *
Mario Six8fac2912018-07-10 08:40:17 +0200472 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200473 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200474static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100475 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200476 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100477{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100478 efi_status_t r;
479
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100480 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200481 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100482 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100483}
484
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200485/**
Mario Six8fac2912018-07-10 08:40:17 +0200486 * efi_free_pool_ext() - free memory from pool
487 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200488 *
489 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200490 *
Mario Six8fac2912018-07-10 08:40:17 +0200491 * See the Unified Extensible Firmware Interface (UEFI) specification for
492 * details.
493 *
494 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200495 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200496static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100497{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100498 efi_status_t r;
499
500 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200501 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100502 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100503}
504
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200505/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200506 * efi_add_handle() - add a new handle to the object list
507 *
508 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100509 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200510 * The protocols list is initialized. The handle is added to the list of known
511 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100512 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200513void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100514{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200515 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100516 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200517 INIT_LIST_HEAD(&handle->protocols);
518 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100519}
520
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200521/**
Mario Six8fac2912018-07-10 08:40:17 +0200522 * efi_create_handle() - create handle
523 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200524 *
Mario Six8fac2912018-07-10 08:40:17 +0200525 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200526 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100527efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200528{
529 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200530
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200531 obj = calloc(1, sizeof(struct efi_object));
532 if (!obj)
533 return EFI_OUT_OF_RESOURCES;
534
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100535 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200536 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200537
538 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200539}
540
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200541/**
Mario Six8fac2912018-07-10 08:40:17 +0200542 * efi_search_protocol() - find a protocol on a handle.
543 * @handle: handle
544 * @protocol_guid: GUID of the protocol
545 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 *
Mario Six8fac2912018-07-10 08:40:17 +0200547 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100548 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100549efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100550 const efi_guid_t *protocol_guid,
551 struct efi_handler **handler)
552{
553 struct efi_object *efiobj;
554 struct list_head *lhandle;
555
556 if (!handle || !protocol_guid)
557 return EFI_INVALID_PARAMETER;
558 efiobj = efi_search_obj(handle);
559 if (!efiobj)
560 return EFI_INVALID_PARAMETER;
561 list_for_each(lhandle, &efiobj->protocols) {
562 struct efi_handler *protocol;
563
564 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +0100565 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100566 if (handler)
567 *handler = protocol;
568 return EFI_SUCCESS;
569 }
570 }
571 return EFI_NOT_FOUND;
572}
573
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200574/**
Mario Six8fac2912018-07-10 08:40:17 +0200575 * efi_remove_protocol() - delete protocol from a handle
576 * @handle: handle from which the protocol shall be deleted
577 * @protocol: GUID of the protocol to be deleted
578 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100579 *
Mario Six8fac2912018-07-10 08:40:17 +0200580 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581 */
Ilias Apalodimase2effb82023-06-19 14:14:03 +0300582static efi_status_t efi_remove_protocol(const efi_handle_t handle,
583 const efi_guid_t *protocol,
584 void *protocol_interface)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100585{
586 struct efi_handler *handler;
587 efi_status_t ret;
588
589 ret = efi_search_protocol(handle, protocol, &handler);
590 if (ret != EFI_SUCCESS)
591 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200592 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200593 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594 list_del(&handler->link);
595 free(handler);
596 return EFI_SUCCESS;
597}
598
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200599/**
Mario Six8fac2912018-07-10 08:40:17 +0200600 * efi_remove_all_protocols() - delete all protocols from a handle
601 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100602 *
Mario Six8fac2912018-07-10 08:40:17 +0200603 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100604 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100605static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100606{
607 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100608 struct efi_handler *protocol;
609 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100610
611 efiobj = efi_search_obj(handle);
612 if (!efiobj)
613 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100614 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100615 efi_status_t ret;
616
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300617 ret = efi_uninstall_protocol(handle, &protocol->guid,
618 protocol->protocol_interface);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100619 if (ret != EFI_SUCCESS)
620 return ret;
621 }
622 return EFI_SUCCESS;
623}
624
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200625/**
Mario Six8fac2912018-07-10 08:40:17 +0200626 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100627 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200628 * @handle: handle to delete
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300629 *
630 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100631 */
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300632efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100633{
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200634 efi_status_t ret;
635
636 ret = efi_remove_all_protocols(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300637 if (ret != EFI_SUCCESS) {
638 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
639 return ret;
Etienne Carriereb20a42b2022-09-07 10:20:13 +0200640 }
641
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200642 list_del(&handle->link);
643 free(handle);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300644
645 return ret;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100646}
647
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200648/**
Mario Six8fac2912018-07-10 08:40:17 +0200649 * efi_is_event() - check if a pointer is a valid event
650 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100651 *
Mario Six8fac2912018-07-10 08:40:17 +0200652 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100653 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100654static efi_status_t efi_is_event(const struct efi_event *event)
655{
656 const struct efi_event *evt;
657
658 if (!event)
659 return EFI_INVALID_PARAMETER;
660 list_for_each_entry(evt, &efi_events, link) {
661 if (evt == event)
662 return EFI_SUCCESS;
663 }
664 return EFI_INVALID_PARAMETER;
665}
Alexander Grafc15d9212016-03-04 01:09:59 +0100666
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200667/**
Mario Six8fac2912018-07-10 08:40:17 +0200668 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200669 *
Mario Six8fac2912018-07-10 08:40:17 +0200670 * @type: type of the event to create
671 * @notify_tpl: task priority level of the event
672 * @notify_function: notification function of the event
673 * @notify_context: pointer passed to the notification function
674 * @group: event group
675 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200676 *
677 * This function is used inside U-Boot code to create an event.
678 *
679 * For the API function implementing the CreateEvent service see
680 * efi_create_event_ext.
681 *
Mario Six8fac2912018-07-10 08:40:17 +0200682 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200683 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100684efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200685 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200686 struct efi_event *event,
687 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100688 void *notify_context, efi_guid_t *group,
689 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100690{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100691 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200692 efi_status_t ret;
693 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200694
Jonathan Gray7758b212017-03-12 19:26:07 +1100695 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200696 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100697
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200698 switch (type) {
699 case 0:
700 case EVT_TIMER:
701 case EVT_NOTIFY_SIGNAL:
702 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
703 case EVT_NOTIFY_WAIT:
704 case EVT_TIMER | EVT_NOTIFY_WAIT:
705 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200706 pool_type = EFI_BOOT_SERVICES_DATA;
707 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200708 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200709 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200710 break;
711 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200712 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200713 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100714
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100715 /*
716 * The UEFI specification requires event notification levels to be
717 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
718 *
719 * Parameter NotifyTpl should not be checked if it is not used.
720 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900721 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100722 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
723 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200724 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100725
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200726 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
727 (void **)&evt);
728 if (ret != EFI_SUCCESS)
729 return ret;
730 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100731 evt->type = type;
732 evt->notify_tpl = notify_tpl;
733 evt->notify_function = notify_function;
734 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100735 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200736 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100737 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100738 list_add_tail(&evt->link, &efi_events);
739 *event = evt;
740 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100741}
742
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200743/*
Mario Six8fac2912018-07-10 08:40:17 +0200744 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100745 *
Mario Six8fac2912018-07-10 08:40:17 +0200746 * @type: type of the event to create
747 * @notify_tpl: task priority level of the event
748 * @notify_function: notification function of the event
749 * @notify_context: pointer passed to the notification function
750 * @event: created event
751 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100752 *
753 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100754 *
Mario Six8fac2912018-07-10 08:40:17 +0200755 * See the Unified Extensible Firmware Interface (UEFI) specification for
756 * details.
757 *
758 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100759 */
Heinrich Schuchardt8126a1e2023-02-10 08:50:06 +0100760static
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100761efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
762 void (EFIAPI *notify_function) (
763 struct efi_event *event,
764 void *context),
765 void *notify_context,
766 efi_guid_t *event_group,
767 struct efi_event **event)
768{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200769 efi_status_t ret;
770
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100771 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100772 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200773
774 /*
775 * The allowable input parameters are the same as in CreateEvent()
776 * except for the following two disallowed event types.
777 */
778 switch (type) {
779 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
780 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
781 ret = EFI_INVALID_PARAMETER;
782 goto out;
783 }
784
785 ret = efi_create_event(type, notify_tpl, notify_function,
786 notify_context, event_group, event);
787out:
788 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100789}
790
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200791/**
Mario Six8fac2912018-07-10 08:40:17 +0200792 * efi_create_event_ext() - create an event
793 * @type: type of the event to create
794 * @notify_tpl: task priority level of the event
795 * @notify_function: notification function of the event
796 * @notify_context: pointer passed to the notification function
797 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200798 *
799 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200800 *
801 * See the Unified Extensible Firmware Interface (UEFI) specification for
802 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200803 *
Mario Six8fac2912018-07-10 08:40:17 +0200804 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200805 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200806static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100807 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200808 void (EFIAPI *notify_function) (
809 struct efi_event *event,
810 void *context),
811 void *notify_context, struct efi_event **event)
812{
813 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
814 notify_context);
815 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100816 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200817}
818
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200819/**
Mario Six8fac2912018-07-10 08:40:17 +0200820 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200821 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200822 * Check if a timer event has occurred or a queued notification function should
823 * be called.
824 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100825 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200826 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100827 */
828void efi_timer_check(void)
829{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100830 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100831 u64 now = timer_get_us();
832
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100833 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200834 if (!timers_enabled)
835 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100836 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200837 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100838 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200839 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100840 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200841 break;
842 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100843 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200844 break;
845 default:
846 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200847 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100848 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200849 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100850 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200851 efi_process_event_queue();
Stefan Roese80877fa2022-09-02 14:10:46 +0200852 schedule();
Alexander Grafc15d9212016-03-04 01:09:59 +0100853}
854
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200855/**
Mario Six8fac2912018-07-10 08:40:17 +0200856 * efi_set_timer() - set the trigger time for a timer event or stop the event
857 * @event: event for which the timer is set
858 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200859 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200860 *
861 * This is the function for internal usage in U-Boot. For the API function
862 * implementing the SetTimer service see efi_set_timer_ext.
863 *
Mario Six8fac2912018-07-10 08:40:17 +0200864 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200865 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200866efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200867 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100868{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100869 /* Check that the event is valid */
870 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
871 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100872
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200873 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200874 * The parameter defines a multiple of 100 ns.
875 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200876 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200877 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100878
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100879 switch (type) {
880 case EFI_TIMER_STOP:
881 event->trigger_next = -1ULL;
882 break;
883 case EFI_TIMER_PERIODIC:
884 case EFI_TIMER_RELATIVE:
885 event->trigger_next = timer_get_us() + trigger_time;
886 break;
887 default:
888 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100889 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100890 event->trigger_type = type;
891 event->trigger_time = trigger_time;
892 event->is_signaled = false;
893 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100894}
895
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200896/**
Mario Six8fac2912018-07-10 08:40:17 +0200897 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
898 * event
899 * @event: event for which the timer is set
900 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200901 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200902 *
903 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200904 *
Mario Six8fac2912018-07-10 08:40:17 +0200905 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 * details.
907 *
908 *
909 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200911static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
912 enum efi_timer_delay type,
913 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200914{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900915 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200916 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
917}
918
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200919/**
Mario Six8fac2912018-07-10 08:40:17 +0200920 * efi_wait_for_event() - wait for events to be signaled
921 * @num_events: number of events to be waited for
922 * @event: events to be waited for
923 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200924 *
925 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200926 *
Mario Six8fac2912018-07-10 08:40:17 +0200927 * See the Unified Extensible Firmware Interface (UEFI) specification for
928 * details.
929 *
930 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200931 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100932static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200933 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100934 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100935{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100936 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100937
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +0100938 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100939
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200940 /* Check parameters */
941 if (!num_events || !event)
942 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200943 /* Check TPL */
944 if (efi_tpl != TPL_APPLICATION)
945 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200946 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100947 if (efi_is_event(event[i]) != EFI_SUCCESS)
948 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200949 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
950 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200951 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200952 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200953 }
954
955 /* Wait for signal */
956 for (;;) {
957 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200958 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200959 goto out;
960 }
961 /* Allow events to occur. */
962 efi_timer_check();
963 }
964
965out:
966 /*
967 * Reset the signal which is passed to the caller to allow periodic
968 * events to occur.
969 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200970 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200971 if (index)
972 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100973
974 return EFI_EXIT(EFI_SUCCESS);
975}
976
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200977/**
Mario Six8fac2912018-07-10 08:40:17 +0200978 * efi_signal_event_ext() - signal an EFI event
979 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200980 *
981 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200982 *
983 * See the Unified Extensible Firmware Interface (UEFI) specification for
984 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200985 *
986 * This functions sets the signaled state of the event and queues the
987 * notification function for execution.
988 *
Mario Six8fac2912018-07-10 08:40:17 +0200989 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200990 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200991static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100992{
993 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100994 if (efi_is_event(event) != EFI_SUCCESS)
995 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200996 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100997 return EFI_EXIT(EFI_SUCCESS);
998}
999
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001000/**
Mario Six8fac2912018-07-10 08:40:17 +02001001 * efi_close_event() - close an EFI event
1002 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001003 *
1004 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001005 *
Mario Six8fac2912018-07-10 08:40:17 +02001006 * See the Unified Extensible Firmware Interface (UEFI) specification for
1007 * details.
1008 *
1009 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001010 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001011static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001012{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001013 struct efi_register_notify_event *item, *next;
1014
Alexander Grafc15d9212016-03-04 01:09:59 +01001015 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001016 if (efi_is_event(event) != EFI_SUCCESS)
1017 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001018
1019 /* Remove protocol notify registrations for the event */
1020 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1021 link) {
1022 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001023 struct efi_protocol_notification *hitem, *hnext;
1024
1025 /* Remove signaled handles */
1026 list_for_each_entry_safe(hitem, hnext, &item->handles,
1027 link) {
1028 list_del(&hitem->link);
1029 free(hitem);
1030 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001031 list_del(&item->link);
1032 free(item);
1033 }
1034 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001035 /* Remove event from queue */
1036 if (efi_event_is_queued(event))
1037 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001038
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001039 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001040 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001041 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001042}
1043
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001044/**
Mario Six8fac2912018-07-10 08:40:17 +02001045 * efi_check_event() - check if an event is signaled
1046 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001047 *
1048 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001049 *
1050 * See the Unified Extensible Firmware Interface (UEFI) specification for
1051 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001052 *
Mario Six8fac2912018-07-10 08:40:17 +02001053 * If an event is not signaled yet, the notification function is queued. The
1054 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001055 *
Mario Six8fac2912018-07-10 08:40:17 +02001056 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001057 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001058static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001059{
1060 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001061 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001062 if (efi_is_event(event) != EFI_SUCCESS ||
1063 event->type & EVT_NOTIFY_SIGNAL)
1064 return EFI_EXIT(EFI_INVALID_PARAMETER);
1065 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001066 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001067 if (event->is_signaled) {
1068 event->is_signaled = false;
1069 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001070 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001071 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001072}
1073
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001074/**
Mario Six8fac2912018-07-10 08:40:17 +02001075 * efi_search_obj() - find the internal EFI object for a handle
1076 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001077 *
Mario Six8fac2912018-07-10 08:40:17 +02001078 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001079 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001080struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001081{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001082 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001083
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001084 if (!handle)
1085 return NULL;
1086
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001087 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001088 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001089 return efiobj;
1090 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001091 return NULL;
1092}
1093
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001094/**
Mario Six8fac2912018-07-10 08:40:17 +02001095 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1096 * to a protocol
1097 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001098 *
Mario Six8fac2912018-07-10 08:40:17 +02001099 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001100 */
1101static struct efi_open_protocol_info_entry *efi_create_open_info(
1102 struct efi_handler *handler)
1103{
1104 struct efi_open_protocol_info_item *item;
1105
1106 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1107 if (!item)
1108 return NULL;
1109 /* Append the item to the open protocol info list. */
1110 list_add_tail(&item->link, &handler->open_infos);
1111
1112 return &item->info;
1113}
1114
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001115/**
Mario Six8fac2912018-07-10 08:40:17 +02001116 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1117 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001118 *
Mario Six8fac2912018-07-10 08:40:17 +02001119 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001120 */
1121static efi_status_t efi_delete_open_info(
1122 struct efi_open_protocol_info_item *item)
1123{
1124 list_del(&item->link);
1125 free(item);
1126 return EFI_SUCCESS;
1127}
1128
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001129/**
Mario Six8fac2912018-07-10 08:40:17 +02001130 * efi_add_protocol() - install new protocol on a handle
1131 * @handle: handle on which the protocol shall be installed
1132 * @protocol: GUID of the protocol to be installed
1133 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001134 *
Mario Six8fac2912018-07-10 08:40:17 +02001135 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001136 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001137efi_status_t efi_add_protocol(const efi_handle_t handle,
1138 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001139 void *protocol_interface)
1140{
1141 struct efi_object *efiobj;
1142 struct efi_handler *handler;
1143 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001144 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001145
1146 efiobj = efi_search_obj(handle);
1147 if (!efiobj)
1148 return EFI_INVALID_PARAMETER;
1149 ret = efi_search_protocol(handle, protocol, NULL);
1150 if (ret != EFI_NOT_FOUND)
1151 return EFI_INVALID_PARAMETER;
1152 handler = calloc(1, sizeof(struct efi_handler));
1153 if (!handler)
1154 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001155 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001156 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001157 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001158 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001159
1160 /* Notify registered events */
1161 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001162 if (!guidcmp(protocol, &event->protocol)) {
1163 struct efi_protocol_notification *notif;
1164
1165 notif = calloc(1, sizeof(*notif));
1166 if (!notif) {
1167 list_del(&handler->link);
1168 free(handler);
1169 return EFI_OUT_OF_RESOURCES;
1170 }
1171 notif->handle = handle;
1172 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001173 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001174 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001175 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001176 }
1177
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001178 if (!guidcmp(&efi_guid_device_path, protocol))
1179 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001180 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001181}
1182
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001183/**
Mario Six8fac2912018-07-10 08:40:17 +02001184 * efi_install_protocol_interface() - install protocol interface
1185 * @handle: handle on which the protocol shall be installed
1186 * @protocol: GUID of the protocol to be installed
1187 * @protocol_interface_type: type of the interface to be installed,
1188 * always EFI_NATIVE_INTERFACE
1189 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001190 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001191 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001192 *
Mario Six8fac2912018-07-10 08:40:17 +02001193 * See the Unified Extensible Firmware Interface (UEFI) specification for
1194 * details.
1195 *
1196 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001197 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001198static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001199 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001200 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001201{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001202 efi_status_t r;
1203
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001204 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001205 protocol_interface);
1206
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001207 if (!handle || !protocol ||
1208 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1209 r = EFI_INVALID_PARAMETER;
1210 goto out;
1211 }
1212
1213 /* Create new handle if requested. */
1214 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001215 r = efi_create_handle(handle);
1216 if (r != EFI_SUCCESS)
1217 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001218 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001219 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001220 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001221 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001222 /* Add new protocol */
1223 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001224out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001225 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001226}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001227
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001228/**
Mario Six8fac2912018-07-10 08:40:17 +02001229 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001230 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001231 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001232 * @number_of_drivers: number of child controllers
1233 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001234 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001235 * The allocated buffer has to be freed with free().
1236 *
Mario Six8fac2912018-07-10 08:40:17 +02001237 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001238 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001239static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001240 const efi_guid_t *protocol,
1241 efi_uintn_t *number_of_drivers,
1242 efi_handle_t **driver_handle_buffer)
1243{
1244 struct efi_handler *handler;
1245 struct efi_open_protocol_info_item *item;
1246 efi_uintn_t count = 0, i;
1247 bool duplicate;
1248
1249 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001250 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001251 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001252 continue;
1253 list_for_each_entry(item, &handler->open_infos, link) {
1254 if (item->info.attributes &
1255 EFI_OPEN_PROTOCOL_BY_DRIVER)
1256 ++count;
1257 }
1258 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001259 *number_of_drivers = 0;
1260 if (!count) {
1261 *driver_handle_buffer = NULL;
1262 return EFI_SUCCESS;
1263 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001264 /*
1265 * Create buffer. In case of duplicate driver assignments the buffer
1266 * will be too large. But that does not harm.
1267 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001268 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1269 if (!*driver_handle_buffer)
1270 return EFI_OUT_OF_RESOURCES;
1271 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001272 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01001273 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001274 continue;
1275 list_for_each_entry(item, &handler->open_infos, link) {
1276 if (item->info.attributes &
1277 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1278 /* Check this is a new driver */
1279 duplicate = false;
1280 for (i = 0; i < *number_of_drivers; ++i) {
1281 if ((*driver_handle_buffer)[i] ==
1282 item->info.agent_handle)
1283 duplicate = true;
1284 }
1285 /* Copy handle to buffer */
1286 if (!duplicate) {
1287 i = (*number_of_drivers)++;
1288 (*driver_handle_buffer)[i] =
1289 item->info.agent_handle;
1290 }
1291 }
1292 }
1293 }
1294 return EFI_SUCCESS;
1295}
1296
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001297/**
Mario Six8fac2912018-07-10 08:40:17 +02001298 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001299 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001300 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001301 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001302 *
1303 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001304 *
1305 * See the Unified Extensible Firmware Interface (UEFI) specification for
1306 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001307 *
Mario Six8fac2912018-07-10 08:40:17 +02001308 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001309 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001310static efi_status_t efi_disconnect_all_drivers
1311 (efi_handle_t handle,
1312 const efi_guid_t *protocol,
1313 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001314{
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001315 efi_uintn_t number_of_drivers, tmp;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001316 efi_handle_t *driver_handle_buffer;
1317 efi_status_t r, ret;
1318
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001319 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001320 &driver_handle_buffer);
1321 if (ret != EFI_SUCCESS)
1322 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001323 if (!number_of_drivers)
1324 return EFI_SUCCESS;
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001325
1326 tmp = number_of_drivers;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001327 while (number_of_drivers) {
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001328 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001329 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001330 driver_handle_buffer[--number_of_drivers],
1331 child_handle));
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001332 if (ret != EFI_SUCCESS)
1333 goto reconnect;
1334 }
1335
1336 free(driver_handle_buffer);
1337 return ret;
1338
1339reconnect:
1340 /* Reconnect all disconnected drivers */
1341 for (; number_of_drivers < tmp; number_of_drivers++) {
1342 r = EFI_CALL(efi_connect_controller(handle,
1343 &driver_handle_buffer[number_of_drivers],
1344 NULL, true));
1345 if (r != EFI_SUCCESS)
1346 EFI_PRINT("Failed to reconnect controller\n");
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001347 }
Ilias Apalodimas1e10d622023-06-20 09:19:28 +03001348
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001349 free(driver_handle_buffer);
1350 return ret;
1351}
1352
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001353/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001354 * efi_uninstall_protocol() - uninstall protocol interface
1355 *
Mario Six8fac2912018-07-10 08:40:17 +02001356 * @handle: handle from which the protocol shall be removed
1357 * @protocol: GUID of the protocol to be removed
1358 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001359 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001360 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001361 *
1362 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001363 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001364static efi_status_t efi_uninstall_protocol
1365 (efi_handle_t handle, const efi_guid_t *protocol,
1366 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001367{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001368 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001369 struct efi_open_protocol_info_item *item;
1370 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001371 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001372
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001373 /* Find the protocol on the handle */
1374 r = efi_search_protocol(handle, protocol, &handler);
1375 if (r != EFI_SUCCESS)
1376 goto out;
Ilias Apalodimasac1abbe2023-06-20 09:19:30 +03001377 if (handler->protocol_interface != protocol_interface)
1378 return EFI_NOT_FOUND;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001379 /* Disconnect controllers */
Heinrich Schuchardtd822f862023-06-18 09:00:45 +02001380 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001381 if (r != EFI_SUCCESS) {
1382 r = EFI_ACCESS_DENIED;
1383 goto out;
1384 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001385 /* Close protocol */
1386 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1387 if (item->info.attributes ==
1388 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1389 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1390 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001391 efi_delete_open_info(item);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001392 }
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001393 /* if agents didn't close the protocols properly */
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001394 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001395 r = EFI_ACCESS_DENIED;
Ilias Apalodimas106f0df2023-06-20 09:19:29 +03001396 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001397 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001398 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001399 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001400out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001401 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001402}
1403
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001404/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001405 * efi_uninstall_protocol_interface() - uninstall protocol interface
1406 * @handle: handle from which the protocol shall be removed
1407 * @protocol: GUID of the protocol to be removed
1408 * @protocol_interface: interface to be removed
1409 *
1410 * This function implements the UninstallProtocolInterface service.
1411 *
1412 * See the Unified Extensible Firmware Interface (UEFI) specification for
1413 * details.
1414 *
1415 * Return: status code
1416 */
1417static efi_status_t EFIAPI efi_uninstall_protocol_interface
1418 (efi_handle_t handle, const efi_guid_t *protocol,
1419 void *protocol_interface)
1420{
1421 efi_status_t ret;
1422
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001423 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001424
1425 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1426 if (ret != EFI_SUCCESS)
1427 goto out;
1428
1429 /* If the last protocol has been removed, delete the handle. */
1430 if (list_empty(&handle->protocols)) {
1431 list_del(&handle->link);
1432 free(handle);
1433 }
1434out:
1435 return EFI_EXIT(ret);
1436}
1437
1438/**
Mario Six8fac2912018-07-10 08:40:17 +02001439 * efi_register_protocol_notify() - register an event for notification when a
1440 * protocol is installed.
1441 * @protocol: GUID of the protocol whose installation shall be notified
1442 * @event: event to be signaled upon installation of the protocol
1443 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001444 *
1445 * This function implements the RegisterProtocolNotify service.
1446 * See the Unified Extensible Firmware Interface (UEFI) specification
1447 * for details.
1448 *
Mario Six8fac2912018-07-10 08:40:17 +02001449 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001450 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001451efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1452 struct efi_event *event,
1453 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001454{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001455 struct efi_register_notify_event *item;
1456 efi_status_t ret = EFI_SUCCESS;
1457
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001458 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001459
1460 if (!protocol || !event || !registration) {
1461 ret = EFI_INVALID_PARAMETER;
1462 goto out;
1463 }
1464
1465 item = calloc(1, sizeof(struct efi_register_notify_event));
1466 if (!item) {
1467 ret = EFI_OUT_OF_RESOURCES;
1468 goto out;
1469 }
1470
1471 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301472 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001473 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001474
1475 list_add_tail(&item->link, &efi_register_notify_events);
1476
1477 *registration = item;
1478out:
1479 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001480}
1481
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001482/**
Mario Six8fac2912018-07-10 08:40:17 +02001483 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001484 *
Mario Six8fac2912018-07-10 08:40:17 +02001485 * @search_type: selection criterion
1486 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001487 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001488 *
1489 * See the documentation of the LocateHandle service in the UEFI specification.
1490 *
Mario Six8fac2912018-07-10 08:40:17 +02001491 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001492 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001493static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001494 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001495{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001496 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001497
1498 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001499 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001500 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001501 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001502 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001503 return (ret != EFI_SUCCESS);
1504 default:
1505 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001506 return -1;
1507 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001508}
1509
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001510/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001511 * efi_check_register_notify_event() - check if registration key is valid
1512 *
1513 * Check that a pointer is a valid registration key as returned by
1514 * RegisterProtocolNotify().
1515 *
1516 * @key: registration key
1517 * Return: valid registration key or NULL
1518 */
1519static struct efi_register_notify_event *efi_check_register_notify_event
1520 (void *key)
1521{
1522 struct efi_register_notify_event *event;
1523
1524 list_for_each_entry(event, &efi_register_notify_events, link) {
1525 if (event == (struct efi_register_notify_event *)key)
1526 return event;
1527 }
1528 return NULL;
1529}
1530
1531/**
Mario Six8fac2912018-07-10 08:40:17 +02001532 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001533 *
1534 * @search_type: selection criterion
1535 * @protocol: GUID of the protocol
1536 * @search_key: registration key
1537 * @buffer_size: size of the buffer to receive the handles in bytes
1538 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001539 *
1540 * This function is meant for U-Boot internal calls. For the API implementation
1541 * of the LocateHandle service see efi_locate_handle_ext.
1542 *
Mario Six8fac2912018-07-10 08:40:17 +02001543 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001544 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001545static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001546 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001547 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001548 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001549{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001550 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001551 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001552 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001553 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001554
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001555 /* Check parameters */
1556 switch (search_type) {
1557 case ALL_HANDLES:
1558 break;
1559 case BY_REGISTER_NOTIFY:
1560 if (!search_key)
1561 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001562 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001563 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001564 if (!event)
1565 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001566 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001567 case BY_PROTOCOL:
1568 if (!protocol)
1569 return EFI_INVALID_PARAMETER;
1570 break;
1571 default:
1572 return EFI_INVALID_PARAMETER;
1573 }
1574
Alexander Grafc15d9212016-03-04 01:09:59 +01001575 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001576 if (search_type == BY_REGISTER_NOTIFY) {
1577 if (list_empty(&event->handles))
1578 return EFI_NOT_FOUND;
1579 handle = list_first_entry(&event->handles,
1580 struct efi_protocol_notification,
1581 link);
1582 efiobj = handle->handle;
1583 size += sizeof(void *);
1584 } else {
1585 list_for_each_entry(efiobj, &efi_obj_list, link) {
1586 if (!efi_search(search_type, protocol, efiobj))
1587 size += sizeof(void *);
1588 }
1589 if (size == 0)
1590 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001591 }
1592
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001593 if (!buffer_size)
1594 return EFI_INVALID_PARAMETER;
1595
Alexander Grafc15d9212016-03-04 01:09:59 +01001596 if (*buffer_size < size) {
1597 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001598 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001599 }
1600
Rob Clarkcdee3372017-08-06 14:10:07 -04001601 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001602
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001603 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001604 if (!buffer)
1605 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001606
Alexander Grafc15d9212016-03-04 01:09:59 +01001607 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001608 if (search_type == BY_REGISTER_NOTIFY) {
1609 *buffer = efiobj;
1610 list_del(&handle->link);
1611 } else {
1612 list_for_each_entry(efiobj, &efi_obj_list, link) {
1613 if (!efi_search(search_type, protocol, efiobj))
1614 *buffer++ = efiobj;
1615 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001616 }
1617
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001618 return EFI_SUCCESS;
1619}
1620
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001621/**
Mario Six8fac2912018-07-10 08:40:17 +02001622 * efi_locate_handle_ext() - locate handles implementing a protocol.
1623 * @search_type: selection criterion
1624 * @protocol: GUID of the protocol
1625 * @search_key: registration key
1626 * @buffer_size: size of the buffer to receive the handles in bytes
1627 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001628 *
1629 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001630 *
Mario Six8fac2912018-07-10 08:40:17 +02001631 * See the Unified Extensible Firmware Interface (UEFI) specification for
1632 * details.
1633 *
1634 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001635 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001636static efi_status_t EFIAPI efi_locate_handle_ext(
1637 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001638 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001639 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001640{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001641 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001642 buffer_size, buffer);
1643
1644 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1645 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001646}
1647
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001648/**
Mario Six8fac2912018-07-10 08:40:17 +02001649 * efi_remove_configuration_table() - collapses configuration table entries,
1650 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001651 *
Mario Six8fac2912018-07-10 08:40:17 +02001652 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001653 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001654static void efi_remove_configuration_table(int i)
1655{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001656 struct efi_configuration_table *this = &systab.tables[i];
1657 struct efi_configuration_table *next = &systab.tables[i + 1];
1658 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001659
1660 memmove(this, next, (ulong)end - (ulong)next);
1661 systab.nr_tables--;
1662}
1663
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001664/**
Mario Six8fac2912018-07-10 08:40:17 +02001665 * efi_install_configuration_table() - adds, updates, or removes a
1666 * configuration table
1667 * @guid: GUID of the installed table
1668 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001669 *
1670 * This function is used for internal calls. For the API implementation of the
1671 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1672 *
Mario Six8fac2912018-07-10 08:40:17 +02001673 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001674 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001675efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1676 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001677{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001678 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001679 int i;
1680
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001681 if (!guid)
1682 return EFI_INVALID_PARAMETER;
1683
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001684 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001685 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001686 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001687 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001688 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001689 else
1690 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001691 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001692 }
1693 }
1694
Alexander Graffe3366f2017-07-26 13:41:04 +02001695 if (!table)
1696 return EFI_NOT_FOUND;
1697
Alexander Grafc15d9212016-03-04 01:09:59 +01001698 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001699 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001700 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001701
1702 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301703 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001704 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001705 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001706
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001707out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001708 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001709 efi_update_table_header_crc32(&systab.hdr);
1710
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001711 /* Notify that the configuration table was changed */
1712 list_for_each_entry(evt, &efi_events, link) {
1713 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001714 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001715 break;
1716 }
1717 }
1718
Alexander Grafc5c11632016-08-19 01:23:24 +02001719 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001720}
1721
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001722/**
Mario Six8fac2912018-07-10 08:40:17 +02001723 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1724 * configuration table.
1725 * @guid: GUID of the installed table
1726 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001727 *
1728 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001729 *
Mario Six8fac2912018-07-10 08:40:17 +02001730 * See the Unified Extensible Firmware Interface (UEFI) specification for
1731 * details.
1732 *
1733 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001734 */
Masahisa Kojimabc38d772021-10-22 20:24:24 +09001735static efi_status_t
1736EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1737 void *table)
Alexander Grafc5c11632016-08-19 01:23:24 +02001738{
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001739 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001740 return EFI_EXIT(efi_install_configuration_table(guid, table));
1741}
1742
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001743/**
Mario Six8fac2912018-07-10 08:40:17 +02001744 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001745 *
1746 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001747 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001748 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001749 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001750 * code is returned.
1751 *
1752 * @device_path: device path of the loaded image
1753 * @file_path: file path of the loaded image
1754 * @handle_ptr: handle of the loaded image
1755 * @info_ptr: loaded image protocol
1756 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001757 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001758efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1759 struct efi_device_path *file_path,
1760 struct efi_loaded_image_obj **handle_ptr,
1761 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001762{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001763 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001764 struct efi_loaded_image *info = NULL;
1765 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001766 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001767
1768 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1769 *handle_ptr = NULL;
1770 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001771
1772 info = calloc(1, sizeof(*info));
1773 if (!info)
1774 return EFI_OUT_OF_RESOURCES;
1775 obj = calloc(1, sizeof(*obj));
1776 if (!obj) {
1777 free(info);
1778 return EFI_OUT_OF_RESOURCES;
1779 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001780 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001781
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001782 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001783 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001784
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001785 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001786 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001787 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001788
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001789 if (device_path) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +01001790 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001791
1792 dp = efi_dp_append(device_path, file_path);
1793 if (!dp) {
1794 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001795 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001796 }
1797 } else {
1798 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001799 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001800 ret = efi_add_protocol(&obj->header,
1801 &efi_guid_loaded_image_device_path, dp);
1802 if (ret != EFI_SUCCESS)
1803 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001804
1805 /*
1806 * When asking for the loaded_image interface, just
1807 * return handle which points to loaded_image_info
1808 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001809 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001810 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001811 if (ret != EFI_SUCCESS)
1812 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001813
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001814 *info_ptr = info;
1815 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001816
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001817 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001818failure:
1819 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001820 efi_delete_handle(&obj->header);
1821 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001822 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001823}
1824
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001825/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001826 * efi_locate_device_path() - Get the device path and handle of an device
1827 * implementing a protocol
1828 * @protocol: GUID of the protocol
1829 * @device_path: device path
1830 * @device: handle of the device
1831 *
1832 * This function implements the LocateDevicePath service.
1833 *
1834 * See the Unified Extensible Firmware Interface (UEFI) specification for
1835 * details.
1836 *
1837 * Return: status code
1838 */
AKASHI Takahiro537a6932022-04-28 17:09:38 +09001839efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1840 struct efi_device_path **device_path,
1841 efi_handle_t *device)
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001842{
1843 struct efi_device_path *dp;
1844 size_t i;
1845 struct efi_handler *handler;
1846 efi_handle_t *handles;
1847 size_t len, len_dp;
1848 size_t len_best = 0;
1849 efi_uintn_t no_handles;
1850 u8 *remainder;
1851 efi_status_t ret;
1852
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01001853 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001854
1855 if (!protocol || !device_path || !*device_path) {
1856 ret = EFI_INVALID_PARAMETER;
1857 goto out;
1858 }
1859
1860 /* Find end of device path */
1861 len = efi_dp_instance_size(*device_path);
1862
1863 /* Get all handles implementing the protocol */
1864 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1865 &no_handles, &handles));
1866 if (ret != EFI_SUCCESS)
1867 goto out;
1868
1869 for (i = 0; i < no_handles; ++i) {
1870 /* Find the device path protocol */
1871 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1872 &handler);
1873 if (ret != EFI_SUCCESS)
1874 continue;
1875 dp = (struct efi_device_path *)handler->protocol_interface;
1876 len_dp = efi_dp_instance_size(dp);
1877 /*
1878 * This handle can only be a better fit
1879 * if its device path length is longer than the best fit and
1880 * if its device path length is shorter of equal the searched
1881 * device path.
1882 */
1883 if (len_dp <= len_best || len_dp > len)
1884 continue;
1885 /* Check if dp is a subpath of device_path */
1886 if (memcmp(*device_path, dp, len_dp))
1887 continue;
1888 if (!device) {
1889 ret = EFI_INVALID_PARAMETER;
1890 goto out;
1891 }
1892 *device = handles[i];
1893 len_best = len_dp;
1894 }
1895 if (len_best) {
1896 remainder = (u8 *)*device_path + len_best;
1897 *device_path = (struct efi_device_path *)remainder;
1898 ret = EFI_SUCCESS;
1899 } else {
1900 ret = EFI_NOT_FOUND;
1901 }
1902out:
1903 return EFI_EXIT(ret);
1904}
1905
1906/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001907 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001908 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001909 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1910 * callers obligation to update the memory type as needed.
1911 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001912 * @file_path: the path of the image to load
1913 * @buffer: buffer containing the loaded image
1914 * @size: size of the loaded image
1915 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001916 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001917static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001918efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001919 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001920{
Rob Clark857a1222017-09-13 18:05:35 -04001921 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001922 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001923 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001924 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001925
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001926 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001927 f = efi_file_from_path(file_path);
1928 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001929 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001930
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001931 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001932 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001933 goto error;
1934
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001935 /*
1936 * When reading the file we do not yet know if it contains an
1937 * application, a boottime driver, or a runtime driver. So here we
1938 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1939 * update the reservation according to the image type.
1940 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001941 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1942 EFI_BOOT_SERVICES_DATA,
1943 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001944 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001945 ret = EFI_OUT_OF_RESOURCES;
1946 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001947 }
1948
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001949 /* Read file */
1950 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1951 if (ret != EFI_SUCCESS)
1952 efi_free_pages(addr, efi_size_in_pages(bs));
1953 *buffer = (void *)(uintptr_t)addr;
1954 *size = bs;
1955error:
1956 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001957 return ret;
1958}
1959
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001960/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001961 * efi_load_image_from_path() - load an image using a file path
1962 *
1963 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1964 * callers obligation to update the memory type as needed.
1965 *
1966 * @boot_policy: true for request originating from the boot manager
1967 * @file_path: the path of the image to load
1968 * @buffer: buffer containing the loaded image
1969 * @size: size of the loaded image
1970 * Return: status code
1971 */
1972static
1973efi_status_t efi_load_image_from_path(bool boot_policy,
1974 struct efi_device_path *file_path,
1975 void **buffer, efi_uintn_t *size)
1976{
1977 efi_handle_t device;
1978 efi_status_t ret;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001979 struct efi_device_path *dp, *rem;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001980 struct efi_load_file_protocol *load_file_protocol = NULL;
1981 efi_uintn_t buffer_size;
1982 uint64_t addr, pages;
1983 const efi_guid_t *guid;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01001984 struct efi_handler *handler;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001985
1986 /* In case of failure nothing is returned */
1987 *buffer = NULL;
1988 *size = 0;
1989
1990 dp = file_path;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001991 device = efi_dp_find_obj(dp, NULL, &rem);
1992 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
1993 NULL);
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001994 if (ret == EFI_SUCCESS)
1995 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001996
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01001997 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001998 if (ret == EFI_SUCCESS) {
1999 guid = &efi_guid_load_file_protocol;
2000 } else if (!boot_policy) {
2001 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002002 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002003 }
2004 if (ret != EFI_SUCCESS)
2005 return EFI_NOT_FOUND;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002006 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002007 if (ret != EFI_SUCCESS)
2008 return EFI_NOT_FOUND;
2009 buffer_size = 0;
Heinrich Schuchardt94d72672023-01-24 20:36:45 +01002010 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002011 ret = EFI_CALL(load_file_protocol->load_file(
2012 load_file_protocol, rem, boot_policy,
2013 &buffer_size, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002014 if (ret != EFI_BUFFER_TOO_SMALL)
2015 goto out;
2016 pages = efi_size_in_pages(buffer_size);
2017 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2018 pages, &addr);
2019 if (ret != EFI_SUCCESS) {
2020 ret = EFI_OUT_OF_RESOURCES;
2021 goto out;
2022 }
2023 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardta1d82462022-02-26 12:05:30 +01002024 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002025 &buffer_size, (void *)(uintptr_t)addr));
2026 if (ret != EFI_SUCCESS)
2027 efi_free_pages(addr, pages);
2028out:
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002029 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01002030 if (ret == EFI_SUCCESS) {
2031 *buffer = (void *)(uintptr_t)addr;
2032 *size = buffer_size;
2033 }
2034
2035 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01002036}
2037
2038/**
Mario Six8fac2912018-07-10 08:40:17 +02002039 * efi_load_image() - load an EFI image into memory
2040 * @boot_policy: true for request originating from the boot manager
2041 * @parent_image: the caller's image handle
2042 * @file_path: the path of the image to load
2043 * @source_buffer: memory location from which the image is installed
2044 * @source_size: size of the memory area from which the image is installed
2045 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002046 *
2047 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002048 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002049 * See the Unified Extensible Firmware Interface (UEFI) specification
2050 * for details.
2051 *
Mario Six8fac2912018-07-10 08:40:17 +02002052 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002053 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002054efi_status_t EFIAPI efi_load_image(bool boot_policy,
2055 efi_handle_t parent_image,
2056 struct efi_device_path *file_path,
2057 void *source_buffer,
2058 efi_uintn_t source_size,
2059 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002060{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002061 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002062 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002063 struct efi_loaded_image_obj **image_obj =
2064 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002065 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002066 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002067
Heinrich Schuchardte1e8a652022-02-03 22:21:51 +01002068 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002069 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002070
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002071 if (!image_handle || (!source_buffer && !file_path) ||
2072 !efi_search_obj(parent_image) ||
2073 /* The parent image handle must refer to a loaded image */
2074 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002075 ret = EFI_INVALID_PARAMETER;
2076 goto error;
2077 }
Rob Clark857a1222017-09-13 18:05:35 -04002078
2079 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002080 ret = efi_load_image_from_path(boot_policy, file_path,
2081 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002082 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002083 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002084 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002085 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002086 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002087 /* split file_path which contains both the device and file parts */
2088 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002089 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002090 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002091 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002092 if (!source_buffer)
2093 /* Release buffer to which file was loaded */
2094 efi_free_pages((uintptr_t)dest_buffer,
2095 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002096 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002097 info->system_table = &systab;
2098 info->parent_handle = parent_image;
2099 } else {
2100 /* The image is invalid. Release all associated resources. */
2101 efi_delete_handle(*image_handle);
2102 *image_handle = NULL;
2103 free(info);
2104 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002105error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002106 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002107}
2108
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002109/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002110 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2111 */
2112static void efi_exit_caches(void)
2113{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002114#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002115 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002116 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2117 * caches are enabled.
2118 *
2119 * TODO:
2120 * According to the UEFI spec caches that can be managed via CP15
2121 * operations should be enabled. Caches requiring platform information
2122 * to manage should be disabled. This should not happen in
2123 * ExitBootServices() but before invoking any UEFI binary is invoked.
2124 *
2125 * We want to keep the current workaround while GRUB prior to version
2126 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002127 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002128 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002129#endif
2130}
2131
2132/**
Mario Six8fac2912018-07-10 08:40:17 +02002133 * efi_exit_boot_services() - stop all boot services
2134 * @image_handle: handle of the loaded image
2135 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002136 *
2137 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002138 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002139 * See the Unified Extensible Firmware Interface (UEFI) specification
2140 * for details.
2141 *
Mario Six8fac2912018-07-10 08:40:17 +02002142 * All timer events are disabled. For exit boot services events the
2143 * notification function is called. The boot services are disabled in the
2144 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002145 *
Mario Six8fac2912018-07-10 08:40:17 +02002146 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002147 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002148static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002149 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002150{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002151 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002152 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002153
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002154 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002155
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002156 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002157 if (map_key != efi_memory_map_key) {
2158 ret = EFI_INVALID_PARAMETER;
2159 goto out;
2160 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002161
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002162 /* Check if ExitBootServices has already been called */
2163 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002164 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002165
Heinrich Schuchardt44772c42021-11-16 18:46:27 +01002166 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2167 list_for_each_entry(evt, &efi_events, link) {
2168 if (evt->group &&
2169 !guidcmp(evt->group,
2170 &efi_guid_event_group_before_exit_boot_services)) {
2171 efi_signal_event(evt);
2172 break;
2173 }
2174 }
2175
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002176 /* Stop all timer related activities */
2177 timers_enabled = false;
2178
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002179 /* Add related events to the event group */
2180 list_for_each_entry(evt, &efi_events, link) {
2181 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2182 evt->group = &efi_guid_event_group_exit_boot_services;
2183 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002184 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002185 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002186 if (evt->group &&
2187 !guidcmp(evt->group,
2188 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002189 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002190 break;
2191 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002192 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002193
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002194 /* Make sure that notification functions are not called anymore */
2195 efi_tpl = TPL_HIGH_LEVEL;
2196
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002197 /* Notify variable services */
2198 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002199
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002200 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2201 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2202 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2203 list_del(&evt->link);
2204 }
2205
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002206 if (!efi_st_keep_devices) {
Tom Rini669ef7f2021-11-19 16:33:04 -05002207 bootm_disable_interrupts();
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002208 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002209 udc_disconnect();
2210 board_quiesce_devices();
2211 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2212 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002213
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002214 /* Patch out unsupported runtime function */
2215 efi_runtime_detach();
2216
Alexander Graffa5246b2018-11-15 21:23:47 +01002217 /* Fix up caches for EFI payloads if necessary */
2218 efi_exit_caches();
2219
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002220 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002221 systab.con_in_handle = NULL;
2222 systab.con_in = NULL;
2223 systab.con_out_handle = NULL;
2224 systab.con_out = NULL;
2225 systab.stderr_handle = NULL;
2226 systab.std_err = NULL;
2227 systab.boottime = NULL;
2228
2229 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002230 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002231
Alexander Grafc15d9212016-03-04 01:09:59 +01002232 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002233 efi_set_watchdog(0);
Stefan Roese80877fa2022-09-02 14:10:46 +02002234 schedule();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002235out:
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002236 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2237 if (ret != EFI_SUCCESS)
2238 efi_tcg2_notify_exit_boot_services_failed();
2239 }
2240
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002241 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002242}
2243
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002244/**
Mario Six8fac2912018-07-10 08:40:17 +02002245 * efi_get_next_monotonic_count() - get next value of the counter
2246 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002247 *
2248 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002249 *
2250 * See the Unified Extensible Firmware Interface (UEFI) specification for
2251 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002252 *
Mario Six8fac2912018-07-10 08:40:17 +02002253 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002254 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002255static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2256{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002257 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002258 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002259
Alexander Grafc15d9212016-03-04 01:09:59 +01002260 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002261 if (!count) {
2262 ret = EFI_INVALID_PARAMETER;
2263 goto out;
2264 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002265 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002266 ret = EFI_SUCCESS;
2267out:
2268 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002269}
2270
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002271/**
Mario Six8fac2912018-07-10 08:40:17 +02002272 * efi_stall() - sleep
2273 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002274 *
Mario Six8fac2912018-07-10 08:40:17 +02002275 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002276 *
Mario Six8fac2912018-07-10 08:40:17 +02002277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2278 * details.
2279 *
2280 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002281 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002282static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2283{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002284 u64 end_tick;
2285
Alexander Grafc15d9212016-03-04 01:09:59 +01002286 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002287
2288 end_tick = get_ticks() + usec_to_tick(microseconds);
2289 while (get_ticks() < end_tick)
2290 efi_timer_check();
2291
Alexander Grafc15d9212016-03-04 01:09:59 +01002292 return EFI_EXIT(EFI_SUCCESS);
2293}
2294
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002295/**
Mario Six8fac2912018-07-10 08:40:17 +02002296 * efi_set_watchdog_timer() - reset the watchdog timer
2297 * @timeout: seconds before reset by watchdog
2298 * @watchdog_code: code to be logged when resetting
2299 * @data_size: size of buffer in bytes
2300 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002301 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002302 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002303 *
Mario Six8fac2912018-07-10 08:40:17 +02002304 * See the Unified Extensible Firmware Interface (UEFI) specification for
2305 * details.
2306 *
2307 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002308 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002309static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2310 uint64_t watchdog_code,
2311 unsigned long data_size,
2312 uint16_t *watchdog_data)
2313{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002314 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002315 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002316 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002317}
2318
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002319/**
Mario Six8fac2912018-07-10 08:40:17 +02002320 * efi_close_protocol() - close a protocol
2321 * @handle: handle on which the protocol shall be closed
2322 * @protocol: GUID of the protocol to close
2323 * @agent_handle: handle of the driver
2324 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002325 *
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002326 * This is the function implementing the CloseProtocol service is for internal
2327 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002328 *
Mario Six8fac2912018-07-10 08:40:17 +02002329 * See the Unified Extensible Firmware Interface (UEFI) specification for
2330 * details.
2331 *
2332 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002333 */
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002334efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2335 efi_handle_t agent_handle,
2336 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002337{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002338 struct efi_handler *handler;
2339 struct efi_open_protocol_info_item *item;
2340 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002341 efi_status_t ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002342
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002343 if (!efi_search_obj(agent_handle) ||
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002344 (controller_handle && !efi_search_obj(controller_handle)))
2345 return EFI_INVALID_PARAMETER;
2346 ret = efi_search_protocol(handle, protocol, &handler);
2347 if (ret != EFI_SUCCESS)
2348 return ret;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002349
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002350 ret = EFI_NOT_FOUND;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002351 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2352 if (item->info.agent_handle == agent_handle &&
2353 item->info.controller_handle == controller_handle) {
2354 efi_delete_open_info(item);
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002355 ret = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002356 }
2357 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02002358
2359 return ret;
2360}
2361
2362/**
2363 * efi_close_protocol_ext() - close a protocol
2364 * @handle: handle on which the protocol shall be closed
2365 * @protocol: GUID of the protocol to close
2366 * @agent_handle: handle of the driver
2367 * @controller_handle: handle of the controller
2368 *
2369 * This function implements the CloseProtocol service.
2370 *
2371 * See the Unified Extensible Firmware Interface (UEFI) specification for
2372 * details.
2373 *
2374 * Return: status code
2375 */
2376static efi_status_t EFIAPI
2377efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2378 efi_handle_t agent_handle,
2379 efi_handle_t controller_handle)
2380{
2381 efi_status_t ret;
2382
2383 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2384 controller_handle);
2385
2386 ret = efi_close_protocol(handle, protocol,
2387 agent_handle, controller_handle);
2388
2389 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002390}
2391
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002392/**
Mario Six8fac2912018-07-10 08:40:17 +02002393 * efi_open_protocol_information() - provide information about then open status
2394 * of a protocol on a handle
2395 * @handle: handle for which the information shall be retrieved
2396 * @protocol: GUID of the protocol
2397 * @entry_buffer: buffer to receive the open protocol information
2398 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002399 *
2400 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002401 *
2402 * See the Unified Extensible Firmware Interface (UEFI) specification for
2403 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002404 *
Mario Six8fac2912018-07-10 08:40:17 +02002405 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002406 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002407static efi_status_t EFIAPI efi_open_protocol_information(
2408 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002409 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002410 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002411{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002412 unsigned long buffer_size;
2413 unsigned long count;
2414 struct efi_handler *handler;
2415 struct efi_open_protocol_info_item *item;
2416 efi_status_t r;
2417
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002418 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002419 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002420
2421 /* Check parameters */
2422 if (!entry_buffer) {
2423 r = EFI_INVALID_PARAMETER;
2424 goto out;
2425 }
2426 r = efi_search_protocol(handle, protocol, &handler);
2427 if (r != EFI_SUCCESS)
2428 goto out;
2429
2430 /* Count entries */
2431 count = 0;
2432 list_for_each_entry(item, &handler->open_infos, link) {
2433 if (item->info.open_count)
2434 ++count;
2435 }
2436 *entry_count = count;
2437 *entry_buffer = NULL;
2438 if (!count) {
2439 r = EFI_SUCCESS;
2440 goto out;
2441 }
2442
2443 /* Copy entries */
2444 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002445 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002446 (void **)entry_buffer);
2447 if (r != EFI_SUCCESS)
2448 goto out;
2449 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2450 if (item->info.open_count)
2451 (*entry_buffer)[--count] = item->info;
2452 }
2453out:
2454 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002455}
2456
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002457/**
Mario Six8fac2912018-07-10 08:40:17 +02002458 * efi_protocols_per_handle() - get protocols installed on a handle
2459 * @handle: handle for which the information is retrieved
2460 * @protocol_buffer: buffer with protocol GUIDs
2461 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002462 *
2463 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002464 *
Mario Six8fac2912018-07-10 08:40:17 +02002465 * See the Unified Extensible Firmware Interface (UEFI) specification for
2466 * details.
2467 *
2468 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002469 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002470static efi_status_t EFIAPI efi_protocols_per_handle(
2471 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002472 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002473{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002474 unsigned long buffer_size;
2475 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002476 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002477 efi_status_t r;
2478
Alexander Grafc15d9212016-03-04 01:09:59 +01002479 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2480 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002481
2482 if (!handle || !protocol_buffer || !protocol_buffer_count)
2483 return EFI_EXIT(EFI_INVALID_PARAMETER);
2484
2485 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002486 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002487
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002488 efiobj = efi_search_obj(handle);
2489 if (!efiobj)
2490 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002491
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002492 /* Count protocols */
2493 list_for_each(protocol_handle, &efiobj->protocols) {
2494 ++*protocol_buffer_count;
2495 }
2496
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002497 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002498 if (*protocol_buffer_count) {
2499 size_t j = 0;
2500
2501 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002502 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002503 (void **)protocol_buffer);
2504 if (r != EFI_SUCCESS)
2505 return EFI_EXIT(r);
2506 list_for_each(protocol_handle, &efiobj->protocols) {
2507 struct efi_handler *protocol;
2508
2509 protocol = list_entry(protocol_handle,
2510 struct efi_handler, link);
Heinrich Schuchardt2a22db92022-03-09 19:56:23 +01002511 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002512 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002513 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002514 }
2515
2516 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002517}
2518
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002519efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2520 const efi_guid_t *protocol, void *search_key,
2521 efi_uintn_t *no_handles, efi_handle_t **buffer)
2522{
2523 efi_status_t r;
2524 efi_uintn_t buffer_size = 0;
2525
2526 if (!no_handles || !buffer) {
2527 r = EFI_INVALID_PARAMETER;
2528 goto out;
2529 }
2530 *no_handles = 0;
2531 *buffer = NULL;
2532 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2533 *buffer);
2534 if (r != EFI_BUFFER_TOO_SMALL)
2535 goto out;
2536 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2537 (void **)buffer);
2538 if (r != EFI_SUCCESS)
2539 goto out;
2540 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2541 *buffer);
2542 if (r == EFI_SUCCESS)
2543 *no_handles = buffer_size / sizeof(efi_handle_t);
2544out:
2545 return r;
2546}
2547
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002548/**
Mario Six8fac2912018-07-10 08:40:17 +02002549 * efi_locate_handle_buffer() - locate handles implementing a protocol
2550 * @search_type: selection criterion
2551 * @protocol: GUID of the protocol
2552 * @search_key: registration key
2553 * @no_handles: number of returned handles
2554 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002555 *
2556 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002557 *
Mario Six8fac2912018-07-10 08:40:17 +02002558 * See the Unified Extensible Firmware Interface (UEFI) specification for
2559 * details.
2560 *
2561 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002562 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002563efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002564 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002565 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002566 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002567{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002568 efi_status_t r;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002569
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002570 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002571 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002572
Masahisa Kojimac5ff0a02022-09-12 17:33:50 +09002573 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2574 no_handles, buffer);
2575
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002576 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002577}
2578
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002579/**
Mario Six8fac2912018-07-10 08:40:17 +02002580 * efi_locate_protocol() - find an interface implementing a protocol
2581 * @protocol: GUID of the protocol
2582 * @registration: registration key passed to the notification function
2583 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002584 *
2585 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002586 *
2587 * See the Unified Extensible Firmware Interface (UEFI) specification for
2588 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002589 *
Mario Six8fac2912018-07-10 08:40:17 +02002590 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002591 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002592static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002593 void *registration,
2594 void **protocol_interface)
2595{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002596 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002597 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002598 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002599
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01002600 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002601
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002602 /*
2603 * The UEFI spec explicitly requires a protocol even if a registration
2604 * key is provided. This differs from the logic in LocateHandle().
2605 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002606 if (!protocol || !protocol_interface)
2607 return EFI_EXIT(EFI_INVALID_PARAMETER);
2608
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002609 if (registration) {
2610 struct efi_register_notify_event *event;
2611 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002612
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002613 event = efi_check_register_notify_event(registration);
2614 if (!event)
2615 return EFI_EXIT(EFI_INVALID_PARAMETER);
2616 /*
2617 * The UEFI spec requires to return EFI_NOT_FOUND if no
2618 * protocol instance matches protocol and registration.
2619 * So let's do the same for a mismatch between protocol and
2620 * registration.
2621 */
2622 if (guidcmp(&event->protocol, protocol))
2623 goto not_found;
2624 if (list_empty(&event->handles))
2625 goto not_found;
2626 handle = list_first_entry(&event->handles,
2627 struct efi_protocol_notification,
2628 link);
2629 efiobj = handle->handle;
2630 list_del(&handle->link);
2631 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002632 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002633 if (ret == EFI_SUCCESS)
2634 goto found;
2635 } else {
2636 list_for_each_entry(efiobj, &efi_obj_list, link) {
2637 ret = efi_search_protocol(efiobj, protocol, &handler);
2638 if (ret == EFI_SUCCESS)
2639 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002640 }
2641 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002642not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002643 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002644 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002645found:
2646 *protocol_interface = handler->protocol_interface;
2647 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002648}
2649
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002650/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002651 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
Mario Six8fac2912018-07-10 08:40:17 +02002652 * interfaces
2653 * @handle: handle on which the protocol interfaces shall be installed
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002654 * @argptr: va_list of args
Mario Six8fac2912018-07-10 08:40:17 +02002655 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002656 * Core functionality of efi_install_multiple_protocol_interfaces
2657 * Must not be called directly
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002658 *
Mario Six8fac2912018-07-10 08:40:17 +02002659 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002660 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002661static efi_status_t EFIAPI
2662efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2663 efi_va_list argptr)
Alexander Grafc15d9212016-03-04 01:09:59 +01002664{
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002665 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002666 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002667 efi_handle_t old_handle;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002668 efi_status_t ret = EFI_SUCCESS;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002669 int i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002670 efi_va_list argptr_copy;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002671
2672 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002673 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002674
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002675 efi_va_copy(argptr_copy, argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002676 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002677 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002678 if (!protocol)
2679 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002680 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002681 /* Check that a device path has not been installed before */
2682 if (!guidcmp(protocol, &efi_guid_device_path)) {
2683 struct efi_device_path *dp = protocol_interface;
2684
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002685 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2686 &old_handle));
2687 if (ret == EFI_SUCCESS &&
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002688 dp->type == DEVICE_PATH_TYPE_END) {
2689 EFI_PRINT("Path %pD already installed\n",
2690 protocol_interface);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002691 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002692 break;
2693 }
2694 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002695 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2696 EFI_NATIVE_INTERFACE,
2697 protocol_interface));
2698 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002699 break;
2700 i++;
2701 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002702 if (ret == EFI_SUCCESS)
2703 goto out;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002704
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002705 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002706 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002707 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2708 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002709 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002710 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002711 }
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002712
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002713out:
2714 efi_va_end(argptr_copy);
2715 return ret;
2716
Alexander Grafc15d9212016-03-04 01:09:59 +01002717}
2718
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002719/**
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002720 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2721 * interfaces
2722 * @handle: handle on which the protocol interfaces shall be installed
Mario Six8fac2912018-07-10 08:40:17 +02002723 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2724 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002725 *
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002726 *
2727 * This is the function for internal usage in U-Boot. For the API function
2728 * implementing the InstallMultipleProtocol service see
2729 * efi_install_multiple_protocol_interfaces_ext()
2730 *
2731 * Return: status code
2732 */
2733efi_status_t EFIAPI
2734efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2735{
2736 efi_status_t ret;
2737 efi_va_list argptr;
2738
2739 efi_va_start(argptr, handle);
2740 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2741 efi_va_end(argptr);
2742 return ret;
2743}
2744
2745/**
2746 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2747 * interfaces
2748 * @handle: handle on which the protocol interfaces shall be installed
2749 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2750 * interfaces
2751 *
2752 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002753 *
Mario Six8fac2912018-07-10 08:40:17 +02002754 * See the Unified Extensible Firmware Interface (UEFI) specification for
2755 * details.
2756 *
2757 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002758 */
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002759static efi_status_t EFIAPI
2760efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002761{
2762 EFI_ENTRY("%p", handle);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002763 efi_status_t ret;
Alexander Graf404a7c82018-06-18 17:23:05 +02002764 efi_va_list argptr;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002765
2766 efi_va_start(argptr, handle);
2767 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2768 efi_va_end(argptr);
2769 return EFI_EXIT(ret);
2770}
2771
2772/**
2773 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2774 * multiple protocol
2775 * interfaces
2776 * @handle: handle from which the protocol interfaces shall be removed
2777 * @argptr: va_list of args
2778 *
2779 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2780 * Must not be called directly
2781 *
2782 * Return: status code
2783 */
2784static efi_status_t EFIAPI
2785efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2786 efi_va_list argptr)
2787{
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002788 const efi_guid_t *protocol;
2789 void *protocol_interface;
Ilias Apalodimas3b820bc2022-11-10 10:21:24 +02002790 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002791 size_t i = 0;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002792 efi_va_list argptr_copy;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002793
2794 if (!handle)
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002795 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002796
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002797 efi_va_copy(argptr_copy, argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002798 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002799 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002800 if (!protocol)
2801 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002802 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002803 ret = efi_uninstall_protocol(handle, protocol,
2804 protocol_interface);
2805 if (ret != EFI_SUCCESS)
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002806 break;
2807 i++;
2808 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002809 if (ret == EFI_SUCCESS) {
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002810 /* If the last protocol has been removed, delete the handle. */
2811 if (list_empty(&handle->protocols)) {
2812 list_del(&handle->link);
2813 free(handle);
2814 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002815 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002816 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002817
2818 /* If an error occurred undo all changes. */
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002819 for (; i; --i) {
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002820 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2821 protocol_interface = efi_va_arg(argptr_copy, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002822 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2823 EFI_NATIVE_INTERFACE,
2824 protocol_interface));
2825 }
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002826 /*
2827 * If any errors are generated while the protocol interfaces are being
2828 * uninstalled, then the protocols uninstalled prior to the error will
2829 * be reinstalled using InstallProtocolInterface() and the status code
2830 * EFI_INVALID_PARAMETER is returned.
2831 */
2832 ret = EFI_INVALID_PARAMETER;
2833
2834out:
2835 efi_va_end(argptr_copy);
2836 return ret;
2837}
2838
2839/**
2840 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2841 * interfaces
2842 * @handle: handle from which the protocol interfaces shall be removed
2843 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2844 * interfaces
2845 *
2846 * This function implements the UninstallMultipleProtocolInterfaces service.
2847 *
2848 * This is the function for internal usage in U-Boot. For the API function
2849 * implementing the UninstallMultipleProtocolInterfaces service see
2850 * efi_uninstall_multiple_protocol_interfaces_ext()
2851 *
2852 * Return: status code
2853 */
2854efi_status_t EFIAPI
2855efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2856{
2857 efi_status_t ret;
2858 efi_va_list argptr;
2859
2860 efi_va_start(argptr, handle);
2861 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Graf404a7c82018-06-18 17:23:05 +02002862 efi_va_end(argptr);
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002863 return ret;
2864}
2865
2866/**
2867 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2868 * interfaces
2869 * @handle: handle from which the protocol interfaces shall be removed
2870 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2871 * interfaces
2872 *
2873 * This function implements the UninstallMultipleProtocolInterfaces service.
2874 *
2875 * See the Unified Extensible Firmware Interface (UEFI) specification for
2876 * details.
2877 *
2878 * Return: status code
2879 */
2880static efi_status_t EFIAPI
2881efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2882{
2883 EFI_ENTRY("%p", handle);
2884 efi_status_t ret;
2885 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002886
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03002887 efi_va_start(argptr, handle);
2888 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2889 efi_va_end(argptr);
2890 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002891}
2892
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002893/**
Mario Six8fac2912018-07-10 08:40:17 +02002894 * efi_calculate_crc32() - calculate cyclic redundancy code
2895 * @data: buffer with data
2896 * @data_size: size of buffer in bytes
2897 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002898 *
2899 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002900 *
2901 * See the Unified Extensible Firmware Interface (UEFI) specification for
2902 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002903 *
Mario Six8fac2912018-07-10 08:40:17 +02002904 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002905 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002906static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2907 efi_uintn_t data_size,
2908 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002909{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002910 efi_status_t ret = EFI_SUCCESS;
2911
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002912 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002913 if (!data || !data_size || !crc32_p) {
2914 ret = EFI_INVALID_PARAMETER;
2915 goto out;
2916 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002917 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002918out:
2919 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002920}
2921
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002922/**
Mario Six8fac2912018-07-10 08:40:17 +02002923 * efi_copy_mem() - copy memory
2924 * @destination: destination of the copy operation
2925 * @source: source of the copy operation
2926 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002927 *
2928 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002929 *
Mario Six8fac2912018-07-10 08:40:17 +02002930 * See the Unified Extensible Firmware Interface (UEFI) specification for
2931 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002932 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002933static void EFIAPI efi_copy_mem(void *destination, const void *source,
2934 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002935{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002936 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002937 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002938 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002939}
2940
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002941/**
Mario Six8fac2912018-07-10 08:40:17 +02002942 * efi_set_mem() - Fill memory with a byte value.
2943 * @buffer: buffer to fill
2944 * @size: size of buffer in bytes
2945 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002946 *
2947 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002948 *
Mario Six8fac2912018-07-10 08:40:17 +02002949 * See the Unified Extensible Firmware Interface (UEFI) specification for
2950 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002951 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002952static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002953{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002954 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002955 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002956 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002957}
2958
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002959/**
Mario Six8fac2912018-07-10 08:40:17 +02002960 * efi_protocol_open() - open protocol interface on a handle
2961 * @handler: handler of a protocol
2962 * @protocol_interface: interface implementing the protocol
2963 * @agent_handle: handle of the driver
2964 * @controller_handle: handle of the controller
2965 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002966 *
Mario Six8fac2912018-07-10 08:40:17 +02002967 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002968 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002969efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002970 struct efi_handler *handler,
2971 void **protocol_interface, void *agent_handle,
2972 void *controller_handle, uint32_t attributes)
2973{
2974 struct efi_open_protocol_info_item *item;
2975 struct efi_open_protocol_info_entry *match = NULL;
2976 bool opened_by_driver = false;
2977 bool opened_exclusive = false;
2978
2979 /* If there is no agent, only return the interface */
2980 if (!agent_handle)
2981 goto out;
2982
2983 /* For TEST_PROTOCOL ignore interface attribute */
2984 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2985 *protocol_interface = NULL;
2986
2987 /*
2988 * Check if the protocol is already opened by a driver with the same
2989 * attributes or opened exclusively
2990 */
2991 list_for_each_entry(item, &handler->open_infos, link) {
2992 if (item->info.agent_handle == agent_handle) {
2993 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2994 (item->info.attributes == attributes))
2995 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002996 } else {
2997 if (item->info.attributes &
2998 EFI_OPEN_PROTOCOL_BY_DRIVER)
2999 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003000 }
3001 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3002 opened_exclusive = true;
3003 }
3004
3005 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02003006 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3007 if (opened_exclusive)
3008 return EFI_ACCESS_DENIED;
3009 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3010 if (opened_exclusive || opened_by_driver)
3011 return EFI_ACCESS_DENIED;
3012 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003013
3014 /* Prepare exclusive opening */
3015 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3016 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003017disconnect_next:
3018 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003019 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003020 efi_status_t ret;
3021
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003022 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003023 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3024 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003025 item->info.controller_handle,
3026 item->info.agent_handle,
3027 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003028 if (ret == EFI_SUCCESS)
3029 /*
3030 * Child controllers may have been
3031 * removed from the open_infos list. So
3032 * let's restart the loop.
3033 */
3034 goto disconnect_next;
3035 else
3036 opened_by_driver = true;
3037 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003038 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02003039 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003040 if (opened_by_driver)
3041 return EFI_ACCESS_DENIED;
3042 }
3043
3044 /* Find existing entry */
3045 list_for_each_entry(item, &handler->open_infos, link) {
3046 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02003047 item->info.controller_handle == controller_handle &&
3048 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003049 match = &item->info;
3050 }
3051 /* None found, create one */
3052 if (!match) {
3053 match = efi_create_open_info(handler);
3054 if (!match)
3055 return EFI_OUT_OF_RESOURCES;
3056 }
3057
3058 match->agent_handle = agent_handle;
3059 match->controller_handle = controller_handle;
3060 match->attributes = attributes;
3061 match->open_count++;
3062
3063out:
3064 /* For TEST_PROTOCOL ignore interface attribute. */
3065 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3066 *protocol_interface = handler->protocol_interface;
3067
3068 return EFI_SUCCESS;
3069}
3070
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003071/**
Mario Six8fac2912018-07-10 08:40:17 +02003072 * efi_open_protocol() - open protocol interface on a handle
3073 * @handle: handle on which the protocol shall be opened
3074 * @protocol: GUID of the protocol
3075 * @protocol_interface: interface implementing the protocol
3076 * @agent_handle: handle of the driver
3077 * @controller_handle: handle of the controller
3078 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003079 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003080 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003081 *
Mario Six8fac2912018-07-10 08:40:17 +02003082 * See the Unified Extensible Firmware Interface (UEFI) specification for
3083 * details.
3084 *
3085 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003086 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02003087static efi_status_t EFIAPI efi_open_protocol
3088 (efi_handle_t handle, const efi_guid_t *protocol,
3089 void **protocol_interface, efi_handle_t agent_handle,
3090 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01003091{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003092 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003093 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01003094
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003095 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003096 protocol_interface, agent_handle, controller_handle,
3097 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003098
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003099 if (!handle || !protocol ||
3100 (!protocol_interface && attributes !=
3101 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02003102 goto out;
3103 }
3104
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003105 switch (attributes) {
3106 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3107 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3108 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3109 break;
3110 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3111 if (controller_handle == handle)
3112 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003113 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003114 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3115 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003116 /* Check that the controller handle is valid */
3117 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003118 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003119 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003120 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003121 /* Check that the agent handle is valid */
3122 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02003123 goto out;
3124 break;
3125 default:
3126 goto out;
3127 }
3128
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003129 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003130 switch (r) {
3131 case EFI_SUCCESS:
3132 break;
3133 case EFI_NOT_FOUND:
3134 r = EFI_UNSUPPORTED;
3135 goto out;
3136 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01003137 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02003138 }
Alexander Grafc15d9212016-03-04 01:09:59 +01003139
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01003140 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3141 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01003142out:
3143 return EFI_EXIT(r);
3144}
3145
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003146/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003147 * efi_start_image() - call the entry point of an image
3148 * @image_handle: handle of the image
3149 * @exit_data_size: size of the buffer
3150 * @exit_data: buffer to receive the exit data of the called image
3151 *
3152 * This function implements the StartImage service.
3153 *
3154 * See the Unified Extensible Firmware Interface (UEFI) specification for
3155 * details.
3156 *
3157 * Return: status code
3158 */
3159efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3160 efi_uintn_t *exit_data_size,
3161 u16 **exit_data)
3162{
3163 struct efi_loaded_image_obj *image_obj =
3164 (struct efi_loaded_image_obj *)image_handle;
3165 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003166 void *info;
3167 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003168 efi_status_t exit_status;
3169 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003170
3171 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3172
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003173 if (!efi_search_obj(image_handle))
3174 return EFI_EXIT(EFI_INVALID_PARAMETER);
3175
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003176 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02003177 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3178 return EFI_EXIT(EFI_INVALID_PARAMETER);
3179
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09003180 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3181 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3182
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003183 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3184 &info, NULL, NULL,
3185 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3186 if (ret != EFI_SUCCESS)
3187 return EFI_EXIT(EFI_INVALID_PARAMETER);
3188
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003189 image_obj->exit_data_size = exit_data_size;
3190 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003191 image_obj->exit_status = &exit_status;
3192 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003193
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003194 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3195 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09003196 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima38155ea2021-12-07 14:15:33 +09003197 if (ret == EFI_SECURITY_VIOLATION) {
3198 /*
3199 * TCG2 Protocol is installed but no TPM device found,
3200 * this is not expected.
3201 */
3202 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003203 }
3204 }
3205 }
3206
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003207 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003208 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003209 /*
3210 * We called the entry point of the child image with EFI_CALL
3211 * in the lines below. The child image called the Exit() boot
3212 * service efi_exit() which executed the long jump that brought
3213 * us to the current line. This implies that the second half
3214 * of the EFI_CALL macro has not been executed.
3215 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003216#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003217 /*
3218 * efi_exit() called efi_restore_gd(). We have to undo this
3219 * otherwise __efi_entry_check() will put the wrong value into
3220 * app_gd.
3221 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003222 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003223#endif
3224 /*
3225 * To get ready to call EFI_EXIT below we have to execute the
3226 * missed out steps of EFI_CALL.
3227 */
3228 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003229 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003230 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003231 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003232 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003233 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003234 }
3235
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003236 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003237 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003238 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003239 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3240
3241 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003242 * Control is returned from a started UEFI image either by calling
3243 * Exit() (where exit data can be provided) or by simply returning from
3244 * the entry point. In the latter case call Exit() on behalf of the
3245 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003246 */
3247 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3248}
3249
3250/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003251 * efi_delete_image() - delete loaded image from memory)
3252 *
3253 * @image_obj: handle of the loaded image
3254 * @loaded_image_protocol: loaded image protocol
3255 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003256static efi_status_t efi_delete_image
3257 (struct efi_loaded_image_obj *image_obj,
3258 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003259{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003260 struct efi_object *efiobj;
3261 efi_status_t r, ret = EFI_SUCCESS;
3262
3263close_next:
3264 list_for_each_entry(efiobj, &efi_obj_list, link) {
3265 struct efi_handler *protocol;
3266
3267 list_for_each_entry(protocol, &efiobj->protocols, link) {
3268 struct efi_open_protocol_info_item *info;
3269
3270 list_for_each_entry(info, &protocol->open_infos, link) {
3271 if (info->info.agent_handle !=
3272 (efi_handle_t)image_obj)
3273 continue;
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003274 r = efi_close_protocol(
3275 efiobj, &protocol->guid,
3276 info->info.agent_handle,
3277 info->info.controller_handle);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003278 if (r != EFI_SUCCESS)
3279 ret = r;
3280 /*
3281 * Closing protocols may results in further
3282 * items being deleted. To play it safe loop
3283 * over all elements again.
3284 */
3285 goto close_next;
3286 }
3287 }
3288 }
3289
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003290 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3291 efi_size_in_pages(loaded_image_protocol->image_size));
3292 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003293
3294 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003295}
3296
3297/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003298 * efi_unload_image() - unload an EFI image
3299 * @image_handle: handle of the image to be unloaded
3300 *
3301 * This function implements the UnloadImage service.
3302 *
3303 * See the Unified Extensible Firmware Interface (UEFI) specification for
3304 * details.
3305 *
3306 * Return: status code
3307 */
3308efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3309{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003310 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003311 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003312 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003313
3314 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003315
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003316 efiobj = efi_search_obj(image_handle);
3317 if (!efiobj) {
3318 ret = EFI_INVALID_PARAMETER;
3319 goto out;
3320 }
3321 /* Find the loaded image protocol */
3322 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3323 (void **)&loaded_image_protocol,
3324 NULL, NULL,
3325 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3326 if (ret != EFI_SUCCESS) {
3327 ret = EFI_INVALID_PARAMETER;
3328 goto out;
3329 }
3330 switch (efiobj->type) {
3331 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3332 /* Call the unload function */
3333 if (!loaded_image_protocol->unload) {
3334 ret = EFI_UNSUPPORTED;
3335 goto out;
3336 }
3337 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3338 if (ret != EFI_SUCCESS)
3339 goto out;
3340 break;
3341 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3342 break;
3343 default:
3344 ret = EFI_INVALID_PARAMETER;
3345 goto out;
3346 }
3347 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3348 loaded_image_protocol);
3349out:
3350 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003351}
3352
3353/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003354 * efi_update_exit_data() - fill exit data parameters of StartImage()
3355 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003356 * @image_obj: image handle
3357 * @exit_data_size: size of the exit data buffer
3358 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003359 * Return: status code
3360 */
3361static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3362 efi_uintn_t exit_data_size,
3363 u16 *exit_data)
3364{
3365 efi_status_t ret;
3366
3367 /*
3368 * If exit_data is not provided to StartImage(), exit_data_size must be
3369 * ignored.
3370 */
3371 if (!image_obj->exit_data)
3372 return EFI_SUCCESS;
3373 if (image_obj->exit_data_size)
3374 *image_obj->exit_data_size = exit_data_size;
3375 if (exit_data_size && exit_data) {
3376 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3377 exit_data_size,
3378 (void **)image_obj->exit_data);
3379 if (ret != EFI_SUCCESS)
3380 return ret;
3381 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3382 } else {
3383 image_obj->exit_data = NULL;
3384 }
3385 return EFI_SUCCESS;
3386}
3387
3388/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003389 * efi_exit() - leave an EFI application or driver
3390 * @image_handle: handle of the application or driver that is exiting
3391 * @exit_status: status code
3392 * @exit_data_size: size of the buffer in bytes
3393 * @exit_data: buffer with data describing an error
3394 *
3395 * This function implements the Exit service.
3396 *
3397 * See the Unified Extensible Firmware Interface (UEFI) specification for
3398 * details.
3399 *
3400 * Return: status code
3401 */
3402static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3403 efi_status_t exit_status,
3404 efi_uintn_t exit_data_size,
3405 u16 *exit_data)
3406{
3407 /*
3408 * TODO: We should call the unload procedure of the loaded
3409 * image protocol.
3410 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003411 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003412 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003413 struct efi_loaded_image_obj *image_obj =
3414 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003415 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003416
3417 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3418 exit_data_size, exit_data);
3419
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003420 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003421 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003422 (void **)&loaded_image_protocol,
3423 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003424 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003425 if (ret != EFI_SUCCESS) {
3426 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003427 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003428 }
3429
3430 /* Unloading of unstarted images */
3431 switch (image_obj->header.type) {
3432 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3433 break;
3434 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3435 efi_delete_image(image_obj, loaded_image_protocol);
3436 ret = EFI_SUCCESS;
3437 goto out;
3438 default:
3439 /* Handle does not refer to loaded image */
3440 ret = EFI_INVALID_PARAMETER;
3441 goto out;
3442 }
3443 /* A started image can only be unloaded it is the last one started. */
3444 if (image_handle != current_image) {
3445 ret = EFI_INVALID_PARAMETER;
3446 goto out;
3447 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003448
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003449 /* Exit data is only foreseen in case of failure. */
3450 if (exit_status != EFI_SUCCESS) {
3451 ret = efi_update_exit_data(image_obj, exit_data_size,
3452 exit_data);
3453 /* Exiting has priority. Don't return error to caller. */
3454 if (ret != EFI_SUCCESS)
3455 EFI_PRINT("%s: out of memory\n", __func__);
3456 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003457 /* efi_delete_image() frees image_obj. Copy before the call. */
3458 exit_jmp = image_obj->exit_jmp;
3459 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003460 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3461 exit_status != EFI_SUCCESS)
3462 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003463
Masahisa Kojima8173cd42021-08-13 16:12:40 +09003464 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3465 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3466 ret = efi_tcg2_measure_efi_app_exit();
3467 if (ret != EFI_SUCCESS) {
3468 log_warning("tcg2 measurement fails(0x%lx)\n",
3469 ret);
3470 }
3471 }
3472 }
3473
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003474 /* Make sure entry/exit counts for EFI world cross-overs match */
3475 EFI_EXIT(exit_status);
3476
3477 /*
3478 * But longjmp out with the U-Boot gd, not the application's, as
3479 * the other end is a setjmp call inside EFI context.
3480 */
3481 efi_restore_gd();
3482
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003483 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003484
3485 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003486out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003487 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003488}
3489
3490/**
Mario Six8fac2912018-07-10 08:40:17 +02003491 * efi_handle_protocol() - get interface of a protocol on a handle
3492 * @handle: handle on which the protocol shall be opened
3493 * @protocol: GUID of the protocol
3494 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003495 *
3496 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003497 *
3498 * See the Unified Extensible Firmware Interface (UEFI) specification for
3499 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003500 *
Mario Six8fac2912018-07-10 08:40:17 +02003501 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003502 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003503efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3504 const efi_guid_t *protocol,
3505 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003506{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003507 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003508 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003509}
3510
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003511/**
Mario Six8fac2912018-07-10 08:40:17 +02003512 * efi_bind_controller() - bind a single driver to a controller
3513 * @controller_handle: controller handle
3514 * @driver_image_handle: driver handle
3515 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003516 *
Mario Six8fac2912018-07-10 08:40:17 +02003517 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003518 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003519static efi_status_t efi_bind_controller(
3520 efi_handle_t controller_handle,
3521 efi_handle_t driver_image_handle,
3522 struct efi_device_path *remain_device_path)
3523{
3524 struct efi_driver_binding_protocol *binding_protocol;
3525 efi_status_t r;
3526
3527 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3528 &efi_guid_driver_binding_protocol,
3529 (void **)&binding_protocol,
3530 driver_image_handle, NULL,
3531 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3532 if (r != EFI_SUCCESS)
3533 return r;
3534 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3535 controller_handle,
3536 remain_device_path));
3537 if (r == EFI_SUCCESS)
3538 r = EFI_CALL(binding_protocol->start(binding_protocol,
3539 controller_handle,
3540 remain_device_path));
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003541 efi_close_protocol(driver_image_handle,
3542 &efi_guid_driver_binding_protocol,
3543 driver_image_handle, NULL);
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003544 return r;
3545}
3546
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003547/**
Mario Six8fac2912018-07-10 08:40:17 +02003548 * efi_connect_single_controller() - connect a single driver to a controller
3549 * @controller_handle: controller
3550 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003551 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003552 *
Mario Six8fac2912018-07-10 08:40:17 +02003553 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003554 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003555static efi_status_t efi_connect_single_controller(
3556 efi_handle_t controller_handle,
3557 efi_handle_t *driver_image_handle,
3558 struct efi_device_path *remain_device_path)
3559{
3560 efi_handle_t *buffer;
3561 size_t count;
3562 size_t i;
3563 efi_status_t r;
3564 size_t connected = 0;
3565
3566 /* Get buffer with all handles with driver binding protocol */
3567 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3568 &efi_guid_driver_binding_protocol,
3569 NULL, &count, &buffer));
3570 if (r != EFI_SUCCESS)
3571 return r;
3572
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003573 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003574 if (driver_image_handle) {
3575 for (; *driver_image_handle; ++driver_image_handle) {
3576 for (i = 0; i < count; ++i) {
3577 if (buffer[i] == *driver_image_handle) {
3578 buffer[i] = NULL;
3579 r = efi_bind_controller(
3580 controller_handle,
3581 *driver_image_handle,
3582 remain_device_path);
3583 /*
3584 * For drivers that do not support the
3585 * controller or are already connected
3586 * we receive an error code here.
3587 */
3588 if (r == EFI_SUCCESS)
3589 ++connected;
3590 }
3591 }
3592 }
3593 }
3594
3595 /*
3596 * TODO: Some overrides are not yet implemented:
3597 * - Platform Driver Override
3598 * - Driver Family Override Search
3599 * - Bus Specific Driver Override
3600 */
3601
3602 /* Driver Binding Search */
3603 for (i = 0; i < count; ++i) {
3604 if (buffer[i]) {
3605 r = efi_bind_controller(controller_handle,
3606 buffer[i],
3607 remain_device_path);
3608 if (r == EFI_SUCCESS)
3609 ++connected;
3610 }
3611 }
3612
3613 efi_free_pool(buffer);
3614 if (!connected)
3615 return EFI_NOT_FOUND;
3616 return EFI_SUCCESS;
3617}
3618
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003619/**
Mario Six8fac2912018-07-10 08:40:17 +02003620 * efi_connect_controller() - connect a controller to a driver
3621 * @controller_handle: handle of the controller
3622 * @driver_image_handle: handle of the driver
3623 * @remain_device_path: device path of a child controller
3624 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003625 *
3626 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003627 *
3628 * See the Unified Extensible Firmware Interface (UEFI) specification for
3629 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003630 *
3631 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003632 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003633 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3634 *
Mario Six8fac2912018-07-10 08:40:17 +02003635 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003636 */
3637static efi_status_t EFIAPI efi_connect_controller(
3638 efi_handle_t controller_handle,
3639 efi_handle_t *driver_image_handle,
3640 struct efi_device_path *remain_device_path,
3641 bool recursive)
3642{
3643 efi_status_t r;
3644 efi_status_t ret = EFI_NOT_FOUND;
3645 struct efi_object *efiobj;
3646
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003647 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003648 remain_device_path, recursive);
3649
3650 efiobj = efi_search_obj(controller_handle);
3651 if (!efiobj) {
3652 ret = EFI_INVALID_PARAMETER;
3653 goto out;
3654 }
3655
3656 r = efi_connect_single_controller(controller_handle,
3657 driver_image_handle,
3658 remain_device_path);
3659 if (r == EFI_SUCCESS)
3660 ret = EFI_SUCCESS;
3661 if (recursive) {
3662 struct efi_handler *handler;
3663 struct efi_open_protocol_info_item *item;
3664
3665 list_for_each_entry(handler, &efiobj->protocols, link) {
3666 list_for_each_entry(item, &handler->open_infos, link) {
3667 if (item->info.attributes &
3668 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3669 r = EFI_CALL(efi_connect_controller(
3670 item->info.controller_handle,
3671 driver_image_handle,
3672 remain_device_path,
3673 recursive));
3674 if (r == EFI_SUCCESS)
3675 ret = EFI_SUCCESS;
3676 }
3677 }
3678 }
3679 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003680 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003681 if (ret != EFI_SUCCESS && remain_device_path &&
3682 remain_device_path->type == DEVICE_PATH_TYPE_END)
3683 ret = EFI_SUCCESS;
3684out:
3685 return EFI_EXIT(ret);
3686}
3687
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003688/**
Mario Six8fac2912018-07-10 08:40:17 +02003689 * efi_reinstall_protocol_interface() - reinstall protocol interface
3690 * @handle: handle on which the protocol shall be reinstalled
3691 * @protocol: GUID of the protocol to be installed
3692 * @old_interface: interface to be removed
3693 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003694 *
3695 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003696 *
3697 * See the Unified Extensible Firmware Interface (UEFI) specification for
3698 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003699 *
3700 * The old interface is uninstalled. The new interface is installed.
3701 * Drivers are connected.
3702 *
Mario Six8fac2912018-07-10 08:40:17 +02003703 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003704 */
3705static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3706 efi_handle_t handle, const efi_guid_t *protocol,
3707 void *old_interface, void *new_interface)
3708{
3709 efi_status_t ret;
3710
Heinrich Schuchardt282249d2022-01-16 14:15:31 +01003711 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003712 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003713
3714 /* Uninstall protocol but do not delete handle */
3715 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003716 if (ret != EFI_SUCCESS)
3717 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003718
3719 /* Install the new protocol */
3720 ret = efi_add_protocol(handle, protocol, new_interface);
3721 /*
3722 * The UEFI spec does not specify what should happen to the handle
3723 * if in case of an error no protocol interface remains on the handle.
3724 * So let's do nothing here.
3725 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003726 if (ret != EFI_SUCCESS)
3727 goto out;
3728 /*
3729 * The returned status code has to be ignored.
3730 * Do not create an error if no suitable driver for the handle exists.
3731 */
3732 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3733out:
3734 return EFI_EXIT(ret);
3735}
3736
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003737/**
Mario Six8fac2912018-07-10 08:40:17 +02003738 * efi_get_child_controllers() - get all child controllers associated to a driver
3739 * @efiobj: handle of the controller
3740 * @driver_handle: handle of the driver
3741 * @number_of_children: number of child controllers
3742 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003743 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003744 * The allocated buffer has to be freed with free().
3745 *
Mario Six8fac2912018-07-10 08:40:17 +02003746 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003747 */
3748static efi_status_t efi_get_child_controllers(
3749 struct efi_object *efiobj,
3750 efi_handle_t driver_handle,
3751 efi_uintn_t *number_of_children,
3752 efi_handle_t **child_handle_buffer)
3753{
3754 struct efi_handler *handler;
3755 struct efi_open_protocol_info_item *item;
3756 efi_uintn_t count = 0, i;
3757 bool duplicate;
3758
3759 /* Count all child controller associations */
3760 list_for_each_entry(handler, &efiobj->protocols, link) {
3761 list_for_each_entry(item, &handler->open_infos, link) {
3762 if (item->info.agent_handle == driver_handle &&
3763 item->info.attributes &
3764 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3765 ++count;
3766 }
3767 }
3768 /*
3769 * Create buffer. In case of duplicate child controller assignments
3770 * the buffer will be too large. But that does not harm.
3771 */
3772 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003773 if (!count)
3774 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003775 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3776 if (!*child_handle_buffer)
3777 return EFI_OUT_OF_RESOURCES;
3778 /* Copy unique child handles */
3779 list_for_each_entry(handler, &efiobj->protocols, link) {
3780 list_for_each_entry(item, &handler->open_infos, link) {
3781 if (item->info.agent_handle == driver_handle &&
3782 item->info.attributes &
3783 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3784 /* Check this is a new child controller */
3785 duplicate = false;
3786 for (i = 0; i < *number_of_children; ++i) {
3787 if ((*child_handle_buffer)[i] ==
3788 item->info.controller_handle)
3789 duplicate = true;
3790 }
3791 /* Copy handle to buffer */
3792 if (!duplicate) {
3793 i = (*number_of_children)++;
3794 (*child_handle_buffer)[i] =
3795 item->info.controller_handle;
3796 }
3797 }
3798 }
3799 }
3800 return EFI_SUCCESS;
3801}
3802
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003803/**
Mario Six8fac2912018-07-10 08:40:17 +02003804 * efi_disconnect_controller() - disconnect a controller from a driver
3805 * @controller_handle: handle of the controller
3806 * @driver_image_handle: handle of the driver
3807 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003808 *
3809 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003810 *
3811 * See the Unified Extensible Firmware Interface (UEFI) specification for
3812 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003813 *
Mario Six8fac2912018-07-10 08:40:17 +02003814 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003815 */
3816static efi_status_t EFIAPI efi_disconnect_controller(
3817 efi_handle_t controller_handle,
3818 efi_handle_t driver_image_handle,
3819 efi_handle_t child_handle)
3820{
3821 struct efi_driver_binding_protocol *binding_protocol;
3822 efi_handle_t *child_handle_buffer = NULL;
3823 size_t number_of_children = 0;
3824 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003825 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003826 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003827
3828 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3829 child_handle);
3830
3831 efiobj = efi_search_obj(controller_handle);
3832 if (!efiobj) {
3833 r = EFI_INVALID_PARAMETER;
3834 goto out;
3835 }
3836
3837 if (child_handle && !efi_search_obj(child_handle)) {
3838 r = EFI_INVALID_PARAMETER;
3839 goto out;
3840 }
3841
3842 /* If no driver handle is supplied, disconnect all drivers */
3843 if (!driver_image_handle) {
3844 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3845 goto out;
3846 }
3847
3848 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003849 r = efi_get_child_controllers(efiobj,
3850 driver_image_handle,
3851 &number_of_children,
3852 &child_handle_buffer);
3853 if (r != EFI_SUCCESS)
3854 return r;
3855 sole_child = (number_of_children == 1);
3856
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003857 if (child_handle) {
3858 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003859 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003860 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003861 }
3862
3863 /* Get the driver binding protocol */
3864 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3865 &efi_guid_driver_binding_protocol,
3866 (void **)&binding_protocol,
3867 driver_image_handle, NULL,
3868 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003869 if (r != EFI_SUCCESS) {
3870 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003871 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003872 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003873 /* Remove the children */
3874 if (number_of_children) {
3875 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3876 controller_handle,
3877 number_of_children,
3878 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003879 if (r != EFI_SUCCESS) {
3880 r = EFI_DEVICE_ERROR;
3881 goto out;
3882 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003883 }
3884 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003885 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003886 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3887 controller_handle,
3888 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003889 if (r != EFI_SUCCESS) {
3890 r = EFI_DEVICE_ERROR;
3891 goto out;
3892 }
3893 }
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003894 efi_close_protocol(driver_image_handle,
3895 &efi_guid_driver_binding_protocol,
3896 driver_image_handle, NULL);
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003897 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003898out:
3899 if (!child_handle)
3900 free(child_handle_buffer);
3901 return EFI_EXIT(r);
3902}
3903
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003904static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003905 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003906 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3907 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003908 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003909 },
3910 .raise_tpl = efi_raise_tpl,
3911 .restore_tpl = efi_restore_tpl,
3912 .allocate_pages = efi_allocate_pages_ext,
3913 .free_pages = efi_free_pages_ext,
3914 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003915 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003916 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003917 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003918 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003919 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003920 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003921 .close_event = efi_close_event,
3922 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003923 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003924 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003925 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003926 .handle_protocol = efi_handle_protocol,
3927 .reserved = NULL,
3928 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003929 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003930 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003931 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003932 .load_image = efi_load_image,
3933 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003934 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003935 .unload_image = efi_unload_image,
3936 .exit_boot_services = efi_exit_boot_services,
3937 .get_next_monotonic_count = efi_get_next_monotonic_count,
3938 .stall = efi_stall,
3939 .set_watchdog_timer = efi_set_watchdog_timer,
3940 .connect_controller = efi_connect_controller,
3941 .disconnect_controller = efi_disconnect_controller,
3942 .open_protocol = efi_open_protocol,
Heinrich Schuchardt5e394332022-10-07 15:18:15 +02003943 .close_protocol = efi_close_protocol_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003944 .open_protocol_information = efi_open_protocol_information,
3945 .protocols_per_handle = efi_protocols_per_handle,
3946 .locate_handle_buffer = efi_locate_handle_buffer,
3947 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003948 .install_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003949 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003950 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +03003951 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003952 .calculate_crc32 = efi_calculate_crc32,
3953 .copy_mem = efi_copy_mem,
3954 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003955 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003956};
3957
Simon Glass90975372022-01-23 12:55:12 -07003958static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003959
Alexander Graf393dd912016-10-14 13:45:30 +02003960struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003961 .hdr = {
3962 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003963 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003964 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003965 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003966 .fw_vendor = firmware_vendor,
3967 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003968 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003969 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003970 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003971};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003972
3973/**
3974 * efi_initialize_system_table() - Initialize system table
3975 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003976 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003977 */
3978efi_status_t efi_initialize_system_table(void)
3979{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003980 efi_status_t ret;
3981
3982 /* Allocate configuration table array */
3983 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3984 EFI_MAX_CONFIGURATION_TABLES *
3985 sizeof(struct efi_configuration_table),
3986 (void **)&systab.tables);
3987
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003988 /*
3989 * These entries will be set to NULL in ExitBootServices(). To avoid
3990 * relocation in SetVirtualAddressMap(), set them dynamically.
3991 */
Heinrich Schuchardted647682023-01-04 05:56:09 +01003992 systab.con_in_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003993 systab.con_in = &efi_con_in;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003994 systab.con_out_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003995 systab.con_out = &efi_con_out;
Heinrich Schuchardted647682023-01-04 05:56:09 +01003996 systab.stderr_handle = efi_root;
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003997 systab.std_err = &efi_con_out;
3998 systab.boottime = &efi_boot_services;
3999
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02004000 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004001 efi_update_table_header_crc32(&systab.hdr);
4002 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4003 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02004004
4005 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02004006}