blob: d49145fc76b698a18217a87b42781aac87ae6ca1 [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>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070011#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <u-boot/crc.h>
17#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020018#include <pe.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010020#include <watchdog.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020024/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010025static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020026
Alexander Grafc15d9212016-03-04 01:09:59 +010027/* This list contains all the EFI objects our payload has access to */
28LIST_HEAD(efi_obj_list);
29
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010030/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020031__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010032
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020033/* List of queued events */
34LIST_HEAD(efi_event_queue);
35
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020036/* Flag to disable timer activity in ExitBootServices() */
37static bool timers_enabled = true;
38
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020039/* List of all events registered by RegisterProtocolNotify() */
40LIST_HEAD(efi_register_notify_events);
41
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010042/* Handle of the currently executing image */
43static efi_handle_t current_image;
44
Simon Glasscdfe6962016-09-25 15:27:35 -060045#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010046/*
47 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
48 * fixed when compiling U-Boot. However, the payload does not know about that
49 * restriction so we need to manually swap its and our view of that register on
50 * EFI callback entry/exit.
51 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020052static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060053#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010054
Heinrich Schuchardt72506232019-02-09 14:10:39 +010055/* 1 if inside U-Boot code, 0 if inside EFI payload code */
56static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040057static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010058/* GUID of the device tree table */
59const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010060/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
61const efi_guid_t efi_guid_driver_binding_protocol =
62 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040063
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010064/* event group ExitBootServices() invoked */
65const efi_guid_t efi_guid_event_group_exit_boot_services =
66 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
67/* event group SetVirtualAddressMap() invoked */
68const efi_guid_t efi_guid_event_group_virtual_address_change =
69 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
70/* event group memory map changed */
71const efi_guid_t efi_guid_event_group_memory_map_change =
72 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
73/* event group boot manager about to boot */
74const efi_guid_t efi_guid_event_group_ready_to_boot =
75 EFI_EVENT_GROUP_READY_TO_BOOT;
76/* event group ResetSystem() invoked (before ExitBootServices) */
77const efi_guid_t efi_guid_event_group_reset_system =
78 EFI_EVENT_GROUP_RESET_SYSTEM;
79
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010080static efi_status_t EFIAPI efi_disconnect_controller(
81 efi_handle_t controller_handle,
82 efi_handle_t driver_image_handle,
83 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010084
Rob Clark86789d52017-07-27 08:04:18 -040085/* Called on every callback entry */
86int __efi_entry_check(void)
87{
88 int ret = entry_count++ == 0;
89#ifdef CONFIG_ARM
90 assert(efi_gd);
91 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020092 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -040093#endif
94 return ret;
95}
96
97/* Called on every callback exit */
98int __efi_exit_check(void)
99{
100 int ret = --entry_count == 0;
101#ifdef CONFIG_ARM
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200102 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400103#endif
104 return ret;
105}
106
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200107/**
108 * efi_save_gd() - save global data register
109 *
110 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
111 * As this register may be overwritten by an EFI payload we save it here
112 * and restore it on every callback entered.
113 *
114 * This function is called after relocation from initr_reloc_global_data().
115 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100116void efi_save_gd(void)
117{
Simon Glasscdfe6962016-09-25 15:27:35 -0600118#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100119 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600120#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100121}
122
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200123/**
124 * efi_restore_gd() - restore global data register
125 *
126 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
127 * Restore it after returning from the UEFI world to the value saved via
128 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400129 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100130void efi_restore_gd(void)
131{
Simon Glasscdfe6962016-09-25 15:27:35 -0600132#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100133 /* Only restore if we're already in EFI context */
134 if (!efi_gd)
135 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200136 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600137#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100138}
139
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200140/**
Mario Six8fac2912018-07-10 08:40:17 +0200141 * indent_string() - returns a string for indenting with two spaces per level
142 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100143 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200144 * A maximum of ten indent levels is supported. Higher indent levels will be
145 * truncated.
146 *
Mario Six8fac2912018-07-10 08:40:17 +0200147 * Return: A string for indenting with two spaces per level is
148 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400149 */
150static const char *indent_string(int level)
151{
152 const char *indent = " ";
153 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100154
Rob Clarke7896c32017-07-27 08:04:19 -0400155 level = min(max, level * 2);
156 return &indent[max - level];
157}
158
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200159const char *__efi_nesting(void)
160{
161 return indent_string(nesting_level);
162}
163
Rob Clarke7896c32017-07-27 08:04:19 -0400164const char *__efi_nesting_inc(void)
165{
166 return indent_string(nesting_level++);
167}
168
169const char *__efi_nesting_dec(void)
170{
171 return indent_string(--nesting_level);
172}
173
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200174/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200175 * efi_event_is_queued() - check if an event is queued
176 *
177 * @event: event
178 * Return: true if event is queued
179 */
180static bool efi_event_is_queued(struct efi_event *event)
181{
182 return !!event->queue_link.next;
183}
184
185/**
186 * efi_process_event_queue() - process event queue
187 */
188static void efi_process_event_queue(void)
189{
190 while (!list_empty(&efi_event_queue)) {
191 struct efi_event *event;
192 efi_uintn_t old_tpl;
193
194 event = list_first_entry(&efi_event_queue, struct efi_event,
195 queue_link);
196 if (efi_tpl >= event->notify_tpl)
197 return;
198 list_del(&event->queue_link);
199 event->queue_link.next = NULL;
200 event->queue_link.prev = NULL;
201 /* Events must be executed at the event's TPL */
202 old_tpl = efi_tpl;
203 efi_tpl = event->notify_tpl;
204 EFI_CALL_VOID(event->notify_function(event,
205 event->notify_context));
206 efi_tpl = old_tpl;
207 if (event->type == EVT_NOTIFY_SIGNAL)
208 event->is_signaled = 0;
209 }
210}
211
212/**
Mario Six8fac2912018-07-10 08:40:17 +0200213 * efi_queue_event() - queue an EFI event
214 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200215 *
216 * This function queues the notification function of the event for future
217 * execution.
218 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200219 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200220static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200221{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100222 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200223
224 if (!event->notify_function)
225 return;
226
227 if (!efi_event_is_queued(event)) {
228 /*
229 * Events must be notified in order of decreasing task priority
230 * level. Insert the new event accordingly.
231 */
232 list_for_each_entry(item, &efi_event_queue, queue_link) {
233 if (item->notify_tpl < event->notify_tpl) {
234 list_add_tail(&event->queue_link,
235 &item->queue_link);
236 event = NULL;
237 break;
238 }
239 }
240 if (event)
241 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200242 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200243 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200244}
245
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200246/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200247 * is_valid_tpl() - check if the task priority level is valid
248 *
249 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200250 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200251 */
252efi_status_t is_valid_tpl(efi_uintn_t tpl)
253{
254 switch (tpl) {
255 case TPL_APPLICATION:
256 case TPL_CALLBACK:
257 case TPL_NOTIFY:
258 case TPL_HIGH_LEVEL:
259 return EFI_SUCCESS;
260 default:
261 return EFI_INVALID_PARAMETER;
262 }
263}
264
265/**
Mario Six8fac2912018-07-10 08:40:17 +0200266 * efi_signal_event() - signal an EFI event
267 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100268 *
Mario Six8fac2912018-07-10 08:40:17 +0200269 * This function signals an event. If the event belongs to an event group all
270 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100271 * their notification function is queued.
272 *
273 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100274 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200275void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100276{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200277 if (event->is_signaled)
278 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100279 if (event->group) {
280 struct efi_event *evt;
281
282 /*
283 * The signaled state has to set before executing any
284 * notification function
285 */
286 list_for_each_entry(evt, &efi_events, link) {
287 if (!evt->group || guidcmp(evt->group, event->group))
288 continue;
289 if (evt->is_signaled)
290 continue;
291 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100292 }
293 list_for_each_entry(evt, &efi_events, link) {
294 if (!evt->group || guidcmp(evt->group, event->group))
295 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200296 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100297 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200298 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100299 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200300 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100301 }
302}
303
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200304/**
Mario Six8fac2912018-07-10 08:40:17 +0200305 * efi_raise_tpl() - raise the task priority level
306 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200307 *
308 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200309 *
310 * See the Unified Extensible Firmware Interface (UEFI) specification for
311 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200312 *
Mario Six8fac2912018-07-10 08:40:17 +0200313 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200314 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100315static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100316{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100317 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200318
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200319 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200320
321 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200322 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200323 efi_tpl = new_tpl;
324 if (efi_tpl > TPL_HIGH_LEVEL)
325 efi_tpl = TPL_HIGH_LEVEL;
326
327 EFI_EXIT(EFI_SUCCESS);
328 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100329}
330
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200331/**
Mario Six8fac2912018-07-10 08:40:17 +0200332 * efi_restore_tpl() - lower the task priority level
333 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200334 *
335 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200336 *
Mario Six8fac2912018-07-10 08:40:17 +0200337 * See the Unified Extensible Firmware Interface (UEFI) specification for
338 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200339 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100340static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100341{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200342 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200343
344 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200345 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200346 efi_tpl = old_tpl;
347 if (efi_tpl > TPL_HIGH_LEVEL)
348 efi_tpl = TPL_HIGH_LEVEL;
349
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100350 /*
351 * Lowering the TPL may have made queued events eligible for execution.
352 */
353 efi_timer_check();
354
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200355 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100356}
357
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200358/**
Mario Six8fac2912018-07-10 08:40:17 +0200359 * efi_allocate_pages_ext() - allocate memory pages
360 * @type: type of allocation to be performed
361 * @memory_type: usage type of the allocated memory
362 * @pages: number of pages to be allocated
363 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200364 *
365 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 *
Mario Six8fac2912018-07-10 08:40:17 +0200367 * See the Unified Extensible Firmware Interface (UEFI) specification for
368 * details.
369 *
370 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200371 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900372static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100373 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900374 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100375{
376 efi_status_t r;
377
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100378 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100379 r = efi_allocate_pages(type, memory_type, pages, memory);
380 return EFI_EXIT(r);
381}
382
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200383/**
Mario Six8fac2912018-07-10 08:40:17 +0200384 * efi_free_pages_ext() - Free memory pages.
385 * @memory: start of the memory area to be freed
386 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200387 *
388 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200389 *
390 * See the Unified Extensible Firmware Interface (UEFI) specification for
391 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200392 *
Mario Six8fac2912018-07-10 08:40:17 +0200393 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200394 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900395static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100396 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100397{
398 efi_status_t r;
399
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900400 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100401 r = efi_free_pages(memory, pages);
402 return EFI_EXIT(r);
403}
404
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200405/**
Mario Six8fac2912018-07-10 08:40:17 +0200406 * efi_get_memory_map_ext() - get map describing memory usage
407 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
408 * on exit the size of the copied memory map
409 * @memory_map: buffer to which the memory map is written
410 * @map_key: key for the memory map
411 * @descriptor_size: size of an individual memory descriptor
412 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200413 *
414 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200415 *
Mario Six8fac2912018-07-10 08:40:17 +0200416 * See the Unified Extensible Firmware Interface (UEFI) specification for
417 * details.
418 *
419 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200420 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900421static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100422 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900423 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100424 efi_uintn_t *map_key,
425 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900426 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100427{
428 efi_status_t r;
429
430 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
431 map_key, descriptor_size, descriptor_version);
432 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
433 descriptor_size, descriptor_version);
434 return EFI_EXIT(r);
435}
436
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200437/**
Mario Six8fac2912018-07-10 08:40:17 +0200438 * efi_allocate_pool_ext() - allocate memory from pool
439 * @pool_type: type of the pool from which memory is to be allocated
440 * @size: number of bytes to be allocated
441 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200442 *
443 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200444 *
445 * See the Unified Extensible Firmware Interface (UEFI) specification for
446 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200447 *
Mario Six8fac2912018-07-10 08:40:17 +0200448 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200449 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200450static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100451 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200452 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100453{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100454 efi_status_t r;
455
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100456 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200457 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100458 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100459}
460
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200461/**
Mario Six8fac2912018-07-10 08:40:17 +0200462 * efi_free_pool_ext() - free memory from pool
463 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200464 *
465 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200466 *
Mario Six8fac2912018-07-10 08:40:17 +0200467 * See the Unified Extensible Firmware Interface (UEFI) specification for
468 * details.
469 *
470 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200471 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200472static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100473{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100474 efi_status_t r;
475
476 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200477 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100478 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100479}
480
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200481/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200482 * efi_add_handle() - add a new handle to the object list
483 *
484 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100485 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200486 * The protocols list is initialized. The handle is added to the list of known
487 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100488 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200489void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100490{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200491 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100492 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200493 INIT_LIST_HEAD(&handle->protocols);
494 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100495}
496
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200497/**
Mario Six8fac2912018-07-10 08:40:17 +0200498 * efi_create_handle() - create handle
499 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200500 *
Mario Six8fac2912018-07-10 08:40:17 +0200501 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200502 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100503efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200504{
505 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200506
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200507 obj = calloc(1, sizeof(struct efi_object));
508 if (!obj)
509 return EFI_OUT_OF_RESOURCES;
510
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100511 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200512 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200513
514 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200515}
516
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200517/**
Mario Six8fac2912018-07-10 08:40:17 +0200518 * efi_search_protocol() - find a protocol on a handle.
519 * @handle: handle
520 * @protocol_guid: GUID of the protocol
521 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100522 *
Mario Six8fac2912018-07-10 08:40:17 +0200523 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100525efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526 const efi_guid_t *protocol_guid,
527 struct efi_handler **handler)
528{
529 struct efi_object *efiobj;
530 struct list_head *lhandle;
531
532 if (!handle || !protocol_guid)
533 return EFI_INVALID_PARAMETER;
534 efiobj = efi_search_obj(handle);
535 if (!efiobj)
536 return EFI_INVALID_PARAMETER;
537 list_for_each(lhandle, &efiobj->protocols) {
538 struct efi_handler *protocol;
539
540 protocol = list_entry(lhandle, struct efi_handler, link);
541 if (!guidcmp(protocol->guid, protocol_guid)) {
542 if (handler)
543 *handler = protocol;
544 return EFI_SUCCESS;
545 }
546 }
547 return EFI_NOT_FOUND;
548}
549
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200550/**
Mario Six8fac2912018-07-10 08:40:17 +0200551 * efi_remove_protocol() - delete protocol from a handle
552 * @handle: handle from which the protocol shall be deleted
553 * @protocol: GUID of the protocol to be deleted
554 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100555 *
Mario Six8fac2912018-07-10 08:40:17 +0200556 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100558efi_status_t efi_remove_protocol(const efi_handle_t handle,
559 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100560 void *protocol_interface)
561{
562 struct efi_handler *handler;
563 efi_status_t ret;
564
565 ret = efi_search_protocol(handle, protocol, &handler);
566 if (ret != EFI_SUCCESS)
567 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200568 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200569 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100570 list_del(&handler->link);
571 free(handler);
572 return EFI_SUCCESS;
573}
574
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200575/**
Mario Six8fac2912018-07-10 08:40:17 +0200576 * efi_remove_all_protocols() - delete all protocols from a handle
577 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100578 *
Mario Six8fac2912018-07-10 08:40:17 +0200579 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100580 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100581efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100582{
583 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100584 struct efi_handler *protocol;
585 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100586
587 efiobj = efi_search_obj(handle);
588 if (!efiobj)
589 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100590 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100591 efi_status_t ret;
592
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100593 ret = efi_remove_protocol(handle, protocol->guid,
594 protocol->protocol_interface);
595 if (ret != EFI_SUCCESS)
596 return ret;
597 }
598 return EFI_SUCCESS;
599}
600
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200601/**
Mario Six8fac2912018-07-10 08:40:17 +0200602 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100603 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200604 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100605 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200606void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100607{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200608 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100609 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200610 efi_remove_all_protocols(handle);
611 list_del(&handle->link);
612 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100613}
614
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200615/**
Mario Six8fac2912018-07-10 08:40:17 +0200616 * efi_is_event() - check if a pointer is a valid event
617 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100618 *
Mario Six8fac2912018-07-10 08:40:17 +0200619 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100620 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100621static efi_status_t efi_is_event(const struct efi_event *event)
622{
623 const struct efi_event *evt;
624
625 if (!event)
626 return EFI_INVALID_PARAMETER;
627 list_for_each_entry(evt, &efi_events, link) {
628 if (evt == event)
629 return EFI_SUCCESS;
630 }
631 return EFI_INVALID_PARAMETER;
632}
Alexander Grafc15d9212016-03-04 01:09:59 +0100633
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200634/**
Mario Six8fac2912018-07-10 08:40:17 +0200635 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200636 *
Mario Six8fac2912018-07-10 08:40:17 +0200637 * @type: type of the event to create
638 * @notify_tpl: task priority level of the event
639 * @notify_function: notification function of the event
640 * @notify_context: pointer passed to the notification function
641 * @group: event group
642 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200643 *
644 * This function is used inside U-Boot code to create an event.
645 *
646 * For the API function implementing the CreateEvent service see
647 * efi_create_event_ext.
648 *
Mario Six8fac2912018-07-10 08:40:17 +0200649 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200650 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100651efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200652 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200653 struct efi_event *event,
654 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100655 void *notify_context, efi_guid_t *group,
656 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100657{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100658 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200659 efi_status_t ret;
660 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200661
Jonathan Gray7758b212017-03-12 19:26:07 +1100662 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200663 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100664
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200665 switch (type) {
666 case 0:
667 case EVT_TIMER:
668 case EVT_NOTIFY_SIGNAL:
669 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
670 case EVT_NOTIFY_WAIT:
671 case EVT_TIMER | EVT_NOTIFY_WAIT:
672 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200673 pool_type = EFI_BOOT_SERVICES_DATA;
674 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200675 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200676 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200677 break;
678 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200679 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200680 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100681
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900682 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200683 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200684 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100685
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200686 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
687 (void **)&evt);
688 if (ret != EFI_SUCCESS)
689 return ret;
690 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100691 evt->type = type;
692 evt->notify_tpl = notify_tpl;
693 evt->notify_function = notify_function;
694 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100695 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200696 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100697 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100698 list_add_tail(&evt->link, &efi_events);
699 *event = evt;
700 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100701}
702
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703/*
Mario Six8fac2912018-07-10 08:40:17 +0200704 * efi_create_event_ex() - create an event in a group
705 * @type: type of the event to create
706 * @notify_tpl: task priority level of the event
707 * @notify_function: notification function of the event
708 * @notify_context: pointer passed to the notification function
709 * @event: created event
710 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100711 *
712 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100713 *
Mario Six8fac2912018-07-10 08:40:17 +0200714 * See the Unified Extensible Firmware Interface (UEFI) specification for
715 * details.
716 *
717 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100718 */
719efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
720 void (EFIAPI *notify_function) (
721 struct efi_event *event,
722 void *context),
723 void *notify_context,
724 efi_guid_t *event_group,
725 struct efi_event **event)
726{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200727 efi_status_t ret;
728
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100729 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
730 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200731
732 /*
733 * The allowable input parameters are the same as in CreateEvent()
734 * except for the following two disallowed event types.
735 */
736 switch (type) {
737 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
738 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
739 ret = EFI_INVALID_PARAMETER;
740 goto out;
741 }
742
743 ret = efi_create_event(type, notify_tpl, notify_function,
744 notify_context, event_group, event);
745out:
746 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100747}
748
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200749/**
Mario Six8fac2912018-07-10 08:40:17 +0200750 * efi_create_event_ext() - create an event
751 * @type: type of the event to create
752 * @notify_tpl: task priority level of the event
753 * @notify_function: notification function of the event
754 * @notify_context: pointer passed to the notification function
755 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200756 *
757 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200758 *
759 * See the Unified Extensible Firmware Interface (UEFI) specification for
760 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200761 *
Mario Six8fac2912018-07-10 08:40:17 +0200762 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200763 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200764static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100765 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200766 void (EFIAPI *notify_function) (
767 struct efi_event *event,
768 void *context),
769 void *notify_context, struct efi_event **event)
770{
771 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
772 notify_context);
773 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100774 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200775}
776
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200777/**
Mario Six8fac2912018-07-10 08:40:17 +0200778 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200779 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200780 * Check if a timer event has occurred or a queued notification function should
781 * be called.
782 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100783 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200784 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100785 */
786void efi_timer_check(void)
787{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100788 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100789 u64 now = timer_get_us();
790
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100791 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200792 if (!timers_enabled)
793 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100794 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200795 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100796 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200797 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100798 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200799 break;
800 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100801 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200802 break;
803 default:
804 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200805 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100806 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200807 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100808 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200809 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100810 WATCHDOG_RESET();
811}
812
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200813/**
Mario Six8fac2912018-07-10 08:40:17 +0200814 * efi_set_timer() - set the trigger time for a timer event or stop the event
815 * @event: event for which the timer is set
816 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200817 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200818 *
819 * This is the function for internal usage in U-Boot. For the API function
820 * implementing the SetTimer service see efi_set_timer_ext.
821 *
Mario Six8fac2912018-07-10 08:40:17 +0200822 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200823 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200824efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200825 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100826{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100827 /* Check that the event is valid */
828 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
829 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100830
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200831 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200832 * The parameter defines a multiple of 100 ns.
833 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200834 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200835 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100836
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100837 switch (type) {
838 case EFI_TIMER_STOP:
839 event->trigger_next = -1ULL;
840 break;
841 case EFI_TIMER_PERIODIC:
842 case EFI_TIMER_RELATIVE:
843 event->trigger_next = timer_get_us() + trigger_time;
844 break;
845 default:
846 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100847 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100848 event->trigger_type = type;
849 event->trigger_time = trigger_time;
850 event->is_signaled = false;
851 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100852}
853
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200854/**
Mario Six8fac2912018-07-10 08:40:17 +0200855 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
856 * event
857 * @event: event for which the timer is set
858 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200859 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200860 *
861 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200862 *
Mario Six8fac2912018-07-10 08:40:17 +0200863 * See the Unified Extensible Firmware Interface (UEFI) specification for
864 * details.
865 *
866 *
867 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200868 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200869static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
870 enum efi_timer_delay type,
871 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200872{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900873 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200874 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
875}
876
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200877/**
Mario Six8fac2912018-07-10 08:40:17 +0200878 * efi_wait_for_event() - wait for events to be signaled
879 * @num_events: number of events to be waited for
880 * @event: events to be waited for
881 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200882 *
883 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 *
Mario Six8fac2912018-07-10 08:40:17 +0200885 * See the Unified Extensible Firmware Interface (UEFI) specification for
886 * details.
887 *
888 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200889 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100890static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200891 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100892 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100893{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100894 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100895
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100896 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100897
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200898 /* Check parameters */
899 if (!num_events || !event)
900 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200901 /* Check TPL */
902 if (efi_tpl != TPL_APPLICATION)
903 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200904 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100905 if (efi_is_event(event[i]) != EFI_SUCCESS)
906 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200907 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200909 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200910 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200911 }
912
913 /* Wait for signal */
914 for (;;) {
915 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200916 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200917 goto out;
918 }
919 /* Allow events to occur. */
920 efi_timer_check();
921 }
922
923out:
924 /*
925 * Reset the signal which is passed to the caller to allow periodic
926 * events to occur.
927 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200928 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200929 if (index)
930 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100931
932 return EFI_EXIT(EFI_SUCCESS);
933}
934
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200935/**
Mario Six8fac2912018-07-10 08:40:17 +0200936 * efi_signal_event_ext() - signal an EFI event
937 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200938 *
939 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200940 *
941 * See the Unified Extensible Firmware Interface (UEFI) specification for
942 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200943 *
944 * This functions sets the signaled state of the event and queues the
945 * notification function for execution.
946 *
Mario Six8fac2912018-07-10 08:40:17 +0200947 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200948 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200949static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100950{
951 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100952 if (efi_is_event(event) != EFI_SUCCESS)
953 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200954 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100955 return EFI_EXIT(EFI_SUCCESS);
956}
957
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200958/**
Mario Six8fac2912018-07-10 08:40:17 +0200959 * efi_close_event() - close an EFI event
960 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200961 *
962 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200963 *
Mario Six8fac2912018-07-10 08:40:17 +0200964 * See the Unified Extensible Firmware Interface (UEFI) specification for
965 * details.
966 *
967 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200968 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200969static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100970{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200971 struct efi_register_notify_event *item, *next;
972
Alexander Grafc15d9212016-03-04 01:09:59 +0100973 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100974 if (efi_is_event(event) != EFI_SUCCESS)
975 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200976
977 /* Remove protocol notify registrations for the event */
978 list_for_each_entry_safe(item, next, &efi_register_notify_events,
979 link) {
980 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200981 struct efi_protocol_notification *hitem, *hnext;
982
983 /* Remove signaled handles */
984 list_for_each_entry_safe(hitem, hnext, &item->handles,
985 link) {
986 list_del(&hitem->link);
987 free(hitem);
988 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200989 list_del(&item->link);
990 free(item);
991 }
992 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200993 /* Remove event from queue */
994 if (efi_event_is_queued(event))
995 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200996
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100997 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200998 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100999 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001000}
1001
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001002/**
Mario Six8fac2912018-07-10 08:40:17 +02001003 * efi_check_event() - check if an event is signaled
1004 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001005 *
1006 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001007 *
1008 * See the Unified Extensible Firmware Interface (UEFI) specification for
1009 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001010 *
Mario Six8fac2912018-07-10 08:40:17 +02001011 * If an event is not signaled yet, the notification function is queued. The
1012 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001013 *
Mario Six8fac2912018-07-10 08:40:17 +02001014 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001015 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001016static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001017{
1018 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001019 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001020 if (efi_is_event(event) != EFI_SUCCESS ||
1021 event->type & EVT_NOTIFY_SIGNAL)
1022 return EFI_EXIT(EFI_INVALID_PARAMETER);
1023 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001024 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001025 if (event->is_signaled) {
1026 event->is_signaled = false;
1027 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001028 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001029 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001030}
1031
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001032/**
Mario Six8fac2912018-07-10 08:40:17 +02001033 * efi_search_obj() - find the internal EFI object for a handle
1034 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001035 *
Mario Six8fac2912018-07-10 08:40:17 +02001036 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001037 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001038struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001039{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001040 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001041
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001042 if (!handle)
1043 return NULL;
1044
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001045 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001046 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001047 return efiobj;
1048 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001049 return NULL;
1050}
1051
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001052/**
Mario Six8fac2912018-07-10 08:40:17 +02001053 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1054 * to a protocol
1055 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001056 *
Mario Six8fac2912018-07-10 08:40:17 +02001057 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001058 */
1059static struct efi_open_protocol_info_entry *efi_create_open_info(
1060 struct efi_handler *handler)
1061{
1062 struct efi_open_protocol_info_item *item;
1063
1064 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1065 if (!item)
1066 return NULL;
1067 /* Append the item to the open protocol info list. */
1068 list_add_tail(&item->link, &handler->open_infos);
1069
1070 return &item->info;
1071}
1072
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001073/**
Mario Six8fac2912018-07-10 08:40:17 +02001074 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1075 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001076 *
Mario Six8fac2912018-07-10 08:40:17 +02001077 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001078 */
1079static efi_status_t efi_delete_open_info(
1080 struct efi_open_protocol_info_item *item)
1081{
1082 list_del(&item->link);
1083 free(item);
1084 return EFI_SUCCESS;
1085}
1086
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001087/**
Mario Six8fac2912018-07-10 08:40:17 +02001088 * efi_add_protocol() - install new protocol on a handle
1089 * @handle: handle on which the protocol shall be installed
1090 * @protocol: GUID of the protocol to be installed
1091 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001092 *
Mario Six8fac2912018-07-10 08:40:17 +02001093 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001094 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001095efi_status_t efi_add_protocol(const efi_handle_t handle,
1096 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001097 void *protocol_interface)
1098{
1099 struct efi_object *efiobj;
1100 struct efi_handler *handler;
1101 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001102 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001103
1104 efiobj = efi_search_obj(handle);
1105 if (!efiobj)
1106 return EFI_INVALID_PARAMETER;
1107 ret = efi_search_protocol(handle, protocol, NULL);
1108 if (ret != EFI_NOT_FOUND)
1109 return EFI_INVALID_PARAMETER;
1110 handler = calloc(1, sizeof(struct efi_handler));
1111 if (!handler)
1112 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001113 handler->guid = protocol;
1114 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001115 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001116 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001117
1118 /* Notify registered events */
1119 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001120 if (!guidcmp(protocol, &event->protocol)) {
1121 struct efi_protocol_notification *notif;
1122
1123 notif = calloc(1, sizeof(*notif));
1124 if (!notif) {
1125 list_del(&handler->link);
1126 free(handler);
1127 return EFI_OUT_OF_RESOURCES;
1128 }
1129 notif->handle = handle;
1130 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001131 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001132 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001133 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001134 }
1135
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001136 if (!guidcmp(&efi_guid_device_path, protocol))
1137 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001138 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001139}
1140
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001141/**
Mario Six8fac2912018-07-10 08:40:17 +02001142 * efi_install_protocol_interface() - install protocol interface
1143 * @handle: handle on which the protocol shall be installed
1144 * @protocol: GUID of the protocol to be installed
1145 * @protocol_interface_type: type of the interface to be installed,
1146 * always EFI_NATIVE_INTERFACE
1147 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001148 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001149 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001150 *
Mario Six8fac2912018-07-10 08:40:17 +02001151 * See the Unified Extensible Firmware Interface (UEFI) specification for
1152 * details.
1153 *
1154 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001155 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001156static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001157 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001158 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001159{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001160 efi_status_t r;
1161
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001162 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1163 protocol_interface);
1164
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001165 if (!handle || !protocol ||
1166 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1167 r = EFI_INVALID_PARAMETER;
1168 goto out;
1169 }
1170
1171 /* Create new handle if requested. */
1172 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001173 r = efi_create_handle(handle);
1174 if (r != EFI_SUCCESS)
1175 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001176 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001177 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001178 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001179 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001180 /* Add new protocol */
1181 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001182out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001183 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001184}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001185
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001186/**
Mario Six8fac2912018-07-10 08:40:17 +02001187 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001188 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001189 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001190 * @number_of_drivers: number of child controllers
1191 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001192 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001193 * The allocated buffer has to be freed with free().
1194 *
Mario Six8fac2912018-07-10 08:40:17 +02001195 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001196 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001197static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001198 const efi_guid_t *protocol,
1199 efi_uintn_t *number_of_drivers,
1200 efi_handle_t **driver_handle_buffer)
1201{
1202 struct efi_handler *handler;
1203 struct efi_open_protocol_info_item *item;
1204 efi_uintn_t count = 0, i;
1205 bool duplicate;
1206
1207 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001208 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001209 if (protocol && guidcmp(handler->guid, protocol))
1210 continue;
1211 list_for_each_entry(item, &handler->open_infos, link) {
1212 if (item->info.attributes &
1213 EFI_OPEN_PROTOCOL_BY_DRIVER)
1214 ++count;
1215 }
1216 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001217 *number_of_drivers = 0;
1218 if (!count) {
1219 *driver_handle_buffer = NULL;
1220 return EFI_SUCCESS;
1221 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001222 /*
1223 * Create buffer. In case of duplicate driver assignments the buffer
1224 * will be too large. But that does not harm.
1225 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001226 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1227 if (!*driver_handle_buffer)
1228 return EFI_OUT_OF_RESOURCES;
1229 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001230 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001231 if (protocol && guidcmp(handler->guid, protocol))
1232 continue;
1233 list_for_each_entry(item, &handler->open_infos, link) {
1234 if (item->info.attributes &
1235 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1236 /* Check this is a new driver */
1237 duplicate = false;
1238 for (i = 0; i < *number_of_drivers; ++i) {
1239 if ((*driver_handle_buffer)[i] ==
1240 item->info.agent_handle)
1241 duplicate = true;
1242 }
1243 /* Copy handle to buffer */
1244 if (!duplicate) {
1245 i = (*number_of_drivers)++;
1246 (*driver_handle_buffer)[i] =
1247 item->info.agent_handle;
1248 }
1249 }
1250 }
1251 }
1252 return EFI_SUCCESS;
1253}
1254
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001255/**
Mario Six8fac2912018-07-10 08:40:17 +02001256 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001257 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001258 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001259 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001260 *
1261 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001262 *
1263 * See the Unified Extensible Firmware Interface (UEFI) specification for
1264 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001265 *
Mario Six8fac2912018-07-10 08:40:17 +02001266 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001267 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001268static efi_status_t efi_disconnect_all_drivers
1269 (efi_handle_t handle,
1270 const efi_guid_t *protocol,
1271 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001272{
1273 efi_uintn_t number_of_drivers;
1274 efi_handle_t *driver_handle_buffer;
1275 efi_status_t r, ret;
1276
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001277 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001278 &driver_handle_buffer);
1279 if (ret != EFI_SUCCESS)
1280 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001281 if (!number_of_drivers)
1282 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001283 ret = EFI_NOT_FOUND;
1284 while (number_of_drivers) {
1285 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001286 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001287 driver_handle_buffer[--number_of_drivers],
1288 child_handle));
1289 if (r == EFI_SUCCESS)
1290 ret = r;
1291 }
1292 free(driver_handle_buffer);
1293 return ret;
1294}
1295
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001296/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001297 * efi_uninstall_protocol() - uninstall protocol interface
1298 *
Mario Six8fac2912018-07-10 08:40:17 +02001299 * @handle: handle from which the protocol shall be removed
1300 * @protocol: GUID of the protocol to be removed
1301 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001302 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001303 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001304 *
1305 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001306 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001307static efi_status_t efi_uninstall_protocol
1308 (efi_handle_t handle, const efi_guid_t *protocol,
1309 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001310{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001311 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001312 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001313 struct efi_open_protocol_info_item *item;
1314 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001315 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001316
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001317 /* Check handle */
1318 efiobj = efi_search_obj(handle);
1319 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001320 r = EFI_INVALID_PARAMETER;
1321 goto out;
1322 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001323 /* Find the protocol on the handle */
1324 r = efi_search_protocol(handle, protocol, &handler);
1325 if (r != EFI_SUCCESS)
1326 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001327 /* Disconnect controllers */
1328 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001329 /* Close protocol */
1330 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1331 if (item->info.attributes ==
1332 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1333 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1334 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1335 list_del(&item->link);
1336 }
1337 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001338 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001339 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001340 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001341 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001342out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001343 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001344}
1345
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001346/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001347 * efi_uninstall_protocol_interface() - uninstall protocol interface
1348 * @handle: handle from which the protocol shall be removed
1349 * @protocol: GUID of the protocol to be removed
1350 * @protocol_interface: interface to be removed
1351 *
1352 * This function implements the UninstallProtocolInterface service.
1353 *
1354 * See the Unified Extensible Firmware Interface (UEFI) specification for
1355 * details.
1356 *
1357 * Return: status code
1358 */
1359static efi_status_t EFIAPI efi_uninstall_protocol_interface
1360 (efi_handle_t handle, const efi_guid_t *protocol,
1361 void *protocol_interface)
1362{
1363 efi_status_t ret;
1364
1365 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1366
1367 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1368 if (ret != EFI_SUCCESS)
1369 goto out;
1370
1371 /* If the last protocol has been removed, delete the handle. */
1372 if (list_empty(&handle->protocols)) {
1373 list_del(&handle->link);
1374 free(handle);
1375 }
1376out:
1377 return EFI_EXIT(ret);
1378}
1379
1380/**
Mario Six8fac2912018-07-10 08:40:17 +02001381 * efi_register_protocol_notify() - register an event for notification when a
1382 * protocol is installed.
1383 * @protocol: GUID of the protocol whose installation shall be notified
1384 * @event: event to be signaled upon installation of the protocol
1385 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001386 *
1387 * This function implements the RegisterProtocolNotify service.
1388 * See the Unified Extensible Firmware Interface (UEFI) specification
1389 * for details.
1390 *
Mario Six8fac2912018-07-10 08:40:17 +02001391 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001392 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001393static efi_status_t EFIAPI efi_register_protocol_notify(
1394 const efi_guid_t *protocol,
1395 struct efi_event *event,
1396 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001397{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001398 struct efi_register_notify_event *item;
1399 efi_status_t ret = EFI_SUCCESS;
1400
Rob Clark238f88c2017-09-13 18:05:41 -04001401 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001402
1403 if (!protocol || !event || !registration) {
1404 ret = EFI_INVALID_PARAMETER;
1405 goto out;
1406 }
1407
1408 item = calloc(1, sizeof(struct efi_register_notify_event));
1409 if (!item) {
1410 ret = EFI_OUT_OF_RESOURCES;
1411 goto out;
1412 }
1413
1414 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301415 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001416 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001417
1418 list_add_tail(&item->link, &efi_register_notify_events);
1419
1420 *registration = item;
1421out:
1422 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001423}
1424
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001425/**
Mario Six8fac2912018-07-10 08:40:17 +02001426 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001427 *
Mario Six8fac2912018-07-10 08:40:17 +02001428 * @search_type: selection criterion
1429 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001430 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001431 *
1432 * See the documentation of the LocateHandle service in the UEFI specification.
1433 *
Mario Six8fac2912018-07-10 08:40:17 +02001434 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001435 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001436static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001437 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001438{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001439 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001440
1441 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001442 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001443 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001444 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001445 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001446 return (ret != EFI_SUCCESS);
1447 default:
1448 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001449 return -1;
1450 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001451}
1452
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001453/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001454 * efi_check_register_notify_event() - check if registration key is valid
1455 *
1456 * Check that a pointer is a valid registration key as returned by
1457 * RegisterProtocolNotify().
1458 *
1459 * @key: registration key
1460 * Return: valid registration key or NULL
1461 */
1462static struct efi_register_notify_event *efi_check_register_notify_event
1463 (void *key)
1464{
1465 struct efi_register_notify_event *event;
1466
1467 list_for_each_entry(event, &efi_register_notify_events, link) {
1468 if (event == (struct efi_register_notify_event *)key)
1469 return event;
1470 }
1471 return NULL;
1472}
1473
1474/**
Mario Six8fac2912018-07-10 08:40:17 +02001475 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001476 *
1477 * @search_type: selection criterion
1478 * @protocol: GUID of the protocol
1479 * @search_key: registration key
1480 * @buffer_size: size of the buffer to receive the handles in bytes
1481 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001482 *
1483 * This function is meant for U-Boot internal calls. For the API implementation
1484 * of the LocateHandle service see efi_locate_handle_ext.
1485 *
Mario Six8fac2912018-07-10 08:40:17 +02001486 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001487 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001488static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001489 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001490 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001491 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001492{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001493 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001494 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001495 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001496 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001497
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001498 /* Check parameters */
1499 switch (search_type) {
1500 case ALL_HANDLES:
1501 break;
1502 case BY_REGISTER_NOTIFY:
1503 if (!search_key)
1504 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001505 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001506 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001507 if (!event)
1508 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001509 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001510 case BY_PROTOCOL:
1511 if (!protocol)
1512 return EFI_INVALID_PARAMETER;
1513 break;
1514 default:
1515 return EFI_INVALID_PARAMETER;
1516 }
1517
Alexander Grafc15d9212016-03-04 01:09:59 +01001518 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001519 if (search_type == BY_REGISTER_NOTIFY) {
1520 if (list_empty(&event->handles))
1521 return EFI_NOT_FOUND;
1522 handle = list_first_entry(&event->handles,
1523 struct efi_protocol_notification,
1524 link);
1525 efiobj = handle->handle;
1526 size += sizeof(void *);
1527 } else {
1528 list_for_each_entry(efiobj, &efi_obj_list, link) {
1529 if (!efi_search(search_type, protocol, efiobj))
1530 size += sizeof(void *);
1531 }
1532 if (size == 0)
1533 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001534 }
1535
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001536 if (!buffer_size)
1537 return EFI_INVALID_PARAMETER;
1538
Alexander Grafc15d9212016-03-04 01:09:59 +01001539 if (*buffer_size < size) {
1540 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001541 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001542 }
1543
Rob Clarkcdee3372017-08-06 14:10:07 -04001544 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001545
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001546 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001547 if (!buffer)
1548 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001549
Alexander Grafc15d9212016-03-04 01:09:59 +01001550 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001551 if (search_type == BY_REGISTER_NOTIFY) {
1552 *buffer = efiobj;
1553 list_del(&handle->link);
1554 } else {
1555 list_for_each_entry(efiobj, &efi_obj_list, link) {
1556 if (!efi_search(search_type, protocol, efiobj))
1557 *buffer++ = efiobj;
1558 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001559 }
1560
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001561 return EFI_SUCCESS;
1562}
1563
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001564/**
Mario Six8fac2912018-07-10 08:40:17 +02001565 * efi_locate_handle_ext() - locate handles implementing a protocol.
1566 * @search_type: selection criterion
1567 * @protocol: GUID of the protocol
1568 * @search_key: registration key
1569 * @buffer_size: size of the buffer to receive the handles in bytes
1570 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001571 *
1572 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001573 *
Mario Six8fac2912018-07-10 08:40:17 +02001574 * See the Unified Extensible Firmware Interface (UEFI) specification for
1575 * details.
1576 *
1577 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001578 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001579static efi_status_t EFIAPI efi_locate_handle_ext(
1580 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001581 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001582 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001583{
Rob Clark238f88c2017-09-13 18:05:41 -04001584 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001585 buffer_size, buffer);
1586
1587 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1588 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001589}
1590
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001591/**
Mario Six8fac2912018-07-10 08:40:17 +02001592 * efi_remove_configuration_table() - collapses configuration table entries,
1593 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001594 *
Mario Six8fac2912018-07-10 08:40:17 +02001595 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001596 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001597static void efi_remove_configuration_table(int i)
1598{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001599 struct efi_configuration_table *this = &systab.tables[i];
1600 struct efi_configuration_table *next = &systab.tables[i + 1];
1601 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001602
1603 memmove(this, next, (ulong)end - (ulong)next);
1604 systab.nr_tables--;
1605}
1606
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001607/**
Mario Six8fac2912018-07-10 08:40:17 +02001608 * efi_install_configuration_table() - adds, updates, or removes a
1609 * configuration table
1610 * @guid: GUID of the installed table
1611 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001612 *
1613 * This function is used for internal calls. For the API implementation of the
1614 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1615 *
Mario Six8fac2912018-07-10 08:40:17 +02001616 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001617 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001618efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1619 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001620{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001621 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001622 int i;
1623
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001624 if (!guid)
1625 return EFI_INVALID_PARAMETER;
1626
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001627 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001628 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001629 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001630 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001631 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001632 else
1633 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001634 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001635 }
1636 }
1637
Alexander Graffe3366f2017-07-26 13:41:04 +02001638 if (!table)
1639 return EFI_NOT_FOUND;
1640
Alexander Grafc15d9212016-03-04 01:09:59 +01001641 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001642 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001643 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001644
1645 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301646 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001647 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001648 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001649
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001650out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001651 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001652 efi_update_table_header_crc32(&systab.hdr);
1653
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001654 /* Notify that the configuration table was changed */
1655 list_for_each_entry(evt, &efi_events, link) {
1656 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001657 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001658 break;
1659 }
1660 }
1661
Alexander Grafc5c11632016-08-19 01:23:24 +02001662 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001663}
1664
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001665/**
Mario Six8fac2912018-07-10 08:40:17 +02001666 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1667 * configuration table.
1668 * @guid: GUID of the installed table
1669 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001670 *
1671 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001672 *
Mario Six8fac2912018-07-10 08:40:17 +02001673 * See the Unified Extensible Firmware Interface (UEFI) specification for
1674 * details.
1675 *
1676 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001677 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001678static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1679 void *table)
1680{
Rob Clark238f88c2017-09-13 18:05:41 -04001681 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001682 return EFI_EXIT(efi_install_configuration_table(guid, table));
1683}
1684
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001685/**
Mario Six8fac2912018-07-10 08:40:17 +02001686 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001687 *
1688 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001689 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001690 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001691 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001692 * code is returned.
1693 *
1694 * @device_path: device path of the loaded image
1695 * @file_path: file path of the loaded image
1696 * @handle_ptr: handle of the loaded image
1697 * @info_ptr: loaded image protocol
1698 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001699 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001700efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1701 struct efi_device_path *file_path,
1702 struct efi_loaded_image_obj **handle_ptr,
1703 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001704{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001705 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001706 struct efi_loaded_image *info = NULL;
1707 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001708 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001709
1710 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1711 *handle_ptr = NULL;
1712 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001713
1714 info = calloc(1, sizeof(*info));
1715 if (!info)
1716 return EFI_OUT_OF_RESOURCES;
1717 obj = calloc(1, sizeof(*obj));
1718 if (!obj) {
1719 free(info);
1720 return EFI_OUT_OF_RESOURCES;
1721 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001722 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001723
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001724 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001725 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001726
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001727 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001728 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001729 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001730
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001731 if (device_path) {
1732 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001733
1734 dp = efi_dp_append(device_path, file_path);
1735 if (!dp) {
1736 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001737 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001738 }
1739 } else {
1740 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001741 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001742 ret = efi_add_protocol(&obj->header,
1743 &efi_guid_loaded_image_device_path, dp);
1744 if (ret != EFI_SUCCESS)
1745 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001746
1747 /*
1748 * When asking for the loaded_image interface, just
1749 * return handle which points to loaded_image_info
1750 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001751 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001752 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001753 if (ret != EFI_SUCCESS)
1754 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001755
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001756 *info_ptr = info;
1757 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001758
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001759 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001760failure:
1761 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001762 efi_delete_handle(&obj->header);
1763 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001764 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001765}
1766
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001767/**
Mario Six8fac2912018-07-10 08:40:17 +02001768 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001769 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001770 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1771 * callers obligation to update the memory type as needed.
1772 *
1773 * @file_path: the path of the image to load
1774 * @buffer: buffer containing the loaded image
1775 * @size: size of the loaded image
1776 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001777 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001778static
Rob Clarkc84c1102017-09-13 18:05:38 -04001779efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001780 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001781{
1782 struct efi_file_info *info = NULL;
1783 struct efi_file_handle *f;
1784 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001785 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001786 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001787
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001788 /* In case of failure nothing is returned */
1789 *buffer = NULL;
1790 *size = 0;
1791
1792 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001793 f = efi_file_from_path(file_path);
1794 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001795 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001796
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001797 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001798 bs = 0;
1799 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1800 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001801 if (ret != EFI_BUFFER_TOO_SMALL) {
1802 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001803 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001804 }
Rob Clark857a1222017-09-13 18:05:35 -04001805
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001806 info = malloc(bs);
1807 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1808 info));
1809 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001810 goto error;
1811
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001812 /*
1813 * When reading the file we do not yet know if it contains an
1814 * application, a boottime driver, or a runtime driver. So here we
1815 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1816 * update the reservation according to the image type.
1817 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001818 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001819 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1820 EFI_BOOT_SERVICES_DATA,
1821 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001822 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001823 ret = EFI_OUT_OF_RESOURCES;
1824 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001825 }
1826
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001827 /* Read file */
1828 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1829 if (ret != EFI_SUCCESS)
1830 efi_free_pages(addr, efi_size_in_pages(bs));
1831 *buffer = (void *)(uintptr_t)addr;
1832 *size = bs;
1833error:
1834 EFI_CALL(f->close(f));
1835 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001836 return ret;
1837}
1838
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001839/**
Mario Six8fac2912018-07-10 08:40:17 +02001840 * efi_load_image() - load an EFI image into memory
1841 * @boot_policy: true for request originating from the boot manager
1842 * @parent_image: the caller's image handle
1843 * @file_path: the path of the image to load
1844 * @source_buffer: memory location from which the image is installed
1845 * @source_size: size of the memory area from which the image is installed
1846 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001847 *
1848 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001849 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001850 * See the Unified Extensible Firmware Interface (UEFI) specification
1851 * for details.
1852 *
Mario Six8fac2912018-07-10 08:40:17 +02001853 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001854 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001855efi_status_t EFIAPI efi_load_image(bool boot_policy,
1856 efi_handle_t parent_image,
1857 struct efi_device_path *file_path,
1858 void *source_buffer,
1859 efi_uintn_t source_size,
1860 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001861{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001862 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001863 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001864 struct efi_loaded_image_obj **image_obj =
1865 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001866 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001867 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001868
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001869 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001870 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001871
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001872 if (!image_handle || (!source_buffer && !file_path) ||
1873 !efi_search_obj(parent_image) ||
1874 /* The parent image handle must refer to a loaded image */
1875 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001876 ret = EFI_INVALID_PARAMETER;
1877 goto error;
1878 }
Rob Clark857a1222017-09-13 18:05:35 -04001879
1880 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001881 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001882 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001883 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001884 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001885 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001886 if (!source_size) {
1887 ret = EFI_LOAD_ERROR;
1888 goto error;
1889 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001890 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001891 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001892 /* split file_path which contains both the device and file parts */
1893 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001894 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001895 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001896 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001897 if (!source_buffer)
1898 /* Release buffer to which file was loaded */
1899 efi_free_pages((uintptr_t)dest_buffer,
1900 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001901 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001902 info->system_table = &systab;
1903 info->parent_handle = parent_image;
1904 } else {
1905 /* The image is invalid. Release all associated resources. */
1906 efi_delete_handle(*image_handle);
1907 *image_handle = NULL;
1908 free(info);
1909 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001910error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001911 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001912}
1913
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001914/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001915 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1916 */
1917static void efi_exit_caches(void)
1918{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001919#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01001920 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001921 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1922 * caches are enabled.
1923 *
1924 * TODO:
1925 * According to the UEFI spec caches that can be managed via CP15
1926 * operations should be enabled. Caches requiring platform information
1927 * to manage should be disabled. This should not happen in
1928 * ExitBootServices() but before invoking any UEFI binary is invoked.
1929 *
1930 * We want to keep the current workaround while GRUB prior to version
1931 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01001932 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001933 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01001934#endif
1935}
1936
1937/**
Mario Six8fac2912018-07-10 08:40:17 +02001938 * efi_exit_boot_services() - stop all boot services
1939 * @image_handle: handle of the loaded image
1940 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001941 *
1942 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001943 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001944 * See the Unified Extensible Firmware Interface (UEFI) specification
1945 * for details.
1946 *
Mario Six8fac2912018-07-10 08:40:17 +02001947 * All timer events are disabled. For exit boot services events the
1948 * notification function is called. The boot services are disabled in the
1949 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001950 *
Mario Six8fac2912018-07-10 08:40:17 +02001951 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001952 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001953static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001954 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001955{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001956 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001957 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001958
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001959 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001960
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001961 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001962 if (map_key != efi_memory_map_key) {
1963 ret = EFI_INVALID_PARAMETER;
1964 goto out;
1965 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001966
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001967 /* Check if ExitBootServices has already been called */
1968 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001969 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001970
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001971 /* Stop all timer related activities */
1972 timers_enabled = false;
1973
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001974 /* Add related events to the event group */
1975 list_for_each_entry(evt, &efi_events, link) {
1976 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1977 evt->group = &efi_guid_event_group_exit_boot_services;
1978 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001979 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001980 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001981 if (evt->group &&
1982 !guidcmp(evt->group,
1983 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001984 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001985 break;
1986 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001987 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001988
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001989 /* Make sure that notification functions are not called anymore */
1990 efi_tpl = TPL_HIGH_LEVEL;
1991
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001992 /* Notify variable services */
1993 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001994
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001995 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1996 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1997 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1998 list_del(&evt->link);
1999 }
2000
Alexander Graf2ebeb442016-11-17 01:02:57 +01002001 board_quiesce_devices();
2002
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002003 /* Patch out unsupported runtime function */
2004 efi_runtime_detach();
2005
Alexander Graffa5246b2018-11-15 21:23:47 +01002006 /* Fix up caches for EFI payloads if necessary */
2007 efi_exit_caches();
2008
Alexander Grafc15d9212016-03-04 01:09:59 +01002009 /* This stops all lingering devices */
2010 bootm_disable_interrupts();
2011
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002012 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002013 systab.con_in_handle = NULL;
2014 systab.con_in = NULL;
2015 systab.con_out_handle = NULL;
2016 systab.con_out = NULL;
2017 systab.stderr_handle = NULL;
2018 systab.std_err = NULL;
2019 systab.boottime = NULL;
2020
2021 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002022 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002023
Alexander Grafc15d9212016-03-04 01:09:59 +01002024 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002025 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002026 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002027out:
2028 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002029}
2030
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002031/**
Mario Six8fac2912018-07-10 08:40:17 +02002032 * efi_get_next_monotonic_count() - get next value of the counter
2033 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002034 *
2035 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002036 *
2037 * See the Unified Extensible Firmware Interface (UEFI) specification for
2038 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002039 *
Mario Six8fac2912018-07-10 08:40:17 +02002040 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002041 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002042static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2043{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002044 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002045 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002046
Alexander Grafc15d9212016-03-04 01:09:59 +01002047 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002048 if (!count) {
2049 ret = EFI_INVALID_PARAMETER;
2050 goto out;
2051 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002052 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002053 ret = EFI_SUCCESS;
2054out:
2055 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002056}
2057
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002058/**
Mario Six8fac2912018-07-10 08:40:17 +02002059 * efi_stall() - sleep
2060 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002061 *
Mario Six8fac2912018-07-10 08:40:17 +02002062 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002063 *
Mario Six8fac2912018-07-10 08:40:17 +02002064 * See the Unified Extensible Firmware Interface (UEFI) specification for
2065 * details.
2066 *
2067 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002068 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002069static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2070{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002071 u64 end_tick;
2072
Alexander Grafc15d9212016-03-04 01:09:59 +01002073 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002074
2075 end_tick = get_ticks() + usec_to_tick(microseconds);
2076 while (get_ticks() < end_tick)
2077 efi_timer_check();
2078
Alexander Grafc15d9212016-03-04 01:09:59 +01002079 return EFI_EXIT(EFI_SUCCESS);
2080}
2081
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002082/**
Mario Six8fac2912018-07-10 08:40:17 +02002083 * efi_set_watchdog_timer() - reset the watchdog timer
2084 * @timeout: seconds before reset by watchdog
2085 * @watchdog_code: code to be logged when resetting
2086 * @data_size: size of buffer in bytes
2087 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002088 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002089 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002090 *
Mario Six8fac2912018-07-10 08:40:17 +02002091 * See the Unified Extensible Firmware Interface (UEFI) specification for
2092 * details.
2093 *
2094 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002095 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002096static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2097 uint64_t watchdog_code,
2098 unsigned long data_size,
2099 uint16_t *watchdog_data)
2100{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002101 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002102 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002103 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002104}
2105
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002106/**
Mario Six8fac2912018-07-10 08:40:17 +02002107 * efi_close_protocol() - close a protocol
2108 * @handle: handle on which the protocol shall be closed
2109 * @protocol: GUID of the protocol to close
2110 * @agent_handle: handle of the driver
2111 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002112 *
2113 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002114 *
Mario Six8fac2912018-07-10 08:40:17 +02002115 * See the Unified Extensible Firmware Interface (UEFI) specification for
2116 * details.
2117 *
2118 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002119 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002120efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2121 const efi_guid_t *protocol,
2122 efi_handle_t agent_handle,
2123 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002124{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002125 struct efi_handler *handler;
2126 struct efi_open_protocol_info_item *item;
2127 struct efi_open_protocol_info_item *pos;
2128 efi_status_t r;
2129
Rob Clark238f88c2017-09-13 18:05:41 -04002130 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002131 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002132
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002133 if (!efi_search_obj(agent_handle) ||
2134 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002135 r = EFI_INVALID_PARAMETER;
2136 goto out;
2137 }
2138 r = efi_search_protocol(handle, protocol, &handler);
2139 if (r != EFI_SUCCESS)
2140 goto out;
2141
2142 r = EFI_NOT_FOUND;
2143 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2144 if (item->info.agent_handle == agent_handle &&
2145 item->info.controller_handle == controller_handle) {
2146 efi_delete_open_info(item);
2147 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002148 }
2149 }
2150out:
2151 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002152}
2153
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002154/**
Mario Six8fac2912018-07-10 08:40:17 +02002155 * efi_open_protocol_information() - provide information about then open status
2156 * of a protocol on a handle
2157 * @handle: handle for which the information shall be retrieved
2158 * @protocol: GUID of the protocol
2159 * @entry_buffer: buffer to receive the open protocol information
2160 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002161 *
2162 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002163 *
2164 * See the Unified Extensible Firmware Interface (UEFI) specification for
2165 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002166 *
Mario Six8fac2912018-07-10 08:40:17 +02002167 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002168 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002169static efi_status_t EFIAPI efi_open_protocol_information(
2170 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002171 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002172 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002173{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002174 unsigned long buffer_size;
2175 unsigned long count;
2176 struct efi_handler *handler;
2177 struct efi_open_protocol_info_item *item;
2178 efi_status_t r;
2179
Rob Clark238f88c2017-09-13 18:05:41 -04002180 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002181 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002182
2183 /* Check parameters */
2184 if (!entry_buffer) {
2185 r = EFI_INVALID_PARAMETER;
2186 goto out;
2187 }
2188 r = efi_search_protocol(handle, protocol, &handler);
2189 if (r != EFI_SUCCESS)
2190 goto out;
2191
2192 /* Count entries */
2193 count = 0;
2194 list_for_each_entry(item, &handler->open_infos, link) {
2195 if (item->info.open_count)
2196 ++count;
2197 }
2198 *entry_count = count;
2199 *entry_buffer = NULL;
2200 if (!count) {
2201 r = EFI_SUCCESS;
2202 goto out;
2203 }
2204
2205 /* Copy entries */
2206 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002207 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002208 (void **)entry_buffer);
2209 if (r != EFI_SUCCESS)
2210 goto out;
2211 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2212 if (item->info.open_count)
2213 (*entry_buffer)[--count] = item->info;
2214 }
2215out:
2216 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002217}
2218
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002219/**
Mario Six8fac2912018-07-10 08:40:17 +02002220 * efi_protocols_per_handle() - get protocols installed on a handle
2221 * @handle: handle for which the information is retrieved
2222 * @protocol_buffer: buffer with protocol GUIDs
2223 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002224 *
2225 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002226 *
Mario Six8fac2912018-07-10 08:40:17 +02002227 * See the Unified Extensible Firmware Interface (UEFI) specification for
2228 * details.
2229 *
2230 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002231 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002232static efi_status_t EFIAPI efi_protocols_per_handle(
2233 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002234 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002235{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002236 unsigned long buffer_size;
2237 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002238 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002239 efi_status_t r;
2240
Alexander Grafc15d9212016-03-04 01:09:59 +01002241 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2242 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002243
2244 if (!handle || !protocol_buffer || !protocol_buffer_count)
2245 return EFI_EXIT(EFI_INVALID_PARAMETER);
2246
2247 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002248 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002249
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002250 efiobj = efi_search_obj(handle);
2251 if (!efiobj)
2252 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002253
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002254 /* Count protocols */
2255 list_for_each(protocol_handle, &efiobj->protocols) {
2256 ++*protocol_buffer_count;
2257 }
2258
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002259 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002260 if (*protocol_buffer_count) {
2261 size_t j = 0;
2262
2263 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002264 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002265 (void **)protocol_buffer);
2266 if (r != EFI_SUCCESS)
2267 return EFI_EXIT(r);
2268 list_for_each(protocol_handle, &efiobj->protocols) {
2269 struct efi_handler *protocol;
2270
2271 protocol = list_entry(protocol_handle,
2272 struct efi_handler, link);
2273 (*protocol_buffer)[j] = (void *)protocol->guid;
2274 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002275 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002276 }
2277
2278 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002279}
2280
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002281/**
Mario Six8fac2912018-07-10 08:40:17 +02002282 * efi_locate_handle_buffer() - locate handles implementing a protocol
2283 * @search_type: selection criterion
2284 * @protocol: GUID of the protocol
2285 * @search_key: registration key
2286 * @no_handles: number of returned handles
2287 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002288 *
2289 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002290 *
Mario Six8fac2912018-07-10 08:40:17 +02002291 * See the Unified Extensible Firmware Interface (UEFI) specification for
2292 * details.
2293 *
2294 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002295 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002296efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002297 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002298 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002299 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002300{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002301 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002302 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002303
Rob Clark238f88c2017-09-13 18:05:41 -04002304 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002305 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002306
2307 if (!no_handles || !buffer) {
2308 r = EFI_INVALID_PARAMETER;
2309 goto out;
2310 }
2311 *no_handles = 0;
2312 *buffer = NULL;
2313 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2314 *buffer);
2315 if (r != EFI_BUFFER_TOO_SMALL)
2316 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002317 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002318 (void **)buffer);
2319 if (r != EFI_SUCCESS)
2320 goto out;
2321 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2322 *buffer);
2323 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002324 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002325out:
2326 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002327}
2328
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002329/**
Mario Six8fac2912018-07-10 08:40:17 +02002330 * efi_locate_protocol() - find an interface implementing a protocol
2331 * @protocol: GUID of the protocol
2332 * @registration: registration key passed to the notification function
2333 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002334 *
2335 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002336 *
2337 * See the Unified Extensible Firmware Interface (UEFI) specification for
2338 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002339 *
Mario Six8fac2912018-07-10 08:40:17 +02002340 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002341 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002342static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002343 void *registration,
2344 void **protocol_interface)
2345{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002346 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002347 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002348 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002349
Rob Clark238f88c2017-09-13 18:05:41 -04002350 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002351
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002352 /*
2353 * The UEFI spec explicitly requires a protocol even if a registration
2354 * key is provided. This differs from the logic in LocateHandle().
2355 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002356 if (!protocol || !protocol_interface)
2357 return EFI_EXIT(EFI_INVALID_PARAMETER);
2358
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002359 if (registration) {
2360 struct efi_register_notify_event *event;
2361 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002362
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002363 event = efi_check_register_notify_event(registration);
2364 if (!event)
2365 return EFI_EXIT(EFI_INVALID_PARAMETER);
2366 /*
2367 * The UEFI spec requires to return EFI_NOT_FOUND if no
2368 * protocol instance matches protocol and registration.
2369 * So let's do the same for a mismatch between protocol and
2370 * registration.
2371 */
2372 if (guidcmp(&event->protocol, protocol))
2373 goto not_found;
2374 if (list_empty(&event->handles))
2375 goto not_found;
2376 handle = list_first_entry(&event->handles,
2377 struct efi_protocol_notification,
2378 link);
2379 efiobj = handle->handle;
2380 list_del(&handle->link);
2381 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002382 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002383 if (ret == EFI_SUCCESS)
2384 goto found;
2385 } else {
2386 list_for_each_entry(efiobj, &efi_obj_list, link) {
2387 ret = efi_search_protocol(efiobj, protocol, &handler);
2388 if (ret == EFI_SUCCESS)
2389 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002390 }
2391 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002392not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002393 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002394 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002395found:
2396 *protocol_interface = handler->protocol_interface;
2397 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002398}
2399
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002400/**
Mario Six8fac2912018-07-10 08:40:17 +02002401 * efi_locate_device_path() - Get the device path and handle of an device
2402 * implementing a protocol
2403 * @protocol: GUID of the protocol
2404 * @device_path: device path
2405 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002406 *
2407 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002408 *
Mario Six8fac2912018-07-10 08:40:17 +02002409 * See the Unified Extensible Firmware Interface (UEFI) specification for
2410 * details.
2411 *
2412 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002413 */
2414static efi_status_t EFIAPI efi_locate_device_path(
2415 const efi_guid_t *protocol,
2416 struct efi_device_path **device_path,
2417 efi_handle_t *device)
2418{
2419 struct efi_device_path *dp;
2420 size_t i;
2421 struct efi_handler *handler;
2422 efi_handle_t *handles;
2423 size_t len, len_dp;
2424 size_t len_best = 0;
2425 efi_uintn_t no_handles;
2426 u8 *remainder;
2427 efi_status_t ret;
2428
2429 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2430
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002431 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002432 ret = EFI_INVALID_PARAMETER;
2433 goto out;
2434 }
2435
2436 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002437 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002438
2439 /* Get all handles implementing the protocol */
2440 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2441 &no_handles, &handles));
2442 if (ret != EFI_SUCCESS)
2443 goto out;
2444
2445 for (i = 0; i < no_handles; ++i) {
2446 /* Find the device path protocol */
2447 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2448 &handler);
2449 if (ret != EFI_SUCCESS)
2450 continue;
2451 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002452 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002453 /*
2454 * This handle can only be a better fit
2455 * if its device path length is longer than the best fit and
2456 * if its device path length is shorter of equal the searched
2457 * device path.
2458 */
2459 if (len_dp <= len_best || len_dp > len)
2460 continue;
2461 /* Check if dp is a subpath of device_path */
2462 if (memcmp(*device_path, dp, len_dp))
2463 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002464 if (!device) {
2465 ret = EFI_INVALID_PARAMETER;
2466 goto out;
2467 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002468 *device = handles[i];
2469 len_best = len_dp;
2470 }
2471 if (len_best) {
2472 remainder = (u8 *)*device_path + len_best;
2473 *device_path = (struct efi_device_path *)remainder;
2474 ret = EFI_SUCCESS;
2475 } else {
2476 ret = EFI_NOT_FOUND;
2477 }
2478out:
2479 return EFI_EXIT(ret);
2480}
2481
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002482/**
Mario Six8fac2912018-07-10 08:40:17 +02002483 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2484 * interfaces
2485 * @handle: handle on which the protocol interfaces shall be installed
2486 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2487 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002488 *
2489 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002490 *
2491 * See the Unified Extensible Firmware Interface (UEFI) specification for
2492 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002493 *
Mario Six8fac2912018-07-10 08:40:17 +02002494 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002495 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002496efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002497 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002498{
2499 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002500
Alexander Graf404a7c82018-06-18 17:23:05 +02002501 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002502 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002503 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002504 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002505 efi_status_t r = EFI_SUCCESS;
2506 int i = 0;
2507
2508 if (!handle)
2509 return EFI_EXIT(EFI_INVALID_PARAMETER);
2510
Alexander Graf404a7c82018-06-18 17:23:05 +02002511 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002512 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002513 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002514 if (!protocol)
2515 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002516 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002517 /* Check that a device path has not been installed before */
2518 if (!guidcmp(protocol, &efi_guid_device_path)) {
2519 struct efi_device_path *dp = protocol_interface;
2520
2521 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2522 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002523 if (r == EFI_SUCCESS &&
2524 dp->type == DEVICE_PATH_TYPE_END) {
2525 EFI_PRINT("Path %pD already installed\n",
2526 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002527 r = EFI_ALREADY_STARTED;
2528 break;
2529 }
2530 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002531 r = EFI_CALL(efi_install_protocol_interface(
2532 handle, protocol,
2533 EFI_NATIVE_INTERFACE,
2534 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002535 if (r != EFI_SUCCESS)
2536 break;
2537 i++;
2538 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002539 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002540 if (r == EFI_SUCCESS)
2541 return EFI_EXIT(r);
2542
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002543 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002544 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002545 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002546 protocol = efi_va_arg(argptr, efi_guid_t*);
2547 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002548 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002549 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002550 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002551 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002552
2553 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002554}
2555
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002556/**
Mario Six8fac2912018-07-10 08:40:17 +02002557 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2558 * interfaces
2559 * @handle: handle from which the protocol interfaces shall be removed
2560 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2561 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002562 *
2563 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002564 *
Mario Six8fac2912018-07-10 08:40:17 +02002565 * See the Unified Extensible Firmware Interface (UEFI) specification for
2566 * details.
2567 *
2568 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002569 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002570static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002571 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002572{
2573 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002574
Alexander Graf404a7c82018-06-18 17:23:05 +02002575 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002576 const efi_guid_t *protocol;
2577 void *protocol_interface;
2578 efi_status_t r = EFI_SUCCESS;
2579 size_t i = 0;
2580
2581 if (!handle)
2582 return EFI_EXIT(EFI_INVALID_PARAMETER);
2583
Alexander Graf404a7c82018-06-18 17:23:05 +02002584 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002585 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002586 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002587 if (!protocol)
2588 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002589 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002590 r = efi_uninstall_protocol(handle, protocol,
2591 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002592 if (r != EFI_SUCCESS)
2593 break;
2594 i++;
2595 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002596 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002597 if (r == EFI_SUCCESS) {
2598 /* If the last protocol has been removed, delete the handle. */
2599 if (list_empty(&handle->protocols)) {
2600 list_del(&handle->link);
2601 free(handle);
2602 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002603 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002604 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002605
2606 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002607 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002608 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002609 protocol = efi_va_arg(argptr, efi_guid_t*);
2610 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002611 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2612 EFI_NATIVE_INTERFACE,
2613 protocol_interface));
2614 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002615 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002616
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002617 /* In case of an error always return EFI_INVALID_PARAMETER */
2618 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002619}
2620
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002621/**
Mario Six8fac2912018-07-10 08:40:17 +02002622 * efi_calculate_crc32() - calculate cyclic redundancy code
2623 * @data: buffer with data
2624 * @data_size: size of buffer in bytes
2625 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002626 *
2627 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002628 *
2629 * See the Unified Extensible Firmware Interface (UEFI) specification for
2630 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002631 *
Mario Six8fac2912018-07-10 08:40:17 +02002632 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002633 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002634static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2635 efi_uintn_t data_size,
2636 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002637{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002638 efi_status_t ret = EFI_SUCCESS;
2639
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002640 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002641 if (!data || !data_size || !crc32_p) {
2642 ret = EFI_INVALID_PARAMETER;
2643 goto out;
2644 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002645 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002646out:
2647 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002648}
2649
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002650/**
Mario Six8fac2912018-07-10 08:40:17 +02002651 * efi_copy_mem() - copy memory
2652 * @destination: destination of the copy operation
2653 * @source: source of the copy operation
2654 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002655 *
2656 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002657 *
Mario Six8fac2912018-07-10 08:40:17 +02002658 * See the Unified Extensible Firmware Interface (UEFI) specification for
2659 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002660 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002661static void EFIAPI efi_copy_mem(void *destination, const void *source,
2662 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002663{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002664 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002665 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002666 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002667}
2668
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002669/**
Mario Six8fac2912018-07-10 08:40:17 +02002670 * efi_set_mem() - Fill memory with a byte value.
2671 * @buffer: buffer to fill
2672 * @size: size of buffer in bytes
2673 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002674 *
2675 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002676 *
Mario Six8fac2912018-07-10 08:40:17 +02002677 * See the Unified Extensible Firmware Interface (UEFI) specification for
2678 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002679 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002680static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002681{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002682 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002683 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002684 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002685}
2686
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002687/**
Mario Six8fac2912018-07-10 08:40:17 +02002688 * efi_protocol_open() - open protocol interface on a handle
2689 * @handler: handler of a protocol
2690 * @protocol_interface: interface implementing the protocol
2691 * @agent_handle: handle of the driver
2692 * @controller_handle: handle of the controller
2693 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002694 *
Mario Six8fac2912018-07-10 08:40:17 +02002695 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002696 */
2697static efi_status_t efi_protocol_open(
2698 struct efi_handler *handler,
2699 void **protocol_interface, void *agent_handle,
2700 void *controller_handle, uint32_t attributes)
2701{
2702 struct efi_open_protocol_info_item *item;
2703 struct efi_open_protocol_info_entry *match = NULL;
2704 bool opened_by_driver = false;
2705 bool opened_exclusive = false;
2706
2707 /* If there is no agent, only return the interface */
2708 if (!agent_handle)
2709 goto out;
2710
2711 /* For TEST_PROTOCOL ignore interface attribute */
2712 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2713 *protocol_interface = NULL;
2714
2715 /*
2716 * Check if the protocol is already opened by a driver with the same
2717 * attributes or opened exclusively
2718 */
2719 list_for_each_entry(item, &handler->open_infos, link) {
2720 if (item->info.agent_handle == agent_handle) {
2721 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2722 (item->info.attributes == attributes))
2723 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002724 } else {
2725 if (item->info.attributes &
2726 EFI_OPEN_PROTOCOL_BY_DRIVER)
2727 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002728 }
2729 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2730 opened_exclusive = true;
2731 }
2732
2733 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002734 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2735 if (opened_exclusive)
2736 return EFI_ACCESS_DENIED;
2737 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2738 if (opened_exclusive || opened_by_driver)
2739 return EFI_ACCESS_DENIED;
2740 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002741
2742 /* Prepare exclusive opening */
2743 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2744 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002745disconnect_next:
2746 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002747 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002748 efi_status_t ret;
2749
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002750 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002751 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2752 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002753 item->info.controller_handle,
2754 item->info.agent_handle,
2755 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002756 if (ret == EFI_SUCCESS)
2757 /*
2758 * Child controllers may have been
2759 * removed from the open_infos list. So
2760 * let's restart the loop.
2761 */
2762 goto disconnect_next;
2763 else
2764 opened_by_driver = true;
2765 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002766 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002767 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002768 if (opened_by_driver)
2769 return EFI_ACCESS_DENIED;
2770 }
2771
2772 /* Find existing entry */
2773 list_for_each_entry(item, &handler->open_infos, link) {
2774 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002775 item->info.controller_handle == controller_handle &&
2776 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002777 match = &item->info;
2778 }
2779 /* None found, create one */
2780 if (!match) {
2781 match = efi_create_open_info(handler);
2782 if (!match)
2783 return EFI_OUT_OF_RESOURCES;
2784 }
2785
2786 match->agent_handle = agent_handle;
2787 match->controller_handle = controller_handle;
2788 match->attributes = attributes;
2789 match->open_count++;
2790
2791out:
2792 /* For TEST_PROTOCOL ignore interface attribute. */
2793 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2794 *protocol_interface = handler->protocol_interface;
2795
2796 return EFI_SUCCESS;
2797}
2798
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002799/**
Mario Six8fac2912018-07-10 08:40:17 +02002800 * efi_open_protocol() - open protocol interface on a handle
2801 * @handle: handle on which the protocol shall be opened
2802 * @protocol: GUID of the protocol
2803 * @protocol_interface: interface implementing the protocol
2804 * @agent_handle: handle of the driver
2805 * @controller_handle: handle of the controller
2806 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002807 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002808 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002809 *
Mario Six8fac2912018-07-10 08:40:17 +02002810 * See the Unified Extensible Firmware Interface (UEFI) specification for
2811 * details.
2812 *
2813 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002814 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002815static efi_status_t EFIAPI efi_open_protocol
2816 (efi_handle_t handle, const efi_guid_t *protocol,
2817 void **protocol_interface, efi_handle_t agent_handle,
2818 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002819{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002820 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002821 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002822
Rob Clark238f88c2017-09-13 18:05:41 -04002823 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002824 protocol_interface, agent_handle, controller_handle,
2825 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002826
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002827 if (!handle || !protocol ||
2828 (!protocol_interface && attributes !=
2829 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002830 goto out;
2831 }
2832
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002833 switch (attributes) {
2834 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2835 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2836 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2837 break;
2838 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2839 if (controller_handle == handle)
2840 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002841 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002842 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2843 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002844 /* Check that the controller handle is valid */
2845 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002846 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002847 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002848 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002849 /* Check that the agent handle is valid */
2850 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002851 goto out;
2852 break;
2853 default:
2854 goto out;
2855 }
2856
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002857 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002858 switch (r) {
2859 case EFI_SUCCESS:
2860 break;
2861 case EFI_NOT_FOUND:
2862 r = EFI_UNSUPPORTED;
2863 goto out;
2864 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002865 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002866 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002867
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002868 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2869 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002870out:
2871 return EFI_EXIT(r);
2872}
2873
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002874/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002875 * efi_start_image() - call the entry point of an image
2876 * @image_handle: handle of the image
2877 * @exit_data_size: size of the buffer
2878 * @exit_data: buffer to receive the exit data of the called image
2879 *
2880 * This function implements the StartImage service.
2881 *
2882 * See the Unified Extensible Firmware Interface (UEFI) specification for
2883 * details.
2884 *
2885 * Return: status code
2886 */
2887efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2888 efi_uintn_t *exit_data_size,
2889 u16 **exit_data)
2890{
2891 struct efi_loaded_image_obj *image_obj =
2892 (struct efi_loaded_image_obj *)image_handle;
2893 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002894 void *info;
2895 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002896
2897 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2898
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002899 if (!efi_search_obj(image_handle))
2900 return EFI_EXIT(EFI_INVALID_PARAMETER);
2901
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002902 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002903 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2904 return EFI_EXIT(EFI_INVALID_PARAMETER);
2905
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002906 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2907 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2908
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002909 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2910 &info, NULL, NULL,
2911 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2912 if (ret != EFI_SUCCESS)
2913 return EFI_EXIT(EFI_INVALID_PARAMETER);
2914
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002915 image_obj->exit_data_size = exit_data_size;
2916 image_obj->exit_data = exit_data;
2917
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002918 /* call the image! */
2919 if (setjmp(&image_obj->exit_jmp)) {
2920 /*
2921 * We called the entry point of the child image with EFI_CALL
2922 * in the lines below. The child image called the Exit() boot
2923 * service efi_exit() which executed the long jump that brought
2924 * us to the current line. This implies that the second half
2925 * of the EFI_CALL macro has not been executed.
2926 */
2927#ifdef CONFIG_ARM
2928 /*
2929 * efi_exit() called efi_restore_gd(). We have to undo this
2930 * otherwise __efi_entry_check() will put the wrong value into
2931 * app_gd.
2932 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02002933 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002934#endif
2935 /*
2936 * To get ready to call EFI_EXIT below we have to execute the
2937 * missed out steps of EFI_CALL.
2938 */
2939 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002940 EFI_PRINT("%lu returned by started image\n",
2941 (unsigned long)((uintptr_t)image_obj->exit_status &
2942 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002943 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002944 return EFI_EXIT(image_obj->exit_status);
2945 }
2946
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002947 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002948 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002949 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002950 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2951
2952 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01002953 * Control is returned from a started UEFI image either by calling
2954 * Exit() (where exit data can be provided) or by simply returning from
2955 * the entry point. In the latter case call Exit() on behalf of the
2956 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002957 */
2958 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2959}
2960
2961/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002962 * efi_delete_image() - delete loaded image from memory)
2963 *
2964 * @image_obj: handle of the loaded image
2965 * @loaded_image_protocol: loaded image protocol
2966 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002967static efi_status_t efi_delete_image
2968 (struct efi_loaded_image_obj *image_obj,
2969 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002970{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002971 struct efi_object *efiobj;
2972 efi_status_t r, ret = EFI_SUCCESS;
2973
2974close_next:
2975 list_for_each_entry(efiobj, &efi_obj_list, link) {
2976 struct efi_handler *protocol;
2977
2978 list_for_each_entry(protocol, &efiobj->protocols, link) {
2979 struct efi_open_protocol_info_item *info;
2980
2981 list_for_each_entry(info, &protocol->open_infos, link) {
2982 if (info->info.agent_handle !=
2983 (efi_handle_t)image_obj)
2984 continue;
2985 r = EFI_CALL(efi_close_protocol
2986 (efiobj, protocol->guid,
2987 info->info.agent_handle,
2988 info->info.controller_handle
2989 ));
2990 if (r != EFI_SUCCESS)
2991 ret = r;
2992 /*
2993 * Closing protocols may results in further
2994 * items being deleted. To play it safe loop
2995 * over all elements again.
2996 */
2997 goto close_next;
2998 }
2999 }
3000 }
3001
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003002 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3003 efi_size_in_pages(loaded_image_protocol->image_size));
3004 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003005
3006 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003007}
3008
3009/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003010 * efi_unload_image() - unload an EFI image
3011 * @image_handle: handle of the image to be unloaded
3012 *
3013 * This function implements the UnloadImage service.
3014 *
3015 * See the Unified Extensible Firmware Interface (UEFI) specification for
3016 * details.
3017 *
3018 * Return: status code
3019 */
3020efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3021{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003022 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003023 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003024 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003025
3026 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003027
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003028 efiobj = efi_search_obj(image_handle);
3029 if (!efiobj) {
3030 ret = EFI_INVALID_PARAMETER;
3031 goto out;
3032 }
3033 /* Find the loaded image protocol */
3034 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3035 (void **)&loaded_image_protocol,
3036 NULL, NULL,
3037 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3038 if (ret != EFI_SUCCESS) {
3039 ret = EFI_INVALID_PARAMETER;
3040 goto out;
3041 }
3042 switch (efiobj->type) {
3043 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3044 /* Call the unload function */
3045 if (!loaded_image_protocol->unload) {
3046 ret = EFI_UNSUPPORTED;
3047 goto out;
3048 }
3049 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3050 if (ret != EFI_SUCCESS)
3051 goto out;
3052 break;
3053 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3054 break;
3055 default:
3056 ret = EFI_INVALID_PARAMETER;
3057 goto out;
3058 }
3059 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3060 loaded_image_protocol);
3061out:
3062 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003063}
3064
3065/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003066 * efi_update_exit_data() - fill exit data parameters of StartImage()
3067 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003068 * @image_obj: image handle
3069 * @exit_data_size: size of the exit data buffer
3070 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003071 * Return: status code
3072 */
3073static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3074 efi_uintn_t exit_data_size,
3075 u16 *exit_data)
3076{
3077 efi_status_t ret;
3078
3079 /*
3080 * If exit_data is not provided to StartImage(), exit_data_size must be
3081 * ignored.
3082 */
3083 if (!image_obj->exit_data)
3084 return EFI_SUCCESS;
3085 if (image_obj->exit_data_size)
3086 *image_obj->exit_data_size = exit_data_size;
3087 if (exit_data_size && exit_data) {
3088 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3089 exit_data_size,
3090 (void **)image_obj->exit_data);
3091 if (ret != EFI_SUCCESS)
3092 return ret;
3093 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3094 } else {
3095 image_obj->exit_data = NULL;
3096 }
3097 return EFI_SUCCESS;
3098}
3099
3100/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003101 * efi_exit() - leave an EFI application or driver
3102 * @image_handle: handle of the application or driver that is exiting
3103 * @exit_status: status code
3104 * @exit_data_size: size of the buffer in bytes
3105 * @exit_data: buffer with data describing an error
3106 *
3107 * This function implements the Exit service.
3108 *
3109 * See the Unified Extensible Firmware Interface (UEFI) specification for
3110 * details.
3111 *
3112 * Return: status code
3113 */
3114static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3115 efi_status_t exit_status,
3116 efi_uintn_t exit_data_size,
3117 u16 *exit_data)
3118{
3119 /*
3120 * TODO: We should call the unload procedure of the loaded
3121 * image protocol.
3122 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003123 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003124 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003125 struct efi_loaded_image_obj *image_obj =
3126 (struct efi_loaded_image_obj *)image_handle;
3127
3128 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3129 exit_data_size, exit_data);
3130
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003131 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003132 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003133 (void **)&loaded_image_protocol,
3134 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003135 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003136 if (ret != EFI_SUCCESS) {
3137 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003138 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003139 }
3140
3141 /* Unloading of unstarted images */
3142 switch (image_obj->header.type) {
3143 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3144 break;
3145 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3146 efi_delete_image(image_obj, loaded_image_protocol);
3147 ret = EFI_SUCCESS;
3148 goto out;
3149 default:
3150 /* Handle does not refer to loaded image */
3151 ret = EFI_INVALID_PARAMETER;
3152 goto out;
3153 }
3154 /* A started image can only be unloaded it is the last one started. */
3155 if (image_handle != current_image) {
3156 ret = EFI_INVALID_PARAMETER;
3157 goto out;
3158 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003159
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003160 /* Exit data is only foreseen in case of failure. */
3161 if (exit_status != EFI_SUCCESS) {
3162 ret = efi_update_exit_data(image_obj, exit_data_size,
3163 exit_data);
3164 /* Exiting has priority. Don't return error to caller. */
3165 if (ret != EFI_SUCCESS)
3166 EFI_PRINT("%s: out of memory\n", __func__);
3167 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003168 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3169 exit_status != EFI_SUCCESS)
3170 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003171
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003172 /* Make sure entry/exit counts for EFI world cross-overs match */
3173 EFI_EXIT(exit_status);
3174
3175 /*
3176 * But longjmp out with the U-Boot gd, not the application's, as
3177 * the other end is a setjmp call inside EFI context.
3178 */
3179 efi_restore_gd();
3180
3181 image_obj->exit_status = exit_status;
3182 longjmp(&image_obj->exit_jmp, 1);
3183
3184 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003185out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003186 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003187}
3188
3189/**
Mario Six8fac2912018-07-10 08:40:17 +02003190 * efi_handle_protocol() - get interface of a protocol on a handle
3191 * @handle: handle on which the protocol shall be opened
3192 * @protocol: GUID of the protocol
3193 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003194 *
3195 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003196 *
3197 * See the Unified Extensible Firmware Interface (UEFI) specification for
3198 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003199 *
Mario Six8fac2912018-07-10 08:40:17 +02003200 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003201 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003202efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3203 const efi_guid_t *protocol,
3204 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003205{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003206 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003207 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003208}
3209
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003210/**
Mario Six8fac2912018-07-10 08:40:17 +02003211 * efi_bind_controller() - bind a single driver to a controller
3212 * @controller_handle: controller handle
3213 * @driver_image_handle: driver handle
3214 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003215 *
Mario Six8fac2912018-07-10 08:40:17 +02003216 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003217 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003218static efi_status_t efi_bind_controller(
3219 efi_handle_t controller_handle,
3220 efi_handle_t driver_image_handle,
3221 struct efi_device_path *remain_device_path)
3222{
3223 struct efi_driver_binding_protocol *binding_protocol;
3224 efi_status_t r;
3225
3226 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3227 &efi_guid_driver_binding_protocol,
3228 (void **)&binding_protocol,
3229 driver_image_handle, NULL,
3230 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3231 if (r != EFI_SUCCESS)
3232 return r;
3233 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3234 controller_handle,
3235 remain_device_path));
3236 if (r == EFI_SUCCESS)
3237 r = EFI_CALL(binding_protocol->start(binding_protocol,
3238 controller_handle,
3239 remain_device_path));
3240 EFI_CALL(efi_close_protocol(driver_image_handle,
3241 &efi_guid_driver_binding_protocol,
3242 driver_image_handle, NULL));
3243 return r;
3244}
3245
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003246/**
Mario Six8fac2912018-07-10 08:40:17 +02003247 * efi_connect_single_controller() - connect a single driver to a controller
3248 * @controller_handle: controller
3249 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003250 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003251 *
Mario Six8fac2912018-07-10 08:40:17 +02003252 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003253 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003254static efi_status_t efi_connect_single_controller(
3255 efi_handle_t controller_handle,
3256 efi_handle_t *driver_image_handle,
3257 struct efi_device_path *remain_device_path)
3258{
3259 efi_handle_t *buffer;
3260 size_t count;
3261 size_t i;
3262 efi_status_t r;
3263 size_t connected = 0;
3264
3265 /* Get buffer with all handles with driver binding protocol */
3266 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3267 &efi_guid_driver_binding_protocol,
3268 NULL, &count, &buffer));
3269 if (r != EFI_SUCCESS)
3270 return r;
3271
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003272 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003273 if (driver_image_handle) {
3274 for (; *driver_image_handle; ++driver_image_handle) {
3275 for (i = 0; i < count; ++i) {
3276 if (buffer[i] == *driver_image_handle) {
3277 buffer[i] = NULL;
3278 r = efi_bind_controller(
3279 controller_handle,
3280 *driver_image_handle,
3281 remain_device_path);
3282 /*
3283 * For drivers that do not support the
3284 * controller or are already connected
3285 * we receive an error code here.
3286 */
3287 if (r == EFI_SUCCESS)
3288 ++connected;
3289 }
3290 }
3291 }
3292 }
3293
3294 /*
3295 * TODO: Some overrides are not yet implemented:
3296 * - Platform Driver Override
3297 * - Driver Family Override Search
3298 * - Bus Specific Driver Override
3299 */
3300
3301 /* Driver Binding Search */
3302 for (i = 0; i < count; ++i) {
3303 if (buffer[i]) {
3304 r = efi_bind_controller(controller_handle,
3305 buffer[i],
3306 remain_device_path);
3307 if (r == EFI_SUCCESS)
3308 ++connected;
3309 }
3310 }
3311
3312 efi_free_pool(buffer);
3313 if (!connected)
3314 return EFI_NOT_FOUND;
3315 return EFI_SUCCESS;
3316}
3317
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003318/**
Mario Six8fac2912018-07-10 08:40:17 +02003319 * efi_connect_controller() - connect a controller to a driver
3320 * @controller_handle: handle of the controller
3321 * @driver_image_handle: handle of the driver
3322 * @remain_device_path: device path of a child controller
3323 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003324 *
3325 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003326 *
3327 * See the Unified Extensible Firmware Interface (UEFI) specification for
3328 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003329 *
3330 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003331 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003332 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3333 *
Mario Six8fac2912018-07-10 08:40:17 +02003334 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003335 */
3336static efi_status_t EFIAPI efi_connect_controller(
3337 efi_handle_t controller_handle,
3338 efi_handle_t *driver_image_handle,
3339 struct efi_device_path *remain_device_path,
3340 bool recursive)
3341{
3342 efi_status_t r;
3343 efi_status_t ret = EFI_NOT_FOUND;
3344 struct efi_object *efiobj;
3345
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003346 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003347 remain_device_path, recursive);
3348
3349 efiobj = efi_search_obj(controller_handle);
3350 if (!efiobj) {
3351 ret = EFI_INVALID_PARAMETER;
3352 goto out;
3353 }
3354
3355 r = efi_connect_single_controller(controller_handle,
3356 driver_image_handle,
3357 remain_device_path);
3358 if (r == EFI_SUCCESS)
3359 ret = EFI_SUCCESS;
3360 if (recursive) {
3361 struct efi_handler *handler;
3362 struct efi_open_protocol_info_item *item;
3363
3364 list_for_each_entry(handler, &efiobj->protocols, link) {
3365 list_for_each_entry(item, &handler->open_infos, link) {
3366 if (item->info.attributes &
3367 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3368 r = EFI_CALL(efi_connect_controller(
3369 item->info.controller_handle,
3370 driver_image_handle,
3371 remain_device_path,
3372 recursive));
3373 if (r == EFI_SUCCESS)
3374 ret = EFI_SUCCESS;
3375 }
3376 }
3377 }
3378 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003379 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003380 if (ret != EFI_SUCCESS && remain_device_path &&
3381 remain_device_path->type == DEVICE_PATH_TYPE_END)
3382 ret = EFI_SUCCESS;
3383out:
3384 return EFI_EXIT(ret);
3385}
3386
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003387/**
Mario Six8fac2912018-07-10 08:40:17 +02003388 * efi_reinstall_protocol_interface() - reinstall protocol interface
3389 * @handle: handle on which the protocol shall be reinstalled
3390 * @protocol: GUID of the protocol to be installed
3391 * @old_interface: interface to be removed
3392 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003393 *
3394 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003395 *
3396 * See the Unified Extensible Firmware Interface (UEFI) specification for
3397 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003398 *
3399 * The old interface is uninstalled. The new interface is installed.
3400 * Drivers are connected.
3401 *
Mario Six8fac2912018-07-10 08:40:17 +02003402 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003403 */
3404static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3405 efi_handle_t handle, const efi_guid_t *protocol,
3406 void *old_interface, void *new_interface)
3407{
3408 efi_status_t ret;
3409
3410 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3411 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003412
3413 /* Uninstall protocol but do not delete handle */
3414 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003415 if (ret != EFI_SUCCESS)
3416 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003417
3418 /* Install the new protocol */
3419 ret = efi_add_protocol(handle, protocol, new_interface);
3420 /*
3421 * The UEFI spec does not specify what should happen to the handle
3422 * if in case of an error no protocol interface remains on the handle.
3423 * So let's do nothing here.
3424 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003425 if (ret != EFI_SUCCESS)
3426 goto out;
3427 /*
3428 * The returned status code has to be ignored.
3429 * Do not create an error if no suitable driver for the handle exists.
3430 */
3431 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3432out:
3433 return EFI_EXIT(ret);
3434}
3435
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003436/**
Mario Six8fac2912018-07-10 08:40:17 +02003437 * efi_get_child_controllers() - get all child controllers associated to a driver
3438 * @efiobj: handle of the controller
3439 * @driver_handle: handle of the driver
3440 * @number_of_children: number of child controllers
3441 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003442 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003443 * The allocated buffer has to be freed with free().
3444 *
Mario Six8fac2912018-07-10 08:40:17 +02003445 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003446 */
3447static efi_status_t efi_get_child_controllers(
3448 struct efi_object *efiobj,
3449 efi_handle_t driver_handle,
3450 efi_uintn_t *number_of_children,
3451 efi_handle_t **child_handle_buffer)
3452{
3453 struct efi_handler *handler;
3454 struct efi_open_protocol_info_item *item;
3455 efi_uintn_t count = 0, i;
3456 bool duplicate;
3457
3458 /* Count all child controller associations */
3459 list_for_each_entry(handler, &efiobj->protocols, link) {
3460 list_for_each_entry(item, &handler->open_infos, link) {
3461 if (item->info.agent_handle == driver_handle &&
3462 item->info.attributes &
3463 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3464 ++count;
3465 }
3466 }
3467 /*
3468 * Create buffer. In case of duplicate child controller assignments
3469 * the buffer will be too large. But that does not harm.
3470 */
3471 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003472 if (!count)
3473 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003474 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3475 if (!*child_handle_buffer)
3476 return EFI_OUT_OF_RESOURCES;
3477 /* Copy unique child handles */
3478 list_for_each_entry(handler, &efiobj->protocols, link) {
3479 list_for_each_entry(item, &handler->open_infos, link) {
3480 if (item->info.agent_handle == driver_handle &&
3481 item->info.attributes &
3482 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3483 /* Check this is a new child controller */
3484 duplicate = false;
3485 for (i = 0; i < *number_of_children; ++i) {
3486 if ((*child_handle_buffer)[i] ==
3487 item->info.controller_handle)
3488 duplicate = true;
3489 }
3490 /* Copy handle to buffer */
3491 if (!duplicate) {
3492 i = (*number_of_children)++;
3493 (*child_handle_buffer)[i] =
3494 item->info.controller_handle;
3495 }
3496 }
3497 }
3498 }
3499 return EFI_SUCCESS;
3500}
3501
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003502/**
Mario Six8fac2912018-07-10 08:40:17 +02003503 * efi_disconnect_controller() - disconnect a controller from a driver
3504 * @controller_handle: handle of the controller
3505 * @driver_image_handle: handle of the driver
3506 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003507 *
3508 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003509 *
3510 * See the Unified Extensible Firmware Interface (UEFI) specification for
3511 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003512 *
Mario Six8fac2912018-07-10 08:40:17 +02003513 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003514 */
3515static efi_status_t EFIAPI efi_disconnect_controller(
3516 efi_handle_t controller_handle,
3517 efi_handle_t driver_image_handle,
3518 efi_handle_t child_handle)
3519{
3520 struct efi_driver_binding_protocol *binding_protocol;
3521 efi_handle_t *child_handle_buffer = NULL;
3522 size_t number_of_children = 0;
3523 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003524 struct efi_object *efiobj;
3525
3526 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3527 child_handle);
3528
3529 efiobj = efi_search_obj(controller_handle);
3530 if (!efiobj) {
3531 r = EFI_INVALID_PARAMETER;
3532 goto out;
3533 }
3534
3535 if (child_handle && !efi_search_obj(child_handle)) {
3536 r = EFI_INVALID_PARAMETER;
3537 goto out;
3538 }
3539
3540 /* If no driver handle is supplied, disconnect all drivers */
3541 if (!driver_image_handle) {
3542 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3543 goto out;
3544 }
3545
3546 /* Create list of child handles */
3547 if (child_handle) {
3548 number_of_children = 1;
3549 child_handle_buffer = &child_handle;
3550 } else {
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003551 r = efi_get_child_controllers(efiobj,
3552 driver_image_handle,
3553 &number_of_children,
3554 &child_handle_buffer);
3555 if (r != EFI_SUCCESS)
3556 return r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003557 }
3558
3559 /* Get the driver binding protocol */
3560 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3561 &efi_guid_driver_binding_protocol,
3562 (void **)&binding_protocol,
3563 driver_image_handle, NULL,
3564 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003565 if (r != EFI_SUCCESS) {
3566 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003567 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003568 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003569 /* Remove the children */
3570 if (number_of_children) {
3571 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3572 controller_handle,
3573 number_of_children,
3574 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003575 if (r != EFI_SUCCESS) {
3576 r = EFI_DEVICE_ERROR;
3577 goto out;
3578 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003579 }
3580 /* Remove the driver */
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003581 if (!child_handle) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003582 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3583 controller_handle,
3584 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003585 if (r != EFI_SUCCESS) {
3586 r = EFI_DEVICE_ERROR;
3587 goto out;
3588 }
3589 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003590 EFI_CALL(efi_close_protocol(driver_image_handle,
3591 &efi_guid_driver_binding_protocol,
3592 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003593 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003594out:
3595 if (!child_handle)
3596 free(child_handle_buffer);
3597 return EFI_EXIT(r);
3598}
3599
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003600static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003601 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003602 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3603 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003604 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003605 },
3606 .raise_tpl = efi_raise_tpl,
3607 .restore_tpl = efi_restore_tpl,
3608 .allocate_pages = efi_allocate_pages_ext,
3609 .free_pages = efi_free_pages_ext,
3610 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003611 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003612 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003613 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003614 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003615 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003616 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003617 .close_event = efi_close_event,
3618 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003619 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003620 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003621 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003622 .handle_protocol = efi_handle_protocol,
3623 .reserved = NULL,
3624 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003625 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003626 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003627 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003628 .load_image = efi_load_image,
3629 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003630 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003631 .unload_image = efi_unload_image,
3632 .exit_boot_services = efi_exit_boot_services,
3633 .get_next_monotonic_count = efi_get_next_monotonic_count,
3634 .stall = efi_stall,
3635 .set_watchdog_timer = efi_set_watchdog_timer,
3636 .connect_controller = efi_connect_controller,
3637 .disconnect_controller = efi_disconnect_controller,
3638 .open_protocol = efi_open_protocol,
3639 .close_protocol = efi_close_protocol,
3640 .open_protocol_information = efi_open_protocol_information,
3641 .protocols_per_handle = efi_protocols_per_handle,
3642 .locate_handle_buffer = efi_locate_handle_buffer,
3643 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003644 .install_multiple_protocol_interfaces =
3645 efi_install_multiple_protocol_interfaces,
3646 .uninstall_multiple_protocol_interfaces =
3647 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003648 .calculate_crc32 = efi_calculate_crc32,
3649 .copy_mem = efi_copy_mem,
3650 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003651 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003652};
3653
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003654static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003655
Alexander Graf393dd912016-10-14 13:45:30 +02003656struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003657 .hdr = {
3658 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003659 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003660 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003661 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003662 .fw_vendor = firmware_vendor,
3663 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003664 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003665 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003666 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003667};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003668
3669/**
3670 * efi_initialize_system_table() - Initialize system table
3671 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003672 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003673 */
3674efi_status_t efi_initialize_system_table(void)
3675{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003676 efi_status_t ret;
3677
3678 /* Allocate configuration table array */
3679 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3680 EFI_MAX_CONFIGURATION_TABLES *
3681 sizeof(struct efi_configuration_table),
3682 (void **)&systab.tables);
3683
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003684 /*
3685 * These entries will be set to NULL in ExitBootServices(). To avoid
3686 * relocation in SetVirtualAddressMap(), set them dynamically.
3687 */
3688 systab.con_in = &efi_con_in;
3689 systab.con_out = &efi_con_out;
3690 systab.std_err = &efi_con_out;
3691 systab.boottime = &efi_boot_services;
3692
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003693 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003694 efi_update_table_header_crc32(&systab.hdr);
3695 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3696 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003697
3698 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003699}