blob: 1823990d9bd54dbfb80a4b95921d233bb687b7dd [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
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass36bf4462019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030018#include <time.h>
Simon Glass3db71102019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimas529441c2020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafbee91162016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060022#include <asm/global_data.h>
AKASHI Takahiro86a3d432021-07-20 14:57:02 +090023#include <asm/setjmp.h>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030024#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010025
26DECLARE_GLOBAL_DATA_PTR;
27
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020028/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010029static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020030
Alexander Grafbee91162016-03-04 01:09:59 +010031/* This list contains all the EFI objects our payload has access to */
32LIST_HEAD(efi_obj_list);
33
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010034/* List of all events */
Heinrich Schuchardt14b40482019-07-11 20:15:09 +020035__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010036
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020037/* List of queued events */
38LIST_HEAD(efi_event_queue);
39
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +020040/* Flag to disable timer activity in ExitBootServices() */
41static bool timers_enabled = true;
42
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +010043/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
44bool efi_st_keep_devices;
45
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020046/* List of all events registered by RegisterProtocolNotify() */
47LIST_HEAD(efi_register_notify_events);
48
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010049/* Handle of the currently executing image */
50static efi_handle_t current_image;
51
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020052#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +010053/*
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020054 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafbee91162016-03-04 01:09:59 +010055 * fixed when compiling U-Boot. However, the payload does not know about that
56 * restriction so we need to manually swap its and our view of that register on
57 * EFI callback entry/exit.
58 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +020059static volatile gd_t *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060060#endif
Alexander Grafbee91162016-03-04 01:09:59 +010061
Heinrich Schuchardt914df752019-02-09 14:10:39 +010062/* 1 if inside U-Boot code, 0 if inside EFI payload code */
63static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040064static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010065/* GUID of the device tree table */
66const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010067/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
68const efi_guid_t efi_guid_driver_binding_protocol =
69 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040070
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010071/* event group ExitBootServices() invoked */
72const efi_guid_t efi_guid_event_group_exit_boot_services =
73 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
74/* event group SetVirtualAddressMap() invoked */
75const efi_guid_t efi_guid_event_group_virtual_address_change =
76 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
77/* event group memory map changed */
78const efi_guid_t efi_guid_event_group_memory_map_change =
79 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
80/* event group boot manager about to boot */
81const efi_guid_t efi_guid_event_group_ready_to_boot =
82 EFI_EVENT_GROUP_READY_TO_BOOT;
83/* event group ResetSystem() invoked (before ExitBootServices) */
84const efi_guid_t efi_guid_event_group_reset_system =
85 EFI_EVENT_GROUP_RESET_SYSTEM;
Heinrich Schuchardtb6f11092020-12-04 03:33:41 +010086/* GUIDs of the Load File and Load File2 protocols */
87const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
88const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090089/* GUID of the SMBIOS table */
90const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010091
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010092static efi_status_t EFIAPI efi_disconnect_controller(
93 efi_handle_t controller_handle,
94 efi_handle_t driver_image_handle,
95 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010096
Rob Clarkc160d2f2017-07-27 08:04:18 -040097/* Called on every callback entry */
98int __efi_entry_check(void)
99{
100 int ret = entry_count++ == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200101#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clarkc160d2f2017-07-27 08:04:18 -0400102 assert(efi_gd);
103 app_gd = gd;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200104 set_gd(efi_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400105#endif
106 return ret;
107}
108
109/* Called on every callback exit */
110int __efi_exit_check(void)
111{
112 int ret = --entry_count == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200113#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200114 set_gd(app_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400115#endif
116 return ret;
117}
118
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200119/**
120 * efi_save_gd() - save global data register
121 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200122 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200123 * As this register may be overwritten by an EFI payload we save it here
124 * and restore it on every callback entered.
125 *
126 * This function is called after relocation from initr_reloc_global_data().
127 */
Alexander Grafbee91162016-03-04 01:09:59 +0100128void efi_save_gd(void)
129{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200130#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100131 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600132#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100133}
134
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200135/**
136 * efi_restore_gd() - restore global data register
137 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200138 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200139 * Restore it after returning from the UEFI world to the value saved via
140 * efi_save_gd().
Rob Clarkc160d2f2017-07-27 08:04:18 -0400141 */
Alexander Grafbee91162016-03-04 01:09:59 +0100142void efi_restore_gd(void)
143{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200144#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100145 /* Only restore if we're already in EFI context */
146 if (!efi_gd)
147 return;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200148 set_gd(efi_gd);
Simon Glass65e4c0b2016-09-25 15:27:35 -0600149#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100150}
151
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200152/**
Mario Six78a88f72018-07-10 08:40:17 +0200153 * indent_string() - returns a string for indenting with two spaces per level
154 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100155 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200156 * A maximum of ten indent levels is supported. Higher indent levels will be
157 * truncated.
158 *
Mario Six78a88f72018-07-10 08:40:17 +0200159 * Return: A string for indenting with two spaces per level is
160 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400161 */
162static const char *indent_string(int level)
163{
164 const char *indent = " ";
165 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100166
Rob Clarkaf65db82017-07-27 08:04:19 -0400167 level = min(max, level * 2);
168 return &indent[max - level];
169}
170
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200171const char *__efi_nesting(void)
172{
173 return indent_string(nesting_level);
174}
175
Rob Clarkaf65db82017-07-27 08:04:19 -0400176const char *__efi_nesting_inc(void)
177{
178 return indent_string(nesting_level++);
179}
180
181const char *__efi_nesting_dec(void)
182{
183 return indent_string(--nesting_level);
184}
185
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200186/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200187 * efi_event_is_queued() - check if an event is queued
188 *
189 * @event: event
190 * Return: true if event is queued
191 */
192static bool efi_event_is_queued(struct efi_event *event)
193{
194 return !!event->queue_link.next;
195}
196
197/**
198 * efi_process_event_queue() - process event queue
199 */
200static void efi_process_event_queue(void)
201{
202 while (!list_empty(&efi_event_queue)) {
203 struct efi_event *event;
204 efi_uintn_t old_tpl;
205
206 event = list_first_entry(&efi_event_queue, struct efi_event,
207 queue_link);
208 if (efi_tpl >= event->notify_tpl)
209 return;
210 list_del(&event->queue_link);
211 event->queue_link.next = NULL;
212 event->queue_link.prev = NULL;
213 /* Events must be executed at the event's TPL */
214 old_tpl = efi_tpl;
215 efi_tpl = event->notify_tpl;
216 EFI_CALL_VOID(event->notify_function(event,
217 event->notify_context));
218 efi_tpl = old_tpl;
219 if (event->type == EVT_NOTIFY_SIGNAL)
220 event->is_signaled = 0;
221 }
222}
223
224/**
Mario Six78a88f72018-07-10 08:40:17 +0200225 * efi_queue_event() - queue an EFI event
226 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200227 *
228 * This function queues the notification function of the event for future
229 * execution.
230 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200231 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200232static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200233{
Heinrich Schuchardt2b8568f2020-03-06 21:56:10 +0100234 struct efi_event *item;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200235
236 if (!event->notify_function)
237 return;
238
239 if (!efi_event_is_queued(event)) {
240 /*
241 * Events must be notified in order of decreasing task priority
242 * level. Insert the new event accordingly.
243 */
244 list_for_each_entry(item, &efi_event_queue, queue_link) {
245 if (item->notify_tpl < event->notify_tpl) {
246 list_add_tail(&event->queue_link,
247 &item->queue_link);
248 event = NULL;
249 break;
250 }
251 }
252 if (event)
253 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardtb7d186f2020-12-28 00:25:34 +0100254 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200255 }
256}
257
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200258/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200259 * is_valid_tpl() - check if the task priority level is valid
260 *
261 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200262 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200263 */
264efi_status_t is_valid_tpl(efi_uintn_t tpl)
265{
266 switch (tpl) {
267 case TPL_APPLICATION:
268 case TPL_CALLBACK:
269 case TPL_NOTIFY:
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200270 return EFI_SUCCESS;
271 default:
272 return EFI_INVALID_PARAMETER;
273 }
274}
275
276/**
Mario Six78a88f72018-07-10 08:40:17 +0200277 * efi_signal_event() - signal an EFI event
278 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100279 *
Heinrich Schuchardt2a0f80f2020-12-28 00:59:09 +0100280 * This function signals an event. If the event belongs to an event group, all
281 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100282 * their notification function is queued.
283 *
284 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100285 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200286void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100287{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200288 if (event->is_signaled)
289 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100290 if (event->group) {
291 struct efi_event *evt;
292
293 /*
294 * The signaled state has to set before executing any
295 * notification function
296 */
297 list_for_each_entry(evt, &efi_events, link) {
298 if (!evt->group || guidcmp(evt->group, event->group))
299 continue;
300 if (evt->is_signaled)
301 continue;
302 evt->is_signaled = true;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100303 }
304 list_for_each_entry(evt, &efi_events, link) {
305 if (!evt->group || guidcmp(evt->group, event->group))
306 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200307 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100308 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200309 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100310 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200311 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100312 }
313}
314
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200315/**
Mario Six78a88f72018-07-10 08:40:17 +0200316 * efi_raise_tpl() - raise the task priority level
317 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200318 *
319 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200320 *
Mario Six78a88f72018-07-10 08:40:17 +0200321 * See the Unified Extensible Firmware Interface (UEFI) specification for
322 * details.
323 *
324 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200325 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100326static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100327{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100328 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200329
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200330 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200331
332 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200333 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200334 efi_tpl = new_tpl;
335 if (efi_tpl > TPL_HIGH_LEVEL)
336 efi_tpl = TPL_HIGH_LEVEL;
337
338 EFI_EXIT(EFI_SUCCESS);
339 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100340}
341
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200342/**
Mario Six78a88f72018-07-10 08:40:17 +0200343 * efi_restore_tpl() - lower the task priority level
344 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200345 *
346 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200347 *
Mario Six78a88f72018-07-10 08:40:17 +0200348 * See the Unified Extensible Firmware Interface (UEFI) specification for
349 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200350 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100351static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100352{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200353 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200354
355 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200356 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200357 efi_tpl = old_tpl;
358 if (efi_tpl > TPL_HIGH_LEVEL)
359 efi_tpl = TPL_HIGH_LEVEL;
360
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100361 /*
362 * Lowering the TPL may have made queued events eligible for execution.
363 */
364 efi_timer_check();
365
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200366 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100367}
368
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200369/**
Mario Six78a88f72018-07-10 08:40:17 +0200370 * efi_allocate_pages_ext() - allocate memory pages
371 * @type: type of allocation to be performed
372 * @memory_type: usage type of the allocated memory
373 * @pages: number of pages to be allocated
374 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200375 *
376 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200377 *
Mario Six78a88f72018-07-10 08:40:17 +0200378 * See the Unified Extensible Firmware Interface (UEFI) specification for
379 * details.
380 *
381 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200382 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900383static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100384 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900385 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100386{
387 efi_status_t r;
388
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100389 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100390 r = efi_allocate_pages(type, memory_type, pages, memory);
391 return EFI_EXIT(r);
392}
393
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200394/**
Mario Six78a88f72018-07-10 08:40:17 +0200395 * efi_free_pages_ext() - Free memory pages.
396 * @memory: start of the memory area to be freed
397 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200398 *
399 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200400 *
Mario Six78a88f72018-07-10 08:40:17 +0200401 * See the Unified Extensible Firmware Interface (UEFI) specification for
402 * details.
403 *
404 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200405 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900406static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100407 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100408{
409 efi_status_t r;
410
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900411 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100412 r = efi_free_pages(memory, pages);
413 return EFI_EXIT(r);
414}
415
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200416/**
Mario Six78a88f72018-07-10 08:40:17 +0200417 * efi_get_memory_map_ext() - get map describing memory usage
418 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
419 * on exit the size of the copied memory map
420 * @memory_map: buffer to which the memory map is written
421 * @map_key: key for the memory map
422 * @descriptor_size: size of an individual memory descriptor
423 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200424 *
425 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200426 *
Mario Six78a88f72018-07-10 08:40:17 +0200427 * See the Unified Extensible Firmware Interface (UEFI) specification for
428 * details.
429 *
430 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200431 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900432static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100433 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900434 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100435 efi_uintn_t *map_key,
436 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900437 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100438{
439 efi_status_t r;
440
441 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
442 map_key, descriptor_size, descriptor_version);
443 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
444 descriptor_size, descriptor_version);
445 return EFI_EXIT(r);
446}
447
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200448/**
Mario Six78a88f72018-07-10 08:40:17 +0200449 * efi_allocate_pool_ext() - allocate memory from pool
450 * @pool_type: type of the pool from which memory is to be allocated
451 * @size: number of bytes to be allocated
452 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200453 *
454 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200455 *
Mario Six78a88f72018-07-10 08:40:17 +0200456 * See the Unified Extensible Firmware Interface (UEFI) specification for
457 * details.
458 *
459 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200460 */
Stefan Brünsead12742016-10-09 22:17:18 +0200461static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100462 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200463 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100464{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100465 efi_status_t r;
466
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100467 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200468 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100469 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100470}
471
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200472/**
Mario Six78a88f72018-07-10 08:40:17 +0200473 * efi_free_pool_ext() - free memory from pool
474 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200475 *
476 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200477 *
Mario Six78a88f72018-07-10 08:40:17 +0200478 * See the Unified Extensible Firmware Interface (UEFI) specification for
479 * details.
480 *
481 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200482 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200483static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100484{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100485 efi_status_t r;
486
487 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200488 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100489 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100490}
491
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200492/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200493 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100494 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200495 * @handle: handle to be added
496 *
497 * The protocols list is initialized. The handle is added to the list of known
498 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100499 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200500void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100501{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200502 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100503 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200504 INIT_LIST_HEAD(&handle->protocols);
505 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100506}
507
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200508/**
Mario Six78a88f72018-07-10 08:40:17 +0200509 * efi_create_handle() - create handle
510 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200511 *
Mario Six78a88f72018-07-10 08:40:17 +0200512 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200513 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100514efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200515{
516 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200517
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200518 obj = calloc(1, sizeof(struct efi_object));
519 if (!obj)
520 return EFI_OUT_OF_RESOURCES;
521
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100522 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200523 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200524
525 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200526}
527
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200528/**
Mario Six78a88f72018-07-10 08:40:17 +0200529 * efi_search_protocol() - find a protocol on a handle.
530 * @handle: handle
531 * @protocol_guid: GUID of the protocol
532 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100533 *
Mario Six78a88f72018-07-10 08:40:17 +0200534 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100535 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100536efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100537 const efi_guid_t *protocol_guid,
538 struct efi_handler **handler)
539{
540 struct efi_object *efiobj;
541 struct list_head *lhandle;
542
543 if (!handle || !protocol_guid)
544 return EFI_INVALID_PARAMETER;
545 efiobj = efi_search_obj(handle);
546 if (!efiobj)
547 return EFI_INVALID_PARAMETER;
548 list_for_each(lhandle, &efiobj->protocols) {
549 struct efi_handler *protocol;
550
551 protocol = list_entry(lhandle, struct efi_handler, link);
552 if (!guidcmp(protocol->guid, protocol_guid)) {
553 if (handler)
554 *handler = protocol;
555 return EFI_SUCCESS;
556 }
557 }
558 return EFI_NOT_FOUND;
559}
560
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200561/**
Mario Six78a88f72018-07-10 08:40:17 +0200562 * efi_remove_protocol() - delete protocol from a handle
563 * @handle: handle from which the protocol shall be deleted
564 * @protocol: GUID of the protocol to be deleted
565 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100566 *
Mario Six78a88f72018-07-10 08:40:17 +0200567 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100568 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100569efi_status_t efi_remove_protocol(const efi_handle_t handle,
570 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100571 void *protocol_interface)
572{
573 struct efi_handler *handler;
574 efi_status_t ret;
575
576 ret = efi_search_protocol(handle, protocol, &handler);
577 if (ret != EFI_SUCCESS)
578 return ret;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200579 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200580 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100581 list_del(&handler->link);
582 free(handler);
583 return EFI_SUCCESS;
584}
585
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200586/**
Mario Six78a88f72018-07-10 08:40:17 +0200587 * efi_remove_all_protocols() - delete all protocols from a handle
588 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100589 *
Mario Six78a88f72018-07-10 08:40:17 +0200590 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100591 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100592efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100593{
594 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100595 struct efi_handler *protocol;
596 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100597
598 efiobj = efi_search_obj(handle);
599 if (!efiobj)
600 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100601 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100602 efi_status_t ret;
603
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100604 ret = efi_remove_protocol(handle, protocol->guid,
605 protocol->protocol_interface);
606 if (ret != EFI_SUCCESS)
607 return ret;
608 }
609 return EFI_SUCCESS;
610}
611
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200612/**
Mario Six78a88f72018-07-10 08:40:17 +0200613 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100614 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200615 * @handle: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100616 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200617void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100618{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200619 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100620 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200621 efi_remove_all_protocols(handle);
622 list_del(&handle->link);
623 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100624}
625
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200626/**
Mario Six78a88f72018-07-10 08:40:17 +0200627 * efi_is_event() - check if a pointer is a valid event
628 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100629 *
Mario Six78a88f72018-07-10 08:40:17 +0200630 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100631 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100632static efi_status_t efi_is_event(const struct efi_event *event)
633{
634 const struct efi_event *evt;
635
636 if (!event)
637 return EFI_INVALID_PARAMETER;
638 list_for_each_entry(evt, &efi_events, link) {
639 if (evt == event)
640 return EFI_SUCCESS;
641 }
642 return EFI_INVALID_PARAMETER;
643}
Alexander Grafbee91162016-03-04 01:09:59 +0100644
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200645/**
Mario Six78a88f72018-07-10 08:40:17 +0200646 * efi_create_event() - create an event
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200647 *
Mario Six78a88f72018-07-10 08:40:17 +0200648 * @type: type of the event to create
649 * @notify_tpl: task priority level of the event
650 * @notify_function: notification function of the event
651 * @notify_context: pointer passed to the notification function
652 * @group: event group
653 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200654 *
655 * This function is used inside U-Boot code to create an event.
656 *
657 * For the API function implementing the CreateEvent service see
658 * efi_create_event_ext.
659 *
Mario Six78a88f72018-07-10 08:40:17 +0200660 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200661 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100662efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200663 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200664 struct efi_event *event,
665 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100666 void *notify_context, efi_guid_t *group,
667 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100668{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100669 struct efi_event *evt;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200670 efi_status_t ret;
671 int pool_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200672
Jonathan Graya95343b2017-03-12 19:26:07 +1100673 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200674 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100675
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200676 switch (type) {
677 case 0:
678 case EVT_TIMER:
679 case EVT_NOTIFY_SIGNAL:
680 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
681 case EVT_NOTIFY_WAIT:
682 case EVT_TIMER | EVT_NOTIFY_WAIT:
683 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200684 pool_type = EFI_BOOT_SERVICES_DATA;
685 break;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200686 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200687 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200688 break;
689 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200690 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200691 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100692
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100693 /*
694 * The UEFI specification requires event notification levels to be
695 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
696 *
697 * Parameter NotifyTpl should not be checked if it is not used.
698 */
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900699 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt2cfb68f2021-01-06 12:55:22 +0100700 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
701 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200702 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100703
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200704 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
705 (void **)&evt);
706 if (ret != EFI_SUCCESS)
707 return ret;
708 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100709 evt->type = type;
710 evt->notify_tpl = notify_tpl;
711 evt->notify_function = notify_function;
712 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100713 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200714 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100715 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100716 list_add_tail(&evt->link, &efi_events);
717 *event = evt;
718 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100719}
720
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200721/*
Mario Six78a88f72018-07-10 08:40:17 +0200722 * efi_create_event_ex() - create an event in a group
723 * @type: type of the event to create
724 * @notify_tpl: task priority level of the event
725 * @notify_function: notification function of the event
726 * @notify_context: pointer passed to the notification function
727 * @event: created event
728 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100729 *
730 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100731 *
Mario Six78a88f72018-07-10 08:40:17 +0200732 * See the Unified Extensible Firmware Interface (UEFI) specification for
733 * details.
734 *
735 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100736 */
737efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
738 void (EFIAPI *notify_function) (
739 struct efi_event *event,
740 void *context),
741 void *notify_context,
742 efi_guid_t *event_group,
743 struct efi_event **event)
744{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200745 efi_status_t ret;
746
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100747 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
748 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200749
750 /*
751 * The allowable input parameters are the same as in CreateEvent()
752 * except for the following two disallowed event types.
753 */
754 switch (type) {
755 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
756 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
757 ret = EFI_INVALID_PARAMETER;
758 goto out;
759 }
760
761 ret = efi_create_event(type, notify_tpl, notify_function,
762 notify_context, event_group, event);
763out:
764 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100765}
766
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200767/**
Mario Six78a88f72018-07-10 08:40:17 +0200768 * efi_create_event_ext() - create an event
769 * @type: type of the event to create
770 * @notify_tpl: task priority level of the event
771 * @notify_function: notification function of the event
772 * @notify_context: pointer passed to the notification function
773 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200774 *
775 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200776 *
Mario Six78a88f72018-07-10 08:40:17 +0200777 * See the Unified Extensible Firmware Interface (UEFI) specification for
778 * details.
779 *
780 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200781 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200782static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100783 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200784 void (EFIAPI *notify_function) (
785 struct efi_event *event,
786 void *context),
787 void *notify_context, struct efi_event **event)
788{
789 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
790 notify_context);
791 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100792 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200793}
794
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200795/**
Mario Six78a88f72018-07-10 08:40:17 +0200796 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200797 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200798 * Check if a timer event has occurred or a queued notification function should
799 * be called.
800 *
Alexander Grafbee91162016-03-04 01:09:59 +0100801 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200802 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100803 */
804void efi_timer_check(void)
805{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100806 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100807 u64 now = timer_get_us();
808
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100809 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200810 if (!timers_enabled)
811 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100812 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200813 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100814 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200815 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100816 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200817 break;
818 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100819 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200820 break;
821 default:
822 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200823 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100824 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200825 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100826 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200827 efi_process_event_queue();
Alexander Grafbee91162016-03-04 01:09:59 +0100828 WATCHDOG_RESET();
829}
830
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200831/**
Mario Six78a88f72018-07-10 08:40:17 +0200832 * efi_set_timer() - set the trigger time for a timer event or stop the event
833 * @event: event for which the timer is set
834 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200835 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200836 *
837 * This is the function for internal usage in U-Boot. For the API function
838 * implementing the SetTimer service see efi_set_timer_ext.
839 *
Mario Six78a88f72018-07-10 08:40:17 +0200840 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200841 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200842efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200843 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100844{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100845 /* Check that the event is valid */
846 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
847 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100848
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200849 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200850 * The parameter defines a multiple of 100 ns.
851 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200852 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200853 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100854
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100855 switch (type) {
856 case EFI_TIMER_STOP:
857 event->trigger_next = -1ULL;
858 break;
859 case EFI_TIMER_PERIODIC:
860 case EFI_TIMER_RELATIVE:
861 event->trigger_next = timer_get_us() + trigger_time;
862 break;
863 default:
864 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100865 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100866 event->trigger_type = type;
867 event->trigger_time = trigger_time;
868 event->is_signaled = false;
869 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200870}
871
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200872/**
Mario Six78a88f72018-07-10 08:40:17 +0200873 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
874 * event
875 * @event: event for which the timer is set
876 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200877 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200878 *
879 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200880 *
Mario Six78a88f72018-07-10 08:40:17 +0200881 * See the Unified Extensible Firmware Interface (UEFI) specification for
882 * details.
883 *
884 *
885 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200886 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200887static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
888 enum efi_timer_delay type,
889 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200890{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900891 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200892 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100893}
894
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200895/**
Mario Six78a88f72018-07-10 08:40:17 +0200896 * efi_wait_for_event() - wait for events to be signaled
897 * @num_events: number of events to be waited for
898 * @event: events to be waited for
899 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200900 *
901 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200902 *
Mario Six78a88f72018-07-10 08:40:17 +0200903 * See the Unified Extensible Firmware Interface (UEFI) specification for
904 * details.
905 *
906 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200907 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100908static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200909 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100910 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100911{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100912 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100913
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100914 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100915
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200916 /* Check parameters */
917 if (!num_events || !event)
918 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200919 /* Check TPL */
920 if (efi_tpl != TPL_APPLICATION)
921 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200922 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100923 if (efi_is_event(event[i]) != EFI_SUCCESS)
924 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200925 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
926 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200927 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200928 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200929 }
930
931 /* Wait for signal */
932 for (;;) {
933 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200934 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200935 goto out;
936 }
937 /* Allow events to occur. */
938 efi_timer_check();
939 }
940
941out:
942 /*
943 * Reset the signal which is passed to the caller to allow periodic
944 * events to occur.
945 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200946 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200947 if (index)
948 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100949
950 return EFI_EXIT(EFI_SUCCESS);
951}
952
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200953/**
Mario Six78a88f72018-07-10 08:40:17 +0200954 * efi_signal_event_ext() - signal an EFI event
955 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200956 *
957 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200958 *
959 * See the Unified Extensible Firmware Interface (UEFI) specification for
960 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200961 *
962 * This functions sets the signaled state of the event and queues the
963 * notification function for execution.
964 *
Mario Six78a88f72018-07-10 08:40:17 +0200965 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200966 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200967static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100968{
969 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100970 if (efi_is_event(event) != EFI_SUCCESS)
971 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200972 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +0100973 return EFI_EXIT(EFI_SUCCESS);
974}
975
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200976/**
Mario Six78a88f72018-07-10 08:40:17 +0200977 * efi_close_event() - close an EFI event
978 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200979 *
980 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200981 *
Mario Six78a88f72018-07-10 08:40:17 +0200982 * See the Unified Extensible Firmware Interface (UEFI) specification for
983 * details.
984 *
985 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200986 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200987static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100988{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200989 struct efi_register_notify_event *item, *next;
990
Alexander Grafbee91162016-03-04 01:09:59 +0100991 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100992 if (efi_is_event(event) != EFI_SUCCESS)
993 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200994
995 /* Remove protocol notify registrations for the event */
996 list_for_each_entry_safe(item, next, &efi_register_notify_events,
997 link) {
998 if (event == item->event) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +0200999 struct efi_protocol_notification *hitem, *hnext;
1000
1001 /* Remove signaled handles */
1002 list_for_each_entry_safe(hitem, hnext, &item->handles,
1003 link) {
1004 list_del(&hitem->link);
1005 free(hitem);
1006 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001007 list_del(&item->link);
1008 free(item);
1009 }
1010 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +02001011 /* Remove event from queue */
1012 if (efi_event_is_queued(event))
1013 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001014
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001015 list_del(&event->link);
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001016 efi_free_pool(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001017 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001018}
1019
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001020/**
Mario Six78a88f72018-07-10 08:40:17 +02001021 * efi_check_event() - check if an event is signaled
1022 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001023 *
1024 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001025 *
Mario Six78a88f72018-07-10 08:40:17 +02001026 * See the Unified Extensible Firmware Interface (UEFI) specification for
1027 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001028 *
Mario Six78a88f72018-07-10 08:40:17 +02001029 * If an event is not signaled yet, the notification function is queued. The
1030 * signaled state is cleared.
1031 *
1032 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001033 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001034static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001035{
1036 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001037 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001038 if (efi_is_event(event) != EFI_SUCCESS ||
1039 event->type & EVT_NOTIFY_SIGNAL)
1040 return EFI_EXIT(EFI_INVALID_PARAMETER);
1041 if (!event->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001042 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001043 if (event->is_signaled) {
1044 event->is_signaled = false;
1045 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001046 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001047 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001048}
1049
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001050/**
Mario Six78a88f72018-07-10 08:40:17 +02001051 * efi_search_obj() - find the internal EFI object for a handle
1052 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001053 *
Mario Six78a88f72018-07-10 08:40:17 +02001054 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001055 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001056struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001057{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001058 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001059
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001060 if (!handle)
1061 return NULL;
1062
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001063 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001064 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001065 return efiobj;
1066 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001067 return NULL;
1068}
1069
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001070/**
Mario Six78a88f72018-07-10 08:40:17 +02001071 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1072 * to a protocol
1073 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001074 *
Mario Six78a88f72018-07-10 08:40:17 +02001075 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001076 */
1077static struct efi_open_protocol_info_entry *efi_create_open_info(
1078 struct efi_handler *handler)
1079{
1080 struct efi_open_protocol_info_item *item;
1081
1082 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1083 if (!item)
1084 return NULL;
1085 /* Append the item to the open protocol info list. */
1086 list_add_tail(&item->link, &handler->open_infos);
1087
1088 return &item->info;
1089}
1090
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001091/**
Mario Six78a88f72018-07-10 08:40:17 +02001092 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1093 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001094 *
Mario Six78a88f72018-07-10 08:40:17 +02001095 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001096 */
1097static efi_status_t efi_delete_open_info(
1098 struct efi_open_protocol_info_item *item)
1099{
1100 list_del(&item->link);
1101 free(item);
1102 return EFI_SUCCESS;
1103}
1104
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001105/**
Mario Six78a88f72018-07-10 08:40:17 +02001106 * efi_add_protocol() - install new protocol on a handle
1107 * @handle: handle on which the protocol shall be installed
1108 * @protocol: GUID of the protocol to be installed
1109 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001110 *
Mario Six78a88f72018-07-10 08:40:17 +02001111 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001112 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001113efi_status_t efi_add_protocol(const efi_handle_t handle,
1114 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001115 void *protocol_interface)
1116{
1117 struct efi_object *efiobj;
1118 struct efi_handler *handler;
1119 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001120 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001121
1122 efiobj = efi_search_obj(handle);
1123 if (!efiobj)
1124 return EFI_INVALID_PARAMETER;
1125 ret = efi_search_protocol(handle, protocol, NULL);
1126 if (ret != EFI_NOT_FOUND)
1127 return EFI_INVALID_PARAMETER;
1128 handler = calloc(1, sizeof(struct efi_handler));
1129 if (!handler)
1130 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001131 handler->guid = protocol;
1132 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001133 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001134 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001135
1136 /* Notify registered events */
1137 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001138 if (!guidcmp(protocol, &event->protocol)) {
1139 struct efi_protocol_notification *notif;
1140
1141 notif = calloc(1, sizeof(*notif));
1142 if (!notif) {
1143 list_del(&handler->link);
1144 free(handler);
1145 return EFI_OUT_OF_RESOURCES;
1146 }
1147 notif->handle = handle;
1148 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001149 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001150 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001151 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001152 }
1153
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001154 if (!guidcmp(&efi_guid_device_path, protocol))
1155 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001156 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001157}
1158
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001159/**
Mario Six78a88f72018-07-10 08:40:17 +02001160 * efi_install_protocol_interface() - install protocol interface
1161 * @handle: handle on which the protocol shall be installed
1162 * @protocol: GUID of the protocol to be installed
1163 * @protocol_interface_type: type of the interface to be installed,
1164 * always EFI_NATIVE_INTERFACE
1165 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001166 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001167 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001168 *
Mario Six78a88f72018-07-10 08:40:17 +02001169 * See the Unified Extensible Firmware Interface (UEFI) specification for
1170 * details.
1171 *
1172 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001173 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001174static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001175 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001176 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001177{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001178 efi_status_t r;
1179
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001180 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1181 protocol_interface);
1182
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001183 if (!handle || !protocol ||
1184 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1185 r = EFI_INVALID_PARAMETER;
1186 goto out;
1187 }
1188
1189 /* Create new handle if requested. */
1190 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001191 r = efi_create_handle(handle);
1192 if (r != EFI_SUCCESS)
1193 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001194 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001195 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001196 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001197 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001198 /* Add new protocol */
1199 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001200out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001201 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001202}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001203
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001204/**
Mario Six78a88f72018-07-10 08:40:17 +02001205 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001206 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001207 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001208 * @number_of_drivers: number of child controllers
1209 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001210 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001211 * The allocated buffer has to be freed with free().
1212 *
Mario Six78a88f72018-07-10 08:40:17 +02001213 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001214 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001215static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001216 const efi_guid_t *protocol,
1217 efi_uintn_t *number_of_drivers,
1218 efi_handle_t **driver_handle_buffer)
1219{
1220 struct efi_handler *handler;
1221 struct efi_open_protocol_info_item *item;
1222 efi_uintn_t count = 0, i;
1223 bool duplicate;
1224
1225 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001226 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001227 if (protocol && guidcmp(handler->guid, protocol))
1228 continue;
1229 list_for_each_entry(item, &handler->open_infos, link) {
1230 if (item->info.attributes &
1231 EFI_OPEN_PROTOCOL_BY_DRIVER)
1232 ++count;
1233 }
1234 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001235 *number_of_drivers = 0;
1236 if (!count) {
1237 *driver_handle_buffer = NULL;
1238 return EFI_SUCCESS;
1239 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001240 /*
1241 * Create buffer. In case of duplicate driver assignments the buffer
1242 * will be too large. But that does not harm.
1243 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001244 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1245 if (!*driver_handle_buffer)
1246 return EFI_OUT_OF_RESOURCES;
1247 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001248 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001249 if (protocol && guidcmp(handler->guid, protocol))
1250 continue;
1251 list_for_each_entry(item, &handler->open_infos, link) {
1252 if (item->info.attributes &
1253 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1254 /* Check this is a new driver */
1255 duplicate = false;
1256 for (i = 0; i < *number_of_drivers; ++i) {
1257 if ((*driver_handle_buffer)[i] ==
1258 item->info.agent_handle)
1259 duplicate = true;
1260 }
1261 /* Copy handle to buffer */
1262 if (!duplicate) {
1263 i = (*number_of_drivers)++;
1264 (*driver_handle_buffer)[i] =
1265 item->info.agent_handle;
1266 }
1267 }
1268 }
1269 }
1270 return EFI_SUCCESS;
1271}
1272
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001273/**
Mario Six78a88f72018-07-10 08:40:17 +02001274 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001275 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001276 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001277 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001278 *
1279 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001280 *
Mario Six78a88f72018-07-10 08:40:17 +02001281 * See the Unified Extensible Firmware Interface (UEFI) specification for
1282 * details.
1283 *
1284 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001285 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001286static efi_status_t efi_disconnect_all_drivers
1287 (efi_handle_t handle,
1288 const efi_guid_t *protocol,
1289 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001290{
1291 efi_uintn_t number_of_drivers;
1292 efi_handle_t *driver_handle_buffer;
1293 efi_status_t r, ret;
1294
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001295 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001296 &driver_handle_buffer);
1297 if (ret != EFI_SUCCESS)
1298 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001299 if (!number_of_drivers)
1300 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001301 ret = EFI_NOT_FOUND;
1302 while (number_of_drivers) {
1303 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001304 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001305 driver_handle_buffer[--number_of_drivers],
1306 child_handle));
1307 if (r == EFI_SUCCESS)
1308 ret = r;
1309 }
1310 free(driver_handle_buffer);
1311 return ret;
1312}
1313
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001314/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001315 * efi_uninstall_protocol() - uninstall protocol interface
1316 *
Mario Six78a88f72018-07-10 08:40:17 +02001317 * @handle: handle from which the protocol shall be removed
1318 * @protocol: GUID of the protocol to be removed
1319 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001320 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001321 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001322 *
1323 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001324 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001325static efi_status_t efi_uninstall_protocol
1326 (efi_handle_t handle, const efi_guid_t *protocol,
1327 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001328{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001329 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001330 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001331 struct efi_open_protocol_info_item *item;
1332 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001333 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001334
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001335 /* Check handle */
1336 efiobj = efi_search_obj(handle);
1337 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001338 r = EFI_INVALID_PARAMETER;
1339 goto out;
1340 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001341 /* Find the protocol on the handle */
1342 r = efi_search_protocol(handle, protocol, &handler);
1343 if (r != EFI_SUCCESS)
1344 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001345 /* Disconnect controllers */
1346 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001347 /* Close protocol */
1348 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1349 if (item->info.attributes ==
1350 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1351 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1352 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1353 list_del(&item->link);
1354 }
1355 if (!list_empty(&handler->open_infos)) {
1356 r = EFI_ACCESS_DENIED;
1357 goto out;
1358 }
1359 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001360out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001361 return r;
1362}
1363
1364/**
1365 * efi_uninstall_protocol_interface() - uninstall protocol interface
1366 * @handle: handle from which the protocol shall be removed
1367 * @protocol: GUID of the protocol to be removed
1368 * @protocol_interface: interface to be removed
1369 *
1370 * This function implements the UninstallProtocolInterface service.
1371 *
1372 * See the Unified Extensible Firmware Interface (UEFI) specification for
1373 * details.
1374 *
1375 * Return: status code
1376 */
1377static efi_status_t EFIAPI efi_uninstall_protocol_interface
1378 (efi_handle_t handle, const efi_guid_t *protocol,
1379 void *protocol_interface)
1380{
1381 efi_status_t ret;
1382
1383 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1384
1385 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1386 if (ret != EFI_SUCCESS)
1387 goto out;
1388
1389 /* If the last protocol has been removed, delete the handle. */
1390 if (list_empty(&handle->protocols)) {
1391 list_del(&handle->link);
1392 free(handle);
1393 }
1394out:
1395 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001396}
1397
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001398/**
Mario Six78a88f72018-07-10 08:40:17 +02001399 * efi_register_protocol_notify() - register an event for notification when a
1400 * protocol is installed.
1401 * @protocol: GUID of the protocol whose installation shall be notified
1402 * @event: event to be signaled upon installation of the protocol
1403 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001404 *
1405 * This function implements the RegisterProtocolNotify service.
1406 * See the Unified Extensible Firmware Interface (UEFI) specification
1407 * for details.
1408 *
Mario Six78a88f72018-07-10 08:40:17 +02001409 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001410 */
Jose Marinho64a8aae2021-03-02 17:26:38 +00001411efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1412 struct efi_event *event,
1413 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001414{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001415 struct efi_register_notify_event *item;
1416 efi_status_t ret = EFI_SUCCESS;
1417
Rob Clark778e6af2017-09-13 18:05:41 -04001418 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001419
1420 if (!protocol || !event || !registration) {
1421 ret = EFI_INVALID_PARAMETER;
1422 goto out;
1423 }
1424
1425 item = calloc(1, sizeof(struct efi_register_notify_event));
1426 if (!item) {
1427 ret = EFI_OUT_OF_RESOURCES;
1428 goto out;
1429 }
1430
1431 item->event = event;
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301432 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001433 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001434
1435 list_add_tail(&item->link, &efi_register_notify_events);
1436
1437 *registration = item;
1438out:
1439 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001440}
1441
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001442/**
Mario Six78a88f72018-07-10 08:40:17 +02001443 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001444 *
Mario Six78a88f72018-07-10 08:40:17 +02001445 * @search_type: selection criterion
1446 * @protocol: GUID of the protocol
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001447 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001448 *
1449 * See the documentation of the LocateHandle service in the UEFI specification.
1450 *
Mario Six78a88f72018-07-10 08:40:17 +02001451 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001452 */
Alexander Grafbee91162016-03-04 01:09:59 +01001453static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001454 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001455{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001456 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001457
1458 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001459 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001460 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001461 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001462 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001463 return (ret != EFI_SUCCESS);
1464 default:
1465 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001466 return -1;
1467 }
Alexander Grafbee91162016-03-04 01:09:59 +01001468}
1469
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001470/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001471 * efi_check_register_notify_event() - check if registration key is valid
1472 *
1473 * Check that a pointer is a valid registration key as returned by
1474 * RegisterProtocolNotify().
1475 *
1476 * @key: registration key
1477 * Return: valid registration key or NULL
1478 */
1479static struct efi_register_notify_event *efi_check_register_notify_event
1480 (void *key)
1481{
1482 struct efi_register_notify_event *event;
1483
1484 list_for_each_entry(event, &efi_register_notify_events, link) {
1485 if (event == (struct efi_register_notify_event *)key)
1486 return event;
1487 }
1488 return NULL;
1489}
1490
1491/**
Mario Six78a88f72018-07-10 08:40:17 +02001492 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001493 *
1494 * @search_type: selection criterion
1495 * @protocol: GUID of the protocol
1496 * @search_key: registration key
1497 * @buffer_size: size of the buffer to receive the handles in bytes
1498 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001499 *
1500 * This function is meant for U-Boot internal calls. For the API implementation
1501 * of the LocateHandle service see efi_locate_handle_ext.
1502 *
Mario Six78a88f72018-07-10 08:40:17 +02001503 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001504 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001505static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001506 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001507 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001508 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001509{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001510 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001511 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001512 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001513 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001514
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001515 /* Check parameters */
1516 switch (search_type) {
1517 case ALL_HANDLES:
1518 break;
1519 case BY_REGISTER_NOTIFY:
1520 if (!search_key)
1521 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001522 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001523 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001524 if (!event)
1525 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001526 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001527 case BY_PROTOCOL:
1528 if (!protocol)
1529 return EFI_INVALID_PARAMETER;
1530 break;
1531 default:
1532 return EFI_INVALID_PARAMETER;
1533 }
1534
Alexander Grafbee91162016-03-04 01:09:59 +01001535 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001536 if (search_type == BY_REGISTER_NOTIFY) {
1537 if (list_empty(&event->handles))
1538 return EFI_NOT_FOUND;
1539 handle = list_first_entry(&event->handles,
1540 struct efi_protocol_notification,
1541 link);
1542 efiobj = handle->handle;
1543 size += sizeof(void *);
1544 } else {
1545 list_for_each_entry(efiobj, &efi_obj_list, link) {
1546 if (!efi_search(search_type, protocol, efiobj))
1547 size += sizeof(void *);
1548 }
1549 if (size == 0)
1550 return EFI_NOT_FOUND;
Alexander Grafbee91162016-03-04 01:09:59 +01001551 }
1552
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001553 if (!buffer_size)
1554 return EFI_INVALID_PARAMETER;
1555
Alexander Grafbee91162016-03-04 01:09:59 +01001556 if (*buffer_size < size) {
1557 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001558 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001559 }
1560
Rob Clark796a78c2017-08-06 14:10:07 -04001561 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001562
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001563 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001564 if (!buffer)
1565 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001566
Alexander Grafbee91162016-03-04 01:09:59 +01001567 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001568 if (search_type == BY_REGISTER_NOTIFY) {
1569 *buffer = efiobj;
1570 list_del(&handle->link);
1571 } else {
1572 list_for_each_entry(efiobj, &efi_obj_list, link) {
1573 if (!efi_search(search_type, protocol, efiobj))
1574 *buffer++ = efiobj;
1575 }
Alexander Grafbee91162016-03-04 01:09:59 +01001576 }
1577
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001578 return EFI_SUCCESS;
1579}
1580
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001581/**
Mario Six78a88f72018-07-10 08:40:17 +02001582 * efi_locate_handle_ext() - locate handles implementing a protocol.
1583 * @search_type: selection criterion
1584 * @protocol: GUID of the protocol
1585 * @search_key: registration key
1586 * @buffer_size: size of the buffer to receive the handles in bytes
1587 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001588 *
1589 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001590 *
Mario Six78a88f72018-07-10 08:40:17 +02001591 * See the Unified Extensible Firmware Interface (UEFI) specification for
1592 * details.
1593 *
1594 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001595 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001596static efi_status_t EFIAPI efi_locate_handle_ext(
1597 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001598 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001599 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001600{
Rob Clark778e6af2017-09-13 18:05:41 -04001601 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001602 buffer_size, buffer);
1603
1604 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1605 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001606}
1607
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001608/**
Mario Six78a88f72018-07-10 08:40:17 +02001609 * efi_remove_configuration_table() - collapses configuration table entries,
1610 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001611 *
Mario Six78a88f72018-07-10 08:40:17 +02001612 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001613 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001614static void efi_remove_configuration_table(int i)
1615{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001616 struct efi_configuration_table *this = &systab.tables[i];
1617 struct efi_configuration_table *next = &systab.tables[i + 1];
1618 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001619
1620 memmove(this, next, (ulong)end - (ulong)next);
1621 systab.nr_tables--;
1622}
1623
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001624/**
Mario Six78a88f72018-07-10 08:40:17 +02001625 * efi_install_configuration_table() - adds, updates, or removes a
1626 * configuration table
1627 * @guid: GUID of the installed table
1628 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001629 *
1630 * This function is used for internal calls. For the API implementation of the
1631 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1632 *
Mario Six78a88f72018-07-10 08:40:17 +02001633 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001634 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001635efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1636 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001637{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001638 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001639 int i;
1640
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001641 if (!guid)
1642 return EFI_INVALID_PARAMETER;
1643
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001644 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001645 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001646 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001647 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001648 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001649 else
1650 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001651 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001652 }
1653 }
1654
Alexander Grafd98cdf62017-07-26 13:41:04 +02001655 if (!table)
1656 return EFI_NOT_FOUND;
1657
Alexander Grafbee91162016-03-04 01:09:59 +01001658 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001659 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001660 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001661
1662 /* Add a new entry */
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301663 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001664 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001665 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001666
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001667out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001668 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001669 efi_update_table_header_crc32(&systab.hdr);
1670
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001671 /* Notify that the configuration table was changed */
1672 list_for_each_entry(evt, &efi_events, link) {
1673 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001674 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001675 break;
1676 }
1677 }
1678
Alexander Graf488bf122016-08-19 01:23:24 +02001679 return EFI_SUCCESS;
1680}
1681
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001682/**
Mario Six78a88f72018-07-10 08:40:17 +02001683 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1684 * configuration table.
1685 * @guid: GUID of the installed table
1686 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001687 *
1688 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001689 *
Mario Six78a88f72018-07-10 08:40:17 +02001690 * See the Unified Extensible Firmware Interface (UEFI) specification for
1691 * details.
1692 *
1693 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001694 */
Masahisa Kojimaf86352e2021-10-22 20:24:24 +09001695static efi_status_t
1696EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1697 void *table)
Alexander Graf488bf122016-08-19 01:23:24 +02001698{
Rob Clark778e6af2017-09-13 18:05:41 -04001699 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001700 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001701}
1702
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001703/**
Mario Six78a88f72018-07-10 08:40:17 +02001704 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001705 *
1706 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001707 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001708 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001709 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001710 * code is returned.
1711 *
1712 * @device_path: device path of the loaded image
1713 * @file_path: file path of the loaded image
1714 * @handle_ptr: handle of the loaded image
1715 * @info_ptr: loaded image protocol
1716 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001717 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001718efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1719 struct efi_device_path *file_path,
1720 struct efi_loaded_image_obj **handle_ptr,
1721 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001722{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001723 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001724 struct efi_loaded_image *info = NULL;
1725 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001726 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001727
1728 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1729 *handle_ptr = NULL;
1730 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001731
1732 info = calloc(1, sizeof(*info));
1733 if (!info)
1734 return EFI_OUT_OF_RESOURCES;
1735 obj = calloc(1, sizeof(*obj));
1736 if (!obj) {
1737 free(info);
1738 return EFI_OUT_OF_RESOURCES;
1739 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001740 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001741
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001742 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001743 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001744
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001745 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001746 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001747 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001748
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001749 if (device_path) {
1750 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001751
1752 dp = efi_dp_append(device_path, file_path);
1753 if (!dp) {
1754 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001755 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001756 }
1757 } else {
1758 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001759 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001760 ret = efi_add_protocol(&obj->header,
1761 &efi_guid_loaded_image_device_path, dp);
1762 if (ret != EFI_SUCCESS)
1763 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001764
1765 /*
1766 * When asking for the loaded_image interface, just
1767 * return handle which points to loaded_image_info
1768 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001769 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001770 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001771 if (ret != EFI_SUCCESS)
1772 goto failure;
1773
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001774 *info_ptr = info;
1775 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001776
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001777 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001778failure:
1779 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001780 efi_delete_handle(&obj->header);
1781 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001782 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001783}
1784
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001785/**
Heinrich Schuchardt0e9d2d72020-12-04 03:02:03 +01001786 * efi_locate_device_path() - Get the device path and handle of an device
1787 * implementing a protocol
1788 * @protocol: GUID of the protocol
1789 * @device_path: device path
1790 * @device: handle of the device
1791 *
1792 * This function implements the LocateDevicePath service.
1793 *
1794 * See the Unified Extensible Firmware Interface (UEFI) specification for
1795 * details.
1796 *
1797 * Return: status code
1798 */
1799static efi_status_t EFIAPI efi_locate_device_path(
1800 const efi_guid_t *protocol,
1801 struct efi_device_path **device_path,
1802 efi_handle_t *device)
1803{
1804 struct efi_device_path *dp;
1805 size_t i;
1806 struct efi_handler *handler;
1807 efi_handle_t *handles;
1808 size_t len, len_dp;
1809 size_t len_best = 0;
1810 efi_uintn_t no_handles;
1811 u8 *remainder;
1812 efi_status_t ret;
1813
1814 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1815
1816 if (!protocol || !device_path || !*device_path) {
1817 ret = EFI_INVALID_PARAMETER;
1818 goto out;
1819 }
1820
1821 /* Find end of device path */
1822 len = efi_dp_instance_size(*device_path);
1823
1824 /* Get all handles implementing the protocol */
1825 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1826 &no_handles, &handles));
1827 if (ret != EFI_SUCCESS)
1828 goto out;
1829
1830 for (i = 0; i < no_handles; ++i) {
1831 /* Find the device path protocol */
1832 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1833 &handler);
1834 if (ret != EFI_SUCCESS)
1835 continue;
1836 dp = (struct efi_device_path *)handler->protocol_interface;
1837 len_dp = efi_dp_instance_size(dp);
1838 /*
1839 * This handle can only be a better fit
1840 * if its device path length is longer than the best fit and
1841 * if its device path length is shorter of equal the searched
1842 * device path.
1843 */
1844 if (len_dp <= len_best || len_dp > len)
1845 continue;
1846 /* Check if dp is a subpath of device_path */
1847 if (memcmp(*device_path, dp, len_dp))
1848 continue;
1849 if (!device) {
1850 ret = EFI_INVALID_PARAMETER;
1851 goto out;
1852 }
1853 *device = handles[i];
1854 len_best = len_dp;
1855 }
1856 if (len_best) {
1857 remainder = (u8 *)*device_path + len_best;
1858 *device_path = (struct efi_device_path *)remainder;
1859 ret = EFI_SUCCESS;
1860 } else {
1861 ret = EFI_NOT_FOUND;
1862 }
1863out:
1864 return EFI_EXIT(ret);
1865}
1866
1867/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001868 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001869 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001870 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1871 * callers obligation to update the memory type as needed.
1872 *
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01001873 * @file_path: the path of the image to load
1874 * @buffer: buffer containing the loaded image
1875 * @size: size of the loaded image
1876 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001877 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001878static
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001879efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001880 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001881{
Rob Clark838ee4b2017-09-13 18:05:35 -04001882 struct efi_file_handle *f;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001883 efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001884 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001885 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001886
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001887 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001888 f = efi_file_from_path(file_path);
1889 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001890 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001891
Ilias Apalodimasac30aad2021-03-25 21:55:16 +02001892 ret = efi_file_size(f, &bs);
Rob Clark838ee4b2017-09-13 18:05:35 -04001893 if (ret != EFI_SUCCESS)
1894 goto error;
1895
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001896 /*
1897 * When reading the file we do not yet know if it contains an
1898 * application, a boottime driver, or a runtime driver. So here we
1899 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1900 * update the reservation according to the image type.
1901 */
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001902 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1903 EFI_BOOT_SERVICES_DATA,
1904 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001905 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001906 ret = EFI_OUT_OF_RESOURCES;
1907 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001908 }
1909
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001910 /* Read file */
1911 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1912 if (ret != EFI_SUCCESS)
1913 efi_free_pages(addr, efi_size_in_pages(bs));
1914 *buffer = (void *)(uintptr_t)addr;
1915 *size = bs;
1916error:
1917 EFI_CALL(f->close(f));
Rob Clark838ee4b2017-09-13 18:05:35 -04001918 return ret;
1919}
1920
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001921/**
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001922 * efi_load_image_from_path() - load an image using a file path
1923 *
1924 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1925 * callers obligation to update the memory type as needed.
1926 *
1927 * @boot_policy: true for request originating from the boot manager
1928 * @file_path: the path of the image to load
1929 * @buffer: buffer containing the loaded image
1930 * @size: size of the loaded image
1931 * Return: status code
1932 */
1933static
1934efi_status_t efi_load_image_from_path(bool boot_policy,
1935 struct efi_device_path *file_path,
1936 void **buffer, efi_uintn_t *size)
1937{
1938 efi_handle_t device;
1939 efi_status_t ret;
1940 struct efi_device_path *dp;
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001941 struct efi_load_file_protocol *load_file_protocol = NULL;
1942 efi_uintn_t buffer_size;
1943 uint64_t addr, pages;
1944 const efi_guid_t *guid;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001945
1946 /* In case of failure nothing is returned */
1947 *buffer = NULL;
1948 *size = 0;
1949
1950 dp = file_path;
1951 ret = EFI_CALL(efi_locate_device_path(
1952 &efi_simple_file_system_protocol_guid, &dp, &device));
1953 if (ret == EFI_SUCCESS)
1954 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001955
1956 ret = EFI_CALL(efi_locate_device_path(
1957 &efi_guid_load_file_protocol, &dp, &device));
1958 if (ret == EFI_SUCCESS) {
1959 guid = &efi_guid_load_file_protocol;
1960 } else if (!boot_policy) {
1961 guid = &efi_guid_load_file2_protocol;
1962 ret = EFI_CALL(efi_locate_device_path(guid, &dp, &device));
1963 }
1964 if (ret != EFI_SUCCESS)
1965 return EFI_NOT_FOUND;
1966 ret = EFI_CALL(efi_handle_protocol(device, guid,
1967 (void **)&load_file_protocol));
1968 if (ret != EFI_SUCCESS)
1969 return EFI_NOT_FOUND;
1970 buffer_size = 0;
1971 ret = load_file_protocol->load_file(load_file_protocol, dp,
1972 boot_policy, &buffer_size,
1973 NULL);
1974 if (ret != EFI_BUFFER_TOO_SMALL)
1975 goto out;
1976 pages = efi_size_in_pages(buffer_size);
1977 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1978 pages, &addr);
1979 if (ret != EFI_SUCCESS) {
1980 ret = EFI_OUT_OF_RESOURCES;
1981 goto out;
1982 }
1983 ret = EFI_CALL(load_file_protocol->load_file(
1984 load_file_protocol, dp, boot_policy,
1985 &buffer_size, (void *)(uintptr_t)addr));
1986 if (ret != EFI_SUCCESS)
1987 efi_free_pages(addr, pages);
1988out:
Heinrich Schuchardt6e8c28c2021-01-20 22:57:46 +01001989 EFI_CALL(efi_close_protocol(device, guid, efi_root, NULL));
Heinrich Schuchardt3da0b282020-12-06 13:00:15 +01001990 if (ret == EFI_SUCCESS) {
1991 *buffer = (void *)(uintptr_t)addr;
1992 *size = buffer_size;
1993 }
1994
1995 return ret;
Heinrich Schuchardt0e074d12020-12-06 10:47:57 +01001996}
1997
1998/**
Mario Six78a88f72018-07-10 08:40:17 +02001999 * efi_load_image() - load an EFI image into memory
2000 * @boot_policy: true for request originating from the boot manager
2001 * @parent_image: the caller's image handle
2002 * @file_path: the path of the image to load
2003 * @source_buffer: memory location from which the image is installed
2004 * @source_size: size of the memory area from which the image is installed
2005 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002006 *
2007 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02002008 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002009 * See the Unified Extensible Firmware Interface (UEFI) specification
2010 * for details.
2011 *
Mario Six78a88f72018-07-10 08:40:17 +02002012 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002013 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09002014efi_status_t EFIAPI efi_load_image(bool boot_policy,
2015 efi_handle_t parent_image,
2016 struct efi_device_path *file_path,
2017 void *source_buffer,
2018 efi_uintn_t source_size,
2019 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002020{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002021 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04002022 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02002023 struct efi_loaded_image_obj **image_obj =
2024 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002025 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002026 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01002027
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02002028 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01002029 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04002030
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02002031 if (!image_handle || (!source_buffer && !file_path) ||
2032 !efi_search_obj(parent_image) ||
2033 /* The parent image handle must refer to a loaded image */
2034 !parent_image->type) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02002035 ret = EFI_INVALID_PARAMETER;
2036 goto error;
2037 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002038
Rob Clark838ee4b2017-09-13 18:05:35 -04002039 if (!source_buffer) {
Heinrich Schuchardtc06c55b2020-12-04 09:27:41 +01002040 ret = efi_load_image_from_path(boot_policy, file_path,
2041 &dest_buffer, &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002042 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002043 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04002044 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002045 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04002046 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00002047 /* split file_path which contains both the device and file parts */
2048 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01002049 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002050 if (ret == EFI_SUCCESS)
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002051 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002052 if (!source_buffer)
2053 /* Release buffer to which file was loaded */
2054 efi_free_pages((uintptr_t)dest_buffer,
2055 efi_size_in_pages(source_size));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002056 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01002057 info->system_table = &systab;
2058 info->parent_handle = parent_image;
2059 } else {
2060 /* The image is invalid. Release all associated resources. */
2061 efi_delete_handle(*image_handle);
2062 *image_handle = NULL;
2063 free(info);
2064 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01002065error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01002066 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002067}
2068
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002069/**
Alexander Graff31239a2018-11-15 21:23:47 +01002070 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2071 */
2072static void efi_exit_caches(void)
2073{
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002074#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graff31239a2018-11-15 21:23:47 +01002075 /*
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002076 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2077 * caches are enabled.
2078 *
2079 * TODO:
2080 * According to the UEFI spec caches that can be managed via CP15
2081 * operations should be enabled. Caches requiring platform information
2082 * to manage should be disabled. This should not happen in
2083 * ExitBootServices() but before invoking any UEFI binary is invoked.
2084 *
2085 * We want to keep the current workaround while GRUB prior to version
2086 * 2.04 is still in use.
Alexander Graff31239a2018-11-15 21:23:47 +01002087 */
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02002088 cleanup_before_linux();
Alexander Graff31239a2018-11-15 21:23:47 +01002089#endif
2090}
2091
2092/**
Mario Six78a88f72018-07-10 08:40:17 +02002093 * efi_exit_boot_services() - stop all boot services
2094 * @image_handle: handle of the loaded image
2095 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002096 *
2097 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02002098 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002099 * See the Unified Extensible Firmware Interface (UEFI) specification
2100 * for details.
2101 *
Mario Six78a88f72018-07-10 08:40:17 +02002102 * All timer events are disabled. For exit boot services events the
2103 * notification function is called. The boot services are disabled in the
2104 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002105 *
Mario Six78a88f72018-07-10 08:40:17 +02002106 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002107 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002108static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002109 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01002110{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002111 struct efi_event *evt, *next_event;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002112 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002113
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02002114 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01002115
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002116 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002117 if (map_key != efi_memory_map_key) {
2118 ret = EFI_INVALID_PARAMETER;
2119 goto out;
2120 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02002121
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002122 /* Check if ExitBootServices has already been called */
2123 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002124 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002125
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002126 /* Stop all timer related activities */
2127 timers_enabled = false;
2128
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002129 /* Add related events to the event group */
2130 list_for_each_entry(evt, &efi_events, link) {
2131 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2132 evt->group = &efi_guid_event_group_exit_boot_services;
2133 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002134 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01002135 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002136 if (evt->group &&
2137 !guidcmp(evt->group,
2138 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002139 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01002140 break;
2141 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002142 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02002143
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02002144 /* Make sure that notification functions are not called anymore */
2145 efi_tpl = TPL_HIGH_LEVEL;
2146
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02002147 /* Notify variable services */
2148 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04002149
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02002150 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2151 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2152 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2153 list_del(&evt->link);
2154 }
2155
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002156 if (!efi_st_keep_devices) {
Heinrich Schuchardtdb6288d2020-12-27 15:33:09 +01002157 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002158 udc_disconnect();
2159 board_quiesce_devices();
2160 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2161 }
Alexander Grafb7b84102016-11-17 01:02:57 +01002162
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02002163 /* Patch out unsupported runtime function */
2164 efi_runtime_detach();
2165
Alexander Graff31239a2018-11-15 21:23:47 +01002166 /* Fix up caches for EFI payloads if necessary */
2167 efi_exit_caches();
2168
Alexander Grafbee91162016-03-04 01:09:59 +01002169 /* This stops all lingering devices */
2170 bootm_disable_interrupts();
2171
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002172 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002173 systab.con_in_handle = NULL;
2174 systab.con_in = NULL;
2175 systab.con_out_handle = NULL;
2176 systab.con_out = NULL;
2177 systab.stderr_handle = NULL;
2178 systab.std_err = NULL;
2179 systab.boottime = NULL;
2180
2181 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02002182 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002183
Alexander Grafbee91162016-03-04 01:09:59 +01002184 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002185 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01002186 WATCHDOG_RESET();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002187out:
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002188 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2189 if (ret != EFI_SUCCESS)
2190 efi_tcg2_notify_exit_boot_services_failed();
2191 }
2192
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002193 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002194}
2195
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002196/**
Mario Six78a88f72018-07-10 08:40:17 +02002197 * efi_get_next_monotonic_count() - get next value of the counter
2198 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002199 *
2200 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002201 *
Mario Six78a88f72018-07-10 08:40:17 +02002202 * See the Unified Extensible Firmware Interface (UEFI) specification for
2203 * details.
2204 *
2205 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002206 */
Alexander Grafbee91162016-03-04 01:09:59 +01002207static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2208{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002209 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002210 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002211
Alexander Grafbee91162016-03-04 01:09:59 +01002212 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002213 if (!count) {
2214 ret = EFI_INVALID_PARAMETER;
2215 goto out;
2216 }
Alexander Grafbee91162016-03-04 01:09:59 +01002217 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002218 ret = EFI_SUCCESS;
2219out:
2220 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002221}
2222
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002223/**
Mario Six78a88f72018-07-10 08:40:17 +02002224 * efi_stall() - sleep
2225 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002226 *
Mario Six78a88f72018-07-10 08:40:17 +02002227 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002228 *
Mario Six78a88f72018-07-10 08:40:17 +02002229 * See the Unified Extensible Firmware Interface (UEFI) specification for
2230 * details.
2231 *
2232 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002233 */
Alexander Grafbee91162016-03-04 01:09:59 +01002234static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2235{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002236 u64 end_tick;
2237
Alexander Grafbee91162016-03-04 01:09:59 +01002238 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002239
2240 end_tick = get_ticks() + usec_to_tick(microseconds);
2241 while (get_ticks() < end_tick)
2242 efi_timer_check();
2243
Alexander Grafbee91162016-03-04 01:09:59 +01002244 return EFI_EXIT(EFI_SUCCESS);
2245}
2246
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002247/**
Mario Six78a88f72018-07-10 08:40:17 +02002248 * efi_set_watchdog_timer() - reset the watchdog timer
2249 * @timeout: seconds before reset by watchdog
2250 * @watchdog_code: code to be logged when resetting
2251 * @data_size: size of buffer in bytes
2252 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002253 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002254 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002255 *
Mario Six78a88f72018-07-10 08:40:17 +02002256 * See the Unified Extensible Firmware Interface (UEFI) specification for
2257 * details.
2258 *
2259 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002260 */
Alexander Grafbee91162016-03-04 01:09:59 +01002261static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2262 uint64_t watchdog_code,
2263 unsigned long data_size,
2264 uint16_t *watchdog_data)
2265{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002266 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002267 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002268 return EFI_EXIT(efi_set_watchdog(timeout));
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_close_protocol() - close a protocol
2273 * @handle: handle on which the protocol shall be closed
2274 * @protocol: GUID of the protocol to close
2275 * @agent_handle: handle of the driver
2276 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002277 *
2278 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002279 *
Mario Six78a88f72018-07-10 08:40:17 +02002280 * See the Unified Extensible Firmware Interface (UEFI) specification for
2281 * details.
2282 *
2283 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002284 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002285efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2286 const efi_guid_t *protocol,
2287 efi_handle_t agent_handle,
2288 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002289{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002290 struct efi_handler *handler;
2291 struct efi_open_protocol_info_item *item;
2292 struct efi_open_protocol_info_item *pos;
2293 efi_status_t r;
2294
Rob Clark778e6af2017-09-13 18:05:41 -04002295 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002296 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002297
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02002298 if (!efi_search_obj(agent_handle) ||
2299 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002300 r = EFI_INVALID_PARAMETER;
2301 goto out;
2302 }
2303 r = efi_search_protocol(handle, protocol, &handler);
2304 if (r != EFI_SUCCESS)
2305 goto out;
2306
2307 r = EFI_NOT_FOUND;
2308 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2309 if (item->info.agent_handle == agent_handle &&
2310 item->info.controller_handle == controller_handle) {
2311 efi_delete_open_info(item);
2312 r = EFI_SUCCESS;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002313 }
2314 }
2315out:
2316 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002317}
2318
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002319/**
Mario Six78a88f72018-07-10 08:40:17 +02002320 * efi_open_protocol_information() - provide information about then open status
2321 * of a protocol on a handle
2322 * @handle: handle for which the information shall be retrieved
2323 * @protocol: GUID of the protocol
2324 * @entry_buffer: buffer to receive the open protocol information
2325 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002326 *
2327 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002328 *
Mario Six78a88f72018-07-10 08:40:17 +02002329 * See the Unified Extensible Firmware Interface (UEFI) specification for
2330 * details.
2331 *
2332 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002333 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002334static efi_status_t EFIAPI efi_open_protocol_information(
2335 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002336 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002337 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002338{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002339 unsigned long buffer_size;
2340 unsigned long count;
2341 struct efi_handler *handler;
2342 struct efi_open_protocol_info_item *item;
2343 efi_status_t r;
2344
Rob Clark778e6af2017-09-13 18:05:41 -04002345 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002346 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002347
2348 /* Check parameters */
2349 if (!entry_buffer) {
2350 r = EFI_INVALID_PARAMETER;
2351 goto out;
2352 }
2353 r = efi_search_protocol(handle, protocol, &handler);
2354 if (r != EFI_SUCCESS)
2355 goto out;
2356
2357 /* Count entries */
2358 count = 0;
2359 list_for_each_entry(item, &handler->open_infos, link) {
2360 if (item->info.open_count)
2361 ++count;
2362 }
2363 *entry_count = count;
2364 *entry_buffer = NULL;
2365 if (!count) {
2366 r = EFI_SUCCESS;
2367 goto out;
2368 }
2369
2370 /* Copy entries */
2371 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002372 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002373 (void **)entry_buffer);
2374 if (r != EFI_SUCCESS)
2375 goto out;
2376 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2377 if (item->info.open_count)
2378 (*entry_buffer)[--count] = item->info;
2379 }
2380out:
2381 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002382}
2383
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002384/**
Mario Six78a88f72018-07-10 08:40:17 +02002385 * efi_protocols_per_handle() - get protocols installed on a handle
2386 * @handle: handle for which the information is retrieved
2387 * @protocol_buffer: buffer with protocol GUIDs
2388 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002389 *
2390 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002391 *
Mario Six78a88f72018-07-10 08:40:17 +02002392 * See the Unified Extensible Firmware Interface (UEFI) specification for
2393 * details.
2394 *
2395 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002396 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002397static efi_status_t EFIAPI efi_protocols_per_handle(
2398 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002399 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002400{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002401 unsigned long buffer_size;
2402 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002403 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002404 efi_status_t r;
2405
Alexander Grafbee91162016-03-04 01:09:59 +01002406 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2407 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002408
2409 if (!handle || !protocol_buffer || !protocol_buffer_count)
2410 return EFI_EXIT(EFI_INVALID_PARAMETER);
2411
2412 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002413 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002414
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002415 efiobj = efi_search_obj(handle);
2416 if (!efiobj)
2417 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002418
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002419 /* Count protocols */
2420 list_for_each(protocol_handle, &efiobj->protocols) {
2421 ++*protocol_buffer_count;
2422 }
2423
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002424 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002425 if (*protocol_buffer_count) {
2426 size_t j = 0;
2427
2428 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002429 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002430 (void **)protocol_buffer);
2431 if (r != EFI_SUCCESS)
2432 return EFI_EXIT(r);
2433 list_for_each(protocol_handle, &efiobj->protocols) {
2434 struct efi_handler *protocol;
2435
2436 protocol = list_entry(protocol_handle,
2437 struct efi_handler, link);
2438 (*protocol_buffer)[j] = (void *)protocol->guid;
2439 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002440 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002441 }
2442
2443 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002444}
2445
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002446/**
Mario Six78a88f72018-07-10 08:40:17 +02002447 * efi_locate_handle_buffer() - locate handles implementing a protocol
2448 * @search_type: selection criterion
2449 * @protocol: GUID of the protocol
2450 * @search_key: registration key
2451 * @no_handles: number of returned handles
2452 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002453 *
2454 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002455 *
Mario Six78a88f72018-07-10 08:40:17 +02002456 * See the Unified Extensible Firmware Interface (UEFI) specification for
2457 * details.
2458 *
2459 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002460 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002461efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafbee91162016-03-04 01:09:59 +01002462 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002463 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002464 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002465{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002466 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002467 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002468
Rob Clark778e6af2017-09-13 18:05:41 -04002469 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002470 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002471
2472 if (!no_handles || !buffer) {
2473 r = EFI_INVALID_PARAMETER;
2474 goto out;
2475 }
2476 *no_handles = 0;
2477 *buffer = NULL;
2478 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2479 *buffer);
2480 if (r != EFI_BUFFER_TOO_SMALL)
2481 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002482 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002483 (void **)buffer);
2484 if (r != EFI_SUCCESS)
2485 goto out;
2486 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2487 *buffer);
2488 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002489 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002490out:
2491 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002492}
2493
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002494/**
Mario Six78a88f72018-07-10 08:40:17 +02002495 * efi_locate_protocol() - find an interface implementing a protocol
2496 * @protocol: GUID of the protocol
2497 * @registration: registration key passed to the notification function
2498 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002499 *
2500 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002501 *
Mario Six78a88f72018-07-10 08:40:17 +02002502 * See the Unified Extensible Firmware Interface (UEFI) specification for
2503 * details.
2504 *
2505 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002506 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002507static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002508 void *registration,
2509 void **protocol_interface)
2510{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002511 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002512 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002513 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002514
Rob Clark778e6af2017-09-13 18:05:41 -04002515 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002516
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002517 /*
2518 * The UEFI spec explicitly requires a protocol even if a registration
2519 * key is provided. This differs from the logic in LocateHandle().
2520 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002521 if (!protocol || !protocol_interface)
2522 return EFI_EXIT(EFI_INVALID_PARAMETER);
2523
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002524 if (registration) {
2525 struct efi_register_notify_event *event;
2526 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002527
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002528 event = efi_check_register_notify_event(registration);
2529 if (!event)
2530 return EFI_EXIT(EFI_INVALID_PARAMETER);
2531 /*
2532 * The UEFI spec requires to return EFI_NOT_FOUND if no
2533 * protocol instance matches protocol and registration.
2534 * So let's do the same for a mismatch between protocol and
2535 * registration.
2536 */
2537 if (guidcmp(&event->protocol, protocol))
2538 goto not_found;
2539 if (list_empty(&event->handles))
2540 goto not_found;
2541 handle = list_first_entry(&event->handles,
2542 struct efi_protocol_notification,
2543 link);
2544 efiobj = handle->handle;
2545 list_del(&handle->link);
2546 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002547 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002548 if (ret == EFI_SUCCESS)
2549 goto found;
2550 } else {
2551 list_for_each_entry(efiobj, &efi_obj_list, link) {
2552 ret = efi_search_protocol(efiobj, protocol, &handler);
2553 if (ret == EFI_SUCCESS)
2554 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002555 }
2556 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002557not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002558 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002559 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002560found:
2561 *protocol_interface = handler->protocol_interface;
2562 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002563}
2564
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002565/**
Mario Six78a88f72018-07-10 08:40:17 +02002566 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2567 * interfaces
2568 * @handle: handle on which the protocol interfaces shall be installed
2569 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2570 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002571 *
2572 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002573 *
Mario Six78a88f72018-07-10 08:40:17 +02002574 * See the Unified Extensible Firmware Interface (UEFI) specification for
2575 * details.
2576 *
2577 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002578 */
Heinrich Schuchardt7657ae12019-04-12 06:59:49 +02002579efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002580 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002581{
2582 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002583
Alexander Grafbeb077a2018-06-18 17:23:05 +02002584 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002585 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002586 void *protocol_interface;
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002587 efi_handle_t old_handle;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002588 efi_status_t r = EFI_SUCCESS;
2589 int i = 0;
2590
2591 if (!handle)
2592 return EFI_EXIT(EFI_INVALID_PARAMETER);
2593
Alexander Grafbeb077a2018-06-18 17:23:05 +02002594 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002595 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002596 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002597 if (!protocol)
2598 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002599 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002600 /* Check that a device path has not been installed before */
2601 if (!guidcmp(protocol, &efi_guid_device_path)) {
2602 struct efi_device_path *dp = protocol_interface;
2603
2604 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2605 &old_handle));
Heinrich Schuchardt20562892019-05-20 19:55:18 +00002606 if (r == EFI_SUCCESS &&
2607 dp->type == DEVICE_PATH_TYPE_END) {
2608 EFI_PRINT("Path %pD already installed\n",
2609 protocol_interface);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002610 r = EFI_ALREADY_STARTED;
2611 break;
2612 }
2613 }
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002614 r = EFI_CALL(efi_install_protocol_interface(
2615 handle, protocol,
2616 EFI_NATIVE_INTERFACE,
2617 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002618 if (r != EFI_SUCCESS)
2619 break;
2620 i++;
2621 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002622 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002623 if (r == EFI_SUCCESS)
2624 return EFI_EXIT(r);
2625
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002626 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002627 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002628 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002629 protocol = efi_va_arg(argptr, efi_guid_t*);
2630 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002631 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002632 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002633 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002634 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002635
2636 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002637}
2638
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002639/**
Mario Six78a88f72018-07-10 08:40:17 +02002640 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2641 * interfaces
2642 * @handle: handle from which the protocol interfaces shall be removed
2643 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2644 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002645 *
2646 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002647 *
Mario Six78a88f72018-07-10 08:40:17 +02002648 * See the Unified Extensible Firmware Interface (UEFI) specification for
2649 * details.
2650 *
2651 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002652 */
Alexander Grafbee91162016-03-04 01:09:59 +01002653static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002654 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002655{
2656 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002657
Alexander Grafbeb077a2018-06-18 17:23:05 +02002658 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002659 const efi_guid_t *protocol;
2660 void *protocol_interface;
2661 efi_status_t r = EFI_SUCCESS;
2662 size_t i = 0;
2663
2664 if (!handle)
2665 return EFI_EXIT(EFI_INVALID_PARAMETER);
2666
Alexander Grafbeb077a2018-06-18 17:23:05 +02002667 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002668 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002669 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002670 if (!protocol)
2671 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002672 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002673 r = efi_uninstall_protocol(handle, protocol,
2674 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002675 if (r != EFI_SUCCESS)
2676 break;
2677 i++;
2678 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002679 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002680 if (r == EFI_SUCCESS) {
2681 /* If the last protocol has been removed, delete the handle. */
2682 if (list_empty(&handle->protocols)) {
2683 list_del(&handle->link);
2684 free(handle);
2685 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002686 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002687 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002688
2689 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002690 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002691 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002692 protocol = efi_va_arg(argptr, efi_guid_t*);
2693 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002694 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2695 EFI_NATIVE_INTERFACE,
2696 protocol_interface));
2697 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002698 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002699
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002700 /* In case of an error always return EFI_INVALID_PARAMETER */
2701 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002702}
2703
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002704/**
Mario Six78a88f72018-07-10 08:40:17 +02002705 * efi_calculate_crc32() - calculate cyclic redundancy code
2706 * @data: buffer with data
2707 * @data_size: size of buffer in bytes
2708 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002709 *
2710 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002711 *
Mario Six78a88f72018-07-10 08:40:17 +02002712 * See the Unified Extensible Firmware Interface (UEFI) specification for
2713 * details.
2714 *
2715 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002716 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002717static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2718 efi_uintn_t data_size,
2719 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002720{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002721 efi_status_t ret = EFI_SUCCESS;
2722
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002723 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002724 if (!data || !data_size || !crc32_p) {
2725 ret = EFI_INVALID_PARAMETER;
2726 goto out;
2727 }
Alexander Grafbee91162016-03-04 01:09:59 +01002728 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002729out:
2730 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002731}
2732
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002733/**
Mario Six78a88f72018-07-10 08:40:17 +02002734 * efi_copy_mem() - copy memory
2735 * @destination: destination of the copy operation
2736 * @source: source of the copy operation
2737 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002738 *
2739 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002740 *
Mario Six78a88f72018-07-10 08:40:17 +02002741 * See the Unified Extensible Firmware Interface (UEFI) specification for
2742 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002743 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002744static void EFIAPI efi_copy_mem(void *destination, const void *source,
2745 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002746{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002747 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002748 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002749 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002750}
2751
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002752/**
Mario Six78a88f72018-07-10 08:40:17 +02002753 * efi_set_mem() - Fill memory with a byte value.
2754 * @buffer: buffer to fill
2755 * @size: size of buffer in bytes
2756 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002757 *
2758 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002759 *
Mario Six78a88f72018-07-10 08:40:17 +02002760 * See the Unified Extensible Firmware Interface (UEFI) specification for
2761 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002762 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002763static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002764{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002765 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002766 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002767 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002768}
2769
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002770/**
Mario Six78a88f72018-07-10 08:40:17 +02002771 * efi_protocol_open() - open protocol interface on a handle
2772 * @handler: handler of a protocol
2773 * @protocol_interface: interface implementing the protocol
2774 * @agent_handle: handle of the driver
2775 * @controller_handle: handle of the controller
2776 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002777 *
Mario Six78a88f72018-07-10 08:40:17 +02002778 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002779 */
Heinrich Schuchardtf9ad2402020-01-10 12:33:59 +01002780efi_status_t efi_protocol_open(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002781 struct efi_handler *handler,
2782 void **protocol_interface, void *agent_handle,
2783 void *controller_handle, uint32_t attributes)
2784{
2785 struct efi_open_protocol_info_item *item;
2786 struct efi_open_protocol_info_entry *match = NULL;
2787 bool opened_by_driver = false;
2788 bool opened_exclusive = false;
2789
2790 /* If there is no agent, only return the interface */
2791 if (!agent_handle)
2792 goto out;
2793
2794 /* For TEST_PROTOCOL ignore interface attribute */
2795 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2796 *protocol_interface = NULL;
2797
2798 /*
2799 * Check if the protocol is already opened by a driver with the same
2800 * attributes or opened exclusively
2801 */
2802 list_for_each_entry(item, &handler->open_infos, link) {
2803 if (item->info.agent_handle == agent_handle) {
2804 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2805 (item->info.attributes == attributes))
2806 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002807 } else {
2808 if (item->info.attributes &
2809 EFI_OPEN_PROTOCOL_BY_DRIVER)
2810 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002811 }
2812 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2813 opened_exclusive = true;
2814 }
2815
2816 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002817 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2818 if (opened_exclusive)
2819 return EFI_ACCESS_DENIED;
2820 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2821 if (opened_exclusive || opened_by_driver)
2822 return EFI_ACCESS_DENIED;
2823 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002824
2825 /* Prepare exclusive opening */
2826 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2827 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002828disconnect_next:
2829 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002830 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002831 efi_status_t ret;
2832
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002833 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002834 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2835 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002836 item->info.controller_handle,
2837 item->info.agent_handle,
2838 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002839 if (ret == EFI_SUCCESS)
2840 /*
2841 * Child controllers may have been
2842 * removed from the open_infos list. So
2843 * let's restart the loop.
2844 */
2845 goto disconnect_next;
2846 else
2847 opened_by_driver = true;
2848 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002849 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002850 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002851 if (opened_by_driver)
2852 return EFI_ACCESS_DENIED;
2853 }
2854
2855 /* Find existing entry */
2856 list_for_each_entry(item, &handler->open_infos, link) {
2857 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02002858 item->info.controller_handle == controller_handle &&
2859 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002860 match = &item->info;
2861 }
2862 /* None found, create one */
2863 if (!match) {
2864 match = efi_create_open_info(handler);
2865 if (!match)
2866 return EFI_OUT_OF_RESOURCES;
2867 }
2868
2869 match->agent_handle = agent_handle;
2870 match->controller_handle = controller_handle;
2871 match->attributes = attributes;
2872 match->open_count++;
2873
2874out:
2875 /* For TEST_PROTOCOL ignore interface attribute. */
2876 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2877 *protocol_interface = handler->protocol_interface;
2878
2879 return EFI_SUCCESS;
2880}
2881
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002882/**
Mario Six78a88f72018-07-10 08:40:17 +02002883 * efi_open_protocol() - open protocol interface on a handle
2884 * @handle: handle on which the protocol shall be opened
2885 * @protocol: GUID of the protocol
2886 * @protocol_interface: interface implementing the protocol
2887 * @agent_handle: handle of the driver
2888 * @controller_handle: handle of the controller
2889 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002890 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002891 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002892 *
Mario Six78a88f72018-07-10 08:40:17 +02002893 * See the Unified Extensible Firmware Interface (UEFI) specification for
2894 * details.
2895 *
2896 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002897 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002898static efi_status_t EFIAPI efi_open_protocol
2899 (efi_handle_t handle, const efi_guid_t *protocol,
2900 void **protocol_interface, efi_handle_t agent_handle,
2901 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002902{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002903 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002904 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002905
Rob Clark778e6af2017-09-13 18:05:41 -04002906 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002907 protocol_interface, agent_handle, controller_handle,
2908 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002909
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002910 if (!handle || !protocol ||
2911 (!protocol_interface && attributes !=
2912 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2913 goto out;
2914 }
2915
2916 switch (attributes) {
2917 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2918 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2919 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2920 break;
2921 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2922 if (controller_handle == handle)
2923 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002924 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002925 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2926 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002927 /* Check that the controller handle is valid */
2928 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002929 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002930 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002931 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002932 /* Check that the agent handle is valid */
2933 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002934 goto out;
2935 break;
2936 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002937 goto out;
2938 }
2939
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002940 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002941 switch (r) {
2942 case EFI_SUCCESS:
2943 break;
2944 case EFI_NOT_FOUND:
2945 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002946 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002947 default:
2948 goto out;
2949 }
Alexander Grafbee91162016-03-04 01:09:59 +01002950
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002951 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2952 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002953out:
2954 return EFI_EXIT(r);
2955}
2956
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002957/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002958 * efi_start_image() - call the entry point of an image
2959 * @image_handle: handle of the image
2960 * @exit_data_size: size of the buffer
2961 * @exit_data: buffer to receive the exit data of the called image
2962 *
2963 * This function implements the StartImage service.
2964 *
2965 * See the Unified Extensible Firmware Interface (UEFI) specification for
2966 * details.
2967 *
2968 * Return: status code
2969 */
2970efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2971 efi_uintn_t *exit_data_size,
2972 u16 **exit_data)
2973{
2974 struct efi_loaded_image_obj *image_obj =
2975 (struct efi_loaded_image_obj *)image_handle;
2976 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002977 void *info;
2978 efi_handle_t parent_image = current_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01002979 efi_status_t exit_status;
2980 struct jmp_buf_data exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002981
2982 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2983
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002984 if (!efi_search_obj(image_handle))
2985 return EFI_EXIT(EFI_INVALID_PARAMETER);
2986
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002987 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02002988 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2989 return EFI_EXIT(EFI_INVALID_PARAMETER);
2990
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002991 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2992 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2993
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002994 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2995 &info, NULL, NULL,
2996 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2997 if (ret != EFI_SUCCESS)
2998 return EFI_EXIT(EFI_INVALID_PARAMETER);
2999
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003000 image_obj->exit_data_size = exit_data_size;
3001 image_obj->exit_data = exit_data;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003002 image_obj->exit_status = &exit_status;
3003 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003004
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003005 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3006 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09003007 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003008 if (ret != EFI_SUCCESS) {
3009 log_warning("tcg2 measurement fails(0x%lx)\n",
3010 ret);
3011 }
3012 }
3013 }
3014
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003015 /* call the image! */
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003016 if (setjmp(&exit_jmp)) {
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003017 /*
3018 * We called the entry point of the child image with EFI_CALL
3019 * in the lines below. The child image called the Exit() boot
3020 * service efi_exit() which executed the long jump that brought
3021 * us to the current line. This implies that the second half
3022 * of the EFI_CALL macro has not been executed.
3023 */
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +02003024#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003025 /*
3026 * efi_exit() called efi_restore_gd(). We have to undo this
3027 * otherwise __efi_entry_check() will put the wrong value into
3028 * app_gd.
3029 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +02003030 set_gd(app_gd);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003031#endif
3032 /*
3033 * To get ready to call EFI_EXIT below we have to execute the
3034 * missed out steps of EFI_CALL.
3035 */
3036 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003037 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003038 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02003039 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003040 current_image = parent_image;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003041 return EFI_EXIT(exit_status);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003042 }
3043
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003044 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02003045 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09003046 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003047 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3048
3049 /*
Heinrich Schuchardt55111c52020-01-10 22:06:54 +01003050 * Control is returned from a started UEFI image either by calling
3051 * Exit() (where exit data can be provided) or by simply returning from
3052 * the entry point. In the latter case call Exit() on behalf of the
3053 * image.
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003054 */
3055 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3056}
3057
3058/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003059 * efi_delete_image() - delete loaded image from memory)
3060 *
3061 * @image_obj: handle of the loaded image
3062 * @loaded_image_protocol: loaded image protocol
3063 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003064static efi_status_t efi_delete_image
3065 (struct efi_loaded_image_obj *image_obj,
3066 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003067{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003068 struct efi_object *efiobj;
3069 efi_status_t r, ret = EFI_SUCCESS;
3070
3071close_next:
3072 list_for_each_entry(efiobj, &efi_obj_list, link) {
3073 struct efi_handler *protocol;
3074
3075 list_for_each_entry(protocol, &efiobj->protocols, link) {
3076 struct efi_open_protocol_info_item *info;
3077
3078 list_for_each_entry(info, &protocol->open_infos, link) {
3079 if (info->info.agent_handle !=
3080 (efi_handle_t)image_obj)
3081 continue;
3082 r = EFI_CALL(efi_close_protocol
3083 (efiobj, protocol->guid,
3084 info->info.agent_handle,
3085 info->info.controller_handle
3086 ));
3087 if (r != EFI_SUCCESS)
3088 ret = r;
3089 /*
3090 * Closing protocols may results in further
3091 * items being deleted. To play it safe loop
3092 * over all elements again.
3093 */
3094 goto close_next;
3095 }
3096 }
3097 }
3098
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003099 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3100 efi_size_in_pages(loaded_image_protocol->image_size));
3101 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003102
3103 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003104}
3105
3106/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003107 * efi_unload_image() - unload an EFI image
3108 * @image_handle: handle of the image to be unloaded
3109 *
3110 * This function implements the UnloadImage service.
3111 *
3112 * See the Unified Extensible Firmware Interface (UEFI) specification for
3113 * details.
3114 *
3115 * Return: status code
3116 */
3117efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3118{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003119 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003120 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003121 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003122
3123 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003124
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003125 efiobj = efi_search_obj(image_handle);
3126 if (!efiobj) {
3127 ret = EFI_INVALID_PARAMETER;
3128 goto out;
3129 }
3130 /* Find the loaded image protocol */
3131 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3132 (void **)&loaded_image_protocol,
3133 NULL, NULL,
3134 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3135 if (ret != EFI_SUCCESS) {
3136 ret = EFI_INVALID_PARAMETER;
3137 goto out;
3138 }
3139 switch (efiobj->type) {
3140 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3141 /* Call the unload function */
3142 if (!loaded_image_protocol->unload) {
3143 ret = EFI_UNSUPPORTED;
3144 goto out;
3145 }
3146 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3147 if (ret != EFI_SUCCESS)
3148 goto out;
3149 break;
3150 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3151 break;
3152 default:
3153 ret = EFI_INVALID_PARAMETER;
3154 goto out;
3155 }
3156 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3157 loaded_image_protocol);
3158out:
3159 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003160}
3161
3162/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003163 * efi_update_exit_data() - fill exit data parameters of StartImage()
3164 *
Heinrich Schuchardt9ce91272019-07-14 11:25:06 +02003165 * @image_obj: image handle
3166 * @exit_data_size: size of the exit data buffer
3167 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003168 * Return: status code
3169 */
3170static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3171 efi_uintn_t exit_data_size,
3172 u16 *exit_data)
3173{
3174 efi_status_t ret;
3175
3176 /*
3177 * If exit_data is not provided to StartImage(), exit_data_size must be
3178 * ignored.
3179 */
3180 if (!image_obj->exit_data)
3181 return EFI_SUCCESS;
3182 if (image_obj->exit_data_size)
3183 *image_obj->exit_data_size = exit_data_size;
3184 if (exit_data_size && exit_data) {
3185 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3186 exit_data_size,
3187 (void **)image_obj->exit_data);
3188 if (ret != EFI_SUCCESS)
3189 return ret;
3190 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3191 } else {
3192 image_obj->exit_data = NULL;
3193 }
3194 return EFI_SUCCESS;
3195}
3196
3197/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003198 * efi_exit() - leave an EFI application or driver
3199 * @image_handle: handle of the application or driver that is exiting
3200 * @exit_status: status code
3201 * @exit_data_size: size of the buffer in bytes
3202 * @exit_data: buffer with data describing an error
3203 *
3204 * This function implements the Exit service.
3205 *
3206 * See the Unified Extensible Firmware Interface (UEFI) specification for
3207 * details.
3208 *
3209 * Return: status code
3210 */
3211static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3212 efi_status_t exit_status,
3213 efi_uintn_t exit_data_size,
3214 u16 *exit_data)
3215{
3216 /*
3217 * TODO: We should call the unload procedure of the loaded
3218 * image protocol.
3219 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003220 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003221 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003222 struct efi_loaded_image_obj *image_obj =
3223 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003224 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003225
3226 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3227 exit_data_size, exit_data);
3228
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003229 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003230 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003231 (void **)&loaded_image_protocol,
3232 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003233 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003234 if (ret != EFI_SUCCESS) {
3235 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003236 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003237 }
3238
3239 /* Unloading of unstarted images */
3240 switch (image_obj->header.type) {
3241 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3242 break;
3243 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3244 efi_delete_image(image_obj, loaded_image_protocol);
3245 ret = EFI_SUCCESS;
3246 goto out;
3247 default:
3248 /* Handle does not refer to loaded image */
3249 ret = EFI_INVALID_PARAMETER;
3250 goto out;
3251 }
3252 /* A started image can only be unloaded it is the last one started. */
3253 if (image_handle != current_image) {
3254 ret = EFI_INVALID_PARAMETER;
3255 goto out;
3256 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003257
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003258 /* Exit data is only foreseen in case of failure. */
3259 if (exit_status != EFI_SUCCESS) {
3260 ret = efi_update_exit_data(image_obj, exit_data_size,
3261 exit_data);
3262 /* Exiting has priority. Don't return error to caller. */
3263 if (ret != EFI_SUCCESS)
3264 EFI_PRINT("%s: out of memory\n", __func__);
3265 }
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003266 /* efi_delete_image() frees image_obj. Copy before the call. */
3267 exit_jmp = image_obj->exit_jmp;
3268 *image_obj->exit_status = exit_status;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003269 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3270 exit_status != EFI_SUCCESS)
3271 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003272
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09003273 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3274 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3275 ret = efi_tcg2_measure_efi_app_exit();
3276 if (ret != EFI_SUCCESS) {
3277 log_warning("tcg2 measurement fails(0x%lx)\n",
3278 ret);
3279 }
3280 }
3281 }
3282
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003283 /* Make sure entry/exit counts for EFI world cross-overs match */
3284 EFI_EXIT(exit_status);
3285
3286 /*
3287 * But longjmp out with the U-Boot gd, not the application's, as
3288 * the other end is a setjmp call inside EFI context.
3289 */
3290 efi_restore_gd();
3291
Heinrich Schuchardtf8212f02020-12-28 23:24:40 +01003292 longjmp(exit_jmp, 1);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003293
3294 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003295out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003296 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003297}
3298
3299/**
Mario Six78a88f72018-07-10 08:40:17 +02003300 * efi_handle_protocol() - get interface of a protocol on a handle
3301 * @handle: handle on which the protocol shall be opened
3302 * @protocol: GUID of the protocol
3303 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003304 *
3305 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003306 *
Mario Six78a88f72018-07-10 08:40:17 +02003307 * See the Unified Extensible Firmware Interface (UEFI) specification for
3308 * details.
3309 *
3310 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003311 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09003312efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3313 const efi_guid_t *protocol,
3314 void **protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01003315{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003316 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003317 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003318}
3319
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003320/**
Mario Six78a88f72018-07-10 08:40:17 +02003321 * efi_bind_controller() - bind a single driver to a controller
3322 * @controller_handle: controller handle
3323 * @driver_image_handle: driver handle
3324 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003325 *
Mario Six78a88f72018-07-10 08:40:17 +02003326 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003327 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003328static efi_status_t efi_bind_controller(
3329 efi_handle_t controller_handle,
3330 efi_handle_t driver_image_handle,
3331 struct efi_device_path *remain_device_path)
3332{
3333 struct efi_driver_binding_protocol *binding_protocol;
3334 efi_status_t r;
3335
3336 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3337 &efi_guid_driver_binding_protocol,
3338 (void **)&binding_protocol,
3339 driver_image_handle, NULL,
3340 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3341 if (r != EFI_SUCCESS)
3342 return r;
3343 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3344 controller_handle,
3345 remain_device_path));
3346 if (r == EFI_SUCCESS)
3347 r = EFI_CALL(binding_protocol->start(binding_protocol,
3348 controller_handle,
3349 remain_device_path));
3350 EFI_CALL(efi_close_protocol(driver_image_handle,
3351 &efi_guid_driver_binding_protocol,
3352 driver_image_handle, NULL));
3353 return r;
3354}
3355
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003356/**
Mario Six78a88f72018-07-10 08:40:17 +02003357 * efi_connect_single_controller() - connect a single driver to a controller
3358 * @controller_handle: controller
3359 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003360 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003361 *
Mario Six78a88f72018-07-10 08:40:17 +02003362 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003363 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003364static efi_status_t efi_connect_single_controller(
3365 efi_handle_t controller_handle,
3366 efi_handle_t *driver_image_handle,
3367 struct efi_device_path *remain_device_path)
3368{
3369 efi_handle_t *buffer;
3370 size_t count;
3371 size_t i;
3372 efi_status_t r;
3373 size_t connected = 0;
3374
3375 /* Get buffer with all handles with driver binding protocol */
3376 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3377 &efi_guid_driver_binding_protocol,
3378 NULL, &count, &buffer));
3379 if (r != EFI_SUCCESS)
3380 return r;
3381
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003382 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003383 if (driver_image_handle) {
3384 for (; *driver_image_handle; ++driver_image_handle) {
3385 for (i = 0; i < count; ++i) {
3386 if (buffer[i] == *driver_image_handle) {
3387 buffer[i] = NULL;
3388 r = efi_bind_controller(
3389 controller_handle,
3390 *driver_image_handle,
3391 remain_device_path);
3392 /*
3393 * For drivers that do not support the
3394 * controller or are already connected
3395 * we receive an error code here.
3396 */
3397 if (r == EFI_SUCCESS)
3398 ++connected;
3399 }
3400 }
3401 }
3402 }
3403
3404 /*
3405 * TODO: Some overrides are not yet implemented:
3406 * - Platform Driver Override
3407 * - Driver Family Override Search
3408 * - Bus Specific Driver Override
3409 */
3410
3411 /* Driver Binding Search */
3412 for (i = 0; i < count; ++i) {
3413 if (buffer[i]) {
3414 r = efi_bind_controller(controller_handle,
3415 buffer[i],
3416 remain_device_path);
3417 if (r == EFI_SUCCESS)
3418 ++connected;
3419 }
3420 }
3421
3422 efi_free_pool(buffer);
3423 if (!connected)
3424 return EFI_NOT_FOUND;
3425 return EFI_SUCCESS;
3426}
3427
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003428/**
Mario Six78a88f72018-07-10 08:40:17 +02003429 * efi_connect_controller() - connect a controller to a driver
3430 * @controller_handle: handle of the controller
3431 * @driver_image_handle: handle of the driver
3432 * @remain_device_path: device path of a child controller
3433 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003434 *
3435 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003436 *
3437 * See the Unified Extensible Firmware Interface (UEFI) specification for
3438 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003439 *
3440 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003441 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003442 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3443 *
Mario Six78a88f72018-07-10 08:40:17 +02003444 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003445 */
3446static efi_status_t EFIAPI efi_connect_controller(
3447 efi_handle_t controller_handle,
3448 efi_handle_t *driver_image_handle,
3449 struct efi_device_path *remain_device_path,
3450 bool recursive)
3451{
3452 efi_status_t r;
3453 efi_status_t ret = EFI_NOT_FOUND;
3454 struct efi_object *efiobj;
3455
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003456 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003457 remain_device_path, recursive);
3458
3459 efiobj = efi_search_obj(controller_handle);
3460 if (!efiobj) {
3461 ret = EFI_INVALID_PARAMETER;
3462 goto out;
3463 }
3464
3465 r = efi_connect_single_controller(controller_handle,
3466 driver_image_handle,
3467 remain_device_path);
3468 if (r == EFI_SUCCESS)
3469 ret = EFI_SUCCESS;
3470 if (recursive) {
3471 struct efi_handler *handler;
3472 struct efi_open_protocol_info_item *item;
3473
3474 list_for_each_entry(handler, &efiobj->protocols, link) {
3475 list_for_each_entry(item, &handler->open_infos, link) {
3476 if (item->info.attributes &
3477 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3478 r = EFI_CALL(efi_connect_controller(
3479 item->info.controller_handle,
3480 driver_image_handle,
3481 remain_device_path,
3482 recursive));
3483 if (r == EFI_SUCCESS)
3484 ret = EFI_SUCCESS;
3485 }
3486 }
3487 }
3488 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003489 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003490 if (ret != EFI_SUCCESS && remain_device_path &&
3491 remain_device_path->type == DEVICE_PATH_TYPE_END)
3492 ret = EFI_SUCCESS;
3493out:
3494 return EFI_EXIT(ret);
3495}
3496
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003497/**
Mario Six78a88f72018-07-10 08:40:17 +02003498 * efi_reinstall_protocol_interface() - reinstall protocol interface
3499 * @handle: handle on which the protocol shall be reinstalled
3500 * @protocol: GUID of the protocol to be installed
3501 * @old_interface: interface to be removed
3502 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003503 *
3504 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003505 *
3506 * See the Unified Extensible Firmware Interface (UEFI) specification for
3507 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003508 *
3509 * The old interface is uninstalled. The new interface is installed.
3510 * Drivers are connected.
3511 *
Mario Six78a88f72018-07-10 08:40:17 +02003512 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003513 */
3514static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3515 efi_handle_t handle, const efi_guid_t *protocol,
3516 void *old_interface, void *new_interface)
3517{
3518 efi_status_t ret;
3519
3520 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3521 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003522
3523 /* Uninstall protocol but do not delete handle */
3524 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003525 if (ret != EFI_SUCCESS)
3526 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003527
3528 /* Install the new protocol */
3529 ret = efi_add_protocol(handle, protocol, new_interface);
3530 /*
3531 * The UEFI spec does not specify what should happen to the handle
3532 * if in case of an error no protocol interface remains on the handle.
3533 * So let's do nothing here.
3534 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003535 if (ret != EFI_SUCCESS)
3536 goto out;
3537 /*
3538 * The returned status code has to be ignored.
3539 * Do not create an error if no suitable driver for the handle exists.
3540 */
3541 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3542out:
3543 return EFI_EXIT(ret);
3544}
3545
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003546/**
Mario Six78a88f72018-07-10 08:40:17 +02003547 * efi_get_child_controllers() - get all child controllers associated to a driver
3548 * @efiobj: handle of the controller
3549 * @driver_handle: handle of the driver
3550 * @number_of_children: number of child controllers
3551 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003552 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003553 * The allocated buffer has to be freed with free().
3554 *
Mario Six78a88f72018-07-10 08:40:17 +02003555 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003556 */
3557static efi_status_t efi_get_child_controllers(
3558 struct efi_object *efiobj,
3559 efi_handle_t driver_handle,
3560 efi_uintn_t *number_of_children,
3561 efi_handle_t **child_handle_buffer)
3562{
3563 struct efi_handler *handler;
3564 struct efi_open_protocol_info_item *item;
3565 efi_uintn_t count = 0, i;
3566 bool duplicate;
3567
3568 /* Count all child controller associations */
3569 list_for_each_entry(handler, &efiobj->protocols, link) {
3570 list_for_each_entry(item, &handler->open_infos, link) {
3571 if (item->info.agent_handle == driver_handle &&
3572 item->info.attributes &
3573 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3574 ++count;
3575 }
3576 }
3577 /*
3578 * Create buffer. In case of duplicate child controller assignments
3579 * the buffer will be too large. But that does not harm.
3580 */
3581 *number_of_children = 0;
Heinrich Schuchardt1047c6e2020-07-07 04:21:26 +02003582 if (!count)
3583 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003584 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3585 if (!*child_handle_buffer)
3586 return EFI_OUT_OF_RESOURCES;
3587 /* Copy unique child handles */
3588 list_for_each_entry(handler, &efiobj->protocols, link) {
3589 list_for_each_entry(item, &handler->open_infos, link) {
3590 if (item->info.agent_handle == driver_handle &&
3591 item->info.attributes &
3592 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3593 /* Check this is a new child controller */
3594 duplicate = false;
3595 for (i = 0; i < *number_of_children; ++i) {
3596 if ((*child_handle_buffer)[i] ==
3597 item->info.controller_handle)
3598 duplicate = true;
3599 }
3600 /* Copy handle to buffer */
3601 if (!duplicate) {
3602 i = (*number_of_children)++;
3603 (*child_handle_buffer)[i] =
3604 item->info.controller_handle;
3605 }
3606 }
3607 }
3608 }
3609 return EFI_SUCCESS;
3610}
3611
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003612/**
Mario Six78a88f72018-07-10 08:40:17 +02003613 * efi_disconnect_controller() - disconnect a controller from a driver
3614 * @controller_handle: handle of the controller
3615 * @driver_image_handle: handle of the driver
3616 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003617 *
3618 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003619 *
Mario Six78a88f72018-07-10 08:40:17 +02003620 * See the Unified Extensible Firmware Interface (UEFI) specification for
3621 * details.
3622 *
3623 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003624 */
3625static efi_status_t EFIAPI efi_disconnect_controller(
3626 efi_handle_t controller_handle,
3627 efi_handle_t driver_image_handle,
3628 efi_handle_t child_handle)
3629{
3630 struct efi_driver_binding_protocol *binding_protocol;
3631 efi_handle_t *child_handle_buffer = NULL;
3632 size_t number_of_children = 0;
3633 efi_status_t r;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003634 struct efi_object *efiobj;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003635 bool sole_child;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003636
3637 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3638 child_handle);
3639
3640 efiobj = efi_search_obj(controller_handle);
3641 if (!efiobj) {
3642 r = EFI_INVALID_PARAMETER;
3643 goto out;
3644 }
3645
3646 if (child_handle && !efi_search_obj(child_handle)) {
3647 r = EFI_INVALID_PARAMETER;
3648 goto out;
3649 }
3650
3651 /* If no driver handle is supplied, disconnect all drivers */
3652 if (!driver_image_handle) {
3653 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3654 goto out;
3655 }
3656
3657 /* Create list of child handles */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003658 r = efi_get_child_controllers(efiobj,
3659 driver_image_handle,
3660 &number_of_children,
3661 &child_handle_buffer);
3662 if (r != EFI_SUCCESS)
3663 return r;
3664 sole_child = (number_of_children == 1);
3665
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003666 if (child_handle) {
3667 number_of_children = 1;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003668 free(child_handle_buffer);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003669 child_handle_buffer = &child_handle;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003670 }
3671
3672 /* Get the driver binding protocol */
3673 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3674 &efi_guid_driver_binding_protocol,
3675 (void **)&binding_protocol,
3676 driver_image_handle, NULL,
3677 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003678 if (r != EFI_SUCCESS) {
3679 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003680 goto out;
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003681 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003682 /* Remove the children */
3683 if (number_of_children) {
3684 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3685 controller_handle,
3686 number_of_children,
3687 child_handle_buffer));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003688 if (r != EFI_SUCCESS) {
3689 r = EFI_DEVICE_ERROR;
3690 goto out;
3691 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003692 }
3693 /* Remove the driver */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003694 if (!child_handle || sole_child) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003695 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3696 controller_handle,
3697 0, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003698 if (r != EFI_SUCCESS) {
3699 r = EFI_DEVICE_ERROR;
3700 goto out;
3701 }
3702 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003703 EFI_CALL(efi_close_protocol(driver_image_handle,
3704 &efi_guid_driver_binding_protocol,
3705 driver_image_handle, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003706 r = EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003707out:
3708 if (!child_handle)
3709 free(child_handle_buffer);
3710 return EFI_EXIT(r);
3711}
3712
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003713static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003714 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003715 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3716 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003717 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003718 },
3719 .raise_tpl = efi_raise_tpl,
3720 .restore_tpl = efi_restore_tpl,
3721 .allocate_pages = efi_allocate_pages_ext,
3722 .free_pages = efi_free_pages_ext,
3723 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003724 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003725 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003726 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003727 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003728 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003729 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003730 .close_event = efi_close_event,
3731 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003732 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003733 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003734 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003735 .handle_protocol = efi_handle_protocol,
3736 .reserved = NULL,
3737 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003738 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003739 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003740 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003741 .load_image = efi_load_image,
3742 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003743 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003744 .unload_image = efi_unload_image,
3745 .exit_boot_services = efi_exit_boot_services,
3746 .get_next_monotonic_count = efi_get_next_monotonic_count,
3747 .stall = efi_stall,
3748 .set_watchdog_timer = efi_set_watchdog_timer,
3749 .connect_controller = efi_connect_controller,
3750 .disconnect_controller = efi_disconnect_controller,
3751 .open_protocol = efi_open_protocol,
3752 .close_protocol = efi_close_protocol,
3753 .open_protocol_information = efi_open_protocol_information,
3754 .protocols_per_handle = efi_protocols_per_handle,
3755 .locate_handle_buffer = efi_locate_handle_buffer,
3756 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003757 .install_multiple_protocol_interfaces =
3758 efi_install_multiple_protocol_interfaces,
3759 .uninstall_multiple_protocol_interfaces =
3760 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003761 .calculate_crc32 = efi_calculate_crc32,
3762 .copy_mem = efi_copy_mem,
3763 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003764 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003765};
3766
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003767static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003768
Alexander Graf3c63db92016-10-14 13:45:30 +02003769struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003770 .hdr = {
3771 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003772 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003773 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003774 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003775 .fw_vendor = firmware_vendor,
3776 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02003777 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003778 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003779 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003780};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003781
3782/**
3783 * efi_initialize_system_table() - Initialize system table
3784 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003785 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003786 */
3787efi_status_t efi_initialize_system_table(void)
3788{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003789 efi_status_t ret;
3790
3791 /* Allocate configuration table array */
3792 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3793 EFI_MAX_CONFIGURATION_TABLES *
3794 sizeof(struct efi_configuration_table),
3795 (void **)&systab.tables);
3796
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003797 /*
3798 * These entries will be set to NULL in ExitBootServices(). To avoid
3799 * relocation in SetVirtualAddressMap(), set them dynamically.
3800 */
3801 systab.con_in = &efi_con_in;
3802 systab.con_out = &efi_con_out;
3803 systab.std_err = &efi_con_out;
3804 systab.boottime = &efi_boot_services;
3805
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003806 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003807 efi_update_table_header_crc32(&systab.hdr);
3808 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3809 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003810
3811 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003812}