blob: fdbdfc4670b63aee3faa53fd95574d495239510f [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 Schuchardtcd522cb2017-08-27 00:51:09 +0200324static efi_status_t efi_create_handle(void **handle)
325{
326 struct efi_object *obj;
327 efi_status_t r;
328
329 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
330 sizeof(struct efi_object),
331 (void **)&obj);
332 if (r != EFI_SUCCESS)
333 return r;
334 memset(obj, 0, sizeof(struct efi_object));
335 obj->handle = obj;
336 list_add_tail(&obj->link, &efi_obj_list);
337 *handle = obj;
338 return r;
339}
340
Alexander Grafc15d9212016-03-04 01:09:59 +0100341/*
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200342 * Our event capabilities are very limited. Only a small limited
343 * number of events is allowed to coexist.
Alexander Grafc15d9212016-03-04 01:09:59 +0100344 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200345static struct efi_event efi_events[16];
Alexander Grafc15d9212016-03-04 01:09:59 +0100346
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347/*
348 * Create an event.
349 *
350 * This function is used inside U-Boot code to create an event.
351 *
352 * For the API function implementing the CreateEvent service see
353 * efi_create_event_ext.
354 *
355 * @type type of the event to create
356 * @notify_tpl task priority level of the event
357 * @notify_function notification function of the event
358 * @notify_context pointer passed to the notification function
359 * @event created event
360 * @return status code
361 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100362efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200363 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200364 struct efi_event *event,
365 void *context),
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200366 void *notify_context, struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100367{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200368 int i;
369
Jonathan Gray7758b212017-03-12 19:26:07 +1100370 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200371 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100372
373 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200374 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100375
376 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
377 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200378 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100379
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200380 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
381 if (efi_events[i].type)
382 continue;
383 efi_events[i].type = type;
384 efi_events[i].notify_tpl = notify_tpl;
385 efi_events[i].notify_function = notify_function;
386 efi_events[i].notify_context = notify_context;
387 /* Disable timers on bootup */
388 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200389 efi_events[i].is_queued = false;
390 efi_events[i].is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200391 *event = &efi_events[i];
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200392 return EFI_SUCCESS;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200393 }
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200394 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +0100395}
396
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200397/*
398 * Create an event.
399 *
400 * This function implements the CreateEvent service.
401 * See the Unified Extensible Firmware Interface (UEFI) specification
402 * for details.
403 *
404 * @type type of the event to create
405 * @notify_tpl task priority level of the event
406 * @notify_function notification function of the event
407 * @notify_context pointer passed to the notification function
408 * @event created event
409 * @return status code
410 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200411static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100412 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200413 void (EFIAPI *notify_function) (
414 struct efi_event *event,
415 void *context),
416 void *notify_context, struct efi_event **event)
417{
418 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
419 notify_context);
420 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
421 notify_context, event));
422}
423
424
Alexander Grafc15d9212016-03-04 01:09:59 +0100425/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200426 * Check if a timer event has occurred or a queued notification function should
427 * be called.
428 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100429 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200430 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100431 */
432void efi_timer_check(void)
433{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200434 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100435 u64 now = timer_get_us();
436
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200437 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200438 if (!efi_events[i].type)
439 continue;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200440 if (efi_events[i].is_queued)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200441 efi_signal_event(&efi_events[i]);
442 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200443 now < efi_events[i].trigger_next)
444 continue;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200445 switch (efi_events[i].trigger_type) {
446 case EFI_TIMER_RELATIVE:
447 efi_events[i].trigger_type = EFI_TIMER_STOP;
448 break;
449 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200450 efi_events[i].trigger_next +=
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200451 efi_events[i].trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200452 break;
453 default:
454 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200455 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200456 efi_events[i].is_signaled = true;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200457 efi_signal_event(&efi_events[i]);
Alexander Grafc15d9212016-03-04 01:09:59 +0100458 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100459 WATCHDOG_RESET();
460}
461
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200462/*
463 * Set the trigger time for a timer event or stop the event.
464 *
465 * This is the function for internal usage in U-Boot. For the API function
466 * implementing the SetTimer service see efi_set_timer_ext.
467 *
468 * @event event for which the timer is set
469 * @type type of the timer
470 * @trigger_time trigger period in multiples of 100ns
471 * @return status code
472 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200473efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200474 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100475{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200476 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100477
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200478 /*
479 * The parameter defines a multiple of 100ns.
480 * We use multiples of 1000ns. So divide by 10.
481 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200482 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100483
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200484 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
485 if (event != &efi_events[i])
486 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +0100487
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200488 if (!(event->type & EVT_TIMER))
489 break;
490 switch (type) {
491 case EFI_TIMER_STOP:
492 event->trigger_next = -1ULL;
493 break;
494 case EFI_TIMER_PERIODIC:
495 case EFI_TIMER_RELATIVE:
496 event->trigger_next =
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200497 timer_get_us() + trigger_time;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200498 break;
499 default:
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200500 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200501 }
502 event->trigger_type = type;
503 event->trigger_time = trigger_time;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200504 event->is_signaled = false;
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200505 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100506 }
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200507 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100508}
509
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200510/*
511 * Set the trigger time for a timer event or stop the event.
512 *
513 * This function implements the SetTimer service.
514 * See the Unified Extensible Firmware Interface (UEFI) specification
515 * for details.
516 *
517 * @event event for which the timer is set
518 * @type type of the timer
519 * @trigger_time trigger period in multiples of 100ns
520 * @return status code
521 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200522static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
523 enum efi_timer_delay type,
524 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200525{
526 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
527 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
528}
529
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200530/*
531 * Wait for events to be signaled.
532 *
533 * This function implements the WaitForEvent service.
534 * See the Unified Extensible Firmware Interface (UEFI) specification
535 * for details.
536 *
537 * @num_events number of events to be waited for
538 * @events events to be waited for
539 * @index index of the event that was signaled
540 * @return status code
541 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100542static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200543 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100544 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100545{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200546 int i, j;
Alexander Grafc15d9212016-03-04 01:09:59 +0100547
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100548 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100549
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200550 /* Check parameters */
551 if (!num_events || !event)
552 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200553 /* Check TPL */
554 if (efi_tpl != TPL_APPLICATION)
555 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200556 for (i = 0; i < num_events; ++i) {
557 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
558 if (event[i] == &efi_events[j])
559 goto known_event;
560 }
561 return EFI_EXIT(EFI_INVALID_PARAMETER);
562known_event:
563 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
564 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200565 if (!event[i]->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200566 efi_signal_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200567 }
568
569 /* Wait for signal */
570 for (;;) {
571 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200572 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200573 goto out;
574 }
575 /* Allow events to occur. */
576 efi_timer_check();
577 }
578
579out:
580 /*
581 * Reset the signal which is passed to the caller to allow periodic
582 * events to occur.
583 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200584 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200585 if (index)
586 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100587
588 return EFI_EXIT(EFI_SUCCESS);
589}
590
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200591/*
592 * Signal an EFI event.
593 *
594 * This function implements the SignalEvent service.
595 * See the Unified Extensible Firmware Interface (UEFI) specification
596 * for details.
597 *
598 * This functions sets the signaled state of the event and queues the
599 * notification function for execution.
600 *
601 * @event event to signal
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +0200602 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200603 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200604static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100605{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200606 int i;
607
Alexander Grafc15d9212016-03-04 01:09:59 +0100608 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200609 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
610 if (event != &efi_events[i])
611 continue;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200612 if (event->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200613 break;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200614 event->is_signaled = true;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200615 if (event->type & EVT_NOTIFY_SIGNAL)
616 efi_signal_event(event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200617 break;
618 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100619 return EFI_EXIT(EFI_SUCCESS);
620}
621
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200622/*
623 * Close an EFI event.
624 *
625 * This function implements the CloseEvent service.
626 * See the Unified Extensible Firmware Interface (UEFI) specification
627 * for details.
628 *
629 * @event event to close
630 * @return status code
631 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200632static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100633{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200634 int i;
635
Alexander Grafc15d9212016-03-04 01:09:59 +0100636 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200637 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
638 if (event == &efi_events[i]) {
639 event->type = 0;
640 event->trigger_next = -1ULL;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200641 event->is_queued = false;
642 event->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200643 return EFI_EXIT(EFI_SUCCESS);
644 }
645 }
646 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100647}
648
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200649/*
650 * Check if an event is signaled.
651 *
652 * This function implements the CheckEvent service.
653 * See the Unified Extensible Firmware Interface (UEFI) specification
654 * for details.
655 *
656 * If an event is not signaled yet the notification function is queued.
657 *
658 * @event event to check
659 * @return status code
660 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200661static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100662{
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200663 int i;
664
Alexander Grafc15d9212016-03-04 01:09:59 +0100665 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200666 efi_timer_check();
667 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
668 if (event != &efi_events[i])
669 continue;
670 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
671 break;
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200672 if (!event->is_signaled)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200673 efi_signal_event(event);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200674 if (event->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200675 return EFI_EXIT(EFI_SUCCESS);
676 return EFI_EXIT(EFI_NOT_READY);
677 }
678 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +0100679}
680
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200681/*
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200682 * Find the internal EFI object for a handle.
683 *
684 * @handle handle to find
685 * @return EFI object
686 */
687static struct efi_object *efi_search_obj(void *handle)
688{
689 struct list_head *lhandle;
690
691 list_for_each(lhandle, &efi_obj_list) {
692 struct efi_object *efiobj;
693
694 efiobj = list_entry(lhandle, struct efi_object, link);
695 if (efiobj->handle == handle)
696 return efiobj;
697 }
698
699 return NULL;
700}
701
702/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703 * Install protocol interface.
704 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100705 * This function implements the InstallProtocolInterface service.
706 * See the Unified Extensible Firmware Interface (UEFI) specification
707 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200708 *
709 * @handle handle on which the protocol shall be installed
710 * @protocol GUID of the protocol to be installed
711 * @protocol_interface_type type of the interface to be installed,
712 * always EFI_NATIVE_INTERFACE
713 * @protocol_interface interface of the protocol implementation
714 * @return status code
715 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100716static efi_status_t EFIAPI efi_install_protocol_interface(
717 void **handle, const efi_guid_t *protocol,
718 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100719{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200720 struct list_head *lhandle;
721 int i;
722 efi_status_t r;
723
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100724 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
725 protocol_interface);
726
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200727 if (!handle || !protocol ||
728 protocol_interface_type != EFI_NATIVE_INTERFACE) {
729 r = EFI_INVALID_PARAMETER;
730 goto out;
731 }
732
733 /* Create new handle if requested. */
734 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200735 r = efi_create_handle(handle);
736 if (r != EFI_SUCCESS)
737 goto out;
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200738 }
739 /* Find object. */
740 list_for_each(lhandle, &efi_obj_list) {
741 struct efi_object *efiobj;
742 efiobj = list_entry(lhandle, struct efi_object, link);
743
744 if (efiobj->handle != *handle)
745 continue;
746 /* Check if protocol is already installed on the handle. */
747 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
748 struct efi_handler *handler = &efiobj->protocols[i];
749
750 if (!handler->guid)
751 continue;
752 if (!guidcmp(handler->guid, protocol)) {
753 r = EFI_INVALID_PARAMETER;
754 goto out;
755 }
756 }
757 /* Install protocol in first empty slot. */
758 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
759 struct efi_handler *handler = &efiobj->protocols[i];
760
761 if (handler->guid)
762 continue;
763
764 handler->guid = protocol;
765 handler->protocol_interface = protocol_interface;
766 r = EFI_SUCCESS;
767 goto out;
768 }
769 r = EFI_OUT_OF_RESOURCES;
770 goto out;
771 }
772 r = EFI_INVALID_PARAMETER;
773out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100774 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100775}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200776
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200777/*
778 * Reinstall protocol interface.
779 *
780 * This function implements the ReinstallProtocolInterface service.
781 * See the Unified Extensible Firmware Interface (UEFI) specification
782 * for details.
783 *
784 * @handle handle on which the protocol shall be
785 * reinstalled
786 * @protocol GUID of the protocol to be installed
787 * @old_interface interface to be removed
788 * @new_interface interface to be installed
789 * @return status code
790 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100791static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200792 const efi_guid_t *protocol, void *old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100793 void *new_interface)
794{
Rob Clark238f88c2017-09-13 18:05:41 -0400795 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100796 new_interface);
797 return EFI_EXIT(EFI_ACCESS_DENIED);
798}
799
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200800/*
801 * Uninstall protocol interface.
802 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100803 * This function implements the UninstallProtocolInterface service.
804 * See the Unified Extensible Firmware Interface (UEFI) specification
805 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200806 *
807 * @handle handle from which the protocol shall be removed
808 * @protocol GUID of the protocol to be removed
809 * @protocol_interface interface to be removed
810 * @return status code
811 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100812static efi_status_t EFIAPI efi_uninstall_protocol_interface(
813 void *handle, const efi_guid_t *protocol,
814 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100815{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200816 struct list_head *lhandle;
817 int i;
818 efi_status_t r = EFI_NOT_FOUND;
819
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100820 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
821
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200822 if (!handle || !protocol) {
823 r = EFI_INVALID_PARAMETER;
824 goto out;
825 }
826
827 list_for_each(lhandle, &efi_obj_list) {
828 struct efi_object *efiobj;
829 efiobj = list_entry(lhandle, struct efi_object, link);
830
831 if (efiobj->handle != handle)
832 continue;
833
834 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
835 struct efi_handler *handler = &efiobj->protocols[i];
836 const efi_guid_t *hprotocol = handler->guid;
837
838 if (!hprotocol)
839 continue;
840 if (!guidcmp(hprotocol, protocol)) {
841 if (handler->protocol_interface) {
842 r = EFI_ACCESS_DENIED;
843 } else {
844 handler->guid = 0;
845 r = EFI_SUCCESS;
846 }
847 goto out;
848 }
849 }
850 }
851
852out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100853 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100854}
855
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200856/*
857 * Register an event for notification when a protocol is installed.
858 *
859 * This function implements the RegisterProtocolNotify service.
860 * See the Unified Extensible Firmware Interface (UEFI) specification
861 * for details.
862 *
863 * @protocol GUID of the protocol whose installation shall be
864 * notified
865 * @event event to be signaled upon installation of the protocol
866 * @registration key for retrieving the registration information
867 * @return status code
868 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200869static efi_status_t EFIAPI efi_register_protocol_notify(
870 const efi_guid_t *protocol,
871 struct efi_event *event,
872 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +0100873{
Rob Clark238f88c2017-09-13 18:05:41 -0400874 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +0100875 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
876}
877
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200878/*
879 * Determine if an EFI handle implements a protocol.
880 *
881 * See the documentation of the LocateHandle service in the UEFI specification.
882 *
883 * @search_type selection criterion
884 * @protocol GUID of the protocol
885 * @search_key registration key
886 * @efiobj handle
887 * @return 0 if the handle implements the protocol
888 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100889static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200890 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +0100891 struct efi_object *efiobj)
892{
893 int i;
894
895 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100896 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +0100897 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100898 case BY_REGISTER_NOTIFY:
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100899 /* RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +0100900 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100901 case BY_PROTOCOL:
Alexander Grafc15d9212016-03-04 01:09:59 +0100902 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
903 const efi_guid_t *guid = efiobj->protocols[i].guid;
904 if (guid && !guidcmp(guid, protocol))
905 return 0;
906 }
907 return -1;
908 }
909
910 return -1;
911}
912
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200913/*
914 * Locate handles implementing a protocol.
915 *
916 * This function is meant for U-Boot internal calls. For the API implementation
917 * of the LocateHandle service see efi_locate_handle_ext.
918 *
919 * @search_type selection criterion
920 * @protocol GUID of the protocol
921 * @search_key registration key
922 * @buffer_size size of the buffer to receive the handles in bytes
923 * @buffer buffer to receive the relevant handles
924 * @return status code
925 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200926static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100927 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200928 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100929 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100930{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100931 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100932 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +0100933
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100934 /* Check parameters */
935 switch (search_type) {
936 case ALL_HANDLES:
937 break;
938 case BY_REGISTER_NOTIFY:
939 if (!search_key)
940 return EFI_INVALID_PARAMETER;
941 /* RegisterProtocolNotify is not implemented yet */
942 return EFI_UNSUPPORTED;
943 case BY_PROTOCOL:
944 if (!protocol)
945 return EFI_INVALID_PARAMETER;
946 break;
947 default:
948 return EFI_INVALID_PARAMETER;
949 }
950
951 /*
952 * efi_locate_handle_buffer uses this function for
953 * the calculation of the necessary buffer size.
954 * So do not require a buffer for buffersize == 0.
955 */
956 if (!buffer_size || (*buffer_size && !buffer))
957 return EFI_INVALID_PARAMETER;
958
Alexander Grafc15d9212016-03-04 01:09:59 +0100959 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100960 list_for_each_entry(efiobj, &efi_obj_list, link) {
961 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafc15d9212016-03-04 01:09:59 +0100962 size += sizeof(void*);
Alexander Grafc15d9212016-03-04 01:09:59 +0100963 }
964
965 if (*buffer_size < size) {
966 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200967 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100968 }
969
Rob Clarkcdee3372017-08-06 14:10:07 -0400970 *buffer_size = size;
971 if (size == 0)
972 return EFI_NOT_FOUND;
973
Alexander Grafc15d9212016-03-04 01:09:59 +0100974 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100975 list_for_each_entry(efiobj, &efi_obj_list, link) {
976 if (!efi_search(search_type, protocol, search_key, efiobj))
977 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +0100978 }
979
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200980 return EFI_SUCCESS;
981}
982
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200983/*
984 * Locate handles implementing a protocol.
985 *
986 * This function implements the LocateHandle service.
987 * See the Unified Extensible Firmware Interface (UEFI) specification
988 * for details.
989 *
990 * @search_type selection criterion
991 * @protocol GUID of the protocol
992 * @search_key registration key
993 * @buffer_size size of the buffer to receive the handles in bytes
994 * @buffer buffer to receive the relevant handles
995 * @return 0 if the handle implements the protocol
996 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200997static efi_status_t EFIAPI efi_locate_handle_ext(
998 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200999 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001000 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001001{
Rob Clark238f88c2017-09-13 18:05:41 -04001002 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001003 buffer_size, buffer);
1004
1005 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1006 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001007}
1008
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001009/*
1010 * Get the device path and handle of an device implementing a protocol.
1011 *
1012 * This function implements the LocateDevicePath service.
1013 * See the Unified Extensible Firmware Interface (UEFI) specification
1014 * for details.
1015 *
1016 * @protocol GUID of the protocol
1017 * @device_path device path
1018 * @device handle of the device
1019 * @return status code
1020 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001021static efi_status_t EFIAPI efi_locate_device_path(
1022 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001023 struct efi_device_path **device_path,
1024 efi_handle_t *device)
1025{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001026 struct efi_object *efiobj;
1027
1028 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1029
1030 efiobj = efi_dp_find_obj(*device_path, device_path);
1031 if (!efiobj)
1032 return EFI_EXIT(EFI_NOT_FOUND);
1033
1034 *device = efiobj->handle;
1035
1036 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001037}
1038
Alexander Graffe3366f2017-07-26 13:41:04 +02001039/* Collapses configuration table entries, removing index i */
1040static void efi_remove_configuration_table(int i)
1041{
1042 struct efi_configuration_table *this = &efi_conf_table[i];
1043 struct efi_configuration_table *next = &efi_conf_table[i+1];
1044 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1045
1046 memmove(this, next, (ulong)end - (ulong)next);
1047 systab.nr_tables--;
1048}
1049
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001050/*
1051 * Adds, updates, or removes a configuration table.
1052 *
1053 * This function is used for internal calls. For the API implementation of the
1054 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1055 *
1056 * @guid GUID of the installed table
1057 * @table table to be installed
1058 * @return status code
1059 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001060efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001061{
1062 int i;
1063
Alexander Grafc15d9212016-03-04 01:09:59 +01001064 /* Check for guid override */
1065 for (i = 0; i < systab.nr_tables; i++) {
1066 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001067 if (table)
1068 efi_conf_table[i].table = table;
1069 else
1070 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +02001071 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001072 }
1073 }
1074
Alexander Graffe3366f2017-07-26 13:41:04 +02001075 if (!table)
1076 return EFI_NOT_FOUND;
1077
Alexander Grafc15d9212016-03-04 01:09:59 +01001078 /* No override, check for overflow */
1079 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001080 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001081
1082 /* Add a new entry */
1083 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1084 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001085 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001086
Alexander Grafc5c11632016-08-19 01:23:24 +02001087 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001088}
1089
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001090/*
1091 * Adds, updates, or removes a configuration table.
1092 *
1093 * This function implements the InstallConfigurationTable service.
1094 * See the Unified Extensible Firmware Interface (UEFI) specification
1095 * for details.
1096 *
1097 * @guid GUID of the installed table
1098 * @table table to be installed
1099 * @return status code
1100 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001101static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1102 void *table)
1103{
Rob Clark238f88c2017-09-13 18:05:41 -04001104 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001105 return EFI_EXIT(efi_install_configuration_table(guid, table));
1106}
1107
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001108/*
1109 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001110 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001111 *
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001112 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001113 * image
1114 * @obj internal object associated with the loaded image
1115 * @device_path device path of the loaded image
1116 * @file_path file path of the loaded image
Rob Clarkf8db9222017-09-13 18:05:33 -04001117 */
1118void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1119 struct efi_device_path *device_path,
1120 struct efi_device_path *file_path)
1121{
1122 obj->handle = info;
1123
1124 /*
1125 * When asking for the device path interface, return
1126 * bootefi_device_path
1127 */
1128 obj->protocols[0].guid = &efi_guid_device_path;
1129 obj->protocols[0].protocol_interface = device_path;
1130
1131 /*
1132 * When asking for the loaded_image interface, just
1133 * return handle which points to loaded_image_info
1134 */
1135 obj->protocols[1].guid = &efi_guid_loaded_image;
1136 obj->protocols[1].protocol_interface = info;
1137
1138 obj->protocols[2].guid = &efi_guid_console_control;
1139 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1140
1141 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1142 obj->protocols[3].protocol_interface =
1143 (void *)&efi_device_path_to_text;
1144
1145 info->file_path = file_path;
Heinrich Schuchardt8cb12542017-10-08 06:57:26 +02001146 if (device_path)
1147 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clarkf8db9222017-09-13 18:05:33 -04001148
1149 list_add_tail(&obj->link, &efi_obj_list);
1150}
1151
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001152/*
1153 * Load an image using a file path.
1154 *
1155 * @file_path the path of the image to load
1156 * @buffer buffer containing the loaded image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001157 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001158 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001159efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1160 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001161{
1162 struct efi_file_info *info = NULL;
1163 struct efi_file_handle *f;
1164 static efi_status_t ret;
1165 uint64_t bs;
1166
1167 f = efi_file_from_path(file_path);
1168 if (!f)
1169 return EFI_DEVICE_ERROR;
1170
1171 bs = 0;
1172 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1173 &bs, info));
1174 if (ret == EFI_BUFFER_TOO_SMALL) {
1175 info = malloc(bs);
1176 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1177 &bs, info));
1178 }
1179 if (ret != EFI_SUCCESS)
1180 goto error;
1181
1182 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1183 if (ret)
1184 goto error;
1185
1186 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1187
1188error:
1189 free(info);
1190 EFI_CALL(f->close(f));
1191
1192 if (ret != EFI_SUCCESS) {
1193 efi_free_pool(*buffer);
1194 *buffer = NULL;
1195 }
1196
1197 return ret;
1198}
1199
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001200/*
1201 * Load an EFI image into memory.
1202 *
1203 * This function implements the LoadImage service.
1204 * See the Unified Extensible Firmware Interface (UEFI) specification
1205 * for details.
1206 *
1207 * @boot_policy true for request originating from the boot manager
1208 * @parent_image the calles's image handle
1209 * @file_path the path of the image to load
1210 * @source_buffer memory location from which the image is installed
1211 * @source_size size of the memory area from which the image is
1212 * installed
1213 * @image_handle handle for the newly installed image
1214 * @return status code
1215 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001216static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1217 efi_handle_t parent_image,
1218 struct efi_device_path *file_path,
1219 void *source_buffer,
1220 unsigned long source_size,
1221 efi_handle_t *image_handle)
1222{
Alexander Grafc15d9212016-03-04 01:09:59 +01001223 struct efi_loaded_image *info;
1224 struct efi_object *obj;
1225
1226 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1227 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001228
1229 info = calloc(1, sizeof(*info));
1230 obj = calloc(1, sizeof(*obj));
1231
1232 if (!source_buffer) {
1233 struct efi_device_path *dp, *fp;
1234 efi_status_t ret;
1235
Rob Clarkc84c1102017-09-13 18:05:38 -04001236 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark857a1222017-09-13 18:05:35 -04001237 if (ret != EFI_SUCCESS) {
1238 free(info);
1239 free(obj);
1240 return EFI_EXIT(ret);
1241 }
1242
1243 /*
1244 * split file_path which contains both the device and
1245 * file parts:
1246 */
1247 efi_dp_split_file_path(file_path, &dp, &fp);
1248
1249 efi_setup_loaded_image(info, obj, dp, fp);
1250 } else {
1251 /* In this case, file_path is the "device" path, ie.
1252 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1253 */
1254 efi_setup_loaded_image(info, obj, file_path, NULL);
1255 }
1256
Alexander Grafc15d9212016-03-04 01:09:59 +01001257 info->reserved = efi_load_pe(source_buffer, info);
1258 if (!info->reserved) {
1259 free(info);
1260 free(obj);
1261 return EFI_EXIT(EFI_UNSUPPORTED);
1262 }
1263
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001264 info->system_table = &systab;
1265 info->parent_handle = parent_image;
Alexander Grafc15d9212016-03-04 01:09:59 +01001266 *image_handle = info;
Alexander Grafc15d9212016-03-04 01:09:59 +01001267
1268 return EFI_EXIT(EFI_SUCCESS);
1269}
1270
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001271/*
1272 * Call the entry point of an image.
1273 *
1274 * This function implements the StartImage service.
1275 * See the Unified Extensible Firmware Interface (UEFI) specification
1276 * for details.
1277 *
1278 * @image_handle handle of the image
1279 * @exit_data_size size of the buffer
1280 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001281 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001282 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001283static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1284 unsigned long *exit_data_size,
1285 s16 **exit_data)
1286{
1287 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1288 struct efi_loaded_image *info = image_handle;
1289
1290 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1291 entry = info->reserved;
1292
1293 efi_is_direct_boot = false;
1294
1295 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001296 if (setjmp(&info->exit_jmp)) {
1297 /* We returned from the child image */
1298 return EFI_EXIT(info->exit_status);
1299 }
1300
Rob Clarke7896c32017-07-27 08:04:19 -04001301 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -04001302 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +01001303 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -04001304 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -04001305 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +01001306
1307 /* Should usually never get here */
1308 return EFI_EXIT(EFI_SUCCESS);
1309}
1310
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001311/*
1312 * Leave an EFI application or driver.
1313 *
1314 * This function implements the Exit service.
1315 * See the Unified Extensible Firmware Interface (UEFI) specification
1316 * for details.
1317 *
1318 * @image_handle handle of the application or driver that is exiting
1319 * @exit_status status code
1320 * @exit_data_size size of the buffer in bytes
1321 * @exit_data buffer with data describing an error
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001322 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001323 */
Alexander Graf988c0662016-05-20 23:28:23 +02001324static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1325 efi_status_t exit_status, unsigned long exit_data_size,
1326 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001327{
Alexander Graf988c0662016-05-20 23:28:23 +02001328 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1329
Alexander Grafc15d9212016-03-04 01:09:59 +01001330 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1331 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001332
Alexander Graf87e787a2017-09-03 14:14:17 +02001333 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001334 __efi_exit_check();
1335
Alexander Graf87e787a2017-09-03 14:14:17 +02001336 /*
1337 * But longjmp out with the U-Boot gd, not the application's, as
1338 * the other end is a setjmp call inside EFI context.
1339 */
1340 efi_restore_gd();
1341
Alexander Graf988c0662016-05-20 23:28:23 +02001342 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001343 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001344
1345 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001346}
1347
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001348/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001349 * Unload an EFI image.
1350 *
1351 * This function implements the UnloadImage service.
1352 * See the Unified Extensible Firmware Interface (UEFI) specification
1353 * for details.
1354 *
1355 * @image_handle handle of the image to be unloaded
1356 * @return status code
1357 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001358static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1359{
1360 struct efi_object *efiobj;
1361
1362 EFI_ENTRY("%p", image_handle);
1363 efiobj = efi_search_obj(image_handle);
1364 if (efiobj)
1365 list_del(&efiobj->link);
1366
1367 return EFI_EXIT(EFI_SUCCESS);
1368}
1369
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001370/*
1371 * Fix up caches for EFI payloads if necessary.
1372 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001373static void efi_exit_caches(void)
1374{
1375#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1376 /*
1377 * Grub on 32bit ARM needs to have caches disabled before jumping into
1378 * a zImage, but does not know of all cache layers. Give it a hand.
1379 */
1380 if (efi_is_direct_boot)
1381 cleanup_before_linux();
1382#endif
1383}
1384
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001385/*
1386 * Stop boot services.
1387 *
1388 * This function implements the ExitBootServices service.
1389 * See the Unified Extensible Firmware Interface (UEFI) specification
1390 * for details.
1391 *
1392 * @image_handle handle of the loaded image
1393 * @map_key key of the memory map
1394 * @return status code
1395 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001396static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1397 unsigned long map_key)
1398{
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001399 int i;
1400
Alexander Grafc15d9212016-03-04 01:09:59 +01001401 EFI_ENTRY("%p, %ld", image_handle, map_key);
1402
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001403 /* Notify that ExitBootServices is invoked. */
1404 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1405 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1406 continue;
1407 efi_signal_event(&efi_events[i]);
1408 }
1409 /* Make sure that notification functions are not called anymore */
1410 efi_tpl = TPL_HIGH_LEVEL;
1411
Alexander Graf600af332017-10-19 23:23:50 +02001412 /* XXX Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001413
Alexander Graf2ebeb442016-11-17 01:02:57 +01001414 board_quiesce_devices();
1415
Alexander Grafc15d9212016-03-04 01:09:59 +01001416 /* Fix up caches for EFI payloads if necessary */
1417 efi_exit_caches();
1418
1419 /* This stops all lingering devices */
1420 bootm_disable_interrupts();
1421
1422 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001423 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001424 WATCHDOG_RESET();
1425
1426 return EFI_EXIT(EFI_SUCCESS);
1427}
1428
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001429/*
1430 * Get next value of the counter.
1431 *
1432 * This function implements the NextMonotonicCount service.
1433 * See the Unified Extensible Firmware Interface (UEFI) specification
1434 * for details.
1435 *
1436 * @count returned value of the counter
1437 * @return status code
1438 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001439static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1440{
1441 static uint64_t mono = 0;
1442 EFI_ENTRY("%p", count);
1443 *count = mono++;
1444 return EFI_EXIT(EFI_SUCCESS);
1445}
1446
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001447/*
1448 * Sleep.
1449 *
1450 * This function implements the Stall sercive.
1451 * See the Unified Extensible Firmware Interface (UEFI) specification
1452 * for details.
1453 *
1454 * @microseconds period to sleep in microseconds
1455 * @return status code
1456 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001457static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1458{
1459 EFI_ENTRY("%ld", microseconds);
1460 udelay(microseconds);
1461 return EFI_EXIT(EFI_SUCCESS);
1462}
1463
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001464/*
1465 * Reset the watchdog timer.
1466 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001467 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001468 * See the Unified Extensible Firmware Interface (UEFI) specification
1469 * for details.
1470 *
1471 * @timeout seconds before reset by watchdog
1472 * @watchdog_code code to be logged when resetting
1473 * @data_size size of buffer in bytes
1474 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001475 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001476 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001477static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1478 uint64_t watchdog_code,
1479 unsigned long data_size,
1480 uint16_t *watchdog_data)
1481{
1482 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1483 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001484 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001485}
1486
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001487/*
1488 * Connect a controller to a driver.
1489 *
1490 * This function implements the ConnectController service.
1491 * See the Unified Extensible Firmware Interface (UEFI) specification
1492 * for details.
1493 *
1494 * @controller_handle handle of the controller
1495 * @driver_image_handle handle of the driver
1496 * @remain_device_path device path of a child controller
1497 * @recursive true to connect all child controllers
1498 * @return status code
1499 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001500static efi_status_t EFIAPI efi_connect_controller(
1501 efi_handle_t controller_handle,
1502 efi_handle_t *driver_image_handle,
1503 struct efi_device_path *remain_device_path,
1504 bool recursive)
1505{
1506 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1507 remain_device_path, recursive);
1508 return EFI_EXIT(EFI_NOT_FOUND);
1509}
1510
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001511/*
1512 * Disconnect a controller from a driver.
1513 *
1514 * This function implements the DisconnectController service.
1515 * See the Unified Extensible Firmware Interface (UEFI) specification
1516 * for details.
1517 *
1518 * @controller_handle handle of the controller
1519 * @driver_image_handle handle of the driver
1520 * @child_handle handle of the child to destroy
1521 * @return status code
1522 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001523static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1524 void *driver_image_handle,
1525 void *child_handle)
1526{
1527 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1528 child_handle);
1529 return EFI_EXIT(EFI_INVALID_PARAMETER);
1530}
1531
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001532/*
1533 * Close a protocol.
1534 *
1535 * This function implements the CloseProtocol service.
1536 * See the Unified Extensible Firmware Interface (UEFI) specification
1537 * for details.
1538 *
1539 * @handle handle on which the protocol shall be closed
1540 * @protocol GUID of the protocol to close
1541 * @agent_handle handle of the driver
1542 * @controller_handle handle of the controller
1543 * @return status code
1544 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001545static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001546 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001547 void *agent_handle,
1548 void *controller_handle)
1549{
Rob Clark238f88c2017-09-13 18:05:41 -04001550 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001551 controller_handle);
1552 return EFI_EXIT(EFI_NOT_FOUND);
1553}
1554
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001555/*
1556 * Provide information about then open status of a protocol on a handle
1557 *
1558 * This function implements the OpenProtocolInformation service.
1559 * See the Unified Extensible Firmware Interface (UEFI) specification
1560 * for details.
1561 *
1562 * @handle handle for which the information shall be retrieved
1563 * @protocol GUID of the protocol
1564 * @entry_buffer buffer to receive the open protocol information
1565 * @entry_count number of entries available in the buffer
1566 * @return status code
1567 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001568static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001569 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001570 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001571 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001572{
Rob Clark238f88c2017-09-13 18:05:41 -04001573 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 entry_count);
1575 return EFI_EXIT(EFI_NOT_FOUND);
1576}
1577
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001578/*
1579 * Get protocols installed on a handle.
1580 *
1581 * This function implements the ProtocolsPerHandleService.
1582 * See the Unified Extensible Firmware Interface (UEFI) specification
1583 * for details.
1584 *
1585 * @handle handle for which the information is retrieved
1586 * @protocol_buffer buffer with protocol GUIDs
1587 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001588 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001589 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001590static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1591 efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001592 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001593{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001594 unsigned long buffer_size;
1595 struct efi_object *efiobj;
1596 unsigned long i, j;
1597 struct list_head *lhandle;
1598 efi_status_t r;
1599
Alexander Grafc15d9212016-03-04 01:09:59 +01001600 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1601 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001602
1603 if (!handle || !protocol_buffer || !protocol_buffer_count)
1604 return EFI_EXIT(EFI_INVALID_PARAMETER);
1605
1606 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04001607 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001608 list_for_each(lhandle, &efi_obj_list) {
1609 efiobj = list_entry(lhandle, struct efi_object, link);
1610
1611 if (efiobj->handle != handle)
1612 continue;
1613
1614 /* Count protocols */
1615 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1616 if (efiobj->protocols[i].guid)
1617 ++*protocol_buffer_count;
1618 }
1619 /* Copy guids */
1620 if (*protocol_buffer_count) {
1621 buffer_size = sizeof(efi_guid_t *) *
1622 *protocol_buffer_count;
1623 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1624 buffer_size,
1625 (void **)protocol_buffer);
1626 if (r != EFI_SUCCESS)
1627 return EFI_EXIT(r);
1628 j = 0;
1629 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1630 if (efiobj->protocols[i].guid) {
1631 (*protocol_buffer)[j] = (void *)
1632 efiobj->protocols[i].guid;
1633 ++j;
1634 }
1635 }
1636 }
1637 break;
1638 }
1639
1640 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001641}
1642
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001643/*
1644 * Locate handles implementing a protocol.
1645 *
1646 * This function implements the LocateHandleBuffer service.
1647 * See the Unified Extensible Firmware Interface (UEFI) specification
1648 * for details.
1649 *
1650 * @search_type selection criterion
1651 * @protocol GUID of the protocol
1652 * @search_key registration key
1653 * @no_handles number of returned handles
1654 * @buffer buffer with the returned handles
1655 * @return status code
1656 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001657static efi_status_t EFIAPI efi_locate_handle_buffer(
1658 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001659 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001660 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001661{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001662 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001663 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001664
Rob Clark238f88c2017-09-13 18:05:41 -04001665 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001666 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001667
1668 if (!no_handles || !buffer) {
1669 r = EFI_INVALID_PARAMETER;
1670 goto out;
1671 }
1672 *no_handles = 0;
1673 *buffer = NULL;
1674 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1675 *buffer);
1676 if (r != EFI_BUFFER_TOO_SMALL)
1677 goto out;
1678 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1679 (void **)buffer);
1680 if (r != EFI_SUCCESS)
1681 goto out;
1682 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1683 *buffer);
1684 if (r == EFI_SUCCESS)
1685 *no_handles = buffer_size / sizeof(void *);
1686out:
1687 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001688}
1689
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001690/*
1691 * Find an interface implementing a protocol.
1692 *
1693 * This function implements the LocateProtocol service.
1694 * See the Unified Extensible Firmware Interface (UEFI) specification
1695 * for details.
1696 *
1697 * @protocol GUID of the protocol
1698 * @registration registration key passed to the notification function
1699 * @protocol_interface interface implementing the protocol
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001700 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001701 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001702static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001703 void *registration,
1704 void **protocol_interface)
1705{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001706 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001707 int i;
1708
Rob Clark238f88c2017-09-13 18:05:41 -04001709 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001710
1711 if (!protocol || !protocol_interface)
1712 return EFI_EXIT(EFI_INVALID_PARAMETER);
1713
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001714 EFI_PRINT_GUID("protocol", protocol);
1715
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001716 list_for_each(lhandle, &efi_obj_list) {
1717 struct efi_object *efiobj;
1718
1719 efiobj = list_entry(lhandle, struct efi_object, link);
1720 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1721 struct efi_handler *handler = &efiobj->protocols[i];
1722
1723 if (!handler->guid)
1724 continue;
1725 if (!guidcmp(handler->guid, protocol)) {
1726 *protocol_interface =
1727 handler->protocol_interface;
1728 return EFI_EXIT(EFI_SUCCESS);
1729 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001730 }
1731 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001732 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001733
1734 return EFI_EXIT(EFI_NOT_FOUND);
1735}
1736
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001737/*
1738 * Install multiple protocol interfaces.
1739 *
1740 * This function implements the MultipleProtocolInterfaces service.
1741 * See the Unified Extensible Firmware Interface (UEFI) specification
1742 * for details.
1743 *
1744 * @handle handle on which the protocol interfaces shall be installed
1745 * @... NULL terminated argument list with pairs of protocol GUIDS and
1746 * interfaces
1747 * @return status code
1748 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001749static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1750 void **handle, ...)
1751{
1752 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001753
1754 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001755 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001756 void *protocol_interface;
1757 efi_status_t r = EFI_SUCCESS;
1758 int i = 0;
1759
1760 if (!handle)
1761 return EFI_EXIT(EFI_INVALID_PARAMETER);
1762
1763 va_start(argptr, handle);
1764 for (;;) {
1765 protocol = va_arg(argptr, efi_guid_t*);
1766 if (!protocol)
1767 break;
1768 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001769 r = EFI_CALL(efi_install_protocol_interface(
1770 handle, protocol,
1771 EFI_NATIVE_INTERFACE,
1772 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001773 if (r != EFI_SUCCESS)
1774 break;
1775 i++;
1776 }
1777 va_end(argptr);
1778 if (r == EFI_SUCCESS)
1779 return EFI_EXIT(r);
1780
1781 /* If an error occured undo all changes. */
1782 va_start(argptr, handle);
1783 for (; i; --i) {
1784 protocol = va_arg(argptr, efi_guid_t*);
1785 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001786 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1787 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001788 }
1789 va_end(argptr);
1790
1791 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001792}
1793
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001794/*
1795 * Uninstall multiple protocol interfaces.
1796 *
1797 * This function implements the UninstallMultipleProtocolInterfaces service.
1798 * See the Unified Extensible Firmware Interface (UEFI) specification
1799 * for details.
1800 *
1801 * @handle handle from which the protocol interfaces shall be removed
1802 * @... NULL terminated argument list with pairs of protocol GUIDS and
1803 * interfaces
1804 * @return status code
1805 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001806static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1807 void *handle, ...)
1808{
1809 EFI_ENTRY("%p", handle);
1810 return EFI_EXIT(EFI_INVALID_PARAMETER);
1811}
1812
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001813/*
1814 * Calculate cyclic redundancy code.
1815 *
1816 * This function implements the CalculateCrc32 service.
1817 * See the Unified Extensible Firmware Interface (UEFI) specification
1818 * for details.
1819 *
1820 * @data buffer with data
1821 * @data_size size of buffer in bytes
1822 * @crc32_p cyclic redundancy code
1823 * @return status code
1824 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001825static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1826 unsigned long data_size,
1827 uint32_t *crc32_p)
1828{
1829 EFI_ENTRY("%p, %ld", data, data_size);
1830 *crc32_p = crc32(0, data, data_size);
1831 return EFI_EXIT(EFI_SUCCESS);
1832}
1833
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001834/*
1835 * Copy memory.
1836 *
1837 * This function implements the CopyMem service.
1838 * See the Unified Extensible Firmware Interface (UEFI) specification
1839 * for details.
1840 *
1841 * @destination destination of the copy operation
1842 * @source source of the copy operation
1843 * @length number of bytes to copy
1844 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001845static void EFIAPI efi_copy_mem(void *destination, const void *source,
1846 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01001847{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001848 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01001849 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001850 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001851}
1852
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001853/*
1854 * Fill memory with a byte value.
1855 *
1856 * This function implements the SetMem service.
1857 * See the Unified Extensible Firmware Interface (UEFI) specification
1858 * for details.
1859 *
1860 * @buffer buffer to fill
1861 * @size size of buffer in bytes
1862 * @value byte to copy to the buffer
1863 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001864static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01001865{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001866 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01001867 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001868 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001869}
1870
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001871/*
1872 * Open protocol interface on a handle.
1873 *
1874 * This function implements the OpenProtocol interface.
1875 * See the Unified Extensible Firmware Interface (UEFI) specification
1876 * for details.
1877 *
1878 * @handle handle on which the protocol shall be opened
1879 * @protocol GUID of the protocol
1880 * @protocol_interface interface implementing the protocol
1881 * @agent_handle handle of the driver
1882 * @controller_handle handle of the controller
1883 * @attributes attributes indicating how to open the protocol
1884 * @return status code
1885 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001886static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001887 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001888 void **protocol_interface, void *agent_handle,
1889 void *controller_handle, uint32_t attributes)
1890{
1891 struct list_head *lhandle;
1892 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001893 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001894
Rob Clark238f88c2017-09-13 18:05:41 -04001895 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001896 protocol_interface, agent_handle, controller_handle,
1897 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001898
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001899 if (!handle || !protocol ||
1900 (!protocol_interface && attributes !=
1901 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001902 goto out;
1903 }
1904
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001905 EFI_PRINT_GUID("protocol", protocol);
1906
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001907 switch (attributes) {
1908 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1909 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1910 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1911 break;
1912 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1913 if (controller_handle == handle)
1914 goto out;
1915 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1916 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1917 if (controller_handle == NULL)
1918 goto out;
1919 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1920 if (agent_handle == NULL)
1921 goto out;
1922 break;
1923 default:
1924 goto out;
1925 }
1926
Alexander Grafc15d9212016-03-04 01:09:59 +01001927 list_for_each(lhandle, &efi_obj_list) {
1928 struct efi_object *efiobj;
1929 efiobj = list_entry(lhandle, struct efi_object, link);
1930
1931 if (efiobj->handle != handle)
1932 continue;
1933
1934 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1935 struct efi_handler *handler = &efiobj->protocols[i];
1936 const efi_guid_t *hprotocol = handler->guid;
1937 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001938 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001939 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001940 if (attributes !=
1941 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1942 *protocol_interface =
1943 handler->protocol_interface;
1944 }
1945 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001946 goto out;
1947 }
1948 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001949 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001950 }
1951
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001952unsupported:
1953 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01001954out:
1955 return EFI_EXIT(r);
1956}
1957
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001958/*
1959 * Get interface of a protocol on a handle.
1960 *
1961 * This function implements the HandleProtocol service.
1962 * See the Unified Extensible Firmware Interface (UEFI) specification
1963 * for details.
1964 *
1965 * @handle handle on which the protocol shall be opened
1966 * @protocol GUID of the protocol
1967 * @protocol_interface interface implementing the protocol
1968 * @return status code
1969 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001970static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001971 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001972 void **protocol_interface)
1973{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02001974 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1975 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01001976}
1977
1978static const struct efi_boot_services efi_boot_services = {
1979 .hdr = {
1980 .headersize = sizeof(struct efi_table_hdr),
1981 },
1982 .raise_tpl = efi_raise_tpl,
1983 .restore_tpl = efi_restore_tpl,
1984 .allocate_pages = efi_allocate_pages_ext,
1985 .free_pages = efi_free_pages_ext,
1986 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02001987 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02001988 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02001989 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02001990 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001991 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001992 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01001993 .close_event = efi_close_event,
1994 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001995 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01001996 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001997 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01001998 .handle_protocol = efi_handle_protocol,
1999 .reserved = NULL,
2000 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02002001 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002002 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02002003 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002004 .load_image = efi_load_image,
2005 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02002006 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01002007 .unload_image = efi_unload_image,
2008 .exit_boot_services = efi_exit_boot_services,
2009 .get_next_monotonic_count = efi_get_next_monotonic_count,
2010 .stall = efi_stall,
2011 .set_watchdog_timer = efi_set_watchdog_timer,
2012 .connect_controller = efi_connect_controller,
2013 .disconnect_controller = efi_disconnect_controller,
2014 .open_protocol = efi_open_protocol,
2015 .close_protocol = efi_close_protocol,
2016 .open_protocol_information = efi_open_protocol_information,
2017 .protocols_per_handle = efi_protocols_per_handle,
2018 .locate_handle_buffer = efi_locate_handle_buffer,
2019 .locate_protocol = efi_locate_protocol,
2020 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2021 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2022 .calculate_crc32 = efi_calculate_crc32,
2023 .copy_mem = efi_copy_mem,
2024 .set_mem = efi_set_mem,
2025};
2026
2027
Alexander Graf393dd912016-10-14 13:45:30 +02002028static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01002029 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2030
Alexander Graf393dd912016-10-14 13:45:30 +02002031struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01002032 .hdr = {
2033 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2034 .revision = 0x20005, /* 2.5 */
2035 .headersize = sizeof(struct efi_table_hdr),
2036 },
2037 .fw_vendor = (long)firmware_vendor,
2038 .con_in = (void*)&efi_con_in,
2039 .con_out = (void*)&efi_con_out,
2040 .std_err = (void*)&efi_con_out,
2041 .runtime = (void*)&efi_runtime_services,
2042 .boottime = (void*)&efi_boot_services,
2043 .nr_tables = 0,
2044 .tables = (void*)efi_conf_table,
2045};