blob: b1c0007e5a34a12f9007ab651703416930ec38a9 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020022
Alexander Grafbee91162016-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 Schuchardt43bce442018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010028
Alexander Graff31239a2018-11-15 21:23:47 +010029/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
Simon Glass65e4c0b2016-09-25 15:27:35 -060037#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010038/*
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
43 */
44static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060045#endif
Alexander Grafbee91162016-03-04 01:09:59 +010046
Rob Clarkc160d2f2017-07-27 08:04:18 -040047static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040048static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010049/* GUID of the device tree table */
50const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010051/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
52const efi_guid_t efi_guid_driver_binding_protocol =
53 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040054
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010055/* event group ExitBootServices() invoked */
56const efi_guid_t efi_guid_event_group_exit_boot_services =
57 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
58/* event group SetVirtualAddressMap() invoked */
59const efi_guid_t efi_guid_event_group_virtual_address_change =
60 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
61/* event group memory map changed */
62const efi_guid_t efi_guid_event_group_memory_map_change =
63 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
64/* event group boot manager about to boot */
65const efi_guid_t efi_guid_event_group_ready_to_boot =
66 EFI_EVENT_GROUP_READY_TO_BOOT;
67/* event group ResetSystem() invoked (before ExitBootServices) */
68const efi_guid_t efi_guid_event_group_reset_system =
69 EFI_EVENT_GROUP_RESET_SYSTEM;
70
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010071static efi_status_t EFIAPI efi_disconnect_controller(
72 efi_handle_t controller_handle,
73 efi_handle_t driver_image_handle,
74 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010075
Rob Clarkc160d2f2017-07-27 08:04:18 -040076/* Called on every callback entry */
77int __efi_entry_check(void)
78{
79 int ret = entry_count++ == 0;
80#ifdef CONFIG_ARM
81 assert(efi_gd);
82 app_gd = gd;
83 gd = efi_gd;
84#endif
85 return ret;
86}
87
88/* Called on every callback exit */
89int __efi_exit_check(void)
90{
91 int ret = --entry_count == 0;
92#ifdef CONFIG_ARM
93 gd = app_gd;
94#endif
95 return ret;
96}
97
Alexander Grafbee91162016-03-04 01:09:59 +010098/* Called from do_bootefi_exec() */
99void efi_save_gd(void)
100{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600101#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100102 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600103#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100104}
105
Rob Clarkc160d2f2017-07-27 08:04:18 -0400106/*
Mario Six78a88f72018-07-10 08:40:17 +0200107 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200108 * world so we can dump out an abort message, without any care about returning
109 * back to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400110 */
Alexander Grafbee91162016-03-04 01:09:59 +0100111void efi_restore_gd(void)
112{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600113#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100114 /* Only restore if we're already in EFI context */
115 if (!efi_gd)
116 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100117 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600118#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100119}
120
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200121/**
Mario Six78a88f72018-07-10 08:40:17 +0200122 * indent_string() - returns a string for indenting with two spaces per level
123 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100124 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200125 * A maximum of ten indent levels is supported. Higher indent levels will be
126 * truncated.
127 *
Mario Six78a88f72018-07-10 08:40:17 +0200128 * Return: A string for indenting with two spaces per level is
129 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400130 */
131static const char *indent_string(int level)
132{
133 const char *indent = " ";
134 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100135
Rob Clarkaf65db82017-07-27 08:04:19 -0400136 level = min(max, level * 2);
137 return &indent[max - level];
138}
139
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200140const char *__efi_nesting(void)
141{
142 return indent_string(nesting_level);
143}
144
Rob Clarkaf65db82017-07-27 08:04:19 -0400145const char *__efi_nesting_inc(void)
146{
147 return indent_string(nesting_level++);
148}
149
150const char *__efi_nesting_dec(void)
151{
152 return indent_string(--nesting_level);
153}
154
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200155/**
Mario Six78a88f72018-07-10 08:40:17 +0200156 * efi_queue_event() - queue an EFI event
157 * @event: event to signal
158 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 *
160 * This function queues the notification function of the event for future
161 * execution.
162 *
Mario Six78a88f72018-07-10 08:40:17 +0200163 * The notification function is called if the task priority level of the event
164 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200165 *
166 * For the SignalEvent service see efi_signal_event_ext.
167 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200168 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100169static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200170{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200171 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200172 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100174 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200175 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200176 EFI_CALL_VOID(event->notify_function(event,
177 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200178 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200179 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200180}
181
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200182/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200183 * is_valid_tpl() - check if the task priority level is valid
184 *
185 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200186 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200187 */
188efi_status_t is_valid_tpl(efi_uintn_t tpl)
189{
190 switch (tpl) {
191 case TPL_APPLICATION:
192 case TPL_CALLBACK:
193 case TPL_NOTIFY:
194 case TPL_HIGH_LEVEL:
195 return EFI_SUCCESS;
196 default:
197 return EFI_INVALID_PARAMETER;
198 }
199}
200
201/**
Mario Six78a88f72018-07-10 08:40:17 +0200202 * efi_signal_event() - signal an EFI event
203 * @event: event to signal
204 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100205 *
Mario Six78a88f72018-07-10 08:40:17 +0200206 * This function signals an event. If the event belongs to an event group all
207 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100208 * their notification function is queued.
209 *
210 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100211 */
212void efi_signal_event(struct efi_event *event, bool check_tpl)
213{
214 if (event->group) {
215 struct efi_event *evt;
216
217 /*
218 * The signaled state has to set before executing any
219 * notification function
220 */
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
223 continue;
224 if (evt->is_signaled)
225 continue;
226 evt->is_signaled = true;
227 if (evt->type & EVT_NOTIFY_SIGNAL &&
228 evt->notify_function)
229 evt->is_queued = true;
230 }
231 list_for_each_entry(evt, &efi_events, link) {
232 if (!evt->group || guidcmp(evt->group, event->group))
233 continue;
234 if (evt->is_queued)
235 efi_queue_event(evt, check_tpl);
236 }
237 } else if (!event->is_signaled) {
238 event->is_signaled = true;
239 if (event->type & EVT_NOTIFY_SIGNAL)
240 efi_queue_event(event, check_tpl);
241 }
242}
243
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200244/**
Mario Six78a88f72018-07-10 08:40:17 +0200245 * efi_raise_tpl() - raise the task priority level
246 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200247 *
248 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200249 *
Mario Six78a88f72018-07-10 08:40:17 +0200250 * See the Unified Extensible Firmware Interface (UEFI) specification for
251 * details.
252 *
253 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200254 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100255static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100256{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100257 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200258
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200259 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200260
261 if (new_tpl < efi_tpl)
262 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
263 efi_tpl = new_tpl;
264 if (efi_tpl > TPL_HIGH_LEVEL)
265 efi_tpl = TPL_HIGH_LEVEL;
266
267 EFI_EXIT(EFI_SUCCESS);
268 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100269}
270
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200271/**
Mario Six78a88f72018-07-10 08:40:17 +0200272 * efi_restore_tpl() - lower the task priority level
273 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200274 *
275 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200276 *
Mario Six78a88f72018-07-10 08:40:17 +0200277 * See the Unified Extensible Firmware Interface (UEFI) specification for
278 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200279 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100280static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100281{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200282 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200283
284 if (old_tpl > efi_tpl)
285 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
286 efi_tpl = old_tpl;
287 if (efi_tpl > TPL_HIGH_LEVEL)
288 efi_tpl = TPL_HIGH_LEVEL;
289
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100290 /*
291 * Lowering the TPL may have made queued events eligible for execution.
292 */
293 efi_timer_check();
294
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200295 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100296}
297
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200298/**
Mario Six78a88f72018-07-10 08:40:17 +0200299 * efi_allocate_pages_ext() - allocate memory pages
300 * @type: type of allocation to be performed
301 * @memory_type: usage type of the allocated memory
302 * @pages: number of pages to be allocated
303 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200304 *
305 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200306 *
Mario Six78a88f72018-07-10 08:40:17 +0200307 * See the Unified Extensible Firmware Interface (UEFI) specification for
308 * details.
309 *
310 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200311 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900312static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100313 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900314 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100315{
316 efi_status_t r;
317
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100318 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100319 r = efi_allocate_pages(type, memory_type, pages, memory);
320 return EFI_EXIT(r);
321}
322
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200323/**
Mario Six78a88f72018-07-10 08:40:17 +0200324 * efi_free_pages_ext() - Free memory pages.
325 * @memory: start of the memory area to be freed
326 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200327 *
328 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200329 *
Mario Six78a88f72018-07-10 08:40:17 +0200330 * See the Unified Extensible Firmware Interface (UEFI) specification for
331 * details.
332 *
333 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200334 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900335static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100336 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100337{
338 efi_status_t r;
339
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900340 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100341 r = efi_free_pages(memory, pages);
342 return EFI_EXIT(r);
343}
344
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200345/**
Mario Six78a88f72018-07-10 08:40:17 +0200346 * efi_get_memory_map_ext() - get map describing memory usage
347 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
348 * on exit the size of the copied memory map
349 * @memory_map: buffer to which the memory map is written
350 * @map_key: key for the memory map
351 * @descriptor_size: size of an individual memory descriptor
352 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200353 *
354 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200355 *
Mario Six78a88f72018-07-10 08:40:17 +0200356 * See the Unified Extensible Firmware Interface (UEFI) specification for
357 * details.
358 *
359 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200360 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900361static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100362 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900363 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100364 efi_uintn_t *map_key,
365 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900366 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100367{
368 efi_status_t r;
369
370 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
371 map_key, descriptor_size, descriptor_version);
372 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
373 descriptor_size, descriptor_version);
374 return EFI_EXIT(r);
375}
376
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200377/**
Mario Six78a88f72018-07-10 08:40:17 +0200378 * efi_allocate_pool_ext() - allocate memory from pool
379 * @pool_type: type of the pool from which memory is to be allocated
380 * @size: number of bytes to be allocated
381 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200382 *
383 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200384 *
Mario Six78a88f72018-07-10 08:40:17 +0200385 * See the Unified Extensible Firmware Interface (UEFI) specification for
386 * details.
387 *
388 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200389 */
Stefan Brünsead12742016-10-09 22:17:18 +0200390static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100391 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200392 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100393{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100394 efi_status_t r;
395
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100396 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200397 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100398 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100399}
400
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200401/**
Mario Six78a88f72018-07-10 08:40:17 +0200402 * efi_free_pool_ext() - free memory from pool
403 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200404 *
405 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200406 *
Mario Six78a88f72018-07-10 08:40:17 +0200407 * See the Unified Extensible Firmware Interface (UEFI) specification for
408 * details.
409 *
410 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200411 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200412static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100413{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100414 efi_status_t r;
415
416 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200417 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100418 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100419}
420
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200421/**
Mario Six78a88f72018-07-10 08:40:17 +0200422 * efi_add_handle() - add a new object to the object list
423 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100424 *
Mario Six78a88f72018-07-10 08:40:17 +0200425 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100426 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200427void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100428{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200429 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100430 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200431 INIT_LIST_HEAD(&handle->protocols);
432 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100433}
434
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200435/**
Mario Six78a88f72018-07-10 08:40:17 +0200436 * efi_create_handle() - create handle
437 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200438 *
Mario Six78a88f72018-07-10 08:40:17 +0200439 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200440 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100441efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200442{
443 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200444
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200445 obj = calloc(1, sizeof(struct efi_object));
446 if (!obj)
447 return EFI_OUT_OF_RESOURCES;
448
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100449 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200450 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200451
452 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200453}
454
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200455/**
Mario Six78a88f72018-07-10 08:40:17 +0200456 * efi_search_protocol() - find a protocol on a handle.
457 * @handle: handle
458 * @protocol_guid: GUID of the protocol
459 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100460 *
Mario Six78a88f72018-07-10 08:40:17 +0200461 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100462 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100463efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100464 const efi_guid_t *protocol_guid,
465 struct efi_handler **handler)
466{
467 struct efi_object *efiobj;
468 struct list_head *lhandle;
469
470 if (!handle || !protocol_guid)
471 return EFI_INVALID_PARAMETER;
472 efiobj = efi_search_obj(handle);
473 if (!efiobj)
474 return EFI_INVALID_PARAMETER;
475 list_for_each(lhandle, &efiobj->protocols) {
476 struct efi_handler *protocol;
477
478 protocol = list_entry(lhandle, struct efi_handler, link);
479 if (!guidcmp(protocol->guid, protocol_guid)) {
480 if (handler)
481 *handler = protocol;
482 return EFI_SUCCESS;
483 }
484 }
485 return EFI_NOT_FOUND;
486}
487
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200488/**
Mario Six78a88f72018-07-10 08:40:17 +0200489 * efi_remove_protocol() - delete protocol from a handle
490 * @handle: handle from which the protocol shall be deleted
491 * @protocol: GUID of the protocol to be deleted
492 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100493 *
Mario Six78a88f72018-07-10 08:40:17 +0200494 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100495 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100496efi_status_t efi_remove_protocol(const efi_handle_t handle,
497 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100498 void *protocol_interface)
499{
500 struct efi_handler *handler;
501 efi_status_t ret;
502
503 ret = efi_search_protocol(handle, protocol, &handler);
504 if (ret != EFI_SUCCESS)
505 return ret;
506 if (guidcmp(handler->guid, protocol))
507 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200508 if (handler->protocol_interface != protocol_interface)
509 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100510 list_del(&handler->link);
511 free(handler);
512 return EFI_SUCCESS;
513}
514
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200515/**
Mario Six78a88f72018-07-10 08:40:17 +0200516 * efi_remove_all_protocols() - delete all protocols from a handle
517 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100518 *
Mario Six78a88f72018-07-10 08:40:17 +0200519 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100520 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100521efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100522{
523 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100524 struct efi_handler *protocol;
525 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100526
527 efiobj = efi_search_obj(handle);
528 if (!efiobj)
529 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100530 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100531 efi_status_t ret;
532
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100533 ret = efi_remove_protocol(handle, protocol->guid,
534 protocol->protocol_interface);
535 if (ret != EFI_SUCCESS)
536 return ret;
537 }
538 return EFI_SUCCESS;
539}
540
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200541/**
Mario Six78a88f72018-07-10 08:40:17 +0200542 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100543 *
Mario Six78a88f72018-07-10 08:40:17 +0200544 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100545 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200546void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100547{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200548 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100549 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200550 efi_remove_all_protocols(handle);
551 list_del(&handle->link);
552 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100553}
554
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200555/**
Mario Six78a88f72018-07-10 08:40:17 +0200556 * efi_is_event() - check if a pointer is a valid event
557 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100558 *
Mario Six78a88f72018-07-10 08:40:17 +0200559 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100560 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100561static efi_status_t efi_is_event(const struct efi_event *event)
562{
563 const struct efi_event *evt;
564
565 if (!event)
566 return EFI_INVALID_PARAMETER;
567 list_for_each_entry(evt, &efi_events, link) {
568 if (evt == event)
569 return EFI_SUCCESS;
570 }
571 return EFI_INVALID_PARAMETER;
572}
Alexander Grafbee91162016-03-04 01:09:59 +0100573
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200574/**
Mario Six78a88f72018-07-10 08:40:17 +0200575 * efi_create_event() - create an event
576 * @type: type of the event to create
577 * @notify_tpl: task priority level of the event
578 * @notify_function: notification function of the event
579 * @notify_context: pointer passed to the notification function
580 * @group: event group
581 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200582 *
583 * This function is used inside U-Boot code to create an event.
584 *
585 * For the API function implementing the CreateEvent service see
586 * efi_create_event_ext.
587 *
Mario Six78a88f72018-07-10 08:40:17 +0200588 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200589 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100590efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200591 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200592 struct efi_event *event,
593 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100594 void *notify_context, efi_guid_t *group,
595 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100596{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100597 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200598
Jonathan Graya95343b2017-03-12 19:26:07 +1100599 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200600 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100601
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200602 switch (type) {
603 case 0:
604 case EVT_TIMER:
605 case EVT_NOTIFY_SIGNAL:
606 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
607 case EVT_NOTIFY_WAIT:
608 case EVT_TIMER | EVT_NOTIFY_WAIT:
609 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
610 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
611 break;
612 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200613 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200614 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100615
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900616 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
617 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200618 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100619
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100620 evt = calloc(1, sizeof(struct efi_event));
621 if (!evt)
622 return EFI_OUT_OF_RESOURCES;
623 evt->type = type;
624 evt->notify_tpl = notify_tpl;
625 evt->notify_function = notify_function;
626 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100627 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200628 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100629 evt->trigger_next = -1ULL;
630 evt->is_queued = false;
631 evt->is_signaled = false;
632 list_add_tail(&evt->link, &efi_events);
633 *event = evt;
634 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100635}
636
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200637/*
Mario Six78a88f72018-07-10 08:40:17 +0200638 * efi_create_event_ex() - create an event in a group
639 * @type: type of the event to create
640 * @notify_tpl: task priority level of the event
641 * @notify_function: notification function of the event
642 * @notify_context: pointer passed to the notification function
643 * @event: created event
644 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100645 *
646 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100647 *
Mario Six78a88f72018-07-10 08:40:17 +0200648 * See the Unified Extensible Firmware Interface (UEFI) specification for
649 * details.
650 *
651 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100652 */
653efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
654 void (EFIAPI *notify_function) (
655 struct efi_event *event,
656 void *context),
657 void *notify_context,
658 efi_guid_t *event_group,
659 struct efi_event **event)
660{
661 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
662 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100663 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100664 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100665}
666
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200667/**
Mario Six78a88f72018-07-10 08:40:17 +0200668 * efi_create_event_ext() - create an event
669 * @type: type of the event to create
670 * @notify_tpl: task priority level of the event
671 * @notify_function: notification function of the event
672 * @notify_context: pointer passed to the notification function
673 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200674 *
675 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200676 *
Mario Six78a88f72018-07-10 08:40:17 +0200677 * See the Unified Extensible Firmware Interface (UEFI) specification for
678 * details.
679 *
680 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200681 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200682static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100683 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200684 void (EFIAPI *notify_function) (
685 struct efi_event *event,
686 void *context),
687 void *notify_context, struct efi_event **event)
688{
689 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
690 notify_context);
691 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100692 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200693}
694
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200695/**
Mario Six78a88f72018-07-10 08:40:17 +0200696 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200697 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200698 * Check if a timer event has occurred or a queued notification function should
699 * be called.
700 *
Alexander Grafbee91162016-03-04 01:09:59 +0100701 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200702 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100703 */
704void efi_timer_check(void)
705{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100706 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100707 u64 now = timer_get_us();
708
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100709 list_for_each_entry(evt, &efi_events, link) {
710 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100711 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100712 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200713 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100714 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200715 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100716 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200717 break;
718 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100719 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200720 break;
721 default:
722 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200723 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100724 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100725 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100726 }
Alexander Grafbee91162016-03-04 01:09:59 +0100727 WATCHDOG_RESET();
728}
729
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200730/**
Mario Six78a88f72018-07-10 08:40:17 +0200731 * efi_set_timer() - set the trigger time for a timer event or stop the event
732 * @event: event for which the timer is set
733 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200734 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200735 *
736 * This is the function for internal usage in U-Boot. For the API function
737 * implementing the SetTimer service see efi_set_timer_ext.
738 *
Mario Six78a88f72018-07-10 08:40:17 +0200739 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200740 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200741efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200742 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100743{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100744 /* Check that the event is valid */
745 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
746 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100747
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200748 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200749 * The parameter defines a multiple of 100 ns.
750 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200751 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200752 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100753
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100754 switch (type) {
755 case EFI_TIMER_STOP:
756 event->trigger_next = -1ULL;
757 break;
758 case EFI_TIMER_PERIODIC:
759 case EFI_TIMER_RELATIVE:
760 event->trigger_next = timer_get_us() + trigger_time;
761 break;
762 default:
763 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100764 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100765 event->trigger_type = type;
766 event->trigger_time = trigger_time;
767 event->is_signaled = false;
768 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200769}
770
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200771/**
Mario Six78a88f72018-07-10 08:40:17 +0200772 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
773 * event
774 * @event: event for which the timer is set
775 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200776 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200777 *
778 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200779 *
Mario Six78a88f72018-07-10 08:40:17 +0200780 * See the Unified Extensible Firmware Interface (UEFI) specification for
781 * details.
782 *
783 *
784 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200785 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200786static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
787 enum efi_timer_delay type,
788 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200789{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900790 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200791 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100792}
793
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200794/**
Mario Six78a88f72018-07-10 08:40:17 +0200795 * efi_wait_for_event() - wait for events to be signaled
796 * @num_events: number of events to be waited for
797 * @event: events to be waited for
798 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200799 *
800 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200801 *
Mario Six78a88f72018-07-10 08:40:17 +0200802 * See the Unified Extensible Firmware Interface (UEFI) specification for
803 * details.
804 *
805 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200806 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100807static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200808 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100809 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100810{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100811 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100812
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100813 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100814
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200815 /* Check parameters */
816 if (!num_events || !event)
817 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200818 /* Check TPL */
819 if (efi_tpl != TPL_APPLICATION)
820 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200821 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100822 if (efi_is_event(event[i]) != EFI_SUCCESS)
823 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200824 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
825 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200826 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100827 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200828 }
829
830 /* Wait for signal */
831 for (;;) {
832 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200833 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200834 goto out;
835 }
836 /* Allow events to occur. */
837 efi_timer_check();
838 }
839
840out:
841 /*
842 * Reset the signal which is passed to the caller to allow periodic
843 * events to occur.
844 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200845 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200846 if (index)
847 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100848
849 return EFI_EXIT(EFI_SUCCESS);
850}
851
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200852/**
Mario Six78a88f72018-07-10 08:40:17 +0200853 * efi_signal_event_ext() - signal an EFI event
854 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200855 *
856 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200857 *
858 * See the Unified Extensible Firmware Interface (UEFI) specification for
859 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200860 *
861 * This functions sets the signaled state of the event and queues the
862 * notification function for execution.
863 *
Mario Six78a88f72018-07-10 08:40:17 +0200864 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200865 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200866static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100867{
868 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100869 if (efi_is_event(event) != EFI_SUCCESS)
870 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100871 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100872 return EFI_EXIT(EFI_SUCCESS);
873}
874
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200875/**
Mario Six78a88f72018-07-10 08:40:17 +0200876 * efi_close_event() - close an EFI event
877 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200878 *
879 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200880 *
Mario Six78a88f72018-07-10 08:40:17 +0200881 * See the Unified Extensible Firmware Interface (UEFI) specification for
882 * details.
883 *
884 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200885 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200886static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100887{
888 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100889 if (efi_is_event(event) != EFI_SUCCESS)
890 return EFI_EXIT(EFI_INVALID_PARAMETER);
891 list_del(&event->link);
892 free(event);
893 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100894}
895
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200896/**
Mario Six78a88f72018-07-10 08:40:17 +0200897 * efi_check_event() - check if an event is signaled
898 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200899 *
900 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200901 *
Mario Six78a88f72018-07-10 08:40:17 +0200902 * See the Unified Extensible Firmware Interface (UEFI) specification for
903 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200904 *
Mario Six78a88f72018-07-10 08:40:17 +0200905 * If an event is not signaled yet, the notification function is queued. The
906 * signaled state is cleared.
907 *
908 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200909 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200910static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100911{
912 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200913 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100914 if (efi_is_event(event) != EFI_SUCCESS ||
915 event->type & EVT_NOTIFY_SIGNAL)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
917 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100918 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100919 if (event->is_signaled) {
920 event->is_signaled = false;
921 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200922 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100923 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100924}
925
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200926/**
Mario Six78a88f72018-07-10 08:40:17 +0200927 * efi_search_obj() - find the internal EFI object for a handle
928 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200929 *
Mario Six78a88f72018-07-10 08:40:17 +0200930 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200931 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100932struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200933{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100934 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200935
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100936 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200937 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200938 return efiobj;
939 }
940
941 return NULL;
942}
943
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200944/**
Mario Six78a88f72018-07-10 08:40:17 +0200945 * efi_open_protocol_info_entry() - create open protocol info entry and add it
946 * to a protocol
947 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100948 *
Mario Six78a88f72018-07-10 08:40:17 +0200949 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100950 */
951static struct efi_open_protocol_info_entry *efi_create_open_info(
952 struct efi_handler *handler)
953{
954 struct efi_open_protocol_info_item *item;
955
956 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
957 if (!item)
958 return NULL;
959 /* Append the item to the open protocol info list. */
960 list_add_tail(&item->link, &handler->open_infos);
961
962 return &item->info;
963}
964
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200965/**
Mario Six78a88f72018-07-10 08:40:17 +0200966 * efi_delete_open_info() - remove an open protocol info entry from a protocol
967 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100968 *
Mario Six78a88f72018-07-10 08:40:17 +0200969 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100970 */
971static efi_status_t efi_delete_open_info(
972 struct efi_open_protocol_info_item *item)
973{
974 list_del(&item->link);
975 free(item);
976 return EFI_SUCCESS;
977}
978
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200979/**
Mario Six78a88f72018-07-10 08:40:17 +0200980 * efi_add_protocol() - install new protocol on a handle
981 * @handle: handle on which the protocol shall be installed
982 * @protocol: GUID of the protocol to be installed
983 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200984 *
Mario Six78a88f72018-07-10 08:40:17 +0200985 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200986 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100987efi_status_t efi_add_protocol(const efi_handle_t handle,
988 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200989 void *protocol_interface)
990{
991 struct efi_object *efiobj;
992 struct efi_handler *handler;
993 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200994
995 efiobj = efi_search_obj(handle);
996 if (!efiobj)
997 return EFI_INVALID_PARAMETER;
998 ret = efi_search_protocol(handle, protocol, NULL);
999 if (ret != EFI_NOT_FOUND)
1000 return EFI_INVALID_PARAMETER;
1001 handler = calloc(1, sizeof(struct efi_handler));
1002 if (!handler)
1003 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001004 handler->guid = protocol;
1005 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001006 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001007 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001008 if (!guidcmp(&efi_guid_device_path, protocol))
1009 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001010 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001011}
1012
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001013/**
Mario Six78a88f72018-07-10 08:40:17 +02001014 * efi_install_protocol_interface() - install protocol interface
1015 * @handle: handle on which the protocol shall be installed
1016 * @protocol: GUID of the protocol to be installed
1017 * @protocol_interface_type: type of the interface to be installed,
1018 * always EFI_NATIVE_INTERFACE
1019 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001020 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001021 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001022 *
Mario Six78a88f72018-07-10 08:40:17 +02001023 * See the Unified Extensible Firmware Interface (UEFI) specification for
1024 * details.
1025 *
1026 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001027 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001028static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001029 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001030 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001031{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001032 efi_status_t r;
1033
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001034 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1035 protocol_interface);
1036
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001037 if (!handle || !protocol ||
1038 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1039 r = EFI_INVALID_PARAMETER;
1040 goto out;
1041 }
1042
1043 /* Create new handle if requested. */
1044 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001045 r = efi_create_handle(handle);
1046 if (r != EFI_SUCCESS)
1047 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001048 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1049 *handle);
1050 } else {
1051 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1052 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001053 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001054 /* Add new protocol */
1055 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001056out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001057 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001058}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001059
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001060/**
Mario Six78a88f72018-07-10 08:40:17 +02001061 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001062 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001063 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001064 * @number_of_drivers: number of child controllers
1065 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001066 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001067 * The allocated buffer has to be freed with free().
1068 *
Mario Six78a88f72018-07-10 08:40:17 +02001069 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001070 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001071static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001072 const efi_guid_t *protocol,
1073 efi_uintn_t *number_of_drivers,
1074 efi_handle_t **driver_handle_buffer)
1075{
1076 struct efi_handler *handler;
1077 struct efi_open_protocol_info_item *item;
1078 efi_uintn_t count = 0, i;
1079 bool duplicate;
1080
1081 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001082 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001083 if (protocol && guidcmp(handler->guid, protocol))
1084 continue;
1085 list_for_each_entry(item, &handler->open_infos, link) {
1086 if (item->info.attributes &
1087 EFI_OPEN_PROTOCOL_BY_DRIVER)
1088 ++count;
1089 }
1090 }
1091 /*
1092 * Create buffer. In case of duplicate driver assignments the buffer
1093 * will be too large. But that does not harm.
1094 */
1095 *number_of_drivers = 0;
1096 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1097 if (!*driver_handle_buffer)
1098 return EFI_OUT_OF_RESOURCES;
1099 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001100 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001101 if (protocol && guidcmp(handler->guid, protocol))
1102 continue;
1103 list_for_each_entry(item, &handler->open_infos, link) {
1104 if (item->info.attributes &
1105 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1106 /* Check this is a new driver */
1107 duplicate = false;
1108 for (i = 0; i < *number_of_drivers; ++i) {
1109 if ((*driver_handle_buffer)[i] ==
1110 item->info.agent_handle)
1111 duplicate = true;
1112 }
1113 /* Copy handle to buffer */
1114 if (!duplicate) {
1115 i = (*number_of_drivers)++;
1116 (*driver_handle_buffer)[i] =
1117 item->info.agent_handle;
1118 }
1119 }
1120 }
1121 }
1122 return EFI_SUCCESS;
1123}
1124
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001125/**
Mario Six78a88f72018-07-10 08:40:17 +02001126 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001127 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001128 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001129 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001130 *
1131 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001132 *
Mario Six78a88f72018-07-10 08:40:17 +02001133 * See the Unified Extensible Firmware Interface (UEFI) specification for
1134 * details.
1135 *
1136 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001137 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001138static efi_status_t efi_disconnect_all_drivers
1139 (efi_handle_t handle,
1140 const efi_guid_t *protocol,
1141 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001142{
1143 efi_uintn_t number_of_drivers;
1144 efi_handle_t *driver_handle_buffer;
1145 efi_status_t r, ret;
1146
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001147 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001148 &driver_handle_buffer);
1149 if (ret != EFI_SUCCESS)
1150 return ret;
1151
1152 ret = EFI_NOT_FOUND;
1153 while (number_of_drivers) {
1154 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001155 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001156 driver_handle_buffer[--number_of_drivers],
1157 child_handle));
1158 if (r == EFI_SUCCESS)
1159 ret = r;
1160 }
1161 free(driver_handle_buffer);
1162 return ret;
1163}
1164
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001165/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001166 * efi_uninstall_protocol() - uninstall protocol interface
1167 *
Mario Six78a88f72018-07-10 08:40:17 +02001168 * @handle: handle from which the protocol shall be removed
1169 * @protocol: GUID of the protocol to be removed
1170 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001171 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001172 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001173 *
1174 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001175 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001176static efi_status_t efi_uninstall_protocol
1177 (efi_handle_t handle, const efi_guid_t *protocol,
1178 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001179{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001180 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001181 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001182 struct efi_open_protocol_info_item *item;
1183 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001184 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001185
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001186 /* Check handle */
1187 efiobj = efi_search_obj(handle);
1188 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001189 r = EFI_INVALID_PARAMETER;
1190 goto out;
1191 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001192 /* Find the protocol on the handle */
1193 r = efi_search_protocol(handle, protocol, &handler);
1194 if (r != EFI_SUCCESS)
1195 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001196 /* Disconnect controllers */
1197 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1198 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001199 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001200 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001201 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001202 /* Close protocol */
1203 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1204 if (item->info.attributes ==
1205 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1206 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1207 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1208 list_del(&item->link);
1209 }
1210 if (!list_empty(&handler->open_infos)) {
1211 r = EFI_ACCESS_DENIED;
1212 goto out;
1213 }
1214 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001215out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001216 return r;
1217}
1218
1219/**
1220 * efi_uninstall_protocol_interface() - uninstall protocol interface
1221 * @handle: handle from which the protocol shall be removed
1222 * @protocol: GUID of the protocol to be removed
1223 * @protocol_interface: interface to be removed
1224 *
1225 * This function implements the UninstallProtocolInterface service.
1226 *
1227 * See the Unified Extensible Firmware Interface (UEFI) specification for
1228 * details.
1229 *
1230 * Return: status code
1231 */
1232static efi_status_t EFIAPI efi_uninstall_protocol_interface
1233 (efi_handle_t handle, const efi_guid_t *protocol,
1234 void *protocol_interface)
1235{
1236 efi_status_t ret;
1237
1238 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1239
1240 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1241 if (ret != EFI_SUCCESS)
1242 goto out;
1243
1244 /* If the last protocol has been removed, delete the handle. */
1245 if (list_empty(&handle->protocols)) {
1246 list_del(&handle->link);
1247 free(handle);
1248 }
1249out:
1250 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001251}
1252
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001253/**
Mario Six78a88f72018-07-10 08:40:17 +02001254 * efi_register_protocol_notify() - register an event for notification when a
1255 * protocol is installed.
1256 * @protocol: GUID of the protocol whose installation shall be notified
1257 * @event: event to be signaled upon installation of the protocol
1258 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001259 *
1260 * This function implements the RegisterProtocolNotify service.
1261 * See the Unified Extensible Firmware Interface (UEFI) specification
1262 * for details.
1263 *
Mario Six78a88f72018-07-10 08:40:17 +02001264 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001265 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001266static efi_status_t EFIAPI efi_register_protocol_notify(
1267 const efi_guid_t *protocol,
1268 struct efi_event *event,
1269 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001270{
Rob Clark778e6af2017-09-13 18:05:41 -04001271 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001272 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1273}
1274
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001275/**
Mario Six78a88f72018-07-10 08:40:17 +02001276 * efi_search() - determine if an EFI handle implements a protocol
1277 * @search_type: selection criterion
1278 * @protocol: GUID of the protocol
1279 * @search_key: registration key
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001280 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001281 *
1282 * See the documentation of the LocateHandle service in the UEFI specification.
1283 *
Mario Six78a88f72018-07-10 08:40:17 +02001284 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001285 */
Alexander Grafbee91162016-03-04 01:09:59 +01001286static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001287 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001288 efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001289{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001290 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001291
1292 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001293 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001294 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001295 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001296 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001297 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001298 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001299 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001300 return (ret != EFI_SUCCESS);
1301 default:
1302 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001303 return -1;
1304 }
Alexander Grafbee91162016-03-04 01:09:59 +01001305}
1306
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001307/**
Mario Six78a88f72018-07-10 08:40:17 +02001308 * efi_locate_handle() - locate handles implementing a protocol
1309 * @search_type: selection criterion
1310 * @protocol: GUID of the protocol
1311 * @search_key: registration key
1312 * @buffer_size: size of the buffer to receive the handles in bytes
1313 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001314 *
1315 * This function is meant for U-Boot internal calls. For the API implementation
1316 * of the LocateHandle service see efi_locate_handle_ext.
1317 *
Mario Six78a88f72018-07-10 08:40:17 +02001318 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001319 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001320static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001321 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001322 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001323 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001324{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001325 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001326 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001327
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001328 /* Check parameters */
1329 switch (search_type) {
1330 case ALL_HANDLES:
1331 break;
1332 case BY_REGISTER_NOTIFY:
1333 if (!search_key)
1334 return EFI_INVALID_PARAMETER;
1335 /* RegisterProtocolNotify is not implemented yet */
1336 return EFI_UNSUPPORTED;
1337 case BY_PROTOCOL:
1338 if (!protocol)
1339 return EFI_INVALID_PARAMETER;
1340 break;
1341 default:
1342 return EFI_INVALID_PARAMETER;
1343 }
1344
1345 /*
1346 * efi_locate_handle_buffer uses this function for
1347 * the calculation of the necessary buffer size.
1348 * So do not require a buffer for buffersize == 0.
1349 */
1350 if (!buffer_size || (*buffer_size && !buffer))
1351 return EFI_INVALID_PARAMETER;
1352
Alexander Grafbee91162016-03-04 01:09:59 +01001353 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001354 list_for_each_entry(efiobj, &efi_obj_list, link) {
1355 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001356 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001357 }
1358
1359 if (*buffer_size < size) {
1360 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001361 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001362 }
1363
Rob Clark796a78c2017-08-06 14:10:07 -04001364 *buffer_size = size;
1365 if (size == 0)
1366 return EFI_NOT_FOUND;
1367
Alexander Grafbee91162016-03-04 01:09:59 +01001368 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001369 list_for_each_entry(efiobj, &efi_obj_list, link) {
1370 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001371 *buffer++ = efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01001372 }
1373
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001374 return EFI_SUCCESS;
1375}
1376
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001377/**
Mario Six78a88f72018-07-10 08:40:17 +02001378 * efi_locate_handle_ext() - locate handles implementing a protocol.
1379 * @search_type: selection criterion
1380 * @protocol: GUID of the protocol
1381 * @search_key: registration key
1382 * @buffer_size: size of the buffer to receive the handles in bytes
1383 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001384 *
1385 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001386 *
Mario Six78a88f72018-07-10 08:40:17 +02001387 * See the Unified Extensible Firmware Interface (UEFI) specification for
1388 * details.
1389 *
1390 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001391 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001392static efi_status_t EFIAPI efi_locate_handle_ext(
1393 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001394 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001395 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001396{
Rob Clark778e6af2017-09-13 18:05:41 -04001397 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001398 buffer_size, buffer);
1399
1400 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1401 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001402}
1403
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001404/**
Mario Six78a88f72018-07-10 08:40:17 +02001405 * efi_remove_configuration_table() - collapses configuration table entries,
1406 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001407 *
Mario Six78a88f72018-07-10 08:40:17 +02001408 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001409 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001410static void efi_remove_configuration_table(int i)
1411{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001412 struct efi_configuration_table *this = &systab.tables[i];
1413 struct efi_configuration_table *next = &systab.tables[i + 1];
1414 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001415
1416 memmove(this, next, (ulong)end - (ulong)next);
1417 systab.nr_tables--;
1418}
1419
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001420/**
Mario Six78a88f72018-07-10 08:40:17 +02001421 * efi_install_configuration_table() - adds, updates, or removes a
1422 * configuration table
1423 * @guid: GUID of the installed table
1424 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001425 *
1426 * This function is used for internal calls. For the API implementation of the
1427 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1428 *
Mario Six78a88f72018-07-10 08:40:17 +02001429 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001430 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001431efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1432 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001433{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001434 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001435 int i;
1436
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001437 if (!guid)
1438 return EFI_INVALID_PARAMETER;
1439
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001440 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001441 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001442 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001443 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001444 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001445 else
1446 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001447 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001448 }
1449 }
1450
Alexander Grafd98cdf62017-07-26 13:41:04 +02001451 if (!table)
1452 return EFI_NOT_FOUND;
1453
Alexander Grafbee91162016-03-04 01:09:59 +01001454 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001455 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001456 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001457
1458 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001459 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1460 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001461 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001462
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001463out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001464 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001465 efi_update_table_header_crc32(&systab.hdr);
1466
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001467 /* Notify that the configuration table was changed */
1468 list_for_each_entry(evt, &efi_events, link) {
1469 if (evt->group && !guidcmp(evt->group, guid)) {
1470 efi_signal_event(evt, false);
1471 break;
1472 }
1473 }
1474
Alexander Graf488bf122016-08-19 01:23:24 +02001475 return EFI_SUCCESS;
1476}
1477
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001478/**
Mario Six78a88f72018-07-10 08:40:17 +02001479 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1480 * configuration table.
1481 * @guid: GUID of the installed table
1482 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001483 *
1484 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001485 *
Mario Six78a88f72018-07-10 08:40:17 +02001486 * See the Unified Extensible Firmware Interface (UEFI) specification for
1487 * details.
1488 *
1489 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001490 */
Alexander Graf488bf122016-08-19 01:23:24 +02001491static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1492 void *table)
1493{
Rob Clark778e6af2017-09-13 18:05:41 -04001494 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001495 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001496}
1497
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001498/**
Mario Six78a88f72018-07-10 08:40:17 +02001499 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001500 *
1501 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001502 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001503 *
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001504 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1505 * code is returned.
1506 *
1507 * @device_path: device path of the loaded image
1508 * @file_path: file path of the loaded image
1509 * @handle_ptr: handle of the loaded image
1510 * @info_ptr: loaded image protocol
1511 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001512 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001513efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1514 struct efi_device_path *file_path,
1515 struct efi_loaded_image_obj **handle_ptr,
1516 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001517{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001518 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001519 struct efi_loaded_image *info = NULL;
1520 struct efi_loaded_image_obj *obj = NULL;
1521
1522 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1523 *handle_ptr = NULL;
1524 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001525
1526 info = calloc(1, sizeof(*info));
1527 if (!info)
1528 return EFI_OUT_OF_RESOURCES;
1529 obj = calloc(1, sizeof(*obj));
1530 if (!obj) {
1531 free(info);
1532 return EFI_OUT_OF_RESOURCES;
1533 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001534
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001535 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001536 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001537
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001538 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001539 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001540 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001541
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001542 if (device_path) {
1543 info->device_handle = efi_dp_find_obj(device_path, NULL);
1544 /*
1545 * When asking for the device path interface, return
1546 * bootefi_device_path
1547 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001548 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001549 &efi_guid_device_path, device_path);
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001550 if (ret != EFI_SUCCESS)
1551 goto failure;
1552 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001553
1554 /*
1555 * When asking for the loaded_image interface, just
1556 * return handle which points to loaded_image_info
1557 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001558 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001559 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001560 if (ret != EFI_SUCCESS)
1561 goto failure;
1562
Alexander Graf5fbb2892019-02-11 15:24:00 +01001563#if CONFIG_IS_ENABLED(EFI_LOADER_HII)
Leif Lindholmc9bfb222019-01-21 12:12:57 +09001564 ret = efi_add_protocol(&obj->header,
1565 &efi_guid_hii_string_protocol,
1566 (void *)&efi_hii_string);
1567 if (ret != EFI_SUCCESS)
1568 goto failure;
1569
1570 ret = efi_add_protocol(&obj->header,
1571 &efi_guid_hii_database_protocol,
1572 (void *)&efi_hii_database);
1573 if (ret != EFI_SUCCESS)
1574 goto failure;
1575
AKASHI Takahirocb728e52019-01-21 12:13:00 +09001576 ret = efi_add_protocol(&obj->header,
1577 &efi_guid_hii_config_routing_protocol,
1578 (void *)&efi_hii_config_routing);
1579 if (ret != EFI_SUCCESS)
1580 goto failure;
Alexander Graf5fbb2892019-02-11 15:24:00 +01001581#endif
AKASHI Takahirocb728e52019-01-21 12:13:00 +09001582
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001583 if (info_ptr)
1584 *info_ptr = info;
1585 if (handle_ptr)
1586 *handle_ptr = obj;
1587
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001588 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001589failure:
1590 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001591 efi_delete_handle(&obj->header);
1592 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001593 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001594}
1595
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001596/**
Mario Six78a88f72018-07-10 08:40:17 +02001597 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001598 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001599 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1600 * callers obligation to update the memory type as needed.
1601 *
1602 * @file_path: the path of the image to load
1603 * @buffer: buffer containing the loaded image
1604 * @size: size of the loaded image
1605 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001606 */
Rob Clark9975fe92017-09-13 18:05:38 -04001607efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001608 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001609{
1610 struct efi_file_info *info = NULL;
1611 struct efi_file_handle *f;
1612 static efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001613 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001614 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001615
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001616 /* In case of failure nothing is returned */
1617 *buffer = NULL;
1618 *size = 0;
1619
1620 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001621 f = efi_file_from_path(file_path);
1622 if (!f)
1623 return EFI_DEVICE_ERROR;
1624
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001625 /* Get file size */
Rob Clark838ee4b2017-09-13 18:05:35 -04001626 bs = 0;
1627 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1628 &bs, info));
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001629 if (ret != EFI_BUFFER_TOO_SMALL) {
1630 ret = EFI_DEVICE_ERROR;
1631 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001632 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001633
1634 info = malloc(bs);
1635 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1636 info));
Rob Clark838ee4b2017-09-13 18:05:35 -04001637 if (ret != EFI_SUCCESS)
1638 goto error;
1639
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001640 /*
1641 * When reading the file we do not yet know if it contains an
1642 * application, a boottime driver, or a runtime driver. So here we
1643 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1644 * update the reservation according to the image type.
1645 */
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001646 bs = info->file_size;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001647 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1648 EFI_BOOT_SERVICES_DATA,
1649 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001650 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001651 ret = EFI_OUT_OF_RESOURCES;
1652 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001653 }
1654
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001655 /* Read file */
1656 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1657 if (ret != EFI_SUCCESS)
1658 efi_free_pages(addr, efi_size_in_pages(bs));
1659 *buffer = (void *)(uintptr_t)addr;
1660 *size = bs;
1661error:
1662 EFI_CALL(f->close(f));
1663 free(info);
Rob Clark838ee4b2017-09-13 18:05:35 -04001664 return ret;
1665}
1666
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001667/**
Mario Six78a88f72018-07-10 08:40:17 +02001668 * efi_load_image() - load an EFI image into memory
1669 * @boot_policy: true for request originating from the boot manager
1670 * @parent_image: the caller's image handle
1671 * @file_path: the path of the image to load
1672 * @source_buffer: memory location from which the image is installed
1673 * @source_size: size of the memory area from which the image is installed
1674 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001675 *
1676 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001677 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001678 * See the Unified Extensible Firmware Interface (UEFI) specification
1679 * for details.
1680 *
Mario Six78a88f72018-07-10 08:40:17 +02001681 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001682 */
Alexander Grafbee91162016-03-04 01:09:59 +01001683static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1684 efi_handle_t parent_image,
1685 struct efi_device_path *file_path,
1686 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001687 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001688 efi_handle_t *image_handle)
1689{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001690 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04001691 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001692 struct efi_loaded_image_obj **image_obj =
1693 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001694 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001695
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001696 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001697 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001698
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001699 if (!image_handle || !parent_image) {
1700 ret = EFI_INVALID_PARAMETER;
1701 goto error;
1702 }
1703
1704 if (!source_buffer && !file_path) {
1705 ret = EFI_NOT_FOUND;
1706 goto error;
1707 }
1708
Rob Clark838ee4b2017-09-13 18:05:35 -04001709 if (!source_buffer) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001710 ret = efi_load_image_from_path(file_path, &source_buffer,
1711 &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001712 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001713 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001714 /*
1715 * split file_path which contains both the device and
1716 * file parts:
1717 */
1718 efi_dp_split_file_path(file_path, &dp, &fp);
Rob Clark838ee4b2017-09-13 18:05:35 -04001719 } else {
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001720 /* In this case, file_path is the "device" path, i.e.
Rob Clark838ee4b2017-09-13 18:05:35 -04001721 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1722 */
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001723 u64 addr;
1724 void *dest_buffer;
1725
1726 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1727 EFI_RUNTIME_SERVICES_CODE,
1728 efi_size_in_pages(source_size), &addr);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001729 if (ret != EFI_SUCCESS)
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001730 goto error;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001731 dest_buffer = (void *)(uintptr_t)addr;
1732 memcpy(dest_buffer, source_buffer, source_size);
1733 source_buffer = dest_buffer;
1734
1735 dp = file_path;
1736 fp = NULL;
Rob Clark838ee4b2017-09-13 18:05:35 -04001737 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001738 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1739 if (ret != EFI_SUCCESS)
1740 goto error_invalid_image;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001741 (*image_obj)->entry = efi_load_pe(*image_obj, source_buffer, info);
1742 if (!(*image_obj)->entry) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001743 ret = EFI_UNSUPPORTED;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001744 goto error_invalid_image;
Alexander Grafbee91162016-03-04 01:09:59 +01001745 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001746 /* Update the type of the allocated memory */
1747 efi_add_memory_map((uintptr_t)source_buffer,
1748 efi_size_in_pages(source_size),
1749 info->image_code_type, false);
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001750 info->system_table = &systab;
1751 info->parent_handle = parent_image;
Alexander Grafbee91162016-03-04 01:09:59 +01001752 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001753error_invalid_image:
1754 /* The image is invalid. Release all associated resources. */
1755 efi_free_pages((uintptr_t)source_buffer,
1756 efi_size_in_pages(source_size));
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001757 efi_delete_handle(*image_handle);
1758 *image_handle = NULL;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001759 free(info);
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001760error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001761 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001762}
1763
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001764/**
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001765 * efi_start_image() - call the entry point of an image
Mario Six78a88f72018-07-10 08:40:17 +02001766 * @image_handle: handle of the image
1767 * @exit_data_size: size of the buffer
1768 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001769 *
1770 * This function implements the StartImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001771 *
Mario Six78a88f72018-07-10 08:40:17 +02001772 * See the Unified Extensible Firmware Interface (UEFI) specification for
1773 * details.
1774 *
1775 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001776 */
Alexander Grafbee91162016-03-04 01:09:59 +01001777static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
Heinrich Schuchardtcc8e3412018-12-28 12:41:15 +01001778 efi_uintn_t *exit_data_size,
1779 u16 **exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001780{
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001781 struct efi_loaded_image_obj *image_obj =
1782 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001783 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001784
1785 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
Alexander Grafbee91162016-03-04 01:09:59 +01001786
Alexander Graff31239a2018-11-15 21:23:47 +01001787 efi_is_direct_boot = false;
1788
Alexander Grafbee91162016-03-04 01:09:59 +01001789 /* call the image! */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001790 if (setjmp(&image_obj->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001791 /*
1792 * We called the entry point of the child image with EFI_CALL
1793 * in the lines below. The child image called the Exit() boot
1794 * service efi_exit() which executed the long jump that brought
1795 * us to the current line. This implies that the second half
1796 * of the EFI_CALL macro has not been executed.
1797 */
1798#ifdef CONFIG_ARM
1799 /*
1800 * efi_exit() called efi_restore_gd(). We have to undo this
1801 * otherwise __efi_entry_check() will put the wrong value into
1802 * app_gd.
1803 */
1804 gd = app_gd;
1805#endif
1806 /*
1807 * To get ready to call EFI_EXIT below we have to execute the
1808 * missed out steps of EFI_CALL.
1809 */
1810 assert(__efi_entry_check());
1811 debug("%sEFI: %lu returned by started image\n",
1812 __efi_nesting_dec(),
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001813 (unsigned long)((uintptr_t)image_obj->exit_status &
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001814 ~EFI_ERROR_MASK));
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001815 return EFI_EXIT(image_obj->exit_status);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001816 }
1817
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001818 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001819
Alexander Graf56672bf2018-01-26 00:47:53 +01001820 /*
1821 * Usually UEFI applications call Exit() instead of returning.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001822 * But because the world doesn't consist of ponies and unicorns,
Alexander Graf56672bf2018-01-26 00:47:53 +01001823 * we're happy to emulate that behavior on behalf of a payload
1824 * that forgot.
1825 */
1826 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafbee91162016-03-04 01:09:59 +01001827}
1828
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001829/**
Mario Six78a88f72018-07-10 08:40:17 +02001830 * efi_exit() - leave an EFI application or driver
1831 * @image_handle: handle of the application or driver that is exiting
1832 * @exit_status: status code
1833 * @exit_data_size: size of the buffer in bytes
1834 * @exit_data: buffer with data describing an error
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001835 *
1836 * This function implements the Exit service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001837 *
Mario Six78a88f72018-07-10 08:40:17 +02001838 * See the Unified Extensible Firmware Interface (UEFI) specification for
1839 * details.
1840 *
1841 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001842 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001843static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001844 efi_status_t exit_status,
Heinrich Schuchardtcc8e3412018-12-28 12:41:15 +01001845 efi_uintn_t exit_data_size,
1846 u16 *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001847{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001848 /*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001849 * TODO: We should call the unload procedure of the loaded
1850 * image protocol.
1851 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001852 struct efi_loaded_image_obj *image_obj =
1853 (struct efi_loaded_image_obj *)image_handle;
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001854
Heinrich Schuchardtcc8e3412018-12-28 12:41:15 +01001855 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
Alexander Grafbee91162016-03-04 01:09:59 +01001856 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001857
Alexander Grafa1489202017-09-03 14:14:17 +02001858 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001859 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001860
Alexander Grafa1489202017-09-03 14:14:17 +02001861 /*
1862 * But longjmp out with the U-Boot gd, not the application's, as
1863 * the other end is a setjmp call inside EFI context.
1864 */
1865 efi_restore_gd();
1866
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001867 image_obj->exit_status = exit_status;
1868 longjmp(&image_obj->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001869
1870 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001871}
1872
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001873/**
Mario Six78a88f72018-07-10 08:40:17 +02001874 * efi_unload_image() - unload an EFI image
1875 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001876 *
1877 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001878 *
Mario Six78a88f72018-07-10 08:40:17 +02001879 * See the Unified Extensible Firmware Interface (UEFI) specification for
1880 * details.
1881 *
1882 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001883 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001884static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001885{
1886 struct efi_object *efiobj;
1887
1888 EFI_ENTRY("%p", image_handle);
1889 efiobj = efi_search_obj(image_handle);
1890 if (efiobj)
1891 list_del(&efiobj->link);
1892
1893 return EFI_EXIT(EFI_SUCCESS);
1894}
1895
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001896/**
Alexander Graff31239a2018-11-15 21:23:47 +01001897 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1898 */
1899static void efi_exit_caches(void)
1900{
1901#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1902 /*
1903 * Grub on 32bit ARM needs to have caches disabled before jumping into
1904 * a zImage, but does not know of all cache layers. Give it a hand.
1905 */
1906 if (efi_is_direct_boot)
1907 cleanup_before_linux();
1908#endif
1909}
1910
1911/**
Mario Six78a88f72018-07-10 08:40:17 +02001912 * efi_exit_boot_services() - stop all boot services
1913 * @image_handle: handle of the loaded image
1914 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001915 *
1916 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001917 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001918 * See the Unified Extensible Firmware Interface (UEFI) specification
1919 * for details.
1920 *
Mario Six78a88f72018-07-10 08:40:17 +02001921 * All timer events are disabled. For exit boot services events the
1922 * notification function is called. The boot services are disabled in the
1923 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001924 *
Mario Six78a88f72018-07-10 08:40:17 +02001925 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001926 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001927static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001928 unsigned long map_key)
1929{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001930 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001931
Alexander Grafbee91162016-03-04 01:09:59 +01001932 EFI_ENTRY("%p, %ld", image_handle, map_key);
1933
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001934 /* Check that the caller has read the current memory map */
1935 if (map_key != efi_memory_map_key)
1936 return EFI_INVALID_PARAMETER;
1937
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001938 /* Make sure that notification functions are not called anymore */
1939 efi_tpl = TPL_HIGH_LEVEL;
1940
1941 /* Check if ExitBootServices has already been called */
1942 if (!systab.boottime)
1943 return EFI_EXIT(EFI_SUCCESS);
1944
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001945 /* Add related events to the event group */
1946 list_for_each_entry(evt, &efi_events, link) {
1947 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1948 evt->group = &efi_guid_event_group_exit_boot_services;
1949 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001950 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001951 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001952 if (evt->group &&
1953 !guidcmp(evt->group,
1954 &efi_guid_event_group_exit_boot_services)) {
1955 efi_signal_event(evt, false);
1956 break;
1957 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001958 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001959
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001960 /* TODO: Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001961
Alexander Grafb7b84102016-11-17 01:02:57 +01001962 board_quiesce_devices();
1963
Alexander Graff31239a2018-11-15 21:23:47 +01001964 /* Fix up caches for EFI payloads if necessary */
1965 efi_exit_caches();
1966
Alexander Grafbee91162016-03-04 01:09:59 +01001967 /* This stops all lingering devices */
1968 bootm_disable_interrupts();
1969
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001970 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001971 systab.con_in_handle = NULL;
1972 systab.con_in = NULL;
1973 systab.con_out_handle = NULL;
1974 systab.con_out = NULL;
1975 systab.stderr_handle = NULL;
1976 systab.std_err = NULL;
1977 systab.boottime = NULL;
1978
1979 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001980 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001981
Alexander Grafbee91162016-03-04 01:09:59 +01001982 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001983 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001984 WATCHDOG_RESET();
1985
1986 return EFI_EXIT(EFI_SUCCESS);
1987}
1988
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001989/**
Mario Six78a88f72018-07-10 08:40:17 +02001990 * efi_get_next_monotonic_count() - get next value of the counter
1991 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001992 *
1993 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001994 *
Mario Six78a88f72018-07-10 08:40:17 +02001995 * See the Unified Extensible Firmware Interface (UEFI) specification for
1996 * details.
1997 *
1998 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001999 */
Alexander Grafbee91162016-03-04 01:09:59 +01002000static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2001{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002002 static uint64_t mono;
2003
Alexander Grafbee91162016-03-04 01:09:59 +01002004 EFI_ENTRY("%p", count);
2005 *count = mono++;
2006 return EFI_EXIT(EFI_SUCCESS);
2007}
2008
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002009/**
Mario Six78a88f72018-07-10 08:40:17 +02002010 * efi_stall() - sleep
2011 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002012 *
Mario Six78a88f72018-07-10 08:40:17 +02002013 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002014 *
Mario Six78a88f72018-07-10 08:40:17 +02002015 * See the Unified Extensible Firmware Interface (UEFI) specification for
2016 * details.
2017 *
2018 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002019 */
Alexander Grafbee91162016-03-04 01:09:59 +01002020static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2021{
2022 EFI_ENTRY("%ld", microseconds);
2023 udelay(microseconds);
2024 return EFI_EXIT(EFI_SUCCESS);
2025}
2026
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002027/**
Mario Six78a88f72018-07-10 08:40:17 +02002028 * efi_set_watchdog_timer() - reset the watchdog timer
2029 * @timeout: seconds before reset by watchdog
2030 * @watchdog_code: code to be logged when resetting
2031 * @data_size: size of buffer in bytes
2032 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002033 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002034 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002035 *
Mario Six78a88f72018-07-10 08:40:17 +02002036 * See the Unified Extensible Firmware Interface (UEFI) specification for
2037 * details.
2038 *
2039 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002040 */
Alexander Grafbee91162016-03-04 01:09:59 +01002041static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2042 uint64_t watchdog_code,
2043 unsigned long data_size,
2044 uint16_t *watchdog_data)
2045{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002046 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002047 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002048 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002049}
2050
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002051/**
Mario Six78a88f72018-07-10 08:40:17 +02002052 * efi_close_protocol() - close a protocol
2053 * @handle: handle on which the protocol shall be closed
2054 * @protocol: GUID of the protocol to close
2055 * @agent_handle: handle of the driver
2056 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002057 *
2058 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002059 *
Mario Six78a88f72018-07-10 08:40:17 +02002060 * See the Unified Extensible Firmware Interface (UEFI) specification for
2061 * details.
2062 *
2063 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002064 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002065static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002066 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002067 efi_handle_t agent_handle,
2068 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002069{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002070 struct efi_handler *handler;
2071 struct efi_open_protocol_info_item *item;
2072 struct efi_open_protocol_info_item *pos;
2073 efi_status_t r;
2074
Rob Clark778e6af2017-09-13 18:05:41 -04002075 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002076 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002077
2078 if (!agent_handle) {
2079 r = EFI_INVALID_PARAMETER;
2080 goto out;
2081 }
2082 r = efi_search_protocol(handle, protocol, &handler);
2083 if (r != EFI_SUCCESS)
2084 goto out;
2085
2086 r = EFI_NOT_FOUND;
2087 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2088 if (item->info.agent_handle == agent_handle &&
2089 item->info.controller_handle == controller_handle) {
2090 efi_delete_open_info(item);
2091 r = EFI_SUCCESS;
2092 break;
2093 }
2094 }
2095out:
2096 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002097}
2098
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002099/**
Mario Six78a88f72018-07-10 08:40:17 +02002100 * efi_open_protocol_information() - provide information about then open status
2101 * of a protocol on a handle
2102 * @handle: handle for which the information shall be retrieved
2103 * @protocol: GUID of the protocol
2104 * @entry_buffer: buffer to receive the open protocol information
2105 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002106 *
2107 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002108 *
Mario Six78a88f72018-07-10 08:40:17 +02002109 * See the Unified Extensible Firmware Interface (UEFI) specification for
2110 * details.
2111 *
2112 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002113 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002114static efi_status_t EFIAPI efi_open_protocol_information(
2115 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002116 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002117 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002118{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002119 unsigned long buffer_size;
2120 unsigned long count;
2121 struct efi_handler *handler;
2122 struct efi_open_protocol_info_item *item;
2123 efi_status_t r;
2124
Rob Clark778e6af2017-09-13 18:05:41 -04002125 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002126 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002127
2128 /* Check parameters */
2129 if (!entry_buffer) {
2130 r = EFI_INVALID_PARAMETER;
2131 goto out;
2132 }
2133 r = efi_search_protocol(handle, protocol, &handler);
2134 if (r != EFI_SUCCESS)
2135 goto out;
2136
2137 /* Count entries */
2138 count = 0;
2139 list_for_each_entry(item, &handler->open_infos, link) {
2140 if (item->info.open_count)
2141 ++count;
2142 }
2143 *entry_count = count;
2144 *entry_buffer = NULL;
2145 if (!count) {
2146 r = EFI_SUCCESS;
2147 goto out;
2148 }
2149
2150 /* Copy entries */
2151 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002152 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002153 (void **)entry_buffer);
2154 if (r != EFI_SUCCESS)
2155 goto out;
2156 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2157 if (item->info.open_count)
2158 (*entry_buffer)[--count] = item->info;
2159 }
2160out:
2161 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002162}
2163
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002164/**
Mario Six78a88f72018-07-10 08:40:17 +02002165 * efi_protocols_per_handle() - get protocols installed on a handle
2166 * @handle: handle for which the information is retrieved
2167 * @protocol_buffer: buffer with protocol GUIDs
2168 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002169 *
2170 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002171 *
Mario Six78a88f72018-07-10 08:40:17 +02002172 * See the Unified Extensible Firmware Interface (UEFI) specification for
2173 * details.
2174 *
2175 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002176 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002177static efi_status_t EFIAPI efi_protocols_per_handle(
2178 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002179 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002180{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002181 unsigned long buffer_size;
2182 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002183 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002184 efi_status_t r;
2185
Alexander Grafbee91162016-03-04 01:09:59 +01002186 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2187 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002188
2189 if (!handle || !protocol_buffer || !protocol_buffer_count)
2190 return EFI_EXIT(EFI_INVALID_PARAMETER);
2191
2192 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002193 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002194
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002195 efiobj = efi_search_obj(handle);
2196 if (!efiobj)
2197 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002198
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002199 /* Count protocols */
2200 list_for_each(protocol_handle, &efiobj->protocols) {
2201 ++*protocol_buffer_count;
2202 }
2203
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002204 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002205 if (*protocol_buffer_count) {
2206 size_t j = 0;
2207
2208 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002209 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002210 (void **)protocol_buffer);
2211 if (r != EFI_SUCCESS)
2212 return EFI_EXIT(r);
2213 list_for_each(protocol_handle, &efiobj->protocols) {
2214 struct efi_handler *protocol;
2215
2216 protocol = list_entry(protocol_handle,
2217 struct efi_handler, link);
2218 (*protocol_buffer)[j] = (void *)protocol->guid;
2219 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002220 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002221 }
2222
2223 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002224}
2225
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002226/**
Mario Six78a88f72018-07-10 08:40:17 +02002227 * efi_locate_handle_buffer() - locate handles implementing a protocol
2228 * @search_type: selection criterion
2229 * @protocol: GUID of the protocol
2230 * @search_key: registration key
2231 * @no_handles: number of returned handles
2232 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002233 *
2234 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002235 *
Mario Six78a88f72018-07-10 08:40:17 +02002236 * See the Unified Extensible Firmware Interface (UEFI) specification for
2237 * details.
2238 *
2239 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002240 */
Alexander Grafbee91162016-03-04 01:09:59 +01002241static efi_status_t EFIAPI efi_locate_handle_buffer(
2242 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002243 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002244 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002245{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002246 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002247 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002248
Rob Clark778e6af2017-09-13 18:05:41 -04002249 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002250 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002251
2252 if (!no_handles || !buffer) {
2253 r = EFI_INVALID_PARAMETER;
2254 goto out;
2255 }
2256 *no_handles = 0;
2257 *buffer = NULL;
2258 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2259 *buffer);
2260 if (r != EFI_BUFFER_TOO_SMALL)
2261 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002262 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002263 (void **)buffer);
2264 if (r != EFI_SUCCESS)
2265 goto out;
2266 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2267 *buffer);
2268 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002269 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002270out:
2271 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002272}
2273
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002274/**
Mario Six78a88f72018-07-10 08:40:17 +02002275 * efi_locate_protocol() - find an interface implementing a protocol
2276 * @protocol: GUID of the protocol
2277 * @registration: registration key passed to the notification function
2278 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002279 *
2280 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002281 *
Mario Six78a88f72018-07-10 08:40:17 +02002282 * See the Unified Extensible Firmware Interface (UEFI) specification for
2283 * details.
2284 *
2285 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002286 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002287static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002288 void *registration,
2289 void **protocol_interface)
2290{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002291 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002292 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002293
Rob Clark778e6af2017-09-13 18:05:41 -04002294 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002295
2296 if (!protocol || !protocol_interface)
2297 return EFI_EXIT(EFI_INVALID_PARAMETER);
2298
2299 list_for_each(lhandle, &efi_obj_list) {
2300 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002301 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002302
2303 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002304
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002305 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002306 if (ret == EFI_SUCCESS) {
2307 *protocol_interface = handler->protocol_interface;
2308 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002309 }
2310 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002311 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002312
2313 return EFI_EXIT(EFI_NOT_FOUND);
2314}
2315
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002316/**
Mario Six78a88f72018-07-10 08:40:17 +02002317 * efi_locate_device_path() - Get the device path and handle of an device
2318 * implementing a protocol
2319 * @protocol: GUID of the protocol
2320 * @device_path: device path
2321 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002322 *
2323 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002324 *
Mario Six78a88f72018-07-10 08:40:17 +02002325 * See the Unified Extensible Firmware Interface (UEFI) specification for
2326 * details.
2327 *
2328 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002329 */
2330static efi_status_t EFIAPI efi_locate_device_path(
2331 const efi_guid_t *protocol,
2332 struct efi_device_path **device_path,
2333 efi_handle_t *device)
2334{
2335 struct efi_device_path *dp;
2336 size_t i;
2337 struct efi_handler *handler;
2338 efi_handle_t *handles;
2339 size_t len, len_dp;
2340 size_t len_best = 0;
2341 efi_uintn_t no_handles;
2342 u8 *remainder;
2343 efi_status_t ret;
2344
2345 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2346
2347 if (!protocol || !device_path || !*device_path || !device) {
2348 ret = EFI_INVALID_PARAMETER;
2349 goto out;
2350 }
2351
2352 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002353 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002354
2355 /* Get all handles implementing the protocol */
2356 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2357 &no_handles, &handles));
2358 if (ret != EFI_SUCCESS)
2359 goto out;
2360
2361 for (i = 0; i < no_handles; ++i) {
2362 /* Find the device path protocol */
2363 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2364 &handler);
2365 if (ret != EFI_SUCCESS)
2366 continue;
2367 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002368 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002369 /*
2370 * This handle can only be a better fit
2371 * if its device path length is longer than the best fit and
2372 * if its device path length is shorter of equal the searched
2373 * device path.
2374 */
2375 if (len_dp <= len_best || len_dp > len)
2376 continue;
2377 /* Check if dp is a subpath of device_path */
2378 if (memcmp(*device_path, dp, len_dp))
2379 continue;
2380 *device = handles[i];
2381 len_best = len_dp;
2382 }
2383 if (len_best) {
2384 remainder = (u8 *)*device_path + len_best;
2385 *device_path = (struct efi_device_path *)remainder;
2386 ret = EFI_SUCCESS;
2387 } else {
2388 ret = EFI_NOT_FOUND;
2389 }
2390out:
2391 return EFI_EXIT(ret);
2392}
2393
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002394/**
Mario Six78a88f72018-07-10 08:40:17 +02002395 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2396 * interfaces
2397 * @handle: handle on which the protocol interfaces shall be installed
2398 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2399 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002400 *
2401 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002402 *
Mario Six78a88f72018-07-10 08:40:17 +02002403 * See the Unified Extensible Firmware Interface (UEFI) specification for
2404 * details.
2405 *
2406 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002407 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002408static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2409 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002410{
2411 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002412
Alexander Grafbeb077a2018-06-18 17:23:05 +02002413 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002414 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002415 void *protocol_interface;
2416 efi_status_t r = EFI_SUCCESS;
2417 int i = 0;
2418
2419 if (!handle)
2420 return EFI_EXIT(EFI_INVALID_PARAMETER);
2421
Alexander Grafbeb077a2018-06-18 17:23:05 +02002422 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002423 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002424 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002425 if (!protocol)
2426 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002427 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002428 r = EFI_CALL(efi_install_protocol_interface(
2429 handle, protocol,
2430 EFI_NATIVE_INTERFACE,
2431 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002432 if (r != EFI_SUCCESS)
2433 break;
2434 i++;
2435 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002436 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002437 if (r == EFI_SUCCESS)
2438 return EFI_EXIT(r);
2439
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002440 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002441 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002442 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002443 protocol = efi_va_arg(argptr, efi_guid_t*);
2444 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002445 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002446 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002447 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002448 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002449
2450 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002451}
2452
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002453/**
Mario Six78a88f72018-07-10 08:40:17 +02002454 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2455 * interfaces
2456 * @handle: handle from which the protocol interfaces shall be removed
2457 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2458 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002459 *
2460 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002461 *
Mario Six78a88f72018-07-10 08:40:17 +02002462 * See the Unified Extensible Firmware Interface (UEFI) specification for
2463 * details.
2464 *
2465 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002466 */
Alexander Grafbee91162016-03-04 01:09:59 +01002467static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002468 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002469{
2470 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002471
Alexander Grafbeb077a2018-06-18 17:23:05 +02002472 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002473 const efi_guid_t *protocol;
2474 void *protocol_interface;
2475 efi_status_t r = EFI_SUCCESS;
2476 size_t i = 0;
2477
2478 if (!handle)
2479 return EFI_EXIT(EFI_INVALID_PARAMETER);
2480
Alexander Grafbeb077a2018-06-18 17:23:05 +02002481 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002482 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002483 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002484 if (!protocol)
2485 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002486 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002487 r = efi_uninstall_protocol(handle, protocol,
2488 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002489 if (r != EFI_SUCCESS)
2490 break;
2491 i++;
2492 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002493 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002494 if (r == EFI_SUCCESS) {
2495 /* If the last protocol has been removed, delete the handle. */
2496 if (list_empty(&handle->protocols)) {
2497 list_del(&handle->link);
2498 free(handle);
2499 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002500 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002501 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002502
2503 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002504 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002505 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002506 protocol = efi_va_arg(argptr, efi_guid_t*);
2507 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002508 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2509 EFI_NATIVE_INTERFACE,
2510 protocol_interface));
2511 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002512 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002513
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002514 /* In case of an error always return EFI_INVALID_PARAMETER */
2515 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002516}
2517
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002518/**
Mario Six78a88f72018-07-10 08:40:17 +02002519 * efi_calculate_crc32() - calculate cyclic redundancy code
2520 * @data: buffer with data
2521 * @data_size: size of buffer in bytes
2522 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002523 *
2524 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002525 *
Mario Six78a88f72018-07-10 08:40:17 +02002526 * See the Unified Extensible Firmware Interface (UEFI) specification for
2527 * details.
2528 *
2529 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002530 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002531static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2532 efi_uintn_t data_size,
2533 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002534{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002535 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002536 *crc32_p = crc32(0, data, data_size);
2537 return EFI_EXIT(EFI_SUCCESS);
2538}
2539
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002540/**
Mario Six78a88f72018-07-10 08:40:17 +02002541 * efi_copy_mem() - copy memory
2542 * @destination: destination of the copy operation
2543 * @source: source of the copy operation
2544 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002545 *
2546 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002547 *
Mario Six78a88f72018-07-10 08:40:17 +02002548 * See the Unified Extensible Firmware Interface (UEFI) specification for
2549 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002550 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002551static void EFIAPI efi_copy_mem(void *destination, const void *source,
2552 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002553{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002554 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002555 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002556 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002557}
2558
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002559/**
Mario Six78a88f72018-07-10 08:40:17 +02002560 * efi_set_mem() - Fill memory with a byte value.
2561 * @buffer: buffer to fill
2562 * @size: size of buffer in bytes
2563 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002564 *
2565 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002566 *
Mario Six78a88f72018-07-10 08:40:17 +02002567 * See the Unified Extensible Firmware Interface (UEFI) specification for
2568 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002569 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002570static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002571{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002572 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002573 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002574 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002575}
2576
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002577/**
Mario Six78a88f72018-07-10 08:40:17 +02002578 * efi_protocol_open() - open protocol interface on a handle
2579 * @handler: handler of a protocol
2580 * @protocol_interface: interface implementing the protocol
2581 * @agent_handle: handle of the driver
2582 * @controller_handle: handle of the controller
2583 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002584 *
Mario Six78a88f72018-07-10 08:40:17 +02002585 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002586 */
2587static efi_status_t efi_protocol_open(
2588 struct efi_handler *handler,
2589 void **protocol_interface, void *agent_handle,
2590 void *controller_handle, uint32_t attributes)
2591{
2592 struct efi_open_protocol_info_item *item;
2593 struct efi_open_protocol_info_entry *match = NULL;
2594 bool opened_by_driver = false;
2595 bool opened_exclusive = false;
2596
2597 /* If there is no agent, only return the interface */
2598 if (!agent_handle)
2599 goto out;
2600
2601 /* For TEST_PROTOCOL ignore interface attribute */
2602 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2603 *protocol_interface = NULL;
2604
2605 /*
2606 * Check if the protocol is already opened by a driver with the same
2607 * attributes or opened exclusively
2608 */
2609 list_for_each_entry(item, &handler->open_infos, link) {
2610 if (item->info.agent_handle == agent_handle) {
2611 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2612 (item->info.attributes == attributes))
2613 return EFI_ALREADY_STARTED;
2614 }
2615 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2616 opened_exclusive = true;
2617 }
2618
2619 /* Only one controller can open the protocol exclusively */
2620 if (opened_exclusive && attributes &
2621 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2622 return EFI_ACCESS_DENIED;
2623
2624 /* Prepare exclusive opening */
2625 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2626 /* Try to disconnect controllers */
2627 list_for_each_entry(item, &handler->open_infos, link) {
2628 if (item->info.attributes ==
2629 EFI_OPEN_PROTOCOL_BY_DRIVER)
2630 EFI_CALL(efi_disconnect_controller(
2631 item->info.controller_handle,
2632 item->info.agent_handle,
2633 NULL));
2634 }
2635 opened_by_driver = false;
2636 /* Check if all controllers are disconnected */
2637 list_for_each_entry(item, &handler->open_infos, link) {
2638 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2639 opened_by_driver = true;
2640 }
Heinrich Schuchardt4f37fa42018-09-30 13:40:43 +02002641 /* Only one controller can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002642 if (opened_by_driver)
2643 return EFI_ACCESS_DENIED;
2644 }
2645
2646 /* Find existing entry */
2647 list_for_each_entry(item, &handler->open_infos, link) {
2648 if (item->info.agent_handle == agent_handle &&
2649 item->info.controller_handle == controller_handle)
2650 match = &item->info;
2651 }
2652 /* None found, create one */
2653 if (!match) {
2654 match = efi_create_open_info(handler);
2655 if (!match)
2656 return EFI_OUT_OF_RESOURCES;
2657 }
2658
2659 match->agent_handle = agent_handle;
2660 match->controller_handle = controller_handle;
2661 match->attributes = attributes;
2662 match->open_count++;
2663
2664out:
2665 /* For TEST_PROTOCOL ignore interface attribute. */
2666 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2667 *protocol_interface = handler->protocol_interface;
2668
2669 return EFI_SUCCESS;
2670}
2671
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002672/**
Mario Six78a88f72018-07-10 08:40:17 +02002673 * efi_open_protocol() - open protocol interface on a handle
2674 * @handle: handle on which the protocol shall be opened
2675 * @protocol: GUID of the protocol
2676 * @protocol_interface: interface implementing the protocol
2677 * @agent_handle: handle of the driver
2678 * @controller_handle: handle of the controller
2679 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002680 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002681 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002682 *
Mario Six78a88f72018-07-10 08:40:17 +02002683 * See the Unified Extensible Firmware Interface (UEFI) specification for
2684 * details.
2685 *
2686 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002687 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002688static efi_status_t EFIAPI efi_open_protocol
2689 (efi_handle_t handle, const efi_guid_t *protocol,
2690 void **protocol_interface, efi_handle_t agent_handle,
2691 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002692{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002693 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002694 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002695
Rob Clark778e6af2017-09-13 18:05:41 -04002696 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002697 protocol_interface, agent_handle, controller_handle,
2698 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002699
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002700 if (!handle || !protocol ||
2701 (!protocol_interface && attributes !=
2702 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2703 goto out;
2704 }
2705
2706 switch (attributes) {
2707 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2708 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2709 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2710 break;
2711 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2712 if (controller_handle == handle)
2713 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002714 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002715 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2716 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002717 /* Check that the controller handle is valid */
2718 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002719 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002720 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002721 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002722 /* Check that the agent handle is valid */
2723 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002724 goto out;
2725 break;
2726 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002727 goto out;
2728 }
2729
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002730 r = efi_search_protocol(handle, protocol, &handler);
2731 if (r != EFI_SUCCESS)
2732 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002733
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002734 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2735 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002736out:
2737 return EFI_EXIT(r);
2738}
2739
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002740/**
Mario Six78a88f72018-07-10 08:40:17 +02002741 * efi_handle_protocol() - get interface of a protocol on a handle
2742 * @handle: handle on which the protocol shall be opened
2743 * @protocol: GUID of the protocol
2744 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002745 *
2746 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002747 *
Mario Six78a88f72018-07-10 08:40:17 +02002748 * See the Unified Extensible Firmware Interface (UEFI) specification for
2749 * details.
2750 *
2751 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002752 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002753static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002754 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002755 void **protocol_interface)
2756{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002757 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2758 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002759}
2760
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002761/**
Mario Six78a88f72018-07-10 08:40:17 +02002762 * efi_bind_controller() - bind a single driver to a controller
2763 * @controller_handle: controller handle
2764 * @driver_image_handle: driver handle
2765 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002766 *
Mario Six78a88f72018-07-10 08:40:17 +02002767 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002768 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002769static efi_status_t efi_bind_controller(
2770 efi_handle_t controller_handle,
2771 efi_handle_t driver_image_handle,
2772 struct efi_device_path *remain_device_path)
2773{
2774 struct efi_driver_binding_protocol *binding_protocol;
2775 efi_status_t r;
2776
2777 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2778 &efi_guid_driver_binding_protocol,
2779 (void **)&binding_protocol,
2780 driver_image_handle, NULL,
2781 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2782 if (r != EFI_SUCCESS)
2783 return r;
2784 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2785 controller_handle,
2786 remain_device_path));
2787 if (r == EFI_SUCCESS)
2788 r = EFI_CALL(binding_protocol->start(binding_protocol,
2789 controller_handle,
2790 remain_device_path));
2791 EFI_CALL(efi_close_protocol(driver_image_handle,
2792 &efi_guid_driver_binding_protocol,
2793 driver_image_handle, NULL));
2794 return r;
2795}
2796
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002797/**
Mario Six78a88f72018-07-10 08:40:17 +02002798 * efi_connect_single_controller() - connect a single driver to a controller
2799 * @controller_handle: controller
2800 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002801 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002802 *
Mario Six78a88f72018-07-10 08:40:17 +02002803 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002804 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002805static efi_status_t efi_connect_single_controller(
2806 efi_handle_t controller_handle,
2807 efi_handle_t *driver_image_handle,
2808 struct efi_device_path *remain_device_path)
2809{
2810 efi_handle_t *buffer;
2811 size_t count;
2812 size_t i;
2813 efi_status_t r;
2814 size_t connected = 0;
2815
2816 /* Get buffer with all handles with driver binding protocol */
2817 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2818 &efi_guid_driver_binding_protocol,
2819 NULL, &count, &buffer));
2820 if (r != EFI_SUCCESS)
2821 return r;
2822
2823 /* Context Override */
2824 if (driver_image_handle) {
2825 for (; *driver_image_handle; ++driver_image_handle) {
2826 for (i = 0; i < count; ++i) {
2827 if (buffer[i] == *driver_image_handle) {
2828 buffer[i] = NULL;
2829 r = efi_bind_controller(
2830 controller_handle,
2831 *driver_image_handle,
2832 remain_device_path);
2833 /*
2834 * For drivers that do not support the
2835 * controller or are already connected
2836 * we receive an error code here.
2837 */
2838 if (r == EFI_SUCCESS)
2839 ++connected;
2840 }
2841 }
2842 }
2843 }
2844
2845 /*
2846 * TODO: Some overrides are not yet implemented:
2847 * - Platform Driver Override
2848 * - Driver Family Override Search
2849 * - Bus Specific Driver Override
2850 */
2851
2852 /* Driver Binding Search */
2853 for (i = 0; i < count; ++i) {
2854 if (buffer[i]) {
2855 r = efi_bind_controller(controller_handle,
2856 buffer[i],
2857 remain_device_path);
2858 if (r == EFI_SUCCESS)
2859 ++connected;
2860 }
2861 }
2862
2863 efi_free_pool(buffer);
2864 if (!connected)
2865 return EFI_NOT_FOUND;
2866 return EFI_SUCCESS;
2867}
2868
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002869/**
Mario Six78a88f72018-07-10 08:40:17 +02002870 * efi_connect_controller() - connect a controller to a driver
2871 * @controller_handle: handle of the controller
2872 * @driver_image_handle: handle of the driver
2873 * @remain_device_path: device path of a child controller
2874 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002875 *
2876 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002877 *
2878 * See the Unified Extensible Firmware Interface (UEFI) specification for
2879 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002880 *
2881 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002882 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002883 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2884 *
Mario Six78a88f72018-07-10 08:40:17 +02002885 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002886 */
2887static efi_status_t EFIAPI efi_connect_controller(
2888 efi_handle_t controller_handle,
2889 efi_handle_t *driver_image_handle,
2890 struct efi_device_path *remain_device_path,
2891 bool recursive)
2892{
2893 efi_status_t r;
2894 efi_status_t ret = EFI_NOT_FOUND;
2895 struct efi_object *efiobj;
2896
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01002897 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002898 remain_device_path, recursive);
2899
2900 efiobj = efi_search_obj(controller_handle);
2901 if (!efiobj) {
2902 ret = EFI_INVALID_PARAMETER;
2903 goto out;
2904 }
2905
2906 r = efi_connect_single_controller(controller_handle,
2907 driver_image_handle,
2908 remain_device_path);
2909 if (r == EFI_SUCCESS)
2910 ret = EFI_SUCCESS;
2911 if (recursive) {
2912 struct efi_handler *handler;
2913 struct efi_open_protocol_info_item *item;
2914
2915 list_for_each_entry(handler, &efiobj->protocols, link) {
2916 list_for_each_entry(item, &handler->open_infos, link) {
2917 if (item->info.attributes &
2918 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2919 r = EFI_CALL(efi_connect_controller(
2920 item->info.controller_handle,
2921 driver_image_handle,
2922 remain_device_path,
2923 recursive));
2924 if (r == EFI_SUCCESS)
2925 ret = EFI_SUCCESS;
2926 }
2927 }
2928 }
2929 }
2930 /* Check for child controller specified by end node */
2931 if (ret != EFI_SUCCESS && remain_device_path &&
2932 remain_device_path->type == DEVICE_PATH_TYPE_END)
2933 ret = EFI_SUCCESS;
2934out:
2935 return EFI_EXIT(ret);
2936}
2937
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002938/**
Mario Six78a88f72018-07-10 08:40:17 +02002939 * efi_reinstall_protocol_interface() - reinstall protocol interface
2940 * @handle: handle on which the protocol shall be reinstalled
2941 * @protocol: GUID of the protocol to be installed
2942 * @old_interface: interface to be removed
2943 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002944 *
2945 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002946 *
2947 * See the Unified Extensible Firmware Interface (UEFI) specification for
2948 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002949 *
2950 * The old interface is uninstalled. The new interface is installed.
2951 * Drivers are connected.
2952 *
Mario Six78a88f72018-07-10 08:40:17 +02002953 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002954 */
2955static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2956 efi_handle_t handle, const efi_guid_t *protocol,
2957 void *old_interface, void *new_interface)
2958{
2959 efi_status_t ret;
2960
2961 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2962 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002963
2964 /* Uninstall protocol but do not delete handle */
2965 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002966 if (ret != EFI_SUCCESS)
2967 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002968
2969 /* Install the new protocol */
2970 ret = efi_add_protocol(handle, protocol, new_interface);
2971 /*
2972 * The UEFI spec does not specify what should happen to the handle
2973 * if in case of an error no protocol interface remains on the handle.
2974 * So let's do nothing here.
2975 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002976 if (ret != EFI_SUCCESS)
2977 goto out;
2978 /*
2979 * The returned status code has to be ignored.
2980 * Do not create an error if no suitable driver for the handle exists.
2981 */
2982 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2983out:
2984 return EFI_EXIT(ret);
2985}
2986
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002987/**
Mario Six78a88f72018-07-10 08:40:17 +02002988 * efi_get_child_controllers() - get all child controllers associated to a driver
2989 * @efiobj: handle of the controller
2990 * @driver_handle: handle of the driver
2991 * @number_of_children: number of child controllers
2992 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002993 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002994 * The allocated buffer has to be freed with free().
2995 *
Mario Six78a88f72018-07-10 08:40:17 +02002996 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002997 */
2998static efi_status_t efi_get_child_controllers(
2999 struct efi_object *efiobj,
3000 efi_handle_t driver_handle,
3001 efi_uintn_t *number_of_children,
3002 efi_handle_t **child_handle_buffer)
3003{
3004 struct efi_handler *handler;
3005 struct efi_open_protocol_info_item *item;
3006 efi_uintn_t count = 0, i;
3007 bool duplicate;
3008
3009 /* Count all child controller associations */
3010 list_for_each_entry(handler, &efiobj->protocols, link) {
3011 list_for_each_entry(item, &handler->open_infos, link) {
3012 if (item->info.agent_handle == driver_handle &&
3013 item->info.attributes &
3014 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3015 ++count;
3016 }
3017 }
3018 /*
3019 * Create buffer. In case of duplicate child controller assignments
3020 * the buffer will be too large. But that does not harm.
3021 */
3022 *number_of_children = 0;
3023 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3024 if (!*child_handle_buffer)
3025 return EFI_OUT_OF_RESOURCES;
3026 /* Copy unique child handles */
3027 list_for_each_entry(handler, &efiobj->protocols, link) {
3028 list_for_each_entry(item, &handler->open_infos, link) {
3029 if (item->info.agent_handle == driver_handle &&
3030 item->info.attributes &
3031 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3032 /* Check this is a new child controller */
3033 duplicate = false;
3034 for (i = 0; i < *number_of_children; ++i) {
3035 if ((*child_handle_buffer)[i] ==
3036 item->info.controller_handle)
3037 duplicate = true;
3038 }
3039 /* Copy handle to buffer */
3040 if (!duplicate) {
3041 i = (*number_of_children)++;
3042 (*child_handle_buffer)[i] =
3043 item->info.controller_handle;
3044 }
3045 }
3046 }
3047 }
3048 return EFI_SUCCESS;
3049}
3050
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003051/**
Mario Six78a88f72018-07-10 08:40:17 +02003052 * efi_disconnect_controller() - disconnect a controller from a driver
3053 * @controller_handle: handle of the controller
3054 * @driver_image_handle: handle of the driver
3055 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003056 *
3057 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003058 *
Mario Six78a88f72018-07-10 08:40:17 +02003059 * See the Unified Extensible Firmware Interface (UEFI) specification for
3060 * details.
3061 *
3062 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003063 */
3064static efi_status_t EFIAPI efi_disconnect_controller(
3065 efi_handle_t controller_handle,
3066 efi_handle_t driver_image_handle,
3067 efi_handle_t child_handle)
3068{
3069 struct efi_driver_binding_protocol *binding_protocol;
3070 efi_handle_t *child_handle_buffer = NULL;
3071 size_t number_of_children = 0;
3072 efi_status_t r;
3073 size_t stop_count = 0;
3074 struct efi_object *efiobj;
3075
3076 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3077 child_handle);
3078
3079 efiobj = efi_search_obj(controller_handle);
3080 if (!efiobj) {
3081 r = EFI_INVALID_PARAMETER;
3082 goto out;
3083 }
3084
3085 if (child_handle && !efi_search_obj(child_handle)) {
3086 r = EFI_INVALID_PARAMETER;
3087 goto out;
3088 }
3089
3090 /* If no driver handle is supplied, disconnect all drivers */
3091 if (!driver_image_handle) {
3092 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3093 goto out;
3094 }
3095
3096 /* Create list of child handles */
3097 if (child_handle) {
3098 number_of_children = 1;
3099 child_handle_buffer = &child_handle;
3100 } else {
3101 efi_get_child_controllers(efiobj,
3102 driver_image_handle,
3103 &number_of_children,
3104 &child_handle_buffer);
3105 }
3106
3107 /* Get the driver binding protocol */
3108 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3109 &efi_guid_driver_binding_protocol,
3110 (void **)&binding_protocol,
3111 driver_image_handle, NULL,
3112 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3113 if (r != EFI_SUCCESS)
3114 goto out;
3115 /* Remove the children */
3116 if (number_of_children) {
3117 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3118 controller_handle,
3119 number_of_children,
3120 child_handle_buffer));
3121 if (r == EFI_SUCCESS)
3122 ++stop_count;
3123 }
3124 /* Remove the driver */
3125 if (!child_handle)
3126 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3127 controller_handle,
3128 0, NULL));
3129 if (r == EFI_SUCCESS)
3130 ++stop_count;
3131 EFI_CALL(efi_close_protocol(driver_image_handle,
3132 &efi_guid_driver_binding_protocol,
3133 driver_image_handle, NULL));
3134
3135 if (stop_count)
3136 r = EFI_SUCCESS;
3137 else
3138 r = EFI_NOT_FOUND;
3139out:
3140 if (!child_handle)
3141 free(child_handle_buffer);
3142 return EFI_EXIT(r);
3143}
3144
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003145static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003146 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003147 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3148 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003149 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003150 },
3151 .raise_tpl = efi_raise_tpl,
3152 .restore_tpl = efi_restore_tpl,
3153 .allocate_pages = efi_allocate_pages_ext,
3154 .free_pages = efi_free_pages_ext,
3155 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003156 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003157 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003158 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003159 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003160 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003161 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003162 .close_event = efi_close_event,
3163 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003164 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003165 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003166 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003167 .handle_protocol = efi_handle_protocol,
3168 .reserved = NULL,
3169 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003170 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003171 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003172 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003173 .load_image = efi_load_image,
3174 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003175 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003176 .unload_image = efi_unload_image,
3177 .exit_boot_services = efi_exit_boot_services,
3178 .get_next_monotonic_count = efi_get_next_monotonic_count,
3179 .stall = efi_stall,
3180 .set_watchdog_timer = efi_set_watchdog_timer,
3181 .connect_controller = efi_connect_controller,
3182 .disconnect_controller = efi_disconnect_controller,
3183 .open_protocol = efi_open_protocol,
3184 .close_protocol = efi_close_protocol,
3185 .open_protocol_information = efi_open_protocol_information,
3186 .protocols_per_handle = efi_protocols_per_handle,
3187 .locate_handle_buffer = efi_locate_handle_buffer,
3188 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003189 .install_multiple_protocol_interfaces =
3190 efi_install_multiple_protocol_interfaces,
3191 .uninstall_multiple_protocol_interfaces =
3192 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003193 .calculate_crc32 = efi_calculate_crc32,
3194 .copy_mem = efi_copy_mem,
3195 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003196 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003197};
3198
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003199static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003200
Alexander Graf3c63db92016-10-14 13:45:30 +02003201struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003202 .hdr = {
3203 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003204 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003205 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003206 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003207 .fw_vendor = firmware_vendor,
3208 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003209 .con_in = (void *)&efi_con_in,
3210 .con_out = (void *)&efi_con_out,
3211 .std_err = (void *)&efi_con_out,
3212 .runtime = (void *)&efi_runtime_services,
3213 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003214 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003215 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003216};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003217
3218/**
3219 * efi_initialize_system_table() - Initialize system table
3220 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003221 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003222 */
3223efi_status_t efi_initialize_system_table(void)
3224{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003225 efi_status_t ret;
3226
3227 /* Allocate configuration table array */
3228 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3229 EFI_MAX_CONFIGURATION_TABLES *
3230 sizeof(struct efi_configuration_table),
3231 (void **)&systab.tables);
3232
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003233 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003234 efi_update_table_header_crc32(&systab.hdr);
3235 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3236 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003237
3238 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003239}