blob: ff11d028da8a2e8068ce1e90f2d291f1bbf18e32 [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 Schuchardt2074f702018-01-11 08:16:09 +010063static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010067
Rob Clarkc160d2f2017-07-27 08:04:18 -040068/* Called on every callback entry */
69int __efi_entry_check(void)
70{
71 int ret = entry_count++ == 0;
72#ifdef CONFIG_ARM
73 assert(efi_gd);
74 app_gd = gd;
75 gd = efi_gd;
76#endif
77 return ret;
78}
79
80/* Called on every callback exit */
81int __efi_exit_check(void)
82{
83 int ret = --entry_count == 0;
84#ifdef CONFIG_ARM
85 gd = app_gd;
86#endif
87 return ret;
88}
89
Alexander Grafbee91162016-03-04 01:09:59 +010090/* Called from do_bootefi_exec() */
91void efi_save_gd(void)
92{
Simon Glass65e4c0b2016-09-25 15:27:35 -060093#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010094 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060095#endif
Alexander Grafbee91162016-03-04 01:09:59 +010096}
97
Rob Clarkc160d2f2017-07-27 08:04:18 -040098/*
99 * Special case handler for error/abort that just forces things back
100 * to u-boot world so we can dump out an abort msg, without any care
101 * about returning back to UEFI world.
102 */
Alexander Grafbee91162016-03-04 01:09:59 +0100103void efi_restore_gd(void)
104{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100106 /* Only restore if we're already in EFI context */
107 if (!efi_gd)
108 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100109 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600110#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100111}
112
Rob Clarkaf65db82017-07-27 08:04:19 -0400113/*
114 * Two spaces per indent level, maxing out at 10.. which ought to be
115 * enough for anyone ;-)
116 */
117static const char *indent_string(int level)
118{
119 const char *indent = " ";
120 const int max = strlen(indent);
121 level = min(max, level * 2);
122 return &indent[max - level];
123}
124
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200125const char *__efi_nesting(void)
126{
127 return indent_string(nesting_level);
128}
129
Rob Clarkaf65db82017-07-27 08:04:19 -0400130const char *__efi_nesting_inc(void)
131{
132 return indent_string(nesting_level++);
133}
134
135const char *__efi_nesting_dec(void)
136{
137 return indent_string(--nesting_level);
138}
139
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200140/*
141 * Queue an EFI event.
142 *
143 * This function queues the notification function of the event for future
144 * execution.
145 *
146 * The notification function is called if the task priority level of the
147 * event is higher than the current task priority level.
148 *
149 * For the SignalEvent service see efi_signal_event_ext.
150 *
151 * @event event to signal
152 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200153void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200154{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200155 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200156 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200157 /* Check TPL */
158 if (efi_tpl >= event->notify_tpl)
159 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200160 EFI_CALL_VOID(event->notify_function(event,
161 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200162 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200163 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200164}
165
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200166/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200167 * Raise the task priority level.
168 *
169 * This function implements the RaiseTpl service.
170 * See the Unified Extensible Firmware Interface (UEFI) specification
171 * for details.
172 *
173 * @new_tpl new value of the task priority level
174 * @return old value of the task priority level
175 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100176static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100177{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100178 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200179
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200180 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200181
182 if (new_tpl < efi_tpl)
183 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
184 efi_tpl = new_tpl;
185 if (efi_tpl > TPL_HIGH_LEVEL)
186 efi_tpl = TPL_HIGH_LEVEL;
187
188 EFI_EXIT(EFI_SUCCESS);
189 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100190}
191
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200192/*
193 * Lower the task priority level.
194 *
195 * This function implements the RestoreTpl service.
196 * See the Unified Extensible Firmware Interface (UEFI) specification
197 * for details.
198 *
199 * @old_tpl value of the task priority level to be restored
200 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100201static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100202{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200203 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200204
205 if (old_tpl > efi_tpl)
206 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
207 efi_tpl = old_tpl;
208 if (efi_tpl > TPL_HIGH_LEVEL)
209 efi_tpl = TPL_HIGH_LEVEL;
210
211 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100212}
213
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200214/*
215 * Allocate memory pages.
216 *
217 * This function implements the AllocatePages service.
218 * See the Unified Extensible Firmware Interface (UEFI) specification
219 * for details.
220 *
221 * @type type of allocation to be performed
222 * @memory_type usage type of the allocated memory
223 * @pages number of pages to be allocated
224 * @memory allocated memory
225 * @return status code
226 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900227static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100228 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900229 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100230{
231 efi_status_t r;
232
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100233 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100234 r = efi_allocate_pages(type, memory_type, pages, memory);
235 return EFI_EXIT(r);
236}
237
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200238/*
239 * Free memory pages.
240 *
241 * This function implements the FreePages service.
242 * See the Unified Extensible Firmware Interface (UEFI) specification
243 * for details.
244 *
245 * @memory start of the memory area to be freed
246 * @pages number of pages to be freed
247 * @return status code
248 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900249static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100250 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100251{
252 efi_status_t r;
253
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100254 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100255 r = efi_free_pages(memory, pages);
256 return EFI_EXIT(r);
257}
258
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200259/*
260 * Get map describing memory usage.
261 *
262 * This function implements the GetMemoryMap service.
263 * See the Unified Extensible Firmware Interface (UEFI) specification
264 * for details.
265 *
266 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
267 * on exit the size of the copied memory map
268 * @memory_map buffer to which the memory map is written
269 * @map_key key for the memory map
270 * @descriptor_size size of an individual memory descriptor
271 * @descriptor_version version number of the memory descriptor structure
272 * @return status code
273 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900274static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100275 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900276 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100277 efi_uintn_t *map_key,
278 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900279 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100280{
281 efi_status_t r;
282
283 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
284 map_key, descriptor_size, descriptor_version);
285 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
286 descriptor_size, descriptor_version);
287 return EFI_EXIT(r);
288}
289
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200290/*
291 * Allocate memory from pool.
292 *
293 * This function implements the AllocatePool service.
294 * See the Unified Extensible Firmware Interface (UEFI) specification
295 * for details.
296 *
297 * @pool_type type of the pool from which memory is to be allocated
298 * @size number of bytes to be allocated
299 * @buffer allocated memory
300 * @return status code
301 */
Stefan Brünsead12742016-10-09 22:17:18 +0200302static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100303 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200304 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100305{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100306 efi_status_t r;
307
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100308 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200309 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100310 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100311}
312
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200313/*
314 * Free memory from pool.
315 *
316 * This function implements the FreePool service.
317 * See the Unified Extensible Firmware Interface (UEFI) specification
318 * for details.
319 *
320 * @buffer start of memory to be freed
321 * @return status code
322 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200323static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100324{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100325 efi_status_t r;
326
327 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200328 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100329 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100330}
331
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200332/*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100333 * Add a new object to the object list.
334 *
335 * The protocols list is initialized.
336 * The object handle is set.
337 *
338 * @obj object to be added
339 */
340void efi_add_handle(struct efi_object *obj)
341{
342 if (!obj)
343 return;
344 INIT_LIST_HEAD(&obj->protocols);
345 obj->handle = obj;
346 list_add_tail(&obj->link, &efi_obj_list);
347}
348
349/*
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200350 * Create handle.
351 *
352 * @handle new handle
353 * @return status code
354 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100355efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200356{
357 struct efi_object *obj;
358 efi_status_t r;
359
360 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
361 sizeof(struct efi_object),
362 (void **)&obj);
363 if (r != EFI_SUCCESS)
364 return r;
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100365 efi_add_handle(obj);
366 *handle = obj->handle;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200367 return r;
368}
369
Alexander Grafbee91162016-03-04 01:09:59 +0100370/*
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100371 * Find a protocol on a handle.
372 *
373 * @handle handle
374 * @protocol_guid GUID of the protocol
375 * @handler reference to the protocol
376 * @return status code
377 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100378efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100379 const efi_guid_t *protocol_guid,
380 struct efi_handler **handler)
381{
382 struct efi_object *efiobj;
383 struct list_head *lhandle;
384
385 if (!handle || !protocol_guid)
386 return EFI_INVALID_PARAMETER;
387 efiobj = efi_search_obj(handle);
388 if (!efiobj)
389 return EFI_INVALID_PARAMETER;
390 list_for_each(lhandle, &efiobj->protocols) {
391 struct efi_handler *protocol;
392
393 protocol = list_entry(lhandle, struct efi_handler, link);
394 if (!guidcmp(protocol->guid, protocol_guid)) {
395 if (handler)
396 *handler = protocol;
397 return EFI_SUCCESS;
398 }
399 }
400 return EFI_NOT_FOUND;
401}
402
403/*
404 * Delete protocol from a handle.
405 *
406 * @handle handle from which the protocol shall be deleted
407 * @protocol GUID of the protocol to be deleted
408 * @protocol_interface interface of the protocol implementation
409 * @return status code
410 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100411efi_status_t efi_remove_protocol(const efi_handle_t handle,
412 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100413 void *protocol_interface)
414{
415 struct efi_handler *handler;
416 efi_status_t ret;
417
418 ret = efi_search_protocol(handle, protocol, &handler);
419 if (ret != EFI_SUCCESS)
420 return ret;
421 if (guidcmp(handler->guid, protocol))
422 return EFI_INVALID_PARAMETER;
423 list_del(&handler->link);
424 free(handler);
425 return EFI_SUCCESS;
426}
427
428/*
429 * Delete all protocols from a handle.
430 *
431 * @handle handle from which the protocols shall be deleted
432 * @return status code
433 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100434efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100435{
436 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100437 struct efi_handler *protocol;
438 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100439
440 efiobj = efi_search_obj(handle);
441 if (!efiobj)
442 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100443 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100444 efi_status_t ret;
445
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100446 ret = efi_remove_protocol(handle, protocol->guid,
447 protocol->protocol_interface);
448 if (ret != EFI_SUCCESS)
449 return ret;
450 }
451 return EFI_SUCCESS;
452}
453
454/*
455 * Delete handle.
456 *
457 * @handle handle to delete
458 */
459void efi_delete_handle(struct efi_object *obj)
460{
461 if (!obj)
462 return;
463 efi_remove_all_protocols(obj->handle);
464 list_del(&obj->link);
465 free(obj);
466}
467
468/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200469 * Our event capabilities are very limited. Only a small limited
470 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100471 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200472static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100473
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200474/*
475 * Create an event.
476 *
477 * This function is used inside U-Boot code to create an event.
478 *
479 * For the API function implementing the CreateEvent service see
480 * efi_create_event_ext.
481 *
482 * @type type of the event to create
483 * @notify_tpl task priority level of the event
484 * @notify_function notification function of the event
485 * @notify_context pointer passed to the notification function
486 * @event created event
487 * @return status code
488 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100489efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200490 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200491 struct efi_event *event,
492 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200493 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100494{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200495 int i;
496
Jonathan Graya95343b2017-03-12 19:26:07 +1100497 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200498 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100499
500 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200501 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100502
503 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
504 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200505 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100506
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200507 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
508 if (efi_events[i].type)
509 continue;
510 efi_events[i].type = type;
511 efi_events[i].notify_tpl = notify_tpl;
512 efi_events[i].notify_function = notify_function;
513 efi_events[i].notify_context = notify_context;
514 /* Disable timers on bootup */
515 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200516 efi_events[i].is_queued = false;
517 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200518 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200519 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200520 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200521 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100522}
523
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200524/*
525 * Create an event.
526 *
527 * This function implements the CreateEvent service.
528 * See the Unified Extensible Firmware Interface (UEFI) specification
529 * for details.
530 *
531 * @type type of the event to create
532 * @notify_tpl task priority level of the event
533 * @notify_function notification function of the event
534 * @notify_context pointer passed to the notification function
535 * @event created event
536 * @return status code
537 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200538static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100539 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200540 void (EFIAPI *notify_function) (
541 struct efi_event *event,
542 void *context),
543 void *notify_context, struct efi_event **event)
544{
545 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
546 notify_context);
547 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
548 notify_context, event));
549}
550
551
Alexander Grafbee91162016-03-04 01:09:59 +0100552/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200553 * Check if a timer event has occurred or a queued notification function should
554 * be called.
555 *
Alexander Grafbee91162016-03-04 01:09:59 +0100556 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200557 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100558 */
559void efi_timer_check(void)
560{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200561 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100562 u64 now = timer_get_us();
563
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200564 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200565 if (!efi_events[i].type)
566 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200567 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200568 efi_signal_event(&efi_events[i]);
569 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200570 now < efi_events[i].trigger_next)
571 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200572 switch (efi_events[i].trigger_type) {
573 case EFI_TIMER_RELATIVE:
574 efi_events[i].trigger_type = EFI_TIMER_STOP;
575 break;
576 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200577 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200578 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200579 break;
580 default:
581 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200582 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200583 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200584 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100585 }
Alexander Grafbee91162016-03-04 01:09:59 +0100586 WATCHDOG_RESET();
587}
588
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200589/*
590 * Set the trigger time for a timer event or stop the event.
591 *
592 * This is the function for internal usage in U-Boot. For the API function
593 * implementing the SetTimer service see efi_set_timer_ext.
594 *
595 * @event event for which the timer is set
596 * @type type of the timer
597 * @trigger_time trigger period in multiples of 100ns
598 * @return status code
599 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200600efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200601 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100602{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200603 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100604
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200605 /*
606 * The parameter defines a multiple of 100ns.
607 * We use multiples of 1000ns. So divide by 10.
608 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200609 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100610
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200611 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
612 if (event != &efi_events[i])
613 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100614
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200615 if (!(event->type & EVT_TIMER))
616 break;
617 switch (type) {
618 case EFI_TIMER_STOP:
619 event->trigger_next = -1ULL;
620 break;
621 case EFI_TIMER_PERIODIC:
622 case EFI_TIMER_RELATIVE:
623 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200624 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200625 break;
626 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200627 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200628 }
629 event->trigger_type = type;
630 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200631 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200632 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100633 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200634 return EFI_INVALID_PARAMETER;
635}
636
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200637/*
638 * Set the trigger time for a timer event or stop the event.
639 *
640 * This function implements the SetTimer service.
641 * See the Unified Extensible Firmware Interface (UEFI) specification
642 * for details.
643 *
644 * @event event for which the timer is set
645 * @type type of the timer
646 * @trigger_time trigger period in multiples of 100ns
647 * @return status code
648 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200649static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
650 enum efi_timer_delay type,
651 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200652{
653 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
654 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100655}
656
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200657/*
658 * Wait for events to be signaled.
659 *
660 * This function implements the WaitForEvent service.
661 * See the Unified Extensible Firmware Interface (UEFI) specification
662 * for details.
663 *
664 * @num_events number of events to be waited for
665 * @events events to be waited for
666 * @index index of the event that was signaled
667 * @return status code
668 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100669static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200670 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100671 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100672{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200673 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100674
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100675 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100676
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200677 /* Check parameters */
678 if (!num_events || !event)
679 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200680 /* Check TPL */
681 if (efi_tpl != TPL_APPLICATION)
682 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200683 for (i = 0; i < num_events; ++i) {
684 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
685 if (event[i] == &efi_events[j])
686 goto known_event;
687 }
688 return EFI_EXIT(EFI_INVALID_PARAMETER);
689known_event:
690 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
691 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200692 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200693 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200694 }
695
696 /* Wait for signal */
697 for (;;) {
698 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200699 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200700 goto out;
701 }
702 /* Allow events to occur. */
703 efi_timer_check();
704 }
705
706out:
707 /*
708 * Reset the signal which is passed to the caller to allow periodic
709 * events to occur.
710 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200711 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200712 if (index)
713 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100714
715 return EFI_EXIT(EFI_SUCCESS);
716}
717
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200718/*
719 * Signal an EFI event.
720 *
721 * This function implements the SignalEvent service.
722 * See the Unified Extensible Firmware Interface (UEFI) specification
723 * for details.
724 *
725 * This functions sets the signaled state of the event and queues the
726 * notification function for execution.
727 *
728 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200729 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200730 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200731static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100732{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200733 int i;
734
Alexander Grafbee91162016-03-04 01:09:59 +0100735 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200736 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
737 if (event != &efi_events[i])
738 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200739 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200740 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200741 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200742 if (event->type & EVT_NOTIFY_SIGNAL)
743 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200744 break;
745 }
Alexander Grafbee91162016-03-04 01:09:59 +0100746 return EFI_EXIT(EFI_SUCCESS);
747}
748
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200749/*
750 * Close an EFI event.
751 *
752 * This function implements the CloseEvent service.
753 * See the Unified Extensible Firmware Interface (UEFI) specification
754 * for details.
755 *
756 * @event event to close
757 * @return status code
758 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200759static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100760{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200761 int i;
762
Alexander Grafbee91162016-03-04 01:09:59 +0100763 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200764 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
765 if (event == &efi_events[i]) {
766 event->type = 0;
767 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200768 event->is_queued = false;
769 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200770 return EFI_EXIT(EFI_SUCCESS);
771 }
772 }
773 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100774}
775
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200776/*
777 * Check if an event is signaled.
778 *
779 * This function implements the CheckEvent service.
780 * See the Unified Extensible Firmware Interface (UEFI) specification
781 * for details.
782 *
783 * If an event is not signaled yet the notification function is queued.
784 *
785 * @event event to check
786 * @return status code
787 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200788static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100789{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200790 int i;
791
Alexander Grafbee91162016-03-04 01:09:59 +0100792 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200793 efi_timer_check();
794 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
795 if (event != &efi_events[i])
796 continue;
797 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
798 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200799 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200800 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200801 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200802 return EFI_EXIT(EFI_SUCCESS);
803 return EFI_EXIT(EFI_NOT_READY);
804 }
805 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100806}
807
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200808/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200809 * Find the internal EFI object for a handle.
810 *
811 * @handle handle to find
812 * @return EFI object
813 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100814struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200815{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100816 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200817
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100818 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200819 if (efiobj->handle == handle)
820 return efiobj;
821 }
822
823 return NULL;
824}
825
826/*
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100827 * Create open protocol info entry and add it to a protocol.
828 *
829 * @handler handler of a protocol
830 * @return open protocol info entry
831 */
832static struct efi_open_protocol_info_entry *efi_create_open_info(
833 struct efi_handler *handler)
834{
835 struct efi_open_protocol_info_item *item;
836
837 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
838 if (!item)
839 return NULL;
840 /* Append the item to the open protocol info list. */
841 list_add_tail(&item->link, &handler->open_infos);
842
843 return &item->info;
844}
845
846/*
847 * Remove an open protocol info entry from a protocol.
848 *
849 * @handler handler of a protocol
850 * @return status code
851 */
852static efi_status_t efi_delete_open_info(
853 struct efi_open_protocol_info_item *item)
854{
855 list_del(&item->link);
856 free(item);
857 return EFI_SUCCESS;
858}
859
860/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200861 * Install new protocol on a handle.
862 *
863 * @handle handle on which the protocol shall be installed
864 * @protocol GUID of the protocol to be installed
865 * @protocol_interface interface of the protocol implementation
866 * @return status code
867 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100868efi_status_t efi_add_protocol(const efi_handle_t handle,
869 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200870 void *protocol_interface)
871{
872 struct efi_object *efiobj;
873 struct efi_handler *handler;
874 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200875
876 efiobj = efi_search_obj(handle);
877 if (!efiobj)
878 return EFI_INVALID_PARAMETER;
879 ret = efi_search_protocol(handle, protocol, NULL);
880 if (ret != EFI_NOT_FOUND)
881 return EFI_INVALID_PARAMETER;
882 handler = calloc(1, sizeof(struct efi_handler));
883 if (!handler)
884 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100885 handler->guid = protocol;
886 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100887 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100888 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +0100889 if (!guidcmp(&efi_guid_device_path, protocol))
890 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100891 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200892}
893
894/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200895 * Install protocol interface.
896 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100897 * This function implements the InstallProtocolInterface service.
898 * See the Unified Extensible Firmware Interface (UEFI) specification
899 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200900 *
901 * @handle handle on which the protocol shall be installed
902 * @protocol GUID of the protocol to be installed
903 * @protocol_interface_type type of the interface to be installed,
904 * always EFI_NATIVE_INTERFACE
905 * @protocol_interface interface of the protocol implementation
906 * @return status code
907 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100908static efi_status_t EFIAPI efi_install_protocol_interface(
909 void **handle, const efi_guid_t *protocol,
910 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100911{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200912 efi_status_t r;
913
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100914 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
915 protocol_interface);
916
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200917 if (!handle || !protocol ||
918 protocol_interface_type != EFI_NATIVE_INTERFACE) {
919 r = EFI_INVALID_PARAMETER;
920 goto out;
921 }
922
923 /* Create new handle if requested. */
924 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200925 r = efi_create_handle(handle);
926 if (r != EFI_SUCCESS)
927 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200928 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
929 *handle);
930 } else {
931 debug("%sEFI: handle %p\n", indent_string(nesting_level),
932 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200933 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200934 /* Add new protocol */
935 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200936out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100937 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100938}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200939
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200940/*
941 * Reinstall protocol interface.
942 *
943 * This function implements the ReinstallProtocolInterface service.
944 * See the Unified Extensible Firmware Interface (UEFI) specification
945 * for details.
946 *
947 * @handle handle on which the protocol shall be
948 * reinstalled
949 * @protocol GUID of the protocol to be installed
950 * @old_interface interface to be removed
951 * @new_interface interface to be installed
952 * @return status code
953 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100954static efi_status_t EFIAPI efi_reinstall_protocol_interface(
955 efi_handle_t handle, const efi_guid_t *protocol,
956 void *old_interface, void *new_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100957{
Rob Clark778e6af2017-09-13 18:05:41 -0400958 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100959 new_interface);
960 return EFI_EXIT(EFI_ACCESS_DENIED);
961}
962
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200963/*
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +0100964 * Get all drivers associated to a controller.
965 * The allocated buffer has to be freed with free().
966 *
967 * @efiobj handle of the controller
968 * @protocol protocol guid (optional)
969 * @number_of_drivers number of child controllers
970 * @driver_handle_buffer handles of the the drivers
971 * @return status code
972 */
973static efi_status_t efi_get_drivers(struct efi_object *efiobj,
974 const efi_guid_t *protocol,
975 efi_uintn_t *number_of_drivers,
976 efi_handle_t **driver_handle_buffer)
977{
978 struct efi_handler *handler;
979 struct efi_open_protocol_info_item *item;
980 efi_uintn_t count = 0, i;
981 bool duplicate;
982
983 /* Count all driver associations */
984 list_for_each_entry(handler, &efiobj->protocols, link) {
985 if (protocol && guidcmp(handler->guid, protocol))
986 continue;
987 list_for_each_entry(item, &handler->open_infos, link) {
988 if (item->info.attributes &
989 EFI_OPEN_PROTOCOL_BY_DRIVER)
990 ++count;
991 }
992 }
993 /*
994 * Create buffer. In case of duplicate driver assignments the buffer
995 * will be too large. But that does not harm.
996 */
997 *number_of_drivers = 0;
998 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
999 if (!*driver_handle_buffer)
1000 return EFI_OUT_OF_RESOURCES;
1001 /* Collect unique driver handles */
1002 list_for_each_entry(handler, &efiobj->protocols, link) {
1003 if (protocol && guidcmp(handler->guid, protocol))
1004 continue;
1005 list_for_each_entry(item, &handler->open_infos, link) {
1006 if (item->info.attributes &
1007 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1008 /* Check this is a new driver */
1009 duplicate = false;
1010 for (i = 0; i < *number_of_drivers; ++i) {
1011 if ((*driver_handle_buffer)[i] ==
1012 item->info.agent_handle)
1013 duplicate = true;
1014 }
1015 /* Copy handle to buffer */
1016 if (!duplicate) {
1017 i = (*number_of_drivers)++;
1018 (*driver_handle_buffer)[i] =
1019 item->info.agent_handle;
1020 }
1021 }
1022 }
1023 }
1024 return EFI_SUCCESS;
1025}
1026
1027/*
1028 * Disconnect all drivers from a controller.
1029 *
1030 * This function implements the DisconnectController service.
1031 * See the Unified Extensible Firmware Interface (UEFI) specification
1032 * for details.
1033 *
1034 * @efiobj handle of the controller
1035 * @protocol protocol guid (optional)
1036 * @child_handle handle of the child to destroy
1037 * @return status code
1038 */
1039static efi_status_t efi_disconnect_all_drivers(
1040 struct efi_object *efiobj,
1041 const efi_guid_t *protocol,
1042 efi_handle_t child_handle)
1043{
1044 efi_uintn_t number_of_drivers;
1045 efi_handle_t *driver_handle_buffer;
1046 efi_status_t r, ret;
1047
1048 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1049 &driver_handle_buffer);
1050 if (ret != EFI_SUCCESS)
1051 return ret;
1052
1053 ret = EFI_NOT_FOUND;
1054 while (number_of_drivers) {
1055 r = EFI_CALL(efi_disconnect_controller(
1056 efiobj->handle,
1057 driver_handle_buffer[--number_of_drivers],
1058 child_handle));
1059 if (r == EFI_SUCCESS)
1060 ret = r;
1061 }
1062 free(driver_handle_buffer);
1063 return ret;
1064}
1065
1066/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001067 * Uninstall protocol interface.
1068 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001069 * This function implements the UninstallProtocolInterface service.
1070 * See the Unified Extensible Firmware Interface (UEFI) specification
1071 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001072 *
1073 * @handle handle from which the protocol shall be removed
1074 * @protocol GUID of the protocol to be removed
1075 * @protocol_interface interface to be removed
1076 * @return status code
1077 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001078static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001079 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001080 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001081{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001082 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001083 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001084 struct efi_open_protocol_info_item *item;
1085 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001086 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001087
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001088 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1089
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001090 /* Check handle */
1091 efiobj = efi_search_obj(handle);
1092 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001093 r = EFI_INVALID_PARAMETER;
1094 goto out;
1095 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001096 /* Find the protocol on the handle */
1097 r = efi_search_protocol(handle, protocol, &handler);
1098 if (r != EFI_SUCCESS)
1099 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001100 /* Disconnect controllers */
1101 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1102 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001103 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001104 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001105 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001106 /* Close protocol */
1107 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1108 if (item->info.attributes ==
1109 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1110 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1111 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1112 list_del(&item->link);
1113 }
1114 if (!list_empty(&handler->open_infos)) {
1115 r = EFI_ACCESS_DENIED;
1116 goto out;
1117 }
1118 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001119out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001120 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001121}
1122
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001123/*
1124 * Register an event for notification when a protocol is installed.
1125 *
1126 * This function implements the RegisterProtocolNotify service.
1127 * See the Unified Extensible Firmware Interface (UEFI) specification
1128 * for details.
1129 *
1130 * @protocol GUID of the protocol whose installation shall be
1131 * notified
1132 * @event event to be signaled upon installation of the protocol
1133 * @registration key for retrieving the registration information
1134 * @return status code
1135 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001136static efi_status_t EFIAPI efi_register_protocol_notify(
1137 const efi_guid_t *protocol,
1138 struct efi_event *event,
1139 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001140{
Rob Clark778e6af2017-09-13 18:05:41 -04001141 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001142 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1143}
1144
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001145/*
1146 * Determine if an EFI handle implements a protocol.
1147 *
1148 * See the documentation of the LocateHandle service in the UEFI specification.
1149 *
1150 * @search_type selection criterion
1151 * @protocol GUID of the protocol
1152 * @search_key registration key
1153 * @efiobj handle
1154 * @return 0 if the handle implements the protocol
1155 */
Alexander Grafbee91162016-03-04 01:09:59 +01001156static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001157 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001158 struct efi_object *efiobj)
1159{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001160 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001161
1162 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001163 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001164 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001165 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001166 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001167 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001168 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001169 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1170 return (ret != EFI_SUCCESS);
1171 default:
1172 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001173 return -1;
1174 }
Alexander Grafbee91162016-03-04 01:09:59 +01001175}
1176
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001177/*
1178 * Locate handles implementing a protocol.
1179 *
1180 * This function is meant for U-Boot internal calls. For the API implementation
1181 * of the LocateHandle service see efi_locate_handle_ext.
1182 *
1183 * @search_type selection criterion
1184 * @protocol GUID of the protocol
1185 * @search_key registration key
1186 * @buffer_size size of the buffer to receive the handles in bytes
1187 * @buffer buffer to receive the relevant handles
1188 * @return status code
1189 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001190static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001191 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001192 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001193 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001194{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001195 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001196 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001197
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001198 /* Check parameters */
1199 switch (search_type) {
1200 case ALL_HANDLES:
1201 break;
1202 case BY_REGISTER_NOTIFY:
1203 if (!search_key)
1204 return EFI_INVALID_PARAMETER;
1205 /* RegisterProtocolNotify is not implemented yet */
1206 return EFI_UNSUPPORTED;
1207 case BY_PROTOCOL:
1208 if (!protocol)
1209 return EFI_INVALID_PARAMETER;
1210 break;
1211 default:
1212 return EFI_INVALID_PARAMETER;
1213 }
1214
1215 /*
1216 * efi_locate_handle_buffer uses this function for
1217 * the calculation of the necessary buffer size.
1218 * So do not require a buffer for buffersize == 0.
1219 */
1220 if (!buffer_size || (*buffer_size && !buffer))
1221 return EFI_INVALID_PARAMETER;
1222
Alexander Grafbee91162016-03-04 01:09:59 +01001223 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001224 list_for_each_entry(efiobj, &efi_obj_list, link) {
1225 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001226 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001227 }
1228
1229 if (*buffer_size < size) {
1230 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001231 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001232 }
1233
Rob Clark796a78c2017-08-06 14:10:07 -04001234 *buffer_size = size;
1235 if (size == 0)
1236 return EFI_NOT_FOUND;
1237
Alexander Grafbee91162016-03-04 01:09:59 +01001238 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001239 list_for_each_entry(efiobj, &efi_obj_list, link) {
1240 if (!efi_search(search_type, protocol, search_key, efiobj))
1241 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001242 }
1243
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001244 return EFI_SUCCESS;
1245}
1246
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001247/*
1248 * Locate handles implementing a protocol.
1249 *
1250 * This function implements the LocateHandle service.
1251 * See the Unified Extensible Firmware Interface (UEFI) specification
1252 * for details.
1253 *
1254 * @search_type selection criterion
1255 * @protocol GUID of the protocol
1256 * @search_key registration key
1257 * @buffer_size size of the buffer to receive the handles in bytes
1258 * @buffer buffer to receive the relevant handles
1259 * @return 0 if the handle implements the protocol
1260 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001261static efi_status_t EFIAPI efi_locate_handle_ext(
1262 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001263 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001264 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001265{
Rob Clark778e6af2017-09-13 18:05:41 -04001266 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001267 buffer_size, buffer);
1268
1269 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1270 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001271}
1272
Alexander Grafd98cdf62017-07-26 13:41:04 +02001273/* Collapses configuration table entries, removing index i */
1274static void efi_remove_configuration_table(int i)
1275{
1276 struct efi_configuration_table *this = &efi_conf_table[i];
1277 struct efi_configuration_table *next = &efi_conf_table[i+1];
1278 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1279
1280 memmove(this, next, (ulong)end - (ulong)next);
1281 systab.nr_tables--;
1282}
1283
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001284/*
1285 * Adds, updates, or removes a configuration table.
1286 *
1287 * This function is used for internal calls. For the API implementation of the
1288 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1289 *
1290 * @guid GUID of the installed table
1291 * @table table to be installed
1292 * @return status code
1293 */
Alexander Graf488bf122016-08-19 01:23:24 +02001294efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001295{
1296 int i;
1297
Alexander Grafbee91162016-03-04 01:09:59 +01001298 /* Check for guid override */
1299 for (i = 0; i < systab.nr_tables; i++) {
1300 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001301 if (table)
1302 efi_conf_table[i].table = table;
1303 else
1304 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001305 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001306 }
1307 }
1308
Alexander Grafd98cdf62017-07-26 13:41:04 +02001309 if (!table)
1310 return EFI_NOT_FOUND;
1311
Alexander Grafbee91162016-03-04 01:09:59 +01001312 /* No override, check for overflow */
1313 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001314 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001315
1316 /* Add a new entry */
1317 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1318 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001319 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001320
Alexander Graf488bf122016-08-19 01:23:24 +02001321 return EFI_SUCCESS;
1322}
1323
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001324/*
1325 * Adds, updates, or removes a configuration table.
1326 *
1327 * This function implements the InstallConfigurationTable service.
1328 * See the Unified Extensible Firmware Interface (UEFI) specification
1329 * for details.
1330 *
1331 * @guid GUID of the installed table
1332 * @table table to be installed
1333 * @return status code
1334 */
Alexander Graf488bf122016-08-19 01:23:24 +02001335static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1336 void *table)
1337{
Rob Clark778e6af2017-09-13 18:05:41 -04001338 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001339 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001340}
1341
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001342/*
1343 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001344 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001345 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001346 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001347 * image
1348 * @obj internal object associated with the loaded image
1349 * @device_path device path of the loaded image
1350 * @file_path file path of the loaded image
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001351 * @return status code
Rob Clark95c55532017-09-13 18:05:33 -04001352 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001353efi_status_t efi_setup_loaded_image(
1354 struct efi_loaded_image *info, struct efi_object *obj,
1355 struct efi_device_path *device_path,
1356 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001357{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001358 efi_status_t ret;
1359
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001360 /* Add internal object to object list */
1361 efi_add_handle(obj);
1362 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001363 obj->handle = info;
1364
Rob Clark95c55532017-09-13 18:05:33 -04001365 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001366 if (device_path)
1367 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001368
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001369 /*
1370 * When asking for the device path interface, return
1371 * bootefi_device_path
1372 */
1373 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1374 if (ret != EFI_SUCCESS)
1375 goto failure;
1376
1377 /*
1378 * When asking for the loaded_image interface, just
1379 * return handle which points to loaded_image_info
1380 */
1381 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1382 if (ret != EFI_SUCCESS)
1383 goto failure;
1384
1385 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1386 (void *)&efi_console_control);
1387 if (ret != EFI_SUCCESS)
1388 goto failure;
1389
1390 ret = efi_add_protocol(obj->handle,
1391 &efi_guid_device_path_to_text_protocol,
1392 (void *)&efi_device_path_to_text);
1393 if (ret != EFI_SUCCESS)
1394 goto failure;
1395
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001396 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001397failure:
1398 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001399 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001400}
1401
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001402/*
1403 * Load an image using a file path.
1404 *
1405 * @file_path the path of the image to load
1406 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001407 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001408 */
Rob Clark9975fe92017-09-13 18:05:38 -04001409efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1410 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001411{
1412 struct efi_file_info *info = NULL;
1413 struct efi_file_handle *f;
1414 static efi_status_t ret;
1415 uint64_t bs;
1416
1417 f = efi_file_from_path(file_path);
1418 if (!f)
1419 return EFI_DEVICE_ERROR;
1420
1421 bs = 0;
1422 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1423 &bs, info));
1424 if (ret == EFI_BUFFER_TOO_SMALL) {
1425 info = malloc(bs);
1426 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1427 &bs, info));
1428 }
1429 if (ret != EFI_SUCCESS)
1430 goto error;
1431
1432 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1433 if (ret)
1434 goto error;
1435
1436 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1437
1438error:
1439 free(info);
1440 EFI_CALL(f->close(f));
1441
1442 if (ret != EFI_SUCCESS) {
1443 efi_free_pool(*buffer);
1444 *buffer = NULL;
1445 }
1446
1447 return ret;
1448}
1449
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001450/*
1451 * Load an EFI image into memory.
1452 *
1453 * This function implements the LoadImage service.
1454 * See the Unified Extensible Firmware Interface (UEFI) specification
1455 * for details.
1456 *
1457 * @boot_policy true for request originating from the boot manager
1458 * @parent_image the calles's image handle
1459 * @file_path the path of the image to load
1460 * @source_buffer memory location from which the image is installed
1461 * @source_size size of the memory area from which the image is
1462 * installed
1463 * @image_handle handle for the newly installed image
1464 * @return status code
1465 */
Alexander Grafbee91162016-03-04 01:09:59 +01001466static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1467 efi_handle_t parent_image,
1468 struct efi_device_path *file_path,
1469 void *source_buffer,
1470 unsigned long source_size,
1471 efi_handle_t *image_handle)
1472{
Alexander Grafbee91162016-03-04 01:09:59 +01001473 struct efi_loaded_image *info;
1474 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001475 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001476
Heinrich Schuchardt476ed962018-01-19 20:24:40 +01001477 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001478 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001479
1480 info = calloc(1, sizeof(*info));
1481 obj = calloc(1, sizeof(*obj));
1482
1483 if (!source_buffer) {
1484 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001485
Rob Clark9975fe92017-09-13 18:05:38 -04001486 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001487 if (ret != EFI_SUCCESS)
1488 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001489 /*
1490 * split file_path which contains both the device and
1491 * file parts:
1492 */
1493 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001494 ret = efi_setup_loaded_image(info, obj, dp, fp);
1495 if (ret != EFI_SUCCESS)
1496 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001497 } else {
1498 /* In this case, file_path is the "device" path, ie.
1499 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1500 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001501 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1502 if (ret != EFI_SUCCESS)
1503 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001504 }
Alexander Grafbee91162016-03-04 01:09:59 +01001505 info->reserved = efi_load_pe(source_buffer, info);
1506 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001507 ret = EFI_UNSUPPORTED;
1508 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001509 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001510 info->system_table = &systab;
1511 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001512 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001513 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001514failure:
1515 free(info);
1516 efi_delete_handle(obj);
1517 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001518}
1519
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001520/*
1521 * Call the entry point of an image.
1522 *
1523 * This function implements the StartImage service.
1524 * See the Unified Extensible Firmware Interface (UEFI) specification
1525 * for details.
1526 *
1527 * @image_handle handle of the image
1528 * @exit_data_size size of the buffer
1529 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001530 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001531 */
Alexander Grafbee91162016-03-04 01:09:59 +01001532static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1533 unsigned long *exit_data_size,
1534 s16 **exit_data)
1535{
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001536 ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st);
Alexander Grafbee91162016-03-04 01:09:59 +01001537 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001538 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001539
1540 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1541 entry = info->reserved;
1542
1543 efi_is_direct_boot = false;
1544
1545 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001546 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001547 /*
1548 * We called the entry point of the child image with EFI_CALL
1549 * in the lines below. The child image called the Exit() boot
1550 * service efi_exit() which executed the long jump that brought
1551 * us to the current line. This implies that the second half
1552 * of the EFI_CALL macro has not been executed.
1553 */
1554#ifdef CONFIG_ARM
1555 /*
1556 * efi_exit() called efi_restore_gd(). We have to undo this
1557 * otherwise __efi_entry_check() will put the wrong value into
1558 * app_gd.
1559 */
1560 gd = app_gd;
1561#endif
1562 /*
1563 * To get ready to call EFI_EXIT below we have to execute the
1564 * missed out steps of EFI_CALL.
1565 */
1566 assert(__efi_entry_check());
1567 debug("%sEFI: %lu returned by started image\n",
1568 __efi_nesting_dec(),
1569 (unsigned long)((uintptr_t)info->exit_status &
1570 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001571 return EFI_EXIT(info->exit_status);
1572 }
1573
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001574 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001575
1576 /* Should usually never get here */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001577 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001578}
1579
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001580/*
1581 * Leave an EFI application or driver.
1582 *
1583 * This function implements the Exit service.
1584 * See the Unified Extensible Firmware Interface (UEFI) specification
1585 * for details.
1586 *
1587 * @image_handle handle of the application or driver that is exiting
1588 * @exit_status status code
1589 * @exit_data_size size of the buffer in bytes
1590 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001591 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001592 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001593static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1594 efi_status_t exit_status, unsigned long exit_data_size,
1595 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001596{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001597 /*
1598 * We require that the handle points to the original loaded
1599 * image protocol interface.
1600 *
1601 * For getting the longjmp address this is safer than locating
1602 * the protocol because the protocol may have been reinstalled
1603 * pointing to another memory location.
1604 *
1605 * TODO: We should call the unload procedure of the loaded
1606 * image protocol.
1607 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001608 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1609
Alexander Grafbee91162016-03-04 01:09:59 +01001610 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1611 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001612
Alexander Grafa1489202017-09-03 14:14:17 +02001613 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001614 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001615
Alexander Grafa1489202017-09-03 14:14:17 +02001616 /*
1617 * But longjmp out with the U-Boot gd, not the application's, as
1618 * the other end is a setjmp call inside EFI context.
1619 */
1620 efi_restore_gd();
1621
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001622 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001623 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001624
1625 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001626}
1627
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001628/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001629 * Unload an EFI image.
1630 *
1631 * This function implements the UnloadImage service.
1632 * See the Unified Extensible Firmware Interface (UEFI) specification
1633 * for details.
1634 *
1635 * @image_handle handle of the image to be unloaded
1636 * @return status code
1637 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001638static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001639{
1640 struct efi_object *efiobj;
1641
1642 EFI_ENTRY("%p", image_handle);
1643 efiobj = efi_search_obj(image_handle);
1644 if (efiobj)
1645 list_del(&efiobj->link);
1646
1647 return EFI_EXIT(EFI_SUCCESS);
1648}
1649
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001650/*
1651 * Fix up caches for EFI payloads if necessary.
1652 */
Alexander Grafbee91162016-03-04 01:09:59 +01001653static void efi_exit_caches(void)
1654{
1655#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1656 /*
1657 * Grub on 32bit ARM needs to have caches disabled before jumping into
1658 * a zImage, but does not know of all cache layers. Give it a hand.
1659 */
1660 if (efi_is_direct_boot)
1661 cleanup_before_linux();
1662#endif
1663}
1664
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001665/*
1666 * Stop boot services.
1667 *
1668 * This function implements the ExitBootServices service.
1669 * See the Unified Extensible Firmware Interface (UEFI) specification
1670 * for details.
1671 *
1672 * @image_handle handle of the loaded image
1673 * @map_key key of the memory map
1674 * @return status code
1675 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001676static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001677 unsigned long map_key)
1678{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001679 int i;
1680
Alexander Grafbee91162016-03-04 01:09:59 +01001681 EFI_ENTRY("%p, %ld", image_handle, map_key);
1682
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001683 /* Notify that ExitBootServices is invoked. */
1684 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1685 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1686 continue;
1687 efi_signal_event(&efi_events[i]);
1688 }
1689 /* Make sure that notification functions are not called anymore */
1690 efi_tpl = TPL_HIGH_LEVEL;
1691
Alexander Graf40583562017-10-19 23:23:50 +02001692 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001693
Alexander Grafb7b84102016-11-17 01:02:57 +01001694 board_quiesce_devices();
1695
Alexander Grafbee91162016-03-04 01:09:59 +01001696 /* Fix up caches for EFI payloads if necessary */
1697 efi_exit_caches();
1698
1699 /* This stops all lingering devices */
1700 bootm_disable_interrupts();
1701
1702 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001703 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001704 WATCHDOG_RESET();
1705
1706 return EFI_EXIT(EFI_SUCCESS);
1707}
1708
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001709/*
1710 * Get next value of the counter.
1711 *
1712 * This function implements the NextMonotonicCount service.
1713 * See the Unified Extensible Firmware Interface (UEFI) specification
1714 * for details.
1715 *
1716 * @count returned value of the counter
1717 * @return status code
1718 */
Alexander Grafbee91162016-03-04 01:09:59 +01001719static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1720{
1721 static uint64_t mono = 0;
1722 EFI_ENTRY("%p", count);
1723 *count = mono++;
1724 return EFI_EXIT(EFI_SUCCESS);
1725}
1726
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001727/*
1728 * Sleep.
1729 *
1730 * This function implements the Stall sercive.
1731 * See the Unified Extensible Firmware Interface (UEFI) specification
1732 * for details.
1733 *
1734 * @microseconds period to sleep in microseconds
1735 * @return status code
1736 */
Alexander Grafbee91162016-03-04 01:09:59 +01001737static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1738{
1739 EFI_ENTRY("%ld", microseconds);
1740 udelay(microseconds);
1741 return EFI_EXIT(EFI_SUCCESS);
1742}
1743
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001744/*
1745 * Reset the watchdog timer.
1746 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001747 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001748 * See the Unified Extensible Firmware Interface (UEFI) specification
1749 * for details.
1750 *
1751 * @timeout seconds before reset by watchdog
1752 * @watchdog_code code to be logged when resetting
1753 * @data_size size of buffer in bytes
1754 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001755 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001756 */
Alexander Grafbee91162016-03-04 01:09:59 +01001757static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1758 uint64_t watchdog_code,
1759 unsigned long data_size,
1760 uint16_t *watchdog_data)
1761{
1762 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1763 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001764 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001765}
1766
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001767/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001768 * Close a protocol.
1769 *
1770 * This function implements the CloseProtocol service.
1771 * See the Unified Extensible Firmware Interface (UEFI) specification
1772 * for details.
1773 *
1774 * @handle handle on which the protocol shall be closed
1775 * @protocol GUID of the protocol to close
1776 * @agent_handle handle of the driver
1777 * @controller_handle handle of the controller
1778 * @return status code
1779 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001780static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001781 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001782 efi_handle_t agent_handle,
1783 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001784{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001785 struct efi_handler *handler;
1786 struct efi_open_protocol_info_item *item;
1787 struct efi_open_protocol_info_item *pos;
1788 efi_status_t r;
1789
Rob Clark778e6af2017-09-13 18:05:41 -04001790 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001791 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001792
1793 if (!agent_handle) {
1794 r = EFI_INVALID_PARAMETER;
1795 goto out;
1796 }
1797 r = efi_search_protocol(handle, protocol, &handler);
1798 if (r != EFI_SUCCESS)
1799 goto out;
1800
1801 r = EFI_NOT_FOUND;
1802 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1803 if (item->info.agent_handle == agent_handle &&
1804 item->info.controller_handle == controller_handle) {
1805 efi_delete_open_info(item);
1806 r = EFI_SUCCESS;
1807 break;
1808 }
1809 }
1810out:
1811 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001812}
1813
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001814/*
1815 * Provide information about then open status of a protocol on a handle
1816 *
1817 * This function implements the OpenProtocolInformation service.
1818 * See the Unified Extensible Firmware Interface (UEFI) specification
1819 * for details.
1820 *
1821 * @handle handle for which the information shall be retrieved
1822 * @protocol GUID of the protocol
1823 * @entry_buffer buffer to receive the open protocol information
1824 * @entry_count number of entries available in the buffer
1825 * @return status code
1826 */
Alexander Grafbee91162016-03-04 01:09:59 +01001827static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001828 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001829 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001830 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001831{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001832 unsigned long buffer_size;
1833 unsigned long count;
1834 struct efi_handler *handler;
1835 struct efi_open_protocol_info_item *item;
1836 efi_status_t r;
1837
Rob Clark778e6af2017-09-13 18:05:41 -04001838 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001839 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001840
1841 /* Check parameters */
1842 if (!entry_buffer) {
1843 r = EFI_INVALID_PARAMETER;
1844 goto out;
1845 }
1846 r = efi_search_protocol(handle, protocol, &handler);
1847 if (r != EFI_SUCCESS)
1848 goto out;
1849
1850 /* Count entries */
1851 count = 0;
1852 list_for_each_entry(item, &handler->open_infos, link) {
1853 if (item->info.open_count)
1854 ++count;
1855 }
1856 *entry_count = count;
1857 *entry_buffer = NULL;
1858 if (!count) {
1859 r = EFI_SUCCESS;
1860 goto out;
1861 }
1862
1863 /* Copy entries */
1864 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1865 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1866 (void **)entry_buffer);
1867 if (r != EFI_SUCCESS)
1868 goto out;
1869 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1870 if (item->info.open_count)
1871 (*entry_buffer)[--count] = item->info;
1872 }
1873out:
1874 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001875}
1876
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001877/*
1878 * Get protocols installed on a handle.
1879 *
1880 * This function implements the ProtocolsPerHandleService.
1881 * See the Unified Extensible Firmware Interface (UEFI) specification
1882 * for details.
1883 *
1884 * @handle handle for which the information is retrieved
1885 * @protocol_buffer buffer with protocol GUIDs
1886 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001887 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001888 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001889static efi_status_t EFIAPI efi_protocols_per_handle(
1890 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001891 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001892{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001893 unsigned long buffer_size;
1894 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001895 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001896 efi_status_t r;
1897
Alexander Grafbee91162016-03-04 01:09:59 +01001898 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1899 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001900
1901 if (!handle || !protocol_buffer || !protocol_buffer_count)
1902 return EFI_EXIT(EFI_INVALID_PARAMETER);
1903
1904 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001905 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001906
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001907 efiobj = efi_search_obj(handle);
1908 if (!efiobj)
1909 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001910
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001911 /* Count protocols */
1912 list_for_each(protocol_handle, &efiobj->protocols) {
1913 ++*protocol_buffer_count;
1914 }
1915
1916 /* Copy guids */
1917 if (*protocol_buffer_count) {
1918 size_t j = 0;
1919
1920 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1921 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1922 (void **)protocol_buffer);
1923 if (r != EFI_SUCCESS)
1924 return EFI_EXIT(r);
1925 list_for_each(protocol_handle, &efiobj->protocols) {
1926 struct efi_handler *protocol;
1927
1928 protocol = list_entry(protocol_handle,
1929 struct efi_handler, link);
1930 (*protocol_buffer)[j] = (void *)protocol->guid;
1931 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001932 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001933 }
1934
1935 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001936}
1937
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001938/*
1939 * Locate handles implementing a protocol.
1940 *
1941 * This function implements the LocateHandleBuffer service.
1942 * See the Unified Extensible Firmware Interface (UEFI) specification
1943 * for details.
1944 *
1945 * @search_type selection criterion
1946 * @protocol GUID of the protocol
1947 * @search_key registration key
1948 * @no_handles number of returned handles
1949 * @buffer buffer with the returned handles
1950 * @return status code
1951 */
Alexander Grafbee91162016-03-04 01:09:59 +01001952static efi_status_t EFIAPI efi_locate_handle_buffer(
1953 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001954 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001955 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001956{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001957 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001958 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001959
Rob Clark778e6af2017-09-13 18:05:41 -04001960 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001961 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001962
1963 if (!no_handles || !buffer) {
1964 r = EFI_INVALID_PARAMETER;
1965 goto out;
1966 }
1967 *no_handles = 0;
1968 *buffer = NULL;
1969 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1970 *buffer);
1971 if (r != EFI_BUFFER_TOO_SMALL)
1972 goto out;
1973 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1974 (void **)buffer);
1975 if (r != EFI_SUCCESS)
1976 goto out;
1977 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1978 *buffer);
1979 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001980 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001981out:
1982 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001983}
1984
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001985/*
1986 * Find an interface implementing a protocol.
1987 *
1988 * This function implements the LocateProtocol service.
1989 * See the Unified Extensible Firmware Interface (UEFI) specification
1990 * for details.
1991 *
1992 * @protocol GUID of the protocol
1993 * @registration registration key passed to the notification function
1994 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001995 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001996 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001997static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001998 void *registration,
1999 void **protocol_interface)
2000{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002001 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002002 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002003
Rob Clark778e6af2017-09-13 18:05:41 -04002004 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002005
2006 if (!protocol || !protocol_interface)
2007 return EFI_EXIT(EFI_INVALID_PARAMETER);
2008
2009 list_for_each(lhandle, &efi_obj_list) {
2010 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002011 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002012
2013 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002014
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002015 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2016 if (ret == EFI_SUCCESS) {
2017 *protocol_interface = handler->protocol_interface;
2018 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002019 }
2020 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002021 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002022
2023 return EFI_EXIT(EFI_NOT_FOUND);
2024}
2025
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002026/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002027 * Get the device path and handle of an device implementing a protocol.
2028 *
2029 * This function implements the LocateDevicePath service.
2030 * See the Unified Extensible Firmware Interface (UEFI) specification
2031 * for details.
2032 *
2033 * @protocol GUID of the protocol
2034 * @device_path device path
2035 * @device handle of the device
2036 * @return status code
2037 */
2038static efi_status_t EFIAPI efi_locate_device_path(
2039 const efi_guid_t *protocol,
2040 struct efi_device_path **device_path,
2041 efi_handle_t *device)
2042{
2043 struct efi_device_path *dp;
2044 size_t i;
2045 struct efi_handler *handler;
2046 efi_handle_t *handles;
2047 size_t len, len_dp;
2048 size_t len_best = 0;
2049 efi_uintn_t no_handles;
2050 u8 *remainder;
2051 efi_status_t ret;
2052
2053 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2054
2055 if (!protocol || !device_path || !*device_path || !device) {
2056 ret = EFI_INVALID_PARAMETER;
2057 goto out;
2058 }
2059
2060 /* Find end of device path */
2061 len = efi_dp_size(*device_path);
2062
2063 /* Get all handles implementing the protocol */
2064 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2065 &no_handles, &handles));
2066 if (ret != EFI_SUCCESS)
2067 goto out;
2068
2069 for (i = 0; i < no_handles; ++i) {
2070 /* Find the device path protocol */
2071 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2072 &handler);
2073 if (ret != EFI_SUCCESS)
2074 continue;
2075 dp = (struct efi_device_path *)handler->protocol_interface;
2076 len_dp = efi_dp_size(dp);
2077 /*
2078 * This handle can only be a better fit
2079 * if its device path length is longer than the best fit and
2080 * if its device path length is shorter of equal the searched
2081 * device path.
2082 */
2083 if (len_dp <= len_best || len_dp > len)
2084 continue;
2085 /* Check if dp is a subpath of device_path */
2086 if (memcmp(*device_path, dp, len_dp))
2087 continue;
2088 *device = handles[i];
2089 len_best = len_dp;
2090 }
2091 if (len_best) {
2092 remainder = (u8 *)*device_path + len_best;
2093 *device_path = (struct efi_device_path *)remainder;
2094 ret = EFI_SUCCESS;
2095 } else {
2096 ret = EFI_NOT_FOUND;
2097 }
2098out:
2099 return EFI_EXIT(ret);
2100}
2101
2102/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002103 * Install multiple protocol interfaces.
2104 *
2105 * This function implements the MultipleProtocolInterfaces service.
2106 * See the Unified Extensible Firmware Interface (UEFI) specification
2107 * for details.
2108 *
2109 * @handle handle on which the protocol interfaces shall be installed
2110 * @... NULL terminated argument list with pairs of protocol GUIDS and
2111 * interfaces
2112 * @return status code
2113 */
Alexander Grafbee91162016-03-04 01:09:59 +01002114static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2115 void **handle, ...)
2116{
2117 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002118
2119 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002120 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002121 void *protocol_interface;
2122 efi_status_t r = EFI_SUCCESS;
2123 int i = 0;
2124
2125 if (!handle)
2126 return EFI_EXIT(EFI_INVALID_PARAMETER);
2127
2128 va_start(argptr, handle);
2129 for (;;) {
2130 protocol = va_arg(argptr, efi_guid_t*);
2131 if (!protocol)
2132 break;
2133 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002134 r = EFI_CALL(efi_install_protocol_interface(
2135 handle, protocol,
2136 EFI_NATIVE_INTERFACE,
2137 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002138 if (r != EFI_SUCCESS)
2139 break;
2140 i++;
2141 }
2142 va_end(argptr);
2143 if (r == EFI_SUCCESS)
2144 return EFI_EXIT(r);
2145
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002146 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002147 va_start(argptr, handle);
2148 for (; i; --i) {
2149 protocol = va_arg(argptr, efi_guid_t*);
2150 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002151 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2152 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002153 }
2154 va_end(argptr);
2155
2156 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002157}
2158
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002159/*
2160 * Uninstall multiple protocol interfaces.
2161 *
2162 * This function implements the UninstallMultipleProtocolInterfaces service.
2163 * See the Unified Extensible Firmware Interface (UEFI) specification
2164 * for details.
2165 *
2166 * @handle handle from which the protocol interfaces shall be removed
2167 * @... NULL terminated argument list with pairs of protocol GUIDS and
2168 * interfaces
2169 * @return status code
2170 */
Alexander Grafbee91162016-03-04 01:09:59 +01002171static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2172 void *handle, ...)
2173{
2174 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002175
2176 va_list argptr;
2177 const efi_guid_t *protocol;
2178 void *protocol_interface;
2179 efi_status_t r = EFI_SUCCESS;
2180 size_t i = 0;
2181
2182 if (!handle)
2183 return EFI_EXIT(EFI_INVALID_PARAMETER);
2184
2185 va_start(argptr, handle);
2186 for (;;) {
2187 protocol = va_arg(argptr, efi_guid_t*);
2188 if (!protocol)
2189 break;
2190 protocol_interface = va_arg(argptr, void*);
2191 r = EFI_CALL(efi_uninstall_protocol_interface(
2192 handle, protocol,
2193 protocol_interface));
2194 if (r != EFI_SUCCESS)
2195 break;
2196 i++;
2197 }
2198 va_end(argptr);
2199 if (r == EFI_SUCCESS)
2200 return EFI_EXIT(r);
2201
2202 /* If an error occurred undo all changes. */
2203 va_start(argptr, handle);
2204 for (; i; --i) {
2205 protocol = va_arg(argptr, efi_guid_t*);
2206 protocol_interface = va_arg(argptr, void*);
2207 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2208 EFI_NATIVE_INTERFACE,
2209 protocol_interface));
2210 }
2211 va_end(argptr);
2212
2213 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002214}
2215
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002216/*
2217 * Calculate cyclic redundancy code.
2218 *
2219 * This function implements the CalculateCrc32 service.
2220 * See the Unified Extensible Firmware Interface (UEFI) specification
2221 * for details.
2222 *
2223 * @data buffer with data
2224 * @data_size size of buffer in bytes
2225 * @crc32_p cyclic redundancy code
2226 * @return status code
2227 */
Alexander Grafbee91162016-03-04 01:09:59 +01002228static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2229 unsigned long data_size,
2230 uint32_t *crc32_p)
2231{
2232 EFI_ENTRY("%p, %ld", data, data_size);
2233 *crc32_p = crc32(0, data, data_size);
2234 return EFI_EXIT(EFI_SUCCESS);
2235}
2236
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002237/*
2238 * Copy memory.
2239 *
2240 * This function implements the CopyMem service.
2241 * See the Unified Extensible Firmware Interface (UEFI) specification
2242 * for details.
2243 *
2244 * @destination destination of the copy operation
2245 * @source source of the copy operation
2246 * @length number of bytes to copy
2247 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002248static void EFIAPI efi_copy_mem(void *destination, const void *source,
2249 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002250{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002251 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002252 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002253 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002254}
2255
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002256/*
2257 * Fill memory with a byte value.
2258 *
2259 * This function implements the SetMem service.
2260 * See the Unified Extensible Firmware Interface (UEFI) specification
2261 * for details.
2262 *
2263 * @buffer buffer to fill
2264 * @size size of buffer in bytes
2265 * @value byte to copy to the buffer
2266 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002267static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002268{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002269 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002270 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002271 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002272}
2273
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002274/*
2275 * Open protocol interface on a handle.
2276 *
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002277 * @handler handler of a protocol
2278 * @protocol_interface interface implementing the protocol
2279 * @agent_handle handle of the driver
2280 * @controller_handle handle of the controller
2281 * @attributes attributes indicating how to open the protocol
2282 * @return status code
2283 */
2284static efi_status_t efi_protocol_open(
2285 struct efi_handler *handler,
2286 void **protocol_interface, void *agent_handle,
2287 void *controller_handle, uint32_t attributes)
2288{
2289 struct efi_open_protocol_info_item *item;
2290 struct efi_open_protocol_info_entry *match = NULL;
2291 bool opened_by_driver = false;
2292 bool opened_exclusive = false;
2293
2294 /* If there is no agent, only return the interface */
2295 if (!agent_handle)
2296 goto out;
2297
2298 /* For TEST_PROTOCOL ignore interface attribute */
2299 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2300 *protocol_interface = NULL;
2301
2302 /*
2303 * Check if the protocol is already opened by a driver with the same
2304 * attributes or opened exclusively
2305 */
2306 list_for_each_entry(item, &handler->open_infos, link) {
2307 if (item->info.agent_handle == agent_handle) {
2308 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2309 (item->info.attributes == attributes))
2310 return EFI_ALREADY_STARTED;
2311 }
2312 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2313 opened_exclusive = true;
2314 }
2315
2316 /* Only one controller can open the protocol exclusively */
2317 if (opened_exclusive && attributes &
2318 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2319 return EFI_ACCESS_DENIED;
2320
2321 /* Prepare exclusive opening */
2322 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2323 /* Try to disconnect controllers */
2324 list_for_each_entry(item, &handler->open_infos, link) {
2325 if (item->info.attributes ==
2326 EFI_OPEN_PROTOCOL_BY_DRIVER)
2327 EFI_CALL(efi_disconnect_controller(
2328 item->info.controller_handle,
2329 item->info.agent_handle,
2330 NULL));
2331 }
2332 opened_by_driver = false;
2333 /* Check if all controllers are disconnected */
2334 list_for_each_entry(item, &handler->open_infos, link) {
2335 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2336 opened_by_driver = true;
2337 }
2338 /* Only one controller can be conncected */
2339 if (opened_by_driver)
2340 return EFI_ACCESS_DENIED;
2341 }
2342
2343 /* Find existing entry */
2344 list_for_each_entry(item, &handler->open_infos, link) {
2345 if (item->info.agent_handle == agent_handle &&
2346 item->info.controller_handle == controller_handle)
2347 match = &item->info;
2348 }
2349 /* None found, create one */
2350 if (!match) {
2351 match = efi_create_open_info(handler);
2352 if (!match)
2353 return EFI_OUT_OF_RESOURCES;
2354 }
2355
2356 match->agent_handle = agent_handle;
2357 match->controller_handle = controller_handle;
2358 match->attributes = attributes;
2359 match->open_count++;
2360
2361out:
2362 /* For TEST_PROTOCOL ignore interface attribute. */
2363 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2364 *protocol_interface = handler->protocol_interface;
2365
2366 return EFI_SUCCESS;
2367}
2368
2369/*
2370 * Open protocol interface on a handle.
2371 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002372 * This function implements the OpenProtocol interface.
2373 * See the Unified Extensible Firmware Interface (UEFI) specification
2374 * for details.
2375 *
2376 * @handle handle on which the protocol shall be opened
2377 * @protocol GUID of the protocol
2378 * @protocol_interface interface implementing the protocol
2379 * @agent_handle handle of the driver
2380 * @controller_handle handle of the controller
2381 * @attributes attributes indicating how to open the protocol
2382 * @return status code
2383 */
Alexander Grafbee91162016-03-04 01:09:59 +01002384static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002385 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002386 void **protocol_interface, void *agent_handle,
2387 void *controller_handle, uint32_t attributes)
2388{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002389 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002390 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002391
Rob Clark778e6af2017-09-13 18:05:41 -04002392 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002393 protocol_interface, agent_handle, controller_handle,
2394 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002395
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002396 if (!handle || !protocol ||
2397 (!protocol_interface && attributes !=
2398 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2399 goto out;
2400 }
2401
2402 switch (attributes) {
2403 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2404 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2405 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2406 break;
2407 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2408 if (controller_handle == handle)
2409 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002410 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002411 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2412 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002413 /* Check that the controller handle is valid */
2414 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002415 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002416 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002417 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002418 /* Check that the agent handle is valid */
2419 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002420 goto out;
2421 break;
2422 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002423 goto out;
2424 }
2425
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002426 r = efi_search_protocol(handle, protocol, &handler);
2427 if (r != EFI_SUCCESS)
2428 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002429
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002430 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2431 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002432out:
2433 return EFI_EXIT(r);
2434}
2435
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002436/*
2437 * Get interface of a protocol on a handle.
2438 *
2439 * This function implements the HandleProtocol service.
2440 * See the Unified Extensible Firmware Interface (UEFI) specification
2441 * for details.
2442 *
2443 * @handle handle on which the protocol shall be opened
2444 * @protocol GUID of the protocol
2445 * @protocol_interface interface implementing the protocol
2446 * @return status code
2447 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002448static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002449 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002450 void **protocol_interface)
2451{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002452 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2453 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002454}
2455
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002456static efi_status_t efi_bind_controller(
2457 efi_handle_t controller_handle,
2458 efi_handle_t driver_image_handle,
2459 struct efi_device_path *remain_device_path)
2460{
2461 struct efi_driver_binding_protocol *binding_protocol;
2462 efi_status_t r;
2463
2464 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2465 &efi_guid_driver_binding_protocol,
2466 (void **)&binding_protocol,
2467 driver_image_handle, NULL,
2468 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2469 if (r != EFI_SUCCESS)
2470 return r;
2471 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2472 controller_handle,
2473 remain_device_path));
2474 if (r == EFI_SUCCESS)
2475 r = EFI_CALL(binding_protocol->start(binding_protocol,
2476 controller_handle,
2477 remain_device_path));
2478 EFI_CALL(efi_close_protocol(driver_image_handle,
2479 &efi_guid_driver_binding_protocol,
2480 driver_image_handle, NULL));
2481 return r;
2482}
2483
2484static efi_status_t efi_connect_single_controller(
2485 efi_handle_t controller_handle,
2486 efi_handle_t *driver_image_handle,
2487 struct efi_device_path *remain_device_path)
2488{
2489 efi_handle_t *buffer;
2490 size_t count;
2491 size_t i;
2492 efi_status_t r;
2493 size_t connected = 0;
2494
2495 /* Get buffer with all handles with driver binding protocol */
2496 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2497 &efi_guid_driver_binding_protocol,
2498 NULL, &count, &buffer));
2499 if (r != EFI_SUCCESS)
2500 return r;
2501
2502 /* Context Override */
2503 if (driver_image_handle) {
2504 for (; *driver_image_handle; ++driver_image_handle) {
2505 for (i = 0; i < count; ++i) {
2506 if (buffer[i] == *driver_image_handle) {
2507 buffer[i] = NULL;
2508 r = efi_bind_controller(
2509 controller_handle,
2510 *driver_image_handle,
2511 remain_device_path);
2512 /*
2513 * For drivers that do not support the
2514 * controller or are already connected
2515 * we receive an error code here.
2516 */
2517 if (r == EFI_SUCCESS)
2518 ++connected;
2519 }
2520 }
2521 }
2522 }
2523
2524 /*
2525 * TODO: Some overrides are not yet implemented:
2526 * - Platform Driver Override
2527 * - Driver Family Override Search
2528 * - Bus Specific Driver Override
2529 */
2530
2531 /* Driver Binding Search */
2532 for (i = 0; i < count; ++i) {
2533 if (buffer[i]) {
2534 r = efi_bind_controller(controller_handle,
2535 buffer[i],
2536 remain_device_path);
2537 if (r == EFI_SUCCESS)
2538 ++connected;
2539 }
2540 }
2541
2542 efi_free_pool(buffer);
2543 if (!connected)
2544 return EFI_NOT_FOUND;
2545 return EFI_SUCCESS;
2546}
2547
2548/*
2549 * Connect a controller to a driver.
2550 *
2551 * This function implements the ConnectController service.
2552 * See the Unified Extensible Firmware Interface (UEFI) specification
2553 * for details.
2554 *
2555 * First all driver binding protocol handles are tried for binding drivers.
2556 * Afterwards all handles that have openened a protocol of the controller
2557 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2558 *
2559 * @controller_handle handle of the controller
2560 * @driver_image_handle handle of the driver
2561 * @remain_device_path device path of a child controller
2562 * @recursive true to connect all child controllers
2563 * @return status code
2564 */
2565static efi_status_t EFIAPI efi_connect_controller(
2566 efi_handle_t controller_handle,
2567 efi_handle_t *driver_image_handle,
2568 struct efi_device_path *remain_device_path,
2569 bool recursive)
2570{
2571 efi_status_t r;
2572 efi_status_t ret = EFI_NOT_FOUND;
2573 struct efi_object *efiobj;
2574
2575 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2576 remain_device_path, recursive);
2577
2578 efiobj = efi_search_obj(controller_handle);
2579 if (!efiobj) {
2580 ret = EFI_INVALID_PARAMETER;
2581 goto out;
2582 }
2583
2584 r = efi_connect_single_controller(controller_handle,
2585 driver_image_handle,
2586 remain_device_path);
2587 if (r == EFI_SUCCESS)
2588 ret = EFI_SUCCESS;
2589 if (recursive) {
2590 struct efi_handler *handler;
2591 struct efi_open_protocol_info_item *item;
2592
2593 list_for_each_entry(handler, &efiobj->protocols, link) {
2594 list_for_each_entry(item, &handler->open_infos, link) {
2595 if (item->info.attributes &
2596 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2597 r = EFI_CALL(efi_connect_controller(
2598 item->info.controller_handle,
2599 driver_image_handle,
2600 remain_device_path,
2601 recursive));
2602 if (r == EFI_SUCCESS)
2603 ret = EFI_SUCCESS;
2604 }
2605 }
2606 }
2607 }
2608 /* Check for child controller specified by end node */
2609 if (ret != EFI_SUCCESS && remain_device_path &&
2610 remain_device_path->type == DEVICE_PATH_TYPE_END)
2611 ret = EFI_SUCCESS;
2612out:
2613 return EFI_EXIT(ret);
2614}
2615
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002616/*
2617 * Get all child controllers associated to a driver.
2618 * The allocated buffer has to be freed with free().
2619 *
2620 * @efiobj handle of the controller
2621 * @driver_handle handle of the driver
2622 * @number_of_children number of child controllers
2623 * @child_handle_buffer handles of the the child controllers
2624 */
2625static efi_status_t efi_get_child_controllers(
2626 struct efi_object *efiobj,
2627 efi_handle_t driver_handle,
2628 efi_uintn_t *number_of_children,
2629 efi_handle_t **child_handle_buffer)
2630{
2631 struct efi_handler *handler;
2632 struct efi_open_protocol_info_item *item;
2633 efi_uintn_t count = 0, i;
2634 bool duplicate;
2635
2636 /* Count all child controller associations */
2637 list_for_each_entry(handler, &efiobj->protocols, link) {
2638 list_for_each_entry(item, &handler->open_infos, link) {
2639 if (item->info.agent_handle == driver_handle &&
2640 item->info.attributes &
2641 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2642 ++count;
2643 }
2644 }
2645 /*
2646 * Create buffer. In case of duplicate child controller assignments
2647 * the buffer will be too large. But that does not harm.
2648 */
2649 *number_of_children = 0;
2650 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2651 if (!*child_handle_buffer)
2652 return EFI_OUT_OF_RESOURCES;
2653 /* Copy unique child handles */
2654 list_for_each_entry(handler, &efiobj->protocols, link) {
2655 list_for_each_entry(item, &handler->open_infos, link) {
2656 if (item->info.agent_handle == driver_handle &&
2657 item->info.attributes &
2658 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2659 /* Check this is a new child controller */
2660 duplicate = false;
2661 for (i = 0; i < *number_of_children; ++i) {
2662 if ((*child_handle_buffer)[i] ==
2663 item->info.controller_handle)
2664 duplicate = true;
2665 }
2666 /* Copy handle to buffer */
2667 if (!duplicate) {
2668 i = (*number_of_children)++;
2669 (*child_handle_buffer)[i] =
2670 item->info.controller_handle;
2671 }
2672 }
2673 }
2674 }
2675 return EFI_SUCCESS;
2676}
2677
2678/*
2679 * Disconnect a controller from a driver.
2680 *
2681 * This function implements the DisconnectController service.
2682 * See the Unified Extensible Firmware Interface (UEFI) specification
2683 * for details.
2684 *
2685 * @controller_handle handle of the controller
2686 * @driver_image_handle handle of the driver
2687 * @child_handle handle of the child to destroy
2688 * @return status code
2689 */
2690static efi_status_t EFIAPI efi_disconnect_controller(
2691 efi_handle_t controller_handle,
2692 efi_handle_t driver_image_handle,
2693 efi_handle_t child_handle)
2694{
2695 struct efi_driver_binding_protocol *binding_protocol;
2696 efi_handle_t *child_handle_buffer = NULL;
2697 size_t number_of_children = 0;
2698 efi_status_t r;
2699 size_t stop_count = 0;
2700 struct efi_object *efiobj;
2701
2702 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2703 child_handle);
2704
2705 efiobj = efi_search_obj(controller_handle);
2706 if (!efiobj) {
2707 r = EFI_INVALID_PARAMETER;
2708 goto out;
2709 }
2710
2711 if (child_handle && !efi_search_obj(child_handle)) {
2712 r = EFI_INVALID_PARAMETER;
2713 goto out;
2714 }
2715
2716 /* If no driver handle is supplied, disconnect all drivers */
2717 if (!driver_image_handle) {
2718 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2719 goto out;
2720 }
2721
2722 /* Create list of child handles */
2723 if (child_handle) {
2724 number_of_children = 1;
2725 child_handle_buffer = &child_handle;
2726 } else {
2727 efi_get_child_controllers(efiobj,
2728 driver_image_handle,
2729 &number_of_children,
2730 &child_handle_buffer);
2731 }
2732
2733 /* Get the driver binding protocol */
2734 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2735 &efi_guid_driver_binding_protocol,
2736 (void **)&binding_protocol,
2737 driver_image_handle, NULL,
2738 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2739 if (r != EFI_SUCCESS)
2740 goto out;
2741 /* Remove the children */
2742 if (number_of_children) {
2743 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2744 controller_handle,
2745 number_of_children,
2746 child_handle_buffer));
2747 if (r == EFI_SUCCESS)
2748 ++stop_count;
2749 }
2750 /* Remove the driver */
2751 if (!child_handle)
2752 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2753 controller_handle,
2754 0, NULL));
2755 if (r == EFI_SUCCESS)
2756 ++stop_count;
2757 EFI_CALL(efi_close_protocol(driver_image_handle,
2758 &efi_guid_driver_binding_protocol,
2759 driver_image_handle, NULL));
2760
2761 if (stop_count)
2762 r = EFI_SUCCESS;
2763 else
2764 r = EFI_NOT_FOUND;
2765out:
2766 if (!child_handle)
2767 free(child_handle_buffer);
2768 return EFI_EXIT(r);
2769}
2770
Alexander Grafbee91162016-03-04 01:09:59 +01002771static const struct efi_boot_services efi_boot_services = {
2772 .hdr = {
2773 .headersize = sizeof(struct efi_table_hdr),
2774 },
2775 .raise_tpl = efi_raise_tpl,
2776 .restore_tpl = efi_restore_tpl,
2777 .allocate_pages = efi_allocate_pages_ext,
2778 .free_pages = efi_free_pages_ext,
2779 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002780 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002781 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002782 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002783 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002784 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002785 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002786 .close_event = efi_close_event,
2787 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002788 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002789 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002790 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002791 .handle_protocol = efi_handle_protocol,
2792 .reserved = NULL,
2793 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002794 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002795 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002796 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002797 .load_image = efi_load_image,
2798 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002799 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002800 .unload_image = efi_unload_image,
2801 .exit_boot_services = efi_exit_boot_services,
2802 .get_next_monotonic_count = efi_get_next_monotonic_count,
2803 .stall = efi_stall,
2804 .set_watchdog_timer = efi_set_watchdog_timer,
2805 .connect_controller = efi_connect_controller,
2806 .disconnect_controller = efi_disconnect_controller,
2807 .open_protocol = efi_open_protocol,
2808 .close_protocol = efi_close_protocol,
2809 .open_protocol_information = efi_open_protocol_information,
2810 .protocols_per_handle = efi_protocols_per_handle,
2811 .locate_handle_buffer = efi_locate_handle_buffer,
2812 .locate_protocol = efi_locate_protocol,
2813 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2814 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2815 .calculate_crc32 = efi_calculate_crc32,
2816 .copy_mem = efi_copy_mem,
2817 .set_mem = efi_set_mem,
2818};
2819
2820
Heinrich Schuchardt05b6f562017-12-11 20:10:20 +01002821static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01002822
Alexander Graf3c63db92016-10-14 13:45:30 +02002823struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002824 .hdr = {
2825 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2826 .revision = 0x20005, /* 2.5 */
2827 .headersize = sizeof(struct efi_table_hdr),
2828 },
2829 .fw_vendor = (long)firmware_vendor,
2830 .con_in = (void*)&efi_con_in,
2831 .con_out = (void*)&efi_con_out,
2832 .std_err = (void*)&efi_con_out,
2833 .runtime = (void*)&efi_runtime_services,
2834 .boottime = (void*)&efi_boot_services,
2835 .nr_tables = 0,
2836 .tables = (void*)efi_conf_table,
2837};