blob: c146b471285df6ffef974a69a31a1f4013620333 [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/*
3 * EFI application boot time services
4 *
5 * 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>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020016#include <pe.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010017#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020030/* Flag to disable timer activity in ExitBootServices() */
31static bool timers_enabled = true;
32
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020033/* List of all events registered by RegisterProtocolNotify() */
34LIST_HEAD(efi_register_notify_events);
35
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010036/* Handle of the currently executing image */
37static efi_handle_t current_image;
38
Alexander Graffa5246b2018-11-15 21:23:47 +010039/*
40 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
41 * we need to do trickery with caches. Since we don't want to break the EFI
42 * aware boot path, only apply hacks when loading exiting directly (breaking
43 * direct Linux EFI booting along the way - oh well).
44 */
45static bool efi_is_direct_boot = true;
46
Simon Glasscdfe6962016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010048/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060055#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010056
Heinrich Schuchardt72506232019-02-09 14:10:39 +010057/* 1 if inside U-Boot code, 0 if inside EFI payload code */
58static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040059static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010060/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010062/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040065
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010066/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010082static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010086
Rob Clark86789d52017-07-27 08:04:18 -040087/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
91#ifdef CONFIG_ARM
92 assert(efi_gd);
93 app_gd = gd;
94 gd = efi_gd;
95#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
103#ifdef CONFIG_ARM
104 gd = app_gd;
105#endif
106 return ret;
107}
108
Alexander Grafc15d9212016-03-04 01:09:59 +0100109/* Called from do_bootefi_exec() */
110void efi_save_gd(void)
111{
Simon Glasscdfe6962016-09-25 15:27:35 -0600112#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100113 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100115}
116
Rob Clark86789d52017-07-27 08:04:18 -0400117/*
Mario Six8fac2912018-07-10 08:40:17 +0200118 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200119 * world so we can dump out an abort message, without any care about returning
120 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400121 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100122void efi_restore_gd(void)
123{
Simon Glasscdfe6962016-09-25 15:27:35 -0600124#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 /* Only restore if we're already in EFI context */
126 if (!efi_gd)
127 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100128 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600129#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100130}
131
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200132/**
Mario Six8fac2912018-07-10 08:40:17 +0200133 * indent_string() - returns a string for indenting with two spaces per level
134 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100135 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200136 * A maximum of ten indent levels is supported. Higher indent levels will be
137 * truncated.
138 *
Mario Six8fac2912018-07-10 08:40:17 +0200139 * Return: A string for indenting with two spaces per level is
140 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400141 */
142static const char *indent_string(int level)
143{
144 const char *indent = " ";
145 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100146
Rob Clarke7896c32017-07-27 08:04:19 -0400147 level = min(max, level * 2);
148 return &indent[max - level];
149}
150
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200151const char *__efi_nesting(void)
152{
153 return indent_string(nesting_level);
154}
155
Rob Clarke7896c32017-07-27 08:04:19 -0400156const char *__efi_nesting_inc(void)
157{
158 return indent_string(nesting_level++);
159}
160
161const char *__efi_nesting_dec(void)
162{
163 return indent_string(--nesting_level);
164}
165
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200166/**
Mario Six8fac2912018-07-10 08:40:17 +0200167 * efi_queue_event() - queue an EFI event
168 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200169 *
170 * This function queues the notification function of the event for future
171 * execution.
172 *
Mario Six8fac2912018-07-10 08:40:17 +0200173 * The notification function is called if the task priority level of the event
174 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200175 *
176 * For the SignalEvent service see efi_signal_event_ext.
177 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200178 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200179static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200180{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200181 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200182 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200183 /* Check TPL */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200184 if (efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200185 return;
Heinrich Schuchardtad559972019-05-11 21:44:59 +0200186 event->is_queued = false;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
Heinrich Schuchardtad559972019-05-11 21:44:59 +0200189 } else {
190 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200191 }
192}
193
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200194/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200195 * is_valid_tpl() - check if the task priority level is valid
196 *
197 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200198 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200199 */
200efi_status_t is_valid_tpl(efi_uintn_t tpl)
201{
202 switch (tpl) {
203 case TPL_APPLICATION:
204 case TPL_CALLBACK:
205 case TPL_NOTIFY:
206 case TPL_HIGH_LEVEL:
207 return EFI_SUCCESS;
208 default:
209 return EFI_INVALID_PARAMETER;
210 }
211}
212
213/**
Mario Six8fac2912018-07-10 08:40:17 +0200214 * efi_signal_event() - signal an EFI event
215 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100216 *
Mario Six8fac2912018-07-10 08:40:17 +0200217 * This function signals an event. If the event belongs to an event group all
218 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100219 * their notification function is queued.
220 *
221 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100222 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200223void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100224{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200225 if (event->is_signaled)
226 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100227 if (event->group) {
228 struct efi_event *evt;
229
230 /*
231 * The signaled state has to set before executing any
232 * notification function
233 */
234 list_for_each_entry(evt, &efi_events, link) {
235 if (!evt->group || guidcmp(evt->group, event->group))
236 continue;
237 if (evt->is_signaled)
238 continue;
239 evt->is_signaled = true;
240 if (evt->type & EVT_NOTIFY_SIGNAL &&
241 evt->notify_function)
242 evt->is_queued = true;
243 }
244 list_for_each_entry(evt, &efi_events, link) {
245 if (!evt->group || guidcmp(evt->group, event->group))
246 continue;
247 if (evt->is_queued)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200248 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100249 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200250 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100251 event->is_signaled = true;
252 if (event->type & EVT_NOTIFY_SIGNAL)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200253 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100254 }
255}
256
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200257/**
Mario Six8fac2912018-07-10 08:40:17 +0200258 * efi_raise_tpl() - raise the task priority level
259 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200260 *
261 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200262 *
263 * See the Unified Extensible Firmware Interface (UEFI) specification for
264 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200265 *
Mario Six8fac2912018-07-10 08:40:17 +0200266 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200267 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100268static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100269{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100270 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200271
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200272 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200273
274 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200275 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200276 efi_tpl = new_tpl;
277 if (efi_tpl > TPL_HIGH_LEVEL)
278 efi_tpl = TPL_HIGH_LEVEL;
279
280 EFI_EXIT(EFI_SUCCESS);
281 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100282}
283
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200284/**
Mario Six8fac2912018-07-10 08:40:17 +0200285 * efi_restore_tpl() - lower the task priority level
286 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200287 *
288 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200289 *
Mario Six8fac2912018-07-10 08:40:17 +0200290 * See the Unified Extensible Firmware Interface (UEFI) specification for
291 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200292 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100293static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100294{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200295 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200296
297 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200298 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200299 efi_tpl = old_tpl;
300 if (efi_tpl > TPL_HIGH_LEVEL)
301 efi_tpl = TPL_HIGH_LEVEL;
302
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100303 /*
304 * Lowering the TPL may have made queued events eligible for execution.
305 */
306 efi_timer_check();
307
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200308 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100309}
310
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200311/**
Mario Six8fac2912018-07-10 08:40:17 +0200312 * efi_allocate_pages_ext() - allocate memory pages
313 * @type: type of allocation to be performed
314 * @memory_type: usage type of the allocated memory
315 * @pages: number of pages to be allocated
316 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200317 *
318 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200319 *
Mario Six8fac2912018-07-10 08:40:17 +0200320 * See the Unified Extensible Firmware Interface (UEFI) specification for
321 * details.
322 *
323 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900325static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100326 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900327 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100328{
329 efi_status_t r;
330
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100331 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100332 r = efi_allocate_pages(type, memory_type, pages, memory);
333 return EFI_EXIT(r);
334}
335
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200336/**
Mario Six8fac2912018-07-10 08:40:17 +0200337 * efi_free_pages_ext() - Free memory pages.
338 * @memory: start of the memory area to be freed
339 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200340 *
341 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200342 *
343 * See the Unified Extensible Firmware Interface (UEFI) specification for
344 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200345 *
Mario Six8fac2912018-07-10 08:40:17 +0200346 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900348static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100349 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100350{
351 efi_status_t r;
352
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900353 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100354 r = efi_free_pages(memory, pages);
355 return EFI_EXIT(r);
356}
357
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200358/**
Mario Six8fac2912018-07-10 08:40:17 +0200359 * efi_get_memory_map_ext() - get map describing memory usage
360 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
361 * on exit the size of the copied memory map
362 * @memory_map: buffer to which the memory map is written
363 * @map_key: key for the memory map
364 * @descriptor_size: size of an individual memory descriptor
365 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 *
367 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200368 *
Mario Six8fac2912018-07-10 08:40:17 +0200369 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * details.
371 *
372 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900374static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100375 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900376 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100377 efi_uintn_t *map_key,
378 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900379 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100380{
381 efi_status_t r;
382
383 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
384 map_key, descriptor_size, descriptor_version);
385 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
386 descriptor_size, descriptor_version);
387 return EFI_EXIT(r);
388}
389
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200390/**
Mario Six8fac2912018-07-10 08:40:17 +0200391 * efi_allocate_pool_ext() - allocate memory from pool
392 * @pool_type: type of the pool from which memory is to be allocated
393 * @size: number of bytes to be allocated
394 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
396 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200397 *
398 * See the Unified Extensible Firmware Interface (UEFI) specification for
399 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200400 *
Mario Six8fac2912018-07-10 08:40:17 +0200401 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200402 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200403static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100404 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200405 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100406{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100407 efi_status_t r;
408
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100409 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200410 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100411 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100412}
413
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200414/**
Mario Six8fac2912018-07-10 08:40:17 +0200415 * efi_free_pool_ext() - free memory from pool
416 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200417 *
418 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200419 *
Mario Six8fac2912018-07-10 08:40:17 +0200420 * See the Unified Extensible Firmware Interface (UEFI) specification for
421 * details.
422 *
423 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200424 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200425static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100426{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100427 efi_status_t r;
428
429 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200430 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100431 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100432}
433
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200434/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200435 * efi_add_handle() - add a new handle to the object list
436 *
437 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100438 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200439 * The protocols list is initialized. The handle is added to the list of known
440 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100441 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200442void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100443{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200444 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100445 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200446 INIT_LIST_HEAD(&handle->protocols);
447 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100448}
449
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200450/**
Mario Six8fac2912018-07-10 08:40:17 +0200451 * efi_create_handle() - create handle
452 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200453 *
Mario Six8fac2912018-07-10 08:40:17 +0200454 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200455 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100456efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200457{
458 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200459
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200460 obj = calloc(1, sizeof(struct efi_object));
461 if (!obj)
462 return EFI_OUT_OF_RESOURCES;
463
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100464 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200465 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200466
467 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200468}
469
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200470/**
Mario Six8fac2912018-07-10 08:40:17 +0200471 * efi_search_protocol() - find a protocol on a handle.
472 * @handle: handle
473 * @protocol_guid: GUID of the protocol
474 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100475 *
Mario Six8fac2912018-07-10 08:40:17 +0200476 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100477 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100478efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100479 const efi_guid_t *protocol_guid,
480 struct efi_handler **handler)
481{
482 struct efi_object *efiobj;
483 struct list_head *lhandle;
484
485 if (!handle || !protocol_guid)
486 return EFI_INVALID_PARAMETER;
487 efiobj = efi_search_obj(handle);
488 if (!efiobj)
489 return EFI_INVALID_PARAMETER;
490 list_for_each(lhandle, &efiobj->protocols) {
491 struct efi_handler *protocol;
492
493 protocol = list_entry(lhandle, struct efi_handler, link);
494 if (!guidcmp(protocol->guid, protocol_guid)) {
495 if (handler)
496 *handler = protocol;
497 return EFI_SUCCESS;
498 }
499 }
500 return EFI_NOT_FOUND;
501}
502
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200503/**
Mario Six8fac2912018-07-10 08:40:17 +0200504 * efi_remove_protocol() - delete protocol from a handle
505 * @handle: handle from which the protocol shall be deleted
506 * @protocol: GUID of the protocol to be deleted
507 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100508 *
Mario Six8fac2912018-07-10 08:40:17 +0200509 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100510 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100511efi_status_t efi_remove_protocol(const efi_handle_t handle,
512 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100513 void *protocol_interface)
514{
515 struct efi_handler *handler;
516 efi_status_t ret;
517
518 ret = efi_search_protocol(handle, protocol, &handler);
519 if (ret != EFI_SUCCESS)
520 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200521 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200522 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100523 list_del(&handler->link);
524 free(handler);
525 return EFI_SUCCESS;
526}
527
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200528/**
Mario Six8fac2912018-07-10 08:40:17 +0200529 * efi_remove_all_protocols() - delete all protocols from a handle
530 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100531 *
Mario Six8fac2912018-07-10 08:40:17 +0200532 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100533 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100534efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100535{
536 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100537 struct efi_handler *protocol;
538 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100539
540 efiobj = efi_search_obj(handle);
541 if (!efiobj)
542 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100543 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100544 efi_status_t ret;
545
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 ret = efi_remove_protocol(handle, protocol->guid,
547 protocol->protocol_interface);
548 if (ret != EFI_SUCCESS)
549 return ret;
550 }
551 return EFI_SUCCESS;
552}
553
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200554/**
Mario Six8fac2912018-07-10 08:40:17 +0200555 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100556 *
Mario Six8fac2912018-07-10 08:40:17 +0200557 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100558 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200559void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100560{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200561 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100562 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200563 efi_remove_all_protocols(handle);
564 list_del(&handle->link);
565 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100566}
567
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200568/**
Mario Six8fac2912018-07-10 08:40:17 +0200569 * efi_is_event() - check if a pointer is a valid event
570 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100571 *
Mario Six8fac2912018-07-10 08:40:17 +0200572 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100573 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100574static efi_status_t efi_is_event(const struct efi_event *event)
575{
576 const struct efi_event *evt;
577
578 if (!event)
579 return EFI_INVALID_PARAMETER;
580 list_for_each_entry(evt, &efi_events, link) {
581 if (evt == event)
582 return EFI_SUCCESS;
583 }
584 return EFI_INVALID_PARAMETER;
585}
Alexander Grafc15d9212016-03-04 01:09:59 +0100586
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200587/**
Mario Six8fac2912018-07-10 08:40:17 +0200588 * efi_create_event() - create an event
589 * @type: type of the event to create
590 * @notify_tpl: task priority level of the event
591 * @notify_function: notification function of the event
592 * @notify_context: pointer passed to the notification function
593 * @group: event group
594 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200595 *
596 * This function is used inside U-Boot code to create an event.
597 *
598 * For the API function implementing the CreateEvent service see
599 * efi_create_event_ext.
600 *
Mario Six8fac2912018-07-10 08:40:17 +0200601 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200602 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100603efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200604 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200605 struct efi_event *event,
606 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100607 void *notify_context, efi_guid_t *group,
608 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100609{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100610 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200611
Jonathan Gray7758b212017-03-12 19:26:07 +1100612 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200613 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100614
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200615 switch (type) {
616 case 0:
617 case EVT_TIMER:
618 case EVT_NOTIFY_SIGNAL:
619 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
620 case EVT_NOTIFY_WAIT:
621 case EVT_TIMER | EVT_NOTIFY_WAIT:
622 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
623 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
624 break;
625 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200626 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200627 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100628
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900629 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200630 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200631 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100632
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100633 evt = calloc(1, sizeof(struct efi_event));
634 if (!evt)
635 return EFI_OUT_OF_RESOURCES;
636 evt->type = type;
637 evt->notify_tpl = notify_tpl;
638 evt->notify_function = notify_function;
639 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100640 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200641 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100642 evt->trigger_next = -1ULL;
643 evt->is_queued = false;
644 evt->is_signaled = false;
645 list_add_tail(&evt->link, &efi_events);
646 *event = evt;
647 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100648}
649
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200650/*
Mario Six8fac2912018-07-10 08:40:17 +0200651 * efi_create_event_ex() - create an event in a group
652 * @type: type of the event to create
653 * @notify_tpl: task priority level of the event
654 * @notify_function: notification function of the event
655 * @notify_context: pointer passed to the notification function
656 * @event: created event
657 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100658 *
659 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100660 *
Mario Six8fac2912018-07-10 08:40:17 +0200661 * See the Unified Extensible Firmware Interface (UEFI) specification for
662 * details.
663 *
664 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100665 */
666efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
667 void (EFIAPI *notify_function) (
668 struct efi_event *event,
669 void *context),
670 void *notify_context,
671 efi_guid_t *event_group,
672 struct efi_event **event)
673{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200674 efi_status_t ret;
675
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100676 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
677 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200678
679 /*
680 * The allowable input parameters are the same as in CreateEvent()
681 * except for the following two disallowed event types.
682 */
683 switch (type) {
684 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
685 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
686 ret = EFI_INVALID_PARAMETER;
687 goto out;
688 }
689
690 ret = efi_create_event(type, notify_tpl, notify_function,
691 notify_context, event_group, event);
692out:
693 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100694}
695
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200696/**
Mario Six8fac2912018-07-10 08:40:17 +0200697 * efi_create_event_ext() - create an event
698 * @type: type of the event to create
699 * @notify_tpl: task priority level of the event
700 * @notify_function: notification function of the event
701 * @notify_context: pointer passed to the notification function
702 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703 *
704 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200705 *
706 * See the Unified Extensible Firmware Interface (UEFI) specification for
707 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200708 *
Mario Six8fac2912018-07-10 08:40:17 +0200709 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200710 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200711static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100712 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200713 void (EFIAPI *notify_function) (
714 struct efi_event *event,
715 void *context),
716 void *notify_context, struct efi_event **event)
717{
718 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
719 notify_context);
720 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100721 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200722}
723
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200724/**
Mario Six8fac2912018-07-10 08:40:17 +0200725 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200726 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200727 * Check if a timer event has occurred or a queued notification function should
728 * be called.
729 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100730 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200731 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100732 */
733void efi_timer_check(void)
734{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100735 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100736 u64 now = timer_get_us();
737
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100738 list_for_each_entry(evt, &efi_events, link) {
739 if (evt->is_queued)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200740 efi_queue_event(evt);
741 if (!timers_enabled)
742 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100743 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200744 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100745 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200746 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100747 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200748 break;
749 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100750 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200751 break;
752 default:
753 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200754 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100755 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200756 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100757 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100758 WATCHDOG_RESET();
759}
760
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200761/**
Mario Six8fac2912018-07-10 08:40:17 +0200762 * efi_set_timer() - set the trigger time for a timer event or stop the event
763 * @event: event for which the timer is set
764 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200765 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200766 *
767 * This is the function for internal usage in U-Boot. For the API function
768 * implementing the SetTimer service see efi_set_timer_ext.
769 *
Mario Six8fac2912018-07-10 08:40:17 +0200770 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200771 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200772efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200773 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100774{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100775 /* Check that the event is valid */
776 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
777 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100778
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200779 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200780 * The parameter defines a multiple of 100 ns.
781 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200782 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200783 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100784
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100785 switch (type) {
786 case EFI_TIMER_STOP:
787 event->trigger_next = -1ULL;
788 break;
789 case EFI_TIMER_PERIODIC:
790 case EFI_TIMER_RELATIVE:
791 event->trigger_next = timer_get_us() + trigger_time;
792 break;
793 default:
794 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100795 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100796 event->trigger_type = type;
797 event->trigger_time = trigger_time;
798 event->is_signaled = false;
799 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100800}
801
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200802/**
Mario Six8fac2912018-07-10 08:40:17 +0200803 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
804 * event
805 * @event: event for which the timer is set
806 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200807 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200808 *
809 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200810 *
Mario Six8fac2912018-07-10 08:40:17 +0200811 * See the Unified Extensible Firmware Interface (UEFI) specification for
812 * details.
813 *
814 *
815 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200816 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200817static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
818 enum efi_timer_delay type,
819 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200820{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900821 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200822 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
823}
824
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200825/**
Mario Six8fac2912018-07-10 08:40:17 +0200826 * efi_wait_for_event() - wait for events to be signaled
827 * @num_events: number of events to be waited for
828 * @event: events to be waited for
829 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200830 *
831 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200832 *
Mario Six8fac2912018-07-10 08:40:17 +0200833 * See the Unified Extensible Firmware Interface (UEFI) specification for
834 * details.
835 *
836 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200837 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100838static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200839 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100840 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100841{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100842 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100843
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100844 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100845
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200846 /* Check parameters */
847 if (!num_events || !event)
848 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200849 /* Check TPL */
850 if (efi_tpl != TPL_APPLICATION)
851 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200852 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100853 if (efi_is_event(event[i]) != EFI_SUCCESS)
854 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200855 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
856 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200857 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200858 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200859 }
860
861 /* Wait for signal */
862 for (;;) {
863 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200864 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200865 goto out;
866 }
867 /* Allow events to occur. */
868 efi_timer_check();
869 }
870
871out:
872 /*
873 * Reset the signal which is passed to the caller to allow periodic
874 * events to occur.
875 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200876 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200877 if (index)
878 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100879
880 return EFI_EXIT(EFI_SUCCESS);
881}
882
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200883/**
Mario Six8fac2912018-07-10 08:40:17 +0200884 * efi_signal_event_ext() - signal an EFI event
885 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 *
887 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200888 *
889 * See the Unified Extensible Firmware Interface (UEFI) specification for
890 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 *
892 * This functions sets the signaled state of the event and queues the
893 * notification function for execution.
894 *
Mario Six8fac2912018-07-10 08:40:17 +0200895 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200896 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200897static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100898{
899 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100900 if (efi_is_event(event) != EFI_SUCCESS)
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200902 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100903 return EFI_EXIT(EFI_SUCCESS);
904}
905
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200906/**
Mario Six8fac2912018-07-10 08:40:17 +0200907 * efi_close_event() - close an EFI event
908 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200909 *
910 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200911 *
Mario Six8fac2912018-07-10 08:40:17 +0200912 * See the Unified Extensible Firmware Interface (UEFI) specification for
913 * details.
914 *
915 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200916 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200917static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100918{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200919 struct efi_register_notify_event *item, *next;
920
Alexander Grafc15d9212016-03-04 01:09:59 +0100921 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100922 if (efi_is_event(event) != EFI_SUCCESS)
923 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200924
925 /* Remove protocol notify registrations for the event */
926 list_for_each_entry_safe(item, next, &efi_register_notify_events,
927 link) {
928 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200929 struct efi_protocol_notification *hitem, *hnext;
930
931 /* Remove signaled handles */
932 list_for_each_entry_safe(hitem, hnext, &item->handles,
933 link) {
934 list_del(&hitem->link);
935 free(hitem);
936 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200937 list_del(&item->link);
938 free(item);
939 }
940 }
941
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100942 list_del(&event->link);
943 free(event);
944 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100945}
946
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200947/**
Mario Six8fac2912018-07-10 08:40:17 +0200948 * efi_check_event() - check if an event is signaled
949 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200950 *
951 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200952 *
953 * See the Unified Extensible Firmware Interface (UEFI) specification for
954 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200955 *
Mario Six8fac2912018-07-10 08:40:17 +0200956 * If an event is not signaled yet, the notification function is queued. The
957 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200958 *
Mario Six8fac2912018-07-10 08:40:17 +0200959 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200960 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200961static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100962{
963 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200964 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100965 if (efi_is_event(event) != EFI_SUCCESS ||
966 event->type & EVT_NOTIFY_SIGNAL)
967 return EFI_EXIT(EFI_INVALID_PARAMETER);
968 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200969 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100970 if (event->is_signaled) {
971 event->is_signaled = false;
972 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200973 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100974 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100975}
976
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200977/**
Mario Six8fac2912018-07-10 08:40:17 +0200978 * efi_search_obj() - find the internal EFI object for a handle
979 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200980 *
Mario Six8fac2912018-07-10 08:40:17 +0200981 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200982 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100983struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200984{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100985 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200986
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +0200987 if (!handle)
988 return NULL;
989
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100990 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200991 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200992 return efiobj;
993 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200994 return NULL;
995}
996
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200997/**
Mario Six8fac2912018-07-10 08:40:17 +0200998 * efi_open_protocol_info_entry() - create open protocol info entry and add it
999 * to a protocol
1000 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001001 *
Mario Six8fac2912018-07-10 08:40:17 +02001002 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001003 */
1004static struct efi_open_protocol_info_entry *efi_create_open_info(
1005 struct efi_handler *handler)
1006{
1007 struct efi_open_protocol_info_item *item;
1008
1009 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1010 if (!item)
1011 return NULL;
1012 /* Append the item to the open protocol info list. */
1013 list_add_tail(&item->link, &handler->open_infos);
1014
1015 return &item->info;
1016}
1017
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001018/**
Mario Six8fac2912018-07-10 08:40:17 +02001019 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1020 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001021 *
Mario Six8fac2912018-07-10 08:40:17 +02001022 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001023 */
1024static efi_status_t efi_delete_open_info(
1025 struct efi_open_protocol_info_item *item)
1026{
1027 list_del(&item->link);
1028 free(item);
1029 return EFI_SUCCESS;
1030}
1031
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001032/**
Mario Six8fac2912018-07-10 08:40:17 +02001033 * efi_add_protocol() - install new protocol on a handle
1034 * @handle: handle on which the protocol shall be installed
1035 * @protocol: GUID of the protocol to be installed
1036 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001037 *
Mario Six8fac2912018-07-10 08:40:17 +02001038 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001039 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001040efi_status_t efi_add_protocol(const efi_handle_t handle,
1041 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001042 void *protocol_interface)
1043{
1044 struct efi_object *efiobj;
1045 struct efi_handler *handler;
1046 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001047 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001048
1049 efiobj = efi_search_obj(handle);
1050 if (!efiobj)
1051 return EFI_INVALID_PARAMETER;
1052 ret = efi_search_protocol(handle, protocol, NULL);
1053 if (ret != EFI_NOT_FOUND)
1054 return EFI_INVALID_PARAMETER;
1055 handler = calloc(1, sizeof(struct efi_handler));
1056 if (!handler)
1057 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001058 handler->guid = protocol;
1059 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001060 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001061 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001062
1063 /* Notify registered events */
1064 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001065 if (!guidcmp(protocol, &event->protocol)) {
1066 struct efi_protocol_notification *notif;
1067
1068 notif = calloc(1, sizeof(*notif));
1069 if (!notif) {
1070 list_del(&handler->link);
1071 free(handler);
1072 return EFI_OUT_OF_RESOURCES;
1073 }
1074 notif->handle = handle;
1075 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001076 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001077 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001078 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001079 }
1080
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001081 if (!guidcmp(&efi_guid_device_path, protocol))
1082 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001083 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001084}
1085
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001086/**
Mario Six8fac2912018-07-10 08:40:17 +02001087 * efi_install_protocol_interface() - install protocol interface
1088 * @handle: handle on which the protocol shall be installed
1089 * @protocol: GUID of the protocol to be installed
1090 * @protocol_interface_type: type of the interface to be installed,
1091 * always EFI_NATIVE_INTERFACE
1092 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001093 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001094 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001095 *
Mario Six8fac2912018-07-10 08:40:17 +02001096 * See the Unified Extensible Firmware Interface (UEFI) specification for
1097 * details.
1098 *
1099 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001100 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001101static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001102 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001103 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001104{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001105 efi_status_t r;
1106
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001107 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1108 protocol_interface);
1109
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001110 if (!handle || !protocol ||
1111 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1112 r = EFI_INVALID_PARAMETER;
1113 goto out;
1114 }
1115
1116 /* Create new handle if requested. */
1117 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001118 r = efi_create_handle(handle);
1119 if (r != EFI_SUCCESS)
1120 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001121 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001122 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001123 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001124 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001125 /* Add new protocol */
1126 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001127out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001128 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001129}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001130
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001131/**
Mario Six8fac2912018-07-10 08:40:17 +02001132 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001133 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001134 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001135 * @number_of_drivers: number of child controllers
1136 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001137 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001138 * The allocated buffer has to be freed with free().
1139 *
Mario Six8fac2912018-07-10 08:40:17 +02001140 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001141 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001142static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001143 const efi_guid_t *protocol,
1144 efi_uintn_t *number_of_drivers,
1145 efi_handle_t **driver_handle_buffer)
1146{
1147 struct efi_handler *handler;
1148 struct efi_open_protocol_info_item *item;
1149 efi_uintn_t count = 0, i;
1150 bool duplicate;
1151
1152 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001153 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001154 if (protocol && guidcmp(handler->guid, protocol))
1155 continue;
1156 list_for_each_entry(item, &handler->open_infos, link) {
1157 if (item->info.attributes &
1158 EFI_OPEN_PROTOCOL_BY_DRIVER)
1159 ++count;
1160 }
1161 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001162 *number_of_drivers = 0;
1163 if (!count) {
1164 *driver_handle_buffer = NULL;
1165 return EFI_SUCCESS;
1166 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001167 /*
1168 * Create buffer. In case of duplicate driver assignments the buffer
1169 * will be too large. But that does not harm.
1170 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001171 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1172 if (!*driver_handle_buffer)
1173 return EFI_OUT_OF_RESOURCES;
1174 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001175 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001176 if (protocol && guidcmp(handler->guid, protocol))
1177 continue;
1178 list_for_each_entry(item, &handler->open_infos, link) {
1179 if (item->info.attributes &
1180 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1181 /* Check this is a new driver */
1182 duplicate = false;
1183 for (i = 0; i < *number_of_drivers; ++i) {
1184 if ((*driver_handle_buffer)[i] ==
1185 item->info.agent_handle)
1186 duplicate = true;
1187 }
1188 /* Copy handle to buffer */
1189 if (!duplicate) {
1190 i = (*number_of_drivers)++;
1191 (*driver_handle_buffer)[i] =
1192 item->info.agent_handle;
1193 }
1194 }
1195 }
1196 }
1197 return EFI_SUCCESS;
1198}
1199
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001200/**
Mario Six8fac2912018-07-10 08:40:17 +02001201 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001202 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001203 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001204 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001205 *
1206 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001207 *
1208 * See the Unified Extensible Firmware Interface (UEFI) specification for
1209 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001210 *
Mario Six8fac2912018-07-10 08:40:17 +02001211 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001212 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001213static efi_status_t efi_disconnect_all_drivers
1214 (efi_handle_t handle,
1215 const efi_guid_t *protocol,
1216 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001217{
1218 efi_uintn_t number_of_drivers;
1219 efi_handle_t *driver_handle_buffer;
1220 efi_status_t r, ret;
1221
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001222 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001223 &driver_handle_buffer);
1224 if (ret != EFI_SUCCESS)
1225 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001226 if (!number_of_drivers)
1227 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001228 ret = EFI_NOT_FOUND;
1229 while (number_of_drivers) {
1230 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001231 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001232 driver_handle_buffer[--number_of_drivers],
1233 child_handle));
1234 if (r == EFI_SUCCESS)
1235 ret = r;
1236 }
1237 free(driver_handle_buffer);
1238 return ret;
1239}
1240
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001241/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001242 * efi_uninstall_protocol() - uninstall protocol interface
1243 *
Mario Six8fac2912018-07-10 08:40:17 +02001244 * @handle: handle from which the protocol shall be removed
1245 * @protocol: GUID of the protocol to be removed
1246 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001247 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001248 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001249 *
1250 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001251 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001252static efi_status_t efi_uninstall_protocol
1253 (efi_handle_t handle, const efi_guid_t *protocol,
1254 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001255{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001256 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001257 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001258 struct efi_open_protocol_info_item *item;
1259 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001260 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001261
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001262 /* Check handle */
1263 efiobj = efi_search_obj(handle);
1264 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001265 r = EFI_INVALID_PARAMETER;
1266 goto out;
1267 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001268 /* Find the protocol on the handle */
1269 r = efi_search_protocol(handle, protocol, &handler);
1270 if (r != EFI_SUCCESS)
1271 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001272 /* Disconnect controllers */
1273 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001274 /* Close protocol */
1275 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1276 if (item->info.attributes ==
1277 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1278 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1279 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1280 list_del(&item->link);
1281 }
1282 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001283 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001284 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001285 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001286 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001287out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001288 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001289}
1290
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001291/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001292 * efi_uninstall_protocol_interface() - uninstall protocol interface
1293 * @handle: handle from which the protocol shall be removed
1294 * @protocol: GUID of the protocol to be removed
1295 * @protocol_interface: interface to be removed
1296 *
1297 * This function implements the UninstallProtocolInterface service.
1298 *
1299 * See the Unified Extensible Firmware Interface (UEFI) specification for
1300 * details.
1301 *
1302 * Return: status code
1303 */
1304static efi_status_t EFIAPI efi_uninstall_protocol_interface
1305 (efi_handle_t handle, const efi_guid_t *protocol,
1306 void *protocol_interface)
1307{
1308 efi_status_t ret;
1309
1310 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1311
1312 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1313 if (ret != EFI_SUCCESS)
1314 goto out;
1315
1316 /* If the last protocol has been removed, delete the handle. */
1317 if (list_empty(&handle->protocols)) {
1318 list_del(&handle->link);
1319 free(handle);
1320 }
1321out:
1322 return EFI_EXIT(ret);
1323}
1324
1325/**
Mario Six8fac2912018-07-10 08:40:17 +02001326 * efi_register_protocol_notify() - register an event for notification when a
1327 * protocol is installed.
1328 * @protocol: GUID of the protocol whose installation shall be notified
1329 * @event: event to be signaled upon installation of the protocol
1330 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001331 *
1332 * This function implements the RegisterProtocolNotify service.
1333 * See the Unified Extensible Firmware Interface (UEFI) specification
1334 * for details.
1335 *
Mario Six8fac2912018-07-10 08:40:17 +02001336 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001337 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001338static efi_status_t EFIAPI efi_register_protocol_notify(
1339 const efi_guid_t *protocol,
1340 struct efi_event *event,
1341 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001342{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001343 struct efi_register_notify_event *item;
1344 efi_status_t ret = EFI_SUCCESS;
1345
Rob Clark238f88c2017-09-13 18:05:41 -04001346 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001347
1348 if (!protocol || !event || !registration) {
1349 ret = EFI_INVALID_PARAMETER;
1350 goto out;
1351 }
1352
1353 item = calloc(1, sizeof(struct efi_register_notify_event));
1354 if (!item) {
1355 ret = EFI_OUT_OF_RESOURCES;
1356 goto out;
1357 }
1358
1359 item->event = event;
1360 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001361 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001362
1363 list_add_tail(&item->link, &efi_register_notify_events);
1364
1365 *registration = item;
1366out:
1367 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001368}
1369
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001370/**
Mario Six8fac2912018-07-10 08:40:17 +02001371 * efi_search() - determine if an EFI handle implements a protocol
1372 * @search_type: selection criterion
1373 * @protocol: GUID of the protocol
1374 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001375 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001376 *
1377 * See the documentation of the LocateHandle service in the UEFI specification.
1378 *
Mario Six8fac2912018-07-10 08:40:17 +02001379 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001380 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001381static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001382 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001383{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001384 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001385
1386 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001387 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001388 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001389 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001390 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001391 return (ret != EFI_SUCCESS);
1392 default:
1393 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001394 return -1;
1395 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001396}
1397
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001398/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001399 * efi_check_register_notify_event() - check if registration key is valid
1400 *
1401 * Check that a pointer is a valid registration key as returned by
1402 * RegisterProtocolNotify().
1403 *
1404 * @key: registration key
1405 * Return: valid registration key or NULL
1406 */
1407static struct efi_register_notify_event *efi_check_register_notify_event
1408 (void *key)
1409{
1410 struct efi_register_notify_event *event;
1411
1412 list_for_each_entry(event, &efi_register_notify_events, link) {
1413 if (event == (struct efi_register_notify_event *)key)
1414 return event;
1415 }
1416 return NULL;
1417}
1418
1419/**
Mario Six8fac2912018-07-10 08:40:17 +02001420 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001421 *
1422 * @search_type: selection criterion
1423 * @protocol: GUID of the protocol
1424 * @search_key: registration key
1425 * @buffer_size: size of the buffer to receive the handles in bytes
1426 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001427 *
1428 * This function is meant for U-Boot internal calls. For the API implementation
1429 * of the LocateHandle service see efi_locate_handle_ext.
1430 *
Mario Six8fac2912018-07-10 08:40:17 +02001431 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001432 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001433static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001434 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001435 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001436 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001437{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001438 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001439 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001440 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001441 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001442
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001443 /* Check parameters */
1444 switch (search_type) {
1445 case ALL_HANDLES:
1446 break;
1447 case BY_REGISTER_NOTIFY:
1448 if (!search_key)
1449 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001450 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001451 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001452 if (!event)
1453 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001454 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001455 case BY_PROTOCOL:
1456 if (!protocol)
1457 return EFI_INVALID_PARAMETER;
1458 break;
1459 default:
1460 return EFI_INVALID_PARAMETER;
1461 }
1462
Alexander Grafc15d9212016-03-04 01:09:59 +01001463 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001464 if (search_type == BY_REGISTER_NOTIFY) {
1465 if (list_empty(&event->handles))
1466 return EFI_NOT_FOUND;
1467 handle = list_first_entry(&event->handles,
1468 struct efi_protocol_notification,
1469 link);
1470 efiobj = handle->handle;
1471 size += sizeof(void *);
1472 } else {
1473 list_for_each_entry(efiobj, &efi_obj_list, link) {
1474 if (!efi_search(search_type, protocol, efiobj))
1475 size += sizeof(void *);
1476 }
1477 if (size == 0)
1478 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001479 }
1480
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001481 if (!buffer_size)
1482 return EFI_INVALID_PARAMETER;
1483
Alexander Grafc15d9212016-03-04 01:09:59 +01001484 if (*buffer_size < size) {
1485 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001486 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001487 }
1488
Rob Clarkcdee3372017-08-06 14:10:07 -04001489 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001490
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001491 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001492 if (!buffer)
1493 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001494
Alexander Grafc15d9212016-03-04 01:09:59 +01001495 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001496 if (search_type == BY_REGISTER_NOTIFY) {
1497 *buffer = efiobj;
1498 list_del(&handle->link);
1499 } else {
1500 list_for_each_entry(efiobj, &efi_obj_list, link) {
1501 if (!efi_search(search_type, protocol, efiobj))
1502 *buffer++ = efiobj;
1503 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001504 }
1505
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001506 return EFI_SUCCESS;
1507}
1508
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001509/**
Mario Six8fac2912018-07-10 08:40:17 +02001510 * efi_locate_handle_ext() - locate handles implementing a protocol.
1511 * @search_type: selection criterion
1512 * @protocol: GUID of the protocol
1513 * @search_key: registration key
1514 * @buffer_size: size of the buffer to receive the handles in bytes
1515 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001516 *
1517 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001518 *
Mario Six8fac2912018-07-10 08:40:17 +02001519 * See the Unified Extensible Firmware Interface (UEFI) specification for
1520 * details.
1521 *
1522 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001523 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001524static efi_status_t EFIAPI efi_locate_handle_ext(
1525 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001526 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001527 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001528{
Rob Clark238f88c2017-09-13 18:05:41 -04001529 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001530 buffer_size, buffer);
1531
1532 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1533 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001534}
1535
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001536/**
Mario Six8fac2912018-07-10 08:40:17 +02001537 * efi_remove_configuration_table() - collapses configuration table entries,
1538 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001539 *
Mario Six8fac2912018-07-10 08:40:17 +02001540 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001541 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001542static void efi_remove_configuration_table(int i)
1543{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001544 struct efi_configuration_table *this = &systab.tables[i];
1545 struct efi_configuration_table *next = &systab.tables[i + 1];
1546 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001547
1548 memmove(this, next, (ulong)end - (ulong)next);
1549 systab.nr_tables--;
1550}
1551
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001552/**
Mario Six8fac2912018-07-10 08:40:17 +02001553 * efi_install_configuration_table() - adds, updates, or removes a
1554 * configuration table
1555 * @guid: GUID of the installed table
1556 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001557 *
1558 * This function is used for internal calls. For the API implementation of the
1559 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1560 *
Mario Six8fac2912018-07-10 08:40:17 +02001561 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001562 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001563efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1564 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001565{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001566 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001567 int i;
1568
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001569 if (!guid)
1570 return EFI_INVALID_PARAMETER;
1571
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001572 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001573 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001574 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001575 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001576 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001577 else
1578 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001579 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001580 }
1581 }
1582
Alexander Graffe3366f2017-07-26 13:41:04 +02001583 if (!table)
1584 return EFI_NOT_FOUND;
1585
Alexander Grafc15d9212016-03-04 01:09:59 +01001586 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001587 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001588 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001589
1590 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001591 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1592 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001593 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001594
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001595out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001596 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001597 efi_update_table_header_crc32(&systab.hdr);
1598
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001599 /* Notify that the configuration table was changed */
1600 list_for_each_entry(evt, &efi_events, link) {
1601 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001602 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001603 break;
1604 }
1605 }
1606
Alexander Grafc5c11632016-08-19 01:23:24 +02001607 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001608}
1609
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001610/**
Mario Six8fac2912018-07-10 08:40:17 +02001611 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1612 * configuration table.
1613 * @guid: GUID of the installed table
1614 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001615 *
1616 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001617 *
Mario Six8fac2912018-07-10 08:40:17 +02001618 * See the Unified Extensible Firmware Interface (UEFI) specification for
1619 * details.
1620 *
1621 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001622 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001623static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1624 void *table)
1625{
Rob Clark238f88c2017-09-13 18:05:41 -04001626 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001627 return EFI_EXIT(efi_install_configuration_table(guid, table));
1628}
1629
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001630/**
Mario Six8fac2912018-07-10 08:40:17 +02001631 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001632 *
1633 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001634 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001635 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001636 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1637 * code is returned.
1638 *
1639 * @device_path: device path of the loaded image
1640 * @file_path: file path of the loaded image
1641 * @handle_ptr: handle of the loaded image
1642 * @info_ptr: loaded image protocol
1643 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001644 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001645efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1646 struct efi_device_path *file_path,
1647 struct efi_loaded_image_obj **handle_ptr,
1648 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001649{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001650 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001651 struct efi_loaded_image *info = NULL;
1652 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001653 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001654
1655 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1656 *handle_ptr = NULL;
1657 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001658
1659 info = calloc(1, sizeof(*info));
1660 if (!info)
1661 return EFI_OUT_OF_RESOURCES;
1662 obj = calloc(1, sizeof(*obj));
1663 if (!obj) {
1664 free(info);
1665 return EFI_OUT_OF_RESOURCES;
1666 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001667 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001668
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001669 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001670 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001671
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001672 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001673 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001674 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001675
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001676 if (device_path) {
1677 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001678
1679 dp = efi_dp_append(device_path, file_path);
1680 if (!dp) {
1681 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001682 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001683 }
1684 } else {
1685 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001686 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001687 ret = efi_add_protocol(&obj->header,
1688 &efi_guid_loaded_image_device_path, dp);
1689 if (ret != EFI_SUCCESS)
1690 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001691
1692 /*
1693 * When asking for the loaded_image interface, just
1694 * return handle which points to loaded_image_info
1695 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001696 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001697 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001698 if (ret != EFI_SUCCESS)
1699 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001700
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001701 *info_ptr = info;
1702 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001703
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001704 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001705failure:
1706 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001707 efi_delete_handle(&obj->header);
1708 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001709 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001710}
1711
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001712/**
Mario Six8fac2912018-07-10 08:40:17 +02001713 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001714 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001715 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1716 * callers obligation to update the memory type as needed.
1717 *
1718 * @file_path: the path of the image to load
1719 * @buffer: buffer containing the loaded image
1720 * @size: size of the loaded image
1721 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001722 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001723static
Rob Clarkc84c1102017-09-13 18:05:38 -04001724efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001725 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001726{
1727 struct efi_file_info *info = NULL;
1728 struct efi_file_handle *f;
1729 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001730 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001731 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001732
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001733 /* In case of failure nothing is returned */
1734 *buffer = NULL;
1735 *size = 0;
1736
1737 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001738 f = efi_file_from_path(file_path);
1739 if (!f)
1740 return EFI_DEVICE_ERROR;
1741
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001742 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001743 bs = 0;
1744 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1745 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001746 if (ret != EFI_BUFFER_TOO_SMALL) {
1747 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001748 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001749 }
Rob Clark857a1222017-09-13 18:05:35 -04001750
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001751 info = malloc(bs);
1752 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1753 info));
1754 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001755 goto error;
1756
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001757 /*
1758 * When reading the file we do not yet know if it contains an
1759 * application, a boottime driver, or a runtime driver. So here we
1760 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1761 * update the reservation according to the image type.
1762 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001763 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001764 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1765 EFI_BOOT_SERVICES_DATA,
1766 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001767 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001768 ret = EFI_OUT_OF_RESOURCES;
1769 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001770 }
1771
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001772 /* Read file */
1773 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1774 if (ret != EFI_SUCCESS)
1775 efi_free_pages(addr, efi_size_in_pages(bs));
1776 *buffer = (void *)(uintptr_t)addr;
1777 *size = bs;
1778error:
1779 EFI_CALL(f->close(f));
1780 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001781 return ret;
1782}
1783
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001784/**
Mario Six8fac2912018-07-10 08:40:17 +02001785 * efi_load_image() - load an EFI image into memory
1786 * @boot_policy: true for request originating from the boot manager
1787 * @parent_image: the caller's image handle
1788 * @file_path: the path of the image to load
1789 * @source_buffer: memory location from which the image is installed
1790 * @source_size: size of the memory area from which the image is installed
1791 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001792 *
1793 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001794 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001795 * See the Unified Extensible Firmware Interface (UEFI) specification
1796 * for details.
1797 *
Mario Six8fac2912018-07-10 08:40:17 +02001798 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001799 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001800efi_status_t EFIAPI efi_load_image(bool boot_policy,
1801 efi_handle_t parent_image,
1802 struct efi_device_path *file_path,
1803 void *source_buffer,
1804 efi_uintn_t source_size,
1805 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001806{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001807 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001808 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001809 struct efi_loaded_image_obj **image_obj =
1810 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001811 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001812 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001813
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001814 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001815 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001816
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001817 if (!image_handle || !efi_search_obj(parent_image)) {
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001818 ret = EFI_INVALID_PARAMETER;
1819 goto error;
1820 }
1821
1822 if (!source_buffer && !file_path) {
1823 ret = EFI_NOT_FOUND;
1824 goto error;
1825 }
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001826 /* The parent image handle must refer to a loaded image */
1827 if (!parent_image->type) {
1828 ret = EFI_INVALID_PARAMETER;
1829 goto error;
1830 }
Rob Clark857a1222017-09-13 18:05:35 -04001831
1832 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001833 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001834 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001835 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001836 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001837 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001838 if (!source_size) {
1839 ret = EFI_LOAD_ERROR;
1840 goto error;
1841 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001842 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001843 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001844 /* split file_path which contains both the device and file parts */
1845 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001846 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001847 if (ret == EFI_SUCCESS)
1848 ret = efi_load_pe(*image_obj, dest_buffer, info);
1849 if (!source_buffer)
1850 /* Release buffer to which file was loaded */
1851 efi_free_pages((uintptr_t)dest_buffer,
1852 efi_size_in_pages(source_size));
1853 if (ret == EFI_SUCCESS) {
1854 info->system_table = &systab;
1855 info->parent_handle = parent_image;
1856 } else {
1857 /* The image is invalid. Release all associated resources. */
1858 efi_delete_handle(*image_handle);
1859 *image_handle = NULL;
1860 free(info);
1861 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001862error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001863 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001864}
1865
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001866/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001867 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1868 */
1869static void efi_exit_caches(void)
1870{
1871#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1872 /*
1873 * Grub on 32bit ARM needs to have caches disabled before jumping into
1874 * a zImage, but does not know of all cache layers. Give it a hand.
1875 */
1876 if (efi_is_direct_boot)
1877 cleanup_before_linux();
1878#endif
1879}
1880
1881/**
Mario Six8fac2912018-07-10 08:40:17 +02001882 * efi_exit_boot_services() - stop all boot services
1883 * @image_handle: handle of the loaded image
1884 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001885 *
1886 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001887 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001888 * See the Unified Extensible Firmware Interface (UEFI) specification
1889 * for details.
1890 *
Mario Six8fac2912018-07-10 08:40:17 +02001891 * All timer events are disabled. For exit boot services events the
1892 * notification function is called. The boot services are disabled in the
1893 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001894 *
Mario Six8fac2912018-07-10 08:40:17 +02001895 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001896 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001897static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001898 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001899{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001900 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001901
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001902 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001903
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001904 /* Check that the caller has read the current memory map */
1905 if (map_key != efi_memory_map_key)
1906 return EFI_INVALID_PARAMETER;
1907
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001908 /* Check if ExitBootServices has already been called */
1909 if (!systab.boottime)
1910 return EFI_EXIT(EFI_SUCCESS);
1911
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001912 /* Stop all timer related activities */
1913 timers_enabled = false;
1914
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001915 /* Add related events to the event group */
1916 list_for_each_entry(evt, &efi_events, link) {
1917 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1918 evt->group = &efi_guid_event_group_exit_boot_services;
1919 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001920 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001921 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001922 if (evt->group &&
1923 !guidcmp(evt->group,
1924 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001925 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001926 break;
1927 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001928 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001929
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001930 /* Make sure that notification functions are not called anymore */
1931 efi_tpl = TPL_HIGH_LEVEL;
1932
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001933 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001934
Alexander Graf2ebeb442016-11-17 01:02:57 +01001935 board_quiesce_devices();
1936
Alexander Graffa5246b2018-11-15 21:23:47 +01001937 /* Fix up caches for EFI payloads if necessary */
1938 efi_exit_caches();
1939
Alexander Grafc15d9212016-03-04 01:09:59 +01001940 /* This stops all lingering devices */
1941 bootm_disable_interrupts();
1942
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001943 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001944 systab.con_in_handle = NULL;
1945 systab.con_in = NULL;
1946 systab.con_out_handle = NULL;
1947 systab.con_out = NULL;
1948 systab.stderr_handle = NULL;
1949 systab.std_err = NULL;
1950 systab.boottime = NULL;
1951
1952 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001953 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001954
Alexander Grafc15d9212016-03-04 01:09:59 +01001955 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001956 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001957 WATCHDOG_RESET();
1958
1959 return EFI_EXIT(EFI_SUCCESS);
1960}
1961
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001962/**
Mario Six8fac2912018-07-10 08:40:17 +02001963 * efi_get_next_monotonic_count() - get next value of the counter
1964 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001965 *
1966 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001967 *
1968 * See the Unified Extensible Firmware Interface (UEFI) specification for
1969 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001970 *
Mario Six8fac2912018-07-10 08:40:17 +02001971 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001972 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001973static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1974{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001975 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001976 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001977
Alexander Grafc15d9212016-03-04 01:09:59 +01001978 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001979 if (!count) {
1980 ret = EFI_INVALID_PARAMETER;
1981 goto out;
1982 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001983 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001984 ret = EFI_SUCCESS;
1985out:
1986 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001987}
1988
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001989/**
Mario Six8fac2912018-07-10 08:40:17 +02001990 * efi_stall() - sleep
1991 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001992 *
Mario Six8fac2912018-07-10 08:40:17 +02001993 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001994 *
Mario Six8fac2912018-07-10 08:40:17 +02001995 * See the Unified Extensible Firmware Interface (UEFI) specification for
1996 * details.
1997 *
1998 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001999 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002000static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2001{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002002 u64 end_tick;
2003
Alexander Grafc15d9212016-03-04 01:09:59 +01002004 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002005
2006 end_tick = get_ticks() + usec_to_tick(microseconds);
2007 while (get_ticks() < end_tick)
2008 efi_timer_check();
2009
Alexander Grafc15d9212016-03-04 01:09:59 +01002010 return EFI_EXIT(EFI_SUCCESS);
2011}
2012
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002013/**
Mario Six8fac2912018-07-10 08:40:17 +02002014 * efi_set_watchdog_timer() - reset the watchdog timer
2015 * @timeout: seconds before reset by watchdog
2016 * @watchdog_code: code to be logged when resetting
2017 * @data_size: size of buffer in bytes
2018 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002019 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002020 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002021 *
Mario Six8fac2912018-07-10 08:40:17 +02002022 * See the Unified Extensible Firmware Interface (UEFI) specification for
2023 * details.
2024 *
2025 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002026 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002027static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2028 uint64_t watchdog_code,
2029 unsigned long data_size,
2030 uint16_t *watchdog_data)
2031{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002032 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002033 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002034 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002035}
2036
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002037/**
Mario Six8fac2912018-07-10 08:40:17 +02002038 * efi_close_protocol() - close a protocol
2039 * @handle: handle on which the protocol shall be closed
2040 * @protocol: GUID of the protocol to close
2041 * @agent_handle: handle of the driver
2042 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002043 *
2044 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002045 *
Mario Six8fac2912018-07-10 08:40:17 +02002046 * See the Unified Extensible Firmware Interface (UEFI) specification for
2047 * details.
2048 *
2049 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002050 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002051static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002052 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002053 efi_handle_t agent_handle,
2054 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002055{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002056 struct efi_handler *handler;
2057 struct efi_open_protocol_info_item *item;
2058 struct efi_open_protocol_info_item *pos;
2059 efi_status_t r;
2060
Rob Clark238f88c2017-09-13 18:05:41 -04002061 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002062 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002063
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002064 if (!efi_search_obj(agent_handle) ||
2065 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002066 r = EFI_INVALID_PARAMETER;
2067 goto out;
2068 }
2069 r = efi_search_protocol(handle, protocol, &handler);
2070 if (r != EFI_SUCCESS)
2071 goto out;
2072
2073 r = EFI_NOT_FOUND;
2074 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2075 if (item->info.agent_handle == agent_handle &&
2076 item->info.controller_handle == controller_handle) {
2077 efi_delete_open_info(item);
2078 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002079 }
2080 }
2081out:
2082 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002083}
2084
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002085/**
Mario Six8fac2912018-07-10 08:40:17 +02002086 * efi_open_protocol_information() - provide information about then open status
2087 * of a protocol on a handle
2088 * @handle: handle for which the information shall be retrieved
2089 * @protocol: GUID of the protocol
2090 * @entry_buffer: buffer to receive the open protocol information
2091 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002092 *
2093 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002094 *
2095 * See the Unified Extensible Firmware Interface (UEFI) specification for
2096 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002097 *
Mario Six8fac2912018-07-10 08:40:17 +02002098 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002099 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002100static efi_status_t EFIAPI efi_open_protocol_information(
2101 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002102 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002103 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002104{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002105 unsigned long buffer_size;
2106 unsigned long count;
2107 struct efi_handler *handler;
2108 struct efi_open_protocol_info_item *item;
2109 efi_status_t r;
2110
Rob Clark238f88c2017-09-13 18:05:41 -04002111 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002112 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002113
2114 /* Check parameters */
2115 if (!entry_buffer) {
2116 r = EFI_INVALID_PARAMETER;
2117 goto out;
2118 }
2119 r = efi_search_protocol(handle, protocol, &handler);
2120 if (r != EFI_SUCCESS)
2121 goto out;
2122
2123 /* Count entries */
2124 count = 0;
2125 list_for_each_entry(item, &handler->open_infos, link) {
2126 if (item->info.open_count)
2127 ++count;
2128 }
2129 *entry_count = count;
2130 *entry_buffer = NULL;
2131 if (!count) {
2132 r = EFI_SUCCESS;
2133 goto out;
2134 }
2135
2136 /* Copy entries */
2137 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002138 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002139 (void **)entry_buffer);
2140 if (r != EFI_SUCCESS)
2141 goto out;
2142 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2143 if (item->info.open_count)
2144 (*entry_buffer)[--count] = item->info;
2145 }
2146out:
2147 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002148}
2149
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002150/**
Mario Six8fac2912018-07-10 08:40:17 +02002151 * efi_protocols_per_handle() - get protocols installed on a handle
2152 * @handle: handle for which the information is retrieved
2153 * @protocol_buffer: buffer with protocol GUIDs
2154 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002155 *
2156 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002157 *
Mario Six8fac2912018-07-10 08:40:17 +02002158 * See the Unified Extensible Firmware Interface (UEFI) specification for
2159 * details.
2160 *
2161 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002162 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002163static efi_status_t EFIAPI efi_protocols_per_handle(
2164 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002165 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002166{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002167 unsigned long buffer_size;
2168 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002169 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002170 efi_status_t r;
2171
Alexander Grafc15d9212016-03-04 01:09:59 +01002172 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2173 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002174
2175 if (!handle || !protocol_buffer || !protocol_buffer_count)
2176 return EFI_EXIT(EFI_INVALID_PARAMETER);
2177
2178 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002179 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002180
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002181 efiobj = efi_search_obj(handle);
2182 if (!efiobj)
2183 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002184
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002185 /* Count protocols */
2186 list_for_each(protocol_handle, &efiobj->protocols) {
2187 ++*protocol_buffer_count;
2188 }
2189
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002190 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002191 if (*protocol_buffer_count) {
2192 size_t j = 0;
2193
2194 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002195 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002196 (void **)protocol_buffer);
2197 if (r != EFI_SUCCESS)
2198 return EFI_EXIT(r);
2199 list_for_each(protocol_handle, &efiobj->protocols) {
2200 struct efi_handler *protocol;
2201
2202 protocol = list_entry(protocol_handle,
2203 struct efi_handler, link);
2204 (*protocol_buffer)[j] = (void *)protocol->guid;
2205 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002206 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002207 }
2208
2209 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002210}
2211
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002212/**
Mario Six8fac2912018-07-10 08:40:17 +02002213 * efi_locate_handle_buffer() - locate handles implementing a protocol
2214 * @search_type: selection criterion
2215 * @protocol: GUID of the protocol
2216 * @search_key: registration key
2217 * @no_handles: number of returned handles
2218 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002219 *
2220 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002221 *
Mario Six8fac2912018-07-10 08:40:17 +02002222 * See the Unified Extensible Firmware Interface (UEFI) specification for
2223 * details.
2224 *
2225 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002226 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002227static efi_status_t EFIAPI efi_locate_handle_buffer(
2228 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002229 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002230 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002231{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002232 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002233 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002234
Rob Clark238f88c2017-09-13 18:05:41 -04002235 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002236 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002237
2238 if (!no_handles || !buffer) {
2239 r = EFI_INVALID_PARAMETER;
2240 goto out;
2241 }
2242 *no_handles = 0;
2243 *buffer = NULL;
2244 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2245 *buffer);
2246 if (r != EFI_BUFFER_TOO_SMALL)
2247 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002248 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002249 (void **)buffer);
2250 if (r != EFI_SUCCESS)
2251 goto out;
2252 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2253 *buffer);
2254 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002255 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002256out:
2257 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002258}
2259
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002260/**
Mario Six8fac2912018-07-10 08:40:17 +02002261 * efi_locate_protocol() - find an interface implementing a protocol
2262 * @protocol: GUID of the protocol
2263 * @registration: registration key passed to the notification function
2264 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002265 *
2266 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002267 *
2268 * See the Unified Extensible Firmware Interface (UEFI) specification for
2269 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002270 *
Mario Six8fac2912018-07-10 08:40:17 +02002271 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002272 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002273static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002274 void *registration,
2275 void **protocol_interface)
2276{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002277 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002278 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002279 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002280
Rob Clark238f88c2017-09-13 18:05:41 -04002281 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002282
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002283 /*
2284 * The UEFI spec explicitly requires a protocol even if a registration
2285 * key is provided. This differs from the logic in LocateHandle().
2286 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002287 if (!protocol || !protocol_interface)
2288 return EFI_EXIT(EFI_INVALID_PARAMETER);
2289
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002290 if (registration) {
2291 struct efi_register_notify_event *event;
2292 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002293
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002294 event = efi_check_register_notify_event(registration);
2295 if (!event)
2296 return EFI_EXIT(EFI_INVALID_PARAMETER);
2297 /*
2298 * The UEFI spec requires to return EFI_NOT_FOUND if no
2299 * protocol instance matches protocol and registration.
2300 * So let's do the same for a mismatch between protocol and
2301 * registration.
2302 */
2303 if (guidcmp(&event->protocol, protocol))
2304 goto not_found;
2305 if (list_empty(&event->handles))
2306 goto not_found;
2307 handle = list_first_entry(&event->handles,
2308 struct efi_protocol_notification,
2309 link);
2310 efiobj = handle->handle;
2311 list_del(&handle->link);
2312 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002313 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002314 if (ret == EFI_SUCCESS)
2315 goto found;
2316 } else {
2317 list_for_each_entry(efiobj, &efi_obj_list, link) {
2318 ret = efi_search_protocol(efiobj, protocol, &handler);
2319 if (ret == EFI_SUCCESS)
2320 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002321 }
2322 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002323not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002324 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002325 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002326found:
2327 *protocol_interface = handler->protocol_interface;
2328 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002329}
2330
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002331/**
Mario Six8fac2912018-07-10 08:40:17 +02002332 * efi_locate_device_path() - Get the device path and handle of an device
2333 * implementing a protocol
2334 * @protocol: GUID of the protocol
2335 * @device_path: device path
2336 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002337 *
2338 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002339 *
Mario Six8fac2912018-07-10 08:40:17 +02002340 * See the Unified Extensible Firmware Interface (UEFI) specification for
2341 * details.
2342 *
2343 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002344 */
2345static efi_status_t EFIAPI efi_locate_device_path(
2346 const efi_guid_t *protocol,
2347 struct efi_device_path **device_path,
2348 efi_handle_t *device)
2349{
2350 struct efi_device_path *dp;
2351 size_t i;
2352 struct efi_handler *handler;
2353 efi_handle_t *handles;
2354 size_t len, len_dp;
2355 size_t len_best = 0;
2356 efi_uintn_t no_handles;
2357 u8 *remainder;
2358 efi_status_t ret;
2359
2360 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2361
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002362 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002363 ret = EFI_INVALID_PARAMETER;
2364 goto out;
2365 }
2366
2367 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002368 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002369
2370 /* Get all handles implementing the protocol */
2371 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2372 &no_handles, &handles));
2373 if (ret != EFI_SUCCESS)
2374 goto out;
2375
2376 for (i = 0; i < no_handles; ++i) {
2377 /* Find the device path protocol */
2378 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2379 &handler);
2380 if (ret != EFI_SUCCESS)
2381 continue;
2382 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002383 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002384 /*
2385 * This handle can only be a better fit
2386 * if its device path length is longer than the best fit and
2387 * if its device path length is shorter of equal the searched
2388 * device path.
2389 */
2390 if (len_dp <= len_best || len_dp > len)
2391 continue;
2392 /* Check if dp is a subpath of device_path */
2393 if (memcmp(*device_path, dp, len_dp))
2394 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002395 if (!device) {
2396 ret = EFI_INVALID_PARAMETER;
2397 goto out;
2398 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002399 *device = handles[i];
2400 len_best = len_dp;
2401 }
2402 if (len_best) {
2403 remainder = (u8 *)*device_path + len_best;
2404 *device_path = (struct efi_device_path *)remainder;
2405 ret = EFI_SUCCESS;
2406 } else {
2407 ret = EFI_NOT_FOUND;
2408 }
2409out:
2410 return EFI_EXIT(ret);
2411}
2412
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002413/**
Mario Six8fac2912018-07-10 08:40:17 +02002414 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2415 * interfaces
2416 * @handle: handle on which the protocol interfaces shall be installed
2417 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2418 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002419 *
2420 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002421 *
2422 * See the Unified Extensible Firmware Interface (UEFI) specification for
2423 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002424 *
Mario Six8fac2912018-07-10 08:40:17 +02002425 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002426 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002427efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002428 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002429{
2430 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002431
Alexander Graf404a7c82018-06-18 17:23:05 +02002432 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002433 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002434 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002435 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002436 efi_status_t r = EFI_SUCCESS;
2437 int i = 0;
2438
2439 if (!handle)
2440 return EFI_EXIT(EFI_INVALID_PARAMETER);
2441
Alexander Graf404a7c82018-06-18 17:23:05 +02002442 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002443 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002444 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002445 if (!protocol)
2446 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002447 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002448 /* Check that a device path has not been installed before */
2449 if (!guidcmp(protocol, &efi_guid_device_path)) {
2450 struct efi_device_path *dp = protocol_interface;
2451
2452 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2453 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002454 if (r == EFI_SUCCESS &&
2455 dp->type == DEVICE_PATH_TYPE_END) {
2456 EFI_PRINT("Path %pD already installed\n",
2457 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002458 r = EFI_ALREADY_STARTED;
2459 break;
2460 }
2461 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002462 r = EFI_CALL(efi_install_protocol_interface(
2463 handle, protocol,
2464 EFI_NATIVE_INTERFACE,
2465 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002466 if (r != EFI_SUCCESS)
2467 break;
2468 i++;
2469 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002470 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002471 if (r == EFI_SUCCESS)
2472 return EFI_EXIT(r);
2473
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002474 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002475 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002476 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002477 protocol = efi_va_arg(argptr, efi_guid_t*);
2478 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002479 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002480 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002481 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002482 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002483
2484 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002485}
2486
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002487/**
Mario Six8fac2912018-07-10 08:40:17 +02002488 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2489 * interfaces
2490 * @handle: handle from which the protocol interfaces shall be removed
2491 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2492 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002493 *
2494 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002495 *
Mario Six8fac2912018-07-10 08:40:17 +02002496 * See the Unified Extensible Firmware Interface (UEFI) specification for
2497 * details.
2498 *
2499 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002500 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002501static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002502 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002503{
2504 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002505
Alexander Graf404a7c82018-06-18 17:23:05 +02002506 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002507 const efi_guid_t *protocol;
2508 void *protocol_interface;
2509 efi_status_t r = EFI_SUCCESS;
2510 size_t i = 0;
2511
2512 if (!handle)
2513 return EFI_EXIT(EFI_INVALID_PARAMETER);
2514
Alexander Graf404a7c82018-06-18 17:23:05 +02002515 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002516 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002517 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002518 if (!protocol)
2519 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002520 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002521 r = efi_uninstall_protocol(handle, protocol,
2522 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002523 if (r != EFI_SUCCESS)
2524 break;
2525 i++;
2526 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002527 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002528 if (r == EFI_SUCCESS) {
2529 /* If the last protocol has been removed, delete the handle. */
2530 if (list_empty(&handle->protocols)) {
2531 list_del(&handle->link);
2532 free(handle);
2533 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002534 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002535 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002536
2537 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002538 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002539 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002540 protocol = efi_va_arg(argptr, efi_guid_t*);
2541 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002542 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2543 EFI_NATIVE_INTERFACE,
2544 protocol_interface));
2545 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002546 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002547
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002548 /* In case of an error always return EFI_INVALID_PARAMETER */
2549 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002550}
2551
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002552/**
Mario Six8fac2912018-07-10 08:40:17 +02002553 * efi_calculate_crc32() - calculate cyclic redundancy code
2554 * @data: buffer with data
2555 * @data_size: size of buffer in bytes
2556 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002557 *
2558 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002559 *
2560 * See the Unified Extensible Firmware Interface (UEFI) specification for
2561 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002562 *
Mario Six8fac2912018-07-10 08:40:17 +02002563 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002564 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002565static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2566 efi_uintn_t data_size,
2567 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002568{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002569 efi_status_t ret = EFI_SUCCESS;
2570
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002571 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002572 if (!data || !data_size || !crc32_p) {
2573 ret = EFI_INVALID_PARAMETER;
2574 goto out;
2575 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002576 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002577out:
2578 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002579}
2580
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002581/**
Mario Six8fac2912018-07-10 08:40:17 +02002582 * efi_copy_mem() - copy memory
2583 * @destination: destination of the copy operation
2584 * @source: source of the copy operation
2585 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002586 *
2587 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002588 *
Mario Six8fac2912018-07-10 08:40:17 +02002589 * See the Unified Extensible Firmware Interface (UEFI) specification for
2590 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002591 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002592static void EFIAPI efi_copy_mem(void *destination, const void *source,
2593 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002594{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002595 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002596 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002597 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002598}
2599
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002600/**
Mario Six8fac2912018-07-10 08:40:17 +02002601 * efi_set_mem() - Fill memory with a byte value.
2602 * @buffer: buffer to fill
2603 * @size: size of buffer in bytes
2604 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002605 *
2606 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002607 *
Mario Six8fac2912018-07-10 08:40:17 +02002608 * See the Unified Extensible Firmware Interface (UEFI) specification for
2609 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002610 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002611static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002612{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002613 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002614 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002615 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002616}
2617
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002618/**
Mario Six8fac2912018-07-10 08:40:17 +02002619 * efi_protocol_open() - open protocol interface on a handle
2620 * @handler: handler of a protocol
2621 * @protocol_interface: interface implementing the protocol
2622 * @agent_handle: handle of the driver
2623 * @controller_handle: handle of the controller
2624 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002625 *
Mario Six8fac2912018-07-10 08:40:17 +02002626 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002627 */
2628static efi_status_t efi_protocol_open(
2629 struct efi_handler *handler,
2630 void **protocol_interface, void *agent_handle,
2631 void *controller_handle, uint32_t attributes)
2632{
2633 struct efi_open_protocol_info_item *item;
2634 struct efi_open_protocol_info_entry *match = NULL;
2635 bool opened_by_driver = false;
2636 bool opened_exclusive = false;
2637
2638 /* If there is no agent, only return the interface */
2639 if (!agent_handle)
2640 goto out;
2641
2642 /* For TEST_PROTOCOL ignore interface attribute */
2643 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2644 *protocol_interface = NULL;
2645
2646 /*
2647 * Check if the protocol is already opened by a driver with the same
2648 * attributes or opened exclusively
2649 */
2650 list_for_each_entry(item, &handler->open_infos, link) {
2651 if (item->info.agent_handle == agent_handle) {
2652 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2653 (item->info.attributes == attributes))
2654 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002655 } else {
2656 if (item->info.attributes &
2657 EFI_OPEN_PROTOCOL_BY_DRIVER)
2658 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002659 }
2660 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2661 opened_exclusive = true;
2662 }
2663
2664 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002665 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2666 if (opened_exclusive)
2667 return EFI_ACCESS_DENIED;
2668 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2669 if (opened_exclusive || opened_by_driver)
2670 return EFI_ACCESS_DENIED;
2671 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002672
2673 /* Prepare exclusive opening */
2674 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2675 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002676disconnect_next:
2677 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002678 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002679 efi_status_t ret;
2680
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002681 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002682 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2683 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002684 item->info.controller_handle,
2685 item->info.agent_handle,
2686 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002687 if (ret == EFI_SUCCESS)
2688 /*
2689 * Child controllers may have been
2690 * removed from the open_infos list. So
2691 * let's restart the loop.
2692 */
2693 goto disconnect_next;
2694 else
2695 opened_by_driver = true;
2696 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002697 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002698 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002699 if (opened_by_driver)
2700 return EFI_ACCESS_DENIED;
2701 }
2702
2703 /* Find existing entry */
2704 list_for_each_entry(item, &handler->open_infos, link) {
2705 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002706 item->info.controller_handle == controller_handle &&
2707 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002708 match = &item->info;
2709 }
2710 /* None found, create one */
2711 if (!match) {
2712 match = efi_create_open_info(handler);
2713 if (!match)
2714 return EFI_OUT_OF_RESOURCES;
2715 }
2716
2717 match->agent_handle = agent_handle;
2718 match->controller_handle = controller_handle;
2719 match->attributes = attributes;
2720 match->open_count++;
2721
2722out:
2723 /* For TEST_PROTOCOL ignore interface attribute. */
2724 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2725 *protocol_interface = handler->protocol_interface;
2726
2727 return EFI_SUCCESS;
2728}
2729
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002730/**
Mario Six8fac2912018-07-10 08:40:17 +02002731 * efi_open_protocol() - open protocol interface on a handle
2732 * @handle: handle on which the protocol shall be opened
2733 * @protocol: GUID of the protocol
2734 * @protocol_interface: interface implementing the protocol
2735 * @agent_handle: handle of the driver
2736 * @controller_handle: handle of the controller
2737 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002738 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002739 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002740 *
Mario Six8fac2912018-07-10 08:40:17 +02002741 * See the Unified Extensible Firmware Interface (UEFI) specification for
2742 * details.
2743 *
2744 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002745 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002746static efi_status_t EFIAPI efi_open_protocol
2747 (efi_handle_t handle, const efi_guid_t *protocol,
2748 void **protocol_interface, efi_handle_t agent_handle,
2749 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002750{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002751 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002752 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002753
Rob Clark238f88c2017-09-13 18:05:41 -04002754 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002755 protocol_interface, agent_handle, controller_handle,
2756 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002757
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002758 if (!handle || !protocol ||
2759 (!protocol_interface && attributes !=
2760 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002761 goto out;
2762 }
2763
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002764 switch (attributes) {
2765 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2766 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2767 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2768 break;
2769 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2770 if (controller_handle == handle)
2771 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002772 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002773 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2774 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002775 /* Check that the controller handle is valid */
2776 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002777 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002778 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002779 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002780 /* Check that the agent handle is valid */
2781 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002782 goto out;
2783 break;
2784 default:
2785 goto out;
2786 }
2787
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002788 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002789 switch (r) {
2790 case EFI_SUCCESS:
2791 break;
2792 case EFI_NOT_FOUND:
2793 r = EFI_UNSUPPORTED;
2794 goto out;
2795 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002796 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002797 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002798
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002799 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2800 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002801out:
2802 return EFI_EXIT(r);
2803}
2804
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002805/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002806 * efi_start_image() - call the entry point of an image
2807 * @image_handle: handle of the image
2808 * @exit_data_size: size of the buffer
2809 * @exit_data: buffer to receive the exit data of the called image
2810 *
2811 * This function implements the StartImage service.
2812 *
2813 * See the Unified Extensible Firmware Interface (UEFI) specification for
2814 * details.
2815 *
2816 * Return: status code
2817 */
2818efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2819 efi_uintn_t *exit_data_size,
2820 u16 **exit_data)
2821{
2822 struct efi_loaded_image_obj *image_obj =
2823 (struct efi_loaded_image_obj *)image_handle;
2824 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002825 void *info;
2826 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002827
2828 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2829
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002830 /* Check parameters */
2831 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2832 &info, NULL, NULL,
2833 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2834 if (ret != EFI_SUCCESS)
2835 return EFI_EXIT(EFI_INVALID_PARAMETER);
2836
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002837 efi_is_direct_boot = false;
2838
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002839 image_obj->exit_data_size = exit_data_size;
2840 image_obj->exit_data = exit_data;
2841
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002842 /* call the image! */
2843 if (setjmp(&image_obj->exit_jmp)) {
2844 /*
2845 * We called the entry point of the child image with EFI_CALL
2846 * in the lines below. The child image called the Exit() boot
2847 * service efi_exit() which executed the long jump that brought
2848 * us to the current line. This implies that the second half
2849 * of the EFI_CALL macro has not been executed.
2850 */
2851#ifdef CONFIG_ARM
2852 /*
2853 * efi_exit() called efi_restore_gd(). We have to undo this
2854 * otherwise __efi_entry_check() will put the wrong value into
2855 * app_gd.
2856 */
2857 gd = app_gd;
2858#endif
2859 /*
2860 * To get ready to call EFI_EXIT below we have to execute the
2861 * missed out steps of EFI_CALL.
2862 */
2863 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002864 EFI_PRINT("%lu returned by started image\n",
2865 (unsigned long)((uintptr_t)image_obj->exit_status &
2866 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002867 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002868 return EFI_EXIT(image_obj->exit_status);
2869 }
2870
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002871 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002872 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002873 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002874 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2875
2876 /*
2877 * Usually UEFI applications call Exit() instead of returning.
2878 * But because the world doesn't consist of ponies and unicorns,
2879 * we're happy to emulate that behavior on behalf of a payload
2880 * that forgot.
2881 */
2882 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2883}
2884
2885/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002886 * efi_delete_image() - delete loaded image from memory)
2887 *
2888 * @image_obj: handle of the loaded image
2889 * @loaded_image_protocol: loaded image protocol
2890 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002891static efi_status_t efi_delete_image
2892 (struct efi_loaded_image_obj *image_obj,
2893 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002894{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002895 struct efi_object *efiobj;
2896 efi_status_t r, ret = EFI_SUCCESS;
2897
2898close_next:
2899 list_for_each_entry(efiobj, &efi_obj_list, link) {
2900 struct efi_handler *protocol;
2901
2902 list_for_each_entry(protocol, &efiobj->protocols, link) {
2903 struct efi_open_protocol_info_item *info;
2904
2905 list_for_each_entry(info, &protocol->open_infos, link) {
2906 if (info->info.agent_handle !=
2907 (efi_handle_t)image_obj)
2908 continue;
2909 r = EFI_CALL(efi_close_protocol
2910 (efiobj, protocol->guid,
2911 info->info.agent_handle,
2912 info->info.controller_handle
2913 ));
2914 if (r != EFI_SUCCESS)
2915 ret = r;
2916 /*
2917 * Closing protocols may results in further
2918 * items being deleted. To play it safe loop
2919 * over all elements again.
2920 */
2921 goto close_next;
2922 }
2923 }
2924 }
2925
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002926 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2927 efi_size_in_pages(loaded_image_protocol->image_size));
2928 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002929
2930 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002931}
2932
2933/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002934 * efi_unload_image() - unload an EFI image
2935 * @image_handle: handle of the image to be unloaded
2936 *
2937 * This function implements the UnloadImage service.
2938 *
2939 * See the Unified Extensible Firmware Interface (UEFI) specification for
2940 * details.
2941 *
2942 * Return: status code
2943 */
2944efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2945{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002946 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002947 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002948 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002949
2950 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002951
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002952 efiobj = efi_search_obj(image_handle);
2953 if (!efiobj) {
2954 ret = EFI_INVALID_PARAMETER;
2955 goto out;
2956 }
2957 /* Find the loaded image protocol */
2958 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2959 (void **)&loaded_image_protocol,
2960 NULL, NULL,
2961 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2962 if (ret != EFI_SUCCESS) {
2963 ret = EFI_INVALID_PARAMETER;
2964 goto out;
2965 }
2966 switch (efiobj->type) {
2967 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2968 /* Call the unload function */
2969 if (!loaded_image_protocol->unload) {
2970 ret = EFI_UNSUPPORTED;
2971 goto out;
2972 }
2973 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2974 if (ret != EFI_SUCCESS)
2975 goto out;
2976 break;
2977 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2978 break;
2979 default:
2980 ret = EFI_INVALID_PARAMETER;
2981 goto out;
2982 }
2983 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2984 loaded_image_protocol);
2985out:
2986 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002987}
2988
2989/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002990 * efi_update_exit_data() - fill exit data parameters of StartImage()
2991 *
2992 * @image_obj image handle
2993 * @exit_data_size size of the exit data buffer
2994 * @exit_data buffer with data returned by UEFI payload
2995 * Return: status code
2996 */
2997static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2998 efi_uintn_t exit_data_size,
2999 u16 *exit_data)
3000{
3001 efi_status_t ret;
3002
3003 /*
3004 * If exit_data is not provided to StartImage(), exit_data_size must be
3005 * ignored.
3006 */
3007 if (!image_obj->exit_data)
3008 return EFI_SUCCESS;
3009 if (image_obj->exit_data_size)
3010 *image_obj->exit_data_size = exit_data_size;
3011 if (exit_data_size && exit_data) {
3012 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3013 exit_data_size,
3014 (void **)image_obj->exit_data);
3015 if (ret != EFI_SUCCESS)
3016 return ret;
3017 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3018 } else {
3019 image_obj->exit_data = NULL;
3020 }
3021 return EFI_SUCCESS;
3022}
3023
3024/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003025 * efi_exit() - leave an EFI application or driver
3026 * @image_handle: handle of the application or driver that is exiting
3027 * @exit_status: status code
3028 * @exit_data_size: size of the buffer in bytes
3029 * @exit_data: buffer with data describing an error
3030 *
3031 * This function implements the Exit service.
3032 *
3033 * See the Unified Extensible Firmware Interface (UEFI) specification for
3034 * details.
3035 *
3036 * Return: status code
3037 */
3038static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3039 efi_status_t exit_status,
3040 efi_uintn_t exit_data_size,
3041 u16 *exit_data)
3042{
3043 /*
3044 * TODO: We should call the unload procedure of the loaded
3045 * image protocol.
3046 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003047 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003048 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003049 struct efi_loaded_image_obj *image_obj =
3050 (struct efi_loaded_image_obj *)image_handle;
3051
3052 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3053 exit_data_size, exit_data);
3054
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003055 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003056 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003057 (void **)&loaded_image_protocol,
3058 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003059 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003060 if (ret != EFI_SUCCESS) {
3061 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003062 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003063 }
3064
3065 /* Unloading of unstarted images */
3066 switch (image_obj->header.type) {
3067 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3068 break;
3069 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3070 efi_delete_image(image_obj, loaded_image_protocol);
3071 ret = EFI_SUCCESS;
3072 goto out;
3073 default:
3074 /* Handle does not refer to loaded image */
3075 ret = EFI_INVALID_PARAMETER;
3076 goto out;
3077 }
3078 /* A started image can only be unloaded it is the last one started. */
3079 if (image_handle != current_image) {
3080 ret = EFI_INVALID_PARAMETER;
3081 goto out;
3082 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003083
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003084 /* Exit data is only foreseen in case of failure. */
3085 if (exit_status != EFI_SUCCESS) {
3086 ret = efi_update_exit_data(image_obj, exit_data_size,
3087 exit_data);
3088 /* Exiting has priority. Don't return error to caller. */
3089 if (ret != EFI_SUCCESS)
3090 EFI_PRINT("%s: out of memory\n", __func__);
3091 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003092 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3093 exit_status != EFI_SUCCESS)
3094 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003095
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003096 /* Make sure entry/exit counts for EFI world cross-overs match */
3097 EFI_EXIT(exit_status);
3098
3099 /*
3100 * But longjmp out with the U-Boot gd, not the application's, as
3101 * the other end is a setjmp call inside EFI context.
3102 */
3103 efi_restore_gd();
3104
3105 image_obj->exit_status = exit_status;
3106 longjmp(&image_obj->exit_jmp, 1);
3107
3108 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003109out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003110 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003111}
3112
3113/**
Mario Six8fac2912018-07-10 08:40:17 +02003114 * efi_handle_protocol() - get interface of a protocol on a handle
3115 * @handle: handle on which the protocol shall be opened
3116 * @protocol: GUID of the protocol
3117 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003118 *
3119 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003120 *
3121 * See the Unified Extensible Firmware Interface (UEFI) specification for
3122 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003123 *
Mario Six8fac2912018-07-10 08:40:17 +02003124 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003125 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01003126static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02003127 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003128 void **protocol_interface)
3129{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003130 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003131 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003132}
3133
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003134/**
Mario Six8fac2912018-07-10 08:40:17 +02003135 * efi_bind_controller() - bind a single driver to a controller
3136 * @controller_handle: controller handle
3137 * @driver_image_handle: driver handle
3138 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003139 *
Mario Six8fac2912018-07-10 08:40:17 +02003140 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003141 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003142static efi_status_t efi_bind_controller(
3143 efi_handle_t controller_handle,
3144 efi_handle_t driver_image_handle,
3145 struct efi_device_path *remain_device_path)
3146{
3147 struct efi_driver_binding_protocol *binding_protocol;
3148 efi_status_t r;
3149
3150 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3151 &efi_guid_driver_binding_protocol,
3152 (void **)&binding_protocol,
3153 driver_image_handle, NULL,
3154 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3155 if (r != EFI_SUCCESS)
3156 return r;
3157 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3158 controller_handle,
3159 remain_device_path));
3160 if (r == EFI_SUCCESS)
3161 r = EFI_CALL(binding_protocol->start(binding_protocol,
3162 controller_handle,
3163 remain_device_path));
3164 EFI_CALL(efi_close_protocol(driver_image_handle,
3165 &efi_guid_driver_binding_protocol,
3166 driver_image_handle, NULL));
3167 return r;
3168}
3169
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003170/**
Mario Six8fac2912018-07-10 08:40:17 +02003171 * efi_connect_single_controller() - connect a single driver to a controller
3172 * @controller_handle: controller
3173 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003174 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003175 *
Mario Six8fac2912018-07-10 08:40:17 +02003176 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003177 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003178static efi_status_t efi_connect_single_controller(
3179 efi_handle_t controller_handle,
3180 efi_handle_t *driver_image_handle,
3181 struct efi_device_path *remain_device_path)
3182{
3183 efi_handle_t *buffer;
3184 size_t count;
3185 size_t i;
3186 efi_status_t r;
3187 size_t connected = 0;
3188
3189 /* Get buffer with all handles with driver binding protocol */
3190 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3191 &efi_guid_driver_binding_protocol,
3192 NULL, &count, &buffer));
3193 if (r != EFI_SUCCESS)
3194 return r;
3195
3196 /* Context Override */
3197 if (driver_image_handle) {
3198 for (; *driver_image_handle; ++driver_image_handle) {
3199 for (i = 0; i < count; ++i) {
3200 if (buffer[i] == *driver_image_handle) {
3201 buffer[i] = NULL;
3202 r = efi_bind_controller(
3203 controller_handle,
3204 *driver_image_handle,
3205 remain_device_path);
3206 /*
3207 * For drivers that do not support the
3208 * controller or are already connected
3209 * we receive an error code here.
3210 */
3211 if (r == EFI_SUCCESS)
3212 ++connected;
3213 }
3214 }
3215 }
3216 }
3217
3218 /*
3219 * TODO: Some overrides are not yet implemented:
3220 * - Platform Driver Override
3221 * - Driver Family Override Search
3222 * - Bus Specific Driver Override
3223 */
3224
3225 /* Driver Binding Search */
3226 for (i = 0; i < count; ++i) {
3227 if (buffer[i]) {
3228 r = efi_bind_controller(controller_handle,
3229 buffer[i],
3230 remain_device_path);
3231 if (r == EFI_SUCCESS)
3232 ++connected;
3233 }
3234 }
3235
3236 efi_free_pool(buffer);
3237 if (!connected)
3238 return EFI_NOT_FOUND;
3239 return EFI_SUCCESS;
3240}
3241
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003242/**
Mario Six8fac2912018-07-10 08:40:17 +02003243 * efi_connect_controller() - connect a controller to a driver
3244 * @controller_handle: handle of the controller
3245 * @driver_image_handle: handle of the driver
3246 * @remain_device_path: device path of a child controller
3247 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003248 *
3249 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003250 *
3251 * See the Unified Extensible Firmware Interface (UEFI) specification for
3252 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003253 *
3254 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003255 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003256 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3257 *
Mario Six8fac2912018-07-10 08:40:17 +02003258 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003259 */
3260static efi_status_t EFIAPI efi_connect_controller(
3261 efi_handle_t controller_handle,
3262 efi_handle_t *driver_image_handle,
3263 struct efi_device_path *remain_device_path,
3264 bool recursive)
3265{
3266 efi_status_t r;
3267 efi_status_t ret = EFI_NOT_FOUND;
3268 struct efi_object *efiobj;
3269
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003270 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003271 remain_device_path, recursive);
3272
3273 efiobj = efi_search_obj(controller_handle);
3274 if (!efiobj) {
3275 ret = EFI_INVALID_PARAMETER;
3276 goto out;
3277 }
3278
3279 r = efi_connect_single_controller(controller_handle,
3280 driver_image_handle,
3281 remain_device_path);
3282 if (r == EFI_SUCCESS)
3283 ret = EFI_SUCCESS;
3284 if (recursive) {
3285 struct efi_handler *handler;
3286 struct efi_open_protocol_info_item *item;
3287
3288 list_for_each_entry(handler, &efiobj->protocols, link) {
3289 list_for_each_entry(item, &handler->open_infos, link) {
3290 if (item->info.attributes &
3291 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3292 r = EFI_CALL(efi_connect_controller(
3293 item->info.controller_handle,
3294 driver_image_handle,
3295 remain_device_path,
3296 recursive));
3297 if (r == EFI_SUCCESS)
3298 ret = EFI_SUCCESS;
3299 }
3300 }
3301 }
3302 }
3303 /* Check for child controller specified by end node */
3304 if (ret != EFI_SUCCESS && remain_device_path &&
3305 remain_device_path->type == DEVICE_PATH_TYPE_END)
3306 ret = EFI_SUCCESS;
3307out:
3308 return EFI_EXIT(ret);
3309}
3310
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003311/**
Mario Six8fac2912018-07-10 08:40:17 +02003312 * efi_reinstall_protocol_interface() - reinstall protocol interface
3313 * @handle: handle on which the protocol shall be reinstalled
3314 * @protocol: GUID of the protocol to be installed
3315 * @old_interface: interface to be removed
3316 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003317 *
3318 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003319 *
3320 * See the Unified Extensible Firmware Interface (UEFI) specification for
3321 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003322 *
3323 * The old interface is uninstalled. The new interface is installed.
3324 * Drivers are connected.
3325 *
Mario Six8fac2912018-07-10 08:40:17 +02003326 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003327 */
3328static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3329 efi_handle_t handle, const efi_guid_t *protocol,
3330 void *old_interface, void *new_interface)
3331{
3332 efi_status_t ret;
3333
3334 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3335 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003336
3337 /* Uninstall protocol but do not delete handle */
3338 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003339 if (ret != EFI_SUCCESS)
3340 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003341
3342 /* Install the new protocol */
3343 ret = efi_add_protocol(handle, protocol, new_interface);
3344 /*
3345 * The UEFI spec does not specify what should happen to the handle
3346 * if in case of an error no protocol interface remains on the handle.
3347 * So let's do nothing here.
3348 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003349 if (ret != EFI_SUCCESS)
3350 goto out;
3351 /*
3352 * The returned status code has to be ignored.
3353 * Do not create an error if no suitable driver for the handle exists.
3354 */
3355 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3356out:
3357 return EFI_EXIT(ret);
3358}
3359
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003360/**
Mario Six8fac2912018-07-10 08:40:17 +02003361 * efi_get_child_controllers() - get all child controllers associated to a driver
3362 * @efiobj: handle of the controller
3363 * @driver_handle: handle of the driver
3364 * @number_of_children: number of child controllers
3365 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003366 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003367 * The allocated buffer has to be freed with free().
3368 *
Mario Six8fac2912018-07-10 08:40:17 +02003369 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003370 */
3371static efi_status_t efi_get_child_controllers(
3372 struct efi_object *efiobj,
3373 efi_handle_t driver_handle,
3374 efi_uintn_t *number_of_children,
3375 efi_handle_t **child_handle_buffer)
3376{
3377 struct efi_handler *handler;
3378 struct efi_open_protocol_info_item *item;
3379 efi_uintn_t count = 0, i;
3380 bool duplicate;
3381
3382 /* Count all child controller associations */
3383 list_for_each_entry(handler, &efiobj->protocols, link) {
3384 list_for_each_entry(item, &handler->open_infos, link) {
3385 if (item->info.agent_handle == driver_handle &&
3386 item->info.attributes &
3387 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3388 ++count;
3389 }
3390 }
3391 /*
3392 * Create buffer. In case of duplicate child controller assignments
3393 * the buffer will be too large. But that does not harm.
3394 */
3395 *number_of_children = 0;
3396 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3397 if (!*child_handle_buffer)
3398 return EFI_OUT_OF_RESOURCES;
3399 /* Copy unique child handles */
3400 list_for_each_entry(handler, &efiobj->protocols, link) {
3401 list_for_each_entry(item, &handler->open_infos, link) {
3402 if (item->info.agent_handle == driver_handle &&
3403 item->info.attributes &
3404 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3405 /* Check this is a new child controller */
3406 duplicate = false;
3407 for (i = 0; i < *number_of_children; ++i) {
3408 if ((*child_handle_buffer)[i] ==
3409 item->info.controller_handle)
3410 duplicate = true;
3411 }
3412 /* Copy handle to buffer */
3413 if (!duplicate) {
3414 i = (*number_of_children)++;
3415 (*child_handle_buffer)[i] =
3416 item->info.controller_handle;
3417 }
3418 }
3419 }
3420 }
3421 return EFI_SUCCESS;
3422}
3423
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003424/**
Mario Six8fac2912018-07-10 08:40:17 +02003425 * efi_disconnect_controller() - disconnect a controller from a driver
3426 * @controller_handle: handle of the controller
3427 * @driver_image_handle: handle of the driver
3428 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003429 *
3430 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003431 *
3432 * See the Unified Extensible Firmware Interface (UEFI) specification for
3433 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003434 *
Mario Six8fac2912018-07-10 08:40:17 +02003435 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003436 */
3437static efi_status_t EFIAPI efi_disconnect_controller(
3438 efi_handle_t controller_handle,
3439 efi_handle_t driver_image_handle,
3440 efi_handle_t child_handle)
3441{
3442 struct efi_driver_binding_protocol *binding_protocol;
3443 efi_handle_t *child_handle_buffer = NULL;
3444 size_t number_of_children = 0;
3445 efi_status_t r;
3446 size_t stop_count = 0;
3447 struct efi_object *efiobj;
3448
3449 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3450 child_handle);
3451
3452 efiobj = efi_search_obj(controller_handle);
3453 if (!efiobj) {
3454 r = EFI_INVALID_PARAMETER;
3455 goto out;
3456 }
3457
3458 if (child_handle && !efi_search_obj(child_handle)) {
3459 r = EFI_INVALID_PARAMETER;
3460 goto out;
3461 }
3462
3463 /* If no driver handle is supplied, disconnect all drivers */
3464 if (!driver_image_handle) {
3465 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3466 goto out;
3467 }
3468
3469 /* Create list of child handles */
3470 if (child_handle) {
3471 number_of_children = 1;
3472 child_handle_buffer = &child_handle;
3473 } else {
3474 efi_get_child_controllers(efiobj,
3475 driver_image_handle,
3476 &number_of_children,
3477 &child_handle_buffer);
3478 }
3479
3480 /* Get the driver binding protocol */
3481 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3482 &efi_guid_driver_binding_protocol,
3483 (void **)&binding_protocol,
3484 driver_image_handle, NULL,
3485 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3486 if (r != EFI_SUCCESS)
3487 goto out;
3488 /* Remove the children */
3489 if (number_of_children) {
3490 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3491 controller_handle,
3492 number_of_children,
3493 child_handle_buffer));
3494 if (r == EFI_SUCCESS)
3495 ++stop_count;
3496 }
3497 /* Remove the driver */
3498 if (!child_handle)
3499 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3500 controller_handle,
3501 0, NULL));
3502 if (r == EFI_SUCCESS)
3503 ++stop_count;
3504 EFI_CALL(efi_close_protocol(driver_image_handle,
3505 &efi_guid_driver_binding_protocol,
3506 driver_image_handle, NULL));
3507
3508 if (stop_count)
3509 r = EFI_SUCCESS;
3510 else
3511 r = EFI_NOT_FOUND;
3512out:
3513 if (!child_handle)
3514 free(child_handle_buffer);
3515 return EFI_EXIT(r);
3516}
3517
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003518static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003519 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003520 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3521 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003522 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003523 },
3524 .raise_tpl = efi_raise_tpl,
3525 .restore_tpl = efi_restore_tpl,
3526 .allocate_pages = efi_allocate_pages_ext,
3527 .free_pages = efi_free_pages_ext,
3528 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003529 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003530 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003531 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003532 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003533 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003534 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003535 .close_event = efi_close_event,
3536 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003537 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003538 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003539 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003540 .handle_protocol = efi_handle_protocol,
3541 .reserved = NULL,
3542 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003543 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003544 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003545 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003546 .load_image = efi_load_image,
3547 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003548 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003549 .unload_image = efi_unload_image,
3550 .exit_boot_services = efi_exit_boot_services,
3551 .get_next_monotonic_count = efi_get_next_monotonic_count,
3552 .stall = efi_stall,
3553 .set_watchdog_timer = efi_set_watchdog_timer,
3554 .connect_controller = efi_connect_controller,
3555 .disconnect_controller = efi_disconnect_controller,
3556 .open_protocol = efi_open_protocol,
3557 .close_protocol = efi_close_protocol,
3558 .open_protocol_information = efi_open_protocol_information,
3559 .protocols_per_handle = efi_protocols_per_handle,
3560 .locate_handle_buffer = efi_locate_handle_buffer,
3561 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003562 .install_multiple_protocol_interfaces =
3563 efi_install_multiple_protocol_interfaces,
3564 .uninstall_multiple_protocol_interfaces =
3565 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003566 .calculate_crc32 = efi_calculate_crc32,
3567 .copy_mem = efi_copy_mem,
3568 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003569 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003570};
3571
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003572static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003573
Alexander Graf393dd912016-10-14 13:45:30 +02003574struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003575 .hdr = {
3576 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003577 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003578 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003579 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003580 .fw_vendor = firmware_vendor,
3581 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003582 .con_in = (void *)&efi_con_in,
3583 .con_out = (void *)&efi_con_out,
3584 .std_err = (void *)&efi_con_out,
3585 .runtime = (void *)&efi_runtime_services,
3586 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003587 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003588 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003589};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003590
3591/**
3592 * efi_initialize_system_table() - Initialize system table
3593 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003594 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003595 */
3596efi_status_t efi_initialize_system_table(void)
3597{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003598 efi_status_t ret;
3599
3600 /* Allocate configuration table array */
3601 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3602 EFI_MAX_CONFIGURATION_TABLES *
3603 sizeof(struct efi_configuration_table),
3604 (void **)&systab.tables);
3605
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003606 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003607 efi_update_table_header_crc32(&systab.hdr);
3608 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3609 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003610
3611 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003612}