blob: 1192bdb5f5fa1f60631d95e9be7c7a93a3cc567f [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
Simon Glass65e4c0b2016-09-25 15:27:35 -060038#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010039/*
40 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
41 * fixed when compiling U-Boot. However, the payload does not know about that
42 * restriction so we need to manually swap its and our view of that register on
43 * EFI callback entry/exit.
44 */
45static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060046#endif
Alexander Grafbee91162016-03-04 01:09:59 +010047
Rob Clarkc160d2f2017-07-27 08:04:18 -040048static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040049static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010050/* GUID of the device tree table */
51const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010052/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
53const efi_guid_t efi_guid_driver_binding_protocol =
54 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040055
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010056/* event group ExitBootServices() invoked */
57const efi_guid_t efi_guid_event_group_exit_boot_services =
58 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
59/* event group SetVirtualAddressMap() invoked */
60const efi_guid_t efi_guid_event_group_virtual_address_change =
61 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
62/* event group memory map changed */
63const efi_guid_t efi_guid_event_group_memory_map_change =
64 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
65/* event group boot manager about to boot */
66const efi_guid_t efi_guid_event_group_ready_to_boot =
67 EFI_EVENT_GROUP_READY_TO_BOOT;
68/* event group ResetSystem() invoked (before ExitBootServices) */
69const efi_guid_t efi_guid_event_group_reset_system =
70 EFI_EVENT_GROUP_RESET_SYSTEM;
71
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010072static efi_status_t EFIAPI efi_disconnect_controller(
73 efi_handle_t controller_handle,
74 efi_handle_t driver_image_handle,
75 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010076
Rob Clarkc160d2f2017-07-27 08:04:18 -040077/* Called on every callback entry */
78int __efi_entry_check(void)
79{
80 int ret = entry_count++ == 0;
81#ifdef CONFIG_ARM
82 assert(efi_gd);
83 app_gd = gd;
84 gd = efi_gd;
85#endif
86 return ret;
87}
88
89/* Called on every callback exit */
90int __efi_exit_check(void)
91{
92 int ret = --entry_count == 0;
93#ifdef CONFIG_ARM
94 gd = app_gd;
95#endif
96 return ret;
97}
98
Alexander Grafbee91162016-03-04 01:09:59 +010099/* Called from do_bootefi_exec() */
100void efi_save_gd(void)
101{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600102#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100103 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600104#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100105}
106
Rob Clarkc160d2f2017-07-27 08:04:18 -0400107/*
Mario Six78a88f72018-07-10 08:40:17 +0200108 * Special case handler for error/abort that just forces things back to u-boot
109 * world so we can dump out an abort msg, without any care about returning back
110 * to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400111 */
Alexander Grafbee91162016-03-04 01:09:59 +0100112void efi_restore_gd(void)
113{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600114#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100115 /* Only restore if we're already in EFI context */
116 if (!efi_gd)
117 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100118 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600119#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100120}
121
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200122/**
Mario Six78a88f72018-07-10 08:40:17 +0200123 * indent_string() - returns a string for indenting with two spaces per level
124 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100125 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200126 * A maximum of ten indent levels is supported. Higher indent levels will be
127 * truncated.
128 *
Mario Six78a88f72018-07-10 08:40:17 +0200129 * Return: A string for indenting with two spaces per level is
130 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400131 */
132static const char *indent_string(int level)
133{
134 const char *indent = " ";
135 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100136
Rob Clarkaf65db82017-07-27 08:04:19 -0400137 level = min(max, level * 2);
138 return &indent[max - level];
139}
140
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200141const char *__efi_nesting(void)
142{
143 return indent_string(nesting_level);
144}
145
Rob Clarkaf65db82017-07-27 08:04:19 -0400146const char *__efi_nesting_inc(void)
147{
148 return indent_string(nesting_level++);
149}
150
151const char *__efi_nesting_dec(void)
152{
153 return indent_string(--nesting_level);
154}
155
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200156/**
Heinrich Schuchardt640adad2018-06-28 12:45:31 +0200157 * efi_update_table_header_crc32() - Update CRC32 in table header
158 *
159 * @table: EFI table
160 */
161static void efi_update_table_header_crc32(struct efi_table_hdr *table)
162{
163 table->crc32 = 0;
164 table->crc32 = crc32(0, (const unsigned char *)table,
165 table->headersize);
166}
167
168/**
Mario Six78a88f72018-07-10 08:40:17 +0200169 * efi_queue_event() - queue an EFI event
170 * @event: event to signal
171 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200172 *
173 * This function queues the notification function of the event for future
174 * execution.
175 *
Mario Six78a88f72018-07-10 08:40:17 +0200176 * The notification function is called if the task priority level of the event
177 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200178 *
179 * For the SignalEvent service see efi_signal_event_ext.
180 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200181 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100182static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200183{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200184 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200185 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200186 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100187 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200188 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200189 EFI_CALL_VOID(event->notify_function(event,
190 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200191 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200192 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200193}
194
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200195/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200196 * is_valid_tpl() - check if the task priority level is valid
197 *
198 * @tpl: TPL level to check
199 * ReturnValue: status code
200 */
201efi_status_t is_valid_tpl(efi_uintn_t tpl)
202{
203 switch (tpl) {
204 case TPL_APPLICATION:
205 case TPL_CALLBACK:
206 case TPL_NOTIFY:
207 case TPL_HIGH_LEVEL:
208 return EFI_SUCCESS;
209 default:
210 return EFI_INVALID_PARAMETER;
211 }
212}
213
214/**
Mario Six78a88f72018-07-10 08:40:17 +0200215 * efi_signal_event() - signal an EFI event
216 * @event: event to signal
217 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100218 *
Mario Six78a88f72018-07-10 08:40:17 +0200219 * This function signals an event. If the event belongs to an event group all
220 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100221 * their notification function is queued.
222 *
223 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100224 */
225void efi_signal_event(struct efi_event *event, bool check_tpl)
226{
227 if (event->group) {
228 struct efi_event *evt;
229
230 /*
231 * The signaled state has to set before executing any
232 * notification function
233 */
234 list_for_each_entry(evt, &efi_events, link) {
235 if (!evt->group || guidcmp(evt->group, event->group))
236 continue;
237 if (evt->is_signaled)
238 continue;
239 evt->is_signaled = true;
240 if (evt->type & EVT_NOTIFY_SIGNAL &&
241 evt->notify_function)
242 evt->is_queued = true;
243 }
244 list_for_each_entry(evt, &efi_events, link) {
245 if (!evt->group || guidcmp(evt->group, event->group))
246 continue;
247 if (evt->is_queued)
248 efi_queue_event(evt, check_tpl);
249 }
250 } else if (!event->is_signaled) {
251 event->is_signaled = true;
252 if (event->type & EVT_NOTIFY_SIGNAL)
253 efi_queue_event(event, check_tpl);
254 }
255}
256
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200257/**
Mario Six78a88f72018-07-10 08:40:17 +0200258 * efi_raise_tpl() - raise the task priority level
259 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200260 *
261 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200262 *
Mario Six78a88f72018-07-10 08:40:17 +0200263 * See the Unified Extensible Firmware Interface (UEFI) specification for
264 * details.
265 *
266 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200267 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100268static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100269{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100270 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200271
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200272 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200273
274 if (new_tpl < efi_tpl)
275 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
276 efi_tpl = new_tpl;
277 if (efi_tpl > TPL_HIGH_LEVEL)
278 efi_tpl = TPL_HIGH_LEVEL;
279
280 EFI_EXIT(EFI_SUCCESS);
281 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100282}
283
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200284/**
Mario Six78a88f72018-07-10 08:40:17 +0200285 * efi_restore_tpl() - lower the task priority level
286 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200287 *
288 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200289 *
Mario Six78a88f72018-07-10 08:40:17 +0200290 * See the Unified Extensible Firmware Interface (UEFI) specification for
291 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200292 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100293static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100294{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200295 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200296
297 if (old_tpl > efi_tpl)
298 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
299 efi_tpl = old_tpl;
300 if (efi_tpl > TPL_HIGH_LEVEL)
301 efi_tpl = TPL_HIGH_LEVEL;
302
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100303 /*
304 * Lowering the TPL may have made queued events eligible for execution.
305 */
306 efi_timer_check();
307
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200308 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100309}
310
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200311/**
Mario Six78a88f72018-07-10 08:40:17 +0200312 * efi_allocate_pages_ext() - allocate memory pages
313 * @type: type of allocation to be performed
314 * @memory_type: usage type of the allocated memory
315 * @pages: number of pages to be allocated
316 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200317 *
318 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200319 *
Mario Six78a88f72018-07-10 08:40:17 +0200320 * See the Unified Extensible Firmware Interface (UEFI) specification for
321 * details.
322 *
323 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200324 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900325static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100326 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900327 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100328{
329 efi_status_t r;
330
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100331 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100332 r = efi_allocate_pages(type, memory_type, pages, memory);
333 return EFI_EXIT(r);
334}
335
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200336/**
Mario Six78a88f72018-07-10 08:40:17 +0200337 * efi_free_pages_ext() - Free memory pages.
338 * @memory: start of the memory area to be freed
339 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200340 *
341 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200342 *
Mario Six78a88f72018-07-10 08:40:17 +0200343 * See the Unified Extensible Firmware Interface (UEFI) specification for
344 * details.
345 *
346 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200347 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900348static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100349 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100350{
351 efi_status_t r;
352
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100353 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100354 r = efi_free_pages(memory, pages);
355 return EFI_EXIT(r);
356}
357
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200358/**
Mario Six78a88f72018-07-10 08:40:17 +0200359 * efi_get_memory_map_ext() - get map describing memory usage
360 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
361 * on exit the size of the copied memory map
362 * @memory_map: buffer to which the memory map is written
363 * @map_key: key for the memory map
364 * @descriptor_size: size of an individual memory descriptor
365 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200366 *
367 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200368 *
Mario Six78a88f72018-07-10 08:40:17 +0200369 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * details.
371 *
372 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200373 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900374static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100375 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900376 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100377 efi_uintn_t *map_key,
378 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900379 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100380{
381 efi_status_t r;
382
383 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
384 map_key, descriptor_size, descriptor_version);
385 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
386 descriptor_size, descriptor_version);
387 return EFI_EXIT(r);
388}
389
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200390/**
Mario Six78a88f72018-07-10 08:40:17 +0200391 * efi_allocate_pool_ext() - allocate memory from pool
392 * @pool_type: type of the pool from which memory is to be allocated
393 * @size: number of bytes to be allocated
394 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200395 *
396 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200397 *
Mario Six78a88f72018-07-10 08:40:17 +0200398 * See the Unified Extensible Firmware Interface (UEFI) specification for
399 * details.
400 *
401 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200402 */
Stefan Brünsead12742016-10-09 22:17:18 +0200403static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100404 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200405 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100406{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100407 efi_status_t r;
408
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100409 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200410 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100411 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100412}
413
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200414/**
Mario Six78a88f72018-07-10 08:40:17 +0200415 * efi_free_pool_ext() - free memory from pool
416 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200417 *
418 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200419 *
Mario Six78a88f72018-07-10 08:40:17 +0200420 * See the Unified Extensible Firmware Interface (UEFI) specification for
421 * details.
422 *
423 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200424 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200425static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100426{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100427 efi_status_t r;
428
429 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200430 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100431 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100432}
433
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200434/**
Mario Six78a88f72018-07-10 08:40:17 +0200435 * efi_add_handle() - add a new object to the object list
436 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100437 *
Mario Six78a88f72018-07-10 08:40:17 +0200438 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100439 */
440void efi_add_handle(struct efi_object *obj)
441{
442 if (!obj)
443 return;
444 INIT_LIST_HEAD(&obj->protocols);
445 obj->handle = obj;
446 list_add_tail(&obj->link, &efi_obj_list);
447}
448
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200449/**
Mario Six78a88f72018-07-10 08:40:17 +0200450 * efi_create_handle() - create handle
451 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200452 *
Mario Six78a88f72018-07-10 08:40:17 +0200453 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200454 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100455efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200456{
457 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200458
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200459 obj = calloc(1, sizeof(struct efi_object));
460 if (!obj)
461 return EFI_OUT_OF_RESOURCES;
462
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100463 efi_add_handle(obj);
464 *handle = obj->handle;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200465
466 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200467}
468
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200469/**
Mario Six78a88f72018-07-10 08:40:17 +0200470 * efi_search_protocol() - find a protocol on a handle.
471 * @handle: handle
472 * @protocol_guid: GUID of the protocol
473 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100474 *
Mario Six78a88f72018-07-10 08:40:17 +0200475 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100476 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100477efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100478 const efi_guid_t *protocol_guid,
479 struct efi_handler **handler)
480{
481 struct efi_object *efiobj;
482 struct list_head *lhandle;
483
484 if (!handle || !protocol_guid)
485 return EFI_INVALID_PARAMETER;
486 efiobj = efi_search_obj(handle);
487 if (!efiobj)
488 return EFI_INVALID_PARAMETER;
489 list_for_each(lhandle, &efiobj->protocols) {
490 struct efi_handler *protocol;
491
492 protocol = list_entry(lhandle, struct efi_handler, link);
493 if (!guidcmp(protocol->guid, protocol_guid)) {
494 if (handler)
495 *handler = protocol;
496 return EFI_SUCCESS;
497 }
498 }
499 return EFI_NOT_FOUND;
500}
501
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200502/**
Mario Six78a88f72018-07-10 08:40:17 +0200503 * efi_remove_protocol() - delete protocol from a handle
504 * @handle: handle from which the protocol shall be deleted
505 * @protocol: GUID of the protocol to be deleted
506 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100507 *
Mario Six78a88f72018-07-10 08:40:17 +0200508 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100509 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100510efi_status_t efi_remove_protocol(const efi_handle_t handle,
511 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100512 void *protocol_interface)
513{
514 struct efi_handler *handler;
515 efi_status_t ret;
516
517 ret = efi_search_protocol(handle, protocol, &handler);
518 if (ret != EFI_SUCCESS)
519 return ret;
520 if (guidcmp(handler->guid, protocol))
521 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200522 if (handler->protocol_interface != protocol_interface)
523 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100524 list_del(&handler->link);
525 free(handler);
526 return EFI_SUCCESS;
527}
528
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200529/**
Mario Six78a88f72018-07-10 08:40:17 +0200530 * efi_remove_all_protocols() - delete all protocols from a handle
531 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100532 *
Mario Six78a88f72018-07-10 08:40:17 +0200533 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100534 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100535efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100536{
537 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100538 struct efi_handler *protocol;
539 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100540
541 efiobj = efi_search_obj(handle);
542 if (!efiobj)
543 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100544 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100545 efi_status_t ret;
546
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100547 ret = efi_remove_protocol(handle, protocol->guid,
548 protocol->protocol_interface);
549 if (ret != EFI_SUCCESS)
550 return ret;
551 }
552 return EFI_SUCCESS;
553}
554
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200555/**
Mario Six78a88f72018-07-10 08:40:17 +0200556 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100557 *
Mario Six78a88f72018-07-10 08:40:17 +0200558 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100559 */
560void efi_delete_handle(struct efi_object *obj)
561{
562 if (!obj)
563 return;
564 efi_remove_all_protocols(obj->handle);
565 list_del(&obj->link);
566 free(obj);
567}
568
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200569/**
Mario Six78a88f72018-07-10 08:40:17 +0200570 * efi_is_event() - check if a pointer is a valid event
571 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100572 *
Mario Six78a88f72018-07-10 08:40:17 +0200573 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100574 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100575static efi_status_t efi_is_event(const struct efi_event *event)
576{
577 const struct efi_event *evt;
578
579 if (!event)
580 return EFI_INVALID_PARAMETER;
581 list_for_each_entry(evt, &efi_events, link) {
582 if (evt == event)
583 return EFI_SUCCESS;
584 }
585 return EFI_INVALID_PARAMETER;
586}
Alexander Grafbee91162016-03-04 01:09:59 +0100587
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200588/**
Mario Six78a88f72018-07-10 08:40:17 +0200589 * efi_create_event() - create an event
590 * @type: type of the event to create
591 * @notify_tpl: task priority level of the event
592 * @notify_function: notification function of the event
593 * @notify_context: pointer passed to the notification function
594 * @group: event group
595 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200596 *
597 * This function is used inside U-Boot code to create an event.
598 *
599 * For the API function implementing the CreateEvent service see
600 * efi_create_event_ext.
601 *
Mario Six78a88f72018-07-10 08:40:17 +0200602 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200603 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100604efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200605 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200606 struct efi_event *event,
607 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100608 void *notify_context, efi_guid_t *group,
609 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100610{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100611 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200612
Jonathan Graya95343b2017-03-12 19:26:07 +1100613 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200614 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100615
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200616 switch (type) {
617 case 0:
618 case EVT_TIMER:
619 case EVT_NOTIFY_SIGNAL:
620 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
621 case EVT_NOTIFY_WAIT:
622 case EVT_TIMER | EVT_NOTIFY_WAIT:
623 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
624 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
625 break;
626 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200627 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200628 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100629
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900630 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
631 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200632 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100633
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100634 evt = calloc(1, sizeof(struct efi_event));
635 if (!evt)
636 return EFI_OUT_OF_RESOURCES;
637 evt->type = type;
638 evt->notify_tpl = notify_tpl;
639 evt->notify_function = notify_function;
640 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100641 evt->group = group;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100642 /* Disable timers on bootup */
643 evt->trigger_next = -1ULL;
644 evt->is_queued = false;
645 evt->is_signaled = false;
646 list_add_tail(&evt->link, &efi_events);
647 *event = evt;
648 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100649}
650
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200651/*
Mario Six78a88f72018-07-10 08:40:17 +0200652 * efi_create_event_ex() - create an event in a group
653 * @type: type of the event to create
654 * @notify_tpl: task priority level of the event
655 * @notify_function: notification function of the event
656 * @notify_context: pointer passed to the notification function
657 * @event: created event
658 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100659 *
660 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100661 *
Mario Six78a88f72018-07-10 08:40:17 +0200662 * See the Unified Extensible Firmware Interface (UEFI) specification for
663 * details.
664 *
665 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100666 */
667efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
668 void (EFIAPI *notify_function) (
669 struct efi_event *event,
670 void *context),
671 void *notify_context,
672 efi_guid_t *event_group,
673 struct efi_event **event)
674{
675 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
676 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100677 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100678 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100679}
680
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200681/**
Mario Six78a88f72018-07-10 08:40:17 +0200682 * efi_create_event_ext() - create an event
683 * @type: type of the event to create
684 * @notify_tpl: task priority level of the event
685 * @notify_function: notification function of the event
686 * @notify_context: pointer passed to the notification function
687 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200688 *
689 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200690 *
Mario Six78a88f72018-07-10 08:40:17 +0200691 * See the Unified Extensible Firmware Interface (UEFI) specification for
692 * details.
693 *
694 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200695 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200696static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100697 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200698 void (EFIAPI *notify_function) (
699 struct efi_event *event,
700 void *context),
701 void *notify_context, struct efi_event **event)
702{
703 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
704 notify_context);
705 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100706 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200707}
708
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200709/**
Mario Six78a88f72018-07-10 08:40:17 +0200710 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200711 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200712 * Check if a timer event has occurred or a queued notification function should
713 * be called.
714 *
Alexander Grafbee91162016-03-04 01:09:59 +0100715 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200716 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100717 */
718void efi_timer_check(void)
719{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100720 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100721 u64 now = timer_get_us();
722
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100723 list_for_each_entry(evt, &efi_events, link) {
724 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100725 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100726 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200727 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100728 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200729 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100730 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200731 break;
732 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100733 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200734 break;
735 default:
736 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200737 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100738 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100739 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100740 }
Alexander Grafbee91162016-03-04 01:09:59 +0100741 WATCHDOG_RESET();
742}
743
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200744/**
Mario Six78a88f72018-07-10 08:40:17 +0200745 * efi_set_timer() - set the trigger time for a timer event or stop the event
746 * @event: event for which the timer is set
747 * @type: type of the timer
748 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200749 *
750 * This is the function for internal usage in U-Boot. For the API function
751 * implementing the SetTimer service see efi_set_timer_ext.
752 *
Mario Six78a88f72018-07-10 08:40:17 +0200753 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200754 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200755efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200756 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100757{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100758 /* Check that the event is valid */
759 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
760 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100761
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200762 /*
763 * The parameter defines a multiple of 100ns.
764 * We use multiples of 1000ns. So divide by 10.
765 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200766 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100767
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100768 switch (type) {
769 case EFI_TIMER_STOP:
770 event->trigger_next = -1ULL;
771 break;
772 case EFI_TIMER_PERIODIC:
773 case EFI_TIMER_RELATIVE:
774 event->trigger_next = timer_get_us() + trigger_time;
775 break;
776 default:
777 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100778 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100779 event->trigger_type = type;
780 event->trigger_time = trigger_time;
781 event->is_signaled = false;
782 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200783}
784
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200785/**
Mario Six78a88f72018-07-10 08:40:17 +0200786 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
787 * event
788 * @event: event for which the timer is set
789 * @type: type of the timer
790 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200791 *
792 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200793 *
Mario Six78a88f72018-07-10 08:40:17 +0200794 * See the Unified Extensible Firmware Interface (UEFI) specification for
795 * details.
796 *
797 *
798 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200799 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200800static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
801 enum efi_timer_delay type,
802 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200803{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100804 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200805 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100806}
807
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200808/**
Mario Six78a88f72018-07-10 08:40:17 +0200809 * efi_wait_for_event() - wait for events to be signaled
810 * @num_events: number of events to be waited for
811 * @event: events to be waited for
812 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200813 *
814 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200815 *
Mario Six78a88f72018-07-10 08:40:17 +0200816 * See the Unified Extensible Firmware Interface (UEFI) specification for
817 * details.
818 *
819 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200820 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100821static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200822 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100823 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100824{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100825 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100826
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100827 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100828
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200829 /* Check parameters */
830 if (!num_events || !event)
831 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200832 /* Check TPL */
833 if (efi_tpl != TPL_APPLICATION)
834 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200835 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100836 if (efi_is_event(event[i]) != EFI_SUCCESS)
837 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200838 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
839 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200840 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100841 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200842 }
843
844 /* Wait for signal */
845 for (;;) {
846 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200847 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200848 goto out;
849 }
850 /* Allow events to occur. */
851 efi_timer_check();
852 }
853
854out:
855 /*
856 * Reset the signal which is passed to the caller to allow periodic
857 * events to occur.
858 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200859 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200860 if (index)
861 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100862
863 return EFI_EXIT(EFI_SUCCESS);
864}
865
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200866/**
Mario Six78a88f72018-07-10 08:40:17 +0200867 * efi_signal_event_ext() - signal an EFI event
868 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200869 *
870 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200871 *
872 * See the Unified Extensible Firmware Interface (UEFI) specification for
873 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200874 *
875 * This functions sets the signaled state of the event and queues the
876 * notification function for execution.
877 *
Mario Six78a88f72018-07-10 08:40:17 +0200878 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200879 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200880static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100881{
882 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100883 if (efi_is_event(event) != EFI_SUCCESS)
884 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100885 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100886 return EFI_EXIT(EFI_SUCCESS);
887}
888
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200889/**
Mario Six78a88f72018-07-10 08:40:17 +0200890 * efi_close_event() - close an EFI event
891 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200892 *
893 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200894 *
Mario Six78a88f72018-07-10 08:40:17 +0200895 * See the Unified Extensible Firmware Interface (UEFI) specification for
896 * details.
897 *
898 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200899 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200900static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100901{
902 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100903 if (efi_is_event(event) != EFI_SUCCESS)
904 return EFI_EXIT(EFI_INVALID_PARAMETER);
905 list_del(&event->link);
906 free(event);
907 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100908}
909
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200910/**
Mario Six78a88f72018-07-10 08:40:17 +0200911 * efi_check_event() - check if an event is signaled
912 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200913 *
914 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200915 *
Mario Six78a88f72018-07-10 08:40:17 +0200916 * See the Unified Extensible Firmware Interface (UEFI) specification for
917 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200918 *
Mario Six78a88f72018-07-10 08:40:17 +0200919 * If an event is not signaled yet, the notification function is queued. The
920 * signaled state is cleared.
921 *
922 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200923 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200924static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100925{
926 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200927 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100928 if (efi_is_event(event) != EFI_SUCCESS ||
929 event->type & EVT_NOTIFY_SIGNAL)
930 return EFI_EXIT(EFI_INVALID_PARAMETER);
931 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100932 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100933 if (event->is_signaled) {
934 event->is_signaled = false;
935 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200936 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100937 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100938}
939
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200940/**
Mario Six78a88f72018-07-10 08:40:17 +0200941 * efi_search_obj() - find the internal EFI object for a handle
942 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200943 *
Mario Six78a88f72018-07-10 08:40:17 +0200944 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200945 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100946struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200947{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100948 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200949
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100950 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200951 if (efiobj->handle == handle)
952 return efiobj;
953 }
954
955 return NULL;
956}
957
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200958/**
Mario Six78a88f72018-07-10 08:40:17 +0200959 * efi_open_protocol_info_entry() - create open protocol info entry and add it
960 * to a protocol
961 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100962 *
Mario Six78a88f72018-07-10 08:40:17 +0200963 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100964 */
965static struct efi_open_protocol_info_entry *efi_create_open_info(
966 struct efi_handler *handler)
967{
968 struct efi_open_protocol_info_item *item;
969
970 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
971 if (!item)
972 return NULL;
973 /* Append the item to the open protocol info list. */
974 list_add_tail(&item->link, &handler->open_infos);
975
976 return &item->info;
977}
978
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200979/**
Mario Six78a88f72018-07-10 08:40:17 +0200980 * efi_delete_open_info() - remove an open protocol info entry from a protocol
981 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100982 *
Mario Six78a88f72018-07-10 08:40:17 +0200983 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100984 */
985static efi_status_t efi_delete_open_info(
986 struct efi_open_protocol_info_item *item)
987{
988 list_del(&item->link);
989 free(item);
990 return EFI_SUCCESS;
991}
992
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200993/**
Mario Six78a88f72018-07-10 08:40:17 +0200994 * efi_add_protocol() - install new protocol on a handle
995 * @handle: handle on which the protocol shall be installed
996 * @protocol: GUID of the protocol to be installed
997 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200998 *
Mario Six78a88f72018-07-10 08:40:17 +0200999 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001000 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001001efi_status_t efi_add_protocol(const efi_handle_t handle,
1002 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001003 void *protocol_interface)
1004{
1005 struct efi_object *efiobj;
1006 struct efi_handler *handler;
1007 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001008
1009 efiobj = efi_search_obj(handle);
1010 if (!efiobj)
1011 return EFI_INVALID_PARAMETER;
1012 ret = efi_search_protocol(handle, protocol, NULL);
1013 if (ret != EFI_NOT_FOUND)
1014 return EFI_INVALID_PARAMETER;
1015 handler = calloc(1, sizeof(struct efi_handler));
1016 if (!handler)
1017 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001018 handler->guid = protocol;
1019 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001020 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001021 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001022 if (!guidcmp(&efi_guid_device_path, protocol))
1023 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001024 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001025}
1026
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001027/**
Mario Six78a88f72018-07-10 08:40:17 +02001028 * efi_install_protocol_interface() - install protocol interface
1029 * @handle: handle on which the protocol shall be installed
1030 * @protocol: GUID of the protocol to be installed
1031 * @protocol_interface_type: type of the interface to be installed,
1032 * always EFI_NATIVE_INTERFACE
1033 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001034 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001035 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001036 *
Mario Six78a88f72018-07-10 08:40:17 +02001037 * See the Unified Extensible Firmware Interface (UEFI) specification for
1038 * details.
1039 *
1040 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001041 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001042static efi_status_t EFIAPI efi_install_protocol_interface(
1043 void **handle, const efi_guid_t *protocol,
1044 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001045{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001046 efi_status_t r;
1047
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001048 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1049 protocol_interface);
1050
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001051 if (!handle || !protocol ||
1052 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1053 r = EFI_INVALID_PARAMETER;
1054 goto out;
1055 }
1056
1057 /* Create new handle if requested. */
1058 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001059 r = efi_create_handle(handle);
1060 if (r != EFI_SUCCESS)
1061 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001062 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1063 *handle);
1064 } else {
1065 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1066 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001067 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001068 /* Add new protocol */
1069 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001070out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001071 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001072}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001073
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001074/**
Mario Six78a88f72018-07-10 08:40:17 +02001075 * efi_get_drivers() - get all drivers associated to a controller
1076 * @efiobj: handle of the controller
1077 * @protocol: protocol guid (optional)
1078 * @number_of_drivers: number of child controllers
1079 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001080 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001081 * The allocated buffer has to be freed with free().
1082 *
Mario Six78a88f72018-07-10 08:40:17 +02001083 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001084 */
1085static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1086 const efi_guid_t *protocol,
1087 efi_uintn_t *number_of_drivers,
1088 efi_handle_t **driver_handle_buffer)
1089{
1090 struct efi_handler *handler;
1091 struct efi_open_protocol_info_item *item;
1092 efi_uintn_t count = 0, i;
1093 bool duplicate;
1094
1095 /* Count all driver associations */
1096 list_for_each_entry(handler, &efiobj->protocols, link) {
1097 if (protocol && guidcmp(handler->guid, protocol))
1098 continue;
1099 list_for_each_entry(item, &handler->open_infos, link) {
1100 if (item->info.attributes &
1101 EFI_OPEN_PROTOCOL_BY_DRIVER)
1102 ++count;
1103 }
1104 }
1105 /*
1106 * Create buffer. In case of duplicate driver assignments the buffer
1107 * will be too large. But that does not harm.
1108 */
1109 *number_of_drivers = 0;
1110 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1111 if (!*driver_handle_buffer)
1112 return EFI_OUT_OF_RESOURCES;
1113 /* Collect unique driver handles */
1114 list_for_each_entry(handler, &efiobj->protocols, link) {
1115 if (protocol && guidcmp(handler->guid, protocol))
1116 continue;
1117 list_for_each_entry(item, &handler->open_infos, link) {
1118 if (item->info.attributes &
1119 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1120 /* Check this is a new driver */
1121 duplicate = false;
1122 for (i = 0; i < *number_of_drivers; ++i) {
1123 if ((*driver_handle_buffer)[i] ==
1124 item->info.agent_handle)
1125 duplicate = true;
1126 }
1127 /* Copy handle to buffer */
1128 if (!duplicate) {
1129 i = (*number_of_drivers)++;
1130 (*driver_handle_buffer)[i] =
1131 item->info.agent_handle;
1132 }
1133 }
1134 }
1135 }
1136 return EFI_SUCCESS;
1137}
1138
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001139/**
Mario Six78a88f72018-07-10 08:40:17 +02001140 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1141 * @efiobj: handle of the controller
1142 * @protocol: protocol guid (optional)
1143 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001144 *
1145 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001146 *
Mario Six78a88f72018-07-10 08:40:17 +02001147 * See the Unified Extensible Firmware Interface (UEFI) specification for
1148 * details.
1149 *
1150 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001151 */
1152static efi_status_t efi_disconnect_all_drivers(
1153 struct efi_object *efiobj,
1154 const efi_guid_t *protocol,
1155 efi_handle_t child_handle)
1156{
1157 efi_uintn_t number_of_drivers;
1158 efi_handle_t *driver_handle_buffer;
1159 efi_status_t r, ret;
1160
1161 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1162 &driver_handle_buffer);
1163 if (ret != EFI_SUCCESS)
1164 return ret;
1165
1166 ret = EFI_NOT_FOUND;
1167 while (number_of_drivers) {
1168 r = EFI_CALL(efi_disconnect_controller(
1169 efiobj->handle,
1170 driver_handle_buffer[--number_of_drivers],
1171 child_handle));
1172 if (r == EFI_SUCCESS)
1173 ret = r;
1174 }
1175 free(driver_handle_buffer);
1176 return ret;
1177}
1178
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001179/**
Mario Six78a88f72018-07-10 08:40:17 +02001180 * efi_uninstall_protocol_interface() - uninstall protocol interface
1181 * @handle: handle from which the protocol shall be removed
1182 * @protocol: GUID of the protocol to be removed
1183 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001184 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001185 * This function implements the UninstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001186 *
Mario Six78a88f72018-07-10 08:40:17 +02001187 * See the Unified Extensible Firmware Interface (UEFI) specification for
1188 * details.
1189 *
1190 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001191 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001192static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001193 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001194 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001195{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001196 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001197 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001198 struct efi_open_protocol_info_item *item;
1199 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001200 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001201
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001202 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1203
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001204 /* Check handle */
1205 efiobj = efi_search_obj(handle);
1206 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001207 r = EFI_INVALID_PARAMETER;
1208 goto out;
1209 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001210 /* Find the protocol on the handle */
1211 r = efi_search_protocol(handle, protocol, &handler);
1212 if (r != EFI_SUCCESS)
1213 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001214 /* Disconnect controllers */
1215 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1216 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001217 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001218 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001219 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001220 /* Close protocol */
1221 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1222 if (item->info.attributes ==
1223 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1224 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1225 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1226 list_del(&item->link);
1227 }
1228 if (!list_empty(&handler->open_infos)) {
1229 r = EFI_ACCESS_DENIED;
1230 goto out;
1231 }
1232 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001233out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001234 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001235}
1236
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001237/**
Mario Six78a88f72018-07-10 08:40:17 +02001238 * efi_register_protocol_notify() - register an event for notification when a
1239 * protocol is installed.
1240 * @protocol: GUID of the protocol whose installation shall be notified
1241 * @event: event to be signaled upon installation of the protocol
1242 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001243 *
1244 * This function implements the RegisterProtocolNotify service.
1245 * See the Unified Extensible Firmware Interface (UEFI) specification
1246 * for details.
1247 *
Mario Six78a88f72018-07-10 08:40:17 +02001248 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001249 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001250static efi_status_t EFIAPI efi_register_protocol_notify(
1251 const efi_guid_t *protocol,
1252 struct efi_event *event,
1253 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001254{
Rob Clark778e6af2017-09-13 18:05:41 -04001255 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001256 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1257}
1258
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001259/**
Mario Six78a88f72018-07-10 08:40:17 +02001260 * efi_search() - determine if an EFI handle implements a protocol
1261 * @search_type: selection criterion
1262 * @protocol: GUID of the protocol
1263 * @search_key: registration key
1264 * @efiobj: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001265 *
1266 * See the documentation of the LocateHandle service in the UEFI specification.
1267 *
Mario Six78a88f72018-07-10 08:40:17 +02001268 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001269 */
Alexander Grafbee91162016-03-04 01:09:59 +01001270static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001271 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001272 struct efi_object *efiobj)
1273{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001274 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001275
1276 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001277 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001278 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001279 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001280 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001281 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001282 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001283 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1284 return (ret != EFI_SUCCESS);
1285 default:
1286 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001287 return -1;
1288 }
Alexander Grafbee91162016-03-04 01:09:59 +01001289}
1290
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001291/**
Mario Six78a88f72018-07-10 08:40:17 +02001292 * efi_locate_handle() - locate handles implementing a protocol
1293 * @search_type: selection criterion
1294 * @protocol: GUID of the protocol
1295 * @search_key: registration key
1296 * @buffer_size: size of the buffer to receive the handles in bytes
1297 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001298 *
1299 * This function is meant for U-Boot internal calls. For the API implementation
1300 * of the LocateHandle service see efi_locate_handle_ext.
1301 *
Mario Six78a88f72018-07-10 08:40:17 +02001302 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001303 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001304static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001305 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001306 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001307 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001308{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001309 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001310 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001311
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001312 /* Check parameters */
1313 switch (search_type) {
1314 case ALL_HANDLES:
1315 break;
1316 case BY_REGISTER_NOTIFY:
1317 if (!search_key)
1318 return EFI_INVALID_PARAMETER;
1319 /* RegisterProtocolNotify is not implemented yet */
1320 return EFI_UNSUPPORTED;
1321 case BY_PROTOCOL:
1322 if (!protocol)
1323 return EFI_INVALID_PARAMETER;
1324 break;
1325 default:
1326 return EFI_INVALID_PARAMETER;
1327 }
1328
1329 /*
1330 * efi_locate_handle_buffer uses this function for
1331 * the calculation of the necessary buffer size.
1332 * So do not require a buffer for buffersize == 0.
1333 */
1334 if (!buffer_size || (*buffer_size && !buffer))
1335 return EFI_INVALID_PARAMETER;
1336
Alexander Grafbee91162016-03-04 01:09:59 +01001337 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001338 list_for_each_entry(efiobj, &efi_obj_list, link) {
1339 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001340 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001341 }
1342
1343 if (*buffer_size < size) {
1344 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001345 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001346 }
1347
Rob Clark796a78c2017-08-06 14:10:07 -04001348 *buffer_size = size;
1349 if (size == 0)
1350 return EFI_NOT_FOUND;
1351
Alexander Grafbee91162016-03-04 01:09:59 +01001352 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001353 list_for_each_entry(efiobj, &efi_obj_list, link) {
1354 if (!efi_search(search_type, protocol, search_key, efiobj))
1355 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001356 }
1357
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001358 return EFI_SUCCESS;
1359}
1360
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001361/**
Mario Six78a88f72018-07-10 08:40:17 +02001362 * efi_locate_handle_ext() - locate handles implementing a protocol.
1363 * @search_type: selection criterion
1364 * @protocol: GUID of the protocol
1365 * @search_key: registration key
1366 * @buffer_size: size of the buffer to receive the handles in bytes
1367 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001368 *
1369 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001370 *
Mario Six78a88f72018-07-10 08:40:17 +02001371 * See the Unified Extensible Firmware Interface (UEFI) specification for
1372 * details.
1373 *
1374 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001375 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001376static efi_status_t EFIAPI efi_locate_handle_ext(
1377 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001378 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001379 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001380{
Rob Clark778e6af2017-09-13 18:05:41 -04001381 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001382 buffer_size, buffer);
1383
1384 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1385 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001386}
1387
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001388/**
Mario Six78a88f72018-07-10 08:40:17 +02001389 * efi_remove_configuration_table() - collapses configuration table entries,
1390 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001391 *
Mario Six78a88f72018-07-10 08:40:17 +02001392 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001393 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001394static void efi_remove_configuration_table(int i)
1395{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001396 struct efi_configuration_table *this = &systab.tables[i];
1397 struct efi_configuration_table *next = &systab.tables[i + 1];
1398 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001399
1400 memmove(this, next, (ulong)end - (ulong)next);
1401 systab.nr_tables--;
1402}
1403
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001404/**
Mario Six78a88f72018-07-10 08:40:17 +02001405 * efi_install_configuration_table() - adds, updates, or removes a
1406 * configuration table
1407 * @guid: GUID of the installed table
1408 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001409 *
1410 * This function is used for internal calls. For the API implementation of the
1411 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1412 *
Mario Six78a88f72018-07-10 08:40:17 +02001413 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001414 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001415efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1416 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001417{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001418 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001419 int i;
1420
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001421 if (!guid)
1422 return EFI_INVALID_PARAMETER;
1423
Alexander Grafbee91162016-03-04 01:09:59 +01001424 /* Check for guid override */
1425 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001426 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001427 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001428 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001429 else
1430 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001431 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001432 }
1433 }
1434
Alexander Grafd98cdf62017-07-26 13:41:04 +02001435 if (!table)
1436 return EFI_NOT_FOUND;
1437
Alexander Grafbee91162016-03-04 01:09:59 +01001438 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001439 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001440 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001441
1442 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001443 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1444 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001445 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001446
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001447out:
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001448 /* systab.nr_tables may have changed. So we need to update the crc32 */
1449 efi_update_table_header_crc32(&systab.hdr);
1450
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001451 /* Notify that the configuration table was changed */
1452 list_for_each_entry(evt, &efi_events, link) {
1453 if (evt->group && !guidcmp(evt->group, guid)) {
1454 efi_signal_event(evt, false);
1455 break;
1456 }
1457 }
1458
Alexander Graf488bf122016-08-19 01:23:24 +02001459 return EFI_SUCCESS;
1460}
1461
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001462/**
Mario Six78a88f72018-07-10 08:40:17 +02001463 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1464 * configuration table.
1465 * @guid: GUID of the installed table
1466 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001467 *
1468 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001469 *
Mario Six78a88f72018-07-10 08:40:17 +02001470 * See the Unified Extensible Firmware Interface (UEFI) specification for
1471 * details.
1472 *
1473 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001474 */
Alexander Graf488bf122016-08-19 01:23:24 +02001475static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1476 void *table)
1477{
Rob Clark778e6af2017-09-13 18:05:41 -04001478 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001479 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001480}
1481
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001482/**
Mario Six78a88f72018-07-10 08:40:17 +02001483 * efi_setup_loaded_image() - initialize a loaded image
1484 * @info: loaded image info to be passed to the entry point of the image
1485 * @obj: internal object associated with the loaded image
1486 * @device_path: device path of the loaded image
1487 * @file_path: file path of the loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001488 *
1489 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001490 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001491 *
Mario Six78a88f72018-07-10 08:40:17 +02001492 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001493 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001494efi_status_t efi_setup_loaded_image(
1495 struct efi_loaded_image *info, struct efi_object *obj,
1496 struct efi_device_path *device_path,
1497 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001498{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001499 efi_status_t ret;
1500
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001501 /* Add internal object to object list */
1502 efi_add_handle(obj);
1503 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001504 obj->handle = info;
1505
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001506 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001507 info->file_path = file_path;
Rob Clark95c55532017-09-13 18:05:33 -04001508
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001509 if (device_path) {
1510 info->device_handle = efi_dp_find_obj(device_path, NULL);
1511 /*
1512 * When asking for the device path interface, return
1513 * bootefi_device_path
1514 */
1515 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1516 device_path);
1517 if (ret != EFI_SUCCESS)
1518 goto failure;
1519 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001520
1521 /*
1522 * When asking for the loaded_image interface, just
1523 * return handle which points to loaded_image_info
1524 */
1525 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1526 if (ret != EFI_SUCCESS)
1527 goto failure;
1528
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001529 ret = efi_add_protocol(obj->handle,
1530 &efi_guid_device_path_to_text_protocol,
1531 (void *)&efi_device_path_to_text);
1532 if (ret != EFI_SUCCESS)
1533 goto failure;
1534
Leif Lindholme70f8df2018-03-09 17:43:21 +01001535 ret = efi_add_protocol(obj->handle,
1536 &efi_guid_device_path_utilities_protocol,
1537 (void *)&efi_device_path_utilities);
1538 if (ret != EFI_SUCCESS)
1539 goto failure;
1540
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001541 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001542failure:
1543 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001544 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001545}
1546
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001547/**
Mario Six78a88f72018-07-10 08:40:17 +02001548 * efi_load_image_from_path() - load an image using a file path
1549 * @file_path: the path of the image to load
1550 * @buffer: buffer containing the loaded image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001551 *
Mario Six78a88f72018-07-10 08:40:17 +02001552 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001553 */
Rob Clark9975fe92017-09-13 18:05:38 -04001554efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1555 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001556{
1557 struct efi_file_info *info = NULL;
1558 struct efi_file_handle *f;
1559 static efi_status_t ret;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001560 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001561
1562 f = efi_file_from_path(file_path);
1563 if (!f)
1564 return EFI_DEVICE_ERROR;
1565
1566 bs = 0;
1567 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1568 &bs, info));
1569 if (ret == EFI_BUFFER_TOO_SMALL) {
1570 info = malloc(bs);
1571 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1572 &bs, info));
1573 }
1574 if (ret != EFI_SUCCESS)
1575 goto error;
1576
1577 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1578 if (ret)
1579 goto error;
1580
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001581 bs = info->file_size;
1582 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark838ee4b2017-09-13 18:05:35 -04001583
1584error:
1585 free(info);
1586 EFI_CALL(f->close(f));
1587
1588 if (ret != EFI_SUCCESS) {
1589 efi_free_pool(*buffer);
1590 *buffer = NULL;
1591 }
1592
1593 return ret;
1594}
1595
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001596/**
Mario Six78a88f72018-07-10 08:40:17 +02001597 * efi_load_image() - load an EFI image into memory
1598 * @boot_policy: true for request originating from the boot manager
1599 * @parent_image: the caller's image handle
1600 * @file_path: the path of the image to load
1601 * @source_buffer: memory location from which the image is installed
1602 * @source_size: size of the memory area from which the image is installed
1603 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001604 *
1605 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001606 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001607 * See the Unified Extensible Firmware Interface (UEFI) specification
1608 * for details.
1609 *
Mario Six78a88f72018-07-10 08:40:17 +02001610 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001611 */
Alexander Grafbee91162016-03-04 01:09:59 +01001612static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1613 efi_handle_t parent_image,
1614 struct efi_device_path *file_path,
1615 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001616 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001617 efi_handle_t *image_handle)
1618{
Alexander Grafbee91162016-03-04 01:09:59 +01001619 struct efi_loaded_image *info;
1620 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001621 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001622
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001623 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001624 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001625
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001626 if (!image_handle || !parent_image) {
1627 ret = EFI_INVALID_PARAMETER;
1628 goto error;
1629 }
1630
1631 if (!source_buffer && !file_path) {
1632 ret = EFI_NOT_FOUND;
1633 goto error;
1634 }
1635
Rob Clark838ee4b2017-09-13 18:05:35 -04001636 info = calloc(1, sizeof(*info));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001637 if (!info) {
1638 ret = EFI_OUT_OF_RESOURCES;
1639 goto error;
1640 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001641 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001642 if (!obj) {
1643 free(info);
1644 ret = EFI_OUT_OF_RESOURCES;
1645 goto error;
1646 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001647
1648 if (!source_buffer) {
1649 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001650
Rob Clark9975fe92017-09-13 18:05:38 -04001651 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001652 if (ret != EFI_SUCCESS)
1653 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001654 /*
1655 * split file_path which contains both the device and
1656 * file parts:
1657 */
1658 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001659 ret = efi_setup_loaded_image(info, obj, dp, fp);
1660 if (ret != EFI_SUCCESS)
1661 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001662 } else {
1663 /* In this case, file_path is the "device" path, ie.
1664 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1665 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001666 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1667 if (ret != EFI_SUCCESS)
1668 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001669 }
Alexander Grafbee91162016-03-04 01:09:59 +01001670 info->reserved = efi_load_pe(source_buffer, info);
1671 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001672 ret = EFI_UNSUPPORTED;
1673 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001674 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001675 info->system_table = &systab;
1676 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001677 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001678 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001679failure:
1680 free(info);
1681 efi_delete_handle(obj);
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001682error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001683 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001684}
1685
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001686/**
Mario Six78a88f72018-07-10 08:40:17 +02001687 * efi_start_image() - dall the entry point of an image
1688 * @image_handle: handle of the image
1689 * @exit_data_size: size of the buffer
1690 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001691 *
1692 * This function implements the StartImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001693 *
Mario Six78a88f72018-07-10 08:40:17 +02001694 * See the Unified Extensible Firmware Interface (UEFI) specification for
1695 * details.
1696 *
1697 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001698 */
Alexander Grafbee91162016-03-04 01:09:59 +01001699static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1700 unsigned long *exit_data_size,
1701 s16 **exit_data)
1702{
Alexander Grafc6fa5df2018-01-24 00:18:08 +01001703 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1704 struct efi_system_table *st);
Alexander Grafbee91162016-03-04 01:09:59 +01001705 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001706 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001707
1708 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1709 entry = info->reserved;
1710
1711 efi_is_direct_boot = false;
1712
1713 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001714 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001715 /*
1716 * We called the entry point of the child image with EFI_CALL
1717 * in the lines below. The child image called the Exit() boot
1718 * service efi_exit() which executed the long jump that brought
1719 * us to the current line. This implies that the second half
1720 * of the EFI_CALL macro has not been executed.
1721 */
1722#ifdef CONFIG_ARM
1723 /*
1724 * efi_exit() called efi_restore_gd(). We have to undo this
1725 * otherwise __efi_entry_check() will put the wrong value into
1726 * app_gd.
1727 */
1728 gd = app_gd;
1729#endif
1730 /*
1731 * To get ready to call EFI_EXIT below we have to execute the
1732 * missed out steps of EFI_CALL.
1733 */
1734 assert(__efi_entry_check());
1735 debug("%sEFI: %lu returned by started image\n",
1736 __efi_nesting_dec(),
1737 (unsigned long)((uintptr_t)info->exit_status &
1738 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001739 return EFI_EXIT(info->exit_status);
1740 }
1741
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001742 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001743
Alexander Graf56672bf2018-01-26 00:47:53 +01001744 /*
1745 * Usually UEFI applications call Exit() instead of returning.
1746 * But because the world doesn not consist of ponies and unicorns,
1747 * we're happy to emulate that behavior on behalf of a payload
1748 * that forgot.
1749 */
1750 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafbee91162016-03-04 01:09:59 +01001751}
1752
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001753/**
Mario Six78a88f72018-07-10 08:40:17 +02001754 * efi_exit() - leave an EFI application or driver
1755 * @image_handle: handle of the application or driver that is exiting
1756 * @exit_status: status code
1757 * @exit_data_size: size of the buffer in bytes
1758 * @exit_data: buffer with data describing an error
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001759 *
1760 * This function implements the Exit service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001761 *
Mario Six78a88f72018-07-10 08:40:17 +02001762 * See the Unified Extensible Firmware Interface (UEFI) specification for
1763 * details.
1764 *
1765 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001766 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001767static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001768 efi_status_t exit_status,
1769 unsigned long exit_data_size,
1770 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001771{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001772 /*
1773 * We require that the handle points to the original loaded
1774 * image protocol interface.
1775 *
1776 * For getting the longjmp address this is safer than locating
1777 * the protocol because the protocol may have been reinstalled
1778 * pointing to another memory location.
1779 *
1780 * TODO: We should call the unload procedure of the loaded
1781 * image protocol.
1782 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001783 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001784
Alexander Grafbee91162016-03-04 01:09:59 +01001785 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1786 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001787
Alexander Grafa1489202017-09-03 14:14:17 +02001788 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001789 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001790
Alexander Grafa1489202017-09-03 14:14:17 +02001791 /*
1792 * But longjmp out with the U-Boot gd, not the application's, as
1793 * the other end is a setjmp call inside EFI context.
1794 */
1795 efi_restore_gd();
1796
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001797 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001798 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001799
1800 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001801}
1802
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001803/**
Mario Six78a88f72018-07-10 08:40:17 +02001804 * efi_unload_image() - unload an EFI image
1805 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001806 *
1807 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001808 *
Mario Six78a88f72018-07-10 08:40:17 +02001809 * See the Unified Extensible Firmware Interface (UEFI) specification for
1810 * details.
1811 *
1812 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001813 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001814static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001815{
1816 struct efi_object *efiobj;
1817
1818 EFI_ENTRY("%p", image_handle);
1819 efiobj = efi_search_obj(image_handle);
1820 if (efiobj)
1821 list_del(&efiobj->link);
1822
1823 return EFI_EXIT(EFI_SUCCESS);
1824}
1825
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001826/**
Mario Six78a88f72018-07-10 08:40:17 +02001827 * efi_exit_caches() - fix up caches for EFI payloads if necessary
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001828 */
Alexander Grafbee91162016-03-04 01:09:59 +01001829static void efi_exit_caches(void)
1830{
1831#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1832 /*
1833 * Grub on 32bit ARM needs to have caches disabled before jumping into
1834 * a zImage, but does not know of all cache layers. Give it a hand.
1835 */
1836 if (efi_is_direct_boot)
1837 cleanup_before_linux();
1838#endif
1839}
1840
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001841/**
Mario Six78a88f72018-07-10 08:40:17 +02001842 * efi_exit_boot_services() - stop all boot services
1843 * @image_handle: handle of the loaded image
1844 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001845 *
1846 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001847 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001848 * See the Unified Extensible Firmware Interface (UEFI) specification
1849 * for details.
1850 *
Mario Six78a88f72018-07-10 08:40:17 +02001851 * All timer events are disabled. For exit boot services events the
1852 * notification function is called. The boot services are disabled in the
1853 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001854 *
Mario Six78a88f72018-07-10 08:40:17 +02001855 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001856 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001857static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001858 unsigned long map_key)
1859{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001860 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001861
Alexander Grafbee91162016-03-04 01:09:59 +01001862 EFI_ENTRY("%p, %ld", image_handle, map_key);
1863
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001864 /* Check that the caller has read the current memory map */
1865 if (map_key != efi_memory_map_key)
1866 return EFI_INVALID_PARAMETER;
1867
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001868 /* Make sure that notification functions are not called anymore */
1869 efi_tpl = TPL_HIGH_LEVEL;
1870
1871 /* Check if ExitBootServices has already been called */
1872 if (!systab.boottime)
1873 return EFI_EXIT(EFI_SUCCESS);
1874
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001875 /* Add related events to the event group */
1876 list_for_each_entry(evt, &efi_events, link) {
1877 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1878 evt->group = &efi_guid_event_group_exit_boot_services;
1879 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001880 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001881 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001882 if (evt->group &&
1883 !guidcmp(evt->group,
1884 &efi_guid_event_group_exit_boot_services)) {
1885 efi_signal_event(evt, false);
1886 break;
1887 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001888 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001889
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001890 /* TODO Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001891
Alexander Grafb7b84102016-11-17 01:02:57 +01001892 board_quiesce_devices();
1893
Alexander Grafbee91162016-03-04 01:09:59 +01001894 /* Fix up caches for EFI payloads if necessary */
1895 efi_exit_caches();
1896
1897 /* This stops all lingering devices */
1898 bootm_disable_interrupts();
1899
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001900 /* Disable boottime services */
1901 systab.con_in_handle = NULL;
1902 systab.con_in = NULL;
1903 systab.con_out_handle = NULL;
1904 systab.con_out = NULL;
1905 systab.stderr_handle = NULL;
1906 systab.std_err = NULL;
1907 systab.boottime = NULL;
1908
1909 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001910 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001911
Alexander Grafbee91162016-03-04 01:09:59 +01001912 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001913 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001914 WATCHDOG_RESET();
1915
1916 return EFI_EXIT(EFI_SUCCESS);
1917}
1918
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001919/**
Mario Six78a88f72018-07-10 08:40:17 +02001920 * efi_get_next_monotonic_count() - get next value of the counter
1921 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001922 *
1923 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001924 *
Mario Six78a88f72018-07-10 08:40:17 +02001925 * See the Unified Extensible Firmware Interface (UEFI) specification for
1926 * details.
1927 *
1928 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001929 */
Alexander Grafbee91162016-03-04 01:09:59 +01001930static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1931{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001932 static uint64_t mono;
1933
Alexander Grafbee91162016-03-04 01:09:59 +01001934 EFI_ENTRY("%p", count);
1935 *count = mono++;
1936 return EFI_EXIT(EFI_SUCCESS);
1937}
1938
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001939/**
Mario Six78a88f72018-07-10 08:40:17 +02001940 * efi_stall() - sleep
1941 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001942 *
Mario Six78a88f72018-07-10 08:40:17 +02001943 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001944 *
Mario Six78a88f72018-07-10 08:40:17 +02001945 * See the Unified Extensible Firmware Interface (UEFI) specification for
1946 * details.
1947 *
1948 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001949 */
Alexander Grafbee91162016-03-04 01:09:59 +01001950static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1951{
1952 EFI_ENTRY("%ld", microseconds);
1953 udelay(microseconds);
1954 return EFI_EXIT(EFI_SUCCESS);
1955}
1956
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001957/**
Mario Six78a88f72018-07-10 08:40:17 +02001958 * efi_set_watchdog_timer() - reset the watchdog timer
1959 * @timeout: seconds before reset by watchdog
1960 * @watchdog_code: code to be logged when resetting
1961 * @data_size: size of buffer in bytes
1962 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001963 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001964 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001965 *
Mario Six78a88f72018-07-10 08:40:17 +02001966 * See the Unified Extensible Firmware Interface (UEFI) specification for
1967 * details.
1968 *
1969 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001970 */
Alexander Grafbee91162016-03-04 01:09:59 +01001971static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1972 uint64_t watchdog_code,
1973 unsigned long data_size,
1974 uint16_t *watchdog_data)
1975{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001976 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001977 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001978 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001979}
1980
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001981/**
Mario Six78a88f72018-07-10 08:40:17 +02001982 * efi_close_protocol() - close a protocol
1983 * @handle: handle on which the protocol shall be closed
1984 * @protocol: GUID of the protocol to close
1985 * @agent_handle: handle of the driver
1986 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001987 *
1988 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001989 *
Mario Six78a88f72018-07-10 08:40:17 +02001990 * See the Unified Extensible Firmware Interface (UEFI) specification for
1991 * details.
1992 *
1993 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001994 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001995static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001996 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001997 efi_handle_t agent_handle,
1998 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001999{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002000 struct efi_handler *handler;
2001 struct efi_open_protocol_info_item *item;
2002 struct efi_open_protocol_info_item *pos;
2003 efi_status_t r;
2004
Rob Clark778e6af2017-09-13 18:05:41 -04002005 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002006 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002007
2008 if (!agent_handle) {
2009 r = EFI_INVALID_PARAMETER;
2010 goto out;
2011 }
2012 r = efi_search_protocol(handle, protocol, &handler);
2013 if (r != EFI_SUCCESS)
2014 goto out;
2015
2016 r = EFI_NOT_FOUND;
2017 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2018 if (item->info.agent_handle == agent_handle &&
2019 item->info.controller_handle == controller_handle) {
2020 efi_delete_open_info(item);
2021 r = EFI_SUCCESS;
2022 break;
2023 }
2024 }
2025out:
2026 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002027}
2028
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002029/**
Mario Six78a88f72018-07-10 08:40:17 +02002030 * efi_open_protocol_information() - provide information about then open status
2031 * of a protocol on a handle
2032 * @handle: handle for which the information shall be retrieved
2033 * @protocol: GUID of the protocol
2034 * @entry_buffer: buffer to receive the open protocol information
2035 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002036 *
2037 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002038 *
Mario Six78a88f72018-07-10 08:40:17 +02002039 * See the Unified Extensible Firmware Interface (UEFI) specification for
2040 * details.
2041 *
2042 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002043 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002044static efi_status_t EFIAPI efi_open_protocol_information(
2045 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002046 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002047 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002048{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002049 unsigned long buffer_size;
2050 unsigned long count;
2051 struct efi_handler *handler;
2052 struct efi_open_protocol_info_item *item;
2053 efi_status_t r;
2054
Rob Clark778e6af2017-09-13 18:05:41 -04002055 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002056 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002057
2058 /* Check parameters */
2059 if (!entry_buffer) {
2060 r = EFI_INVALID_PARAMETER;
2061 goto out;
2062 }
2063 r = efi_search_protocol(handle, protocol, &handler);
2064 if (r != EFI_SUCCESS)
2065 goto out;
2066
2067 /* Count entries */
2068 count = 0;
2069 list_for_each_entry(item, &handler->open_infos, link) {
2070 if (item->info.open_count)
2071 ++count;
2072 }
2073 *entry_count = count;
2074 *entry_buffer = NULL;
2075 if (!count) {
2076 r = EFI_SUCCESS;
2077 goto out;
2078 }
2079
2080 /* Copy entries */
2081 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2082 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2083 (void **)entry_buffer);
2084 if (r != EFI_SUCCESS)
2085 goto out;
2086 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2087 if (item->info.open_count)
2088 (*entry_buffer)[--count] = item->info;
2089 }
2090out:
2091 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002092}
2093
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002094/**
Mario Six78a88f72018-07-10 08:40:17 +02002095 * efi_protocols_per_handle() - get protocols installed on a handle
2096 * @handle: handle for which the information is retrieved
2097 * @protocol_buffer: buffer with protocol GUIDs
2098 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002099 *
2100 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002101 *
Mario Six78a88f72018-07-10 08:40:17 +02002102 * See the Unified Extensible Firmware Interface (UEFI) specification for
2103 * details.
2104 *
2105 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002106 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002107static efi_status_t EFIAPI efi_protocols_per_handle(
2108 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002109 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002110{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002111 unsigned long buffer_size;
2112 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002113 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002114 efi_status_t r;
2115
Alexander Grafbee91162016-03-04 01:09:59 +01002116 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2117 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002118
2119 if (!handle || !protocol_buffer || !protocol_buffer_count)
2120 return EFI_EXIT(EFI_INVALID_PARAMETER);
2121
2122 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002123 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002124
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002125 efiobj = efi_search_obj(handle);
2126 if (!efiobj)
2127 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002128
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002129 /* Count protocols */
2130 list_for_each(protocol_handle, &efiobj->protocols) {
2131 ++*protocol_buffer_count;
2132 }
2133
2134 /* Copy guids */
2135 if (*protocol_buffer_count) {
2136 size_t j = 0;
2137
2138 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2139 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2140 (void **)protocol_buffer);
2141 if (r != EFI_SUCCESS)
2142 return EFI_EXIT(r);
2143 list_for_each(protocol_handle, &efiobj->protocols) {
2144 struct efi_handler *protocol;
2145
2146 protocol = list_entry(protocol_handle,
2147 struct efi_handler, link);
2148 (*protocol_buffer)[j] = (void *)protocol->guid;
2149 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002150 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002151 }
2152
2153 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002154}
2155
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002156/**
Mario Six78a88f72018-07-10 08:40:17 +02002157 * efi_locate_handle_buffer() - locate handles implementing a protocol
2158 * @search_type: selection criterion
2159 * @protocol: GUID of the protocol
2160 * @search_key: registration key
2161 * @no_handles: number of returned handles
2162 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002163 *
2164 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002165 *
Mario Six78a88f72018-07-10 08:40:17 +02002166 * See the Unified Extensible Firmware Interface (UEFI) specification for
2167 * details.
2168 *
2169 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002170 */
Alexander Grafbee91162016-03-04 01:09:59 +01002171static efi_status_t EFIAPI efi_locate_handle_buffer(
2172 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002173 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002174 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002175{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002176 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002177 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002178
Rob Clark778e6af2017-09-13 18:05:41 -04002179 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002180 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002181
2182 if (!no_handles || !buffer) {
2183 r = EFI_INVALID_PARAMETER;
2184 goto out;
2185 }
2186 *no_handles = 0;
2187 *buffer = NULL;
2188 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2189 *buffer);
2190 if (r != EFI_BUFFER_TOO_SMALL)
2191 goto out;
2192 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2193 (void **)buffer);
2194 if (r != EFI_SUCCESS)
2195 goto out;
2196 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2197 *buffer);
2198 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002199 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002200out:
2201 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002202}
2203
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002204/**
Mario Six78a88f72018-07-10 08:40:17 +02002205 * efi_locate_protocol() - find an interface implementing a protocol
2206 * @protocol: GUID of the protocol
2207 * @registration: registration key passed to the notification function
2208 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002209 *
2210 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002211 *
Mario Six78a88f72018-07-10 08:40:17 +02002212 * See the Unified Extensible Firmware Interface (UEFI) specification for
2213 * details.
2214 *
2215 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002216 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002217static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002218 void *registration,
2219 void **protocol_interface)
2220{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002221 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002222 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002223
Rob Clark778e6af2017-09-13 18:05:41 -04002224 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002225
2226 if (!protocol || !protocol_interface)
2227 return EFI_EXIT(EFI_INVALID_PARAMETER);
2228
2229 list_for_each(lhandle, &efi_obj_list) {
2230 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002231 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002232
2233 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002234
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002235 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2236 if (ret == EFI_SUCCESS) {
2237 *protocol_interface = handler->protocol_interface;
2238 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002239 }
2240 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002241 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002242
2243 return EFI_EXIT(EFI_NOT_FOUND);
2244}
2245
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002246/**
Mario Six78a88f72018-07-10 08:40:17 +02002247 * efi_locate_device_path() - Get the device path and handle of an device
2248 * implementing a protocol
2249 * @protocol: GUID of the protocol
2250 * @device_path: device path
2251 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002252 *
2253 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002254 *
Mario Six78a88f72018-07-10 08:40:17 +02002255 * See the Unified Extensible Firmware Interface (UEFI) specification for
2256 * details.
2257 *
2258 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002259 */
2260static efi_status_t EFIAPI efi_locate_device_path(
2261 const efi_guid_t *protocol,
2262 struct efi_device_path **device_path,
2263 efi_handle_t *device)
2264{
2265 struct efi_device_path *dp;
2266 size_t i;
2267 struct efi_handler *handler;
2268 efi_handle_t *handles;
2269 size_t len, len_dp;
2270 size_t len_best = 0;
2271 efi_uintn_t no_handles;
2272 u8 *remainder;
2273 efi_status_t ret;
2274
2275 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2276
2277 if (!protocol || !device_path || !*device_path || !device) {
2278 ret = EFI_INVALID_PARAMETER;
2279 goto out;
2280 }
2281
2282 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002283 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002284
2285 /* Get all handles implementing the protocol */
2286 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2287 &no_handles, &handles));
2288 if (ret != EFI_SUCCESS)
2289 goto out;
2290
2291 for (i = 0; i < no_handles; ++i) {
2292 /* Find the device path protocol */
2293 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2294 &handler);
2295 if (ret != EFI_SUCCESS)
2296 continue;
2297 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002298 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002299 /*
2300 * This handle can only be a better fit
2301 * if its device path length is longer than the best fit and
2302 * if its device path length is shorter of equal the searched
2303 * device path.
2304 */
2305 if (len_dp <= len_best || len_dp > len)
2306 continue;
2307 /* Check if dp is a subpath of device_path */
2308 if (memcmp(*device_path, dp, len_dp))
2309 continue;
2310 *device = handles[i];
2311 len_best = len_dp;
2312 }
2313 if (len_best) {
2314 remainder = (u8 *)*device_path + len_best;
2315 *device_path = (struct efi_device_path *)remainder;
2316 ret = EFI_SUCCESS;
2317 } else {
2318 ret = EFI_NOT_FOUND;
2319 }
2320out:
2321 return EFI_EXIT(ret);
2322}
2323
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002324/**
Mario Six78a88f72018-07-10 08:40:17 +02002325 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2326 * interfaces
2327 * @handle: handle on which the protocol interfaces shall be installed
2328 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2329 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002330 *
2331 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002332 *
Mario Six78a88f72018-07-10 08:40:17 +02002333 * See the Unified Extensible Firmware Interface (UEFI) specification for
2334 * details.
2335 *
2336 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002337 */
Alexander Grafbee91162016-03-04 01:09:59 +01002338static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2339 void **handle, ...)
2340{
2341 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002342
Alexander Grafbeb077a2018-06-18 17:23:05 +02002343 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002344 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002345 void *protocol_interface;
2346 efi_status_t r = EFI_SUCCESS;
2347 int i = 0;
2348
2349 if (!handle)
2350 return EFI_EXIT(EFI_INVALID_PARAMETER);
2351
Alexander Grafbeb077a2018-06-18 17:23:05 +02002352 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002353 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002354 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002355 if (!protocol)
2356 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002357 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002358 r = EFI_CALL(efi_install_protocol_interface(
2359 handle, protocol,
2360 EFI_NATIVE_INTERFACE,
2361 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002362 if (r != EFI_SUCCESS)
2363 break;
2364 i++;
2365 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002366 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002367 if (r == EFI_SUCCESS)
2368 return EFI_EXIT(r);
2369
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002370 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002371 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002372 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002373 protocol = efi_va_arg(argptr, efi_guid_t*);
2374 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002375 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2376 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002377 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002378 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002379
2380 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002381}
2382
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002383/**
Mario Six78a88f72018-07-10 08:40:17 +02002384 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2385 * interfaces
2386 * @handle: handle from which the protocol interfaces shall be removed
2387 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2388 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002389 *
2390 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002391 *
Mario Six78a88f72018-07-10 08:40:17 +02002392 * See the Unified Extensible Firmware Interface (UEFI) specification for
2393 * details.
2394 *
2395 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002396 */
Alexander Grafbee91162016-03-04 01:09:59 +01002397static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2398 void *handle, ...)
2399{
2400 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002401
Alexander Grafbeb077a2018-06-18 17:23:05 +02002402 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002403 const efi_guid_t *protocol;
2404 void *protocol_interface;
2405 efi_status_t r = EFI_SUCCESS;
2406 size_t i = 0;
2407
2408 if (!handle)
2409 return EFI_EXIT(EFI_INVALID_PARAMETER);
2410
Alexander Grafbeb077a2018-06-18 17:23:05 +02002411 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002412 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002413 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002414 if (!protocol)
2415 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002416 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002417 r = EFI_CALL(efi_uninstall_protocol_interface(
2418 handle, protocol,
2419 protocol_interface));
2420 if (r != EFI_SUCCESS)
2421 break;
2422 i++;
2423 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002424 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002425 if (r == EFI_SUCCESS)
2426 return EFI_EXIT(r);
2427
2428 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002429 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002430 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002431 protocol = efi_va_arg(argptr, efi_guid_t*);
2432 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002433 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2434 EFI_NATIVE_INTERFACE,
2435 protocol_interface));
2436 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002437 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002438
2439 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002440}
2441
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002442/**
Mario Six78a88f72018-07-10 08:40:17 +02002443 * efi_calculate_crc32() - calculate cyclic redundancy code
2444 * @data: buffer with data
2445 * @data_size: size of buffer in bytes
2446 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002447 *
2448 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002449 *
Mario Six78a88f72018-07-10 08:40:17 +02002450 * See the Unified Extensible Firmware Interface (UEFI) specification for
2451 * details.
2452 *
2453 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002454 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002455static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2456 efi_uintn_t data_size,
2457 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002458{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002459 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002460 *crc32_p = crc32(0, data, data_size);
2461 return EFI_EXIT(EFI_SUCCESS);
2462}
2463
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002464/**
Mario Six78a88f72018-07-10 08:40:17 +02002465 * efi_copy_mem() - copy memory
2466 * @destination: destination of the copy operation
2467 * @source: source of the copy operation
2468 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002469 *
2470 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002471 *
Mario Six78a88f72018-07-10 08:40:17 +02002472 * See the Unified Extensible Firmware Interface (UEFI) specification for
2473 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002474 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002475static void EFIAPI efi_copy_mem(void *destination, const void *source,
2476 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002477{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002478 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002479 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002480 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002481}
2482
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002483/**
Mario Six78a88f72018-07-10 08:40:17 +02002484 * efi_set_mem() - Fill memory with a byte value.
2485 * @buffer: buffer to fill
2486 * @size: size of buffer in bytes
2487 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002488 *
2489 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002490 *
Mario Six78a88f72018-07-10 08:40:17 +02002491 * See the Unified Extensible Firmware Interface (UEFI) specification for
2492 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002493 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002494static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002495{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002496 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002497 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002498 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002499}
2500
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002501/**
Mario Six78a88f72018-07-10 08:40:17 +02002502 * efi_protocol_open() - open protocol interface on a handle
2503 * @handler: handler of a protocol
2504 * @protocol_interface: interface implementing the protocol
2505 * @agent_handle: handle of the driver
2506 * @controller_handle: handle of the controller
2507 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002508 *
Mario Six78a88f72018-07-10 08:40:17 +02002509 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002510 */
2511static efi_status_t efi_protocol_open(
2512 struct efi_handler *handler,
2513 void **protocol_interface, void *agent_handle,
2514 void *controller_handle, uint32_t attributes)
2515{
2516 struct efi_open_protocol_info_item *item;
2517 struct efi_open_protocol_info_entry *match = NULL;
2518 bool opened_by_driver = false;
2519 bool opened_exclusive = false;
2520
2521 /* If there is no agent, only return the interface */
2522 if (!agent_handle)
2523 goto out;
2524
2525 /* For TEST_PROTOCOL ignore interface attribute */
2526 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2527 *protocol_interface = NULL;
2528
2529 /*
2530 * Check if the protocol is already opened by a driver with the same
2531 * attributes or opened exclusively
2532 */
2533 list_for_each_entry(item, &handler->open_infos, link) {
2534 if (item->info.agent_handle == agent_handle) {
2535 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2536 (item->info.attributes == attributes))
2537 return EFI_ALREADY_STARTED;
2538 }
2539 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2540 opened_exclusive = true;
2541 }
2542
2543 /* Only one controller can open the protocol exclusively */
2544 if (opened_exclusive && attributes &
2545 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2546 return EFI_ACCESS_DENIED;
2547
2548 /* Prepare exclusive opening */
2549 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2550 /* Try to disconnect controllers */
2551 list_for_each_entry(item, &handler->open_infos, link) {
2552 if (item->info.attributes ==
2553 EFI_OPEN_PROTOCOL_BY_DRIVER)
2554 EFI_CALL(efi_disconnect_controller(
2555 item->info.controller_handle,
2556 item->info.agent_handle,
2557 NULL));
2558 }
2559 opened_by_driver = false;
2560 /* Check if all controllers are disconnected */
2561 list_for_each_entry(item, &handler->open_infos, link) {
2562 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2563 opened_by_driver = true;
2564 }
2565 /* Only one controller can be conncected */
2566 if (opened_by_driver)
2567 return EFI_ACCESS_DENIED;
2568 }
2569
2570 /* Find existing entry */
2571 list_for_each_entry(item, &handler->open_infos, link) {
2572 if (item->info.agent_handle == agent_handle &&
2573 item->info.controller_handle == controller_handle)
2574 match = &item->info;
2575 }
2576 /* None found, create one */
2577 if (!match) {
2578 match = efi_create_open_info(handler);
2579 if (!match)
2580 return EFI_OUT_OF_RESOURCES;
2581 }
2582
2583 match->agent_handle = agent_handle;
2584 match->controller_handle = controller_handle;
2585 match->attributes = attributes;
2586 match->open_count++;
2587
2588out:
2589 /* For TEST_PROTOCOL ignore interface attribute. */
2590 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2591 *protocol_interface = handler->protocol_interface;
2592
2593 return EFI_SUCCESS;
2594}
2595
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002596/**
Mario Six78a88f72018-07-10 08:40:17 +02002597 * efi_open_protocol() - open protocol interface on a handle
2598 * @handle: handle on which the protocol shall be opened
2599 * @protocol: GUID of the protocol
2600 * @protocol_interface: interface implementing the protocol
2601 * @agent_handle: handle of the driver
2602 * @controller_handle: handle of the controller
2603 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002604 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002605 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002606 *
Mario Six78a88f72018-07-10 08:40:17 +02002607 * See the Unified Extensible Firmware Interface (UEFI) specification for
2608 * details.
2609 *
2610 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002611 */
Alexander Grafbee91162016-03-04 01:09:59 +01002612static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002613 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002614 void **protocol_interface, void *agent_handle,
2615 void *controller_handle, uint32_t attributes)
2616{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002617 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002618 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002619
Rob Clark778e6af2017-09-13 18:05:41 -04002620 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002621 protocol_interface, agent_handle, controller_handle,
2622 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002623
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002624 if (!handle || !protocol ||
2625 (!protocol_interface && attributes !=
2626 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2627 goto out;
2628 }
2629
2630 switch (attributes) {
2631 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2632 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2633 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2634 break;
2635 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2636 if (controller_handle == handle)
2637 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002638 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002639 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2640 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002641 /* Check that the controller handle is valid */
2642 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002643 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002644 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002645 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002646 /* Check that the agent handle is valid */
2647 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002648 goto out;
2649 break;
2650 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002651 goto out;
2652 }
2653
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002654 r = efi_search_protocol(handle, protocol, &handler);
2655 if (r != EFI_SUCCESS)
2656 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002657
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002658 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2659 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002660out:
2661 return EFI_EXIT(r);
2662}
2663
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002664/**
Mario Six78a88f72018-07-10 08:40:17 +02002665 * efi_handle_protocol() - get interface of a protocol on a handle
2666 * @handle: handle on which the protocol shall be opened
2667 * @protocol: GUID of the protocol
2668 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002669 *
2670 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002671 *
Mario Six78a88f72018-07-10 08:40:17 +02002672 * See the Unified Extensible Firmware Interface (UEFI) specification for
2673 * details.
2674 *
2675 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002676 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002677static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002678 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002679 void **protocol_interface)
2680{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002681 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2682 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002683}
2684
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002685/**
Mario Six78a88f72018-07-10 08:40:17 +02002686 * efi_bind_controller() - bind a single driver to a controller
2687 * @controller_handle: controller handle
2688 * @driver_image_handle: driver handle
2689 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002690 *
Mario Six78a88f72018-07-10 08:40:17 +02002691 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002692 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002693static efi_status_t efi_bind_controller(
2694 efi_handle_t controller_handle,
2695 efi_handle_t driver_image_handle,
2696 struct efi_device_path *remain_device_path)
2697{
2698 struct efi_driver_binding_protocol *binding_protocol;
2699 efi_status_t r;
2700
2701 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2702 &efi_guid_driver_binding_protocol,
2703 (void **)&binding_protocol,
2704 driver_image_handle, NULL,
2705 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2706 if (r != EFI_SUCCESS)
2707 return r;
2708 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2709 controller_handle,
2710 remain_device_path));
2711 if (r == EFI_SUCCESS)
2712 r = EFI_CALL(binding_protocol->start(binding_protocol,
2713 controller_handle,
2714 remain_device_path));
2715 EFI_CALL(efi_close_protocol(driver_image_handle,
2716 &efi_guid_driver_binding_protocol,
2717 driver_image_handle, NULL));
2718 return r;
2719}
2720
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002721/**
Mario Six78a88f72018-07-10 08:40:17 +02002722 * efi_connect_single_controller() - connect a single driver to a controller
2723 * @controller_handle: controller
2724 * @driver_image_handle: driver
2725 * @remain_device_path: remainting path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002726 *
Mario Six78a88f72018-07-10 08:40:17 +02002727 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002728 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002729static efi_status_t efi_connect_single_controller(
2730 efi_handle_t controller_handle,
2731 efi_handle_t *driver_image_handle,
2732 struct efi_device_path *remain_device_path)
2733{
2734 efi_handle_t *buffer;
2735 size_t count;
2736 size_t i;
2737 efi_status_t r;
2738 size_t connected = 0;
2739
2740 /* Get buffer with all handles with driver binding protocol */
2741 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2742 &efi_guid_driver_binding_protocol,
2743 NULL, &count, &buffer));
2744 if (r != EFI_SUCCESS)
2745 return r;
2746
2747 /* Context Override */
2748 if (driver_image_handle) {
2749 for (; *driver_image_handle; ++driver_image_handle) {
2750 for (i = 0; i < count; ++i) {
2751 if (buffer[i] == *driver_image_handle) {
2752 buffer[i] = NULL;
2753 r = efi_bind_controller(
2754 controller_handle,
2755 *driver_image_handle,
2756 remain_device_path);
2757 /*
2758 * For drivers that do not support the
2759 * controller or are already connected
2760 * we receive an error code here.
2761 */
2762 if (r == EFI_SUCCESS)
2763 ++connected;
2764 }
2765 }
2766 }
2767 }
2768
2769 /*
2770 * TODO: Some overrides are not yet implemented:
2771 * - Platform Driver Override
2772 * - Driver Family Override Search
2773 * - Bus Specific Driver Override
2774 */
2775
2776 /* Driver Binding Search */
2777 for (i = 0; i < count; ++i) {
2778 if (buffer[i]) {
2779 r = efi_bind_controller(controller_handle,
2780 buffer[i],
2781 remain_device_path);
2782 if (r == EFI_SUCCESS)
2783 ++connected;
2784 }
2785 }
2786
2787 efi_free_pool(buffer);
2788 if (!connected)
2789 return EFI_NOT_FOUND;
2790 return EFI_SUCCESS;
2791}
2792
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002793/**
Mario Six78a88f72018-07-10 08:40:17 +02002794 * efi_connect_controller() - connect a controller to a driver
2795 * @controller_handle: handle of the controller
2796 * @driver_image_handle: handle of the driver
2797 * @remain_device_path: device path of a child controller
2798 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002799 *
2800 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002801 *
2802 * See the Unified Extensible Firmware Interface (UEFI) specification for
2803 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002804 *
2805 * First all driver binding protocol handles are tried for binding drivers.
2806 * Afterwards all handles that have openened a protocol of the controller
2807 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2808 *
Mario Six78a88f72018-07-10 08:40:17 +02002809 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002810 */
2811static efi_status_t EFIAPI efi_connect_controller(
2812 efi_handle_t controller_handle,
2813 efi_handle_t *driver_image_handle,
2814 struct efi_device_path *remain_device_path,
2815 bool recursive)
2816{
2817 efi_status_t r;
2818 efi_status_t ret = EFI_NOT_FOUND;
2819 struct efi_object *efiobj;
2820
2821 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2822 remain_device_path, recursive);
2823
2824 efiobj = efi_search_obj(controller_handle);
2825 if (!efiobj) {
2826 ret = EFI_INVALID_PARAMETER;
2827 goto out;
2828 }
2829
2830 r = efi_connect_single_controller(controller_handle,
2831 driver_image_handle,
2832 remain_device_path);
2833 if (r == EFI_SUCCESS)
2834 ret = EFI_SUCCESS;
2835 if (recursive) {
2836 struct efi_handler *handler;
2837 struct efi_open_protocol_info_item *item;
2838
2839 list_for_each_entry(handler, &efiobj->protocols, link) {
2840 list_for_each_entry(item, &handler->open_infos, link) {
2841 if (item->info.attributes &
2842 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2843 r = EFI_CALL(efi_connect_controller(
2844 item->info.controller_handle,
2845 driver_image_handle,
2846 remain_device_path,
2847 recursive));
2848 if (r == EFI_SUCCESS)
2849 ret = EFI_SUCCESS;
2850 }
2851 }
2852 }
2853 }
2854 /* Check for child controller specified by end node */
2855 if (ret != EFI_SUCCESS && remain_device_path &&
2856 remain_device_path->type == DEVICE_PATH_TYPE_END)
2857 ret = EFI_SUCCESS;
2858out:
2859 return EFI_EXIT(ret);
2860}
2861
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002862/**
Mario Six78a88f72018-07-10 08:40:17 +02002863 * efi_reinstall_protocol_interface() - reinstall protocol interface
2864 * @handle: handle on which the protocol shall be reinstalled
2865 * @protocol: GUID of the protocol to be installed
2866 * @old_interface: interface to be removed
2867 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002868 *
2869 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002870 *
2871 * See the Unified Extensible Firmware Interface (UEFI) specification for
2872 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002873 *
2874 * The old interface is uninstalled. The new interface is installed.
2875 * Drivers are connected.
2876 *
Mario Six78a88f72018-07-10 08:40:17 +02002877 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002878 */
2879static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2880 efi_handle_t handle, const efi_guid_t *protocol,
2881 void *old_interface, void *new_interface)
2882{
2883 efi_status_t ret;
2884
2885 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2886 new_interface);
2887 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2888 old_interface));
2889 if (ret != EFI_SUCCESS)
2890 goto out;
2891 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2892 EFI_NATIVE_INTERFACE,
2893 new_interface));
2894 if (ret != EFI_SUCCESS)
2895 goto out;
2896 /*
2897 * The returned status code has to be ignored.
2898 * Do not create an error if no suitable driver for the handle exists.
2899 */
2900 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2901out:
2902 return EFI_EXIT(ret);
2903}
2904
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002905/**
Mario Six78a88f72018-07-10 08:40:17 +02002906 * efi_get_child_controllers() - get all child controllers associated to a driver
2907 * @efiobj: handle of the controller
2908 * @driver_handle: handle of the driver
2909 * @number_of_children: number of child controllers
2910 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002911 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002912 * The allocated buffer has to be freed with free().
2913 *
Mario Six78a88f72018-07-10 08:40:17 +02002914 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002915 */
2916static efi_status_t efi_get_child_controllers(
2917 struct efi_object *efiobj,
2918 efi_handle_t driver_handle,
2919 efi_uintn_t *number_of_children,
2920 efi_handle_t **child_handle_buffer)
2921{
2922 struct efi_handler *handler;
2923 struct efi_open_protocol_info_item *item;
2924 efi_uintn_t count = 0, i;
2925 bool duplicate;
2926
2927 /* Count all child controller associations */
2928 list_for_each_entry(handler, &efiobj->protocols, link) {
2929 list_for_each_entry(item, &handler->open_infos, link) {
2930 if (item->info.agent_handle == driver_handle &&
2931 item->info.attributes &
2932 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2933 ++count;
2934 }
2935 }
2936 /*
2937 * Create buffer. In case of duplicate child controller assignments
2938 * the buffer will be too large. But that does not harm.
2939 */
2940 *number_of_children = 0;
2941 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2942 if (!*child_handle_buffer)
2943 return EFI_OUT_OF_RESOURCES;
2944 /* Copy unique child handles */
2945 list_for_each_entry(handler, &efiobj->protocols, link) {
2946 list_for_each_entry(item, &handler->open_infos, link) {
2947 if (item->info.agent_handle == driver_handle &&
2948 item->info.attributes &
2949 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2950 /* Check this is a new child controller */
2951 duplicate = false;
2952 for (i = 0; i < *number_of_children; ++i) {
2953 if ((*child_handle_buffer)[i] ==
2954 item->info.controller_handle)
2955 duplicate = true;
2956 }
2957 /* Copy handle to buffer */
2958 if (!duplicate) {
2959 i = (*number_of_children)++;
2960 (*child_handle_buffer)[i] =
2961 item->info.controller_handle;
2962 }
2963 }
2964 }
2965 }
2966 return EFI_SUCCESS;
2967}
2968
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002969/**
Mario Six78a88f72018-07-10 08:40:17 +02002970 * efi_disconnect_controller() - disconnect a controller from a driver
2971 * @controller_handle: handle of the controller
2972 * @driver_image_handle: handle of the driver
2973 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002974 *
2975 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002976 *
Mario Six78a88f72018-07-10 08:40:17 +02002977 * See the Unified Extensible Firmware Interface (UEFI) specification for
2978 * details.
2979 *
2980 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002981 */
2982static efi_status_t EFIAPI efi_disconnect_controller(
2983 efi_handle_t controller_handle,
2984 efi_handle_t driver_image_handle,
2985 efi_handle_t child_handle)
2986{
2987 struct efi_driver_binding_protocol *binding_protocol;
2988 efi_handle_t *child_handle_buffer = NULL;
2989 size_t number_of_children = 0;
2990 efi_status_t r;
2991 size_t stop_count = 0;
2992 struct efi_object *efiobj;
2993
2994 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2995 child_handle);
2996
2997 efiobj = efi_search_obj(controller_handle);
2998 if (!efiobj) {
2999 r = EFI_INVALID_PARAMETER;
3000 goto out;
3001 }
3002
3003 if (child_handle && !efi_search_obj(child_handle)) {
3004 r = EFI_INVALID_PARAMETER;
3005 goto out;
3006 }
3007
3008 /* If no driver handle is supplied, disconnect all drivers */
3009 if (!driver_image_handle) {
3010 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3011 goto out;
3012 }
3013
3014 /* Create list of child handles */
3015 if (child_handle) {
3016 number_of_children = 1;
3017 child_handle_buffer = &child_handle;
3018 } else {
3019 efi_get_child_controllers(efiobj,
3020 driver_image_handle,
3021 &number_of_children,
3022 &child_handle_buffer);
3023 }
3024
3025 /* Get the driver binding protocol */
3026 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3027 &efi_guid_driver_binding_protocol,
3028 (void **)&binding_protocol,
3029 driver_image_handle, NULL,
3030 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3031 if (r != EFI_SUCCESS)
3032 goto out;
3033 /* Remove the children */
3034 if (number_of_children) {
3035 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3036 controller_handle,
3037 number_of_children,
3038 child_handle_buffer));
3039 if (r == EFI_SUCCESS)
3040 ++stop_count;
3041 }
3042 /* Remove the driver */
3043 if (!child_handle)
3044 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3045 controller_handle,
3046 0, NULL));
3047 if (r == EFI_SUCCESS)
3048 ++stop_count;
3049 EFI_CALL(efi_close_protocol(driver_image_handle,
3050 &efi_guid_driver_binding_protocol,
3051 driver_image_handle, NULL));
3052
3053 if (stop_count)
3054 r = EFI_SUCCESS;
3055 else
3056 r = EFI_NOT_FOUND;
3057out:
3058 if (!child_handle)
3059 free(child_handle_buffer);
3060 return EFI_EXIT(r);
3061}
3062
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003063static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003064 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003065 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3066 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003067 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003068 },
3069 .raise_tpl = efi_raise_tpl,
3070 .restore_tpl = efi_restore_tpl,
3071 .allocate_pages = efi_allocate_pages_ext,
3072 .free_pages = efi_free_pages_ext,
3073 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003074 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003075 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003076 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003077 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003078 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003079 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003080 .close_event = efi_close_event,
3081 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003082 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003083 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003084 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003085 .handle_protocol = efi_handle_protocol,
3086 .reserved = NULL,
3087 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003088 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003089 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003090 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003091 .load_image = efi_load_image,
3092 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003093 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003094 .unload_image = efi_unload_image,
3095 .exit_boot_services = efi_exit_boot_services,
3096 .get_next_monotonic_count = efi_get_next_monotonic_count,
3097 .stall = efi_stall,
3098 .set_watchdog_timer = efi_set_watchdog_timer,
3099 .connect_controller = efi_connect_controller,
3100 .disconnect_controller = efi_disconnect_controller,
3101 .open_protocol = efi_open_protocol,
3102 .close_protocol = efi_close_protocol,
3103 .open_protocol_information = efi_open_protocol_information,
3104 .protocols_per_handle = efi_protocols_per_handle,
3105 .locate_handle_buffer = efi_locate_handle_buffer,
3106 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003107 .install_multiple_protocol_interfaces =
3108 efi_install_multiple_protocol_interfaces,
3109 .uninstall_multiple_protocol_interfaces =
3110 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003111 .calculate_crc32 = efi_calculate_crc32,
3112 .copy_mem = efi_copy_mem,
3113 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003114 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003115};
3116
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003117static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003118
Alexander Graf3c63db92016-10-14 13:45:30 +02003119struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003120 .hdr = {
3121 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003122 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003123 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003124 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003125 .fw_vendor = firmware_vendor,
3126 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003127 .con_in = (void *)&efi_con_in,
3128 .con_out = (void *)&efi_con_out,
3129 .std_err = (void *)&efi_con_out,
3130 .runtime = (void *)&efi_runtime_services,
3131 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003132 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003133 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003134};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003135
3136/**
3137 * efi_initialize_system_table() - Initialize system table
3138 *
3139 * Return Value: status code
3140 */
3141efi_status_t efi_initialize_system_table(void)
3142{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003143 efi_status_t ret;
3144
3145 /* Allocate configuration table array */
3146 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3147 EFI_MAX_CONFIGURATION_TABLES *
3148 sizeof(struct efi_configuration_table),
3149 (void **)&systab.tables);
3150
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003151 /* Set crc32 field in table headers */
3152 efi_update_table_header_crc32(&systab.hdr);
3153 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3154 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003155
3156 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003157}