blob: e372c8bb94f05e2df55b1a540abe0ef687e8ca0d [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/*
325 * Create handle.
326 *
327 * @handle new handle
328 * @return status code
329 */
330efi_status_t efi_create_handle(void **handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200331{
332 struct efi_object *obj;
333 efi_status_t r;
334
335 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
336 sizeof(struct efi_object),
337 (void **)&obj);
338 if (r != EFI_SUCCESS)
339 return r;
340 memset(obj, 0, sizeof(struct efi_object));
341 obj->handle = obj;
342 list_add_tail(&obj->link, &efi_obj_list);
343 *handle = obj;
344 return r;
345}
346
Alexander Grafbee91162016-03-04 01:09:59 +0100347/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200348 * Our event capabilities are very limited. Only a small limited
349 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100350 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200351static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100352
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200353/*
354 * Create an event.
355 *
356 * This function is used inside U-Boot code to create an event.
357 *
358 * For the API function implementing the CreateEvent service see
359 * efi_create_event_ext.
360 *
361 * @type type of the event to create
362 * @notify_tpl task priority level of the event
363 * @notify_function notification function of the event
364 * @notify_context pointer passed to the notification function
365 * @event created event
366 * @return status code
367 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100368efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200369 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200370 struct efi_event *event,
371 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200372 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100373{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200374 int i;
375
Jonathan Graya95343b2017-03-12 19:26:07 +1100376 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200377 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100378
379 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200380 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100381
382 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
383 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200384 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100385
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200386 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
387 if (efi_events[i].type)
388 continue;
389 efi_events[i].type = type;
390 efi_events[i].notify_tpl = notify_tpl;
391 efi_events[i].notify_function = notify_function;
392 efi_events[i].notify_context = notify_context;
393 /* Disable timers on bootup */
394 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200395 efi_events[i].is_queued = false;
396 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200397 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200398 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200399 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200400 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100401}
402
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200403/*
404 * Create an event.
405 *
406 * This function implements the CreateEvent service.
407 * See the Unified Extensible Firmware Interface (UEFI) specification
408 * for details.
409 *
410 * @type type of the event to create
411 * @notify_tpl task priority level of the event
412 * @notify_function notification function of the event
413 * @notify_context pointer passed to the notification function
414 * @event created event
415 * @return status code
416 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200417static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100418 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200419 void (EFIAPI *notify_function) (
420 struct efi_event *event,
421 void *context),
422 void *notify_context, struct efi_event **event)
423{
424 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
425 notify_context);
426 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
427 notify_context, event));
428}
429
430
Alexander Grafbee91162016-03-04 01:09:59 +0100431/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200432 * Check if a timer event has occurred or a queued notification function should
433 * be called.
434 *
Alexander Grafbee91162016-03-04 01:09:59 +0100435 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200436 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100437 */
438void efi_timer_check(void)
439{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200440 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100441 u64 now = timer_get_us();
442
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200444 if (!efi_events[i].type)
445 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200446 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200447 efi_signal_event(&efi_events[i]);
448 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200449 now < efi_events[i].trigger_next)
450 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200451 switch (efi_events[i].trigger_type) {
452 case EFI_TIMER_RELATIVE:
453 efi_events[i].trigger_type = EFI_TIMER_STOP;
454 break;
455 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200456 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200457 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200458 break;
459 default:
460 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200461 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200462 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200463 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100464 }
Alexander Grafbee91162016-03-04 01:09:59 +0100465 WATCHDOG_RESET();
466}
467
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200468/*
469 * Set the trigger time for a timer event or stop the event.
470 *
471 * This is the function for internal usage in U-Boot. For the API function
472 * implementing the SetTimer service see efi_set_timer_ext.
473 *
474 * @event event for which the timer is set
475 * @type type of the timer
476 * @trigger_time trigger period in multiples of 100ns
477 * @return status code
478 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200479efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200480 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100481{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200482 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100483
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200484 /*
485 * The parameter defines a multiple of 100ns.
486 * We use multiples of 1000ns. So divide by 10.
487 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200488 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100489
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200490 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
491 if (event != &efi_events[i])
492 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100493
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200494 if (!(event->type & EVT_TIMER))
495 break;
496 switch (type) {
497 case EFI_TIMER_STOP:
498 event->trigger_next = -1ULL;
499 break;
500 case EFI_TIMER_PERIODIC:
501 case EFI_TIMER_RELATIVE:
502 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200503 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200504 break;
505 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200506 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200507 }
508 event->trigger_type = type;
509 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200510 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200511 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100512 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200513 return EFI_INVALID_PARAMETER;
514}
515
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200516/*
517 * Set the trigger time for a timer event or stop the event.
518 *
519 * This function implements the SetTimer service.
520 * See the Unified Extensible Firmware Interface (UEFI) specification
521 * for details.
522 *
523 * @event event for which the timer is set
524 * @type type of the timer
525 * @trigger_time trigger period in multiples of 100ns
526 * @return status code
527 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200528static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
529 enum efi_timer_delay type,
530 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200531{
532 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
533 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100534}
535
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200536/*
537 * Wait for events to be signaled.
538 *
539 * This function implements the WaitForEvent service.
540 * See the Unified Extensible Firmware Interface (UEFI) specification
541 * for details.
542 *
543 * @num_events number of events to be waited for
544 * @events events to be waited for
545 * @index index of the event that was signaled
546 * @return status code
547 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100548static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200549 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100550 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100551{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200552 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100553
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100554 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100555
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200556 /* Check parameters */
557 if (!num_events || !event)
558 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200559 /* Check TPL */
560 if (efi_tpl != TPL_APPLICATION)
561 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200562 for (i = 0; i < num_events; ++i) {
563 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
564 if (event[i] == &efi_events[j])
565 goto known_event;
566 }
567 return EFI_EXIT(EFI_INVALID_PARAMETER);
568known_event:
569 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
570 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200571 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200572 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200573 }
574
575 /* Wait for signal */
576 for (;;) {
577 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200578 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200579 goto out;
580 }
581 /* Allow events to occur. */
582 efi_timer_check();
583 }
584
585out:
586 /*
587 * Reset the signal which is passed to the caller to allow periodic
588 * events to occur.
589 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200590 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200591 if (index)
592 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100593
594 return EFI_EXIT(EFI_SUCCESS);
595}
596
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200597/*
598 * Signal an EFI event.
599 *
600 * This function implements the SignalEvent service.
601 * See the Unified Extensible Firmware Interface (UEFI) specification
602 * for details.
603 *
604 * This functions sets the signaled state of the event and queues the
605 * notification function for execution.
606 *
607 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200608 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200609 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200610static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100611{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200612 int i;
613
Alexander Grafbee91162016-03-04 01:09:59 +0100614 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200615 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
616 if (event != &efi_events[i])
617 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200618 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200619 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200620 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200621 if (event->type & EVT_NOTIFY_SIGNAL)
622 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200623 break;
624 }
Alexander Grafbee91162016-03-04 01:09:59 +0100625 return EFI_EXIT(EFI_SUCCESS);
626}
627
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200628/*
629 * Close an EFI event.
630 *
631 * This function implements the CloseEvent service.
632 * See the Unified Extensible Firmware Interface (UEFI) specification
633 * for details.
634 *
635 * @event event to close
636 * @return status code
637 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200638static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100639{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200640 int i;
641
Alexander Grafbee91162016-03-04 01:09:59 +0100642 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200643 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
644 if (event == &efi_events[i]) {
645 event->type = 0;
646 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200647 event->is_queued = false;
648 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200649 return EFI_EXIT(EFI_SUCCESS);
650 }
651 }
652 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100653}
654
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200655/*
656 * Check if an event is signaled.
657 *
658 * This function implements the CheckEvent service.
659 * See the Unified Extensible Firmware Interface (UEFI) specification
660 * for details.
661 *
662 * If an event is not signaled yet the notification function is queued.
663 *
664 * @event event to check
665 * @return status code
666 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200667static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100668{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200669 int i;
670
Alexander Grafbee91162016-03-04 01:09:59 +0100671 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200672 efi_timer_check();
673 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
674 if (event != &efi_events[i])
675 continue;
676 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
677 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200678 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200679 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200680 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200681 return EFI_EXIT(EFI_SUCCESS);
682 return EFI_EXIT(EFI_NOT_READY);
683 }
684 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100685}
686
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200687/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200688 * Find the internal EFI object for a handle.
689 *
690 * @handle handle to find
691 * @return EFI object
692 */
Heinrich Schuchardt085d07c2017-10-26 19:25:50 +0200693struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200694{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100695 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200696
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100697 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200698 if (efiobj->handle == handle)
699 return efiobj;
700 }
701
702 return NULL;
703}
704
705/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200706 * Find a protocol on a handle.
707 *
708 * @handle handle
709 * @protocol_guid GUID of the protocol
710 * @handler reference to the protocol
711 * @return status code
712 */
713efi_status_t efi_search_protocol(const void *handle,
714 const efi_guid_t *protocol_guid,
715 struct efi_handler **handler)
716{
717 struct efi_object *efiobj;
718 size_t i;
719 struct efi_handler *protocol;
720
721 if (!handle || !protocol_guid)
722 return EFI_INVALID_PARAMETER;
723 efiobj = efi_search_obj(handle);
724 if (!efiobj)
725 return EFI_INVALID_PARAMETER;
726 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
727 protocol = &efiobj->protocols[i];
728 if (!protocol->guid)
729 continue;
730 if (!guidcmp(protocol->guid, protocol_guid)) {
731 if (handler)
732 *handler = protocol;
733 return EFI_SUCCESS;
734 }
735 }
736 return EFI_NOT_FOUND;
737}
738
739/*
740 * Install new protocol on a handle.
741 *
742 * @handle handle on which the protocol shall be installed
743 * @protocol GUID of the protocol to be installed
744 * @protocol_interface interface of the protocol implementation
745 * @return status code
746 */
747efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
748 void *protocol_interface)
749{
750 struct efi_object *efiobj;
751 struct efi_handler *handler;
752 efi_status_t ret;
753 size_t i;
754
755 efiobj = efi_search_obj(handle);
756 if (!efiobj)
757 return EFI_INVALID_PARAMETER;
758 ret = efi_search_protocol(handle, protocol, NULL);
759 if (ret != EFI_NOT_FOUND)
760 return EFI_INVALID_PARAMETER;
761 handler = calloc(1, sizeof(struct efi_handler));
762 if (!handler)
763 return EFI_OUT_OF_RESOURCES;
764 /* Install protocol in first empty slot. */
765 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
766 handler = &efiobj->protocols[i];
767 if (handler->guid)
768 continue;
769 handler->guid = protocol;
770 handler->protocol_interface = protocol_interface;
771 return EFI_SUCCESS;
772 }
773 return EFI_OUT_OF_RESOURCES;
774}
775
776/*
777 * Delete protocol from a handle.
778 *
779 * @handle handle from which the protocol shall be deleted
780 * @protocol GUID of the protocol to be deleted
781 * @protocol_interface interface of the protocol implementation
782 * @return status code
783 */
784efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
785 void *protocol_interface)
786{
787 struct efi_handler *handler;
788 efi_status_t ret;
789
790 ret = efi_search_protocol(handle, protocol, &handler);
791 if (ret != EFI_SUCCESS)
792 return ret;
793 if (handler->protocol_interface != protocol_interface)
794 return EFI_NOT_FOUND;
795 handler->guid = NULL;
796 handler->protocol_interface = NULL;
797 return EFI_SUCCESS;
798}
799
800/*
801 * Delete all protocols from a handle.
802 *
803 * @handle handle from which the protocols shall be deleted
804 * @return status code
805 */
806efi_status_t efi_remove_all_protocols(const void *handle)
807{
808 struct efi_object *efiobj;
809 struct efi_handler *handler;
810 size_t i;
811
812 efiobj = efi_search_obj(handle);
813 if (!efiobj)
814 return EFI_INVALID_PARAMETER;
815
816 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
817 handler = &efiobj->protocols[i];
818 handler->guid = NULL;
819 handler->protocol_interface = NULL;
820 }
821 return EFI_SUCCESS;
822}
823
824/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200825 * Install protocol interface.
826 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100827 * This function implements the InstallProtocolInterface service.
828 * See the Unified Extensible Firmware Interface (UEFI) specification
829 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200830 *
831 * @handle handle on which the protocol shall be installed
832 * @protocol GUID of the protocol to be installed
833 * @protocol_interface_type type of the interface to be installed,
834 * always EFI_NATIVE_INTERFACE
835 * @protocol_interface interface of the protocol implementation
836 * @return status code
837 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100838static efi_status_t EFIAPI efi_install_protocol_interface(
839 void **handle, const efi_guid_t *protocol,
840 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100841{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200842 struct list_head *lhandle;
843 int i;
844 efi_status_t r;
845
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100846 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
847 protocol_interface);
848
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200849 if (!handle || !protocol ||
850 protocol_interface_type != EFI_NATIVE_INTERFACE) {
851 r = EFI_INVALID_PARAMETER;
852 goto out;
853 }
854
855 /* Create new handle if requested. */
856 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200857 r = efi_create_handle(handle);
858 if (r != EFI_SUCCESS)
859 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200860 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
861 *handle);
862 } else {
863 debug("%sEFI: handle %p\n", indent_string(nesting_level),
864 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200865 }
866 /* Find object. */
867 list_for_each(lhandle, &efi_obj_list) {
868 struct efi_object *efiobj;
869 efiobj = list_entry(lhandle, struct efi_object, link);
870
871 if (efiobj->handle != *handle)
872 continue;
873 /* Check if protocol is already installed on the handle. */
874 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
875 struct efi_handler *handler = &efiobj->protocols[i];
876
877 if (!handler->guid)
878 continue;
879 if (!guidcmp(handler->guid, protocol)) {
880 r = EFI_INVALID_PARAMETER;
881 goto out;
882 }
883 }
884 /* Install protocol in first empty slot. */
885 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
886 struct efi_handler *handler = &efiobj->protocols[i];
887
888 if (handler->guid)
889 continue;
890
891 handler->guid = protocol;
892 handler->protocol_interface = protocol_interface;
893 r = EFI_SUCCESS;
894 goto out;
895 }
896 r = EFI_OUT_OF_RESOURCES;
897 goto out;
898 }
899 r = EFI_INVALID_PARAMETER;
900out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100901 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100902}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200903
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200904/*
905 * Reinstall protocol interface.
906 *
907 * This function implements the ReinstallProtocolInterface service.
908 * See the Unified Extensible Firmware Interface (UEFI) specification
909 * for details.
910 *
911 * @handle handle on which the protocol shall be
912 * reinstalled
913 * @protocol GUID of the protocol to be installed
914 * @old_interface interface to be removed
915 * @new_interface interface to be installed
916 * @return status code
917 */
Alexander Grafbee91162016-03-04 01:09:59 +0100918static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200919 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100920 void *new_interface)
921{
Rob Clark778e6af2017-09-13 18:05:41 -0400922 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100923 new_interface);
924 return EFI_EXIT(EFI_ACCESS_DENIED);
925}
926
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200927/*
928 * Uninstall protocol interface.
929 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100930 * This function implements the UninstallProtocolInterface service.
931 * See the Unified Extensible Firmware Interface (UEFI) specification
932 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200933 *
934 * @handle handle from which the protocol shall be removed
935 * @protocol GUID of the protocol to be removed
936 * @protocol_interface interface to be removed
937 * @return status code
938 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100939static efi_status_t EFIAPI efi_uninstall_protocol_interface(
940 void *handle, const efi_guid_t *protocol,
941 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100942{
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200943 struct list_head *lhandle;
944 int i;
945 efi_status_t r = EFI_NOT_FOUND;
946
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100947 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
948
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200949 if (!handle || !protocol) {
950 r = EFI_INVALID_PARAMETER;
951 goto out;
952 }
953
954 list_for_each(lhandle, &efi_obj_list) {
955 struct efi_object *efiobj;
956 efiobj = list_entry(lhandle, struct efi_object, link);
957
958 if (efiobj->handle != handle)
959 continue;
960
961 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
962 struct efi_handler *handler = &efiobj->protocols[i];
963 const efi_guid_t *hprotocol = handler->guid;
964
965 if (!hprotocol)
966 continue;
967 if (!guidcmp(hprotocol, protocol)) {
968 if (handler->protocol_interface) {
969 r = EFI_ACCESS_DENIED;
970 } else {
971 handler->guid = 0;
972 r = EFI_SUCCESS;
973 }
974 goto out;
975 }
976 }
977 }
978
979out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100980 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100981}
982
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200983/*
984 * Register an event for notification when a protocol is installed.
985 *
986 * This function implements the RegisterProtocolNotify service.
987 * See the Unified Extensible Firmware Interface (UEFI) specification
988 * for details.
989 *
990 * @protocol GUID of the protocol whose installation shall be
991 * notified
992 * @event event to be signaled upon installation of the protocol
993 * @registration key for retrieving the registration information
994 * @return status code
995 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200996static efi_status_t EFIAPI efi_register_protocol_notify(
997 const efi_guid_t *protocol,
998 struct efi_event *event,
999 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001000{
Rob Clark778e6af2017-09-13 18:05:41 -04001001 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001002 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1003}
1004
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001005/*
1006 * Determine if an EFI handle implements a protocol.
1007 *
1008 * See the documentation of the LocateHandle service in the UEFI specification.
1009 *
1010 * @search_type selection criterion
1011 * @protocol GUID of the protocol
1012 * @search_key registration key
1013 * @efiobj handle
1014 * @return 0 if the handle implements the protocol
1015 */
Alexander Grafbee91162016-03-04 01:09:59 +01001016static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001017 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001018 struct efi_object *efiobj)
1019{
1020 int i;
1021
1022 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001023 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001024 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001025 case BY_REGISTER_NOTIFY:
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001026 /* RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001027 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001028 case BY_PROTOCOL:
Alexander Grafbee91162016-03-04 01:09:59 +01001029 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1030 const efi_guid_t *guid = efiobj->protocols[i].guid;
1031 if (guid && !guidcmp(guid, protocol))
1032 return 0;
1033 }
1034 return -1;
1035 }
1036
1037 return -1;
1038}
1039
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001040/*
1041 * Locate handles implementing a protocol.
1042 *
1043 * This function is meant for U-Boot internal calls. For the API implementation
1044 * of the LocateHandle service see efi_locate_handle_ext.
1045 *
1046 * @search_type selection criterion
1047 * @protocol GUID of the protocol
1048 * @search_key registration key
1049 * @buffer_size size of the buffer to receive the handles in bytes
1050 * @buffer buffer to receive the relevant handles
1051 * @return status code
1052 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001053static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001054 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001055 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001056 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001057{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001058 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001059 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001060
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001061 /* Check parameters */
1062 switch (search_type) {
1063 case ALL_HANDLES:
1064 break;
1065 case BY_REGISTER_NOTIFY:
1066 if (!search_key)
1067 return EFI_INVALID_PARAMETER;
1068 /* RegisterProtocolNotify is not implemented yet */
1069 return EFI_UNSUPPORTED;
1070 case BY_PROTOCOL:
1071 if (!protocol)
1072 return EFI_INVALID_PARAMETER;
1073 break;
1074 default:
1075 return EFI_INVALID_PARAMETER;
1076 }
1077
1078 /*
1079 * efi_locate_handle_buffer uses this function for
1080 * the calculation of the necessary buffer size.
1081 * So do not require a buffer for buffersize == 0.
1082 */
1083 if (!buffer_size || (*buffer_size && !buffer))
1084 return EFI_INVALID_PARAMETER;
1085
Alexander Grafbee91162016-03-04 01:09:59 +01001086 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001087 list_for_each_entry(efiobj, &efi_obj_list, link) {
1088 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001089 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001090 }
1091
1092 if (*buffer_size < size) {
1093 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001094 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001095 }
1096
Rob Clark796a78c2017-08-06 14:10:07 -04001097 *buffer_size = size;
1098 if (size == 0)
1099 return EFI_NOT_FOUND;
1100
Alexander Grafbee91162016-03-04 01:09:59 +01001101 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001102 list_for_each_entry(efiobj, &efi_obj_list, link) {
1103 if (!efi_search(search_type, protocol, search_key, efiobj))
1104 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001105 }
1106
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001107 return EFI_SUCCESS;
1108}
1109
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001110/*
1111 * Locate handles implementing a protocol.
1112 *
1113 * This function implements the LocateHandle service.
1114 * See the Unified Extensible Firmware Interface (UEFI) specification
1115 * for details.
1116 *
1117 * @search_type selection criterion
1118 * @protocol GUID of the protocol
1119 * @search_key registration key
1120 * @buffer_size size of the buffer to receive the handles in bytes
1121 * @buffer buffer to receive the relevant handles
1122 * @return 0 if the handle implements the protocol
1123 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001124static efi_status_t EFIAPI efi_locate_handle_ext(
1125 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001126 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001127 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001128{
Rob Clark778e6af2017-09-13 18:05:41 -04001129 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001130 buffer_size, buffer);
1131
1132 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1133 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001134}
1135
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001136/*
1137 * Get the device path and handle of an device implementing a protocol.
1138 *
1139 * This function implements the LocateDevicePath service.
1140 * See the Unified Extensible Firmware Interface (UEFI) specification
1141 * for details.
1142 *
1143 * @protocol GUID of the protocol
1144 * @device_path device path
1145 * @device handle of the device
1146 * @return status code
1147 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001148static efi_status_t EFIAPI efi_locate_device_path(
1149 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001150 struct efi_device_path **device_path,
1151 efi_handle_t *device)
1152{
Rob Clarkb66c60d2017-09-13 18:05:28 -04001153 struct efi_object *efiobj;
1154
1155 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1156
1157 efiobj = efi_dp_find_obj(*device_path, device_path);
1158 if (!efiobj)
1159 return EFI_EXIT(EFI_NOT_FOUND);
1160
1161 *device = efiobj->handle;
1162
1163 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001164}
1165
Alexander Grafd98cdf62017-07-26 13:41:04 +02001166/* Collapses configuration table entries, removing index i */
1167static void efi_remove_configuration_table(int i)
1168{
1169 struct efi_configuration_table *this = &efi_conf_table[i];
1170 struct efi_configuration_table *next = &efi_conf_table[i+1];
1171 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1172
1173 memmove(this, next, (ulong)end - (ulong)next);
1174 systab.nr_tables--;
1175}
1176
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001177/*
1178 * Adds, updates, or removes a configuration table.
1179 *
1180 * This function is used for internal calls. For the API implementation of the
1181 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1182 *
1183 * @guid GUID of the installed table
1184 * @table table to be installed
1185 * @return status code
1186 */
Alexander Graf488bf122016-08-19 01:23:24 +02001187efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001188{
1189 int i;
1190
Alexander Grafbee91162016-03-04 01:09:59 +01001191 /* Check for guid override */
1192 for (i = 0; i < systab.nr_tables; i++) {
1193 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001194 if (table)
1195 efi_conf_table[i].table = table;
1196 else
1197 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001198 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001199 }
1200 }
1201
Alexander Grafd98cdf62017-07-26 13:41:04 +02001202 if (!table)
1203 return EFI_NOT_FOUND;
1204
Alexander Grafbee91162016-03-04 01:09:59 +01001205 /* No override, check for overflow */
1206 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001207 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001208
1209 /* Add a new entry */
1210 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1211 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001212 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001213
Alexander Graf488bf122016-08-19 01:23:24 +02001214 return EFI_SUCCESS;
1215}
1216
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001217/*
1218 * Adds, updates, or removes a configuration table.
1219 *
1220 * This function implements the InstallConfigurationTable service.
1221 * See the Unified Extensible Firmware Interface (UEFI) specification
1222 * for details.
1223 *
1224 * @guid GUID of the installed table
1225 * @table table to be installed
1226 * @return status code
1227 */
Alexander Graf488bf122016-08-19 01:23:24 +02001228static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1229 void *table)
1230{
Rob Clark778e6af2017-09-13 18:05:41 -04001231 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001232 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001233}
1234
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001235/*
1236 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001237 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001238 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001239 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001240 * image
1241 * @obj internal object associated with the loaded image
1242 * @device_path device path of the loaded image
1243 * @file_path file path of the loaded image
Rob Clark95c55532017-09-13 18:05:33 -04001244 */
1245void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1246 struct efi_device_path *device_path,
1247 struct efi_device_path *file_path)
1248{
1249 obj->handle = info;
1250
1251 /*
1252 * When asking for the device path interface, return
1253 * bootefi_device_path
1254 */
1255 obj->protocols[0].guid = &efi_guid_device_path;
1256 obj->protocols[0].protocol_interface = device_path;
1257
1258 /*
1259 * When asking for the loaded_image interface, just
1260 * return handle which points to loaded_image_info
1261 */
1262 obj->protocols[1].guid = &efi_guid_loaded_image;
1263 obj->protocols[1].protocol_interface = info;
1264
1265 obj->protocols[2].guid = &efi_guid_console_control;
1266 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1267
1268 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1269 obj->protocols[3].protocol_interface =
1270 (void *)&efi_device_path_to_text;
1271
1272 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001273 if (device_path)
1274 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001275
1276 list_add_tail(&obj->link, &efi_obj_list);
1277}
1278
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001279/*
1280 * Load an image using a file path.
1281 *
1282 * @file_path the path of the image to load
1283 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001284 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001285 */
Rob Clark9975fe92017-09-13 18:05:38 -04001286efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1287 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001288{
1289 struct efi_file_info *info = NULL;
1290 struct efi_file_handle *f;
1291 static efi_status_t ret;
1292 uint64_t bs;
1293
1294 f = efi_file_from_path(file_path);
1295 if (!f)
1296 return EFI_DEVICE_ERROR;
1297
1298 bs = 0;
1299 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1300 &bs, info));
1301 if (ret == EFI_BUFFER_TOO_SMALL) {
1302 info = malloc(bs);
1303 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1304 &bs, info));
1305 }
1306 if (ret != EFI_SUCCESS)
1307 goto error;
1308
1309 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1310 if (ret)
1311 goto error;
1312
1313 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1314
1315error:
1316 free(info);
1317 EFI_CALL(f->close(f));
1318
1319 if (ret != EFI_SUCCESS) {
1320 efi_free_pool(*buffer);
1321 *buffer = NULL;
1322 }
1323
1324 return ret;
1325}
1326
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001327/*
1328 * Load an EFI image into memory.
1329 *
1330 * This function implements the LoadImage service.
1331 * See the Unified Extensible Firmware Interface (UEFI) specification
1332 * for details.
1333 *
1334 * @boot_policy true for request originating from the boot manager
1335 * @parent_image the calles's image handle
1336 * @file_path the path of the image to load
1337 * @source_buffer memory location from which the image is installed
1338 * @source_size size of the memory area from which the image is
1339 * installed
1340 * @image_handle handle for the newly installed image
1341 * @return status code
1342 */
Alexander Grafbee91162016-03-04 01:09:59 +01001343static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1344 efi_handle_t parent_image,
1345 struct efi_device_path *file_path,
1346 void *source_buffer,
1347 unsigned long source_size,
1348 efi_handle_t *image_handle)
1349{
Alexander Grafbee91162016-03-04 01:09:59 +01001350 struct efi_loaded_image *info;
1351 struct efi_object *obj;
1352
1353 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1354 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001355
1356 info = calloc(1, sizeof(*info));
1357 obj = calloc(1, sizeof(*obj));
1358
1359 if (!source_buffer) {
1360 struct efi_device_path *dp, *fp;
1361 efi_status_t ret;
1362
Rob Clark9975fe92017-09-13 18:05:38 -04001363 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark838ee4b2017-09-13 18:05:35 -04001364 if (ret != EFI_SUCCESS) {
1365 free(info);
1366 free(obj);
1367 return EFI_EXIT(ret);
1368 }
1369
1370 /*
1371 * split file_path which contains both the device and
1372 * file parts:
1373 */
1374 efi_dp_split_file_path(file_path, &dp, &fp);
1375
1376 efi_setup_loaded_image(info, obj, dp, fp);
1377 } else {
1378 /* In this case, file_path is the "device" path, ie.
1379 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1380 */
1381 efi_setup_loaded_image(info, obj, file_path, NULL);
1382 }
1383
Alexander Grafbee91162016-03-04 01:09:59 +01001384 info->reserved = efi_load_pe(source_buffer, info);
1385 if (!info->reserved) {
1386 free(info);
1387 free(obj);
1388 return EFI_EXIT(EFI_UNSUPPORTED);
1389 }
1390
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001391 info->system_table = &systab;
1392 info->parent_handle = parent_image;
Alexander Grafbee91162016-03-04 01:09:59 +01001393 *image_handle = info;
Alexander Grafbee91162016-03-04 01:09:59 +01001394
1395 return EFI_EXIT(EFI_SUCCESS);
1396}
1397
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001398/*
1399 * Call the entry point of an image.
1400 *
1401 * This function implements the StartImage service.
1402 * See the Unified Extensible Firmware Interface (UEFI) specification
1403 * for details.
1404 *
1405 * @image_handle handle of the image
1406 * @exit_data_size size of the buffer
1407 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001408 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001409 */
Alexander Grafbee91162016-03-04 01:09:59 +01001410static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1411 unsigned long *exit_data_size,
1412 s16 **exit_data)
1413{
1414 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1415 struct efi_loaded_image *info = image_handle;
1416
1417 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1418 entry = info->reserved;
1419
1420 efi_is_direct_boot = false;
1421
1422 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001423 if (setjmp(&info->exit_jmp)) {
1424 /* We returned from the child image */
1425 return EFI_EXIT(info->exit_status);
1426 }
1427
Rob Clarkaf65db82017-07-27 08:04:19 -04001428 __efi_nesting_dec();
Rob Clarkc160d2f2017-07-27 08:04:18 -04001429 __efi_exit_check();
Alexander Grafbee91162016-03-04 01:09:59 +01001430 entry(image_handle, &systab);
Rob Clarkc160d2f2017-07-27 08:04:18 -04001431 __efi_entry_check();
Rob Clarkaf65db82017-07-27 08:04:19 -04001432 __efi_nesting_inc();
Alexander Grafbee91162016-03-04 01:09:59 +01001433
1434 /* Should usually never get here */
1435 return EFI_EXIT(EFI_SUCCESS);
1436}
1437
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001438/*
1439 * Leave an EFI application or driver.
1440 *
1441 * This function implements the Exit service.
1442 * See the Unified Extensible Firmware Interface (UEFI) specification
1443 * for details.
1444 *
1445 * @image_handle handle of the application or driver that is exiting
1446 * @exit_status status code
1447 * @exit_data_size size of the buffer in bytes
1448 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001449 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001450 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001451static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1452 efi_status_t exit_status, unsigned long exit_data_size,
1453 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001454{
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001455 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1456
Alexander Grafbee91162016-03-04 01:09:59 +01001457 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1458 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001459
Alexander Grafa1489202017-09-03 14:14:17 +02001460 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001461 __efi_exit_check();
1462
Alexander Grafa1489202017-09-03 14:14:17 +02001463 /*
1464 * But longjmp out with the U-Boot gd, not the application's, as
1465 * the other end is a setjmp call inside EFI context.
1466 */
1467 efi_restore_gd();
1468
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001469 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001470 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001471
1472 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001473}
1474
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001475/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001476 * Unload an EFI image.
1477 *
1478 * This function implements the UnloadImage service.
1479 * See the Unified Extensible Firmware Interface (UEFI) specification
1480 * for details.
1481 *
1482 * @image_handle handle of the image to be unloaded
1483 * @return status code
1484 */
Alexander Grafbee91162016-03-04 01:09:59 +01001485static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1486{
1487 struct efi_object *efiobj;
1488
1489 EFI_ENTRY("%p", image_handle);
1490 efiobj = efi_search_obj(image_handle);
1491 if (efiobj)
1492 list_del(&efiobj->link);
1493
1494 return EFI_EXIT(EFI_SUCCESS);
1495}
1496
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001497/*
1498 * Fix up caches for EFI payloads if necessary.
1499 */
Alexander Grafbee91162016-03-04 01:09:59 +01001500static void efi_exit_caches(void)
1501{
1502#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1503 /*
1504 * Grub on 32bit ARM needs to have caches disabled before jumping into
1505 * a zImage, but does not know of all cache layers. Give it a hand.
1506 */
1507 if (efi_is_direct_boot)
1508 cleanup_before_linux();
1509#endif
1510}
1511
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001512/*
1513 * Stop boot services.
1514 *
1515 * This function implements the ExitBootServices service.
1516 * See the Unified Extensible Firmware Interface (UEFI) specification
1517 * for details.
1518 *
1519 * @image_handle handle of the loaded image
1520 * @map_key key of the memory map
1521 * @return status code
1522 */
Alexander Grafbee91162016-03-04 01:09:59 +01001523static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1524 unsigned long map_key)
1525{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001526 int i;
1527
Alexander Grafbee91162016-03-04 01:09:59 +01001528 EFI_ENTRY("%p, %ld", image_handle, map_key);
1529
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001530 /* Notify that ExitBootServices is invoked. */
1531 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1532 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1533 continue;
1534 efi_signal_event(&efi_events[i]);
1535 }
1536 /* Make sure that notification functions are not called anymore */
1537 efi_tpl = TPL_HIGH_LEVEL;
1538
Alexander Graf40583562017-10-19 23:23:50 +02001539 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001540
Alexander Grafb7b84102016-11-17 01:02:57 +01001541 board_quiesce_devices();
1542
Alexander Grafbee91162016-03-04 01:09:59 +01001543 /* Fix up caches for EFI payloads if necessary */
1544 efi_exit_caches();
1545
1546 /* This stops all lingering devices */
1547 bootm_disable_interrupts();
1548
1549 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001550 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001551 WATCHDOG_RESET();
1552
1553 return EFI_EXIT(EFI_SUCCESS);
1554}
1555
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001556/*
1557 * Get next value of the counter.
1558 *
1559 * This function implements the NextMonotonicCount service.
1560 * See the Unified Extensible Firmware Interface (UEFI) specification
1561 * for details.
1562 *
1563 * @count returned value of the counter
1564 * @return status code
1565 */
Alexander Grafbee91162016-03-04 01:09:59 +01001566static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1567{
1568 static uint64_t mono = 0;
1569 EFI_ENTRY("%p", count);
1570 *count = mono++;
1571 return EFI_EXIT(EFI_SUCCESS);
1572}
1573
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001574/*
1575 * Sleep.
1576 *
1577 * This function implements the Stall sercive.
1578 * See the Unified Extensible Firmware Interface (UEFI) specification
1579 * for details.
1580 *
1581 * @microseconds period to sleep in microseconds
1582 * @return status code
1583 */
Alexander Grafbee91162016-03-04 01:09:59 +01001584static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1585{
1586 EFI_ENTRY("%ld", microseconds);
1587 udelay(microseconds);
1588 return EFI_EXIT(EFI_SUCCESS);
1589}
1590
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001591/*
1592 * Reset the watchdog timer.
1593 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001594 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001595 * See the Unified Extensible Firmware Interface (UEFI) specification
1596 * for details.
1597 *
1598 * @timeout seconds before reset by watchdog
1599 * @watchdog_code code to be logged when resetting
1600 * @data_size size of buffer in bytes
1601 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001602 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001603 */
Alexander Grafbee91162016-03-04 01:09:59 +01001604static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1605 uint64_t watchdog_code,
1606 unsigned long data_size,
1607 uint16_t *watchdog_data)
1608{
1609 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1610 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001611 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001612}
1613
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001614/*
1615 * Connect a controller to a driver.
1616 *
1617 * This function implements the ConnectController service.
1618 * See the Unified Extensible Firmware Interface (UEFI) specification
1619 * for details.
1620 *
1621 * @controller_handle handle of the controller
1622 * @driver_image_handle handle of the driver
1623 * @remain_device_path device path of a child controller
1624 * @recursive true to connect all child controllers
1625 * @return status code
1626 */
Alexander Grafbee91162016-03-04 01:09:59 +01001627static efi_status_t EFIAPI efi_connect_controller(
1628 efi_handle_t controller_handle,
1629 efi_handle_t *driver_image_handle,
1630 struct efi_device_path *remain_device_path,
1631 bool recursive)
1632{
1633 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1634 remain_device_path, recursive);
1635 return EFI_EXIT(EFI_NOT_FOUND);
1636}
1637
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001638/*
1639 * Disconnect a controller from a driver.
1640 *
1641 * This function implements the DisconnectController service.
1642 * See the Unified Extensible Firmware Interface (UEFI) specification
1643 * for details.
1644 *
1645 * @controller_handle handle of the controller
1646 * @driver_image_handle handle of the driver
1647 * @child_handle handle of the child to destroy
1648 * @return status code
1649 */
Alexander Grafbee91162016-03-04 01:09:59 +01001650static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1651 void *driver_image_handle,
1652 void *child_handle)
1653{
1654 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1655 child_handle);
1656 return EFI_EXIT(EFI_INVALID_PARAMETER);
1657}
1658
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001659/*
1660 * Close a protocol.
1661 *
1662 * This function implements the CloseProtocol service.
1663 * See the Unified Extensible Firmware Interface (UEFI) specification
1664 * for details.
1665 *
1666 * @handle handle on which the protocol shall be closed
1667 * @protocol GUID of the protocol to close
1668 * @agent_handle handle of the driver
1669 * @controller_handle handle of the controller
1670 * @return status code
1671 */
Alexander Grafbee91162016-03-04 01:09:59 +01001672static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001673 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001674 void *agent_handle,
1675 void *controller_handle)
1676{
Rob Clark778e6af2017-09-13 18:05:41 -04001677 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001678 controller_handle);
1679 return EFI_EXIT(EFI_NOT_FOUND);
1680}
1681
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001682/*
1683 * Provide information about then open status of a protocol on a handle
1684 *
1685 * This function implements the OpenProtocolInformation service.
1686 * See the Unified Extensible Firmware Interface (UEFI) specification
1687 * for details.
1688 *
1689 * @handle handle for which the information shall be retrieved
1690 * @protocol GUID of the protocol
1691 * @entry_buffer buffer to receive the open protocol information
1692 * @entry_count number of entries available in the buffer
1693 * @return status code
1694 */
Alexander Grafbee91162016-03-04 01:09:59 +01001695static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001696 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001697 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001698 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001699{
Rob Clark778e6af2017-09-13 18:05:41 -04001700 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001701 entry_count);
1702 return EFI_EXIT(EFI_NOT_FOUND);
1703}
1704
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001705/*
1706 * Get protocols installed on a handle.
1707 *
1708 * This function implements the ProtocolsPerHandleService.
1709 * See the Unified Extensible Firmware Interface (UEFI) specification
1710 * for details.
1711 *
1712 * @handle handle for which the information is retrieved
1713 * @protocol_buffer buffer with protocol GUIDs
1714 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001715 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001716 */
Alexander Grafbee91162016-03-04 01:09:59 +01001717static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1718 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001719 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001720{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001721 unsigned long buffer_size;
1722 struct efi_object *efiobj;
1723 unsigned long i, j;
1724 struct list_head *lhandle;
1725 efi_status_t r;
1726
Alexander Grafbee91162016-03-04 01:09:59 +01001727 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1728 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001729
1730 if (!handle || !protocol_buffer || !protocol_buffer_count)
1731 return EFI_EXIT(EFI_INVALID_PARAMETER);
1732
1733 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001734 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001735 list_for_each(lhandle, &efi_obj_list) {
1736 efiobj = list_entry(lhandle, struct efi_object, link);
1737
1738 if (efiobj->handle != handle)
1739 continue;
1740
1741 /* Count protocols */
1742 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1743 if (efiobj->protocols[i].guid)
1744 ++*protocol_buffer_count;
1745 }
1746 /* Copy guids */
1747 if (*protocol_buffer_count) {
1748 buffer_size = sizeof(efi_guid_t *) *
1749 *protocol_buffer_count;
1750 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1751 buffer_size,
1752 (void **)protocol_buffer);
1753 if (r != EFI_SUCCESS)
1754 return EFI_EXIT(r);
1755 j = 0;
1756 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1757 if (efiobj->protocols[i].guid) {
1758 (*protocol_buffer)[j] = (void *)
1759 efiobj->protocols[i].guid;
1760 ++j;
1761 }
1762 }
1763 }
1764 break;
1765 }
1766
1767 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001768}
1769
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001770/*
1771 * Locate handles implementing a protocol.
1772 *
1773 * This function implements the LocateHandleBuffer service.
1774 * See the Unified Extensible Firmware Interface (UEFI) specification
1775 * for details.
1776 *
1777 * @search_type selection criterion
1778 * @protocol GUID of the protocol
1779 * @search_key registration key
1780 * @no_handles number of returned handles
1781 * @buffer buffer with the returned handles
1782 * @return status code
1783 */
Alexander Grafbee91162016-03-04 01:09:59 +01001784static efi_status_t EFIAPI efi_locate_handle_buffer(
1785 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001786 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001787 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001788{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001789 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001790 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001791
Rob Clark778e6af2017-09-13 18:05:41 -04001792 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001793 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001794
1795 if (!no_handles || !buffer) {
1796 r = EFI_INVALID_PARAMETER;
1797 goto out;
1798 }
1799 *no_handles = 0;
1800 *buffer = NULL;
1801 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1802 *buffer);
1803 if (r != EFI_BUFFER_TOO_SMALL)
1804 goto out;
1805 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1806 (void **)buffer);
1807 if (r != EFI_SUCCESS)
1808 goto out;
1809 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1810 *buffer);
1811 if (r == EFI_SUCCESS)
1812 *no_handles = buffer_size / sizeof(void *);
1813out:
1814 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001815}
1816
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001817/*
1818 * Find an interface implementing a protocol.
1819 *
1820 * This function implements the LocateProtocol service.
1821 * See the Unified Extensible Firmware Interface (UEFI) specification
1822 * for details.
1823 *
1824 * @protocol GUID of the protocol
1825 * @registration registration key passed to the notification function
1826 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001827 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001828 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001829static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001830 void *registration,
1831 void **protocol_interface)
1832{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001833 struct list_head *lhandle;
Alexander Grafbee91162016-03-04 01:09:59 +01001834 int i;
1835
Rob Clark778e6af2017-09-13 18:05:41 -04001836 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001837
1838 if (!protocol || !protocol_interface)
1839 return EFI_EXIT(EFI_INVALID_PARAMETER);
1840
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +02001841 EFI_PRINT_GUID("protocol", protocol);
1842
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001843 list_for_each(lhandle, &efi_obj_list) {
1844 struct efi_object *efiobj;
1845
1846 efiobj = list_entry(lhandle, struct efi_object, link);
1847 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1848 struct efi_handler *handler = &efiobj->protocols[i];
1849
1850 if (!handler->guid)
1851 continue;
1852 if (!guidcmp(handler->guid, protocol)) {
1853 *protocol_interface =
1854 handler->protocol_interface;
1855 return EFI_EXIT(EFI_SUCCESS);
1856 }
Alexander Grafbee91162016-03-04 01:09:59 +01001857 }
1858 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001859 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001860
1861 return EFI_EXIT(EFI_NOT_FOUND);
1862}
1863
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001864/*
1865 * Install multiple protocol interfaces.
1866 *
1867 * This function implements the MultipleProtocolInterfaces service.
1868 * See the Unified Extensible Firmware Interface (UEFI) specification
1869 * for details.
1870 *
1871 * @handle handle on which the protocol interfaces shall be installed
1872 * @... NULL terminated argument list with pairs of protocol GUIDS and
1873 * interfaces
1874 * @return status code
1875 */
Alexander Grafbee91162016-03-04 01:09:59 +01001876static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1877 void **handle, ...)
1878{
1879 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001880
1881 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001882 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001883 void *protocol_interface;
1884 efi_status_t r = EFI_SUCCESS;
1885 int i = 0;
1886
1887 if (!handle)
1888 return EFI_EXIT(EFI_INVALID_PARAMETER);
1889
1890 va_start(argptr, handle);
1891 for (;;) {
1892 protocol = va_arg(argptr, efi_guid_t*);
1893 if (!protocol)
1894 break;
1895 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001896 r = EFI_CALL(efi_install_protocol_interface(
1897 handle, protocol,
1898 EFI_NATIVE_INTERFACE,
1899 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001900 if (r != EFI_SUCCESS)
1901 break;
1902 i++;
1903 }
1904 va_end(argptr);
1905 if (r == EFI_SUCCESS)
1906 return EFI_EXIT(r);
1907
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02001908 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001909 va_start(argptr, handle);
1910 for (; i; --i) {
1911 protocol = va_arg(argptr, efi_guid_t*);
1912 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001913 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1914 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001915 }
1916 va_end(argptr);
1917
1918 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001919}
1920
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001921/*
1922 * Uninstall multiple protocol interfaces.
1923 *
1924 * This function implements the UninstallMultipleProtocolInterfaces service.
1925 * See the Unified Extensible Firmware Interface (UEFI) specification
1926 * for details.
1927 *
1928 * @handle handle from which the protocol interfaces shall be removed
1929 * @... NULL terminated argument list with pairs of protocol GUIDS and
1930 * interfaces
1931 * @return status code
1932 */
Alexander Grafbee91162016-03-04 01:09:59 +01001933static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1934 void *handle, ...)
1935{
1936 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02001937
1938 va_list argptr;
1939 const efi_guid_t *protocol;
1940 void *protocol_interface;
1941 efi_status_t r = EFI_SUCCESS;
1942 size_t i = 0;
1943
1944 if (!handle)
1945 return EFI_EXIT(EFI_INVALID_PARAMETER);
1946
1947 va_start(argptr, handle);
1948 for (;;) {
1949 protocol = va_arg(argptr, efi_guid_t*);
1950 if (!protocol)
1951 break;
1952 protocol_interface = va_arg(argptr, void*);
1953 r = EFI_CALL(efi_uninstall_protocol_interface(
1954 handle, protocol,
1955 protocol_interface));
1956 if (r != EFI_SUCCESS)
1957 break;
1958 i++;
1959 }
1960 va_end(argptr);
1961 if (r == EFI_SUCCESS)
1962 return EFI_EXIT(r);
1963
1964 /* If an error occurred undo all changes. */
1965 va_start(argptr, handle);
1966 for (; i; --i) {
1967 protocol = va_arg(argptr, efi_guid_t*);
1968 protocol_interface = va_arg(argptr, void*);
1969 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1970 EFI_NATIVE_INTERFACE,
1971 protocol_interface));
1972 }
1973 va_end(argptr);
1974
1975 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001976}
1977
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001978/*
1979 * Calculate cyclic redundancy code.
1980 *
1981 * This function implements the CalculateCrc32 service.
1982 * See the Unified Extensible Firmware Interface (UEFI) specification
1983 * for details.
1984 *
1985 * @data buffer with data
1986 * @data_size size of buffer in bytes
1987 * @crc32_p cyclic redundancy code
1988 * @return status code
1989 */
Alexander Grafbee91162016-03-04 01:09:59 +01001990static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1991 unsigned long data_size,
1992 uint32_t *crc32_p)
1993{
1994 EFI_ENTRY("%p, %ld", data, data_size);
1995 *crc32_p = crc32(0, data, data_size);
1996 return EFI_EXIT(EFI_SUCCESS);
1997}
1998
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001999/*
2000 * Copy memory.
2001 *
2002 * This function implements the CopyMem service.
2003 * See the Unified Extensible Firmware Interface (UEFI) specification
2004 * for details.
2005 *
2006 * @destination destination of the copy operation
2007 * @source source of the copy operation
2008 * @length number of bytes to copy
2009 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002010static void EFIAPI efi_copy_mem(void *destination, const void *source,
2011 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002012{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002013 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002014 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002015 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002016}
2017
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002018/*
2019 * Fill memory with a byte value.
2020 *
2021 * This function implements the SetMem service.
2022 * See the Unified Extensible Firmware Interface (UEFI) specification
2023 * for details.
2024 *
2025 * @buffer buffer to fill
2026 * @size size of buffer in bytes
2027 * @value byte to copy to the buffer
2028 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002029static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002030{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002031 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002032 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002033 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002034}
2035
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002036/*
2037 * Open protocol interface on a handle.
2038 *
2039 * This function implements the OpenProtocol interface.
2040 * See the Unified Extensible Firmware Interface (UEFI) specification
2041 * for details.
2042 *
2043 * @handle handle on which the protocol shall be opened
2044 * @protocol GUID of the protocol
2045 * @protocol_interface interface implementing the protocol
2046 * @agent_handle handle of the driver
2047 * @controller_handle handle of the controller
2048 * @attributes attributes indicating how to open the protocol
2049 * @return status code
2050 */
Alexander Grafbee91162016-03-04 01:09:59 +01002051static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002052 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002053 void **protocol_interface, void *agent_handle,
2054 void *controller_handle, uint32_t attributes)
2055{
2056 struct list_head *lhandle;
2057 int i;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002058 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002059
Rob Clark778e6af2017-09-13 18:05:41 -04002060 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002061 protocol_interface, agent_handle, controller_handle,
2062 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002063
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002064 if (!handle || !protocol ||
2065 (!protocol_interface && attributes !=
2066 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2067 goto out;
2068 }
2069
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +02002070 EFI_PRINT_GUID("protocol", protocol);
2071
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002072 switch (attributes) {
2073 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2074 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2075 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2076 break;
2077 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2078 if (controller_handle == handle)
2079 goto out;
2080 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2081 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2082 if (controller_handle == NULL)
2083 goto out;
2084 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2085 if (agent_handle == NULL)
2086 goto out;
2087 break;
2088 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002089 goto out;
2090 }
2091
Alexander Grafbee91162016-03-04 01:09:59 +01002092 list_for_each(lhandle, &efi_obj_list) {
2093 struct efi_object *efiobj;
2094 efiobj = list_entry(lhandle, struct efi_object, link);
2095
2096 if (efiobj->handle != handle)
2097 continue;
2098
2099 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
2100 struct efi_handler *handler = &efiobj->protocols[i];
2101 const efi_guid_t *hprotocol = handler->guid;
2102 if (!hprotocol)
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02002103 continue;
Alexander Grafbee91162016-03-04 01:09:59 +01002104 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002105 if (attributes !=
2106 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
2107 *protocol_interface =
2108 handler->protocol_interface;
2109 }
2110 r = EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01002111 goto out;
2112 }
2113 }
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002114 goto unsupported;
Alexander Grafbee91162016-03-04 01:09:59 +01002115 }
2116
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002117unsupported:
2118 r = EFI_UNSUPPORTED;
Alexander Grafbee91162016-03-04 01:09:59 +01002119out:
2120 return EFI_EXIT(r);
2121}
2122
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002123/*
2124 * Get interface of a protocol on a handle.
2125 *
2126 * This function implements the HandleProtocol service.
2127 * See the Unified Extensible Firmware Interface (UEFI) specification
2128 * for details.
2129 *
2130 * @handle handle on which the protocol shall be opened
2131 * @protocol GUID of the protocol
2132 * @protocol_interface interface implementing the protocol
2133 * @return status code
2134 */
Alexander Grafbee91162016-03-04 01:09:59 +01002135static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002136 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002137 void **protocol_interface)
2138{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002139 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2140 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002141}
2142
2143static const struct efi_boot_services efi_boot_services = {
2144 .hdr = {
2145 .headersize = sizeof(struct efi_table_hdr),
2146 },
2147 .raise_tpl = efi_raise_tpl,
2148 .restore_tpl = efi_restore_tpl,
2149 .allocate_pages = efi_allocate_pages_ext,
2150 .free_pages = efi_free_pages_ext,
2151 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002152 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002153 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002154 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002155 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002156 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002157 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002158 .close_event = efi_close_event,
2159 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002160 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002161 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002162 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002163 .handle_protocol = efi_handle_protocol,
2164 .reserved = NULL,
2165 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002166 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002167 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002168 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002169 .load_image = efi_load_image,
2170 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002171 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002172 .unload_image = efi_unload_image,
2173 .exit_boot_services = efi_exit_boot_services,
2174 .get_next_monotonic_count = efi_get_next_monotonic_count,
2175 .stall = efi_stall,
2176 .set_watchdog_timer = efi_set_watchdog_timer,
2177 .connect_controller = efi_connect_controller,
2178 .disconnect_controller = efi_disconnect_controller,
2179 .open_protocol = efi_open_protocol,
2180 .close_protocol = efi_close_protocol,
2181 .open_protocol_information = efi_open_protocol_information,
2182 .protocols_per_handle = efi_protocols_per_handle,
2183 .locate_handle_buffer = efi_locate_handle_buffer,
2184 .locate_protocol = efi_locate_protocol,
2185 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2186 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2187 .calculate_crc32 = efi_calculate_crc32,
2188 .copy_mem = efi_copy_mem,
2189 .set_mem = efi_set_mem,
2190};
2191
2192
Alexander Graf3c63db92016-10-14 13:45:30 +02002193static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafbee91162016-03-04 01:09:59 +01002194 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2195
Alexander Graf3c63db92016-10-14 13:45:30 +02002196struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002197 .hdr = {
2198 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2199 .revision = 0x20005, /* 2.5 */
2200 .headersize = sizeof(struct efi_table_hdr),
2201 },
2202 .fw_vendor = (long)firmware_vendor,
2203 .con_in = (void*)&efi_con_in,
2204 .con_out = (void*)&efi_con_out,
2205 .std_err = (void*)&efi_con_out,
2206 .runtime = (void*)&efi_runtime_services,
2207 .boottime = (void*)&efi_boot_services,
2208 .nr_tables = 0,
2209 .tables = (void*)efi_conf_table,
2210};