blob: f18e384c39b3f881bd41416889df0c28210f2132 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030022#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010023
24DECLARE_GLOBAL_DATA_PTR;
25
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020026/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010027static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020028
Alexander Grafc15d9212016-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 Schuchardt370c7a82018-02-18 15:17:50 +010032/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020033__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010034
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020035/* List of queued events */
36LIST_HEAD(efi_event_queue);
37
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020038/* Flag to disable timer activity in ExitBootServices() */
39static bool timers_enabled = true;
40
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010041/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
42bool efi_st_keep_devices;
43
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020044/* List of all events registered by RegisterProtocolNotify() */
45LIST_HEAD(efi_register_notify_events);
46
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010047/* Handle of the currently executing image */
48static efi_handle_t current_image;
49
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020050#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010051/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020052 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-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 Schuchardt1a3732c2020-05-27 01:58:30 +020057static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060058#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010059
Heinrich Schuchardt72506232019-02-09 14:10:39 +010060/* 1 if inside U-Boot code, 0 if inside EFI payload code */
61static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040062static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010063/* GUID of the device tree table */
64const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-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 Clark86789d52017-07-27 08:04:18 -040068
Heinrich Schuchardt672d99e2018-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;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010084/* GUIDs of the Load File and Load File2 protocols */
85const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
86const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010087
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010088static efi_status_t EFIAPI efi_disconnect_controller(
89 efi_handle_t controller_handle,
90 efi_handle_t driver_image_handle,
91 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010092
Rob Clark86789d52017-07-27 08:04:18 -040093/* Called on every callback entry */
94int __efi_entry_check(void)
95{
96 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020097#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -040098 assert(efi_gd);
99 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200100 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400101#endif
102 return ret;
103}
104
105/* Called on every callback exit */
106int __efi_exit_check(void)
107{
108 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200109#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200110 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400111#endif
112 return ret;
113}
114
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200115/**
116 * efi_save_gd() - save global data register
117 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200118 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200119 * As this register may be overwritten by an EFI payload we save it here
120 * and restore it on every callback entered.
121 *
122 * This function is called after relocation from initr_reloc_global_data().
123 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100124void efi_save_gd(void)
125{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200126#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100127 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600128#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100129}
130
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200131/**
132 * efi_restore_gd() - restore global data register
133 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200134 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200135 * Restore it after returning from the UEFI world to the value saved via
136 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400137 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100138void efi_restore_gd(void)
139{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200140#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100141 /* Only restore if we're already in EFI context */
142 if (!efi_gd)
143 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200144 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600145#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100146}
147
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200148/**
Mario Six8fac2912018-07-10 08:40:17 +0200149 * indent_string() - returns a string for indenting with two spaces per level
150 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100151 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200152 * A maximum of ten indent levels is supported. Higher indent levels will be
153 * truncated.
154 *
Mario Six8fac2912018-07-10 08:40:17 +0200155 * Return: A string for indenting with two spaces per level is
156 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400157 */
158static const char *indent_string(int level)
159{
160 const char *indent = " ";
161 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100162
Rob Clarke7896c32017-07-27 08:04:19 -0400163 level = min(max, level * 2);
164 return &indent[max - level];
165}
166
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200167const char *__efi_nesting(void)
168{
169 return indent_string(nesting_level);
170}
171
Rob Clarke7896c32017-07-27 08:04:19 -0400172const char *__efi_nesting_inc(void)
173{
174 return indent_string(nesting_level++);
175}
176
177const char *__efi_nesting_dec(void)
178{
179 return indent_string(--nesting_level);
180}
181
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200182/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200183 * efi_event_is_queued() - check if an event is queued
184 *
185 * @event: event
186 * Return: true if event is queued
187 */
188static bool efi_event_is_queued(struct efi_event *event)
189{
190 return !!event->queue_link.next;
191}
192
193/**
194 * efi_process_event_queue() - process event queue
195 */
196static void efi_process_event_queue(void)
197{
198 while (!list_empty(&efi_event_queue)) {
199 struct efi_event *event;
200 efi_uintn_t old_tpl;
201
202 event = list_first_entry(&efi_event_queue, struct efi_event,
203 queue_link);
204 if (efi_tpl >= event->notify_tpl)
205 return;
206 list_del(&event->queue_link);
207 event->queue_link.next = NULL;
208 event->queue_link.prev = NULL;
209 /* Events must be executed at the event's TPL */
210 old_tpl = efi_tpl;
211 efi_tpl = event->notify_tpl;
212 EFI_CALL_VOID(event->notify_function(event,
213 event->notify_context));
214 efi_tpl = old_tpl;
215 if (event->type == EVT_NOTIFY_SIGNAL)
216 event->is_signaled = 0;
217 }
218}
219
220/**
Mario Six8fac2912018-07-10 08:40:17 +0200221 * efi_queue_event() - queue an EFI event
222 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200223 *
224 * This function queues the notification function of the event for future
225 * execution.
226 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200227 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200228static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200229{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100230 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200231
232 if (!event->notify_function)
233 return;
234
235 if (!efi_event_is_queued(event)) {
236 /*
237 * Events must be notified in order of decreasing task priority
238 * level. Insert the new event accordingly.
239 */
240 list_for_each_entry(item, &efi_event_queue, queue_link) {
241 if (item->notify_tpl < event->notify_tpl) {
242 list_add_tail(&event->queue_link,
243 &item->queue_link);
244 event = NULL;
245 break;
246 }
247 }
248 if (event)
249 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200250 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200251 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200252}
253
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200254/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200255 * is_valid_tpl() - check if the task priority level is valid
256 *
257 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200258 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200259 */
260efi_status_t is_valid_tpl(efi_uintn_t tpl)
261{
262 switch (tpl) {
263 case TPL_APPLICATION:
264 case TPL_CALLBACK:
265 case TPL_NOTIFY:
266 case TPL_HIGH_LEVEL:
267 return EFI_SUCCESS;
268 default:
269 return EFI_INVALID_PARAMETER;
270 }
271}
272
273/**
Mario Six8fac2912018-07-10 08:40:17 +0200274 * efi_signal_event() - signal an EFI event
275 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100276 *
Mario Six8fac2912018-07-10 08:40:17 +0200277 * This function signals an event. If the event belongs to an event group all
278 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100279 * their notification function is queued.
280 *
281 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100282 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200283void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100284{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200285 if (event->is_signaled)
286 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100287 if (event->group) {
288 struct efi_event *evt;
289
290 /*
291 * The signaled state has to set before executing any
292 * notification function
293 */
294 list_for_each_entry(evt, &efi_events, link) {
295 if (!evt->group || guidcmp(evt->group, event->group))
296 continue;
297 if (evt->is_signaled)
298 continue;
299 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100300 }
301 list_for_each_entry(evt, &efi_events, link) {
302 if (!evt->group || guidcmp(evt->group, event->group))
303 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200304 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100305 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200306 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100307 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200308 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100309 }
310}
311
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200312/**
Mario Six8fac2912018-07-10 08:40:17 +0200313 * efi_raise_tpl() - raise the task priority level
314 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200315 *
316 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200317 *
318 * See the Unified Extensible Firmware Interface (UEFI) specification for
319 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200320 *
Mario Six8fac2912018-07-10 08:40:17 +0200321 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200322 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100323static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100324{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100325 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200326
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200327 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200328
329 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200330 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200331 efi_tpl = new_tpl;
332 if (efi_tpl > TPL_HIGH_LEVEL)
333 efi_tpl = TPL_HIGH_LEVEL;
334
335 EFI_EXIT(EFI_SUCCESS);
336 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100337}
338
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200339/**
Mario Six8fac2912018-07-10 08:40:17 +0200340 * efi_restore_tpl() - lower the task priority level
341 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200342 *
343 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200344 *
Mario Six8fac2912018-07-10 08:40:17 +0200345 * See the Unified Extensible Firmware Interface (UEFI) specification for
346 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100348static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100349{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200350 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200351
352 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200353 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200354 efi_tpl = old_tpl;
355 if (efi_tpl > TPL_HIGH_LEVEL)
356 efi_tpl = TPL_HIGH_LEVEL;
357
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100358 /*
359 * Lowering the TPL may have made queued events eligible for execution.
360 */
361 efi_timer_check();
362
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200363 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100364}
365
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200366/**
Mario Six8fac2912018-07-10 08:40:17 +0200367 * efi_allocate_pages_ext() - allocate memory pages
368 * @type: type of allocation to be performed
369 * @memory_type: usage type of the allocated memory
370 * @pages: number of pages to be allocated
371 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200372 *
373 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200374 *
Mario Six8fac2912018-07-10 08:40:17 +0200375 * See the Unified Extensible Firmware Interface (UEFI) specification for
376 * details.
377 *
378 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200379 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900380static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100381 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900382 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100383{
384 efi_status_t r;
385
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100386 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100387 r = efi_allocate_pages(type, memory_type, pages, memory);
388 return EFI_EXIT(r);
389}
390
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200391/**
Mario Six8fac2912018-07-10 08:40:17 +0200392 * efi_free_pages_ext() - Free memory pages.
393 * @memory: start of the memory area to be freed
394 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
396 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200397 *
398 * See the Unified Extensible Firmware Interface (UEFI) specification for
399 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200400 *
Mario Six8fac2912018-07-10 08:40:17 +0200401 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200402 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900403static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100404 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
406 efi_status_t r;
407
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900408 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100409 r = efi_free_pages(memory, pages);
410 return EFI_EXIT(r);
411}
412
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200413/**
Mario Six8fac2912018-07-10 08:40:17 +0200414 * efi_get_memory_map_ext() - get map describing memory usage
415 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
416 * on exit the size of the copied memory map
417 * @memory_map: buffer to which the memory map is written
418 * @map_key: key for the memory map
419 * @descriptor_size: size of an individual memory descriptor
420 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200421 *
422 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 *
Mario Six8fac2912018-07-10 08:40:17 +0200424 * See the Unified Extensible Firmware Interface (UEFI) specification for
425 * details.
426 *
427 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200428 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900429static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100430 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900431 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100432 efi_uintn_t *map_key,
433 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900434 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100435{
436 efi_status_t r;
437
438 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
439 map_key, descriptor_size, descriptor_version);
440 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
441 descriptor_size, descriptor_version);
442 return EFI_EXIT(r);
443}
444
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200445/**
Mario Six8fac2912018-07-10 08:40:17 +0200446 * efi_allocate_pool_ext() - allocate memory from pool
447 * @pool_type: type of the pool from which memory is to be allocated
448 * @size: number of bytes to be allocated
449 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200450 *
451 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200452 *
453 * See the Unified Extensible Firmware Interface (UEFI) specification for
454 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200455 *
Mario Six8fac2912018-07-10 08:40:17 +0200456 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200457 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200458static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100459 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200460 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100461{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100462 efi_status_t r;
463
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100464 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200465 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100466 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100467}
468
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200469/**
Mario Six8fac2912018-07-10 08:40:17 +0200470 * efi_free_pool_ext() - free memory from pool
471 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200472 *
473 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200474 *
Mario Six8fac2912018-07-10 08:40:17 +0200475 * See the Unified Extensible Firmware Interface (UEFI) specification for
476 * details.
477 *
478 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200479 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200480static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100481{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100482 efi_status_t r;
483
484 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200485 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100486 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100487}
488
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200489/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200490 * efi_add_handle() - add a new handle to the object list
491 *
492 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100493 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200494 * The protocols list is initialized. The handle is added to the list of known
495 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100496 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200497void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100498{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200499 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100500 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200501 INIT_LIST_HEAD(&handle->protocols);
502 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100503}
504
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200505/**
Mario Six8fac2912018-07-10 08:40:17 +0200506 * efi_create_handle() - create handle
507 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200508 *
Mario Six8fac2912018-07-10 08:40:17 +0200509 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200510 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100511efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200512{
513 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200514
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200515 obj = calloc(1, sizeof(struct efi_object));
516 if (!obj)
517 return EFI_OUT_OF_RESOURCES;
518
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100519 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200520 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200521
522 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200523}
524
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200525/**
Mario Six8fac2912018-07-10 08:40:17 +0200526 * efi_search_protocol() - find a protocol on a handle.
527 * @handle: handle
528 * @protocol_guid: GUID of the protocol
529 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100530 *
Mario Six8fac2912018-07-10 08:40:17 +0200531 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100532 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100533efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534 const efi_guid_t *protocol_guid,
535 struct efi_handler **handler)
536{
537 struct efi_object *efiobj;
538 struct list_head *lhandle;
539
540 if (!handle || !protocol_guid)
541 return EFI_INVALID_PARAMETER;
542 efiobj = efi_search_obj(handle);
543 if (!efiobj)
544 return EFI_INVALID_PARAMETER;
545 list_for_each(lhandle, &efiobj->protocols) {
546 struct efi_handler *protocol;
547
548 protocol = list_entry(lhandle, struct efi_handler, link);
549 if (!guidcmp(protocol->guid, protocol_guid)) {
550 if (handler)
551 *handler = protocol;
552 return EFI_SUCCESS;
553 }
554 }
555 return EFI_NOT_FOUND;
556}
557
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200558/**
Mario Six8fac2912018-07-10 08:40:17 +0200559 * efi_remove_protocol() - delete protocol from a handle
560 * @handle: handle from which the protocol shall be deleted
561 * @protocol: GUID of the protocol to be deleted
562 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100563 *
Mario Six8fac2912018-07-10 08:40:17 +0200564 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100565 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100566efi_status_t efi_remove_protocol(const efi_handle_t handle,
567 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100568 void *protocol_interface)
569{
570 struct efi_handler *handler;
571 efi_status_t ret;
572
573 ret = efi_search_protocol(handle, protocol, &handler);
574 if (ret != EFI_SUCCESS)
575 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200576 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200577 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100578 list_del(&handler->link);
579 free(handler);
580 return EFI_SUCCESS;
581}
582
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200583/**
Mario Six8fac2912018-07-10 08:40:17 +0200584 * efi_remove_all_protocols() - delete all protocols from a handle
585 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100586 *
Mario Six8fac2912018-07-10 08:40:17 +0200587 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100588 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100589efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100590{
591 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100592 struct efi_handler *protocol;
593 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594
595 efiobj = efi_search_obj(handle);
596 if (!efiobj)
597 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100598 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599 efi_status_t ret;
600
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100601 ret = efi_remove_protocol(handle, protocol->guid,
602 protocol->protocol_interface);
603 if (ret != EFI_SUCCESS)
604 return ret;
605 }
606 return EFI_SUCCESS;
607}
608
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200609/**
Mario Six8fac2912018-07-10 08:40:17 +0200610 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100611 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200612 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100613 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200614void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100615{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200616 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100617 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200618 efi_remove_all_protocols(handle);
619 list_del(&handle->link);
620 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100621}
622
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200623/**
Mario Six8fac2912018-07-10 08:40:17 +0200624 * efi_is_event() - check if a pointer is a valid event
625 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100626 *
Mario Six8fac2912018-07-10 08:40:17 +0200627 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100628 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100629static efi_status_t efi_is_event(const struct efi_event *event)
630{
631 const struct efi_event *evt;
632
633 if (!event)
634 return EFI_INVALID_PARAMETER;
635 list_for_each_entry(evt, &efi_events, link) {
636 if (evt == event)
637 return EFI_SUCCESS;
638 }
639 return EFI_INVALID_PARAMETER;
640}
Alexander Grafc15d9212016-03-04 01:09:59 +0100641
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200642/**
Mario Six8fac2912018-07-10 08:40:17 +0200643 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200644 *
Mario Six8fac2912018-07-10 08:40:17 +0200645 * @type: type of the event to create
646 * @notify_tpl: task priority level of the event
647 * @notify_function: notification function of the event
648 * @notify_context: pointer passed to the notification function
649 * @group: event group
650 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200651 *
652 * This function is used inside U-Boot code to create an event.
653 *
654 * For the API function implementing the CreateEvent service see
655 * efi_create_event_ext.
656 *
Mario Six8fac2912018-07-10 08:40:17 +0200657 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200658 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100659efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200660 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200661 struct efi_event *event,
662 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100663 void *notify_context, efi_guid_t *group,
664 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100665{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100666 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200667 efi_status_t ret;
668 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200669
Jonathan Gray7758b212017-03-12 19:26:07 +1100670 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200671 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100672
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200673 switch (type) {
674 case 0:
675 case EVT_TIMER:
676 case EVT_NOTIFY_SIGNAL:
677 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
678 case EVT_NOTIFY_WAIT:
679 case EVT_TIMER | EVT_NOTIFY_WAIT:
680 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200681 pool_type = EFI_BOOT_SERVICES_DATA;
682 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200683 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200684 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200685 break;
686 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200687 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200688 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100689
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900690 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200691 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200692 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100693
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200694 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
695 (void **)&evt);
696 if (ret != EFI_SUCCESS)
697 return ret;
698 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100699 evt->type = type;
700 evt->notify_tpl = notify_tpl;
701 evt->notify_function = notify_function;
702 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100703 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200704 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100705 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100706 list_add_tail(&evt->link, &efi_events);
707 *event = evt;
708 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100709}
710
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200711/*
Mario Six8fac2912018-07-10 08:40:17 +0200712 * efi_create_event_ex() - create an event in a group
713 * @type: type of the event to create
714 * @notify_tpl: task priority level of the event
715 * @notify_function: notification function of the event
716 * @notify_context: pointer passed to the notification function
717 * @event: created event
718 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100719 *
720 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100721 *
Mario Six8fac2912018-07-10 08:40:17 +0200722 * See the Unified Extensible Firmware Interface (UEFI) specification for
723 * details.
724 *
725 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100726 */
727efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
728 void (EFIAPI *notify_function) (
729 struct efi_event *event,
730 void *context),
731 void *notify_context,
732 efi_guid_t *event_group,
733 struct efi_event **event)
734{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200735 efi_status_t ret;
736
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100737 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
738 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200739
740 /*
741 * The allowable input parameters are the same as in CreateEvent()
742 * except for the following two disallowed event types.
743 */
744 switch (type) {
745 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
746 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
747 ret = EFI_INVALID_PARAMETER;
748 goto out;
749 }
750
751 ret = efi_create_event(type, notify_tpl, notify_function,
752 notify_context, event_group, event);
753out:
754 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100755}
756
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200757/**
Mario Six8fac2912018-07-10 08:40:17 +0200758 * efi_create_event_ext() - create an event
759 * @type: type of the event to create
760 * @notify_tpl: task priority level of the event
761 * @notify_function: notification function of the event
762 * @notify_context: pointer passed to the notification function
763 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200764 *
765 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200766 *
767 * See the Unified Extensible Firmware Interface (UEFI) specification for
768 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200769 *
Mario Six8fac2912018-07-10 08:40:17 +0200770 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200771 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200772static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100773 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200774 void (EFIAPI *notify_function) (
775 struct efi_event *event,
776 void *context),
777 void *notify_context, struct efi_event **event)
778{
779 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
780 notify_context);
781 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100782 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200783}
784
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200785/**
Mario Six8fac2912018-07-10 08:40:17 +0200786 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200787 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200788 * Check if a timer event has occurred or a queued notification function should
789 * be called.
790 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100791 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200792 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100793 */
794void efi_timer_check(void)
795{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100796 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100797 u64 now = timer_get_us();
798
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100799 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200800 if (!timers_enabled)
801 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100802 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200803 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100804 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200805 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100806 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200807 break;
808 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100809 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200810 break;
811 default:
812 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200813 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100814 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200815 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100816 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200817 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100818 WATCHDOG_RESET();
819}
820
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200821/**
Mario Six8fac2912018-07-10 08:40:17 +0200822 * efi_set_timer() - set the trigger time for a timer event or stop the event
823 * @event: event for which the timer is set
824 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200825 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200826 *
827 * This is the function for internal usage in U-Boot. For the API function
828 * implementing the SetTimer service see efi_set_timer_ext.
829 *
Mario Six8fac2912018-07-10 08:40:17 +0200830 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200831 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200832efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200833 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100834{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100835 /* Check that the event is valid */
836 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
837 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100838
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200839 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200840 * The parameter defines a multiple of 100 ns.
841 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200842 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200843 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100844
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100845 switch (type) {
846 case EFI_TIMER_STOP:
847 event->trigger_next = -1ULL;
848 break;
849 case EFI_TIMER_PERIODIC:
850 case EFI_TIMER_RELATIVE:
851 event->trigger_next = timer_get_us() + trigger_time;
852 break;
853 default:
854 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100855 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100856 event->trigger_type = type;
857 event->trigger_time = trigger_time;
858 event->is_signaled = false;
859 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100860}
861
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200862/**
Mario Six8fac2912018-07-10 08:40:17 +0200863 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
864 * event
865 * @event: event for which the timer is set
866 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200867 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200868 *
869 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200870 *
Mario Six8fac2912018-07-10 08:40:17 +0200871 * See the Unified Extensible Firmware Interface (UEFI) specification for
872 * details.
873 *
874 *
875 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200876 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200877static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
878 enum efi_timer_delay type,
879 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200880{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900881 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200882 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
883}
884
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200885/**
Mario Six8fac2912018-07-10 08:40:17 +0200886 * efi_wait_for_event() - wait for events to be signaled
887 * @num_events: number of events to be waited for
888 * @event: events to be waited for
889 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200890 *
891 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200892 *
Mario Six8fac2912018-07-10 08:40:17 +0200893 * See the Unified Extensible Firmware Interface (UEFI) specification for
894 * details.
895 *
896 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200897 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100898static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200899 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100900 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100901{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100902 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100903
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100904 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100905
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200906 /* Check parameters */
907 if (!num_events || !event)
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200909 /* Check TPL */
910 if (efi_tpl != TPL_APPLICATION)
911 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200912 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100913 if (efi_is_event(event[i]) != EFI_SUCCESS)
914 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200915 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200917 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200918 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 }
920
921 /* Wait for signal */
922 for (;;) {
923 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200924 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200925 goto out;
926 }
927 /* Allow events to occur. */
928 efi_timer_check();
929 }
930
931out:
932 /*
933 * Reset the signal which is passed to the caller to allow periodic
934 * events to occur.
935 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200936 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200937 if (index)
938 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100939
940 return EFI_EXIT(EFI_SUCCESS);
941}
942
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200943/**
Mario Six8fac2912018-07-10 08:40:17 +0200944 * efi_signal_event_ext() - signal an EFI event
945 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200946 *
947 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200948 *
949 * See the Unified Extensible Firmware Interface (UEFI) specification for
950 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200951 *
952 * This functions sets the signaled state of the event and queues the
953 * notification function for execution.
954 *
Mario Six8fac2912018-07-10 08:40:17 +0200955 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200956 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200957static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100958{
959 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100960 if (efi_is_event(event) != EFI_SUCCESS)
961 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200962 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100963 return EFI_EXIT(EFI_SUCCESS);
964}
965
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200966/**
Mario Six8fac2912018-07-10 08:40:17 +0200967 * efi_close_event() - close an EFI event
968 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200969 *
970 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200971 *
Mario Six8fac2912018-07-10 08:40:17 +0200972 * See the Unified Extensible Firmware Interface (UEFI) specification for
973 * details.
974 *
975 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200976 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200977static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100978{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200979 struct efi_register_notify_event *item, *next;
980
Alexander Grafc15d9212016-03-04 01:09:59 +0100981 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100982 if (efi_is_event(event) != EFI_SUCCESS)
983 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200984
985 /* Remove protocol notify registrations for the event */
986 list_for_each_entry_safe(item, next, &efi_register_notify_events,
987 link) {
988 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200989 struct efi_protocol_notification *hitem, *hnext;
990
991 /* Remove signaled handles */
992 list_for_each_entry_safe(hitem, hnext, &item->handles,
993 link) {
994 list_del(&hitem->link);
995 free(hitem);
996 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200997 list_del(&item->link);
998 free(item);
999 }
1000 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001001 /* Remove event from queue */
1002 if (efi_event_is_queued(event))
1003 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001004
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001005 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001006 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001007 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001008}
1009
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001010/**
Mario Six8fac2912018-07-10 08:40:17 +02001011 * efi_check_event() - check if an event is signaled
1012 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001013 *
1014 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001015 *
1016 * See the Unified Extensible Firmware Interface (UEFI) specification for
1017 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001018 *
Mario Six8fac2912018-07-10 08:40:17 +02001019 * If an event is not signaled yet, the notification function is queued. The
1020 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001021 *
Mario Six8fac2912018-07-10 08:40:17 +02001022 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001023 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001024static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001025{
1026 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001027 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001028 if (efi_is_event(event) != EFI_SUCCESS ||
1029 event->type & EVT_NOTIFY_SIGNAL)
1030 return EFI_EXIT(EFI_INVALID_PARAMETER);
1031 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001032 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001033 if (event->is_signaled) {
1034 event->is_signaled = false;
1035 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001036 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001037 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001038}
1039
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001040/**
Mario Six8fac2912018-07-10 08:40:17 +02001041 * efi_search_obj() - find the internal EFI object for a handle
1042 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001043 *
Mario Six8fac2912018-07-10 08:40:17 +02001044 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001045 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001046struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001047{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001048 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001049
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001050 if (!handle)
1051 return NULL;
1052
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001053 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001054 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001055 return efiobj;
1056 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001057 return NULL;
1058}
1059
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001060/**
Mario Six8fac2912018-07-10 08:40:17 +02001061 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1062 * to a protocol
1063 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001064 *
Mario Six8fac2912018-07-10 08:40:17 +02001065 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001066 */
1067static struct efi_open_protocol_info_entry *efi_create_open_info(
1068 struct efi_handler *handler)
1069{
1070 struct efi_open_protocol_info_item *item;
1071
1072 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1073 if (!item)
1074 return NULL;
1075 /* Append the item to the open protocol info list. */
1076 list_add_tail(&item->link, &handler->open_infos);
1077
1078 return &item->info;
1079}
1080
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001081/**
Mario Six8fac2912018-07-10 08:40:17 +02001082 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1083 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001084 *
Mario Six8fac2912018-07-10 08:40:17 +02001085 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001086 */
1087static efi_status_t efi_delete_open_info(
1088 struct efi_open_protocol_info_item *item)
1089{
1090 list_del(&item->link);
1091 free(item);
1092 return EFI_SUCCESS;
1093}
1094
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001095/**
Mario Six8fac2912018-07-10 08:40:17 +02001096 * efi_add_protocol() - install new protocol on a handle
1097 * @handle: handle on which the protocol shall be installed
1098 * @protocol: GUID of the protocol to be installed
1099 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001100 *
Mario Six8fac2912018-07-10 08:40:17 +02001101 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001102 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001103efi_status_t efi_add_protocol(const efi_handle_t handle,
1104 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001105 void *protocol_interface)
1106{
1107 struct efi_object *efiobj;
1108 struct efi_handler *handler;
1109 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001110 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001111
1112 efiobj = efi_search_obj(handle);
1113 if (!efiobj)
1114 return EFI_INVALID_PARAMETER;
1115 ret = efi_search_protocol(handle, protocol, NULL);
1116 if (ret != EFI_NOT_FOUND)
1117 return EFI_INVALID_PARAMETER;
1118 handler = calloc(1, sizeof(struct efi_handler));
1119 if (!handler)
1120 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001121 handler->guid = protocol;
1122 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001123 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001124 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001125
1126 /* Notify registered events */
1127 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001128 if (!guidcmp(protocol, &event->protocol)) {
1129 struct efi_protocol_notification *notif;
1130
1131 notif = calloc(1, sizeof(*notif));
1132 if (!notif) {
1133 list_del(&handler->link);
1134 free(handler);
1135 return EFI_OUT_OF_RESOURCES;
1136 }
1137 notif->handle = handle;
1138 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001139 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001140 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001141 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001142 }
1143
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001144 if (!guidcmp(&efi_guid_device_path, protocol))
1145 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001146 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001147}
1148
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001149/**
Mario Six8fac2912018-07-10 08:40:17 +02001150 * efi_install_protocol_interface() - install protocol interface
1151 * @handle: handle on which the protocol shall be installed
1152 * @protocol: GUID of the protocol to be installed
1153 * @protocol_interface_type: type of the interface to be installed,
1154 * always EFI_NATIVE_INTERFACE
1155 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001156 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001157 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001158 *
Mario Six8fac2912018-07-10 08:40:17 +02001159 * See the Unified Extensible Firmware Interface (UEFI) specification for
1160 * details.
1161 *
1162 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001163 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001164static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001165 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001166 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001167{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001168 efi_status_t r;
1169
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001170 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1171 protocol_interface);
1172
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001173 if (!handle || !protocol ||
1174 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1175 r = EFI_INVALID_PARAMETER;
1176 goto out;
1177 }
1178
1179 /* Create new handle if requested. */
1180 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001181 r = efi_create_handle(handle);
1182 if (r != EFI_SUCCESS)
1183 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001184 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001185 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001186 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001187 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001188 /* Add new protocol */
1189 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001190out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001191 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001192}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001193
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001194/**
Mario Six8fac2912018-07-10 08:40:17 +02001195 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001196 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001197 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001198 * @number_of_drivers: number of child controllers
1199 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001200 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001201 * The allocated buffer has to be freed with free().
1202 *
Mario Six8fac2912018-07-10 08:40:17 +02001203 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001204 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001205static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001206 const efi_guid_t *protocol,
1207 efi_uintn_t *number_of_drivers,
1208 efi_handle_t **driver_handle_buffer)
1209{
1210 struct efi_handler *handler;
1211 struct efi_open_protocol_info_item *item;
1212 efi_uintn_t count = 0, i;
1213 bool duplicate;
1214
1215 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001216 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001217 if (protocol && guidcmp(handler->guid, protocol))
1218 continue;
1219 list_for_each_entry(item, &handler->open_infos, link) {
1220 if (item->info.attributes &
1221 EFI_OPEN_PROTOCOL_BY_DRIVER)
1222 ++count;
1223 }
1224 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001225 *number_of_drivers = 0;
1226 if (!count) {
1227 *driver_handle_buffer = NULL;
1228 return EFI_SUCCESS;
1229 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001230 /*
1231 * Create buffer. In case of duplicate driver assignments the buffer
1232 * will be too large. But that does not harm.
1233 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001234 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1235 if (!*driver_handle_buffer)
1236 return EFI_OUT_OF_RESOURCES;
1237 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001238 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001239 if (protocol && guidcmp(handler->guid, protocol))
1240 continue;
1241 list_for_each_entry(item, &handler->open_infos, link) {
1242 if (item->info.attributes &
1243 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1244 /* Check this is a new driver */
1245 duplicate = false;
1246 for (i = 0; i < *number_of_drivers; ++i) {
1247 if ((*driver_handle_buffer)[i] ==
1248 item->info.agent_handle)
1249 duplicate = true;
1250 }
1251 /* Copy handle to buffer */
1252 if (!duplicate) {
1253 i = (*number_of_drivers)++;
1254 (*driver_handle_buffer)[i] =
1255 item->info.agent_handle;
1256 }
1257 }
1258 }
1259 }
1260 return EFI_SUCCESS;
1261}
1262
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001263/**
Mario Six8fac2912018-07-10 08:40:17 +02001264 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001265 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001266 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001267 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001268 *
1269 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001270 *
1271 * See the Unified Extensible Firmware Interface (UEFI) specification for
1272 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001273 *
Mario Six8fac2912018-07-10 08:40:17 +02001274 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001275 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001276static efi_status_t efi_disconnect_all_drivers
1277 (efi_handle_t handle,
1278 const efi_guid_t *protocol,
1279 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001280{
1281 efi_uintn_t number_of_drivers;
1282 efi_handle_t *driver_handle_buffer;
1283 efi_status_t r, ret;
1284
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001285 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001286 &driver_handle_buffer);
1287 if (ret != EFI_SUCCESS)
1288 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001289 if (!number_of_drivers)
1290 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001291 ret = EFI_NOT_FOUND;
1292 while (number_of_drivers) {
1293 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001294 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001295 driver_handle_buffer[--number_of_drivers],
1296 child_handle));
1297 if (r == EFI_SUCCESS)
1298 ret = r;
1299 }
1300 free(driver_handle_buffer);
1301 return ret;
1302}
1303
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001304/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001305 * efi_uninstall_protocol() - uninstall protocol interface
1306 *
Mario Six8fac2912018-07-10 08:40:17 +02001307 * @handle: handle from which the protocol shall be removed
1308 * @protocol: GUID of the protocol to be removed
1309 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001310 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001311 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001312 *
1313 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001314 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001315static efi_status_t efi_uninstall_protocol
1316 (efi_handle_t handle, const efi_guid_t *protocol,
1317 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001318{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001319 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001320 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001321 struct efi_open_protocol_info_item *item;
1322 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001323 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001324
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001325 /* Check handle */
1326 efiobj = efi_search_obj(handle);
1327 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001328 r = EFI_INVALID_PARAMETER;
1329 goto out;
1330 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001331 /* Find the protocol on the handle */
1332 r = efi_search_protocol(handle, protocol, &handler);
1333 if (r != EFI_SUCCESS)
1334 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001335 /* Disconnect controllers */
1336 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001337 /* Close protocol */
1338 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1339 if (item->info.attributes ==
1340 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1341 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1342 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1343 list_del(&item->link);
1344 }
1345 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001346 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001347 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001348 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001349 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001350out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001351 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001352}
1353
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001354/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001355 * efi_uninstall_protocol_interface() - uninstall protocol interface
1356 * @handle: handle from which the protocol shall be removed
1357 * @protocol: GUID of the protocol to be removed
1358 * @protocol_interface: interface to be removed
1359 *
1360 * This function implements the UninstallProtocolInterface service.
1361 *
1362 * See the Unified Extensible Firmware Interface (UEFI) specification for
1363 * details.
1364 *
1365 * Return: status code
1366 */
1367static efi_status_t EFIAPI efi_uninstall_protocol_interface
1368 (efi_handle_t handle, const efi_guid_t *protocol,
1369 void *protocol_interface)
1370{
1371 efi_status_t ret;
1372
1373 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1374
1375 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1376 if (ret != EFI_SUCCESS)
1377 goto out;
1378
1379 /* If the last protocol has been removed, delete the handle. */
1380 if (list_empty(&handle->protocols)) {
1381 list_del(&handle->link);
1382 free(handle);
1383 }
1384out:
1385 return EFI_EXIT(ret);
1386}
1387
1388/**
Mario Six8fac2912018-07-10 08:40:17 +02001389 * efi_register_protocol_notify() - register an event for notification when a
1390 * protocol is installed.
1391 * @protocol: GUID of the protocol whose installation shall be notified
1392 * @event: event to be signaled upon installation of the protocol
1393 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001394 *
1395 * This function implements the RegisterProtocolNotify service.
1396 * See the Unified Extensible Firmware Interface (UEFI) specification
1397 * for details.
1398 *
Mario Six8fac2912018-07-10 08:40:17 +02001399 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001400 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001401static efi_status_t EFIAPI efi_register_protocol_notify(
1402 const efi_guid_t *protocol,
1403 struct efi_event *event,
1404 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001405{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001406 struct efi_register_notify_event *item;
1407 efi_status_t ret = EFI_SUCCESS;
1408
Rob Clark238f88c2017-09-13 18:05:41 -04001409 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001410
1411 if (!protocol || !event || !registration) {
1412 ret = EFI_INVALID_PARAMETER;
1413 goto out;
1414 }
1415
1416 item = calloc(1, sizeof(struct efi_register_notify_event));
1417 if (!item) {
1418 ret = EFI_OUT_OF_RESOURCES;
1419 goto out;
1420 }
1421
1422 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301423 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001424 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001425
1426 list_add_tail(&item->link, &efi_register_notify_events);
1427
1428 *registration = item;
1429out:
1430 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001431}
1432
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001433/**
Mario Six8fac2912018-07-10 08:40:17 +02001434 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001435 *
Mario Six8fac2912018-07-10 08:40:17 +02001436 * @search_type: selection criterion
1437 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001438 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001439 *
1440 * See the documentation of the LocateHandle service in the UEFI specification.
1441 *
Mario Six8fac2912018-07-10 08:40:17 +02001442 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001443 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001444static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001445 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001446{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001447 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001448
1449 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001450 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001451 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001452 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001453 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001454 return (ret != EFI_SUCCESS);
1455 default:
1456 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001457 return -1;
1458 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001459}
1460
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001461/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001462 * efi_check_register_notify_event() - check if registration key is valid
1463 *
1464 * Check that a pointer is a valid registration key as returned by
1465 * RegisterProtocolNotify().
1466 *
1467 * @key: registration key
1468 * Return: valid registration key or NULL
1469 */
1470static struct efi_register_notify_event *efi_check_register_notify_event
1471 (void *key)
1472{
1473 struct efi_register_notify_event *event;
1474
1475 list_for_each_entry(event, &efi_register_notify_events, link) {
1476 if (event == (struct efi_register_notify_event *)key)
1477 return event;
1478 }
1479 return NULL;
1480}
1481
1482/**
Mario Six8fac2912018-07-10 08:40:17 +02001483 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001484 *
1485 * @search_type: selection criterion
1486 * @protocol: GUID of the protocol
1487 * @search_key: registration key
1488 * @buffer_size: size of the buffer to receive the handles in bytes
1489 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001490 *
1491 * This function is meant for U-Boot internal calls. For the API implementation
1492 * of the LocateHandle service see efi_locate_handle_ext.
1493 *
Mario Six8fac2912018-07-10 08:40:17 +02001494 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001495 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001496static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001497 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001498 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001499 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001500{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001501 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001502 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001503 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001504 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001505
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001506 /* Check parameters */
1507 switch (search_type) {
1508 case ALL_HANDLES:
1509 break;
1510 case BY_REGISTER_NOTIFY:
1511 if (!search_key)
1512 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001513 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001514 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001515 if (!event)
1516 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001517 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001518 case BY_PROTOCOL:
1519 if (!protocol)
1520 return EFI_INVALID_PARAMETER;
1521 break;
1522 default:
1523 return EFI_INVALID_PARAMETER;
1524 }
1525
Alexander Grafc15d9212016-03-04 01:09:59 +01001526 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001527 if (search_type == BY_REGISTER_NOTIFY) {
1528 if (list_empty(&event->handles))
1529 return EFI_NOT_FOUND;
1530 handle = list_first_entry(&event->handles,
1531 struct efi_protocol_notification,
1532 link);
1533 efiobj = handle->handle;
1534 size += sizeof(void *);
1535 } else {
1536 list_for_each_entry(efiobj, &efi_obj_list, link) {
1537 if (!efi_search(search_type, protocol, efiobj))
1538 size += sizeof(void *);
1539 }
1540 if (size == 0)
1541 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001542 }
1543
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001544 if (!buffer_size)
1545 return EFI_INVALID_PARAMETER;
1546
Alexander Grafc15d9212016-03-04 01:09:59 +01001547 if (*buffer_size < size) {
1548 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001549 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001550 }
1551
Rob Clarkcdee3372017-08-06 14:10:07 -04001552 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001553
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001554 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001555 if (!buffer)
1556 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001557
Alexander Grafc15d9212016-03-04 01:09:59 +01001558 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001559 if (search_type == BY_REGISTER_NOTIFY) {
1560 *buffer = efiobj;
1561 list_del(&handle->link);
1562 } else {
1563 list_for_each_entry(efiobj, &efi_obj_list, link) {
1564 if (!efi_search(search_type, protocol, efiobj))
1565 *buffer++ = efiobj;
1566 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001567 }
1568
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001569 return EFI_SUCCESS;
1570}
1571
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001572/**
Mario Six8fac2912018-07-10 08:40:17 +02001573 * efi_locate_handle_ext() - locate handles implementing a protocol.
1574 * @search_type: selection criterion
1575 * @protocol: GUID of the protocol
1576 * @search_key: registration key
1577 * @buffer_size: size of the buffer to receive the handles in bytes
1578 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001579 *
1580 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001581 *
Mario Six8fac2912018-07-10 08:40:17 +02001582 * See the Unified Extensible Firmware Interface (UEFI) specification for
1583 * details.
1584 *
1585 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001586 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001587static efi_status_t EFIAPI efi_locate_handle_ext(
1588 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001589 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001590 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001591{
Rob Clark238f88c2017-09-13 18:05:41 -04001592 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001593 buffer_size, buffer);
1594
1595 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1596 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001597}
1598
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001599/**
Mario Six8fac2912018-07-10 08:40:17 +02001600 * efi_remove_configuration_table() - collapses configuration table entries,
1601 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001602 *
Mario Six8fac2912018-07-10 08:40:17 +02001603 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001604 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001605static void efi_remove_configuration_table(int i)
1606{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001607 struct efi_configuration_table *this = &systab.tables[i];
1608 struct efi_configuration_table *next = &systab.tables[i + 1];
1609 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001610
1611 memmove(this, next, (ulong)end - (ulong)next);
1612 systab.nr_tables--;
1613}
1614
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001615/**
Mario Six8fac2912018-07-10 08:40:17 +02001616 * efi_install_configuration_table() - adds, updates, or removes a
1617 * configuration table
1618 * @guid: GUID of the installed table
1619 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001620 *
1621 * This function is used for internal calls. For the API implementation of the
1622 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1623 *
Mario Six8fac2912018-07-10 08:40:17 +02001624 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001625 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001626efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1627 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001628{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001629 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001630 int i;
1631
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001632 if (!guid)
1633 return EFI_INVALID_PARAMETER;
1634
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001635 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001636 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001637 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001638 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001639 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001640 else
1641 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001642 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001643 }
1644 }
1645
Alexander Graffe3366f2017-07-26 13:41:04 +02001646 if (!table)
1647 return EFI_NOT_FOUND;
1648
Alexander Grafc15d9212016-03-04 01:09:59 +01001649 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001650 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001651 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001652
1653 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301654 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001655 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001656 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001657
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001658out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001659 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001660 efi_update_table_header_crc32(&systab.hdr);
1661
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001662 /* Notify that the configuration table was changed */
1663 list_for_each_entry(evt, &efi_events, link) {
1664 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001665 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001666 break;
1667 }
1668 }
1669
Alexander Grafc5c11632016-08-19 01:23:24 +02001670 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001671}
1672
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001673/**
Mario Six8fac2912018-07-10 08:40:17 +02001674 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1675 * configuration table.
1676 * @guid: GUID of the installed table
1677 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001678 *
1679 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001680 *
Mario Six8fac2912018-07-10 08:40:17 +02001681 * See the Unified Extensible Firmware Interface (UEFI) specification for
1682 * details.
1683 *
1684 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001685 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001686static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1687 void *table)
1688{
Rob Clark238f88c2017-09-13 18:05:41 -04001689 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001690 return EFI_EXIT(efi_install_configuration_table(guid, table));
1691}
1692
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001693/**
Mario Six8fac2912018-07-10 08:40:17 +02001694 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001695 *
1696 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001697 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001698 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001699 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001700 * code is returned.
1701 *
1702 * @device_path: device path of the loaded image
1703 * @file_path: file path of the loaded image
1704 * @handle_ptr: handle of the loaded image
1705 * @info_ptr: loaded image protocol
1706 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001707 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001708efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1709 struct efi_device_path *file_path,
1710 struct efi_loaded_image_obj **handle_ptr,
1711 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001712{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001713 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001714 struct efi_loaded_image *info = NULL;
1715 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001716 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001717
1718 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1719 *handle_ptr = NULL;
1720 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001721
1722 info = calloc(1, sizeof(*info));
1723 if (!info)
1724 return EFI_OUT_OF_RESOURCES;
1725 obj = calloc(1, sizeof(*obj));
1726 if (!obj) {
1727 free(info);
1728 return EFI_OUT_OF_RESOURCES;
1729 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001730 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001731
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001732 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001733 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001734
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001735 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001736 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001737 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001738
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001739 if (device_path) {
1740 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001741
1742 dp = efi_dp_append(device_path, file_path);
1743 if (!dp) {
1744 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001745 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001746 }
1747 } else {
1748 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001749 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001750 ret = efi_add_protocol(&obj->header,
1751 &efi_guid_loaded_image_device_path, dp);
1752 if (ret != EFI_SUCCESS)
1753 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001754
1755 /*
1756 * When asking for the loaded_image interface, just
1757 * return handle which points to loaded_image_info
1758 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001759 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001760 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001761 if (ret != EFI_SUCCESS)
1762 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001763
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001764 *info_ptr = info;
1765 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001766
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001767 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001768failure:
1769 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001770 efi_delete_handle(&obj->header);
1771 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001772 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001773}
1774
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001775/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001776 * efi_locate_device_path() - Get the device path and handle of an device
1777 * implementing a protocol
1778 * @protocol: GUID of the protocol
1779 * @device_path: device path
1780 * @device: handle of the device
1781 *
1782 * This function implements the LocateDevicePath service.
1783 *
1784 * See the Unified Extensible Firmware Interface (UEFI) specification for
1785 * details.
1786 *
1787 * Return: status code
1788 */
1789static efi_status_t EFIAPI efi_locate_device_path(
1790 const efi_guid_t *protocol,
1791 struct efi_device_path **device_path,
1792 efi_handle_t *device)
1793{
1794 struct efi_device_path *dp;
1795 size_t i;
1796 struct efi_handler *handler;
1797 efi_handle_t *handles;
1798 size_t len, len_dp;
1799 size_t len_best = 0;
1800 efi_uintn_t no_handles;
1801 u8 *remainder;
1802 efi_status_t ret;
1803
1804 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1805
1806 if (!protocol || !device_path || !*device_path) {
1807 ret = EFI_INVALID_PARAMETER;
1808 goto out;
1809 }
1810
1811 /* Find end of device path */
1812 len = efi_dp_instance_size(*device_path);
1813
1814 /* Get all handles implementing the protocol */
1815 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1816 &no_handles, &handles));
1817 if (ret != EFI_SUCCESS)
1818 goto out;
1819
1820 for (i = 0; i < no_handles; ++i) {
1821 /* Find the device path protocol */
1822 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1823 &handler);
1824 if (ret != EFI_SUCCESS)
1825 continue;
1826 dp = (struct efi_device_path *)handler->protocol_interface;
1827 len_dp = efi_dp_instance_size(dp);
1828 /*
1829 * This handle can only be a better fit
1830 * if its device path length is longer than the best fit and
1831 * if its device path length is shorter of equal the searched
1832 * device path.
1833 */
1834 if (len_dp <= len_best || len_dp > len)
1835 continue;
1836 /* Check if dp is a subpath of device_path */
1837 if (memcmp(*device_path, dp, len_dp))
1838 continue;
1839 if (!device) {
1840 ret = EFI_INVALID_PARAMETER;
1841 goto out;
1842 }
1843 *device = handles[i];
1844 len_best = len_dp;
1845 }
1846 if (len_best) {
1847 remainder = (u8 *)*device_path + len_best;
1848 *device_path = (struct efi_device_path *)remainder;
1849 ret = EFI_SUCCESS;
1850 } else {
1851 ret = EFI_NOT_FOUND;
1852 }
1853out:
1854 return EFI_EXIT(ret);
1855}
1856
1857/**
Mario Six8fac2912018-07-10 08:40:17 +02001858 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001859 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001860 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1861 * callers obligation to update the memory type as needed.
1862 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001863 * @boot_policy: true for request originating from the boot manager
1864 * @file_path: the path of the image to load
1865 * @buffer: buffer containing the loaded image
1866 * @size: size of the loaded image
1867 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001868 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001869static
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001870efi_status_t efi_load_image_from_path(bool boot_policy,
1871 struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001872 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001873{
1874 struct efi_file_info *info = NULL;
1875 struct efi_file_handle *f;
1876 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001877 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001878 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001879
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001880 /* In case of failure nothing is returned */
1881 *buffer = NULL;
1882 *size = 0;
1883
1884 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001885 f = efi_file_from_path(file_path);
1886 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001887 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001888
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001889 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001890 bs = 0;
1891 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1892 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001893 if (ret != EFI_BUFFER_TOO_SMALL) {
1894 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001895 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001896 }
Rob Clark857a1222017-09-13 18:05:35 -04001897
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001898 info = malloc(bs);
1899 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1900 info));
1901 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001902 goto error;
1903
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001904 /*
1905 * When reading the file we do not yet know if it contains an
1906 * application, a boottime driver, or a runtime driver. So here we
1907 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1908 * update the reservation according to the image type.
1909 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001910 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001911 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1912 EFI_BOOT_SERVICES_DATA,
1913 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001914 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001915 ret = EFI_OUT_OF_RESOURCES;
1916 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001917 }
1918
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001919 /* Read file */
1920 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1921 if (ret != EFI_SUCCESS)
1922 efi_free_pages(addr, efi_size_in_pages(bs));
1923 *buffer = (void *)(uintptr_t)addr;
1924 *size = bs;
1925error:
1926 EFI_CALL(f->close(f));
1927 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001928 return ret;
1929}
1930
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001931/**
Mario Six8fac2912018-07-10 08:40:17 +02001932 * efi_load_image() - load an EFI image into memory
1933 * @boot_policy: true for request originating from the boot manager
1934 * @parent_image: the caller's image handle
1935 * @file_path: the path of the image to load
1936 * @source_buffer: memory location from which the image is installed
1937 * @source_size: size of the memory area from which the image is installed
1938 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001939 *
1940 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001941 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001942 * See the Unified Extensible Firmware Interface (UEFI) specification
1943 * for details.
1944 *
Mario Six8fac2912018-07-10 08:40:17 +02001945 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001946 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001947efi_status_t EFIAPI efi_load_image(bool boot_policy,
1948 efi_handle_t parent_image,
1949 struct efi_device_path *file_path,
1950 void *source_buffer,
1951 efi_uintn_t source_size,
1952 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001953{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001954 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001955 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001956 struct efi_loaded_image_obj **image_obj =
1957 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001958 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001959 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001960
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001961 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001962 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001963
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001964 if (!image_handle || (!source_buffer && !file_path) ||
1965 !efi_search_obj(parent_image) ||
1966 /* The parent image handle must refer to a loaded image */
1967 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001968 ret = EFI_INVALID_PARAMETER;
1969 goto error;
1970 }
Rob Clark857a1222017-09-13 18:05:35 -04001971
1972 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001973 ret = efi_load_image_from_path(boot_policy, file_path,
1974 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001975 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001976 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001977 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001978 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001979 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001980 /* split file_path which contains both the device and file parts */
1981 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001982 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001983 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001984 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001985 if (!source_buffer)
1986 /* Release buffer to which file was loaded */
1987 efi_free_pages((uintptr_t)dest_buffer,
1988 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001989 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001990 info->system_table = &systab;
1991 info->parent_handle = parent_image;
1992 } else {
1993 /* The image is invalid. Release all associated resources. */
1994 efi_delete_handle(*image_handle);
1995 *image_handle = NULL;
1996 free(info);
1997 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001998error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001999 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002000}
2001
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002002/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002003 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2004 */
2005static void efi_exit_caches(void)
2006{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002007#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002008 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002009 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2010 * caches are enabled.
2011 *
2012 * TODO:
2013 * According to the UEFI spec caches that can be managed via CP15
2014 * operations should be enabled. Caches requiring platform information
2015 * to manage should be disabled. This should not happen in
2016 * ExitBootServices() but before invoking any UEFI binary is invoked.
2017 *
2018 * We want to keep the current workaround while GRUB prior to version
2019 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002020 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002021 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002022#endif
2023}
2024
2025/**
Mario Six8fac2912018-07-10 08:40:17 +02002026 * efi_exit_boot_services() - stop all boot services
2027 * @image_handle: handle of the loaded image
2028 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002029 *
2030 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002031 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002032 * See the Unified Extensible Firmware Interface (UEFI) specification
2033 * for details.
2034 *
Mario Six8fac2912018-07-10 08:40:17 +02002035 * All timer events are disabled. For exit boot services events the
2036 * notification function is called. The boot services are disabled in the
2037 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002038 *
Mario Six8fac2912018-07-10 08:40:17 +02002039 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002040 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002041static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002042 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002043{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002044 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002045 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002046
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002047 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002048
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002049 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002050 if (map_key != efi_memory_map_key) {
2051 ret = EFI_INVALID_PARAMETER;
2052 goto out;
2053 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002054
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002055 /* Check if ExitBootServices has already been called */
2056 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002057 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002058
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002059 /* Stop all timer related activities */
2060 timers_enabled = false;
2061
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002062 /* Add related events to the event group */
2063 list_for_each_entry(evt, &efi_events, link) {
2064 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2065 evt->group = &efi_guid_event_group_exit_boot_services;
2066 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002067 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002068 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002069 if (evt->group &&
2070 !guidcmp(evt->group,
2071 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002072 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002073 break;
2074 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002075 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002076
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002077 /* Make sure that notification functions are not called anymore */
2078 efi_tpl = TPL_HIGH_LEVEL;
2079
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002080 /* Notify variable services */
2081 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002082
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002083 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2084 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2085 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2086 list_del(&evt->link);
2087 }
2088
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002089 if (!efi_st_keep_devices) {
2090 if IS_ENABLED(CONFIG_USB_DEVICE)
2091 udc_disconnect();
2092 board_quiesce_devices();
2093 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2094 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002095
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002096 /* Patch out unsupported runtime function */
2097 efi_runtime_detach();
2098
Alexander Graffa5246b2018-11-15 21:23:47 +01002099 /* Fix up caches for EFI payloads if necessary */
2100 efi_exit_caches();
2101
Alexander Grafc15d9212016-03-04 01:09:59 +01002102 /* This stops all lingering devices */
2103 bootm_disable_interrupts();
2104
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002105 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002106 systab.con_in_handle = NULL;
2107 systab.con_in = NULL;
2108 systab.con_out_handle = NULL;
2109 systab.con_out = NULL;
2110 systab.stderr_handle = NULL;
2111 systab.std_err = NULL;
2112 systab.boottime = NULL;
2113
2114 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002115 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002116
Alexander Grafc15d9212016-03-04 01:09:59 +01002117 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002118 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002119 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002120out:
2121 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002122}
2123
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002124/**
Mario Six8fac2912018-07-10 08:40:17 +02002125 * efi_get_next_monotonic_count() - get next value of the counter
2126 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002127 *
2128 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002129 *
2130 * See the Unified Extensible Firmware Interface (UEFI) specification for
2131 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002132 *
Mario Six8fac2912018-07-10 08:40:17 +02002133 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002134 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002135static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2136{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002137 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002138 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002139
Alexander Grafc15d9212016-03-04 01:09:59 +01002140 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002141 if (!count) {
2142 ret = EFI_INVALID_PARAMETER;
2143 goto out;
2144 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002145 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002146 ret = EFI_SUCCESS;
2147out:
2148 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002149}
2150
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002151/**
Mario Six8fac2912018-07-10 08:40:17 +02002152 * efi_stall() - sleep
2153 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002154 *
Mario Six8fac2912018-07-10 08:40:17 +02002155 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002156 *
Mario Six8fac2912018-07-10 08:40:17 +02002157 * See the Unified Extensible Firmware Interface (UEFI) specification for
2158 * details.
2159 *
2160 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002161 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002162static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2163{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002164 u64 end_tick;
2165
Alexander Grafc15d9212016-03-04 01:09:59 +01002166 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002167
2168 end_tick = get_ticks() + usec_to_tick(microseconds);
2169 while (get_ticks() < end_tick)
2170 efi_timer_check();
2171
Alexander Grafc15d9212016-03-04 01:09:59 +01002172 return EFI_EXIT(EFI_SUCCESS);
2173}
2174
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002175/**
Mario Six8fac2912018-07-10 08:40:17 +02002176 * efi_set_watchdog_timer() - reset the watchdog timer
2177 * @timeout: seconds before reset by watchdog
2178 * @watchdog_code: code to be logged when resetting
2179 * @data_size: size of buffer in bytes
2180 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002181 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002182 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002183 *
Mario Six8fac2912018-07-10 08:40:17 +02002184 * See the Unified Extensible Firmware Interface (UEFI) specification for
2185 * details.
2186 *
2187 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002188 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002189static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2190 uint64_t watchdog_code,
2191 unsigned long data_size,
2192 uint16_t *watchdog_data)
2193{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002194 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002195 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002196 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002197}
2198
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002199/**
Mario Six8fac2912018-07-10 08:40:17 +02002200 * efi_close_protocol() - close a protocol
2201 * @handle: handle on which the protocol shall be closed
2202 * @protocol: GUID of the protocol to close
2203 * @agent_handle: handle of the driver
2204 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002205 *
2206 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002207 *
Mario Six8fac2912018-07-10 08:40:17 +02002208 * See the Unified Extensible Firmware Interface (UEFI) specification for
2209 * details.
2210 *
2211 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002212 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002213efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2214 const efi_guid_t *protocol,
2215 efi_handle_t agent_handle,
2216 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002217{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002218 struct efi_handler *handler;
2219 struct efi_open_protocol_info_item *item;
2220 struct efi_open_protocol_info_item *pos;
2221 efi_status_t r;
2222
Rob Clark238f88c2017-09-13 18:05:41 -04002223 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002224 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002225
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002226 if (!efi_search_obj(agent_handle) ||
2227 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002228 r = EFI_INVALID_PARAMETER;
2229 goto out;
2230 }
2231 r = efi_search_protocol(handle, protocol, &handler);
2232 if (r != EFI_SUCCESS)
2233 goto out;
2234
2235 r = EFI_NOT_FOUND;
2236 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2237 if (item->info.agent_handle == agent_handle &&
2238 item->info.controller_handle == controller_handle) {
2239 efi_delete_open_info(item);
2240 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002241 }
2242 }
2243out:
2244 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002245}
2246
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002247/**
Mario Six8fac2912018-07-10 08:40:17 +02002248 * efi_open_protocol_information() - provide information about then open status
2249 * of a protocol on a handle
2250 * @handle: handle for which the information shall be retrieved
2251 * @protocol: GUID of the protocol
2252 * @entry_buffer: buffer to receive the open protocol information
2253 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002254 *
2255 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002256 *
2257 * See the Unified Extensible Firmware Interface (UEFI) specification for
2258 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002259 *
Mario Six8fac2912018-07-10 08:40:17 +02002260 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002261 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002262static efi_status_t EFIAPI efi_open_protocol_information(
2263 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002264 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002265 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002266{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002267 unsigned long buffer_size;
2268 unsigned long count;
2269 struct efi_handler *handler;
2270 struct efi_open_protocol_info_item *item;
2271 efi_status_t r;
2272
Rob Clark238f88c2017-09-13 18:05:41 -04002273 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002274 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002275
2276 /* Check parameters */
2277 if (!entry_buffer) {
2278 r = EFI_INVALID_PARAMETER;
2279 goto out;
2280 }
2281 r = efi_search_protocol(handle, protocol, &handler);
2282 if (r != EFI_SUCCESS)
2283 goto out;
2284
2285 /* Count entries */
2286 count = 0;
2287 list_for_each_entry(item, &handler->open_infos, link) {
2288 if (item->info.open_count)
2289 ++count;
2290 }
2291 *entry_count = count;
2292 *entry_buffer = NULL;
2293 if (!count) {
2294 r = EFI_SUCCESS;
2295 goto out;
2296 }
2297
2298 /* Copy entries */
2299 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002300 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002301 (void **)entry_buffer);
2302 if (r != EFI_SUCCESS)
2303 goto out;
2304 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2305 if (item->info.open_count)
2306 (*entry_buffer)[--count] = item->info;
2307 }
2308out:
2309 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002310}
2311
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002312/**
Mario Six8fac2912018-07-10 08:40:17 +02002313 * efi_protocols_per_handle() - get protocols installed on a handle
2314 * @handle: handle for which the information is retrieved
2315 * @protocol_buffer: buffer with protocol GUIDs
2316 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002317 *
2318 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002319 *
Mario Six8fac2912018-07-10 08:40:17 +02002320 * See the Unified Extensible Firmware Interface (UEFI) specification for
2321 * details.
2322 *
2323 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002324 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002325static efi_status_t EFIAPI efi_protocols_per_handle(
2326 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002327 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002328{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002329 unsigned long buffer_size;
2330 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002331 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002332 efi_status_t r;
2333
Alexander Grafc15d9212016-03-04 01:09:59 +01002334 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2335 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002336
2337 if (!handle || !protocol_buffer || !protocol_buffer_count)
2338 return EFI_EXIT(EFI_INVALID_PARAMETER);
2339
2340 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002341 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002342
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002343 efiobj = efi_search_obj(handle);
2344 if (!efiobj)
2345 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002346
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002347 /* Count protocols */
2348 list_for_each(protocol_handle, &efiobj->protocols) {
2349 ++*protocol_buffer_count;
2350 }
2351
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002352 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002353 if (*protocol_buffer_count) {
2354 size_t j = 0;
2355
2356 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002357 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002358 (void **)protocol_buffer);
2359 if (r != EFI_SUCCESS)
2360 return EFI_EXIT(r);
2361 list_for_each(protocol_handle, &efiobj->protocols) {
2362 struct efi_handler *protocol;
2363
2364 protocol = list_entry(protocol_handle,
2365 struct efi_handler, link);
2366 (*protocol_buffer)[j] = (void *)protocol->guid;
2367 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002368 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002369 }
2370
2371 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002372}
2373
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002374/**
Mario Six8fac2912018-07-10 08:40:17 +02002375 * efi_locate_handle_buffer() - locate handles implementing a protocol
2376 * @search_type: selection criterion
2377 * @protocol: GUID of the protocol
2378 * @search_key: registration key
2379 * @no_handles: number of returned handles
2380 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002381 *
2382 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002383 *
Mario Six8fac2912018-07-10 08:40:17 +02002384 * See the Unified Extensible Firmware Interface (UEFI) specification for
2385 * details.
2386 *
2387 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002388 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002389efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002390 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002391 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002392 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002393{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002394 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002395 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002396
Rob Clark238f88c2017-09-13 18:05:41 -04002397 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002398 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002399
2400 if (!no_handles || !buffer) {
2401 r = EFI_INVALID_PARAMETER;
2402 goto out;
2403 }
2404 *no_handles = 0;
2405 *buffer = NULL;
2406 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2407 *buffer);
2408 if (r != EFI_BUFFER_TOO_SMALL)
2409 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002410 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002411 (void **)buffer);
2412 if (r != EFI_SUCCESS)
2413 goto out;
2414 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2415 *buffer);
2416 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002417 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002418out:
2419 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002420}
2421
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002422/**
Mario Six8fac2912018-07-10 08:40:17 +02002423 * efi_locate_protocol() - find an interface implementing a protocol
2424 * @protocol: GUID of the protocol
2425 * @registration: registration key passed to the notification function
2426 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002427 *
2428 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002429 *
2430 * See the Unified Extensible Firmware Interface (UEFI) specification for
2431 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002432 *
Mario Six8fac2912018-07-10 08:40:17 +02002433 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002434 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002435static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002436 void *registration,
2437 void **protocol_interface)
2438{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002439 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002440 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002441 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002442
Rob Clark238f88c2017-09-13 18:05:41 -04002443 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002444
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002445 /*
2446 * The UEFI spec explicitly requires a protocol even if a registration
2447 * key is provided. This differs from the logic in LocateHandle().
2448 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002449 if (!protocol || !protocol_interface)
2450 return EFI_EXIT(EFI_INVALID_PARAMETER);
2451
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002452 if (registration) {
2453 struct efi_register_notify_event *event;
2454 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002455
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002456 event = efi_check_register_notify_event(registration);
2457 if (!event)
2458 return EFI_EXIT(EFI_INVALID_PARAMETER);
2459 /*
2460 * The UEFI spec requires to return EFI_NOT_FOUND if no
2461 * protocol instance matches protocol and registration.
2462 * So let's do the same for a mismatch between protocol and
2463 * registration.
2464 */
2465 if (guidcmp(&event->protocol, protocol))
2466 goto not_found;
2467 if (list_empty(&event->handles))
2468 goto not_found;
2469 handle = list_first_entry(&event->handles,
2470 struct efi_protocol_notification,
2471 link);
2472 efiobj = handle->handle;
2473 list_del(&handle->link);
2474 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002475 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002476 if (ret == EFI_SUCCESS)
2477 goto found;
2478 } else {
2479 list_for_each_entry(efiobj, &efi_obj_list, link) {
2480 ret = efi_search_protocol(efiobj, protocol, &handler);
2481 if (ret == EFI_SUCCESS)
2482 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002483 }
2484 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002485not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002486 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002487 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002488found:
2489 *protocol_interface = handler->protocol_interface;
2490 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002491}
2492
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002493/**
Mario Six8fac2912018-07-10 08:40:17 +02002494 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2495 * interfaces
2496 * @handle: handle on which the protocol interfaces shall be installed
2497 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2498 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002499 *
2500 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002501 *
2502 * See the Unified Extensible Firmware Interface (UEFI) specification for
2503 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002504 *
Mario Six8fac2912018-07-10 08:40:17 +02002505 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002506 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002507efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002508 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002509{
2510 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002511
Alexander Graf404a7c82018-06-18 17:23:05 +02002512 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002513 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002514 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002515 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002516 efi_status_t r = EFI_SUCCESS;
2517 int i = 0;
2518
2519 if (!handle)
2520 return EFI_EXIT(EFI_INVALID_PARAMETER);
2521
Alexander Graf404a7c82018-06-18 17:23:05 +02002522 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002523 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002524 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002525 if (!protocol)
2526 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002527 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002528 /* Check that a device path has not been installed before */
2529 if (!guidcmp(protocol, &efi_guid_device_path)) {
2530 struct efi_device_path *dp = protocol_interface;
2531
2532 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2533 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002534 if (r == EFI_SUCCESS &&
2535 dp->type == DEVICE_PATH_TYPE_END) {
2536 EFI_PRINT("Path %pD already installed\n",
2537 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002538 r = EFI_ALREADY_STARTED;
2539 break;
2540 }
2541 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002542 r = EFI_CALL(efi_install_protocol_interface(
2543 handle, protocol,
2544 EFI_NATIVE_INTERFACE,
2545 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002546 if (r != EFI_SUCCESS)
2547 break;
2548 i++;
2549 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002550 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002551 if (r == EFI_SUCCESS)
2552 return EFI_EXIT(r);
2553
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002554 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002555 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002556 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002557 protocol = efi_va_arg(argptr, efi_guid_t*);
2558 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002559 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002560 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002561 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002562 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002563
2564 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002565}
2566
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002567/**
Mario Six8fac2912018-07-10 08:40:17 +02002568 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2569 * interfaces
2570 * @handle: handle from which the protocol interfaces shall be removed
2571 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2572 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002573 *
2574 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002575 *
Mario Six8fac2912018-07-10 08:40:17 +02002576 * See the Unified Extensible Firmware Interface (UEFI) specification for
2577 * details.
2578 *
2579 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002580 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002581static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002582 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002583{
2584 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002585
Alexander Graf404a7c82018-06-18 17:23:05 +02002586 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002587 const efi_guid_t *protocol;
2588 void *protocol_interface;
2589 efi_status_t r = EFI_SUCCESS;
2590 size_t i = 0;
2591
2592 if (!handle)
2593 return EFI_EXIT(EFI_INVALID_PARAMETER);
2594
Alexander Graf404a7c82018-06-18 17:23:05 +02002595 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002596 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002597 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002598 if (!protocol)
2599 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002600 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002601 r = efi_uninstall_protocol(handle, protocol,
2602 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002603 if (r != EFI_SUCCESS)
2604 break;
2605 i++;
2606 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002607 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002608 if (r == EFI_SUCCESS) {
2609 /* If the last protocol has been removed, delete the handle. */
2610 if (list_empty(&handle->protocols)) {
2611 list_del(&handle->link);
2612 free(handle);
2613 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002614 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002615 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002616
2617 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002618 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002619 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002620 protocol = efi_va_arg(argptr, efi_guid_t*);
2621 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002622 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2623 EFI_NATIVE_INTERFACE,
2624 protocol_interface));
2625 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002626 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002627
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002628 /* In case of an error always return EFI_INVALID_PARAMETER */
2629 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002630}
2631
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002632/**
Mario Six8fac2912018-07-10 08:40:17 +02002633 * efi_calculate_crc32() - calculate cyclic redundancy code
2634 * @data: buffer with data
2635 * @data_size: size of buffer in bytes
2636 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002637 *
2638 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002639 *
2640 * See the Unified Extensible Firmware Interface (UEFI) specification for
2641 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002642 *
Mario Six8fac2912018-07-10 08:40:17 +02002643 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002644 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002645static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2646 efi_uintn_t data_size,
2647 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002648{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002649 efi_status_t ret = EFI_SUCCESS;
2650
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002651 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002652 if (!data || !data_size || !crc32_p) {
2653 ret = EFI_INVALID_PARAMETER;
2654 goto out;
2655 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002656 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002657out:
2658 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002659}
2660
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002661/**
Mario Six8fac2912018-07-10 08:40:17 +02002662 * efi_copy_mem() - copy memory
2663 * @destination: destination of the copy operation
2664 * @source: source of the copy operation
2665 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002666 *
2667 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002668 *
Mario Six8fac2912018-07-10 08:40:17 +02002669 * See the Unified Extensible Firmware Interface (UEFI) specification for
2670 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002671 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002672static void EFIAPI efi_copy_mem(void *destination, const void *source,
2673 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002674{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002675 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002676 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002677 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002678}
2679
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002680/**
Mario Six8fac2912018-07-10 08:40:17 +02002681 * efi_set_mem() - Fill memory with a byte value.
2682 * @buffer: buffer to fill
2683 * @size: size of buffer in bytes
2684 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002685 *
2686 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002687 *
Mario Six8fac2912018-07-10 08:40:17 +02002688 * See the Unified Extensible Firmware Interface (UEFI) specification for
2689 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002690 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002691static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002692{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002693 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002694 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002695 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002696}
2697
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002698/**
Mario Six8fac2912018-07-10 08:40:17 +02002699 * efi_protocol_open() - open protocol interface on a handle
2700 * @handler: handler of a protocol
2701 * @protocol_interface: interface implementing the protocol
2702 * @agent_handle: handle of the driver
2703 * @controller_handle: handle of the controller
2704 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002705 *
Mario Six8fac2912018-07-10 08:40:17 +02002706 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002707 */
2708static efi_status_t efi_protocol_open(
2709 struct efi_handler *handler,
2710 void **protocol_interface, void *agent_handle,
2711 void *controller_handle, uint32_t attributes)
2712{
2713 struct efi_open_protocol_info_item *item;
2714 struct efi_open_protocol_info_entry *match = NULL;
2715 bool opened_by_driver = false;
2716 bool opened_exclusive = false;
2717
2718 /* If there is no agent, only return the interface */
2719 if (!agent_handle)
2720 goto out;
2721
2722 /* For TEST_PROTOCOL ignore interface attribute */
2723 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2724 *protocol_interface = NULL;
2725
2726 /*
2727 * Check if the protocol is already opened by a driver with the same
2728 * attributes or opened exclusively
2729 */
2730 list_for_each_entry(item, &handler->open_infos, link) {
2731 if (item->info.agent_handle == agent_handle) {
2732 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2733 (item->info.attributes == attributes))
2734 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002735 } else {
2736 if (item->info.attributes &
2737 EFI_OPEN_PROTOCOL_BY_DRIVER)
2738 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002739 }
2740 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2741 opened_exclusive = true;
2742 }
2743
2744 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002745 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2746 if (opened_exclusive)
2747 return EFI_ACCESS_DENIED;
2748 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2749 if (opened_exclusive || opened_by_driver)
2750 return EFI_ACCESS_DENIED;
2751 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002752
2753 /* Prepare exclusive opening */
2754 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2755 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002756disconnect_next:
2757 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002758 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002759 efi_status_t ret;
2760
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002761 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002762 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2763 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002764 item->info.controller_handle,
2765 item->info.agent_handle,
2766 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002767 if (ret == EFI_SUCCESS)
2768 /*
2769 * Child controllers may have been
2770 * removed from the open_infos list. So
2771 * let's restart the loop.
2772 */
2773 goto disconnect_next;
2774 else
2775 opened_by_driver = true;
2776 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002777 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002778 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002779 if (opened_by_driver)
2780 return EFI_ACCESS_DENIED;
2781 }
2782
2783 /* Find existing entry */
2784 list_for_each_entry(item, &handler->open_infos, link) {
2785 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002786 item->info.controller_handle == controller_handle &&
2787 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002788 match = &item->info;
2789 }
2790 /* None found, create one */
2791 if (!match) {
2792 match = efi_create_open_info(handler);
2793 if (!match)
2794 return EFI_OUT_OF_RESOURCES;
2795 }
2796
2797 match->agent_handle = agent_handle;
2798 match->controller_handle = controller_handle;
2799 match->attributes = attributes;
2800 match->open_count++;
2801
2802out:
2803 /* For TEST_PROTOCOL ignore interface attribute. */
2804 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2805 *protocol_interface = handler->protocol_interface;
2806
2807 return EFI_SUCCESS;
2808}
2809
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002810/**
Mario Six8fac2912018-07-10 08:40:17 +02002811 * efi_open_protocol() - open protocol interface on a handle
2812 * @handle: handle on which the protocol shall be opened
2813 * @protocol: GUID of the protocol
2814 * @protocol_interface: interface implementing the protocol
2815 * @agent_handle: handle of the driver
2816 * @controller_handle: handle of the controller
2817 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002818 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002819 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002820 *
Mario Six8fac2912018-07-10 08:40:17 +02002821 * See the Unified Extensible Firmware Interface (UEFI) specification for
2822 * details.
2823 *
2824 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002825 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002826static efi_status_t EFIAPI efi_open_protocol
2827 (efi_handle_t handle, const efi_guid_t *protocol,
2828 void **protocol_interface, efi_handle_t agent_handle,
2829 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002830{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002831 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002832 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002833
Rob Clark238f88c2017-09-13 18:05:41 -04002834 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002835 protocol_interface, agent_handle, controller_handle,
2836 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002837
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002838 if (!handle || !protocol ||
2839 (!protocol_interface && attributes !=
2840 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002841 goto out;
2842 }
2843
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002844 switch (attributes) {
2845 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2846 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2847 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2848 break;
2849 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2850 if (controller_handle == handle)
2851 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002852 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002853 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2854 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002855 /* Check that the controller handle is valid */
2856 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002857 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002858 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002859 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002860 /* Check that the agent handle is valid */
2861 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002862 goto out;
2863 break;
2864 default:
2865 goto out;
2866 }
2867
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002868 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002869 switch (r) {
2870 case EFI_SUCCESS:
2871 break;
2872 case EFI_NOT_FOUND:
2873 r = EFI_UNSUPPORTED;
2874 goto out;
2875 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002876 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002877 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002878
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002879 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2880 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002881out:
2882 return EFI_EXIT(r);
2883}
2884
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002885/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002886 * efi_start_image() - call the entry point of an image
2887 * @image_handle: handle of the image
2888 * @exit_data_size: size of the buffer
2889 * @exit_data: buffer to receive the exit data of the called image
2890 *
2891 * This function implements the StartImage service.
2892 *
2893 * See the Unified Extensible Firmware Interface (UEFI) specification for
2894 * details.
2895 *
2896 * Return: status code
2897 */
2898efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2899 efi_uintn_t *exit_data_size,
2900 u16 **exit_data)
2901{
2902 struct efi_loaded_image_obj *image_obj =
2903 (struct efi_loaded_image_obj *)image_handle;
2904 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002905 void *info;
2906 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002907
2908 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2909
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002910 if (!efi_search_obj(image_handle))
2911 return EFI_EXIT(EFI_INVALID_PARAMETER);
2912
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002913 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002914 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2915 return EFI_EXIT(EFI_INVALID_PARAMETER);
2916
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002917 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2918 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2919
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002920 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2921 &info, NULL, NULL,
2922 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2923 if (ret != EFI_SUCCESS)
2924 return EFI_EXIT(EFI_INVALID_PARAMETER);
2925
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002926 image_obj->exit_data_size = exit_data_size;
2927 image_obj->exit_data = exit_data;
2928
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002929 /* call the image! */
2930 if (setjmp(&image_obj->exit_jmp)) {
2931 /*
2932 * We called the entry point of the child image with EFI_CALL
2933 * in the lines below. The child image called the Exit() boot
2934 * service efi_exit() which executed the long jump that brought
2935 * us to the current line. This implies that the second half
2936 * of the EFI_CALL macro has not been executed.
2937 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02002938#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002939 /*
2940 * efi_exit() called efi_restore_gd(). We have to undo this
2941 * otherwise __efi_entry_check() will put the wrong value into
2942 * app_gd.
2943 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02002944 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002945#endif
2946 /*
2947 * To get ready to call EFI_EXIT below we have to execute the
2948 * missed out steps of EFI_CALL.
2949 */
2950 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002951 EFI_PRINT("%lu returned by started image\n",
2952 (unsigned long)((uintptr_t)image_obj->exit_status &
2953 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002954 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002955 return EFI_EXIT(image_obj->exit_status);
2956 }
2957
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002958 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002959 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002960 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002961 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2962
2963 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01002964 * Control is returned from a started UEFI image either by calling
2965 * Exit() (where exit data can be provided) or by simply returning from
2966 * the entry point. In the latter case call Exit() on behalf of the
2967 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002968 */
2969 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2970}
2971
2972/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002973 * efi_delete_image() - delete loaded image from memory)
2974 *
2975 * @image_obj: handle of the loaded image
2976 * @loaded_image_protocol: loaded image protocol
2977 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002978static efi_status_t efi_delete_image
2979 (struct efi_loaded_image_obj *image_obj,
2980 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002981{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002982 struct efi_object *efiobj;
2983 efi_status_t r, ret = EFI_SUCCESS;
2984
2985close_next:
2986 list_for_each_entry(efiobj, &efi_obj_list, link) {
2987 struct efi_handler *protocol;
2988
2989 list_for_each_entry(protocol, &efiobj->protocols, link) {
2990 struct efi_open_protocol_info_item *info;
2991
2992 list_for_each_entry(info, &protocol->open_infos, link) {
2993 if (info->info.agent_handle !=
2994 (efi_handle_t)image_obj)
2995 continue;
2996 r = EFI_CALL(efi_close_protocol
2997 (efiobj, protocol->guid,
2998 info->info.agent_handle,
2999 info->info.controller_handle
3000 ));
3001 if (r != EFI_SUCCESS)
3002 ret = r;
3003 /*
3004 * Closing protocols may results in further
3005 * items being deleted. To play it safe loop
3006 * over all elements again.
3007 */
3008 goto close_next;
3009 }
3010 }
3011 }
3012
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003013 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3014 efi_size_in_pages(loaded_image_protocol->image_size));
3015 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003016
3017 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003018}
3019
3020/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003021 * efi_unload_image() - unload an EFI image
3022 * @image_handle: handle of the image to be unloaded
3023 *
3024 * This function implements the UnloadImage service.
3025 *
3026 * See the Unified Extensible Firmware Interface (UEFI) specification for
3027 * details.
3028 *
3029 * Return: status code
3030 */
3031efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3032{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003033 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003034 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003035 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003036
3037 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003038
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003039 efiobj = efi_search_obj(image_handle);
3040 if (!efiobj) {
3041 ret = EFI_INVALID_PARAMETER;
3042 goto out;
3043 }
3044 /* Find the loaded image protocol */
3045 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3046 (void **)&loaded_image_protocol,
3047 NULL, NULL,
3048 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3049 if (ret != EFI_SUCCESS) {
3050 ret = EFI_INVALID_PARAMETER;
3051 goto out;
3052 }
3053 switch (efiobj->type) {
3054 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3055 /* Call the unload function */
3056 if (!loaded_image_protocol->unload) {
3057 ret = EFI_UNSUPPORTED;
3058 goto out;
3059 }
3060 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3061 if (ret != EFI_SUCCESS)
3062 goto out;
3063 break;
3064 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3065 break;
3066 default:
3067 ret = EFI_INVALID_PARAMETER;
3068 goto out;
3069 }
3070 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3071 loaded_image_protocol);
3072out:
3073 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003074}
3075
3076/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003077 * efi_update_exit_data() - fill exit data parameters of StartImage()
3078 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003079 * @image_obj: image handle
3080 * @exit_data_size: size of the exit data buffer
3081 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003082 * Return: status code
3083 */
3084static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3085 efi_uintn_t exit_data_size,
3086 u16 *exit_data)
3087{
3088 efi_status_t ret;
3089
3090 /*
3091 * If exit_data is not provided to StartImage(), exit_data_size must be
3092 * ignored.
3093 */
3094 if (!image_obj->exit_data)
3095 return EFI_SUCCESS;
3096 if (image_obj->exit_data_size)
3097 *image_obj->exit_data_size = exit_data_size;
3098 if (exit_data_size && exit_data) {
3099 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3100 exit_data_size,
3101 (void **)image_obj->exit_data);
3102 if (ret != EFI_SUCCESS)
3103 return ret;
3104 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3105 } else {
3106 image_obj->exit_data = NULL;
3107 }
3108 return EFI_SUCCESS;
3109}
3110
3111/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003112 * efi_exit() - leave an EFI application or driver
3113 * @image_handle: handle of the application or driver that is exiting
3114 * @exit_status: status code
3115 * @exit_data_size: size of the buffer in bytes
3116 * @exit_data: buffer with data describing an error
3117 *
3118 * This function implements the Exit service.
3119 *
3120 * See the Unified Extensible Firmware Interface (UEFI) specification for
3121 * details.
3122 *
3123 * Return: status code
3124 */
3125static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3126 efi_status_t exit_status,
3127 efi_uintn_t exit_data_size,
3128 u16 *exit_data)
3129{
3130 /*
3131 * TODO: We should call the unload procedure of the loaded
3132 * image protocol.
3133 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003134 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003135 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003136 struct efi_loaded_image_obj *image_obj =
3137 (struct efi_loaded_image_obj *)image_handle;
3138
3139 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3140 exit_data_size, exit_data);
3141
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003142 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003143 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003144 (void **)&loaded_image_protocol,
3145 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003146 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003147 if (ret != EFI_SUCCESS) {
3148 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003149 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003150 }
3151
3152 /* Unloading of unstarted images */
3153 switch (image_obj->header.type) {
3154 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3155 break;
3156 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3157 efi_delete_image(image_obj, loaded_image_protocol);
3158 ret = EFI_SUCCESS;
3159 goto out;
3160 default:
3161 /* Handle does not refer to loaded image */
3162 ret = EFI_INVALID_PARAMETER;
3163 goto out;
3164 }
3165 /* A started image can only be unloaded it is the last one started. */
3166 if (image_handle != current_image) {
3167 ret = EFI_INVALID_PARAMETER;
3168 goto out;
3169 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003170
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003171 /* Exit data is only foreseen in case of failure. */
3172 if (exit_status != EFI_SUCCESS) {
3173 ret = efi_update_exit_data(image_obj, exit_data_size,
3174 exit_data);
3175 /* Exiting has priority. Don't return error to caller. */
3176 if (ret != EFI_SUCCESS)
3177 EFI_PRINT("%s: out of memory\n", __func__);
3178 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003179 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3180 exit_status != EFI_SUCCESS)
3181 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003182
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003183 /* Make sure entry/exit counts for EFI world cross-overs match */
3184 EFI_EXIT(exit_status);
3185
3186 /*
3187 * But longjmp out with the U-Boot gd, not the application's, as
3188 * the other end is a setjmp call inside EFI context.
3189 */
3190 efi_restore_gd();
3191
3192 image_obj->exit_status = exit_status;
3193 longjmp(&image_obj->exit_jmp, 1);
3194
3195 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003196out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003197 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003198}
3199
3200/**
Mario Six8fac2912018-07-10 08:40:17 +02003201 * efi_handle_protocol() - get interface of a protocol on a handle
3202 * @handle: handle on which the protocol shall be opened
3203 * @protocol: GUID of the protocol
3204 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003205 *
3206 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003207 *
3208 * See the Unified Extensible Firmware Interface (UEFI) specification for
3209 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003210 *
Mario Six8fac2912018-07-10 08:40:17 +02003211 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003212 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003213efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3214 const efi_guid_t *protocol,
3215 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003216{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003217 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003218 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003219}
3220
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003221/**
Mario Six8fac2912018-07-10 08:40:17 +02003222 * efi_bind_controller() - bind a single driver to a controller
3223 * @controller_handle: controller handle
3224 * @driver_image_handle: driver handle
3225 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003226 *
Mario Six8fac2912018-07-10 08:40:17 +02003227 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003228 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003229static efi_status_t efi_bind_controller(
3230 efi_handle_t controller_handle,
3231 efi_handle_t driver_image_handle,
3232 struct efi_device_path *remain_device_path)
3233{
3234 struct efi_driver_binding_protocol *binding_protocol;
3235 efi_status_t r;
3236
3237 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3238 &efi_guid_driver_binding_protocol,
3239 (void **)&binding_protocol,
3240 driver_image_handle, NULL,
3241 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3242 if (r != EFI_SUCCESS)
3243 return r;
3244 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3245 controller_handle,
3246 remain_device_path));
3247 if (r == EFI_SUCCESS)
3248 r = EFI_CALL(binding_protocol->start(binding_protocol,
3249 controller_handle,
3250 remain_device_path));
3251 EFI_CALL(efi_close_protocol(driver_image_handle,
3252 &efi_guid_driver_binding_protocol,
3253 driver_image_handle, NULL));
3254 return r;
3255}
3256
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003257/**
Mario Six8fac2912018-07-10 08:40:17 +02003258 * efi_connect_single_controller() - connect a single driver to a controller
3259 * @controller_handle: controller
3260 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003261 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003262 *
Mario Six8fac2912018-07-10 08:40:17 +02003263 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003264 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003265static efi_status_t efi_connect_single_controller(
3266 efi_handle_t controller_handle,
3267 efi_handle_t *driver_image_handle,
3268 struct efi_device_path *remain_device_path)
3269{
3270 efi_handle_t *buffer;
3271 size_t count;
3272 size_t i;
3273 efi_status_t r;
3274 size_t connected = 0;
3275
3276 /* Get buffer with all handles with driver binding protocol */
3277 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3278 &efi_guid_driver_binding_protocol,
3279 NULL, &count, &buffer));
3280 if (r != EFI_SUCCESS)
3281 return r;
3282
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003283 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003284 if (driver_image_handle) {
3285 for (; *driver_image_handle; ++driver_image_handle) {
3286 for (i = 0; i < count; ++i) {
3287 if (buffer[i] == *driver_image_handle) {
3288 buffer[i] = NULL;
3289 r = efi_bind_controller(
3290 controller_handle,
3291 *driver_image_handle,
3292 remain_device_path);
3293 /*
3294 * For drivers that do not support the
3295 * controller or are already connected
3296 * we receive an error code here.
3297 */
3298 if (r == EFI_SUCCESS)
3299 ++connected;
3300 }
3301 }
3302 }
3303 }
3304
3305 /*
3306 * TODO: Some overrides are not yet implemented:
3307 * - Platform Driver Override
3308 * - Driver Family Override Search
3309 * - Bus Specific Driver Override
3310 */
3311
3312 /* Driver Binding Search */
3313 for (i = 0; i < count; ++i) {
3314 if (buffer[i]) {
3315 r = efi_bind_controller(controller_handle,
3316 buffer[i],
3317 remain_device_path);
3318 if (r == EFI_SUCCESS)
3319 ++connected;
3320 }
3321 }
3322
3323 efi_free_pool(buffer);
3324 if (!connected)
3325 return EFI_NOT_FOUND;
3326 return EFI_SUCCESS;
3327}
3328
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003329/**
Mario Six8fac2912018-07-10 08:40:17 +02003330 * efi_connect_controller() - connect a controller to a driver
3331 * @controller_handle: handle of the controller
3332 * @driver_image_handle: handle of the driver
3333 * @remain_device_path: device path of a child controller
3334 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003335 *
3336 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003337 *
3338 * See the Unified Extensible Firmware Interface (UEFI) specification for
3339 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003340 *
3341 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003342 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003343 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3344 *
Mario Six8fac2912018-07-10 08:40:17 +02003345 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003346 */
3347static efi_status_t EFIAPI efi_connect_controller(
3348 efi_handle_t controller_handle,
3349 efi_handle_t *driver_image_handle,
3350 struct efi_device_path *remain_device_path,
3351 bool recursive)
3352{
3353 efi_status_t r;
3354 efi_status_t ret = EFI_NOT_FOUND;
3355 struct efi_object *efiobj;
3356
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003357 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003358 remain_device_path, recursive);
3359
3360 efiobj = efi_search_obj(controller_handle);
3361 if (!efiobj) {
3362 ret = EFI_INVALID_PARAMETER;
3363 goto out;
3364 }
3365
3366 r = efi_connect_single_controller(controller_handle,
3367 driver_image_handle,
3368 remain_device_path);
3369 if (r == EFI_SUCCESS)
3370 ret = EFI_SUCCESS;
3371 if (recursive) {
3372 struct efi_handler *handler;
3373 struct efi_open_protocol_info_item *item;
3374
3375 list_for_each_entry(handler, &efiobj->protocols, link) {
3376 list_for_each_entry(item, &handler->open_infos, link) {
3377 if (item->info.attributes &
3378 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3379 r = EFI_CALL(efi_connect_controller(
3380 item->info.controller_handle,
3381 driver_image_handle,
3382 remain_device_path,
3383 recursive));
3384 if (r == EFI_SUCCESS)
3385 ret = EFI_SUCCESS;
3386 }
3387 }
3388 }
3389 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003390 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003391 if (ret != EFI_SUCCESS && remain_device_path &&
3392 remain_device_path->type == DEVICE_PATH_TYPE_END)
3393 ret = EFI_SUCCESS;
3394out:
3395 return EFI_EXIT(ret);
3396}
3397
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003398/**
Mario Six8fac2912018-07-10 08:40:17 +02003399 * efi_reinstall_protocol_interface() - reinstall protocol interface
3400 * @handle: handle on which the protocol shall be reinstalled
3401 * @protocol: GUID of the protocol to be installed
3402 * @old_interface: interface to be removed
3403 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003404 *
3405 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003406 *
3407 * See the Unified Extensible Firmware Interface (UEFI) specification for
3408 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003409 *
3410 * The old interface is uninstalled. The new interface is installed.
3411 * Drivers are connected.
3412 *
Mario Six8fac2912018-07-10 08:40:17 +02003413 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003414 */
3415static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3416 efi_handle_t handle, const efi_guid_t *protocol,
3417 void *old_interface, void *new_interface)
3418{
3419 efi_status_t ret;
3420
3421 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3422 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003423
3424 /* Uninstall protocol but do not delete handle */
3425 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003426 if (ret != EFI_SUCCESS)
3427 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003428
3429 /* Install the new protocol */
3430 ret = efi_add_protocol(handle, protocol, new_interface);
3431 /*
3432 * The UEFI spec does not specify what should happen to the handle
3433 * if in case of an error no protocol interface remains on the handle.
3434 * So let's do nothing here.
3435 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003436 if (ret != EFI_SUCCESS)
3437 goto out;
3438 /*
3439 * The returned status code has to be ignored.
3440 * Do not create an error if no suitable driver for the handle exists.
3441 */
3442 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3443out:
3444 return EFI_EXIT(ret);
3445}
3446
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003447/**
Mario Six8fac2912018-07-10 08:40:17 +02003448 * efi_get_child_controllers() - get all child controllers associated to a driver
3449 * @efiobj: handle of the controller
3450 * @driver_handle: handle of the driver
3451 * @number_of_children: number of child controllers
3452 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003453 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003454 * The allocated buffer has to be freed with free().
3455 *
Mario Six8fac2912018-07-10 08:40:17 +02003456 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003457 */
3458static efi_status_t efi_get_child_controllers(
3459 struct efi_object *efiobj,
3460 efi_handle_t driver_handle,
3461 efi_uintn_t *number_of_children,
3462 efi_handle_t **child_handle_buffer)
3463{
3464 struct efi_handler *handler;
3465 struct efi_open_protocol_info_item *item;
3466 efi_uintn_t count = 0, i;
3467 bool duplicate;
3468
3469 /* Count all child controller associations */
3470 list_for_each_entry(handler, &efiobj->protocols, link) {
3471 list_for_each_entry(item, &handler->open_infos, link) {
3472 if (item->info.agent_handle == driver_handle &&
3473 item->info.attributes &
3474 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3475 ++count;
3476 }
3477 }
3478 /*
3479 * Create buffer. In case of duplicate child controller assignments
3480 * the buffer will be too large. But that does not harm.
3481 */
3482 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003483 if (!count)
3484 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003485 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3486 if (!*child_handle_buffer)
3487 return EFI_OUT_OF_RESOURCES;
3488 /* Copy unique child handles */
3489 list_for_each_entry(handler, &efiobj->protocols, link) {
3490 list_for_each_entry(item, &handler->open_infos, link) {
3491 if (item->info.agent_handle == driver_handle &&
3492 item->info.attributes &
3493 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3494 /* Check this is a new child controller */
3495 duplicate = false;
3496 for (i = 0; i < *number_of_children; ++i) {
3497 if ((*child_handle_buffer)[i] ==
3498 item->info.controller_handle)
3499 duplicate = true;
3500 }
3501 /* Copy handle to buffer */
3502 if (!duplicate) {
3503 i = (*number_of_children)++;
3504 (*child_handle_buffer)[i] =
3505 item->info.controller_handle;
3506 }
3507 }
3508 }
3509 }
3510 return EFI_SUCCESS;
3511}
3512
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003513/**
Mario Six8fac2912018-07-10 08:40:17 +02003514 * efi_disconnect_controller() - disconnect a controller from a driver
3515 * @controller_handle: handle of the controller
3516 * @driver_image_handle: handle of the driver
3517 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003518 *
3519 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003520 *
3521 * See the Unified Extensible Firmware Interface (UEFI) specification for
3522 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003523 *
Mario Six8fac2912018-07-10 08:40:17 +02003524 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003525 */
3526static efi_status_t EFIAPI efi_disconnect_controller(
3527 efi_handle_t controller_handle,
3528 efi_handle_t driver_image_handle,
3529 efi_handle_t child_handle)
3530{
3531 struct efi_driver_binding_protocol *binding_protocol;
3532 efi_handle_t *child_handle_buffer = NULL;
3533 size_t number_of_children = 0;
3534 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003535 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003536 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003537
3538 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3539 child_handle);
3540
3541 efiobj = efi_search_obj(controller_handle);
3542 if (!efiobj) {
3543 r = EFI_INVALID_PARAMETER;
3544 goto out;
3545 }
3546
3547 if (child_handle && !efi_search_obj(child_handle)) {
3548 r = EFI_INVALID_PARAMETER;
3549 goto out;
3550 }
3551
3552 /* If no driver handle is supplied, disconnect all drivers */
3553 if (!driver_image_handle) {
3554 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3555 goto out;
3556 }
3557
3558 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003559 r = efi_get_child_controllers(efiobj,
3560 driver_image_handle,
3561 &number_of_children,
3562 &child_handle_buffer);
3563 if (r != EFI_SUCCESS)
3564 return r;
3565 sole_child = (number_of_children == 1);
3566
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003567 if (child_handle) {
3568 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003569 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003570 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003571 }
3572
3573 /* Get the driver binding protocol */
3574 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3575 &efi_guid_driver_binding_protocol,
3576 (void **)&binding_protocol,
3577 driver_image_handle, NULL,
3578 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003579 if (r != EFI_SUCCESS) {
3580 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003581 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003582 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003583 /* Remove the children */
3584 if (number_of_children) {
3585 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3586 controller_handle,
3587 number_of_children,
3588 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003589 if (r != EFI_SUCCESS) {
3590 r = EFI_DEVICE_ERROR;
3591 goto out;
3592 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003593 }
3594 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003595 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003596 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3597 controller_handle,
3598 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003599 if (r != EFI_SUCCESS) {
3600 r = EFI_DEVICE_ERROR;
3601 goto out;
3602 }
3603 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003604 EFI_CALL(efi_close_protocol(driver_image_handle,
3605 &efi_guid_driver_binding_protocol,
3606 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003607 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003608out:
3609 if (!child_handle)
3610 free(child_handle_buffer);
3611 return EFI_EXIT(r);
3612}
3613
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003614static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003615 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003616 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3617 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003618 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003619 },
3620 .raise_tpl = efi_raise_tpl,
3621 .restore_tpl = efi_restore_tpl,
3622 .allocate_pages = efi_allocate_pages_ext,
3623 .free_pages = efi_free_pages_ext,
3624 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003625 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003626 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003627 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003628 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003629 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003630 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003631 .close_event = efi_close_event,
3632 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003633 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003634 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003635 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003636 .handle_protocol = efi_handle_protocol,
3637 .reserved = NULL,
3638 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003639 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003640 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003641 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003642 .load_image = efi_load_image,
3643 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003644 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003645 .unload_image = efi_unload_image,
3646 .exit_boot_services = efi_exit_boot_services,
3647 .get_next_monotonic_count = efi_get_next_monotonic_count,
3648 .stall = efi_stall,
3649 .set_watchdog_timer = efi_set_watchdog_timer,
3650 .connect_controller = efi_connect_controller,
3651 .disconnect_controller = efi_disconnect_controller,
3652 .open_protocol = efi_open_protocol,
3653 .close_protocol = efi_close_protocol,
3654 .open_protocol_information = efi_open_protocol_information,
3655 .protocols_per_handle = efi_protocols_per_handle,
3656 .locate_handle_buffer = efi_locate_handle_buffer,
3657 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003658 .install_multiple_protocol_interfaces =
3659 efi_install_multiple_protocol_interfaces,
3660 .uninstall_multiple_protocol_interfaces =
3661 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003662 .calculate_crc32 = efi_calculate_crc32,
3663 .copy_mem = efi_copy_mem,
3664 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003665 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003666};
3667
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003668static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003669
Alexander Graf393dd912016-10-14 13:45:30 +02003670struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003671 .hdr = {
3672 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003673 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003674 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003675 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003676 .fw_vendor = firmware_vendor,
3677 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003678 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003679 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003680 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003681};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003682
3683/**
3684 * efi_initialize_system_table() - Initialize system table
3685 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003686 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003687 */
3688efi_status_t efi_initialize_system_table(void)
3689{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003690 efi_status_t ret;
3691
3692 /* Allocate configuration table array */
3693 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3694 EFI_MAX_CONFIGURATION_TABLES *
3695 sizeof(struct efi_configuration_table),
3696 (void **)&systab.tables);
3697
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003698 /*
3699 * These entries will be set to NULL in ExitBootServices(). To avoid
3700 * relocation in SetVirtualAddressMap(), set them dynamically.
3701 */
3702 systab.con_in = &efi_con_in;
3703 systab.con_out = &efi_con_out;
3704 systab.std_err = &efi_con_out;
3705 systab.boottime = &efi_boot_services;
3706
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003707 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003708 efi_update_table_header_crc32(&systab.hdr);
3709 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3710 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003711
3712 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003713}