blob: b9e54f551a4fd62e180161d38b4f848d5784fec2 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029
Alexander Grafc15d9212016-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 Glasscdfe6962016-09-25 15:27:35 -060038#ifdef CONFIG_ARM
Alexander Grafc15d9212016-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 Glasscdfe6962016-09-25 15:27:35 -060046#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010047
Rob Clark86789d52017-07-27 08:04:18 -040048static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040049static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010050/* GUID of the device tree table */
51const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-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 Clark86789d52017-07-27 08:04:18 -040055
Heinrich Schuchardt672d99e2018-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 Schuchardtb7cb8b42018-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 Schuchardte9943282018-01-11 08:16:04 +010076
Rob Clark86789d52017-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 Grafc15d9212016-03-04 01:09:59 +010099/* Called from do_bootefi_exec() */
100void efi_save_gd(void)
101{
Simon Glasscdfe6962016-09-25 15:27:35 -0600102#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100103 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600104#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100105}
106
Rob Clark86789d52017-07-27 08:04:18 -0400107/*
Mario Six8fac2912018-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 Clark86789d52017-07-27 08:04:18 -0400111 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100112void efi_restore_gd(void)
113{
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100115 /* Only restore if we're already in EFI context */
116 if (!efi_gd)
117 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100118 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600119#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100120}
121
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200122/**
Mario Six8fac2912018-07-10 08:40:17 +0200123 * indent_string() - returns a string for indenting with two spaces per level
124 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100125 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200126 * A maximum of ten indent levels is supported. Higher indent levels will be
127 * truncated.
128 *
Mario Six8fac2912018-07-10 08:40:17 +0200129 * Return: A string for indenting with two spaces per level is
130 * returned.
Rob Clarke7896c32017-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 Schuchardt91064592018-02-18 15:17:49 +0100136
Rob Clarke7896c32017-07-27 08:04:19 -0400137 level = min(max, level * 2);
138 return &indent[max - level];
139}
140
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200141const char *__efi_nesting(void)
142{
143 return indent_string(nesting_level);
144}
145
Rob Clarke7896c32017-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 Schuchardt59999172018-05-11 18:15:41 +0200156/**
Heinrich Schuchardt15070db2018-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 Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200172 *
173 * This function queues the notification function of the event for future
174 * execution.
175 *
Mario Six8fac2912018-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 Schuchardta40db6e2017-09-21 18:30:11 +0200178 *
179 * For the SignalEvent service see efi_signal_event_ext.
180 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200181 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100182static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200183{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200184 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200185 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200186 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100187 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200188 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200189 EFI_CALL_VOID(event->notify_function(event,
190 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200191 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200192 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200193}
194
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200195/**
Heinrich Schuchardt7f0f0052018-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 Six8fac2912018-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 Schuchardtbf7f1692018-02-18 15:17:52 +0100218 *
Mario Six8fac2912018-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 Schuchardtbf7f1692018-02-18 15:17:52 +0100221 * their notification function is queued.
222 *
223 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-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 Schuchardt59999172018-05-11 18:15:41 +0200257/**
Mario Six8fac2912018-07-10 08:40:17 +0200258 * efi_raise_tpl() - raise the task priority level
259 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200260 *
261 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200262 *
263 * See the Unified Extensible Firmware Interface (UEFI) specification for
264 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200265 *
Mario Six8fac2912018-07-10 08:40:17 +0200266 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200267 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100268static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100269{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100270 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200271
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200272 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200273
274 if (new_tpl < efi_tpl)
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 Grafc15d9212016-03-04 01:09:59 +0100282}
283
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200284/**
Mario Six8fac2912018-07-10 08:40:17 +0200285 * efi_restore_tpl() - lower the task priority level
286 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200287 *
288 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200289 *
Mario Six8fac2912018-07-10 08:40:17 +0200290 * See the Unified Extensible Firmware Interface (UEFI) specification for
291 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200292 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100293static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100294{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200295 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200296
297 if (old_tpl > efi_tpl)
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 Schuchardt59b20952018-03-24 18:40:21 +0100303 /*
304 * Lowering the TPL may have made queued events eligible for execution.
305 */
306 efi_timer_check();
307
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200308 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100309}
310
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200311/**
Mario Six8fac2912018-07-10 08:40:17 +0200312 * efi_allocate_pages_ext() - allocate memory pages
313 * @type: type of allocation to be performed
314 * @memory_type: usage type of the allocated memory
315 * @pages: number of pages to be allocated
316 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200317 *
318 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200319 *
Mario Six8fac2912018-07-10 08:40:17 +0200320 * See the Unified Extensible Firmware Interface (UEFI) specification for
321 * details.
322 *
323 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900325static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100326 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900327 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100328{
329 efi_status_t r;
330
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100331 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100332 r = efi_allocate_pages(type, memory_type, pages, memory);
333 return EFI_EXIT(r);
334}
335
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200336/**
Mario Six8fac2912018-07-10 08:40:17 +0200337 * efi_free_pages_ext() - Free memory pages.
338 * @memory: start of the memory area to be freed
339 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200340 *
341 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200342 *
343 * See the Unified Extensible Firmware Interface (UEFI) specification for
344 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200345 *
Mario Six8fac2912018-07-10 08:40:17 +0200346 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900348static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100349 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100350{
351 efi_status_t r;
352
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100353 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100354 r = efi_free_pages(memory, pages);
355 return EFI_EXIT(r);
356}
357
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200358/**
Mario Six8fac2912018-07-10 08:40:17 +0200359 * efi_get_memory_map_ext() - get map describing memory usage
360 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
361 * on exit the size of the copied memory map
362 * @memory_map: buffer to which the memory map is written
363 * @map_key: key for the memory map
364 * @descriptor_size: size of an individual memory descriptor
365 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200366 *
367 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200368 *
Mario Six8fac2912018-07-10 08:40:17 +0200369 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * details.
371 *
372 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900374static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100375 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900376 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100377 efi_uintn_t *map_key,
378 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900379 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100380{
381 efi_status_t r;
382
383 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
384 map_key, descriptor_size, descriptor_version);
385 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
386 descriptor_size, descriptor_version);
387 return EFI_EXIT(r);
388}
389
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200390/**
Mario Six8fac2912018-07-10 08:40:17 +0200391 * efi_allocate_pool_ext() - allocate memory from pool
392 * @pool_type: type of the pool from which memory is to be allocated
393 * @size: number of bytes to be allocated
394 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
396 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200397 *
398 * See the Unified Extensible Firmware Interface (UEFI) specification for
399 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200400 *
Mario Six8fac2912018-07-10 08:40:17 +0200401 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200402 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200403static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100404 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200405 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100406{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100407 efi_status_t r;
408
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100409 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200410 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100411 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100412}
413
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200414/**
Mario Six8fac2912018-07-10 08:40:17 +0200415 * efi_free_pool_ext() - free memory from pool
416 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200417 *
418 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200419 *
Mario Six8fac2912018-07-10 08:40:17 +0200420 * See the Unified Extensible Firmware Interface (UEFI) specification for
421 * details.
422 *
423 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200424 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200425static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100426{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100427 efi_status_t r;
428
429 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200430 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100431 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100432}
433
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200434/**
Mario Six8fac2912018-07-10 08:40:17 +0200435 * efi_add_handle() - add a new object to the object list
436 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100437 *
Mario Six8fac2912018-07-10 08:40:17 +0200438 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt967d7de2017-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 Schuchardt59999172018-05-11 18:15:41 +0200449/**
Mario Six8fac2912018-07-10 08:40:17 +0200450 * efi_create_handle() - create handle
451 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200452 *
Mario Six8fac2912018-07-10 08:40:17 +0200453 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200454 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100455efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200456{
457 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200458
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200459 obj = calloc(1, sizeof(struct efi_object));
460 if (!obj)
461 return EFI_OUT_OF_RESOURCES;
462
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100463 efi_add_handle(obj);
464 *handle = obj->handle;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200465
466 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200467}
468
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200469/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100474 *
Mario Six8fac2912018-07-10 08:40:17 +0200475 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100476 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100477efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-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 Schuchardt59999172018-05-11 18:15:41 +0200502/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100507 *
Mario Six8fac2912018-07-10 08:40:17 +0200508 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100509 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100510efi_status_t efi_remove_protocol(const efi_handle_t handle,
511 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-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 Schuchardtb27594d2018-05-11 12:09:21 +0200522 if (handler->protocol_interface != protocol_interface)
523 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 list_del(&handler->link);
525 free(handler);
526 return EFI_SUCCESS;
527}
528
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200529/**
Mario Six8fac2912018-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 Schuchardt7d135462017-12-04 18:03:02 +0100532 *
Mario Six8fac2912018-07-10 08:40:17 +0200533 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100535efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100536{
537 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100538 struct efi_handler *protocol;
539 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100540
541 efiobj = efi_search_obj(handle);
542 if (!efiobj)
543 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100544 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100545 efi_status_t ret;
546
Heinrich Schuchardt7d135462017-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 Schuchardt59999172018-05-11 18:15:41 +0200555/**
Mario Six8fac2912018-07-10 08:40:17 +0200556 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557 *
Mario Six8fac2912018-07-10 08:40:17 +0200558 * @obj: handle to delete
Heinrich Schuchardt7d135462017-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 Schuchardt59999172018-05-11 18:15:41 +0200569/**
Mario Six8fac2912018-07-10 08:40:17 +0200570 * efi_is_event() - check if a pointer is a valid event
571 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100572 *
Mario Six8fac2912018-07-10 08:40:17 +0200573 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100574 */
Heinrich Schuchardt370c7a82018-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 Grafc15d9212016-03-04 01:09:59 +0100587
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200588/**
Mario Six8fac2912018-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 Schuchardta40db6e2017-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 Six8fac2912018-07-10 08:40:17 +0200602 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200603 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100604efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200605 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200606 struct efi_event *event,
607 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100608 void *notify_context, efi_guid_t *group,
609 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100610{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100611 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200612
Jonathan Gray7758b212017-03-12 19:26:07 +1100613 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200614 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100615
Heinrich Schuchardt7f0f0052018-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.de852a0e1772017-07-18 20:17:20 +0200627 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200628 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100629
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200630 if (is_valid_tpl(notify_tpl) != EFI_SUCCESS)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200631 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100632
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100633 evt = calloc(1, sizeof(struct efi_event));
634 if (!evt)
635 return EFI_OUT_OF_RESOURCES;
636 evt->type = type;
637 evt->notify_tpl = notify_tpl;
638 evt->notify_function = notify_function;
639 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100640 evt->group = group;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100641 /* Disable timers on bootup */
642 evt->trigger_next = -1ULL;
643 evt->is_queued = false;
644 evt->is_signaled = false;
645 list_add_tail(&evt->link, &efi_events);
646 *event = evt;
647 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100648}
649
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200650/*
Mario Six8fac2912018-07-10 08:40:17 +0200651 * efi_create_event_ex() - create an event in a group
652 * @type: type of the event to create
653 * @notify_tpl: task priority level of the event
654 * @notify_function: notification function of the event
655 * @notify_context: pointer passed to the notification function
656 * @event: created event
657 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100658 *
659 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100660 *
Mario Six8fac2912018-07-10 08:40:17 +0200661 * See the Unified Extensible Firmware Interface (UEFI) specification for
662 * details.
663 *
664 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100665 */
666efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
667 void (EFIAPI *notify_function) (
668 struct efi_event *event,
669 void *context),
670 void *notify_context,
671 efi_guid_t *event_group,
672 struct efi_event **event)
673{
674 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
675 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100676 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100677 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100678}
679
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200680/**
Mario Six8fac2912018-07-10 08:40:17 +0200681 * efi_create_event_ext() - create an event
682 * @type: type of the event to create
683 * @notify_tpl: task priority level of the event
684 * @notify_function: notification function of the event
685 * @notify_context: pointer passed to the notification function
686 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200687 *
688 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200689 *
690 * See the Unified Extensible Firmware Interface (UEFI) specification for
691 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200692 *
Mario Six8fac2912018-07-10 08:40:17 +0200693 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200694 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200695static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100696 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200697 void (EFIAPI *notify_function) (
698 struct efi_event *event,
699 void *context),
700 void *notify_context, struct efi_event **event)
701{
702 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
703 notify_context);
704 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100705 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200706}
707
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200708/**
Mario Six8fac2912018-07-10 08:40:17 +0200709 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200710 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200711 * Check if a timer event has occurred or a queued notification function should
712 * be called.
713 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100714 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200715 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100716 */
717void efi_timer_check(void)
718{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100719 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100720 u64 now = timer_get_us();
721
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100722 list_for_each_entry(evt, &efi_events, link) {
723 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100724 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100725 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200726 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100727 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200728 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100729 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200730 break;
731 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100732 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200733 break;
734 default:
735 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200736 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100737 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100738 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100739 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100740 WATCHDOG_RESET();
741}
742
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200743/**
Mario Six8fac2912018-07-10 08:40:17 +0200744 * efi_set_timer() - set the trigger time for a timer event or stop the event
745 * @event: event for which the timer is set
746 * @type: type of the timer
747 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200748 *
749 * This is the function for internal usage in U-Boot. For the API function
750 * implementing the SetTimer service see efi_set_timer_ext.
751 *
Mario Six8fac2912018-07-10 08:40:17 +0200752 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200753 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200754efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200755 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100756{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100757 /* Check that the event is valid */
758 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
759 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100760
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200761 /*
762 * The parameter defines a multiple of 100ns.
763 * We use multiples of 1000ns. So divide by 10.
764 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200765 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100766
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100767 switch (type) {
768 case EFI_TIMER_STOP:
769 event->trigger_next = -1ULL;
770 break;
771 case EFI_TIMER_PERIODIC:
772 case EFI_TIMER_RELATIVE:
773 event->trigger_next = timer_get_us() + trigger_time;
774 break;
775 default:
776 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100777 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100778 event->trigger_type = type;
779 event->trigger_time = trigger_time;
780 event->is_signaled = false;
781 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100782}
783
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200784/**
Mario Six8fac2912018-07-10 08:40:17 +0200785 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
786 * event
787 * @event: event for which the timer is set
788 * @type: type of the timer
789 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200790 *
791 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200792 *
Mario Six8fac2912018-07-10 08:40:17 +0200793 * See the Unified Extensible Firmware Interface (UEFI) specification for
794 * details.
795 *
796 *
797 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200798 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200799static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
800 enum efi_timer_delay type,
801 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200802{
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100803 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200804 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
805}
806
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200807/**
Mario Six8fac2912018-07-10 08:40:17 +0200808 * efi_wait_for_event() - wait for events to be signaled
809 * @num_events: number of events to be waited for
810 * @event: events to be waited for
811 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200812 *
813 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200814 *
Mario Six8fac2912018-07-10 08:40:17 +0200815 * See the Unified Extensible Firmware Interface (UEFI) specification for
816 * details.
817 *
818 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200819 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100820static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200821 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100822 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100823{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100824 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100825
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100826 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100827
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200828 /* Check parameters */
829 if (!num_events || !event)
830 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200831 /* Check TPL */
832 if (efi_tpl != TPL_APPLICATION)
833 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200834 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100835 if (efi_is_event(event[i]) != EFI_SUCCESS)
836 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200837 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
838 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200839 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100840 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200841 }
842
843 /* Wait for signal */
844 for (;;) {
845 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200846 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200847 goto out;
848 }
849 /* Allow events to occur. */
850 efi_timer_check();
851 }
852
853out:
854 /*
855 * Reset the signal which is passed to the caller to allow periodic
856 * events to occur.
857 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200858 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200859 if (index)
860 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100861
862 return EFI_EXIT(EFI_SUCCESS);
863}
864
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200865/**
Mario Six8fac2912018-07-10 08:40:17 +0200866 * efi_signal_event_ext() - signal an EFI event
867 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200868 *
869 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200870 *
871 * See the Unified Extensible Firmware Interface (UEFI) specification for
872 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200873 *
874 * This functions sets the signaled state of the event and queues the
875 * notification function for execution.
876 *
Mario Six8fac2912018-07-10 08:40:17 +0200877 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200878 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200879static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100880{
881 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100882 if (efi_is_event(event) != EFI_SUCCESS)
883 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100884 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100885 return EFI_EXIT(EFI_SUCCESS);
886}
887
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200888/**
Mario Six8fac2912018-07-10 08:40:17 +0200889 * efi_close_event() - close an EFI event
890 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 *
892 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200893 *
Mario Six8fac2912018-07-10 08:40:17 +0200894 * See the Unified Extensible Firmware Interface (UEFI) specification for
895 * details.
896 *
897 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200898 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200899static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100900{
901 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100902 if (efi_is_event(event) != EFI_SUCCESS)
903 return EFI_EXIT(EFI_INVALID_PARAMETER);
904 list_del(&event->link);
905 free(event);
906 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100907}
908
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200909/**
Mario Six8fac2912018-07-10 08:40:17 +0200910 * efi_check_event() - check if an event is signaled
911 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200912 *
913 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200914 *
915 * See the Unified Extensible Firmware Interface (UEFI) specification for
916 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200917 *
Mario Six8fac2912018-07-10 08:40:17 +0200918 * If an event is not signaled yet, the notification function is queued. The
919 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200920 *
Mario Six8fac2912018-07-10 08:40:17 +0200921 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200922 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200923static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100924{
925 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200926 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100927 if (efi_is_event(event) != EFI_SUCCESS ||
928 event->type & EVT_NOTIFY_SIGNAL)
929 return EFI_EXIT(EFI_INVALID_PARAMETER);
930 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100931 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100932 if (event->is_signaled) {
933 event->is_signaled = false;
934 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200935 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100936 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100937}
938
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200939/**
Mario Six8fac2912018-07-10 08:40:17 +0200940 * efi_search_obj() - find the internal EFI object for a handle
941 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200942 *
Mario Six8fac2912018-07-10 08:40:17 +0200943 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200944 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100945struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200946{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100947 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200948
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100949 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200950 if (efiobj->handle == handle)
951 return efiobj;
952 }
953
954 return NULL;
955}
956
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200957/**
Mario Six8fac2912018-07-10 08:40:17 +0200958 * efi_open_protocol_info_entry() - create open protocol info entry and add it
959 * to a protocol
960 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100961 *
Mario Six8fac2912018-07-10 08:40:17 +0200962 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100963 */
964static struct efi_open_protocol_info_entry *efi_create_open_info(
965 struct efi_handler *handler)
966{
967 struct efi_open_protocol_info_item *item;
968
969 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
970 if (!item)
971 return NULL;
972 /* Append the item to the open protocol info list. */
973 list_add_tail(&item->link, &handler->open_infos);
974
975 return &item->info;
976}
977
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200978/**
Mario Six8fac2912018-07-10 08:40:17 +0200979 * efi_delete_open_info() - remove an open protocol info entry from a protocol
980 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100981 *
Mario Six8fac2912018-07-10 08:40:17 +0200982 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100983 */
984static efi_status_t efi_delete_open_info(
985 struct efi_open_protocol_info_item *item)
986{
987 list_del(&item->link);
988 free(item);
989 return EFI_SUCCESS;
990}
991
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200992/**
Mario Six8fac2912018-07-10 08:40:17 +0200993 * efi_add_protocol() - install new protocol on a handle
994 * @handle: handle on which the protocol shall be installed
995 * @protocol: GUID of the protocol to be installed
996 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200997 *
Mario Six8fac2912018-07-10 08:40:17 +0200998 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200999 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001000efi_status_t efi_add_protocol(const efi_handle_t handle,
1001 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001002 void *protocol_interface)
1003{
1004 struct efi_object *efiobj;
1005 struct efi_handler *handler;
1006 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001007
1008 efiobj = efi_search_obj(handle);
1009 if (!efiobj)
1010 return EFI_INVALID_PARAMETER;
1011 ret = efi_search_protocol(handle, protocol, NULL);
1012 if (ret != EFI_NOT_FOUND)
1013 return EFI_INVALID_PARAMETER;
1014 handler = calloc(1, sizeof(struct efi_handler));
1015 if (!handler)
1016 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001017 handler->guid = protocol;
1018 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001019 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001020 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001021 if (!guidcmp(&efi_guid_device_path, protocol))
1022 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001023 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001024}
1025
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001026/**
Mario Six8fac2912018-07-10 08:40:17 +02001027 * efi_install_protocol_interface() - install protocol interface
1028 * @handle: handle on which the protocol shall be installed
1029 * @protocol: GUID of the protocol to be installed
1030 * @protocol_interface_type: type of the interface to be installed,
1031 * always EFI_NATIVE_INTERFACE
1032 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001033 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001034 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001035 *
Mario Six8fac2912018-07-10 08:40:17 +02001036 * See the Unified Extensible Firmware Interface (UEFI) specification for
1037 * details.
1038 *
1039 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001040 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001041static efi_status_t EFIAPI efi_install_protocol_interface(
1042 void **handle, const efi_guid_t *protocol,
1043 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001044{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001045 efi_status_t r;
1046
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001047 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1048 protocol_interface);
1049
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001050 if (!handle || !protocol ||
1051 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1052 r = EFI_INVALID_PARAMETER;
1053 goto out;
1054 }
1055
1056 /* Create new handle if requested. */
1057 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001058 r = efi_create_handle(handle);
1059 if (r != EFI_SUCCESS)
1060 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001061 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1062 *handle);
1063 } else {
1064 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1065 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001066 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001067 /* Add new protocol */
1068 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001069out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001070 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001071}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001072
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001073/**
Mario Six8fac2912018-07-10 08:40:17 +02001074 * efi_get_drivers() - get all drivers associated to a controller
1075 * @efiobj: handle of the controller
1076 * @protocol: protocol guid (optional)
1077 * @number_of_drivers: number of child controllers
1078 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001079 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001080 * The allocated buffer has to be freed with free().
1081 *
Mario Six8fac2912018-07-10 08:40:17 +02001082 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001083 */
1084static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1085 const efi_guid_t *protocol,
1086 efi_uintn_t *number_of_drivers,
1087 efi_handle_t **driver_handle_buffer)
1088{
1089 struct efi_handler *handler;
1090 struct efi_open_protocol_info_item *item;
1091 efi_uintn_t count = 0, i;
1092 bool duplicate;
1093
1094 /* Count all driver associations */
1095 list_for_each_entry(handler, &efiobj->protocols, link) {
1096 if (protocol && guidcmp(handler->guid, protocol))
1097 continue;
1098 list_for_each_entry(item, &handler->open_infos, link) {
1099 if (item->info.attributes &
1100 EFI_OPEN_PROTOCOL_BY_DRIVER)
1101 ++count;
1102 }
1103 }
1104 /*
1105 * Create buffer. In case of duplicate driver assignments the buffer
1106 * will be too large. But that does not harm.
1107 */
1108 *number_of_drivers = 0;
1109 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1110 if (!*driver_handle_buffer)
1111 return EFI_OUT_OF_RESOURCES;
1112 /* Collect unique driver handles */
1113 list_for_each_entry(handler, &efiobj->protocols, link) {
1114 if (protocol && guidcmp(handler->guid, protocol))
1115 continue;
1116 list_for_each_entry(item, &handler->open_infos, link) {
1117 if (item->info.attributes &
1118 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1119 /* Check this is a new driver */
1120 duplicate = false;
1121 for (i = 0; i < *number_of_drivers; ++i) {
1122 if ((*driver_handle_buffer)[i] ==
1123 item->info.agent_handle)
1124 duplicate = true;
1125 }
1126 /* Copy handle to buffer */
1127 if (!duplicate) {
1128 i = (*number_of_drivers)++;
1129 (*driver_handle_buffer)[i] =
1130 item->info.agent_handle;
1131 }
1132 }
1133 }
1134 }
1135 return EFI_SUCCESS;
1136}
1137
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001138/**
Mario Six8fac2912018-07-10 08:40:17 +02001139 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1140 * @efiobj: handle of the controller
1141 * @protocol: protocol guid (optional)
1142 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001143 *
1144 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001145 *
1146 * See the Unified Extensible Firmware Interface (UEFI) specification for
1147 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001148 *
Mario Six8fac2912018-07-10 08:40:17 +02001149 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001150 */
1151static efi_status_t efi_disconnect_all_drivers(
1152 struct efi_object *efiobj,
1153 const efi_guid_t *protocol,
1154 efi_handle_t child_handle)
1155{
1156 efi_uintn_t number_of_drivers;
1157 efi_handle_t *driver_handle_buffer;
1158 efi_status_t r, ret;
1159
1160 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1161 &driver_handle_buffer);
1162 if (ret != EFI_SUCCESS)
1163 return ret;
1164
1165 ret = EFI_NOT_FOUND;
1166 while (number_of_drivers) {
1167 r = EFI_CALL(efi_disconnect_controller(
1168 efiobj->handle,
1169 driver_handle_buffer[--number_of_drivers],
1170 child_handle));
1171 if (r == EFI_SUCCESS)
1172 ret = r;
1173 }
1174 free(driver_handle_buffer);
1175 return ret;
1176}
1177
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001178/**
Mario Six8fac2912018-07-10 08:40:17 +02001179 * efi_uninstall_protocol_interface() - uninstall protocol interface
1180 * @handle: handle from which the protocol shall be removed
1181 * @protocol: GUID of the protocol to be removed
1182 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001183 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001184 * This function implements the UninstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001185 *
Mario Six8fac2912018-07-10 08:40:17 +02001186 * See the Unified Extensible Firmware Interface (UEFI) specification for
1187 * details.
1188 *
1189 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001190 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001191static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001192 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001193 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001194{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001195 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001196 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001197 struct efi_open_protocol_info_item *item;
1198 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001199 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001200
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001201 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1202
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001203 /* Check handle */
1204 efiobj = efi_search_obj(handle);
1205 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001206 r = EFI_INVALID_PARAMETER;
1207 goto out;
1208 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001209 /* Find the protocol on the handle */
1210 r = efi_search_protocol(handle, protocol, &handler);
1211 if (r != EFI_SUCCESS)
1212 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001213 /* Disconnect controllers */
1214 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1215 if (!list_empty(&handler->open_infos)) {
1216 r = EFI_ACCESS_DENIED;
1217 goto out;
1218 }
1219 /* Close protocol */
1220 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1221 if (item->info.attributes ==
1222 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1223 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1224 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1225 list_del(&item->link);
1226 }
1227 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001228 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001229 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001230 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001231 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001232out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001233 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001234}
1235
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001236/**
Mario Six8fac2912018-07-10 08:40:17 +02001237 * efi_register_protocol_notify() - register an event for notification when a
1238 * protocol is installed.
1239 * @protocol: GUID of the protocol whose installation shall be notified
1240 * @event: event to be signaled upon installation of the protocol
1241 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001242 *
1243 * This function implements the RegisterProtocolNotify service.
1244 * See the Unified Extensible Firmware Interface (UEFI) specification
1245 * for details.
1246 *
Mario Six8fac2912018-07-10 08:40:17 +02001247 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001248 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001249static efi_status_t EFIAPI efi_register_protocol_notify(
1250 const efi_guid_t *protocol,
1251 struct efi_event *event,
1252 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001253{
Rob Clark238f88c2017-09-13 18:05:41 -04001254 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001255 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1256}
1257
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001258/**
Mario Six8fac2912018-07-10 08:40:17 +02001259 * efi_search() - determine if an EFI handle implements a protocol
1260 * @search_type: selection criterion
1261 * @protocol: GUID of the protocol
1262 * @search_key: registration key
1263 * @efiobj: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001264 *
1265 * See the documentation of the LocateHandle service in the UEFI specification.
1266 *
Mario Six8fac2912018-07-10 08:40:17 +02001267 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001268 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001269static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001270 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001271 struct efi_object *efiobj)
1272{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001273 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001274
1275 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001276 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001277 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001278 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001279 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001280 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001281 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001282 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1283 return (ret != EFI_SUCCESS);
1284 default:
1285 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001286 return -1;
1287 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001288}
1289
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001290/**
Mario Six8fac2912018-07-10 08:40:17 +02001291 * efi_locate_handle() - locate handles implementing a protocol
1292 * @search_type: selection criterion
1293 * @protocol: GUID of the protocol
1294 * @search_key: registration key
1295 * @buffer_size: size of the buffer to receive the handles in bytes
1296 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001297 *
1298 * This function is meant for U-Boot internal calls. For the API implementation
1299 * of the LocateHandle service see efi_locate_handle_ext.
1300 *
Mario Six8fac2912018-07-10 08:40:17 +02001301 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001302 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001303static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001304 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001305 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001306 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001307{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001308 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001309 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001310
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001311 /* Check parameters */
1312 switch (search_type) {
1313 case ALL_HANDLES:
1314 break;
1315 case BY_REGISTER_NOTIFY:
1316 if (!search_key)
1317 return EFI_INVALID_PARAMETER;
1318 /* RegisterProtocolNotify is not implemented yet */
1319 return EFI_UNSUPPORTED;
1320 case BY_PROTOCOL:
1321 if (!protocol)
1322 return EFI_INVALID_PARAMETER;
1323 break;
1324 default:
1325 return EFI_INVALID_PARAMETER;
1326 }
1327
1328 /*
1329 * efi_locate_handle_buffer uses this function for
1330 * the calculation of the necessary buffer size.
1331 * So do not require a buffer for buffersize == 0.
1332 */
1333 if (!buffer_size || (*buffer_size && !buffer))
1334 return EFI_INVALID_PARAMETER;
1335
Alexander Grafc15d9212016-03-04 01:09:59 +01001336 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001337 list_for_each_entry(efiobj, &efi_obj_list, link) {
1338 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001339 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001340 }
1341
1342 if (*buffer_size < size) {
1343 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001344 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001345 }
1346
Rob Clarkcdee3372017-08-06 14:10:07 -04001347 *buffer_size = size;
1348 if (size == 0)
1349 return EFI_NOT_FOUND;
1350
Alexander Grafc15d9212016-03-04 01:09:59 +01001351 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001352 list_for_each_entry(efiobj, &efi_obj_list, link) {
1353 if (!efi_search(search_type, protocol, search_key, efiobj))
1354 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001355 }
1356
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001357 return EFI_SUCCESS;
1358}
1359
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001360/**
Mario Six8fac2912018-07-10 08:40:17 +02001361 * efi_locate_handle_ext() - locate handles implementing a protocol.
1362 * @search_type: selection criterion
1363 * @protocol: GUID of the protocol
1364 * @search_key: registration key
1365 * @buffer_size: size of the buffer to receive the handles in bytes
1366 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001367 *
1368 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001369 *
Mario Six8fac2912018-07-10 08:40:17 +02001370 * See the Unified Extensible Firmware Interface (UEFI) specification for
1371 * details.
1372 *
1373 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001374 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001375static efi_status_t EFIAPI efi_locate_handle_ext(
1376 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001377 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001378 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001379{
Rob Clark238f88c2017-09-13 18:05:41 -04001380 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001381 buffer_size, buffer);
1382
1383 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1384 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001385}
1386
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001387/**
Mario Six8fac2912018-07-10 08:40:17 +02001388 * efi_remove_configuration_table() - collapses configuration table entries,
1389 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001390 *
Mario Six8fac2912018-07-10 08:40:17 +02001391 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001392 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001393static void efi_remove_configuration_table(int i)
1394{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001395 struct efi_configuration_table *this = &systab.tables[i];
1396 struct efi_configuration_table *next = &systab.tables[i + 1];
1397 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001398
1399 memmove(this, next, (ulong)end - (ulong)next);
1400 systab.nr_tables--;
1401}
1402
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001403/**
Mario Six8fac2912018-07-10 08:40:17 +02001404 * efi_install_configuration_table() - adds, updates, or removes a
1405 * configuration table
1406 * @guid: GUID of the installed table
1407 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001408 *
1409 * This function is used for internal calls. For the API implementation of the
1410 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1411 *
Mario Six8fac2912018-07-10 08:40:17 +02001412 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001413 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001414efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1415 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001416{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001417 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001418 int i;
1419
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001420 if (!guid)
1421 return EFI_INVALID_PARAMETER;
1422
Alexander Grafc15d9212016-03-04 01:09:59 +01001423 /* Check for guid override */
1424 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001425 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001426 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001427 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001428 else
1429 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001430 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001431 }
1432 }
1433
Alexander Graffe3366f2017-07-26 13:41:04 +02001434 if (!table)
1435 return EFI_NOT_FOUND;
1436
Alexander Grafc15d9212016-03-04 01:09:59 +01001437 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001438 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001439 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001440
1441 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001442 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1443 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001444 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001445
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001446out:
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001447 /* systab.nr_tables may have changed. So we need to update the crc32 */
1448 efi_update_table_header_crc32(&systab.hdr);
1449
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001450 /* Notify that the configuration table was changed */
1451 list_for_each_entry(evt, &efi_events, link) {
1452 if (evt->group && !guidcmp(evt->group, guid)) {
1453 efi_signal_event(evt, false);
1454 break;
1455 }
1456 }
1457
Alexander Grafc5c11632016-08-19 01:23:24 +02001458 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001459}
1460
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001461/**
Mario Six8fac2912018-07-10 08:40:17 +02001462 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1463 * configuration table.
1464 * @guid: GUID of the installed table
1465 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001466 *
1467 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001468 *
Mario Six8fac2912018-07-10 08:40:17 +02001469 * See the Unified Extensible Firmware Interface (UEFI) specification for
1470 * details.
1471 *
1472 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001473 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001474static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1475 void *table)
1476{
Rob Clark238f88c2017-09-13 18:05:41 -04001477 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001478 return EFI_EXIT(efi_install_configuration_table(guid, table));
1479}
1480
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001481/**
Mario Six8fac2912018-07-10 08:40:17 +02001482 * efi_setup_loaded_image() - initialize a loaded image
1483 * @info: loaded image info to be passed to the entry point of the image
1484 * @obj: internal object associated with the loaded image
1485 * @device_path: device path of the loaded image
1486 * @file_path: file path of the loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001487 *
1488 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001489 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001490 *
Mario Six8fac2912018-07-10 08:40:17 +02001491 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001492 */
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001493efi_status_t efi_setup_loaded_image(
1494 struct efi_loaded_image *info, struct efi_object *obj,
1495 struct efi_device_path *device_path,
1496 struct efi_device_path *file_path)
Rob Clarkf8db9222017-09-13 18:05:33 -04001497{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001498 efi_status_t ret;
1499
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001500 /* Add internal object to object list */
1501 efi_add_handle(obj);
1502 /* efi_exit() assumes that the handle points to the info */
Rob Clarkf8db9222017-09-13 18:05:33 -04001503 obj->handle = info;
1504
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001505 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001506 info->file_path = file_path;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001507
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001508 if (device_path) {
1509 info->device_handle = efi_dp_find_obj(device_path, NULL);
1510 /*
1511 * When asking for the device path interface, return
1512 * bootefi_device_path
1513 */
1514 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1515 device_path);
1516 if (ret != EFI_SUCCESS)
1517 goto failure;
1518 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001519
1520 /*
1521 * When asking for the loaded_image interface, just
1522 * return handle which points to loaded_image_info
1523 */
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001524 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1525 if (ret != EFI_SUCCESS)
1526 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001527
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001528 ret = efi_add_protocol(obj->handle,
1529 &efi_guid_device_path_to_text_protocol,
1530 (void *)&efi_device_path_to_text);
1531 if (ret != EFI_SUCCESS)
1532 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001533
Leif Lindholmb6e6fdc2018-03-09 17:43:21 +01001534 ret = efi_add_protocol(obj->handle,
1535 &efi_guid_device_path_utilities_protocol,
1536 (void *)&efi_device_path_utilities);
1537 if (ret != EFI_SUCCESS)
1538 goto failure;
1539
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001540 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001541failure:
1542 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001543 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001544}
1545
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001546/**
Mario Six8fac2912018-07-10 08:40:17 +02001547 * efi_load_image_from_path() - load an image using a file path
1548 * @file_path: the path of the image to load
1549 * @buffer: buffer containing the loaded image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001550 *
Mario Six8fac2912018-07-10 08:40:17 +02001551 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001552 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001553efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1554 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001555{
1556 struct efi_file_info *info = NULL;
1557 struct efi_file_handle *f;
1558 static efi_status_t ret;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001559 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001560
1561 f = efi_file_from_path(file_path);
1562 if (!f)
1563 return EFI_DEVICE_ERROR;
1564
1565 bs = 0;
1566 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1567 &bs, info));
1568 if (ret == EFI_BUFFER_TOO_SMALL) {
1569 info = malloc(bs);
1570 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1571 &bs, info));
1572 }
1573 if (ret != EFI_SUCCESS)
1574 goto error;
1575
1576 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1577 if (ret)
1578 goto error;
1579
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001580 bs = info->file_size;
1581 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark857a1222017-09-13 18:05:35 -04001582
1583error:
1584 free(info);
1585 EFI_CALL(f->close(f));
1586
1587 if (ret != EFI_SUCCESS) {
1588 efi_free_pool(*buffer);
1589 *buffer = NULL;
1590 }
1591
1592 return ret;
1593}
1594
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001595/**
Mario Six8fac2912018-07-10 08:40:17 +02001596 * efi_load_image() - load an EFI image into memory
1597 * @boot_policy: true for request originating from the boot manager
1598 * @parent_image: the caller's image handle
1599 * @file_path: the path of the image to load
1600 * @source_buffer: memory location from which the image is installed
1601 * @source_size: size of the memory area from which the image is installed
1602 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001603 *
1604 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001605 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001606 * See the Unified Extensible Firmware Interface (UEFI) specification
1607 * for details.
1608 *
Mario Six8fac2912018-07-10 08:40:17 +02001609 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001610 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001611static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1612 efi_handle_t parent_image,
1613 struct efi_device_path *file_path,
1614 void *source_buffer,
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001615 efi_uintn_t source_size,
Alexander Grafc15d9212016-03-04 01:09:59 +01001616 efi_handle_t *image_handle)
1617{
Alexander Grafc15d9212016-03-04 01:09:59 +01001618 struct efi_loaded_image *info;
1619 struct efi_object *obj;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001620 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001621
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001622 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001623 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001624
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001625 if (!image_handle || !parent_image) {
1626 ret = EFI_INVALID_PARAMETER;
1627 goto error;
1628 }
1629
1630 if (!source_buffer && !file_path) {
1631 ret = EFI_NOT_FOUND;
1632 goto error;
1633 }
1634
Rob Clark857a1222017-09-13 18:05:35 -04001635 info = calloc(1, sizeof(*info));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001636 if (!info) {
1637 ret = EFI_OUT_OF_RESOURCES;
1638 goto error;
1639 }
Rob Clark857a1222017-09-13 18:05:35 -04001640 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001641 if (!obj) {
1642 free(info);
1643 ret = EFI_OUT_OF_RESOURCES;
1644 goto error;
1645 }
Rob Clark857a1222017-09-13 18:05:35 -04001646
1647 if (!source_buffer) {
1648 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001649
Rob Clarkc84c1102017-09-13 18:05:38 -04001650 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001651 if (ret != EFI_SUCCESS)
1652 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001653 /*
1654 * split file_path which contains both the device and
1655 * file parts:
1656 */
1657 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001658 ret = efi_setup_loaded_image(info, obj, dp, fp);
1659 if (ret != EFI_SUCCESS)
1660 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001661 } else {
1662 /* In this case, file_path is the "device" path, ie.
1663 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1664 */
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001665 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1666 if (ret != EFI_SUCCESS)
1667 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001668 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001669 info->reserved = efi_load_pe(source_buffer, info);
1670 if (!info->reserved) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001671 ret = EFI_UNSUPPORTED;
1672 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001673 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001674 info->system_table = &systab;
1675 info->parent_handle = parent_image;
Heinrich Schuchardt754d4972017-11-26 14:05:22 +01001676 *image_handle = obj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001677 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001678failure:
1679 free(info);
1680 efi_delete_handle(obj);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001681error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001682 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001683}
1684
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001685/**
Mario Six8fac2912018-07-10 08:40:17 +02001686 * efi_start_image() - dall the entry point of an image
1687 * @image_handle: handle of the image
1688 * @exit_data_size: size of the buffer
1689 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001690 *
1691 * This function implements the StartImage service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001692 *
Mario Six8fac2912018-07-10 08:40:17 +02001693 * See the Unified Extensible Firmware Interface (UEFI) specification for
1694 * details.
1695 *
1696 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001697 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001698static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1699 unsigned long *exit_data_size,
1700 s16 **exit_data)
1701{
Alexander Graf54d8cdf2018-01-24 00:18:08 +01001702 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1703 struct efi_system_table *st);
Alexander Grafc15d9212016-03-04 01:09:59 +01001704 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001705 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001706
1707 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1708 entry = info->reserved;
1709
1710 efi_is_direct_boot = false;
1711
1712 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001713 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001714 /*
1715 * We called the entry point of the child image with EFI_CALL
1716 * in the lines below. The child image called the Exit() boot
1717 * service efi_exit() which executed the long jump that brought
1718 * us to the current line. This implies that the second half
1719 * of the EFI_CALL macro has not been executed.
1720 */
1721#ifdef CONFIG_ARM
1722 /*
1723 * efi_exit() called efi_restore_gd(). We have to undo this
1724 * otherwise __efi_entry_check() will put the wrong value into
1725 * app_gd.
1726 */
1727 gd = app_gd;
1728#endif
1729 /*
1730 * To get ready to call EFI_EXIT below we have to execute the
1731 * missed out steps of EFI_CALL.
1732 */
1733 assert(__efi_entry_check());
1734 debug("%sEFI: %lu returned by started image\n",
1735 __efi_nesting_dec(),
1736 (unsigned long)((uintptr_t)info->exit_status &
1737 ~EFI_ERROR_MASK));
Alexander Graf988c0662016-05-20 23:28:23 +02001738 return EFI_EXIT(info->exit_status);
1739 }
1740
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001741 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001742
Alexander Grafeff317c2018-01-26 00:47:53 +01001743 /*
1744 * Usually UEFI applications call Exit() instead of returning.
1745 * But because the world doesn not consist of ponies and unicorns,
1746 * we're happy to emulate that behavior on behalf of a payload
1747 * that forgot.
1748 */
1749 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001750}
1751
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001752/**
Mario Six8fac2912018-07-10 08:40:17 +02001753 * efi_exit() - leave an EFI application or driver
1754 * @image_handle: handle of the application or driver that is exiting
1755 * @exit_status: status code
1756 * @exit_data_size: size of the buffer in bytes
1757 * @exit_data: buffer with data describing an error
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001758 *
1759 * This function implements the Exit service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001760 *
Mario Six8fac2912018-07-10 08:40:17 +02001761 * See the Unified Extensible Firmware Interface (UEFI) specification for
1762 * details.
1763 *
1764 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001765 */
Alexander Graf988c0662016-05-20 23:28:23 +02001766static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001767 efi_status_t exit_status,
1768 unsigned long exit_data_size,
1769 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001770{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001771 /*
1772 * We require that the handle points to the original loaded
1773 * image protocol interface.
1774 *
1775 * For getting the longjmp address this is safer than locating
1776 * the protocol because the protocol may have been reinstalled
1777 * pointing to another memory location.
1778 *
1779 * TODO: We should call the unload procedure of the loaded
1780 * image protocol.
1781 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001782 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001783
Alexander Grafc15d9212016-03-04 01:09:59 +01001784 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1785 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001786
Alexander Graf87e787a2017-09-03 14:14:17 +02001787 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001788 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001789
Alexander Graf87e787a2017-09-03 14:14:17 +02001790 /*
1791 * But longjmp out with the U-Boot gd, not the application's, as
1792 * the other end is a setjmp call inside EFI context.
1793 */
1794 efi_restore_gd();
1795
Alexander Graf988c0662016-05-20 23:28:23 +02001796 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001797 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001798
1799 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001800}
1801
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001802/**
Mario Six8fac2912018-07-10 08:40:17 +02001803 * efi_unload_image() - unload an EFI image
1804 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001805 *
1806 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001807 *
1808 * See the Unified Extensible Firmware Interface (UEFI) specification for
1809 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001810 *
Mario Six8fac2912018-07-10 08:40:17 +02001811 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001812 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001813static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001814{
1815 struct efi_object *efiobj;
1816
1817 EFI_ENTRY("%p", image_handle);
1818 efiobj = efi_search_obj(image_handle);
1819 if (efiobj)
1820 list_del(&efiobj->link);
1821
1822 return EFI_EXIT(EFI_SUCCESS);
1823}
1824
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001825/**
Mario Six8fac2912018-07-10 08:40:17 +02001826 * efi_exit_caches() - fix up caches for EFI payloads if necessary
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001827 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001828static void efi_exit_caches(void)
1829{
1830#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1831 /*
1832 * Grub on 32bit ARM needs to have caches disabled before jumping into
1833 * a zImage, but does not know of all cache layers. Give it a hand.
1834 */
1835 if (efi_is_direct_boot)
1836 cleanup_before_linux();
1837#endif
1838}
1839
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001840/**
Mario Six8fac2912018-07-10 08:40:17 +02001841 * efi_exit_boot_services() - stop all boot services
1842 * @image_handle: handle of the loaded image
1843 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001844 *
1845 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001846 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001847 * See the Unified Extensible Firmware Interface (UEFI) specification
1848 * for details.
1849 *
Mario Six8fac2912018-07-10 08:40:17 +02001850 * All timer events are disabled. For exit boot services events the
1851 * notification function is called. The boot services are disabled in the
1852 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001853 *
Mario Six8fac2912018-07-10 08:40:17 +02001854 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001855 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001856static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001857 unsigned long map_key)
1858{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001859 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001860
Alexander Grafc15d9212016-03-04 01:09:59 +01001861 EFI_ENTRY("%p, %ld", image_handle, map_key);
1862
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001863 /* Check that the caller has read the current memory map */
1864 if (map_key != efi_memory_map_key)
1865 return EFI_INVALID_PARAMETER;
1866
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001867 /* Make sure that notification functions are not called anymore */
1868 efi_tpl = TPL_HIGH_LEVEL;
1869
1870 /* Check if ExitBootServices has already been called */
1871 if (!systab.boottime)
1872 return EFI_EXIT(EFI_SUCCESS);
1873
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001874 /* Add related events to the event group */
1875 list_for_each_entry(evt, &efi_events, link) {
1876 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1877 evt->group = &efi_guid_event_group_exit_boot_services;
1878 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001879 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001880 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001881 if (evt->group &&
1882 !guidcmp(evt->group,
1883 &efi_guid_event_group_exit_boot_services)) {
1884 efi_signal_event(evt, false);
1885 break;
1886 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001887 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001888
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001889 /* TODO Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001890
Alexander Graf2ebeb442016-11-17 01:02:57 +01001891 board_quiesce_devices();
1892
Alexander Grafc15d9212016-03-04 01:09:59 +01001893 /* Fix up caches for EFI payloads if necessary */
1894 efi_exit_caches();
1895
1896 /* This stops all lingering devices */
1897 bootm_disable_interrupts();
1898
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001899 /* Disable boottime services */
1900 systab.con_in_handle = NULL;
1901 systab.con_in = NULL;
1902 systab.con_out_handle = NULL;
1903 systab.con_out = NULL;
1904 systab.stderr_handle = NULL;
1905 systab.std_err = NULL;
1906 systab.boottime = NULL;
1907
1908 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001909 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001910
Alexander Grafc15d9212016-03-04 01:09:59 +01001911 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001912 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001913 WATCHDOG_RESET();
1914
1915 return EFI_EXIT(EFI_SUCCESS);
1916}
1917
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001918/**
Mario Six8fac2912018-07-10 08:40:17 +02001919 * efi_get_next_monotonic_count() - get next value of the counter
1920 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001921 *
1922 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001923 *
1924 * See the Unified Extensible Firmware Interface (UEFI) specification for
1925 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001926 *
Mario Six8fac2912018-07-10 08:40:17 +02001927 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001928 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001929static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1930{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001931 static uint64_t mono;
1932
Alexander Grafc15d9212016-03-04 01:09:59 +01001933 EFI_ENTRY("%p", count);
1934 *count = mono++;
1935 return EFI_EXIT(EFI_SUCCESS);
1936}
1937
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001938/**
Mario Six8fac2912018-07-10 08:40:17 +02001939 * efi_stall() - sleep
1940 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001941 *
Mario Six8fac2912018-07-10 08:40:17 +02001942 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001943 *
Mario Six8fac2912018-07-10 08:40:17 +02001944 * See the Unified Extensible Firmware Interface (UEFI) specification for
1945 * details.
1946 *
1947 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001948 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001949static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1950{
1951 EFI_ENTRY("%ld", microseconds);
1952 udelay(microseconds);
1953 return EFI_EXIT(EFI_SUCCESS);
1954}
1955
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001956/**
Mario Six8fac2912018-07-10 08:40:17 +02001957 * efi_set_watchdog_timer() - reset the watchdog timer
1958 * @timeout: seconds before reset by watchdog
1959 * @watchdog_code: code to be logged when resetting
1960 * @data_size: size of buffer in bytes
1961 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001962 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001963 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001964 *
Mario Six8fac2912018-07-10 08:40:17 +02001965 * See the Unified Extensible Firmware Interface (UEFI) specification for
1966 * details.
1967 *
1968 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001969 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001970static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1971 uint64_t watchdog_code,
1972 unsigned long data_size,
1973 uint16_t *watchdog_data)
1974{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001975 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001976 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001977 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001978}
1979
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001980/**
Mario Six8fac2912018-07-10 08:40:17 +02001981 * efi_close_protocol() - close a protocol
1982 * @handle: handle on which the protocol shall be closed
1983 * @protocol: GUID of the protocol to close
1984 * @agent_handle: handle of the driver
1985 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001986 *
1987 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001988 *
Mario Six8fac2912018-07-10 08:40:17 +02001989 * See the Unified Extensible Firmware Interface (UEFI) specification for
1990 * details.
1991 *
1992 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001993 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001994static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001995 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001996 efi_handle_t agent_handle,
1997 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001998{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001999 struct efi_handler *handler;
2000 struct efi_open_protocol_info_item *item;
2001 struct efi_open_protocol_info_item *pos;
2002 efi_status_t r;
2003
Rob Clark238f88c2017-09-13 18:05:41 -04002004 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002005 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002006
2007 if (!agent_handle) {
2008 r = EFI_INVALID_PARAMETER;
2009 goto out;
2010 }
2011 r = efi_search_protocol(handle, protocol, &handler);
2012 if (r != EFI_SUCCESS)
2013 goto out;
2014
2015 r = EFI_NOT_FOUND;
2016 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2017 if (item->info.agent_handle == agent_handle &&
2018 item->info.controller_handle == controller_handle) {
2019 efi_delete_open_info(item);
2020 r = EFI_SUCCESS;
2021 break;
2022 }
2023 }
2024out:
2025 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002026}
2027
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002028/**
Mario Six8fac2912018-07-10 08:40:17 +02002029 * efi_open_protocol_information() - provide information about then open status
2030 * of a protocol on a handle
2031 * @handle: handle for which the information shall be retrieved
2032 * @protocol: GUID of the protocol
2033 * @entry_buffer: buffer to receive the open protocol information
2034 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002035 *
2036 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002037 *
2038 * See the Unified Extensible Firmware Interface (UEFI) specification for
2039 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002040 *
Mario Six8fac2912018-07-10 08:40:17 +02002041 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002042 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002043static efi_status_t EFIAPI efi_open_protocol_information(
2044 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002045 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002046 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002047{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002048 unsigned long buffer_size;
2049 unsigned long count;
2050 struct efi_handler *handler;
2051 struct efi_open_protocol_info_item *item;
2052 efi_status_t r;
2053
Rob Clark238f88c2017-09-13 18:05:41 -04002054 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002055 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002056
2057 /* Check parameters */
2058 if (!entry_buffer) {
2059 r = EFI_INVALID_PARAMETER;
2060 goto out;
2061 }
2062 r = efi_search_protocol(handle, protocol, &handler);
2063 if (r != EFI_SUCCESS)
2064 goto out;
2065
2066 /* Count entries */
2067 count = 0;
2068 list_for_each_entry(item, &handler->open_infos, link) {
2069 if (item->info.open_count)
2070 ++count;
2071 }
2072 *entry_count = count;
2073 *entry_buffer = NULL;
2074 if (!count) {
2075 r = EFI_SUCCESS;
2076 goto out;
2077 }
2078
2079 /* Copy entries */
2080 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2081 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2082 (void **)entry_buffer);
2083 if (r != EFI_SUCCESS)
2084 goto out;
2085 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2086 if (item->info.open_count)
2087 (*entry_buffer)[--count] = item->info;
2088 }
2089out:
2090 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002091}
2092
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002093/**
Mario Six8fac2912018-07-10 08:40:17 +02002094 * efi_protocols_per_handle() - get protocols installed on a handle
2095 * @handle: handle for which the information is retrieved
2096 * @protocol_buffer: buffer with protocol GUIDs
2097 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002098 *
2099 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002100 *
Mario Six8fac2912018-07-10 08:40:17 +02002101 * See the Unified Extensible Firmware Interface (UEFI) specification for
2102 * details.
2103 *
2104 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002105 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002106static efi_status_t EFIAPI efi_protocols_per_handle(
2107 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002108 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002109{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002110 unsigned long buffer_size;
2111 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002112 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002113 efi_status_t r;
2114
Alexander Grafc15d9212016-03-04 01:09:59 +01002115 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2116 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002117
2118 if (!handle || !protocol_buffer || !protocol_buffer_count)
2119 return EFI_EXIT(EFI_INVALID_PARAMETER);
2120
2121 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002122 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002123
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002124 efiobj = efi_search_obj(handle);
2125 if (!efiobj)
2126 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002127
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002128 /* Count protocols */
2129 list_for_each(protocol_handle, &efiobj->protocols) {
2130 ++*protocol_buffer_count;
2131 }
2132
2133 /* Copy guids */
2134 if (*protocol_buffer_count) {
2135 size_t j = 0;
2136
2137 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2138 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2139 (void **)protocol_buffer);
2140 if (r != EFI_SUCCESS)
2141 return EFI_EXIT(r);
2142 list_for_each(protocol_handle, &efiobj->protocols) {
2143 struct efi_handler *protocol;
2144
2145 protocol = list_entry(protocol_handle,
2146 struct efi_handler, link);
2147 (*protocol_buffer)[j] = (void *)protocol->guid;
2148 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002149 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002150 }
2151
2152 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002153}
2154
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002155/**
Mario Six8fac2912018-07-10 08:40:17 +02002156 * efi_locate_handle_buffer() - locate handles implementing a protocol
2157 * @search_type: selection criterion
2158 * @protocol: GUID of the protocol
2159 * @search_key: registration key
2160 * @no_handles: number of returned handles
2161 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002162 *
2163 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002164 *
Mario Six8fac2912018-07-10 08:40:17 +02002165 * See the Unified Extensible Firmware Interface (UEFI) specification for
2166 * details.
2167 *
2168 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002169 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002170static efi_status_t EFIAPI efi_locate_handle_buffer(
2171 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002172 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002173 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002174{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002175 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002176 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002177
Rob Clark238f88c2017-09-13 18:05:41 -04002178 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002179 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002180
2181 if (!no_handles || !buffer) {
2182 r = EFI_INVALID_PARAMETER;
2183 goto out;
2184 }
2185 *no_handles = 0;
2186 *buffer = NULL;
2187 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2188 *buffer);
2189 if (r != EFI_BUFFER_TOO_SMALL)
2190 goto out;
2191 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2192 (void **)buffer);
2193 if (r != EFI_SUCCESS)
2194 goto out;
2195 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2196 *buffer);
2197 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002198 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002199out:
2200 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002201}
2202
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002203/**
Mario Six8fac2912018-07-10 08:40:17 +02002204 * efi_locate_protocol() - find an interface implementing a protocol
2205 * @protocol: GUID of the protocol
2206 * @registration: registration key passed to the notification function
2207 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002208 *
2209 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002210 *
2211 * See the Unified Extensible Firmware Interface (UEFI) specification for
2212 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002213 *
Mario Six8fac2912018-07-10 08:40:17 +02002214 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002215 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002216static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002217 void *registration,
2218 void **protocol_interface)
2219{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002220 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002221 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002222
Rob Clark238f88c2017-09-13 18:05:41 -04002223 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002224
2225 if (!protocol || !protocol_interface)
2226 return EFI_EXIT(EFI_INVALID_PARAMETER);
2227
2228 list_for_each(lhandle, &efi_obj_list) {
2229 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002230 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002231
2232 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002233
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002234 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2235 if (ret == EFI_SUCCESS) {
2236 *protocol_interface = handler->protocol_interface;
2237 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002238 }
2239 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002240 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002241
2242 return EFI_EXIT(EFI_NOT_FOUND);
2243}
2244
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002245/**
Mario Six8fac2912018-07-10 08:40:17 +02002246 * efi_locate_device_path() - Get the device path and handle of an device
2247 * implementing a protocol
2248 * @protocol: GUID of the protocol
2249 * @device_path: device path
2250 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002251 *
2252 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002253 *
Mario Six8fac2912018-07-10 08:40:17 +02002254 * See the Unified Extensible Firmware Interface (UEFI) specification for
2255 * details.
2256 *
2257 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002258 */
2259static efi_status_t EFIAPI efi_locate_device_path(
2260 const efi_guid_t *protocol,
2261 struct efi_device_path **device_path,
2262 efi_handle_t *device)
2263{
2264 struct efi_device_path *dp;
2265 size_t i;
2266 struct efi_handler *handler;
2267 efi_handle_t *handles;
2268 size_t len, len_dp;
2269 size_t len_best = 0;
2270 efi_uintn_t no_handles;
2271 u8 *remainder;
2272 efi_status_t ret;
2273
2274 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2275
2276 if (!protocol || !device_path || !*device_path || !device) {
2277 ret = EFI_INVALID_PARAMETER;
2278 goto out;
2279 }
2280
2281 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002282 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002283
2284 /* Get all handles implementing the protocol */
2285 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2286 &no_handles, &handles));
2287 if (ret != EFI_SUCCESS)
2288 goto out;
2289
2290 for (i = 0; i < no_handles; ++i) {
2291 /* Find the device path protocol */
2292 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2293 &handler);
2294 if (ret != EFI_SUCCESS)
2295 continue;
2296 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002297 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002298 /*
2299 * This handle can only be a better fit
2300 * if its device path length is longer than the best fit and
2301 * if its device path length is shorter of equal the searched
2302 * device path.
2303 */
2304 if (len_dp <= len_best || len_dp > len)
2305 continue;
2306 /* Check if dp is a subpath of device_path */
2307 if (memcmp(*device_path, dp, len_dp))
2308 continue;
2309 *device = handles[i];
2310 len_best = len_dp;
2311 }
2312 if (len_best) {
2313 remainder = (u8 *)*device_path + len_best;
2314 *device_path = (struct efi_device_path *)remainder;
2315 ret = EFI_SUCCESS;
2316 } else {
2317 ret = EFI_NOT_FOUND;
2318 }
2319out:
2320 return EFI_EXIT(ret);
2321}
2322
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002323/**
Mario Six8fac2912018-07-10 08:40:17 +02002324 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2325 * interfaces
2326 * @handle: handle on which the protocol interfaces shall be installed
2327 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2328 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002329 *
2330 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002331 *
2332 * See the Unified Extensible Firmware Interface (UEFI) specification for
2333 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002334 *
Mario Six8fac2912018-07-10 08:40:17 +02002335 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002336 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002337static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2338 void **handle, ...)
2339{
2340 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002341
Alexander Graf404a7c82018-06-18 17:23:05 +02002342 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002343 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002344 void *protocol_interface;
2345 efi_status_t r = EFI_SUCCESS;
2346 int i = 0;
2347
2348 if (!handle)
2349 return EFI_EXIT(EFI_INVALID_PARAMETER);
2350
Alexander Graf404a7c82018-06-18 17:23:05 +02002351 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002352 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002353 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002354 if (!protocol)
2355 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002356 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002357 r = EFI_CALL(efi_install_protocol_interface(
2358 handle, protocol,
2359 EFI_NATIVE_INTERFACE,
2360 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002361 if (r != EFI_SUCCESS)
2362 break;
2363 i++;
2364 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002365 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002366 if (r == EFI_SUCCESS)
2367 return EFI_EXIT(r);
2368
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002369 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002370 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002371 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002372 protocol = efi_va_arg(argptr, efi_guid_t*);
2373 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002374 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2375 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002376 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002377 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002378
2379 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002380}
2381
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002382/**
Mario Six8fac2912018-07-10 08:40:17 +02002383 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2384 * interfaces
2385 * @handle: handle from which the protocol interfaces shall be removed
2386 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2387 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002388 *
2389 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002390 *
Mario Six8fac2912018-07-10 08:40:17 +02002391 * See the Unified Extensible Firmware Interface (UEFI) specification for
2392 * details.
2393 *
2394 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002395 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002396static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2397 void *handle, ...)
2398{
2399 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002400
Alexander Graf404a7c82018-06-18 17:23:05 +02002401 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002402 const efi_guid_t *protocol;
2403 void *protocol_interface;
2404 efi_status_t r = EFI_SUCCESS;
2405 size_t i = 0;
2406
2407 if (!handle)
2408 return EFI_EXIT(EFI_INVALID_PARAMETER);
2409
Alexander Graf404a7c82018-06-18 17:23:05 +02002410 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002411 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002412 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002413 if (!protocol)
2414 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002415 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002416 r = EFI_CALL(efi_uninstall_protocol_interface(
2417 handle, protocol,
2418 protocol_interface));
2419 if (r != EFI_SUCCESS)
2420 break;
2421 i++;
2422 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002423 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002424 if (r == EFI_SUCCESS)
2425 return EFI_EXIT(r);
2426
2427 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002428 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002429 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002430 protocol = efi_va_arg(argptr, efi_guid_t*);
2431 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002432 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2433 EFI_NATIVE_INTERFACE,
2434 protocol_interface));
2435 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002436 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002437
2438 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002439}
2440
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002441/**
Mario Six8fac2912018-07-10 08:40:17 +02002442 * efi_calculate_crc32() - calculate cyclic redundancy code
2443 * @data: buffer with data
2444 * @data_size: size of buffer in bytes
2445 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002446 *
2447 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002448 *
2449 * See the Unified Extensible Firmware Interface (UEFI) specification for
2450 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002451 *
Mario Six8fac2912018-07-10 08:40:17 +02002452 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002453 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002454static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2455 efi_uintn_t data_size,
2456 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002457{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002458 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002459 *crc32_p = crc32(0, data, data_size);
2460 return EFI_EXIT(EFI_SUCCESS);
2461}
2462
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002463/**
Mario Six8fac2912018-07-10 08:40:17 +02002464 * efi_copy_mem() - copy memory
2465 * @destination: destination of the copy operation
2466 * @source: source of the copy operation
2467 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002468 *
2469 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002470 *
Mario Six8fac2912018-07-10 08:40:17 +02002471 * See the Unified Extensible Firmware Interface (UEFI) specification for
2472 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002473 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002474static void EFIAPI efi_copy_mem(void *destination, const void *source,
2475 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002476{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002477 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002478 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002479 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002480}
2481
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002482/**
Mario Six8fac2912018-07-10 08:40:17 +02002483 * efi_set_mem() - Fill memory with a byte value.
2484 * @buffer: buffer to fill
2485 * @size: size of buffer in bytes
2486 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002487 *
2488 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002489 *
Mario Six8fac2912018-07-10 08:40:17 +02002490 * See the Unified Extensible Firmware Interface (UEFI) specification for
2491 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002492 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002493static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002494{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002495 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002496 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002497 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002498}
2499
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002500/**
Mario Six8fac2912018-07-10 08:40:17 +02002501 * efi_protocol_open() - open protocol interface on a handle
2502 * @handler: handler of a protocol
2503 * @protocol_interface: interface implementing the protocol
2504 * @agent_handle: handle of the driver
2505 * @controller_handle: handle of the controller
2506 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002507 *
Mario Six8fac2912018-07-10 08:40:17 +02002508 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002509 */
2510static efi_status_t efi_protocol_open(
2511 struct efi_handler *handler,
2512 void **protocol_interface, void *agent_handle,
2513 void *controller_handle, uint32_t attributes)
2514{
2515 struct efi_open_protocol_info_item *item;
2516 struct efi_open_protocol_info_entry *match = NULL;
2517 bool opened_by_driver = false;
2518 bool opened_exclusive = false;
2519
2520 /* If there is no agent, only return the interface */
2521 if (!agent_handle)
2522 goto out;
2523
2524 /* For TEST_PROTOCOL ignore interface attribute */
2525 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2526 *protocol_interface = NULL;
2527
2528 /*
2529 * Check if the protocol is already opened by a driver with the same
2530 * attributes or opened exclusively
2531 */
2532 list_for_each_entry(item, &handler->open_infos, link) {
2533 if (item->info.agent_handle == agent_handle) {
2534 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2535 (item->info.attributes == attributes))
2536 return EFI_ALREADY_STARTED;
2537 }
2538 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2539 opened_exclusive = true;
2540 }
2541
2542 /* Only one controller can open the protocol exclusively */
2543 if (opened_exclusive && attributes &
2544 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2545 return EFI_ACCESS_DENIED;
2546
2547 /* Prepare exclusive opening */
2548 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2549 /* Try to disconnect controllers */
2550 list_for_each_entry(item, &handler->open_infos, link) {
2551 if (item->info.attributes ==
2552 EFI_OPEN_PROTOCOL_BY_DRIVER)
2553 EFI_CALL(efi_disconnect_controller(
2554 item->info.controller_handle,
2555 item->info.agent_handle,
2556 NULL));
2557 }
2558 opened_by_driver = false;
2559 /* Check if all controllers are disconnected */
2560 list_for_each_entry(item, &handler->open_infos, link) {
2561 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2562 opened_by_driver = true;
2563 }
2564 /* Only one controller can be conncected */
2565 if (opened_by_driver)
2566 return EFI_ACCESS_DENIED;
2567 }
2568
2569 /* Find existing entry */
2570 list_for_each_entry(item, &handler->open_infos, link) {
2571 if (item->info.agent_handle == agent_handle &&
2572 item->info.controller_handle == controller_handle)
2573 match = &item->info;
2574 }
2575 /* None found, create one */
2576 if (!match) {
2577 match = efi_create_open_info(handler);
2578 if (!match)
2579 return EFI_OUT_OF_RESOURCES;
2580 }
2581
2582 match->agent_handle = agent_handle;
2583 match->controller_handle = controller_handle;
2584 match->attributes = attributes;
2585 match->open_count++;
2586
2587out:
2588 /* For TEST_PROTOCOL ignore interface attribute. */
2589 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2590 *protocol_interface = handler->protocol_interface;
2591
2592 return EFI_SUCCESS;
2593}
2594
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002595/**
Mario Six8fac2912018-07-10 08:40:17 +02002596 * efi_open_protocol() - open protocol interface on a handle
2597 * @handle: handle on which the protocol shall be opened
2598 * @protocol: GUID of the protocol
2599 * @protocol_interface: interface implementing the protocol
2600 * @agent_handle: handle of the driver
2601 * @controller_handle: handle of the controller
2602 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002603 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002604 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002605 *
Mario Six8fac2912018-07-10 08:40:17 +02002606 * See the Unified Extensible Firmware Interface (UEFI) specification for
2607 * details.
2608 *
2609 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002610 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002611static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002612 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002613 void **protocol_interface, void *agent_handle,
2614 void *controller_handle, uint32_t attributes)
2615{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002616 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002617 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002618
Rob Clark238f88c2017-09-13 18:05:41 -04002619 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002620 protocol_interface, agent_handle, controller_handle,
2621 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002622
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002623 if (!handle || !protocol ||
2624 (!protocol_interface && attributes !=
2625 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002626 goto out;
2627 }
2628
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002629 switch (attributes) {
2630 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2631 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2632 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2633 break;
2634 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2635 if (controller_handle == handle)
2636 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002637 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002638 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2639 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002640 /* Check that the controller handle is valid */
2641 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002642 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002643 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002644 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002645 /* Check that the agent handle is valid */
2646 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002647 goto out;
2648 break;
2649 default:
2650 goto out;
2651 }
2652
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002653 r = efi_search_protocol(handle, protocol, &handler);
2654 if (r != EFI_SUCCESS)
2655 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002656
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002657 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2658 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002659out:
2660 return EFI_EXIT(r);
2661}
2662
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002663/**
Mario Six8fac2912018-07-10 08:40:17 +02002664 * efi_handle_protocol() - get interface of a protocol on a handle
2665 * @handle: handle on which the protocol shall be opened
2666 * @protocol: GUID of the protocol
2667 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002668 *
2669 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002670 *
2671 * See the Unified Extensible Firmware Interface (UEFI) specification for
2672 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002673 *
Mario Six8fac2912018-07-10 08:40:17 +02002674 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002675 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002676static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002677 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002678 void **protocol_interface)
2679{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002680 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2681 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002682}
2683
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002684/**
Mario Six8fac2912018-07-10 08:40:17 +02002685 * efi_bind_controller() - bind a single driver to a controller
2686 * @controller_handle: controller handle
2687 * @driver_image_handle: driver handle
2688 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002689 *
Mario Six8fac2912018-07-10 08:40:17 +02002690 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002691 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002692static efi_status_t efi_bind_controller(
2693 efi_handle_t controller_handle,
2694 efi_handle_t driver_image_handle,
2695 struct efi_device_path *remain_device_path)
2696{
2697 struct efi_driver_binding_protocol *binding_protocol;
2698 efi_status_t r;
2699
2700 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2701 &efi_guid_driver_binding_protocol,
2702 (void **)&binding_protocol,
2703 driver_image_handle, NULL,
2704 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2705 if (r != EFI_SUCCESS)
2706 return r;
2707 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2708 controller_handle,
2709 remain_device_path));
2710 if (r == EFI_SUCCESS)
2711 r = EFI_CALL(binding_protocol->start(binding_protocol,
2712 controller_handle,
2713 remain_device_path));
2714 EFI_CALL(efi_close_protocol(driver_image_handle,
2715 &efi_guid_driver_binding_protocol,
2716 driver_image_handle, NULL));
2717 return r;
2718}
2719
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002720/**
Mario Six8fac2912018-07-10 08:40:17 +02002721 * efi_connect_single_controller() - connect a single driver to a controller
2722 * @controller_handle: controller
2723 * @driver_image_handle: driver
2724 * @remain_device_path: remainting path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002725 *
Mario Six8fac2912018-07-10 08:40:17 +02002726 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002727 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002728static efi_status_t efi_connect_single_controller(
2729 efi_handle_t controller_handle,
2730 efi_handle_t *driver_image_handle,
2731 struct efi_device_path *remain_device_path)
2732{
2733 efi_handle_t *buffer;
2734 size_t count;
2735 size_t i;
2736 efi_status_t r;
2737 size_t connected = 0;
2738
2739 /* Get buffer with all handles with driver binding protocol */
2740 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2741 &efi_guid_driver_binding_protocol,
2742 NULL, &count, &buffer));
2743 if (r != EFI_SUCCESS)
2744 return r;
2745
2746 /* Context Override */
2747 if (driver_image_handle) {
2748 for (; *driver_image_handle; ++driver_image_handle) {
2749 for (i = 0; i < count; ++i) {
2750 if (buffer[i] == *driver_image_handle) {
2751 buffer[i] = NULL;
2752 r = efi_bind_controller(
2753 controller_handle,
2754 *driver_image_handle,
2755 remain_device_path);
2756 /*
2757 * For drivers that do not support the
2758 * controller or are already connected
2759 * we receive an error code here.
2760 */
2761 if (r == EFI_SUCCESS)
2762 ++connected;
2763 }
2764 }
2765 }
2766 }
2767
2768 /*
2769 * TODO: Some overrides are not yet implemented:
2770 * - Platform Driver Override
2771 * - Driver Family Override Search
2772 * - Bus Specific Driver Override
2773 */
2774
2775 /* Driver Binding Search */
2776 for (i = 0; i < count; ++i) {
2777 if (buffer[i]) {
2778 r = efi_bind_controller(controller_handle,
2779 buffer[i],
2780 remain_device_path);
2781 if (r == EFI_SUCCESS)
2782 ++connected;
2783 }
2784 }
2785
2786 efi_free_pool(buffer);
2787 if (!connected)
2788 return EFI_NOT_FOUND;
2789 return EFI_SUCCESS;
2790}
2791
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002792/**
Mario Six8fac2912018-07-10 08:40:17 +02002793 * efi_connect_controller() - connect a controller to a driver
2794 * @controller_handle: handle of the controller
2795 * @driver_image_handle: handle of the driver
2796 * @remain_device_path: device path of a child controller
2797 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002798 *
2799 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002800 *
2801 * See the Unified Extensible Firmware Interface (UEFI) specification for
2802 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002803 *
2804 * First all driver binding protocol handles are tried for binding drivers.
2805 * Afterwards all handles that have openened a protocol of the controller
2806 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2807 *
Mario Six8fac2912018-07-10 08:40:17 +02002808 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002809 */
2810static efi_status_t EFIAPI efi_connect_controller(
2811 efi_handle_t controller_handle,
2812 efi_handle_t *driver_image_handle,
2813 struct efi_device_path *remain_device_path,
2814 bool recursive)
2815{
2816 efi_status_t r;
2817 efi_status_t ret = EFI_NOT_FOUND;
2818 struct efi_object *efiobj;
2819
2820 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2821 remain_device_path, recursive);
2822
2823 efiobj = efi_search_obj(controller_handle);
2824 if (!efiobj) {
2825 ret = EFI_INVALID_PARAMETER;
2826 goto out;
2827 }
2828
2829 r = efi_connect_single_controller(controller_handle,
2830 driver_image_handle,
2831 remain_device_path);
2832 if (r == EFI_SUCCESS)
2833 ret = EFI_SUCCESS;
2834 if (recursive) {
2835 struct efi_handler *handler;
2836 struct efi_open_protocol_info_item *item;
2837
2838 list_for_each_entry(handler, &efiobj->protocols, link) {
2839 list_for_each_entry(item, &handler->open_infos, link) {
2840 if (item->info.attributes &
2841 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2842 r = EFI_CALL(efi_connect_controller(
2843 item->info.controller_handle,
2844 driver_image_handle,
2845 remain_device_path,
2846 recursive));
2847 if (r == EFI_SUCCESS)
2848 ret = EFI_SUCCESS;
2849 }
2850 }
2851 }
2852 }
2853 /* Check for child controller specified by end node */
2854 if (ret != EFI_SUCCESS && remain_device_path &&
2855 remain_device_path->type == DEVICE_PATH_TYPE_END)
2856 ret = EFI_SUCCESS;
2857out:
2858 return EFI_EXIT(ret);
2859}
2860
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002861/**
Mario Six8fac2912018-07-10 08:40:17 +02002862 * efi_reinstall_protocol_interface() - reinstall protocol interface
2863 * @handle: handle on which the protocol shall be reinstalled
2864 * @protocol: GUID of the protocol to be installed
2865 * @old_interface: interface to be removed
2866 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002867 *
2868 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002869 *
2870 * See the Unified Extensible Firmware Interface (UEFI) specification for
2871 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002872 *
2873 * The old interface is uninstalled. The new interface is installed.
2874 * Drivers are connected.
2875 *
Mario Six8fac2912018-07-10 08:40:17 +02002876 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002877 */
2878static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2879 efi_handle_t handle, const efi_guid_t *protocol,
2880 void *old_interface, void *new_interface)
2881{
2882 efi_status_t ret;
2883
2884 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2885 new_interface);
2886 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2887 old_interface));
2888 if (ret != EFI_SUCCESS)
2889 goto out;
2890 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2891 EFI_NATIVE_INTERFACE,
2892 new_interface));
2893 if (ret != EFI_SUCCESS)
2894 goto out;
2895 /*
2896 * The returned status code has to be ignored.
2897 * Do not create an error if no suitable driver for the handle exists.
2898 */
2899 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2900out:
2901 return EFI_EXIT(ret);
2902}
2903
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002904/**
Mario Six8fac2912018-07-10 08:40:17 +02002905 * efi_get_child_controllers() - get all child controllers associated to a driver
2906 * @efiobj: handle of the controller
2907 * @driver_handle: handle of the driver
2908 * @number_of_children: number of child controllers
2909 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002910 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002911 * The allocated buffer has to be freed with free().
2912 *
Mario Six8fac2912018-07-10 08:40:17 +02002913 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002914 */
2915static efi_status_t efi_get_child_controllers(
2916 struct efi_object *efiobj,
2917 efi_handle_t driver_handle,
2918 efi_uintn_t *number_of_children,
2919 efi_handle_t **child_handle_buffer)
2920{
2921 struct efi_handler *handler;
2922 struct efi_open_protocol_info_item *item;
2923 efi_uintn_t count = 0, i;
2924 bool duplicate;
2925
2926 /* Count all child controller associations */
2927 list_for_each_entry(handler, &efiobj->protocols, link) {
2928 list_for_each_entry(item, &handler->open_infos, link) {
2929 if (item->info.agent_handle == driver_handle &&
2930 item->info.attributes &
2931 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2932 ++count;
2933 }
2934 }
2935 /*
2936 * Create buffer. In case of duplicate child controller assignments
2937 * the buffer will be too large. But that does not harm.
2938 */
2939 *number_of_children = 0;
2940 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2941 if (!*child_handle_buffer)
2942 return EFI_OUT_OF_RESOURCES;
2943 /* Copy unique child handles */
2944 list_for_each_entry(handler, &efiobj->protocols, link) {
2945 list_for_each_entry(item, &handler->open_infos, link) {
2946 if (item->info.agent_handle == driver_handle &&
2947 item->info.attributes &
2948 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2949 /* Check this is a new child controller */
2950 duplicate = false;
2951 for (i = 0; i < *number_of_children; ++i) {
2952 if ((*child_handle_buffer)[i] ==
2953 item->info.controller_handle)
2954 duplicate = true;
2955 }
2956 /* Copy handle to buffer */
2957 if (!duplicate) {
2958 i = (*number_of_children)++;
2959 (*child_handle_buffer)[i] =
2960 item->info.controller_handle;
2961 }
2962 }
2963 }
2964 }
2965 return EFI_SUCCESS;
2966}
2967
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002968/**
Mario Six8fac2912018-07-10 08:40:17 +02002969 * efi_disconnect_controller() - disconnect a controller from a driver
2970 * @controller_handle: handle of the controller
2971 * @driver_image_handle: handle of the driver
2972 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002973 *
2974 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002975 *
2976 * See the Unified Extensible Firmware Interface (UEFI) specification for
2977 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002978 *
Mario Six8fac2912018-07-10 08:40:17 +02002979 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002980 */
2981static efi_status_t EFIAPI efi_disconnect_controller(
2982 efi_handle_t controller_handle,
2983 efi_handle_t driver_image_handle,
2984 efi_handle_t child_handle)
2985{
2986 struct efi_driver_binding_protocol *binding_protocol;
2987 efi_handle_t *child_handle_buffer = NULL;
2988 size_t number_of_children = 0;
2989 efi_status_t r;
2990 size_t stop_count = 0;
2991 struct efi_object *efiobj;
2992
2993 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2994 child_handle);
2995
2996 efiobj = efi_search_obj(controller_handle);
2997 if (!efiobj) {
2998 r = EFI_INVALID_PARAMETER;
2999 goto out;
3000 }
3001
3002 if (child_handle && !efi_search_obj(child_handle)) {
3003 r = EFI_INVALID_PARAMETER;
3004 goto out;
3005 }
3006
3007 /* If no driver handle is supplied, disconnect all drivers */
3008 if (!driver_image_handle) {
3009 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3010 goto out;
3011 }
3012
3013 /* Create list of child handles */
3014 if (child_handle) {
3015 number_of_children = 1;
3016 child_handle_buffer = &child_handle;
3017 } else {
3018 efi_get_child_controllers(efiobj,
3019 driver_image_handle,
3020 &number_of_children,
3021 &child_handle_buffer);
3022 }
3023
3024 /* Get the driver binding protocol */
3025 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3026 &efi_guid_driver_binding_protocol,
3027 (void **)&binding_protocol,
3028 driver_image_handle, NULL,
3029 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3030 if (r != EFI_SUCCESS)
3031 goto out;
3032 /* Remove the children */
3033 if (number_of_children) {
3034 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3035 controller_handle,
3036 number_of_children,
3037 child_handle_buffer));
3038 if (r == EFI_SUCCESS)
3039 ++stop_count;
3040 }
3041 /* Remove the driver */
3042 if (!child_handle)
3043 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3044 controller_handle,
3045 0, NULL));
3046 if (r == EFI_SUCCESS)
3047 ++stop_count;
3048 EFI_CALL(efi_close_protocol(driver_image_handle,
3049 &efi_guid_driver_binding_protocol,
3050 driver_image_handle, NULL));
3051
3052 if (stop_count)
3053 r = EFI_SUCCESS;
3054 else
3055 r = EFI_NOT_FOUND;
3056out:
3057 if (!child_handle)
3058 free(child_handle_buffer);
3059 return EFI_EXIT(r);
3060}
3061
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003062static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003063 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003064 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3065 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003066 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003067 },
3068 .raise_tpl = efi_raise_tpl,
3069 .restore_tpl = efi_restore_tpl,
3070 .allocate_pages = efi_allocate_pages_ext,
3071 .free_pages = efi_free_pages_ext,
3072 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003073 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003074 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003075 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003076 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003077 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003078 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003079 .close_event = efi_close_event,
3080 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003081 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003082 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003083 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003084 .handle_protocol = efi_handle_protocol,
3085 .reserved = NULL,
3086 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003087 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003088 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003089 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003090 .load_image = efi_load_image,
3091 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003092 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003093 .unload_image = efi_unload_image,
3094 .exit_boot_services = efi_exit_boot_services,
3095 .get_next_monotonic_count = efi_get_next_monotonic_count,
3096 .stall = efi_stall,
3097 .set_watchdog_timer = efi_set_watchdog_timer,
3098 .connect_controller = efi_connect_controller,
3099 .disconnect_controller = efi_disconnect_controller,
3100 .open_protocol = efi_open_protocol,
3101 .close_protocol = efi_close_protocol,
3102 .open_protocol_information = efi_open_protocol_information,
3103 .protocols_per_handle = efi_protocols_per_handle,
3104 .locate_handle_buffer = efi_locate_handle_buffer,
3105 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003106 .install_multiple_protocol_interfaces =
3107 efi_install_multiple_protocol_interfaces,
3108 .uninstall_multiple_protocol_interfaces =
3109 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003110 .calculate_crc32 = efi_calculate_crc32,
3111 .copy_mem = efi_copy_mem,
3112 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003113 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003114};
3115
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003116static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003117
Alexander Graf393dd912016-10-14 13:45:30 +02003118struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003119 .hdr = {
3120 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003121 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003122 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003123 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003124 .fw_vendor = firmware_vendor,
3125 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003126 .con_in = (void *)&efi_con_in,
3127 .con_out = (void *)&efi_con_out,
3128 .std_err = (void *)&efi_con_out,
3129 .runtime = (void *)&efi_runtime_services,
3130 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003131 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003132 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003133};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003134
3135/**
3136 * efi_initialize_system_table() - Initialize system table
3137 *
3138 * Return Value: status code
3139 */
3140efi_status_t efi_initialize_system_table(void)
3141{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003142 efi_status_t ret;
3143
3144 /* Allocate configuration table array */
3145 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3146 EFI_MAX_CONFIGURATION_TABLES *
3147 sizeof(struct efi_configuration_table),
3148 (void **)&systab.tables);
3149
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003150 /* Set crc32 field in table headers */
3151 efi_update_table_header_crc32(&systab.hdr);
3152 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3153 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003154
3155 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003156}