blob: 50d311548e2d4cc09d56102bd95884a29206b6d6 [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
38/*
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
42 *
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 */
Alexander Graf393dd912016-10-14 13:45:30 +020046static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafc15d9212016-03-04 01:09:59 +010047
Simon Glasscdfe6962016-09-25 15:27:35 -060048#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010049/*
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
54 */
55static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060056#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010057
Rob Clark86789d52017-07-27 08:04:18 -040058static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040059static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010060/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010062/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040065
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010066/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010082static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010086
Rob Clark86789d52017-07-27 08:04:18 -040087/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
91#ifdef CONFIG_ARM
92 assert(efi_gd);
93 app_gd = gd;
94 gd = efi_gd;
95#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
103#ifdef CONFIG_ARM
104 gd = app_gd;
105#endif
106 return ret;
107}
108
Alexander Grafc15d9212016-03-04 01:09:59 +0100109/* Called from do_bootefi_exec() */
110void efi_save_gd(void)
111{
Simon Glasscdfe6962016-09-25 15:27:35 -0600112#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100113 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100115}
116
Rob Clark86789d52017-07-27 08:04:18 -0400117/*
118 * Special case handler for error/abort that just forces things back
119 * to u-boot world so we can dump out an abort msg, without any care
120 * about returning back to UEFI world.
121 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100122void efi_restore_gd(void)
123{
Simon Glasscdfe6962016-09-25 15:27:35 -0600124#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 /* Only restore if we're already in EFI context */
126 if (!efi_gd)
127 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100128 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600129#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100130}
131
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200132/**
133 * indent_string - returns a string for indenting with two spaces per level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100134 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200135 * A maximum of ten indent levels is supported. Higher indent levels will be
136 * truncated.
137 *
138 * @level: indent level
139 * Return Value: A string for indenting with two spaces per level is
140 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400141 */
142static const char *indent_string(int level)
143{
144 const char *indent = " ";
145 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100146
Rob Clarke7896c32017-07-27 08:04:19 -0400147 level = min(max, level * 2);
148 return &indent[max - level];
149}
150
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200151const char *__efi_nesting(void)
152{
153 return indent_string(nesting_level);
154}
155
Rob Clarke7896c32017-07-27 08:04:19 -0400156const char *__efi_nesting_inc(void)
157{
158 return indent_string(nesting_level++);
159}
160
161const char *__efi_nesting_dec(void)
162{
163 return indent_string(--nesting_level);
164}
165
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200166/**
167 * efi_queue_event - queue an EFI event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200168 *
169 * This function queues the notification function of the event for future
170 * execution.
171 *
172 * The notification function is called if the task priority level of the
173 * event is higher than the current task priority level.
174 *
175 * For the SignalEvent service see efi_signal_event_ext.
176 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200177 * @event: event to signal
178 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200179 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100180static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200181{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200182 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200183 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200184 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100185 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200186 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200189 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200190 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200191}
192
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200193/**
194 * efi_signal_event - signal an EFI event
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100195 *
196 * This function signals an event. If the event belongs to an event group
197 * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
198 * their notification function is queued.
199 *
200 * For the SignalEvent service see efi_signal_event_ext.
201 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200202 * @event: event to signal
203 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100204 */
205void efi_signal_event(struct efi_event *event, bool check_tpl)
206{
207 if (event->group) {
208 struct efi_event *evt;
209
210 /*
211 * The signaled state has to set before executing any
212 * notification function
213 */
214 list_for_each_entry(evt, &efi_events, link) {
215 if (!evt->group || guidcmp(evt->group, event->group))
216 continue;
217 if (evt->is_signaled)
218 continue;
219 evt->is_signaled = true;
220 if (evt->type & EVT_NOTIFY_SIGNAL &&
221 evt->notify_function)
222 evt->is_queued = true;
223 }
224 list_for_each_entry(evt, &efi_events, link) {
225 if (!evt->group || guidcmp(evt->group, event->group))
226 continue;
227 if (evt->is_queued)
228 efi_queue_event(evt, check_tpl);
229 }
230 } else if (!event->is_signaled) {
231 event->is_signaled = true;
232 if (event->type & EVT_NOTIFY_SIGNAL)
233 efi_queue_event(event, check_tpl);
234 }
235}
236
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200237/**
238 * efi_raise_tpl - raise the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200239 *
240 * This function implements the RaiseTpl service.
241 * See the Unified Extensible Firmware Interface (UEFI) specification
242 * for details.
243 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200244 * @new_tpl: new value of the task priority level
245 * Return Value: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200246 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100247static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100248{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100249 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200250
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200251 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200252
253 if (new_tpl < efi_tpl)
254 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
255 efi_tpl = new_tpl;
256 if (efi_tpl > TPL_HIGH_LEVEL)
257 efi_tpl = TPL_HIGH_LEVEL;
258
259 EFI_EXIT(EFI_SUCCESS);
260 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100261}
262
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200263/**
264 * efi_restore_tpl - lower the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200265 *
266 * This function implements the RestoreTpl service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
268 * for details.
269 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200270 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200271 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100272static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100273{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200274 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200275
276 if (old_tpl > efi_tpl)
277 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
278 efi_tpl = old_tpl;
279 if (efi_tpl > TPL_HIGH_LEVEL)
280 efi_tpl = TPL_HIGH_LEVEL;
281
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100282 /*
283 * Lowering the TPL may have made queued events eligible for execution.
284 */
285 efi_timer_check();
286
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200287 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100288}
289
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200290/**
291 * efi_allocate_pages_ext - allocate memory pages
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200292 *
293 * This function implements the AllocatePages service.
294 * See the Unified Extensible Firmware Interface (UEFI) specification
295 * for details.
296 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200297 * @type: type of allocation to be performed
298 * @memory_type: usage type of the allocated memory
299 * @pages: number of pages to be allocated
300 * @memory: allocated memory
301 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200302 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900303static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100304 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900305 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100306{
307 efi_status_t r;
308
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100309 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100310 r = efi_allocate_pages(type, memory_type, pages, memory);
311 return EFI_EXIT(r);
312}
313
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200314/**
315 * efi_free_pages_ext - Free memory pages.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200316 *
317 * This function implements the FreePages service.
318 * See the Unified Extensible Firmware Interface (UEFI) specification
319 * for details.
320 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200321 * @memory: start of the memory area to be freed
322 * @pages: number of pages to be freed
323 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900325static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100326 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100327{
328 efi_status_t r;
329
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100330 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100331 r = efi_free_pages(memory, pages);
332 return EFI_EXIT(r);
333}
334
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200335/**
336 * efi_get_memory_map_ext - get map describing memory usage
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200337 *
338 * This function implements the GetMemoryMap service.
339 * See the Unified Extensible Firmware Interface (UEFI) specification
340 * for details.
341 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200342 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200343 * on exit the size of the copied memory map
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200344 * @memory_map: buffer to which the memory map is written
345 * @map_key: key for the memory map
346 * @descriptor_size: size of an individual memory descriptor
347 * @descriptor_version: version number of the memory descriptor structure
348 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200349 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900350static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100351 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900352 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100353 efi_uintn_t *map_key,
354 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900355 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100356{
357 efi_status_t r;
358
359 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
360 map_key, descriptor_size, descriptor_version);
361 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
362 descriptor_size, descriptor_version);
363 return EFI_EXIT(r);
364}
365
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200366/**
367 * efi_allocate_pool_ext - allocate memory from pool
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200368 *
369 * This function implements the AllocatePool service.
370 * See the Unified Extensible Firmware Interface (UEFI) specification
371 * for details.
372 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200373 * @pool_type: type of the pool from which memory is to be allocated
374 * @size: number of bytes to be allocated
375 * @buffer: allocated memory
376 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200377 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200378static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100379 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200380 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100381{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100382 efi_status_t r;
383
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100384 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200385 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100386 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100387}
388
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200389/**
390 * efi_free_pool_ext - free memory from pool
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 *
392 * This function implements the FreePool service.
393 * See the Unified Extensible Firmware Interface (UEFI) specification
394 * for details.
395 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200396 * @buffer: start of memory to be freed
397 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200398 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200399static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100400{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100401 efi_status_t r;
402
403 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200404 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100405 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100406}
407
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200408/**
409 * efi_add_handle - add a new object to the object list
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100410 *
411 * The protocols list is initialized.
412 * The object handle is set.
413 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200414 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100415 */
416void efi_add_handle(struct efi_object *obj)
417{
418 if (!obj)
419 return;
420 INIT_LIST_HEAD(&obj->protocols);
421 obj->handle = obj;
422 list_add_tail(&obj->link, &efi_obj_list);
423}
424
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200425/**
426 * efi_create_handle - create handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200427 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200428 * @handle: new handle
429 * Return Value: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200430 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100431efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200432{
433 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200434
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200435 obj = calloc(1, sizeof(struct efi_object));
436 if (!obj)
437 return EFI_OUT_OF_RESOURCES;
438
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100439 efi_add_handle(obj);
440 *handle = obj->handle;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200441
442 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200443}
444
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200445/**
446 * efi_search_protocol - find a protocol on a handle.
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100447 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200448 * @handle: handle
449 * @protocol_guid: GUID of the protocol
450 * @handler: reference to the protocol
451 * Return Value: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100452 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100453efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100454 const efi_guid_t *protocol_guid,
455 struct efi_handler **handler)
456{
457 struct efi_object *efiobj;
458 struct list_head *lhandle;
459
460 if (!handle || !protocol_guid)
461 return EFI_INVALID_PARAMETER;
462 efiobj = efi_search_obj(handle);
463 if (!efiobj)
464 return EFI_INVALID_PARAMETER;
465 list_for_each(lhandle, &efiobj->protocols) {
466 struct efi_handler *protocol;
467
468 protocol = list_entry(lhandle, struct efi_handler, link);
469 if (!guidcmp(protocol->guid, protocol_guid)) {
470 if (handler)
471 *handler = protocol;
472 return EFI_SUCCESS;
473 }
474 }
475 return EFI_NOT_FOUND;
476}
477
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200478/**
479 * efi_remove_protocol - delete protocol from a handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100480 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200481 * @handle: handle from which the protocol shall be deleted
482 * @protocol: GUID of the protocol to be deleted
483 * @protocol_interface: interface of the protocol implementation
484 * Return Value: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100485 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100486efi_status_t efi_remove_protocol(const efi_handle_t handle,
487 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100488 void *protocol_interface)
489{
490 struct efi_handler *handler;
491 efi_status_t ret;
492
493 ret = efi_search_protocol(handle, protocol, &handler);
494 if (ret != EFI_SUCCESS)
495 return ret;
496 if (guidcmp(handler->guid, protocol))
497 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200498 if (handler->protocol_interface != protocol_interface)
499 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100500 list_del(&handler->link);
501 free(handler);
502 return EFI_SUCCESS;
503}
504
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200505/**
506 * efi_remove_all_protocols - delete all protocols from a handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100507 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200508 * @handle: handle from which the protocols shall be deleted
509 * Return Value: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100510 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100511efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100512{
513 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100514 struct efi_handler *protocol;
515 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100516
517 efiobj = efi_search_obj(handle);
518 if (!efiobj)
519 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100520 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100521 efi_status_t ret;
522
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100523 ret = efi_remove_protocol(handle, protocol->guid,
524 protocol->protocol_interface);
525 if (ret != EFI_SUCCESS)
526 return ret;
527 }
528 return EFI_SUCCESS;
529}
530
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200531/**
532 * efi_delete_handle - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100533 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200534 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100535 */
536void efi_delete_handle(struct efi_object *obj)
537{
538 if (!obj)
539 return;
540 efi_remove_all_protocols(obj->handle);
541 list_del(&obj->link);
542 free(obj);
543}
544
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200545/**
546 * efi_is_event - check if a pointer is a valid event
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100547 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200548 * @event: pointer to check
549 * Return Value: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100550 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100551static efi_status_t efi_is_event(const struct efi_event *event)
552{
553 const struct efi_event *evt;
554
555 if (!event)
556 return EFI_INVALID_PARAMETER;
557 list_for_each_entry(evt, &efi_events, link) {
558 if (evt == event)
559 return EFI_SUCCESS;
560 }
561 return EFI_INVALID_PARAMETER;
562}
Alexander Grafc15d9212016-03-04 01:09:59 +0100563
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200564/**
565 * efi_create_event - create an event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200566 *
567 * This function is used inside U-Boot code to create an event.
568 *
569 * For the API function implementing the CreateEvent service see
570 * efi_create_event_ext.
571 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200572 * @type: type of the event to create
573 * @notify_tpl: task priority level of the event
574 * @notify_function: notification function of the event
575 * @notify_context: pointer passed to the notification function
576 * @group: event group
577 * @event: created event
578 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200579 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100580efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200581 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200582 struct efi_event *event,
583 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100584 void *notify_context, efi_guid_t *group,
585 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100586{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100587 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200588
Jonathan Gray7758b212017-03-12 19:26:07 +1100589 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200590 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100591
592 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200593 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100594
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100595 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
Jonathan Gray7758b212017-03-12 19:26:07 +1100596 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200597 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100598
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100599 evt = calloc(1, sizeof(struct efi_event));
600 if (!evt)
601 return EFI_OUT_OF_RESOURCES;
602 evt->type = type;
603 evt->notify_tpl = notify_tpl;
604 evt->notify_function = notify_function;
605 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100606 evt->group = group;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100607 /* Disable timers on bootup */
608 evt->trigger_next = -1ULL;
609 evt->is_queued = false;
610 evt->is_signaled = false;
611 list_add_tail(&evt->link, &efi_events);
612 *event = evt;
613 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100614}
615
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200616/*
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200617 * efi_create_event_ex - create an event in a group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100618 *
619 * This function implements the CreateEventEx service.
620 * See the Unified Extensible Firmware Interface (UEFI) specification
621 * for details.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100622 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200623 * @type: type of the event to create
624 * @notify_tpl: task priority level of the event
625 * @notify_function: notification function of the event
626 * @notify_context: pointer passed to the notification function
627 * @event: created event
628 * @event_group: event group
629 * Return Value: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100630 */
631efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
632 void (EFIAPI *notify_function) (
633 struct efi_event *event,
634 void *context),
635 void *notify_context,
636 efi_guid_t *event_group,
637 struct efi_event **event)
638{
639 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
640 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100641 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100642 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100643}
644
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200645/**
646 * efi_create_event_ext - create an event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200647 *
648 * This function implements the CreateEvent service.
649 * See the Unified Extensible Firmware Interface (UEFI) specification
650 * for details.
651 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200652 * @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 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200658 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200659static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100660 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200661 void (EFIAPI *notify_function) (
662 struct efi_event *event,
663 void *context),
664 void *notify_context, struct efi_event **event)
665{
666 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
667 notify_context);
668 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100669 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200670}
671
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200672/**
673 * efi_timer_check - check if a timer event has occurred
674 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200675 * Check if a timer event has occurred or a queued notification function should
676 * be called.
677 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100678 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200679 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100680 */
681void efi_timer_check(void)
682{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100683 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100684 u64 now = timer_get_us();
685
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100686 list_for_each_entry(evt, &efi_events, link) {
687 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100688 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100689 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200690 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100691 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200692 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100693 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200694 break;
695 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100696 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200697 break;
698 default:
699 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200700 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100701 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100702 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100703 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100704 WATCHDOG_RESET();
705}
706
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200707/**
708 * efi_set_timer - set the trigger time for a timer event or stop the event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200709 *
710 * This is the function for internal usage in U-Boot. For the API function
711 * implementing the SetTimer service see efi_set_timer_ext.
712 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200713 * @event: event for which the timer is set
714 * @type: type of the timer
715 * @trigger_time: trigger period in multiples of 100ns
716 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200717 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200718efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200719 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100720{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100721 /* Check that the event is valid */
722 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
723 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100724
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200725 /*
726 * The parameter defines a multiple of 100ns.
727 * We use multiples of 1000ns. So divide by 10.
728 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200729 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100730
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100731 switch (type) {
732 case EFI_TIMER_STOP:
733 event->trigger_next = -1ULL;
734 break;
735 case EFI_TIMER_PERIODIC:
736 case EFI_TIMER_RELATIVE:
737 event->trigger_next = timer_get_us() + trigger_time;
738 break;
739 default:
740 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100741 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100742 event->trigger_type = type;
743 event->trigger_time = trigger_time;
744 event->is_signaled = false;
745 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100746}
747
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200748/**
749 * efi_set_timer_ext - Set the trigger time for a timer event or stop the event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200750 *
751 * This function implements the SetTimer service.
752 * See the Unified Extensible Firmware Interface (UEFI) specification
753 * for details.
754 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200755 * @event: event for which the timer is set
756 * @type: type of the timer
757 * @trigger_time: trigger period in multiples of 100ns
758 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200759 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200760static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
761 enum efi_timer_delay type,
762 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200763{
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100764 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200765 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
766}
767
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200768/**
769 * efi_wait_for_event - wait for events to be signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200770 *
771 * This function implements the WaitForEvent service.
772 * See the Unified Extensible Firmware Interface (UEFI) specification
773 * for details.
774 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200775 * @num_events: number of events to be waited for
776 * @event: events to be waited for
777 * @index: index of the event that was signaled
778 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200779 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100780static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200781 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100782 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100783{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100784 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100785
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100786 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100787
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200788 /* Check parameters */
789 if (!num_events || !event)
790 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200791 /* Check TPL */
792 if (efi_tpl != TPL_APPLICATION)
793 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200794 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100795 if (efi_is_event(event[i]) != EFI_SUCCESS)
796 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200797 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
798 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200799 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100800 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200801 }
802
803 /* Wait for signal */
804 for (;;) {
805 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200806 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200807 goto out;
808 }
809 /* Allow events to occur. */
810 efi_timer_check();
811 }
812
813out:
814 /*
815 * Reset the signal which is passed to the caller to allow periodic
816 * events to occur.
817 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200818 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200819 if (index)
820 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100821
822 return EFI_EXIT(EFI_SUCCESS);
823}
824
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200825/**
826 * efi_signal_event_ext - signal an EFI event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200827 *
828 * This function implements the SignalEvent service.
829 * See the Unified Extensible Firmware Interface (UEFI) specification
830 * for details.
831 *
832 * This functions sets the signaled state of the event and queues the
833 * notification function for execution.
834 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200835 * @event: event to signal
836 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200837 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200838static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100839{
840 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100841 if (efi_is_event(event) != EFI_SUCCESS)
842 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100843 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100844 return EFI_EXIT(EFI_SUCCESS);
845}
846
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200847/**
848 * efi_close_event - close an EFI event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200849 *
850 * This function implements the CloseEvent service.
851 * See the Unified Extensible Firmware Interface (UEFI) specification
852 * for details.
853 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200854 * @event: event to close
855 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200856 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200857static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100858{
859 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100860 if (efi_is_event(event) != EFI_SUCCESS)
861 return EFI_EXIT(EFI_INVALID_PARAMETER);
862 list_del(&event->link);
863 free(event);
864 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100865}
866
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200867/**
868 * efi_check_event - check if an event is signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200869 *
870 * This function implements the CheckEvent service.
871 * See the Unified Extensible Firmware Interface (UEFI) specification
872 * for details.
873 *
Heinrich Schuchardtf375f372018-02-18 11:32:02 +0100874 * If an event is not signaled yet, the notification function is queued.
875 * The signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200876 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200877 * @event: event to check
878 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200879 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200880static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100881{
882 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200883 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100884 if (efi_is_event(event) != EFI_SUCCESS ||
885 event->type & EVT_NOTIFY_SIGNAL)
886 return EFI_EXIT(EFI_INVALID_PARAMETER);
887 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100888 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100889 if (event->is_signaled) {
890 event->is_signaled = false;
891 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200892 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100893 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100894}
895
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200896/**
897 * efi_search_obj - find the internal EFI object for a handle
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200898 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200899 * @handle: handle to find
900 * Return Value: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200901 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100902struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200903{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100904 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200905
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100906 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200907 if (efiobj->handle == handle)
908 return efiobj;
909 }
910
911 return NULL;
912}
913
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200914/**
915 * efi_open_protocol_info_entry - create open protocol info entry and add it
916 * to a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100917 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200918 * @handler: handler of a protocol
919 * Return Value: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100920 */
921static struct efi_open_protocol_info_entry *efi_create_open_info(
922 struct efi_handler *handler)
923{
924 struct efi_open_protocol_info_item *item;
925
926 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
927 if (!item)
928 return NULL;
929 /* Append the item to the open protocol info list. */
930 list_add_tail(&item->link, &handler->open_infos);
931
932 return &item->info;
933}
934
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200935/**
936 * efi_delete_open_info - remove an open protocol info entry from a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100937 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200938 * @item: open protocol info entry to delete
939 * Return Value: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100940 */
941static efi_status_t efi_delete_open_info(
942 struct efi_open_protocol_info_item *item)
943{
944 list_del(&item->link);
945 free(item);
946 return EFI_SUCCESS;
947}
948
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200949/**
950 * efi_add_protocol - install new protocol on a handle
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200951 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200952 * @handle: handle on which the protocol shall be installed
953 * @protocol: GUID of the protocol to be installed
954 * @protocol_interface: interface of the protocol implementation
955 * Return Value: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200956 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100957efi_status_t efi_add_protocol(const efi_handle_t handle,
958 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200959 void *protocol_interface)
960{
961 struct efi_object *efiobj;
962 struct efi_handler *handler;
963 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200964
965 efiobj = efi_search_obj(handle);
966 if (!efiobj)
967 return EFI_INVALID_PARAMETER;
968 ret = efi_search_protocol(handle, protocol, NULL);
969 if (ret != EFI_NOT_FOUND)
970 return EFI_INVALID_PARAMETER;
971 handler = calloc(1, sizeof(struct efi_handler));
972 if (!handler)
973 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100974 handler->guid = protocol;
975 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100976 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100977 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +0100978 if (!guidcmp(&efi_guid_device_path, protocol))
979 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100980 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200981}
982
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200983/**
984 * efi_install_protocol_interface - install protocol interface
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200985 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100986 * This function implements the InstallProtocolInterface service.
987 * See the Unified Extensible Firmware Interface (UEFI) specification
988 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200989 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200990 * @handle: handle on which the protocol shall be installed
991 * @protocol: GUID of the protocol to be installed
992 * @protocol_interface_type: type of the interface to be installed,
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200993 * always EFI_NATIVE_INTERFACE
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200994 * @protocol_interface: interface of the protocol implementation
995 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200996 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100997static efi_status_t EFIAPI efi_install_protocol_interface(
998 void **handle, const efi_guid_t *protocol,
999 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001000{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001001 efi_status_t r;
1002
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001003 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1004 protocol_interface);
1005
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001006 if (!handle || !protocol ||
1007 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1008 r = EFI_INVALID_PARAMETER;
1009 goto out;
1010 }
1011
1012 /* Create new handle if requested. */
1013 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001014 r = efi_create_handle(handle);
1015 if (r != EFI_SUCCESS)
1016 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001017 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1018 *handle);
1019 } else {
1020 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1021 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001022 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001023 /* Add new protocol */
1024 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001025out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001026 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001027}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001028
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001029/**
1030 * efi_get_drivers - get all drivers associated to a controller
1031 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001032 * The allocated buffer has to be freed with free().
1033 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001034 * @efiobj: handle of the controller
1035 * @protocol: protocol guid (optional)
1036 * @number_of_drivers: number of child controllers
1037 * @driver_handle_buffer: handles of the the drivers
1038 * Return Value: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001039 */
1040static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1041 const efi_guid_t *protocol,
1042 efi_uintn_t *number_of_drivers,
1043 efi_handle_t **driver_handle_buffer)
1044{
1045 struct efi_handler *handler;
1046 struct efi_open_protocol_info_item *item;
1047 efi_uintn_t count = 0, i;
1048 bool duplicate;
1049
1050 /* Count all driver associations */
1051 list_for_each_entry(handler, &efiobj->protocols, link) {
1052 if (protocol && guidcmp(handler->guid, protocol))
1053 continue;
1054 list_for_each_entry(item, &handler->open_infos, link) {
1055 if (item->info.attributes &
1056 EFI_OPEN_PROTOCOL_BY_DRIVER)
1057 ++count;
1058 }
1059 }
1060 /*
1061 * Create buffer. In case of duplicate driver assignments the buffer
1062 * will be too large. But that does not harm.
1063 */
1064 *number_of_drivers = 0;
1065 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1066 if (!*driver_handle_buffer)
1067 return EFI_OUT_OF_RESOURCES;
1068 /* Collect unique driver handles */
1069 list_for_each_entry(handler, &efiobj->protocols, link) {
1070 if (protocol && guidcmp(handler->guid, protocol))
1071 continue;
1072 list_for_each_entry(item, &handler->open_infos, link) {
1073 if (item->info.attributes &
1074 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1075 /* Check this is a new driver */
1076 duplicate = false;
1077 for (i = 0; i < *number_of_drivers; ++i) {
1078 if ((*driver_handle_buffer)[i] ==
1079 item->info.agent_handle)
1080 duplicate = true;
1081 }
1082 /* Copy handle to buffer */
1083 if (!duplicate) {
1084 i = (*number_of_drivers)++;
1085 (*driver_handle_buffer)[i] =
1086 item->info.agent_handle;
1087 }
1088 }
1089 }
1090 }
1091 return EFI_SUCCESS;
1092}
1093
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001094/**
1095 * efi_disconnect_all_drivers - disconnect all drivers from a controller
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001096 *
1097 * This function implements the DisconnectController service.
1098 * See the Unified Extensible Firmware Interface (UEFI) specification
1099 * for details.
1100 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001101 * @efiobj: handle of the controller
1102 * @protocol: protocol guid (optional)
1103 * @child_handle: handle of the child to destroy
1104 * Return Value: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001105 */
1106static efi_status_t efi_disconnect_all_drivers(
1107 struct efi_object *efiobj,
1108 const efi_guid_t *protocol,
1109 efi_handle_t child_handle)
1110{
1111 efi_uintn_t number_of_drivers;
1112 efi_handle_t *driver_handle_buffer;
1113 efi_status_t r, ret;
1114
1115 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1116 &driver_handle_buffer);
1117 if (ret != EFI_SUCCESS)
1118 return ret;
1119
1120 ret = EFI_NOT_FOUND;
1121 while (number_of_drivers) {
1122 r = EFI_CALL(efi_disconnect_controller(
1123 efiobj->handle,
1124 driver_handle_buffer[--number_of_drivers],
1125 child_handle));
1126 if (r == EFI_SUCCESS)
1127 ret = r;
1128 }
1129 free(driver_handle_buffer);
1130 return ret;
1131}
1132
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001133/**
1134 * efi_uninstall_protocol_interface - uninstall protocol interface
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001135 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001136 * This function implements the UninstallProtocolInterface service.
1137 * See the Unified Extensible Firmware Interface (UEFI) specification
1138 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001139 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001140 * @handle: handle from which the protocol shall be removed
1141 * @protocol: GUID of the protocol to be removed
1142 * @protocol_interface: interface to be removed
1143 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001144 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001145static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001146 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001147 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001148{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001149 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001150 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001151 struct efi_open_protocol_info_item *item;
1152 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001153 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001154
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001155 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1156
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001157 /* Check handle */
1158 efiobj = efi_search_obj(handle);
1159 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001160 r = EFI_INVALID_PARAMETER;
1161 goto out;
1162 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001163 /* Find the protocol on the handle */
1164 r = efi_search_protocol(handle, protocol, &handler);
1165 if (r != EFI_SUCCESS)
1166 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001167 /* Disconnect controllers */
1168 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1169 if (!list_empty(&handler->open_infos)) {
1170 r = EFI_ACCESS_DENIED;
1171 goto out;
1172 }
1173 /* Close protocol */
1174 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1175 if (item->info.attributes ==
1176 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1177 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1178 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1179 list_del(&item->link);
1180 }
1181 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001182 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001183 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001184 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001185 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001186out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001187 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001188}
1189
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001190/**
1191 * efi_register_protocol_notify - register an event for notification when a
1192 * protocol is installed.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001193 *
1194 * This function implements the RegisterProtocolNotify service.
1195 * See the Unified Extensible Firmware Interface (UEFI) specification
1196 * for details.
1197 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001198 * @protocol: GUID of the protocol whose installation shall be
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001199 * notified
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001200 * @event: event to be signaled upon installation of the protocol
1201 * @registration: key for retrieving the registration information
1202 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001203 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001204static efi_status_t EFIAPI efi_register_protocol_notify(
1205 const efi_guid_t *protocol,
1206 struct efi_event *event,
1207 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001208{
Rob Clark238f88c2017-09-13 18:05:41 -04001209 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001210 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1211}
1212
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001213/**
1214 * efi_search - determine if an EFI handle implements a protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001215 *
1216 * See the documentation of the LocateHandle service in the UEFI specification.
1217 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001218 * @search_type: selection criterion
1219 * @protocol: GUID of the protocol
1220 * @search_key: registration key
1221 * @efiobj: handle
1222 * Return Value: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001223 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001224static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001225 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001226 struct efi_object *efiobj)
1227{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001228 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001229
1230 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001231 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001232 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001233 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001234 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001235 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001236 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001237 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1238 return (ret != EFI_SUCCESS);
1239 default:
1240 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001241 return -1;
1242 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001243}
1244
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001245/**
1246 * efi_locate_handle - locate handles implementing a protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001247 *
1248 * This function is meant for U-Boot internal calls. For the API implementation
1249 * of the LocateHandle service see efi_locate_handle_ext.
1250 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001251 * @search_type: selection criterion
1252 * @protocol: GUID of the protocol
1253 * @search_key: registration key
1254 * @buffer_size: size of the buffer to receive the handles in bytes
1255 * @buffer: buffer to receive the relevant handles
1256 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001257 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001258static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001259 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001260 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001261 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001262{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001263 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001264 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001265
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001266 /* Check parameters */
1267 switch (search_type) {
1268 case ALL_HANDLES:
1269 break;
1270 case BY_REGISTER_NOTIFY:
1271 if (!search_key)
1272 return EFI_INVALID_PARAMETER;
1273 /* RegisterProtocolNotify is not implemented yet */
1274 return EFI_UNSUPPORTED;
1275 case BY_PROTOCOL:
1276 if (!protocol)
1277 return EFI_INVALID_PARAMETER;
1278 break;
1279 default:
1280 return EFI_INVALID_PARAMETER;
1281 }
1282
1283 /*
1284 * efi_locate_handle_buffer uses this function for
1285 * the calculation of the necessary buffer size.
1286 * So do not require a buffer for buffersize == 0.
1287 */
1288 if (!buffer_size || (*buffer_size && !buffer))
1289 return EFI_INVALID_PARAMETER;
1290
Alexander Grafc15d9212016-03-04 01:09:59 +01001291 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001292 list_for_each_entry(efiobj, &efi_obj_list, link) {
1293 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001294 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001295 }
1296
1297 if (*buffer_size < size) {
1298 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001299 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001300 }
1301
Rob Clarkcdee3372017-08-06 14:10:07 -04001302 *buffer_size = size;
1303 if (size == 0)
1304 return EFI_NOT_FOUND;
1305
Alexander Grafc15d9212016-03-04 01:09:59 +01001306 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001307 list_for_each_entry(efiobj, &efi_obj_list, link) {
1308 if (!efi_search(search_type, protocol, search_key, efiobj))
1309 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001310 }
1311
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001312 return EFI_SUCCESS;
1313}
1314
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001315/**
1316 * efi_locate_handle_ext - locate handles implementing a protocol.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001317 *
1318 * This function implements the LocateHandle service.
1319 * See the Unified Extensible Firmware Interface (UEFI) specification
1320 * for details.
1321 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001322 * @search_type: selection criterion
1323 * @protocol: GUID of the protocol
1324 * @search_key: registration key
1325 * @buffer_size: size of the buffer to receive the handles in bytes
1326 * @buffer: buffer to receive the relevant handles
1327 * Return Value: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001328 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001329static efi_status_t EFIAPI efi_locate_handle_ext(
1330 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001331 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001332 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001333{
Rob Clark238f88c2017-09-13 18:05:41 -04001334 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001335 buffer_size, buffer);
1336
1337 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1338 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001339}
1340
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001341/**
1342 * efi_remove_configuration_table - collapses configuration table entries,
1343 * removing index i
1344 *
1345 * @i: index of the table entry to be removed
1346 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001347static void efi_remove_configuration_table(int i)
1348{
1349 struct efi_configuration_table *this = &efi_conf_table[i];
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001350 struct efi_configuration_table *next = &efi_conf_table[i + 1];
Alexander Graffe3366f2017-07-26 13:41:04 +02001351 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1352
1353 memmove(this, next, (ulong)end - (ulong)next);
1354 systab.nr_tables--;
1355}
1356
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001357/**
1358 * efi_install_configuration_table - adds, updates, or removes a configuration
1359 * table
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001360 *
1361 * This function is used for internal calls. For the API implementation of the
1362 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1363 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001364 * @guid: GUID of the installed table
1365 * @table: table to be installed
1366 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001367 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001368efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1369 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001370{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001371 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001372 int i;
1373
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001374 if (!guid)
1375 return EFI_INVALID_PARAMETER;
1376
Alexander Grafc15d9212016-03-04 01:09:59 +01001377 /* Check for guid override */
1378 for (i = 0; i < systab.nr_tables; i++) {
1379 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001380 if (table)
1381 efi_conf_table[i].table = table;
1382 else
1383 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001384 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001385 }
1386 }
1387
Alexander Graffe3366f2017-07-26 13:41:04 +02001388 if (!table)
1389 return EFI_NOT_FOUND;
1390
Alexander Grafc15d9212016-03-04 01:09:59 +01001391 /* No override, check for overflow */
1392 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001393 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001394
1395 /* Add a new entry */
1396 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1397 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001398 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001399
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001400out:
1401 /* Notify that the configuration table was changed */
1402 list_for_each_entry(evt, &efi_events, link) {
1403 if (evt->group && !guidcmp(evt->group, guid)) {
1404 efi_signal_event(evt, false);
1405 break;
1406 }
1407 }
1408
Alexander Grafc5c11632016-08-19 01:23:24 +02001409 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001410}
1411
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001412/**
1413 * efi_install_configuration_table_ex - Adds, updates, or removes a
1414 * configuration table.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001415 *
1416 * This function implements the InstallConfigurationTable service.
1417 * See the Unified Extensible Firmware Interface (UEFI) specification
1418 * for details.
1419 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001420 * @guid: GUID of the installed table
1421 * @table: table to be installed
1422 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001423 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001424static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1425 void *table)
1426{
Rob Clark238f88c2017-09-13 18:05:41 -04001427 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001428 return EFI_EXIT(efi_install_configuration_table(guid, table));
1429}
1430
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001431/**
1432 * efi_setup_loaded_image - initialize a loaded image
1433 *
1434 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001435 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001436 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001437 * @info: loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001438 * image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001439 * @obj: internal object associated with the loaded image
1440 * @device_path: device path of the loaded image
1441 * @file_path: file path of the loaded image
1442 * Return Value: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001443 */
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001444efi_status_t efi_setup_loaded_image(
1445 struct efi_loaded_image *info, struct efi_object *obj,
1446 struct efi_device_path *device_path,
1447 struct efi_device_path *file_path)
Rob Clarkf8db9222017-09-13 18:05:33 -04001448{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001449 efi_status_t ret;
1450
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001451 /* Add internal object to object list */
1452 efi_add_handle(obj);
1453 /* efi_exit() assumes that the handle points to the info */
Rob Clarkf8db9222017-09-13 18:05:33 -04001454 obj->handle = info;
1455
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001456 info->file_path = file_path;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001457
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001458 if (device_path) {
1459 info->device_handle = efi_dp_find_obj(device_path, NULL);
1460 /*
1461 * When asking for the device path interface, return
1462 * bootefi_device_path
1463 */
1464 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1465 device_path);
1466 if (ret != EFI_SUCCESS)
1467 goto failure;
1468 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001469
1470 /*
1471 * When asking for the loaded_image interface, just
1472 * return handle which points to loaded_image_info
1473 */
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001474 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1475 if (ret != EFI_SUCCESS)
1476 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001477
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001478 ret = efi_add_protocol(obj->handle,
1479 &efi_guid_device_path_to_text_protocol,
1480 (void *)&efi_device_path_to_text);
1481 if (ret != EFI_SUCCESS)
1482 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001483
Leif Lindholmb6e6fdc2018-03-09 17:43:21 +01001484 ret = efi_add_protocol(obj->handle,
1485 &efi_guid_device_path_utilities_protocol,
1486 (void *)&efi_device_path_utilities);
1487 if (ret != EFI_SUCCESS)
1488 goto failure;
1489
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001490 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001491failure:
1492 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001493 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001494}
1495
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001496/**
1497 * efi_load_image_from_path - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001498 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001499 * @file_path: the path of the image to load
1500 * @buffer: buffer containing the loaded image
1501 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001502 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001503efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1504 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001505{
1506 struct efi_file_info *info = NULL;
1507 struct efi_file_handle *f;
1508 static efi_status_t ret;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001509 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001510
1511 f = efi_file_from_path(file_path);
1512 if (!f)
1513 return EFI_DEVICE_ERROR;
1514
1515 bs = 0;
1516 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1517 &bs, info));
1518 if (ret == EFI_BUFFER_TOO_SMALL) {
1519 info = malloc(bs);
1520 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1521 &bs, info));
1522 }
1523 if (ret != EFI_SUCCESS)
1524 goto error;
1525
1526 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1527 if (ret)
1528 goto error;
1529
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001530 bs = info->file_size;
1531 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark857a1222017-09-13 18:05:35 -04001532
1533error:
1534 free(info);
1535 EFI_CALL(f->close(f));
1536
1537 if (ret != EFI_SUCCESS) {
1538 efi_free_pool(*buffer);
1539 *buffer = NULL;
1540 }
1541
1542 return ret;
1543}
1544
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001545/**
1546 * efi_load_image - load an EFI image into memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001547 *
1548 * This function implements the LoadImage service.
1549 * See the Unified Extensible Firmware Interface (UEFI) specification
1550 * for details.
1551 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001552 * @boot_policy: true for request originating from the boot manager
1553 * @parent_image: the caller's image handle
1554 * @file_path: the path of the image to load
1555 * @source_buffer: memory location from which the image is installed
1556 * @source_size: size of the memory area from which the image is
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001557 * installed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001558 * @image_handle: handle for the newly installed image
1559 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001560 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001561static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1562 efi_handle_t parent_image,
1563 struct efi_device_path *file_path,
1564 void *source_buffer,
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001565 efi_uintn_t source_size,
Alexander Grafc15d9212016-03-04 01:09:59 +01001566 efi_handle_t *image_handle)
1567{
Alexander Grafc15d9212016-03-04 01:09:59 +01001568 struct efi_loaded_image *info;
1569 struct efi_object *obj;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001570 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001571
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001572 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001573 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001574
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001575 if (!image_handle || !parent_image) {
1576 ret = EFI_INVALID_PARAMETER;
1577 goto error;
1578 }
1579
1580 if (!source_buffer && !file_path) {
1581 ret = EFI_NOT_FOUND;
1582 goto error;
1583 }
1584
Rob Clark857a1222017-09-13 18:05:35 -04001585 info = calloc(1, sizeof(*info));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001586 if (!info) {
1587 ret = EFI_OUT_OF_RESOURCES;
1588 goto error;
1589 }
Rob Clark857a1222017-09-13 18:05:35 -04001590 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001591 if (!obj) {
1592 free(info);
1593 ret = EFI_OUT_OF_RESOURCES;
1594 goto error;
1595 }
Rob Clark857a1222017-09-13 18:05:35 -04001596
1597 if (!source_buffer) {
1598 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001599
Rob Clarkc84c1102017-09-13 18:05:38 -04001600 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001601 if (ret != EFI_SUCCESS)
1602 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001603 /*
1604 * split file_path which contains both the device and
1605 * file parts:
1606 */
1607 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001608 ret = efi_setup_loaded_image(info, obj, dp, fp);
1609 if (ret != EFI_SUCCESS)
1610 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001611 } else {
1612 /* In this case, file_path is the "device" path, ie.
1613 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1614 */
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001615 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1616 if (ret != EFI_SUCCESS)
1617 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001618 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001619 info->reserved = efi_load_pe(source_buffer, info);
1620 if (!info->reserved) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001621 ret = EFI_UNSUPPORTED;
1622 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001623 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001624 info->system_table = &systab;
1625 info->parent_handle = parent_image;
Heinrich Schuchardt754d4972017-11-26 14:05:22 +01001626 *image_handle = obj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001627 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001628failure:
1629 free(info);
1630 efi_delete_handle(obj);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001631error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001632 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001633}
1634
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001635/**
1636 * efi_start_image - dall the entry point of an image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001637 *
1638 * This function implements the StartImage service.
1639 * See the Unified Extensible Firmware Interface (UEFI) specification
1640 * for details.
1641 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001642 * @image_handle: handle of the image
1643 * @exit_data_size: size of the buffer
1644 * @exit_data: buffer to receive the exit data of the called image
1645 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001646 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001647static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1648 unsigned long *exit_data_size,
1649 s16 **exit_data)
1650{
Alexander Graf54d8cdf2018-01-24 00:18:08 +01001651 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1652 struct efi_system_table *st);
Alexander Grafc15d9212016-03-04 01:09:59 +01001653 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001654 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001655
1656 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1657 entry = info->reserved;
1658
1659 efi_is_direct_boot = false;
1660
1661 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001662 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001663 /*
1664 * We called the entry point of the child image with EFI_CALL
1665 * in the lines below. The child image called the Exit() boot
1666 * service efi_exit() which executed the long jump that brought
1667 * us to the current line. This implies that the second half
1668 * of the EFI_CALL macro has not been executed.
1669 */
1670#ifdef CONFIG_ARM
1671 /*
1672 * efi_exit() called efi_restore_gd(). We have to undo this
1673 * otherwise __efi_entry_check() will put the wrong value into
1674 * app_gd.
1675 */
1676 gd = app_gd;
1677#endif
1678 /*
1679 * To get ready to call EFI_EXIT below we have to execute the
1680 * missed out steps of EFI_CALL.
1681 */
1682 assert(__efi_entry_check());
1683 debug("%sEFI: %lu returned by started image\n",
1684 __efi_nesting_dec(),
1685 (unsigned long)((uintptr_t)info->exit_status &
1686 ~EFI_ERROR_MASK));
Alexander Graf988c0662016-05-20 23:28:23 +02001687 return EFI_EXIT(info->exit_status);
1688 }
1689
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001690 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001691
Alexander Grafeff317c2018-01-26 00:47:53 +01001692 /*
1693 * Usually UEFI applications call Exit() instead of returning.
1694 * But because the world doesn not consist of ponies and unicorns,
1695 * we're happy to emulate that behavior on behalf of a payload
1696 * that forgot.
1697 */
1698 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001699}
1700
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001701/**
1702 * efi_exit - leave an EFI application or driver
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001703 *
1704 * This function implements the Exit service.
1705 * See the Unified Extensible Firmware Interface (UEFI) specification
1706 * for details.
1707 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001708 * @image_handle: handle of the application or driver that is exiting
1709 * @exit_status: status code
1710 * @exit_data_size: size of the buffer in bytes
1711 * @exit_data: buffer with data describing an error
1712 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001713 */
Alexander Graf988c0662016-05-20 23:28:23 +02001714static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001715 efi_status_t exit_status,
1716 unsigned long exit_data_size,
1717 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001718{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001719 /*
1720 * We require that the handle points to the original loaded
1721 * image protocol interface.
1722 *
1723 * For getting the longjmp address this is safer than locating
1724 * the protocol because the protocol may have been reinstalled
1725 * pointing to another memory location.
1726 *
1727 * TODO: We should call the unload procedure of the loaded
1728 * image protocol.
1729 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001730 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001731
Alexander Grafc15d9212016-03-04 01:09:59 +01001732 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1733 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001734
Alexander Graf87e787a2017-09-03 14:14:17 +02001735 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001736 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001737
Alexander Graf87e787a2017-09-03 14:14:17 +02001738 /*
1739 * But longjmp out with the U-Boot gd, not the application's, as
1740 * the other end is a setjmp call inside EFI context.
1741 */
1742 efi_restore_gd();
1743
Alexander Graf988c0662016-05-20 23:28:23 +02001744 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001745 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001746
1747 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001748}
1749
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001750/**
1751 * efi_unload_image - unload an EFI image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001752 *
1753 * This function implements the UnloadImage service.
1754 * See the Unified Extensible Firmware Interface (UEFI) specification
1755 * for details.
1756 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001757 * @image_handle: handle of the image to be unloaded
1758 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001759 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001760static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001761{
1762 struct efi_object *efiobj;
1763
1764 EFI_ENTRY("%p", image_handle);
1765 efiobj = efi_search_obj(image_handle);
1766 if (efiobj)
1767 list_del(&efiobj->link);
1768
1769 return EFI_EXIT(EFI_SUCCESS);
1770}
1771
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001772/**
1773 * efi_exit_caches - fix up caches for EFI payloads if necessary
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001774 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001775static void efi_exit_caches(void)
1776{
1777#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1778 /*
1779 * Grub on 32bit ARM needs to have caches disabled before jumping into
1780 * a zImage, but does not know of all cache layers. Give it a hand.
1781 */
1782 if (efi_is_direct_boot)
1783 cleanup_before_linux();
1784#endif
1785}
1786
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001787/**
1788 * efi_exit_boot_services - stop all boot services
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001789 *
1790 * This function implements the ExitBootServices service.
1791 * See the Unified Extensible Firmware Interface (UEFI) specification
1792 * for details.
1793 *
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001794 * All timer events are disabled.
1795 * For exit boot services events the notification function is called.
1796 * The boot services are disabled in the system table.
1797 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001798 * @image_handle: handle of the loaded image
1799 * @map_key: key of the memory map
1800 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001801 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001802static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001803 unsigned long map_key)
1804{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001805 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001806
Alexander Grafc15d9212016-03-04 01:09:59 +01001807 EFI_ENTRY("%p, %ld", image_handle, map_key);
1808
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001809 /* Make sure that notification functions are not called anymore */
1810 efi_tpl = TPL_HIGH_LEVEL;
1811
1812 /* Check if ExitBootServices has already been called */
1813 if (!systab.boottime)
1814 return EFI_EXIT(EFI_SUCCESS);
1815
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001816 /* Add related events to the event group */
1817 list_for_each_entry(evt, &efi_events, link) {
1818 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1819 evt->group = &efi_guid_event_group_exit_boot_services;
1820 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001821 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001822 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001823 if (evt->group &&
1824 !guidcmp(evt->group,
1825 &efi_guid_event_group_exit_boot_services)) {
1826 efi_signal_event(evt, false);
1827 break;
1828 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001829 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001830
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001831 /* TODO Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001832
Alexander Graf2ebeb442016-11-17 01:02:57 +01001833 board_quiesce_devices();
1834
Alexander Grafc15d9212016-03-04 01:09:59 +01001835 /* Fix up caches for EFI payloads if necessary */
1836 efi_exit_caches();
1837
1838 /* This stops all lingering devices */
1839 bootm_disable_interrupts();
1840
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001841 /* Disable boottime services */
1842 systab.con_in_handle = NULL;
1843 systab.con_in = NULL;
1844 systab.con_out_handle = NULL;
1845 systab.con_out = NULL;
1846 systab.stderr_handle = NULL;
1847 systab.std_err = NULL;
1848 systab.boottime = NULL;
1849
1850 /* Recalculate CRC32 */
1851 systab.hdr.crc32 = 0;
1852 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1853 sizeof(struct efi_system_table));
1854
Alexander Grafc15d9212016-03-04 01:09:59 +01001855 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001856 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001857 WATCHDOG_RESET();
1858
1859 return EFI_EXIT(EFI_SUCCESS);
1860}
1861
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001862/**
1863 * efi_get_next_monotonic_count - get next value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001864 *
1865 * This function implements the NextMonotonicCount service.
1866 * See the Unified Extensible Firmware Interface (UEFI) specification
1867 * for details.
1868 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001869 * @count: returned value of the counter
1870 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001871 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001872static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1873{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001874 static uint64_t mono;
1875
Alexander Grafc15d9212016-03-04 01:09:59 +01001876 EFI_ENTRY("%p", count);
1877 *count = mono++;
1878 return EFI_EXIT(EFI_SUCCESS);
1879}
1880
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001881/**
1882 * efi_stall - sleep
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001883 *
1884 * This function implements the Stall sercive.
1885 * See the Unified Extensible Firmware Interface (UEFI) specification
1886 * for details.
1887 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001888 * @microseconds: period to sleep in microseconds
1889 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001890 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001891static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1892{
1893 EFI_ENTRY("%ld", microseconds);
1894 udelay(microseconds);
1895 return EFI_EXIT(EFI_SUCCESS);
1896}
1897
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001898/**
1899 * efi_set_watchdog_timer - reset the watchdog timer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001900 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001901 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001902 * See the Unified Extensible Firmware Interface (UEFI) specification
1903 * for details.
1904 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001905 * @timeout: seconds before reset by watchdog
1906 * @watchdog_code: code to be logged when resetting
1907 * @data_size: size of buffer in bytes
1908 * @watchdog_data: buffer with data describing the reset reason
1909 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001910 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001911static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1912 uint64_t watchdog_code,
1913 unsigned long data_size,
1914 uint16_t *watchdog_data)
1915{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001916 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001917 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001918 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001919}
1920
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001921/**
1922 * efi_close_protocol - close a protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001923 *
1924 * This function implements the CloseProtocol service.
1925 * See the Unified Extensible Firmware Interface (UEFI) specification
1926 * for details.
1927 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001928 * @handle: handle on which the protocol shall be closed
1929 * @protocol: GUID of the protocol to close
1930 * @agent_handle: handle of the driver
1931 * @controller_handle: handle of the controller
1932 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001933 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001934static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001935 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001936 efi_handle_t agent_handle,
1937 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001938{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001939 struct efi_handler *handler;
1940 struct efi_open_protocol_info_item *item;
1941 struct efi_open_protocol_info_item *pos;
1942 efi_status_t r;
1943
Rob Clark238f88c2017-09-13 18:05:41 -04001944 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001945 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001946
1947 if (!agent_handle) {
1948 r = EFI_INVALID_PARAMETER;
1949 goto out;
1950 }
1951 r = efi_search_protocol(handle, protocol, &handler);
1952 if (r != EFI_SUCCESS)
1953 goto out;
1954
1955 r = EFI_NOT_FOUND;
1956 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1957 if (item->info.agent_handle == agent_handle &&
1958 item->info.controller_handle == controller_handle) {
1959 efi_delete_open_info(item);
1960 r = EFI_SUCCESS;
1961 break;
1962 }
1963 }
1964out:
1965 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001966}
1967
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001968/**
1969 * efi_open_protocol_information - provide information about then open status
1970 * of a protocol on a handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001971 *
1972 * This function implements the OpenProtocolInformation service.
1973 * See the Unified Extensible Firmware Interface (UEFI) specification
1974 * for details.
1975 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001976 * @handle: handle for which the information shall be retrieved
1977 * @protocol: GUID of the protocol
1978 * @entry_buffer: buffer to receive the open protocol information
1979 * @entry_count: number of entries available in the buffer
1980 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001981 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001982static efi_status_t EFIAPI efi_open_protocol_information(
1983 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001984 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001985 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001986{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001987 unsigned long buffer_size;
1988 unsigned long count;
1989 struct efi_handler *handler;
1990 struct efi_open_protocol_info_item *item;
1991 efi_status_t r;
1992
Rob Clark238f88c2017-09-13 18:05:41 -04001993 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001994 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01001995
1996 /* Check parameters */
1997 if (!entry_buffer) {
1998 r = EFI_INVALID_PARAMETER;
1999 goto out;
2000 }
2001 r = efi_search_protocol(handle, protocol, &handler);
2002 if (r != EFI_SUCCESS)
2003 goto out;
2004
2005 /* Count entries */
2006 count = 0;
2007 list_for_each_entry(item, &handler->open_infos, link) {
2008 if (item->info.open_count)
2009 ++count;
2010 }
2011 *entry_count = count;
2012 *entry_buffer = NULL;
2013 if (!count) {
2014 r = EFI_SUCCESS;
2015 goto out;
2016 }
2017
2018 /* Copy entries */
2019 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2020 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2021 (void **)entry_buffer);
2022 if (r != EFI_SUCCESS)
2023 goto out;
2024 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2025 if (item->info.open_count)
2026 (*entry_buffer)[--count] = item->info;
2027 }
2028out:
2029 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002030}
2031
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002032/**
2033 * efi_protocols_per_handle - get protocols installed on a handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002034 *
2035 * This function implements the ProtocolsPerHandleService.
2036 * See the Unified Extensible Firmware Interface (UEFI) specification
2037 * for details.
2038 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002039 * @handle: handle for which the information is retrieved
2040 * @protocol_buffer: buffer with protocol GUIDs
2041 * @protocol_buffer_count: number of entries in the buffer
2042 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002043 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002044static efi_status_t EFIAPI efi_protocols_per_handle(
2045 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002046 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002047{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002048 unsigned long buffer_size;
2049 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002050 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002051 efi_status_t r;
2052
Alexander Grafc15d9212016-03-04 01:09:59 +01002053 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2054 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002055
2056 if (!handle || !protocol_buffer || !protocol_buffer_count)
2057 return EFI_EXIT(EFI_INVALID_PARAMETER);
2058
2059 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002060 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002061
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002062 efiobj = efi_search_obj(handle);
2063 if (!efiobj)
2064 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002065
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002066 /* Count protocols */
2067 list_for_each(protocol_handle, &efiobj->protocols) {
2068 ++*protocol_buffer_count;
2069 }
2070
2071 /* Copy guids */
2072 if (*protocol_buffer_count) {
2073 size_t j = 0;
2074
2075 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2076 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2077 (void **)protocol_buffer);
2078 if (r != EFI_SUCCESS)
2079 return EFI_EXIT(r);
2080 list_for_each(protocol_handle, &efiobj->protocols) {
2081 struct efi_handler *protocol;
2082
2083 protocol = list_entry(protocol_handle,
2084 struct efi_handler, link);
2085 (*protocol_buffer)[j] = (void *)protocol->guid;
2086 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002087 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002088 }
2089
2090 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002091}
2092
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002093/**
2094 * efi_locate_handle_buffer - locate handles implementing a protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002095 *
2096 * This function implements the LocateHandleBuffer service.
2097 * See the Unified Extensible Firmware Interface (UEFI) specification
2098 * for details.
2099 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002100 * @search_type: selection criterion
2101 * @protocol: GUID of the protocol
2102 * @search_key: registration key
2103 * @no_handles: number of returned handles
2104 * @buffer: buffer with the returned handles
2105 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002106 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002107static efi_status_t EFIAPI efi_locate_handle_buffer(
2108 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002109 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002110 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002111{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002112 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002113 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002114
Rob Clark238f88c2017-09-13 18:05:41 -04002115 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002116 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002117
2118 if (!no_handles || !buffer) {
2119 r = EFI_INVALID_PARAMETER;
2120 goto out;
2121 }
2122 *no_handles = 0;
2123 *buffer = NULL;
2124 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2125 *buffer);
2126 if (r != EFI_BUFFER_TOO_SMALL)
2127 goto out;
2128 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2129 (void **)buffer);
2130 if (r != EFI_SUCCESS)
2131 goto out;
2132 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2133 *buffer);
2134 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002135 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002136out:
2137 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002138}
2139
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002140/**
2141 * efi_locate_protocol - find an interface implementing a protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002142 *
2143 * This function implements the LocateProtocol service.
2144 * See the Unified Extensible Firmware Interface (UEFI) specification
2145 * for details.
2146 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002147 * @protocol: GUID of the protocol
2148 * @registration: registration key passed to the notification function
2149 * @protocol_interface: interface implementing the protocol
2150 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002151 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002152static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002153 void *registration,
2154 void **protocol_interface)
2155{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002156 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002157 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002158
Rob Clark238f88c2017-09-13 18:05:41 -04002159 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002160
2161 if (!protocol || !protocol_interface)
2162 return EFI_EXIT(EFI_INVALID_PARAMETER);
2163
2164 list_for_each(lhandle, &efi_obj_list) {
2165 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002166 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002167
2168 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002169
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002170 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2171 if (ret == EFI_SUCCESS) {
2172 *protocol_interface = handler->protocol_interface;
2173 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002174 }
2175 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002176 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002177
2178 return EFI_EXIT(EFI_NOT_FOUND);
2179}
2180
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002181/**
2182 * efi_locate_device_path - Get the device path and handle of an device
2183 * implementing a protocol
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002184 *
2185 * This function implements the LocateDevicePath service.
2186 * See the Unified Extensible Firmware Interface (UEFI) specification
2187 * for details.
2188 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002189 * @protocol: GUID of the protocol
2190 * @device_path: device path
2191 * @device: handle of the device
2192 * Return Value: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002193 */
2194static efi_status_t EFIAPI efi_locate_device_path(
2195 const efi_guid_t *protocol,
2196 struct efi_device_path **device_path,
2197 efi_handle_t *device)
2198{
2199 struct efi_device_path *dp;
2200 size_t i;
2201 struct efi_handler *handler;
2202 efi_handle_t *handles;
2203 size_t len, len_dp;
2204 size_t len_best = 0;
2205 efi_uintn_t no_handles;
2206 u8 *remainder;
2207 efi_status_t ret;
2208
2209 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2210
2211 if (!protocol || !device_path || !*device_path || !device) {
2212 ret = EFI_INVALID_PARAMETER;
2213 goto out;
2214 }
2215
2216 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002217 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002218
2219 /* Get all handles implementing the protocol */
2220 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2221 &no_handles, &handles));
2222 if (ret != EFI_SUCCESS)
2223 goto out;
2224
2225 for (i = 0; i < no_handles; ++i) {
2226 /* Find the device path protocol */
2227 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2228 &handler);
2229 if (ret != EFI_SUCCESS)
2230 continue;
2231 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002232 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002233 /*
2234 * This handle can only be a better fit
2235 * if its device path length is longer than the best fit and
2236 * if its device path length is shorter of equal the searched
2237 * device path.
2238 */
2239 if (len_dp <= len_best || len_dp > len)
2240 continue;
2241 /* Check if dp is a subpath of device_path */
2242 if (memcmp(*device_path, dp, len_dp))
2243 continue;
2244 *device = handles[i];
2245 len_best = len_dp;
2246 }
2247 if (len_best) {
2248 remainder = (u8 *)*device_path + len_best;
2249 *device_path = (struct efi_device_path *)remainder;
2250 ret = EFI_SUCCESS;
2251 } else {
2252 ret = EFI_NOT_FOUND;
2253 }
2254out:
2255 return EFI_EXIT(ret);
2256}
2257
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002258/**
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002259 * Install multiple protocol interfaces.
2260 *
2261 * This function implements the MultipleProtocolInterfaces service.
2262 * See the Unified Extensible Firmware Interface (UEFI) specification
2263 * for details.
2264 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002265 * @handle: handle on which the protocol interfaces shall be
2266 * installed
2267 * @...: NULL terminated argument list with pairs of protocol
2268 * GUIDS and interfaces
2269 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002270 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002271static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2272 void **handle, ...)
2273{
2274 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002275
2276 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002277 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002278 void *protocol_interface;
2279 efi_status_t r = EFI_SUCCESS;
2280 int i = 0;
2281
2282 if (!handle)
2283 return EFI_EXIT(EFI_INVALID_PARAMETER);
2284
2285 va_start(argptr, handle);
2286 for (;;) {
2287 protocol = va_arg(argptr, efi_guid_t*);
2288 if (!protocol)
2289 break;
2290 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002291 r = EFI_CALL(efi_install_protocol_interface(
2292 handle, protocol,
2293 EFI_NATIVE_INTERFACE,
2294 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002295 if (r != EFI_SUCCESS)
2296 break;
2297 i++;
2298 }
2299 va_end(argptr);
2300 if (r == EFI_SUCCESS)
2301 return EFI_EXIT(r);
2302
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002303 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002304 va_start(argptr, handle);
2305 for (; i; --i) {
2306 protocol = va_arg(argptr, efi_guid_t*);
2307 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002308 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2309 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002310 }
2311 va_end(argptr);
2312
2313 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002314}
2315
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002316/**
2317 * efi_uninstall_multiple_protocol_interfaces - uninstall multiple protocol
2318 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002319 *
2320 * This function implements the UninstallMultipleProtocolInterfaces service.
2321 * See the Unified Extensible Firmware Interface (UEFI) specification
2322 * for details.
2323 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002324 * @handle: handle from which the protocol interfaces shall be
2325 * removed
2326 * @...: NULL terminated argument list with pairs of protocol
2327 * GUIDS and interfaces
2328 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002329 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002330static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2331 void *handle, ...)
2332{
2333 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002334
2335 va_list argptr;
2336 const efi_guid_t *protocol;
2337 void *protocol_interface;
2338 efi_status_t r = EFI_SUCCESS;
2339 size_t i = 0;
2340
2341 if (!handle)
2342 return EFI_EXIT(EFI_INVALID_PARAMETER);
2343
2344 va_start(argptr, handle);
2345 for (;;) {
2346 protocol = va_arg(argptr, efi_guid_t*);
2347 if (!protocol)
2348 break;
2349 protocol_interface = va_arg(argptr, void*);
2350 r = EFI_CALL(efi_uninstall_protocol_interface(
2351 handle, protocol,
2352 protocol_interface));
2353 if (r != EFI_SUCCESS)
2354 break;
2355 i++;
2356 }
2357 va_end(argptr);
2358 if (r == EFI_SUCCESS)
2359 return EFI_EXIT(r);
2360
2361 /* If an error occurred undo all changes. */
2362 va_start(argptr, handle);
2363 for (; i; --i) {
2364 protocol = va_arg(argptr, efi_guid_t*);
2365 protocol_interface = va_arg(argptr, void*);
2366 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2367 EFI_NATIVE_INTERFACE,
2368 protocol_interface));
2369 }
2370 va_end(argptr);
2371
2372 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002373}
2374
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002375/**
2376 * efi_calculate_crc32 - calculate cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002377 *
2378 * This function implements the CalculateCrc32 service.
2379 * See the Unified Extensible Firmware Interface (UEFI) specification
2380 * for details.
2381 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002382 * @data: buffer with data
2383 * @data_size: size of buffer in bytes
2384 * @crc32_p: cyclic redundancy code
2385 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002386 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002387static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2388 unsigned long data_size,
2389 uint32_t *crc32_p)
2390{
2391 EFI_ENTRY("%p, %ld", data, data_size);
2392 *crc32_p = crc32(0, data, data_size);
2393 return EFI_EXIT(EFI_SUCCESS);
2394}
2395
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002396/**
2397 * efi_copy_mem - copy memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002398 *
2399 * This function implements the CopyMem service.
2400 * See the Unified Extensible Firmware Interface (UEFI) specification
2401 * for details.
2402 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002403 * @destination: destination of the copy operation
2404 * @source: source of the copy operation
2405 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002406 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002407static void EFIAPI efi_copy_mem(void *destination, const void *source,
2408 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002409{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002410 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002411 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002412 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002413}
2414
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002415/**
2416 * efi_set_mem - Fill memory with a byte value.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002417 *
2418 * This function implements the SetMem service.
2419 * See the Unified Extensible Firmware Interface (UEFI) specification
2420 * for details.
2421 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002422 * @buffer: buffer to fill
2423 * @size: size of buffer in bytes
2424 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002425 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002426static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002427{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002428 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002429 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002430 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002431}
2432
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002433/**
2434 * efi_protocol_open - open protocol interface on a handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002435 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002436 * @handler: handler of a protocol
2437 * @protocol_interface: interface implementing the protocol
2438 * @agent_handle: handle of the driver
2439 * @controller_handle: handle of the controller
2440 * @attributes: attributes indicating how to open the protocol
2441 * Return Value: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002442 */
2443static efi_status_t efi_protocol_open(
2444 struct efi_handler *handler,
2445 void **protocol_interface, void *agent_handle,
2446 void *controller_handle, uint32_t attributes)
2447{
2448 struct efi_open_protocol_info_item *item;
2449 struct efi_open_protocol_info_entry *match = NULL;
2450 bool opened_by_driver = false;
2451 bool opened_exclusive = false;
2452
2453 /* If there is no agent, only return the interface */
2454 if (!agent_handle)
2455 goto out;
2456
2457 /* For TEST_PROTOCOL ignore interface attribute */
2458 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2459 *protocol_interface = NULL;
2460
2461 /*
2462 * Check if the protocol is already opened by a driver with the same
2463 * attributes or opened exclusively
2464 */
2465 list_for_each_entry(item, &handler->open_infos, link) {
2466 if (item->info.agent_handle == agent_handle) {
2467 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2468 (item->info.attributes == attributes))
2469 return EFI_ALREADY_STARTED;
2470 }
2471 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2472 opened_exclusive = true;
2473 }
2474
2475 /* Only one controller can open the protocol exclusively */
2476 if (opened_exclusive && attributes &
2477 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2478 return EFI_ACCESS_DENIED;
2479
2480 /* Prepare exclusive opening */
2481 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2482 /* Try to disconnect controllers */
2483 list_for_each_entry(item, &handler->open_infos, link) {
2484 if (item->info.attributes ==
2485 EFI_OPEN_PROTOCOL_BY_DRIVER)
2486 EFI_CALL(efi_disconnect_controller(
2487 item->info.controller_handle,
2488 item->info.agent_handle,
2489 NULL));
2490 }
2491 opened_by_driver = false;
2492 /* Check if all controllers are disconnected */
2493 list_for_each_entry(item, &handler->open_infos, link) {
2494 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2495 opened_by_driver = true;
2496 }
2497 /* Only one controller can be conncected */
2498 if (opened_by_driver)
2499 return EFI_ACCESS_DENIED;
2500 }
2501
2502 /* Find existing entry */
2503 list_for_each_entry(item, &handler->open_infos, link) {
2504 if (item->info.agent_handle == agent_handle &&
2505 item->info.controller_handle == controller_handle)
2506 match = &item->info;
2507 }
2508 /* None found, create one */
2509 if (!match) {
2510 match = efi_create_open_info(handler);
2511 if (!match)
2512 return EFI_OUT_OF_RESOURCES;
2513 }
2514
2515 match->agent_handle = agent_handle;
2516 match->controller_handle = controller_handle;
2517 match->attributes = attributes;
2518 match->open_count++;
2519
2520out:
2521 /* For TEST_PROTOCOL ignore interface attribute. */
2522 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2523 *protocol_interface = handler->protocol_interface;
2524
2525 return EFI_SUCCESS;
2526}
2527
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002528/**
2529 * efi_open_protocol - open protocol interface on a handle
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002530 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002531 * This function implements the OpenProtocol interface.
2532 * See the Unified Extensible Firmware Interface (UEFI) specification
2533 * for details.
2534 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002535 * @handle: handle on which the protocol shall be opened
2536 * @protocol: GUID of the protocol
2537 * @protocol_interface: interface implementing the protocol
2538 * @agent_handle: handle of the driver
2539 * @controller_handle: handle of the controller
2540 * @attributes: attributes indicating how to open the protocol
2541 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002542 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002543static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002544 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002545 void **protocol_interface, void *agent_handle,
2546 void *controller_handle, uint32_t attributes)
2547{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002548 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002549 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002550
Rob Clark238f88c2017-09-13 18:05:41 -04002551 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002552 protocol_interface, agent_handle, controller_handle,
2553 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002554
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002555 if (!handle || !protocol ||
2556 (!protocol_interface && attributes !=
2557 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002558 goto out;
2559 }
2560
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002561 switch (attributes) {
2562 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2563 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2564 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2565 break;
2566 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2567 if (controller_handle == handle)
2568 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002569 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002570 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2571 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002572 /* Check that the controller handle is valid */
2573 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002574 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002575 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002576 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002577 /* Check that the agent handle is valid */
2578 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002579 goto out;
2580 break;
2581 default:
2582 goto out;
2583 }
2584
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002585 r = efi_search_protocol(handle, protocol, &handler);
2586 if (r != EFI_SUCCESS)
2587 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002588
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002589 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2590 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002591out:
2592 return EFI_EXIT(r);
2593}
2594
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002595/**
2596 * efi_handle_protocol - get interface of a protocol on a handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002597 *
2598 * This function implements the HandleProtocol service.
2599 * See the Unified Extensible Firmware Interface (UEFI) specification
2600 * for details.
2601 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002602 * @handle: handle on which the protocol shall be opened
2603 * @protocol: GUID of the protocol
2604 * @protocol_interface: interface implementing the protocol
2605 * Return Value: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002606 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002607static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002608 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002609 void **protocol_interface)
2610{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002611 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2612 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002613}
2614
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002615/**
2616 * efi_bind_controller - bind a single driver to a controller
2617 *
2618 * @controller_handle: controller handle
2619 * @driver_image_handle: driver handle
2620 * @remain_device_path: remaining path
2621 * Return Value: status code
2622 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002623static efi_status_t efi_bind_controller(
2624 efi_handle_t controller_handle,
2625 efi_handle_t driver_image_handle,
2626 struct efi_device_path *remain_device_path)
2627{
2628 struct efi_driver_binding_protocol *binding_protocol;
2629 efi_status_t r;
2630
2631 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2632 &efi_guid_driver_binding_protocol,
2633 (void **)&binding_protocol,
2634 driver_image_handle, NULL,
2635 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2636 if (r != EFI_SUCCESS)
2637 return r;
2638 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2639 controller_handle,
2640 remain_device_path));
2641 if (r == EFI_SUCCESS)
2642 r = EFI_CALL(binding_protocol->start(binding_protocol,
2643 controller_handle,
2644 remain_device_path));
2645 EFI_CALL(efi_close_protocol(driver_image_handle,
2646 &efi_guid_driver_binding_protocol,
2647 driver_image_handle, NULL));
2648 return r;
2649}
2650
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002651/**
2652 * efi_connect_single_controller - connect a single driver to a controller
2653 *
2654 * @controller_handle: controller
2655 * @driver_image_handle: driver
2656 * @remain_device_path: remainting path
2657 * Return Value: status code
2658 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002659static efi_status_t efi_connect_single_controller(
2660 efi_handle_t controller_handle,
2661 efi_handle_t *driver_image_handle,
2662 struct efi_device_path *remain_device_path)
2663{
2664 efi_handle_t *buffer;
2665 size_t count;
2666 size_t i;
2667 efi_status_t r;
2668 size_t connected = 0;
2669
2670 /* Get buffer with all handles with driver binding protocol */
2671 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2672 &efi_guid_driver_binding_protocol,
2673 NULL, &count, &buffer));
2674 if (r != EFI_SUCCESS)
2675 return r;
2676
2677 /* Context Override */
2678 if (driver_image_handle) {
2679 for (; *driver_image_handle; ++driver_image_handle) {
2680 for (i = 0; i < count; ++i) {
2681 if (buffer[i] == *driver_image_handle) {
2682 buffer[i] = NULL;
2683 r = efi_bind_controller(
2684 controller_handle,
2685 *driver_image_handle,
2686 remain_device_path);
2687 /*
2688 * For drivers that do not support the
2689 * controller or are already connected
2690 * we receive an error code here.
2691 */
2692 if (r == EFI_SUCCESS)
2693 ++connected;
2694 }
2695 }
2696 }
2697 }
2698
2699 /*
2700 * TODO: Some overrides are not yet implemented:
2701 * - Platform Driver Override
2702 * - Driver Family Override Search
2703 * - Bus Specific Driver Override
2704 */
2705
2706 /* Driver Binding Search */
2707 for (i = 0; i < count; ++i) {
2708 if (buffer[i]) {
2709 r = efi_bind_controller(controller_handle,
2710 buffer[i],
2711 remain_device_path);
2712 if (r == EFI_SUCCESS)
2713 ++connected;
2714 }
2715 }
2716
2717 efi_free_pool(buffer);
2718 if (!connected)
2719 return EFI_NOT_FOUND;
2720 return EFI_SUCCESS;
2721}
2722
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002723/**
2724 * efi_connect_controller - connect a controller to a driver
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002725 *
2726 * This function implements the ConnectController service.
2727 * See the Unified Extensible Firmware Interface (UEFI) specification
2728 * for details.
2729 *
2730 * First all driver binding protocol handles are tried for binding drivers.
2731 * Afterwards all handles that have openened a protocol of the controller
2732 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2733 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002734 * @controller_handle: handle of the controller
2735 * @driver_image_handle: handle of the driver
2736 * @remain_device_path: device path of a child controller
2737 * @recursive: true to connect all child controllers
2738 * Return Value: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002739 */
2740static efi_status_t EFIAPI efi_connect_controller(
2741 efi_handle_t controller_handle,
2742 efi_handle_t *driver_image_handle,
2743 struct efi_device_path *remain_device_path,
2744 bool recursive)
2745{
2746 efi_status_t r;
2747 efi_status_t ret = EFI_NOT_FOUND;
2748 struct efi_object *efiobj;
2749
2750 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2751 remain_device_path, recursive);
2752
2753 efiobj = efi_search_obj(controller_handle);
2754 if (!efiobj) {
2755 ret = EFI_INVALID_PARAMETER;
2756 goto out;
2757 }
2758
2759 r = efi_connect_single_controller(controller_handle,
2760 driver_image_handle,
2761 remain_device_path);
2762 if (r == EFI_SUCCESS)
2763 ret = EFI_SUCCESS;
2764 if (recursive) {
2765 struct efi_handler *handler;
2766 struct efi_open_protocol_info_item *item;
2767
2768 list_for_each_entry(handler, &efiobj->protocols, link) {
2769 list_for_each_entry(item, &handler->open_infos, link) {
2770 if (item->info.attributes &
2771 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2772 r = EFI_CALL(efi_connect_controller(
2773 item->info.controller_handle,
2774 driver_image_handle,
2775 remain_device_path,
2776 recursive));
2777 if (r == EFI_SUCCESS)
2778 ret = EFI_SUCCESS;
2779 }
2780 }
2781 }
2782 }
2783 /* Check for child controller specified by end node */
2784 if (ret != EFI_SUCCESS && remain_device_path &&
2785 remain_device_path->type == DEVICE_PATH_TYPE_END)
2786 ret = EFI_SUCCESS;
2787out:
2788 return EFI_EXIT(ret);
2789}
2790
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002791/**
2792 * efi_reinstall_protocol_interface - reinstall protocol interface
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002793 *
2794 * This function implements the ReinstallProtocolInterface service.
2795 * See the Unified Extensible Firmware Interface (UEFI) specification
2796 * for details.
2797 *
2798 * The old interface is uninstalled. The new interface is installed.
2799 * Drivers are connected.
2800 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002801 * @handle: handle on which the protocol shall be
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002802 * reinstalled
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002803 * @protocol: GUID of the protocol to be installed
2804 * @old_interface: interface to be removed
2805 * @new_interface: interface to be installed
2806 * Return Value: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002807 */
2808static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2809 efi_handle_t handle, const efi_guid_t *protocol,
2810 void *old_interface, void *new_interface)
2811{
2812 efi_status_t ret;
2813
2814 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2815 new_interface);
2816 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2817 old_interface));
2818 if (ret != EFI_SUCCESS)
2819 goto out;
2820 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2821 EFI_NATIVE_INTERFACE,
2822 new_interface));
2823 if (ret != EFI_SUCCESS)
2824 goto out;
2825 /*
2826 * The returned status code has to be ignored.
2827 * Do not create an error if no suitable driver for the handle exists.
2828 */
2829 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2830out:
2831 return EFI_EXIT(ret);
2832}
2833
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002834/**
2835 * efi_get_child_controllers - get all child controllers associated to a driver
2836 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002837 * The allocated buffer has to be freed with free().
2838 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002839 * @efiobj: handle of the controller
2840 * @driver_handle: handle of the driver
2841 * @number_of_children: number of child controllers
2842 * @child_handle_buffer: handles of the the child controllers
2843 * Return Value: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002844 */
2845static efi_status_t efi_get_child_controllers(
2846 struct efi_object *efiobj,
2847 efi_handle_t driver_handle,
2848 efi_uintn_t *number_of_children,
2849 efi_handle_t **child_handle_buffer)
2850{
2851 struct efi_handler *handler;
2852 struct efi_open_protocol_info_item *item;
2853 efi_uintn_t count = 0, i;
2854 bool duplicate;
2855
2856 /* Count all child controller associations */
2857 list_for_each_entry(handler, &efiobj->protocols, link) {
2858 list_for_each_entry(item, &handler->open_infos, link) {
2859 if (item->info.agent_handle == driver_handle &&
2860 item->info.attributes &
2861 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2862 ++count;
2863 }
2864 }
2865 /*
2866 * Create buffer. In case of duplicate child controller assignments
2867 * the buffer will be too large. But that does not harm.
2868 */
2869 *number_of_children = 0;
2870 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2871 if (!*child_handle_buffer)
2872 return EFI_OUT_OF_RESOURCES;
2873 /* Copy unique child handles */
2874 list_for_each_entry(handler, &efiobj->protocols, link) {
2875 list_for_each_entry(item, &handler->open_infos, link) {
2876 if (item->info.agent_handle == driver_handle &&
2877 item->info.attributes &
2878 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2879 /* Check this is a new child controller */
2880 duplicate = false;
2881 for (i = 0; i < *number_of_children; ++i) {
2882 if ((*child_handle_buffer)[i] ==
2883 item->info.controller_handle)
2884 duplicate = true;
2885 }
2886 /* Copy handle to buffer */
2887 if (!duplicate) {
2888 i = (*number_of_children)++;
2889 (*child_handle_buffer)[i] =
2890 item->info.controller_handle;
2891 }
2892 }
2893 }
2894 }
2895 return EFI_SUCCESS;
2896}
2897
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002898/**
2899 * efi_disconnect_controller - disconnect a controller from a driver
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002900 *
2901 * This function implements the DisconnectController service.
2902 * See the Unified Extensible Firmware Interface (UEFI) specification
2903 * for details.
2904 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002905 * @controller_handle: handle of the controller
2906 * @driver_image_handle: handle of the driver
2907 * @child_handle: handle of the child to destroy
2908 * Return Value: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002909 */
2910static efi_status_t EFIAPI efi_disconnect_controller(
2911 efi_handle_t controller_handle,
2912 efi_handle_t driver_image_handle,
2913 efi_handle_t child_handle)
2914{
2915 struct efi_driver_binding_protocol *binding_protocol;
2916 efi_handle_t *child_handle_buffer = NULL;
2917 size_t number_of_children = 0;
2918 efi_status_t r;
2919 size_t stop_count = 0;
2920 struct efi_object *efiobj;
2921
2922 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2923 child_handle);
2924
2925 efiobj = efi_search_obj(controller_handle);
2926 if (!efiobj) {
2927 r = EFI_INVALID_PARAMETER;
2928 goto out;
2929 }
2930
2931 if (child_handle && !efi_search_obj(child_handle)) {
2932 r = EFI_INVALID_PARAMETER;
2933 goto out;
2934 }
2935
2936 /* If no driver handle is supplied, disconnect all drivers */
2937 if (!driver_image_handle) {
2938 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2939 goto out;
2940 }
2941
2942 /* Create list of child handles */
2943 if (child_handle) {
2944 number_of_children = 1;
2945 child_handle_buffer = &child_handle;
2946 } else {
2947 efi_get_child_controllers(efiobj,
2948 driver_image_handle,
2949 &number_of_children,
2950 &child_handle_buffer);
2951 }
2952
2953 /* Get the driver binding protocol */
2954 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2955 &efi_guid_driver_binding_protocol,
2956 (void **)&binding_protocol,
2957 driver_image_handle, NULL,
2958 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2959 if (r != EFI_SUCCESS)
2960 goto out;
2961 /* Remove the children */
2962 if (number_of_children) {
2963 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2964 controller_handle,
2965 number_of_children,
2966 child_handle_buffer));
2967 if (r == EFI_SUCCESS)
2968 ++stop_count;
2969 }
2970 /* Remove the driver */
2971 if (!child_handle)
2972 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2973 controller_handle,
2974 0, NULL));
2975 if (r == EFI_SUCCESS)
2976 ++stop_count;
2977 EFI_CALL(efi_close_protocol(driver_image_handle,
2978 &efi_guid_driver_binding_protocol,
2979 driver_image_handle, NULL));
2980
2981 if (stop_count)
2982 r = EFI_SUCCESS;
2983 else
2984 r = EFI_NOT_FOUND;
2985out:
2986 if (!child_handle)
2987 free(child_handle_buffer);
2988 return EFI_EXIT(r);
2989}
2990
Alexander Grafc15d9212016-03-04 01:09:59 +01002991static const struct efi_boot_services efi_boot_services = {
2992 .hdr = {
2993 .headersize = sizeof(struct efi_table_hdr),
2994 },
2995 .raise_tpl = efi_raise_tpl,
2996 .restore_tpl = efi_restore_tpl,
2997 .allocate_pages = efi_allocate_pages_ext,
2998 .free_pages = efi_free_pages_ext,
2999 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003000 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003001 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003002 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003003 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003004 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003005 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003006 .close_event = efi_close_event,
3007 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003008 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003009 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003010 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003011 .handle_protocol = efi_handle_protocol,
3012 .reserved = NULL,
3013 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003014 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003015 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003016 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003017 .load_image = efi_load_image,
3018 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003019 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003020 .unload_image = efi_unload_image,
3021 .exit_boot_services = efi_exit_boot_services,
3022 .get_next_monotonic_count = efi_get_next_monotonic_count,
3023 .stall = efi_stall,
3024 .set_watchdog_timer = efi_set_watchdog_timer,
3025 .connect_controller = efi_connect_controller,
3026 .disconnect_controller = efi_disconnect_controller,
3027 .open_protocol = efi_open_protocol,
3028 .close_protocol = efi_close_protocol,
3029 .open_protocol_information = efi_open_protocol_information,
3030 .protocols_per_handle = efi_protocols_per_handle,
3031 .locate_handle_buffer = efi_locate_handle_buffer,
3032 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003033 .install_multiple_protocol_interfaces =
3034 efi_install_multiple_protocol_interfaces,
3035 .uninstall_multiple_protocol_interfaces =
3036 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003037 .calculate_crc32 = efi_calculate_crc32,
3038 .copy_mem = efi_copy_mem,
3039 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003040 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003041};
3042
Heinrich Schuchardt3579b692017-12-11 20:10:20 +01003043static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003044
Alexander Graf393dd912016-10-14 13:45:30 +02003045struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003046 .hdr = {
3047 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt7d495df2018-02-05 18:04:21 +01003048 .revision = 2 << 16 | 70, /* 2.7 */
Alexander Grafc15d9212016-03-04 01:09:59 +01003049 .headersize = sizeof(struct efi_table_hdr),
3050 },
3051 .fw_vendor = (long)firmware_vendor,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003052 .con_in = (void *)&efi_con_in,
3053 .con_out = (void *)&efi_con_out,
3054 .std_err = (void *)&efi_con_out,
3055 .runtime = (void *)&efi_runtime_services,
3056 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003057 .nr_tables = 0,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003058 .tables = (void *)efi_conf_table,
Alexander Grafc15d9212016-03-04 01:09:59 +01003059};