blob: 6bd64b9df9c85aee0daedec6abb02db0f4f10c32 [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>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023
Alexander Grafbee91162016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010029
Alexander Grafbee91162016-03-04 01:09:59 +010030/*
31 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
32 * we need to do trickery with caches. Since we don't want to break the EFI
33 * aware boot path, only apply hacks when loading exiting directly (breaking
34 * direct Linux EFI booting along the way - oh well).
35 */
36static bool efi_is_direct_boot = true;
37
38/*
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
42 *
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 */
Bin Mengbb68c7f2018-06-27 20:38:02 -070046static struct efi_configuration_table __efi_runtime_data efi_conf_table[16];
Alexander Grafbee91162016-03-04 01:09:59 +010047
Simon Glass65e4c0b2016-09-25 15:27:35 -060048#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010049/*
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
54 */
55static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060056#endif
Alexander Grafbee91162016-03-04 01:09:59 +010057
Rob Clarkc160d2f2017-07-27 08:04:18 -040058static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040059static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010060/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010062/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040065
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010066/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010082static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010086
Rob Clarkc160d2f2017-07-27 08:04:18 -040087/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
91#ifdef CONFIG_ARM
92 assert(efi_gd);
93 app_gd = gd;
94 gd = efi_gd;
95#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
103#ifdef CONFIG_ARM
104 gd = app_gd;
105#endif
106 return ret;
107}
108
Alexander Grafbee91162016-03-04 01:09:59 +0100109/* Called from do_bootefi_exec() */
110void efi_save_gd(void)
111{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600112#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100113 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600114#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100115}
116
Rob Clarkc160d2f2017-07-27 08:04:18 -0400117/*
Mario Six78a88f72018-07-10 08:40:17 +0200118 * Special case handler for error/abort that just forces things back to u-boot
119 * world so we can dump out an abort msg, without any care about returning back
120 * to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400121 */
Alexander Grafbee91162016-03-04 01:09:59 +0100122void efi_restore_gd(void)
123{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600124#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100125 /* Only restore if we're already in EFI context */
126 if (!efi_gd)
127 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100128 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600129#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100130}
131
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200132/**
Mario Six78a88f72018-07-10 08:40:17 +0200133 * indent_string() - returns a string for indenting with two spaces per level
134 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100135 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200136 * A maximum of ten indent levels is supported. Higher indent levels will be
137 * truncated.
138 *
Mario Six78a88f72018-07-10 08:40:17 +0200139 * Return: A string for indenting with two spaces per level is
140 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400141 */
142static const char *indent_string(int level)
143{
144 const char *indent = " ";
145 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100146
Rob Clarkaf65db82017-07-27 08:04:19 -0400147 level = min(max, level * 2);
148 return &indent[max - level];
149}
150
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200151const char *__efi_nesting(void)
152{
153 return indent_string(nesting_level);
154}
155
Rob Clarkaf65db82017-07-27 08:04:19 -0400156const char *__efi_nesting_inc(void)
157{
158 return indent_string(nesting_level++);
159}
160
161const char *__efi_nesting_dec(void)
162{
163 return indent_string(--nesting_level);
164}
165
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200166/**
Mario Six78a88f72018-07-10 08:40:17 +0200167 * efi_queue_event() - queue an EFI event
168 * @event: event to signal
169 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200170 *
171 * This function queues the notification function of the event for future
172 * execution.
173 *
Mario Six78a88f72018-07-10 08:40:17 +0200174 * The notification function is called if the task priority level of the event
175 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200176 *
177 * For the SignalEvent service see efi_signal_event_ext.
178 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200179 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100180static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200181{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200182 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200183 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200184 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100185 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200186 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200189 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200190 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200191}
192
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200193/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200194 * is_valid_tpl() - check if the task priority level is valid
195 *
196 * @tpl: TPL level to check
197 * ReturnValue: status code
198 */
199efi_status_t is_valid_tpl(efi_uintn_t tpl)
200{
201 switch (tpl) {
202 case TPL_APPLICATION:
203 case TPL_CALLBACK:
204 case TPL_NOTIFY:
205 case TPL_HIGH_LEVEL:
206 return EFI_SUCCESS;
207 default:
208 return EFI_INVALID_PARAMETER;
209 }
210}
211
212/**
Mario Six78a88f72018-07-10 08:40:17 +0200213 * efi_signal_event() - signal an EFI event
214 * @event: event to signal
215 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100216 *
Mario Six78a88f72018-07-10 08:40:17 +0200217 * This function signals an event. If the event belongs to an event group all
218 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100219 * their notification function is queued.
220 *
221 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100222 */
223void efi_signal_event(struct efi_event *event, bool check_tpl)
224{
225 if (event->group) {
226 struct efi_event *evt;
227
228 /*
229 * The signaled state has to set before executing any
230 * notification function
231 */
232 list_for_each_entry(evt, &efi_events, link) {
233 if (!evt->group || guidcmp(evt->group, event->group))
234 continue;
235 if (evt->is_signaled)
236 continue;
237 evt->is_signaled = true;
238 if (evt->type & EVT_NOTIFY_SIGNAL &&
239 evt->notify_function)
240 evt->is_queued = true;
241 }
242 list_for_each_entry(evt, &efi_events, link) {
243 if (!evt->group || guidcmp(evt->group, event->group))
244 continue;
245 if (evt->is_queued)
246 efi_queue_event(evt, check_tpl);
247 }
248 } else if (!event->is_signaled) {
249 event->is_signaled = true;
250 if (event->type & EVT_NOTIFY_SIGNAL)
251 efi_queue_event(event, check_tpl);
252 }
253}
254
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200255/**
Mario Six78a88f72018-07-10 08:40:17 +0200256 * efi_raise_tpl() - raise the task priority level
257 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200258 *
259 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200260 *
Mario Six78a88f72018-07-10 08:40:17 +0200261 * See the Unified Extensible Firmware Interface (UEFI) specification for
262 * details.
263 *
264 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200265 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100266static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100267{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100268 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200269
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200270 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200271
272 if (new_tpl < efi_tpl)
273 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
274 efi_tpl = new_tpl;
275 if (efi_tpl > TPL_HIGH_LEVEL)
276 efi_tpl = TPL_HIGH_LEVEL;
277
278 EFI_EXIT(EFI_SUCCESS);
279 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100280}
281
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200282/**
Mario Six78a88f72018-07-10 08:40:17 +0200283 * efi_restore_tpl() - lower the task priority level
284 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200285 *
286 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200287 *
Mario Six78a88f72018-07-10 08:40:17 +0200288 * See the Unified Extensible Firmware Interface (UEFI) specification for
289 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200290 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100291static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100292{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200293 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200294
295 if (old_tpl > efi_tpl)
296 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
297 efi_tpl = old_tpl;
298 if (efi_tpl > TPL_HIGH_LEVEL)
299 efi_tpl = TPL_HIGH_LEVEL;
300
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100301 /*
302 * Lowering the TPL may have made queued events eligible for execution.
303 */
304 efi_timer_check();
305
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200306 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100307}
308
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200309/**
Mario Six78a88f72018-07-10 08:40:17 +0200310 * efi_allocate_pages_ext() - allocate memory pages
311 * @type: type of allocation to be performed
312 * @memory_type: usage type of the allocated memory
313 * @pages: number of pages to be allocated
314 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200315 *
316 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200317 *
Mario Six78a88f72018-07-10 08:40:17 +0200318 * See the Unified Extensible Firmware Interface (UEFI) specification for
319 * details.
320 *
321 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200322 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900323static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100324 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900325 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100326{
327 efi_status_t r;
328
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100329 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100330 r = efi_allocate_pages(type, memory_type, pages, memory);
331 return EFI_EXIT(r);
332}
333
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200334/**
Mario Six78a88f72018-07-10 08:40:17 +0200335 * efi_free_pages_ext() - Free memory pages.
336 * @memory: start of the memory area to be freed
337 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200338 *
339 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200340 *
Mario Six78a88f72018-07-10 08:40:17 +0200341 * See the Unified Extensible Firmware Interface (UEFI) specification for
342 * details.
343 *
344 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200345 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900346static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100347 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100348{
349 efi_status_t r;
350
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100351 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100352 r = efi_free_pages(memory, pages);
353 return EFI_EXIT(r);
354}
355
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200356/**
Mario Six78a88f72018-07-10 08:40:17 +0200357 * efi_get_memory_map_ext() - get map describing memory usage
358 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
359 * on exit the size of the copied memory map
360 * @memory_map: buffer to which the memory map is written
361 * @map_key: key for the memory map
362 * @descriptor_size: size of an individual memory descriptor
363 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200364 *
365 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200366 *
Mario Six78a88f72018-07-10 08:40:17 +0200367 * See the Unified Extensible Firmware Interface (UEFI) specification for
368 * details.
369 *
370 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200371 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900372static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100373 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900374 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100375 efi_uintn_t *map_key,
376 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900377 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100378{
379 efi_status_t r;
380
381 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
382 map_key, descriptor_size, descriptor_version);
383 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
384 descriptor_size, descriptor_version);
385 return EFI_EXIT(r);
386}
387
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200388/**
Mario Six78a88f72018-07-10 08:40:17 +0200389 * efi_allocate_pool_ext() - allocate memory from pool
390 * @pool_type: type of the pool from which memory is to be allocated
391 * @size: number of bytes to be allocated
392 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200393 *
394 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200395 *
Mario Six78a88f72018-07-10 08:40:17 +0200396 * See the Unified Extensible Firmware Interface (UEFI) specification for
397 * details.
398 *
399 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200400 */
Stefan Brünsead12742016-10-09 22:17:18 +0200401static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100402 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200403 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100404{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100405 efi_status_t r;
406
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100407 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200408 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100409 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100410}
411
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200412/**
Mario Six78a88f72018-07-10 08:40:17 +0200413 * efi_free_pool_ext() - free memory from pool
414 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200415 *
416 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200417 *
Mario Six78a88f72018-07-10 08:40:17 +0200418 * See the Unified Extensible Firmware Interface (UEFI) specification for
419 * details.
420 *
421 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200422 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200423static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100424{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100425 efi_status_t r;
426
427 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200428 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100429 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100430}
431
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200432/**
Mario Six78a88f72018-07-10 08:40:17 +0200433 * efi_add_handle() - add a new object to the object list
434 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100435 *
Mario Six78a88f72018-07-10 08:40:17 +0200436 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100437 */
438void efi_add_handle(struct efi_object *obj)
439{
440 if (!obj)
441 return;
442 INIT_LIST_HEAD(&obj->protocols);
443 obj->handle = obj;
444 list_add_tail(&obj->link, &efi_obj_list);
445}
446
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200447/**
Mario Six78a88f72018-07-10 08:40:17 +0200448 * efi_create_handle() - create handle
449 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200450 *
Mario Six78a88f72018-07-10 08:40:17 +0200451 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200452 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100453efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200454{
455 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200456
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200457 obj = calloc(1, sizeof(struct efi_object));
458 if (!obj)
459 return EFI_OUT_OF_RESOURCES;
460
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100461 efi_add_handle(obj);
462 *handle = obj->handle;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200463
464 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200465}
466
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200467/**
Mario Six78a88f72018-07-10 08:40:17 +0200468 * efi_search_protocol() - find a protocol on a handle.
469 * @handle: handle
470 * @protocol_guid: GUID of the protocol
471 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100472 *
Mario Six78a88f72018-07-10 08:40:17 +0200473 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100474 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100475efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100476 const efi_guid_t *protocol_guid,
477 struct efi_handler **handler)
478{
479 struct efi_object *efiobj;
480 struct list_head *lhandle;
481
482 if (!handle || !protocol_guid)
483 return EFI_INVALID_PARAMETER;
484 efiobj = efi_search_obj(handle);
485 if (!efiobj)
486 return EFI_INVALID_PARAMETER;
487 list_for_each(lhandle, &efiobj->protocols) {
488 struct efi_handler *protocol;
489
490 protocol = list_entry(lhandle, struct efi_handler, link);
491 if (!guidcmp(protocol->guid, protocol_guid)) {
492 if (handler)
493 *handler = protocol;
494 return EFI_SUCCESS;
495 }
496 }
497 return EFI_NOT_FOUND;
498}
499
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200500/**
Mario Six78a88f72018-07-10 08:40:17 +0200501 * efi_remove_protocol() - delete protocol from a handle
502 * @handle: handle from which the protocol shall be deleted
503 * @protocol: GUID of the protocol to be deleted
504 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100505 *
Mario Six78a88f72018-07-10 08:40:17 +0200506 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100507 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100508efi_status_t efi_remove_protocol(const efi_handle_t handle,
509 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100510 void *protocol_interface)
511{
512 struct efi_handler *handler;
513 efi_status_t ret;
514
515 ret = efi_search_protocol(handle, protocol, &handler);
516 if (ret != EFI_SUCCESS)
517 return ret;
518 if (guidcmp(handler->guid, protocol))
519 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200520 if (handler->protocol_interface != protocol_interface)
521 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100522 list_del(&handler->link);
523 free(handler);
524 return EFI_SUCCESS;
525}
526
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200527/**
Mario Six78a88f72018-07-10 08:40:17 +0200528 * efi_remove_all_protocols() - delete all protocols from a handle
529 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100530 *
Mario Six78a88f72018-07-10 08:40:17 +0200531 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100532 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100533efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100534{
535 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100536 struct efi_handler *protocol;
537 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100538
539 efiobj = efi_search_obj(handle);
540 if (!efiobj)
541 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100542 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100543 efi_status_t ret;
544
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100545 ret = efi_remove_protocol(handle, protocol->guid,
546 protocol->protocol_interface);
547 if (ret != EFI_SUCCESS)
548 return ret;
549 }
550 return EFI_SUCCESS;
551}
552
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200553/**
Mario Six78a88f72018-07-10 08:40:17 +0200554 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100555 *
Mario Six78a88f72018-07-10 08:40:17 +0200556 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100557 */
558void efi_delete_handle(struct efi_object *obj)
559{
560 if (!obj)
561 return;
562 efi_remove_all_protocols(obj->handle);
563 list_del(&obj->link);
564 free(obj);
565}
566
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200567/**
Mario Six78a88f72018-07-10 08:40:17 +0200568 * efi_is_event() - check if a pointer is a valid event
569 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100570 *
Mario Six78a88f72018-07-10 08:40:17 +0200571 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100572 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100573static efi_status_t efi_is_event(const struct efi_event *event)
574{
575 const struct efi_event *evt;
576
577 if (!event)
578 return EFI_INVALID_PARAMETER;
579 list_for_each_entry(evt, &efi_events, link) {
580 if (evt == event)
581 return EFI_SUCCESS;
582 }
583 return EFI_INVALID_PARAMETER;
584}
Alexander Grafbee91162016-03-04 01:09:59 +0100585
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200586/**
Mario Six78a88f72018-07-10 08:40:17 +0200587 * efi_create_event() - create an event
588 * @type: type of the event to create
589 * @notify_tpl: task priority level of the event
590 * @notify_function: notification function of the event
591 * @notify_context: pointer passed to the notification function
592 * @group: event group
593 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200594 *
595 * This function is used inside U-Boot code to create an event.
596 *
597 * For the API function implementing the CreateEvent service see
598 * efi_create_event_ext.
599 *
Mario Six78a88f72018-07-10 08:40:17 +0200600 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200601 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100602efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200603 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200604 struct efi_event *event,
605 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100606 void *notify_context, efi_guid_t *group,
607 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100608{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100609 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200610
Jonathan Graya95343b2017-03-12 19:26:07 +1100611 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200612 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100613
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200614 switch (type) {
615 case 0:
616 case EVT_TIMER:
617 case EVT_NOTIFY_SIGNAL:
618 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
619 case EVT_NOTIFY_WAIT:
620 case EVT_TIMER | EVT_NOTIFY_WAIT:
621 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
622 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
623 break;
624 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200625 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200626 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100627
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200628 if (is_valid_tpl(notify_tpl) != EFI_SUCCESS)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200629 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100630
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100631 evt = calloc(1, sizeof(struct efi_event));
632 if (!evt)
633 return EFI_OUT_OF_RESOURCES;
634 evt->type = type;
635 evt->notify_tpl = notify_tpl;
636 evt->notify_function = notify_function;
637 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100638 evt->group = group;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100639 /* Disable timers on bootup */
640 evt->trigger_next = -1ULL;
641 evt->is_queued = false;
642 evt->is_signaled = false;
643 list_add_tail(&evt->link, &efi_events);
644 *event = evt;
645 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100646}
647
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200648/*
Mario Six78a88f72018-07-10 08:40:17 +0200649 * efi_create_event_ex() - create an event in a group
650 * @type: type of the event to create
651 * @notify_tpl: task priority level of the event
652 * @notify_function: notification function of the event
653 * @notify_context: pointer passed to the notification function
654 * @event: created event
655 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100656 *
657 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100658 *
Mario Six78a88f72018-07-10 08:40:17 +0200659 * See the Unified Extensible Firmware Interface (UEFI) specification for
660 * details.
661 *
662 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100663 */
664efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
665 void (EFIAPI *notify_function) (
666 struct efi_event *event,
667 void *context),
668 void *notify_context,
669 efi_guid_t *event_group,
670 struct efi_event **event)
671{
672 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
673 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100674 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100675 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100676}
677
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200678/**
Mario Six78a88f72018-07-10 08:40:17 +0200679 * efi_create_event_ext() - create an event
680 * @type: type of the event to create
681 * @notify_tpl: task priority level of the event
682 * @notify_function: notification function of the event
683 * @notify_context: pointer passed to the notification function
684 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200685 *
686 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200687 *
Mario Six78a88f72018-07-10 08:40:17 +0200688 * See the Unified Extensible Firmware Interface (UEFI) specification for
689 * details.
690 *
691 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200692 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200693static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100694 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200695 void (EFIAPI *notify_function) (
696 struct efi_event *event,
697 void *context),
698 void *notify_context, struct efi_event **event)
699{
700 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
701 notify_context);
702 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100703 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200704}
705
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200706/**
Mario Six78a88f72018-07-10 08:40:17 +0200707 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200708 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200709 * Check if a timer event has occurred or a queued notification function should
710 * be called.
711 *
Alexander Grafbee91162016-03-04 01:09:59 +0100712 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200713 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100714 */
715void efi_timer_check(void)
716{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100717 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100718 u64 now = timer_get_us();
719
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100720 list_for_each_entry(evt, &efi_events, link) {
721 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100722 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100723 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200724 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100725 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200726 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100727 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200728 break;
729 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100730 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200731 break;
732 default:
733 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200734 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100735 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100736 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100737 }
Alexander Grafbee91162016-03-04 01:09:59 +0100738 WATCHDOG_RESET();
739}
740
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200741/**
Mario Six78a88f72018-07-10 08:40:17 +0200742 * efi_set_timer() - set the trigger time for a timer event or stop the event
743 * @event: event for which the timer is set
744 * @type: type of the timer
745 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200746 *
747 * This is the function for internal usage in U-Boot. For the API function
748 * implementing the SetTimer service see efi_set_timer_ext.
749 *
Mario Six78a88f72018-07-10 08:40:17 +0200750 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200751 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200752efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200753 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100754{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100755 /* Check that the event is valid */
756 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
757 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100758
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200759 /*
760 * The parameter defines a multiple of 100ns.
761 * We use multiples of 1000ns. So divide by 10.
762 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200763 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100764
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100765 switch (type) {
766 case EFI_TIMER_STOP:
767 event->trigger_next = -1ULL;
768 break;
769 case EFI_TIMER_PERIODIC:
770 case EFI_TIMER_RELATIVE:
771 event->trigger_next = timer_get_us() + trigger_time;
772 break;
773 default:
774 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100775 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100776 event->trigger_type = type;
777 event->trigger_time = trigger_time;
778 event->is_signaled = false;
779 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200780}
781
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200782/**
Mario Six78a88f72018-07-10 08:40:17 +0200783 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
784 * event
785 * @event: event for which the timer is set
786 * @type: type of the timer
787 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200788 *
789 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200790 *
Mario Six78a88f72018-07-10 08:40:17 +0200791 * See the Unified Extensible Firmware Interface (UEFI) specification for
792 * details.
793 *
794 *
795 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200796 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200797static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
798 enum efi_timer_delay type,
799 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200800{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100801 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200802 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100803}
804
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200805/**
Mario Six78a88f72018-07-10 08:40:17 +0200806 * efi_wait_for_event() - wait for events to be signaled
807 * @num_events: number of events to be waited for
808 * @event: events to be waited for
809 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200810 *
811 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200812 *
Mario Six78a88f72018-07-10 08:40:17 +0200813 * See the Unified Extensible Firmware Interface (UEFI) specification for
814 * details.
815 *
816 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200817 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100818static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200819 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100820 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100821{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100822 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100823
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100824 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100825
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200826 /* Check parameters */
827 if (!num_events || !event)
828 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200829 /* Check TPL */
830 if (efi_tpl != TPL_APPLICATION)
831 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200832 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100833 if (efi_is_event(event[i]) != EFI_SUCCESS)
834 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200835 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
836 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200837 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100838 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200839 }
840
841 /* Wait for signal */
842 for (;;) {
843 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200844 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200845 goto out;
846 }
847 /* Allow events to occur. */
848 efi_timer_check();
849 }
850
851out:
852 /*
853 * Reset the signal which is passed to the caller to allow periodic
854 * events to occur.
855 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200856 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200857 if (index)
858 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100859
860 return EFI_EXIT(EFI_SUCCESS);
861}
862
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200863/**
Mario Six78a88f72018-07-10 08:40:17 +0200864 * efi_signal_event_ext() - signal an EFI event
865 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200866 *
867 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200868 *
869 * See the Unified Extensible Firmware Interface (UEFI) specification for
870 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200871 *
872 * This functions sets the signaled state of the event and queues the
873 * notification function for execution.
874 *
Mario Six78a88f72018-07-10 08:40:17 +0200875 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200876 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200877static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100878{
879 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100880 if (efi_is_event(event) != EFI_SUCCESS)
881 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100882 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100883 return EFI_EXIT(EFI_SUCCESS);
884}
885
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200886/**
Mario Six78a88f72018-07-10 08:40:17 +0200887 * efi_close_event() - close an EFI event
888 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200889 *
890 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200891 *
Mario Six78a88f72018-07-10 08:40:17 +0200892 * See the Unified Extensible Firmware Interface (UEFI) specification for
893 * details.
894 *
895 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200896 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200897static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100898{
899 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100900 if (efi_is_event(event) != EFI_SUCCESS)
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
902 list_del(&event->link);
903 free(event);
904 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100905}
906
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200907/**
Mario Six78a88f72018-07-10 08:40:17 +0200908 * efi_check_event() - check if an event is signaled
909 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200910 *
911 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200912 *
Mario Six78a88f72018-07-10 08:40:17 +0200913 * See the Unified Extensible Firmware Interface (UEFI) specification for
914 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200915 *
Mario Six78a88f72018-07-10 08:40:17 +0200916 * If an event is not signaled yet, the notification function is queued. The
917 * signaled state is cleared.
918 *
919 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200920 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200921static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100922{
923 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200924 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100925 if (efi_is_event(event) != EFI_SUCCESS ||
926 event->type & EVT_NOTIFY_SIGNAL)
927 return EFI_EXIT(EFI_INVALID_PARAMETER);
928 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100929 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100930 if (event->is_signaled) {
931 event->is_signaled = false;
932 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200933 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100934 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100935}
936
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200937/**
Mario Six78a88f72018-07-10 08:40:17 +0200938 * efi_search_obj() - find the internal EFI object for a handle
939 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200940 *
Mario Six78a88f72018-07-10 08:40:17 +0200941 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200942 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100943struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200944{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100945 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200946
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100947 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200948 if (efiobj->handle == handle)
949 return efiobj;
950 }
951
952 return NULL;
953}
954
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200955/**
Mario Six78a88f72018-07-10 08:40:17 +0200956 * efi_open_protocol_info_entry() - create open protocol info entry and add it
957 * to a protocol
958 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100959 *
Mario Six78a88f72018-07-10 08:40:17 +0200960 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100961 */
962static struct efi_open_protocol_info_entry *efi_create_open_info(
963 struct efi_handler *handler)
964{
965 struct efi_open_protocol_info_item *item;
966
967 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
968 if (!item)
969 return NULL;
970 /* Append the item to the open protocol info list. */
971 list_add_tail(&item->link, &handler->open_infos);
972
973 return &item->info;
974}
975
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200976/**
Mario Six78a88f72018-07-10 08:40:17 +0200977 * efi_delete_open_info() - remove an open protocol info entry from a protocol
978 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100979 *
Mario Six78a88f72018-07-10 08:40:17 +0200980 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100981 */
982static efi_status_t efi_delete_open_info(
983 struct efi_open_protocol_info_item *item)
984{
985 list_del(&item->link);
986 free(item);
987 return EFI_SUCCESS;
988}
989
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200990/**
Mario Six78a88f72018-07-10 08:40:17 +0200991 * efi_add_protocol() - install new protocol on a handle
992 * @handle: handle on which the protocol shall be installed
993 * @protocol: GUID of the protocol to be installed
994 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200995 *
Mario Six78a88f72018-07-10 08:40:17 +0200996 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200997 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100998efi_status_t efi_add_protocol(const efi_handle_t handle,
999 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001000 void *protocol_interface)
1001{
1002 struct efi_object *efiobj;
1003 struct efi_handler *handler;
1004 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001005
1006 efiobj = efi_search_obj(handle);
1007 if (!efiobj)
1008 return EFI_INVALID_PARAMETER;
1009 ret = efi_search_protocol(handle, protocol, NULL);
1010 if (ret != EFI_NOT_FOUND)
1011 return EFI_INVALID_PARAMETER;
1012 handler = calloc(1, sizeof(struct efi_handler));
1013 if (!handler)
1014 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001015 handler->guid = protocol;
1016 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001017 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001018 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001019 if (!guidcmp(&efi_guid_device_path, protocol))
1020 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001021 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001022}
1023
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001024/**
Mario Six78a88f72018-07-10 08:40:17 +02001025 * efi_install_protocol_interface() - install protocol interface
1026 * @handle: handle on which the protocol shall be installed
1027 * @protocol: GUID of the protocol to be installed
1028 * @protocol_interface_type: type of the interface to be installed,
1029 * always EFI_NATIVE_INTERFACE
1030 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001031 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001032 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001033 *
Mario Six78a88f72018-07-10 08:40:17 +02001034 * See the Unified Extensible Firmware Interface (UEFI) specification for
1035 * details.
1036 *
1037 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001038 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001039static efi_status_t EFIAPI efi_install_protocol_interface(
1040 void **handle, const efi_guid_t *protocol,
1041 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001042{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001043 efi_status_t r;
1044
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001045 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1046 protocol_interface);
1047
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001048 if (!handle || !protocol ||
1049 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1050 r = EFI_INVALID_PARAMETER;
1051 goto out;
1052 }
1053
1054 /* Create new handle if requested. */
1055 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001056 r = efi_create_handle(handle);
1057 if (r != EFI_SUCCESS)
1058 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001059 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1060 *handle);
1061 } else {
1062 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1063 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001064 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001065 /* Add new protocol */
1066 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001067out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001068 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001069}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001070
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001071/**
Mario Six78a88f72018-07-10 08:40:17 +02001072 * efi_get_drivers() - get all drivers associated to a controller
1073 * @efiobj: handle of the controller
1074 * @protocol: protocol guid (optional)
1075 * @number_of_drivers: number of child controllers
1076 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001077 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001078 * The allocated buffer has to be freed with free().
1079 *
Mario Six78a88f72018-07-10 08:40:17 +02001080 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001081 */
1082static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1083 const efi_guid_t *protocol,
1084 efi_uintn_t *number_of_drivers,
1085 efi_handle_t **driver_handle_buffer)
1086{
1087 struct efi_handler *handler;
1088 struct efi_open_protocol_info_item *item;
1089 efi_uintn_t count = 0, i;
1090 bool duplicate;
1091
1092 /* Count all driver associations */
1093 list_for_each_entry(handler, &efiobj->protocols, link) {
1094 if (protocol && guidcmp(handler->guid, protocol))
1095 continue;
1096 list_for_each_entry(item, &handler->open_infos, link) {
1097 if (item->info.attributes &
1098 EFI_OPEN_PROTOCOL_BY_DRIVER)
1099 ++count;
1100 }
1101 }
1102 /*
1103 * Create buffer. In case of duplicate driver assignments the buffer
1104 * will be too large. But that does not harm.
1105 */
1106 *number_of_drivers = 0;
1107 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1108 if (!*driver_handle_buffer)
1109 return EFI_OUT_OF_RESOURCES;
1110 /* Collect unique driver handles */
1111 list_for_each_entry(handler, &efiobj->protocols, link) {
1112 if (protocol && guidcmp(handler->guid, protocol))
1113 continue;
1114 list_for_each_entry(item, &handler->open_infos, link) {
1115 if (item->info.attributes &
1116 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1117 /* Check this is a new driver */
1118 duplicate = false;
1119 for (i = 0; i < *number_of_drivers; ++i) {
1120 if ((*driver_handle_buffer)[i] ==
1121 item->info.agent_handle)
1122 duplicate = true;
1123 }
1124 /* Copy handle to buffer */
1125 if (!duplicate) {
1126 i = (*number_of_drivers)++;
1127 (*driver_handle_buffer)[i] =
1128 item->info.agent_handle;
1129 }
1130 }
1131 }
1132 }
1133 return EFI_SUCCESS;
1134}
1135
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001136/**
Mario Six78a88f72018-07-10 08:40:17 +02001137 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1138 * @efiobj: handle of the controller
1139 * @protocol: protocol guid (optional)
1140 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001141 *
1142 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001143 *
Mario Six78a88f72018-07-10 08:40:17 +02001144 * See the Unified Extensible Firmware Interface (UEFI) specification for
1145 * details.
1146 *
1147 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001148 */
1149static efi_status_t efi_disconnect_all_drivers(
1150 struct efi_object *efiobj,
1151 const efi_guid_t *protocol,
1152 efi_handle_t child_handle)
1153{
1154 efi_uintn_t number_of_drivers;
1155 efi_handle_t *driver_handle_buffer;
1156 efi_status_t r, ret;
1157
1158 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1159 &driver_handle_buffer);
1160 if (ret != EFI_SUCCESS)
1161 return ret;
1162
1163 ret = EFI_NOT_FOUND;
1164 while (number_of_drivers) {
1165 r = EFI_CALL(efi_disconnect_controller(
1166 efiobj->handle,
1167 driver_handle_buffer[--number_of_drivers],
1168 child_handle));
1169 if (r == EFI_SUCCESS)
1170 ret = r;
1171 }
1172 free(driver_handle_buffer);
1173 return ret;
1174}
1175
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001176/**
Mario Six78a88f72018-07-10 08:40:17 +02001177 * efi_uninstall_protocol_interface() - uninstall protocol interface
1178 * @handle: handle from which the protocol shall be removed
1179 * @protocol: GUID of the protocol to be removed
1180 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001181 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001182 * This function implements the UninstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001183 *
Mario Six78a88f72018-07-10 08:40:17 +02001184 * See the Unified Extensible Firmware Interface (UEFI) specification for
1185 * details.
1186 *
1187 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001188 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001189static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001190 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001191 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001192{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001193 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001194 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001195 struct efi_open_protocol_info_item *item;
1196 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001197 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001198
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001199 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1200
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001201 /* Check handle */
1202 efiobj = efi_search_obj(handle);
1203 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001204 r = EFI_INVALID_PARAMETER;
1205 goto out;
1206 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001207 /* Find the protocol on the handle */
1208 r = efi_search_protocol(handle, protocol, &handler);
1209 if (r != EFI_SUCCESS)
1210 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001211 /* Disconnect controllers */
1212 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1213 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001214 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001215 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001216 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001217 /* Close protocol */
1218 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1219 if (item->info.attributes ==
1220 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1221 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1222 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1223 list_del(&item->link);
1224 }
1225 if (!list_empty(&handler->open_infos)) {
1226 r = EFI_ACCESS_DENIED;
1227 goto out;
1228 }
1229 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001230out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001231 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001232}
1233
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001234/**
Mario Six78a88f72018-07-10 08:40:17 +02001235 * efi_register_protocol_notify() - register an event for notification when a
1236 * protocol is installed.
1237 * @protocol: GUID of the protocol whose installation shall be notified
1238 * @event: event to be signaled upon installation of the protocol
1239 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001240 *
1241 * This function implements the RegisterProtocolNotify service.
1242 * See the Unified Extensible Firmware Interface (UEFI) specification
1243 * for details.
1244 *
Mario Six78a88f72018-07-10 08:40:17 +02001245 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001246 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001247static efi_status_t EFIAPI efi_register_protocol_notify(
1248 const efi_guid_t *protocol,
1249 struct efi_event *event,
1250 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001251{
Rob Clark778e6af2017-09-13 18:05:41 -04001252 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001253 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1254}
1255
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001256/**
Mario Six78a88f72018-07-10 08:40:17 +02001257 * efi_search() - determine if an EFI handle implements a protocol
1258 * @search_type: selection criterion
1259 * @protocol: GUID of the protocol
1260 * @search_key: registration key
1261 * @efiobj: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001262 *
1263 * See the documentation of the LocateHandle service in the UEFI specification.
1264 *
Mario Six78a88f72018-07-10 08:40:17 +02001265 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001266 */
Alexander Grafbee91162016-03-04 01:09:59 +01001267static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001268 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001269 struct efi_object *efiobj)
1270{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001271 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001272
1273 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001274 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001275 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001276 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001277 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001278 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001279 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001280 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1281 return (ret != EFI_SUCCESS);
1282 default:
1283 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001284 return -1;
1285 }
Alexander Grafbee91162016-03-04 01:09:59 +01001286}
1287
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001288/**
Mario Six78a88f72018-07-10 08:40:17 +02001289 * efi_locate_handle() - locate handles implementing a protocol
1290 * @search_type: selection criterion
1291 * @protocol: GUID of the protocol
1292 * @search_key: registration key
1293 * @buffer_size: size of the buffer to receive the handles in bytes
1294 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001295 *
1296 * This function is meant for U-Boot internal calls. For the API implementation
1297 * of the LocateHandle service see efi_locate_handle_ext.
1298 *
Mario Six78a88f72018-07-10 08:40:17 +02001299 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001300 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001301static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001302 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001303 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001304 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001305{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001306 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001307 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001308
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001309 /* Check parameters */
1310 switch (search_type) {
1311 case ALL_HANDLES:
1312 break;
1313 case BY_REGISTER_NOTIFY:
1314 if (!search_key)
1315 return EFI_INVALID_PARAMETER;
1316 /* RegisterProtocolNotify is not implemented yet */
1317 return EFI_UNSUPPORTED;
1318 case BY_PROTOCOL:
1319 if (!protocol)
1320 return EFI_INVALID_PARAMETER;
1321 break;
1322 default:
1323 return EFI_INVALID_PARAMETER;
1324 }
1325
1326 /*
1327 * efi_locate_handle_buffer uses this function for
1328 * the calculation of the necessary buffer size.
1329 * So do not require a buffer for buffersize == 0.
1330 */
1331 if (!buffer_size || (*buffer_size && !buffer))
1332 return EFI_INVALID_PARAMETER;
1333
Alexander Grafbee91162016-03-04 01:09:59 +01001334 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001335 list_for_each_entry(efiobj, &efi_obj_list, link) {
1336 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001337 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001338 }
1339
1340 if (*buffer_size < size) {
1341 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001342 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001343 }
1344
Rob Clark796a78c2017-08-06 14:10:07 -04001345 *buffer_size = size;
1346 if (size == 0)
1347 return EFI_NOT_FOUND;
1348
Alexander Grafbee91162016-03-04 01:09:59 +01001349 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001350 list_for_each_entry(efiobj, &efi_obj_list, link) {
1351 if (!efi_search(search_type, protocol, search_key, efiobj))
1352 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001353 }
1354
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001355 return EFI_SUCCESS;
1356}
1357
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001358/**
Mario Six78a88f72018-07-10 08:40:17 +02001359 * efi_locate_handle_ext() - locate handles implementing a protocol.
1360 * @search_type: selection criterion
1361 * @protocol: GUID of the protocol
1362 * @search_key: registration key
1363 * @buffer_size: size of the buffer to receive the handles in bytes
1364 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001365 *
1366 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001367 *
Mario Six78a88f72018-07-10 08:40:17 +02001368 * See the Unified Extensible Firmware Interface (UEFI) specification for
1369 * details.
1370 *
1371 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001372 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001373static efi_status_t EFIAPI efi_locate_handle_ext(
1374 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001375 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001376 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001377{
Rob Clark778e6af2017-09-13 18:05:41 -04001378 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001379 buffer_size, buffer);
1380
1381 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1382 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001383}
1384
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001385/**
Mario Six78a88f72018-07-10 08:40:17 +02001386 * efi_remove_configuration_table() - collapses configuration table entries,
1387 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001388 *
Mario Six78a88f72018-07-10 08:40:17 +02001389 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001390 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001391static void efi_remove_configuration_table(int i)
1392{
1393 struct efi_configuration_table *this = &efi_conf_table[i];
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001394 struct efi_configuration_table *next = &efi_conf_table[i + 1];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001395 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1396
1397 memmove(this, next, (ulong)end - (ulong)next);
1398 systab.nr_tables--;
1399}
1400
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001401/**
Mario Six78a88f72018-07-10 08:40:17 +02001402 * efi_install_configuration_table() - adds, updates, or removes a
1403 * configuration table
1404 * @guid: GUID of the installed table
1405 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001406 *
1407 * This function is used for internal calls. For the API implementation of the
1408 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1409 *
Mario Six78a88f72018-07-10 08:40:17 +02001410 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001411 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001412efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1413 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001414{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001415 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001416 int i;
1417
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001418 if (!guid)
1419 return EFI_INVALID_PARAMETER;
1420
Alexander Grafbee91162016-03-04 01:09:59 +01001421 /* Check for guid override */
1422 for (i = 0; i < systab.nr_tables; i++) {
1423 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001424 if (table)
1425 efi_conf_table[i].table = table;
1426 else
1427 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001428 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001429 }
1430 }
1431
Alexander Grafd98cdf62017-07-26 13:41:04 +02001432 if (!table)
1433 return EFI_NOT_FOUND;
1434
Alexander Grafbee91162016-03-04 01:09:59 +01001435 /* No override, check for overflow */
1436 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001437 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001438
1439 /* Add a new entry */
1440 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1441 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001442 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001443
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001444out:
1445 /* Notify that the configuration table was changed */
1446 list_for_each_entry(evt, &efi_events, link) {
1447 if (evt->group && !guidcmp(evt->group, guid)) {
1448 efi_signal_event(evt, false);
1449 break;
1450 }
1451 }
1452
Alexander Graf488bf122016-08-19 01:23:24 +02001453 return EFI_SUCCESS;
1454}
1455
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001456/**
Mario Six78a88f72018-07-10 08:40:17 +02001457 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1458 * configuration table.
1459 * @guid: GUID of the installed table
1460 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001461 *
1462 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001463 *
Mario Six78a88f72018-07-10 08:40:17 +02001464 * See the Unified Extensible Firmware Interface (UEFI) specification for
1465 * details.
1466 *
1467 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001468 */
Alexander Graf488bf122016-08-19 01:23:24 +02001469static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1470 void *table)
1471{
Rob Clark778e6af2017-09-13 18:05:41 -04001472 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001473 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001474}
1475
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001476/**
Mario Six78a88f72018-07-10 08:40:17 +02001477 * efi_setup_loaded_image() - initialize a loaded image
1478 * @info: loaded image info to be passed to the entry point of the image
1479 * @obj: internal object associated with the loaded image
1480 * @device_path: device path of the loaded image
1481 * @file_path: file path of the loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001482 *
1483 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001484 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001485 *
Mario Six78a88f72018-07-10 08:40:17 +02001486 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001487 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001488efi_status_t efi_setup_loaded_image(
1489 struct efi_loaded_image *info, struct efi_object *obj,
1490 struct efi_device_path *device_path,
1491 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001492{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001493 efi_status_t ret;
1494
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001495 /* Add internal object to object list */
1496 efi_add_handle(obj);
1497 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001498 obj->handle = info;
1499
Rob Clark95c55532017-09-13 18:05:33 -04001500 info->file_path = file_path;
Rob Clark95c55532017-09-13 18:05:33 -04001501
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001502 if (device_path) {
1503 info->device_handle = efi_dp_find_obj(device_path, NULL);
1504 /*
1505 * When asking for the device path interface, return
1506 * bootefi_device_path
1507 */
1508 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1509 device_path);
1510 if (ret != EFI_SUCCESS)
1511 goto failure;
1512 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001513
1514 /*
1515 * When asking for the loaded_image interface, just
1516 * return handle which points to loaded_image_info
1517 */
1518 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1519 if (ret != EFI_SUCCESS)
1520 goto failure;
1521
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001522 ret = efi_add_protocol(obj->handle,
1523 &efi_guid_device_path_to_text_protocol,
1524 (void *)&efi_device_path_to_text);
1525 if (ret != EFI_SUCCESS)
1526 goto failure;
1527
Leif Lindholme70f8df2018-03-09 17:43:21 +01001528 ret = efi_add_protocol(obj->handle,
1529 &efi_guid_device_path_utilities_protocol,
1530 (void *)&efi_device_path_utilities);
1531 if (ret != EFI_SUCCESS)
1532 goto failure;
1533
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001534 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001535failure:
1536 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001537 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001538}
1539
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001540/**
Mario Six78a88f72018-07-10 08:40:17 +02001541 * efi_load_image_from_path() - load an image using a file path
1542 * @file_path: the path of the image to load
1543 * @buffer: buffer containing the loaded image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001544 *
Mario Six78a88f72018-07-10 08:40:17 +02001545 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001546 */
Rob Clark9975fe92017-09-13 18:05:38 -04001547efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1548 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001549{
1550 struct efi_file_info *info = NULL;
1551 struct efi_file_handle *f;
1552 static efi_status_t ret;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001553 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001554
1555 f = efi_file_from_path(file_path);
1556 if (!f)
1557 return EFI_DEVICE_ERROR;
1558
1559 bs = 0;
1560 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1561 &bs, info));
1562 if (ret == EFI_BUFFER_TOO_SMALL) {
1563 info = malloc(bs);
1564 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1565 &bs, info));
1566 }
1567 if (ret != EFI_SUCCESS)
1568 goto error;
1569
1570 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1571 if (ret)
1572 goto error;
1573
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001574 bs = info->file_size;
1575 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark838ee4b2017-09-13 18:05:35 -04001576
1577error:
1578 free(info);
1579 EFI_CALL(f->close(f));
1580
1581 if (ret != EFI_SUCCESS) {
1582 efi_free_pool(*buffer);
1583 *buffer = NULL;
1584 }
1585
1586 return ret;
1587}
1588
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001589/**
Mario Six78a88f72018-07-10 08:40:17 +02001590 * efi_load_image() - load an EFI image into memory
1591 * @boot_policy: true for request originating from the boot manager
1592 * @parent_image: the caller's image handle
1593 * @file_path: the path of the image to load
1594 * @source_buffer: memory location from which the image is installed
1595 * @source_size: size of the memory area from which the image is installed
1596 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001597 *
1598 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001599 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001600 * See the Unified Extensible Firmware Interface (UEFI) specification
1601 * for details.
1602 *
Mario Six78a88f72018-07-10 08:40:17 +02001603 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001604 */
Alexander Grafbee91162016-03-04 01:09:59 +01001605static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1606 efi_handle_t parent_image,
1607 struct efi_device_path *file_path,
1608 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001609 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001610 efi_handle_t *image_handle)
1611{
Alexander Grafbee91162016-03-04 01:09:59 +01001612 struct efi_loaded_image *info;
1613 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001614 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001615
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001616 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001617 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001618
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001619 if (!image_handle || !parent_image) {
1620 ret = EFI_INVALID_PARAMETER;
1621 goto error;
1622 }
1623
1624 if (!source_buffer && !file_path) {
1625 ret = EFI_NOT_FOUND;
1626 goto error;
1627 }
1628
Rob Clark838ee4b2017-09-13 18:05:35 -04001629 info = calloc(1, sizeof(*info));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001630 if (!info) {
1631 ret = EFI_OUT_OF_RESOURCES;
1632 goto error;
1633 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001634 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001635 if (!obj) {
1636 free(info);
1637 ret = EFI_OUT_OF_RESOURCES;
1638 goto error;
1639 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001640
1641 if (!source_buffer) {
1642 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001643
Rob Clark9975fe92017-09-13 18:05:38 -04001644 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001645 if (ret != EFI_SUCCESS)
1646 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001647 /*
1648 * split file_path which contains both the device and
1649 * file parts:
1650 */
1651 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001652 ret = efi_setup_loaded_image(info, obj, dp, fp);
1653 if (ret != EFI_SUCCESS)
1654 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001655 } else {
1656 /* In this case, file_path is the "device" path, ie.
1657 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1658 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001659 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1660 if (ret != EFI_SUCCESS)
1661 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001662 }
Alexander Grafbee91162016-03-04 01:09:59 +01001663 info->reserved = efi_load_pe(source_buffer, info);
1664 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001665 ret = EFI_UNSUPPORTED;
1666 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001667 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001668 info->system_table = &systab;
1669 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001670 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001671 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001672failure:
1673 free(info);
1674 efi_delete_handle(obj);
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001675error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001676 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001677}
1678
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001679/**
Mario Six78a88f72018-07-10 08:40:17 +02001680 * efi_start_image() - dall the entry point of an image
1681 * @image_handle: handle of the image
1682 * @exit_data_size: size of the buffer
1683 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001684 *
1685 * This function implements the StartImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001686 *
Mario Six78a88f72018-07-10 08:40:17 +02001687 * See the Unified Extensible Firmware Interface (UEFI) specification for
1688 * details.
1689 *
1690 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001691 */
Alexander Grafbee91162016-03-04 01:09:59 +01001692static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1693 unsigned long *exit_data_size,
1694 s16 **exit_data)
1695{
Alexander Grafc6fa5df2018-01-24 00:18:08 +01001696 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1697 struct efi_system_table *st);
Alexander Grafbee91162016-03-04 01:09:59 +01001698 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001699 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001700
1701 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1702 entry = info->reserved;
1703
1704 efi_is_direct_boot = false;
1705
1706 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001707 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001708 /*
1709 * We called the entry point of the child image with EFI_CALL
1710 * in the lines below. The child image called the Exit() boot
1711 * service efi_exit() which executed the long jump that brought
1712 * us to the current line. This implies that the second half
1713 * of the EFI_CALL macro has not been executed.
1714 */
1715#ifdef CONFIG_ARM
1716 /*
1717 * efi_exit() called efi_restore_gd(). We have to undo this
1718 * otherwise __efi_entry_check() will put the wrong value into
1719 * app_gd.
1720 */
1721 gd = app_gd;
1722#endif
1723 /*
1724 * To get ready to call EFI_EXIT below we have to execute the
1725 * missed out steps of EFI_CALL.
1726 */
1727 assert(__efi_entry_check());
1728 debug("%sEFI: %lu returned by started image\n",
1729 __efi_nesting_dec(),
1730 (unsigned long)((uintptr_t)info->exit_status &
1731 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001732 return EFI_EXIT(info->exit_status);
1733 }
1734
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001735 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001736
Alexander Graf56672bf2018-01-26 00:47:53 +01001737 /*
1738 * Usually UEFI applications call Exit() instead of returning.
1739 * But because the world doesn not consist of ponies and unicorns,
1740 * we're happy to emulate that behavior on behalf of a payload
1741 * that forgot.
1742 */
1743 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafbee91162016-03-04 01:09:59 +01001744}
1745
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001746/**
Mario Six78a88f72018-07-10 08:40:17 +02001747 * efi_exit() - leave an EFI application or driver
1748 * @image_handle: handle of the application or driver that is exiting
1749 * @exit_status: status code
1750 * @exit_data_size: size of the buffer in bytes
1751 * @exit_data: buffer with data describing an error
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001752 *
1753 * This function implements the Exit service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001754 *
Mario Six78a88f72018-07-10 08:40:17 +02001755 * See the Unified Extensible Firmware Interface (UEFI) specification for
1756 * details.
1757 *
1758 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001759 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001760static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001761 efi_status_t exit_status,
1762 unsigned long exit_data_size,
1763 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001764{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001765 /*
1766 * We require that the handle points to the original loaded
1767 * image protocol interface.
1768 *
1769 * For getting the longjmp address this is safer than locating
1770 * the protocol because the protocol may have been reinstalled
1771 * pointing to another memory location.
1772 *
1773 * TODO: We should call the unload procedure of the loaded
1774 * image protocol.
1775 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001776 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001777
Alexander Grafbee91162016-03-04 01:09:59 +01001778 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1779 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001780
Alexander Grafa1489202017-09-03 14:14:17 +02001781 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001782 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001783
Alexander Grafa1489202017-09-03 14:14:17 +02001784 /*
1785 * But longjmp out with the U-Boot gd, not the application's, as
1786 * the other end is a setjmp call inside EFI context.
1787 */
1788 efi_restore_gd();
1789
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001790 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001791 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001792
1793 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001794}
1795
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001796/**
Mario Six78a88f72018-07-10 08:40:17 +02001797 * efi_unload_image() - unload an EFI image
1798 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001799 *
1800 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001801 *
Mario Six78a88f72018-07-10 08:40:17 +02001802 * See the Unified Extensible Firmware Interface (UEFI) specification for
1803 * details.
1804 *
1805 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001806 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001807static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001808{
1809 struct efi_object *efiobj;
1810
1811 EFI_ENTRY("%p", image_handle);
1812 efiobj = efi_search_obj(image_handle);
1813 if (efiobj)
1814 list_del(&efiobj->link);
1815
1816 return EFI_EXIT(EFI_SUCCESS);
1817}
1818
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001819/**
Mario Six78a88f72018-07-10 08:40:17 +02001820 * efi_exit_caches() - fix up caches for EFI payloads if necessary
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001821 */
Alexander Grafbee91162016-03-04 01:09:59 +01001822static void efi_exit_caches(void)
1823{
1824#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1825 /*
1826 * Grub on 32bit ARM needs to have caches disabled before jumping into
1827 * a zImage, but does not know of all cache layers. Give it a hand.
1828 */
1829 if (efi_is_direct_boot)
1830 cleanup_before_linux();
1831#endif
1832}
1833
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001834/**
Mario Six78a88f72018-07-10 08:40:17 +02001835 * efi_exit_boot_services() - stop all boot services
1836 * @image_handle: handle of the loaded image
1837 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001838 *
1839 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001840 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001841 * See the Unified Extensible Firmware Interface (UEFI) specification
1842 * for details.
1843 *
Mario Six78a88f72018-07-10 08:40:17 +02001844 * All timer events are disabled. For exit boot services events the
1845 * notification function is called. The boot services are disabled in the
1846 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001847 *
Mario Six78a88f72018-07-10 08:40:17 +02001848 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001849 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001850static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001851 unsigned long map_key)
1852{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001853 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001854
Alexander Grafbee91162016-03-04 01:09:59 +01001855 EFI_ENTRY("%p, %ld", image_handle, map_key);
1856
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001857 /* Check that the caller has read the current memory map */
1858 if (map_key != efi_memory_map_key)
1859 return EFI_INVALID_PARAMETER;
1860
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001861 /* Make sure that notification functions are not called anymore */
1862 efi_tpl = TPL_HIGH_LEVEL;
1863
1864 /* Check if ExitBootServices has already been called */
1865 if (!systab.boottime)
1866 return EFI_EXIT(EFI_SUCCESS);
1867
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001868 /* Add related events to the event group */
1869 list_for_each_entry(evt, &efi_events, link) {
1870 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1871 evt->group = &efi_guid_event_group_exit_boot_services;
1872 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001873 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001874 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001875 if (evt->group &&
1876 !guidcmp(evt->group,
1877 &efi_guid_event_group_exit_boot_services)) {
1878 efi_signal_event(evt, false);
1879 break;
1880 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001881 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001882
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001883 /* TODO Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001884
Alexander Grafb7b84102016-11-17 01:02:57 +01001885 board_quiesce_devices();
1886
Alexander Grafbee91162016-03-04 01:09:59 +01001887 /* Fix up caches for EFI payloads if necessary */
1888 efi_exit_caches();
1889
1890 /* This stops all lingering devices */
1891 bootm_disable_interrupts();
1892
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001893 /* Disable boottime services */
1894 systab.con_in_handle = NULL;
1895 systab.con_in = NULL;
1896 systab.con_out_handle = NULL;
1897 systab.con_out = NULL;
1898 systab.stderr_handle = NULL;
1899 systab.std_err = NULL;
1900 systab.boottime = NULL;
1901
1902 /* Recalculate CRC32 */
1903 systab.hdr.crc32 = 0;
1904 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1905 sizeof(struct efi_system_table));
1906
Alexander Grafbee91162016-03-04 01:09:59 +01001907 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001908 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001909 WATCHDOG_RESET();
1910
1911 return EFI_EXIT(EFI_SUCCESS);
1912}
1913
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001914/**
Mario Six78a88f72018-07-10 08:40:17 +02001915 * efi_get_next_monotonic_count() - get next value of the counter
1916 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001917 *
1918 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001919 *
Mario Six78a88f72018-07-10 08:40:17 +02001920 * See the Unified Extensible Firmware Interface (UEFI) specification for
1921 * details.
1922 *
1923 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001924 */
Alexander Grafbee91162016-03-04 01:09:59 +01001925static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1926{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001927 static uint64_t mono;
1928
Alexander Grafbee91162016-03-04 01:09:59 +01001929 EFI_ENTRY("%p", count);
1930 *count = mono++;
1931 return EFI_EXIT(EFI_SUCCESS);
1932}
1933
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001934/**
Mario Six78a88f72018-07-10 08:40:17 +02001935 * efi_stall() - sleep
1936 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001937 *
Mario Six78a88f72018-07-10 08:40:17 +02001938 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001939 *
Mario Six78a88f72018-07-10 08:40:17 +02001940 * See the Unified Extensible Firmware Interface (UEFI) specification for
1941 * details.
1942 *
1943 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001944 */
Alexander Grafbee91162016-03-04 01:09:59 +01001945static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1946{
1947 EFI_ENTRY("%ld", microseconds);
1948 udelay(microseconds);
1949 return EFI_EXIT(EFI_SUCCESS);
1950}
1951
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001952/**
Mario Six78a88f72018-07-10 08:40:17 +02001953 * efi_set_watchdog_timer() - reset the watchdog timer
1954 * @timeout: seconds before reset by watchdog
1955 * @watchdog_code: code to be logged when resetting
1956 * @data_size: size of buffer in bytes
1957 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001958 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001959 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001960 *
Mario Six78a88f72018-07-10 08:40:17 +02001961 * See the Unified Extensible Firmware Interface (UEFI) specification for
1962 * details.
1963 *
1964 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001965 */
Alexander Grafbee91162016-03-04 01:09:59 +01001966static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1967 uint64_t watchdog_code,
1968 unsigned long data_size,
1969 uint16_t *watchdog_data)
1970{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001971 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001972 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001973 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001974}
1975
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001976/**
Mario Six78a88f72018-07-10 08:40:17 +02001977 * efi_close_protocol() - close a protocol
1978 * @handle: handle on which the protocol shall be closed
1979 * @protocol: GUID of the protocol to close
1980 * @agent_handle: handle of the driver
1981 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001982 *
1983 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001984 *
Mario Six78a88f72018-07-10 08:40:17 +02001985 * See the Unified Extensible Firmware Interface (UEFI) specification for
1986 * details.
1987 *
1988 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001989 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001990static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001991 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001992 efi_handle_t agent_handle,
1993 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001994{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001995 struct efi_handler *handler;
1996 struct efi_open_protocol_info_item *item;
1997 struct efi_open_protocol_info_item *pos;
1998 efi_status_t r;
1999
Rob Clark778e6af2017-09-13 18:05:41 -04002000 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002001 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002002
2003 if (!agent_handle) {
2004 r = EFI_INVALID_PARAMETER;
2005 goto out;
2006 }
2007 r = efi_search_protocol(handle, protocol, &handler);
2008 if (r != EFI_SUCCESS)
2009 goto out;
2010
2011 r = EFI_NOT_FOUND;
2012 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2013 if (item->info.agent_handle == agent_handle &&
2014 item->info.controller_handle == controller_handle) {
2015 efi_delete_open_info(item);
2016 r = EFI_SUCCESS;
2017 break;
2018 }
2019 }
2020out:
2021 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002022}
2023
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002024/**
Mario Six78a88f72018-07-10 08:40:17 +02002025 * efi_open_protocol_information() - provide information about then open status
2026 * of a protocol on a handle
2027 * @handle: handle for which the information shall be retrieved
2028 * @protocol: GUID of the protocol
2029 * @entry_buffer: buffer to receive the open protocol information
2030 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002031 *
2032 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002033 *
Mario Six78a88f72018-07-10 08:40:17 +02002034 * See the Unified Extensible Firmware Interface (UEFI) specification for
2035 * details.
2036 *
2037 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002038 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002039static efi_status_t EFIAPI efi_open_protocol_information(
2040 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002041 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002042 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002043{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002044 unsigned long buffer_size;
2045 unsigned long count;
2046 struct efi_handler *handler;
2047 struct efi_open_protocol_info_item *item;
2048 efi_status_t r;
2049
Rob Clark778e6af2017-09-13 18:05:41 -04002050 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002051 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002052
2053 /* Check parameters */
2054 if (!entry_buffer) {
2055 r = EFI_INVALID_PARAMETER;
2056 goto out;
2057 }
2058 r = efi_search_protocol(handle, protocol, &handler);
2059 if (r != EFI_SUCCESS)
2060 goto out;
2061
2062 /* Count entries */
2063 count = 0;
2064 list_for_each_entry(item, &handler->open_infos, link) {
2065 if (item->info.open_count)
2066 ++count;
2067 }
2068 *entry_count = count;
2069 *entry_buffer = NULL;
2070 if (!count) {
2071 r = EFI_SUCCESS;
2072 goto out;
2073 }
2074
2075 /* Copy entries */
2076 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2077 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2078 (void **)entry_buffer);
2079 if (r != EFI_SUCCESS)
2080 goto out;
2081 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2082 if (item->info.open_count)
2083 (*entry_buffer)[--count] = item->info;
2084 }
2085out:
2086 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002087}
2088
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002089/**
Mario Six78a88f72018-07-10 08:40:17 +02002090 * efi_protocols_per_handle() - get protocols installed on a handle
2091 * @handle: handle for which the information is retrieved
2092 * @protocol_buffer: buffer with protocol GUIDs
2093 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002094 *
2095 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002096 *
Mario Six78a88f72018-07-10 08:40:17 +02002097 * See the Unified Extensible Firmware Interface (UEFI) specification for
2098 * details.
2099 *
2100 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002101 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002102static efi_status_t EFIAPI efi_protocols_per_handle(
2103 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002104 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002105{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002106 unsigned long buffer_size;
2107 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002108 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002109 efi_status_t r;
2110
Alexander Grafbee91162016-03-04 01:09:59 +01002111 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2112 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002113
2114 if (!handle || !protocol_buffer || !protocol_buffer_count)
2115 return EFI_EXIT(EFI_INVALID_PARAMETER);
2116
2117 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002118 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002119
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002120 efiobj = efi_search_obj(handle);
2121 if (!efiobj)
2122 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002123
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002124 /* Count protocols */
2125 list_for_each(protocol_handle, &efiobj->protocols) {
2126 ++*protocol_buffer_count;
2127 }
2128
2129 /* Copy guids */
2130 if (*protocol_buffer_count) {
2131 size_t j = 0;
2132
2133 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2134 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2135 (void **)protocol_buffer);
2136 if (r != EFI_SUCCESS)
2137 return EFI_EXIT(r);
2138 list_for_each(protocol_handle, &efiobj->protocols) {
2139 struct efi_handler *protocol;
2140
2141 protocol = list_entry(protocol_handle,
2142 struct efi_handler, link);
2143 (*protocol_buffer)[j] = (void *)protocol->guid;
2144 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002145 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002146 }
2147
2148 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002149}
2150
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002151/**
Mario Six78a88f72018-07-10 08:40:17 +02002152 * efi_locate_handle_buffer() - locate handles implementing a protocol
2153 * @search_type: selection criterion
2154 * @protocol: GUID of the protocol
2155 * @search_key: registration key
2156 * @no_handles: number of returned handles
2157 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002158 *
2159 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002160 *
Mario Six78a88f72018-07-10 08:40:17 +02002161 * See the Unified Extensible Firmware Interface (UEFI) specification for
2162 * details.
2163 *
2164 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002165 */
Alexander Grafbee91162016-03-04 01:09:59 +01002166static efi_status_t EFIAPI efi_locate_handle_buffer(
2167 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002168 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002169 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002170{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002171 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002172 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002173
Rob Clark778e6af2017-09-13 18:05:41 -04002174 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002175 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002176
2177 if (!no_handles || !buffer) {
2178 r = EFI_INVALID_PARAMETER;
2179 goto out;
2180 }
2181 *no_handles = 0;
2182 *buffer = NULL;
2183 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2184 *buffer);
2185 if (r != EFI_BUFFER_TOO_SMALL)
2186 goto out;
2187 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2188 (void **)buffer);
2189 if (r != EFI_SUCCESS)
2190 goto out;
2191 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2192 *buffer);
2193 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002194 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002195out:
2196 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002197}
2198
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002199/**
Mario Six78a88f72018-07-10 08:40:17 +02002200 * efi_locate_protocol() - find an interface implementing a protocol
2201 * @protocol: GUID of the protocol
2202 * @registration: registration key passed to the notification function
2203 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002204 *
2205 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002206 *
Mario Six78a88f72018-07-10 08:40:17 +02002207 * See the Unified Extensible Firmware Interface (UEFI) specification for
2208 * details.
2209 *
2210 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002211 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002212static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002213 void *registration,
2214 void **protocol_interface)
2215{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002216 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002217 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002218
Rob Clark778e6af2017-09-13 18:05:41 -04002219 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002220
2221 if (!protocol || !protocol_interface)
2222 return EFI_EXIT(EFI_INVALID_PARAMETER);
2223
2224 list_for_each(lhandle, &efi_obj_list) {
2225 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002226 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002227
2228 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002229
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002230 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2231 if (ret == EFI_SUCCESS) {
2232 *protocol_interface = handler->protocol_interface;
2233 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002234 }
2235 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002236 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002237
2238 return EFI_EXIT(EFI_NOT_FOUND);
2239}
2240
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002241/**
Mario Six78a88f72018-07-10 08:40:17 +02002242 * efi_locate_device_path() - Get the device path and handle of an device
2243 * implementing a protocol
2244 * @protocol: GUID of the protocol
2245 * @device_path: device path
2246 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002247 *
2248 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002249 *
Mario Six78a88f72018-07-10 08:40:17 +02002250 * See the Unified Extensible Firmware Interface (UEFI) specification for
2251 * details.
2252 *
2253 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002254 */
2255static efi_status_t EFIAPI efi_locate_device_path(
2256 const efi_guid_t *protocol,
2257 struct efi_device_path **device_path,
2258 efi_handle_t *device)
2259{
2260 struct efi_device_path *dp;
2261 size_t i;
2262 struct efi_handler *handler;
2263 efi_handle_t *handles;
2264 size_t len, len_dp;
2265 size_t len_best = 0;
2266 efi_uintn_t no_handles;
2267 u8 *remainder;
2268 efi_status_t ret;
2269
2270 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2271
2272 if (!protocol || !device_path || !*device_path || !device) {
2273 ret = EFI_INVALID_PARAMETER;
2274 goto out;
2275 }
2276
2277 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002278 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002279
2280 /* Get all handles implementing the protocol */
2281 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2282 &no_handles, &handles));
2283 if (ret != EFI_SUCCESS)
2284 goto out;
2285
2286 for (i = 0; i < no_handles; ++i) {
2287 /* Find the device path protocol */
2288 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2289 &handler);
2290 if (ret != EFI_SUCCESS)
2291 continue;
2292 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002293 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002294 /*
2295 * This handle can only be a better fit
2296 * if its device path length is longer than the best fit and
2297 * if its device path length is shorter of equal the searched
2298 * device path.
2299 */
2300 if (len_dp <= len_best || len_dp > len)
2301 continue;
2302 /* Check if dp is a subpath of device_path */
2303 if (memcmp(*device_path, dp, len_dp))
2304 continue;
2305 *device = handles[i];
2306 len_best = len_dp;
2307 }
2308 if (len_best) {
2309 remainder = (u8 *)*device_path + len_best;
2310 *device_path = (struct efi_device_path *)remainder;
2311 ret = EFI_SUCCESS;
2312 } else {
2313 ret = EFI_NOT_FOUND;
2314 }
2315out:
2316 return EFI_EXIT(ret);
2317}
2318
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002319/**
Mario Six78a88f72018-07-10 08:40:17 +02002320 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2321 * interfaces
2322 * @handle: handle on which the protocol interfaces shall be installed
2323 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2324 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002325 *
2326 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002327 *
Mario Six78a88f72018-07-10 08:40:17 +02002328 * See the Unified Extensible Firmware Interface (UEFI) specification for
2329 * details.
2330 *
2331 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002332 */
Alexander Grafbee91162016-03-04 01:09:59 +01002333static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2334 void **handle, ...)
2335{
2336 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002337
Alexander Grafbeb077a2018-06-18 17:23:05 +02002338 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002339 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002340 void *protocol_interface;
2341 efi_status_t r = EFI_SUCCESS;
2342 int i = 0;
2343
2344 if (!handle)
2345 return EFI_EXIT(EFI_INVALID_PARAMETER);
2346
Alexander Grafbeb077a2018-06-18 17:23:05 +02002347 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002348 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002349 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002350 if (!protocol)
2351 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002352 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002353 r = EFI_CALL(efi_install_protocol_interface(
2354 handle, protocol,
2355 EFI_NATIVE_INTERFACE,
2356 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002357 if (r != EFI_SUCCESS)
2358 break;
2359 i++;
2360 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002361 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002362 if (r == EFI_SUCCESS)
2363 return EFI_EXIT(r);
2364
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002365 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002366 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002367 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002368 protocol = efi_va_arg(argptr, efi_guid_t*);
2369 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002370 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2371 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002372 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002373 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002374
2375 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002376}
2377
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002378/**
Mario Six78a88f72018-07-10 08:40:17 +02002379 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2380 * interfaces
2381 * @handle: handle from which the protocol interfaces shall be removed
2382 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2383 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002384 *
2385 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002386 *
Mario Six78a88f72018-07-10 08:40:17 +02002387 * See the Unified Extensible Firmware Interface (UEFI) specification for
2388 * details.
2389 *
2390 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002391 */
Alexander Grafbee91162016-03-04 01:09:59 +01002392static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2393 void *handle, ...)
2394{
2395 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002396
Alexander Grafbeb077a2018-06-18 17:23:05 +02002397 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002398 const efi_guid_t *protocol;
2399 void *protocol_interface;
2400 efi_status_t r = EFI_SUCCESS;
2401 size_t i = 0;
2402
2403 if (!handle)
2404 return EFI_EXIT(EFI_INVALID_PARAMETER);
2405
Alexander Grafbeb077a2018-06-18 17:23:05 +02002406 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002407 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002408 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002409 if (!protocol)
2410 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002411 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002412 r = EFI_CALL(efi_uninstall_protocol_interface(
2413 handle, protocol,
2414 protocol_interface));
2415 if (r != EFI_SUCCESS)
2416 break;
2417 i++;
2418 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002419 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002420 if (r == EFI_SUCCESS)
2421 return EFI_EXIT(r);
2422
2423 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002424 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002425 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002426 protocol = efi_va_arg(argptr, efi_guid_t*);
2427 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002428 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2429 EFI_NATIVE_INTERFACE,
2430 protocol_interface));
2431 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002432 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002433
2434 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002435}
2436
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002437/**
Mario Six78a88f72018-07-10 08:40:17 +02002438 * efi_calculate_crc32() - calculate cyclic redundancy code
2439 * @data: buffer with data
2440 * @data_size: size of buffer in bytes
2441 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002442 *
2443 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002444 *
Mario Six78a88f72018-07-10 08:40:17 +02002445 * See the Unified Extensible Firmware Interface (UEFI) specification for
2446 * details.
2447 *
2448 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002449 */
Alexander Grafbee91162016-03-04 01:09:59 +01002450static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2451 unsigned long data_size,
2452 uint32_t *crc32_p)
2453{
2454 EFI_ENTRY("%p, %ld", data, data_size);
2455 *crc32_p = crc32(0, data, data_size);
2456 return EFI_EXIT(EFI_SUCCESS);
2457}
2458
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002459/**
Mario Six78a88f72018-07-10 08:40:17 +02002460 * efi_copy_mem() - copy memory
2461 * @destination: destination of the copy operation
2462 * @source: source of the copy operation
2463 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002464 *
2465 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002466 *
Mario Six78a88f72018-07-10 08:40:17 +02002467 * See the Unified Extensible Firmware Interface (UEFI) specification for
2468 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002469 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002470static void EFIAPI efi_copy_mem(void *destination, const void *source,
2471 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002472{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002473 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002474 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002475 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002476}
2477
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002478/**
Mario Six78a88f72018-07-10 08:40:17 +02002479 * efi_set_mem() - Fill memory with a byte value.
2480 * @buffer: buffer to fill
2481 * @size: size of buffer in bytes
2482 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002483 *
2484 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002485 *
Mario Six78a88f72018-07-10 08:40:17 +02002486 * See the Unified Extensible Firmware Interface (UEFI) specification for
2487 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002488 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002489static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002490{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002491 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002492 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002493 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002494}
2495
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002496/**
Mario Six78a88f72018-07-10 08:40:17 +02002497 * efi_protocol_open() - open protocol interface on a handle
2498 * @handler: handler of a protocol
2499 * @protocol_interface: interface implementing the protocol
2500 * @agent_handle: handle of the driver
2501 * @controller_handle: handle of the controller
2502 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002503 *
Mario Six78a88f72018-07-10 08:40:17 +02002504 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002505 */
2506static efi_status_t efi_protocol_open(
2507 struct efi_handler *handler,
2508 void **protocol_interface, void *agent_handle,
2509 void *controller_handle, uint32_t attributes)
2510{
2511 struct efi_open_protocol_info_item *item;
2512 struct efi_open_protocol_info_entry *match = NULL;
2513 bool opened_by_driver = false;
2514 bool opened_exclusive = false;
2515
2516 /* If there is no agent, only return the interface */
2517 if (!agent_handle)
2518 goto out;
2519
2520 /* For TEST_PROTOCOL ignore interface attribute */
2521 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2522 *protocol_interface = NULL;
2523
2524 /*
2525 * Check if the protocol is already opened by a driver with the same
2526 * attributes or opened exclusively
2527 */
2528 list_for_each_entry(item, &handler->open_infos, link) {
2529 if (item->info.agent_handle == agent_handle) {
2530 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2531 (item->info.attributes == attributes))
2532 return EFI_ALREADY_STARTED;
2533 }
2534 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2535 opened_exclusive = true;
2536 }
2537
2538 /* Only one controller can open the protocol exclusively */
2539 if (opened_exclusive && attributes &
2540 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2541 return EFI_ACCESS_DENIED;
2542
2543 /* Prepare exclusive opening */
2544 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2545 /* Try to disconnect controllers */
2546 list_for_each_entry(item, &handler->open_infos, link) {
2547 if (item->info.attributes ==
2548 EFI_OPEN_PROTOCOL_BY_DRIVER)
2549 EFI_CALL(efi_disconnect_controller(
2550 item->info.controller_handle,
2551 item->info.agent_handle,
2552 NULL));
2553 }
2554 opened_by_driver = false;
2555 /* Check if all controllers are disconnected */
2556 list_for_each_entry(item, &handler->open_infos, link) {
2557 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2558 opened_by_driver = true;
2559 }
2560 /* Only one controller can be conncected */
2561 if (opened_by_driver)
2562 return EFI_ACCESS_DENIED;
2563 }
2564
2565 /* Find existing entry */
2566 list_for_each_entry(item, &handler->open_infos, link) {
2567 if (item->info.agent_handle == agent_handle &&
2568 item->info.controller_handle == controller_handle)
2569 match = &item->info;
2570 }
2571 /* None found, create one */
2572 if (!match) {
2573 match = efi_create_open_info(handler);
2574 if (!match)
2575 return EFI_OUT_OF_RESOURCES;
2576 }
2577
2578 match->agent_handle = agent_handle;
2579 match->controller_handle = controller_handle;
2580 match->attributes = attributes;
2581 match->open_count++;
2582
2583out:
2584 /* For TEST_PROTOCOL ignore interface attribute. */
2585 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2586 *protocol_interface = handler->protocol_interface;
2587
2588 return EFI_SUCCESS;
2589}
2590
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002591/**
Mario Six78a88f72018-07-10 08:40:17 +02002592 * efi_open_protocol() - open protocol interface on a handle
2593 * @handle: handle on which the protocol shall be opened
2594 * @protocol: GUID of the protocol
2595 * @protocol_interface: interface implementing the protocol
2596 * @agent_handle: handle of the driver
2597 * @controller_handle: handle of the controller
2598 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002599 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002600 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002601 *
Mario Six78a88f72018-07-10 08:40:17 +02002602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2603 * details.
2604 *
2605 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002606 */
Alexander Grafbee91162016-03-04 01:09:59 +01002607static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002608 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002609 void **protocol_interface, void *agent_handle,
2610 void *controller_handle, uint32_t attributes)
2611{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002612 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002613 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002614
Rob Clark778e6af2017-09-13 18:05:41 -04002615 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002616 protocol_interface, agent_handle, controller_handle,
2617 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002618
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002619 if (!handle || !protocol ||
2620 (!protocol_interface && attributes !=
2621 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2622 goto out;
2623 }
2624
2625 switch (attributes) {
2626 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2627 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2628 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2629 break;
2630 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2631 if (controller_handle == handle)
2632 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002633 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002634 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2635 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002636 /* Check that the controller handle is valid */
2637 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002638 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002639 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002640 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002641 /* Check that the agent handle is valid */
2642 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002643 goto out;
2644 break;
2645 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002646 goto out;
2647 }
2648
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002649 r = efi_search_protocol(handle, protocol, &handler);
2650 if (r != EFI_SUCCESS)
2651 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002652
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002653 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2654 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002655out:
2656 return EFI_EXIT(r);
2657}
2658
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002659/**
Mario Six78a88f72018-07-10 08:40:17 +02002660 * efi_handle_protocol() - get interface of a protocol on a handle
2661 * @handle: handle on which the protocol shall be opened
2662 * @protocol: GUID of the protocol
2663 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002664 *
2665 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002666 *
Mario Six78a88f72018-07-10 08:40:17 +02002667 * See the Unified Extensible Firmware Interface (UEFI) specification for
2668 * details.
2669 *
2670 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002671 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002672static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002673 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002674 void **protocol_interface)
2675{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002676 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2677 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002678}
2679
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002680/**
Mario Six78a88f72018-07-10 08:40:17 +02002681 * efi_bind_controller() - bind a single driver to a controller
2682 * @controller_handle: controller handle
2683 * @driver_image_handle: driver handle
2684 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002685 *
Mario Six78a88f72018-07-10 08:40:17 +02002686 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002687 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002688static efi_status_t efi_bind_controller(
2689 efi_handle_t controller_handle,
2690 efi_handle_t driver_image_handle,
2691 struct efi_device_path *remain_device_path)
2692{
2693 struct efi_driver_binding_protocol *binding_protocol;
2694 efi_status_t r;
2695
2696 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2697 &efi_guid_driver_binding_protocol,
2698 (void **)&binding_protocol,
2699 driver_image_handle, NULL,
2700 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2701 if (r != EFI_SUCCESS)
2702 return r;
2703 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2704 controller_handle,
2705 remain_device_path));
2706 if (r == EFI_SUCCESS)
2707 r = EFI_CALL(binding_protocol->start(binding_protocol,
2708 controller_handle,
2709 remain_device_path));
2710 EFI_CALL(efi_close_protocol(driver_image_handle,
2711 &efi_guid_driver_binding_protocol,
2712 driver_image_handle, NULL));
2713 return r;
2714}
2715
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002716/**
Mario Six78a88f72018-07-10 08:40:17 +02002717 * efi_connect_single_controller() - connect a single driver to a controller
2718 * @controller_handle: controller
2719 * @driver_image_handle: driver
2720 * @remain_device_path: remainting path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002721 *
Mario Six78a88f72018-07-10 08:40:17 +02002722 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002723 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002724static efi_status_t efi_connect_single_controller(
2725 efi_handle_t controller_handle,
2726 efi_handle_t *driver_image_handle,
2727 struct efi_device_path *remain_device_path)
2728{
2729 efi_handle_t *buffer;
2730 size_t count;
2731 size_t i;
2732 efi_status_t r;
2733 size_t connected = 0;
2734
2735 /* Get buffer with all handles with driver binding protocol */
2736 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2737 &efi_guid_driver_binding_protocol,
2738 NULL, &count, &buffer));
2739 if (r != EFI_SUCCESS)
2740 return r;
2741
2742 /* Context Override */
2743 if (driver_image_handle) {
2744 for (; *driver_image_handle; ++driver_image_handle) {
2745 for (i = 0; i < count; ++i) {
2746 if (buffer[i] == *driver_image_handle) {
2747 buffer[i] = NULL;
2748 r = efi_bind_controller(
2749 controller_handle,
2750 *driver_image_handle,
2751 remain_device_path);
2752 /*
2753 * For drivers that do not support the
2754 * controller or are already connected
2755 * we receive an error code here.
2756 */
2757 if (r == EFI_SUCCESS)
2758 ++connected;
2759 }
2760 }
2761 }
2762 }
2763
2764 /*
2765 * TODO: Some overrides are not yet implemented:
2766 * - Platform Driver Override
2767 * - Driver Family Override Search
2768 * - Bus Specific Driver Override
2769 */
2770
2771 /* Driver Binding Search */
2772 for (i = 0; i < count; ++i) {
2773 if (buffer[i]) {
2774 r = efi_bind_controller(controller_handle,
2775 buffer[i],
2776 remain_device_path);
2777 if (r == EFI_SUCCESS)
2778 ++connected;
2779 }
2780 }
2781
2782 efi_free_pool(buffer);
2783 if (!connected)
2784 return EFI_NOT_FOUND;
2785 return EFI_SUCCESS;
2786}
2787
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002788/**
Mario Six78a88f72018-07-10 08:40:17 +02002789 * efi_connect_controller() - connect a controller to a driver
2790 * @controller_handle: handle of the controller
2791 * @driver_image_handle: handle of the driver
2792 * @remain_device_path: device path of a child controller
2793 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002794 *
2795 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002796 *
2797 * See the Unified Extensible Firmware Interface (UEFI) specification for
2798 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002799 *
2800 * First all driver binding protocol handles are tried for binding drivers.
2801 * Afterwards all handles that have openened a protocol of the controller
2802 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2803 *
Mario Six78a88f72018-07-10 08:40:17 +02002804 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002805 */
2806static efi_status_t EFIAPI efi_connect_controller(
2807 efi_handle_t controller_handle,
2808 efi_handle_t *driver_image_handle,
2809 struct efi_device_path *remain_device_path,
2810 bool recursive)
2811{
2812 efi_status_t r;
2813 efi_status_t ret = EFI_NOT_FOUND;
2814 struct efi_object *efiobj;
2815
2816 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2817 remain_device_path, recursive);
2818
2819 efiobj = efi_search_obj(controller_handle);
2820 if (!efiobj) {
2821 ret = EFI_INVALID_PARAMETER;
2822 goto out;
2823 }
2824
2825 r = efi_connect_single_controller(controller_handle,
2826 driver_image_handle,
2827 remain_device_path);
2828 if (r == EFI_SUCCESS)
2829 ret = EFI_SUCCESS;
2830 if (recursive) {
2831 struct efi_handler *handler;
2832 struct efi_open_protocol_info_item *item;
2833
2834 list_for_each_entry(handler, &efiobj->protocols, link) {
2835 list_for_each_entry(item, &handler->open_infos, link) {
2836 if (item->info.attributes &
2837 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2838 r = EFI_CALL(efi_connect_controller(
2839 item->info.controller_handle,
2840 driver_image_handle,
2841 remain_device_path,
2842 recursive));
2843 if (r == EFI_SUCCESS)
2844 ret = EFI_SUCCESS;
2845 }
2846 }
2847 }
2848 }
2849 /* Check for child controller specified by end node */
2850 if (ret != EFI_SUCCESS && remain_device_path &&
2851 remain_device_path->type == DEVICE_PATH_TYPE_END)
2852 ret = EFI_SUCCESS;
2853out:
2854 return EFI_EXIT(ret);
2855}
2856
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002857/**
Mario Six78a88f72018-07-10 08:40:17 +02002858 * efi_reinstall_protocol_interface() - reinstall protocol interface
2859 * @handle: handle on which the protocol shall be reinstalled
2860 * @protocol: GUID of the protocol to be installed
2861 * @old_interface: interface to be removed
2862 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002863 *
2864 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002865 *
2866 * See the Unified Extensible Firmware Interface (UEFI) specification for
2867 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002868 *
2869 * The old interface is uninstalled. The new interface is installed.
2870 * Drivers are connected.
2871 *
Mario Six78a88f72018-07-10 08:40:17 +02002872 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002873 */
2874static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2875 efi_handle_t handle, const efi_guid_t *protocol,
2876 void *old_interface, void *new_interface)
2877{
2878 efi_status_t ret;
2879
2880 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2881 new_interface);
2882 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2883 old_interface));
2884 if (ret != EFI_SUCCESS)
2885 goto out;
2886 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2887 EFI_NATIVE_INTERFACE,
2888 new_interface));
2889 if (ret != EFI_SUCCESS)
2890 goto out;
2891 /*
2892 * The returned status code has to be ignored.
2893 * Do not create an error if no suitable driver for the handle exists.
2894 */
2895 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2896out:
2897 return EFI_EXIT(ret);
2898}
2899
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002900/**
Mario Six78a88f72018-07-10 08:40:17 +02002901 * efi_get_child_controllers() - get all child controllers associated to a driver
2902 * @efiobj: handle of the controller
2903 * @driver_handle: handle of the driver
2904 * @number_of_children: number of child controllers
2905 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002906 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002907 * The allocated buffer has to be freed with free().
2908 *
Mario Six78a88f72018-07-10 08:40:17 +02002909 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002910 */
2911static efi_status_t efi_get_child_controllers(
2912 struct efi_object *efiobj,
2913 efi_handle_t driver_handle,
2914 efi_uintn_t *number_of_children,
2915 efi_handle_t **child_handle_buffer)
2916{
2917 struct efi_handler *handler;
2918 struct efi_open_protocol_info_item *item;
2919 efi_uintn_t count = 0, i;
2920 bool duplicate;
2921
2922 /* Count all child controller associations */
2923 list_for_each_entry(handler, &efiobj->protocols, link) {
2924 list_for_each_entry(item, &handler->open_infos, link) {
2925 if (item->info.agent_handle == driver_handle &&
2926 item->info.attributes &
2927 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2928 ++count;
2929 }
2930 }
2931 /*
2932 * Create buffer. In case of duplicate child controller assignments
2933 * the buffer will be too large. But that does not harm.
2934 */
2935 *number_of_children = 0;
2936 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2937 if (!*child_handle_buffer)
2938 return EFI_OUT_OF_RESOURCES;
2939 /* Copy unique child handles */
2940 list_for_each_entry(handler, &efiobj->protocols, link) {
2941 list_for_each_entry(item, &handler->open_infos, link) {
2942 if (item->info.agent_handle == driver_handle &&
2943 item->info.attributes &
2944 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2945 /* Check this is a new child controller */
2946 duplicate = false;
2947 for (i = 0; i < *number_of_children; ++i) {
2948 if ((*child_handle_buffer)[i] ==
2949 item->info.controller_handle)
2950 duplicate = true;
2951 }
2952 /* Copy handle to buffer */
2953 if (!duplicate) {
2954 i = (*number_of_children)++;
2955 (*child_handle_buffer)[i] =
2956 item->info.controller_handle;
2957 }
2958 }
2959 }
2960 }
2961 return EFI_SUCCESS;
2962}
2963
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002964/**
Mario Six78a88f72018-07-10 08:40:17 +02002965 * efi_disconnect_controller() - disconnect a controller from a driver
2966 * @controller_handle: handle of the controller
2967 * @driver_image_handle: handle of the driver
2968 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002969 *
2970 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002971 *
Mario Six78a88f72018-07-10 08:40:17 +02002972 * See the Unified Extensible Firmware Interface (UEFI) specification for
2973 * details.
2974 *
2975 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002976 */
2977static efi_status_t EFIAPI efi_disconnect_controller(
2978 efi_handle_t controller_handle,
2979 efi_handle_t driver_image_handle,
2980 efi_handle_t child_handle)
2981{
2982 struct efi_driver_binding_protocol *binding_protocol;
2983 efi_handle_t *child_handle_buffer = NULL;
2984 size_t number_of_children = 0;
2985 efi_status_t r;
2986 size_t stop_count = 0;
2987 struct efi_object *efiobj;
2988
2989 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2990 child_handle);
2991
2992 efiobj = efi_search_obj(controller_handle);
2993 if (!efiobj) {
2994 r = EFI_INVALID_PARAMETER;
2995 goto out;
2996 }
2997
2998 if (child_handle && !efi_search_obj(child_handle)) {
2999 r = EFI_INVALID_PARAMETER;
3000 goto out;
3001 }
3002
3003 /* If no driver handle is supplied, disconnect all drivers */
3004 if (!driver_image_handle) {
3005 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3006 goto out;
3007 }
3008
3009 /* Create list of child handles */
3010 if (child_handle) {
3011 number_of_children = 1;
3012 child_handle_buffer = &child_handle;
3013 } else {
3014 efi_get_child_controllers(efiobj,
3015 driver_image_handle,
3016 &number_of_children,
3017 &child_handle_buffer);
3018 }
3019
3020 /* Get the driver binding protocol */
3021 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3022 &efi_guid_driver_binding_protocol,
3023 (void **)&binding_protocol,
3024 driver_image_handle, NULL,
3025 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3026 if (r != EFI_SUCCESS)
3027 goto out;
3028 /* Remove the children */
3029 if (number_of_children) {
3030 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3031 controller_handle,
3032 number_of_children,
3033 child_handle_buffer));
3034 if (r == EFI_SUCCESS)
3035 ++stop_count;
3036 }
3037 /* Remove the driver */
3038 if (!child_handle)
3039 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3040 controller_handle,
3041 0, NULL));
3042 if (r == EFI_SUCCESS)
3043 ++stop_count;
3044 EFI_CALL(efi_close_protocol(driver_image_handle,
3045 &efi_guid_driver_binding_protocol,
3046 driver_image_handle, NULL));
3047
3048 if (stop_count)
3049 r = EFI_SUCCESS;
3050 else
3051 r = EFI_NOT_FOUND;
3052out:
3053 if (!child_handle)
3054 free(child_handle_buffer);
3055 return EFI_EXIT(r);
3056}
3057
Alexander Grafbee91162016-03-04 01:09:59 +01003058static const struct efi_boot_services efi_boot_services = {
3059 .hdr = {
3060 .headersize = sizeof(struct efi_table_hdr),
3061 },
3062 .raise_tpl = efi_raise_tpl,
3063 .restore_tpl = efi_restore_tpl,
3064 .allocate_pages = efi_allocate_pages_ext,
3065 .free_pages = efi_free_pages_ext,
3066 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003067 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003068 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003069 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003070 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003071 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003072 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003073 .close_event = efi_close_event,
3074 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003075 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003076 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003077 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003078 .handle_protocol = efi_handle_protocol,
3079 .reserved = NULL,
3080 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003081 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003082 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003083 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003084 .load_image = efi_load_image,
3085 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003086 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003087 .unload_image = efi_unload_image,
3088 .exit_boot_services = efi_exit_boot_services,
3089 .get_next_monotonic_count = efi_get_next_monotonic_count,
3090 .stall = efi_stall,
3091 .set_watchdog_timer = efi_set_watchdog_timer,
3092 .connect_controller = efi_connect_controller,
3093 .disconnect_controller = efi_disconnect_controller,
3094 .open_protocol = efi_open_protocol,
3095 .close_protocol = efi_close_protocol,
3096 .open_protocol_information = efi_open_protocol_information,
3097 .protocols_per_handle = efi_protocols_per_handle,
3098 .locate_handle_buffer = efi_locate_handle_buffer,
3099 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003100 .install_multiple_protocol_interfaces =
3101 efi_install_multiple_protocol_interfaces,
3102 .uninstall_multiple_protocol_interfaces =
3103 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003104 .calculate_crc32 = efi_calculate_crc32,
3105 .copy_mem = efi_copy_mem,
3106 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003107 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003108};
3109
Heinrich Schuchardt05b6f562017-12-11 20:10:20 +01003110static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003111
Alexander Graf3c63db92016-10-14 13:45:30 +02003112struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003113 .hdr = {
3114 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardtf19a95a2018-02-05 18:04:21 +01003115 .revision = 2 << 16 | 70, /* 2.7 */
Alexander Grafbee91162016-03-04 01:09:59 +01003116 .headersize = sizeof(struct efi_table_hdr),
3117 },
3118 .fw_vendor = (long)firmware_vendor,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003119 .con_in = (void *)&efi_con_in,
3120 .con_out = (void *)&efi_con_out,
3121 .std_err = (void *)&efi_con_out,
3122 .runtime = (void *)&efi_runtime_services,
3123 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003124 .nr_tables = 0,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003125 .tables = (void *)efi_conf_table,
Alexander Grafbee91162016-03-04 01:09:59 +01003126};