blob: f5dae40f06625fc24b1eb4f35502438e8a18c97b [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/*
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100114 * Return a string for indenting with two spaces per level. A maximum of ten
115 * indent levels is supported. Higher indent levels will be truncated.
116 *
117 * @level indent level
118 * @return indent string
Rob Clarkaf65db82017-07-27 08:04:19 -0400119 */
120static const char *indent_string(int level)
121{
122 const char *indent = " ";
123 const int max = strlen(indent);
124 level = min(max, level * 2);
125 return &indent[max - level];
126}
127
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200128const char *__efi_nesting(void)
129{
130 return indent_string(nesting_level);
131}
132
Rob Clarkaf65db82017-07-27 08:04:19 -0400133const char *__efi_nesting_inc(void)
134{
135 return indent_string(nesting_level++);
136}
137
138const char *__efi_nesting_dec(void)
139{
140 return indent_string(--nesting_level);
141}
142
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200143/*
144 * Queue an EFI event.
145 *
146 * This function queues the notification function of the event for future
147 * execution.
148 *
149 * The notification function is called if the task priority level of the
150 * event is higher than the current task priority level.
151 *
152 * For the SignalEvent service see efi_signal_event_ext.
153 *
154 * @event event to signal
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100155 * @check_tpl check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200156 */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100157void efi_signal_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200158{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200159 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200160 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200161 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100162 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200163 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200164 EFI_CALL_VOID(event->notify_function(event,
165 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200166 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200167 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200168}
169
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200170/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200171 * Raise the task priority level.
172 *
173 * This function implements the RaiseTpl service.
174 * See the Unified Extensible Firmware Interface (UEFI) specification
175 * for details.
176 *
177 * @new_tpl new value of the task priority level
178 * @return old value of the task priority level
179 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100180static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100181{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100182 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200183
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200184 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200185
186 if (new_tpl < efi_tpl)
187 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
188 efi_tpl = new_tpl;
189 if (efi_tpl > TPL_HIGH_LEVEL)
190 efi_tpl = TPL_HIGH_LEVEL;
191
192 EFI_EXIT(EFI_SUCCESS);
193 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100194}
195
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200196/*
197 * Lower the task priority level.
198 *
199 * This function implements the RestoreTpl service.
200 * See the Unified Extensible Firmware Interface (UEFI) specification
201 * for details.
202 *
203 * @old_tpl value of the task priority level to be restored
204 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100205static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100206{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200207 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200208
209 if (old_tpl > efi_tpl)
210 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
211 efi_tpl = old_tpl;
212 if (efi_tpl > TPL_HIGH_LEVEL)
213 efi_tpl = TPL_HIGH_LEVEL;
214
215 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100216}
217
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200218/*
219 * Allocate memory pages.
220 *
221 * This function implements the AllocatePages service.
222 * See the Unified Extensible Firmware Interface (UEFI) specification
223 * for details.
224 *
225 * @type type of allocation to be performed
226 * @memory_type usage type of the allocated memory
227 * @pages number of pages to be allocated
228 * @memory allocated memory
229 * @return status code
230 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900231static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100232 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900233 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100234{
235 efi_status_t r;
236
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100237 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100238 r = efi_allocate_pages(type, memory_type, pages, memory);
239 return EFI_EXIT(r);
240}
241
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200242/*
243 * Free memory pages.
244 *
245 * This function implements the FreePages service.
246 * See the Unified Extensible Firmware Interface (UEFI) specification
247 * for details.
248 *
249 * @memory start of the memory area to be freed
250 * @pages number of pages to be freed
251 * @return status code
252 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900253static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100254 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100255{
256 efi_status_t r;
257
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100258 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100259 r = efi_free_pages(memory, pages);
260 return EFI_EXIT(r);
261}
262
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200263/*
264 * Get map describing memory usage.
265 *
266 * This function implements the GetMemoryMap service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
268 * for details.
269 *
270 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
271 * on exit the size of the copied memory map
272 * @memory_map buffer to which the memory map is written
273 * @map_key key for the memory map
274 * @descriptor_size size of an individual memory descriptor
275 * @descriptor_version version number of the memory descriptor structure
276 * @return status code
277 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900278static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100279 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900280 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100281 efi_uintn_t *map_key,
282 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900283 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100284{
285 efi_status_t r;
286
287 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
288 map_key, descriptor_size, descriptor_version);
289 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
290 descriptor_size, descriptor_version);
291 return EFI_EXIT(r);
292}
293
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200294/*
295 * Allocate memory from pool.
296 *
297 * This function implements the AllocatePool service.
298 * See the Unified Extensible Firmware Interface (UEFI) specification
299 * for details.
300 *
301 * @pool_type type of the pool from which memory is to be allocated
302 * @size number of bytes to be allocated
303 * @buffer allocated memory
304 * @return status code
305 */
Stefan Brünsead12742016-10-09 22:17:18 +0200306static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100307 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200308 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100309{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100310 efi_status_t r;
311
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100312 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200313 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100314 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100315}
316
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200317/*
318 * Free memory from pool.
319 *
320 * This function implements the FreePool service.
321 * See the Unified Extensible Firmware Interface (UEFI) specification
322 * for details.
323 *
324 * @buffer start of memory to be freed
325 * @return status code
326 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200327static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100328{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100329 efi_status_t r;
330
331 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200332 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100333 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100334}
335
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200336/*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100337 * Add a new object to the object list.
338 *
339 * The protocols list is initialized.
340 * The object handle is set.
341 *
342 * @obj object to be added
343 */
344void efi_add_handle(struct efi_object *obj)
345{
346 if (!obj)
347 return;
348 INIT_LIST_HEAD(&obj->protocols);
349 obj->handle = obj;
350 list_add_tail(&obj->link, &efi_obj_list);
351}
352
353/*
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200354 * Create handle.
355 *
356 * @handle new handle
357 * @return status code
358 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100359efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200360{
361 struct efi_object *obj;
362 efi_status_t r;
363
364 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
365 sizeof(struct efi_object),
366 (void **)&obj);
367 if (r != EFI_SUCCESS)
368 return r;
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100369 efi_add_handle(obj);
370 *handle = obj->handle;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200371 return r;
372}
373
Alexander Grafbee91162016-03-04 01:09:59 +0100374/*
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100375 * Find a protocol on a handle.
376 *
377 * @handle handle
378 * @protocol_guid GUID of the protocol
379 * @handler reference to the protocol
380 * @return status code
381 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100382efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100383 const efi_guid_t *protocol_guid,
384 struct efi_handler **handler)
385{
386 struct efi_object *efiobj;
387 struct list_head *lhandle;
388
389 if (!handle || !protocol_guid)
390 return EFI_INVALID_PARAMETER;
391 efiobj = efi_search_obj(handle);
392 if (!efiobj)
393 return EFI_INVALID_PARAMETER;
394 list_for_each(lhandle, &efiobj->protocols) {
395 struct efi_handler *protocol;
396
397 protocol = list_entry(lhandle, struct efi_handler, link);
398 if (!guidcmp(protocol->guid, protocol_guid)) {
399 if (handler)
400 *handler = protocol;
401 return EFI_SUCCESS;
402 }
403 }
404 return EFI_NOT_FOUND;
405}
406
407/*
408 * Delete protocol from a handle.
409 *
410 * @handle handle from which the protocol shall be deleted
411 * @protocol GUID of the protocol to be deleted
412 * @protocol_interface interface of the protocol implementation
413 * @return status code
414 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100415efi_status_t efi_remove_protocol(const efi_handle_t handle,
416 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100417 void *protocol_interface)
418{
419 struct efi_handler *handler;
420 efi_status_t ret;
421
422 ret = efi_search_protocol(handle, protocol, &handler);
423 if (ret != EFI_SUCCESS)
424 return ret;
425 if (guidcmp(handler->guid, protocol))
426 return EFI_INVALID_PARAMETER;
427 list_del(&handler->link);
428 free(handler);
429 return EFI_SUCCESS;
430}
431
432/*
433 * Delete all protocols from a handle.
434 *
435 * @handle handle from which the protocols shall be deleted
436 * @return status code
437 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100438efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100439{
440 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100441 struct efi_handler *protocol;
442 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100443
444 efiobj = efi_search_obj(handle);
445 if (!efiobj)
446 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100447 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100448 efi_status_t ret;
449
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100450 ret = efi_remove_protocol(handle, protocol->guid,
451 protocol->protocol_interface);
452 if (ret != EFI_SUCCESS)
453 return ret;
454 }
455 return EFI_SUCCESS;
456}
457
458/*
459 * Delete handle.
460 *
461 * @handle handle to delete
462 */
463void efi_delete_handle(struct efi_object *obj)
464{
465 if (!obj)
466 return;
467 efi_remove_all_protocols(obj->handle);
468 list_del(&obj->link);
469 free(obj);
470}
471
472/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200473 * Our event capabilities are very limited. Only a small limited
474 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100475 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200476static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100477
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200478/*
479 * Create an event.
480 *
481 * This function is used inside U-Boot code to create an event.
482 *
483 * For the API function implementing the CreateEvent service see
484 * efi_create_event_ext.
485 *
486 * @type type of the event to create
487 * @notify_tpl task priority level of the event
488 * @notify_function notification function of the event
489 * @notify_context pointer passed to the notification function
490 * @event created event
491 * @return status code
492 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100493efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200494 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200495 struct efi_event *event,
496 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200497 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100498{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200499 int i;
500
Jonathan Graya95343b2017-03-12 19:26:07 +1100501 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200502 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100503
504 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200505 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100506
507 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
508 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200509 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100510
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200511 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
512 if (efi_events[i].type)
513 continue;
514 efi_events[i].type = type;
515 efi_events[i].notify_tpl = notify_tpl;
516 efi_events[i].notify_function = notify_function;
517 efi_events[i].notify_context = notify_context;
518 /* Disable timers on bootup */
519 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200520 efi_events[i].is_queued = false;
521 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200522 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200523 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200524 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200525 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100526}
527
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200528/*
529 * Create an event.
530 *
531 * This function implements the CreateEvent service.
532 * See the Unified Extensible Firmware Interface (UEFI) specification
533 * for details.
534 *
535 * @type type of the event to create
536 * @notify_tpl task priority level of the event
537 * @notify_function notification function of the event
538 * @notify_context pointer passed to the notification function
539 * @event created event
540 * @return status code
541 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200542static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100543 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200544 void (EFIAPI *notify_function) (
545 struct efi_event *event,
546 void *context),
547 void *notify_context, struct efi_event **event)
548{
549 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
550 notify_context);
551 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
552 notify_context, event));
553}
554
555
Alexander Grafbee91162016-03-04 01:09:59 +0100556/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200557 * Check if a timer event has occurred or a queued notification function should
558 * be called.
559 *
Alexander Grafbee91162016-03-04 01:09:59 +0100560 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200561 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100562 */
563void efi_timer_check(void)
564{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200565 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100566 u64 now = timer_get_us();
567
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200568 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200569 if (!efi_events[i].type)
570 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200571 if (efi_events[i].is_queued)
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100572 efi_signal_event(&efi_events[i], true);
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200573 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200574 now < efi_events[i].trigger_next)
575 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200576 switch (efi_events[i].trigger_type) {
577 case EFI_TIMER_RELATIVE:
578 efi_events[i].trigger_type = EFI_TIMER_STOP;
579 break;
580 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200581 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200582 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200583 break;
584 default:
585 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200586 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200587 efi_events[i].is_signaled = true;
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100588 efi_signal_event(&efi_events[i], true);
Alexander Grafbee91162016-03-04 01:09:59 +0100589 }
Alexander Grafbee91162016-03-04 01:09:59 +0100590 WATCHDOG_RESET();
591}
592
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200593/*
594 * Set the trigger time for a timer event or stop the event.
595 *
596 * This is the function for internal usage in U-Boot. For the API function
597 * implementing the SetTimer service see efi_set_timer_ext.
598 *
599 * @event event for which the timer is set
600 * @type type of the timer
601 * @trigger_time trigger period in multiples of 100ns
602 * @return status code
603 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200604efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200605 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100606{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200607 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100608
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200609 /*
610 * The parameter defines a multiple of 100ns.
611 * We use multiples of 1000ns. So divide by 10.
612 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200613 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100614
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200615 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
616 if (event != &efi_events[i])
617 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100618
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200619 if (!(event->type & EVT_TIMER))
620 break;
621 switch (type) {
622 case EFI_TIMER_STOP:
623 event->trigger_next = -1ULL;
624 break;
625 case EFI_TIMER_PERIODIC:
626 case EFI_TIMER_RELATIVE:
627 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200628 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200629 break;
630 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200631 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200632 }
633 event->trigger_type = type;
634 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200635 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200636 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100637 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200638 return EFI_INVALID_PARAMETER;
639}
640
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200641/*
642 * Set the trigger time for a timer event or stop the event.
643 *
644 * This function implements the SetTimer service.
645 * See the Unified Extensible Firmware Interface (UEFI) specification
646 * for details.
647 *
648 * @event event for which the timer is set
649 * @type type of the timer
650 * @trigger_time trigger period in multiples of 100ns
651 * @return status code
652 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200653static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
654 enum efi_timer_delay type,
655 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200656{
657 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
658 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100659}
660
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200661/*
662 * Wait for events to be signaled.
663 *
664 * This function implements the WaitForEvent service.
665 * See the Unified Extensible Firmware Interface (UEFI) specification
666 * for details.
667 *
668 * @num_events number of events to be waited for
669 * @events events to be waited for
670 * @index index of the event that was signaled
671 * @return status code
672 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100673static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200674 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100675 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100676{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200677 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100678
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100679 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100680
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200681 /* Check parameters */
682 if (!num_events || !event)
683 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200684 /* Check TPL */
685 if (efi_tpl != TPL_APPLICATION)
686 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200687 for (i = 0; i < num_events; ++i) {
688 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
689 if (event[i] == &efi_events[j])
690 goto known_event;
691 }
692 return EFI_EXIT(EFI_INVALID_PARAMETER);
693known_event:
694 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
695 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200696 if (!event[i]->is_signaled)
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100697 efi_signal_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200698 }
699
700 /* Wait for signal */
701 for (;;) {
702 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200703 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200704 goto out;
705 }
706 /* Allow events to occur. */
707 efi_timer_check();
708 }
709
710out:
711 /*
712 * Reset the signal which is passed to the caller to allow periodic
713 * events to occur.
714 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200715 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200716 if (index)
717 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100718
719 return EFI_EXIT(EFI_SUCCESS);
720}
721
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200722/*
723 * Signal an EFI event.
724 *
725 * This function implements the SignalEvent service.
726 * See the Unified Extensible Firmware Interface (UEFI) specification
727 * for details.
728 *
729 * This functions sets the signaled state of the event and queues the
730 * notification function for execution.
731 *
732 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200733 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200734 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200735static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100736{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200737 int i;
738
Alexander Grafbee91162016-03-04 01:09:59 +0100739 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200740 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
741 if (event != &efi_events[i])
742 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200743 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200744 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200745 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200746 if (event->type & EVT_NOTIFY_SIGNAL)
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100747 efi_signal_event(event, true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200748 break;
749 }
Alexander Grafbee91162016-03-04 01:09:59 +0100750 return EFI_EXIT(EFI_SUCCESS);
751}
752
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200753/*
754 * Close an EFI event.
755 *
756 * This function implements the CloseEvent service.
757 * See the Unified Extensible Firmware Interface (UEFI) specification
758 * for details.
759 *
760 * @event event to close
761 * @return status code
762 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200763static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100764{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200765 int i;
766
Alexander Grafbee91162016-03-04 01:09:59 +0100767 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200768 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
769 if (event == &efi_events[i]) {
770 event->type = 0;
771 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200772 event->is_queued = false;
773 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200774 return EFI_EXIT(EFI_SUCCESS);
775 }
776 }
777 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100778}
779
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200780/*
781 * Check if an event is signaled.
782 *
783 * This function implements the CheckEvent service.
784 * See the Unified Extensible Firmware Interface (UEFI) specification
785 * for details.
786 *
787 * If an event is not signaled yet the notification function is queued.
788 *
789 * @event event to check
790 * @return status code
791 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200792static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100793{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200794 int i;
795
Alexander Grafbee91162016-03-04 01:09:59 +0100796 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200797 efi_timer_check();
798 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
799 if (event != &efi_events[i])
800 continue;
801 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
802 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200803 if (!event->is_signaled)
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100804 efi_signal_event(event, true);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200805 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200806 return EFI_EXIT(EFI_SUCCESS);
807 return EFI_EXIT(EFI_NOT_READY);
808 }
809 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100810}
811
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200812/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200813 * Find the internal EFI object for a handle.
814 *
815 * @handle handle to find
816 * @return EFI object
817 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100818struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200819{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100820 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200821
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100822 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200823 if (efiobj->handle == handle)
824 return efiobj;
825 }
826
827 return NULL;
828}
829
830/*
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100831 * Create open protocol info entry and add it to a protocol.
832 *
833 * @handler handler of a protocol
834 * @return open protocol info entry
835 */
836static struct efi_open_protocol_info_entry *efi_create_open_info(
837 struct efi_handler *handler)
838{
839 struct efi_open_protocol_info_item *item;
840
841 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
842 if (!item)
843 return NULL;
844 /* Append the item to the open protocol info list. */
845 list_add_tail(&item->link, &handler->open_infos);
846
847 return &item->info;
848}
849
850/*
851 * Remove an open protocol info entry from a protocol.
852 *
853 * @handler handler of a protocol
854 * @return status code
855 */
856static efi_status_t efi_delete_open_info(
857 struct efi_open_protocol_info_item *item)
858{
859 list_del(&item->link);
860 free(item);
861 return EFI_SUCCESS;
862}
863
864/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200865 * Install new protocol on a handle.
866 *
867 * @handle handle on which the protocol shall be installed
868 * @protocol GUID of the protocol to be installed
869 * @protocol_interface interface of the protocol implementation
870 * @return status code
871 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100872efi_status_t efi_add_protocol(const efi_handle_t handle,
873 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200874 void *protocol_interface)
875{
876 struct efi_object *efiobj;
877 struct efi_handler *handler;
878 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200879
880 efiobj = efi_search_obj(handle);
881 if (!efiobj)
882 return EFI_INVALID_PARAMETER;
883 ret = efi_search_protocol(handle, protocol, NULL);
884 if (ret != EFI_NOT_FOUND)
885 return EFI_INVALID_PARAMETER;
886 handler = calloc(1, sizeof(struct efi_handler));
887 if (!handler)
888 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100889 handler->guid = protocol;
890 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100891 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100892 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +0100893 if (!guidcmp(&efi_guid_device_path, protocol))
894 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100895 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200896}
897
898/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200899 * Install protocol interface.
900 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100901 * This function implements the InstallProtocolInterface service.
902 * See the Unified Extensible Firmware Interface (UEFI) specification
903 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200904 *
905 * @handle handle on which the protocol shall be installed
906 * @protocol GUID of the protocol to be installed
907 * @protocol_interface_type type of the interface to be installed,
908 * always EFI_NATIVE_INTERFACE
909 * @protocol_interface interface of the protocol implementation
910 * @return status code
911 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100912static efi_status_t EFIAPI efi_install_protocol_interface(
913 void **handle, const efi_guid_t *protocol,
914 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100915{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200916 efi_status_t r;
917
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100918 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
919 protocol_interface);
920
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200921 if (!handle || !protocol ||
922 protocol_interface_type != EFI_NATIVE_INTERFACE) {
923 r = EFI_INVALID_PARAMETER;
924 goto out;
925 }
926
927 /* Create new handle if requested. */
928 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200929 r = efi_create_handle(handle);
930 if (r != EFI_SUCCESS)
931 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200932 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
933 *handle);
934 } else {
935 debug("%sEFI: handle %p\n", indent_string(nesting_level),
936 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200937 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200938 /* Add new protocol */
939 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200940out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100941 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100942}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200943
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200944/*
945 * Reinstall protocol interface.
946 *
947 * This function implements the ReinstallProtocolInterface service.
948 * See the Unified Extensible Firmware Interface (UEFI) specification
949 * for details.
950 *
951 * @handle handle on which the protocol shall be
952 * reinstalled
953 * @protocol GUID of the protocol to be installed
954 * @old_interface interface to be removed
955 * @new_interface interface to be installed
956 * @return status code
957 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100958static efi_status_t EFIAPI efi_reinstall_protocol_interface(
959 efi_handle_t handle, const efi_guid_t *protocol,
960 void *old_interface, void *new_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100961{
Rob Clark778e6af2017-09-13 18:05:41 -0400962 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100963 new_interface);
964 return EFI_EXIT(EFI_ACCESS_DENIED);
965}
966
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200967/*
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +0100968 * Get all drivers associated to a controller.
969 * The allocated buffer has to be freed with free().
970 *
971 * @efiobj handle of the controller
972 * @protocol protocol guid (optional)
973 * @number_of_drivers number of child controllers
974 * @driver_handle_buffer handles of the the drivers
975 * @return status code
976 */
977static efi_status_t efi_get_drivers(struct efi_object *efiobj,
978 const efi_guid_t *protocol,
979 efi_uintn_t *number_of_drivers,
980 efi_handle_t **driver_handle_buffer)
981{
982 struct efi_handler *handler;
983 struct efi_open_protocol_info_item *item;
984 efi_uintn_t count = 0, i;
985 bool duplicate;
986
987 /* Count all driver associations */
988 list_for_each_entry(handler, &efiobj->protocols, link) {
989 if (protocol && guidcmp(handler->guid, protocol))
990 continue;
991 list_for_each_entry(item, &handler->open_infos, link) {
992 if (item->info.attributes &
993 EFI_OPEN_PROTOCOL_BY_DRIVER)
994 ++count;
995 }
996 }
997 /*
998 * Create buffer. In case of duplicate driver assignments the buffer
999 * will be too large. But that does not harm.
1000 */
1001 *number_of_drivers = 0;
1002 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1003 if (!*driver_handle_buffer)
1004 return EFI_OUT_OF_RESOURCES;
1005 /* Collect unique driver handles */
1006 list_for_each_entry(handler, &efiobj->protocols, link) {
1007 if (protocol && guidcmp(handler->guid, protocol))
1008 continue;
1009 list_for_each_entry(item, &handler->open_infos, link) {
1010 if (item->info.attributes &
1011 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1012 /* Check this is a new driver */
1013 duplicate = false;
1014 for (i = 0; i < *number_of_drivers; ++i) {
1015 if ((*driver_handle_buffer)[i] ==
1016 item->info.agent_handle)
1017 duplicate = true;
1018 }
1019 /* Copy handle to buffer */
1020 if (!duplicate) {
1021 i = (*number_of_drivers)++;
1022 (*driver_handle_buffer)[i] =
1023 item->info.agent_handle;
1024 }
1025 }
1026 }
1027 }
1028 return EFI_SUCCESS;
1029}
1030
1031/*
1032 * Disconnect all drivers from a controller.
1033 *
1034 * This function implements the DisconnectController service.
1035 * See the Unified Extensible Firmware Interface (UEFI) specification
1036 * for details.
1037 *
1038 * @efiobj handle of the controller
1039 * @protocol protocol guid (optional)
1040 * @child_handle handle of the child to destroy
1041 * @return status code
1042 */
1043static efi_status_t efi_disconnect_all_drivers(
1044 struct efi_object *efiobj,
1045 const efi_guid_t *protocol,
1046 efi_handle_t child_handle)
1047{
1048 efi_uintn_t number_of_drivers;
1049 efi_handle_t *driver_handle_buffer;
1050 efi_status_t r, ret;
1051
1052 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1053 &driver_handle_buffer);
1054 if (ret != EFI_SUCCESS)
1055 return ret;
1056
1057 ret = EFI_NOT_FOUND;
1058 while (number_of_drivers) {
1059 r = EFI_CALL(efi_disconnect_controller(
1060 efiobj->handle,
1061 driver_handle_buffer[--number_of_drivers],
1062 child_handle));
1063 if (r == EFI_SUCCESS)
1064 ret = r;
1065 }
1066 free(driver_handle_buffer);
1067 return ret;
1068}
1069
1070/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001071 * Uninstall protocol interface.
1072 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001073 * This function implements the UninstallProtocolInterface service.
1074 * See the Unified Extensible Firmware Interface (UEFI) specification
1075 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001076 *
1077 * @handle handle from which the protocol shall be removed
1078 * @protocol GUID of the protocol to be removed
1079 * @protocol_interface interface to be removed
1080 * @return status code
1081 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001082static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001083 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001084 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001085{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001086 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001087 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001088 struct efi_open_protocol_info_item *item;
1089 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001090 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001091
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001092 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1093
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001094 /* Check handle */
1095 efiobj = efi_search_obj(handle);
1096 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001097 r = EFI_INVALID_PARAMETER;
1098 goto out;
1099 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001100 /* Find the protocol on the handle */
1101 r = efi_search_protocol(handle, protocol, &handler);
1102 if (r != EFI_SUCCESS)
1103 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001104 /* Disconnect controllers */
1105 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1106 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001107 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001108 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001109 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001110 /* Close protocol */
1111 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1112 if (item->info.attributes ==
1113 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1114 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1115 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1116 list_del(&item->link);
1117 }
1118 if (!list_empty(&handler->open_infos)) {
1119 r = EFI_ACCESS_DENIED;
1120 goto out;
1121 }
1122 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001123out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001124 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001125}
1126
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001127/*
1128 * Register an event for notification when a protocol is installed.
1129 *
1130 * This function implements the RegisterProtocolNotify service.
1131 * See the Unified Extensible Firmware Interface (UEFI) specification
1132 * for details.
1133 *
1134 * @protocol GUID of the protocol whose installation shall be
1135 * notified
1136 * @event event to be signaled upon installation of the protocol
1137 * @registration key for retrieving the registration information
1138 * @return status code
1139 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001140static efi_status_t EFIAPI efi_register_protocol_notify(
1141 const efi_guid_t *protocol,
1142 struct efi_event *event,
1143 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001144{
Rob Clark778e6af2017-09-13 18:05:41 -04001145 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001146 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1147}
1148
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001149/*
1150 * Determine if an EFI handle implements a protocol.
1151 *
1152 * See the documentation of the LocateHandle service in the UEFI specification.
1153 *
1154 * @search_type selection criterion
1155 * @protocol GUID of the protocol
1156 * @search_key registration key
1157 * @efiobj handle
1158 * @return 0 if the handle implements the protocol
1159 */
Alexander Grafbee91162016-03-04 01:09:59 +01001160static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001161 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001162 struct efi_object *efiobj)
1163{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001164 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001165
1166 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001167 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001168 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001169 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001170 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001171 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001172 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001173 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1174 return (ret != EFI_SUCCESS);
1175 default:
1176 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001177 return -1;
1178 }
Alexander Grafbee91162016-03-04 01:09:59 +01001179}
1180
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001181/*
1182 * Locate handles implementing a protocol.
1183 *
1184 * This function is meant for U-Boot internal calls. For the API implementation
1185 * of the LocateHandle service see efi_locate_handle_ext.
1186 *
1187 * @search_type selection criterion
1188 * @protocol GUID of the protocol
1189 * @search_key registration key
1190 * @buffer_size size of the buffer to receive the handles in bytes
1191 * @buffer buffer to receive the relevant handles
1192 * @return status code
1193 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001194static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001195 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001196 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001197 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001198{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001199 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001200 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001201
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001202 /* Check parameters */
1203 switch (search_type) {
1204 case ALL_HANDLES:
1205 break;
1206 case BY_REGISTER_NOTIFY:
1207 if (!search_key)
1208 return EFI_INVALID_PARAMETER;
1209 /* RegisterProtocolNotify is not implemented yet */
1210 return EFI_UNSUPPORTED;
1211 case BY_PROTOCOL:
1212 if (!protocol)
1213 return EFI_INVALID_PARAMETER;
1214 break;
1215 default:
1216 return EFI_INVALID_PARAMETER;
1217 }
1218
1219 /*
1220 * efi_locate_handle_buffer uses this function for
1221 * the calculation of the necessary buffer size.
1222 * So do not require a buffer for buffersize == 0.
1223 */
1224 if (!buffer_size || (*buffer_size && !buffer))
1225 return EFI_INVALID_PARAMETER;
1226
Alexander Grafbee91162016-03-04 01:09:59 +01001227 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001228 list_for_each_entry(efiobj, &efi_obj_list, link) {
1229 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001230 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001231 }
1232
1233 if (*buffer_size < size) {
1234 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001235 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001236 }
1237
Rob Clark796a78c2017-08-06 14:10:07 -04001238 *buffer_size = size;
1239 if (size == 0)
1240 return EFI_NOT_FOUND;
1241
Alexander Grafbee91162016-03-04 01:09:59 +01001242 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001243 list_for_each_entry(efiobj, &efi_obj_list, link) {
1244 if (!efi_search(search_type, protocol, search_key, efiobj))
1245 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001246 }
1247
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001248 return EFI_SUCCESS;
1249}
1250
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001251/*
1252 * Locate handles implementing a protocol.
1253 *
1254 * This function implements the LocateHandle service.
1255 * See the Unified Extensible Firmware Interface (UEFI) specification
1256 * for details.
1257 *
1258 * @search_type selection criterion
1259 * @protocol GUID of the protocol
1260 * @search_key registration key
1261 * @buffer_size size of the buffer to receive the handles in bytes
1262 * @buffer buffer to receive the relevant handles
1263 * @return 0 if the handle implements the protocol
1264 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001265static efi_status_t EFIAPI efi_locate_handle_ext(
1266 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001267 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001268 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001269{
Rob Clark778e6af2017-09-13 18:05:41 -04001270 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001271 buffer_size, buffer);
1272
1273 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1274 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001275}
1276
Alexander Grafd98cdf62017-07-26 13:41:04 +02001277/* Collapses configuration table entries, removing index i */
1278static void efi_remove_configuration_table(int i)
1279{
1280 struct efi_configuration_table *this = &efi_conf_table[i];
1281 struct efi_configuration_table *next = &efi_conf_table[i+1];
1282 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1283
1284 memmove(this, next, (ulong)end - (ulong)next);
1285 systab.nr_tables--;
1286}
1287
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001288/*
1289 * Adds, updates, or removes a configuration table.
1290 *
1291 * This function is used for internal calls. For the API implementation of the
1292 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1293 *
1294 * @guid GUID of the installed table
1295 * @table table to be installed
1296 * @return status code
1297 */
Alexander Graf488bf122016-08-19 01:23:24 +02001298efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001299{
1300 int i;
1301
Alexander Grafbee91162016-03-04 01:09:59 +01001302 /* Check for guid override */
1303 for (i = 0; i < systab.nr_tables; i++) {
1304 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001305 if (table)
1306 efi_conf_table[i].table = table;
1307 else
1308 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001309 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001310 }
1311 }
1312
Alexander Grafd98cdf62017-07-26 13:41:04 +02001313 if (!table)
1314 return EFI_NOT_FOUND;
1315
Alexander Grafbee91162016-03-04 01:09:59 +01001316 /* No override, check for overflow */
1317 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001318 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001319
1320 /* Add a new entry */
1321 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1322 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001323 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001324
Alexander Graf488bf122016-08-19 01:23:24 +02001325 return EFI_SUCCESS;
1326}
1327
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001328/*
1329 * Adds, updates, or removes a configuration table.
1330 *
1331 * This function implements the InstallConfigurationTable service.
1332 * See the Unified Extensible Firmware Interface (UEFI) specification
1333 * for details.
1334 *
1335 * @guid GUID of the installed table
1336 * @table table to be installed
1337 * @return status code
1338 */
Alexander Graf488bf122016-08-19 01:23:24 +02001339static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1340 void *table)
1341{
Rob Clark778e6af2017-09-13 18:05:41 -04001342 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001343 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001344}
1345
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001346/*
1347 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001348 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001349 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001350 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001351 * image
1352 * @obj internal object associated with the loaded image
1353 * @device_path device path of the loaded image
1354 * @file_path file path of the loaded image
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001355 * @return status code
Rob Clark95c55532017-09-13 18:05:33 -04001356 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001357efi_status_t efi_setup_loaded_image(
1358 struct efi_loaded_image *info, struct efi_object *obj,
1359 struct efi_device_path *device_path,
1360 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001361{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001362 efi_status_t ret;
1363
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001364 /* Add internal object to object list */
1365 efi_add_handle(obj);
1366 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001367 obj->handle = info;
1368
Rob Clark95c55532017-09-13 18:05:33 -04001369 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001370 if (device_path)
1371 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001372
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001373 /*
1374 * When asking for the device path interface, return
1375 * bootefi_device_path
1376 */
1377 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1378 if (ret != EFI_SUCCESS)
1379 goto failure;
1380
1381 /*
1382 * When asking for the loaded_image interface, just
1383 * return handle which points to loaded_image_info
1384 */
1385 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1386 if (ret != EFI_SUCCESS)
1387 goto failure;
1388
1389 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1390 (void *)&efi_console_control);
1391 if (ret != EFI_SUCCESS)
1392 goto failure;
1393
1394 ret = efi_add_protocol(obj->handle,
1395 &efi_guid_device_path_to_text_protocol,
1396 (void *)&efi_device_path_to_text);
1397 if (ret != EFI_SUCCESS)
1398 goto failure;
1399
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001400 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001401failure:
1402 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001403 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001404}
1405
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001406/*
1407 * Load an image using a file path.
1408 *
1409 * @file_path the path of the image to load
1410 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001411 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001412 */
Rob Clark9975fe92017-09-13 18:05:38 -04001413efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1414 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001415{
1416 struct efi_file_info *info = NULL;
1417 struct efi_file_handle *f;
1418 static efi_status_t ret;
1419 uint64_t bs;
1420
1421 f = efi_file_from_path(file_path);
1422 if (!f)
1423 return EFI_DEVICE_ERROR;
1424
1425 bs = 0;
1426 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1427 &bs, info));
1428 if (ret == EFI_BUFFER_TOO_SMALL) {
1429 info = malloc(bs);
1430 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1431 &bs, info));
1432 }
1433 if (ret != EFI_SUCCESS)
1434 goto error;
1435
1436 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1437 if (ret)
1438 goto error;
1439
1440 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1441
1442error:
1443 free(info);
1444 EFI_CALL(f->close(f));
1445
1446 if (ret != EFI_SUCCESS) {
1447 efi_free_pool(*buffer);
1448 *buffer = NULL;
1449 }
1450
1451 return ret;
1452}
1453
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001454/*
1455 * Load an EFI image into memory.
1456 *
1457 * This function implements the LoadImage service.
1458 * See the Unified Extensible Firmware Interface (UEFI) specification
1459 * for details.
1460 *
1461 * @boot_policy true for request originating from the boot manager
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +01001462 * @parent_image the caller's image handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001463 * @file_path the path of the image to load
1464 * @source_buffer memory location from which the image is installed
1465 * @source_size size of the memory area from which the image is
1466 * installed
1467 * @image_handle handle for the newly installed image
1468 * @return status code
1469 */
Alexander Grafbee91162016-03-04 01:09:59 +01001470static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1471 efi_handle_t parent_image,
1472 struct efi_device_path *file_path,
1473 void *source_buffer,
1474 unsigned long source_size,
1475 efi_handle_t *image_handle)
1476{
Alexander Grafbee91162016-03-04 01:09:59 +01001477 struct efi_loaded_image *info;
1478 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001479 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001480
Heinrich Schuchardt476ed962018-01-19 20:24:40 +01001481 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001482 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001483
1484 info = calloc(1, sizeof(*info));
1485 obj = calloc(1, sizeof(*obj));
1486
1487 if (!source_buffer) {
1488 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001489
Rob Clark9975fe92017-09-13 18:05:38 -04001490 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001491 if (ret != EFI_SUCCESS)
1492 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001493 /*
1494 * split file_path which contains both the device and
1495 * file parts:
1496 */
1497 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001498 ret = efi_setup_loaded_image(info, obj, dp, fp);
1499 if (ret != EFI_SUCCESS)
1500 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001501 } else {
1502 /* In this case, file_path is the "device" path, ie.
1503 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1504 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001505 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1506 if (ret != EFI_SUCCESS)
1507 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001508 }
Alexander Grafbee91162016-03-04 01:09:59 +01001509 info->reserved = efi_load_pe(source_buffer, info);
1510 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001511 ret = EFI_UNSUPPORTED;
1512 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001513 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001514 info->system_table = &systab;
1515 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001516 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001517 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001518failure:
1519 free(info);
1520 efi_delete_handle(obj);
1521 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001522}
1523
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001524/*
1525 * Call the entry point of an image.
1526 *
1527 * This function implements the StartImage service.
1528 * See the Unified Extensible Firmware Interface (UEFI) specification
1529 * for details.
1530 *
1531 * @image_handle handle of the image
1532 * @exit_data_size size of the buffer
1533 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001534 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001535 */
Alexander Grafbee91162016-03-04 01:09:59 +01001536static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1537 unsigned long *exit_data_size,
1538 s16 **exit_data)
1539{
Alexander Grafc6fa5df2018-01-24 00:18:08 +01001540 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1541 struct efi_system_table *st);
Alexander Grafbee91162016-03-04 01:09:59 +01001542 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001543 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001544
1545 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1546 entry = info->reserved;
1547
1548 efi_is_direct_boot = false;
1549
1550 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001551 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001552 /*
1553 * We called the entry point of the child image with EFI_CALL
1554 * in the lines below. The child image called the Exit() boot
1555 * service efi_exit() which executed the long jump that brought
1556 * us to the current line. This implies that the second half
1557 * of the EFI_CALL macro has not been executed.
1558 */
1559#ifdef CONFIG_ARM
1560 /*
1561 * efi_exit() called efi_restore_gd(). We have to undo this
1562 * otherwise __efi_entry_check() will put the wrong value into
1563 * app_gd.
1564 */
1565 gd = app_gd;
1566#endif
1567 /*
1568 * To get ready to call EFI_EXIT below we have to execute the
1569 * missed out steps of EFI_CALL.
1570 */
1571 assert(__efi_entry_check());
1572 debug("%sEFI: %lu returned by started image\n",
1573 __efi_nesting_dec(),
1574 (unsigned long)((uintptr_t)info->exit_status &
1575 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001576 return EFI_EXIT(info->exit_status);
1577 }
1578
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001579 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001580
1581 /* Should usually never get here */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001582 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001583}
1584
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001585/*
1586 * Leave an EFI application or driver.
1587 *
1588 * This function implements the Exit service.
1589 * See the Unified Extensible Firmware Interface (UEFI) specification
1590 * for details.
1591 *
1592 * @image_handle handle of the application or driver that is exiting
1593 * @exit_status status code
1594 * @exit_data_size size of the buffer in bytes
1595 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001596 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001597 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001598static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1599 efi_status_t exit_status, unsigned long exit_data_size,
1600 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001601{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001602 /*
1603 * We require that the handle points to the original loaded
1604 * image protocol interface.
1605 *
1606 * For getting the longjmp address this is safer than locating
1607 * the protocol because the protocol may have been reinstalled
1608 * pointing to another memory location.
1609 *
1610 * TODO: We should call the unload procedure of the loaded
1611 * image protocol.
1612 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001613 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1614
Alexander Grafbee91162016-03-04 01:09:59 +01001615 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1616 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001617
Alexander Grafa1489202017-09-03 14:14:17 +02001618 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001619 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001620
Alexander Grafa1489202017-09-03 14:14:17 +02001621 /*
1622 * But longjmp out with the U-Boot gd, not the application's, as
1623 * the other end is a setjmp call inside EFI context.
1624 */
1625 efi_restore_gd();
1626
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001627 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001628 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001629
1630 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001631}
1632
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001633/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001634 * Unload an EFI image.
1635 *
1636 * This function implements the UnloadImage service.
1637 * See the Unified Extensible Firmware Interface (UEFI) specification
1638 * for details.
1639 *
1640 * @image_handle handle of the image to be unloaded
1641 * @return status code
1642 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001643static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001644{
1645 struct efi_object *efiobj;
1646
1647 EFI_ENTRY("%p", image_handle);
1648 efiobj = efi_search_obj(image_handle);
1649 if (efiobj)
1650 list_del(&efiobj->link);
1651
1652 return EFI_EXIT(EFI_SUCCESS);
1653}
1654
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001655/*
1656 * Fix up caches for EFI payloads if necessary.
1657 */
Alexander Grafbee91162016-03-04 01:09:59 +01001658static void efi_exit_caches(void)
1659{
1660#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1661 /*
1662 * Grub on 32bit ARM needs to have caches disabled before jumping into
1663 * a zImage, but does not know of all cache layers. Give it a hand.
1664 */
1665 if (efi_is_direct_boot)
1666 cleanup_before_linux();
1667#endif
1668}
1669
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001670/*
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001671 * Stop all boot services.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001672 *
1673 * This function implements the ExitBootServices service.
1674 * See the Unified Extensible Firmware Interface (UEFI) specification
1675 * for details.
1676 *
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001677 * All timer events are disabled.
1678 * For exit boot services events the notification function is called.
1679 * The boot services are disabled in the system table.
1680 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001681 * @image_handle handle of the loaded image
1682 * @map_key key of the memory map
1683 * @return status code
1684 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001685static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001686 unsigned long map_key)
1687{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001688 int i;
1689
Alexander Grafbee91162016-03-04 01:09:59 +01001690 EFI_ENTRY("%p, %ld", image_handle, map_key);
1691
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001692 /* Make sure that notification functions are not called anymore */
1693 efi_tpl = TPL_HIGH_LEVEL;
1694
1695 /* Check if ExitBootServices has already been called */
1696 if (!systab.boottime)
1697 return EFI_EXIT(EFI_SUCCESS);
1698
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001699 /* Notify that ExitBootServices is invoked. */
1700 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1701 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1702 continue;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001703 efi_events[i].is_signaled = true;
1704 efi_signal_event(&efi_events[i], false);
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001705 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001706
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001707 /* TODO Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001708
Alexander Grafb7b84102016-11-17 01:02:57 +01001709 board_quiesce_devices();
1710
Alexander Grafbee91162016-03-04 01:09:59 +01001711 /* Fix up caches for EFI payloads if necessary */
1712 efi_exit_caches();
1713
1714 /* This stops all lingering devices */
1715 bootm_disable_interrupts();
1716
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001717 /* Disable boottime services */
1718 systab.con_in_handle = NULL;
1719 systab.con_in = NULL;
1720 systab.con_out_handle = NULL;
1721 systab.con_out = NULL;
1722 systab.stderr_handle = NULL;
1723 systab.std_err = NULL;
1724 systab.boottime = NULL;
1725
1726 /* Recalculate CRC32 */
1727 systab.hdr.crc32 = 0;
1728 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1729 sizeof(struct efi_system_table));
1730
Alexander Grafbee91162016-03-04 01:09:59 +01001731 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001732 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001733 WATCHDOG_RESET();
1734
1735 return EFI_EXIT(EFI_SUCCESS);
1736}
1737
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001738/*
1739 * Get next value of the counter.
1740 *
1741 * This function implements the NextMonotonicCount service.
1742 * See the Unified Extensible Firmware Interface (UEFI) specification
1743 * for details.
1744 *
1745 * @count returned value of the counter
1746 * @return status code
1747 */
Alexander Grafbee91162016-03-04 01:09:59 +01001748static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1749{
1750 static uint64_t mono = 0;
1751 EFI_ENTRY("%p", count);
1752 *count = mono++;
1753 return EFI_EXIT(EFI_SUCCESS);
1754}
1755
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001756/*
1757 * Sleep.
1758 *
1759 * This function implements the Stall sercive.
1760 * See the Unified Extensible Firmware Interface (UEFI) specification
1761 * for details.
1762 *
1763 * @microseconds period to sleep in microseconds
1764 * @return status code
1765 */
Alexander Grafbee91162016-03-04 01:09:59 +01001766static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1767{
1768 EFI_ENTRY("%ld", microseconds);
1769 udelay(microseconds);
1770 return EFI_EXIT(EFI_SUCCESS);
1771}
1772
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001773/*
1774 * Reset the watchdog timer.
1775 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001776 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001777 * See the Unified Extensible Firmware Interface (UEFI) specification
1778 * for details.
1779 *
1780 * @timeout seconds before reset by watchdog
1781 * @watchdog_code code to be logged when resetting
1782 * @data_size size of buffer in bytes
1783 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001784 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001785 */
Alexander Grafbee91162016-03-04 01:09:59 +01001786static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1787 uint64_t watchdog_code,
1788 unsigned long data_size,
1789 uint16_t *watchdog_data)
1790{
1791 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1792 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001793 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001794}
1795
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001796/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001797 * Close a protocol.
1798 *
1799 * This function implements the CloseProtocol service.
1800 * See the Unified Extensible Firmware Interface (UEFI) specification
1801 * for details.
1802 *
1803 * @handle handle on which the protocol shall be closed
1804 * @protocol GUID of the protocol to close
1805 * @agent_handle handle of the driver
1806 * @controller_handle handle of the controller
1807 * @return status code
1808 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001809static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001810 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001811 efi_handle_t agent_handle,
1812 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001813{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001814 struct efi_handler *handler;
1815 struct efi_open_protocol_info_item *item;
1816 struct efi_open_protocol_info_item *pos;
1817 efi_status_t r;
1818
Rob Clark778e6af2017-09-13 18:05:41 -04001819 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001820 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001821
1822 if (!agent_handle) {
1823 r = EFI_INVALID_PARAMETER;
1824 goto out;
1825 }
1826 r = efi_search_protocol(handle, protocol, &handler);
1827 if (r != EFI_SUCCESS)
1828 goto out;
1829
1830 r = EFI_NOT_FOUND;
1831 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1832 if (item->info.agent_handle == agent_handle &&
1833 item->info.controller_handle == controller_handle) {
1834 efi_delete_open_info(item);
1835 r = EFI_SUCCESS;
1836 break;
1837 }
1838 }
1839out:
1840 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001841}
1842
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001843/*
1844 * Provide information about then open status of a protocol on a handle
1845 *
1846 * This function implements the OpenProtocolInformation service.
1847 * See the Unified Extensible Firmware Interface (UEFI) specification
1848 * for details.
1849 *
1850 * @handle handle for which the information shall be retrieved
1851 * @protocol GUID of the protocol
1852 * @entry_buffer buffer to receive the open protocol information
1853 * @entry_count number of entries available in the buffer
1854 * @return status code
1855 */
Alexander Grafbee91162016-03-04 01:09:59 +01001856static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001857 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001858 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001859 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001860{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001861 unsigned long buffer_size;
1862 unsigned long count;
1863 struct efi_handler *handler;
1864 struct efi_open_protocol_info_item *item;
1865 efi_status_t r;
1866
Rob Clark778e6af2017-09-13 18:05:41 -04001867 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001868 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001869
1870 /* Check parameters */
1871 if (!entry_buffer) {
1872 r = EFI_INVALID_PARAMETER;
1873 goto out;
1874 }
1875 r = efi_search_protocol(handle, protocol, &handler);
1876 if (r != EFI_SUCCESS)
1877 goto out;
1878
1879 /* Count entries */
1880 count = 0;
1881 list_for_each_entry(item, &handler->open_infos, link) {
1882 if (item->info.open_count)
1883 ++count;
1884 }
1885 *entry_count = count;
1886 *entry_buffer = NULL;
1887 if (!count) {
1888 r = EFI_SUCCESS;
1889 goto out;
1890 }
1891
1892 /* Copy entries */
1893 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1894 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1895 (void **)entry_buffer);
1896 if (r != EFI_SUCCESS)
1897 goto out;
1898 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1899 if (item->info.open_count)
1900 (*entry_buffer)[--count] = item->info;
1901 }
1902out:
1903 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001904}
1905
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001906/*
1907 * Get protocols installed on a handle.
1908 *
1909 * This function implements the ProtocolsPerHandleService.
1910 * See the Unified Extensible Firmware Interface (UEFI) specification
1911 * for details.
1912 *
1913 * @handle handle for which the information is retrieved
1914 * @protocol_buffer buffer with protocol GUIDs
1915 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001916 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001917 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001918static efi_status_t EFIAPI efi_protocols_per_handle(
1919 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001920 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001921{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001922 unsigned long buffer_size;
1923 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001924 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001925 efi_status_t r;
1926
Alexander Grafbee91162016-03-04 01:09:59 +01001927 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1928 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001929
1930 if (!handle || !protocol_buffer || !protocol_buffer_count)
1931 return EFI_EXIT(EFI_INVALID_PARAMETER);
1932
1933 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001934 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001935
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001936 efiobj = efi_search_obj(handle);
1937 if (!efiobj)
1938 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001939
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001940 /* Count protocols */
1941 list_for_each(protocol_handle, &efiobj->protocols) {
1942 ++*protocol_buffer_count;
1943 }
1944
1945 /* Copy guids */
1946 if (*protocol_buffer_count) {
1947 size_t j = 0;
1948
1949 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1950 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1951 (void **)protocol_buffer);
1952 if (r != EFI_SUCCESS)
1953 return EFI_EXIT(r);
1954 list_for_each(protocol_handle, &efiobj->protocols) {
1955 struct efi_handler *protocol;
1956
1957 protocol = list_entry(protocol_handle,
1958 struct efi_handler, link);
1959 (*protocol_buffer)[j] = (void *)protocol->guid;
1960 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001961 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001962 }
1963
1964 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001965}
1966
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001967/*
1968 * Locate handles implementing a protocol.
1969 *
1970 * This function implements the LocateHandleBuffer service.
1971 * See the Unified Extensible Firmware Interface (UEFI) specification
1972 * for details.
1973 *
1974 * @search_type selection criterion
1975 * @protocol GUID of the protocol
1976 * @search_key registration key
1977 * @no_handles number of returned handles
1978 * @buffer buffer with the returned handles
1979 * @return status code
1980 */
Alexander Grafbee91162016-03-04 01:09:59 +01001981static efi_status_t EFIAPI efi_locate_handle_buffer(
1982 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001983 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001984 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001985{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001986 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001987 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001988
Rob Clark778e6af2017-09-13 18:05:41 -04001989 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001990 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001991
1992 if (!no_handles || !buffer) {
1993 r = EFI_INVALID_PARAMETER;
1994 goto out;
1995 }
1996 *no_handles = 0;
1997 *buffer = NULL;
1998 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1999 *buffer);
2000 if (r != EFI_BUFFER_TOO_SMALL)
2001 goto out;
2002 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2003 (void **)buffer);
2004 if (r != EFI_SUCCESS)
2005 goto out;
2006 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2007 *buffer);
2008 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002009 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002010out:
2011 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002012}
2013
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002014/*
2015 * Find an interface implementing a protocol.
2016 *
2017 * This function implements the LocateProtocol service.
2018 * See the Unified Extensible Firmware Interface (UEFI) specification
2019 * for details.
2020 *
2021 * @protocol GUID of the protocol
2022 * @registration registration key passed to the notification function
2023 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02002024 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002025 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002026static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002027 void *registration,
2028 void **protocol_interface)
2029{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002030 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002031 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002032
Rob Clark778e6af2017-09-13 18:05:41 -04002033 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002034
2035 if (!protocol || !protocol_interface)
2036 return EFI_EXIT(EFI_INVALID_PARAMETER);
2037
2038 list_for_each(lhandle, &efi_obj_list) {
2039 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002040 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002041
2042 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002043
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002044 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2045 if (ret == EFI_SUCCESS) {
2046 *protocol_interface = handler->protocol_interface;
2047 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002048 }
2049 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002050 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002051
2052 return EFI_EXIT(EFI_NOT_FOUND);
2053}
2054
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002055/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002056 * Get the device path and handle of an device implementing a protocol.
2057 *
2058 * This function implements the LocateDevicePath service.
2059 * See the Unified Extensible Firmware Interface (UEFI) specification
2060 * for details.
2061 *
2062 * @protocol GUID of the protocol
2063 * @device_path device path
2064 * @device handle of the device
2065 * @return status code
2066 */
2067static efi_status_t EFIAPI efi_locate_device_path(
2068 const efi_guid_t *protocol,
2069 struct efi_device_path **device_path,
2070 efi_handle_t *device)
2071{
2072 struct efi_device_path *dp;
2073 size_t i;
2074 struct efi_handler *handler;
2075 efi_handle_t *handles;
2076 size_t len, len_dp;
2077 size_t len_best = 0;
2078 efi_uintn_t no_handles;
2079 u8 *remainder;
2080 efi_status_t ret;
2081
2082 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2083
2084 if (!protocol || !device_path || !*device_path || !device) {
2085 ret = EFI_INVALID_PARAMETER;
2086 goto out;
2087 }
2088
2089 /* Find end of device path */
2090 len = efi_dp_size(*device_path);
2091
2092 /* Get all handles implementing the protocol */
2093 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2094 &no_handles, &handles));
2095 if (ret != EFI_SUCCESS)
2096 goto out;
2097
2098 for (i = 0; i < no_handles; ++i) {
2099 /* Find the device path protocol */
2100 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2101 &handler);
2102 if (ret != EFI_SUCCESS)
2103 continue;
2104 dp = (struct efi_device_path *)handler->protocol_interface;
2105 len_dp = efi_dp_size(dp);
2106 /*
2107 * This handle can only be a better fit
2108 * if its device path length is longer than the best fit and
2109 * if its device path length is shorter of equal the searched
2110 * device path.
2111 */
2112 if (len_dp <= len_best || len_dp > len)
2113 continue;
2114 /* Check if dp is a subpath of device_path */
2115 if (memcmp(*device_path, dp, len_dp))
2116 continue;
2117 *device = handles[i];
2118 len_best = len_dp;
2119 }
2120 if (len_best) {
2121 remainder = (u8 *)*device_path + len_best;
2122 *device_path = (struct efi_device_path *)remainder;
2123 ret = EFI_SUCCESS;
2124 } else {
2125 ret = EFI_NOT_FOUND;
2126 }
2127out:
2128 return EFI_EXIT(ret);
2129}
2130
2131/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002132 * Install multiple protocol interfaces.
2133 *
2134 * This function implements the MultipleProtocolInterfaces service.
2135 * See the Unified Extensible Firmware Interface (UEFI) specification
2136 * for details.
2137 *
2138 * @handle handle on which the protocol interfaces shall be installed
2139 * @... NULL terminated argument list with pairs of protocol GUIDS and
2140 * interfaces
2141 * @return status code
2142 */
Alexander Grafbee91162016-03-04 01:09:59 +01002143static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2144 void **handle, ...)
2145{
2146 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002147
2148 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002149 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002150 void *protocol_interface;
2151 efi_status_t r = EFI_SUCCESS;
2152 int i = 0;
2153
2154 if (!handle)
2155 return EFI_EXIT(EFI_INVALID_PARAMETER);
2156
2157 va_start(argptr, handle);
2158 for (;;) {
2159 protocol = va_arg(argptr, efi_guid_t*);
2160 if (!protocol)
2161 break;
2162 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002163 r = EFI_CALL(efi_install_protocol_interface(
2164 handle, protocol,
2165 EFI_NATIVE_INTERFACE,
2166 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002167 if (r != EFI_SUCCESS)
2168 break;
2169 i++;
2170 }
2171 va_end(argptr);
2172 if (r == EFI_SUCCESS)
2173 return EFI_EXIT(r);
2174
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002175 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002176 va_start(argptr, handle);
2177 for (; i; --i) {
2178 protocol = va_arg(argptr, efi_guid_t*);
2179 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002180 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2181 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002182 }
2183 va_end(argptr);
2184
2185 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002186}
2187
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002188/*
2189 * Uninstall multiple protocol interfaces.
2190 *
2191 * This function implements the UninstallMultipleProtocolInterfaces service.
2192 * See the Unified Extensible Firmware Interface (UEFI) specification
2193 * for details.
2194 *
2195 * @handle handle from which the protocol interfaces shall be removed
2196 * @... NULL terminated argument list with pairs of protocol GUIDS and
2197 * interfaces
2198 * @return status code
2199 */
Alexander Grafbee91162016-03-04 01:09:59 +01002200static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2201 void *handle, ...)
2202{
2203 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002204
2205 va_list argptr;
2206 const efi_guid_t *protocol;
2207 void *protocol_interface;
2208 efi_status_t r = EFI_SUCCESS;
2209 size_t i = 0;
2210
2211 if (!handle)
2212 return EFI_EXIT(EFI_INVALID_PARAMETER);
2213
2214 va_start(argptr, handle);
2215 for (;;) {
2216 protocol = va_arg(argptr, efi_guid_t*);
2217 if (!protocol)
2218 break;
2219 protocol_interface = va_arg(argptr, void*);
2220 r = EFI_CALL(efi_uninstall_protocol_interface(
2221 handle, protocol,
2222 protocol_interface));
2223 if (r != EFI_SUCCESS)
2224 break;
2225 i++;
2226 }
2227 va_end(argptr);
2228 if (r == EFI_SUCCESS)
2229 return EFI_EXIT(r);
2230
2231 /* If an error occurred undo all changes. */
2232 va_start(argptr, handle);
2233 for (; i; --i) {
2234 protocol = va_arg(argptr, efi_guid_t*);
2235 protocol_interface = va_arg(argptr, void*);
2236 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2237 EFI_NATIVE_INTERFACE,
2238 protocol_interface));
2239 }
2240 va_end(argptr);
2241
2242 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002243}
2244
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002245/*
2246 * Calculate cyclic redundancy code.
2247 *
2248 * This function implements the CalculateCrc32 service.
2249 * See the Unified Extensible Firmware Interface (UEFI) specification
2250 * for details.
2251 *
2252 * @data buffer with data
2253 * @data_size size of buffer in bytes
2254 * @crc32_p cyclic redundancy code
2255 * @return status code
2256 */
Alexander Grafbee91162016-03-04 01:09:59 +01002257static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2258 unsigned long data_size,
2259 uint32_t *crc32_p)
2260{
2261 EFI_ENTRY("%p, %ld", data, data_size);
2262 *crc32_p = crc32(0, data, data_size);
2263 return EFI_EXIT(EFI_SUCCESS);
2264}
2265
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002266/*
2267 * Copy memory.
2268 *
2269 * This function implements the CopyMem service.
2270 * See the Unified Extensible Firmware Interface (UEFI) specification
2271 * for details.
2272 *
2273 * @destination destination of the copy operation
2274 * @source source of the copy operation
2275 * @length number of bytes to copy
2276 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002277static void EFIAPI efi_copy_mem(void *destination, const void *source,
2278 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002279{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002280 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002281 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002282 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002283}
2284
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002285/*
2286 * Fill memory with a byte value.
2287 *
2288 * This function implements the SetMem service.
2289 * See the Unified Extensible Firmware Interface (UEFI) specification
2290 * for details.
2291 *
2292 * @buffer buffer to fill
2293 * @size size of buffer in bytes
2294 * @value byte to copy to the buffer
2295 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002296static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002297{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002298 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002299 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002300 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002301}
2302
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002303/*
2304 * Open protocol interface on a handle.
2305 *
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002306 * @handler handler of a protocol
2307 * @protocol_interface interface implementing the protocol
2308 * @agent_handle handle of the driver
2309 * @controller_handle handle of the controller
2310 * @attributes attributes indicating how to open the protocol
2311 * @return status code
2312 */
2313static efi_status_t efi_protocol_open(
2314 struct efi_handler *handler,
2315 void **protocol_interface, void *agent_handle,
2316 void *controller_handle, uint32_t attributes)
2317{
2318 struct efi_open_protocol_info_item *item;
2319 struct efi_open_protocol_info_entry *match = NULL;
2320 bool opened_by_driver = false;
2321 bool opened_exclusive = false;
2322
2323 /* If there is no agent, only return the interface */
2324 if (!agent_handle)
2325 goto out;
2326
2327 /* For TEST_PROTOCOL ignore interface attribute */
2328 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2329 *protocol_interface = NULL;
2330
2331 /*
2332 * Check if the protocol is already opened by a driver with the same
2333 * attributes or opened exclusively
2334 */
2335 list_for_each_entry(item, &handler->open_infos, link) {
2336 if (item->info.agent_handle == agent_handle) {
2337 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2338 (item->info.attributes == attributes))
2339 return EFI_ALREADY_STARTED;
2340 }
2341 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2342 opened_exclusive = true;
2343 }
2344
2345 /* Only one controller can open the protocol exclusively */
2346 if (opened_exclusive && attributes &
2347 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2348 return EFI_ACCESS_DENIED;
2349
2350 /* Prepare exclusive opening */
2351 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2352 /* Try to disconnect controllers */
2353 list_for_each_entry(item, &handler->open_infos, link) {
2354 if (item->info.attributes ==
2355 EFI_OPEN_PROTOCOL_BY_DRIVER)
2356 EFI_CALL(efi_disconnect_controller(
2357 item->info.controller_handle,
2358 item->info.agent_handle,
2359 NULL));
2360 }
2361 opened_by_driver = false;
2362 /* Check if all controllers are disconnected */
2363 list_for_each_entry(item, &handler->open_infos, link) {
2364 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2365 opened_by_driver = true;
2366 }
2367 /* Only one controller can be conncected */
2368 if (opened_by_driver)
2369 return EFI_ACCESS_DENIED;
2370 }
2371
2372 /* Find existing entry */
2373 list_for_each_entry(item, &handler->open_infos, link) {
2374 if (item->info.agent_handle == agent_handle &&
2375 item->info.controller_handle == controller_handle)
2376 match = &item->info;
2377 }
2378 /* None found, create one */
2379 if (!match) {
2380 match = efi_create_open_info(handler);
2381 if (!match)
2382 return EFI_OUT_OF_RESOURCES;
2383 }
2384
2385 match->agent_handle = agent_handle;
2386 match->controller_handle = controller_handle;
2387 match->attributes = attributes;
2388 match->open_count++;
2389
2390out:
2391 /* For TEST_PROTOCOL ignore interface attribute. */
2392 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2393 *protocol_interface = handler->protocol_interface;
2394
2395 return EFI_SUCCESS;
2396}
2397
2398/*
2399 * Open protocol interface on a handle.
2400 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002401 * This function implements the OpenProtocol interface.
2402 * See the Unified Extensible Firmware Interface (UEFI) specification
2403 * for details.
2404 *
2405 * @handle handle on which the protocol shall be opened
2406 * @protocol GUID of the protocol
2407 * @protocol_interface interface implementing the protocol
2408 * @agent_handle handle of the driver
2409 * @controller_handle handle of the controller
2410 * @attributes attributes indicating how to open the protocol
2411 * @return status code
2412 */
Alexander Grafbee91162016-03-04 01:09:59 +01002413static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002414 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002415 void **protocol_interface, void *agent_handle,
2416 void *controller_handle, uint32_t attributes)
2417{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002418 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002419 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002420
Rob Clark778e6af2017-09-13 18:05:41 -04002421 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002422 protocol_interface, agent_handle, controller_handle,
2423 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002424
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002425 if (!handle || !protocol ||
2426 (!protocol_interface && attributes !=
2427 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2428 goto out;
2429 }
2430
2431 switch (attributes) {
2432 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2433 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2434 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2435 break;
2436 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2437 if (controller_handle == handle)
2438 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002439 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002440 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2441 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002442 /* Check that the controller handle is valid */
2443 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002444 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002445 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002446 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002447 /* Check that the agent handle is valid */
2448 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002449 goto out;
2450 break;
2451 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002452 goto out;
2453 }
2454
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002455 r = efi_search_protocol(handle, protocol, &handler);
2456 if (r != EFI_SUCCESS)
2457 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002458
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002459 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2460 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002461out:
2462 return EFI_EXIT(r);
2463}
2464
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002465/*
2466 * Get interface of a protocol on a handle.
2467 *
2468 * This function implements the HandleProtocol service.
2469 * See the Unified Extensible Firmware Interface (UEFI) specification
2470 * for details.
2471 *
2472 * @handle handle on which the protocol shall be opened
2473 * @protocol GUID of the protocol
2474 * @protocol_interface interface implementing the protocol
2475 * @return status code
2476 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002477static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002478 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002479 void **protocol_interface)
2480{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002481 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2482 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002483}
2484
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002485static efi_status_t efi_bind_controller(
2486 efi_handle_t controller_handle,
2487 efi_handle_t driver_image_handle,
2488 struct efi_device_path *remain_device_path)
2489{
2490 struct efi_driver_binding_protocol *binding_protocol;
2491 efi_status_t r;
2492
2493 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2494 &efi_guid_driver_binding_protocol,
2495 (void **)&binding_protocol,
2496 driver_image_handle, NULL,
2497 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2498 if (r != EFI_SUCCESS)
2499 return r;
2500 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2501 controller_handle,
2502 remain_device_path));
2503 if (r == EFI_SUCCESS)
2504 r = EFI_CALL(binding_protocol->start(binding_protocol,
2505 controller_handle,
2506 remain_device_path));
2507 EFI_CALL(efi_close_protocol(driver_image_handle,
2508 &efi_guid_driver_binding_protocol,
2509 driver_image_handle, NULL));
2510 return r;
2511}
2512
2513static efi_status_t efi_connect_single_controller(
2514 efi_handle_t controller_handle,
2515 efi_handle_t *driver_image_handle,
2516 struct efi_device_path *remain_device_path)
2517{
2518 efi_handle_t *buffer;
2519 size_t count;
2520 size_t i;
2521 efi_status_t r;
2522 size_t connected = 0;
2523
2524 /* Get buffer with all handles with driver binding protocol */
2525 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2526 &efi_guid_driver_binding_protocol,
2527 NULL, &count, &buffer));
2528 if (r != EFI_SUCCESS)
2529 return r;
2530
2531 /* Context Override */
2532 if (driver_image_handle) {
2533 for (; *driver_image_handle; ++driver_image_handle) {
2534 for (i = 0; i < count; ++i) {
2535 if (buffer[i] == *driver_image_handle) {
2536 buffer[i] = NULL;
2537 r = efi_bind_controller(
2538 controller_handle,
2539 *driver_image_handle,
2540 remain_device_path);
2541 /*
2542 * For drivers that do not support the
2543 * controller or are already connected
2544 * we receive an error code here.
2545 */
2546 if (r == EFI_SUCCESS)
2547 ++connected;
2548 }
2549 }
2550 }
2551 }
2552
2553 /*
2554 * TODO: Some overrides are not yet implemented:
2555 * - Platform Driver Override
2556 * - Driver Family Override Search
2557 * - Bus Specific Driver Override
2558 */
2559
2560 /* Driver Binding Search */
2561 for (i = 0; i < count; ++i) {
2562 if (buffer[i]) {
2563 r = efi_bind_controller(controller_handle,
2564 buffer[i],
2565 remain_device_path);
2566 if (r == EFI_SUCCESS)
2567 ++connected;
2568 }
2569 }
2570
2571 efi_free_pool(buffer);
2572 if (!connected)
2573 return EFI_NOT_FOUND;
2574 return EFI_SUCCESS;
2575}
2576
2577/*
2578 * Connect a controller to a driver.
2579 *
2580 * This function implements the ConnectController service.
2581 * See the Unified Extensible Firmware Interface (UEFI) specification
2582 * for details.
2583 *
2584 * First all driver binding protocol handles are tried for binding drivers.
2585 * Afterwards all handles that have openened a protocol of the controller
2586 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2587 *
2588 * @controller_handle handle of the controller
2589 * @driver_image_handle handle of the driver
2590 * @remain_device_path device path of a child controller
2591 * @recursive true to connect all child controllers
2592 * @return status code
2593 */
2594static efi_status_t EFIAPI efi_connect_controller(
2595 efi_handle_t controller_handle,
2596 efi_handle_t *driver_image_handle,
2597 struct efi_device_path *remain_device_path,
2598 bool recursive)
2599{
2600 efi_status_t r;
2601 efi_status_t ret = EFI_NOT_FOUND;
2602 struct efi_object *efiobj;
2603
2604 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2605 remain_device_path, recursive);
2606
2607 efiobj = efi_search_obj(controller_handle);
2608 if (!efiobj) {
2609 ret = EFI_INVALID_PARAMETER;
2610 goto out;
2611 }
2612
2613 r = efi_connect_single_controller(controller_handle,
2614 driver_image_handle,
2615 remain_device_path);
2616 if (r == EFI_SUCCESS)
2617 ret = EFI_SUCCESS;
2618 if (recursive) {
2619 struct efi_handler *handler;
2620 struct efi_open_protocol_info_item *item;
2621
2622 list_for_each_entry(handler, &efiobj->protocols, link) {
2623 list_for_each_entry(item, &handler->open_infos, link) {
2624 if (item->info.attributes &
2625 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2626 r = EFI_CALL(efi_connect_controller(
2627 item->info.controller_handle,
2628 driver_image_handle,
2629 remain_device_path,
2630 recursive));
2631 if (r == EFI_SUCCESS)
2632 ret = EFI_SUCCESS;
2633 }
2634 }
2635 }
2636 }
2637 /* Check for child controller specified by end node */
2638 if (ret != EFI_SUCCESS && remain_device_path &&
2639 remain_device_path->type == DEVICE_PATH_TYPE_END)
2640 ret = EFI_SUCCESS;
2641out:
2642 return EFI_EXIT(ret);
2643}
2644
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002645/*
2646 * Get all child controllers associated to a driver.
2647 * The allocated buffer has to be freed with free().
2648 *
2649 * @efiobj handle of the controller
2650 * @driver_handle handle of the driver
2651 * @number_of_children number of child controllers
2652 * @child_handle_buffer handles of the the child controllers
2653 */
2654static efi_status_t efi_get_child_controllers(
2655 struct efi_object *efiobj,
2656 efi_handle_t driver_handle,
2657 efi_uintn_t *number_of_children,
2658 efi_handle_t **child_handle_buffer)
2659{
2660 struct efi_handler *handler;
2661 struct efi_open_protocol_info_item *item;
2662 efi_uintn_t count = 0, i;
2663 bool duplicate;
2664
2665 /* Count all child controller associations */
2666 list_for_each_entry(handler, &efiobj->protocols, link) {
2667 list_for_each_entry(item, &handler->open_infos, link) {
2668 if (item->info.agent_handle == driver_handle &&
2669 item->info.attributes &
2670 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2671 ++count;
2672 }
2673 }
2674 /*
2675 * Create buffer. In case of duplicate child controller assignments
2676 * the buffer will be too large. But that does not harm.
2677 */
2678 *number_of_children = 0;
2679 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2680 if (!*child_handle_buffer)
2681 return EFI_OUT_OF_RESOURCES;
2682 /* Copy unique child handles */
2683 list_for_each_entry(handler, &efiobj->protocols, link) {
2684 list_for_each_entry(item, &handler->open_infos, link) {
2685 if (item->info.agent_handle == driver_handle &&
2686 item->info.attributes &
2687 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2688 /* Check this is a new child controller */
2689 duplicate = false;
2690 for (i = 0; i < *number_of_children; ++i) {
2691 if ((*child_handle_buffer)[i] ==
2692 item->info.controller_handle)
2693 duplicate = true;
2694 }
2695 /* Copy handle to buffer */
2696 if (!duplicate) {
2697 i = (*number_of_children)++;
2698 (*child_handle_buffer)[i] =
2699 item->info.controller_handle;
2700 }
2701 }
2702 }
2703 }
2704 return EFI_SUCCESS;
2705}
2706
2707/*
2708 * Disconnect a controller from a driver.
2709 *
2710 * This function implements the DisconnectController service.
2711 * See the Unified Extensible Firmware Interface (UEFI) specification
2712 * for details.
2713 *
2714 * @controller_handle handle of the controller
2715 * @driver_image_handle handle of the driver
2716 * @child_handle handle of the child to destroy
2717 * @return status code
2718 */
2719static efi_status_t EFIAPI efi_disconnect_controller(
2720 efi_handle_t controller_handle,
2721 efi_handle_t driver_image_handle,
2722 efi_handle_t child_handle)
2723{
2724 struct efi_driver_binding_protocol *binding_protocol;
2725 efi_handle_t *child_handle_buffer = NULL;
2726 size_t number_of_children = 0;
2727 efi_status_t r;
2728 size_t stop_count = 0;
2729 struct efi_object *efiobj;
2730
2731 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2732 child_handle);
2733
2734 efiobj = efi_search_obj(controller_handle);
2735 if (!efiobj) {
2736 r = EFI_INVALID_PARAMETER;
2737 goto out;
2738 }
2739
2740 if (child_handle && !efi_search_obj(child_handle)) {
2741 r = EFI_INVALID_PARAMETER;
2742 goto out;
2743 }
2744
2745 /* If no driver handle is supplied, disconnect all drivers */
2746 if (!driver_image_handle) {
2747 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2748 goto out;
2749 }
2750
2751 /* Create list of child handles */
2752 if (child_handle) {
2753 number_of_children = 1;
2754 child_handle_buffer = &child_handle;
2755 } else {
2756 efi_get_child_controllers(efiobj,
2757 driver_image_handle,
2758 &number_of_children,
2759 &child_handle_buffer);
2760 }
2761
2762 /* Get the driver binding protocol */
2763 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2764 &efi_guid_driver_binding_protocol,
2765 (void **)&binding_protocol,
2766 driver_image_handle, NULL,
2767 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2768 if (r != EFI_SUCCESS)
2769 goto out;
2770 /* Remove the children */
2771 if (number_of_children) {
2772 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2773 controller_handle,
2774 number_of_children,
2775 child_handle_buffer));
2776 if (r == EFI_SUCCESS)
2777 ++stop_count;
2778 }
2779 /* Remove the driver */
2780 if (!child_handle)
2781 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2782 controller_handle,
2783 0, NULL));
2784 if (r == EFI_SUCCESS)
2785 ++stop_count;
2786 EFI_CALL(efi_close_protocol(driver_image_handle,
2787 &efi_guid_driver_binding_protocol,
2788 driver_image_handle, NULL));
2789
2790 if (stop_count)
2791 r = EFI_SUCCESS;
2792 else
2793 r = EFI_NOT_FOUND;
2794out:
2795 if (!child_handle)
2796 free(child_handle_buffer);
2797 return EFI_EXIT(r);
2798}
2799
Alexander Grafbee91162016-03-04 01:09:59 +01002800static const struct efi_boot_services efi_boot_services = {
2801 .hdr = {
2802 .headersize = sizeof(struct efi_table_hdr),
2803 },
2804 .raise_tpl = efi_raise_tpl,
2805 .restore_tpl = efi_restore_tpl,
2806 .allocate_pages = efi_allocate_pages_ext,
2807 .free_pages = efi_free_pages_ext,
2808 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002809 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002810 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002811 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002812 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002813 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002814 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002815 .close_event = efi_close_event,
2816 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002817 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002818 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002819 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002820 .handle_protocol = efi_handle_protocol,
2821 .reserved = NULL,
2822 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002823 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002824 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002825 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002826 .load_image = efi_load_image,
2827 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002828 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002829 .unload_image = efi_unload_image,
2830 .exit_boot_services = efi_exit_boot_services,
2831 .get_next_monotonic_count = efi_get_next_monotonic_count,
2832 .stall = efi_stall,
2833 .set_watchdog_timer = efi_set_watchdog_timer,
2834 .connect_controller = efi_connect_controller,
2835 .disconnect_controller = efi_disconnect_controller,
2836 .open_protocol = efi_open_protocol,
2837 .close_protocol = efi_close_protocol,
2838 .open_protocol_information = efi_open_protocol_information,
2839 .protocols_per_handle = efi_protocols_per_handle,
2840 .locate_handle_buffer = efi_locate_handle_buffer,
2841 .locate_protocol = efi_locate_protocol,
2842 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2843 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2844 .calculate_crc32 = efi_calculate_crc32,
2845 .copy_mem = efi_copy_mem,
2846 .set_mem = efi_set_mem,
2847};
2848
2849
Heinrich Schuchardt05b6f562017-12-11 20:10:20 +01002850static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01002851
Alexander Graf3c63db92016-10-14 13:45:30 +02002852struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002853 .hdr = {
2854 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2855 .revision = 0x20005, /* 2.5 */
2856 .headersize = sizeof(struct efi_table_hdr),
2857 },
2858 .fw_vendor = (long)firmware_vendor,
2859 .con_in = (void*)&efi_con_in,
2860 .con_out = (void*)&efi_con_out,
2861 .std_err = (void*)&efi_con_out,
2862 .runtime = (void*)&efi_runtime_services,
2863 .boottime = (void*)&efi_boot_services,
2864 .nr_tables = 0,
2865 .tables = (void*)efi_conf_table,
2866};