blob: 246b59d3b354bc037766b29af2c5d67e8c51abc9 [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>
Ilias Apalodimas19763ea2020-10-22 01:04:20 +030022#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010023
24DECLARE_GLOBAL_DATA_PTR;
25
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020026/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010027static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020028
Alexander Grafbee91162016-03-04 01:09:59 +010029/* This list contains all the EFI objects our payload has access to */
30LIST_HEAD(efi_obj_list);
31
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010032/* List of all events */
Heinrich Schuchardt14b40482019-07-11 20:15:09 +020033__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010034
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020035/* List of queued events */
36LIST_HEAD(efi_event_queue);
37
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +020038/* Flag to disable timer activity in ExitBootServices() */
39static bool timers_enabled = true;
40
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +010041/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
42bool efi_st_keep_devices;
43
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020044/* List of all events registered by RegisterProtocolNotify() */
45LIST_HEAD(efi_register_notify_events);
46
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010047/* Handle of the currently executing image */
48static efi_handle_t current_image;
49
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020050#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +010051/*
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020052 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafbee91162016-03-04 01:09:59 +010053 * fixed when compiling U-Boot. However, the payload does not know about that
54 * restriction so we need to manually swap its and our view of that register on
55 * EFI callback entry/exit.
56 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +020057static volatile gd_t *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060058#endif
Alexander Grafbee91162016-03-04 01:09:59 +010059
Heinrich Schuchardt914df752019-02-09 14:10:39 +010060/* 1 if inside U-Boot code, 0 if inside EFI payload code */
61static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040062static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010063/* GUID of the device tree table */
64const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010065/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
66const efi_guid_t efi_guid_driver_binding_protocol =
67 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040068
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010069/* event group ExitBootServices() invoked */
70const efi_guid_t efi_guid_event_group_exit_boot_services =
71 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
72/* event group SetVirtualAddressMap() invoked */
73const efi_guid_t efi_guid_event_group_virtual_address_change =
74 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
75/* event group memory map changed */
76const efi_guid_t efi_guid_event_group_memory_map_change =
77 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
78/* event group boot manager about to boot */
79const efi_guid_t efi_guid_event_group_ready_to_boot =
80 EFI_EVENT_GROUP_READY_TO_BOOT;
81/* event group ResetSystem() invoked (before ExitBootServices) */
82const efi_guid_t efi_guid_event_group_reset_system =
83 EFI_EVENT_GROUP_RESET_SYSTEM;
84
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010085static efi_status_t EFIAPI efi_disconnect_controller(
86 efi_handle_t controller_handle,
87 efi_handle_t driver_image_handle,
88 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010089
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/* Called on every callback entry */
91int __efi_entry_check(void)
92{
93 int ret = entry_count++ == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +020094#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clarkc160d2f2017-07-27 08:04:18 -040095 assert(efi_gd);
96 app_gd = gd;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +020097 set_gd(efi_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -040098#endif
99 return ret;
100}
101
102/* Called on every callback exit */
103int __efi_exit_check(void)
104{
105 int ret = --entry_count == 0;
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200106#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200107 set_gd(app_gd);
Rob Clarkc160d2f2017-07-27 08:04:18 -0400108#endif
109 return ret;
110}
111
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200112/**
113 * efi_save_gd() - save global data register
114 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200115 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200116 * As this register may be overwritten by an EFI payload we save it here
117 * and restore it on every callback entered.
118 *
119 * This function is called after relocation from initr_reloc_global_data().
120 */
Alexander Grafbee91162016-03-04 01:09:59 +0100121void efi_save_gd(void)
122{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200123#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100124 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600125#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100126}
127
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200128/**
129 * efi_restore_gd() - restore global data register
130 *
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200131 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardte7d64062020-07-18 09:53:01 +0200132 * Restore it after returning from the UEFI world to the value saved via
133 * efi_save_gd().
Rob Clarkc160d2f2017-07-27 08:04:18 -0400134 */
Alexander Grafbee91162016-03-04 01:09:59 +0100135void efi_restore_gd(void)
136{
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +0200137#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafbee91162016-03-04 01:09:59 +0100138 /* Only restore if we're already in EFI context */
139 if (!efi_gd)
140 return;
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +0200141 set_gd(efi_gd);
Simon Glass65e4c0b2016-09-25 15:27:35 -0600142#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100143}
144
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200145/**
Mario Six78a88f72018-07-10 08:40:17 +0200146 * indent_string() - returns a string for indenting with two spaces per level
147 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100148 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200149 * A maximum of ten indent levels is supported. Higher indent levels will be
150 * truncated.
151 *
Mario Six78a88f72018-07-10 08:40:17 +0200152 * Return: A string for indenting with two spaces per level is
153 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400154 */
155static const char *indent_string(int level)
156{
157 const char *indent = " ";
158 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100159
Rob Clarkaf65db82017-07-27 08:04:19 -0400160 level = min(max, level * 2);
161 return &indent[max - level];
162}
163
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200164const char *__efi_nesting(void)
165{
166 return indent_string(nesting_level);
167}
168
Rob Clarkaf65db82017-07-27 08:04:19 -0400169const char *__efi_nesting_inc(void)
170{
171 return indent_string(nesting_level++);
172}
173
174const char *__efi_nesting_dec(void)
175{
176 return indent_string(--nesting_level);
177}
178
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200179/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200180 * efi_event_is_queued() - check if an event is queued
181 *
182 * @event: event
183 * Return: true if event is queued
184 */
185static bool efi_event_is_queued(struct efi_event *event)
186{
187 return !!event->queue_link.next;
188}
189
190/**
191 * efi_process_event_queue() - process event queue
192 */
193static void efi_process_event_queue(void)
194{
195 while (!list_empty(&efi_event_queue)) {
196 struct efi_event *event;
197 efi_uintn_t old_tpl;
198
199 event = list_first_entry(&efi_event_queue, struct efi_event,
200 queue_link);
201 if (efi_tpl >= event->notify_tpl)
202 return;
203 list_del(&event->queue_link);
204 event->queue_link.next = NULL;
205 event->queue_link.prev = NULL;
206 /* Events must be executed at the event's TPL */
207 old_tpl = efi_tpl;
208 efi_tpl = event->notify_tpl;
209 EFI_CALL_VOID(event->notify_function(event,
210 event->notify_context));
211 efi_tpl = old_tpl;
212 if (event->type == EVT_NOTIFY_SIGNAL)
213 event->is_signaled = 0;
214 }
215}
216
217/**
Mario Six78a88f72018-07-10 08:40:17 +0200218 * efi_queue_event() - queue an EFI event
219 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200220 *
221 * This function queues the notification function of the event for future
222 * execution.
223 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200224 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200225static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200226{
Heinrich Schuchardt2b8568f2020-03-06 21:56:10 +0100227 struct efi_event *item;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200228
229 if (!event->notify_function)
230 return;
231
232 if (!efi_event_is_queued(event)) {
233 /*
234 * Events must be notified in order of decreasing task priority
235 * level. Insert the new event accordingly.
236 */
237 list_for_each_entry(item, &efi_event_queue, queue_link) {
238 if (item->notify_tpl < event->notify_tpl) {
239 list_add_tail(&event->queue_link,
240 &item->queue_link);
241 event = NULL;
242 break;
243 }
244 }
245 if (event)
246 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200247 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200248 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200249}
250
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200251/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200252 * is_valid_tpl() - check if the task priority level is valid
253 *
254 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200255 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200256 */
257efi_status_t is_valid_tpl(efi_uintn_t tpl)
258{
259 switch (tpl) {
260 case TPL_APPLICATION:
261 case TPL_CALLBACK:
262 case TPL_NOTIFY:
263 case TPL_HIGH_LEVEL:
264 return EFI_SUCCESS;
265 default:
266 return EFI_INVALID_PARAMETER;
267 }
268}
269
270/**
Mario Six78a88f72018-07-10 08:40:17 +0200271 * efi_signal_event() - signal an EFI event
272 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100273 *
Mario Six78a88f72018-07-10 08:40:17 +0200274 * This function signals an event. If the event belongs to an event group all
275 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100276 * their notification function is queued.
277 *
278 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100279 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200280void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100281{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200282 if (event->is_signaled)
283 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100284 if (event->group) {
285 struct efi_event *evt;
286
287 /*
288 * The signaled state has to set before executing any
289 * notification function
290 */
291 list_for_each_entry(evt, &efi_events, link) {
292 if (!evt->group || guidcmp(evt->group, event->group))
293 continue;
294 if (evt->is_signaled)
295 continue;
296 evt->is_signaled = true;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100297 }
298 list_for_each_entry(evt, &efi_events, link) {
299 if (!evt->group || guidcmp(evt->group, event->group))
300 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200301 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100302 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200303 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100304 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200305 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100306 }
307}
308
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200309/**
Mario Six78a88f72018-07-10 08:40:17 +0200310 * efi_raise_tpl() - raise the task priority level
311 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200312 *
313 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200314 *
Mario Six78a88f72018-07-10 08:40:17 +0200315 * See the Unified Extensible Firmware Interface (UEFI) specification for
316 * details.
317 *
318 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200319 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100320static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100321{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100322 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200323
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200324 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200325
326 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200327 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200328 efi_tpl = new_tpl;
329 if (efi_tpl > TPL_HIGH_LEVEL)
330 efi_tpl = TPL_HIGH_LEVEL;
331
332 EFI_EXIT(EFI_SUCCESS);
333 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100334}
335
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200336/**
Mario Six78a88f72018-07-10 08:40:17 +0200337 * efi_restore_tpl() - lower the task priority level
338 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200339 *
340 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200341 *
Mario Six78a88f72018-07-10 08:40:17 +0200342 * See the Unified Extensible Firmware Interface (UEFI) specification for
343 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200344 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100345static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100346{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200347 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200348
349 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200350 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200351 efi_tpl = old_tpl;
352 if (efi_tpl > TPL_HIGH_LEVEL)
353 efi_tpl = TPL_HIGH_LEVEL;
354
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100355 /*
356 * Lowering the TPL may have made queued events eligible for execution.
357 */
358 efi_timer_check();
359
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200360 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100361}
362
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200363/**
Mario Six78a88f72018-07-10 08:40:17 +0200364 * efi_allocate_pages_ext() - allocate memory pages
365 * @type: type of allocation to be performed
366 * @memory_type: usage type of the allocated memory
367 * @pages: number of pages to be allocated
368 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200369 *
370 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200371 *
Mario Six78a88f72018-07-10 08:40:17 +0200372 * See the Unified Extensible Firmware Interface (UEFI) specification for
373 * details.
374 *
375 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200376 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900377static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100378 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900379 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100380{
381 efi_status_t r;
382
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100383 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100384 r = efi_allocate_pages(type, memory_type, pages, memory);
385 return EFI_EXIT(r);
386}
387
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200388/**
Mario Six78a88f72018-07-10 08:40:17 +0200389 * efi_free_pages_ext() - Free memory pages.
390 * @memory: start of the memory area to be freed
391 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200392 *
393 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200394 *
Mario Six78a88f72018-07-10 08:40:17 +0200395 * See the Unified Extensible Firmware Interface (UEFI) specification for
396 * details.
397 *
398 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200399 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900400static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100401 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100402{
403 efi_status_t r;
404
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900405 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100406 r = efi_free_pages(memory, pages);
407 return EFI_EXIT(r);
408}
409
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200410/**
Mario Six78a88f72018-07-10 08:40:17 +0200411 * efi_get_memory_map_ext() - get map describing memory usage
412 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
413 * on exit the size of the copied memory map
414 * @memory_map: buffer to which the memory map is written
415 * @map_key: key for the memory map
416 * @descriptor_size: size of an individual memory descriptor
417 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200418 *
419 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200420 *
Mario Six78a88f72018-07-10 08:40:17 +0200421 * See the Unified Extensible Firmware Interface (UEFI) specification for
422 * details.
423 *
424 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200425 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900426static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100427 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900428 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100429 efi_uintn_t *map_key,
430 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900431 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100432{
433 efi_status_t r;
434
435 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
436 map_key, descriptor_size, descriptor_version);
437 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
438 descriptor_size, descriptor_version);
439 return EFI_EXIT(r);
440}
441
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200442/**
Mario Six78a88f72018-07-10 08:40:17 +0200443 * efi_allocate_pool_ext() - allocate memory from pool
444 * @pool_type: type of the pool from which memory is to be allocated
445 * @size: number of bytes to be allocated
446 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200447 *
448 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200449 *
Mario Six78a88f72018-07-10 08:40:17 +0200450 * See the Unified Extensible Firmware Interface (UEFI) specification for
451 * details.
452 *
453 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200454 */
Stefan Brünsead12742016-10-09 22:17:18 +0200455static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100456 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200457 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100458{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100459 efi_status_t r;
460
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100461 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200462 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100463 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100464}
465
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200466/**
Mario Six78a88f72018-07-10 08:40:17 +0200467 * efi_free_pool_ext() - free memory from pool
468 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200469 *
470 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200471 *
Mario Six78a88f72018-07-10 08:40:17 +0200472 * See the Unified Extensible Firmware Interface (UEFI) specification for
473 * details.
474 *
475 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200476 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200477static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100478{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100479 efi_status_t r;
480
481 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200482 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100483 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100484}
485
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200486/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200487 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100488 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200489 * @handle: handle to be added
490 *
491 * The protocols list is initialized. The handle is added to the list of known
492 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100493 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200494void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100495{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200496 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100497 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200498 INIT_LIST_HEAD(&handle->protocols);
499 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100500}
501
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200502/**
Mario Six78a88f72018-07-10 08:40:17 +0200503 * efi_create_handle() - create handle
504 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200505 *
Mario Six78a88f72018-07-10 08:40:17 +0200506 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200507 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100508efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200509{
510 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200511
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200512 obj = calloc(1, sizeof(struct efi_object));
513 if (!obj)
514 return EFI_OUT_OF_RESOURCES;
515
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100516 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200517 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200518
519 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200520}
521
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200522/**
Mario Six78a88f72018-07-10 08:40:17 +0200523 * efi_search_protocol() - find a protocol on a handle.
524 * @handle: handle
525 * @protocol_guid: GUID of the protocol
526 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100527 *
Mario Six78a88f72018-07-10 08:40:17 +0200528 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100529 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100530efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100531 const efi_guid_t *protocol_guid,
532 struct efi_handler **handler)
533{
534 struct efi_object *efiobj;
535 struct list_head *lhandle;
536
537 if (!handle || !protocol_guid)
538 return EFI_INVALID_PARAMETER;
539 efiobj = efi_search_obj(handle);
540 if (!efiobj)
541 return EFI_INVALID_PARAMETER;
542 list_for_each(lhandle, &efiobj->protocols) {
543 struct efi_handler *protocol;
544
545 protocol = list_entry(lhandle, struct efi_handler, link);
546 if (!guidcmp(protocol->guid, protocol_guid)) {
547 if (handler)
548 *handler = protocol;
549 return EFI_SUCCESS;
550 }
551 }
552 return EFI_NOT_FOUND;
553}
554
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200555/**
Mario Six78a88f72018-07-10 08:40:17 +0200556 * efi_remove_protocol() - delete protocol from a handle
557 * @handle: handle from which the protocol shall be deleted
558 * @protocol: GUID of the protocol to be deleted
559 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100560 *
Mario Six78a88f72018-07-10 08:40:17 +0200561 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100562 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100563efi_status_t efi_remove_protocol(const efi_handle_t handle,
564 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100565 void *protocol_interface)
566{
567 struct efi_handler *handler;
568 efi_status_t ret;
569
570 ret = efi_search_protocol(handle, protocol, &handler);
571 if (ret != EFI_SUCCESS)
572 return ret;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200573 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200574 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100575 list_del(&handler->link);
576 free(handler);
577 return EFI_SUCCESS;
578}
579
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200580/**
Mario Six78a88f72018-07-10 08:40:17 +0200581 * efi_remove_all_protocols() - delete all protocols from a handle
582 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100583 *
Mario Six78a88f72018-07-10 08:40:17 +0200584 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100585 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100586efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100587{
588 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100589 struct efi_handler *protocol;
590 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100591
592 efiobj = efi_search_obj(handle);
593 if (!efiobj)
594 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100595 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100596 efi_status_t ret;
597
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100598 ret = efi_remove_protocol(handle, protocol->guid,
599 protocol->protocol_interface);
600 if (ret != EFI_SUCCESS)
601 return ret;
602 }
603 return EFI_SUCCESS;
604}
605
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200606/**
Mario Six78a88f72018-07-10 08:40:17 +0200607 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100608 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200609 * @handle: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100610 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200611void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100612{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200613 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100614 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200615 efi_remove_all_protocols(handle);
616 list_del(&handle->link);
617 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100618}
619
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200620/**
Mario Six78a88f72018-07-10 08:40:17 +0200621 * efi_is_event() - check if a pointer is a valid event
622 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100623 *
Mario Six78a88f72018-07-10 08:40:17 +0200624 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100625 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100626static efi_status_t efi_is_event(const struct efi_event *event)
627{
628 const struct efi_event *evt;
629
630 if (!event)
631 return EFI_INVALID_PARAMETER;
632 list_for_each_entry(evt, &efi_events, link) {
633 if (evt == event)
634 return EFI_SUCCESS;
635 }
636 return EFI_INVALID_PARAMETER;
637}
Alexander Grafbee91162016-03-04 01:09:59 +0100638
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200639/**
Mario Six78a88f72018-07-10 08:40:17 +0200640 * efi_create_event() - create an event
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +0200641 *
Mario Six78a88f72018-07-10 08:40:17 +0200642 * @type: type of the event to create
643 * @notify_tpl: task priority level of the event
644 * @notify_function: notification function of the event
645 * @notify_context: pointer passed to the notification function
646 * @group: event group
647 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200648 *
649 * This function is used inside U-Boot code to create an event.
650 *
651 * For the API function implementing the CreateEvent service see
652 * efi_create_event_ext.
653 *
Mario Six78a88f72018-07-10 08:40:17 +0200654 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200655 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100656efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200657 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200658 struct efi_event *event,
659 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100660 void *notify_context, efi_guid_t *group,
661 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100662{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100663 struct efi_event *evt;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200664 efi_status_t ret;
665 int pool_type;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200666
Jonathan Graya95343b2017-03-12 19:26:07 +1100667 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200668 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100669
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200670 switch (type) {
671 case 0:
672 case EVT_TIMER:
673 case EVT_NOTIFY_SIGNAL:
674 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
675 case EVT_NOTIFY_WAIT:
676 case EVT_TIMER | EVT_NOTIFY_WAIT:
677 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200678 pool_type = EFI_BOOT_SERVICES_DATA;
679 break;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200680 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200681 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200682 break;
683 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200684 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200685 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100686
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900687 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt751e9282019-04-25 07:30:41 +0200688 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200689 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100690
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200691 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
692 (void **)&evt);
693 if (ret != EFI_SUCCESS)
694 return ret;
695 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100696 evt->type = type;
697 evt->notify_tpl = notify_tpl;
698 evt->notify_function = notify_function;
699 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100700 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200701 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100702 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100703 list_add_tail(&evt->link, &efi_events);
704 *event = evt;
705 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100706}
707
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200708/*
Mario Six78a88f72018-07-10 08:40:17 +0200709 * efi_create_event_ex() - create an event in a group
710 * @type: type of the event to create
711 * @notify_tpl: task priority level of the event
712 * @notify_function: notification function of the event
713 * @notify_context: pointer passed to the notification function
714 * @event: created event
715 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100716 *
717 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100718 *
Mario Six78a88f72018-07-10 08:40:17 +0200719 * See the Unified Extensible Firmware Interface (UEFI) specification for
720 * details.
721 *
722 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100723 */
724efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
725 void (EFIAPI *notify_function) (
726 struct efi_event *event,
727 void *context),
728 void *notify_context,
729 efi_guid_t *event_group,
730 struct efi_event **event)
731{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200732 efi_status_t ret;
733
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100734 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
735 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200736
737 /*
738 * The allowable input parameters are the same as in CreateEvent()
739 * except for the following two disallowed event types.
740 */
741 switch (type) {
742 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
743 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
744 ret = EFI_INVALID_PARAMETER;
745 goto out;
746 }
747
748 ret = efi_create_event(type, notify_tpl, notify_function,
749 notify_context, event_group, event);
750out:
751 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100752}
753
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200754/**
Mario Six78a88f72018-07-10 08:40:17 +0200755 * efi_create_event_ext() - create an event
756 * @type: type of the event to create
757 * @notify_tpl: task priority level of the event
758 * @notify_function: notification function of the event
759 * @notify_context: pointer passed to the notification function
760 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200761 *
762 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200763 *
Mario Six78a88f72018-07-10 08:40:17 +0200764 * See the Unified Extensible Firmware Interface (UEFI) specification for
765 * details.
766 *
767 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200768 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200769static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100770 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200771 void (EFIAPI *notify_function) (
772 struct efi_event *event,
773 void *context),
774 void *notify_context, struct efi_event **event)
775{
776 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
777 notify_context);
778 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100779 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200780}
781
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200782/**
Mario Six78a88f72018-07-10 08:40:17 +0200783 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200784 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200785 * Check if a timer event has occurred or a queued notification function should
786 * be called.
787 *
Alexander Grafbee91162016-03-04 01:09:59 +0100788 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200789 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100790 */
791void efi_timer_check(void)
792{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100793 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100794 u64 now = timer_get_us();
795
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100796 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200797 if (!timers_enabled)
798 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100799 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200800 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100801 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200802 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100803 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200804 break;
805 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100806 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200807 break;
808 default:
809 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200810 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100811 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200812 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100813 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200814 efi_process_event_queue();
Alexander Grafbee91162016-03-04 01:09:59 +0100815 WATCHDOG_RESET();
816}
817
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200818/**
Mario Six78a88f72018-07-10 08:40:17 +0200819 * efi_set_timer() - set the trigger time for a timer event or stop the event
820 * @event: event for which the timer is set
821 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200822 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200823 *
824 * This is the function for internal usage in U-Boot. For the API function
825 * implementing the SetTimer service see efi_set_timer_ext.
826 *
Mario Six78a88f72018-07-10 08:40:17 +0200827 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200828 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200829efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200830 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100831{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100832 /* Check that the event is valid */
833 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
834 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100835
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200836 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200837 * The parameter defines a multiple of 100 ns.
838 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200839 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200840 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100841
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100842 switch (type) {
843 case EFI_TIMER_STOP:
844 event->trigger_next = -1ULL;
845 break;
846 case EFI_TIMER_PERIODIC:
847 case EFI_TIMER_RELATIVE:
848 event->trigger_next = timer_get_us() + trigger_time;
849 break;
850 default:
851 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100852 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100853 event->trigger_type = type;
854 event->trigger_time = trigger_time;
855 event->is_signaled = false;
856 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200857}
858
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200859/**
Mario Six78a88f72018-07-10 08:40:17 +0200860 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
861 * event
862 * @event: event for which the timer is set
863 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200864 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200865 *
866 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200867 *
Mario Six78a88f72018-07-10 08:40:17 +0200868 * See the Unified Extensible Firmware Interface (UEFI) specification for
869 * details.
870 *
871 *
872 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200873 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200874static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
875 enum efi_timer_delay type,
876 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200877{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900878 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200879 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100880}
881
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200882/**
Mario Six78a88f72018-07-10 08:40:17 +0200883 * efi_wait_for_event() - wait for events to be signaled
884 * @num_events: number of events to be waited for
885 * @event: events to be waited for
886 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200887 *
888 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200889 *
Mario Six78a88f72018-07-10 08:40:17 +0200890 * See the Unified Extensible Firmware Interface (UEFI) specification for
891 * details.
892 *
893 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200894 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100895static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200896 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100897 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100898{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100899 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100900
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100901 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100902
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200903 /* Check parameters */
904 if (!num_events || !event)
905 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200906 /* Check TPL */
907 if (efi_tpl != TPL_APPLICATION)
908 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200909 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100910 if (efi_is_event(event[i]) != EFI_SUCCESS)
911 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200912 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
913 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200914 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200915 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200916 }
917
918 /* Wait for signal */
919 for (;;) {
920 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200921 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200922 goto out;
923 }
924 /* Allow events to occur. */
925 efi_timer_check();
926 }
927
928out:
929 /*
930 * Reset the signal which is passed to the caller to allow periodic
931 * events to occur.
932 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200933 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200934 if (index)
935 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100936
937 return EFI_EXIT(EFI_SUCCESS);
938}
939
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200940/**
Mario Six78a88f72018-07-10 08:40:17 +0200941 * efi_signal_event_ext() - signal an EFI event
942 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200943 *
944 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200945 *
946 * See the Unified Extensible Firmware Interface (UEFI) specification for
947 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200948 *
949 * This functions sets the signaled state of the event and queues the
950 * notification function for execution.
951 *
Mario Six78a88f72018-07-10 08:40:17 +0200952 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200953 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200954static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100955{
956 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100957 if (efi_is_event(event) != EFI_SUCCESS)
958 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200959 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +0100960 return EFI_EXIT(EFI_SUCCESS);
961}
962
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200963/**
Mario Six78a88f72018-07-10 08:40:17 +0200964 * efi_close_event() - close an EFI event
965 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200966 *
967 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200968 *
Mario Six78a88f72018-07-10 08:40:17 +0200969 * See the Unified Extensible Firmware Interface (UEFI) specification for
970 * details.
971 *
972 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200973 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200974static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100975{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200976 struct efi_register_notify_event *item, *next;
977
Alexander Grafbee91162016-03-04 01:09:59 +0100978 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100979 if (efi_is_event(event) != EFI_SUCCESS)
980 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200981
982 /* Remove protocol notify registrations for the event */
983 list_for_each_entry_safe(item, next, &efi_register_notify_events,
984 link) {
985 if (event == item->event) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +0200986 struct efi_protocol_notification *hitem, *hnext;
987
988 /* Remove signaled handles */
989 list_for_each_entry_safe(hitem, hnext, &item->handles,
990 link) {
991 list_del(&hitem->link);
992 free(hitem);
993 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200994 list_del(&item->link);
995 free(item);
996 }
997 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200998 /* Remove event from queue */
999 if (efi_event_is_queued(event))
1000 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001001
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001002 list_del(&event->link);
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001003 efi_free_pool(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001004 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001005}
1006
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001007/**
Mario Six78a88f72018-07-10 08:40:17 +02001008 * efi_check_event() - check if an event is signaled
1009 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001010 *
1011 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001012 *
Mario Six78a88f72018-07-10 08:40:17 +02001013 * See the Unified Extensible Firmware Interface (UEFI) specification for
1014 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001015 *
Mario Six78a88f72018-07-10 08:40:17 +02001016 * If an event is not signaled yet, the notification function is queued. The
1017 * signaled state is cleared.
1018 *
1019 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001020 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001021static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001022{
1023 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001024 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001025 if (efi_is_event(event) != EFI_SUCCESS ||
1026 event->type & EVT_NOTIFY_SIGNAL)
1027 return EFI_EXIT(EFI_INVALID_PARAMETER);
1028 if (!event->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001029 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001030 if (event->is_signaled) {
1031 event->is_signaled = false;
1032 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001033 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001034 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001035}
1036
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001037/**
Mario Six78a88f72018-07-10 08:40:17 +02001038 * efi_search_obj() - find the internal EFI object for a handle
1039 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001040 *
Mario Six78a88f72018-07-10 08:40:17 +02001041 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001042 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001043struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001044{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001045 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001046
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001047 if (!handle)
1048 return NULL;
1049
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001050 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001051 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001052 return efiobj;
1053 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001054 return NULL;
1055}
1056
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001057/**
Mario Six78a88f72018-07-10 08:40:17 +02001058 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1059 * to a protocol
1060 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001061 *
Mario Six78a88f72018-07-10 08:40:17 +02001062 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001063 */
1064static struct efi_open_protocol_info_entry *efi_create_open_info(
1065 struct efi_handler *handler)
1066{
1067 struct efi_open_protocol_info_item *item;
1068
1069 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1070 if (!item)
1071 return NULL;
1072 /* Append the item to the open protocol info list. */
1073 list_add_tail(&item->link, &handler->open_infos);
1074
1075 return &item->info;
1076}
1077
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001078/**
Mario Six78a88f72018-07-10 08:40:17 +02001079 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1080 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001081 *
Mario Six78a88f72018-07-10 08:40:17 +02001082 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001083 */
1084static efi_status_t efi_delete_open_info(
1085 struct efi_open_protocol_info_item *item)
1086{
1087 list_del(&item->link);
1088 free(item);
1089 return EFI_SUCCESS;
1090}
1091
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001092/**
Mario Six78a88f72018-07-10 08:40:17 +02001093 * efi_add_protocol() - install new protocol on a handle
1094 * @handle: handle on which the protocol shall be installed
1095 * @protocol: GUID of the protocol to be installed
1096 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001097 *
Mario Six78a88f72018-07-10 08:40:17 +02001098 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001099 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001100efi_status_t efi_add_protocol(const efi_handle_t handle,
1101 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001102 void *protocol_interface)
1103{
1104 struct efi_object *efiobj;
1105 struct efi_handler *handler;
1106 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001107 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001108
1109 efiobj = efi_search_obj(handle);
1110 if (!efiobj)
1111 return EFI_INVALID_PARAMETER;
1112 ret = efi_search_protocol(handle, protocol, NULL);
1113 if (ret != EFI_NOT_FOUND)
1114 return EFI_INVALID_PARAMETER;
1115 handler = calloc(1, sizeof(struct efi_handler));
1116 if (!handler)
1117 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001118 handler->guid = protocol;
1119 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001120 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001121 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001122
1123 /* Notify registered events */
1124 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001125 if (!guidcmp(protocol, &event->protocol)) {
1126 struct efi_protocol_notification *notif;
1127
1128 notif = calloc(1, sizeof(*notif));
1129 if (!notif) {
1130 list_del(&handler->link);
1131 free(handler);
1132 return EFI_OUT_OF_RESOURCES;
1133 }
1134 notif->handle = handle;
1135 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001136 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001137 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001138 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001139 }
1140
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001141 if (!guidcmp(&efi_guid_device_path, protocol))
1142 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001143 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001144}
1145
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001146/**
Mario Six78a88f72018-07-10 08:40:17 +02001147 * efi_install_protocol_interface() - install protocol interface
1148 * @handle: handle on which the protocol shall be installed
1149 * @protocol: GUID of the protocol to be installed
1150 * @protocol_interface_type: type of the interface to be installed,
1151 * always EFI_NATIVE_INTERFACE
1152 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001153 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001154 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001155 *
Mario Six78a88f72018-07-10 08:40:17 +02001156 * See the Unified Extensible Firmware Interface (UEFI) specification for
1157 * details.
1158 *
1159 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001160 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001161static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001162 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001163 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001164{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001165 efi_status_t r;
1166
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001167 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1168 protocol_interface);
1169
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001170 if (!handle || !protocol ||
1171 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1172 r = EFI_INVALID_PARAMETER;
1173 goto out;
1174 }
1175
1176 /* Create new handle if requested. */
1177 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001178 r = efi_create_handle(handle);
1179 if (r != EFI_SUCCESS)
1180 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001181 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001182 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001183 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001184 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001185 /* Add new protocol */
1186 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001187out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001188 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001189}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001190
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001191/**
Mario Six78a88f72018-07-10 08:40:17 +02001192 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001193 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001194 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001195 * @number_of_drivers: number of child controllers
1196 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001197 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001198 * The allocated buffer has to be freed with free().
1199 *
Mario Six78a88f72018-07-10 08:40:17 +02001200 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001201 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001202static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001203 const efi_guid_t *protocol,
1204 efi_uintn_t *number_of_drivers,
1205 efi_handle_t **driver_handle_buffer)
1206{
1207 struct efi_handler *handler;
1208 struct efi_open_protocol_info_item *item;
1209 efi_uintn_t count = 0, i;
1210 bool duplicate;
1211
1212 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001213 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001214 if (protocol && guidcmp(handler->guid, protocol))
1215 continue;
1216 list_for_each_entry(item, &handler->open_infos, link) {
1217 if (item->info.attributes &
1218 EFI_OPEN_PROTOCOL_BY_DRIVER)
1219 ++count;
1220 }
1221 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001222 *number_of_drivers = 0;
1223 if (!count) {
1224 *driver_handle_buffer = NULL;
1225 return EFI_SUCCESS;
1226 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001227 /*
1228 * Create buffer. In case of duplicate driver assignments the buffer
1229 * will be too large. But that does not harm.
1230 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001231 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1232 if (!*driver_handle_buffer)
1233 return EFI_OUT_OF_RESOURCES;
1234 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001235 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001236 if (protocol && guidcmp(handler->guid, protocol))
1237 continue;
1238 list_for_each_entry(item, &handler->open_infos, link) {
1239 if (item->info.attributes &
1240 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1241 /* Check this is a new driver */
1242 duplicate = false;
1243 for (i = 0; i < *number_of_drivers; ++i) {
1244 if ((*driver_handle_buffer)[i] ==
1245 item->info.agent_handle)
1246 duplicate = true;
1247 }
1248 /* Copy handle to buffer */
1249 if (!duplicate) {
1250 i = (*number_of_drivers)++;
1251 (*driver_handle_buffer)[i] =
1252 item->info.agent_handle;
1253 }
1254 }
1255 }
1256 }
1257 return EFI_SUCCESS;
1258}
1259
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001260/**
Mario Six78a88f72018-07-10 08:40:17 +02001261 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001262 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001263 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001264 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001265 *
1266 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001267 *
Mario Six78a88f72018-07-10 08:40:17 +02001268 * See the Unified Extensible Firmware Interface (UEFI) specification for
1269 * details.
1270 *
1271 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001272 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001273static efi_status_t efi_disconnect_all_drivers
1274 (efi_handle_t handle,
1275 const efi_guid_t *protocol,
1276 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001277{
1278 efi_uintn_t number_of_drivers;
1279 efi_handle_t *driver_handle_buffer;
1280 efi_status_t r, ret;
1281
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001282 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001283 &driver_handle_buffer);
1284 if (ret != EFI_SUCCESS)
1285 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001286 if (!number_of_drivers)
1287 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001288 ret = EFI_NOT_FOUND;
1289 while (number_of_drivers) {
1290 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001291 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001292 driver_handle_buffer[--number_of_drivers],
1293 child_handle));
1294 if (r == EFI_SUCCESS)
1295 ret = r;
1296 }
1297 free(driver_handle_buffer);
1298 return ret;
1299}
1300
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001301/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001302 * efi_uninstall_protocol() - uninstall protocol interface
1303 *
Mario Six78a88f72018-07-10 08:40:17 +02001304 * @handle: handle from which the protocol shall be removed
1305 * @protocol: GUID of the protocol to be removed
1306 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001307 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001308 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001309 *
1310 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001311 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001312static efi_status_t efi_uninstall_protocol
1313 (efi_handle_t handle, const efi_guid_t *protocol,
1314 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001315{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001316 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001317 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001318 struct efi_open_protocol_info_item *item;
1319 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001320 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001321
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001322 /* Check handle */
1323 efiobj = efi_search_obj(handle);
1324 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001325 r = EFI_INVALID_PARAMETER;
1326 goto out;
1327 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001328 /* Find the protocol on the handle */
1329 r = efi_search_protocol(handle, protocol, &handler);
1330 if (r != EFI_SUCCESS)
1331 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001332 /* Disconnect controllers */
1333 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001334 /* Close protocol */
1335 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1336 if (item->info.attributes ==
1337 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1338 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1339 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1340 list_del(&item->link);
1341 }
1342 if (!list_empty(&handler->open_infos)) {
1343 r = EFI_ACCESS_DENIED;
1344 goto out;
1345 }
1346 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001347out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001348 return r;
1349}
1350
1351/**
1352 * efi_uninstall_protocol_interface() - uninstall protocol interface
1353 * @handle: handle from which the protocol shall be removed
1354 * @protocol: GUID of the protocol to be removed
1355 * @protocol_interface: interface to be removed
1356 *
1357 * This function implements the UninstallProtocolInterface service.
1358 *
1359 * See the Unified Extensible Firmware Interface (UEFI) specification for
1360 * details.
1361 *
1362 * Return: status code
1363 */
1364static efi_status_t EFIAPI efi_uninstall_protocol_interface
1365 (efi_handle_t handle, const efi_guid_t *protocol,
1366 void *protocol_interface)
1367{
1368 efi_status_t ret;
1369
1370 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1371
1372 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1373 if (ret != EFI_SUCCESS)
1374 goto out;
1375
1376 /* If the last protocol has been removed, delete the handle. */
1377 if (list_empty(&handle->protocols)) {
1378 list_del(&handle->link);
1379 free(handle);
1380 }
1381out:
1382 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001383}
1384
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001385/**
Mario Six78a88f72018-07-10 08:40:17 +02001386 * efi_register_protocol_notify() - register an event for notification when a
1387 * protocol is installed.
1388 * @protocol: GUID of the protocol whose installation shall be notified
1389 * @event: event to be signaled upon installation of the protocol
1390 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001391 *
1392 * This function implements the RegisterProtocolNotify service.
1393 * See the Unified Extensible Firmware Interface (UEFI) specification
1394 * for details.
1395 *
Mario Six78a88f72018-07-10 08:40:17 +02001396 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001397 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001398static efi_status_t EFIAPI efi_register_protocol_notify(
1399 const efi_guid_t *protocol,
1400 struct efi_event *event,
1401 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001402{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001403 struct efi_register_notify_event *item;
1404 efi_status_t ret = EFI_SUCCESS;
1405
Rob Clark778e6af2017-09-13 18:05:41 -04001406 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001407
1408 if (!protocol || !event || !registration) {
1409 ret = EFI_INVALID_PARAMETER;
1410 goto out;
1411 }
1412
1413 item = calloc(1, sizeof(struct efi_register_notify_event));
1414 if (!item) {
1415 ret = EFI_OUT_OF_RESOURCES;
1416 goto out;
1417 }
1418
1419 item->event = event;
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301420 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001421 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001422
1423 list_add_tail(&item->link, &efi_register_notify_events);
1424
1425 *registration = item;
1426out:
1427 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001428}
1429
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001430/**
Mario Six78a88f72018-07-10 08:40:17 +02001431 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001432 *
Mario Six78a88f72018-07-10 08:40:17 +02001433 * @search_type: selection criterion
1434 * @protocol: GUID of the protocol
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001435 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001436 *
1437 * See the documentation of the LocateHandle service in the UEFI specification.
1438 *
Mario Six78a88f72018-07-10 08:40:17 +02001439 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001440 */
Alexander Grafbee91162016-03-04 01:09:59 +01001441static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001442 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001443{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001444 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001445
1446 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001447 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001448 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001449 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001450 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001451 return (ret != EFI_SUCCESS);
1452 default:
1453 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001454 return -1;
1455 }
Alexander Grafbee91162016-03-04 01:09:59 +01001456}
1457
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001458/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001459 * efi_check_register_notify_event() - check if registration key is valid
1460 *
1461 * Check that a pointer is a valid registration key as returned by
1462 * RegisterProtocolNotify().
1463 *
1464 * @key: registration key
1465 * Return: valid registration key or NULL
1466 */
1467static struct efi_register_notify_event *efi_check_register_notify_event
1468 (void *key)
1469{
1470 struct efi_register_notify_event *event;
1471
1472 list_for_each_entry(event, &efi_register_notify_events, link) {
1473 if (event == (struct efi_register_notify_event *)key)
1474 return event;
1475 }
1476 return NULL;
1477}
1478
1479/**
Mario Six78a88f72018-07-10 08:40:17 +02001480 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001481 *
1482 * @search_type: selection criterion
1483 * @protocol: GUID of the protocol
1484 * @search_key: registration key
1485 * @buffer_size: size of the buffer to receive the handles in bytes
1486 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001487 *
1488 * This function is meant for U-Boot internal calls. For the API implementation
1489 * of the LocateHandle service see efi_locate_handle_ext.
1490 *
Mario Six78a88f72018-07-10 08:40:17 +02001491 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001492 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001493static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001494 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001495 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001496 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001497{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001498 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001499 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001500 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001501 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001502
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001503 /* Check parameters */
1504 switch (search_type) {
1505 case ALL_HANDLES:
1506 break;
1507 case BY_REGISTER_NOTIFY:
1508 if (!search_key)
1509 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001510 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001511 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001512 if (!event)
1513 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001514 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001515 case BY_PROTOCOL:
1516 if (!protocol)
1517 return EFI_INVALID_PARAMETER;
1518 break;
1519 default:
1520 return EFI_INVALID_PARAMETER;
1521 }
1522
Alexander Grafbee91162016-03-04 01:09:59 +01001523 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001524 if (search_type == BY_REGISTER_NOTIFY) {
1525 if (list_empty(&event->handles))
1526 return EFI_NOT_FOUND;
1527 handle = list_first_entry(&event->handles,
1528 struct efi_protocol_notification,
1529 link);
1530 efiobj = handle->handle;
1531 size += sizeof(void *);
1532 } else {
1533 list_for_each_entry(efiobj, &efi_obj_list, link) {
1534 if (!efi_search(search_type, protocol, efiobj))
1535 size += sizeof(void *);
1536 }
1537 if (size == 0)
1538 return EFI_NOT_FOUND;
Alexander Grafbee91162016-03-04 01:09:59 +01001539 }
1540
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001541 if (!buffer_size)
1542 return EFI_INVALID_PARAMETER;
1543
Alexander Grafbee91162016-03-04 01:09:59 +01001544 if (*buffer_size < size) {
1545 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001546 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001547 }
1548
Rob Clark796a78c2017-08-06 14:10:07 -04001549 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001550
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001551 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001552 if (!buffer)
1553 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001554
Alexander Grafbee91162016-03-04 01:09:59 +01001555 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001556 if (search_type == BY_REGISTER_NOTIFY) {
1557 *buffer = efiobj;
1558 list_del(&handle->link);
1559 } else {
1560 list_for_each_entry(efiobj, &efi_obj_list, link) {
1561 if (!efi_search(search_type, protocol, efiobj))
1562 *buffer++ = efiobj;
1563 }
Alexander Grafbee91162016-03-04 01:09:59 +01001564 }
1565
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001566 return EFI_SUCCESS;
1567}
1568
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001569/**
Mario Six78a88f72018-07-10 08:40:17 +02001570 * efi_locate_handle_ext() - locate handles implementing a protocol.
1571 * @search_type: selection criterion
1572 * @protocol: GUID of the protocol
1573 * @search_key: registration key
1574 * @buffer_size: size of the buffer to receive the handles in bytes
1575 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001576 *
1577 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001578 *
Mario Six78a88f72018-07-10 08:40:17 +02001579 * See the Unified Extensible Firmware Interface (UEFI) specification for
1580 * details.
1581 *
1582 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001583 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001584static efi_status_t EFIAPI efi_locate_handle_ext(
1585 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001586 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001587 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001588{
Rob Clark778e6af2017-09-13 18:05:41 -04001589 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001590 buffer_size, buffer);
1591
1592 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1593 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001594}
1595
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001596/**
Mario Six78a88f72018-07-10 08:40:17 +02001597 * efi_remove_configuration_table() - collapses configuration table entries,
1598 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001599 *
Mario Six78a88f72018-07-10 08:40:17 +02001600 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001601 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001602static void efi_remove_configuration_table(int i)
1603{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001604 struct efi_configuration_table *this = &systab.tables[i];
1605 struct efi_configuration_table *next = &systab.tables[i + 1];
1606 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001607
1608 memmove(this, next, (ulong)end - (ulong)next);
1609 systab.nr_tables--;
1610}
1611
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001612/**
Mario Six78a88f72018-07-10 08:40:17 +02001613 * efi_install_configuration_table() - adds, updates, or removes a
1614 * configuration table
1615 * @guid: GUID of the installed table
1616 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001617 *
1618 * This function is used for internal calls. For the API implementation of the
1619 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1620 *
Mario Six78a88f72018-07-10 08:40:17 +02001621 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001622 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001623efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1624 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001625{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001626 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001627 int i;
1628
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001629 if (!guid)
1630 return EFI_INVALID_PARAMETER;
1631
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001632 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001633 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001634 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001635 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001636 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001637 else
1638 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001639 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001640 }
1641 }
1642
Alexander Grafd98cdf62017-07-26 13:41:04 +02001643 if (!table)
1644 return EFI_NOT_FOUND;
1645
Alexander Grafbee91162016-03-04 01:09:59 +01001646 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001647 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001648 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001649
1650 /* Add a new entry */
Sughosh Ganu61e42d92019-12-29 00:01:04 +05301651 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001652 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001653 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001654
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001655out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001656 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001657 efi_update_table_header_crc32(&systab.hdr);
1658
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001659 /* Notify that the configuration table was changed */
1660 list_for_each_entry(evt, &efi_events, link) {
1661 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001662 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001663 break;
1664 }
1665 }
1666
Alexander Graf488bf122016-08-19 01:23:24 +02001667 return EFI_SUCCESS;
1668}
1669
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001670/**
Mario Six78a88f72018-07-10 08:40:17 +02001671 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1672 * configuration table.
1673 * @guid: GUID of the installed table
1674 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001675 *
1676 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001677 *
Mario Six78a88f72018-07-10 08:40:17 +02001678 * See the Unified Extensible Firmware Interface (UEFI) specification for
1679 * details.
1680 *
1681 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001682 */
Alexander Graf488bf122016-08-19 01:23:24 +02001683static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1684 void *table)
1685{
Rob Clark778e6af2017-09-13 18:05:41 -04001686 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001687 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001688}
1689
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001690/**
Mario Six78a88f72018-07-10 08:40:17 +02001691 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001692 *
1693 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001694 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001695 *
Heinrich Schuchardt6631ca52019-07-14 11:05:34 +02001696 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001697 * code is returned.
1698 *
1699 * @device_path: device path of the loaded image
1700 * @file_path: file path of the loaded image
1701 * @handle_ptr: handle of the loaded image
1702 * @info_ptr: loaded image protocol
1703 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001704 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001705efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1706 struct efi_device_path *file_path,
1707 struct efi_loaded_image_obj **handle_ptr,
1708 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001709{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001710 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001711 struct efi_loaded_image *info = NULL;
1712 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001713 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001714
1715 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1716 *handle_ptr = NULL;
1717 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001718
1719 info = calloc(1, sizeof(*info));
1720 if (!info)
1721 return EFI_OUT_OF_RESOURCES;
1722 obj = calloc(1, sizeof(*obj));
1723 if (!obj) {
1724 free(info);
1725 return EFI_OUT_OF_RESOURCES;
1726 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001727 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001728
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001729 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001730 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001731
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001732 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001733 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001734 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001735
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001736 if (device_path) {
1737 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001738
1739 dp = efi_dp_append(device_path, file_path);
1740 if (!dp) {
1741 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001742 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001743 }
1744 } else {
1745 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001746 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001747 ret = efi_add_protocol(&obj->header,
1748 &efi_guid_loaded_image_device_path, dp);
1749 if (ret != EFI_SUCCESS)
1750 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001751
1752 /*
1753 * When asking for the loaded_image interface, just
1754 * return handle which points to loaded_image_info
1755 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001756 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001757 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001758 if (ret != EFI_SUCCESS)
1759 goto failure;
1760
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001761 *info_ptr = info;
1762 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001763
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001764 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001765failure:
1766 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001767 efi_delete_handle(&obj->header);
1768 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001769 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001770}
1771
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001772/**
Mario Six78a88f72018-07-10 08:40:17 +02001773 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001774 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001775 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1776 * callers obligation to update the memory type as needed.
1777 *
1778 * @file_path: the path of the image to load
1779 * @buffer: buffer containing the loaded image
1780 * @size: size of the loaded image
1781 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001782 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001783static
Rob Clark9975fe92017-09-13 18:05:38 -04001784efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001785 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001786{
1787 struct efi_file_info *info = NULL;
1788 struct efi_file_handle *f;
1789 static efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001790 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001791 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001792
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001793 /* In case of failure nothing is returned */
1794 *buffer = NULL;
1795 *size = 0;
1796
1797 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001798 f = efi_file_from_path(file_path);
1799 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001800 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001801
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001802 /* Get file size */
Rob Clark838ee4b2017-09-13 18:05:35 -04001803 bs = 0;
1804 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1805 &bs, info));
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001806 if (ret != EFI_BUFFER_TOO_SMALL) {
1807 ret = EFI_DEVICE_ERROR;
1808 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001809 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001810
1811 info = malloc(bs);
1812 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1813 info));
Rob Clark838ee4b2017-09-13 18:05:35 -04001814 if (ret != EFI_SUCCESS)
1815 goto error;
1816
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001817 /*
1818 * When reading the file we do not yet know if it contains an
1819 * application, a boottime driver, or a runtime driver. So here we
1820 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1821 * update the reservation according to the image type.
1822 */
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001823 bs = info->file_size;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001824 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1825 EFI_BOOT_SERVICES_DATA,
1826 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001827 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001828 ret = EFI_OUT_OF_RESOURCES;
1829 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001830 }
1831
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001832 /* Read file */
1833 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1834 if (ret != EFI_SUCCESS)
1835 efi_free_pages(addr, efi_size_in_pages(bs));
1836 *buffer = (void *)(uintptr_t)addr;
1837 *size = bs;
1838error:
1839 EFI_CALL(f->close(f));
1840 free(info);
Rob Clark838ee4b2017-09-13 18:05:35 -04001841 return ret;
1842}
1843
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001844/**
Mario Six78a88f72018-07-10 08:40:17 +02001845 * efi_load_image() - load an EFI image into memory
1846 * @boot_policy: true for request originating from the boot manager
1847 * @parent_image: the caller's image handle
1848 * @file_path: the path of the image to load
1849 * @source_buffer: memory location from which the image is installed
1850 * @source_size: size of the memory area from which the image is installed
1851 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001852 *
1853 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001854 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001855 * See the Unified Extensible Firmware Interface (UEFI) specification
1856 * for details.
1857 *
Mario Six78a88f72018-07-10 08:40:17 +02001858 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001859 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09001860efi_status_t EFIAPI efi_load_image(bool boot_policy,
1861 efi_handle_t parent_image,
1862 struct efi_device_path *file_path,
1863 void *source_buffer,
1864 efi_uintn_t source_size,
1865 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001866{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001867 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04001868 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001869 struct efi_loaded_image_obj **image_obj =
1870 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001871 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001872 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01001873
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001874 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001875 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001876
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02001877 if (!image_handle || (!source_buffer && !file_path) ||
1878 !efi_search_obj(parent_image) ||
1879 /* The parent image handle must refer to a loaded image */
1880 !parent_image->type) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02001881 ret = EFI_INVALID_PARAMETER;
1882 goto error;
1883 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001884
Rob Clark838ee4b2017-09-13 18:05:35 -04001885 if (!source_buffer) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001886 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001887 &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001888 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001889 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001890 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001891 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04001892 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00001893 /* split file_path which contains both the device and file parts */
1894 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001895 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001896 if (ret == EFI_SUCCESS)
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09001897 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001898 if (!source_buffer)
1899 /* Release buffer to which file was loaded */
1900 efi_free_pages((uintptr_t)dest_buffer,
1901 efi_size_in_pages(source_size));
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09001902 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001903 info->system_table = &systab;
1904 info->parent_handle = parent_image;
1905 } else {
1906 /* The image is invalid. Release all associated resources. */
1907 efi_delete_handle(*image_handle);
1908 *image_handle = NULL;
1909 free(info);
1910 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001911error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001912 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001913}
1914
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001915/**
Alexander Graff31239a2018-11-15 21:23:47 +01001916 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1917 */
1918static void efi_exit_caches(void)
1919{
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02001920#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graff31239a2018-11-15 21:23:47 +01001921 /*
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02001922 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1923 * caches are enabled.
1924 *
1925 * TODO:
1926 * According to the UEFI spec caches that can be managed via CP15
1927 * operations should be enabled. Caches requiring platform information
1928 * to manage should be disabled. This should not happen in
1929 * ExitBootServices() but before invoking any UEFI binary is invoked.
1930 *
1931 * We want to keep the current workaround while GRUB prior to version
1932 * 2.04 is still in use.
Alexander Graff31239a2018-11-15 21:23:47 +01001933 */
Heinrich Schuchardt6f3badb2019-07-22 22:04:36 +02001934 cleanup_before_linux();
Alexander Graff31239a2018-11-15 21:23:47 +01001935#endif
1936}
1937
1938/**
Mario Six78a88f72018-07-10 08:40:17 +02001939 * efi_exit_boot_services() - stop all boot services
1940 * @image_handle: handle of the loaded image
1941 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001942 *
1943 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001944 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001945 * See the Unified Extensible Firmware Interface (UEFI) specification
1946 * for details.
1947 *
Mario Six78a88f72018-07-10 08:40:17 +02001948 * All timer events are disabled. For exit boot services events the
1949 * notification function is called. The boot services are disabled in the
1950 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001951 *
Mario Six78a88f72018-07-10 08:40:17 +02001952 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001953 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001954static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02001955 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01001956{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001957 struct efi_event *evt, *next_event;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001958 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001959
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02001960 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01001961
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001962 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001963 if (map_key != efi_memory_map_key) {
1964 ret = EFI_INVALID_PARAMETER;
1965 goto out;
1966 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001967
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001968 /* Check if ExitBootServices has already been called */
1969 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001970 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001971
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001972 /* Stop all timer related activities */
1973 timers_enabled = false;
1974
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001975 /* Add related events to the event group */
1976 list_for_each_entry(evt, &efi_events, link) {
1977 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1978 evt->group = &efi_guid_event_group_exit_boot_services;
1979 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001980 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001981 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001982 if (evt->group &&
1983 !guidcmp(evt->group,
1984 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001985 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001986 break;
1987 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001988 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001989
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001990 /* Make sure that notification functions are not called anymore */
1991 efi_tpl = TPL_HIGH_LEVEL;
1992
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001993 /* Notify variable services */
1994 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04001995
Heinrich Schuchardt14b40482019-07-11 20:15:09 +02001996 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1997 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1998 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1999 list_del(&evt->link);
2000 }
2001
Heinrich Schuchardtfccd3d92020-11-12 21:26:28 +01002002 if (!efi_st_keep_devices) {
2003 if IS_ENABLED(CONFIG_USB_DEVICE)
2004 udc_disconnect();
2005 board_quiesce_devices();
2006 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2007 }
Alexander Grafb7b84102016-11-17 01:02:57 +01002008
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02002009 /* Patch out unsupported runtime function */
2010 efi_runtime_detach();
2011
Alexander Graff31239a2018-11-15 21:23:47 +01002012 /* Fix up caches for EFI payloads if necessary */
2013 efi_exit_caches();
2014
Alexander Grafbee91162016-03-04 01:09:59 +01002015 /* This stops all lingering devices */
2016 bootm_disable_interrupts();
2017
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002018 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002019 systab.con_in_handle = NULL;
2020 systab.con_in = NULL;
2021 systab.con_out_handle = NULL;
2022 systab.con_out = NULL;
2023 systab.stderr_handle = NULL;
2024 systab.std_err = NULL;
2025 systab.boottime = NULL;
2026
2027 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02002028 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01002029
Alexander Grafbee91162016-03-04 01:09:59 +01002030 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002031 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01002032 WATCHDOG_RESET();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002033out:
2034 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002035}
2036
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002037/**
Mario Six78a88f72018-07-10 08:40:17 +02002038 * efi_get_next_monotonic_count() - get next value of the counter
2039 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002040 *
2041 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002042 *
Mario Six78a88f72018-07-10 08:40:17 +02002043 * See the Unified Extensible Firmware Interface (UEFI) specification for
2044 * details.
2045 *
2046 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002047 */
Alexander Grafbee91162016-03-04 01:09:59 +01002048static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2049{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002050 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002051 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002052
Alexander Grafbee91162016-03-04 01:09:59 +01002053 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002054 if (!count) {
2055 ret = EFI_INVALID_PARAMETER;
2056 goto out;
2057 }
Alexander Grafbee91162016-03-04 01:09:59 +01002058 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002059 ret = EFI_SUCCESS;
2060out:
2061 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002062}
2063
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002064/**
Mario Six78a88f72018-07-10 08:40:17 +02002065 * efi_stall() - sleep
2066 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002067 *
Mario Six78a88f72018-07-10 08:40:17 +02002068 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002069 *
Mario Six78a88f72018-07-10 08:40:17 +02002070 * See the Unified Extensible Firmware Interface (UEFI) specification for
2071 * details.
2072 *
2073 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002074 */
Alexander Grafbee91162016-03-04 01:09:59 +01002075static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2076{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002077 u64 end_tick;
2078
Alexander Grafbee91162016-03-04 01:09:59 +01002079 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002080
2081 end_tick = get_ticks() + usec_to_tick(microseconds);
2082 while (get_ticks() < end_tick)
2083 efi_timer_check();
2084
Alexander Grafbee91162016-03-04 01:09:59 +01002085 return EFI_EXIT(EFI_SUCCESS);
2086}
2087
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002088/**
Mario Six78a88f72018-07-10 08:40:17 +02002089 * efi_set_watchdog_timer() - reset the watchdog timer
2090 * @timeout: seconds before reset by watchdog
2091 * @watchdog_code: code to be logged when resetting
2092 * @data_size: size of buffer in bytes
2093 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002094 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002095 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002096 *
Mario Six78a88f72018-07-10 08:40:17 +02002097 * See the Unified Extensible Firmware Interface (UEFI) specification for
2098 * details.
2099 *
2100 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002101 */
Alexander Grafbee91162016-03-04 01:09:59 +01002102static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2103 uint64_t watchdog_code,
2104 unsigned long data_size,
2105 uint16_t *watchdog_data)
2106{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002107 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002108 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002109 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002110}
2111
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002112/**
Mario Six78a88f72018-07-10 08:40:17 +02002113 * efi_close_protocol() - close a protocol
2114 * @handle: handle on which the protocol shall be closed
2115 * @protocol: GUID of the protocol to close
2116 * @agent_handle: handle of the driver
2117 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002118 *
2119 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002120 *
Mario Six78a88f72018-07-10 08:40:17 +02002121 * See the Unified Extensible Firmware Interface (UEFI) specification for
2122 * details.
2123 *
2124 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002125 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002126efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2127 const efi_guid_t *protocol,
2128 efi_handle_t agent_handle,
2129 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002130{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002131 struct efi_handler *handler;
2132 struct efi_open_protocol_info_item *item;
2133 struct efi_open_protocol_info_item *pos;
2134 efi_status_t r;
2135
Rob Clark778e6af2017-09-13 18:05:41 -04002136 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002137 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002138
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02002139 if (!efi_search_obj(agent_handle) ||
2140 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002141 r = EFI_INVALID_PARAMETER;
2142 goto out;
2143 }
2144 r = efi_search_protocol(handle, protocol, &handler);
2145 if (r != EFI_SUCCESS)
2146 goto out;
2147
2148 r = EFI_NOT_FOUND;
2149 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2150 if (item->info.agent_handle == agent_handle &&
2151 item->info.controller_handle == controller_handle) {
2152 efi_delete_open_info(item);
2153 r = EFI_SUCCESS;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002154 }
2155 }
2156out:
2157 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002158}
2159
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002160/**
Mario Six78a88f72018-07-10 08:40:17 +02002161 * efi_open_protocol_information() - provide information about then open status
2162 * of a protocol on a handle
2163 * @handle: handle for which the information shall be retrieved
2164 * @protocol: GUID of the protocol
2165 * @entry_buffer: buffer to receive the open protocol information
2166 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002167 *
2168 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002169 *
Mario Six78a88f72018-07-10 08:40:17 +02002170 * See the Unified Extensible Firmware Interface (UEFI) specification for
2171 * details.
2172 *
2173 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002174 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002175static efi_status_t EFIAPI efi_open_protocol_information(
2176 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002177 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002178 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002179{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002180 unsigned long buffer_size;
2181 unsigned long count;
2182 struct efi_handler *handler;
2183 struct efi_open_protocol_info_item *item;
2184 efi_status_t r;
2185
Rob Clark778e6af2017-09-13 18:05:41 -04002186 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002187 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002188
2189 /* Check parameters */
2190 if (!entry_buffer) {
2191 r = EFI_INVALID_PARAMETER;
2192 goto out;
2193 }
2194 r = efi_search_protocol(handle, protocol, &handler);
2195 if (r != EFI_SUCCESS)
2196 goto out;
2197
2198 /* Count entries */
2199 count = 0;
2200 list_for_each_entry(item, &handler->open_infos, link) {
2201 if (item->info.open_count)
2202 ++count;
2203 }
2204 *entry_count = count;
2205 *entry_buffer = NULL;
2206 if (!count) {
2207 r = EFI_SUCCESS;
2208 goto out;
2209 }
2210
2211 /* Copy entries */
2212 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002213 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002214 (void **)entry_buffer);
2215 if (r != EFI_SUCCESS)
2216 goto out;
2217 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2218 if (item->info.open_count)
2219 (*entry_buffer)[--count] = item->info;
2220 }
2221out:
2222 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002223}
2224
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002225/**
Mario Six78a88f72018-07-10 08:40:17 +02002226 * efi_protocols_per_handle() - get protocols installed on a handle
2227 * @handle: handle for which the information is retrieved
2228 * @protocol_buffer: buffer with protocol GUIDs
2229 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002230 *
2231 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002232 *
Mario Six78a88f72018-07-10 08:40:17 +02002233 * See the Unified Extensible Firmware Interface (UEFI) specification for
2234 * details.
2235 *
2236 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002237 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002238static efi_status_t EFIAPI efi_protocols_per_handle(
2239 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002240 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002241{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002242 unsigned long buffer_size;
2243 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002244 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002245 efi_status_t r;
2246
Alexander Grafbee91162016-03-04 01:09:59 +01002247 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2248 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002249
2250 if (!handle || !protocol_buffer || !protocol_buffer_count)
2251 return EFI_EXIT(EFI_INVALID_PARAMETER);
2252
2253 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002254 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002255
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002256 efiobj = efi_search_obj(handle);
2257 if (!efiobj)
2258 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002259
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002260 /* Count protocols */
2261 list_for_each(protocol_handle, &efiobj->protocols) {
2262 ++*protocol_buffer_count;
2263 }
2264
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002265 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002266 if (*protocol_buffer_count) {
2267 size_t j = 0;
2268
2269 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002270 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002271 (void **)protocol_buffer);
2272 if (r != EFI_SUCCESS)
2273 return EFI_EXIT(r);
2274 list_for_each(protocol_handle, &efiobj->protocols) {
2275 struct efi_handler *protocol;
2276
2277 protocol = list_entry(protocol_handle,
2278 struct efi_handler, link);
2279 (*protocol_buffer)[j] = (void *)protocol->guid;
2280 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002281 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002282 }
2283
2284 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002285}
2286
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002287/**
Mario Six78a88f72018-07-10 08:40:17 +02002288 * efi_locate_handle_buffer() - locate handles implementing a protocol
2289 * @search_type: selection criterion
2290 * @protocol: GUID of the protocol
2291 * @search_key: registration key
2292 * @no_handles: number of returned handles
2293 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002294 *
2295 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002296 *
Mario Six78a88f72018-07-10 08:40:17 +02002297 * See the Unified Extensible Firmware Interface (UEFI) specification for
2298 * details.
2299 *
2300 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002301 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09002302efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafbee91162016-03-04 01:09:59 +01002303 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002304 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002305 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002306{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002307 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002308 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002309
Rob Clark778e6af2017-09-13 18:05:41 -04002310 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002311 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002312
2313 if (!no_handles || !buffer) {
2314 r = EFI_INVALID_PARAMETER;
2315 goto out;
2316 }
2317 *no_handles = 0;
2318 *buffer = NULL;
2319 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2320 *buffer);
2321 if (r != EFI_BUFFER_TOO_SMALL)
2322 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002323 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002324 (void **)buffer);
2325 if (r != EFI_SUCCESS)
2326 goto out;
2327 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2328 *buffer);
2329 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002330 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002331out:
2332 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002333}
2334
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002335/**
Mario Six78a88f72018-07-10 08:40:17 +02002336 * efi_locate_protocol() - find an interface implementing a protocol
2337 * @protocol: GUID of the protocol
2338 * @registration: registration key passed to the notification function
2339 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002340 *
2341 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002342 *
Mario Six78a88f72018-07-10 08:40:17 +02002343 * See the Unified Extensible Firmware Interface (UEFI) specification for
2344 * details.
2345 *
2346 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002347 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002348static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002349 void *registration,
2350 void **protocol_interface)
2351{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002352 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002353 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002354 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002355
Rob Clark778e6af2017-09-13 18:05:41 -04002356 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002357
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002358 /*
2359 * The UEFI spec explicitly requires a protocol even if a registration
2360 * key is provided. This differs from the logic in LocateHandle().
2361 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002362 if (!protocol || !protocol_interface)
2363 return EFI_EXIT(EFI_INVALID_PARAMETER);
2364
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002365 if (registration) {
2366 struct efi_register_notify_event *event;
2367 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002368
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002369 event = efi_check_register_notify_event(registration);
2370 if (!event)
2371 return EFI_EXIT(EFI_INVALID_PARAMETER);
2372 /*
2373 * The UEFI spec requires to return EFI_NOT_FOUND if no
2374 * protocol instance matches protocol and registration.
2375 * So let's do the same for a mismatch between protocol and
2376 * registration.
2377 */
2378 if (guidcmp(&event->protocol, protocol))
2379 goto not_found;
2380 if (list_empty(&event->handles))
2381 goto not_found;
2382 handle = list_first_entry(&event->handles,
2383 struct efi_protocol_notification,
2384 link);
2385 efiobj = handle->handle;
2386 list_del(&handle->link);
2387 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002388 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002389 if (ret == EFI_SUCCESS)
2390 goto found;
2391 } else {
2392 list_for_each_entry(efiobj, &efi_obj_list, link) {
2393 ret = efi_search_protocol(efiobj, protocol, &handler);
2394 if (ret == EFI_SUCCESS)
2395 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002396 }
2397 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002398not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002399 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002400 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002401found:
2402 *protocol_interface = handler->protocol_interface;
2403 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002404}
2405
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002406/**
Mario Six78a88f72018-07-10 08:40:17 +02002407 * efi_locate_device_path() - Get the device path and handle of an device
2408 * implementing a protocol
2409 * @protocol: GUID of the protocol
2410 * @device_path: device path
2411 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002412 *
2413 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002414 *
Mario Six78a88f72018-07-10 08:40:17 +02002415 * See the Unified Extensible Firmware Interface (UEFI) specification for
2416 * details.
2417 *
2418 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002419 */
2420static efi_status_t EFIAPI efi_locate_device_path(
2421 const efi_guid_t *protocol,
2422 struct efi_device_path **device_path,
2423 efi_handle_t *device)
2424{
2425 struct efi_device_path *dp;
2426 size_t i;
2427 struct efi_handler *handler;
2428 efi_handle_t *handles;
2429 size_t len, len_dp;
2430 size_t len_best = 0;
2431 efi_uintn_t no_handles;
2432 u8 *remainder;
2433 efi_status_t ret;
2434
2435 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2436
Heinrich Schuchardtab557142019-05-10 19:21:41 +02002437 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002438 ret = EFI_INVALID_PARAMETER;
2439 goto out;
2440 }
2441
2442 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002443 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002444
2445 /* Get all handles implementing the protocol */
2446 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2447 &no_handles, &handles));
2448 if (ret != EFI_SUCCESS)
2449 goto out;
2450
2451 for (i = 0; i < no_handles; ++i) {
2452 /* Find the device path protocol */
2453 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2454 &handler);
2455 if (ret != EFI_SUCCESS)
2456 continue;
2457 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002458 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002459 /*
2460 * This handle can only be a better fit
2461 * if its device path length is longer than the best fit and
2462 * if its device path length is shorter of equal the searched
2463 * device path.
2464 */
2465 if (len_dp <= len_best || len_dp > len)
2466 continue;
2467 /* Check if dp is a subpath of device_path */
2468 if (memcmp(*device_path, dp, len_dp))
2469 continue;
Heinrich Schuchardtab557142019-05-10 19:21:41 +02002470 if (!device) {
2471 ret = EFI_INVALID_PARAMETER;
2472 goto out;
2473 }
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002474 *device = handles[i];
2475 len_best = len_dp;
2476 }
2477 if (len_best) {
2478 remainder = (u8 *)*device_path + len_best;
2479 *device_path = (struct efi_device_path *)remainder;
2480 ret = EFI_SUCCESS;
2481 } else {
2482 ret = EFI_NOT_FOUND;
2483 }
2484out:
2485 return EFI_EXIT(ret);
2486}
2487
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002488/**
Mario Six78a88f72018-07-10 08:40:17 +02002489 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2490 * interfaces
2491 * @handle: handle on which the protocol interfaces shall be installed
2492 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2493 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002494 *
2495 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002496 *
Mario Six78a88f72018-07-10 08:40:17 +02002497 * See the Unified Extensible Firmware Interface (UEFI) specification for
2498 * details.
2499 *
2500 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002501 */
Heinrich Schuchardt7657ae12019-04-12 06:59:49 +02002502efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002503 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002504{
2505 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002506
Alexander Grafbeb077a2018-06-18 17:23:05 +02002507 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002508 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002509 void *protocol_interface;
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002510 efi_handle_t old_handle;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002511 efi_status_t r = EFI_SUCCESS;
2512 int i = 0;
2513
2514 if (!handle)
2515 return EFI_EXIT(EFI_INVALID_PARAMETER);
2516
Alexander Grafbeb077a2018-06-18 17:23:05 +02002517 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002518 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002519 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002520 if (!protocol)
2521 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002522 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002523 /* Check that a device path has not been installed before */
2524 if (!guidcmp(protocol, &efi_guid_device_path)) {
2525 struct efi_device_path *dp = protocol_interface;
2526
2527 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2528 &old_handle));
Heinrich Schuchardt20562892019-05-20 19:55:18 +00002529 if (r == EFI_SUCCESS &&
2530 dp->type == DEVICE_PATH_TYPE_END) {
2531 EFI_PRINT("Path %pD already installed\n",
2532 protocol_interface);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002533 r = EFI_ALREADY_STARTED;
2534 break;
2535 }
2536 }
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002537 r = EFI_CALL(efi_install_protocol_interface(
2538 handle, protocol,
2539 EFI_NATIVE_INTERFACE,
2540 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002541 if (r != EFI_SUCCESS)
2542 break;
2543 i++;
2544 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002545 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002546 if (r == EFI_SUCCESS)
2547 return EFI_EXIT(r);
2548
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002549 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002550 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002551 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002552 protocol = efi_va_arg(argptr, efi_guid_t*);
2553 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002554 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002555 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002556 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002557 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002558
2559 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002560}
2561
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002562/**
Mario Six78a88f72018-07-10 08:40:17 +02002563 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2564 * interfaces
2565 * @handle: handle from which the protocol interfaces shall be removed
2566 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2567 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002568 *
2569 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002570 *
Mario Six78a88f72018-07-10 08:40:17 +02002571 * See the Unified Extensible Firmware Interface (UEFI) specification for
2572 * details.
2573 *
2574 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002575 */
Alexander Grafbee91162016-03-04 01:09:59 +01002576static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002577 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002578{
2579 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002580
Alexander Grafbeb077a2018-06-18 17:23:05 +02002581 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002582 const efi_guid_t *protocol;
2583 void *protocol_interface;
2584 efi_status_t r = EFI_SUCCESS;
2585 size_t i = 0;
2586
2587 if (!handle)
2588 return EFI_EXIT(EFI_INVALID_PARAMETER);
2589
Alexander Grafbeb077a2018-06-18 17:23:05 +02002590 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002591 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002592 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002593 if (!protocol)
2594 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002595 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002596 r = efi_uninstall_protocol(handle, protocol,
2597 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002598 if (r != EFI_SUCCESS)
2599 break;
2600 i++;
2601 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002602 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002603 if (r == EFI_SUCCESS) {
2604 /* If the last protocol has been removed, delete the handle. */
2605 if (list_empty(&handle->protocols)) {
2606 list_del(&handle->link);
2607 free(handle);
2608 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002609 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002610 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002611
2612 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002613 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002614 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002615 protocol = efi_va_arg(argptr, efi_guid_t*);
2616 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002617 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2618 EFI_NATIVE_INTERFACE,
2619 protocol_interface));
2620 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002621 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002622
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002623 /* In case of an error always return EFI_INVALID_PARAMETER */
2624 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002625}
2626
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002627/**
Mario Six78a88f72018-07-10 08:40:17 +02002628 * efi_calculate_crc32() - calculate cyclic redundancy code
2629 * @data: buffer with data
2630 * @data_size: size of buffer in bytes
2631 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002632 *
2633 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002634 *
Mario Six78a88f72018-07-10 08:40:17 +02002635 * See the Unified Extensible Firmware Interface (UEFI) specification for
2636 * details.
2637 *
2638 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002639 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002640static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2641 efi_uintn_t data_size,
2642 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002643{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002644 efi_status_t ret = EFI_SUCCESS;
2645
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002646 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002647 if (!data || !data_size || !crc32_p) {
2648 ret = EFI_INVALID_PARAMETER;
2649 goto out;
2650 }
Alexander Grafbee91162016-03-04 01:09:59 +01002651 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002652out:
2653 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002654}
2655
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002656/**
Mario Six78a88f72018-07-10 08:40:17 +02002657 * efi_copy_mem() - copy memory
2658 * @destination: destination of the copy operation
2659 * @source: source of the copy operation
2660 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002661 *
2662 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002663 *
Mario Six78a88f72018-07-10 08:40:17 +02002664 * See the Unified Extensible Firmware Interface (UEFI) specification for
2665 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002666 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002667static void EFIAPI efi_copy_mem(void *destination, const void *source,
2668 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002669{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002670 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002671 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002672 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002673}
2674
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002675/**
Mario Six78a88f72018-07-10 08:40:17 +02002676 * efi_set_mem() - Fill memory with a byte value.
2677 * @buffer: buffer to fill
2678 * @size: size of buffer in bytes
2679 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002680 *
2681 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002682 *
Mario Six78a88f72018-07-10 08:40:17 +02002683 * See the Unified Extensible Firmware Interface (UEFI) specification for
2684 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002685 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002686static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002687{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002688 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002689 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002690 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002691}
2692
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002693/**
Mario Six78a88f72018-07-10 08:40:17 +02002694 * efi_protocol_open() - open protocol interface on a handle
2695 * @handler: handler of a protocol
2696 * @protocol_interface: interface implementing the protocol
2697 * @agent_handle: handle of the driver
2698 * @controller_handle: handle of the controller
2699 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002700 *
Mario Six78a88f72018-07-10 08:40:17 +02002701 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002702 */
2703static efi_status_t efi_protocol_open(
2704 struct efi_handler *handler,
2705 void **protocol_interface, void *agent_handle,
2706 void *controller_handle, uint32_t attributes)
2707{
2708 struct efi_open_protocol_info_item *item;
2709 struct efi_open_protocol_info_entry *match = NULL;
2710 bool opened_by_driver = false;
2711 bool opened_exclusive = false;
2712
2713 /* If there is no agent, only return the interface */
2714 if (!agent_handle)
2715 goto out;
2716
2717 /* For TEST_PROTOCOL ignore interface attribute */
2718 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2719 *protocol_interface = NULL;
2720
2721 /*
2722 * Check if the protocol is already opened by a driver with the same
2723 * attributes or opened exclusively
2724 */
2725 list_for_each_entry(item, &handler->open_infos, link) {
2726 if (item->info.agent_handle == agent_handle) {
2727 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2728 (item->info.attributes == attributes))
2729 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002730 } else {
2731 if (item->info.attributes &
2732 EFI_OPEN_PROTOCOL_BY_DRIVER)
2733 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002734 }
2735 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2736 opened_exclusive = true;
2737 }
2738
2739 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002740 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2741 if (opened_exclusive)
2742 return EFI_ACCESS_DENIED;
2743 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2744 if (opened_exclusive || opened_by_driver)
2745 return EFI_ACCESS_DENIED;
2746 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002747
2748 /* Prepare exclusive opening */
2749 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2750 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002751disconnect_next:
2752 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002753 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002754 efi_status_t ret;
2755
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002756 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002757 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2758 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002759 item->info.controller_handle,
2760 item->info.agent_handle,
2761 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002762 if (ret == EFI_SUCCESS)
2763 /*
2764 * Child controllers may have been
2765 * removed from the open_infos list. So
2766 * let's restart the loop.
2767 */
2768 goto disconnect_next;
2769 else
2770 opened_by_driver = true;
2771 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002772 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002773 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002774 if (opened_by_driver)
2775 return EFI_ACCESS_DENIED;
2776 }
2777
2778 /* Find existing entry */
2779 list_for_each_entry(item, &handler->open_infos, link) {
2780 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02002781 item->info.controller_handle == controller_handle &&
2782 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002783 match = &item->info;
2784 }
2785 /* None found, create one */
2786 if (!match) {
2787 match = efi_create_open_info(handler);
2788 if (!match)
2789 return EFI_OUT_OF_RESOURCES;
2790 }
2791
2792 match->agent_handle = agent_handle;
2793 match->controller_handle = controller_handle;
2794 match->attributes = attributes;
2795 match->open_count++;
2796
2797out:
2798 /* For TEST_PROTOCOL ignore interface attribute. */
2799 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2800 *protocol_interface = handler->protocol_interface;
2801
2802 return EFI_SUCCESS;
2803}
2804
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002805/**
Mario Six78a88f72018-07-10 08:40:17 +02002806 * efi_open_protocol() - open protocol interface on a handle
2807 * @handle: handle on which the protocol shall be opened
2808 * @protocol: GUID of the protocol
2809 * @protocol_interface: interface implementing the protocol
2810 * @agent_handle: handle of the driver
2811 * @controller_handle: handle of the controller
2812 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002813 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002814 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002815 *
Mario Six78a88f72018-07-10 08:40:17 +02002816 * See the Unified Extensible Firmware Interface (UEFI) specification for
2817 * details.
2818 *
2819 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002820 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002821static efi_status_t EFIAPI efi_open_protocol
2822 (efi_handle_t handle, const efi_guid_t *protocol,
2823 void **protocol_interface, efi_handle_t agent_handle,
2824 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002825{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002826 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002827 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002828
Rob Clark778e6af2017-09-13 18:05:41 -04002829 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002830 protocol_interface, agent_handle, controller_handle,
2831 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002832
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002833 if (!handle || !protocol ||
2834 (!protocol_interface && attributes !=
2835 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2836 goto out;
2837 }
2838
2839 switch (attributes) {
2840 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2841 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2842 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2843 break;
2844 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2845 if (controller_handle == handle)
2846 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002847 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002848 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2849 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002850 /* Check that the controller handle is valid */
2851 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002852 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002853 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002854 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002855 /* Check that the agent handle is valid */
2856 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002857 goto out;
2858 break;
2859 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002860 goto out;
2861 }
2862
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002863 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002864 switch (r) {
2865 case EFI_SUCCESS:
2866 break;
2867 case EFI_NOT_FOUND:
2868 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002869 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002870 default:
2871 goto out;
2872 }
Alexander Grafbee91162016-03-04 01:09:59 +01002873
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002874 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2875 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002876out:
2877 return EFI_EXIT(r);
2878}
2879
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002880/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002881 * efi_start_image() - call the entry point of an image
2882 * @image_handle: handle of the image
2883 * @exit_data_size: size of the buffer
2884 * @exit_data: buffer to receive the exit data of the called image
2885 *
2886 * This function implements the StartImage service.
2887 *
2888 * See the Unified Extensible Firmware Interface (UEFI) specification for
2889 * details.
2890 *
2891 * Return: status code
2892 */
2893efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2894 efi_uintn_t *exit_data_size,
2895 u16 **exit_data)
2896{
2897 struct efi_loaded_image_obj *image_obj =
2898 (struct efi_loaded_image_obj *)image_handle;
2899 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002900 void *info;
2901 efi_handle_t parent_image = current_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002902
2903 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2904
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002905 if (!efi_search_obj(image_handle))
2906 return EFI_EXIT(EFI_INVALID_PARAMETER);
2907
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002908 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02002909 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2910 return EFI_EXIT(EFI_INVALID_PARAMETER);
2911
AKASHI Takahiro4540dab2020-04-14 11:51:44 +09002912 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2913 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2914
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002915 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2916 &info, NULL, NULL,
2917 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2918 if (ret != EFI_SUCCESS)
2919 return EFI_EXIT(EFI_INVALID_PARAMETER);
2920
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02002921 image_obj->exit_data_size = exit_data_size;
2922 image_obj->exit_data = exit_data;
2923
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002924 /* call the image! */
2925 if (setjmp(&image_obj->exit_jmp)) {
2926 /*
2927 * We called the entry point of the child image with EFI_CALL
2928 * in the lines below. The child image called the Exit() boot
2929 * service efi_exit() which executed the long jump that brought
2930 * us to the current line. This implies that the second half
2931 * of the EFI_CALL macro has not been executed.
2932 */
Heinrich Schuchardtd68d7f42020-09-10 12:22:54 +02002933#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002934 /*
2935 * efi_exit() called efi_restore_gd(). We have to undo this
2936 * otherwise __efi_entry_check() will put the wrong value into
2937 * app_gd.
2938 */
Heinrich Schuchardt4f7dc5f2020-05-27 01:58:30 +02002939 set_gd(app_gd);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002940#endif
2941 /*
2942 * To get ready to call EFI_EXIT below we have to execute the
2943 * missed out steps of EFI_CALL.
2944 */
2945 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02002946 EFI_PRINT("%lu returned by started image\n",
2947 (unsigned long)((uintptr_t)image_obj->exit_status &
2948 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002949 current_image = parent_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002950 return EFI_EXIT(image_obj->exit_status);
2951 }
2952
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002953 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02002954 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09002955 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002956 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2957
2958 /*
Heinrich Schuchardt55111c52020-01-10 22:06:54 +01002959 * Control is returned from a started UEFI image either by calling
2960 * Exit() (where exit data can be provided) or by simply returning from
2961 * the entry point. In the latter case call Exit() on behalf of the
2962 * image.
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002963 */
2964 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2965}
2966
2967/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002968 * efi_delete_image() - delete loaded image from memory)
2969 *
2970 * @image_obj: handle of the loaded image
2971 * @loaded_image_protocol: loaded image protocol
2972 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02002973static efi_status_t efi_delete_image
2974 (struct efi_loaded_image_obj *image_obj,
2975 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002976{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02002977 struct efi_object *efiobj;
2978 efi_status_t r, ret = EFI_SUCCESS;
2979
2980close_next:
2981 list_for_each_entry(efiobj, &efi_obj_list, link) {
2982 struct efi_handler *protocol;
2983
2984 list_for_each_entry(protocol, &efiobj->protocols, link) {
2985 struct efi_open_protocol_info_item *info;
2986
2987 list_for_each_entry(info, &protocol->open_infos, link) {
2988 if (info->info.agent_handle !=
2989 (efi_handle_t)image_obj)
2990 continue;
2991 r = EFI_CALL(efi_close_protocol
2992 (efiobj, protocol->guid,
2993 info->info.agent_handle,
2994 info->info.controller_handle
2995 ));
2996 if (r != EFI_SUCCESS)
2997 ret = r;
2998 /*
2999 * Closing protocols may results in further
3000 * items being deleted. To play it safe loop
3001 * over all elements again.
3002 */
3003 goto close_next;
3004 }
3005 }
3006 }
3007
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003008 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3009 efi_size_in_pages(loaded_image_protocol->image_size));
3010 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02003011
3012 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003013}
3014
3015/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003016 * efi_unload_image() - unload an EFI image
3017 * @image_handle: handle of the image to be unloaded
3018 *
3019 * This function implements the UnloadImage service.
3020 *
3021 * See the Unified Extensible Firmware Interface (UEFI) specification for
3022 * details.
3023 *
3024 * Return: status code
3025 */
3026efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3027{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003028 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003029 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003030 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003031
3032 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003033
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02003034 efiobj = efi_search_obj(image_handle);
3035 if (!efiobj) {
3036 ret = EFI_INVALID_PARAMETER;
3037 goto out;
3038 }
3039 /* Find the loaded image protocol */
3040 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3041 (void **)&loaded_image_protocol,
3042 NULL, NULL,
3043 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3044 if (ret != EFI_SUCCESS) {
3045 ret = EFI_INVALID_PARAMETER;
3046 goto out;
3047 }
3048 switch (efiobj->type) {
3049 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3050 /* Call the unload function */
3051 if (!loaded_image_protocol->unload) {
3052 ret = EFI_UNSUPPORTED;
3053 goto out;
3054 }
3055 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3056 if (ret != EFI_SUCCESS)
3057 goto out;
3058 break;
3059 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3060 break;
3061 default:
3062 ret = EFI_INVALID_PARAMETER;
3063 goto out;
3064 }
3065 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3066 loaded_image_protocol);
3067out:
3068 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003069}
3070
3071/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003072 * efi_update_exit_data() - fill exit data parameters of StartImage()
3073 *
Heinrich Schuchardt9ce91272019-07-14 11:25:06 +02003074 * @image_obj: image handle
3075 * @exit_data_size: size of the exit data buffer
3076 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003077 * Return: status code
3078 */
3079static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3080 efi_uintn_t exit_data_size,
3081 u16 *exit_data)
3082{
3083 efi_status_t ret;
3084
3085 /*
3086 * If exit_data is not provided to StartImage(), exit_data_size must be
3087 * ignored.
3088 */
3089 if (!image_obj->exit_data)
3090 return EFI_SUCCESS;
3091 if (image_obj->exit_data_size)
3092 *image_obj->exit_data_size = exit_data_size;
3093 if (exit_data_size && exit_data) {
3094 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3095 exit_data_size,
3096 (void **)image_obj->exit_data);
3097 if (ret != EFI_SUCCESS)
3098 return ret;
3099 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3100 } else {
3101 image_obj->exit_data = NULL;
3102 }
3103 return EFI_SUCCESS;
3104}
3105
3106/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003107 * efi_exit() - leave an EFI application or driver
3108 * @image_handle: handle of the application or driver that is exiting
3109 * @exit_status: status code
3110 * @exit_data_size: size of the buffer in bytes
3111 * @exit_data: buffer with data describing an error
3112 *
3113 * This function implements the Exit service.
3114 *
3115 * See the Unified Extensible Firmware Interface (UEFI) specification for
3116 * details.
3117 *
3118 * Return: status code
3119 */
3120static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3121 efi_status_t exit_status,
3122 efi_uintn_t exit_data_size,
3123 u16 *exit_data)
3124{
3125 /*
3126 * TODO: We should call the unload procedure of the loaded
3127 * image protocol.
3128 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003129 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003130 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003131 struct efi_loaded_image_obj *image_obj =
3132 (struct efi_loaded_image_obj *)image_handle;
3133
3134 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3135 exit_data_size, exit_data);
3136
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003137 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003138 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003139 (void **)&loaded_image_protocol,
3140 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003141 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003142 if (ret != EFI_SUCCESS) {
3143 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003144 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003145 }
3146
3147 /* Unloading of unstarted images */
3148 switch (image_obj->header.type) {
3149 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3150 break;
3151 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3152 efi_delete_image(image_obj, loaded_image_protocol);
3153 ret = EFI_SUCCESS;
3154 goto out;
3155 default:
3156 /* Handle does not refer to loaded image */
3157 ret = EFI_INVALID_PARAMETER;
3158 goto out;
3159 }
3160 /* A started image can only be unloaded it is the last one started. */
3161 if (image_handle != current_image) {
3162 ret = EFI_INVALID_PARAMETER;
3163 goto out;
3164 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003165
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003166 /* Exit data is only foreseen in case of failure. */
3167 if (exit_status != EFI_SUCCESS) {
3168 ret = efi_update_exit_data(image_obj, exit_data_size,
3169 exit_data);
3170 /* Exiting has priority. Don't return error to caller. */
3171 if (ret != EFI_SUCCESS)
3172 EFI_PRINT("%s: out of memory\n", __func__);
3173 }
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003174 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3175 exit_status != EFI_SUCCESS)
3176 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003177
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003178 /* Make sure entry/exit counts for EFI world cross-overs match */
3179 EFI_EXIT(exit_status);
3180
3181 /*
3182 * But longjmp out with the U-Boot gd, not the application's, as
3183 * the other end is a setjmp call inside EFI context.
3184 */
3185 efi_restore_gd();
3186
3187 image_obj->exit_status = exit_status;
3188 longjmp(&image_obj->exit_jmp, 1);
3189
3190 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003191out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003192 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003193}
3194
3195/**
Mario Six78a88f72018-07-10 08:40:17 +02003196 * efi_handle_protocol() - get interface of a protocol on a handle
3197 * @handle: handle on which the protocol shall be opened
3198 * @protocol: GUID of the protocol
3199 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003200 *
3201 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003202 *
Mario Six78a88f72018-07-10 08:40:17 +02003203 * See the Unified Extensible Firmware Interface (UEFI) specification for
3204 * details.
3205 *
3206 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003207 */
AKASHI Takahirob51ec632020-03-17 11:12:36 +09003208efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3209 const efi_guid_t *protocol,
3210 void **protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01003211{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003212 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003213 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003214}
3215
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003216/**
Mario Six78a88f72018-07-10 08:40:17 +02003217 * efi_bind_controller() - bind a single driver to a controller
3218 * @controller_handle: controller handle
3219 * @driver_image_handle: driver handle
3220 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003221 *
Mario Six78a88f72018-07-10 08:40:17 +02003222 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003223 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003224static efi_status_t efi_bind_controller(
3225 efi_handle_t controller_handle,
3226 efi_handle_t driver_image_handle,
3227 struct efi_device_path *remain_device_path)
3228{
3229 struct efi_driver_binding_protocol *binding_protocol;
3230 efi_status_t r;
3231
3232 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3233 &efi_guid_driver_binding_protocol,
3234 (void **)&binding_protocol,
3235 driver_image_handle, NULL,
3236 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3237 if (r != EFI_SUCCESS)
3238 return r;
3239 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3240 controller_handle,
3241 remain_device_path));
3242 if (r == EFI_SUCCESS)
3243 r = EFI_CALL(binding_protocol->start(binding_protocol,
3244 controller_handle,
3245 remain_device_path));
3246 EFI_CALL(efi_close_protocol(driver_image_handle,
3247 &efi_guid_driver_binding_protocol,
3248 driver_image_handle, NULL));
3249 return r;
3250}
3251
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003252/**
Mario Six78a88f72018-07-10 08:40:17 +02003253 * efi_connect_single_controller() - connect a single driver to a controller
3254 * @controller_handle: controller
3255 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003256 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003257 *
Mario Six78a88f72018-07-10 08:40:17 +02003258 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003259 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003260static efi_status_t efi_connect_single_controller(
3261 efi_handle_t controller_handle,
3262 efi_handle_t *driver_image_handle,
3263 struct efi_device_path *remain_device_path)
3264{
3265 efi_handle_t *buffer;
3266 size_t count;
3267 size_t i;
3268 efi_status_t r;
3269 size_t connected = 0;
3270
3271 /* Get buffer with all handles with driver binding protocol */
3272 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3273 &efi_guid_driver_binding_protocol,
3274 NULL, &count, &buffer));
3275 if (r != EFI_SUCCESS)
3276 return r;
3277
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003278 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003279 if (driver_image_handle) {
3280 for (; *driver_image_handle; ++driver_image_handle) {
3281 for (i = 0; i < count; ++i) {
3282 if (buffer[i] == *driver_image_handle) {
3283 buffer[i] = NULL;
3284 r = efi_bind_controller(
3285 controller_handle,
3286 *driver_image_handle,
3287 remain_device_path);
3288 /*
3289 * For drivers that do not support the
3290 * controller or are already connected
3291 * we receive an error code here.
3292 */
3293 if (r == EFI_SUCCESS)
3294 ++connected;
3295 }
3296 }
3297 }
3298 }
3299
3300 /*
3301 * TODO: Some overrides are not yet implemented:
3302 * - Platform Driver Override
3303 * - Driver Family Override Search
3304 * - Bus Specific Driver Override
3305 */
3306
3307 /* Driver Binding Search */
3308 for (i = 0; i < count; ++i) {
3309 if (buffer[i]) {
3310 r = efi_bind_controller(controller_handle,
3311 buffer[i],
3312 remain_device_path);
3313 if (r == EFI_SUCCESS)
3314 ++connected;
3315 }
3316 }
3317
3318 efi_free_pool(buffer);
3319 if (!connected)
3320 return EFI_NOT_FOUND;
3321 return EFI_SUCCESS;
3322}
3323
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003324/**
Mario Six78a88f72018-07-10 08:40:17 +02003325 * efi_connect_controller() - connect a controller to a driver
3326 * @controller_handle: handle of the controller
3327 * @driver_image_handle: handle of the driver
3328 * @remain_device_path: device path of a child controller
3329 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003330 *
3331 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003332 *
3333 * See the Unified Extensible Firmware Interface (UEFI) specification for
3334 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003335 *
3336 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003337 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003338 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3339 *
Mario Six78a88f72018-07-10 08:40:17 +02003340 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003341 */
3342static efi_status_t EFIAPI efi_connect_controller(
3343 efi_handle_t controller_handle,
3344 efi_handle_t *driver_image_handle,
3345 struct efi_device_path *remain_device_path,
3346 bool recursive)
3347{
3348 efi_status_t r;
3349 efi_status_t ret = EFI_NOT_FOUND;
3350 struct efi_object *efiobj;
3351
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003352 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003353 remain_device_path, recursive);
3354
3355 efiobj = efi_search_obj(controller_handle);
3356 if (!efiobj) {
3357 ret = EFI_INVALID_PARAMETER;
3358 goto out;
3359 }
3360
3361 r = efi_connect_single_controller(controller_handle,
3362 driver_image_handle,
3363 remain_device_path);
3364 if (r == EFI_SUCCESS)
3365 ret = EFI_SUCCESS;
3366 if (recursive) {
3367 struct efi_handler *handler;
3368 struct efi_open_protocol_info_item *item;
3369
3370 list_for_each_entry(handler, &efiobj->protocols, link) {
3371 list_for_each_entry(item, &handler->open_infos, link) {
3372 if (item->info.attributes &
3373 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3374 r = EFI_CALL(efi_connect_controller(
3375 item->info.controller_handle,
3376 driver_image_handle,
3377 remain_device_path,
3378 recursive));
3379 if (r == EFI_SUCCESS)
3380 ret = EFI_SUCCESS;
3381 }
3382 }
3383 }
3384 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003385 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003386 if (ret != EFI_SUCCESS && remain_device_path &&
3387 remain_device_path->type == DEVICE_PATH_TYPE_END)
3388 ret = EFI_SUCCESS;
3389out:
3390 return EFI_EXIT(ret);
3391}
3392
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003393/**
Mario Six78a88f72018-07-10 08:40:17 +02003394 * efi_reinstall_protocol_interface() - reinstall protocol interface
3395 * @handle: handle on which the protocol shall be reinstalled
3396 * @protocol: GUID of the protocol to be installed
3397 * @old_interface: interface to be removed
3398 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003399 *
3400 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003401 *
3402 * See the Unified Extensible Firmware Interface (UEFI) specification for
3403 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003404 *
3405 * The old interface is uninstalled. The new interface is installed.
3406 * Drivers are connected.
3407 *
Mario Six78a88f72018-07-10 08:40:17 +02003408 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003409 */
3410static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3411 efi_handle_t handle, const efi_guid_t *protocol,
3412 void *old_interface, void *new_interface)
3413{
3414 efi_status_t ret;
3415
3416 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3417 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003418
3419 /* Uninstall protocol but do not delete handle */
3420 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003421 if (ret != EFI_SUCCESS)
3422 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003423
3424 /* Install the new protocol */
3425 ret = efi_add_protocol(handle, protocol, new_interface);
3426 /*
3427 * The UEFI spec does not specify what should happen to the handle
3428 * if in case of an error no protocol interface remains on the handle.
3429 * So let's do nothing here.
3430 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003431 if (ret != EFI_SUCCESS)
3432 goto out;
3433 /*
3434 * The returned status code has to be ignored.
3435 * Do not create an error if no suitable driver for the handle exists.
3436 */
3437 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3438out:
3439 return EFI_EXIT(ret);
3440}
3441
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003442/**
Mario Six78a88f72018-07-10 08:40:17 +02003443 * efi_get_child_controllers() - get all child controllers associated to a driver
3444 * @efiobj: handle of the controller
3445 * @driver_handle: handle of the driver
3446 * @number_of_children: number of child controllers
3447 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003448 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003449 * The allocated buffer has to be freed with free().
3450 *
Mario Six78a88f72018-07-10 08:40:17 +02003451 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003452 */
3453static efi_status_t efi_get_child_controllers(
3454 struct efi_object *efiobj,
3455 efi_handle_t driver_handle,
3456 efi_uintn_t *number_of_children,
3457 efi_handle_t **child_handle_buffer)
3458{
3459 struct efi_handler *handler;
3460 struct efi_open_protocol_info_item *item;
3461 efi_uintn_t count = 0, i;
3462 bool duplicate;
3463
3464 /* Count all child controller associations */
3465 list_for_each_entry(handler, &efiobj->protocols, link) {
3466 list_for_each_entry(item, &handler->open_infos, link) {
3467 if (item->info.agent_handle == driver_handle &&
3468 item->info.attributes &
3469 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3470 ++count;
3471 }
3472 }
3473 /*
3474 * Create buffer. In case of duplicate child controller assignments
3475 * the buffer will be too large. But that does not harm.
3476 */
3477 *number_of_children = 0;
Heinrich Schuchardt1047c6e2020-07-07 04:21:26 +02003478 if (!count)
3479 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003480 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3481 if (!*child_handle_buffer)
3482 return EFI_OUT_OF_RESOURCES;
3483 /* Copy unique child handles */
3484 list_for_each_entry(handler, &efiobj->protocols, link) {
3485 list_for_each_entry(item, &handler->open_infos, link) {
3486 if (item->info.agent_handle == driver_handle &&
3487 item->info.attributes &
3488 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3489 /* Check this is a new child controller */
3490 duplicate = false;
3491 for (i = 0; i < *number_of_children; ++i) {
3492 if ((*child_handle_buffer)[i] ==
3493 item->info.controller_handle)
3494 duplicate = true;
3495 }
3496 /* Copy handle to buffer */
3497 if (!duplicate) {
3498 i = (*number_of_children)++;
3499 (*child_handle_buffer)[i] =
3500 item->info.controller_handle;
3501 }
3502 }
3503 }
3504 }
3505 return EFI_SUCCESS;
3506}
3507
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003508/**
Mario Six78a88f72018-07-10 08:40:17 +02003509 * efi_disconnect_controller() - disconnect a controller from a driver
3510 * @controller_handle: handle of the controller
3511 * @driver_image_handle: handle of the driver
3512 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003513 *
3514 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003515 *
Mario Six78a88f72018-07-10 08:40:17 +02003516 * See the Unified Extensible Firmware Interface (UEFI) specification for
3517 * details.
3518 *
3519 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003520 */
3521static efi_status_t EFIAPI efi_disconnect_controller(
3522 efi_handle_t controller_handle,
3523 efi_handle_t driver_image_handle,
3524 efi_handle_t child_handle)
3525{
3526 struct efi_driver_binding_protocol *binding_protocol;
3527 efi_handle_t *child_handle_buffer = NULL;
3528 size_t number_of_children = 0;
3529 efi_status_t r;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003530 struct efi_object *efiobj;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003531 bool sole_child;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003532
3533 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3534 child_handle);
3535
3536 efiobj = efi_search_obj(controller_handle);
3537 if (!efiobj) {
3538 r = EFI_INVALID_PARAMETER;
3539 goto out;
3540 }
3541
3542 if (child_handle && !efi_search_obj(child_handle)) {
3543 r = EFI_INVALID_PARAMETER;
3544 goto out;
3545 }
3546
3547 /* If no driver handle is supplied, disconnect all drivers */
3548 if (!driver_image_handle) {
3549 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3550 goto out;
3551 }
3552
3553 /* Create list of child handles */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003554 r = efi_get_child_controllers(efiobj,
3555 driver_image_handle,
3556 &number_of_children,
3557 &child_handle_buffer);
3558 if (r != EFI_SUCCESS)
3559 return r;
3560 sole_child = (number_of_children == 1);
3561
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003562 if (child_handle) {
3563 number_of_children = 1;
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003564 free(child_handle_buffer);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003565 child_handle_buffer = &child_handle;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003566 }
3567
3568 /* Get the driver binding protocol */
3569 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3570 &efi_guid_driver_binding_protocol,
3571 (void **)&binding_protocol,
3572 driver_image_handle, NULL,
3573 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003574 if (r != EFI_SUCCESS) {
3575 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003576 goto out;
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003577 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003578 /* Remove the children */
3579 if (number_of_children) {
3580 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3581 controller_handle,
3582 number_of_children,
3583 child_handle_buffer));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003584 if (r != EFI_SUCCESS) {
3585 r = EFI_DEVICE_ERROR;
3586 goto out;
3587 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003588 }
3589 /* Remove the driver */
Heinrich Schuchardt314bed62020-10-28 18:45:47 +01003590 if (!child_handle || sole_child) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003591 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3592 controller_handle,
3593 0, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003594 if (r != EFI_SUCCESS) {
3595 r = EFI_DEVICE_ERROR;
3596 goto out;
3597 }
3598 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003599 EFI_CALL(efi_close_protocol(driver_image_handle,
3600 &efi_guid_driver_binding_protocol,
3601 driver_image_handle, NULL));
Heinrich Schuchardt7dc10c92019-09-13 18:20:40 +02003602 r = EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003603out:
3604 if (!child_handle)
3605 free(child_handle_buffer);
3606 return EFI_EXIT(r);
3607}
3608
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003609static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003610 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003611 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3612 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003613 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003614 },
3615 .raise_tpl = efi_raise_tpl,
3616 .restore_tpl = efi_restore_tpl,
3617 .allocate_pages = efi_allocate_pages_ext,
3618 .free_pages = efi_free_pages_ext,
3619 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003620 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003621 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003622 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003623 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003624 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003625 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003626 .close_event = efi_close_event,
3627 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003628 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003629 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003630 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003631 .handle_protocol = efi_handle_protocol,
3632 .reserved = NULL,
3633 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003634 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003635 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003636 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003637 .load_image = efi_load_image,
3638 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003639 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003640 .unload_image = efi_unload_image,
3641 .exit_boot_services = efi_exit_boot_services,
3642 .get_next_monotonic_count = efi_get_next_monotonic_count,
3643 .stall = efi_stall,
3644 .set_watchdog_timer = efi_set_watchdog_timer,
3645 .connect_controller = efi_connect_controller,
3646 .disconnect_controller = efi_disconnect_controller,
3647 .open_protocol = efi_open_protocol,
3648 .close_protocol = efi_close_protocol,
3649 .open_protocol_information = efi_open_protocol_information,
3650 .protocols_per_handle = efi_protocols_per_handle,
3651 .locate_handle_buffer = efi_locate_handle_buffer,
3652 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003653 .install_multiple_protocol_interfaces =
3654 efi_install_multiple_protocol_interfaces,
3655 .uninstall_multiple_protocol_interfaces =
3656 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003657 .calculate_crc32 = efi_calculate_crc32,
3658 .copy_mem = efi_copy_mem,
3659 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003660 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003661};
3662
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003663static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003664
Alexander Graf3c63db92016-10-14 13:45:30 +02003665struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003666 .hdr = {
3667 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003668 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003669 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003670 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003671 .fw_vendor = firmware_vendor,
3672 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02003673 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003674 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003675 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003676};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003677
3678/**
3679 * efi_initialize_system_table() - Initialize system table
3680 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003681 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003682 */
3683efi_status_t efi_initialize_system_table(void)
3684{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003685 efi_status_t ret;
3686
3687 /* Allocate configuration table array */
3688 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3689 EFI_MAX_CONFIGURATION_TABLES *
3690 sizeof(struct efi_configuration_table),
3691 (void **)&systab.tables);
3692
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003693 /*
3694 * These entries will be set to NULL in ExitBootServices(). To avoid
3695 * relocation in SetVirtualAddressMap(), set them dynamically.
3696 */
3697 systab.con_in = &efi_con_in;
3698 systab.con_out = &efi_con_out;
3699 systab.std_err = &efi_con_out;
3700 systab.boottime = &efi_boot_services;
3701
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003702 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003703 efi_update_table_header_crc32(&systab.hdr);
3704 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3705 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003706
3707 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003708}