blob: a37fb25638ce35aea864a6d7c0ae92b18bdaaafd [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;
Rob Clarkc160d2f2017-07-27 08:04:18 -040059
60/* Called on every callback entry */
61int __efi_entry_check(void)
62{
63 int ret = entry_count++ == 0;
64#ifdef CONFIG_ARM
65 assert(efi_gd);
66 app_gd = gd;
67 gd = efi_gd;
68#endif
69 return ret;
70}
71
72/* Called on every callback exit */
73int __efi_exit_check(void)
74{
75 int ret = --entry_count == 0;
76#ifdef CONFIG_ARM
77 gd = app_gd;
78#endif
79 return ret;
80}
81
Alexander Grafbee91162016-03-04 01:09:59 +010082/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
Simon Glass65e4c0b2016-09-25 15:27:35 -060085#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010086 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060087#endif
Alexander Grafbee91162016-03-04 01:09:59 +010088}
89
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/*
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
94 */
Alexander Grafbee91162016-03-04 01:09:59 +010095void efi_restore_gd(void)
96{
Simon Glass65e4c0b2016-09-25 15:27:35 -060097#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010098 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100101 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600102#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100103}
104
Rob Clarkaf65db82017-07-27 08:04:19 -0400105/*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109static const char *indent_string(int level)
110{
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
115}
116
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
Rob Clarkaf65db82017-07-27 08:04:19 -0400122const char *__efi_nesting_inc(void)
123{
124 return indent_string(nesting_level++);
125}
126
127const char *__efi_nesting_dec(void)
128{
129 return indent_string(--nesting_level);
130}
131
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200132/*
133 * Queue an EFI event.
134 *
135 * This function queues the notification function of the event for future
136 * execution.
137 *
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
140 *
141 * For the SignalEvent service see efi_signal_event_ext.
142 *
143 * @event event to signal
144 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200145void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200146{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200147 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200148 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200154 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200155 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200156}
157
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200158/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 * Raise the task priority level.
160 *
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
163 * for details.
164 *
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
167 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100169{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100170 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200171
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200172 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 efi_tpl = new_tpl;
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
179
180 EFI_EXIT(EFI_SUCCESS);
181 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100182}
183
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200184/*
185 * Lower the task priority level.
186 *
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
189 * for details.
190 *
191 * @old_tpl value of the task priority level to be restored
192 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100194{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200195 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200196
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 efi_tpl = old_tpl;
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
202
203 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100204}
205
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200206/*
207 * Allocate memory pages.
208 *
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
211 * for details.
212 *
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
218 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100220 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900221 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100222{
223 efi_status_t r;
224
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200230/*
231 * Free memory pages.
232 *
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
235 * for details.
236 *
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
240 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100242 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100243{
244 efi_status_t r;
245
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200251/*
252 * Get map describing memory usage.
253 *
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
256 * for details.
257 *
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
265 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900266static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100267 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900268 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900271 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100272{
273 efi_status_t r;
274
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
279 return EFI_EXIT(r);
280}
281
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200282/*
283 * Allocate memory from pool.
284 *
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
287 * for details.
288 *
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
293 */
Stefan Brünsead12742016-10-09 22:17:18 +0200294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100295 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200296 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100297{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100298 efi_status_t r;
299
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200301 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100302 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100303}
304
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200305/*
306 * Free memory from pool.
307 *
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @buffer start of memory to be freed
313 * @return status code
314 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100316{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200320 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100321 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100322}
323
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200324/*
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100325 * Add a new object to the object list.
326 *
327 * The protocols list is initialized.
328 * The object handle is set.
329 *
330 * @obj object to be added
331 */
332void efi_add_handle(struct efi_object *obj)
333{
334 if (!obj)
335 return;
336 INIT_LIST_HEAD(&obj->protocols);
337 obj->handle = obj;
338 list_add_tail(&obj->link, &efi_obj_list);
339}
340
341/*
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200342 * Create handle.
343 *
344 * @handle new handle
345 * @return status code
346 */
347efi_status_t efi_create_handle(void **handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200348{
349 struct efi_object *obj;
350 efi_status_t r;
351
352 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
353 sizeof(struct efi_object),
354 (void **)&obj);
355 if (r != EFI_SUCCESS)
356 return r;
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100357 efi_add_handle(obj);
358 *handle = obj->handle;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200359 return r;
360}
361
Alexander Grafbee91162016-03-04 01:09:59 +0100362/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200363 * Our event capabilities are very limited. Only a small limited
364 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100365 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200366static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100367
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200368/*
369 * Create an event.
370 *
371 * This function is used inside U-Boot code to create an event.
372 *
373 * For the API function implementing the CreateEvent service see
374 * efi_create_event_ext.
375 *
376 * @type type of the event to create
377 * @notify_tpl task priority level of the event
378 * @notify_function notification function of the event
379 * @notify_context pointer passed to the notification function
380 * @event created event
381 * @return status code
382 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100383efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200384 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200385 struct efi_event *event,
386 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200387 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100388{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200389 int i;
390
Jonathan Graya95343b2017-03-12 19:26:07 +1100391 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200392 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100393
394 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200395 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100396
397 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
398 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200399 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100400
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200401 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
402 if (efi_events[i].type)
403 continue;
404 efi_events[i].type = type;
405 efi_events[i].notify_tpl = notify_tpl;
406 efi_events[i].notify_function = notify_function;
407 efi_events[i].notify_context = notify_context;
408 /* Disable timers on bootup */
409 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200410 efi_events[i].is_queued = false;
411 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200412 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200413 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200414 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200415 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100416}
417
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200418/*
419 * Create an event.
420 *
421 * This function implements the CreateEvent service.
422 * See the Unified Extensible Firmware Interface (UEFI) specification
423 * for details.
424 *
425 * @type type of the event to create
426 * @notify_tpl task priority level of the event
427 * @notify_function notification function of the event
428 * @notify_context pointer passed to the notification function
429 * @event created event
430 * @return status code
431 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200432static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100433 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200434 void (EFIAPI *notify_function) (
435 struct efi_event *event,
436 void *context),
437 void *notify_context, struct efi_event **event)
438{
439 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
440 notify_context);
441 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
442 notify_context, event));
443}
444
445
Alexander Grafbee91162016-03-04 01:09:59 +0100446/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200447 * Check if a timer event has occurred or a queued notification function should
448 * be called.
449 *
Alexander Grafbee91162016-03-04 01:09:59 +0100450 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200451 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100452 */
453void efi_timer_check(void)
454{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200455 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100456 u64 now = timer_get_us();
457
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200458 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200459 if (!efi_events[i].type)
460 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200461 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200462 efi_signal_event(&efi_events[i]);
463 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200464 now < efi_events[i].trigger_next)
465 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200466 switch (efi_events[i].trigger_type) {
467 case EFI_TIMER_RELATIVE:
468 efi_events[i].trigger_type = EFI_TIMER_STOP;
469 break;
470 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200471 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200472 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200473 break;
474 default:
475 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200476 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200477 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200478 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100479 }
Alexander Grafbee91162016-03-04 01:09:59 +0100480 WATCHDOG_RESET();
481}
482
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200483/*
484 * Set the trigger time for a timer event or stop the event.
485 *
486 * This is the function for internal usage in U-Boot. For the API function
487 * implementing the SetTimer service see efi_set_timer_ext.
488 *
489 * @event event for which the timer is set
490 * @type type of the timer
491 * @trigger_time trigger period in multiples of 100ns
492 * @return status code
493 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200494efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200495 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100496{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200497 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100498
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200499 /*
500 * The parameter defines a multiple of 100ns.
501 * We use multiples of 1000ns. So divide by 10.
502 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200503 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100504
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200505 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
506 if (event != &efi_events[i])
507 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100508
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200509 if (!(event->type & EVT_TIMER))
510 break;
511 switch (type) {
512 case EFI_TIMER_STOP:
513 event->trigger_next = -1ULL;
514 break;
515 case EFI_TIMER_PERIODIC:
516 case EFI_TIMER_RELATIVE:
517 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200518 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200519 break;
520 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200521 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200522 }
523 event->trigger_type = type;
524 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200525 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200526 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100527 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200528 return EFI_INVALID_PARAMETER;
529}
530
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200531/*
532 * Set the trigger time for a timer event or stop the event.
533 *
534 * This function implements the SetTimer service.
535 * See the Unified Extensible Firmware Interface (UEFI) specification
536 * for details.
537 *
538 * @event event for which the timer is set
539 * @type type of the timer
540 * @trigger_time trigger period in multiples of 100ns
541 * @return status code
542 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200543static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
544 enum efi_timer_delay type,
545 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200546{
547 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
548 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100549}
550
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200551/*
552 * Wait for events to be signaled.
553 *
554 * This function implements the WaitForEvent service.
555 * See the Unified Extensible Firmware Interface (UEFI) specification
556 * for details.
557 *
558 * @num_events number of events to be waited for
559 * @events events to be waited for
560 * @index index of the event that was signaled
561 * @return status code
562 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100563static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200564 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100565 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100566{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200567 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100568
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100569 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100570
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200571 /* Check parameters */
572 if (!num_events || !event)
573 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200574 /* Check TPL */
575 if (efi_tpl != TPL_APPLICATION)
576 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200577 for (i = 0; i < num_events; ++i) {
578 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
579 if (event[i] == &efi_events[j])
580 goto known_event;
581 }
582 return EFI_EXIT(EFI_INVALID_PARAMETER);
583known_event:
584 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
585 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200586 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200587 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200588 }
589
590 /* Wait for signal */
591 for (;;) {
592 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200593 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200594 goto out;
595 }
596 /* Allow events to occur. */
597 efi_timer_check();
598 }
599
600out:
601 /*
602 * Reset the signal which is passed to the caller to allow periodic
603 * events to occur.
604 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200605 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200606 if (index)
607 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100608
609 return EFI_EXIT(EFI_SUCCESS);
610}
611
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200612/*
613 * Signal an EFI event.
614 *
615 * This function implements the SignalEvent service.
616 * See the Unified Extensible Firmware Interface (UEFI) specification
617 * for details.
618 *
619 * This functions sets the signaled state of the event and queues the
620 * notification function for execution.
621 *
622 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200623 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200624 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200625static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100626{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200627 int i;
628
Alexander Grafbee91162016-03-04 01:09:59 +0100629 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200630 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
631 if (event != &efi_events[i])
632 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200633 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200634 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200635 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200636 if (event->type & EVT_NOTIFY_SIGNAL)
637 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200638 break;
639 }
Alexander Grafbee91162016-03-04 01:09:59 +0100640 return EFI_EXIT(EFI_SUCCESS);
641}
642
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200643/*
644 * Close an EFI event.
645 *
646 * This function implements the CloseEvent service.
647 * See the Unified Extensible Firmware Interface (UEFI) specification
648 * for details.
649 *
650 * @event event to close
651 * @return status code
652 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200653static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100654{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200655 int i;
656
Alexander Grafbee91162016-03-04 01:09:59 +0100657 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200658 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
659 if (event == &efi_events[i]) {
660 event->type = 0;
661 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200662 event->is_queued = false;
663 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200664 return EFI_EXIT(EFI_SUCCESS);
665 }
666 }
667 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100668}
669
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200670/*
671 * Check if an event is signaled.
672 *
673 * This function implements the CheckEvent service.
674 * See the Unified Extensible Firmware Interface (UEFI) specification
675 * for details.
676 *
677 * If an event is not signaled yet the notification function is queued.
678 *
679 * @event event to check
680 * @return status code
681 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200682static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100683{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200684 int i;
685
Alexander Grafbee91162016-03-04 01:09:59 +0100686 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200687 efi_timer_check();
688 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
689 if (event != &efi_events[i])
690 continue;
691 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
692 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200693 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200694 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200695 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200696 return EFI_EXIT(EFI_SUCCESS);
697 return EFI_EXIT(EFI_NOT_READY);
698 }
699 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100700}
701
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200702/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200703 * Find the internal EFI object for a handle.
704 *
705 * @handle handle to find
706 * @return EFI object
707 */
Heinrich Schuchardt085d07c2017-10-26 19:25:50 +0200708struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200709{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100710 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200711
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100712 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200713 if (efiobj->handle == handle)
714 return efiobj;
715 }
716
717 return NULL;
718}
719
720/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200721 * Find a protocol on a handle.
722 *
723 * @handle handle
724 * @protocol_guid GUID of the protocol
725 * @handler reference to the protocol
726 * @return status code
727 */
728efi_status_t efi_search_protocol(const void *handle,
729 const efi_guid_t *protocol_guid,
730 struct efi_handler **handler)
731{
732 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100733 struct list_head *lhandle;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200734
735 if (!handle || !protocol_guid)
736 return EFI_INVALID_PARAMETER;
737 efiobj = efi_search_obj(handle);
738 if (!efiobj)
739 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100740 list_for_each(lhandle, &efiobj->protocols) {
741 struct efi_handler *protocol;
742
743 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200744 if (!guidcmp(protocol->guid, protocol_guid)) {
745 if (handler)
746 *handler = protocol;
747 return EFI_SUCCESS;
748 }
749 }
750 return EFI_NOT_FOUND;
751}
752
753/*
754 * Install new protocol on a handle.
755 *
756 * @handle handle on which the protocol shall be installed
757 * @protocol GUID of the protocol to be installed
758 * @protocol_interface interface of the protocol implementation
759 * @return status code
760 */
761efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
762 void *protocol_interface)
763{
764 struct efi_object *efiobj;
765 struct efi_handler *handler;
766 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200767
768 efiobj = efi_search_obj(handle);
769 if (!efiobj)
770 return EFI_INVALID_PARAMETER;
771 ret = efi_search_protocol(handle, protocol, NULL);
772 if (ret != EFI_NOT_FOUND)
773 return EFI_INVALID_PARAMETER;
774 handler = calloc(1, sizeof(struct efi_handler));
775 if (!handler)
776 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100777 handler->guid = protocol;
778 handler->protocol_interface = protocol_interface;
779 list_add_tail(&handler->link, &efiobj->protocols);
780 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200781}
782
783/*
784 * Delete protocol from a handle.
785 *
786 * @handle handle from which the protocol shall be deleted
787 * @protocol GUID of the protocol to be deleted
788 * @protocol_interface interface of the protocol implementation
789 * @return status code
790 */
791efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
792 void *protocol_interface)
793{
794 struct efi_handler *handler;
795 efi_status_t ret;
796
797 ret = efi_search_protocol(handle, protocol, &handler);
798 if (ret != EFI_SUCCESS)
799 return ret;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100800 if (guidcmp(handler->guid, protocol))
801 return EFI_INVALID_PARAMETER;
802 list_del(&handler->link);
803 free(handler);
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200804 return EFI_SUCCESS;
805}
806
807/*
808 * Delete all protocols from a handle.
809 *
810 * @handle handle from which the protocols shall be deleted
811 * @return status code
812 */
813efi_status_t efi_remove_all_protocols(const void *handle)
814{
815 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100816 struct list_head *lhandle;
817 struct list_head *pos;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200818
819 efiobj = efi_search_obj(handle);
820 if (!efiobj)
821 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100822 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
823 struct efi_handler *protocol;
824 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200825
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100826 protocol = list_entry(lhandle, struct efi_handler, link);
827
828 ret = efi_remove_protocol(handle, protocol->guid,
829 protocol->protocol_interface);
830 if (ret != EFI_SUCCESS)
831 return ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200832 }
833 return EFI_SUCCESS;
834}
835
836/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200837 * Install protocol interface.
838 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100839 * This function implements the InstallProtocolInterface service.
840 * See the Unified Extensible Firmware Interface (UEFI) specification
841 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200842 *
843 * @handle handle on which the protocol shall be installed
844 * @protocol GUID of the protocol to be installed
845 * @protocol_interface_type type of the interface to be installed,
846 * always EFI_NATIVE_INTERFACE
847 * @protocol_interface interface of the protocol implementation
848 * @return status code
849 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100850static efi_status_t EFIAPI efi_install_protocol_interface(
851 void **handle, const efi_guid_t *protocol,
852 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100853{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200854 efi_status_t r;
855
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100856 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
857 protocol_interface);
858
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200859 if (!handle || !protocol ||
860 protocol_interface_type != EFI_NATIVE_INTERFACE) {
861 r = EFI_INVALID_PARAMETER;
862 goto out;
863 }
864
865 /* Create new handle if requested. */
866 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200867 r = efi_create_handle(handle);
868 if (r != EFI_SUCCESS)
869 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200870 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
871 *handle);
872 } else {
873 debug("%sEFI: handle %p\n", indent_string(nesting_level),
874 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200875 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200876 /* Add new protocol */
877 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200878out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100879 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100880}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200881
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200882/*
883 * Reinstall protocol interface.
884 *
885 * This function implements the ReinstallProtocolInterface service.
886 * See the Unified Extensible Firmware Interface (UEFI) specification
887 * for details.
888 *
889 * @handle handle on which the protocol shall be
890 * reinstalled
891 * @protocol GUID of the protocol to be installed
892 * @old_interface interface to be removed
893 * @new_interface interface to be installed
894 * @return status code
895 */
Alexander Grafbee91162016-03-04 01:09:59 +0100896static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200897 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100898 void *new_interface)
899{
Rob Clark778e6af2017-09-13 18:05:41 -0400900 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100901 new_interface);
902 return EFI_EXIT(EFI_ACCESS_DENIED);
903}
904
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200905/*
906 * Uninstall protocol interface.
907 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100908 * This function implements the UninstallProtocolInterface service.
909 * See the Unified Extensible Firmware Interface (UEFI) specification
910 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200911 *
912 * @handle handle from which the protocol shall be removed
913 * @protocol GUID of the protocol to be removed
914 * @protocol_interface interface to be removed
915 * @return status code
916 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100917static efi_status_t EFIAPI efi_uninstall_protocol_interface(
918 void *handle, const efi_guid_t *protocol,
919 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100920{
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200921 struct efi_handler *handler;
922 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200923
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100924 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
925
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200926 if (!handle || !protocol) {
927 r = EFI_INVALID_PARAMETER;
928 goto out;
929 }
930
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200931 /* Find the protocol on the handle */
932 r = efi_search_protocol(handle, protocol, &handler);
933 if (r != EFI_SUCCESS)
934 goto out;
935 if (handler->protocol_interface) {
936 /* TODO disconnect controllers */
937 r = EFI_ACCESS_DENIED;
938 } else {
939 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200940 }
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200941out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100942 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100943}
944
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200945/*
946 * Register an event for notification when a protocol is installed.
947 *
948 * This function implements the RegisterProtocolNotify service.
949 * See the Unified Extensible Firmware Interface (UEFI) specification
950 * for details.
951 *
952 * @protocol GUID of the protocol whose installation shall be
953 * notified
954 * @event event to be signaled upon installation of the protocol
955 * @registration key for retrieving the registration information
956 * @return status code
957 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200958static efi_status_t EFIAPI efi_register_protocol_notify(
959 const efi_guid_t *protocol,
960 struct efi_event *event,
961 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +0100962{
Rob Clark778e6af2017-09-13 18:05:41 -0400963 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +0100964 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
965}
966
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200967/*
968 * Determine if an EFI handle implements a protocol.
969 *
970 * See the documentation of the LocateHandle service in the UEFI specification.
971 *
972 * @search_type selection criterion
973 * @protocol GUID of the protocol
974 * @search_key registration key
975 * @efiobj handle
976 * @return 0 if the handle implements the protocol
977 */
Alexander Grafbee91162016-03-04 01:09:59 +0100978static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200979 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +0100980 struct efi_object *efiobj)
981{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200982 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +0100983
984 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100985 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +0100986 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100987 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200988 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +0100989 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100990 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200991 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
992 return (ret != EFI_SUCCESS);
993 default:
994 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +0100995 return -1;
996 }
Alexander Grafbee91162016-03-04 01:09:59 +0100997}
998
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200999/*
1000 * Locate handles implementing a protocol.
1001 *
1002 * This function is meant for U-Boot internal calls. For the API implementation
1003 * of the LocateHandle service see efi_locate_handle_ext.
1004 *
1005 * @search_type selection criterion
1006 * @protocol GUID of the protocol
1007 * @search_key registration key
1008 * @buffer_size size of the buffer to receive the handles in bytes
1009 * @buffer buffer to receive the relevant handles
1010 * @return status code
1011 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001012static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001013 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001014 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001015 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001016{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001017 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001018 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001019
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001020 /* Check parameters */
1021 switch (search_type) {
1022 case ALL_HANDLES:
1023 break;
1024 case BY_REGISTER_NOTIFY:
1025 if (!search_key)
1026 return EFI_INVALID_PARAMETER;
1027 /* RegisterProtocolNotify is not implemented yet */
1028 return EFI_UNSUPPORTED;
1029 case BY_PROTOCOL:
1030 if (!protocol)
1031 return EFI_INVALID_PARAMETER;
1032 break;
1033 default:
1034 return EFI_INVALID_PARAMETER;
1035 }
1036
1037 /*
1038 * efi_locate_handle_buffer uses this function for
1039 * the calculation of the necessary buffer size.
1040 * So do not require a buffer for buffersize == 0.
1041 */
1042 if (!buffer_size || (*buffer_size && !buffer))
1043 return EFI_INVALID_PARAMETER;
1044
Alexander Grafbee91162016-03-04 01:09:59 +01001045 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001046 list_for_each_entry(efiobj, &efi_obj_list, link) {
1047 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001048 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001049 }
1050
1051 if (*buffer_size < size) {
1052 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001053 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001054 }
1055
Rob Clark796a78c2017-08-06 14:10:07 -04001056 *buffer_size = size;
1057 if (size == 0)
1058 return EFI_NOT_FOUND;
1059
Alexander Grafbee91162016-03-04 01:09:59 +01001060 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001061 list_for_each_entry(efiobj, &efi_obj_list, link) {
1062 if (!efi_search(search_type, protocol, search_key, efiobj))
1063 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001064 }
1065
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001066 return EFI_SUCCESS;
1067}
1068
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001069/*
1070 * Locate handles implementing a protocol.
1071 *
1072 * This function implements the LocateHandle service.
1073 * See the Unified Extensible Firmware Interface (UEFI) specification
1074 * for details.
1075 *
1076 * @search_type selection criterion
1077 * @protocol GUID of the protocol
1078 * @search_key registration key
1079 * @buffer_size size of the buffer to receive the handles in bytes
1080 * @buffer buffer to receive the relevant handles
1081 * @return 0 if the handle implements the protocol
1082 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001083static efi_status_t EFIAPI efi_locate_handle_ext(
1084 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001085 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001086 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001087{
Rob Clark778e6af2017-09-13 18:05:41 -04001088 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001089 buffer_size, buffer);
1090
1091 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1092 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001093}
1094
Alexander Grafd98cdf62017-07-26 13:41:04 +02001095/* Collapses configuration table entries, removing index i */
1096static void efi_remove_configuration_table(int i)
1097{
1098 struct efi_configuration_table *this = &efi_conf_table[i];
1099 struct efi_configuration_table *next = &efi_conf_table[i+1];
1100 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1101
1102 memmove(this, next, (ulong)end - (ulong)next);
1103 systab.nr_tables--;
1104}
1105
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001106/*
1107 * Adds, updates, or removes a configuration table.
1108 *
1109 * This function is used for internal calls. For the API implementation of the
1110 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1111 *
1112 * @guid GUID of the installed table
1113 * @table table to be installed
1114 * @return status code
1115 */
Alexander Graf488bf122016-08-19 01:23:24 +02001116efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001117{
1118 int i;
1119
Alexander Grafbee91162016-03-04 01:09:59 +01001120 /* Check for guid override */
1121 for (i = 0; i < systab.nr_tables; i++) {
1122 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001123 if (table)
1124 efi_conf_table[i].table = table;
1125 else
1126 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001127 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001128 }
1129 }
1130
Alexander Grafd98cdf62017-07-26 13:41:04 +02001131 if (!table)
1132 return EFI_NOT_FOUND;
1133
Alexander Grafbee91162016-03-04 01:09:59 +01001134 /* No override, check for overflow */
1135 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001136 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001137
1138 /* Add a new entry */
1139 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1140 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001141 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001142
Alexander Graf488bf122016-08-19 01:23:24 +02001143 return EFI_SUCCESS;
1144}
1145
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001146/*
1147 * Adds, updates, or removes a configuration table.
1148 *
1149 * This function implements the InstallConfigurationTable service.
1150 * See the Unified Extensible Firmware Interface (UEFI) specification
1151 * for details.
1152 *
1153 * @guid GUID of the installed table
1154 * @table table to be installed
1155 * @return status code
1156 */
Alexander Graf488bf122016-08-19 01:23:24 +02001157static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1158 void *table)
1159{
Rob Clark778e6af2017-09-13 18:05:41 -04001160 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001161 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001162}
1163
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001164/*
1165 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001166 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001167 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001168 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001169 * image
1170 * @obj internal object associated with the loaded image
1171 * @device_path device path of the loaded image
1172 * @file_path file path of the loaded image
Rob Clark95c55532017-09-13 18:05:33 -04001173 */
1174void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1175 struct efi_device_path *device_path,
1176 struct efi_device_path *file_path)
1177{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001178 efi_status_t ret;
1179
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001180 /* Add internal object to object list */
1181 efi_add_handle(obj);
1182 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001183 obj->handle = info;
1184
Rob Clark95c55532017-09-13 18:05:33 -04001185 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001186 if (device_path)
1187 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001188
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001189 /*
1190 * When asking for the device path interface, return
1191 * bootefi_device_path
1192 */
1193 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1194 if (ret != EFI_SUCCESS)
1195 goto failure;
1196
1197 /*
1198 * When asking for the loaded_image interface, just
1199 * return handle which points to loaded_image_info
1200 */
1201 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1202 if (ret != EFI_SUCCESS)
1203 goto failure;
1204
1205 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1206 (void *)&efi_console_control);
1207 if (ret != EFI_SUCCESS)
1208 goto failure;
1209
1210 ret = efi_add_protocol(obj->handle,
1211 &efi_guid_device_path_to_text_protocol,
1212 (void *)&efi_device_path_to_text);
1213 if (ret != EFI_SUCCESS)
1214 goto failure;
1215
1216 return;
1217failure:
1218 printf("ERROR: Failure to install protocols for loaded image\n");
Rob Clark95c55532017-09-13 18:05:33 -04001219}
1220
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001221/*
1222 * Load an image using a file path.
1223 *
1224 * @file_path the path of the image to load
1225 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001226 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001227 */
Rob Clark9975fe92017-09-13 18:05:38 -04001228efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1229 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001230{
1231 struct efi_file_info *info = NULL;
1232 struct efi_file_handle *f;
1233 static efi_status_t ret;
1234 uint64_t bs;
1235
1236 f = efi_file_from_path(file_path);
1237 if (!f)
1238 return EFI_DEVICE_ERROR;
1239
1240 bs = 0;
1241 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1242 &bs, info));
1243 if (ret == EFI_BUFFER_TOO_SMALL) {
1244 info = malloc(bs);
1245 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1246 &bs, info));
1247 }
1248 if (ret != EFI_SUCCESS)
1249 goto error;
1250
1251 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1252 if (ret)
1253 goto error;
1254
1255 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1256
1257error:
1258 free(info);
1259 EFI_CALL(f->close(f));
1260
1261 if (ret != EFI_SUCCESS) {
1262 efi_free_pool(*buffer);
1263 *buffer = NULL;
1264 }
1265
1266 return ret;
1267}
1268
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001269/*
1270 * Load an EFI image into memory.
1271 *
1272 * This function implements the LoadImage service.
1273 * See the Unified Extensible Firmware Interface (UEFI) specification
1274 * for details.
1275 *
1276 * @boot_policy true for request originating from the boot manager
1277 * @parent_image the calles's image handle
1278 * @file_path the path of the image to load
1279 * @source_buffer memory location from which the image is installed
1280 * @source_size size of the memory area from which the image is
1281 * installed
1282 * @image_handle handle for the newly installed image
1283 * @return status code
1284 */
Alexander Grafbee91162016-03-04 01:09:59 +01001285static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1286 efi_handle_t parent_image,
1287 struct efi_device_path *file_path,
1288 void *source_buffer,
1289 unsigned long source_size,
1290 efi_handle_t *image_handle)
1291{
Alexander Grafbee91162016-03-04 01:09:59 +01001292 struct efi_loaded_image *info;
1293 struct efi_object *obj;
1294
1295 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1296 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001297
1298 info = calloc(1, sizeof(*info));
1299 obj = calloc(1, sizeof(*obj));
1300
1301 if (!source_buffer) {
1302 struct efi_device_path *dp, *fp;
1303 efi_status_t ret;
1304
Rob Clark9975fe92017-09-13 18:05:38 -04001305 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark838ee4b2017-09-13 18:05:35 -04001306 if (ret != EFI_SUCCESS) {
1307 free(info);
1308 free(obj);
1309 return EFI_EXIT(ret);
1310 }
1311
1312 /*
1313 * split file_path which contains both the device and
1314 * file parts:
1315 */
1316 efi_dp_split_file_path(file_path, &dp, &fp);
1317
1318 efi_setup_loaded_image(info, obj, dp, fp);
1319 } else {
1320 /* In this case, file_path is the "device" path, ie.
1321 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1322 */
1323 efi_setup_loaded_image(info, obj, file_path, NULL);
1324 }
1325
Alexander Grafbee91162016-03-04 01:09:59 +01001326 info->reserved = efi_load_pe(source_buffer, info);
1327 if (!info->reserved) {
1328 free(info);
1329 free(obj);
1330 return EFI_EXIT(EFI_UNSUPPORTED);
1331 }
1332
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001333 info->system_table = &systab;
1334 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001335 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001336
1337 return EFI_EXIT(EFI_SUCCESS);
1338}
1339
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001340/*
1341 * Call the entry point of an image.
1342 *
1343 * This function implements the StartImage service.
1344 * See the Unified Extensible Firmware Interface (UEFI) specification
1345 * for details.
1346 *
1347 * @image_handle handle of the image
1348 * @exit_data_size size of the buffer
1349 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001350 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001351 */
Alexander Grafbee91162016-03-04 01:09:59 +01001352static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1353 unsigned long *exit_data_size,
1354 s16 **exit_data)
1355{
1356 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1357 struct efi_loaded_image *info = image_handle;
1358
1359 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1360 entry = info->reserved;
1361
1362 efi_is_direct_boot = false;
1363
1364 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001365 if (setjmp(&info->exit_jmp)) {
1366 /* We returned from the child image */
1367 return EFI_EXIT(info->exit_status);
1368 }
1369
Rob Clarkaf65db82017-07-27 08:04:19 -04001370 __efi_nesting_dec();
Rob Clarkc160d2f2017-07-27 08:04:18 -04001371 __efi_exit_check();
Alexander Grafbee91162016-03-04 01:09:59 +01001372 entry(image_handle, &systab);
Rob Clarkc160d2f2017-07-27 08:04:18 -04001373 __efi_entry_check();
Rob Clarkaf65db82017-07-27 08:04:19 -04001374 __efi_nesting_inc();
Alexander Grafbee91162016-03-04 01:09:59 +01001375
1376 /* Should usually never get here */
1377 return EFI_EXIT(EFI_SUCCESS);
1378}
1379
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001380/*
1381 * Leave an EFI application or driver.
1382 *
1383 * This function implements the Exit service.
1384 * See the Unified Extensible Firmware Interface (UEFI) specification
1385 * for details.
1386 *
1387 * @image_handle handle of the application or driver that is exiting
1388 * @exit_status status code
1389 * @exit_data_size size of the buffer in bytes
1390 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001391 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001392 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001393static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1394 efi_status_t exit_status, unsigned long exit_data_size,
1395 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001396{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001397 /*
1398 * We require that the handle points to the original loaded
1399 * image protocol interface.
1400 *
1401 * For getting the longjmp address this is safer than locating
1402 * the protocol because the protocol may have been reinstalled
1403 * pointing to another memory location.
1404 *
1405 * TODO: We should call the unload procedure of the loaded
1406 * image protocol.
1407 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001408 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1409
Alexander Grafbee91162016-03-04 01:09:59 +01001410 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1411 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001412
Alexander Grafa1489202017-09-03 14:14:17 +02001413 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001414 __efi_exit_check();
1415
Alexander Grafa1489202017-09-03 14:14:17 +02001416 /*
1417 * But longjmp out with the U-Boot gd, not the application's, as
1418 * the other end is a setjmp call inside EFI context.
1419 */
1420 efi_restore_gd();
1421
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001422 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001423 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001424
1425 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001426}
1427
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001428/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001429 * Unload an EFI image.
1430 *
1431 * This function implements the UnloadImage service.
1432 * See the Unified Extensible Firmware Interface (UEFI) specification
1433 * for details.
1434 *
1435 * @image_handle handle of the image to be unloaded
1436 * @return status code
1437 */
Alexander Grafbee91162016-03-04 01:09:59 +01001438static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1439{
1440 struct efi_object *efiobj;
1441
1442 EFI_ENTRY("%p", image_handle);
1443 efiobj = efi_search_obj(image_handle);
1444 if (efiobj)
1445 list_del(&efiobj->link);
1446
1447 return EFI_EXIT(EFI_SUCCESS);
1448}
1449
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001450/*
1451 * Fix up caches for EFI payloads if necessary.
1452 */
Alexander Grafbee91162016-03-04 01:09:59 +01001453static void efi_exit_caches(void)
1454{
1455#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1456 /*
1457 * Grub on 32bit ARM needs to have caches disabled before jumping into
1458 * a zImage, but does not know of all cache layers. Give it a hand.
1459 */
1460 if (efi_is_direct_boot)
1461 cleanup_before_linux();
1462#endif
1463}
1464
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001465/*
1466 * Stop boot services.
1467 *
1468 * This function implements the ExitBootServices service.
1469 * See the Unified Extensible Firmware Interface (UEFI) specification
1470 * for details.
1471 *
1472 * @image_handle handle of the loaded image
1473 * @map_key key of the memory map
1474 * @return status code
1475 */
Alexander Grafbee91162016-03-04 01:09:59 +01001476static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1477 unsigned long map_key)
1478{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001479 int i;
1480
Alexander Grafbee91162016-03-04 01:09:59 +01001481 EFI_ENTRY("%p, %ld", image_handle, map_key);
1482
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001483 /* Notify that ExitBootServices is invoked. */
1484 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1485 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1486 continue;
1487 efi_signal_event(&efi_events[i]);
1488 }
1489 /* Make sure that notification functions are not called anymore */
1490 efi_tpl = TPL_HIGH_LEVEL;
1491
Alexander Graf40583562017-10-19 23:23:50 +02001492 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001493
Alexander Grafb7b84102016-11-17 01:02:57 +01001494 board_quiesce_devices();
1495
Alexander Grafbee91162016-03-04 01:09:59 +01001496 /* Fix up caches for EFI payloads if necessary */
1497 efi_exit_caches();
1498
1499 /* This stops all lingering devices */
1500 bootm_disable_interrupts();
1501
1502 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001503 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001504 WATCHDOG_RESET();
1505
1506 return EFI_EXIT(EFI_SUCCESS);
1507}
1508
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001509/*
1510 * Get next value of the counter.
1511 *
1512 * This function implements the NextMonotonicCount service.
1513 * See the Unified Extensible Firmware Interface (UEFI) specification
1514 * for details.
1515 *
1516 * @count returned value of the counter
1517 * @return status code
1518 */
Alexander Grafbee91162016-03-04 01:09:59 +01001519static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1520{
1521 static uint64_t mono = 0;
1522 EFI_ENTRY("%p", count);
1523 *count = mono++;
1524 return EFI_EXIT(EFI_SUCCESS);
1525}
1526
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001527/*
1528 * Sleep.
1529 *
1530 * This function implements the Stall sercive.
1531 * See the Unified Extensible Firmware Interface (UEFI) specification
1532 * for details.
1533 *
1534 * @microseconds period to sleep in microseconds
1535 * @return status code
1536 */
Alexander Grafbee91162016-03-04 01:09:59 +01001537static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1538{
1539 EFI_ENTRY("%ld", microseconds);
1540 udelay(microseconds);
1541 return EFI_EXIT(EFI_SUCCESS);
1542}
1543
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001544/*
1545 * Reset the watchdog timer.
1546 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001547 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001548 * See the Unified Extensible Firmware Interface (UEFI) specification
1549 * for details.
1550 *
1551 * @timeout seconds before reset by watchdog
1552 * @watchdog_code code to be logged when resetting
1553 * @data_size size of buffer in bytes
1554 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001555 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001556 */
Alexander Grafbee91162016-03-04 01:09:59 +01001557static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1558 uint64_t watchdog_code,
1559 unsigned long data_size,
1560 uint16_t *watchdog_data)
1561{
1562 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1563 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001564 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001565}
1566
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001567/*
1568 * Connect a controller to a driver.
1569 *
1570 * This function implements the ConnectController service.
1571 * See the Unified Extensible Firmware Interface (UEFI) specification
1572 * for details.
1573 *
1574 * @controller_handle handle of the controller
1575 * @driver_image_handle handle of the driver
1576 * @remain_device_path device path of a child controller
1577 * @recursive true to connect all child controllers
1578 * @return status code
1579 */
Alexander Grafbee91162016-03-04 01:09:59 +01001580static efi_status_t EFIAPI efi_connect_controller(
1581 efi_handle_t controller_handle,
1582 efi_handle_t *driver_image_handle,
1583 struct efi_device_path *remain_device_path,
1584 bool recursive)
1585{
1586 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1587 remain_device_path, recursive);
1588 return EFI_EXIT(EFI_NOT_FOUND);
1589}
1590
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001591/*
1592 * Disconnect a controller from a driver.
1593 *
1594 * This function implements the DisconnectController service.
1595 * See the Unified Extensible Firmware Interface (UEFI) specification
1596 * for details.
1597 *
1598 * @controller_handle handle of the controller
1599 * @driver_image_handle handle of the driver
1600 * @child_handle handle of the child to destroy
1601 * @return status code
1602 */
Alexander Grafbee91162016-03-04 01:09:59 +01001603static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1604 void *driver_image_handle,
1605 void *child_handle)
1606{
1607 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1608 child_handle);
1609 return EFI_EXIT(EFI_INVALID_PARAMETER);
1610}
1611
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001612/*
1613 * Close a protocol.
1614 *
1615 * This function implements the CloseProtocol service.
1616 * See the Unified Extensible Firmware Interface (UEFI) specification
1617 * for details.
1618 *
1619 * @handle handle on which the protocol shall be closed
1620 * @protocol GUID of the protocol to close
1621 * @agent_handle handle of the driver
1622 * @controller_handle handle of the controller
1623 * @return status code
1624 */
Alexander Grafbee91162016-03-04 01:09:59 +01001625static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001626 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001627 void *agent_handle,
1628 void *controller_handle)
1629{
Rob Clark778e6af2017-09-13 18:05:41 -04001630 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001631 controller_handle);
1632 return EFI_EXIT(EFI_NOT_FOUND);
1633}
1634
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001635/*
1636 * Provide information about then open status of a protocol on a handle
1637 *
1638 * This function implements the OpenProtocolInformation service.
1639 * See the Unified Extensible Firmware Interface (UEFI) specification
1640 * for details.
1641 *
1642 * @handle handle for which the information shall be retrieved
1643 * @protocol GUID of the protocol
1644 * @entry_buffer buffer to receive the open protocol information
1645 * @entry_count number of entries available in the buffer
1646 * @return status code
1647 */
Alexander Grafbee91162016-03-04 01:09:59 +01001648static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001649 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001650 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001651 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001652{
Rob Clark778e6af2017-09-13 18:05:41 -04001653 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001654 entry_count);
1655 return EFI_EXIT(EFI_NOT_FOUND);
1656}
1657
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001658/*
1659 * Get protocols installed on a handle.
1660 *
1661 * This function implements the ProtocolsPerHandleService.
1662 * See the Unified Extensible Firmware Interface (UEFI) specification
1663 * for details.
1664 *
1665 * @handle handle for which the information is retrieved
1666 * @protocol_buffer buffer with protocol GUIDs
1667 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001668 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001669 */
Alexander Grafbee91162016-03-04 01:09:59 +01001670static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1671 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001672 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001673{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001674 unsigned long buffer_size;
1675 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001676 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001677 efi_status_t r;
1678
Alexander Grafbee91162016-03-04 01:09:59 +01001679 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1680 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001681
1682 if (!handle || !protocol_buffer || !protocol_buffer_count)
1683 return EFI_EXIT(EFI_INVALID_PARAMETER);
1684
1685 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001686 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001687
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001688 efiobj = efi_search_obj(handle);
1689 if (!efiobj)
1690 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001691
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001692 /* Count protocols */
1693 list_for_each(protocol_handle, &efiobj->protocols) {
1694 ++*protocol_buffer_count;
1695 }
1696
1697 /* Copy guids */
1698 if (*protocol_buffer_count) {
1699 size_t j = 0;
1700
1701 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1702 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1703 (void **)protocol_buffer);
1704 if (r != EFI_SUCCESS)
1705 return EFI_EXIT(r);
1706 list_for_each(protocol_handle, &efiobj->protocols) {
1707 struct efi_handler *protocol;
1708
1709 protocol = list_entry(protocol_handle,
1710 struct efi_handler, link);
1711 (*protocol_buffer)[j] = (void *)protocol->guid;
1712 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001713 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001714 }
1715
1716 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001717}
1718
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001719/*
1720 * Locate handles implementing a protocol.
1721 *
1722 * This function implements the LocateHandleBuffer service.
1723 * See the Unified Extensible Firmware Interface (UEFI) specification
1724 * for details.
1725 *
1726 * @search_type selection criterion
1727 * @protocol GUID of the protocol
1728 * @search_key registration key
1729 * @no_handles number of returned handles
1730 * @buffer buffer with the returned handles
1731 * @return status code
1732 */
Alexander Grafbee91162016-03-04 01:09:59 +01001733static efi_status_t EFIAPI efi_locate_handle_buffer(
1734 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001735 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001736 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001737{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001738 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001739 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001740
Rob Clark778e6af2017-09-13 18:05:41 -04001741 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001742 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001743
1744 if (!no_handles || !buffer) {
1745 r = EFI_INVALID_PARAMETER;
1746 goto out;
1747 }
1748 *no_handles = 0;
1749 *buffer = NULL;
1750 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1751 *buffer);
1752 if (r != EFI_BUFFER_TOO_SMALL)
1753 goto out;
1754 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1755 (void **)buffer);
1756 if (r != EFI_SUCCESS)
1757 goto out;
1758 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1759 *buffer);
1760 if (r == EFI_SUCCESS)
1761 *no_handles = buffer_size / sizeof(void *);
1762out:
1763 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001764}
1765
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001766/*
1767 * Find an interface implementing a protocol.
1768 *
1769 * This function implements the LocateProtocol service.
1770 * See the Unified Extensible Firmware Interface (UEFI) specification
1771 * for details.
1772 *
1773 * @protocol GUID of the protocol
1774 * @registration registration key passed to the notification function
1775 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001776 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001777 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001778static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001779 void *registration,
1780 void **protocol_interface)
1781{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001782 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001783 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001784
Rob Clark778e6af2017-09-13 18:05:41 -04001785 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001786
1787 if (!protocol || !protocol_interface)
1788 return EFI_EXIT(EFI_INVALID_PARAMETER);
1789
1790 list_for_each(lhandle, &efi_obj_list) {
1791 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001792 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001793
1794 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001795
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001796 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1797 if (ret == EFI_SUCCESS) {
1798 *protocol_interface = handler->protocol_interface;
1799 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001800 }
1801 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001802 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001803
1804 return EFI_EXIT(EFI_NOT_FOUND);
1805}
1806
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001807/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01001808 * Get the device path and handle of an device implementing a protocol.
1809 *
1810 * This function implements the LocateDevicePath service.
1811 * See the Unified Extensible Firmware Interface (UEFI) specification
1812 * for details.
1813 *
1814 * @protocol GUID of the protocol
1815 * @device_path device path
1816 * @device handle of the device
1817 * @return status code
1818 */
1819static efi_status_t EFIAPI efi_locate_device_path(
1820 const efi_guid_t *protocol,
1821 struct efi_device_path **device_path,
1822 efi_handle_t *device)
1823{
1824 struct efi_device_path *dp;
1825 size_t i;
1826 struct efi_handler *handler;
1827 efi_handle_t *handles;
1828 size_t len, len_dp;
1829 size_t len_best = 0;
1830 efi_uintn_t no_handles;
1831 u8 *remainder;
1832 efi_status_t ret;
1833
1834 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1835
1836 if (!protocol || !device_path || !*device_path || !device) {
1837 ret = EFI_INVALID_PARAMETER;
1838 goto out;
1839 }
1840
1841 /* Find end of device path */
1842 len = efi_dp_size(*device_path);
1843
1844 /* Get all handles implementing the protocol */
1845 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1846 &no_handles, &handles));
1847 if (ret != EFI_SUCCESS)
1848 goto out;
1849
1850 for (i = 0; i < no_handles; ++i) {
1851 /* Find the device path protocol */
1852 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1853 &handler);
1854 if (ret != EFI_SUCCESS)
1855 continue;
1856 dp = (struct efi_device_path *)handler->protocol_interface;
1857 len_dp = efi_dp_size(dp);
1858 /*
1859 * This handle can only be a better fit
1860 * if its device path length is longer than the best fit and
1861 * if its device path length is shorter of equal the searched
1862 * device path.
1863 */
1864 if (len_dp <= len_best || len_dp > len)
1865 continue;
1866 /* Check if dp is a subpath of device_path */
1867 if (memcmp(*device_path, dp, len_dp))
1868 continue;
1869 *device = handles[i];
1870 len_best = len_dp;
1871 }
1872 if (len_best) {
1873 remainder = (u8 *)*device_path + len_best;
1874 *device_path = (struct efi_device_path *)remainder;
1875 ret = EFI_SUCCESS;
1876 } else {
1877 ret = EFI_NOT_FOUND;
1878 }
1879out:
1880 return EFI_EXIT(ret);
1881}
1882
1883/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001884 * Install multiple protocol interfaces.
1885 *
1886 * This function implements the MultipleProtocolInterfaces service.
1887 * See the Unified Extensible Firmware Interface (UEFI) specification
1888 * for details.
1889 *
1890 * @handle handle on which the protocol interfaces shall be installed
1891 * @... NULL terminated argument list with pairs of protocol GUIDS and
1892 * interfaces
1893 * @return status code
1894 */
Alexander Grafbee91162016-03-04 01:09:59 +01001895static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1896 void **handle, ...)
1897{
1898 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001899
1900 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001901 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001902 void *protocol_interface;
1903 efi_status_t r = EFI_SUCCESS;
1904 int i = 0;
1905
1906 if (!handle)
1907 return EFI_EXIT(EFI_INVALID_PARAMETER);
1908
1909 va_start(argptr, handle);
1910 for (;;) {
1911 protocol = va_arg(argptr, efi_guid_t*);
1912 if (!protocol)
1913 break;
1914 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001915 r = EFI_CALL(efi_install_protocol_interface(
1916 handle, protocol,
1917 EFI_NATIVE_INTERFACE,
1918 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001919 if (r != EFI_SUCCESS)
1920 break;
1921 i++;
1922 }
1923 va_end(argptr);
1924 if (r == EFI_SUCCESS)
1925 return EFI_EXIT(r);
1926
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02001927 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001928 va_start(argptr, handle);
1929 for (; i; --i) {
1930 protocol = va_arg(argptr, efi_guid_t*);
1931 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001932 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1933 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001934 }
1935 va_end(argptr);
1936
1937 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001938}
1939
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001940/*
1941 * Uninstall multiple protocol interfaces.
1942 *
1943 * This function implements the UninstallMultipleProtocolInterfaces service.
1944 * See the Unified Extensible Firmware Interface (UEFI) specification
1945 * for details.
1946 *
1947 * @handle handle from which the protocol interfaces shall be removed
1948 * @... NULL terminated argument list with pairs of protocol GUIDS and
1949 * interfaces
1950 * @return status code
1951 */
Alexander Grafbee91162016-03-04 01:09:59 +01001952static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1953 void *handle, ...)
1954{
1955 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02001956
1957 va_list argptr;
1958 const efi_guid_t *protocol;
1959 void *protocol_interface;
1960 efi_status_t r = EFI_SUCCESS;
1961 size_t i = 0;
1962
1963 if (!handle)
1964 return EFI_EXIT(EFI_INVALID_PARAMETER);
1965
1966 va_start(argptr, handle);
1967 for (;;) {
1968 protocol = va_arg(argptr, efi_guid_t*);
1969 if (!protocol)
1970 break;
1971 protocol_interface = va_arg(argptr, void*);
1972 r = EFI_CALL(efi_uninstall_protocol_interface(
1973 handle, protocol,
1974 protocol_interface));
1975 if (r != EFI_SUCCESS)
1976 break;
1977 i++;
1978 }
1979 va_end(argptr);
1980 if (r == EFI_SUCCESS)
1981 return EFI_EXIT(r);
1982
1983 /* If an error occurred undo all changes. */
1984 va_start(argptr, handle);
1985 for (; i; --i) {
1986 protocol = va_arg(argptr, efi_guid_t*);
1987 protocol_interface = va_arg(argptr, void*);
1988 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1989 EFI_NATIVE_INTERFACE,
1990 protocol_interface));
1991 }
1992 va_end(argptr);
1993
1994 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001995}
1996
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001997/*
1998 * Calculate cyclic redundancy code.
1999 *
2000 * This function implements the CalculateCrc32 service.
2001 * See the Unified Extensible Firmware Interface (UEFI) specification
2002 * for details.
2003 *
2004 * @data buffer with data
2005 * @data_size size of buffer in bytes
2006 * @crc32_p cyclic redundancy code
2007 * @return status code
2008 */
Alexander Grafbee91162016-03-04 01:09:59 +01002009static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2010 unsigned long data_size,
2011 uint32_t *crc32_p)
2012{
2013 EFI_ENTRY("%p, %ld", data, data_size);
2014 *crc32_p = crc32(0, data, data_size);
2015 return EFI_EXIT(EFI_SUCCESS);
2016}
2017
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002018/*
2019 * Copy memory.
2020 *
2021 * This function implements the CopyMem service.
2022 * See the Unified Extensible Firmware Interface (UEFI) specification
2023 * for details.
2024 *
2025 * @destination destination of the copy operation
2026 * @source source of the copy operation
2027 * @length number of bytes to copy
2028 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002029static void EFIAPI efi_copy_mem(void *destination, const void *source,
2030 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002031{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002032 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002033 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002034 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002035}
2036
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002037/*
2038 * Fill memory with a byte value.
2039 *
2040 * This function implements the SetMem service.
2041 * See the Unified Extensible Firmware Interface (UEFI) specification
2042 * for details.
2043 *
2044 * @buffer buffer to fill
2045 * @size size of buffer in bytes
2046 * @value byte to copy to the buffer
2047 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002048static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002049{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002050 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002051 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002052 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002053}
2054
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002055/*
2056 * Open protocol interface on a handle.
2057 *
2058 * This function implements the OpenProtocol interface.
2059 * See the Unified Extensible Firmware Interface (UEFI) specification
2060 * for details.
2061 *
2062 * @handle handle on which the protocol shall be opened
2063 * @protocol GUID of the protocol
2064 * @protocol_interface interface implementing the protocol
2065 * @agent_handle handle of the driver
2066 * @controller_handle handle of the controller
2067 * @attributes attributes indicating how to open the protocol
2068 * @return status code
2069 */
Alexander Grafbee91162016-03-04 01:09:59 +01002070static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002071 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002072 void **protocol_interface, void *agent_handle,
2073 void *controller_handle, uint32_t attributes)
2074{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002075 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002076 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002077
Rob Clark778e6af2017-09-13 18:05:41 -04002078 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002079 protocol_interface, agent_handle, controller_handle,
2080 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002081
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002082 if (!handle || !protocol ||
2083 (!protocol_interface && attributes !=
2084 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2085 goto out;
2086 }
2087
2088 switch (attributes) {
2089 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2090 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2091 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2092 break;
2093 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2094 if (controller_handle == handle)
2095 goto out;
2096 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2097 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2098 if (controller_handle == NULL)
2099 goto out;
2100 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2101 if (agent_handle == NULL)
2102 goto out;
2103 break;
2104 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002105 goto out;
2106 }
2107
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002108 r = efi_search_protocol(handle, protocol, &handler);
2109 if (r != EFI_SUCCESS)
2110 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002111
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002112 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2113 *protocol_interface = handler->protocol_interface;
Alexander Grafbee91162016-03-04 01:09:59 +01002114out:
2115 return EFI_EXIT(r);
2116}
2117
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002118/*
2119 * Get interface of a protocol on a handle.
2120 *
2121 * This function implements the HandleProtocol service.
2122 * See the Unified Extensible Firmware Interface (UEFI) specification
2123 * for details.
2124 *
2125 * @handle handle on which the protocol shall be opened
2126 * @protocol GUID of the protocol
2127 * @protocol_interface interface implementing the protocol
2128 * @return status code
2129 */
Alexander Grafbee91162016-03-04 01:09:59 +01002130static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002131 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002132 void **protocol_interface)
2133{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002134 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2135 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002136}
2137
2138static const struct efi_boot_services efi_boot_services = {
2139 .hdr = {
2140 .headersize = sizeof(struct efi_table_hdr),
2141 },
2142 .raise_tpl = efi_raise_tpl,
2143 .restore_tpl = efi_restore_tpl,
2144 .allocate_pages = efi_allocate_pages_ext,
2145 .free_pages = efi_free_pages_ext,
2146 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002147 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002148 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002149 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002150 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002151 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002152 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002153 .close_event = efi_close_event,
2154 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002155 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002156 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002157 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002158 .handle_protocol = efi_handle_protocol,
2159 .reserved = NULL,
2160 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002161 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002162 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002163 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002164 .load_image = efi_load_image,
2165 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002166 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002167 .unload_image = efi_unload_image,
2168 .exit_boot_services = efi_exit_boot_services,
2169 .get_next_monotonic_count = efi_get_next_monotonic_count,
2170 .stall = efi_stall,
2171 .set_watchdog_timer = efi_set_watchdog_timer,
2172 .connect_controller = efi_connect_controller,
2173 .disconnect_controller = efi_disconnect_controller,
2174 .open_protocol = efi_open_protocol,
2175 .close_protocol = efi_close_protocol,
2176 .open_protocol_information = efi_open_protocol_information,
2177 .protocols_per_handle = efi_protocols_per_handle,
2178 .locate_handle_buffer = efi_locate_handle_buffer,
2179 .locate_protocol = efi_locate_protocol,
2180 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2181 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2182 .calculate_crc32 = efi_calculate_crc32,
2183 .copy_mem = efi_copy_mem,
2184 .set_mem = efi_set_mem,
2185};
2186
2187
Alexander Graf3c63db92016-10-14 13:45:30 +02002188static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafbee91162016-03-04 01:09:59 +01002189 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2190
Alexander Graf3c63db92016-10-14 13:45:30 +02002191struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002192 .hdr = {
2193 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2194 .revision = 0x20005, /* 2.5 */
2195 .headersize = sizeof(struct efi_table_hdr),
2196 },
2197 .fw_vendor = (long)firmware_vendor,
2198 .con_in = (void*)&efi_con_in,
2199 .con_out = (void*)&efi_con_out,
2200 .std_err = (void*)&efi_con_out,
2201 .runtime = (void*)&efi_runtime_services,
2202 .boottime = (void*)&efi_boot_services,
2203 .nr_tables = 0,
2204 .tables = (void*)efi_conf_table,
2205};