blob: 6c272846ea5e2db275559df7c15e7829003246e4 [file] [log] [blame]
Alexander Grafbee91162016-03-04 01:09:59 +01001/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafbee91162016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020025
Alexander Grafbee91162016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
29/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
37/*
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
41 *
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
44 */
Alexander Graf3c63db92016-10-14 13:45:30 +020045static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafbee91162016-03-04 01:09:59 +010046
Simon Glass65e4c0b2016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010048/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060055#endif
Alexander Grafbee91162016-03-04 01:09:59 +010056
Rob Clarkc160d2f2017-07-27 08:04:18 -040057static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040058static int nesting_level;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010059/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040062
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010063static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
64 void *driver_image_handle,
65 void *child_handle);
66
Rob Clarkc160d2f2017-07-27 08:04:18 -040067/* Called on every callback entry */
68int __efi_entry_check(void)
69{
70 int ret = entry_count++ == 0;
71#ifdef CONFIG_ARM
72 assert(efi_gd);
73 app_gd = gd;
74 gd = efi_gd;
75#endif
76 return ret;
77}
78
79/* Called on every callback exit */
80int __efi_exit_check(void)
81{
82 int ret = --entry_count == 0;
83#ifdef CONFIG_ARM
84 gd = app_gd;
85#endif
86 return ret;
87}
88
Alexander Grafbee91162016-03-04 01:09:59 +010089/* Called from do_bootefi_exec() */
90void efi_save_gd(void)
91{
Simon Glass65e4c0b2016-09-25 15:27:35 -060092#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010093 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060094#endif
Alexander Grafbee91162016-03-04 01:09:59 +010095}
96
Rob Clarkc160d2f2017-07-27 08:04:18 -040097/*
98 * Special case handler for error/abort that just forces things back
99 * to u-boot world so we can dump out an abort msg, without any care
100 * about returning back to UEFI world.
101 */
Alexander Grafbee91162016-03-04 01:09:59 +0100102void efi_restore_gd(void)
103{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600104#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100105 /* Only restore if we're already in EFI context */
106 if (!efi_gd)
107 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100108 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600109#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100110}
111
Rob Clarkaf65db82017-07-27 08:04:19 -0400112/*
113 * Two spaces per indent level, maxing out at 10.. which ought to be
114 * enough for anyone ;-)
115 */
116static const char *indent_string(int level)
117{
118 const char *indent = " ";
119 const int max = strlen(indent);
120 level = min(max, level * 2);
121 return &indent[max - level];
122}
123
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200124const char *__efi_nesting(void)
125{
126 return indent_string(nesting_level);
127}
128
Rob Clarkaf65db82017-07-27 08:04:19 -0400129const char *__efi_nesting_inc(void)
130{
131 return indent_string(nesting_level++);
132}
133
134const char *__efi_nesting_dec(void)
135{
136 return indent_string(--nesting_level);
137}
138
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200139/*
140 * Queue an EFI event.
141 *
142 * This function queues the notification function of the event for future
143 * execution.
144 *
145 * The notification function is called if the task priority level of the
146 * event is higher than the current task priority level.
147 *
148 * For the SignalEvent service see efi_signal_event_ext.
149 *
150 * @event event to signal
151 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200152void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200153{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200154 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200155 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200156 /* Check TPL */
157 if (efi_tpl >= event->notify_tpl)
158 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200159 EFI_CALL_VOID(event->notify_function(event,
160 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200161 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200162 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200163}
164
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200165/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200166 * Raise the task priority level.
167 *
168 * This function implements the RaiseTpl service.
169 * See the Unified Extensible Firmware Interface (UEFI) specification
170 * for details.
171 *
172 * @new_tpl new value of the task priority level
173 * @return old value of the task priority level
174 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100175static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100176{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100177 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200178
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200179 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200180
181 if (new_tpl < efi_tpl)
182 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
183 efi_tpl = new_tpl;
184 if (efi_tpl > TPL_HIGH_LEVEL)
185 efi_tpl = TPL_HIGH_LEVEL;
186
187 EFI_EXIT(EFI_SUCCESS);
188 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100189}
190
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200191/*
192 * Lower the task priority level.
193 *
194 * This function implements the RestoreTpl service.
195 * See the Unified Extensible Firmware Interface (UEFI) specification
196 * for details.
197 *
198 * @old_tpl value of the task priority level to be restored
199 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100200static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100201{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200202 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200203
204 if (old_tpl > efi_tpl)
205 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
206 efi_tpl = old_tpl;
207 if (efi_tpl > TPL_HIGH_LEVEL)
208 efi_tpl = TPL_HIGH_LEVEL;
209
210 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100211}
212
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200213/*
214 * Allocate memory pages.
215 *
216 * This function implements the AllocatePages service.
217 * See the Unified Extensible Firmware Interface (UEFI) specification
218 * for details.
219 *
220 * @type type of allocation to be performed
221 * @memory_type usage type of the allocated memory
222 * @pages number of pages to be allocated
223 * @memory allocated memory
224 * @return status code
225 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900226static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100227 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900228 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100229{
230 efi_status_t r;
231
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100232 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100233 r = efi_allocate_pages(type, memory_type, pages, memory);
234 return EFI_EXIT(r);
235}
236
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200237/*
238 * Free memory pages.
239 *
240 * This function implements the FreePages service.
241 * See the Unified Extensible Firmware Interface (UEFI) specification
242 * for details.
243 *
244 * @memory start of the memory area to be freed
245 * @pages number of pages to be freed
246 * @return status code
247 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900248static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100249 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100250{
251 efi_status_t r;
252
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100253 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100254 r = efi_free_pages(memory, pages);
255 return EFI_EXIT(r);
256}
257
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200258/*
259 * Get map describing memory usage.
260 *
261 * This function implements the GetMemoryMap service.
262 * See the Unified Extensible Firmware Interface (UEFI) specification
263 * for details.
264 *
265 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
266 * on exit the size of the copied memory map
267 * @memory_map buffer to which the memory map is written
268 * @map_key key for the memory map
269 * @descriptor_size size of an individual memory descriptor
270 * @descriptor_version version number of the memory descriptor structure
271 * @return status code
272 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900273static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100274 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900275 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100276 efi_uintn_t *map_key,
277 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900278 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100279{
280 efi_status_t r;
281
282 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
283 map_key, descriptor_size, descriptor_version);
284 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
285 descriptor_size, descriptor_version);
286 return EFI_EXIT(r);
287}
288
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200289/*
290 * Allocate memory from pool.
291 *
292 * This function implements the AllocatePool service.
293 * See the Unified Extensible Firmware Interface (UEFI) specification
294 * for details.
295 *
296 * @pool_type type of the pool from which memory is to be allocated
297 * @size number of bytes to be allocated
298 * @buffer allocated memory
299 * @return status code
300 */
Stefan Brünsead12742016-10-09 22:17:18 +0200301static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100302 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200303 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100304{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100305 efi_status_t r;
306
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100307 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200308 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100309 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100310}
311
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200312/*
313 * Free memory from pool.
314 *
315 * This function implements the FreePool service.
316 * See the Unified Extensible Firmware Interface (UEFI) specification
317 * for details.
318 *
319 * @buffer start of memory to be freed
320 * @return status code
321 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200322static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100323{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100324 efi_status_t r;
325
326 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200327 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100328 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100329}
330
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200331/*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100332 * Add a new object to the object list.
333 *
334 * The protocols list is initialized.
335 * The object handle is set.
336 *
337 * @obj object to be added
338 */
339void efi_add_handle(struct efi_object *obj)
340{
341 if (!obj)
342 return;
343 INIT_LIST_HEAD(&obj->protocols);
344 obj->handle = obj;
345 list_add_tail(&obj->link, &efi_obj_list);
346}
347
348/*
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200349 * Create handle.
350 *
351 * @handle new handle
352 * @return status code
353 */
354efi_status_t efi_create_handle(void **handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200355{
356 struct efi_object *obj;
357 efi_status_t r;
358
359 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
360 sizeof(struct efi_object),
361 (void **)&obj);
362 if (r != EFI_SUCCESS)
363 return r;
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100364 efi_add_handle(obj);
365 *handle = obj->handle;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200366 return r;
367}
368
Alexander Grafbee91162016-03-04 01:09:59 +0100369/*
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100370 * Find a protocol on a handle.
371 *
372 * @handle handle
373 * @protocol_guid GUID of the protocol
374 * @handler reference to the protocol
375 * @return status code
376 */
377efi_status_t efi_search_protocol(const void *handle,
378 const efi_guid_t *protocol_guid,
379 struct efi_handler **handler)
380{
381 struct efi_object *efiobj;
382 struct list_head *lhandle;
383
384 if (!handle || !protocol_guid)
385 return EFI_INVALID_PARAMETER;
386 efiobj = efi_search_obj(handle);
387 if (!efiobj)
388 return EFI_INVALID_PARAMETER;
389 list_for_each(lhandle, &efiobj->protocols) {
390 struct efi_handler *protocol;
391
392 protocol = list_entry(lhandle, struct efi_handler, link);
393 if (!guidcmp(protocol->guid, protocol_guid)) {
394 if (handler)
395 *handler = protocol;
396 return EFI_SUCCESS;
397 }
398 }
399 return EFI_NOT_FOUND;
400}
401
402/*
403 * Delete protocol from a handle.
404 *
405 * @handle handle from which the protocol shall be deleted
406 * @protocol GUID of the protocol to be deleted
407 * @protocol_interface interface of the protocol implementation
408 * @return status code
409 */
410efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
411 void *protocol_interface)
412{
413 struct efi_handler *handler;
414 efi_status_t ret;
415
416 ret = efi_search_protocol(handle, protocol, &handler);
417 if (ret != EFI_SUCCESS)
418 return ret;
419 if (guidcmp(handler->guid, protocol))
420 return EFI_INVALID_PARAMETER;
421 list_del(&handler->link);
422 free(handler);
423 return EFI_SUCCESS;
424}
425
426/*
427 * Delete all protocols from a handle.
428 *
429 * @handle handle from which the protocols shall be deleted
430 * @return status code
431 */
432efi_status_t efi_remove_all_protocols(const void *handle)
433{
434 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100435 struct efi_handler *protocol;
436 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100437
438 efiobj = efi_search_obj(handle);
439 if (!efiobj)
440 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100441 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100442 efi_status_t ret;
443
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100444 ret = efi_remove_protocol(handle, protocol->guid,
445 protocol->protocol_interface);
446 if (ret != EFI_SUCCESS)
447 return ret;
448 }
449 return EFI_SUCCESS;
450}
451
452/*
453 * Delete handle.
454 *
455 * @handle handle to delete
456 */
457void efi_delete_handle(struct efi_object *obj)
458{
459 if (!obj)
460 return;
461 efi_remove_all_protocols(obj->handle);
462 list_del(&obj->link);
463 free(obj);
464}
465
466/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200467 * Our event capabilities are very limited. Only a small limited
468 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100469 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200470static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100471
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200472/*
473 * Create an event.
474 *
475 * This function is used inside U-Boot code to create an event.
476 *
477 * For the API function implementing the CreateEvent service see
478 * efi_create_event_ext.
479 *
480 * @type type of the event to create
481 * @notify_tpl task priority level of the event
482 * @notify_function notification function of the event
483 * @notify_context pointer passed to the notification function
484 * @event created event
485 * @return status code
486 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100487efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200488 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200489 struct efi_event *event,
490 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200491 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100492{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200493 int i;
494
Jonathan Graya95343b2017-03-12 19:26:07 +1100495 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200496 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100497
498 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200499 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100500
501 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
502 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200503 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100504
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200505 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
506 if (efi_events[i].type)
507 continue;
508 efi_events[i].type = type;
509 efi_events[i].notify_tpl = notify_tpl;
510 efi_events[i].notify_function = notify_function;
511 efi_events[i].notify_context = notify_context;
512 /* Disable timers on bootup */
513 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200514 efi_events[i].is_queued = false;
515 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200516 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200517 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200518 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200519 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100520}
521
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200522/*
523 * Create an event.
524 *
525 * This function implements the CreateEvent service.
526 * See the Unified Extensible Firmware Interface (UEFI) specification
527 * for details.
528 *
529 * @type type of the event to create
530 * @notify_tpl task priority level of the event
531 * @notify_function notification function of the event
532 * @notify_context pointer passed to the notification function
533 * @event created event
534 * @return status code
535 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200536static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100537 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200538 void (EFIAPI *notify_function) (
539 struct efi_event *event,
540 void *context),
541 void *notify_context, struct efi_event **event)
542{
543 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
544 notify_context);
545 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
546 notify_context, event));
547}
548
549
Alexander Grafbee91162016-03-04 01:09:59 +0100550/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200551 * Check if a timer event has occurred or a queued notification function should
552 * be called.
553 *
Alexander Grafbee91162016-03-04 01:09:59 +0100554 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200555 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100556 */
557void efi_timer_check(void)
558{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200559 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100560 u64 now = timer_get_us();
561
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200562 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200563 if (!efi_events[i].type)
564 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200565 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200566 efi_signal_event(&efi_events[i]);
567 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200568 now < efi_events[i].trigger_next)
569 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200570 switch (efi_events[i].trigger_type) {
571 case EFI_TIMER_RELATIVE:
572 efi_events[i].trigger_type = EFI_TIMER_STOP;
573 break;
574 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200575 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200576 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200577 break;
578 default:
579 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200580 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200581 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200582 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100583 }
Alexander Grafbee91162016-03-04 01:09:59 +0100584 WATCHDOG_RESET();
585}
586
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200587/*
588 * Set the trigger time for a timer event or stop the event.
589 *
590 * This is the function for internal usage in U-Boot. For the API function
591 * implementing the SetTimer service see efi_set_timer_ext.
592 *
593 * @event event for which the timer is set
594 * @type type of the timer
595 * @trigger_time trigger period in multiples of 100ns
596 * @return status code
597 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200598efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200599 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100600{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200601 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100602
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200603 /*
604 * The parameter defines a multiple of 100ns.
605 * We use multiples of 1000ns. So divide by 10.
606 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200607 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100608
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200609 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
610 if (event != &efi_events[i])
611 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100612
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200613 if (!(event->type & EVT_TIMER))
614 break;
615 switch (type) {
616 case EFI_TIMER_STOP:
617 event->trigger_next = -1ULL;
618 break;
619 case EFI_TIMER_PERIODIC:
620 case EFI_TIMER_RELATIVE:
621 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200622 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200623 break;
624 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200625 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200626 }
627 event->trigger_type = type;
628 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200629 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200630 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100631 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200632 return EFI_INVALID_PARAMETER;
633}
634
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200635/*
636 * Set the trigger time for a timer event or stop the event.
637 *
638 * This function implements the SetTimer service.
639 * See the Unified Extensible Firmware Interface (UEFI) specification
640 * for details.
641 *
642 * @event event for which the timer is set
643 * @type type of the timer
644 * @trigger_time trigger period in multiples of 100ns
645 * @return status code
646 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200647static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
648 enum efi_timer_delay type,
649 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200650{
651 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
652 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100653}
654
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200655/*
656 * Wait for events to be signaled.
657 *
658 * This function implements the WaitForEvent service.
659 * See the Unified Extensible Firmware Interface (UEFI) specification
660 * for details.
661 *
662 * @num_events number of events to be waited for
663 * @events events to be waited for
664 * @index index of the event that was signaled
665 * @return status code
666 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100667static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200668 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100669 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100670{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200671 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100672
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100673 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100674
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200675 /* Check parameters */
676 if (!num_events || !event)
677 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200678 /* Check TPL */
679 if (efi_tpl != TPL_APPLICATION)
680 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200681 for (i = 0; i < num_events; ++i) {
682 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
683 if (event[i] == &efi_events[j])
684 goto known_event;
685 }
686 return EFI_EXIT(EFI_INVALID_PARAMETER);
687known_event:
688 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
689 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200690 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200691 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200692 }
693
694 /* Wait for signal */
695 for (;;) {
696 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200697 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200698 goto out;
699 }
700 /* Allow events to occur. */
701 efi_timer_check();
702 }
703
704out:
705 /*
706 * Reset the signal which is passed to the caller to allow periodic
707 * events to occur.
708 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200709 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200710 if (index)
711 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100712
713 return EFI_EXIT(EFI_SUCCESS);
714}
715
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200716/*
717 * Signal an EFI event.
718 *
719 * This function implements the SignalEvent service.
720 * See the Unified Extensible Firmware Interface (UEFI) specification
721 * for details.
722 *
723 * This functions sets the signaled state of the event and queues the
724 * notification function for execution.
725 *
726 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200727 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200728 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200729static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100730{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200731 int i;
732
Alexander Grafbee91162016-03-04 01:09:59 +0100733 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200734 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
735 if (event != &efi_events[i])
736 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200737 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200738 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200739 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200740 if (event->type & EVT_NOTIFY_SIGNAL)
741 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200742 break;
743 }
Alexander Grafbee91162016-03-04 01:09:59 +0100744 return EFI_EXIT(EFI_SUCCESS);
745}
746
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200747/*
748 * Close an EFI event.
749 *
750 * This function implements the CloseEvent service.
751 * See the Unified Extensible Firmware Interface (UEFI) specification
752 * for details.
753 *
754 * @event event to close
755 * @return status code
756 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200757static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100758{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200759 int i;
760
Alexander Grafbee91162016-03-04 01:09:59 +0100761 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200762 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
763 if (event == &efi_events[i]) {
764 event->type = 0;
765 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200766 event->is_queued = false;
767 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200768 return EFI_EXIT(EFI_SUCCESS);
769 }
770 }
771 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100772}
773
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200774/*
775 * Check if an event is signaled.
776 *
777 * This function implements the CheckEvent service.
778 * See the Unified Extensible Firmware Interface (UEFI) specification
779 * for details.
780 *
781 * If an event is not signaled yet the notification function is queued.
782 *
783 * @event event to check
784 * @return status code
785 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200786static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100787{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200788 int i;
789
Alexander Grafbee91162016-03-04 01:09:59 +0100790 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200791 efi_timer_check();
792 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
793 if (event != &efi_events[i])
794 continue;
795 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
796 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200797 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200798 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200799 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200800 return EFI_EXIT(EFI_SUCCESS);
801 return EFI_EXIT(EFI_NOT_READY);
802 }
803 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100804}
805
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200806/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200807 * Find the internal EFI object for a handle.
808 *
809 * @handle handle to find
810 * @return EFI object
811 */
Heinrich Schuchardt085d07c2017-10-26 19:25:50 +0200812struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200813{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100814 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200815
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100816 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200817 if (efiobj->handle == handle)
818 return efiobj;
819 }
820
821 return NULL;
822}
823
824/*
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100825 * Create open protocol info entry and add it to a protocol.
826 *
827 * @handler handler of a protocol
828 * @return open protocol info entry
829 */
830static struct efi_open_protocol_info_entry *efi_create_open_info(
831 struct efi_handler *handler)
832{
833 struct efi_open_protocol_info_item *item;
834
835 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
836 if (!item)
837 return NULL;
838 /* Append the item to the open protocol info list. */
839 list_add_tail(&item->link, &handler->open_infos);
840
841 return &item->info;
842}
843
844/*
845 * Remove an open protocol info entry from a protocol.
846 *
847 * @handler handler of a protocol
848 * @return status code
849 */
850static efi_status_t efi_delete_open_info(
851 struct efi_open_protocol_info_item *item)
852{
853 list_del(&item->link);
854 free(item);
855 return EFI_SUCCESS;
856}
857
858/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200859 * Install new protocol on a handle.
860 *
861 * @handle handle on which the protocol shall be installed
862 * @protocol GUID of the protocol to be installed
863 * @protocol_interface interface of the protocol implementation
864 * @return status code
865 */
866efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
867 void *protocol_interface)
868{
869 struct efi_object *efiobj;
870 struct efi_handler *handler;
871 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200872
873 efiobj = efi_search_obj(handle);
874 if (!efiobj)
875 return EFI_INVALID_PARAMETER;
876 ret = efi_search_protocol(handle, protocol, NULL);
877 if (ret != EFI_NOT_FOUND)
878 return EFI_INVALID_PARAMETER;
879 handler = calloc(1, sizeof(struct efi_handler));
880 if (!handler)
881 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100882 handler->guid = protocol;
883 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100884 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100885 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +0100886 if (!guidcmp(&efi_guid_device_path, protocol))
887 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100888 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200889}
890
891/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200892 * Install protocol interface.
893 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100894 * This function implements the InstallProtocolInterface service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification
896 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200897 *
898 * @handle handle on which the protocol shall be installed
899 * @protocol GUID of the protocol to be installed
900 * @protocol_interface_type type of the interface to be installed,
901 * always EFI_NATIVE_INTERFACE
902 * @protocol_interface interface of the protocol implementation
903 * @return status code
904 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100905static efi_status_t EFIAPI efi_install_protocol_interface(
906 void **handle, const efi_guid_t *protocol,
907 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100908{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200909 efi_status_t r;
910
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100911 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
912 protocol_interface);
913
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200914 if (!handle || !protocol ||
915 protocol_interface_type != EFI_NATIVE_INTERFACE) {
916 r = EFI_INVALID_PARAMETER;
917 goto out;
918 }
919
920 /* Create new handle if requested. */
921 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200922 r = efi_create_handle(handle);
923 if (r != EFI_SUCCESS)
924 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200925 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
926 *handle);
927 } else {
928 debug("%sEFI: handle %p\n", indent_string(nesting_level),
929 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200930 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200931 /* Add new protocol */
932 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200933out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100934 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100935}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200936
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200937/*
938 * Reinstall protocol interface.
939 *
940 * This function implements the ReinstallProtocolInterface service.
941 * See the Unified Extensible Firmware Interface (UEFI) specification
942 * for details.
943 *
944 * @handle handle on which the protocol shall be
945 * reinstalled
946 * @protocol GUID of the protocol to be installed
947 * @old_interface interface to be removed
948 * @new_interface interface to be installed
949 * @return status code
950 */
Alexander Grafbee91162016-03-04 01:09:59 +0100951static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200952 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100953 void *new_interface)
954{
Rob Clark778e6af2017-09-13 18:05:41 -0400955 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100956 new_interface);
957 return EFI_EXIT(EFI_ACCESS_DENIED);
958}
959
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200960/*
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +0100961 * Get all drivers associated to a controller.
962 * The allocated buffer has to be freed with free().
963 *
964 * @efiobj handle of the controller
965 * @protocol protocol guid (optional)
966 * @number_of_drivers number of child controllers
967 * @driver_handle_buffer handles of the the drivers
968 * @return status code
969 */
970static efi_status_t efi_get_drivers(struct efi_object *efiobj,
971 const efi_guid_t *protocol,
972 efi_uintn_t *number_of_drivers,
973 efi_handle_t **driver_handle_buffer)
974{
975 struct efi_handler *handler;
976 struct efi_open_protocol_info_item *item;
977 efi_uintn_t count = 0, i;
978 bool duplicate;
979
980 /* Count all driver associations */
981 list_for_each_entry(handler, &efiobj->protocols, link) {
982 if (protocol && guidcmp(handler->guid, protocol))
983 continue;
984 list_for_each_entry(item, &handler->open_infos, link) {
985 if (item->info.attributes &
986 EFI_OPEN_PROTOCOL_BY_DRIVER)
987 ++count;
988 }
989 }
990 /*
991 * Create buffer. In case of duplicate driver assignments the buffer
992 * will be too large. But that does not harm.
993 */
994 *number_of_drivers = 0;
995 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
996 if (!*driver_handle_buffer)
997 return EFI_OUT_OF_RESOURCES;
998 /* Collect unique driver handles */
999 list_for_each_entry(handler, &efiobj->protocols, link) {
1000 if (protocol && guidcmp(handler->guid, protocol))
1001 continue;
1002 list_for_each_entry(item, &handler->open_infos, link) {
1003 if (item->info.attributes &
1004 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1005 /* Check this is a new driver */
1006 duplicate = false;
1007 for (i = 0; i < *number_of_drivers; ++i) {
1008 if ((*driver_handle_buffer)[i] ==
1009 item->info.agent_handle)
1010 duplicate = true;
1011 }
1012 /* Copy handle to buffer */
1013 if (!duplicate) {
1014 i = (*number_of_drivers)++;
1015 (*driver_handle_buffer)[i] =
1016 item->info.agent_handle;
1017 }
1018 }
1019 }
1020 }
1021 return EFI_SUCCESS;
1022}
1023
1024/*
1025 * Disconnect all drivers from a controller.
1026 *
1027 * This function implements the DisconnectController service.
1028 * See the Unified Extensible Firmware Interface (UEFI) specification
1029 * for details.
1030 *
1031 * @efiobj handle of the controller
1032 * @protocol protocol guid (optional)
1033 * @child_handle handle of the child to destroy
1034 * @return status code
1035 */
1036static efi_status_t efi_disconnect_all_drivers(
1037 struct efi_object *efiobj,
1038 const efi_guid_t *protocol,
1039 efi_handle_t child_handle)
1040{
1041 efi_uintn_t number_of_drivers;
1042 efi_handle_t *driver_handle_buffer;
1043 efi_status_t r, ret;
1044
1045 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1046 &driver_handle_buffer);
1047 if (ret != EFI_SUCCESS)
1048 return ret;
1049
1050 ret = EFI_NOT_FOUND;
1051 while (number_of_drivers) {
1052 r = EFI_CALL(efi_disconnect_controller(
1053 efiobj->handle,
1054 driver_handle_buffer[--number_of_drivers],
1055 child_handle));
1056 if (r == EFI_SUCCESS)
1057 ret = r;
1058 }
1059 free(driver_handle_buffer);
1060 return ret;
1061}
1062
1063/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001064 * Uninstall protocol interface.
1065 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001066 * This function implements the UninstallProtocolInterface service.
1067 * See the Unified Extensible Firmware Interface (UEFI) specification
1068 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001069 *
1070 * @handle handle from which the protocol shall be removed
1071 * @protocol GUID of the protocol to be removed
1072 * @protocol_interface interface to be removed
1073 * @return status code
1074 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001075static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1076 void *handle, const efi_guid_t *protocol,
1077 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001078{
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001079 struct efi_handler *handler;
1080 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001081
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001082 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1083
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001084 if (!handle || !protocol) {
1085 r = EFI_INVALID_PARAMETER;
1086 goto out;
1087 }
1088
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001089 /* Find the protocol on the handle */
1090 r = efi_search_protocol(handle, protocol, &handler);
1091 if (r != EFI_SUCCESS)
1092 goto out;
1093 if (handler->protocol_interface) {
1094 /* TODO disconnect controllers */
1095 r = EFI_ACCESS_DENIED;
1096 } else {
1097 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001098 }
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001099out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001100 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001101}
1102
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001103/*
1104 * Register an event for notification when a protocol is installed.
1105 *
1106 * This function implements the RegisterProtocolNotify service.
1107 * See the Unified Extensible Firmware Interface (UEFI) specification
1108 * for details.
1109 *
1110 * @protocol GUID of the protocol whose installation shall be
1111 * notified
1112 * @event event to be signaled upon installation of the protocol
1113 * @registration key for retrieving the registration information
1114 * @return status code
1115 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001116static efi_status_t EFIAPI efi_register_protocol_notify(
1117 const efi_guid_t *protocol,
1118 struct efi_event *event,
1119 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001120{
Rob Clark778e6af2017-09-13 18:05:41 -04001121 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001122 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1123}
1124
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001125/*
1126 * Determine if an EFI handle implements a protocol.
1127 *
1128 * See the documentation of the LocateHandle service in the UEFI specification.
1129 *
1130 * @search_type selection criterion
1131 * @protocol GUID of the protocol
1132 * @search_key registration key
1133 * @efiobj handle
1134 * @return 0 if the handle implements the protocol
1135 */
Alexander Grafbee91162016-03-04 01:09:59 +01001136static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001137 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001138 struct efi_object *efiobj)
1139{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001140 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001141
1142 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001143 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001144 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001145 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001146 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001147 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001148 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001149 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1150 return (ret != EFI_SUCCESS);
1151 default:
1152 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001153 return -1;
1154 }
Alexander Grafbee91162016-03-04 01:09:59 +01001155}
1156
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001157/*
1158 * Locate handles implementing a protocol.
1159 *
1160 * This function is meant for U-Boot internal calls. For the API implementation
1161 * of the LocateHandle service see efi_locate_handle_ext.
1162 *
1163 * @search_type selection criterion
1164 * @protocol GUID of the protocol
1165 * @search_key registration key
1166 * @buffer_size size of the buffer to receive the handles in bytes
1167 * @buffer buffer to receive the relevant handles
1168 * @return status code
1169 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001170static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001171 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001172 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001173 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001174{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001175 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001176 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001177
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001178 /* Check parameters */
1179 switch (search_type) {
1180 case ALL_HANDLES:
1181 break;
1182 case BY_REGISTER_NOTIFY:
1183 if (!search_key)
1184 return EFI_INVALID_PARAMETER;
1185 /* RegisterProtocolNotify is not implemented yet */
1186 return EFI_UNSUPPORTED;
1187 case BY_PROTOCOL:
1188 if (!protocol)
1189 return EFI_INVALID_PARAMETER;
1190 break;
1191 default:
1192 return EFI_INVALID_PARAMETER;
1193 }
1194
1195 /*
1196 * efi_locate_handle_buffer uses this function for
1197 * the calculation of the necessary buffer size.
1198 * So do not require a buffer for buffersize == 0.
1199 */
1200 if (!buffer_size || (*buffer_size && !buffer))
1201 return EFI_INVALID_PARAMETER;
1202
Alexander Grafbee91162016-03-04 01:09:59 +01001203 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001204 list_for_each_entry(efiobj, &efi_obj_list, link) {
1205 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001206 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001207 }
1208
1209 if (*buffer_size < size) {
1210 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001211 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001212 }
1213
Rob Clark796a78c2017-08-06 14:10:07 -04001214 *buffer_size = size;
1215 if (size == 0)
1216 return EFI_NOT_FOUND;
1217
Alexander Grafbee91162016-03-04 01:09:59 +01001218 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001219 list_for_each_entry(efiobj, &efi_obj_list, link) {
1220 if (!efi_search(search_type, protocol, search_key, efiobj))
1221 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001222 }
1223
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001224 return EFI_SUCCESS;
1225}
1226
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001227/*
1228 * Locate handles implementing a protocol.
1229 *
1230 * This function implements the LocateHandle service.
1231 * See the Unified Extensible Firmware Interface (UEFI) specification
1232 * for details.
1233 *
1234 * @search_type selection criterion
1235 * @protocol GUID of the protocol
1236 * @search_key registration key
1237 * @buffer_size size of the buffer to receive the handles in bytes
1238 * @buffer buffer to receive the relevant handles
1239 * @return 0 if the handle implements the protocol
1240 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001241static efi_status_t EFIAPI efi_locate_handle_ext(
1242 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001243 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001244 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001245{
Rob Clark778e6af2017-09-13 18:05:41 -04001246 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001247 buffer_size, buffer);
1248
1249 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1250 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001251}
1252
Alexander Grafd98cdf62017-07-26 13:41:04 +02001253/* Collapses configuration table entries, removing index i */
1254static void efi_remove_configuration_table(int i)
1255{
1256 struct efi_configuration_table *this = &efi_conf_table[i];
1257 struct efi_configuration_table *next = &efi_conf_table[i+1];
1258 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1259
1260 memmove(this, next, (ulong)end - (ulong)next);
1261 systab.nr_tables--;
1262}
1263
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001264/*
1265 * Adds, updates, or removes a configuration table.
1266 *
1267 * This function is used for internal calls. For the API implementation of the
1268 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1269 *
1270 * @guid GUID of the installed table
1271 * @table table to be installed
1272 * @return status code
1273 */
Alexander Graf488bf122016-08-19 01:23:24 +02001274efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001275{
1276 int i;
1277
Alexander Grafbee91162016-03-04 01:09:59 +01001278 /* Check for guid override */
1279 for (i = 0; i < systab.nr_tables; i++) {
1280 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001281 if (table)
1282 efi_conf_table[i].table = table;
1283 else
1284 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001285 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001286 }
1287 }
1288
Alexander Grafd98cdf62017-07-26 13:41:04 +02001289 if (!table)
1290 return EFI_NOT_FOUND;
1291
Alexander Grafbee91162016-03-04 01:09:59 +01001292 /* No override, check for overflow */
1293 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001294 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001295
1296 /* Add a new entry */
1297 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1298 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001299 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001300
Alexander Graf488bf122016-08-19 01:23:24 +02001301 return EFI_SUCCESS;
1302}
1303
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001304/*
1305 * Adds, updates, or removes a configuration table.
1306 *
1307 * This function implements the InstallConfigurationTable service.
1308 * See the Unified Extensible Firmware Interface (UEFI) specification
1309 * for details.
1310 *
1311 * @guid GUID of the installed table
1312 * @table table to be installed
1313 * @return status code
1314 */
Alexander Graf488bf122016-08-19 01:23:24 +02001315static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1316 void *table)
1317{
Rob Clark778e6af2017-09-13 18:05:41 -04001318 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001319 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001320}
1321
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001322/*
1323 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001324 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001325 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001326 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001327 * image
1328 * @obj internal object associated with the loaded image
1329 * @device_path device path of the loaded image
1330 * @file_path file path of the loaded image
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001331 * @return status code
Rob Clark95c55532017-09-13 18:05:33 -04001332 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001333efi_status_t efi_setup_loaded_image(
1334 struct efi_loaded_image *info, struct efi_object *obj,
1335 struct efi_device_path *device_path,
1336 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001337{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001338 efi_status_t ret;
1339
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001340 /* Add internal object to object list */
1341 efi_add_handle(obj);
1342 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001343 obj->handle = info;
1344
Rob Clark95c55532017-09-13 18:05:33 -04001345 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001346 if (device_path)
1347 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001348
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001349 /*
1350 * When asking for the device path interface, return
1351 * bootefi_device_path
1352 */
1353 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1354 if (ret != EFI_SUCCESS)
1355 goto failure;
1356
1357 /*
1358 * When asking for the loaded_image interface, just
1359 * return handle which points to loaded_image_info
1360 */
1361 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1362 if (ret != EFI_SUCCESS)
1363 goto failure;
1364
1365 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1366 (void *)&efi_console_control);
1367 if (ret != EFI_SUCCESS)
1368 goto failure;
1369
1370 ret = efi_add_protocol(obj->handle,
1371 &efi_guid_device_path_to_text_protocol,
1372 (void *)&efi_device_path_to_text);
1373 if (ret != EFI_SUCCESS)
1374 goto failure;
1375
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001376 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001377failure:
1378 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001379 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001380}
1381
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001382/*
1383 * Load an image using a file path.
1384 *
1385 * @file_path the path of the image to load
1386 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001387 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001388 */
Rob Clark9975fe92017-09-13 18:05:38 -04001389efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1390 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001391{
1392 struct efi_file_info *info = NULL;
1393 struct efi_file_handle *f;
1394 static efi_status_t ret;
1395 uint64_t bs;
1396
1397 f = efi_file_from_path(file_path);
1398 if (!f)
1399 return EFI_DEVICE_ERROR;
1400
1401 bs = 0;
1402 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1403 &bs, info));
1404 if (ret == EFI_BUFFER_TOO_SMALL) {
1405 info = malloc(bs);
1406 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1407 &bs, info));
1408 }
1409 if (ret != EFI_SUCCESS)
1410 goto error;
1411
1412 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1413 if (ret)
1414 goto error;
1415
1416 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1417
1418error:
1419 free(info);
1420 EFI_CALL(f->close(f));
1421
1422 if (ret != EFI_SUCCESS) {
1423 efi_free_pool(*buffer);
1424 *buffer = NULL;
1425 }
1426
1427 return ret;
1428}
1429
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001430/*
1431 * Load an EFI image into memory.
1432 *
1433 * This function implements the LoadImage service.
1434 * See the Unified Extensible Firmware Interface (UEFI) specification
1435 * for details.
1436 *
1437 * @boot_policy true for request originating from the boot manager
1438 * @parent_image the calles's image handle
1439 * @file_path the path of the image to load
1440 * @source_buffer memory location from which the image is installed
1441 * @source_size size of the memory area from which the image is
1442 * installed
1443 * @image_handle handle for the newly installed image
1444 * @return status code
1445 */
Alexander Grafbee91162016-03-04 01:09:59 +01001446static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1447 efi_handle_t parent_image,
1448 struct efi_device_path *file_path,
1449 void *source_buffer,
1450 unsigned long source_size,
1451 efi_handle_t *image_handle)
1452{
Alexander Grafbee91162016-03-04 01:09:59 +01001453 struct efi_loaded_image *info;
1454 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001455 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001456
1457 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1458 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001459
1460 info = calloc(1, sizeof(*info));
1461 obj = calloc(1, sizeof(*obj));
1462
1463 if (!source_buffer) {
1464 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001465
Rob Clark9975fe92017-09-13 18:05:38 -04001466 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001467 if (ret != EFI_SUCCESS)
1468 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001469 /*
1470 * split file_path which contains both the device and
1471 * file parts:
1472 */
1473 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001474 ret = efi_setup_loaded_image(info, obj, dp, fp);
1475 if (ret != EFI_SUCCESS)
1476 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001477 } else {
1478 /* In this case, file_path is the "device" path, ie.
1479 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1480 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001481 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1482 if (ret != EFI_SUCCESS)
1483 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001484 }
Alexander Grafbee91162016-03-04 01:09:59 +01001485 info->reserved = efi_load_pe(source_buffer, info);
1486 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001487 ret = EFI_UNSUPPORTED;
1488 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001489 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001490 info->system_table = &systab;
1491 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001492 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001493 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001494failure:
1495 free(info);
1496 efi_delete_handle(obj);
1497 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001498}
1499
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001500/*
1501 * Call the entry point of an image.
1502 *
1503 * This function implements the StartImage service.
1504 * See the Unified Extensible Firmware Interface (UEFI) specification
1505 * for details.
1506 *
1507 * @image_handle handle of the image
1508 * @exit_data_size size of the buffer
1509 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001510 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001511 */
Alexander Grafbee91162016-03-04 01:09:59 +01001512static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1513 unsigned long *exit_data_size,
1514 s16 **exit_data)
1515{
1516 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1517 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001518 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001519
1520 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1521 entry = info->reserved;
1522
1523 efi_is_direct_boot = false;
1524
1525 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001526 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001527 /*
1528 * We called the entry point of the child image with EFI_CALL
1529 * in the lines below. The child image called the Exit() boot
1530 * service efi_exit() which executed the long jump that brought
1531 * us to the current line. This implies that the second half
1532 * of the EFI_CALL macro has not been executed.
1533 */
1534#ifdef CONFIG_ARM
1535 /*
1536 * efi_exit() called efi_restore_gd(). We have to undo this
1537 * otherwise __efi_entry_check() will put the wrong value into
1538 * app_gd.
1539 */
1540 gd = app_gd;
1541#endif
1542 /*
1543 * To get ready to call EFI_EXIT below we have to execute the
1544 * missed out steps of EFI_CALL.
1545 */
1546 assert(__efi_entry_check());
1547 debug("%sEFI: %lu returned by started image\n",
1548 __efi_nesting_dec(),
1549 (unsigned long)((uintptr_t)info->exit_status &
1550 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001551 return EFI_EXIT(info->exit_status);
1552 }
1553
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001554 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001555
1556 /* Should usually never get here */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001557 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001558}
1559
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001560/*
1561 * Leave an EFI application or driver.
1562 *
1563 * This function implements the Exit service.
1564 * See the Unified Extensible Firmware Interface (UEFI) specification
1565 * for details.
1566 *
1567 * @image_handle handle of the application or driver that is exiting
1568 * @exit_status status code
1569 * @exit_data_size size of the buffer in bytes
1570 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001571 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001572 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001573static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1574 efi_status_t exit_status, unsigned long exit_data_size,
1575 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001576{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001577 /*
1578 * We require that the handle points to the original loaded
1579 * image protocol interface.
1580 *
1581 * For getting the longjmp address this is safer than locating
1582 * the protocol because the protocol may have been reinstalled
1583 * pointing to another memory location.
1584 *
1585 * TODO: We should call the unload procedure of the loaded
1586 * image protocol.
1587 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001588 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1589
Alexander Grafbee91162016-03-04 01:09:59 +01001590 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1591 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001592
Alexander Grafa1489202017-09-03 14:14:17 +02001593 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001594 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001595
Alexander Grafa1489202017-09-03 14:14:17 +02001596 /*
1597 * But longjmp out with the U-Boot gd, not the application's, as
1598 * the other end is a setjmp call inside EFI context.
1599 */
1600 efi_restore_gd();
1601
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001602 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001603 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001604
1605 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001606}
1607
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001608/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001609 * Unload an EFI image.
1610 *
1611 * This function implements the UnloadImage service.
1612 * See the Unified Extensible Firmware Interface (UEFI) specification
1613 * for details.
1614 *
1615 * @image_handle handle of the image to be unloaded
1616 * @return status code
1617 */
Alexander Grafbee91162016-03-04 01:09:59 +01001618static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1619{
1620 struct efi_object *efiobj;
1621
1622 EFI_ENTRY("%p", image_handle);
1623 efiobj = efi_search_obj(image_handle);
1624 if (efiobj)
1625 list_del(&efiobj->link);
1626
1627 return EFI_EXIT(EFI_SUCCESS);
1628}
1629
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001630/*
1631 * Fix up caches for EFI payloads if necessary.
1632 */
Alexander Grafbee91162016-03-04 01:09:59 +01001633static void efi_exit_caches(void)
1634{
1635#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1636 /*
1637 * Grub on 32bit ARM needs to have caches disabled before jumping into
1638 * a zImage, but does not know of all cache layers. Give it a hand.
1639 */
1640 if (efi_is_direct_boot)
1641 cleanup_before_linux();
1642#endif
1643}
1644
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001645/*
1646 * Stop boot services.
1647 *
1648 * This function implements the ExitBootServices service.
1649 * See the Unified Extensible Firmware Interface (UEFI) specification
1650 * for details.
1651 *
1652 * @image_handle handle of the loaded image
1653 * @map_key key of the memory map
1654 * @return status code
1655 */
Alexander Grafbee91162016-03-04 01:09:59 +01001656static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1657 unsigned long map_key)
1658{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001659 int i;
1660
Alexander Grafbee91162016-03-04 01:09:59 +01001661 EFI_ENTRY("%p, %ld", image_handle, map_key);
1662
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001663 /* Notify that ExitBootServices is invoked. */
1664 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1665 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1666 continue;
1667 efi_signal_event(&efi_events[i]);
1668 }
1669 /* Make sure that notification functions are not called anymore */
1670 efi_tpl = TPL_HIGH_LEVEL;
1671
Alexander Graf40583562017-10-19 23:23:50 +02001672 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001673
Alexander Grafb7b84102016-11-17 01:02:57 +01001674 board_quiesce_devices();
1675
Alexander Grafbee91162016-03-04 01:09:59 +01001676 /* Fix up caches for EFI payloads if necessary */
1677 efi_exit_caches();
1678
1679 /* This stops all lingering devices */
1680 bootm_disable_interrupts();
1681
1682 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001683 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001684 WATCHDOG_RESET();
1685
1686 return EFI_EXIT(EFI_SUCCESS);
1687}
1688
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001689/*
1690 * Get next value of the counter.
1691 *
1692 * This function implements the NextMonotonicCount service.
1693 * See the Unified Extensible Firmware Interface (UEFI) specification
1694 * for details.
1695 *
1696 * @count returned value of the counter
1697 * @return status code
1698 */
Alexander Grafbee91162016-03-04 01:09:59 +01001699static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1700{
1701 static uint64_t mono = 0;
1702 EFI_ENTRY("%p", count);
1703 *count = mono++;
1704 return EFI_EXIT(EFI_SUCCESS);
1705}
1706
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001707/*
1708 * Sleep.
1709 *
1710 * This function implements the Stall sercive.
1711 * See the Unified Extensible Firmware Interface (UEFI) specification
1712 * for details.
1713 *
1714 * @microseconds period to sleep in microseconds
1715 * @return status code
1716 */
Alexander Grafbee91162016-03-04 01:09:59 +01001717static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1718{
1719 EFI_ENTRY("%ld", microseconds);
1720 udelay(microseconds);
1721 return EFI_EXIT(EFI_SUCCESS);
1722}
1723
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001724/*
1725 * Reset the watchdog timer.
1726 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001727 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001728 * See the Unified Extensible Firmware Interface (UEFI) specification
1729 * for details.
1730 *
1731 * @timeout seconds before reset by watchdog
1732 * @watchdog_code code to be logged when resetting
1733 * @data_size size of buffer in bytes
1734 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001735 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001736 */
Alexander Grafbee91162016-03-04 01:09:59 +01001737static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1738 uint64_t watchdog_code,
1739 unsigned long data_size,
1740 uint16_t *watchdog_data)
1741{
1742 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1743 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001744 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001745}
1746
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001747/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001748 * Close a protocol.
1749 *
1750 * This function implements the CloseProtocol service.
1751 * See the Unified Extensible Firmware Interface (UEFI) specification
1752 * for details.
1753 *
1754 * @handle handle on which the protocol shall be closed
1755 * @protocol GUID of the protocol to close
1756 * @agent_handle handle of the driver
1757 * @controller_handle handle of the controller
1758 * @return status code
1759 */
Alexander Grafbee91162016-03-04 01:09:59 +01001760static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001761 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001762 void *agent_handle,
1763 void *controller_handle)
1764{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001765 struct efi_handler *handler;
1766 struct efi_open_protocol_info_item *item;
1767 struct efi_open_protocol_info_item *pos;
1768 efi_status_t r;
1769
Rob Clark778e6af2017-09-13 18:05:41 -04001770 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001771 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001772
1773 if (!agent_handle) {
1774 r = EFI_INVALID_PARAMETER;
1775 goto out;
1776 }
1777 r = efi_search_protocol(handle, protocol, &handler);
1778 if (r != EFI_SUCCESS)
1779 goto out;
1780
1781 r = EFI_NOT_FOUND;
1782 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1783 if (item->info.agent_handle == agent_handle &&
1784 item->info.controller_handle == controller_handle) {
1785 efi_delete_open_info(item);
1786 r = EFI_SUCCESS;
1787 break;
1788 }
1789 }
1790out:
1791 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001792}
1793
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001794/*
1795 * Provide information about then open status of a protocol on a handle
1796 *
1797 * This function implements the OpenProtocolInformation service.
1798 * See the Unified Extensible Firmware Interface (UEFI) specification
1799 * for details.
1800 *
1801 * @handle handle for which the information shall be retrieved
1802 * @protocol GUID of the protocol
1803 * @entry_buffer buffer to receive the open protocol information
1804 * @entry_count number of entries available in the buffer
1805 * @return status code
1806 */
Alexander Grafbee91162016-03-04 01:09:59 +01001807static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001808 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001809 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001810 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001811{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001812 unsigned long buffer_size;
1813 unsigned long count;
1814 struct efi_handler *handler;
1815 struct efi_open_protocol_info_item *item;
1816 efi_status_t r;
1817
Rob Clark778e6af2017-09-13 18:05:41 -04001818 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001819 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001820
1821 /* Check parameters */
1822 if (!entry_buffer) {
1823 r = EFI_INVALID_PARAMETER;
1824 goto out;
1825 }
1826 r = efi_search_protocol(handle, protocol, &handler);
1827 if (r != EFI_SUCCESS)
1828 goto out;
1829
1830 /* Count entries */
1831 count = 0;
1832 list_for_each_entry(item, &handler->open_infos, link) {
1833 if (item->info.open_count)
1834 ++count;
1835 }
1836 *entry_count = count;
1837 *entry_buffer = NULL;
1838 if (!count) {
1839 r = EFI_SUCCESS;
1840 goto out;
1841 }
1842
1843 /* Copy entries */
1844 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1845 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1846 (void **)entry_buffer);
1847 if (r != EFI_SUCCESS)
1848 goto out;
1849 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1850 if (item->info.open_count)
1851 (*entry_buffer)[--count] = item->info;
1852 }
1853out:
1854 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001855}
1856
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001857/*
1858 * Get protocols installed on a handle.
1859 *
1860 * This function implements the ProtocolsPerHandleService.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification
1862 * for details.
1863 *
1864 * @handle handle for which the information is retrieved
1865 * @protocol_buffer buffer with protocol GUIDs
1866 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001867 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001868 */
Alexander Grafbee91162016-03-04 01:09:59 +01001869static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1870 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001871 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001872{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001873 unsigned long buffer_size;
1874 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001875 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001876 efi_status_t r;
1877
Alexander Grafbee91162016-03-04 01:09:59 +01001878 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1879 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001880
1881 if (!handle || !protocol_buffer || !protocol_buffer_count)
1882 return EFI_EXIT(EFI_INVALID_PARAMETER);
1883
1884 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001885 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001886
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001887 efiobj = efi_search_obj(handle);
1888 if (!efiobj)
1889 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001890
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001891 /* Count protocols */
1892 list_for_each(protocol_handle, &efiobj->protocols) {
1893 ++*protocol_buffer_count;
1894 }
1895
1896 /* Copy guids */
1897 if (*protocol_buffer_count) {
1898 size_t j = 0;
1899
1900 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1901 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1902 (void **)protocol_buffer);
1903 if (r != EFI_SUCCESS)
1904 return EFI_EXIT(r);
1905 list_for_each(protocol_handle, &efiobj->protocols) {
1906 struct efi_handler *protocol;
1907
1908 protocol = list_entry(protocol_handle,
1909 struct efi_handler, link);
1910 (*protocol_buffer)[j] = (void *)protocol->guid;
1911 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001912 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001913 }
1914
1915 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001916}
1917
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001918/*
1919 * Locate handles implementing a protocol.
1920 *
1921 * This function implements the LocateHandleBuffer service.
1922 * See the Unified Extensible Firmware Interface (UEFI) specification
1923 * for details.
1924 *
1925 * @search_type selection criterion
1926 * @protocol GUID of the protocol
1927 * @search_key registration key
1928 * @no_handles number of returned handles
1929 * @buffer buffer with the returned handles
1930 * @return status code
1931 */
Alexander Grafbee91162016-03-04 01:09:59 +01001932static efi_status_t EFIAPI efi_locate_handle_buffer(
1933 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001934 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001935 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001936{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001937 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001938 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001939
Rob Clark778e6af2017-09-13 18:05:41 -04001940 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001941 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001942
1943 if (!no_handles || !buffer) {
1944 r = EFI_INVALID_PARAMETER;
1945 goto out;
1946 }
1947 *no_handles = 0;
1948 *buffer = NULL;
1949 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1950 *buffer);
1951 if (r != EFI_BUFFER_TOO_SMALL)
1952 goto out;
1953 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1954 (void **)buffer);
1955 if (r != EFI_SUCCESS)
1956 goto out;
1957 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1958 *buffer);
1959 if (r == EFI_SUCCESS)
1960 *no_handles = buffer_size / sizeof(void *);
1961out:
1962 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001963}
1964
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001965/*
1966 * Find an interface implementing a protocol.
1967 *
1968 * This function implements the LocateProtocol service.
1969 * See the Unified Extensible Firmware Interface (UEFI) specification
1970 * for details.
1971 *
1972 * @protocol GUID of the protocol
1973 * @registration registration key passed to the notification function
1974 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001975 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001976 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001977static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001978 void *registration,
1979 void **protocol_interface)
1980{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001981 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001982 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001983
Rob Clark778e6af2017-09-13 18:05:41 -04001984 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001985
1986 if (!protocol || !protocol_interface)
1987 return EFI_EXIT(EFI_INVALID_PARAMETER);
1988
1989 list_for_each(lhandle, &efi_obj_list) {
1990 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001991 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001992
1993 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001994
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001995 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1996 if (ret == EFI_SUCCESS) {
1997 *protocol_interface = handler->protocol_interface;
1998 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001999 }
2000 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002001 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002002
2003 return EFI_EXIT(EFI_NOT_FOUND);
2004}
2005
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002006/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002007 * Get the device path and handle of an device implementing a protocol.
2008 *
2009 * This function implements the LocateDevicePath service.
2010 * See the Unified Extensible Firmware Interface (UEFI) specification
2011 * for details.
2012 *
2013 * @protocol GUID of the protocol
2014 * @device_path device path
2015 * @device handle of the device
2016 * @return status code
2017 */
2018static efi_status_t EFIAPI efi_locate_device_path(
2019 const efi_guid_t *protocol,
2020 struct efi_device_path **device_path,
2021 efi_handle_t *device)
2022{
2023 struct efi_device_path *dp;
2024 size_t i;
2025 struct efi_handler *handler;
2026 efi_handle_t *handles;
2027 size_t len, len_dp;
2028 size_t len_best = 0;
2029 efi_uintn_t no_handles;
2030 u8 *remainder;
2031 efi_status_t ret;
2032
2033 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2034
2035 if (!protocol || !device_path || !*device_path || !device) {
2036 ret = EFI_INVALID_PARAMETER;
2037 goto out;
2038 }
2039
2040 /* Find end of device path */
2041 len = efi_dp_size(*device_path);
2042
2043 /* Get all handles implementing the protocol */
2044 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2045 &no_handles, &handles));
2046 if (ret != EFI_SUCCESS)
2047 goto out;
2048
2049 for (i = 0; i < no_handles; ++i) {
2050 /* Find the device path protocol */
2051 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2052 &handler);
2053 if (ret != EFI_SUCCESS)
2054 continue;
2055 dp = (struct efi_device_path *)handler->protocol_interface;
2056 len_dp = efi_dp_size(dp);
2057 /*
2058 * This handle can only be a better fit
2059 * if its device path length is longer than the best fit and
2060 * if its device path length is shorter of equal the searched
2061 * device path.
2062 */
2063 if (len_dp <= len_best || len_dp > len)
2064 continue;
2065 /* Check if dp is a subpath of device_path */
2066 if (memcmp(*device_path, dp, len_dp))
2067 continue;
2068 *device = handles[i];
2069 len_best = len_dp;
2070 }
2071 if (len_best) {
2072 remainder = (u8 *)*device_path + len_best;
2073 *device_path = (struct efi_device_path *)remainder;
2074 ret = EFI_SUCCESS;
2075 } else {
2076 ret = EFI_NOT_FOUND;
2077 }
2078out:
2079 return EFI_EXIT(ret);
2080}
2081
2082/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002083 * Install multiple protocol interfaces.
2084 *
2085 * This function implements the MultipleProtocolInterfaces service.
2086 * See the Unified Extensible Firmware Interface (UEFI) specification
2087 * for details.
2088 *
2089 * @handle handle on which the protocol interfaces shall be installed
2090 * @... NULL terminated argument list with pairs of protocol GUIDS and
2091 * interfaces
2092 * @return status code
2093 */
Alexander Grafbee91162016-03-04 01:09:59 +01002094static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2095 void **handle, ...)
2096{
2097 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002098
2099 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002100 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002101 void *protocol_interface;
2102 efi_status_t r = EFI_SUCCESS;
2103 int i = 0;
2104
2105 if (!handle)
2106 return EFI_EXIT(EFI_INVALID_PARAMETER);
2107
2108 va_start(argptr, handle);
2109 for (;;) {
2110 protocol = va_arg(argptr, efi_guid_t*);
2111 if (!protocol)
2112 break;
2113 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002114 r = EFI_CALL(efi_install_protocol_interface(
2115 handle, protocol,
2116 EFI_NATIVE_INTERFACE,
2117 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002118 if (r != EFI_SUCCESS)
2119 break;
2120 i++;
2121 }
2122 va_end(argptr);
2123 if (r == EFI_SUCCESS)
2124 return EFI_EXIT(r);
2125
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002126 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002127 va_start(argptr, handle);
2128 for (; i; --i) {
2129 protocol = va_arg(argptr, efi_guid_t*);
2130 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002131 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2132 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002133 }
2134 va_end(argptr);
2135
2136 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002137}
2138
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002139/*
2140 * Uninstall multiple protocol interfaces.
2141 *
2142 * This function implements the UninstallMultipleProtocolInterfaces service.
2143 * See the Unified Extensible Firmware Interface (UEFI) specification
2144 * for details.
2145 *
2146 * @handle handle from which the protocol interfaces shall be removed
2147 * @... NULL terminated argument list with pairs of protocol GUIDS and
2148 * interfaces
2149 * @return status code
2150 */
Alexander Grafbee91162016-03-04 01:09:59 +01002151static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2152 void *handle, ...)
2153{
2154 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002155
2156 va_list argptr;
2157 const efi_guid_t *protocol;
2158 void *protocol_interface;
2159 efi_status_t r = EFI_SUCCESS;
2160 size_t i = 0;
2161
2162 if (!handle)
2163 return EFI_EXIT(EFI_INVALID_PARAMETER);
2164
2165 va_start(argptr, handle);
2166 for (;;) {
2167 protocol = va_arg(argptr, efi_guid_t*);
2168 if (!protocol)
2169 break;
2170 protocol_interface = va_arg(argptr, void*);
2171 r = EFI_CALL(efi_uninstall_protocol_interface(
2172 handle, protocol,
2173 protocol_interface));
2174 if (r != EFI_SUCCESS)
2175 break;
2176 i++;
2177 }
2178 va_end(argptr);
2179 if (r == EFI_SUCCESS)
2180 return EFI_EXIT(r);
2181
2182 /* If an error occurred undo all changes. */
2183 va_start(argptr, handle);
2184 for (; i; --i) {
2185 protocol = va_arg(argptr, efi_guid_t*);
2186 protocol_interface = va_arg(argptr, void*);
2187 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2188 EFI_NATIVE_INTERFACE,
2189 protocol_interface));
2190 }
2191 va_end(argptr);
2192
2193 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002194}
2195
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002196/*
2197 * Calculate cyclic redundancy code.
2198 *
2199 * This function implements the CalculateCrc32 service.
2200 * See the Unified Extensible Firmware Interface (UEFI) specification
2201 * for details.
2202 *
2203 * @data buffer with data
2204 * @data_size size of buffer in bytes
2205 * @crc32_p cyclic redundancy code
2206 * @return status code
2207 */
Alexander Grafbee91162016-03-04 01:09:59 +01002208static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2209 unsigned long data_size,
2210 uint32_t *crc32_p)
2211{
2212 EFI_ENTRY("%p, %ld", data, data_size);
2213 *crc32_p = crc32(0, data, data_size);
2214 return EFI_EXIT(EFI_SUCCESS);
2215}
2216
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002217/*
2218 * Copy memory.
2219 *
2220 * This function implements the CopyMem service.
2221 * See the Unified Extensible Firmware Interface (UEFI) specification
2222 * for details.
2223 *
2224 * @destination destination of the copy operation
2225 * @source source of the copy operation
2226 * @length number of bytes to copy
2227 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002228static void EFIAPI efi_copy_mem(void *destination, const void *source,
2229 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002230{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002231 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002232 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002233 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002234}
2235
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002236/*
2237 * Fill memory with a byte value.
2238 *
2239 * This function implements the SetMem service.
2240 * See the Unified Extensible Firmware Interface (UEFI) specification
2241 * for details.
2242 *
2243 * @buffer buffer to fill
2244 * @size size of buffer in bytes
2245 * @value byte to copy to the buffer
2246 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002247static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002248{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002249 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002250 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002251 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002252}
2253
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002254/*
2255 * Open protocol interface on a handle.
2256 *
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002257 * @handler handler of a protocol
2258 * @protocol_interface interface implementing the protocol
2259 * @agent_handle handle of the driver
2260 * @controller_handle handle of the controller
2261 * @attributes attributes indicating how to open the protocol
2262 * @return status code
2263 */
2264static efi_status_t efi_protocol_open(
2265 struct efi_handler *handler,
2266 void **protocol_interface, void *agent_handle,
2267 void *controller_handle, uint32_t attributes)
2268{
2269 struct efi_open_protocol_info_item *item;
2270 struct efi_open_protocol_info_entry *match = NULL;
2271 bool opened_by_driver = false;
2272 bool opened_exclusive = false;
2273
2274 /* If there is no agent, only return the interface */
2275 if (!agent_handle)
2276 goto out;
2277
2278 /* For TEST_PROTOCOL ignore interface attribute */
2279 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2280 *protocol_interface = NULL;
2281
2282 /*
2283 * Check if the protocol is already opened by a driver with the same
2284 * attributes or opened exclusively
2285 */
2286 list_for_each_entry(item, &handler->open_infos, link) {
2287 if (item->info.agent_handle == agent_handle) {
2288 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2289 (item->info.attributes == attributes))
2290 return EFI_ALREADY_STARTED;
2291 }
2292 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2293 opened_exclusive = true;
2294 }
2295
2296 /* Only one controller can open the protocol exclusively */
2297 if (opened_exclusive && attributes &
2298 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2299 return EFI_ACCESS_DENIED;
2300
2301 /* Prepare exclusive opening */
2302 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2303 /* Try to disconnect controllers */
2304 list_for_each_entry(item, &handler->open_infos, link) {
2305 if (item->info.attributes ==
2306 EFI_OPEN_PROTOCOL_BY_DRIVER)
2307 EFI_CALL(efi_disconnect_controller(
2308 item->info.controller_handle,
2309 item->info.agent_handle,
2310 NULL));
2311 }
2312 opened_by_driver = false;
2313 /* Check if all controllers are disconnected */
2314 list_for_each_entry(item, &handler->open_infos, link) {
2315 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2316 opened_by_driver = true;
2317 }
2318 /* Only one controller can be conncected */
2319 if (opened_by_driver)
2320 return EFI_ACCESS_DENIED;
2321 }
2322
2323 /* Find existing entry */
2324 list_for_each_entry(item, &handler->open_infos, link) {
2325 if (item->info.agent_handle == agent_handle &&
2326 item->info.controller_handle == controller_handle)
2327 match = &item->info;
2328 }
2329 /* None found, create one */
2330 if (!match) {
2331 match = efi_create_open_info(handler);
2332 if (!match)
2333 return EFI_OUT_OF_RESOURCES;
2334 }
2335
2336 match->agent_handle = agent_handle;
2337 match->controller_handle = controller_handle;
2338 match->attributes = attributes;
2339 match->open_count++;
2340
2341out:
2342 /* For TEST_PROTOCOL ignore interface attribute. */
2343 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2344 *protocol_interface = handler->protocol_interface;
2345
2346 return EFI_SUCCESS;
2347}
2348
2349/*
2350 * Open protocol interface on a handle.
2351 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002352 * This function implements the OpenProtocol interface.
2353 * See the Unified Extensible Firmware Interface (UEFI) specification
2354 * for details.
2355 *
2356 * @handle handle on which the protocol shall be opened
2357 * @protocol GUID of the protocol
2358 * @protocol_interface interface implementing the protocol
2359 * @agent_handle handle of the driver
2360 * @controller_handle handle of the controller
2361 * @attributes attributes indicating how to open the protocol
2362 * @return status code
2363 */
Alexander Grafbee91162016-03-04 01:09:59 +01002364static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002365 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002366 void **protocol_interface, void *agent_handle,
2367 void *controller_handle, uint32_t attributes)
2368{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002369 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002370 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002371
Rob Clark778e6af2017-09-13 18:05:41 -04002372 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002373 protocol_interface, agent_handle, controller_handle,
2374 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002375
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002376 if (!handle || !protocol ||
2377 (!protocol_interface && attributes !=
2378 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2379 goto out;
2380 }
2381
2382 switch (attributes) {
2383 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2384 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2385 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2386 break;
2387 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2388 if (controller_handle == handle)
2389 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002390 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002391 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2392 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002393 /* Check that the controller handle is valid */
2394 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002395 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002396 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002397 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002398 /* Check that the agent handle is valid */
2399 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002400 goto out;
2401 break;
2402 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002403 goto out;
2404 }
2405
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002406 r = efi_search_protocol(handle, protocol, &handler);
2407 if (r != EFI_SUCCESS)
2408 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002409
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002410 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2411 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002412out:
2413 return EFI_EXIT(r);
2414}
2415
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002416/*
2417 * Get interface of a protocol on a handle.
2418 *
2419 * This function implements the HandleProtocol service.
2420 * See the Unified Extensible Firmware Interface (UEFI) specification
2421 * for details.
2422 *
2423 * @handle handle on which the protocol shall be opened
2424 * @protocol GUID of the protocol
2425 * @protocol_interface interface implementing the protocol
2426 * @return status code
2427 */
Alexander Grafbee91162016-03-04 01:09:59 +01002428static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002429 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002430 void **protocol_interface)
2431{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002432 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2433 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002434}
2435
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002436static efi_status_t efi_bind_controller(
2437 efi_handle_t controller_handle,
2438 efi_handle_t driver_image_handle,
2439 struct efi_device_path *remain_device_path)
2440{
2441 struct efi_driver_binding_protocol *binding_protocol;
2442 efi_status_t r;
2443
2444 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2445 &efi_guid_driver_binding_protocol,
2446 (void **)&binding_protocol,
2447 driver_image_handle, NULL,
2448 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2449 if (r != EFI_SUCCESS)
2450 return r;
2451 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2452 controller_handle,
2453 remain_device_path));
2454 if (r == EFI_SUCCESS)
2455 r = EFI_CALL(binding_protocol->start(binding_protocol,
2456 controller_handle,
2457 remain_device_path));
2458 EFI_CALL(efi_close_protocol(driver_image_handle,
2459 &efi_guid_driver_binding_protocol,
2460 driver_image_handle, NULL));
2461 return r;
2462}
2463
2464static efi_status_t efi_connect_single_controller(
2465 efi_handle_t controller_handle,
2466 efi_handle_t *driver_image_handle,
2467 struct efi_device_path *remain_device_path)
2468{
2469 efi_handle_t *buffer;
2470 size_t count;
2471 size_t i;
2472 efi_status_t r;
2473 size_t connected = 0;
2474
2475 /* Get buffer with all handles with driver binding protocol */
2476 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2477 &efi_guid_driver_binding_protocol,
2478 NULL, &count, &buffer));
2479 if (r != EFI_SUCCESS)
2480 return r;
2481
2482 /* Context Override */
2483 if (driver_image_handle) {
2484 for (; *driver_image_handle; ++driver_image_handle) {
2485 for (i = 0; i < count; ++i) {
2486 if (buffer[i] == *driver_image_handle) {
2487 buffer[i] = NULL;
2488 r = efi_bind_controller(
2489 controller_handle,
2490 *driver_image_handle,
2491 remain_device_path);
2492 /*
2493 * For drivers that do not support the
2494 * controller or are already connected
2495 * we receive an error code here.
2496 */
2497 if (r == EFI_SUCCESS)
2498 ++connected;
2499 }
2500 }
2501 }
2502 }
2503
2504 /*
2505 * TODO: Some overrides are not yet implemented:
2506 * - Platform Driver Override
2507 * - Driver Family Override Search
2508 * - Bus Specific Driver Override
2509 */
2510
2511 /* Driver Binding Search */
2512 for (i = 0; i < count; ++i) {
2513 if (buffer[i]) {
2514 r = efi_bind_controller(controller_handle,
2515 buffer[i],
2516 remain_device_path);
2517 if (r == EFI_SUCCESS)
2518 ++connected;
2519 }
2520 }
2521
2522 efi_free_pool(buffer);
2523 if (!connected)
2524 return EFI_NOT_FOUND;
2525 return EFI_SUCCESS;
2526}
2527
2528/*
2529 * Connect a controller to a driver.
2530 *
2531 * This function implements the ConnectController service.
2532 * See the Unified Extensible Firmware Interface (UEFI) specification
2533 * for details.
2534 *
2535 * First all driver binding protocol handles are tried for binding drivers.
2536 * Afterwards all handles that have openened a protocol of the controller
2537 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2538 *
2539 * @controller_handle handle of the controller
2540 * @driver_image_handle handle of the driver
2541 * @remain_device_path device path of a child controller
2542 * @recursive true to connect all child controllers
2543 * @return status code
2544 */
2545static efi_status_t EFIAPI efi_connect_controller(
2546 efi_handle_t controller_handle,
2547 efi_handle_t *driver_image_handle,
2548 struct efi_device_path *remain_device_path,
2549 bool recursive)
2550{
2551 efi_status_t r;
2552 efi_status_t ret = EFI_NOT_FOUND;
2553 struct efi_object *efiobj;
2554
2555 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2556 remain_device_path, recursive);
2557
2558 efiobj = efi_search_obj(controller_handle);
2559 if (!efiobj) {
2560 ret = EFI_INVALID_PARAMETER;
2561 goto out;
2562 }
2563
2564 r = efi_connect_single_controller(controller_handle,
2565 driver_image_handle,
2566 remain_device_path);
2567 if (r == EFI_SUCCESS)
2568 ret = EFI_SUCCESS;
2569 if (recursive) {
2570 struct efi_handler *handler;
2571 struct efi_open_protocol_info_item *item;
2572
2573 list_for_each_entry(handler, &efiobj->protocols, link) {
2574 list_for_each_entry(item, &handler->open_infos, link) {
2575 if (item->info.attributes &
2576 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2577 r = EFI_CALL(efi_connect_controller(
2578 item->info.controller_handle,
2579 driver_image_handle,
2580 remain_device_path,
2581 recursive));
2582 if (r == EFI_SUCCESS)
2583 ret = EFI_SUCCESS;
2584 }
2585 }
2586 }
2587 }
2588 /* Check for child controller specified by end node */
2589 if (ret != EFI_SUCCESS && remain_device_path &&
2590 remain_device_path->type == DEVICE_PATH_TYPE_END)
2591 ret = EFI_SUCCESS;
2592out:
2593 return EFI_EXIT(ret);
2594}
2595
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002596/*
2597 * Get all child controllers associated to a driver.
2598 * The allocated buffer has to be freed with free().
2599 *
2600 * @efiobj handle of the controller
2601 * @driver_handle handle of the driver
2602 * @number_of_children number of child controllers
2603 * @child_handle_buffer handles of the the child controllers
2604 */
2605static efi_status_t efi_get_child_controllers(
2606 struct efi_object *efiobj,
2607 efi_handle_t driver_handle,
2608 efi_uintn_t *number_of_children,
2609 efi_handle_t **child_handle_buffer)
2610{
2611 struct efi_handler *handler;
2612 struct efi_open_protocol_info_item *item;
2613 efi_uintn_t count = 0, i;
2614 bool duplicate;
2615
2616 /* Count all child controller associations */
2617 list_for_each_entry(handler, &efiobj->protocols, link) {
2618 list_for_each_entry(item, &handler->open_infos, link) {
2619 if (item->info.agent_handle == driver_handle &&
2620 item->info.attributes &
2621 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2622 ++count;
2623 }
2624 }
2625 /*
2626 * Create buffer. In case of duplicate child controller assignments
2627 * the buffer will be too large. But that does not harm.
2628 */
2629 *number_of_children = 0;
2630 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2631 if (!*child_handle_buffer)
2632 return EFI_OUT_OF_RESOURCES;
2633 /* Copy unique child handles */
2634 list_for_each_entry(handler, &efiobj->protocols, link) {
2635 list_for_each_entry(item, &handler->open_infos, link) {
2636 if (item->info.agent_handle == driver_handle &&
2637 item->info.attributes &
2638 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2639 /* Check this is a new child controller */
2640 duplicate = false;
2641 for (i = 0; i < *number_of_children; ++i) {
2642 if ((*child_handle_buffer)[i] ==
2643 item->info.controller_handle)
2644 duplicate = true;
2645 }
2646 /* Copy handle to buffer */
2647 if (!duplicate) {
2648 i = (*number_of_children)++;
2649 (*child_handle_buffer)[i] =
2650 item->info.controller_handle;
2651 }
2652 }
2653 }
2654 }
2655 return EFI_SUCCESS;
2656}
2657
2658/*
2659 * Disconnect a controller from a driver.
2660 *
2661 * This function implements the DisconnectController service.
2662 * See the Unified Extensible Firmware Interface (UEFI) specification
2663 * for details.
2664 *
2665 * @controller_handle handle of the controller
2666 * @driver_image_handle handle of the driver
2667 * @child_handle handle of the child to destroy
2668 * @return status code
2669 */
2670static efi_status_t EFIAPI efi_disconnect_controller(
2671 efi_handle_t controller_handle,
2672 efi_handle_t driver_image_handle,
2673 efi_handle_t child_handle)
2674{
2675 struct efi_driver_binding_protocol *binding_protocol;
2676 efi_handle_t *child_handle_buffer = NULL;
2677 size_t number_of_children = 0;
2678 efi_status_t r;
2679 size_t stop_count = 0;
2680 struct efi_object *efiobj;
2681
2682 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2683 child_handle);
2684
2685 efiobj = efi_search_obj(controller_handle);
2686 if (!efiobj) {
2687 r = EFI_INVALID_PARAMETER;
2688 goto out;
2689 }
2690
2691 if (child_handle && !efi_search_obj(child_handle)) {
2692 r = EFI_INVALID_PARAMETER;
2693 goto out;
2694 }
2695
2696 /* If no driver handle is supplied, disconnect all drivers */
2697 if (!driver_image_handle) {
2698 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2699 goto out;
2700 }
2701
2702 /* Create list of child handles */
2703 if (child_handle) {
2704 number_of_children = 1;
2705 child_handle_buffer = &child_handle;
2706 } else {
2707 efi_get_child_controllers(efiobj,
2708 driver_image_handle,
2709 &number_of_children,
2710 &child_handle_buffer);
2711 }
2712
2713 /* Get the driver binding protocol */
2714 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2715 &efi_guid_driver_binding_protocol,
2716 (void **)&binding_protocol,
2717 driver_image_handle, NULL,
2718 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2719 if (r != EFI_SUCCESS)
2720 goto out;
2721 /* Remove the children */
2722 if (number_of_children) {
2723 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2724 controller_handle,
2725 number_of_children,
2726 child_handle_buffer));
2727 if (r == EFI_SUCCESS)
2728 ++stop_count;
2729 }
2730 /* Remove the driver */
2731 if (!child_handle)
2732 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2733 controller_handle,
2734 0, NULL));
2735 if (r == EFI_SUCCESS)
2736 ++stop_count;
2737 EFI_CALL(efi_close_protocol(driver_image_handle,
2738 &efi_guid_driver_binding_protocol,
2739 driver_image_handle, NULL));
2740
2741 if (stop_count)
2742 r = EFI_SUCCESS;
2743 else
2744 r = EFI_NOT_FOUND;
2745out:
2746 if (!child_handle)
2747 free(child_handle_buffer);
2748 return EFI_EXIT(r);
2749}
2750
Alexander Grafbee91162016-03-04 01:09:59 +01002751static const struct efi_boot_services efi_boot_services = {
2752 .hdr = {
2753 .headersize = sizeof(struct efi_table_hdr),
2754 },
2755 .raise_tpl = efi_raise_tpl,
2756 .restore_tpl = efi_restore_tpl,
2757 .allocate_pages = efi_allocate_pages_ext,
2758 .free_pages = efi_free_pages_ext,
2759 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002760 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002761 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002762 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002763 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002764 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002765 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002766 .close_event = efi_close_event,
2767 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002768 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002769 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002770 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002771 .handle_protocol = efi_handle_protocol,
2772 .reserved = NULL,
2773 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002774 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002775 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002776 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002777 .load_image = efi_load_image,
2778 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002779 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002780 .unload_image = efi_unload_image,
2781 .exit_boot_services = efi_exit_boot_services,
2782 .get_next_monotonic_count = efi_get_next_monotonic_count,
2783 .stall = efi_stall,
2784 .set_watchdog_timer = efi_set_watchdog_timer,
2785 .connect_controller = efi_connect_controller,
2786 .disconnect_controller = efi_disconnect_controller,
2787 .open_protocol = efi_open_protocol,
2788 .close_protocol = efi_close_protocol,
2789 .open_protocol_information = efi_open_protocol_information,
2790 .protocols_per_handle = efi_protocols_per_handle,
2791 .locate_handle_buffer = efi_locate_handle_buffer,
2792 .locate_protocol = efi_locate_protocol,
2793 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2794 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2795 .calculate_crc32 = efi_calculate_crc32,
2796 .copy_mem = efi_copy_mem,
2797 .set_mem = efi_set_mem,
2798};
2799
2800
Heinrich Schuchardt05b6f562017-12-11 20:10:20 +01002801static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01002802
Alexander Graf3c63db92016-10-14 13:45:30 +02002803struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002804 .hdr = {
2805 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2806 .revision = 0x20005, /* 2.5 */
2807 .headersize = sizeof(struct efi_table_hdr),
2808 },
2809 .fw_vendor = (long)firmware_vendor,
2810 .con_in = (void*)&efi_con_in,
2811 .con_out = (void*)&efi_con_out,
2812 .std_err = (void*)&efi_con_out,
2813 .runtime = (void*)&efi_runtime_services,
2814 .boottime = (void*)&efi_boot_services,
2815 .nr_tables = 0,
2816 .tables = (void*)efi_conf_table,
2817};