blob: e5c46e9f0819b58e4dc80d422f8f44423daa550f [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>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020022
Alexander Grafc15d9212016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010028
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010029/* Handle of the currently executing image */
30static efi_handle_t current_image;
31
Alexander Graffa5246b2018-11-15 21:23:47 +010032/*
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
37 */
38static bool efi_is_direct_boot = true;
39
Simon Glasscdfe6962016-09-25 15:27:35 -060040#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010041/*
42 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
43 * fixed when compiling U-Boot. However, the payload does not know about that
44 * restriction so we need to manually swap its and our view of that register on
45 * EFI callback entry/exit.
46 */
47static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060048#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010049
Heinrich Schuchardt72506232019-02-09 14:10:39 +010050/* 1 if inside U-Boot code, 0 if inside EFI payload code */
51static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040052static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010053/* GUID of the device tree table */
54const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010055/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
56const efi_guid_t efi_guid_driver_binding_protocol =
57 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040058
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010059/* event group ExitBootServices() invoked */
60const efi_guid_t efi_guid_event_group_exit_boot_services =
61 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
62/* event group SetVirtualAddressMap() invoked */
63const efi_guid_t efi_guid_event_group_virtual_address_change =
64 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
65/* event group memory map changed */
66const efi_guid_t efi_guid_event_group_memory_map_change =
67 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
68/* event group boot manager about to boot */
69const efi_guid_t efi_guid_event_group_ready_to_boot =
70 EFI_EVENT_GROUP_READY_TO_BOOT;
71/* event group ResetSystem() invoked (before ExitBootServices) */
72const efi_guid_t efi_guid_event_group_reset_system =
73 EFI_EVENT_GROUP_RESET_SYSTEM;
74
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010075static efi_status_t EFIAPI efi_disconnect_controller(
76 efi_handle_t controller_handle,
77 efi_handle_t driver_image_handle,
78 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010079
Rob Clark86789d52017-07-27 08:04:18 -040080/* Called on every callback entry */
81int __efi_entry_check(void)
82{
83 int ret = entry_count++ == 0;
84#ifdef CONFIG_ARM
85 assert(efi_gd);
86 app_gd = gd;
87 gd = efi_gd;
88#endif
89 return ret;
90}
91
92/* Called on every callback exit */
93int __efi_exit_check(void)
94{
95 int ret = --entry_count == 0;
96#ifdef CONFIG_ARM
97 gd = app_gd;
98#endif
99 return ret;
100}
101
Alexander Grafc15d9212016-03-04 01:09:59 +0100102/* Called from do_bootefi_exec() */
103void efi_save_gd(void)
104{
Simon Glasscdfe6962016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100106 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600107#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100108}
109
Rob Clark86789d52017-07-27 08:04:18 -0400110/*
Mario Six8fac2912018-07-10 08:40:17 +0200111 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200112 * world so we can dump out an abort message, without any care about returning
113 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400114 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100115void efi_restore_gd(void)
116{
Simon Glasscdfe6962016-09-25 15:27:35 -0600117#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100118 /* Only restore if we're already in EFI context */
119 if (!efi_gd)
120 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100121 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600122#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100123}
124
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200125/**
Mario Six8fac2912018-07-10 08:40:17 +0200126 * indent_string() - returns a string for indenting with two spaces per level
127 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100128 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200129 * A maximum of ten indent levels is supported. Higher indent levels will be
130 * truncated.
131 *
Mario Six8fac2912018-07-10 08:40:17 +0200132 * Return: A string for indenting with two spaces per level is
133 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400134 */
135static const char *indent_string(int level)
136{
137 const char *indent = " ";
138 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100139
Rob Clarke7896c32017-07-27 08:04:19 -0400140 level = min(max, level * 2);
141 return &indent[max - level];
142}
143
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200144const char *__efi_nesting(void)
145{
146 return indent_string(nesting_level);
147}
148
Rob Clarke7896c32017-07-27 08:04:19 -0400149const char *__efi_nesting_inc(void)
150{
151 return indent_string(nesting_level++);
152}
153
154const char *__efi_nesting_dec(void)
155{
156 return indent_string(--nesting_level);
157}
158
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200159/**
Mario Six8fac2912018-07-10 08:40:17 +0200160 * efi_queue_event() - queue an EFI event
161 * @event: event to signal
162 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200163 *
164 * This function queues the notification function of the event for future
165 * execution.
166 *
Mario Six8fac2912018-07-10 08:40:17 +0200167 * The notification function is called if the task priority level of the event
168 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200169 *
170 * For the SignalEvent service see efi_signal_event_ext.
171 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200172 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100173static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200174{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200175 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200176 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200177 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100178 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200179 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200180 EFI_CALL_VOID(event->notify_function(event,
181 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200182 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200183 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200184}
185
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200186/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200187 * is_valid_tpl() - check if the task priority level is valid
188 *
189 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200190 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200191 */
192efi_status_t is_valid_tpl(efi_uintn_t tpl)
193{
194 switch (tpl) {
195 case TPL_APPLICATION:
196 case TPL_CALLBACK:
197 case TPL_NOTIFY:
198 case TPL_HIGH_LEVEL:
199 return EFI_SUCCESS;
200 default:
201 return EFI_INVALID_PARAMETER;
202 }
203}
204
205/**
Mario Six8fac2912018-07-10 08:40:17 +0200206 * efi_signal_event() - signal an EFI event
207 * @event: event to signal
208 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100209 *
Mario Six8fac2912018-07-10 08:40:17 +0200210 * This function signals an event. If the event belongs to an event group all
211 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100212 * their notification function is queued.
213 *
214 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100215 */
216void efi_signal_event(struct efi_event *event, bool check_tpl)
217{
218 if (event->group) {
219 struct efi_event *evt;
220
221 /*
222 * The signaled state has to set before executing any
223 * notification function
224 */
225 list_for_each_entry(evt, &efi_events, link) {
226 if (!evt->group || guidcmp(evt->group, event->group))
227 continue;
228 if (evt->is_signaled)
229 continue;
230 evt->is_signaled = true;
231 if (evt->type & EVT_NOTIFY_SIGNAL &&
232 evt->notify_function)
233 evt->is_queued = true;
234 }
235 list_for_each_entry(evt, &efi_events, link) {
236 if (!evt->group || guidcmp(evt->group, event->group))
237 continue;
238 if (evt->is_queued)
239 efi_queue_event(evt, check_tpl);
240 }
241 } else if (!event->is_signaled) {
242 event->is_signaled = true;
243 if (event->type & EVT_NOTIFY_SIGNAL)
244 efi_queue_event(event, check_tpl);
245 }
246}
247
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200248/**
Mario Six8fac2912018-07-10 08:40:17 +0200249 * efi_raise_tpl() - raise the task priority level
250 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200251 *
252 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200253 *
254 * See the Unified Extensible Firmware Interface (UEFI) specification for
255 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200256 *
Mario Six8fac2912018-07-10 08:40:17 +0200257 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200258 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100259static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100260{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100261 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200262
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200263 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200264
265 if (new_tpl < efi_tpl)
266 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
267 efi_tpl = new_tpl;
268 if (efi_tpl > TPL_HIGH_LEVEL)
269 efi_tpl = TPL_HIGH_LEVEL;
270
271 EFI_EXIT(EFI_SUCCESS);
272 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100273}
274
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200275/**
Mario Six8fac2912018-07-10 08:40:17 +0200276 * efi_restore_tpl() - lower the task priority level
277 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200278 *
279 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200280 *
Mario Six8fac2912018-07-10 08:40:17 +0200281 * See the Unified Extensible Firmware Interface (UEFI) specification for
282 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200283 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100284static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100285{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200286 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200287
288 if (old_tpl > efi_tpl)
289 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
290 efi_tpl = old_tpl;
291 if (efi_tpl > TPL_HIGH_LEVEL)
292 efi_tpl = TPL_HIGH_LEVEL;
293
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100294 /*
295 * Lowering the TPL may have made queued events eligible for execution.
296 */
297 efi_timer_check();
298
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200299 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100300}
301
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200302/**
Mario Six8fac2912018-07-10 08:40:17 +0200303 * efi_allocate_pages_ext() - allocate memory pages
304 * @type: type of allocation to be performed
305 * @memory_type: usage type of the allocated memory
306 * @pages: number of pages to be allocated
307 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200308 *
309 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200310 *
Mario Six8fac2912018-07-10 08:40:17 +0200311 * See the Unified Extensible Firmware Interface (UEFI) specification for
312 * details.
313 *
314 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200315 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900316static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100317 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900318 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100319{
320 efi_status_t r;
321
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100322 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100323 r = efi_allocate_pages(type, memory_type, pages, memory);
324 return EFI_EXIT(r);
325}
326
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200327/**
Mario Six8fac2912018-07-10 08:40:17 +0200328 * efi_free_pages_ext() - Free memory pages.
329 * @memory: start of the memory area to be freed
330 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200331 *
332 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200333 *
334 * See the Unified Extensible Firmware Interface (UEFI) specification for
335 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200336 *
Mario Six8fac2912018-07-10 08:40:17 +0200337 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200338 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900339static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100340 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100341{
342 efi_status_t r;
343
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900344 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100345 r = efi_free_pages(memory, pages);
346 return EFI_EXIT(r);
347}
348
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200349/**
Mario Six8fac2912018-07-10 08:40:17 +0200350 * efi_get_memory_map_ext() - get map describing memory usage
351 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
352 * on exit the size of the copied memory map
353 * @memory_map: buffer to which the memory map is written
354 * @map_key: key for the memory map
355 * @descriptor_size: size of an individual memory descriptor
356 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200357 *
358 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200359 *
Mario Six8fac2912018-07-10 08:40:17 +0200360 * See the Unified Extensible Firmware Interface (UEFI) specification for
361 * details.
362 *
363 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200364 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900365static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100366 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900367 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100368 efi_uintn_t *map_key,
369 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900370 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100371{
372 efi_status_t r;
373
374 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
375 map_key, descriptor_size, descriptor_version);
376 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
377 descriptor_size, descriptor_version);
378 return EFI_EXIT(r);
379}
380
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200381/**
Mario Six8fac2912018-07-10 08:40:17 +0200382 * efi_allocate_pool_ext() - allocate memory from pool
383 * @pool_type: type of the pool from which memory is to be allocated
384 * @size: number of bytes to be allocated
385 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200386 *
387 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200388 *
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
390 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 *
Mario Six8fac2912018-07-10 08:40:17 +0200392 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200393 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200394static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100395 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200396 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100397{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100398 efi_status_t r;
399
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100400 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200401 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100402 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100403}
404
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200405/**
Mario Six8fac2912018-07-10 08:40:17 +0200406 * efi_free_pool_ext() - free memory from pool
407 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200408 *
409 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200410 *
Mario Six8fac2912018-07-10 08:40:17 +0200411 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * details.
413 *
414 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200415 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200416static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100417{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100418 efi_status_t r;
419
420 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200421 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100422 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100423}
424
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200425/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200426 * efi_add_handle() - add a new handle to the object list
427 *
428 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100429 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200430 * The protocols list is initialized. The handle is added to the list of known
431 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100432 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200433void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100434{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200435 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100436 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200437 INIT_LIST_HEAD(&handle->protocols);
438 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100439}
440
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200441/**
Mario Six8fac2912018-07-10 08:40:17 +0200442 * efi_create_handle() - create handle
443 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200444 *
Mario Six8fac2912018-07-10 08:40:17 +0200445 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200446 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100447efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200448{
449 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200450
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200451 obj = calloc(1, sizeof(struct efi_object));
452 if (!obj)
453 return EFI_OUT_OF_RESOURCES;
454
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100455 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200456 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200457
458 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200459}
460
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200461/**
Mario Six8fac2912018-07-10 08:40:17 +0200462 * efi_search_protocol() - find a protocol on a handle.
463 * @handle: handle
464 * @protocol_guid: GUID of the protocol
465 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100466 *
Mario Six8fac2912018-07-10 08:40:17 +0200467 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100468 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100469efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100470 const efi_guid_t *protocol_guid,
471 struct efi_handler **handler)
472{
473 struct efi_object *efiobj;
474 struct list_head *lhandle;
475
476 if (!handle || !protocol_guid)
477 return EFI_INVALID_PARAMETER;
478 efiobj = efi_search_obj(handle);
479 if (!efiobj)
480 return EFI_INVALID_PARAMETER;
481 list_for_each(lhandle, &efiobj->protocols) {
482 struct efi_handler *protocol;
483
484 protocol = list_entry(lhandle, struct efi_handler, link);
485 if (!guidcmp(protocol->guid, protocol_guid)) {
486 if (handler)
487 *handler = protocol;
488 return EFI_SUCCESS;
489 }
490 }
491 return EFI_NOT_FOUND;
492}
493
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200494/**
Mario Six8fac2912018-07-10 08:40:17 +0200495 * efi_remove_protocol() - delete protocol from a handle
496 * @handle: handle from which the protocol shall be deleted
497 * @protocol: GUID of the protocol to be deleted
498 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100499 *
Mario Six8fac2912018-07-10 08:40:17 +0200500 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100501 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100502efi_status_t efi_remove_protocol(const efi_handle_t handle,
503 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100504 void *protocol_interface)
505{
506 struct efi_handler *handler;
507 efi_status_t ret;
508
509 ret = efi_search_protocol(handle, protocol, &handler);
510 if (ret != EFI_SUCCESS)
511 return ret;
512 if (guidcmp(handler->guid, protocol))
513 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200514 if (handler->protocol_interface != protocol_interface)
515 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100516 list_del(&handler->link);
517 free(handler);
518 return EFI_SUCCESS;
519}
520
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200521/**
Mario Six8fac2912018-07-10 08:40:17 +0200522 * efi_remove_all_protocols() - delete all protocols from a handle
523 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 *
Mario Six8fac2912018-07-10 08:40:17 +0200525 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100527efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100528{
529 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100530 struct efi_handler *protocol;
531 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100532
533 efiobj = efi_search_obj(handle);
534 if (!efiobj)
535 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100536 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100537 efi_status_t ret;
538
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100539 ret = efi_remove_protocol(handle, protocol->guid,
540 protocol->protocol_interface);
541 if (ret != EFI_SUCCESS)
542 return ret;
543 }
544 return EFI_SUCCESS;
545}
546
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200547/**
Mario Six8fac2912018-07-10 08:40:17 +0200548 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100549 *
Mario Six8fac2912018-07-10 08:40:17 +0200550 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100551 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200552void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100553{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200554 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100555 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200556 efi_remove_all_protocols(handle);
557 list_del(&handle->link);
558 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100559}
560
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200561/**
Mario Six8fac2912018-07-10 08:40:17 +0200562 * efi_is_event() - check if a pointer is a valid event
563 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100564 *
Mario Six8fac2912018-07-10 08:40:17 +0200565 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100566 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100567static efi_status_t efi_is_event(const struct efi_event *event)
568{
569 const struct efi_event *evt;
570
571 if (!event)
572 return EFI_INVALID_PARAMETER;
573 list_for_each_entry(evt, &efi_events, link) {
574 if (evt == event)
575 return EFI_SUCCESS;
576 }
577 return EFI_INVALID_PARAMETER;
578}
Alexander Grafc15d9212016-03-04 01:09:59 +0100579
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200580/**
Mario Six8fac2912018-07-10 08:40:17 +0200581 * efi_create_event() - create an event
582 * @type: type of the event to create
583 * @notify_tpl: task priority level of the event
584 * @notify_function: notification function of the event
585 * @notify_context: pointer passed to the notification function
586 * @group: event group
587 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200588 *
589 * This function is used inside U-Boot code to create an event.
590 *
591 * For the API function implementing the CreateEvent service see
592 * efi_create_event_ext.
593 *
Mario Six8fac2912018-07-10 08:40:17 +0200594 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200595 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100596efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200597 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200598 struct efi_event *event,
599 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100600 void *notify_context, efi_guid_t *group,
601 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100602{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100603 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200604
Jonathan Gray7758b212017-03-12 19:26:07 +1100605 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200606 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100607
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200608 switch (type) {
609 case 0:
610 case EVT_TIMER:
611 case EVT_NOTIFY_SIGNAL:
612 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
613 case EVT_NOTIFY_WAIT:
614 case EVT_TIMER | EVT_NOTIFY_WAIT:
615 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
616 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
617 break;
618 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200619 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200620 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100621
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900622 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200623 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200624 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100625
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100626 evt = calloc(1, sizeof(struct efi_event));
627 if (!evt)
628 return EFI_OUT_OF_RESOURCES;
629 evt->type = type;
630 evt->notify_tpl = notify_tpl;
631 evt->notify_function = notify_function;
632 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100633 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200634 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100635 evt->trigger_next = -1ULL;
636 evt->is_queued = false;
637 evt->is_signaled = false;
638 list_add_tail(&evt->link, &efi_events);
639 *event = evt;
640 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100641}
642
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200643/*
Mario Six8fac2912018-07-10 08:40:17 +0200644 * efi_create_event_ex() - create an event in a group
645 * @type: type of the event to create
646 * @notify_tpl: task priority level of the event
647 * @notify_function: notification function of the event
648 * @notify_context: pointer passed to the notification function
649 * @event: created event
650 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100651 *
652 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100653 *
Mario Six8fac2912018-07-10 08:40:17 +0200654 * See the Unified Extensible Firmware Interface (UEFI) specification for
655 * details.
656 *
657 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100658 */
659efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
660 void (EFIAPI *notify_function) (
661 struct efi_event *event,
662 void *context),
663 void *notify_context,
664 efi_guid_t *event_group,
665 struct efi_event **event)
666{
667 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
668 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100669 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100670 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100671}
672
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200673/**
Mario Six8fac2912018-07-10 08:40:17 +0200674 * efi_create_event_ext() - create an event
675 * @type: type of the event to create
676 * @notify_tpl: task priority level of the event
677 * @notify_function: notification function of the event
678 * @notify_context: pointer passed to the notification function
679 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200680 *
681 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200682 *
683 * See the Unified Extensible Firmware Interface (UEFI) specification for
684 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200685 *
Mario Six8fac2912018-07-10 08:40:17 +0200686 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200687 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200688static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100689 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200690 void (EFIAPI *notify_function) (
691 struct efi_event *event,
692 void *context),
693 void *notify_context, struct efi_event **event)
694{
695 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
696 notify_context);
697 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100698 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200699}
700
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200701/**
Mario Six8fac2912018-07-10 08:40:17 +0200702 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200703 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200704 * Check if a timer event has occurred or a queued notification function should
705 * be called.
706 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100707 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200708 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100709 */
710void efi_timer_check(void)
711{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100712 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100713 u64 now = timer_get_us();
714
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100715 list_for_each_entry(evt, &efi_events, link) {
716 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100717 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100718 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200719 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100720 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200721 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100722 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200723 break;
724 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100725 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200726 break;
727 default:
728 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200729 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100730 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100731 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100732 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100733 WATCHDOG_RESET();
734}
735
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200736/**
Mario Six8fac2912018-07-10 08:40:17 +0200737 * efi_set_timer() - set the trigger time for a timer event or stop the event
738 * @event: event for which the timer is set
739 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200740 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200741 *
742 * This is the function for internal usage in U-Boot. For the API function
743 * implementing the SetTimer service see efi_set_timer_ext.
744 *
Mario Six8fac2912018-07-10 08:40:17 +0200745 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200746 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200747efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200748 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100749{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100750 /* Check that the event is valid */
751 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
752 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100753
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200754 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200755 * The parameter defines a multiple of 100 ns.
756 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200757 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200758 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100759
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100760 switch (type) {
761 case EFI_TIMER_STOP:
762 event->trigger_next = -1ULL;
763 break;
764 case EFI_TIMER_PERIODIC:
765 case EFI_TIMER_RELATIVE:
766 event->trigger_next = timer_get_us() + trigger_time;
767 break;
768 default:
769 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100770 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100771 event->trigger_type = type;
772 event->trigger_time = trigger_time;
773 event->is_signaled = false;
774 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100775}
776
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200777/**
Mario Six8fac2912018-07-10 08:40:17 +0200778 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
779 * event
780 * @event: event for which the timer is set
781 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200782 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200783 *
784 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200785 *
Mario Six8fac2912018-07-10 08:40:17 +0200786 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 * details.
788 *
789 *
790 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200791 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200792static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
793 enum efi_timer_delay type,
794 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200795{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900796 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200797 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
798}
799
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200800/**
Mario Six8fac2912018-07-10 08:40:17 +0200801 * efi_wait_for_event() - wait for events to be signaled
802 * @num_events: number of events to be waited for
803 * @event: events to be waited for
804 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200805 *
806 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200807 *
Mario Six8fac2912018-07-10 08:40:17 +0200808 * See the Unified Extensible Firmware Interface (UEFI) specification for
809 * details.
810 *
811 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200812 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100813static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200814 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100815 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100816{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100817 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100818
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100819 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100820
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200821 /* Check parameters */
822 if (!num_events || !event)
823 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200824 /* Check TPL */
825 if (efi_tpl != TPL_APPLICATION)
826 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200827 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100828 if (efi_is_event(event[i]) != EFI_SUCCESS)
829 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200830 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
831 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200832 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100833 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200834 }
835
836 /* Wait for signal */
837 for (;;) {
838 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200839 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200840 goto out;
841 }
842 /* Allow events to occur. */
843 efi_timer_check();
844 }
845
846out:
847 /*
848 * Reset the signal which is passed to the caller to allow periodic
849 * events to occur.
850 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200851 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200852 if (index)
853 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100854
855 return EFI_EXIT(EFI_SUCCESS);
856}
857
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200858/**
Mario Six8fac2912018-07-10 08:40:17 +0200859 * efi_signal_event_ext() - signal an EFI event
860 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200861 *
862 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200863 *
864 * See the Unified Extensible Firmware Interface (UEFI) specification for
865 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200866 *
867 * This functions sets the signaled state of the event and queues the
868 * notification function for execution.
869 *
Mario Six8fac2912018-07-10 08:40:17 +0200870 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200871 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200872static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100873{
874 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100875 if (efi_is_event(event) != EFI_SUCCESS)
876 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100877 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100878 return EFI_EXIT(EFI_SUCCESS);
879}
880
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200881/**
Mario Six8fac2912018-07-10 08:40:17 +0200882 * efi_close_event() - close an EFI event
883 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 *
885 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 *
Mario Six8fac2912018-07-10 08:40:17 +0200887 * See the Unified Extensible Firmware Interface (UEFI) specification for
888 * details.
889 *
890 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200892static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100893{
894 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100895 if (efi_is_event(event) != EFI_SUCCESS)
896 return EFI_EXIT(EFI_INVALID_PARAMETER);
897 list_del(&event->link);
898 free(event);
899 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100900}
901
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200902/**
Mario Six8fac2912018-07-10 08:40:17 +0200903 * efi_check_event() - check if an event is signaled
904 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200905 *
906 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200907 *
908 * See the Unified Extensible Firmware Interface (UEFI) specification for
909 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 *
Mario Six8fac2912018-07-10 08:40:17 +0200911 * If an event is not signaled yet, the notification function is queued. The
912 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200913 *
Mario Six8fac2912018-07-10 08:40:17 +0200914 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200915 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200916static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100917{
918 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 if (efi_is_event(event) != EFI_SUCCESS ||
921 event->type & EVT_NOTIFY_SIGNAL)
922 return EFI_EXIT(EFI_INVALID_PARAMETER);
923 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100924 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100925 if (event->is_signaled) {
926 event->is_signaled = false;
927 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200928 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100929 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100930}
931
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200932/**
Mario Six8fac2912018-07-10 08:40:17 +0200933 * efi_search_obj() - find the internal EFI object for a handle
934 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200935 *
Mario Six8fac2912018-07-10 08:40:17 +0200936 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200937 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100938struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200939{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100940 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200941
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100942 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200943 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200944 return efiobj;
945 }
946
947 return NULL;
948}
949
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200950/**
Mario Six8fac2912018-07-10 08:40:17 +0200951 * efi_open_protocol_info_entry() - create open protocol info entry and add it
952 * to a protocol
953 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100954 *
Mario Six8fac2912018-07-10 08:40:17 +0200955 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100956 */
957static struct efi_open_protocol_info_entry *efi_create_open_info(
958 struct efi_handler *handler)
959{
960 struct efi_open_protocol_info_item *item;
961
962 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
963 if (!item)
964 return NULL;
965 /* Append the item to the open protocol info list. */
966 list_add_tail(&item->link, &handler->open_infos);
967
968 return &item->info;
969}
970
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200971/**
Mario Six8fac2912018-07-10 08:40:17 +0200972 * efi_delete_open_info() - remove an open protocol info entry from a protocol
973 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100974 *
Mario Six8fac2912018-07-10 08:40:17 +0200975 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100976 */
977static efi_status_t efi_delete_open_info(
978 struct efi_open_protocol_info_item *item)
979{
980 list_del(&item->link);
981 free(item);
982 return EFI_SUCCESS;
983}
984
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200985/**
Mario Six8fac2912018-07-10 08:40:17 +0200986 * efi_add_protocol() - install new protocol on a handle
987 * @handle: handle on which the protocol shall be installed
988 * @protocol: GUID of the protocol to be installed
989 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200990 *
Mario Six8fac2912018-07-10 08:40:17 +0200991 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200992 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100993efi_status_t efi_add_protocol(const efi_handle_t handle,
994 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200995 void *protocol_interface)
996{
997 struct efi_object *efiobj;
998 struct efi_handler *handler;
999 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001000
1001 efiobj = efi_search_obj(handle);
1002 if (!efiobj)
1003 return EFI_INVALID_PARAMETER;
1004 ret = efi_search_protocol(handle, protocol, NULL);
1005 if (ret != EFI_NOT_FOUND)
1006 return EFI_INVALID_PARAMETER;
1007 handler = calloc(1, sizeof(struct efi_handler));
1008 if (!handler)
1009 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001010 handler->guid = protocol;
1011 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001012 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001013 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001014 if (!guidcmp(&efi_guid_device_path, protocol))
1015 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001016 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001017}
1018
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001019/**
Mario Six8fac2912018-07-10 08:40:17 +02001020 * efi_install_protocol_interface() - install protocol interface
1021 * @handle: handle on which the protocol shall be installed
1022 * @protocol: GUID of the protocol to be installed
1023 * @protocol_interface_type: type of the interface to be installed,
1024 * always EFI_NATIVE_INTERFACE
1025 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001026 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001027 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001028 *
Mario Six8fac2912018-07-10 08:40:17 +02001029 * See the Unified Extensible Firmware Interface (UEFI) specification for
1030 * details.
1031 *
1032 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001033 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001034static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001035 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001036 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001037{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001038 efi_status_t r;
1039
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001040 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1041 protocol_interface);
1042
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001043 if (!handle || !protocol ||
1044 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1045 r = EFI_INVALID_PARAMETER;
1046 goto out;
1047 }
1048
1049 /* Create new handle if requested. */
1050 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001051 r = efi_create_handle(handle);
1052 if (r != EFI_SUCCESS)
1053 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001054 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1055 *handle);
1056 } else {
1057 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1058 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001059 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001060 /* Add new protocol */
1061 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001062out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001063 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001064}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001065
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001066/**
Mario Six8fac2912018-07-10 08:40:17 +02001067 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001068 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001069 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001070 * @number_of_drivers: number of child controllers
1071 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001072 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001073 * The allocated buffer has to be freed with free().
1074 *
Mario Six8fac2912018-07-10 08:40:17 +02001075 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001076 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001077static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001078 const efi_guid_t *protocol,
1079 efi_uintn_t *number_of_drivers,
1080 efi_handle_t **driver_handle_buffer)
1081{
1082 struct efi_handler *handler;
1083 struct efi_open_protocol_info_item *item;
1084 efi_uintn_t count = 0, i;
1085 bool duplicate;
1086
1087 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001088 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001089 if (protocol && guidcmp(handler->guid, protocol))
1090 continue;
1091 list_for_each_entry(item, &handler->open_infos, link) {
1092 if (item->info.attributes &
1093 EFI_OPEN_PROTOCOL_BY_DRIVER)
1094 ++count;
1095 }
1096 }
1097 /*
1098 * Create buffer. In case of duplicate driver assignments the buffer
1099 * will be too large. But that does not harm.
1100 */
1101 *number_of_drivers = 0;
1102 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1103 if (!*driver_handle_buffer)
1104 return EFI_OUT_OF_RESOURCES;
1105 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001106 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001107 if (protocol && guidcmp(handler->guid, protocol))
1108 continue;
1109 list_for_each_entry(item, &handler->open_infos, link) {
1110 if (item->info.attributes &
1111 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1112 /* Check this is a new driver */
1113 duplicate = false;
1114 for (i = 0; i < *number_of_drivers; ++i) {
1115 if ((*driver_handle_buffer)[i] ==
1116 item->info.agent_handle)
1117 duplicate = true;
1118 }
1119 /* Copy handle to buffer */
1120 if (!duplicate) {
1121 i = (*number_of_drivers)++;
1122 (*driver_handle_buffer)[i] =
1123 item->info.agent_handle;
1124 }
1125 }
1126 }
1127 }
1128 return EFI_SUCCESS;
1129}
1130
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001131/**
Mario Six8fac2912018-07-10 08:40:17 +02001132 * efi_disconnect_all_drivers() - disconnect all drivers from 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 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001136 *
1137 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001138 *
1139 * See the Unified Extensible Firmware Interface (UEFI) specification for
1140 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001141 *
Mario Six8fac2912018-07-10 08:40:17 +02001142 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001143 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001144static efi_status_t efi_disconnect_all_drivers
1145 (efi_handle_t handle,
1146 const efi_guid_t *protocol,
1147 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001148{
1149 efi_uintn_t number_of_drivers;
1150 efi_handle_t *driver_handle_buffer;
1151 efi_status_t r, ret;
1152
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001153 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001154 &driver_handle_buffer);
1155 if (ret != EFI_SUCCESS)
1156 return ret;
1157
1158 ret = EFI_NOT_FOUND;
1159 while (number_of_drivers) {
1160 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001161 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001162 driver_handle_buffer[--number_of_drivers],
1163 child_handle));
1164 if (r == EFI_SUCCESS)
1165 ret = r;
1166 }
1167 free(driver_handle_buffer);
1168 return ret;
1169}
1170
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001171/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001172 * efi_uninstall_protocol() - uninstall protocol interface
1173 *
Mario Six8fac2912018-07-10 08:40:17 +02001174 * @handle: handle from which the protocol shall be removed
1175 * @protocol: GUID of the protocol to be removed
1176 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001177 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001178 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001179 *
1180 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001181 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001182static efi_status_t efi_uninstall_protocol
1183 (efi_handle_t handle, const efi_guid_t *protocol,
1184 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001185{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001186 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001187 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001188 struct efi_open_protocol_info_item *item;
1189 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001190 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001191
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001192 /* Check handle */
1193 efiobj = efi_search_obj(handle);
1194 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001195 r = EFI_INVALID_PARAMETER;
1196 goto out;
1197 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001198 /* Find the protocol on the handle */
1199 r = efi_search_protocol(handle, protocol, &handler);
1200 if (r != EFI_SUCCESS)
1201 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001202 /* Disconnect controllers */
1203 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1204 if (!list_empty(&handler->open_infos)) {
1205 r = EFI_ACCESS_DENIED;
1206 goto out;
1207 }
1208 /* Close protocol */
1209 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1210 if (item->info.attributes ==
1211 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1212 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1213 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1214 list_del(&item->link);
1215 }
1216 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001217 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001218 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001219 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001220 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001221out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001222 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001223}
1224
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001225/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001226 * efi_uninstall_protocol_interface() - uninstall protocol interface
1227 * @handle: handle from which the protocol shall be removed
1228 * @protocol: GUID of the protocol to be removed
1229 * @protocol_interface: interface to be removed
1230 *
1231 * This function implements the UninstallProtocolInterface service.
1232 *
1233 * See the Unified Extensible Firmware Interface (UEFI) specification for
1234 * details.
1235 *
1236 * Return: status code
1237 */
1238static efi_status_t EFIAPI efi_uninstall_protocol_interface
1239 (efi_handle_t handle, const efi_guid_t *protocol,
1240 void *protocol_interface)
1241{
1242 efi_status_t ret;
1243
1244 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1245
1246 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1247 if (ret != EFI_SUCCESS)
1248 goto out;
1249
1250 /* If the last protocol has been removed, delete the handle. */
1251 if (list_empty(&handle->protocols)) {
1252 list_del(&handle->link);
1253 free(handle);
1254 }
1255out:
1256 return EFI_EXIT(ret);
1257}
1258
1259/**
Mario Six8fac2912018-07-10 08:40:17 +02001260 * efi_register_protocol_notify() - register an event for notification when a
1261 * protocol is installed.
1262 * @protocol: GUID of the protocol whose installation shall be notified
1263 * @event: event to be signaled upon installation of the protocol
1264 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001265 *
1266 * This function implements the RegisterProtocolNotify service.
1267 * See the Unified Extensible Firmware Interface (UEFI) specification
1268 * for details.
1269 *
Mario Six8fac2912018-07-10 08:40:17 +02001270 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001271 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001272static efi_status_t EFIAPI efi_register_protocol_notify(
1273 const efi_guid_t *protocol,
1274 struct efi_event *event,
1275 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001276{
Rob Clark238f88c2017-09-13 18:05:41 -04001277 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001278 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1279}
1280
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001281/**
Mario Six8fac2912018-07-10 08:40:17 +02001282 * efi_search() - determine if an EFI handle implements a protocol
1283 * @search_type: selection criterion
1284 * @protocol: GUID of the protocol
1285 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001286 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001287 *
1288 * See the documentation of the LocateHandle service in the UEFI specification.
1289 *
Mario Six8fac2912018-07-10 08:40:17 +02001290 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001291 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001292static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001293 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001294 efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001295{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001296 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001297
1298 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001299 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001300 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001301 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001302 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001303 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001304 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001305 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001306 return (ret != EFI_SUCCESS);
1307 default:
1308 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001309 return -1;
1310 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001311}
1312
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001313/**
Mario Six8fac2912018-07-10 08:40:17 +02001314 * efi_locate_handle() - locate handles implementing a protocol
1315 * @search_type: selection criterion
1316 * @protocol: GUID of the protocol
1317 * @search_key: registration key
1318 * @buffer_size: size of the buffer to receive the handles in bytes
1319 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001320 *
1321 * This function is meant for U-Boot internal calls. For the API implementation
1322 * of the LocateHandle service see efi_locate_handle_ext.
1323 *
Mario Six8fac2912018-07-10 08:40:17 +02001324 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001325 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001326static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001327 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001328 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001329 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001330{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001331 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001332 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001333
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001334 /* Check parameters */
1335 switch (search_type) {
1336 case ALL_HANDLES:
1337 break;
1338 case BY_REGISTER_NOTIFY:
1339 if (!search_key)
1340 return EFI_INVALID_PARAMETER;
1341 /* RegisterProtocolNotify is not implemented yet */
1342 return EFI_UNSUPPORTED;
1343 case BY_PROTOCOL:
1344 if (!protocol)
1345 return EFI_INVALID_PARAMETER;
1346 break;
1347 default:
1348 return EFI_INVALID_PARAMETER;
1349 }
1350
1351 /*
1352 * efi_locate_handle_buffer uses this function for
1353 * the calculation of the necessary buffer size.
1354 * So do not require a buffer for buffersize == 0.
1355 */
1356 if (!buffer_size || (*buffer_size && !buffer))
1357 return EFI_INVALID_PARAMETER;
1358
Alexander Grafc15d9212016-03-04 01:09:59 +01001359 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001360 list_for_each_entry(efiobj, &efi_obj_list, link) {
1361 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001362 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001363 }
1364
1365 if (*buffer_size < size) {
1366 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001367 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001368 }
1369
Rob Clarkcdee3372017-08-06 14:10:07 -04001370 *buffer_size = size;
1371 if (size == 0)
1372 return EFI_NOT_FOUND;
1373
Alexander Grafc15d9212016-03-04 01:09:59 +01001374 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001375 list_for_each_entry(efiobj, &efi_obj_list, link) {
1376 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001377 *buffer++ = efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01001378 }
1379
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001380 return EFI_SUCCESS;
1381}
1382
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001383/**
Mario Six8fac2912018-07-10 08:40:17 +02001384 * efi_locate_handle_ext() - locate handles implementing a protocol.
1385 * @search_type: selection criterion
1386 * @protocol: GUID of the protocol
1387 * @search_key: registration key
1388 * @buffer_size: size of the buffer to receive the handles in bytes
1389 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001390 *
1391 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001392 *
Mario Six8fac2912018-07-10 08:40:17 +02001393 * See the Unified Extensible Firmware Interface (UEFI) specification for
1394 * details.
1395 *
1396 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001397 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001398static efi_status_t EFIAPI efi_locate_handle_ext(
1399 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001400 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001401 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001402{
Rob Clark238f88c2017-09-13 18:05:41 -04001403 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001404 buffer_size, buffer);
1405
1406 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1407 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001408}
1409
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001410/**
Mario Six8fac2912018-07-10 08:40:17 +02001411 * efi_remove_configuration_table() - collapses configuration table entries,
1412 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001413 *
Mario Six8fac2912018-07-10 08:40:17 +02001414 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001415 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001416static void efi_remove_configuration_table(int i)
1417{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001418 struct efi_configuration_table *this = &systab.tables[i];
1419 struct efi_configuration_table *next = &systab.tables[i + 1];
1420 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001421
1422 memmove(this, next, (ulong)end - (ulong)next);
1423 systab.nr_tables--;
1424}
1425
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001426/**
Mario Six8fac2912018-07-10 08:40:17 +02001427 * efi_install_configuration_table() - adds, updates, or removes a
1428 * configuration table
1429 * @guid: GUID of the installed table
1430 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001431 *
1432 * This function is used for internal calls. For the API implementation of the
1433 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1434 *
Mario Six8fac2912018-07-10 08:40:17 +02001435 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001436 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001437efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1438 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001439{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001440 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001441 int i;
1442
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001443 if (!guid)
1444 return EFI_INVALID_PARAMETER;
1445
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001446 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001447 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001448 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001449 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001450 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001451 else
1452 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001453 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001454 }
1455 }
1456
Alexander Graffe3366f2017-07-26 13:41:04 +02001457 if (!table)
1458 return EFI_NOT_FOUND;
1459
Alexander Grafc15d9212016-03-04 01:09:59 +01001460 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001461 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001462 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001463
1464 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001465 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1466 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001467 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001468
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001469out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001470 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001471 efi_update_table_header_crc32(&systab.hdr);
1472
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001473 /* Notify that the configuration table was changed */
1474 list_for_each_entry(evt, &efi_events, link) {
1475 if (evt->group && !guidcmp(evt->group, guid)) {
1476 efi_signal_event(evt, false);
1477 break;
1478 }
1479 }
1480
Alexander Grafc5c11632016-08-19 01:23:24 +02001481 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001482}
1483
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001484/**
Mario Six8fac2912018-07-10 08:40:17 +02001485 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1486 * configuration table.
1487 * @guid: GUID of the installed table
1488 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001489 *
1490 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001491 *
Mario Six8fac2912018-07-10 08:40:17 +02001492 * See the Unified Extensible Firmware Interface (UEFI) specification for
1493 * details.
1494 *
1495 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001496 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001497static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1498 void *table)
1499{
Rob Clark238f88c2017-09-13 18:05:41 -04001500 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001501 return EFI_EXIT(efi_install_configuration_table(guid, table));
1502}
1503
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001504/**
Mario Six8fac2912018-07-10 08:40:17 +02001505 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001506 *
1507 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001508 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001509 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001510 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1511 * code is returned.
1512 *
1513 * @device_path: device path of the loaded image
1514 * @file_path: file path of the loaded image
1515 * @handle_ptr: handle of the loaded image
1516 * @info_ptr: loaded image protocol
1517 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001518 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001519efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1520 struct efi_device_path *file_path,
1521 struct efi_loaded_image_obj **handle_ptr,
1522 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001523{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001524 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001525 struct efi_loaded_image *info = NULL;
1526 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001527 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001528
1529 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1530 *handle_ptr = NULL;
1531 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001532
1533 info = calloc(1, sizeof(*info));
1534 if (!info)
1535 return EFI_OUT_OF_RESOURCES;
1536 obj = calloc(1, sizeof(*obj));
1537 if (!obj) {
1538 free(info);
1539 return EFI_OUT_OF_RESOURCES;
1540 }
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001541
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001542 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001543 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001544
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001545 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001546 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001547 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001548
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001549 if (device_path) {
1550 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001551
1552 dp = efi_dp_append(device_path, file_path);
1553 if (!dp) {
1554 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001555 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001556 }
1557 } else {
1558 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001559 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001560 ret = efi_add_protocol(&obj->header,
1561 &efi_guid_loaded_image_device_path, dp);
1562 if (ret != EFI_SUCCESS)
1563 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001564
1565 /*
1566 * When asking for the loaded_image interface, just
1567 * return handle which points to loaded_image_info
1568 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001569 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001570 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001571 if (ret != EFI_SUCCESS)
1572 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001573
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001574 *info_ptr = info;
1575 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001576
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001577 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001578failure:
1579 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001580 efi_delete_handle(&obj->header);
1581 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001582 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001583}
1584
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001585/**
Mario Six8fac2912018-07-10 08:40:17 +02001586 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001587 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001588 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1589 * callers obligation to update the memory type as needed.
1590 *
1591 * @file_path: the path of the image to load
1592 * @buffer: buffer containing the loaded image
1593 * @size: size of the loaded image
1594 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001595 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001596static
Rob Clarkc84c1102017-09-13 18:05:38 -04001597efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001598 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001599{
1600 struct efi_file_info *info = NULL;
1601 struct efi_file_handle *f;
1602 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001603 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001604 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001605
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001606 /* In case of failure nothing is returned */
1607 *buffer = NULL;
1608 *size = 0;
1609
1610 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001611 f = efi_file_from_path(file_path);
1612 if (!f)
1613 return EFI_DEVICE_ERROR;
1614
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001615 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001616 bs = 0;
1617 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1618 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001619 if (ret != EFI_BUFFER_TOO_SMALL) {
1620 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001621 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001622 }
Rob Clark857a1222017-09-13 18:05:35 -04001623
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001624 info = malloc(bs);
1625 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1626 info));
1627 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001628 goto error;
1629
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001630 /*
1631 * When reading the file we do not yet know if it contains an
1632 * application, a boottime driver, or a runtime driver. So here we
1633 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1634 * update the reservation according to the image type.
1635 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001636 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001637 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1638 EFI_BOOT_SERVICES_DATA,
1639 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001640 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001641 ret = EFI_OUT_OF_RESOURCES;
1642 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001643 }
1644
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001645 /* Read file */
1646 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1647 if (ret != EFI_SUCCESS)
1648 efi_free_pages(addr, efi_size_in_pages(bs));
1649 *buffer = (void *)(uintptr_t)addr;
1650 *size = bs;
1651error:
1652 EFI_CALL(f->close(f));
1653 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001654 return ret;
1655}
1656
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001657/**
Mario Six8fac2912018-07-10 08:40:17 +02001658 * efi_load_image() - load an EFI image into memory
1659 * @boot_policy: true for request originating from the boot manager
1660 * @parent_image: the caller's image handle
1661 * @file_path: the path of the image to load
1662 * @source_buffer: memory location from which the image is installed
1663 * @source_size: size of the memory area from which the image is installed
1664 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001665 *
1666 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001667 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001668 * See the Unified Extensible Firmware Interface (UEFI) specification
1669 * for details.
1670 *
Mario Six8fac2912018-07-10 08:40:17 +02001671 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001672 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001673efi_status_t EFIAPI efi_load_image(bool boot_policy,
1674 efi_handle_t parent_image,
1675 struct efi_device_path *file_path,
1676 void *source_buffer,
1677 efi_uintn_t source_size,
1678 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001679{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001680 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001681 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001682 struct efi_loaded_image_obj **image_obj =
1683 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001684 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001685 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001686
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001687 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001688 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001689
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001690 if (!image_handle || !parent_image) {
1691 ret = EFI_INVALID_PARAMETER;
1692 goto error;
1693 }
1694
1695 if (!source_buffer && !file_path) {
1696 ret = EFI_NOT_FOUND;
1697 goto error;
1698 }
Rob Clark857a1222017-09-13 18:05:35 -04001699
1700 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001701 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001702 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001703 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001704 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001705 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001706 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001707 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001708 /* split file_path which contains both the device and file parts */
1709 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001710 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001711 if (ret == EFI_SUCCESS)
1712 ret = efi_load_pe(*image_obj, dest_buffer, info);
1713 if (!source_buffer)
1714 /* Release buffer to which file was loaded */
1715 efi_free_pages((uintptr_t)dest_buffer,
1716 efi_size_in_pages(source_size));
1717 if (ret == EFI_SUCCESS) {
1718 info->system_table = &systab;
1719 info->parent_handle = parent_image;
1720 } else {
1721 /* The image is invalid. Release all associated resources. */
1722 efi_delete_handle(*image_handle);
1723 *image_handle = NULL;
1724 free(info);
1725 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001726error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001727 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001728}
1729
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001730/**
Mario Six8fac2912018-07-10 08:40:17 +02001731 * efi_unload_image() - unload an EFI image
1732 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001733 *
1734 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001735 *
1736 * See the Unified Extensible Firmware Interface (UEFI) specification for
1737 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001738 *
Mario Six8fac2912018-07-10 08:40:17 +02001739 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001740 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001741efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001742{
1743 struct efi_object *efiobj;
1744
1745 EFI_ENTRY("%p", image_handle);
1746 efiobj = efi_search_obj(image_handle);
1747 if (efiobj)
1748 list_del(&efiobj->link);
1749
1750 return EFI_EXIT(EFI_SUCCESS);
1751}
1752
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001753/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001754 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1755 */
1756static void efi_exit_caches(void)
1757{
1758#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1759 /*
1760 * Grub on 32bit ARM needs to have caches disabled before jumping into
1761 * a zImage, but does not know of all cache layers. Give it a hand.
1762 */
1763 if (efi_is_direct_boot)
1764 cleanup_before_linux();
1765#endif
1766}
1767
1768/**
Mario Six8fac2912018-07-10 08:40:17 +02001769 * efi_exit_boot_services() - stop all boot services
1770 * @image_handle: handle of the loaded image
1771 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001772 *
1773 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001774 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001775 * See the Unified Extensible Firmware Interface (UEFI) specification
1776 * for details.
1777 *
Mario Six8fac2912018-07-10 08:40:17 +02001778 * All timer events are disabled. For exit boot services events the
1779 * notification function is called. The boot services are disabled in the
1780 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001781 *
Mario Six8fac2912018-07-10 08:40:17 +02001782 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001783 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001784static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001785 unsigned long map_key)
1786{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001787 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001788
Alexander Grafc15d9212016-03-04 01:09:59 +01001789 EFI_ENTRY("%p, %ld", image_handle, map_key);
1790
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001791 /* Check that the caller has read the current memory map */
1792 if (map_key != efi_memory_map_key)
1793 return EFI_INVALID_PARAMETER;
1794
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001795 /* Make sure that notification functions are not called anymore */
1796 efi_tpl = TPL_HIGH_LEVEL;
1797
1798 /* Check if ExitBootServices has already been called */
1799 if (!systab.boottime)
1800 return EFI_EXIT(EFI_SUCCESS);
1801
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001802 /* Add related events to the event group */
1803 list_for_each_entry(evt, &efi_events, link) {
1804 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1805 evt->group = &efi_guid_event_group_exit_boot_services;
1806 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001807 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001808 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001809 if (evt->group &&
1810 !guidcmp(evt->group,
1811 &efi_guid_event_group_exit_boot_services)) {
1812 efi_signal_event(evt, false);
1813 break;
1814 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001815 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001816
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001817 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001818
Alexander Graf2ebeb442016-11-17 01:02:57 +01001819 board_quiesce_devices();
1820
Alexander Graffa5246b2018-11-15 21:23:47 +01001821 /* Fix up caches for EFI payloads if necessary */
1822 efi_exit_caches();
1823
Alexander Grafc15d9212016-03-04 01:09:59 +01001824 /* This stops all lingering devices */
1825 bootm_disable_interrupts();
1826
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001827 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001828 systab.con_in_handle = NULL;
1829 systab.con_in = NULL;
1830 systab.con_out_handle = NULL;
1831 systab.con_out = NULL;
1832 systab.stderr_handle = NULL;
1833 systab.std_err = NULL;
1834 systab.boottime = NULL;
1835
1836 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001837 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001838
Alexander Grafc15d9212016-03-04 01:09:59 +01001839 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001840 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001841 WATCHDOG_RESET();
1842
1843 return EFI_EXIT(EFI_SUCCESS);
1844}
1845
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001846/**
Mario Six8fac2912018-07-10 08:40:17 +02001847 * efi_get_next_monotonic_count() - get next value of the counter
1848 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001849 *
1850 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001851 *
1852 * See the Unified Extensible Firmware Interface (UEFI) specification for
1853 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001854 *
Mario Six8fac2912018-07-10 08:40:17 +02001855 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001856 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001857static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1858{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001859 static uint64_t mono;
1860
Alexander Grafc15d9212016-03-04 01:09:59 +01001861 EFI_ENTRY("%p", count);
1862 *count = mono++;
1863 return EFI_EXIT(EFI_SUCCESS);
1864}
1865
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001866/**
Mario Six8fac2912018-07-10 08:40:17 +02001867 * efi_stall() - sleep
1868 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001869 *
Mario Six8fac2912018-07-10 08:40:17 +02001870 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001871 *
Mario Six8fac2912018-07-10 08:40:17 +02001872 * See the Unified Extensible Firmware Interface (UEFI) specification for
1873 * details.
1874 *
1875 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001876 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001877static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1878{
1879 EFI_ENTRY("%ld", microseconds);
1880 udelay(microseconds);
1881 return EFI_EXIT(EFI_SUCCESS);
1882}
1883
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001884/**
Mario Six8fac2912018-07-10 08:40:17 +02001885 * efi_set_watchdog_timer() - reset the watchdog timer
1886 * @timeout: seconds before reset by watchdog
1887 * @watchdog_code: code to be logged when resetting
1888 * @data_size: size of buffer in bytes
1889 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001890 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001891 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001892 *
Mario Six8fac2912018-07-10 08:40:17 +02001893 * See the Unified Extensible Firmware Interface (UEFI) specification for
1894 * details.
1895 *
1896 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001897 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001898static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1899 uint64_t watchdog_code,
1900 unsigned long data_size,
1901 uint16_t *watchdog_data)
1902{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001903 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001904 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001905 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001906}
1907
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001908/**
Mario Six8fac2912018-07-10 08:40:17 +02001909 * efi_close_protocol() - close a protocol
1910 * @handle: handle on which the protocol shall be closed
1911 * @protocol: GUID of the protocol to close
1912 * @agent_handle: handle of the driver
1913 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001914 *
1915 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001916 *
Mario Six8fac2912018-07-10 08:40:17 +02001917 * See the Unified Extensible Firmware Interface (UEFI) specification for
1918 * details.
1919 *
1920 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001921 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001922static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001923 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001924 efi_handle_t agent_handle,
1925 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001926{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001927 struct efi_handler *handler;
1928 struct efi_open_protocol_info_item *item;
1929 struct efi_open_protocol_info_item *pos;
1930 efi_status_t r;
1931
Rob Clark238f88c2017-09-13 18:05:41 -04001932 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001933 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001934
1935 if (!agent_handle) {
1936 r = EFI_INVALID_PARAMETER;
1937 goto out;
1938 }
1939 r = efi_search_protocol(handle, protocol, &handler);
1940 if (r != EFI_SUCCESS)
1941 goto out;
1942
1943 r = EFI_NOT_FOUND;
1944 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1945 if (item->info.agent_handle == agent_handle &&
1946 item->info.controller_handle == controller_handle) {
1947 efi_delete_open_info(item);
1948 r = EFI_SUCCESS;
1949 break;
1950 }
1951 }
1952out:
1953 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001954}
1955
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001956/**
Mario Six8fac2912018-07-10 08:40:17 +02001957 * efi_open_protocol_information() - provide information about then open status
1958 * of a protocol on a handle
1959 * @handle: handle for which the information shall be retrieved
1960 * @protocol: GUID of the protocol
1961 * @entry_buffer: buffer to receive the open protocol information
1962 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001963 *
1964 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02001965 *
1966 * See the Unified Extensible Firmware Interface (UEFI) specification for
1967 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001968 *
Mario Six8fac2912018-07-10 08:40:17 +02001969 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001970 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001971static efi_status_t EFIAPI efi_open_protocol_information(
1972 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001973 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001974 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001975{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001976 unsigned long buffer_size;
1977 unsigned long count;
1978 struct efi_handler *handler;
1979 struct efi_open_protocol_info_item *item;
1980 efi_status_t r;
1981
Rob Clark238f88c2017-09-13 18:05:41 -04001982 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001983 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001984
1985 /* Check parameters */
1986 if (!entry_buffer) {
1987 r = EFI_INVALID_PARAMETER;
1988 goto out;
1989 }
1990 r = efi_search_protocol(handle, protocol, &handler);
1991 if (r != EFI_SUCCESS)
1992 goto out;
1993
1994 /* Count entries */
1995 count = 0;
1996 list_for_each_entry(item, &handler->open_infos, link) {
1997 if (item->info.open_count)
1998 ++count;
1999 }
2000 *entry_count = count;
2001 *entry_buffer = NULL;
2002 if (!count) {
2003 r = EFI_SUCCESS;
2004 goto out;
2005 }
2006
2007 /* Copy entries */
2008 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002009 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002010 (void **)entry_buffer);
2011 if (r != EFI_SUCCESS)
2012 goto out;
2013 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2014 if (item->info.open_count)
2015 (*entry_buffer)[--count] = item->info;
2016 }
2017out:
2018 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002019}
2020
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002021/**
Mario Six8fac2912018-07-10 08:40:17 +02002022 * efi_protocols_per_handle() - get protocols installed on a handle
2023 * @handle: handle for which the information is retrieved
2024 * @protocol_buffer: buffer with protocol GUIDs
2025 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002026 *
2027 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002028 *
Mario Six8fac2912018-07-10 08:40:17 +02002029 * See the Unified Extensible Firmware Interface (UEFI) specification for
2030 * details.
2031 *
2032 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002033 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002034static efi_status_t EFIAPI efi_protocols_per_handle(
2035 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002036 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002037{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002038 unsigned long buffer_size;
2039 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002040 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002041 efi_status_t r;
2042
Alexander Grafc15d9212016-03-04 01:09:59 +01002043 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2044 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002045
2046 if (!handle || !protocol_buffer || !protocol_buffer_count)
2047 return EFI_EXIT(EFI_INVALID_PARAMETER);
2048
2049 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002050 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002051
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002052 efiobj = efi_search_obj(handle);
2053 if (!efiobj)
2054 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002055
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002056 /* Count protocols */
2057 list_for_each(protocol_handle, &efiobj->protocols) {
2058 ++*protocol_buffer_count;
2059 }
2060
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002061 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002062 if (*protocol_buffer_count) {
2063 size_t j = 0;
2064
2065 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002066 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002067 (void **)protocol_buffer);
2068 if (r != EFI_SUCCESS)
2069 return EFI_EXIT(r);
2070 list_for_each(protocol_handle, &efiobj->protocols) {
2071 struct efi_handler *protocol;
2072
2073 protocol = list_entry(protocol_handle,
2074 struct efi_handler, link);
2075 (*protocol_buffer)[j] = (void *)protocol->guid;
2076 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002077 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002078 }
2079
2080 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002081}
2082
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002083/**
Mario Six8fac2912018-07-10 08:40:17 +02002084 * efi_locate_handle_buffer() - locate handles implementing a protocol
2085 * @search_type: selection criterion
2086 * @protocol: GUID of the protocol
2087 * @search_key: registration key
2088 * @no_handles: number of returned handles
2089 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002090 *
2091 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002092 *
Mario Six8fac2912018-07-10 08:40:17 +02002093 * See the Unified Extensible Firmware Interface (UEFI) specification for
2094 * details.
2095 *
2096 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002097 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002098static efi_status_t EFIAPI efi_locate_handle_buffer(
2099 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002100 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002101 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002102{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002103 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002104 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002105
Rob Clark238f88c2017-09-13 18:05:41 -04002106 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002107 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002108
2109 if (!no_handles || !buffer) {
2110 r = EFI_INVALID_PARAMETER;
2111 goto out;
2112 }
2113 *no_handles = 0;
2114 *buffer = NULL;
2115 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2116 *buffer);
2117 if (r != EFI_BUFFER_TOO_SMALL)
2118 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002119 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002120 (void **)buffer);
2121 if (r != EFI_SUCCESS)
2122 goto out;
2123 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2124 *buffer);
2125 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002126 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002127out:
2128 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002129}
2130
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002131/**
Mario Six8fac2912018-07-10 08:40:17 +02002132 * efi_locate_protocol() - find an interface implementing a protocol
2133 * @protocol: GUID of the protocol
2134 * @registration: registration key passed to the notification function
2135 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002136 *
2137 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002138 *
2139 * See the Unified Extensible Firmware Interface (UEFI) specification for
2140 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002141 *
Mario Six8fac2912018-07-10 08:40:17 +02002142 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002143 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002144static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002145 void *registration,
2146 void **protocol_interface)
2147{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002148 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002149 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002150
Rob Clark238f88c2017-09-13 18:05:41 -04002151 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002152
2153 if (!protocol || !protocol_interface)
2154 return EFI_EXIT(EFI_INVALID_PARAMETER);
2155
2156 list_for_each(lhandle, &efi_obj_list) {
2157 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002158 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002159
2160 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002161
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002162 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002163 if (ret == EFI_SUCCESS) {
2164 *protocol_interface = handler->protocol_interface;
2165 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002166 }
2167 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002168 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002169
2170 return EFI_EXIT(EFI_NOT_FOUND);
2171}
2172
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002173/**
Mario Six8fac2912018-07-10 08:40:17 +02002174 * efi_locate_device_path() - Get the device path and handle of an device
2175 * implementing a protocol
2176 * @protocol: GUID of the protocol
2177 * @device_path: device path
2178 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002179 *
2180 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002181 *
Mario Six8fac2912018-07-10 08:40:17 +02002182 * See the Unified Extensible Firmware Interface (UEFI) specification for
2183 * details.
2184 *
2185 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002186 */
2187static efi_status_t EFIAPI efi_locate_device_path(
2188 const efi_guid_t *protocol,
2189 struct efi_device_path **device_path,
2190 efi_handle_t *device)
2191{
2192 struct efi_device_path *dp;
2193 size_t i;
2194 struct efi_handler *handler;
2195 efi_handle_t *handles;
2196 size_t len, len_dp;
2197 size_t len_best = 0;
2198 efi_uintn_t no_handles;
2199 u8 *remainder;
2200 efi_status_t ret;
2201
2202 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2203
2204 if (!protocol || !device_path || !*device_path || !device) {
2205 ret = EFI_INVALID_PARAMETER;
2206 goto out;
2207 }
2208
2209 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002210 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002211
2212 /* Get all handles implementing the protocol */
2213 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2214 &no_handles, &handles));
2215 if (ret != EFI_SUCCESS)
2216 goto out;
2217
2218 for (i = 0; i < no_handles; ++i) {
2219 /* Find the device path protocol */
2220 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2221 &handler);
2222 if (ret != EFI_SUCCESS)
2223 continue;
2224 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002225 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002226 /*
2227 * This handle can only be a better fit
2228 * if its device path length is longer than the best fit and
2229 * if its device path length is shorter of equal the searched
2230 * device path.
2231 */
2232 if (len_dp <= len_best || len_dp > len)
2233 continue;
2234 /* Check if dp is a subpath of device_path */
2235 if (memcmp(*device_path, dp, len_dp))
2236 continue;
2237 *device = handles[i];
2238 len_best = len_dp;
2239 }
2240 if (len_best) {
2241 remainder = (u8 *)*device_path + len_best;
2242 *device_path = (struct efi_device_path *)remainder;
2243 ret = EFI_SUCCESS;
2244 } else {
2245 ret = EFI_NOT_FOUND;
2246 }
2247out:
2248 return EFI_EXIT(ret);
2249}
2250
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002251/**
Mario Six8fac2912018-07-10 08:40:17 +02002252 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2253 * interfaces
2254 * @handle: handle on which the protocol interfaces shall be installed
2255 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2256 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002257 *
2258 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002259 *
2260 * See the Unified Extensible Firmware Interface (UEFI) specification for
2261 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002262 *
Mario Six8fac2912018-07-10 08:40:17 +02002263 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002264 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002265efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002266 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002267{
2268 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002269
Alexander Graf404a7c82018-06-18 17:23:05 +02002270 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002271 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002272 void *protocol_interface;
2273 efi_status_t r = EFI_SUCCESS;
2274 int i = 0;
2275
2276 if (!handle)
2277 return EFI_EXIT(EFI_INVALID_PARAMETER);
2278
Alexander Graf404a7c82018-06-18 17:23:05 +02002279 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002280 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002281 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002282 if (!protocol)
2283 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002284 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002285 r = EFI_CALL(efi_install_protocol_interface(
2286 handle, protocol,
2287 EFI_NATIVE_INTERFACE,
2288 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002289 if (r != EFI_SUCCESS)
2290 break;
2291 i++;
2292 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002293 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002294 if (r == EFI_SUCCESS)
2295 return EFI_EXIT(r);
2296
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002297 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002298 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002299 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002300 protocol = efi_va_arg(argptr, efi_guid_t*);
2301 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002302 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002303 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002304 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002305 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002306
2307 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002308}
2309
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002310/**
Mario Six8fac2912018-07-10 08:40:17 +02002311 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2312 * interfaces
2313 * @handle: handle from which the protocol interfaces shall be removed
2314 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2315 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002316 *
2317 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002318 *
Mario Six8fac2912018-07-10 08:40:17 +02002319 * See the Unified Extensible Firmware Interface (UEFI) specification for
2320 * details.
2321 *
2322 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002323 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002324static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002325 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002326{
2327 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002328
Alexander Graf404a7c82018-06-18 17:23:05 +02002329 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002330 const efi_guid_t *protocol;
2331 void *protocol_interface;
2332 efi_status_t r = EFI_SUCCESS;
2333 size_t i = 0;
2334
2335 if (!handle)
2336 return EFI_EXIT(EFI_INVALID_PARAMETER);
2337
Alexander Graf404a7c82018-06-18 17:23:05 +02002338 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002339 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002340 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002341 if (!protocol)
2342 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002343 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002344 r = efi_uninstall_protocol(handle, protocol,
2345 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002346 if (r != EFI_SUCCESS)
2347 break;
2348 i++;
2349 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002350 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002351 if (r == EFI_SUCCESS) {
2352 /* If the last protocol has been removed, delete the handle. */
2353 if (list_empty(&handle->protocols)) {
2354 list_del(&handle->link);
2355 free(handle);
2356 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002357 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002358 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002359
2360 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002361 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002362 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002363 protocol = efi_va_arg(argptr, efi_guid_t*);
2364 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002365 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2366 EFI_NATIVE_INTERFACE,
2367 protocol_interface));
2368 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002369 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002370
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002371 /* In case of an error always return EFI_INVALID_PARAMETER */
2372 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002373}
2374
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002375/**
Mario Six8fac2912018-07-10 08:40:17 +02002376 * efi_calculate_crc32() - calculate cyclic redundancy code
2377 * @data: buffer with data
2378 * @data_size: size of buffer in bytes
2379 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002380 *
2381 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002382 *
2383 * See the Unified Extensible Firmware Interface (UEFI) specification for
2384 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002385 *
Mario Six8fac2912018-07-10 08:40:17 +02002386 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002387 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002388static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2389 efi_uintn_t data_size,
2390 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002391{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002392 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002393 *crc32_p = crc32(0, data, data_size);
2394 return EFI_EXIT(EFI_SUCCESS);
2395}
2396
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002397/**
Mario Six8fac2912018-07-10 08:40:17 +02002398 * efi_copy_mem() - copy memory
2399 * @destination: destination of the copy operation
2400 * @source: source of the copy operation
2401 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002402 *
2403 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002404 *
Mario Six8fac2912018-07-10 08:40:17 +02002405 * See the Unified Extensible Firmware Interface (UEFI) specification for
2406 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002407 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002408static void EFIAPI efi_copy_mem(void *destination, const void *source,
2409 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002410{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002411 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002412 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002413 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002414}
2415
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002416/**
Mario Six8fac2912018-07-10 08:40:17 +02002417 * efi_set_mem() - Fill memory with a byte value.
2418 * @buffer: buffer to fill
2419 * @size: size of buffer in bytes
2420 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002421 *
2422 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002423 *
Mario Six8fac2912018-07-10 08:40:17 +02002424 * See the Unified Extensible Firmware Interface (UEFI) specification for
2425 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002426 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002427static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002428{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002429 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002430 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002431 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002432}
2433
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002434/**
Mario Six8fac2912018-07-10 08:40:17 +02002435 * efi_protocol_open() - open protocol interface on a handle
2436 * @handler: handler of a protocol
2437 * @protocol_interface: interface implementing the protocol
2438 * @agent_handle: handle of the driver
2439 * @controller_handle: handle of the controller
2440 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002441 *
Mario Six8fac2912018-07-10 08:40:17 +02002442 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002443 */
2444static efi_status_t efi_protocol_open(
2445 struct efi_handler *handler,
2446 void **protocol_interface, void *agent_handle,
2447 void *controller_handle, uint32_t attributes)
2448{
2449 struct efi_open_protocol_info_item *item;
2450 struct efi_open_protocol_info_entry *match = NULL;
2451 bool opened_by_driver = false;
2452 bool opened_exclusive = false;
2453
2454 /* If there is no agent, only return the interface */
2455 if (!agent_handle)
2456 goto out;
2457
2458 /* For TEST_PROTOCOL ignore interface attribute */
2459 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2460 *protocol_interface = NULL;
2461
2462 /*
2463 * Check if the protocol is already opened by a driver with the same
2464 * attributes or opened exclusively
2465 */
2466 list_for_each_entry(item, &handler->open_infos, link) {
2467 if (item->info.agent_handle == agent_handle) {
2468 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2469 (item->info.attributes == attributes))
2470 return EFI_ALREADY_STARTED;
2471 }
2472 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2473 opened_exclusive = true;
2474 }
2475
2476 /* Only one controller can open the protocol exclusively */
2477 if (opened_exclusive && attributes &
2478 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2479 return EFI_ACCESS_DENIED;
2480
2481 /* Prepare exclusive opening */
2482 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2483 /* Try to disconnect controllers */
2484 list_for_each_entry(item, &handler->open_infos, link) {
2485 if (item->info.attributes ==
2486 EFI_OPEN_PROTOCOL_BY_DRIVER)
2487 EFI_CALL(efi_disconnect_controller(
2488 item->info.controller_handle,
2489 item->info.agent_handle,
2490 NULL));
2491 }
2492 opened_by_driver = false;
2493 /* Check if all controllers are disconnected */
2494 list_for_each_entry(item, &handler->open_infos, link) {
2495 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2496 opened_by_driver = true;
2497 }
Heinrich Schuchardt8d0b45d2018-09-30 13:40:43 +02002498 /* Only one controller can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002499 if (opened_by_driver)
2500 return EFI_ACCESS_DENIED;
2501 }
2502
2503 /* Find existing entry */
2504 list_for_each_entry(item, &handler->open_infos, link) {
2505 if (item->info.agent_handle == agent_handle &&
2506 item->info.controller_handle == controller_handle)
2507 match = &item->info;
2508 }
2509 /* None found, create one */
2510 if (!match) {
2511 match = efi_create_open_info(handler);
2512 if (!match)
2513 return EFI_OUT_OF_RESOURCES;
2514 }
2515
2516 match->agent_handle = agent_handle;
2517 match->controller_handle = controller_handle;
2518 match->attributes = attributes;
2519 match->open_count++;
2520
2521out:
2522 /* For TEST_PROTOCOL ignore interface attribute. */
2523 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2524 *protocol_interface = handler->protocol_interface;
2525
2526 return EFI_SUCCESS;
2527}
2528
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002529/**
Mario Six8fac2912018-07-10 08:40:17 +02002530 * efi_open_protocol() - open protocol interface on a handle
2531 * @handle: handle on which the protocol shall be opened
2532 * @protocol: GUID of the protocol
2533 * @protocol_interface: interface implementing the protocol
2534 * @agent_handle: handle of the driver
2535 * @controller_handle: handle of the controller
2536 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002537 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002538 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002539 *
Mario Six8fac2912018-07-10 08:40:17 +02002540 * See the Unified Extensible Firmware Interface (UEFI) specification for
2541 * details.
2542 *
2543 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002544 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002545static efi_status_t EFIAPI efi_open_protocol
2546 (efi_handle_t handle, const efi_guid_t *protocol,
2547 void **protocol_interface, efi_handle_t agent_handle,
2548 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002549{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002550 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002551 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002552
Rob Clark238f88c2017-09-13 18:05:41 -04002553 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002554 protocol_interface, agent_handle, controller_handle,
2555 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002556
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002557 if (!handle || !protocol ||
2558 (!protocol_interface && attributes !=
2559 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002560 goto out;
2561 }
2562
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002563 switch (attributes) {
2564 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2565 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2566 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2567 break;
2568 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2569 if (controller_handle == handle)
2570 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002571 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002572 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2573 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002574 /* Check that the controller handle is valid */
2575 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002576 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002577 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002578 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002579 /* Check that the agent handle is valid */
2580 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002581 goto out;
2582 break;
2583 default:
2584 goto out;
2585 }
2586
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002587 r = efi_search_protocol(handle, protocol, &handler);
2588 if (r != EFI_SUCCESS)
2589 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002590
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002591 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2592 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002593out:
2594 return EFI_EXIT(r);
2595}
2596
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002597/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002598 * efi_start_image() - call the entry point of an image
2599 * @image_handle: handle of the image
2600 * @exit_data_size: size of the buffer
2601 * @exit_data: buffer to receive the exit data of the called image
2602 *
2603 * This function implements the StartImage service.
2604 *
2605 * See the Unified Extensible Firmware Interface (UEFI) specification for
2606 * details.
2607 *
2608 * Return: status code
2609 */
2610efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2611 efi_uintn_t *exit_data_size,
2612 u16 **exit_data)
2613{
2614 struct efi_loaded_image_obj *image_obj =
2615 (struct efi_loaded_image_obj *)image_handle;
2616 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002617 void *info;
2618 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002619
2620 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2621
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002622 /* Check parameters */
2623 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2624 &info, NULL, NULL,
2625 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2626 if (ret != EFI_SUCCESS)
2627 return EFI_EXIT(EFI_INVALID_PARAMETER);
2628
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002629 efi_is_direct_boot = false;
2630
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002631 image_obj->exit_data_size = exit_data_size;
2632 image_obj->exit_data = exit_data;
2633
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002634 /* call the image! */
2635 if (setjmp(&image_obj->exit_jmp)) {
2636 /*
2637 * We called the entry point of the child image with EFI_CALL
2638 * in the lines below. The child image called the Exit() boot
2639 * service efi_exit() which executed the long jump that brought
2640 * us to the current line. This implies that the second half
2641 * of the EFI_CALL macro has not been executed.
2642 */
2643#ifdef CONFIG_ARM
2644 /*
2645 * efi_exit() called efi_restore_gd(). We have to undo this
2646 * otherwise __efi_entry_check() will put the wrong value into
2647 * app_gd.
2648 */
2649 gd = app_gd;
2650#endif
2651 /*
2652 * To get ready to call EFI_EXIT below we have to execute the
2653 * missed out steps of EFI_CALL.
2654 */
2655 assert(__efi_entry_check());
2656 debug("%sEFI: %lu returned by started image\n",
2657 __efi_nesting_dec(),
2658 (unsigned long)((uintptr_t)image_obj->exit_status &
2659 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002660 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002661 return EFI_EXIT(image_obj->exit_status);
2662 }
2663
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002664 current_image = image_handle;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002665 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002666 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2667
2668 /*
2669 * Usually UEFI applications call Exit() instead of returning.
2670 * But because the world doesn't consist of ponies and unicorns,
2671 * we're happy to emulate that behavior on behalf of a payload
2672 * that forgot.
2673 */
2674 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2675}
2676
2677/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002678 * efi_update_exit_data() - fill exit data parameters of StartImage()
2679 *
2680 * @image_obj image handle
2681 * @exit_data_size size of the exit data buffer
2682 * @exit_data buffer with data returned by UEFI payload
2683 * Return: status code
2684 */
2685static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2686 efi_uintn_t exit_data_size,
2687 u16 *exit_data)
2688{
2689 efi_status_t ret;
2690
2691 /*
2692 * If exit_data is not provided to StartImage(), exit_data_size must be
2693 * ignored.
2694 */
2695 if (!image_obj->exit_data)
2696 return EFI_SUCCESS;
2697 if (image_obj->exit_data_size)
2698 *image_obj->exit_data_size = exit_data_size;
2699 if (exit_data_size && exit_data) {
2700 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2701 exit_data_size,
2702 (void **)image_obj->exit_data);
2703 if (ret != EFI_SUCCESS)
2704 return ret;
2705 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2706 } else {
2707 image_obj->exit_data = NULL;
2708 }
2709 return EFI_SUCCESS;
2710}
2711
2712/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002713 * efi_exit() - leave an EFI application or driver
2714 * @image_handle: handle of the application or driver that is exiting
2715 * @exit_status: status code
2716 * @exit_data_size: size of the buffer in bytes
2717 * @exit_data: buffer with data describing an error
2718 *
2719 * This function implements the Exit service.
2720 *
2721 * See the Unified Extensible Firmware Interface (UEFI) specification for
2722 * details.
2723 *
2724 * Return: status code
2725 */
2726static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2727 efi_status_t exit_status,
2728 efi_uintn_t exit_data_size,
2729 u16 *exit_data)
2730{
2731 /*
2732 * TODO: We should call the unload procedure of the loaded
2733 * image protocol.
2734 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002735 efi_status_t ret;
2736 void *info;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002737 struct efi_loaded_image_obj *image_obj =
2738 (struct efi_loaded_image_obj *)image_handle;
2739
2740 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2741 exit_data_size, exit_data);
2742
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002743 /* Check parameters */
2744 if (image_handle != current_image)
2745 goto out;
2746 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2747 &info, NULL, NULL,
2748 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2749 if (ret != EFI_SUCCESS)
2750 goto out;
2751
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002752 /* Exit data is only foreseen in case of failure. */
2753 if (exit_status != EFI_SUCCESS) {
2754 ret = efi_update_exit_data(image_obj, exit_data_size,
2755 exit_data);
2756 /* Exiting has priority. Don't return error to caller. */
2757 if (ret != EFI_SUCCESS)
2758 EFI_PRINT("%s: out of memory\n", __func__);
2759 }
2760
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002761 /* Make sure entry/exit counts for EFI world cross-overs match */
2762 EFI_EXIT(exit_status);
2763
2764 /*
2765 * But longjmp out with the U-Boot gd, not the application's, as
2766 * the other end is a setjmp call inside EFI context.
2767 */
2768 efi_restore_gd();
2769
2770 image_obj->exit_status = exit_status;
2771 longjmp(&image_obj->exit_jmp, 1);
2772
2773 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002774out:
2775 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002776}
2777
2778/**
Mario Six8fac2912018-07-10 08:40:17 +02002779 * efi_handle_protocol() - get interface of a protocol on a handle
2780 * @handle: handle on which the protocol shall be opened
2781 * @protocol: GUID of the protocol
2782 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002783 *
2784 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002785 *
2786 * See the Unified Extensible Firmware Interface (UEFI) specification for
2787 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002788 *
Mario Six8fac2912018-07-10 08:40:17 +02002789 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002790 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002791static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002792 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002793 void **protocol_interface)
2794{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002795 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2796 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002797}
2798
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002799/**
Mario Six8fac2912018-07-10 08:40:17 +02002800 * efi_bind_controller() - bind a single driver to a controller
2801 * @controller_handle: controller handle
2802 * @driver_image_handle: driver handle
2803 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002804 *
Mario Six8fac2912018-07-10 08:40:17 +02002805 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002806 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002807static efi_status_t efi_bind_controller(
2808 efi_handle_t controller_handle,
2809 efi_handle_t driver_image_handle,
2810 struct efi_device_path *remain_device_path)
2811{
2812 struct efi_driver_binding_protocol *binding_protocol;
2813 efi_status_t r;
2814
2815 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2816 &efi_guid_driver_binding_protocol,
2817 (void **)&binding_protocol,
2818 driver_image_handle, NULL,
2819 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2820 if (r != EFI_SUCCESS)
2821 return r;
2822 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2823 controller_handle,
2824 remain_device_path));
2825 if (r == EFI_SUCCESS)
2826 r = EFI_CALL(binding_protocol->start(binding_protocol,
2827 controller_handle,
2828 remain_device_path));
2829 EFI_CALL(efi_close_protocol(driver_image_handle,
2830 &efi_guid_driver_binding_protocol,
2831 driver_image_handle, NULL));
2832 return r;
2833}
2834
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002835/**
Mario Six8fac2912018-07-10 08:40:17 +02002836 * efi_connect_single_controller() - connect a single driver to a controller
2837 * @controller_handle: controller
2838 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002839 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002840 *
Mario Six8fac2912018-07-10 08:40:17 +02002841 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002842 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002843static efi_status_t efi_connect_single_controller(
2844 efi_handle_t controller_handle,
2845 efi_handle_t *driver_image_handle,
2846 struct efi_device_path *remain_device_path)
2847{
2848 efi_handle_t *buffer;
2849 size_t count;
2850 size_t i;
2851 efi_status_t r;
2852 size_t connected = 0;
2853
2854 /* Get buffer with all handles with driver binding protocol */
2855 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2856 &efi_guid_driver_binding_protocol,
2857 NULL, &count, &buffer));
2858 if (r != EFI_SUCCESS)
2859 return r;
2860
2861 /* Context Override */
2862 if (driver_image_handle) {
2863 for (; *driver_image_handle; ++driver_image_handle) {
2864 for (i = 0; i < count; ++i) {
2865 if (buffer[i] == *driver_image_handle) {
2866 buffer[i] = NULL;
2867 r = efi_bind_controller(
2868 controller_handle,
2869 *driver_image_handle,
2870 remain_device_path);
2871 /*
2872 * For drivers that do not support the
2873 * controller or are already connected
2874 * we receive an error code here.
2875 */
2876 if (r == EFI_SUCCESS)
2877 ++connected;
2878 }
2879 }
2880 }
2881 }
2882
2883 /*
2884 * TODO: Some overrides are not yet implemented:
2885 * - Platform Driver Override
2886 * - Driver Family Override Search
2887 * - Bus Specific Driver Override
2888 */
2889
2890 /* Driver Binding Search */
2891 for (i = 0; i < count; ++i) {
2892 if (buffer[i]) {
2893 r = efi_bind_controller(controller_handle,
2894 buffer[i],
2895 remain_device_path);
2896 if (r == EFI_SUCCESS)
2897 ++connected;
2898 }
2899 }
2900
2901 efi_free_pool(buffer);
2902 if (!connected)
2903 return EFI_NOT_FOUND;
2904 return EFI_SUCCESS;
2905}
2906
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002907/**
Mario Six8fac2912018-07-10 08:40:17 +02002908 * efi_connect_controller() - connect a controller to a driver
2909 * @controller_handle: handle of the controller
2910 * @driver_image_handle: handle of the driver
2911 * @remain_device_path: device path of a child controller
2912 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002913 *
2914 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002915 *
2916 * See the Unified Extensible Firmware Interface (UEFI) specification for
2917 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002918 *
2919 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002920 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002921 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2922 *
Mario Six8fac2912018-07-10 08:40:17 +02002923 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002924 */
2925static efi_status_t EFIAPI efi_connect_controller(
2926 efi_handle_t controller_handle,
2927 efi_handle_t *driver_image_handle,
2928 struct efi_device_path *remain_device_path,
2929 bool recursive)
2930{
2931 efi_status_t r;
2932 efi_status_t ret = EFI_NOT_FOUND;
2933 struct efi_object *efiobj;
2934
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01002935 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002936 remain_device_path, recursive);
2937
2938 efiobj = efi_search_obj(controller_handle);
2939 if (!efiobj) {
2940 ret = EFI_INVALID_PARAMETER;
2941 goto out;
2942 }
2943
2944 r = efi_connect_single_controller(controller_handle,
2945 driver_image_handle,
2946 remain_device_path);
2947 if (r == EFI_SUCCESS)
2948 ret = EFI_SUCCESS;
2949 if (recursive) {
2950 struct efi_handler *handler;
2951 struct efi_open_protocol_info_item *item;
2952
2953 list_for_each_entry(handler, &efiobj->protocols, link) {
2954 list_for_each_entry(item, &handler->open_infos, link) {
2955 if (item->info.attributes &
2956 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2957 r = EFI_CALL(efi_connect_controller(
2958 item->info.controller_handle,
2959 driver_image_handle,
2960 remain_device_path,
2961 recursive));
2962 if (r == EFI_SUCCESS)
2963 ret = EFI_SUCCESS;
2964 }
2965 }
2966 }
2967 }
2968 /* Check for child controller specified by end node */
2969 if (ret != EFI_SUCCESS && remain_device_path &&
2970 remain_device_path->type == DEVICE_PATH_TYPE_END)
2971 ret = EFI_SUCCESS;
2972out:
2973 return EFI_EXIT(ret);
2974}
2975
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002976/**
Mario Six8fac2912018-07-10 08:40:17 +02002977 * efi_reinstall_protocol_interface() - reinstall protocol interface
2978 * @handle: handle on which the protocol shall be reinstalled
2979 * @protocol: GUID of the protocol to be installed
2980 * @old_interface: interface to be removed
2981 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002982 *
2983 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002984 *
2985 * See the Unified Extensible Firmware Interface (UEFI) specification for
2986 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002987 *
2988 * The old interface is uninstalled. The new interface is installed.
2989 * Drivers are connected.
2990 *
Mario Six8fac2912018-07-10 08:40:17 +02002991 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002992 */
2993static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2994 efi_handle_t handle, const efi_guid_t *protocol,
2995 void *old_interface, void *new_interface)
2996{
2997 efi_status_t ret;
2998
2999 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3000 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003001
3002 /* Uninstall protocol but do not delete handle */
3003 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003004 if (ret != EFI_SUCCESS)
3005 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003006
3007 /* Install the new protocol */
3008 ret = efi_add_protocol(handle, protocol, new_interface);
3009 /*
3010 * The UEFI spec does not specify what should happen to the handle
3011 * if in case of an error no protocol interface remains on the handle.
3012 * So let's do nothing here.
3013 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003014 if (ret != EFI_SUCCESS)
3015 goto out;
3016 /*
3017 * The returned status code has to be ignored.
3018 * Do not create an error if no suitable driver for the handle exists.
3019 */
3020 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3021out:
3022 return EFI_EXIT(ret);
3023}
3024
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003025/**
Mario Six8fac2912018-07-10 08:40:17 +02003026 * efi_get_child_controllers() - get all child controllers associated to a driver
3027 * @efiobj: handle of the controller
3028 * @driver_handle: handle of the driver
3029 * @number_of_children: number of child controllers
3030 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003031 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003032 * The allocated buffer has to be freed with free().
3033 *
Mario Six8fac2912018-07-10 08:40:17 +02003034 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003035 */
3036static efi_status_t efi_get_child_controllers(
3037 struct efi_object *efiobj,
3038 efi_handle_t driver_handle,
3039 efi_uintn_t *number_of_children,
3040 efi_handle_t **child_handle_buffer)
3041{
3042 struct efi_handler *handler;
3043 struct efi_open_protocol_info_item *item;
3044 efi_uintn_t count = 0, i;
3045 bool duplicate;
3046
3047 /* Count all child controller associations */
3048 list_for_each_entry(handler, &efiobj->protocols, link) {
3049 list_for_each_entry(item, &handler->open_infos, link) {
3050 if (item->info.agent_handle == driver_handle &&
3051 item->info.attributes &
3052 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3053 ++count;
3054 }
3055 }
3056 /*
3057 * Create buffer. In case of duplicate child controller assignments
3058 * the buffer will be too large. But that does not harm.
3059 */
3060 *number_of_children = 0;
3061 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3062 if (!*child_handle_buffer)
3063 return EFI_OUT_OF_RESOURCES;
3064 /* Copy unique child handles */
3065 list_for_each_entry(handler, &efiobj->protocols, link) {
3066 list_for_each_entry(item, &handler->open_infos, link) {
3067 if (item->info.agent_handle == driver_handle &&
3068 item->info.attributes &
3069 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3070 /* Check this is a new child controller */
3071 duplicate = false;
3072 for (i = 0; i < *number_of_children; ++i) {
3073 if ((*child_handle_buffer)[i] ==
3074 item->info.controller_handle)
3075 duplicate = true;
3076 }
3077 /* Copy handle to buffer */
3078 if (!duplicate) {
3079 i = (*number_of_children)++;
3080 (*child_handle_buffer)[i] =
3081 item->info.controller_handle;
3082 }
3083 }
3084 }
3085 }
3086 return EFI_SUCCESS;
3087}
3088
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003089/**
Mario Six8fac2912018-07-10 08:40:17 +02003090 * efi_disconnect_controller() - disconnect a controller from a driver
3091 * @controller_handle: handle of the controller
3092 * @driver_image_handle: handle of the driver
3093 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003094 *
3095 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003096 *
3097 * See the Unified Extensible Firmware Interface (UEFI) specification for
3098 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003099 *
Mario Six8fac2912018-07-10 08:40:17 +02003100 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003101 */
3102static efi_status_t EFIAPI efi_disconnect_controller(
3103 efi_handle_t controller_handle,
3104 efi_handle_t driver_image_handle,
3105 efi_handle_t child_handle)
3106{
3107 struct efi_driver_binding_protocol *binding_protocol;
3108 efi_handle_t *child_handle_buffer = NULL;
3109 size_t number_of_children = 0;
3110 efi_status_t r;
3111 size_t stop_count = 0;
3112 struct efi_object *efiobj;
3113
3114 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3115 child_handle);
3116
3117 efiobj = efi_search_obj(controller_handle);
3118 if (!efiobj) {
3119 r = EFI_INVALID_PARAMETER;
3120 goto out;
3121 }
3122
3123 if (child_handle && !efi_search_obj(child_handle)) {
3124 r = EFI_INVALID_PARAMETER;
3125 goto out;
3126 }
3127
3128 /* If no driver handle is supplied, disconnect all drivers */
3129 if (!driver_image_handle) {
3130 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3131 goto out;
3132 }
3133
3134 /* Create list of child handles */
3135 if (child_handle) {
3136 number_of_children = 1;
3137 child_handle_buffer = &child_handle;
3138 } else {
3139 efi_get_child_controllers(efiobj,
3140 driver_image_handle,
3141 &number_of_children,
3142 &child_handle_buffer);
3143 }
3144
3145 /* Get the driver binding protocol */
3146 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3147 &efi_guid_driver_binding_protocol,
3148 (void **)&binding_protocol,
3149 driver_image_handle, NULL,
3150 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3151 if (r != EFI_SUCCESS)
3152 goto out;
3153 /* Remove the children */
3154 if (number_of_children) {
3155 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3156 controller_handle,
3157 number_of_children,
3158 child_handle_buffer));
3159 if (r == EFI_SUCCESS)
3160 ++stop_count;
3161 }
3162 /* Remove the driver */
3163 if (!child_handle)
3164 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3165 controller_handle,
3166 0, NULL));
3167 if (r == EFI_SUCCESS)
3168 ++stop_count;
3169 EFI_CALL(efi_close_protocol(driver_image_handle,
3170 &efi_guid_driver_binding_protocol,
3171 driver_image_handle, NULL));
3172
3173 if (stop_count)
3174 r = EFI_SUCCESS;
3175 else
3176 r = EFI_NOT_FOUND;
3177out:
3178 if (!child_handle)
3179 free(child_handle_buffer);
3180 return EFI_EXIT(r);
3181}
3182
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003183static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003184 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003185 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3186 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003187 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003188 },
3189 .raise_tpl = efi_raise_tpl,
3190 .restore_tpl = efi_restore_tpl,
3191 .allocate_pages = efi_allocate_pages_ext,
3192 .free_pages = efi_free_pages_ext,
3193 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003194 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003195 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003196 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003197 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003198 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003199 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003200 .close_event = efi_close_event,
3201 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003202 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003203 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003204 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003205 .handle_protocol = efi_handle_protocol,
3206 .reserved = NULL,
3207 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003208 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003209 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003210 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003211 .load_image = efi_load_image,
3212 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003213 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003214 .unload_image = efi_unload_image,
3215 .exit_boot_services = efi_exit_boot_services,
3216 .get_next_monotonic_count = efi_get_next_monotonic_count,
3217 .stall = efi_stall,
3218 .set_watchdog_timer = efi_set_watchdog_timer,
3219 .connect_controller = efi_connect_controller,
3220 .disconnect_controller = efi_disconnect_controller,
3221 .open_protocol = efi_open_protocol,
3222 .close_protocol = efi_close_protocol,
3223 .open_protocol_information = efi_open_protocol_information,
3224 .protocols_per_handle = efi_protocols_per_handle,
3225 .locate_handle_buffer = efi_locate_handle_buffer,
3226 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003227 .install_multiple_protocol_interfaces =
3228 efi_install_multiple_protocol_interfaces,
3229 .uninstall_multiple_protocol_interfaces =
3230 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003231 .calculate_crc32 = efi_calculate_crc32,
3232 .copy_mem = efi_copy_mem,
3233 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003234 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003235};
3236
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003237static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003238
Alexander Graf393dd912016-10-14 13:45:30 +02003239struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003240 .hdr = {
3241 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003242 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003243 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003244 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003245 .fw_vendor = firmware_vendor,
3246 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003247 .con_in = (void *)&efi_con_in,
3248 .con_out = (void *)&efi_con_out,
3249 .std_err = (void *)&efi_con_out,
3250 .runtime = (void *)&efi_runtime_services,
3251 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003252 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003253 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003254};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003255
3256/**
3257 * efi_initialize_system_table() - Initialize system table
3258 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003259 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003260 */
3261efi_status_t efi_initialize_system_table(void)
3262{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003263 efi_status_t ret;
3264
3265 /* Allocate configuration table array */
3266 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3267 EFI_MAX_CONFIGURATION_TABLES *
3268 sizeof(struct efi_configuration_table),
3269 (void **)&systab.tables);
3270
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003271 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003272 efi_update_table_header_crc32(&systab.hdr);
3273 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3274 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003275
3276 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003277}