blob: 9e8d8f486e0483372edc50758b6af22cbafefa33 [file] [log] [blame]
Alexander Grafc15d9212016-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 Grafc15d9212016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafc15d9212016-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 Schuchardt0c150ca2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020025
Alexander Grafc15d9212016-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 Graf393dd912016-10-14 13:45:30 +020045static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafc15d9212016-03-04 01:09:59 +010046
Simon Glasscdfe6962016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafc15d9212016-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 Glasscdfe6962016-09-25 15:27:35 -060055#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010056
Rob Clark86789d52017-07-27 08:04:18 -040057static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040058static int nesting_level;
Rob Clark86789d52017-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 Grafc15d9212016-03-04 01:09:59 +010082/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
Simon Glasscdfe6962016-09-25 15:27:35 -060085#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010086 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060087#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010088}
89
Rob Clark86789d52017-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 Grafc15d9212016-03-04 01:09:59 +010095void efi_restore_gd(void)
96{
Simon Glasscdfe6962016-09-25 15:27:35 -060097#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010098 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100101 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600102#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100103}
104
Rob Clarke7896c32017-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 Schuchardt4d664892017-08-18 17:45:16 +0200117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
Rob Clarke7896c32017-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 Schuchardta40db6e2017-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.de54c7a8e2017-07-18 20:17:22 +0200145void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200146{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200147 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200148 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200154 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200155 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200156}
157
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200158/*
Heinrich Schuchardta40db6e2017-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 Schuchardtf8d4ec32017-11-06 21:17:47 +0100168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100169{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100170 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200171
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200172 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-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 Grafc15d9212016-03-04 01:09:59 +0100182}
183
Heinrich Schuchardta40db6e2017-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 Schuchardtf8d4ec32017-11-06 21:17:47 +0100193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100194{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200195 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-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 Grafc15d9212016-03-04 01:09:59 +0100204}
205
Heinrich Schuchardta40db6e2017-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 Yamadab2a05c12017-06-22 17:49:03 +0900219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100220 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900221 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100222{
223 efi_status_t r;
224
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
Heinrich Schuchardta40db6e2017-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 Yamadab2a05c12017-06-22 17:49:03 +0900241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100242 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100243{
244 efi_status_t r;
245
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
Heinrich Schuchardta40db6e2017-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 Yamadab2a05c12017-06-22 17:49:03 +0900266static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100267 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900268 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900271 uint32_t *descriptor_version)
Alexander Grafc15d9212016-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 Schuchardta40db6e2017-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üns5a09aef2016-10-09 22:17:18 +0200294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100295 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200296 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100297{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100298 efi_status_t r;
299
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200301 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100302 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100303}
304
Heinrich Schuchardta40db6e2017-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üns67b67d92016-10-09 22:17:26 +0200315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100316{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200320 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100321 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100322}
323
Heinrich Schuchardteb6106e2017-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 Schuchardtcd522cb2017-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 Grafc15d9212016-03-04 01:09:59 +0100347/*
xypron.glpk@gmx.de30708232017-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 Grafc15d9212016-03-04 01:09:59 +0100350 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200351static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100352
Heinrich Schuchardta40db6e2017-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 Schuchardtf8d4ec32017-11-06 21:17:47 +0100368efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200369 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200370 struct efi_event *event,
371 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200372 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100373{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200374 int i;
375
Jonathan Gray7758b212017-03-12 19:26:07 +1100376 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200377 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100378
379 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200380 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100381
382 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
383 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200384 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100385
xypron.glpk@gmx.de30708232017-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 Schuchardt1bbee392017-10-04 15:03:24 +0200395 efi_events[i].is_queued = false;
396 efi_events[i].is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200397 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200398 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200399 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200400 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100401}
402
Heinrich Schuchardta40db6e2017-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.de852a0e1772017-07-18 20:17:20 +0200417static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100418 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-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 Grafc15d9212016-03-04 01:09:59 +0100431/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200432 * Check if a timer event has occurred or a queued notification function should
433 * be called.
434 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100435 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200436 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100437 */
438void efi_timer_check(void)
439{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200440 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100441 u64 now = timer_get_us();
442
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200444 if (!efi_events[i].type)
445 continue;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200446 if (efi_events[i].is_queued)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200447 efi_signal_event(&efi_events[i]);
448 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200449 now < efi_events[i].trigger_next)
450 continue;
Heinrich Schuchardt8b11a8a2017-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.de30708232017-07-18 20:17:18 +0200456 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200457 efi_events[i].trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200458 break;
459 default:
460 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200461 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200462 efi_events[i].is_signaled = true;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200463 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100464 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100465 WATCHDOG_RESET();
466}
467
Heinrich Schuchardta40db6e2017-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.de3ecc6bd2017-07-19 19:22:34 +0200479efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200480 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100481{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200482 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100483
xypron.glpk@gmx.de44c4be02017-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 Schuchardt368ca642017-10-05 16:14:14 +0200488 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100489
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200490 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
491 if (event != &efi_events[i])
492 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100493
xypron.glpk@gmx.de30708232017-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.de44c4be02017-07-18 20:17:23 +0200503 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200504 break;
505 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200506 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200507 }
508 event->trigger_type = type;
509 event->trigger_time = trigger_time;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200510 event->is_signaled = false;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200511 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100512 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200513 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100514}
515
Heinrich Schuchardta40db6e2017-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.de3ecc6bd2017-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.dea587fd12017-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));
534}
535
Heinrich Schuchardta40db6e2017-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 Schuchardt798a4412017-11-06 21:17:48 +0100548static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200549 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100550 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100551{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200552 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100553
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100554 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100555
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200556 /* Check parameters */
557 if (!num_events || !event)
558 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200559 /* Check TPL */
560 if (efi_tpl != TPL_APPLICATION)
561 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-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 Schuchardt1bbee392017-10-04 15:03:24 +0200571 if (!event[i]->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200572 efi_signal_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200573 }
574
575 /* Wait for signal */
576 for (;;) {
577 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200578 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-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 Schuchardt1bbee392017-10-04 15:03:24 +0200590 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200591 if (index)
592 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100593
594 return EFI_EXIT(EFI_SUCCESS);
595}
596
Heinrich Schuchardta40db6e2017-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 Schuchardtf1066d62017-10-08 06:57:27 +0200608 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200609 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200610static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100611{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200612 int i;
613
Alexander Grafc15d9212016-03-04 01:09:59 +0100614 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200615 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
616 if (event != &efi_events[i])
617 continue;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200618 if (event->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200619 break;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200620 event->is_signaled = true;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200621 if (event->type & EVT_NOTIFY_SIGNAL)
622 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200623 break;
624 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100625 return EFI_EXIT(EFI_SUCCESS);
626}
627
Heinrich Schuchardta40db6e2017-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.decdbf3ac2017-07-18 20:17:17 +0200638static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100639{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200640 int i;
641
Alexander Grafc15d9212016-03-04 01:09:59 +0100642 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-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 Schuchardt1bbee392017-10-04 15:03:24 +0200647 event->is_queued = false;
648 event->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200649 return EFI_EXIT(EFI_SUCCESS);
650 }
651 }
652 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100653}
654
Heinrich Schuchardta40db6e2017-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.decdbf3ac2017-07-18 20:17:17 +0200667static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100668{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200669 int i;
670
Alexander Grafc15d9212016-03-04 01:09:59 +0100671 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-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 Schuchardt1bbee392017-10-04 15:03:24 +0200678 if (!event->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200679 efi_signal_event(event);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200680 if (event->is_signaled)
xypron.glpk@gmx.de30708232017-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 Grafc15d9212016-03-04 01:09:59 +0100685}
686
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200687/*
Heinrich Schuchardt37ebcba2017-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 Schuchardt1cdca3f2017-10-26 19:25:50 +0200693struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200694{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100695 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200696
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100697 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200698 if (efiobj->handle == handle)
699 return efiobj;
700 }
701
702 return NULL;
703}
704
705/*
Heinrich Schuchardt5aef61d2017-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 Schuchardta40db6e2017-09-21 18:30:11 +0200825 * Install protocol interface.
826 *
Heinrich Schuchardt0a27ac82017-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 Schuchardta40db6e2017-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 Schuchardt0a27ac82017-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 Grafc15d9212016-03-04 01:09:59 +0100841{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200842 efi_status_t r;
843
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100844 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
845 protocol_interface);
846
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200847 if (!handle || !protocol ||
848 protocol_interface_type != EFI_NATIVE_INTERFACE) {
849 r = EFI_INVALID_PARAMETER;
850 goto out;
851 }
852
853 /* Create new handle if requested. */
854 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200855 r = efi_create_handle(handle);
856 if (r != EFI_SUCCESS)
857 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +0200858 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
859 *handle);
860 } else {
861 debug("%sEFI: handle %p\n", indent_string(nesting_level),
862 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200863 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +0200864 /* Add new protocol */
865 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200866out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100867 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100868}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200869
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200870/*
871 * Reinstall protocol interface.
872 *
873 * This function implements the ReinstallProtocolInterface service.
874 * See the Unified Extensible Firmware Interface (UEFI) specification
875 * for details.
876 *
877 * @handle handle on which the protocol shall be
878 * reinstalled
879 * @protocol GUID of the protocol to be installed
880 * @old_interface interface to be removed
881 * @new_interface interface to be installed
882 * @return status code
883 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100884static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200885 const efi_guid_t *protocol, void *old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100886 void *new_interface)
887{
Rob Clark238f88c2017-09-13 18:05:41 -0400888 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100889 new_interface);
890 return EFI_EXIT(EFI_ACCESS_DENIED);
891}
892
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200893/*
894 * Uninstall protocol interface.
895 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100896 * This function implements the UninstallProtocolInterface service.
897 * See the Unified Extensible Firmware Interface (UEFI) specification
898 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200899 *
900 * @handle handle from which the protocol shall be removed
901 * @protocol GUID of the protocol to be removed
902 * @protocol_interface interface to be removed
903 * @return status code
904 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100905static efi_status_t EFIAPI efi_uninstall_protocol_interface(
906 void *handle, const efi_guid_t *protocol,
907 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100908{
Heinrich Schuchardt7538c272017-10-26 19:25:56 +0200909 struct efi_handler *handler;
910 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200911
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100912 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
913
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200914 if (!handle || !protocol) {
915 r = EFI_INVALID_PARAMETER;
916 goto out;
917 }
918
Heinrich Schuchardt7538c272017-10-26 19:25:56 +0200919 /* Find the protocol on the handle */
920 r = efi_search_protocol(handle, protocol, &handler);
921 if (r != EFI_SUCCESS)
922 goto out;
923 if (handler->protocol_interface) {
924 /* TODO disconnect controllers */
925 r = EFI_ACCESS_DENIED;
926 } else {
927 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200928 }
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200929out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100930 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100931}
932
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200933/*
934 * Register an event for notification when a protocol is installed.
935 *
936 * This function implements the RegisterProtocolNotify service.
937 * See the Unified Extensible Firmware Interface (UEFI) specification
938 * for details.
939 *
940 * @protocol GUID of the protocol whose installation shall be
941 * notified
942 * @event event to be signaled upon installation of the protocol
943 * @registration key for retrieving the registration information
944 * @return status code
945 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200946static efi_status_t EFIAPI efi_register_protocol_notify(
947 const efi_guid_t *protocol,
948 struct efi_event *event,
949 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +0100950{
Rob Clark238f88c2017-09-13 18:05:41 -0400951 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +0100952 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
953}
954
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200955/*
956 * Determine if an EFI handle implements a protocol.
957 *
958 * See the documentation of the LocateHandle service in the UEFI specification.
959 *
960 * @search_type selection criterion
961 * @protocol GUID of the protocol
962 * @search_key registration key
963 * @efiobj handle
964 * @return 0 if the handle implements the protocol
965 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100966static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200967 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +0100968 struct efi_object *efiobj)
969{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +0200970 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +0100971
972 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100973 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +0100974 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100975 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +0200976 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +0100977 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100978 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +0200979 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
980 return (ret != EFI_SUCCESS);
981 default:
982 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +0100983 return -1;
984 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100985}
986
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200987/*
988 * Locate handles implementing a protocol.
989 *
990 * This function is meant for U-Boot internal calls. For the API implementation
991 * of the LocateHandle service see efi_locate_handle_ext.
992 *
993 * @search_type selection criterion
994 * @protocol GUID of the protocol
995 * @search_key registration key
996 * @buffer_size size of the buffer to receive the handles in bytes
997 * @buffer buffer to receive the relevant handles
998 * @return status code
999 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001000static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001001 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001002 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001003 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001004{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001005 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001006 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001007
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001008 /* Check parameters */
1009 switch (search_type) {
1010 case ALL_HANDLES:
1011 break;
1012 case BY_REGISTER_NOTIFY:
1013 if (!search_key)
1014 return EFI_INVALID_PARAMETER;
1015 /* RegisterProtocolNotify is not implemented yet */
1016 return EFI_UNSUPPORTED;
1017 case BY_PROTOCOL:
1018 if (!protocol)
1019 return EFI_INVALID_PARAMETER;
1020 break;
1021 default:
1022 return EFI_INVALID_PARAMETER;
1023 }
1024
1025 /*
1026 * efi_locate_handle_buffer uses this function for
1027 * the calculation of the necessary buffer size.
1028 * So do not require a buffer for buffersize == 0.
1029 */
1030 if (!buffer_size || (*buffer_size && !buffer))
1031 return EFI_INVALID_PARAMETER;
1032
Alexander Grafc15d9212016-03-04 01:09:59 +01001033 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001034 list_for_each_entry(efiobj, &efi_obj_list, link) {
1035 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafc15d9212016-03-04 01:09:59 +01001036 size += sizeof(void*);
Alexander Grafc15d9212016-03-04 01:09:59 +01001037 }
1038
1039 if (*buffer_size < size) {
1040 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001041 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001042 }
1043
Rob Clarkcdee3372017-08-06 14:10:07 -04001044 *buffer_size = size;
1045 if (size == 0)
1046 return EFI_NOT_FOUND;
1047
Alexander Grafc15d9212016-03-04 01:09:59 +01001048 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001049 list_for_each_entry(efiobj, &efi_obj_list, link) {
1050 if (!efi_search(search_type, protocol, search_key, efiobj))
1051 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001052 }
1053
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001054 return EFI_SUCCESS;
1055}
1056
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001057/*
1058 * Locate handles implementing a protocol.
1059 *
1060 * This function implements the LocateHandle service.
1061 * See the Unified Extensible Firmware Interface (UEFI) specification
1062 * for details.
1063 *
1064 * @search_type selection criterion
1065 * @protocol GUID of the protocol
1066 * @search_key registration key
1067 * @buffer_size size of the buffer to receive the handles in bytes
1068 * @buffer buffer to receive the relevant handles
1069 * @return 0 if the handle implements the protocol
1070 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001071static efi_status_t EFIAPI efi_locate_handle_ext(
1072 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001073 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001074 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001075{
Rob Clark238f88c2017-09-13 18:05:41 -04001076 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001077 buffer_size, buffer);
1078
1079 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1080 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001081}
1082
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001083/*
1084 * Get the device path and handle of an device implementing a protocol.
1085 *
1086 * This function implements the LocateDevicePath service.
1087 * See the Unified Extensible Firmware Interface (UEFI) specification
1088 * for details.
1089 *
1090 * @protocol GUID of the protocol
1091 * @device_path device path
1092 * @device handle of the device
1093 * @return status code
1094 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001095static efi_status_t EFIAPI efi_locate_device_path(
1096 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001097 struct efi_device_path **device_path,
1098 efi_handle_t *device)
1099{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001100 struct efi_object *efiobj;
1101
1102 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1103
1104 efiobj = efi_dp_find_obj(*device_path, device_path);
1105 if (!efiobj)
1106 return EFI_EXIT(EFI_NOT_FOUND);
1107
1108 *device = efiobj->handle;
1109
1110 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001111}
1112
Alexander Graffe3366f2017-07-26 13:41:04 +02001113/* Collapses configuration table entries, removing index i */
1114static void efi_remove_configuration_table(int i)
1115{
1116 struct efi_configuration_table *this = &efi_conf_table[i];
1117 struct efi_configuration_table *next = &efi_conf_table[i+1];
1118 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1119
1120 memmove(this, next, (ulong)end - (ulong)next);
1121 systab.nr_tables--;
1122}
1123
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001124/*
1125 * Adds, updates, or removes a configuration table.
1126 *
1127 * This function is used for internal calls. For the API implementation of the
1128 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1129 *
1130 * @guid GUID of the installed table
1131 * @table table to be installed
1132 * @return status code
1133 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001134efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001135{
1136 int i;
1137
Alexander Grafc15d9212016-03-04 01:09:59 +01001138 /* Check for guid override */
1139 for (i = 0; i < systab.nr_tables; i++) {
1140 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001141 if (table)
1142 efi_conf_table[i].table = table;
1143 else
1144 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +02001145 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001146 }
1147 }
1148
Alexander Graffe3366f2017-07-26 13:41:04 +02001149 if (!table)
1150 return EFI_NOT_FOUND;
1151
Alexander Grafc15d9212016-03-04 01:09:59 +01001152 /* No override, check for overflow */
1153 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001154 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001155
1156 /* Add a new entry */
1157 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1158 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001159 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001160
Alexander Grafc5c11632016-08-19 01:23:24 +02001161 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001162}
1163
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001164/*
1165 * Adds, updates, or removes a configuration table.
1166 *
1167 * This function implements the InstallConfigurationTable service.
1168 * See the Unified Extensible Firmware Interface (UEFI) specification
1169 * for details.
1170 *
1171 * @guid GUID of the installed table
1172 * @table table to be installed
1173 * @return status code
1174 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001175static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1176 void *table)
1177{
Rob Clark238f88c2017-09-13 18:05:41 -04001178 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001179 return EFI_EXIT(efi_install_configuration_table(guid, table));
1180}
1181
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001182/*
1183 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001184 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001185 *
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001186 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001187 * image
1188 * @obj internal object associated with the loaded image
1189 * @device_path device path of the loaded image
1190 * @file_path file path of the loaded image
Rob Clarkf8db9222017-09-13 18:05:33 -04001191 */
1192void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1193 struct efi_device_path *device_path,
1194 struct efi_device_path *file_path)
1195{
1196 obj->handle = info;
1197
1198 /*
1199 * When asking for the device path interface, return
1200 * bootefi_device_path
1201 */
1202 obj->protocols[0].guid = &efi_guid_device_path;
1203 obj->protocols[0].protocol_interface = device_path;
1204
1205 /*
1206 * When asking for the loaded_image interface, just
1207 * return handle which points to loaded_image_info
1208 */
1209 obj->protocols[1].guid = &efi_guid_loaded_image;
1210 obj->protocols[1].protocol_interface = info;
1211
1212 obj->protocols[2].guid = &efi_guid_console_control;
1213 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1214
1215 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1216 obj->protocols[3].protocol_interface =
1217 (void *)&efi_device_path_to_text;
1218
1219 info->file_path = file_path;
Heinrich Schuchardt8cb12542017-10-08 06:57:26 +02001220 if (device_path)
1221 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clarkf8db9222017-09-13 18:05:33 -04001222
1223 list_add_tail(&obj->link, &efi_obj_list);
1224}
1225
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001226/*
1227 * Load an image using a file path.
1228 *
1229 * @file_path the path of the image to load
1230 * @buffer buffer containing the loaded image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001231 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001232 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001233efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1234 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001235{
1236 struct efi_file_info *info = NULL;
1237 struct efi_file_handle *f;
1238 static efi_status_t ret;
1239 uint64_t bs;
1240
1241 f = efi_file_from_path(file_path);
1242 if (!f)
1243 return EFI_DEVICE_ERROR;
1244
1245 bs = 0;
1246 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1247 &bs, info));
1248 if (ret == EFI_BUFFER_TOO_SMALL) {
1249 info = malloc(bs);
1250 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1251 &bs, info));
1252 }
1253 if (ret != EFI_SUCCESS)
1254 goto error;
1255
1256 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1257 if (ret)
1258 goto error;
1259
1260 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1261
1262error:
1263 free(info);
1264 EFI_CALL(f->close(f));
1265
1266 if (ret != EFI_SUCCESS) {
1267 efi_free_pool(*buffer);
1268 *buffer = NULL;
1269 }
1270
1271 return ret;
1272}
1273
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001274/*
1275 * Load an EFI image into memory.
1276 *
1277 * This function implements the LoadImage service.
1278 * See the Unified Extensible Firmware Interface (UEFI) specification
1279 * for details.
1280 *
1281 * @boot_policy true for request originating from the boot manager
1282 * @parent_image the calles's image handle
1283 * @file_path the path of the image to load
1284 * @source_buffer memory location from which the image is installed
1285 * @source_size size of the memory area from which the image is
1286 * installed
1287 * @image_handle handle for the newly installed image
1288 * @return status code
1289 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001290static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1291 efi_handle_t parent_image,
1292 struct efi_device_path *file_path,
1293 void *source_buffer,
1294 unsigned long source_size,
1295 efi_handle_t *image_handle)
1296{
Alexander Grafc15d9212016-03-04 01:09:59 +01001297 struct efi_loaded_image *info;
1298 struct efi_object *obj;
1299
1300 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1301 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001302
1303 info = calloc(1, sizeof(*info));
1304 obj = calloc(1, sizeof(*obj));
1305
1306 if (!source_buffer) {
1307 struct efi_device_path *dp, *fp;
1308 efi_status_t ret;
1309
Rob Clarkc84c1102017-09-13 18:05:38 -04001310 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark857a1222017-09-13 18:05:35 -04001311 if (ret != EFI_SUCCESS) {
1312 free(info);
1313 free(obj);
1314 return EFI_EXIT(ret);
1315 }
1316
1317 /*
1318 * split file_path which contains both the device and
1319 * file parts:
1320 */
1321 efi_dp_split_file_path(file_path, &dp, &fp);
1322
1323 efi_setup_loaded_image(info, obj, dp, fp);
1324 } else {
1325 /* In this case, file_path is the "device" path, ie.
1326 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1327 */
1328 efi_setup_loaded_image(info, obj, file_path, NULL);
1329 }
1330
Alexander Grafc15d9212016-03-04 01:09:59 +01001331 info->reserved = efi_load_pe(source_buffer, info);
1332 if (!info->reserved) {
1333 free(info);
1334 free(obj);
1335 return EFI_EXIT(EFI_UNSUPPORTED);
1336 }
1337
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001338 info->system_table = &systab;
1339 info->parent_handle = parent_image;
Alexander Grafc15d9212016-03-04 01:09:59 +01001340 *image_handle = info;
Alexander Grafc15d9212016-03-04 01:09:59 +01001341
1342 return EFI_EXIT(EFI_SUCCESS);
1343}
1344
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001345/*
1346 * Call the entry point of an image.
1347 *
1348 * This function implements the StartImage service.
1349 * See the Unified Extensible Firmware Interface (UEFI) specification
1350 * for details.
1351 *
1352 * @image_handle handle of the image
1353 * @exit_data_size size of the buffer
1354 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001355 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001356 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001357static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1358 unsigned long *exit_data_size,
1359 s16 **exit_data)
1360{
1361 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1362 struct efi_loaded_image *info = image_handle;
1363
1364 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1365 entry = info->reserved;
1366
1367 efi_is_direct_boot = false;
1368
1369 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001370 if (setjmp(&info->exit_jmp)) {
1371 /* We returned from the child image */
1372 return EFI_EXIT(info->exit_status);
1373 }
1374
Rob Clarke7896c32017-07-27 08:04:19 -04001375 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -04001376 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +01001377 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -04001378 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -04001379 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +01001380
1381 /* Should usually never get here */
1382 return EFI_EXIT(EFI_SUCCESS);
1383}
1384
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001385/*
1386 * Leave an EFI application or driver.
1387 *
1388 * This function implements the Exit service.
1389 * See the Unified Extensible Firmware Interface (UEFI) specification
1390 * for details.
1391 *
1392 * @image_handle handle of the application or driver that is exiting
1393 * @exit_status status code
1394 * @exit_data_size size of the buffer in bytes
1395 * @exit_data buffer with data describing an error
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001396 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001397 */
Alexander Graf988c0662016-05-20 23:28:23 +02001398static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1399 efi_status_t exit_status, unsigned long exit_data_size,
1400 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001401{
Alexander Graf988c0662016-05-20 23:28:23 +02001402 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1403
Alexander Grafc15d9212016-03-04 01:09:59 +01001404 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1405 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001406
Alexander Graf87e787a2017-09-03 14:14:17 +02001407 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001408 __efi_exit_check();
1409
Alexander Graf87e787a2017-09-03 14:14:17 +02001410 /*
1411 * But longjmp out with the U-Boot gd, not the application's, as
1412 * the other end is a setjmp call inside EFI context.
1413 */
1414 efi_restore_gd();
1415
Alexander Graf988c0662016-05-20 23:28:23 +02001416 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001417 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001418
1419 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001420}
1421
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001422/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001423 * Unload an EFI image.
1424 *
1425 * This function implements the UnloadImage service.
1426 * See the Unified Extensible Firmware Interface (UEFI) specification
1427 * for details.
1428 *
1429 * @image_handle handle of the image to be unloaded
1430 * @return status code
1431 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001432static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1433{
1434 struct efi_object *efiobj;
1435
1436 EFI_ENTRY("%p", image_handle);
1437 efiobj = efi_search_obj(image_handle);
1438 if (efiobj)
1439 list_del(&efiobj->link);
1440
1441 return EFI_EXIT(EFI_SUCCESS);
1442}
1443
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001444/*
1445 * Fix up caches for EFI payloads if necessary.
1446 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001447static void efi_exit_caches(void)
1448{
1449#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1450 /*
1451 * Grub on 32bit ARM needs to have caches disabled before jumping into
1452 * a zImage, but does not know of all cache layers. Give it a hand.
1453 */
1454 if (efi_is_direct_boot)
1455 cleanup_before_linux();
1456#endif
1457}
1458
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001459/*
1460 * Stop boot services.
1461 *
1462 * This function implements the ExitBootServices service.
1463 * See the Unified Extensible Firmware Interface (UEFI) specification
1464 * for details.
1465 *
1466 * @image_handle handle of the loaded image
1467 * @map_key key of the memory map
1468 * @return status code
1469 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001470static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1471 unsigned long map_key)
1472{
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001473 int i;
1474
Alexander Grafc15d9212016-03-04 01:09:59 +01001475 EFI_ENTRY("%p, %ld", image_handle, map_key);
1476
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001477 /* Notify that ExitBootServices is invoked. */
1478 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1479 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1480 continue;
1481 efi_signal_event(&efi_events[i]);
1482 }
1483 /* Make sure that notification functions are not called anymore */
1484 efi_tpl = TPL_HIGH_LEVEL;
1485
Alexander Graf600af332017-10-19 23:23:50 +02001486 /* XXX Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001487
Alexander Graf2ebeb442016-11-17 01:02:57 +01001488 board_quiesce_devices();
1489
Alexander Grafc15d9212016-03-04 01:09:59 +01001490 /* Fix up caches for EFI payloads if necessary */
1491 efi_exit_caches();
1492
1493 /* This stops all lingering devices */
1494 bootm_disable_interrupts();
1495
1496 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001497 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001498 WATCHDOG_RESET();
1499
1500 return EFI_EXIT(EFI_SUCCESS);
1501}
1502
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001503/*
1504 * Get next value of the counter.
1505 *
1506 * This function implements the NextMonotonicCount service.
1507 * See the Unified Extensible Firmware Interface (UEFI) specification
1508 * for details.
1509 *
1510 * @count returned value of the counter
1511 * @return status code
1512 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001513static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1514{
1515 static uint64_t mono = 0;
1516 EFI_ENTRY("%p", count);
1517 *count = mono++;
1518 return EFI_EXIT(EFI_SUCCESS);
1519}
1520
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001521/*
1522 * Sleep.
1523 *
1524 * This function implements the Stall sercive.
1525 * See the Unified Extensible Firmware Interface (UEFI) specification
1526 * for details.
1527 *
1528 * @microseconds period to sleep in microseconds
1529 * @return status code
1530 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001531static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1532{
1533 EFI_ENTRY("%ld", microseconds);
1534 udelay(microseconds);
1535 return EFI_EXIT(EFI_SUCCESS);
1536}
1537
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001538/*
1539 * Reset the watchdog timer.
1540 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001541 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001542 * See the Unified Extensible Firmware Interface (UEFI) specification
1543 * for details.
1544 *
1545 * @timeout seconds before reset by watchdog
1546 * @watchdog_code code to be logged when resetting
1547 * @data_size size of buffer in bytes
1548 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001549 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001550 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001551static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1552 uint64_t watchdog_code,
1553 unsigned long data_size,
1554 uint16_t *watchdog_data)
1555{
1556 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1557 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001558 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001559}
1560
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001561/*
1562 * Connect a controller to a driver.
1563 *
1564 * This function implements the ConnectController service.
1565 * See the Unified Extensible Firmware Interface (UEFI) specification
1566 * for details.
1567 *
1568 * @controller_handle handle of the controller
1569 * @driver_image_handle handle of the driver
1570 * @remain_device_path device path of a child controller
1571 * @recursive true to connect all child controllers
1572 * @return status code
1573 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001574static efi_status_t EFIAPI efi_connect_controller(
1575 efi_handle_t controller_handle,
1576 efi_handle_t *driver_image_handle,
1577 struct efi_device_path *remain_device_path,
1578 bool recursive)
1579{
1580 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1581 remain_device_path, recursive);
1582 return EFI_EXIT(EFI_NOT_FOUND);
1583}
1584
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001585/*
1586 * Disconnect a controller from a driver.
1587 *
1588 * This function implements the DisconnectController service.
1589 * See the Unified Extensible Firmware Interface (UEFI) specification
1590 * for details.
1591 *
1592 * @controller_handle handle of the controller
1593 * @driver_image_handle handle of the driver
1594 * @child_handle handle of the child to destroy
1595 * @return status code
1596 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001597static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1598 void *driver_image_handle,
1599 void *child_handle)
1600{
1601 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1602 child_handle);
1603 return EFI_EXIT(EFI_INVALID_PARAMETER);
1604}
1605
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001606/*
1607 * Close a protocol.
1608 *
1609 * This function implements the CloseProtocol service.
1610 * See the Unified Extensible Firmware Interface (UEFI) specification
1611 * for details.
1612 *
1613 * @handle handle on which the protocol shall be closed
1614 * @protocol GUID of the protocol to close
1615 * @agent_handle handle of the driver
1616 * @controller_handle handle of the controller
1617 * @return status code
1618 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001619static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001620 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001621 void *agent_handle,
1622 void *controller_handle)
1623{
Rob Clark238f88c2017-09-13 18:05:41 -04001624 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001625 controller_handle);
1626 return EFI_EXIT(EFI_NOT_FOUND);
1627}
1628
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001629/*
1630 * Provide information about then open status of a protocol on a handle
1631 *
1632 * This function implements the OpenProtocolInformation service.
1633 * See the Unified Extensible Firmware Interface (UEFI) specification
1634 * for details.
1635 *
1636 * @handle handle for which the information shall be retrieved
1637 * @protocol GUID of the protocol
1638 * @entry_buffer buffer to receive the open protocol information
1639 * @entry_count number of entries available in the buffer
1640 * @return status code
1641 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001642static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001643 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001644 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001645 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001646{
Rob Clark238f88c2017-09-13 18:05:41 -04001647 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001648 entry_count);
1649 return EFI_EXIT(EFI_NOT_FOUND);
1650}
1651
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001652/*
1653 * Get protocols installed on a handle.
1654 *
1655 * This function implements the ProtocolsPerHandleService.
1656 * See the Unified Extensible Firmware Interface (UEFI) specification
1657 * for details.
1658 *
1659 * @handle handle for which the information is retrieved
1660 * @protocol_buffer buffer with protocol GUIDs
1661 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001662 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001663 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001664static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1665 efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001666 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001667{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001668 unsigned long buffer_size;
1669 struct efi_object *efiobj;
1670 unsigned long i, j;
1671 struct list_head *lhandle;
1672 efi_status_t r;
1673
Alexander Grafc15d9212016-03-04 01:09:59 +01001674 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1675 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001676
1677 if (!handle || !protocol_buffer || !protocol_buffer_count)
1678 return EFI_EXIT(EFI_INVALID_PARAMETER);
1679
1680 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04001681 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001682 list_for_each(lhandle, &efi_obj_list) {
1683 efiobj = list_entry(lhandle, struct efi_object, link);
1684
1685 if (efiobj->handle != handle)
1686 continue;
1687
1688 /* Count protocols */
1689 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1690 if (efiobj->protocols[i].guid)
1691 ++*protocol_buffer_count;
1692 }
1693 /* Copy guids */
1694 if (*protocol_buffer_count) {
1695 buffer_size = sizeof(efi_guid_t *) *
1696 *protocol_buffer_count;
1697 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1698 buffer_size,
1699 (void **)protocol_buffer);
1700 if (r != EFI_SUCCESS)
1701 return EFI_EXIT(r);
1702 j = 0;
1703 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1704 if (efiobj->protocols[i].guid) {
1705 (*protocol_buffer)[j] = (void *)
1706 efiobj->protocols[i].guid;
1707 ++j;
1708 }
1709 }
1710 }
1711 break;
1712 }
1713
1714 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001715}
1716
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001717/*
1718 * Locate handles implementing a protocol.
1719 *
1720 * This function implements the LocateHandleBuffer service.
1721 * See the Unified Extensible Firmware Interface (UEFI) specification
1722 * for details.
1723 *
1724 * @search_type selection criterion
1725 * @protocol GUID of the protocol
1726 * @search_key registration key
1727 * @no_handles number of returned handles
1728 * @buffer buffer with the returned handles
1729 * @return status code
1730 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001731static efi_status_t EFIAPI efi_locate_handle_buffer(
1732 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001733 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001734 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001735{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001736 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001737 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001738
Rob Clark238f88c2017-09-13 18:05:41 -04001739 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001740 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001741
1742 if (!no_handles || !buffer) {
1743 r = EFI_INVALID_PARAMETER;
1744 goto out;
1745 }
1746 *no_handles = 0;
1747 *buffer = NULL;
1748 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1749 *buffer);
1750 if (r != EFI_BUFFER_TOO_SMALL)
1751 goto out;
1752 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1753 (void **)buffer);
1754 if (r != EFI_SUCCESS)
1755 goto out;
1756 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1757 *buffer);
1758 if (r == EFI_SUCCESS)
1759 *no_handles = buffer_size / sizeof(void *);
1760out:
1761 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001762}
1763
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001764/*
1765 * Find an interface implementing a protocol.
1766 *
1767 * This function implements the LocateProtocol service.
1768 * See the Unified Extensible Firmware Interface (UEFI) specification
1769 * for details.
1770 *
1771 * @protocol GUID of the protocol
1772 * @registration registration key passed to the notification function
1773 * @protocol_interface interface implementing the protocol
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001774 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001775 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001776static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001777 void *registration,
1778 void **protocol_interface)
1779{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001780 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02001781 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001782
Rob Clark238f88c2017-09-13 18:05:41 -04001783 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001784
1785 if (!protocol || !protocol_interface)
1786 return EFI_EXIT(EFI_INVALID_PARAMETER);
1787
1788 list_for_each(lhandle, &efi_obj_list) {
1789 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02001790 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001791
1792 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001793
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02001794 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1795 if (ret == EFI_SUCCESS) {
1796 *protocol_interface = handler->protocol_interface;
1797 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001798 }
1799 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001800 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001801
1802 return EFI_EXIT(EFI_NOT_FOUND);
1803}
1804
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001805/*
1806 * Install multiple protocol interfaces.
1807 *
1808 * This function implements the MultipleProtocolInterfaces service.
1809 * See the Unified Extensible Firmware Interface (UEFI) specification
1810 * for details.
1811 *
1812 * @handle handle on which the protocol interfaces shall be installed
1813 * @... NULL terminated argument list with pairs of protocol GUIDS and
1814 * interfaces
1815 * @return status code
1816 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001817static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1818 void **handle, ...)
1819{
1820 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001821
1822 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001823 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001824 void *protocol_interface;
1825 efi_status_t r = EFI_SUCCESS;
1826 int i = 0;
1827
1828 if (!handle)
1829 return EFI_EXIT(EFI_INVALID_PARAMETER);
1830
1831 va_start(argptr, handle);
1832 for (;;) {
1833 protocol = va_arg(argptr, efi_guid_t*);
1834 if (!protocol)
1835 break;
1836 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001837 r = EFI_CALL(efi_install_protocol_interface(
1838 handle, protocol,
1839 EFI_NATIVE_INTERFACE,
1840 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001841 if (r != EFI_SUCCESS)
1842 break;
1843 i++;
1844 }
1845 va_end(argptr);
1846 if (r == EFI_SUCCESS)
1847 return EFI_EXIT(r);
1848
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02001849 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001850 va_start(argptr, handle);
1851 for (; i; --i) {
1852 protocol = va_arg(argptr, efi_guid_t*);
1853 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001854 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1855 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001856 }
1857 va_end(argptr);
1858
1859 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001860}
1861
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001862/*
1863 * Uninstall multiple protocol interfaces.
1864 *
1865 * This function implements the UninstallMultipleProtocolInterfaces service.
1866 * See the Unified Extensible Firmware Interface (UEFI) specification
1867 * for details.
1868 *
1869 * @handle handle from which the protocol interfaces shall be removed
1870 * @... NULL terminated argument list with pairs of protocol GUIDS and
1871 * interfaces
1872 * @return status code
1873 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001874static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1875 void *handle, ...)
1876{
1877 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02001878
1879 va_list argptr;
1880 const efi_guid_t *protocol;
1881 void *protocol_interface;
1882 efi_status_t r = EFI_SUCCESS;
1883 size_t i = 0;
1884
1885 if (!handle)
1886 return EFI_EXIT(EFI_INVALID_PARAMETER);
1887
1888 va_start(argptr, handle);
1889 for (;;) {
1890 protocol = va_arg(argptr, efi_guid_t*);
1891 if (!protocol)
1892 break;
1893 protocol_interface = va_arg(argptr, void*);
1894 r = EFI_CALL(efi_uninstall_protocol_interface(
1895 handle, protocol,
1896 protocol_interface));
1897 if (r != EFI_SUCCESS)
1898 break;
1899 i++;
1900 }
1901 va_end(argptr);
1902 if (r == EFI_SUCCESS)
1903 return EFI_EXIT(r);
1904
1905 /* If an error occurred undo all changes. */
1906 va_start(argptr, handle);
1907 for (; i; --i) {
1908 protocol = va_arg(argptr, efi_guid_t*);
1909 protocol_interface = va_arg(argptr, void*);
1910 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1911 EFI_NATIVE_INTERFACE,
1912 protocol_interface));
1913 }
1914 va_end(argptr);
1915
1916 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001917}
1918
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001919/*
1920 * Calculate cyclic redundancy code.
1921 *
1922 * This function implements the CalculateCrc32 service.
1923 * See the Unified Extensible Firmware Interface (UEFI) specification
1924 * for details.
1925 *
1926 * @data buffer with data
1927 * @data_size size of buffer in bytes
1928 * @crc32_p cyclic redundancy code
1929 * @return status code
1930 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001931static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1932 unsigned long data_size,
1933 uint32_t *crc32_p)
1934{
1935 EFI_ENTRY("%p, %ld", data, data_size);
1936 *crc32_p = crc32(0, data, data_size);
1937 return EFI_EXIT(EFI_SUCCESS);
1938}
1939
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001940/*
1941 * Copy memory.
1942 *
1943 * This function implements the CopyMem service.
1944 * See the Unified Extensible Firmware Interface (UEFI) specification
1945 * for details.
1946 *
1947 * @destination destination of the copy operation
1948 * @source source of the copy operation
1949 * @length number of bytes to copy
1950 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001951static void EFIAPI efi_copy_mem(void *destination, const void *source,
1952 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01001953{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001954 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01001955 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001956 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001957}
1958
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001959/*
1960 * Fill memory with a byte value.
1961 *
1962 * This function implements the SetMem service.
1963 * See the Unified Extensible Firmware Interface (UEFI) specification
1964 * for details.
1965 *
1966 * @buffer buffer to fill
1967 * @size size of buffer in bytes
1968 * @value byte to copy to the buffer
1969 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001970static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01001971{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001972 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01001973 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001974 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001975}
1976
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001977/*
1978 * Open protocol interface on a handle.
1979 *
1980 * This function implements the OpenProtocol interface.
1981 * See the Unified Extensible Firmware Interface (UEFI) specification
1982 * for details.
1983 *
1984 * @handle handle on which the protocol shall be opened
1985 * @protocol GUID of the protocol
1986 * @protocol_interface interface implementing the protocol
1987 * @agent_handle handle of the driver
1988 * @controller_handle handle of the controller
1989 * @attributes attributes indicating how to open the protocol
1990 * @return status code
1991 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001992static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001993 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001994 void **protocol_interface, void *agent_handle,
1995 void *controller_handle, uint32_t attributes)
1996{
1997 struct list_head *lhandle;
1998 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001999 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002000
Rob Clark238f88c2017-09-13 18:05:41 -04002001 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002002 protocol_interface, agent_handle, controller_handle,
2003 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002004
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002005 if (!handle || !protocol ||
2006 (!protocol_interface && attributes !=
2007 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002008 goto out;
2009 }
2010
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02002011 EFI_PRINT_GUID("protocol", protocol);
2012
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002013 switch (attributes) {
2014 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2015 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2016 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2017 break;
2018 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2019 if (controller_handle == handle)
2020 goto out;
2021 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2022 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2023 if (controller_handle == NULL)
2024 goto out;
2025 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2026 if (agent_handle == NULL)
2027 goto out;
2028 break;
2029 default:
2030 goto out;
2031 }
2032
Alexander Grafc15d9212016-03-04 01:09:59 +01002033 list_for_each(lhandle, &efi_obj_list) {
2034 struct efi_object *efiobj;
2035 efiobj = list_entry(lhandle, struct efi_object, link);
2036
2037 if (efiobj->handle != handle)
2038 continue;
2039
2040 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
2041 struct efi_handler *handler = &efiobj->protocols[i];
2042 const efi_guid_t *hprotocol = handler->guid;
2043 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02002044 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01002045 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002046 if (attributes !=
2047 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
2048 *protocol_interface =
2049 handler->protocol_interface;
2050 }
2051 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01002052 goto out;
2053 }
2054 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002055 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01002056 }
2057
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002058unsupported:
2059 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01002060out:
2061 return EFI_EXIT(r);
2062}
2063
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002064/*
2065 * Get interface of a protocol on a handle.
2066 *
2067 * This function implements the HandleProtocol service.
2068 * See the Unified Extensible Firmware Interface (UEFI) specification
2069 * for details.
2070 *
2071 * @handle handle on which the protocol shall be opened
2072 * @protocol GUID of the protocol
2073 * @protocol_interface interface implementing the protocol
2074 * @return status code
2075 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002076static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002077 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002078 void **protocol_interface)
2079{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002080 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2081 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002082}
2083
2084static const struct efi_boot_services efi_boot_services = {
2085 .hdr = {
2086 .headersize = sizeof(struct efi_table_hdr),
2087 },
2088 .raise_tpl = efi_raise_tpl,
2089 .restore_tpl = efi_restore_tpl,
2090 .allocate_pages = efi_allocate_pages_ext,
2091 .free_pages = efi_free_pages_ext,
2092 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02002093 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02002094 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02002095 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02002096 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002097 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02002098 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002099 .close_event = efi_close_event,
2100 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002101 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002102 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002103 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002104 .handle_protocol = efi_handle_protocol,
2105 .reserved = NULL,
2106 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02002107 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002108 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02002109 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002110 .load_image = efi_load_image,
2111 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02002112 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01002113 .unload_image = efi_unload_image,
2114 .exit_boot_services = efi_exit_boot_services,
2115 .get_next_monotonic_count = efi_get_next_monotonic_count,
2116 .stall = efi_stall,
2117 .set_watchdog_timer = efi_set_watchdog_timer,
2118 .connect_controller = efi_connect_controller,
2119 .disconnect_controller = efi_disconnect_controller,
2120 .open_protocol = efi_open_protocol,
2121 .close_protocol = efi_close_protocol,
2122 .open_protocol_information = efi_open_protocol_information,
2123 .protocols_per_handle = efi_protocols_per_handle,
2124 .locate_handle_buffer = efi_locate_handle_buffer,
2125 .locate_protocol = efi_locate_protocol,
2126 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2127 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2128 .calculate_crc32 = efi_calculate_crc32,
2129 .copy_mem = efi_copy_mem,
2130 .set_mem = efi_set_mem,
2131};
2132
2133
Alexander Graf393dd912016-10-14 13:45:30 +02002134static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01002135 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2136
Alexander Graf393dd912016-10-14 13:45:30 +02002137struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01002138 .hdr = {
2139 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2140 .revision = 0x20005, /* 2.5 */
2141 .headersize = sizeof(struct efi_table_hdr),
2142 },
2143 .fw_vendor = (long)firmware_vendor,
2144 .con_in = (void*)&efi_con_in,
2145 .con_out = (void*)&efi_con_out,
2146 .std_err = (void*)&efi_con_out,
2147 .runtime = (void*)&efi_runtime_services,
2148 .boottime = (void*)&efi_boot_services,
2149 .nr_tables = 0,
2150 .tables = (void*)efi_conf_table,
2151};