blob: 1951291747cd5f245d7f73ccafe2083af2888dac [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafbee91162016-03-04 01:09:59 +01004 *
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Ilias Apalodimas19763ea2020-10-22 01:04:20 +03008#include <bootm.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +02009#include <div64.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030010#include <dm/device.h>
11#include <dm/root.h>
Alexander Grafbee91162016-03-04 01:09:59 +010012#include <efi_loader.h>
Simon Glass36bf4462019-11-14 12:57:42 -070013#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Alexander Grafbee91162016-03-04 01:09:59 +010015#include <malloc.h>
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +020016#include <pe.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030017#include <time.h>
Simon Glass3db71102019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030019#include <usb.h>
Alexander Grafbee91162016-03-04 01:09:59 +010020#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
AKASHI Takahiro86a3d432021-07-20 14:57:02 +090022#include <asm/setjmp.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030023#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010024
25DECLARE_GLOBAL_DATA_PTR;
26
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020027/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010028static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020029
Alexander Grafbee91162016-03-04 01:09:59 +010030/* This list contains all the EFI objects our payload has access to */
31LIST_HEAD(efi_obj_list);
32
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010033/* List of all events */
Heinrich Schuchardt14b40482019-07-11 20:15:09 +020034__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010035
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020036/* List of queued events */
Bin Mengee238302023-04-05 20:15:17 +080037static LIST_HEAD(efi_event_queue);
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020038
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +020039/* Flag to disable timer activity in ExitBootServices() */
40static bool timers_enabled = true;
41
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +010042/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
43bool efi_st_keep_devices;
44
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020045/* List of all events registered by RegisterProtocolNotify() */
Bin Mengee238302023-04-05 20:15:17 +080046static LIST_HEAD(efi_register_notify_events);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020047
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010048/* Handle of the currently executing image */
49static efi_handle_t current_image;
50
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020051#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +010052/*
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020053 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafbee91162016-03-04 01:09:59 +010054 * fixed when compiling U-Boot. However, the payload does not know about that
55 * restriction so we need to manually swap its and our view of that register on
56 * EFI callback entry/exit.
57 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +020058static volatile gd_t *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060059#endif
Alexander Grafbee91162016-03-04 01:09:59 +010060
Ilias Apalodimas54edc372023-07-24 13:17:36 +030061static efi_status_t efi_uninstall_protocol
62 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +030063 void *protocol_interface, bool preserve);
Ilias Apalodimas54edc372023-07-24 13:17:36 +030064
Heinrich Schuchardt914df752019-02-09 14:10:39 +010065/* 1 if inside U-Boot code, 0 if inside EFI payload code */
66static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040067static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010068/* GUID of the device tree table */
69const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010070/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
71const efi_guid_t efi_guid_driver_binding_protocol =
72 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040073
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010074/* event group ExitBootServices() invoked */
75const efi_guid_t efi_guid_event_group_exit_boot_services =
76 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
Heinrich Schuchardt43eaf5b2021-11-16 18:46:27 +010077/* event group before ExitBootServices() invoked */
78const efi_guid_t efi_guid_event_group_before_exit_boot_services =
79 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010080/* event group SetVirtualAddressMap() invoked */
81const efi_guid_t efi_guid_event_group_virtual_address_change =
82 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
83/* event group memory map changed */
84const efi_guid_t efi_guid_event_group_memory_map_change =
85 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
86/* event group boot manager about to boot */
87const efi_guid_t efi_guid_event_group_ready_to_boot =
88 EFI_EVENT_GROUP_READY_TO_BOOT;
89/* event group ResetSystem() invoked (before ExitBootServices) */
90const efi_guid_t efi_guid_event_group_reset_system =
91 EFI_EVENT_GROUP_RESET_SYSTEM;
Masahisa Kojimae0d1a1e2023-11-10 13:25:39 +090092/* event group return to efibootmgr */
93const efi_guid_t efi_guid_event_group_return_to_efibootmgr =
94 EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR;
Heinrich Schuchardtb6f11092020-12-04 03:33:41 +010095/* GUIDs of the Load File and Load File2 protocols */
96const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
97const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090098/* GUID of the SMBIOS table */
99const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +0100100
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100101static efi_status_t EFIAPI efi_disconnect_controller(
102 efi_handle_t controller_handle,
103 efi_handle_t driver_image_handle,
104 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +0100105
Ilias Apalodimas239d59a2023-06-20 09:19:28 +0300106static
107efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
108 efi_handle_t *driver_image_handle,
109 struct efi_device_path *remain_device_path,
110 bool recursive);
111
Rob Clarkc160d2f2017-07-27 08:04:18 -0400112/* Called on every callback entry */
113int __efi_entry_check(void)
114{
115 int ret = entry_count++ == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200116#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clarkc160d2f2017-07-27 08:04:18 -0400117 assert(efi_gd);
118 app_gd = gd;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200119 set_gd(efi_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400120#endif
121 return ret;
122}
123
124/* Called on every callback exit */
125int __efi_exit_check(void)
126{
127 int ret = --entry_count == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200128#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200129 set_gd(app_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400130#endif
131 return ret;
132}
133
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200134/**
135 * efi_save_gd() - save global data register
136 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200137 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200138 * As this register may be overwritten by an EFI payload we save it here
139 * and restore it on every callback entered.
140 *
141 * This function is called after relocation from initr_reloc_global_data().
142 */
Alexander Grafbee91162016-03-04 01:09:59 +0100143void efi_save_gd(void)
144{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200145#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100146 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600147#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100148}
149
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200150/**
151 * efi_restore_gd() - restore global data register
152 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200153 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200154 * Restore it after returning from the UEFI world to the value saved via
155 * efi_save_gd().
Rob Clarkc160d2f2017-07-27 08:04:18 -0400156 */
Alexander Grafbee91162016-03-04 01:09:59 +0100157void efi_restore_gd(void)
158{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200159#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100160 /* Only restore if we're already in EFI context */
161 if (!efi_gd)
162 return;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200163 set_gd(efi_gd);
Simon Glass65e4c0b2016-09-25 15:27:35 -0600164#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100165}
166
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200167/**
Mario Six78a88f72018-07-10 08:40:17 +0200168 * indent_string() - returns a string for indenting with two spaces per level
169 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100170 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200171 * A maximum of ten indent levels is supported. Higher indent levels will be
172 * truncated.
173 *
Mario Six78a88f72018-07-10 08:40:17 +0200174 * Return: A string for indenting with two spaces per level is
175 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400176 */
177static const char *indent_string(int level)
178{
179 const char *indent = " ";
180 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100181
Rob Clarkaf65db82017-07-27 08:04:19 -0400182 level = min(max, level * 2);
183 return &indent[max - level];
184}
185
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200186const char *__efi_nesting(void)
187{
188 return indent_string(nesting_level);
189}
190
Rob Clarkaf65db82017-07-27 08:04:19 -0400191const char *__efi_nesting_inc(void)
192{
193 return indent_string(nesting_level++);
194}
195
196const char *__efi_nesting_dec(void)
197{
198 return indent_string(--nesting_level);
199}
200
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200201/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200202 * efi_event_is_queued() - check if an event is queued
203 *
204 * @event: event
205 * Return: true if event is queued
206 */
207static bool efi_event_is_queued(struct efi_event *event)
208{
209 return !!event->queue_link.next;
210}
211
212/**
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300213 * efi_purge_handle() - Clean the deleted handle from the various lists
214 * @handle: handle to remove
215 *
216 * Return: status code
217 */
218static efi_status_t efi_purge_handle(efi_handle_t handle)
219{
220 struct efi_register_notify_event *item;
221
222 if (!list_empty(&handle->protocols))
223 return EFI_ACCESS_DENIED;
224 /* The handle is about to be freed. Remove it from events */
225 list_for_each_entry(item, &efi_register_notify_events, link) {
226 struct efi_protocol_notification *hitem, *hnext;
227
228 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
229 if (handle == hitem->handle) {
230 list_del(&hitem->link);
231 free(hitem);
232 }
233 }
234 }
235 /* The last protocol has been removed, delete the handle. */
236 list_del(&handle->link);
237 free(handle);
238
239 return EFI_SUCCESS;
240}
241
242/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200243 * efi_process_event_queue() - process event queue
244 */
245static void efi_process_event_queue(void)
246{
247 while (!list_empty(&efi_event_queue)) {
248 struct efi_event *event;
249 efi_uintn_t old_tpl;
250
251 event = list_first_entry(&efi_event_queue, struct efi_event,
252 queue_link);
253 if (efi_tpl >= event->notify_tpl)
254 return;
255 list_del(&event->queue_link);
256 event->queue_link.next = NULL;
257 event->queue_link.prev = NULL;
258 /* Events must be executed at the event's TPL */
259 old_tpl = efi_tpl;
260 efi_tpl = event->notify_tpl;
261 EFI_CALL_VOID(event->notify_function(event,
262 event->notify_context));
263 efi_tpl = old_tpl;
264 if (event->type == EVT_NOTIFY_SIGNAL)
265 event->is_signaled = 0;
266 }
267}
268
269/**
Mario Six78a88f72018-07-10 08:40:17 +0200270 * efi_queue_event() - queue an EFI event
271 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200272 *
273 * This function queues the notification function of the event for future
274 * execution.
275 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200276 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200277static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200278{
Heinrich Schuchardt2b8568f2020-03-06 21:56:10 +0100279 struct efi_event *item;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200280
281 if (!event->notify_function)
282 return;
283
284 if (!efi_event_is_queued(event)) {
285 /*
286 * Events must be notified in order of decreasing task priority
287 * level. Insert the new event accordingly.
288 */
289 list_for_each_entry(item, &efi_event_queue, queue_link) {
290 if (item->notify_tpl < event->notify_tpl) {
291 list_add_tail(&event->queue_link,
292 &item->queue_link);
293 event = NULL;
294 break;
295 }
296 }
297 if (event)
298 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardtb7d186f2020-12-28 00:25:34 +0100299 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200300 }
301}
302
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200303/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200304 * is_valid_tpl() - check if the task priority level is valid
305 *
306 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200307 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200308 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100309static efi_status_t is_valid_tpl(efi_uintn_t tpl)
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200310{
311 switch (tpl) {
312 case TPL_APPLICATION:
313 case TPL_CALLBACK:
314 case TPL_NOTIFY:
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200315 return EFI_SUCCESS;
316 default:
317 return EFI_INVALID_PARAMETER;
318 }
319}
320
321/**
Mario Six78a88f72018-07-10 08:40:17 +0200322 * efi_signal_event() - signal an EFI event
323 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100324 *
Heinrich Schuchardt2a0f80f2020-12-28 00:59:09 +0100325 * This function signals an event. If the event belongs to an event group, all
326 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100327 * their notification function is queued.
328 *
329 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100330 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200331void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100332{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200333 if (event->is_signaled)
334 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100335 if (event->group) {
336 struct efi_event *evt;
337
338 /*
339 * The signaled state has to set before executing any
340 * notification function
341 */
342 list_for_each_entry(evt, &efi_events, link) {
343 if (!evt->group || guidcmp(evt->group, event->group))
344 continue;
345 if (evt->is_signaled)
346 continue;
347 evt->is_signaled = true;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100348 }
349 list_for_each_entry(evt, &efi_events, link) {
350 if (!evt->group || guidcmp(evt->group, event->group))
351 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200352 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100353 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200354 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100355 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200356 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100357 }
358}
359
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200360/**
Mario Six78a88f72018-07-10 08:40:17 +0200361 * efi_raise_tpl() - raise the task priority level
362 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200363 *
364 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200365 *
Mario Six78a88f72018-07-10 08:40:17 +0200366 * See the Unified Extensible Firmware Interface (UEFI) specification for
367 * details.
368 *
369 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200370 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100371static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100372{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100373 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200374
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200375 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200376
377 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200378 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200379 efi_tpl = new_tpl;
380 if (efi_tpl > TPL_HIGH_LEVEL)
381 efi_tpl = TPL_HIGH_LEVEL;
382
383 EFI_EXIT(EFI_SUCCESS);
384 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100385}
386
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200387/**
Mario Six78a88f72018-07-10 08:40:17 +0200388 * efi_restore_tpl() - lower the task priority level
389 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200390 *
391 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200392 *
Mario Six78a88f72018-07-10 08:40:17 +0200393 * See the Unified Extensible Firmware Interface (UEFI) specification for
394 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200395 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100396static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100397{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200398 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200399
400 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200401 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200402 efi_tpl = old_tpl;
403 if (efi_tpl > TPL_HIGH_LEVEL)
404 efi_tpl = TPL_HIGH_LEVEL;
405
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100406 /*
407 * Lowering the TPL may have made queued events eligible for execution.
408 */
409 efi_timer_check();
410
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200411 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100412}
413
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200414/**
Mario Six78a88f72018-07-10 08:40:17 +0200415 * efi_allocate_pages_ext() - allocate memory pages
416 * @type: type of allocation to be performed
417 * @memory_type: usage type of the allocated memory
418 * @pages: number of pages to be allocated
419 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200420 *
421 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200422 *
Mario Six78a88f72018-07-10 08:40:17 +0200423 * See the Unified Extensible Firmware Interface (UEFI) specification for
424 * details.
425 *
426 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200427 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900428static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100429 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900430 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100431{
432 efi_status_t r;
433
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100434 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100435 r = efi_allocate_pages(type, memory_type, pages, memory);
436 return EFI_EXIT(r);
437}
438
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200439/**
Mario Six78a88f72018-07-10 08:40:17 +0200440 * efi_free_pages_ext() - Free memory pages.
441 * @memory: start of the memory area to be freed
442 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200443 *
444 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200445 *
Mario Six78a88f72018-07-10 08:40:17 +0200446 * See the Unified Extensible Firmware Interface (UEFI) specification for
447 * details.
448 *
449 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200450 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900451static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100452 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100453{
454 efi_status_t r;
455
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900456 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100457 r = efi_free_pages(memory, pages);
458 return EFI_EXIT(r);
459}
460
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200461/**
Mario Six78a88f72018-07-10 08:40:17 +0200462 * efi_get_memory_map_ext() - get map describing memory usage
463 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
464 * on exit the size of the copied memory map
465 * @memory_map: buffer to which the memory map is written
466 * @map_key: key for the memory map
467 * @descriptor_size: size of an individual memory descriptor
468 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200469 *
470 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200471 *
Mario Six78a88f72018-07-10 08:40:17 +0200472 * See the Unified Extensible Firmware Interface (UEFI) specification for
473 * details.
474 *
475 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200476 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900477static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100478 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900479 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100480 efi_uintn_t *map_key,
481 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900482 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100483{
484 efi_status_t r;
485
486 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
487 map_key, descriptor_size, descriptor_version);
488 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
489 descriptor_size, descriptor_version);
490 return EFI_EXIT(r);
491}
492
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200493/**
Mario Six78a88f72018-07-10 08:40:17 +0200494 * efi_allocate_pool_ext() - allocate memory from pool
495 * @pool_type: type of the pool from which memory is to be allocated
496 * @size: number of bytes to be allocated
497 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200498 *
499 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200500 *
Mario Six78a88f72018-07-10 08:40:17 +0200501 * See the Unified Extensible Firmware Interface (UEFI) specification for
502 * details.
503 *
504 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200505 */
Stefan Brünsead12742016-10-09 22:17:18 +0200506static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100507 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200508 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100509{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100510 efi_status_t r;
511
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100512 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200513 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100514 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100515}
516
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200517/**
Mario Six78a88f72018-07-10 08:40:17 +0200518 * efi_free_pool_ext() - free memory from pool
519 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200520 *
521 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200522 *
Mario Six78a88f72018-07-10 08:40:17 +0200523 * See the Unified Extensible Firmware Interface (UEFI) specification for
524 * details.
525 *
526 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200527 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200528static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100529{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100530 efi_status_t r;
531
532 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200533 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100534 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100535}
536
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200537/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200538 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100539 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200540 * @handle: handle to be added
541 *
542 * The protocols list is initialized. The handle is added to the list of known
543 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100544 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200545void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100546{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200547 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100548 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200549 INIT_LIST_HEAD(&handle->protocols);
550 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100551}
552
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200553/**
Mario Six78a88f72018-07-10 08:40:17 +0200554 * efi_create_handle() - create handle
555 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200556 *
Mario Six78a88f72018-07-10 08:40:17 +0200557 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200558 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100559efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200560{
561 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200562
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200563 obj = calloc(1, sizeof(struct efi_object));
564 if (!obj)
565 return EFI_OUT_OF_RESOURCES;
566
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100567 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200568 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200569
570 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200571}
572
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200573/**
Mario Six78a88f72018-07-10 08:40:17 +0200574 * efi_search_protocol() - find a protocol on a handle.
575 * @handle: handle
576 * @protocol_guid: GUID of the protocol
577 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100578 *
Mario Six78a88f72018-07-10 08:40:17 +0200579 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100580 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100581efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100582 const efi_guid_t *protocol_guid,
583 struct efi_handler **handler)
584{
585 struct efi_object *efiobj;
586 struct list_head *lhandle;
587
588 if (!handle || !protocol_guid)
589 return EFI_INVALID_PARAMETER;
590 efiobj = efi_search_obj(handle);
591 if (!efiobj)
592 return EFI_INVALID_PARAMETER;
593 list_for_each(lhandle, &efiobj->protocols) {
594 struct efi_handler *protocol;
595
596 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +0100597 if (!guidcmp(&protocol->guid, protocol_guid)) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100598 if (handler)
599 *handler = protocol;
600 return EFI_SUCCESS;
601 }
602 }
603 return EFI_NOT_FOUND;
604}
605
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200606/**
Mario Six78a88f72018-07-10 08:40:17 +0200607 * efi_remove_protocol() - delete protocol from a handle
608 * @handle: handle from which the protocol shall be deleted
609 * @protocol: GUID of the protocol to be deleted
610 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100611 *
Mario Six78a88f72018-07-10 08:40:17 +0200612 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100613 */
Ilias Apalodimas21eb7c12023-06-19 14:14:03 +0300614static efi_status_t efi_remove_protocol(const efi_handle_t handle,
615 const efi_guid_t *protocol,
616 void *protocol_interface)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100617{
618 struct efi_handler *handler;
619 efi_status_t ret;
620
621 ret = efi_search_protocol(handle, protocol, &handler);
622 if (ret != EFI_SUCCESS)
623 return ret;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200624 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200625 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100626 list_del(&handler->link);
627 free(handler);
628 return EFI_SUCCESS;
629}
630
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200631/**
Mario Six78a88f72018-07-10 08:40:17 +0200632 * efi_remove_all_protocols() - delete all protocols from a handle
633 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100634 *
Mario Six78a88f72018-07-10 08:40:17 +0200635 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100636 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100637static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100638{
639 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100640 struct efi_handler *protocol;
641 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100642
643 efiobj = efi_search_obj(handle);
644 if (!efiobj)
645 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100646 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100647 efi_status_t ret;
648
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300649 ret = efi_uninstall_protocol(handle, &protocol->guid,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300650 protocol->protocol_interface, true);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100651 if (ret != EFI_SUCCESS)
652 return ret;
653 }
654 return EFI_SUCCESS;
655}
656
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200657/**
Mario Six78a88f72018-07-10 08:40:17 +0200658 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100659 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200660 * @handle: handle to delete
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300661 *
662 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100663 */
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300664efi_status_t efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100665{
Etienne Carriere79325482022-09-07 10:20:13 +0200666 efi_status_t ret;
667
668 ret = efi_remove_all_protocols(handle);
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300669 if (ret != EFI_SUCCESS) {
670 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
671 return ret;
Etienne Carriere79325482022-09-07 10:20:13 +0200672 }
673
Ilias Apalodimascc889bd2023-08-24 17:21:09 +0300674 return efi_purge_handle(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100675}
676
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200677/**
Mario Six78a88f72018-07-10 08:40:17 +0200678 * efi_is_event() - check if a pointer is a valid event
679 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100680 *
Mario Six78a88f72018-07-10 08:40:17 +0200681 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100682 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100683static efi_status_t efi_is_event(const struct efi_event *event)
684{
685 const struct efi_event *evt;
686
687 if (!event)
688 return EFI_INVALID_PARAMETER;
689 list_for_each_entry(evt, &efi_events, link) {
690 if (evt == event)
691 return EFI_SUCCESS;
692 }
693 return EFI_INVALID_PARAMETER;
694}
Alexander Grafbee91162016-03-04 01:09:59 +0100695
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200696/**
Mario Six78a88f72018-07-10 08:40:17 +0200697 * efi_create_event() - create an event
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200698 *
Mario Six78a88f72018-07-10 08:40:17 +0200699 * @type: type of the event to create
700 * @notify_tpl: task priority level of the event
701 * @notify_function: notification function of the event
702 * @notify_context: pointer passed to the notification function
703 * @group: event group
704 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200705 *
706 * This function is used inside U-Boot code to create an event.
707 *
708 * For the API function implementing the CreateEvent service see
709 * efi_create_event_ext.
710 *
Mario Six78a88f72018-07-10 08:40:17 +0200711 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200712 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100713efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200714 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200715 struct efi_event *event,
716 void *context),
Masahisa Kojimae23c8e82023-11-10 13:25:38 +0900717 void *notify_context, const efi_guid_t *group,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100718 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100719{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100720 struct efi_event *evt;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200721 efi_status_t ret;
722 int pool_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200723
Jonathan Graya95343b2017-03-12 19:26:07 +1100724 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200725 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100726
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200727 switch (type) {
728 case 0:
729 case EVT_TIMER:
730 case EVT_NOTIFY_SIGNAL:
731 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
732 case EVT_NOTIFY_WAIT:
733 case EVT_TIMER | EVT_NOTIFY_WAIT:
734 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200735 pool_type = EFI_BOOT_SERVICES_DATA;
736 break;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200737 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200738 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200739 break;
740 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200741 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200742 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100743
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100744 /*
745 * The UEFI specification requires event notification levels to be
746 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
747 *
748 * Parameter NotifyTpl should not be checked if it is not used.
749 */
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900750 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100751 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
752 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200753 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100754
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200755 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
756 (void **)&evt);
757 if (ret != EFI_SUCCESS)
758 return ret;
759 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100760 evt->type = type;
761 evt->notify_tpl = notify_tpl;
762 evt->notify_function = notify_function;
763 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100764 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200765 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100766 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100767 list_add_tail(&evt->link, &efi_events);
768 *event = evt;
769 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100770}
771
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200772/*
Mario Six78a88f72018-07-10 08:40:17 +0200773 * efi_create_event_ex() - create an event in a group
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100774 *
Mario Six78a88f72018-07-10 08:40:17 +0200775 * @type: type of the event to create
776 * @notify_tpl: task priority level of the event
777 * @notify_function: notification function of the event
778 * @notify_context: pointer passed to the notification function
779 * @event: created event
780 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100781 *
782 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100783 *
Mario Six78a88f72018-07-10 08:40:17 +0200784 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * details.
786 *
787 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100788 */
Heinrich Schuchardtbe678472023-02-10 08:50:06 +0100789static
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100790efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
791 void (EFIAPI *notify_function) (
792 struct efi_event *event,
793 void *context),
794 void *notify_context,
Masahisa Kojimae23c8e82023-11-10 13:25:38 +0900795 const efi_guid_t *event_group,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100796 struct efi_event **event)
797{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200798 efi_status_t ret;
799
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100800 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100801 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200802
803 /*
804 * The allowable input parameters are the same as in CreateEvent()
805 * except for the following two disallowed event types.
806 */
807 switch (type) {
808 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
809 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
810 ret = EFI_INVALID_PARAMETER;
811 goto out;
812 }
813
814 ret = efi_create_event(type, notify_tpl, notify_function,
815 notify_context, event_group, event);
816out:
817 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100818}
819
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200820/**
Mario Six78a88f72018-07-10 08:40:17 +0200821 * efi_create_event_ext() - create an event
822 * @type: type of the event to create
823 * @notify_tpl: task priority level of the event
824 * @notify_function: notification function of the event
825 * @notify_context: pointer passed to the notification function
826 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200827 *
828 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200829 *
Mario Six78a88f72018-07-10 08:40:17 +0200830 * See the Unified Extensible Firmware Interface (UEFI) specification for
831 * details.
832 *
833 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200834 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200835static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100836 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200837 void (EFIAPI *notify_function) (
838 struct efi_event *event,
839 void *context),
840 void *notify_context, struct efi_event **event)
841{
842 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
843 notify_context);
844 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100845 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200846}
847
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200848/**
Mario Six78a88f72018-07-10 08:40:17 +0200849 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200850 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200851 * Check if a timer event has occurred or a queued notification function should
852 * be called.
853 *
Alexander Grafbee91162016-03-04 01:09:59 +0100854 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200855 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100856 */
857void efi_timer_check(void)
858{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100859 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100860 u64 now = timer_get_us();
861
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100862 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200863 if (!timers_enabled)
864 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100865 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200866 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100867 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200868 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100869 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200870 break;
871 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100872 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200873 break;
874 default:
875 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200876 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100877 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200878 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100879 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200880 efi_process_event_queue();
Stefan Roese29caf932022-09-02 14:10:46 +0200881 schedule();
Alexander Grafbee91162016-03-04 01:09:59 +0100882}
883
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200884/**
Mario Six78a88f72018-07-10 08:40:17 +0200885 * efi_set_timer() - set the trigger time for a timer event or stop the event
886 * @event: event for which the timer is set
887 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200888 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200889 *
890 * This is the function for internal usage in U-Boot. For the API function
891 * implementing the SetTimer service see efi_set_timer_ext.
892 *
Mario Six78a88f72018-07-10 08:40:17 +0200893 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200894 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200895efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200896 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100897{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100898 /* Check that the event is valid */
899 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
900 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100901
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200902 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200903 * The parameter defines a multiple of 100 ns.
904 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200905 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200906 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100907
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100908 switch (type) {
909 case EFI_TIMER_STOP:
910 event->trigger_next = -1ULL;
911 break;
912 case EFI_TIMER_PERIODIC:
913 case EFI_TIMER_RELATIVE:
914 event->trigger_next = timer_get_us() + trigger_time;
915 break;
916 default:
917 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100918 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100919 event->trigger_type = type;
920 event->trigger_time = trigger_time;
921 event->is_signaled = false;
922 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200923}
924
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200925/**
Mario Six78a88f72018-07-10 08:40:17 +0200926 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
927 * event
928 * @event: event for which the timer is set
929 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200930 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200931 *
932 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200933 *
Mario Six78a88f72018-07-10 08:40:17 +0200934 * See the Unified Extensible Firmware Interface (UEFI) specification for
935 * details.
936 *
937 *
938 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200939 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200940static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
941 enum efi_timer_delay type,
942 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200943{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900944 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200945 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100946}
947
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200948/**
Mario Six78a88f72018-07-10 08:40:17 +0200949 * efi_wait_for_event() - wait for events to be signaled
950 * @num_events: number of events to be waited for
951 * @event: events to be waited for
952 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200953 *
954 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200955 *
Mario Six78a88f72018-07-10 08:40:17 +0200956 * See the Unified Extensible Firmware Interface (UEFI) specification for
957 * details.
958 *
959 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200960 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100961static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200962 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100963 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100964{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100965 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100966
Heinrich Schuchardte9df5492022-02-03 22:21:51 +0100967 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100968
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200969 /* Check parameters */
970 if (!num_events || !event)
971 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200972 /* Check TPL */
973 if (efi_tpl != TPL_APPLICATION)
974 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200975 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100976 if (efi_is_event(event[i]) != EFI_SUCCESS)
977 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200978 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
979 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200980 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200981 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200982 }
983
984 /* Wait for signal */
985 for (;;) {
986 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200987 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200988 goto out;
989 }
990 /* Allow events to occur. */
991 efi_timer_check();
992 }
993
994out:
995 /*
996 * Reset the signal which is passed to the caller to allow periodic
997 * events to occur.
998 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200999 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001000 if (index)
1001 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +01001002
1003 return EFI_EXIT(EFI_SUCCESS);
1004}
1005
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001006/**
Mario Six78a88f72018-07-10 08:40:17 +02001007 * efi_signal_event_ext() - signal an EFI event
1008 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001009 *
1010 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +02001011 *
1012 * See the Unified Extensible Firmware Interface (UEFI) specification for
1013 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001014 *
1015 * This functions sets the signaled state of the event and queues the
1016 * notification function for execution.
1017 *
Mario Six78a88f72018-07-10 08:40:17 +02001018 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001019 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001020static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001021{
1022 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001023 if (efi_is_event(event) != EFI_SUCCESS)
1024 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001025 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +01001026 return EFI_EXIT(EFI_SUCCESS);
1027}
1028
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001029/**
Mario Six78a88f72018-07-10 08:40:17 +02001030 * efi_close_event() - close an EFI event
1031 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001032 *
1033 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001034 *
Mario Six78a88f72018-07-10 08:40:17 +02001035 * See the Unified Extensible Firmware Interface (UEFI) specification for
1036 * details.
1037 *
1038 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001039 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001040static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001041{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001042 struct efi_register_notify_event *item, *next;
1043
Alexander Grafbee91162016-03-04 01:09:59 +01001044 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001045 if (efi_is_event(event) != EFI_SUCCESS)
1046 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001047
1048 /* Remove protocol notify registrations for the event */
1049 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1050 link) {
1051 if (event == item->event) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001052 struct efi_protocol_notification *hitem, *hnext;
1053
1054 /* Remove signaled handles */
1055 list_for_each_entry_safe(hitem, hnext, &item->handles,
1056 link) {
1057 list_del(&hitem->link);
1058 free(hitem);
1059 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001060 list_del(&item->link);
1061 free(item);
1062 }
1063 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +02001064 /* Remove event from queue */
1065 if (efi_event_is_queued(event))
1066 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001067
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001068 list_del(&event->link);
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001069 efi_free_pool(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001070 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001071}
1072
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001073/**
Mario Six78a88f72018-07-10 08:40:17 +02001074 * efi_check_event() - check if an event is signaled
1075 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001076 *
1077 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001078 *
Mario Six78a88f72018-07-10 08:40:17 +02001079 * See the Unified Extensible Firmware Interface (UEFI) specification for
1080 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001081 *
Mario Six78a88f72018-07-10 08:40:17 +02001082 * If an event is not signaled yet, the notification function is queued. The
1083 * signaled state is cleared.
1084 *
1085 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001086 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001087static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001088{
1089 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001090 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001091 if (efi_is_event(event) != EFI_SUCCESS ||
1092 event->type & EVT_NOTIFY_SIGNAL)
1093 return EFI_EXIT(EFI_INVALID_PARAMETER);
1094 if (!event->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001095 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001096 if (event->is_signaled) {
1097 event->is_signaled = false;
1098 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001099 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001100 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001101}
1102
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001103/**
Mario Six78a88f72018-07-10 08:40:17 +02001104 * efi_search_obj() - find the internal EFI object for a handle
1105 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001106 *
Mario Six78a88f72018-07-10 08:40:17 +02001107 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001108 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001109struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001110{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001111 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001112
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001113 if (!handle)
1114 return NULL;
1115
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001116 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001117 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001118 return efiobj;
1119 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001120 return NULL;
1121}
1122
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001123/**
Mario Six78a88f72018-07-10 08:40:17 +02001124 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1125 * to a protocol
1126 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001127 *
Mario Six78a88f72018-07-10 08:40:17 +02001128 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001129 */
1130static struct efi_open_protocol_info_entry *efi_create_open_info(
1131 struct efi_handler *handler)
1132{
1133 struct efi_open_protocol_info_item *item;
1134
1135 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1136 if (!item)
1137 return NULL;
1138 /* Append the item to the open protocol info list. */
1139 list_add_tail(&item->link, &handler->open_infos);
1140
1141 return &item->info;
1142}
1143
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001144/**
Mario Six78a88f72018-07-10 08:40:17 +02001145 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1146 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001147 *
Mario Six78a88f72018-07-10 08:40:17 +02001148 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001149 */
1150static efi_status_t efi_delete_open_info(
1151 struct efi_open_protocol_info_item *item)
1152{
1153 list_del(&item->link);
1154 free(item);
1155 return EFI_SUCCESS;
1156}
1157
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001158/**
Mario Six78a88f72018-07-10 08:40:17 +02001159 * efi_add_protocol() - install new protocol on a handle
1160 * @handle: handle on which the protocol shall be installed
1161 * @protocol: GUID of the protocol to be installed
1162 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001163 *
Mario Six78a88f72018-07-10 08:40:17 +02001164 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001165 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001166efi_status_t efi_add_protocol(const efi_handle_t handle,
1167 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001168 void *protocol_interface)
1169{
1170 struct efi_object *efiobj;
1171 struct efi_handler *handler;
1172 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001173 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001174
1175 efiobj = efi_search_obj(handle);
1176 if (!efiobj)
1177 return EFI_INVALID_PARAMETER;
1178 ret = efi_search_protocol(handle, protocol, NULL);
1179 if (ret != EFI_NOT_FOUND)
1180 return EFI_INVALID_PARAMETER;
1181 handler = calloc(1, sizeof(struct efi_handler));
1182 if (!handler)
1183 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001184 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001185 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001186 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001187 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001188
1189 /* Notify registered events */
1190 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001191 if (!guidcmp(protocol, &event->protocol)) {
1192 struct efi_protocol_notification *notif;
1193
1194 notif = calloc(1, sizeof(*notif));
1195 if (!notif) {
1196 list_del(&handler->link);
1197 free(handler);
1198 return EFI_OUT_OF_RESOURCES;
1199 }
1200 notif->handle = handle;
1201 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001202 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001203 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001204 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001205 }
1206
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001207 if (!guidcmp(&efi_guid_device_path, protocol))
1208 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001209 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001210}
1211
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001212/**
Mario Six78a88f72018-07-10 08:40:17 +02001213 * efi_install_protocol_interface() - install protocol interface
1214 * @handle: handle on which the protocol shall be installed
1215 * @protocol: GUID of the protocol to be installed
1216 * @protocol_interface_type: type of the interface to be installed,
1217 * always EFI_NATIVE_INTERFACE
1218 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001219 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001220 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001221 *
Mario Six78a88f72018-07-10 08:40:17 +02001222 * See the Unified Extensible Firmware Interface (UEFI) specification for
1223 * details.
1224 *
1225 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001226 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001227static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001228 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001229 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001230{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001231 efi_status_t r;
1232
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001233 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001234 protocol_interface);
1235
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001236 if (!handle || !protocol ||
1237 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1238 r = EFI_INVALID_PARAMETER;
1239 goto out;
1240 }
1241
1242 /* Create new handle if requested. */
1243 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001244 r = efi_create_handle(handle);
1245 if (r != EFI_SUCCESS)
1246 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001247 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001248 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001249 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001250 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001251 /* Add new protocol */
1252 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001253out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001254 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001255}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001256
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001257/**
Mario Six78a88f72018-07-10 08:40:17 +02001258 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001259 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001260 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001261 * @number_of_drivers: number of child controllers
1262 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001263 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001264 * The allocated buffer has to be freed with free().
1265 *
Mario Six78a88f72018-07-10 08:40:17 +02001266 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001267 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001268static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001269 const efi_guid_t *protocol,
1270 efi_uintn_t *number_of_drivers,
1271 efi_handle_t **driver_handle_buffer)
1272{
1273 struct efi_handler *handler;
1274 struct efi_open_protocol_info_item *item;
1275 efi_uintn_t count = 0, i;
1276 bool duplicate;
1277
1278 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001279 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001280 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001281 continue;
1282 list_for_each_entry(item, &handler->open_infos, link) {
1283 if (item->info.attributes &
1284 EFI_OPEN_PROTOCOL_BY_DRIVER)
1285 ++count;
1286 }
1287 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001288 *number_of_drivers = 0;
1289 if (!count) {
1290 *driver_handle_buffer = NULL;
1291 return EFI_SUCCESS;
1292 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001293 /*
1294 * Create buffer. In case of duplicate driver assignments the buffer
1295 * will be too large. But that does not harm.
1296 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001297 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1298 if (!*driver_handle_buffer)
1299 return EFI_OUT_OF_RESOURCES;
1300 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001301 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt66028932022-03-09 19:56:23 +01001302 if (protocol && guidcmp(&handler->guid, protocol))
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001303 continue;
1304 list_for_each_entry(item, &handler->open_infos, link) {
1305 if (item->info.attributes &
1306 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1307 /* Check this is a new driver */
1308 duplicate = false;
1309 for (i = 0; i < *number_of_drivers; ++i) {
1310 if ((*driver_handle_buffer)[i] ==
1311 item->info.agent_handle)
1312 duplicate = true;
1313 }
1314 /* Copy handle to buffer */
1315 if (!duplicate) {
1316 i = (*number_of_drivers)++;
1317 (*driver_handle_buffer)[i] =
1318 item->info.agent_handle;
1319 }
1320 }
1321 }
1322 }
1323 return EFI_SUCCESS;
1324}
1325
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001326/**
Mario Six78a88f72018-07-10 08:40:17 +02001327 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001328 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001329 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001330 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001331 *
1332 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001333 *
Mario Six78a88f72018-07-10 08:40:17 +02001334 * See the Unified Extensible Firmware Interface (UEFI) specification for
1335 * details.
1336 *
1337 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001338 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001339static efi_status_t efi_disconnect_all_drivers
1340 (efi_handle_t handle,
1341 const efi_guid_t *protocol,
1342 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001343{
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001344 efi_uintn_t number_of_drivers;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001345 efi_handle_t *driver_handle_buffer;
1346 efi_status_t r, ret;
1347
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001348 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001349 &driver_handle_buffer);
1350 if (ret != EFI_SUCCESS)
1351 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001352 if (!number_of_drivers)
1353 return EFI_SUCCESS;
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001354
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001355 while (number_of_drivers) {
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001356 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001357 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001358 driver_handle_buffer[--number_of_drivers],
1359 child_handle));
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001360 if (r != EFI_SUCCESS)
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001361 ret = r;
Ilias Apalodimas239d59a2023-06-20 09:19:28 +03001362 }
1363
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001364 free(driver_handle_buffer);
1365 return ret;
1366}
1367
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001368/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001369 * efi_uninstall_protocol() - uninstall protocol interface
1370 *
Mario Six78a88f72018-07-10 08:40:17 +02001371 * @handle: handle from which the protocol shall be removed
1372 * @protocol: GUID of the protocol to be removed
1373 * @protocol_interface: interface to be removed
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001374 * @preserve: preserve or delete the handle and remove it from any
1375 * list it participates if no protocols remain
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001376 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001377 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001378 *
1379 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001380 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001381static efi_status_t efi_uninstall_protocol
1382 (efi_handle_t handle, const efi_guid_t *protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001383 void *protocol_interface, bool preserve)
Alexander Grafbee91162016-03-04 01:09:59 +01001384{
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001385 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001386 struct efi_open_protocol_info_item *item;
1387 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001388 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001389
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001390 /* Find the protocol on the handle */
1391 r = efi_search_protocol(handle, protocol, &handler);
1392 if (r != EFI_SUCCESS)
1393 goto out;
Ilias Apalodimas748cb552023-06-20 09:19:30 +03001394 if (handler->protocol_interface != protocol_interface)
1395 return EFI_NOT_FOUND;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001396 /* Disconnect controllers */
Heinrich Schuchardt62870212023-06-18 09:00:45 +02001397 r = efi_disconnect_all_drivers(handle, protocol, NULL);
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001398 if (r != EFI_SUCCESS) {
1399 r = EFI_ACCESS_DENIED;
Ilias Apalodimas6805b4d2023-11-28 21:10:31 +02001400 /*
1401 * This will reconnect all controllers of the handle, even ones
1402 * that were not connected before. This can be done better
1403 * but we are following the EDKII implementation on this for
1404 * now
1405 */
1406 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001407 goto out;
1408 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001409 /* Close protocol */
1410 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1411 if (item->info.attributes ==
1412 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1413 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1414 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001415 efi_delete_open_info(item);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001416 }
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001417 /* if agents didn't close the protocols properly */
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001418 if (!list_empty(&handler->open_infos)) {
1419 r = EFI_ACCESS_DENIED;
Ilias Apalodimas747d16d2023-06-20 09:19:29 +03001420 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001421 goto out;
1422 }
1423 r = efi_remove_protocol(handle, protocol, protocol_interface);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001424 if (r != EFI_SUCCESS)
1425 return r;
1426 /*
1427 * We don't care about the return value here since the
1428 * handle might have more protocols installed
1429 */
1430 if (!preserve)
1431 efi_purge_handle(handle);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001432out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001433 return r;
1434}
1435
1436/**
1437 * efi_uninstall_protocol_interface() - uninstall protocol interface
1438 * @handle: handle from which the protocol shall be removed
1439 * @protocol: GUID of the protocol to be removed
1440 * @protocol_interface: interface to be removed
1441 *
1442 * This function implements the UninstallProtocolInterface service.
1443 *
1444 * See the Unified Extensible Firmware Interface (UEFI) specification for
1445 * details.
1446 *
1447 * Return: status code
1448 */
1449static efi_status_t EFIAPI efi_uninstall_protocol_interface
1450 (efi_handle_t handle, const efi_guid_t *protocol,
1451 void *protocol_interface)
1452{
1453 efi_status_t ret;
1454
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001455 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001456
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03001457 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001458 if (ret != EFI_SUCCESS)
1459 goto out;
1460
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001461out:
1462 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001463}
1464
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001465/**
Mario Six78a88f72018-07-10 08:40:17 +02001466 * efi_register_protocol_notify() - register an event for notification when a
1467 * protocol is installed.
1468 * @protocol: GUID of the protocol whose installation shall be notified
1469 * @event: event to be signaled upon installation of the protocol
1470 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001471 *
1472 * This function implements the RegisterProtocolNotify service.
1473 * See the Unified Extensible Firmware Interface (UEFI) specification
1474 * for details.
1475 *
Mario Six78a88f72018-07-10 08:40:17 +02001476 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001477 */
Jose Marinho64a8aae2021-03-02 17:26:38 +00001478efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1479 struct efi_event *event,
1480 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001481{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001482 struct efi_register_notify_event *item;
1483 efi_status_t ret = EFI_SUCCESS;
1484
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001485 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001486
1487 if (!protocol || !event || !registration) {
1488 ret = EFI_INVALID_PARAMETER;
1489 goto out;
1490 }
1491
1492 item = calloc(1, sizeof(struct efi_register_notify_event));
1493 if (!item) {
1494 ret = EFI_OUT_OF_RESOURCES;
1495 goto out;
1496 }
1497
1498 item->event = event;
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301499 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001500 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001501
1502 list_add_tail(&item->link, &efi_register_notify_events);
1503
1504 *registration = item;
1505out:
1506 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001507}
1508
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001509/**
Mario Six78a88f72018-07-10 08:40:17 +02001510 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001511 *
Mario Six78a88f72018-07-10 08:40:17 +02001512 * @search_type: selection criterion
1513 * @protocol: GUID of the protocol
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001514 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001515 *
1516 * See the documentation of the LocateHandle service in the UEFI specification.
1517 *
Mario Six78a88f72018-07-10 08:40:17 +02001518 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001519 */
Alexander Grafbee91162016-03-04 01:09:59 +01001520static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001521 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001522{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001523 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001524
1525 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001526 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001527 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001528 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001529 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001530 return (ret != EFI_SUCCESS);
1531 default:
1532 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001533 return -1;
1534 }
Alexander Grafbee91162016-03-04 01:09:59 +01001535}
1536
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001537/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001538 * efi_check_register_notify_event() - check if registration key is valid
1539 *
1540 * Check that a pointer is a valid registration key as returned by
1541 * RegisterProtocolNotify().
1542 *
1543 * @key: registration key
1544 * Return: valid registration key or NULL
1545 */
1546static struct efi_register_notify_event *efi_check_register_notify_event
1547 (void *key)
1548{
1549 struct efi_register_notify_event *event;
1550
1551 list_for_each_entry(event, &efi_register_notify_events, link) {
1552 if (event == (struct efi_register_notify_event *)key)
1553 return event;
1554 }
1555 return NULL;
1556}
1557
1558/**
Mario Six78a88f72018-07-10 08:40:17 +02001559 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001560 *
1561 * @search_type: selection criterion
1562 * @protocol: GUID of the protocol
1563 * @search_key: registration key
1564 * @buffer_size: size of the buffer to receive the handles in bytes
1565 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001566 *
1567 * This function is meant for U-Boot internal calls. For the API implementation
1568 * of the LocateHandle service see efi_locate_handle_ext.
1569 *
Mario Six78a88f72018-07-10 08:40:17 +02001570 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001571 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001572static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001573 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001574 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001575 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001576{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001577 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001578 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001579 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001580 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001581
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001582 /* Check parameters */
1583 switch (search_type) {
1584 case ALL_HANDLES:
1585 break;
1586 case BY_REGISTER_NOTIFY:
1587 if (!search_key)
1588 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001589 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001590 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001591 if (!event)
1592 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001593 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001594 case BY_PROTOCOL:
1595 if (!protocol)
1596 return EFI_INVALID_PARAMETER;
1597 break;
1598 default:
1599 return EFI_INVALID_PARAMETER;
1600 }
1601
Alexander Grafbee91162016-03-04 01:09:59 +01001602 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001603 if (search_type == BY_REGISTER_NOTIFY) {
1604 if (list_empty(&event->handles))
1605 return EFI_NOT_FOUND;
1606 handle = list_first_entry(&event->handles,
1607 struct efi_protocol_notification,
1608 link);
1609 efiobj = handle->handle;
1610 size += sizeof(void *);
1611 } else {
1612 list_for_each_entry(efiobj, &efi_obj_list, link) {
1613 if (!efi_search(search_type, protocol, efiobj))
1614 size += sizeof(void *);
1615 }
1616 if (size == 0)
1617 return EFI_NOT_FOUND;
Alexander Grafbee91162016-03-04 01:09:59 +01001618 }
1619
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001620 if (!buffer_size)
1621 return EFI_INVALID_PARAMETER;
1622
Alexander Grafbee91162016-03-04 01:09:59 +01001623 if (*buffer_size < size) {
1624 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001625 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001626 }
1627
Rob Clark796a78c2017-08-06 14:10:07 -04001628 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001629
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001630 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001631 if (!buffer)
1632 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001633
Alexander Grafbee91162016-03-04 01:09:59 +01001634 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001635 if (search_type == BY_REGISTER_NOTIFY) {
1636 *buffer = efiobj;
1637 list_del(&handle->link);
1638 } else {
1639 list_for_each_entry(efiobj, &efi_obj_list, link) {
1640 if (!efi_search(search_type, protocol, efiobj))
1641 *buffer++ = efiobj;
1642 }
Alexander Grafbee91162016-03-04 01:09:59 +01001643 }
1644
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001645 return EFI_SUCCESS;
1646}
1647
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001648/**
Mario Six78a88f72018-07-10 08:40:17 +02001649 * efi_locate_handle_ext() - locate handles implementing a protocol.
1650 * @search_type: selection criterion
1651 * @protocol: GUID of the protocol
1652 * @search_key: registration key
1653 * @buffer_size: size of the buffer to receive the handles in bytes
1654 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001655 *
1656 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001657 *
Mario Six78a88f72018-07-10 08:40:17 +02001658 * See the Unified Extensible Firmware Interface (UEFI) specification for
1659 * details.
1660 *
1661 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001662 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001663static efi_status_t EFIAPI efi_locate_handle_ext(
1664 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001665 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001666 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001667{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001668 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001669 buffer_size, buffer);
1670
1671 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1672 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001673}
1674
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001675/**
Mario Six78a88f72018-07-10 08:40:17 +02001676 * efi_remove_configuration_table() - collapses configuration table entries,
1677 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001678 *
Mario Six78a88f72018-07-10 08:40:17 +02001679 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001680 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001681static void efi_remove_configuration_table(int i)
1682{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001683 struct efi_configuration_table *this = &systab.tables[i];
1684 struct efi_configuration_table *next = &systab.tables[i + 1];
1685 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001686
1687 memmove(this, next, (ulong)end - (ulong)next);
1688 systab.nr_tables--;
1689}
1690
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001691/**
Mario Six78a88f72018-07-10 08:40:17 +02001692 * efi_install_configuration_table() - adds, updates, or removes a
1693 * configuration table
1694 * @guid: GUID of the installed table
1695 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001696 *
1697 * This function is used for internal calls. For the API implementation of the
1698 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1699 *
Mario Six78a88f72018-07-10 08:40:17 +02001700 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001701 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001702efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1703 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001704{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001705 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001706 int i;
1707
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001708 if (!guid)
1709 return EFI_INVALID_PARAMETER;
1710
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001711 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001712 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001713 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001714 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001715 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001716 else
1717 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001718 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001719 }
1720 }
1721
Alexander Grafd98cdf62017-07-26 13:41:04 +02001722 if (!table)
1723 return EFI_NOT_FOUND;
1724
Alexander Grafbee91162016-03-04 01:09:59 +01001725 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001726 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001727 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001728
1729 /* Add a new entry */
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301730 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001731 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001732 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001733
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001734out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001735 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001736 efi_update_table_header_crc32(&systab.hdr);
1737
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001738 /* Notify that the configuration table was changed */
1739 list_for_each_entry(evt, &efi_events, link) {
1740 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001741 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001742 break;
1743 }
1744 }
1745
Alexander Graf488bf122016-08-19 01:23:24 +02001746 return EFI_SUCCESS;
1747}
1748
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001749/**
Mario Six78a88f72018-07-10 08:40:17 +02001750 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1751 * configuration table.
1752 * @guid: GUID of the installed table
1753 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001754 *
1755 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001756 *
Mario Six78a88f72018-07-10 08:40:17 +02001757 * See the Unified Extensible Firmware Interface (UEFI) specification for
1758 * details.
1759 *
1760 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001761 */
Masahisa Kojimaf86352e2021-10-22 20:24:24 +09001762static efi_status_t
1763EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1764 void *table)
Alexander Graf488bf122016-08-19 01:23:24 +02001765{
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001766 EFI_ENTRY("%pUs, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001767 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001768}
1769
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001770/**
Mario Six78a88f72018-07-10 08:40:17 +02001771 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001772 *
1773 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001774 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001775 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001776 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001777 * code is returned.
1778 *
1779 * @device_path: device path of the loaded image
1780 * @file_path: file path of the loaded image
1781 * @handle_ptr: handle of the loaded image
1782 * @info_ptr: loaded image protocol
1783 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001784 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001785efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1786 struct efi_device_path *file_path,
1787 struct efi_loaded_image_obj **handle_ptr,
1788 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001789{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001790 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001791 struct efi_loaded_image *info = NULL;
1792 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001793 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001794
1795 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1796 *handle_ptr = NULL;
1797 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001798
1799 info = calloc(1, sizeof(*info));
1800 if (!info)
1801 return EFI_OUT_OF_RESOURCES;
1802 obj = calloc(1, sizeof(*obj));
1803 if (!obj) {
1804 free(info);
1805 return EFI_OUT_OF_RESOURCES;
1806 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001807 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001808
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001809 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001810 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001811
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001812 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001813 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001814 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001815
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001816 if (device_path) {
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +01001817 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001818
Ilias Apalodimasf19171c2024-01-08 10:55:33 +02001819 dp = efi_dp_concat(device_path, file_path, false);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001820 if (!dp) {
1821 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001822 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001823 }
1824 } else {
1825 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001826 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001827 ret = efi_add_protocol(&obj->header,
1828 &efi_guid_loaded_image_device_path, dp);
1829 if (ret != EFI_SUCCESS)
1830 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001831
1832 /*
1833 * When asking for the loaded_image interface, just
1834 * return handle which points to loaded_image_info
1835 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001836 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001837 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001838 if (ret != EFI_SUCCESS)
1839 goto failure;
1840
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001841 *info_ptr = info;
1842 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001843
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001844 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001845failure:
1846 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001847 efi_delete_handle(&obj->header);
1848 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001849 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001850}
1851
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001852/**
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001853 * efi_locate_device_path() - Get the device path and handle of an device
1854 * implementing a protocol
1855 * @protocol: GUID of the protocol
1856 * @device_path: device path
1857 * @device: handle of the device
1858 *
1859 * This function implements the LocateDevicePath service.
1860 *
1861 * See the Unified Extensible Firmware Interface (UEFI) specification for
1862 * details.
1863 *
1864 * Return: status code
1865 */
AKASHI Takahirod8465ff2022-04-28 17:09:38 +09001866efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1867 struct efi_device_path **device_path,
1868 efi_handle_t *device)
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001869{
1870 struct efi_device_path *dp;
1871 size_t i;
1872 struct efi_handler *handler;
1873 efi_handle_t *handles;
1874 size_t len, len_dp;
1875 size_t len_best = 0;
1876 efi_uintn_t no_handles;
1877 u8 *remainder;
1878 efi_status_t ret;
1879
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01001880 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001881
1882 if (!protocol || !device_path || !*device_path) {
1883 ret = EFI_INVALID_PARAMETER;
1884 goto out;
1885 }
1886
1887 /* Find end of device path */
1888 len = efi_dp_instance_size(*device_path);
1889
1890 /* Get all handles implementing the protocol */
1891 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1892 &no_handles, &handles));
1893 if (ret != EFI_SUCCESS)
1894 goto out;
1895
1896 for (i = 0; i < no_handles; ++i) {
1897 /* Find the device path protocol */
1898 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1899 &handler);
1900 if (ret != EFI_SUCCESS)
1901 continue;
1902 dp = (struct efi_device_path *)handler->protocol_interface;
1903 len_dp = efi_dp_instance_size(dp);
1904 /*
1905 * This handle can only be a better fit
1906 * if its device path length is longer than the best fit and
1907 * if its device path length is shorter of equal the searched
1908 * device path.
1909 */
1910 if (len_dp <= len_best || len_dp > len)
1911 continue;
1912 /* Check if dp is a subpath of device_path */
1913 if (memcmp(*device_path, dp, len_dp))
1914 continue;
1915 if (!device) {
1916 ret = EFI_INVALID_PARAMETER;
1917 goto out;
1918 }
1919 *device = handles[i];
1920 len_best = len_dp;
1921 }
1922 if (len_best) {
1923 remainder = (u8 *)*device_path + len_best;
1924 *device_path = (struct efi_device_path *)remainder;
1925 ret = EFI_SUCCESS;
1926 } else {
1927 ret = EFI_NOT_FOUND;
1928 }
1929out:
1930 return EFI_EXIT(ret);
1931}
1932
1933/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001934 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001935 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001936 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1937 * callers obligation to update the memory type as needed.
1938 *
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01001939 * @file_path: the path of the image to load
1940 * @buffer: buffer containing the loaded image
1941 * @size: size of the loaded image
1942 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001943 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001944static
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001945efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001946 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001947{
Rob Clark838ee4b2017-09-13 18:05:35 -04001948 struct efi_file_handle *f;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001949 efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001950 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001951 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001952
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001953 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001954 f = efi_file_from_path(file_path);
1955 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001956 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001957
Ilias Apalodimasac30aad2021-03-25 21:55:16 +02001958 ret = efi_file_size(f, &bs);
Rob Clark838ee4b2017-09-13 18:05:35 -04001959 if (ret != EFI_SUCCESS)
1960 goto error;
1961
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001962 /*
1963 * When reading the file we do not yet know if it contains an
1964 * application, a boottime driver, or a runtime driver. So here we
1965 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1966 * update the reservation according to the image type.
1967 */
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001968 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1969 EFI_BOOT_SERVICES_DATA,
1970 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001971 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001972 ret = EFI_OUT_OF_RESOURCES;
1973 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001974 }
1975
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001976 /* Read file */
1977 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1978 if (ret != EFI_SUCCESS)
1979 efi_free_pages(addr, efi_size_in_pages(bs));
1980 *buffer = (void *)(uintptr_t)addr;
1981 *size = bs;
1982error:
1983 EFI_CALL(f->close(f));
Rob Clark838ee4b2017-09-13 18:05:35 -04001984 return ret;
1985}
1986
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001987/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001988 * efi_load_image_from_path() - load an image using a file path
1989 *
1990 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1991 * callers obligation to update the memory type as needed.
1992 *
1993 * @boot_policy: true for request originating from the boot manager
1994 * @file_path: the path of the image to load
1995 * @buffer: buffer containing the loaded image
1996 * @size: size of the loaded image
1997 * Return: status code
1998 */
1999static
2000efi_status_t efi_load_image_from_path(bool boot_policy,
2001 struct efi_device_path *file_path,
2002 void **buffer, efi_uintn_t *size)
2003{
2004 efi_handle_t device;
2005 efi_status_t ret;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002006 struct efi_device_path *dp, *rem;
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002007 struct efi_load_file_protocol *load_file_protocol = NULL;
2008 efi_uintn_t buffer_size;
2009 uint64_t addr, pages;
2010 const efi_guid_t *guid;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002011 struct efi_handler *handler;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002012
2013 /* In case of failure nothing is returned */
2014 *buffer = NULL;
2015 *size = 0;
2016
2017 dp = file_path;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002018 device = efi_dp_find_obj(dp, NULL, &rem);
2019 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2020 NULL);
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002021 if (ret == EFI_SUCCESS)
2022 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002023
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002024 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002025 if (ret == EFI_SUCCESS) {
2026 guid = &efi_guid_load_file_protocol;
2027 } else if (!boot_policy) {
2028 guid = &efi_guid_load_file2_protocol;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002029 ret = efi_search_protocol(device, guid, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002030 }
2031 if (ret != EFI_SUCCESS)
2032 return EFI_NOT_FOUND;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002033 ret = efi_search_protocol(device, guid, &handler);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002034 if (ret != EFI_SUCCESS)
2035 return EFI_NOT_FOUND;
2036 buffer_size = 0;
Heinrich Schuchardt15436fa2023-01-24 20:36:45 +01002037 load_file_protocol = handler->protocol_interface;
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002038 ret = EFI_CALL(load_file_protocol->load_file(
2039 load_file_protocol, rem, boot_policy,
2040 &buffer_size, NULL));
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002041 if (ret != EFI_BUFFER_TOO_SMALL)
2042 goto out;
2043 pages = efi_size_in_pages(buffer_size);
2044 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2045 pages, &addr);
2046 if (ret != EFI_SUCCESS) {
2047 ret = EFI_OUT_OF_RESOURCES;
2048 goto out;
2049 }
2050 ret = EFI_CALL(load_file_protocol->load_file(
Heinrich Schuchardt9cdf4702022-02-26 12:05:30 +01002051 load_file_protocol, rem, boot_policy,
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002052 &buffer_size, (void *)(uintptr_t)addr));
2053 if (ret != EFI_SUCCESS)
2054 efi_free_pages(addr, pages);
2055out:
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002056 efi_close_protocol(device, guid, efi_root, NULL);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01002057 if (ret == EFI_SUCCESS) {
2058 *buffer = (void *)(uintptr_t)addr;
2059 *size = buffer_size;
2060 }
2061
2062 return ret;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01002063}
2064
2065/**
Mario Six78a88f72018-07-10 08:40:17 +02002066 * efi_load_image() - load an EFI image into memory
2067 * @boot_policy: true for request originating from the boot manager
2068 * @parent_image: the caller's image handle
2069 * @file_path: the path of the image to load
2070 * @source_buffer: memory location from which the image is installed
2071 * @source_size: size of the memory area from which the image is installed
2072 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002073 *
2074 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02002075 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002076 * See the Unified Extensible Firmware Interface (UEFI) specification
2077 * for details.
2078 *
Mario Six78a88f72018-07-10 08:40:17 +02002079 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002080 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09002081efi_status_t EFIAPI efi_load_image(bool boot_policy,
2082 efi_handle_t parent_image,
2083 struct efi_device_path *file_path,
2084 void *source_buffer,
2085 efi_uintn_t source_size,
2086 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002087{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002088 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04002089 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02002090 struct efi_loaded_image_obj **image_obj =
2091 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002092 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002093 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01002094
Heinrich Schuchardte9df5492022-02-03 22:21:51 +01002095 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01002096 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04002097
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02002098 if (!image_handle || (!source_buffer && !file_path) ||
2099 !efi_search_obj(parent_image) ||
2100 /* The parent image handle must refer to a loaded image */
2101 !parent_image->type) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02002102 ret = EFI_INVALID_PARAMETER;
2103 goto error;
2104 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002105
Rob Clark838ee4b2017-09-13 18:05:35 -04002106 if (!source_buffer) {
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01002107 ret = efi_load_image_from_path(boot_policy, file_path,
2108 &dest_buffer, &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002109 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002110 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04002111 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002112 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04002113 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00002114 /* split file_path which contains both the device and file parts */
2115 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002116 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002117 if (ret == EFI_SUCCESS)
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002118 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002119 if (!source_buffer)
2120 /* Release buffer to which file was loaded */
2121 efi_free_pages((uintptr_t)dest_buffer,
2122 efi_size_in_pages(source_size));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002123 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002124 info->system_table = &systab;
2125 info->parent_handle = parent_image;
2126 } else {
2127 /* The image is invalid. Release all associated resources. */
2128 efi_delete_handle(*image_handle);
2129 *image_handle = NULL;
2130 free(info);
2131 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002132error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002133 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002134}
2135
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002136/**
Alexander Graff31239a2018-11-15 21:23:47 +01002137 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2138 */
2139static void efi_exit_caches(void)
2140{
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002141#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graff31239a2018-11-15 21:23:47 +01002142 /*
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002143 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2144 * caches are enabled.
2145 *
2146 * TODO:
2147 * According to the UEFI spec caches that can be managed via CP15
2148 * operations should be enabled. Caches requiring platform information
2149 * to manage should be disabled. This should not happen in
2150 * ExitBootServices() but before invoking any UEFI binary is invoked.
2151 *
2152 * We want to keep the current workaround while GRUB prior to version
2153 * 2.04 is still in use.
Alexander Graff31239a2018-11-15 21:23:47 +01002154 */
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002155 cleanup_before_linux();
Alexander Graff31239a2018-11-15 21:23:47 +01002156#endif
2157}
2158
2159/**
Mario Six78a88f72018-07-10 08:40:17 +02002160 * efi_exit_boot_services() - stop all boot services
2161 * @image_handle: handle of the loaded image
2162 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002163 *
2164 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02002165 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002166 * See the Unified Extensible Firmware Interface (UEFI) specification
2167 * for details.
2168 *
Mario Six78a88f72018-07-10 08:40:17 +02002169 * All timer events are disabled. For exit boot services events the
2170 * notification function is called. The boot services are disabled in the
2171 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002172 *
Mario Six78a88f72018-07-10 08:40:17 +02002173 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002174 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002175static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002176 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01002177{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002178 struct efi_event *evt, *next_event;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002179 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002180
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002181 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01002182
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002183 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002184 if (map_key != efi_memory_map_key) {
2185 ret = EFI_INVALID_PARAMETER;
2186 goto out;
2187 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002188
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002189 /* Check if ExitBootServices has already been called */
2190 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002191 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002192
Heinrich Schuchardt43eaf5b2021-11-16 18:46:27 +01002193 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2194 list_for_each_entry(evt, &efi_events, link) {
2195 if (evt->group &&
2196 !guidcmp(evt->group,
2197 &efi_guid_event_group_before_exit_boot_services)) {
2198 efi_signal_event(evt);
2199 break;
2200 }
2201 }
2202
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002203 /* Stop all timer related activities */
2204 timers_enabled = false;
2205
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002206 /* Add related events to the event group */
2207 list_for_each_entry(evt, &efi_events, link) {
2208 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2209 evt->group = &efi_guid_event_group_exit_boot_services;
2210 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002211 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01002212 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002213 if (evt->group &&
2214 !guidcmp(evt->group,
2215 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002216 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002217 break;
2218 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002219 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002220
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002221 /* Make sure that notification functions are not called anymore */
2222 efi_tpl = TPL_HIGH_LEVEL;
2223
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02002224 /* Notify variable services */
2225 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04002226
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002227 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2228 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2229 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2230 list_del(&evt->link);
2231 }
2232
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002233 if (!efi_st_keep_devices) {
Tom Rini3f73e792021-11-19 16:33:04 -05002234 bootm_disable_interrupts();
Heinrich Schuchardtdb6288d2020-12-27 15:33:09 +01002235 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002236 udc_disconnect();
2237 board_quiesce_devices();
2238 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2239 }
Alexander Grafb7b84102016-11-17 01:02:57 +01002240
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02002241 /* Patch out unsupported runtime function */
2242 efi_runtime_detach();
2243
Alexander Graff31239a2018-11-15 21:23:47 +01002244 /* Fix up caches for EFI payloads if necessary */
2245 efi_exit_caches();
2246
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002247 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002248 systab.con_in_handle = NULL;
2249 systab.con_in = NULL;
2250 systab.con_out_handle = NULL;
2251 systab.con_out = NULL;
2252 systab.stderr_handle = NULL;
2253 systab.std_err = NULL;
2254 systab.boottime = NULL;
2255
2256 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02002257 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002258
Alexander Grafbee91162016-03-04 01:09:59 +01002259 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002260 efi_set_watchdog(0);
Stefan Roese29caf932022-09-02 14:10:46 +02002261 schedule();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002262out:
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002263 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2264 if (ret != EFI_SUCCESS)
2265 efi_tcg2_notify_exit_boot_services_failed();
2266 }
2267
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002268 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002269}
2270
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002271/**
Mario Six78a88f72018-07-10 08:40:17 +02002272 * efi_get_next_monotonic_count() - get next value of the counter
2273 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002274 *
2275 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002276 *
Mario Six78a88f72018-07-10 08:40:17 +02002277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2278 * details.
2279 *
2280 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002281 */
Alexander Grafbee91162016-03-04 01:09:59 +01002282static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2283{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002284 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002285 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002286
Alexander Grafbee91162016-03-04 01:09:59 +01002287 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002288 if (!count) {
2289 ret = EFI_INVALID_PARAMETER;
2290 goto out;
2291 }
Alexander Grafbee91162016-03-04 01:09:59 +01002292 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002293 ret = EFI_SUCCESS;
2294out:
2295 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002296}
2297
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002298/**
Mario Six78a88f72018-07-10 08:40:17 +02002299 * efi_stall() - sleep
2300 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002301 *
Mario Six78a88f72018-07-10 08:40:17 +02002302 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002303 *
Mario Six78a88f72018-07-10 08:40:17 +02002304 * See the Unified Extensible Firmware Interface (UEFI) specification for
2305 * details.
2306 *
2307 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002308 */
Alexander Grafbee91162016-03-04 01:09:59 +01002309static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2310{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002311 u64 end_tick;
2312
Alexander Grafbee91162016-03-04 01:09:59 +01002313 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002314
2315 end_tick = get_ticks() + usec_to_tick(microseconds);
2316 while (get_ticks() < end_tick)
2317 efi_timer_check();
2318
Alexander Grafbee91162016-03-04 01:09:59 +01002319 return EFI_EXIT(EFI_SUCCESS);
2320}
2321
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002322/**
Mario Six78a88f72018-07-10 08:40:17 +02002323 * efi_set_watchdog_timer() - reset the watchdog timer
2324 * @timeout: seconds before reset by watchdog
2325 * @watchdog_code: code to be logged when resetting
2326 * @data_size: size of buffer in bytes
2327 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002328 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002329 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002330 *
Mario Six78a88f72018-07-10 08:40:17 +02002331 * See the Unified Extensible Firmware Interface (UEFI) specification for
2332 * details.
2333 *
2334 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002335 */
Alexander Grafbee91162016-03-04 01:09:59 +01002336static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2337 uint64_t watchdog_code,
2338 unsigned long data_size,
2339 uint16_t *watchdog_data)
2340{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002341 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002342 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002343 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002344}
2345
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002346/**
Mario Six78a88f72018-07-10 08:40:17 +02002347 * efi_close_protocol() - close a protocol
2348 * @handle: handle on which the protocol shall be closed
2349 * @protocol: GUID of the protocol to close
2350 * @agent_handle: handle of the driver
2351 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002352 *
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002353 * This is the function implementing the CloseProtocol service is for internal
2354 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
2355 *
2356 * See the Unified Extensible Firmware Interface (UEFI) specification for
2357 * details.
2358 *
2359 * Return: status code
2360 */
2361efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2362 efi_handle_t agent_handle,
2363 efi_handle_t controller_handle)
2364{
2365 struct efi_handler *handler;
2366 struct efi_open_protocol_info_item *item;
2367 struct efi_open_protocol_info_item *pos;
2368 efi_status_t ret;
2369
2370 if (!efi_search_obj(agent_handle) ||
2371 (controller_handle && !efi_search_obj(controller_handle)))
2372 return EFI_INVALID_PARAMETER;
2373 ret = efi_search_protocol(handle, protocol, &handler);
2374 if (ret != EFI_SUCCESS)
2375 return ret;
2376
2377 ret = EFI_NOT_FOUND;
2378 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2379 if (item->info.agent_handle == agent_handle &&
2380 item->info.controller_handle == controller_handle) {
2381 efi_delete_open_info(item);
2382 ret = EFI_SUCCESS;
2383 }
2384 }
2385
2386 return ret;
2387}
2388
2389/**
2390 * efi_close_protocol_ext() - close a protocol
2391 * @handle: handle on which the protocol shall be closed
2392 * @protocol: GUID of the protocol to close
2393 * @agent_handle: handle of the driver
2394 * @controller_handle: handle of the controller
2395 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002396 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002397 *
Mario Six78a88f72018-07-10 08:40:17 +02002398 * See the Unified Extensible Firmware Interface (UEFI) specification for
2399 * details.
2400 *
2401 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002402 */
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002403static efi_status_t EFIAPI
2404efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2405 efi_handle_t agent_handle,
2406 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002407{
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002408 efi_status_t ret;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002409
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002410 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002411 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002412
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002413 ret = efi_close_protocol(handle, protocol,
2414 agent_handle, controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002415
Heinrich Schuchardtef185762022-10-07 15:18:15 +02002416 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002417}
2418
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002419/**
Mario Six78a88f72018-07-10 08:40:17 +02002420 * efi_open_protocol_information() - provide information about then open status
2421 * of a protocol on a handle
2422 * @handle: handle for which the information shall be retrieved
2423 * @protocol: GUID of the protocol
2424 * @entry_buffer: buffer to receive the open protocol information
2425 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002426 *
2427 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002428 *
Mario Six78a88f72018-07-10 08:40:17 +02002429 * See the Unified Extensible Firmware Interface (UEFI) specification for
2430 * details.
2431 *
2432 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002433 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002434static efi_status_t EFIAPI efi_open_protocol_information(
2435 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002436 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002437 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002438{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002439 unsigned long buffer_size;
2440 unsigned long count;
2441 struct efi_handler *handler;
2442 struct efi_open_protocol_info_item *item;
2443 efi_status_t r;
2444
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002445 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002446 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002447
2448 /* Check parameters */
2449 if (!entry_buffer) {
2450 r = EFI_INVALID_PARAMETER;
2451 goto out;
2452 }
2453 r = efi_search_protocol(handle, protocol, &handler);
2454 if (r != EFI_SUCCESS)
2455 goto out;
2456
2457 /* Count entries */
2458 count = 0;
2459 list_for_each_entry(item, &handler->open_infos, link) {
2460 if (item->info.open_count)
2461 ++count;
2462 }
2463 *entry_count = count;
2464 *entry_buffer = NULL;
2465 if (!count) {
2466 r = EFI_SUCCESS;
2467 goto out;
2468 }
2469
2470 /* Copy entries */
2471 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002472 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002473 (void **)entry_buffer);
2474 if (r != EFI_SUCCESS)
2475 goto out;
2476 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2477 if (item->info.open_count)
2478 (*entry_buffer)[--count] = item->info;
2479 }
2480out:
2481 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002482}
2483
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002484/**
Mario Six78a88f72018-07-10 08:40:17 +02002485 * efi_protocols_per_handle() - get protocols installed on a handle
2486 * @handle: handle for which the information is retrieved
2487 * @protocol_buffer: buffer with protocol GUIDs
2488 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002489 *
2490 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002491 *
Mario Six78a88f72018-07-10 08:40:17 +02002492 * See the Unified Extensible Firmware Interface (UEFI) specification for
2493 * details.
2494 *
2495 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002496 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002497static efi_status_t EFIAPI efi_protocols_per_handle(
2498 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002499 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002500{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002501 unsigned long buffer_size;
2502 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002503 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002504 efi_status_t r;
2505
Alexander Grafbee91162016-03-04 01:09:59 +01002506 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2507 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002508
2509 if (!handle || !protocol_buffer || !protocol_buffer_count)
2510 return EFI_EXIT(EFI_INVALID_PARAMETER);
2511
2512 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002513 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002514
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002515 efiobj = efi_search_obj(handle);
2516 if (!efiobj)
2517 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002518
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002519 /* Count protocols */
2520 list_for_each(protocol_handle, &efiobj->protocols) {
2521 ++*protocol_buffer_count;
2522 }
2523
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002524 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002525 if (*protocol_buffer_count) {
2526 size_t j = 0;
2527
2528 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002529 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002530 (void **)protocol_buffer);
2531 if (r != EFI_SUCCESS)
2532 return EFI_EXIT(r);
2533 list_for_each(protocol_handle, &efiobj->protocols) {
2534 struct efi_handler *protocol;
2535
2536 protocol = list_entry(protocol_handle,
2537 struct efi_handler, link);
Heinrich Schuchardt66028932022-03-09 19:56:23 +01002538 (*protocol_buffer)[j] = (void *)&protocol->guid;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002539 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002540 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002541 }
2542
2543 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002544}
2545
Masahisa Kojima87d79142022-09-12 17:33:50 +09002546efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2547 const efi_guid_t *protocol, void *search_key,
2548 efi_uintn_t *no_handles, efi_handle_t **buffer)
2549{
2550 efi_status_t r;
2551 efi_uintn_t buffer_size = 0;
2552
2553 if (!no_handles || !buffer) {
2554 r = EFI_INVALID_PARAMETER;
2555 goto out;
2556 }
2557 *no_handles = 0;
2558 *buffer = NULL;
2559 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2560 *buffer);
2561 if (r != EFI_BUFFER_TOO_SMALL)
2562 goto out;
2563 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2564 (void **)buffer);
2565 if (r != EFI_SUCCESS)
2566 goto out;
2567 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2568 *buffer);
2569 if (r == EFI_SUCCESS)
2570 *no_handles = buffer_size / sizeof(efi_handle_t);
2571out:
2572 return r;
2573}
2574
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002575/**
Mario Six78a88f72018-07-10 08:40:17 +02002576 * efi_locate_handle_buffer() - locate handles implementing a protocol
2577 * @search_type: selection criterion
2578 * @protocol: GUID of the protocol
2579 * @search_key: registration key
2580 * @no_handles: number of returned handles
2581 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002582 *
2583 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002584 *
Mario Six78a88f72018-07-10 08:40:17 +02002585 * See the Unified Extensible Firmware Interface (UEFI) specification for
2586 * details.
2587 *
2588 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002589 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002590efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafbee91162016-03-04 01:09:59 +01002591 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002592 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002593 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002594{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002595 efi_status_t r;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002596
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002597 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002598 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002599
Masahisa Kojima87d79142022-09-12 17:33:50 +09002600 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2601 no_handles, buffer);
2602
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002603 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002604}
2605
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002606/**
Mario Six78a88f72018-07-10 08:40:17 +02002607 * efi_locate_protocol() - find an interface implementing a protocol
2608 * @protocol: GUID of the protocol
2609 * @registration: registration key passed to the notification function
2610 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002611 *
2612 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002613 *
Mario Six78a88f72018-07-10 08:40:17 +02002614 * See the Unified Extensible Firmware Interface (UEFI) specification for
2615 * details.
2616 *
2617 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002618 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002619static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002620 void *registration,
2621 void **protocol_interface)
2622{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002623 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002624 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002625 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002626
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01002627 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002628
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002629 /*
2630 * The UEFI spec explicitly requires a protocol even if a registration
2631 * key is provided. This differs from the logic in LocateHandle().
2632 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002633 if (!protocol || !protocol_interface)
2634 return EFI_EXIT(EFI_INVALID_PARAMETER);
2635
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002636 if (registration) {
2637 struct efi_register_notify_event *event;
2638 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002639
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002640 event = efi_check_register_notify_event(registration);
2641 if (!event)
2642 return EFI_EXIT(EFI_INVALID_PARAMETER);
2643 /*
2644 * The UEFI spec requires to return EFI_NOT_FOUND if no
2645 * protocol instance matches protocol and registration.
2646 * So let's do the same for a mismatch between protocol and
2647 * registration.
2648 */
2649 if (guidcmp(&event->protocol, protocol))
2650 goto not_found;
2651 if (list_empty(&event->handles))
2652 goto not_found;
2653 handle = list_first_entry(&event->handles,
2654 struct efi_protocol_notification,
2655 link);
2656 efiobj = handle->handle;
2657 list_del(&handle->link);
2658 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002659 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002660 if (ret == EFI_SUCCESS)
2661 goto found;
2662 } else {
2663 list_for_each_entry(efiobj, &efi_obj_list, link) {
2664 ret = efi_search_protocol(efiobj, protocol, &handler);
2665 if (ret == EFI_SUCCESS)
2666 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002667 }
2668 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002669not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002670 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002671 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002672found:
2673 *protocol_interface = handler->protocol_interface;
2674 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002675}
2676
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002677/**
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002678 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
2679 * interfaces
2680 * @handle: handle on which the protocol interfaces shall be installed
2681 * @argptr: va_list of args
2682 *
2683 * Core functionality of efi_install_multiple_protocol_interfaces
2684 * Must not be called directly
2685 *
2686 * Return: status code
2687 */
2688static efi_status_t EFIAPI
2689efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2690 efi_va_list argptr)
2691{
2692 const efi_guid_t *protocol;
2693 void *protocol_interface;
2694 efi_handle_t old_handle;
2695 efi_status_t ret = EFI_SUCCESS;
2696 int i = 0;
2697 efi_va_list argptr_copy;
2698
2699 if (!handle)
2700 return EFI_INVALID_PARAMETER;
2701
2702 efi_va_copy(argptr_copy, argptr);
2703 for (;;) {
2704 protocol = efi_va_arg(argptr, efi_guid_t*);
2705 if (!protocol)
2706 break;
2707 protocol_interface = efi_va_arg(argptr, void*);
2708 /* Check that a device path has not been installed before */
2709 if (!guidcmp(protocol, &efi_guid_device_path)) {
2710 struct efi_device_path *dp = protocol_interface;
2711
2712 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2713 &old_handle));
2714 if (ret == EFI_SUCCESS &&
2715 dp->type == DEVICE_PATH_TYPE_END) {
2716 EFI_PRINT("Path %pD already installed\n",
2717 protocol_interface);
2718 ret = EFI_ALREADY_STARTED;
2719 break;
2720 }
2721 }
2722 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2723 EFI_NATIVE_INTERFACE,
2724 protocol_interface));
2725 if (ret != EFI_SUCCESS)
2726 break;
2727 i++;
2728 }
2729 if (ret == EFI_SUCCESS)
2730 goto out;
2731
2732 /* If an error occurred undo all changes. */
2733 for (; i; --i) {
2734 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2735 protocol_interface = efi_va_arg(argptr_copy, void*);
2736 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2737 protocol_interface));
2738 }
2739
2740out:
2741 efi_va_end(argptr_copy);
2742 return ret;
2743
2744}
2745
2746/**
Mario Six78a88f72018-07-10 08:40:17 +02002747 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2748 * interfaces
2749 * @handle: handle on which the protocol interfaces shall be installed
2750 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2751 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002752 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002753 *
2754 * This is the function for internal usage in U-Boot. For the API function
2755 * implementing the InstallMultipleProtocol service see
2756 * efi_install_multiple_protocol_interfaces_ext()
2757 *
2758 * Return: status code
2759 */
2760efi_status_t EFIAPI
2761efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2762{
2763 efi_status_t ret;
2764 efi_va_list argptr;
2765
2766 efi_va_start(argptr, handle);
2767 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2768 efi_va_end(argptr);
2769 return ret;
2770}
2771
2772/**
2773 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2774 * interfaces
2775 * @handle: handle on which the protocol interfaces shall be installed
2776 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2777 * interfaces
2778 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002779 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002780 *
Mario Six78a88f72018-07-10 08:40:17 +02002781 * See the Unified Extensible Firmware Interface (UEFI) specification for
2782 * details.
2783 *
2784 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002785 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002786static efi_status_t EFIAPI
2787efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002788{
2789 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002790 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002791 efi_va_list argptr;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002792
Alexander Grafbeb077a2018-06-18 17:23:05 +02002793 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002794 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2795 efi_va_end(argptr);
2796 return EFI_EXIT(ret);
2797}
2798
2799/**
2800 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2801 * multiple protocol
2802 * interfaces
2803 * @handle: handle from which the protocol interfaces shall be removed
2804 * @argptr: va_list of args
2805 *
2806 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2807 * Must not be called directly
2808 *
2809 * Return: status code
2810 */
2811static efi_status_t EFIAPI
2812efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2813 efi_va_list argptr)
2814{
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002815 const efi_guid_t *protocol, *next_protocol;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002816 void *protocol_interface;
Ilias Apalodimas9fb32692022-11-10 10:21:24 +02002817 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002818 size_t i = 0;
2819 efi_va_list argptr_copy;
2820
2821 if (!handle)
2822 return EFI_INVALID_PARAMETER;
2823
2824 efi_va_copy(argptr_copy, argptr);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002825 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002826 for (;;) {
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002827 /*
2828 * If efi_uninstall_protocol() fails we need to be able to
2829 * reinstall the previously uninstalled protocols on the same
2830 * handle.
2831 * Instead of calling efi_uninstall_protocol(...,..., false)
2832 * and potentially removing the handle, only allow the handle
2833 * removal on the last protocol that we requested to uninstall.
2834 * That way we can preserve the handle in case the latter fails
2835 */
2836 bool preserve = true;
2837
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002838 if (!protocol)
2839 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002840 protocol_interface = efi_va_arg(argptr, void*);
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002841 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2842 if (!next_protocol)
2843 preserve = false;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002844 ret = efi_uninstall_protocol(handle, protocol,
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002845 protocol_interface, preserve);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002846 if (ret != EFI_SUCCESS)
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002847 break;
2848 i++;
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002849 protocol = next_protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002850 }
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03002851 if (ret == EFI_SUCCESS)
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002852 goto out;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002853
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002854 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002855 for (; i; --i) {
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002856 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2857 protocol_interface = efi_va_arg(argptr_copy, void*);
2858 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2859 EFI_NATIVE_INTERFACE,
2860 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002861 }
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002862 /*
2863 * If any errors are generated while the protocol interfaces are being
2864 * uninstalled, then the protocols uninstalled prior to the error will
2865 * be reinstalled using InstallProtocolInterface() and the status code
2866 * EFI_INVALID_PARAMETER is returned.
2867 */
2868 ret = EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002869
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002870out:
2871 efi_va_end(argptr_copy);
2872 return ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002873}
2874
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002875/**
Mario Six78a88f72018-07-10 08:40:17 +02002876 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2877 * interfaces
2878 * @handle: handle from which the protocol interfaces shall be removed
2879 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2880 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002881 *
2882 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002883 *
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002884 * This is the function for internal usage in U-Boot. For the API function
2885 * implementing the UninstallMultipleProtocolInterfaces service see
2886 * efi_uninstall_multiple_protocol_interfaces_ext()
2887 *
2888 * Return: status code
2889 */
2890efi_status_t EFIAPI
2891efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2892{
2893 efi_status_t ret;
2894 efi_va_list argptr;
2895
2896 efi_va_start(argptr, handle);
2897 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2898 efi_va_end(argptr);
2899 return ret;
2900}
2901
2902/**
2903 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2904 * interfaces
2905 * @handle: handle from which the protocol interfaces shall be removed
2906 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2907 * interfaces
2908 *
2909 * This function implements the UninstallMultipleProtocolInterfaces service.
2910 *
Mario Six78a88f72018-07-10 08:40:17 +02002911 * See the Unified Extensible Firmware Interface (UEFI) specification for
2912 * details.
2913 *
2914 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002915 */
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002916static efi_status_t EFIAPI
2917efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002918{
2919 EFI_ENTRY("%p", handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002920 efi_status_t ret;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002921 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002922
Alexander Grafbeb077a2018-06-18 17:23:05 +02002923 efi_va_start(argptr, handle);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002924 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
Alexander Grafbeb077a2018-06-18 17:23:05 +02002925 efi_va_end(argptr);
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03002926 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002927}
2928
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002929/**
Mario Six78a88f72018-07-10 08:40:17 +02002930 * efi_calculate_crc32() - calculate cyclic redundancy code
2931 * @data: buffer with data
2932 * @data_size: size of buffer in bytes
2933 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002934 *
2935 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002936 *
Mario Six78a88f72018-07-10 08:40:17 +02002937 * See the Unified Extensible Firmware Interface (UEFI) specification for
2938 * details.
2939 *
2940 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002941 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002942static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2943 efi_uintn_t data_size,
2944 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002945{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002946 efi_status_t ret = EFI_SUCCESS;
2947
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002948 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002949 if (!data || !data_size || !crc32_p) {
2950 ret = EFI_INVALID_PARAMETER;
2951 goto out;
2952 }
Alexander Grafbee91162016-03-04 01:09:59 +01002953 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002954out:
2955 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002956}
2957
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002958/**
Mario Six78a88f72018-07-10 08:40:17 +02002959 * efi_copy_mem() - copy memory
2960 * @destination: destination of the copy operation
2961 * @source: source of the copy operation
2962 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002963 *
2964 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002965 *
Mario Six78a88f72018-07-10 08:40:17 +02002966 * See the Unified Extensible Firmware Interface (UEFI) specification for
2967 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002968 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002969static void EFIAPI efi_copy_mem(void *destination, const void *source,
2970 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002971{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002972 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002973 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002974 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002975}
2976
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002977/**
Mario Six78a88f72018-07-10 08:40:17 +02002978 * efi_set_mem() - Fill memory with a byte value.
2979 * @buffer: buffer to fill
2980 * @size: size of buffer in bytes
2981 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002982 *
2983 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002984 *
Mario Six78a88f72018-07-10 08:40:17 +02002985 * See the Unified Extensible Firmware Interface (UEFI) specification for
2986 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002987 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002988static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002989{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002990 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002991 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002992 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002993}
2994
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002995/**
Mario Six78a88f72018-07-10 08:40:17 +02002996 * efi_protocol_open() - open protocol interface on a handle
2997 * @handler: handler of a protocol
2998 * @protocol_interface: interface implementing the protocol
2999 * @agent_handle: handle of the driver
3000 * @controller_handle: handle of the controller
3001 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003002 *
Mario Six78a88f72018-07-10 08:40:17 +02003003 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003004 */
Heinrich Schuchardtf9ad2402020-01-10 12:33:59 +01003005efi_status_t efi_protocol_open(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003006 struct efi_handler *handler,
3007 void **protocol_interface, void *agent_handle,
3008 void *controller_handle, uint32_t attributes)
3009{
3010 struct efi_open_protocol_info_item *item;
3011 struct efi_open_protocol_info_entry *match = NULL;
3012 bool opened_by_driver = false;
3013 bool opened_exclusive = false;
3014
3015 /* If there is no agent, only return the interface */
3016 if (!agent_handle)
3017 goto out;
3018
3019 /* For TEST_PROTOCOL ignore interface attribute */
3020 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3021 *protocol_interface = NULL;
3022
3023 /*
3024 * Check if the protocol is already opened by a driver with the same
3025 * attributes or opened exclusively
3026 */
3027 list_for_each_entry(item, &handler->open_infos, link) {
3028 if (item->info.agent_handle == agent_handle) {
3029 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3030 (item->info.attributes == attributes))
3031 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02003032 } else {
3033 if (item->info.attributes &
3034 EFI_OPEN_PROTOCOL_BY_DRIVER)
3035 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003036 }
3037 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3038 opened_exclusive = true;
3039 }
3040
3041 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02003042 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3043 if (opened_exclusive)
3044 return EFI_ACCESS_DENIED;
3045 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3046 if (opened_exclusive || opened_by_driver)
3047 return EFI_ACCESS_DENIED;
3048 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003049
3050 /* Prepare exclusive opening */
3051 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3052 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003053disconnect_next:
3054 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003055 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003056 efi_status_t ret;
3057
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003058 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003059 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3060 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003061 item->info.controller_handle,
3062 item->info.agent_handle,
3063 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003064 if (ret == EFI_SUCCESS)
3065 /*
3066 * Child controllers may have been
3067 * removed from the open_infos list. So
3068 * let's restart the loop.
3069 */
3070 goto disconnect_next;
3071 else
3072 opened_by_driver = true;
3073 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003074 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02003075 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003076 if (opened_by_driver)
3077 return EFI_ACCESS_DENIED;
3078 }
3079
3080 /* Find existing entry */
3081 list_for_each_entry(item, &handler->open_infos, link) {
3082 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02003083 item->info.controller_handle == controller_handle &&
3084 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003085 match = &item->info;
3086 }
3087 /* None found, create one */
3088 if (!match) {
3089 match = efi_create_open_info(handler);
3090 if (!match)
3091 return EFI_OUT_OF_RESOURCES;
3092 }
3093
3094 match->agent_handle = agent_handle;
3095 match->controller_handle = controller_handle;
3096 match->attributes = attributes;
3097 match->open_count++;
3098
3099out:
3100 /* For TEST_PROTOCOL ignore interface attribute. */
3101 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3102 *protocol_interface = handler->protocol_interface;
3103
3104 return EFI_SUCCESS;
3105}
3106
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003107/**
Mario Six78a88f72018-07-10 08:40:17 +02003108 * efi_open_protocol() - open protocol interface on a handle
3109 * @handle: handle on which the protocol shall be opened
3110 * @protocol: GUID of the protocol
3111 * @protocol_interface: interface implementing the protocol
3112 * @agent_handle: handle of the driver
3113 * @controller_handle: handle of the controller
3114 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003115 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003116 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003117 *
Mario Six78a88f72018-07-10 08:40:17 +02003118 * See the Unified Extensible Firmware Interface (UEFI) specification for
3119 * details.
3120 *
3121 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003122 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02003123static efi_status_t EFIAPI efi_open_protocol
3124 (efi_handle_t handle, const efi_guid_t *protocol,
3125 void **protocol_interface, efi_handle_t agent_handle,
3126 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01003127{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003128 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003129 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01003130
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003131 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01003132 protocol_interface, agent_handle, controller_handle,
3133 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003134
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003135 if (!handle || !protocol ||
3136 (!protocol_interface && attributes !=
3137 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
3138 goto out;
3139 }
3140
3141 switch (attributes) {
3142 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3143 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3144 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3145 break;
3146 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3147 if (controller_handle == handle)
3148 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003149 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003150 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3151 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003152 /* Check that the controller handle is valid */
3153 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003154 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003155 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003156 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003157 /* Check that the agent handle is valid */
3158 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02003159 goto out;
3160 break;
3161 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02003162 goto out;
3163 }
3164
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003165 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003166 switch (r) {
3167 case EFI_SUCCESS:
3168 break;
3169 case EFI_NOT_FOUND:
3170 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01003171 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02003172 default:
3173 goto out;
3174 }
Alexander Grafbee91162016-03-04 01:09:59 +01003175
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01003176 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3177 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01003178out:
3179 return EFI_EXIT(r);
3180}
3181
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003182/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003183 * efi_start_image() - call the entry point of an image
3184 * @image_handle: handle of the image
3185 * @exit_data_size: size of the buffer
3186 * @exit_data: buffer to receive the exit data of the called image
3187 *
3188 * This function implements the StartImage service.
3189 *
3190 * See the Unified Extensible Firmware Interface (UEFI) specification for
3191 * details.
3192 *
3193 * Return: status code
3194 */
3195efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3196 efi_uintn_t *exit_data_size,
3197 u16 **exit_data)
3198{
3199 struct efi_loaded_image_obj *image_obj =
3200 (struct efi_loaded_image_obj *)image_handle;
3201 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003202 void *info;
3203 efi_handle_t parent_image = current_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003204 efi_status_t exit_status;
3205 struct jmp_buf_data exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003206
3207 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3208
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003209 if (!efi_search_obj(image_handle))
3210 return EFI_EXIT(EFI_INVALID_PARAMETER);
3211
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003212 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02003213 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3214 return EFI_EXIT(EFI_INVALID_PARAMETER);
3215
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09003216 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3217 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3218
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003219 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3220 &info, NULL, NULL,
3221 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3222 if (ret != EFI_SUCCESS)
3223 return EFI_EXIT(EFI_INVALID_PARAMETER);
3224
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003225 image_obj->exit_data_size = exit_data_size;
3226 image_obj->exit_data = exit_data;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003227 image_obj->exit_status = &exit_status;
3228 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003229
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003230 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3231 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09003232 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojimaf9b51dc2021-12-07 14:15:33 +09003233 if (ret == EFI_SECURITY_VIOLATION) {
3234 /*
3235 * TCG2 Protocol is installed but no TPM device found,
3236 * this is not expected.
3237 */
3238 return EFI_EXIT(EFI_SECURITY_VIOLATION);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003239 }
3240 }
3241 }
3242
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003243 /* call the image! */
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003244 if (setjmp(&exit_jmp)) {
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003245 /*
3246 * We called the entry point of the child image with EFI_CALL
3247 * in the lines below. The child image called the Exit() boot
3248 * service efi_exit() which executed the long jump that brought
3249 * us to the current line. This implies that the second half
3250 * of the EFI_CALL macro has not been executed.
3251 */
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +02003252#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003253 /*
3254 * efi_exit() called efi_restore_gd(). We have to undo this
3255 * otherwise __efi_entry_check() will put the wrong value into
3256 * app_gd.
3257 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +02003258 set_gd(app_gd);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003259#endif
3260 /*
3261 * To get ready to call EFI_EXIT below we have to execute the
3262 * missed out steps of EFI_CALL.
3263 */
3264 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003265 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003266 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003267 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003268 current_image = parent_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003269 return EFI_EXIT(exit_status);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003270 }
3271
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003272 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02003273 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09003274 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003275 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3276
3277 /*
Heinrich Schuchardt55111c52020-01-10 22:06:54 +01003278 * Control is returned from a started UEFI image either by calling
3279 * Exit() (where exit data can be provided) or by simply returning from
3280 * the entry point. In the latter case call Exit() on behalf of the
3281 * image.
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003282 */
3283 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3284}
3285
3286/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003287 * efi_delete_image() - delete loaded image from memory)
3288 *
3289 * @image_obj: handle of the loaded image
3290 * @loaded_image_protocol: loaded image protocol
3291 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003292static efi_status_t efi_delete_image
3293 (struct efi_loaded_image_obj *image_obj,
3294 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003295{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003296 struct efi_object *efiobj;
3297 efi_status_t r, ret = EFI_SUCCESS;
3298
3299close_next:
3300 list_for_each_entry(efiobj, &efi_obj_list, link) {
3301 struct efi_handler *protocol;
3302
3303 list_for_each_entry(protocol, &efiobj->protocols, link) {
3304 struct efi_open_protocol_info_item *info;
3305
3306 list_for_each_entry(info, &protocol->open_infos, link) {
3307 if (info->info.agent_handle !=
3308 (efi_handle_t)image_obj)
3309 continue;
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003310 r = efi_close_protocol(
3311 efiobj, &protocol->guid,
3312 info->info.agent_handle,
3313 info->info.controller_handle);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003314 if (r != EFI_SUCCESS)
3315 ret = r;
3316 /*
3317 * Closing protocols may results in further
3318 * items being deleted. To play it safe loop
3319 * over all elements again.
3320 */
3321 goto close_next;
3322 }
3323 }
3324 }
3325
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003326 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3327 efi_size_in_pages(loaded_image_protocol->image_size));
3328 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003329
3330 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003331}
3332
3333/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003334 * efi_unload_image() - unload an EFI image
3335 * @image_handle: handle of the image to be unloaded
3336 *
3337 * This function implements the UnloadImage service.
3338 *
3339 * See the Unified Extensible Firmware Interface (UEFI) specification for
3340 * details.
3341 *
3342 * Return: status code
3343 */
3344efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3345{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003346 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003347 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003348 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003349
3350 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003351
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003352 efiobj = efi_search_obj(image_handle);
3353 if (!efiobj) {
3354 ret = EFI_INVALID_PARAMETER;
3355 goto out;
3356 }
3357 /* Find the loaded image protocol */
3358 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3359 (void **)&loaded_image_protocol,
3360 NULL, NULL,
3361 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3362 if (ret != EFI_SUCCESS) {
3363 ret = EFI_INVALID_PARAMETER;
3364 goto out;
3365 }
3366 switch (efiobj->type) {
3367 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3368 /* Call the unload function */
3369 if (!loaded_image_protocol->unload) {
3370 ret = EFI_UNSUPPORTED;
3371 goto out;
3372 }
3373 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3374 if (ret != EFI_SUCCESS)
3375 goto out;
3376 break;
3377 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3378 break;
3379 default:
3380 ret = EFI_INVALID_PARAMETER;
3381 goto out;
3382 }
3383 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3384 loaded_image_protocol);
3385out:
3386 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003387}
3388
3389/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003390 * efi_update_exit_data() - fill exit data parameters of StartImage()
3391 *
Heinrich Schuchardt9ce91272019-07-14 11:25:06 +02003392 * @image_obj: image handle
3393 * @exit_data_size: size of the exit data buffer
3394 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003395 * Return: status code
3396 */
3397static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3398 efi_uintn_t exit_data_size,
3399 u16 *exit_data)
3400{
3401 efi_status_t ret;
3402
3403 /*
3404 * If exit_data is not provided to StartImage(), exit_data_size must be
3405 * ignored.
3406 */
3407 if (!image_obj->exit_data)
3408 return EFI_SUCCESS;
3409 if (image_obj->exit_data_size)
3410 *image_obj->exit_data_size = exit_data_size;
3411 if (exit_data_size && exit_data) {
3412 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3413 exit_data_size,
3414 (void **)image_obj->exit_data);
3415 if (ret != EFI_SUCCESS)
3416 return ret;
3417 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3418 } else {
3419 image_obj->exit_data = NULL;
3420 }
3421 return EFI_SUCCESS;
3422}
3423
3424/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003425 * efi_exit() - leave an EFI application or driver
3426 * @image_handle: handle of the application or driver that is exiting
3427 * @exit_status: status code
3428 * @exit_data_size: size of the buffer in bytes
3429 * @exit_data: buffer with data describing an error
3430 *
3431 * This function implements the Exit service.
3432 *
3433 * See the Unified Extensible Firmware Interface (UEFI) specification for
3434 * details.
3435 *
3436 * Return: status code
3437 */
3438static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3439 efi_status_t exit_status,
3440 efi_uintn_t exit_data_size,
3441 u16 *exit_data)
3442{
3443 /*
3444 * TODO: We should call the unload procedure of the loaded
3445 * image protocol.
3446 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003447 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003448 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003449 struct efi_loaded_image_obj *image_obj =
3450 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003451 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003452
3453 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3454 exit_data_size, exit_data);
3455
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003456 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003457 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003458 (void **)&loaded_image_protocol,
3459 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003460 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003461 if (ret != EFI_SUCCESS) {
3462 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003463 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003464 }
3465
3466 /* Unloading of unstarted images */
3467 switch (image_obj->header.type) {
3468 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3469 break;
3470 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3471 efi_delete_image(image_obj, loaded_image_protocol);
3472 ret = EFI_SUCCESS;
3473 goto out;
3474 default:
3475 /* Handle does not refer to loaded image */
3476 ret = EFI_INVALID_PARAMETER;
3477 goto out;
3478 }
3479 /* A started image can only be unloaded it is the last one started. */
3480 if (image_handle != current_image) {
3481 ret = EFI_INVALID_PARAMETER;
3482 goto out;
3483 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003484
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003485 /* Exit data is only foreseen in case of failure. */
3486 if (exit_status != EFI_SUCCESS) {
3487 ret = efi_update_exit_data(image_obj, exit_data_size,
3488 exit_data);
3489 /* Exiting has priority. Don't return error to caller. */
3490 if (ret != EFI_SUCCESS)
3491 EFI_PRINT("%s: out of memory\n", __func__);
3492 }
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003493 /* efi_delete_image() frees image_obj. Copy before the call. */
3494 exit_jmp = image_obj->exit_jmp;
3495 *image_obj->exit_status = exit_status;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003496 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3497 exit_status != EFI_SUCCESS)
3498 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003499
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003500 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3501 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3502 ret = efi_tcg2_measure_efi_app_exit();
3503 if (ret != EFI_SUCCESS) {
3504 log_warning("tcg2 measurement fails(0x%lx)\n",
3505 ret);
3506 }
3507 }
3508 }
3509
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003510 /* Make sure entry/exit counts for EFI world cross-overs match */
3511 EFI_EXIT(exit_status);
3512
3513 /*
3514 * But longjmp out with the U-Boot gd, not the application's, as
3515 * the other end is a setjmp call inside EFI context.
3516 */
3517 efi_restore_gd();
3518
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003519 longjmp(exit_jmp, 1);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003520
3521 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003522out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003523 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003524}
3525
3526/**
Mario Six78a88f72018-07-10 08:40:17 +02003527 * efi_handle_protocol() - get interface of a protocol on a handle
3528 * @handle: handle on which the protocol shall be opened
3529 * @protocol: GUID of the protocol
3530 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003531 *
3532 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003533 *
Mario Six78a88f72018-07-10 08:40:17 +02003534 * See the Unified Extensible Firmware Interface (UEFI) specification for
3535 * details.
3536 *
3537 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003538 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09003539efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3540 const efi_guid_t *protocol,
3541 void **protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01003542{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003543 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003544 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003545}
3546
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003547/**
Mario Six78a88f72018-07-10 08:40:17 +02003548 * efi_bind_controller() - bind a single driver to a controller
3549 * @controller_handle: controller handle
3550 * @driver_image_handle: driver handle
3551 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003552 *
Mario Six78a88f72018-07-10 08:40:17 +02003553 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003554 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003555static efi_status_t efi_bind_controller(
3556 efi_handle_t controller_handle,
3557 efi_handle_t driver_image_handle,
3558 struct efi_device_path *remain_device_path)
3559{
3560 struct efi_driver_binding_protocol *binding_protocol;
3561 efi_status_t r;
3562
3563 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3564 &efi_guid_driver_binding_protocol,
3565 (void **)&binding_protocol,
3566 driver_image_handle, NULL,
3567 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3568 if (r != EFI_SUCCESS)
3569 return r;
3570 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3571 controller_handle,
3572 remain_device_path));
3573 if (r == EFI_SUCCESS)
3574 r = EFI_CALL(binding_protocol->start(binding_protocol,
3575 controller_handle,
3576 remain_device_path));
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003577 efi_close_protocol(driver_image_handle,
3578 &efi_guid_driver_binding_protocol,
3579 driver_image_handle, NULL);
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003580 return r;
3581}
3582
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003583/**
Mario Six78a88f72018-07-10 08:40:17 +02003584 * efi_connect_single_controller() - connect a single driver to a controller
3585 * @controller_handle: controller
3586 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003587 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003588 *
Mario Six78a88f72018-07-10 08:40:17 +02003589 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003590 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003591static efi_status_t efi_connect_single_controller(
3592 efi_handle_t controller_handle,
3593 efi_handle_t *driver_image_handle,
3594 struct efi_device_path *remain_device_path)
3595{
3596 efi_handle_t *buffer;
3597 size_t count;
3598 size_t i;
3599 efi_status_t r;
3600 size_t connected = 0;
3601
3602 /* Get buffer with all handles with driver binding protocol */
3603 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3604 &efi_guid_driver_binding_protocol,
3605 NULL, &count, &buffer));
3606 if (r != EFI_SUCCESS)
3607 return r;
3608
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003609 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003610 if (driver_image_handle) {
3611 for (; *driver_image_handle; ++driver_image_handle) {
3612 for (i = 0; i < count; ++i) {
3613 if (buffer[i] == *driver_image_handle) {
3614 buffer[i] = NULL;
3615 r = efi_bind_controller(
3616 controller_handle,
3617 *driver_image_handle,
3618 remain_device_path);
3619 /*
3620 * For drivers that do not support the
3621 * controller or are already connected
3622 * we receive an error code here.
3623 */
3624 if (r == EFI_SUCCESS)
3625 ++connected;
3626 }
3627 }
3628 }
3629 }
3630
3631 /*
3632 * TODO: Some overrides are not yet implemented:
3633 * - Platform Driver Override
3634 * - Driver Family Override Search
3635 * - Bus Specific Driver Override
3636 */
3637
3638 /* Driver Binding Search */
3639 for (i = 0; i < count; ++i) {
3640 if (buffer[i]) {
3641 r = efi_bind_controller(controller_handle,
3642 buffer[i],
3643 remain_device_path);
3644 if (r == EFI_SUCCESS)
3645 ++connected;
3646 }
3647 }
3648
3649 efi_free_pool(buffer);
3650 if (!connected)
3651 return EFI_NOT_FOUND;
3652 return EFI_SUCCESS;
3653}
3654
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003655/**
Mario Six78a88f72018-07-10 08:40:17 +02003656 * efi_connect_controller() - connect a controller to a driver
3657 * @controller_handle: handle of the controller
3658 * @driver_image_handle: handle of the driver
3659 * @remain_device_path: device path of a child controller
3660 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003661 *
3662 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003663 *
3664 * See the Unified Extensible Firmware Interface (UEFI) specification for
3665 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003666 *
3667 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003668 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003669 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3670 *
Mario Six78a88f72018-07-10 08:40:17 +02003671 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003672 */
3673static efi_status_t EFIAPI efi_connect_controller(
3674 efi_handle_t controller_handle,
3675 efi_handle_t *driver_image_handle,
3676 struct efi_device_path *remain_device_path,
3677 bool recursive)
3678{
3679 efi_status_t r;
3680 efi_status_t ret = EFI_NOT_FOUND;
3681 struct efi_object *efiobj;
3682
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003683 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003684 remain_device_path, recursive);
3685
3686 efiobj = efi_search_obj(controller_handle);
3687 if (!efiobj) {
3688 ret = EFI_INVALID_PARAMETER;
3689 goto out;
3690 }
3691
3692 r = efi_connect_single_controller(controller_handle,
3693 driver_image_handle,
3694 remain_device_path);
3695 if (r == EFI_SUCCESS)
3696 ret = EFI_SUCCESS;
3697 if (recursive) {
3698 struct efi_handler *handler;
3699 struct efi_open_protocol_info_item *item;
3700
3701 list_for_each_entry(handler, &efiobj->protocols, link) {
3702 list_for_each_entry(item, &handler->open_infos, link) {
3703 if (item->info.attributes &
3704 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3705 r = EFI_CALL(efi_connect_controller(
3706 item->info.controller_handle,
3707 driver_image_handle,
3708 remain_device_path,
3709 recursive));
3710 if (r == EFI_SUCCESS)
3711 ret = EFI_SUCCESS;
3712 }
3713 }
3714 }
3715 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003716 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003717 if (ret != EFI_SUCCESS && remain_device_path &&
3718 remain_device_path->type == DEVICE_PATH_TYPE_END)
3719 ret = EFI_SUCCESS;
3720out:
3721 return EFI_EXIT(ret);
3722}
3723
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003724/**
Mario Six78a88f72018-07-10 08:40:17 +02003725 * efi_reinstall_protocol_interface() - reinstall protocol interface
3726 * @handle: handle on which the protocol shall be reinstalled
3727 * @protocol: GUID of the protocol to be installed
3728 * @old_interface: interface to be removed
3729 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003730 *
3731 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003732 *
3733 * See the Unified Extensible Firmware Interface (UEFI) specification for
3734 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003735 *
3736 * The old interface is uninstalled. The new interface is installed.
3737 * Drivers are connected.
3738 *
Mario Six78a88f72018-07-10 08:40:17 +02003739 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003740 */
3741static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3742 efi_handle_t handle, const efi_guid_t *protocol,
3743 void *old_interface, void *new_interface)
3744{
3745 efi_status_t ret;
3746
Heinrich Schuchardtce00a742022-01-16 14:15:31 +01003747 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003748 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003749
3750 /* Uninstall protocol but do not delete handle */
Ilias Apalodimascc889bd2023-08-24 17:21:09 +03003751 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003752 if (ret != EFI_SUCCESS)
3753 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003754
3755 /* Install the new protocol */
3756 ret = efi_add_protocol(handle, protocol, new_interface);
3757 /*
3758 * The UEFI spec does not specify what should happen to the handle
3759 * if in case of an error no protocol interface remains on the handle.
3760 * So let's do nothing here.
3761 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003762 if (ret != EFI_SUCCESS)
3763 goto out;
3764 /*
3765 * The returned status code has to be ignored.
3766 * Do not create an error if no suitable driver for the handle exists.
3767 */
3768 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3769out:
3770 return EFI_EXIT(ret);
3771}
3772
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003773/**
Mario Six78a88f72018-07-10 08:40:17 +02003774 * efi_get_child_controllers() - get all child controllers associated to a driver
3775 * @efiobj: handle of the controller
3776 * @driver_handle: handle of the driver
3777 * @number_of_children: number of child controllers
3778 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003779 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003780 * The allocated buffer has to be freed with free().
3781 *
Mario Six78a88f72018-07-10 08:40:17 +02003782 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003783 */
3784static efi_status_t efi_get_child_controllers(
3785 struct efi_object *efiobj,
3786 efi_handle_t driver_handle,
3787 efi_uintn_t *number_of_children,
3788 efi_handle_t **child_handle_buffer)
3789{
3790 struct efi_handler *handler;
3791 struct efi_open_protocol_info_item *item;
3792 efi_uintn_t count = 0, i;
3793 bool duplicate;
3794
3795 /* Count all child controller associations */
3796 list_for_each_entry(handler, &efiobj->protocols, link) {
3797 list_for_each_entry(item, &handler->open_infos, link) {
3798 if (item->info.agent_handle == driver_handle &&
3799 item->info.attributes &
3800 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3801 ++count;
3802 }
3803 }
3804 /*
3805 * Create buffer. In case of duplicate child controller assignments
3806 * the buffer will be too large. But that does not harm.
3807 */
3808 *number_of_children = 0;
Heinrich Schuchardt1047c6e2020-07-07 04:21:26 +02003809 if (!count)
3810 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003811 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3812 if (!*child_handle_buffer)
3813 return EFI_OUT_OF_RESOURCES;
3814 /* Copy unique child handles */
3815 list_for_each_entry(handler, &efiobj->protocols, link) {
3816 list_for_each_entry(item, &handler->open_infos, link) {
3817 if (item->info.agent_handle == driver_handle &&
3818 item->info.attributes &
3819 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3820 /* Check this is a new child controller */
3821 duplicate = false;
3822 for (i = 0; i < *number_of_children; ++i) {
3823 if ((*child_handle_buffer)[i] ==
3824 item->info.controller_handle)
3825 duplicate = true;
3826 }
3827 /* Copy handle to buffer */
3828 if (!duplicate) {
3829 i = (*number_of_children)++;
3830 (*child_handle_buffer)[i] =
3831 item->info.controller_handle;
3832 }
3833 }
3834 }
3835 }
3836 return EFI_SUCCESS;
3837}
3838
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003839/**
Mario Six78a88f72018-07-10 08:40:17 +02003840 * efi_disconnect_controller() - disconnect a controller from a driver
3841 * @controller_handle: handle of the controller
3842 * @driver_image_handle: handle of the driver
3843 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003844 *
3845 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003846 *
Mario Six78a88f72018-07-10 08:40:17 +02003847 * See the Unified Extensible Firmware Interface (UEFI) specification for
3848 * details.
3849 *
3850 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003851 */
3852static efi_status_t EFIAPI efi_disconnect_controller(
3853 efi_handle_t controller_handle,
3854 efi_handle_t driver_image_handle,
3855 efi_handle_t child_handle)
3856{
3857 struct efi_driver_binding_protocol *binding_protocol;
3858 efi_handle_t *child_handle_buffer = NULL;
3859 size_t number_of_children = 0;
3860 efi_status_t r;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003861 struct efi_object *efiobj;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003862 bool sole_child;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003863
3864 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3865 child_handle);
3866
3867 efiobj = efi_search_obj(controller_handle);
3868 if (!efiobj) {
3869 r = EFI_INVALID_PARAMETER;
3870 goto out;
3871 }
3872
3873 if (child_handle && !efi_search_obj(child_handle)) {
3874 r = EFI_INVALID_PARAMETER;
3875 goto out;
3876 }
3877
3878 /* If no driver handle is supplied, disconnect all drivers */
3879 if (!driver_image_handle) {
3880 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3881 goto out;
3882 }
3883
3884 /* Create list of child handles */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003885 r = efi_get_child_controllers(efiobj,
3886 driver_image_handle,
3887 &number_of_children,
3888 &child_handle_buffer);
3889 if (r != EFI_SUCCESS)
3890 return r;
3891 sole_child = (number_of_children == 1);
3892
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003893 if (child_handle) {
3894 number_of_children = 1;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003895 free(child_handle_buffer);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003896 child_handle_buffer = &child_handle;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003897 }
3898
3899 /* Get the driver binding protocol */
3900 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3901 &efi_guid_driver_binding_protocol,
3902 (void **)&binding_protocol,
3903 driver_image_handle, NULL,
3904 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003905 if (r != EFI_SUCCESS) {
3906 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003907 goto out;
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003908 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003909 /* Remove the children */
3910 if (number_of_children) {
3911 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3912 controller_handle,
3913 number_of_children,
3914 child_handle_buffer));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003915 if (r != EFI_SUCCESS) {
3916 r = EFI_DEVICE_ERROR;
3917 goto out;
3918 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003919 }
3920 /* Remove the driver */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003921 if (!child_handle || sole_child) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003922 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3923 controller_handle,
3924 0, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003925 if (r != EFI_SUCCESS) {
3926 r = EFI_DEVICE_ERROR;
3927 goto out;
3928 }
3929 }
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003930 efi_close_protocol(driver_image_handle,
3931 &efi_guid_driver_binding_protocol,
3932 driver_image_handle, NULL);
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003933 r = EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003934out:
3935 if (!child_handle)
3936 free(child_handle_buffer);
3937 return EFI_EXIT(r);
3938}
3939
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003940static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003941 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003942 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3943 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003944 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003945 },
3946 .raise_tpl = efi_raise_tpl,
3947 .restore_tpl = efi_restore_tpl,
3948 .allocate_pages = efi_allocate_pages_ext,
3949 .free_pages = efi_free_pages_ext,
3950 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003951 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003952 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003953 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003954 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003955 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003956 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003957 .close_event = efi_close_event,
3958 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003959 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003960 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003961 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003962 .handle_protocol = efi_handle_protocol,
3963 .reserved = NULL,
3964 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003965 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003966 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003967 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003968 .load_image = efi_load_image,
3969 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003970 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003971 .unload_image = efi_unload_image,
3972 .exit_boot_services = efi_exit_boot_services,
3973 .get_next_monotonic_count = efi_get_next_monotonic_count,
3974 .stall = efi_stall,
3975 .set_watchdog_timer = efi_set_watchdog_timer,
3976 .connect_controller = efi_connect_controller,
3977 .disconnect_controller = efi_disconnect_controller,
3978 .open_protocol = efi_open_protocol,
Heinrich Schuchardtef185762022-10-07 15:18:15 +02003979 .close_protocol = efi_close_protocol_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003980 .open_protocol_information = efi_open_protocol_information,
3981 .protocols_per_handle = efi_protocols_per_handle,
3982 .locate_handle_buffer = efi_locate_handle_buffer,
3983 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003984 .install_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003985 efi_install_multiple_protocol_interfaces_ext,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003986 .uninstall_multiple_protocol_interfaces =
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +03003987 efi_uninstall_multiple_protocol_interfaces_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003988 .calculate_crc32 = efi_calculate_crc32,
3989 .copy_mem = efi_copy_mem,
3990 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003991 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003992};
3993
Simon Glass156ccbc2022-01-23 12:55:12 -07003994static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003995
Alexander Graf3c63db92016-10-14 13:45:30 +02003996struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003997 .hdr = {
3998 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003999 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02004000 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01004001 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02004002 .fw_vendor = firmware_vendor,
4003 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02004004 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01004005 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004006 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01004007};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004008
4009/**
4010 * efi_initialize_system_table() - Initialize system table
4011 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02004012 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004013 */
4014efi_status_t efi_initialize_system_table(void)
4015{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004016 efi_status_t ret;
4017
4018 /* Allocate configuration table array */
4019 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4020 EFI_MAX_CONFIGURATION_TABLES *
4021 sizeof(struct efi_configuration_table),
4022 (void **)&systab.tables);
4023
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004024 /*
4025 * These entries will be set to NULL in ExitBootServices(). To avoid
4026 * relocation in SetVirtualAddressMap(), set them dynamically.
4027 */
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004028 systab.con_in_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004029 systab.con_in = &efi_con_in;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004030 systab.con_out_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004031 systab.con_out = &efi_con_out;
Heinrich Schuchardt60bba6e2023-01-04 05:56:09 +01004032 systab.stderr_handle = efi_root;
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02004033 systab.std_err = &efi_con_out;
4034 systab.boottime = &efi_boot_services;
4035
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02004036 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004037 efi_update_table_header_crc32(&systab.hdr);
4038 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4039 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02004040
4041 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02004042}