blob: 06340fd673762f5cff92e5c5fa9409f7116bde80 [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 Schuchardt274cc872017-11-06 21:17:50 +0100693struct efi_object *efi_search_obj(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 Schuchardta40db6e2017-09-21 18:30:11 +0200706 * Install protocol interface.
707 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100708 * This function implements the InstallProtocolInterface service.
709 * See the Unified Extensible Firmware Interface (UEFI) specification
710 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200711 *
712 * @handle handle on which the protocol shall be installed
713 * @protocol GUID of the protocol to be installed
714 * @protocol_interface_type type of the interface to be installed,
715 * always EFI_NATIVE_INTERFACE
716 * @protocol_interface interface of the protocol implementation
717 * @return status code
718 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100719static efi_status_t EFIAPI efi_install_protocol_interface(
720 void **handle, const efi_guid_t *protocol,
721 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100722{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200723 struct list_head *lhandle;
724 int i;
725 efi_status_t r;
726
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100727 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
728 protocol_interface);
729
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200730 if (!handle || !protocol ||
731 protocol_interface_type != EFI_NATIVE_INTERFACE) {
732 r = EFI_INVALID_PARAMETER;
733 goto out;
734 }
735
736 /* Create new handle if requested. */
737 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200738 r = efi_create_handle(handle);
739 if (r != EFI_SUCCESS)
740 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +0200741 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
742 *handle);
743 } else {
744 debug("%sEFI: handle %p\n", indent_string(nesting_level),
745 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200746 }
747 /* Find object. */
748 list_for_each(lhandle, &efi_obj_list) {
749 struct efi_object *efiobj;
750 efiobj = list_entry(lhandle, struct efi_object, link);
751
752 if (efiobj->handle != *handle)
753 continue;
754 /* Check if protocol is already installed on the handle. */
755 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
756 struct efi_handler *handler = &efiobj->protocols[i];
757
758 if (!handler->guid)
759 continue;
760 if (!guidcmp(handler->guid, protocol)) {
761 r = EFI_INVALID_PARAMETER;
762 goto out;
763 }
764 }
765 /* Install protocol in first empty slot. */
766 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
767 struct efi_handler *handler = &efiobj->protocols[i];
768
769 if (handler->guid)
770 continue;
771
772 handler->guid = protocol;
773 handler->protocol_interface = protocol_interface;
774 r = EFI_SUCCESS;
775 goto out;
776 }
777 r = EFI_OUT_OF_RESOURCES;
778 goto out;
779 }
780 r = EFI_INVALID_PARAMETER;
781out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +0100782 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100783}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +0200784
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200785/*
786 * Reinstall protocol interface.
787 *
788 * This function implements the ReinstallProtocolInterface service.
789 * See the Unified Extensible Firmware Interface (UEFI) specification
790 * for details.
791 *
792 * @handle handle on which the protocol shall be
793 * reinstalled
794 * @protocol GUID of the protocol to be installed
795 * @old_interface interface to be removed
796 * @new_interface interface to be installed
797 * @return status code
798 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100799static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200800 const efi_guid_t *protocol, void *old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100801 void *new_interface)
802{
Rob Clark238f88c2017-09-13 18:05:41 -0400803 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +0100804 new_interface);
805 return EFI_EXIT(EFI_ACCESS_DENIED);
806}
807
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200808/*
809 * Uninstall protocol interface.
810 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100811 * This function implements the UninstallProtocolInterface service.
812 * See the Unified Extensible Firmware Interface (UEFI) specification
813 * for details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200814 *
815 * @handle handle from which the protocol shall be removed
816 * @protocol GUID of the protocol to be removed
817 * @protocol_interface interface to be removed
818 * @return status code
819 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100820static efi_status_t EFIAPI efi_uninstall_protocol_interface(
821 void *handle, const efi_guid_t *protocol,
822 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +0100823{
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200824 struct list_head *lhandle;
825 int i;
826 efi_status_t r = EFI_NOT_FOUND;
827
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100828 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
829
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +0200830 if (!handle || !protocol) {
831 r = EFI_INVALID_PARAMETER;
832 goto out;
833 }
834
835 list_for_each(lhandle, &efi_obj_list) {
836 struct efi_object *efiobj;
837 efiobj = list_entry(lhandle, struct efi_object, link);
838
839 if (efiobj->handle != handle)
840 continue;
841
842 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
843 struct efi_handler *handler = &efiobj->protocols[i];
844 const efi_guid_t *hprotocol = handler->guid;
845
846 if (!hprotocol)
847 continue;
848 if (!guidcmp(hprotocol, protocol)) {
849 if (handler->protocol_interface) {
850 r = EFI_ACCESS_DENIED;
851 } else {
852 handler->guid = 0;
853 r = EFI_SUCCESS;
854 }
855 goto out;
856 }
857 }
858 }
859
860out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +0100861 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100862}
863
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200864/*
865 * Register an event for notification when a protocol is installed.
866 *
867 * This function implements the RegisterProtocolNotify service.
868 * See the Unified Extensible Firmware Interface (UEFI) specification
869 * for details.
870 *
871 * @protocol GUID of the protocol whose installation shall be
872 * notified
873 * @event event to be signaled upon installation of the protocol
874 * @registration key for retrieving the registration information
875 * @return status code
876 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200877static efi_status_t EFIAPI efi_register_protocol_notify(
878 const efi_guid_t *protocol,
879 struct efi_event *event,
880 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +0100881{
Rob Clark238f88c2017-09-13 18:05:41 -0400882 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +0100883 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
884}
885
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886/*
887 * Determine if an EFI handle implements a protocol.
888 *
889 * See the documentation of the LocateHandle service in the UEFI specification.
890 *
891 * @search_type selection criterion
892 * @protocol GUID of the protocol
893 * @search_key registration key
894 * @efiobj handle
895 * @return 0 if the handle implements the protocol
896 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100897static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200898 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +0100899 struct efi_object *efiobj)
900{
901 int i;
902
903 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100904 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +0100905 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100906 case BY_REGISTER_NOTIFY:
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100907 /* RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +0100908 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +0100909 case BY_PROTOCOL:
Alexander Grafc15d9212016-03-04 01:09:59 +0100910 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
911 const efi_guid_t *guid = efiobj->protocols[i].guid;
912 if (guid && !guidcmp(guid, protocol))
913 return 0;
914 }
915 return -1;
916 }
917
918 return -1;
919}
920
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200921/*
922 * Locate handles implementing a protocol.
923 *
924 * This function is meant for U-Boot internal calls. For the API implementation
925 * of the LocateHandle service see efi_locate_handle_ext.
926 *
927 * @search_type selection criterion
928 * @protocol GUID of the protocol
929 * @search_key registration key
930 * @buffer_size size of the buffer to receive the handles in bytes
931 * @buffer buffer to receive the relevant handles
932 * @return status code
933 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +0200934static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +0100935 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +0200936 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100937 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100938{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100939 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100940 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +0100941
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100942 /* Check parameters */
943 switch (search_type) {
944 case ALL_HANDLES:
945 break;
946 case BY_REGISTER_NOTIFY:
947 if (!search_key)
948 return EFI_INVALID_PARAMETER;
949 /* RegisterProtocolNotify is not implemented yet */
950 return EFI_UNSUPPORTED;
951 case BY_PROTOCOL:
952 if (!protocol)
953 return EFI_INVALID_PARAMETER;
954 break;
955 default:
956 return EFI_INVALID_PARAMETER;
957 }
958
959 /*
960 * efi_locate_handle_buffer uses this function for
961 * the calculation of the necessary buffer size.
962 * So do not require a buffer for buffersize == 0.
963 */
964 if (!buffer_size || (*buffer_size && !buffer))
965 return EFI_INVALID_PARAMETER;
966
Alexander Grafc15d9212016-03-04 01:09:59 +0100967 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100968 list_for_each_entry(efiobj, &efi_obj_list, link) {
969 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafc15d9212016-03-04 01:09:59 +0100970 size += sizeof(void*);
Alexander Grafc15d9212016-03-04 01:09:59 +0100971 }
972
973 if (*buffer_size < size) {
974 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200975 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +0100976 }
977
Rob Clarkcdee3372017-08-06 14:10:07 -0400978 *buffer_size = size;
979 if (size == 0)
980 return EFI_NOT_FOUND;
981
Alexander Grafc15d9212016-03-04 01:09:59 +0100982 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +0100983 list_for_each_entry(efiobj, &efi_obj_list, link) {
984 if (!efi_search(search_type, protocol, search_key, efiobj))
985 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +0100986 }
987
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +0200988 return EFI_SUCCESS;
989}
990
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200991/*
992 * Locate handles implementing a protocol.
993 *
994 * This function implements the LocateHandle service.
995 * See the Unified Extensible Firmware Interface (UEFI) specification
996 * for details.
997 *
998 * @search_type selection criterion
999 * @protocol GUID of the protocol
1000 * @search_key registration key
1001 * @buffer_size size of the buffer to receive the handles in bytes
1002 * @buffer buffer to receive the relevant handles
1003 * @return 0 if the handle implements the protocol
1004 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001005static efi_status_t EFIAPI efi_locate_handle_ext(
1006 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001007 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001008 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001009{
Rob Clark238f88c2017-09-13 18:05:41 -04001010 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001011 buffer_size, buffer);
1012
1013 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1014 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001015}
1016
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001017/*
1018 * Get the device path and handle of an device implementing a protocol.
1019 *
1020 * This function implements the LocateDevicePath service.
1021 * See the Unified Extensible Firmware Interface (UEFI) specification
1022 * for details.
1023 *
1024 * @protocol GUID of the protocol
1025 * @device_path device path
1026 * @device handle of the device
1027 * @return status code
1028 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001029static efi_status_t EFIAPI efi_locate_device_path(
1030 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001031 struct efi_device_path **device_path,
1032 efi_handle_t *device)
1033{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001034 struct efi_object *efiobj;
1035
1036 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1037
1038 efiobj = efi_dp_find_obj(*device_path, device_path);
1039 if (!efiobj)
1040 return EFI_EXIT(EFI_NOT_FOUND);
1041
1042 *device = efiobj->handle;
1043
1044 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001045}
1046
Alexander Graffe3366f2017-07-26 13:41:04 +02001047/* Collapses configuration table entries, removing index i */
1048static void efi_remove_configuration_table(int i)
1049{
1050 struct efi_configuration_table *this = &efi_conf_table[i];
1051 struct efi_configuration_table *next = &efi_conf_table[i+1];
1052 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1053
1054 memmove(this, next, (ulong)end - (ulong)next);
1055 systab.nr_tables--;
1056}
1057
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001058/*
1059 * Adds, updates, or removes a configuration table.
1060 *
1061 * This function is used for internal calls. For the API implementation of the
1062 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1063 *
1064 * @guid GUID of the installed table
1065 * @table table to be installed
1066 * @return status code
1067 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001068efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001069{
1070 int i;
1071
Alexander Grafc15d9212016-03-04 01:09:59 +01001072 /* Check for guid override */
1073 for (i = 0; i < systab.nr_tables; i++) {
1074 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001075 if (table)
1076 efi_conf_table[i].table = table;
1077 else
1078 efi_remove_configuration_table(i);
Alexander Grafc5c11632016-08-19 01:23:24 +02001079 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001080 }
1081 }
1082
Alexander Graffe3366f2017-07-26 13:41:04 +02001083 if (!table)
1084 return EFI_NOT_FOUND;
1085
Alexander Grafc15d9212016-03-04 01:09:59 +01001086 /* No override, check for overflow */
1087 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001088 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001089
1090 /* Add a new entry */
1091 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1092 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001093 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001094
Alexander Grafc5c11632016-08-19 01:23:24 +02001095 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001096}
1097
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001098/*
1099 * Adds, updates, or removes a configuration table.
1100 *
1101 * This function implements the InstallConfigurationTable service.
1102 * See the Unified Extensible Firmware Interface (UEFI) specification
1103 * for details.
1104 *
1105 * @guid GUID of the installed table
1106 * @table table to be installed
1107 * @return status code
1108 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001109static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1110 void *table)
1111{
Rob Clark238f88c2017-09-13 18:05:41 -04001112 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001113 return EFI_EXIT(efi_install_configuration_table(guid, table));
1114}
1115
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001116/*
1117 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001118 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001119 *
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001120 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001121 * image
1122 * @obj internal object associated with the loaded image
1123 * @device_path device path of the loaded image
1124 * @file_path file path of the loaded image
Rob Clarkf8db9222017-09-13 18:05:33 -04001125 */
1126void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1127 struct efi_device_path *device_path,
1128 struct efi_device_path *file_path)
1129{
1130 obj->handle = info;
1131
1132 /*
1133 * When asking for the device path interface, return
1134 * bootefi_device_path
1135 */
1136 obj->protocols[0].guid = &efi_guid_device_path;
1137 obj->protocols[0].protocol_interface = device_path;
1138
1139 /*
1140 * When asking for the loaded_image interface, just
1141 * return handle which points to loaded_image_info
1142 */
1143 obj->protocols[1].guid = &efi_guid_loaded_image;
1144 obj->protocols[1].protocol_interface = info;
1145
1146 obj->protocols[2].guid = &efi_guid_console_control;
1147 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1148
1149 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1150 obj->protocols[3].protocol_interface =
1151 (void *)&efi_device_path_to_text;
1152
1153 info->file_path = file_path;
Heinrich Schuchardt8cb12542017-10-08 06:57:26 +02001154 if (device_path)
1155 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clarkf8db9222017-09-13 18:05:33 -04001156
1157 list_add_tail(&obj->link, &efi_obj_list);
1158}
1159
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001160/*
1161 * Load an image using a file path.
1162 *
1163 * @file_path the path of the image to load
1164 * @buffer buffer containing the loaded image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001165 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001166 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001167efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1168 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001169{
1170 struct efi_file_info *info = NULL;
1171 struct efi_file_handle *f;
1172 static efi_status_t ret;
1173 uint64_t bs;
1174
1175 f = efi_file_from_path(file_path);
1176 if (!f)
1177 return EFI_DEVICE_ERROR;
1178
1179 bs = 0;
1180 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1181 &bs, info));
1182 if (ret == EFI_BUFFER_TOO_SMALL) {
1183 info = malloc(bs);
1184 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1185 &bs, info));
1186 }
1187 if (ret != EFI_SUCCESS)
1188 goto error;
1189
1190 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1191 if (ret)
1192 goto error;
1193
1194 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1195
1196error:
1197 free(info);
1198 EFI_CALL(f->close(f));
1199
1200 if (ret != EFI_SUCCESS) {
1201 efi_free_pool(*buffer);
1202 *buffer = NULL;
1203 }
1204
1205 return ret;
1206}
1207
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001208/*
1209 * Load an EFI image into memory.
1210 *
1211 * This function implements the LoadImage service.
1212 * See the Unified Extensible Firmware Interface (UEFI) specification
1213 * for details.
1214 *
1215 * @boot_policy true for request originating from the boot manager
1216 * @parent_image the calles's image handle
1217 * @file_path the path of the image to load
1218 * @source_buffer memory location from which the image is installed
1219 * @source_size size of the memory area from which the image is
1220 * installed
1221 * @image_handle handle for the newly installed image
1222 * @return status code
1223 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001224static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1225 efi_handle_t parent_image,
1226 struct efi_device_path *file_path,
1227 void *source_buffer,
1228 unsigned long source_size,
1229 efi_handle_t *image_handle)
1230{
Alexander Grafc15d9212016-03-04 01:09:59 +01001231 struct efi_loaded_image *info;
1232 struct efi_object *obj;
1233
1234 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1235 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001236
1237 info = calloc(1, sizeof(*info));
1238 obj = calloc(1, sizeof(*obj));
1239
1240 if (!source_buffer) {
1241 struct efi_device_path *dp, *fp;
1242 efi_status_t ret;
1243
Rob Clarkc84c1102017-09-13 18:05:38 -04001244 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark857a1222017-09-13 18:05:35 -04001245 if (ret != EFI_SUCCESS) {
1246 free(info);
1247 free(obj);
1248 return EFI_EXIT(ret);
1249 }
1250
1251 /*
1252 * split file_path which contains both the device and
1253 * file parts:
1254 */
1255 efi_dp_split_file_path(file_path, &dp, &fp);
1256
1257 efi_setup_loaded_image(info, obj, dp, fp);
1258 } else {
1259 /* In this case, file_path is the "device" path, ie.
1260 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1261 */
1262 efi_setup_loaded_image(info, obj, file_path, NULL);
1263 }
1264
Alexander Grafc15d9212016-03-04 01:09:59 +01001265 info->reserved = efi_load_pe(source_buffer, info);
1266 if (!info->reserved) {
1267 free(info);
1268 free(obj);
1269 return EFI_EXIT(EFI_UNSUPPORTED);
1270 }
1271
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001272 info->system_table = &systab;
1273 info->parent_handle = parent_image;
Alexander Grafc15d9212016-03-04 01:09:59 +01001274 *image_handle = info;
Alexander Grafc15d9212016-03-04 01:09:59 +01001275
1276 return EFI_EXIT(EFI_SUCCESS);
1277}
1278
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001279/*
1280 * Call the entry point of an image.
1281 *
1282 * This function implements the StartImage service.
1283 * See the Unified Extensible Firmware Interface (UEFI) specification
1284 * for details.
1285 *
1286 * @image_handle handle of the image
1287 * @exit_data_size size of the buffer
1288 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001289 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001290 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001291static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1292 unsigned long *exit_data_size,
1293 s16 **exit_data)
1294{
1295 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1296 struct efi_loaded_image *info = image_handle;
1297
1298 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1299 entry = info->reserved;
1300
1301 efi_is_direct_boot = false;
1302
1303 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001304 if (setjmp(&info->exit_jmp)) {
1305 /* We returned from the child image */
1306 return EFI_EXIT(info->exit_status);
1307 }
1308
Rob Clarke7896c32017-07-27 08:04:19 -04001309 __efi_nesting_dec();
Rob Clark86789d52017-07-27 08:04:18 -04001310 __efi_exit_check();
Alexander Grafc15d9212016-03-04 01:09:59 +01001311 entry(image_handle, &systab);
Rob Clark86789d52017-07-27 08:04:18 -04001312 __efi_entry_check();
Rob Clarke7896c32017-07-27 08:04:19 -04001313 __efi_nesting_inc();
Alexander Grafc15d9212016-03-04 01:09:59 +01001314
1315 /* Should usually never get here */
1316 return EFI_EXIT(EFI_SUCCESS);
1317}
1318
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001319/*
1320 * Leave an EFI application or driver.
1321 *
1322 * This function implements the Exit service.
1323 * See the Unified Extensible Firmware Interface (UEFI) specification
1324 * for details.
1325 *
1326 * @image_handle handle of the application or driver that is exiting
1327 * @exit_status status code
1328 * @exit_data_size size of the buffer in bytes
1329 * @exit_data buffer with data describing an error
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001330 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001331 */
Alexander Graf988c0662016-05-20 23:28:23 +02001332static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1333 efi_status_t exit_status, unsigned long exit_data_size,
1334 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001335{
Alexander Graf988c0662016-05-20 23:28:23 +02001336 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1337
Alexander Grafc15d9212016-03-04 01:09:59 +01001338 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1339 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001340
Alexander Graf87e787a2017-09-03 14:14:17 +02001341 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001342 __efi_exit_check();
1343
Alexander Graf87e787a2017-09-03 14:14:17 +02001344 /*
1345 * But longjmp out with the U-Boot gd, not the application's, as
1346 * the other end is a setjmp call inside EFI context.
1347 */
1348 efi_restore_gd();
1349
Alexander Graf988c0662016-05-20 23:28:23 +02001350 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001351 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001352
1353 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001354}
1355
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001356/*
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001357 * Unload an EFI image.
1358 *
1359 * This function implements the UnloadImage service.
1360 * See the Unified Extensible Firmware Interface (UEFI) specification
1361 * for details.
1362 *
1363 * @image_handle handle of the image to be unloaded
1364 * @return status code
1365 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001366static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1367{
1368 struct efi_object *efiobj;
1369
1370 EFI_ENTRY("%p", image_handle);
1371 efiobj = efi_search_obj(image_handle);
1372 if (efiobj)
1373 list_del(&efiobj->link);
1374
1375 return EFI_EXIT(EFI_SUCCESS);
1376}
1377
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001378/*
1379 * Fix up caches for EFI payloads if necessary.
1380 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001381static void efi_exit_caches(void)
1382{
1383#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1384 /*
1385 * Grub on 32bit ARM needs to have caches disabled before jumping into
1386 * a zImage, but does not know of all cache layers. Give it a hand.
1387 */
1388 if (efi_is_direct_boot)
1389 cleanup_before_linux();
1390#endif
1391}
1392
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001393/*
1394 * Stop boot services.
1395 *
1396 * This function implements the ExitBootServices service.
1397 * See the Unified Extensible Firmware Interface (UEFI) specification
1398 * for details.
1399 *
1400 * @image_handle handle of the loaded image
1401 * @map_key key of the memory map
1402 * @return status code
1403 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001404static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1405 unsigned long map_key)
1406{
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001407 int i;
1408
Alexander Grafc15d9212016-03-04 01:09:59 +01001409 EFI_ENTRY("%p, %ld", image_handle, map_key);
1410
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001411 /* Notify that ExitBootServices is invoked. */
1412 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1413 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1414 continue;
1415 efi_signal_event(&efi_events[i]);
1416 }
1417 /* Make sure that notification functions are not called anymore */
1418 efi_tpl = TPL_HIGH_LEVEL;
1419
Alexander Graf600af332017-10-19 23:23:50 +02001420 /* XXX Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001421
Alexander Graf2ebeb442016-11-17 01:02:57 +01001422 board_quiesce_devices();
1423
Alexander Grafc15d9212016-03-04 01:09:59 +01001424 /* Fix up caches for EFI payloads if necessary */
1425 efi_exit_caches();
1426
1427 /* This stops all lingering devices */
1428 bootm_disable_interrupts();
1429
1430 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001431 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001432 WATCHDOG_RESET();
1433
1434 return EFI_EXIT(EFI_SUCCESS);
1435}
1436
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001437/*
1438 * Get next value of the counter.
1439 *
1440 * This function implements the NextMonotonicCount service.
1441 * See the Unified Extensible Firmware Interface (UEFI) specification
1442 * for details.
1443 *
1444 * @count returned value of the counter
1445 * @return status code
1446 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001447static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1448{
1449 static uint64_t mono = 0;
1450 EFI_ENTRY("%p", count);
1451 *count = mono++;
1452 return EFI_EXIT(EFI_SUCCESS);
1453}
1454
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001455/*
1456 * Sleep.
1457 *
1458 * This function implements the Stall sercive.
1459 * See the Unified Extensible Firmware Interface (UEFI) specification
1460 * for details.
1461 *
1462 * @microseconds period to sleep in microseconds
1463 * @return status code
1464 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001465static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1466{
1467 EFI_ENTRY("%ld", microseconds);
1468 udelay(microseconds);
1469 return EFI_EXIT(EFI_SUCCESS);
1470}
1471
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001472/*
1473 * Reset the watchdog timer.
1474 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001475 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001476 * See the Unified Extensible Firmware Interface (UEFI) specification
1477 * for details.
1478 *
1479 * @timeout seconds before reset by watchdog
1480 * @watchdog_code code to be logged when resetting
1481 * @data_size size of buffer in bytes
1482 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001483 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001484 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001485static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1486 uint64_t watchdog_code,
1487 unsigned long data_size,
1488 uint16_t *watchdog_data)
1489{
1490 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1491 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001492 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001493}
1494
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001495/*
1496 * Connect a controller to a driver.
1497 *
1498 * This function implements the ConnectController service.
1499 * See the Unified Extensible Firmware Interface (UEFI) specification
1500 * for details.
1501 *
1502 * @controller_handle handle of the controller
1503 * @driver_image_handle handle of the driver
1504 * @remain_device_path device path of a child controller
1505 * @recursive true to connect all child controllers
1506 * @return status code
1507 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001508static efi_status_t EFIAPI efi_connect_controller(
1509 efi_handle_t controller_handle,
1510 efi_handle_t *driver_image_handle,
1511 struct efi_device_path *remain_device_path,
1512 bool recursive)
1513{
1514 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1515 remain_device_path, recursive);
1516 return EFI_EXIT(EFI_NOT_FOUND);
1517}
1518
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001519/*
1520 * Disconnect a controller from a driver.
1521 *
1522 * This function implements the DisconnectController service.
1523 * See the Unified Extensible Firmware Interface (UEFI) specification
1524 * for details.
1525 *
1526 * @controller_handle handle of the controller
1527 * @driver_image_handle handle of the driver
1528 * @child_handle handle of the child to destroy
1529 * @return status code
1530 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001531static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1532 void *driver_image_handle,
1533 void *child_handle)
1534{
1535 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1536 child_handle);
1537 return EFI_EXIT(EFI_INVALID_PARAMETER);
1538}
1539
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001540/*
1541 * Close a protocol.
1542 *
1543 * This function implements the CloseProtocol service.
1544 * See the Unified Extensible Firmware Interface (UEFI) specification
1545 * for details.
1546 *
1547 * @handle handle on which the protocol shall be closed
1548 * @protocol GUID of the protocol to close
1549 * @agent_handle handle of the driver
1550 * @controller_handle handle of the controller
1551 * @return status code
1552 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001553static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001554 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001555 void *agent_handle,
1556 void *controller_handle)
1557{
Rob Clark238f88c2017-09-13 18:05:41 -04001558 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001559 controller_handle);
1560 return EFI_EXIT(EFI_NOT_FOUND);
1561}
1562
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001563/*
1564 * Provide information about then open status of a protocol on a handle
1565 *
1566 * This function implements the OpenProtocolInformation service.
1567 * See the Unified Extensible Firmware Interface (UEFI) specification
1568 * for details.
1569 *
1570 * @handle handle for which the information shall be retrieved
1571 * @protocol GUID of the protocol
1572 * @entry_buffer buffer to receive the open protocol information
1573 * @entry_count number of entries available in the buffer
1574 * @return status code
1575 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001576static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001577 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001578 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001579 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001580{
Rob Clark238f88c2017-09-13 18:05:41 -04001581 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01001582 entry_count);
1583 return EFI_EXIT(EFI_NOT_FOUND);
1584}
1585
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001586/*
1587 * Get protocols installed on a handle.
1588 *
1589 * This function implements the ProtocolsPerHandleService.
1590 * See the Unified Extensible Firmware Interface (UEFI) specification
1591 * for details.
1592 *
1593 * @handle handle for which the information is retrieved
1594 * @protocol_buffer buffer with protocol GUIDs
1595 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001596 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001597 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001598static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1599 efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001600 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01001601{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001602 unsigned long buffer_size;
1603 struct efi_object *efiobj;
1604 unsigned long i, j;
1605 struct list_head *lhandle;
1606 efi_status_t r;
1607
Alexander Grafc15d9212016-03-04 01:09:59 +01001608 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1609 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001610
1611 if (!handle || !protocol_buffer || !protocol_buffer_count)
1612 return EFI_EXIT(EFI_INVALID_PARAMETER);
1613
1614 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04001615 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02001616 list_for_each(lhandle, &efi_obj_list) {
1617 efiobj = list_entry(lhandle, struct efi_object, link);
1618
1619 if (efiobj->handle != handle)
1620 continue;
1621
1622 /* Count protocols */
1623 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1624 if (efiobj->protocols[i].guid)
1625 ++*protocol_buffer_count;
1626 }
1627 /* Copy guids */
1628 if (*protocol_buffer_count) {
1629 buffer_size = sizeof(efi_guid_t *) *
1630 *protocol_buffer_count;
1631 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1632 buffer_size,
1633 (void **)protocol_buffer);
1634 if (r != EFI_SUCCESS)
1635 return EFI_EXIT(r);
1636 j = 0;
1637 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1638 if (efiobj->protocols[i].guid) {
1639 (*protocol_buffer)[j] = (void *)
1640 efiobj->protocols[i].guid;
1641 ++j;
1642 }
1643 }
1644 }
1645 break;
1646 }
1647
1648 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001649}
1650
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001651/*
1652 * Locate handles implementing a protocol.
1653 *
1654 * This function implements the LocateHandleBuffer service.
1655 * See the Unified Extensible Firmware Interface (UEFI) specification
1656 * for details.
1657 *
1658 * @search_type selection criterion
1659 * @protocol GUID of the protocol
1660 * @search_key registration key
1661 * @no_handles number of returned handles
1662 * @buffer buffer with the returned handles
1663 * @return status code
1664 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001665static efi_status_t EFIAPI efi_locate_handle_buffer(
1666 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001667 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001668 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001669{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001670 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001671 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001672
Rob Clark238f88c2017-09-13 18:05:41 -04001673 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001674 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02001675
1676 if (!no_handles || !buffer) {
1677 r = EFI_INVALID_PARAMETER;
1678 goto out;
1679 }
1680 *no_handles = 0;
1681 *buffer = NULL;
1682 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1683 *buffer);
1684 if (r != EFI_BUFFER_TOO_SMALL)
1685 goto out;
1686 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1687 (void **)buffer);
1688 if (r != EFI_SUCCESS)
1689 goto out;
1690 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1691 *buffer);
1692 if (r == EFI_SUCCESS)
1693 *no_handles = buffer_size / sizeof(void *);
1694out:
1695 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001696}
1697
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001698/*
1699 * Find an interface implementing a protocol.
1700 *
1701 * This function implements the LocateProtocol service.
1702 * See the Unified Extensible Firmware Interface (UEFI) specification
1703 * for details.
1704 *
1705 * @protocol GUID of the protocol
1706 * @registration registration key passed to the notification function
1707 * @protocol_interface interface implementing the protocol
Heinrich Schuchardtf1066d62017-10-08 06:57:27 +02001708 * @return status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001709 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001710static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001711 void *registration,
1712 void **protocol_interface)
1713{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001714 struct list_head *lhandle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001715 int i;
1716
Rob Clark238f88c2017-09-13 18:05:41 -04001717 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001718
1719 if (!protocol || !protocol_interface)
1720 return EFI_EXIT(EFI_INVALID_PARAMETER);
1721
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001722 EFI_PRINT_GUID("protocol", protocol);
1723
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001724 list_for_each(lhandle, &efi_obj_list) {
1725 struct efi_object *efiobj;
1726
1727 efiobj = list_entry(lhandle, struct efi_object, link);
1728 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1729 struct efi_handler *handler = &efiobj->protocols[i];
1730
1731 if (!handler->guid)
1732 continue;
1733 if (!guidcmp(handler->guid, protocol)) {
1734 *protocol_interface =
1735 handler->protocol_interface;
1736 return EFI_EXIT(EFI_SUCCESS);
1737 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001738 }
1739 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02001740 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001741
1742 return EFI_EXIT(EFI_NOT_FOUND);
1743}
1744
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001745/*
1746 * Install multiple protocol interfaces.
1747 *
1748 * This function implements the MultipleProtocolInterfaces service.
1749 * See the Unified Extensible Firmware Interface (UEFI) specification
1750 * for details.
1751 *
1752 * @handle handle on which the protocol interfaces shall be installed
1753 * @... NULL terminated argument list with pairs of protocol GUIDS and
1754 * interfaces
1755 * @return status code
1756 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001757static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1758 void **handle, ...)
1759{
1760 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001761
1762 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001763 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001764 void *protocol_interface;
1765 efi_status_t r = EFI_SUCCESS;
1766 int i = 0;
1767
1768 if (!handle)
1769 return EFI_EXIT(EFI_INVALID_PARAMETER);
1770
1771 va_start(argptr, handle);
1772 for (;;) {
1773 protocol = va_arg(argptr, efi_guid_t*);
1774 if (!protocol)
1775 break;
1776 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001777 r = EFI_CALL(efi_install_protocol_interface(
1778 handle, protocol,
1779 EFI_NATIVE_INTERFACE,
1780 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001781 if (r != EFI_SUCCESS)
1782 break;
1783 i++;
1784 }
1785 va_end(argptr);
1786 if (r == EFI_SUCCESS)
1787 return EFI_EXIT(r);
1788
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02001789 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001790 va_start(argptr, handle);
1791 for (; i; --i) {
1792 protocol = va_arg(argptr, efi_guid_t*);
1793 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001794 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1795 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02001796 }
1797 va_end(argptr);
1798
1799 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001800}
1801
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001802/*
1803 * Uninstall multiple protocol interfaces.
1804 *
1805 * This function implements the UninstallMultipleProtocolInterfaces service.
1806 * See the Unified Extensible Firmware Interface (UEFI) specification
1807 * for details.
1808 *
1809 * @handle handle from which the protocol interfaces shall be removed
1810 * @... NULL terminated argument list with pairs of protocol GUIDS and
1811 * interfaces
1812 * @return status code
1813 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001814static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1815 void *handle, ...)
1816{
1817 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02001818
1819 va_list argptr;
1820 const efi_guid_t *protocol;
1821 void *protocol_interface;
1822 efi_status_t r = EFI_SUCCESS;
1823 size_t i = 0;
1824
1825 if (!handle)
1826 return EFI_EXIT(EFI_INVALID_PARAMETER);
1827
1828 va_start(argptr, handle);
1829 for (;;) {
1830 protocol = va_arg(argptr, efi_guid_t*);
1831 if (!protocol)
1832 break;
1833 protocol_interface = va_arg(argptr, void*);
1834 r = EFI_CALL(efi_uninstall_protocol_interface(
1835 handle, protocol,
1836 protocol_interface));
1837 if (r != EFI_SUCCESS)
1838 break;
1839 i++;
1840 }
1841 va_end(argptr);
1842 if (r == EFI_SUCCESS)
1843 return EFI_EXIT(r);
1844
1845 /* If an error occurred undo all changes. */
1846 va_start(argptr, handle);
1847 for (; i; --i) {
1848 protocol = va_arg(argptr, efi_guid_t*);
1849 protocol_interface = va_arg(argptr, void*);
1850 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1851 EFI_NATIVE_INTERFACE,
1852 protocol_interface));
1853 }
1854 va_end(argptr);
1855
1856 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001857}
1858
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001859/*
1860 * Calculate cyclic redundancy code.
1861 *
1862 * This function implements the CalculateCrc32 service.
1863 * See the Unified Extensible Firmware Interface (UEFI) specification
1864 * for details.
1865 *
1866 * @data buffer with data
1867 * @data_size size of buffer in bytes
1868 * @crc32_p cyclic redundancy code
1869 * @return status code
1870 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001871static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1872 unsigned long data_size,
1873 uint32_t *crc32_p)
1874{
1875 EFI_ENTRY("%p, %ld", data, data_size);
1876 *crc32_p = crc32(0, data, data_size);
1877 return EFI_EXIT(EFI_SUCCESS);
1878}
1879
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001880/*
1881 * Copy memory.
1882 *
1883 * This function implements the CopyMem service.
1884 * See the Unified Extensible Firmware Interface (UEFI) specification
1885 * for details.
1886 *
1887 * @destination destination of the copy operation
1888 * @source source of the copy operation
1889 * @length number of bytes to copy
1890 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001891static void EFIAPI efi_copy_mem(void *destination, const void *source,
1892 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01001893{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001894 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01001895 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001896 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001897}
1898
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001899/*
1900 * Fill memory with a byte value.
1901 *
1902 * This function implements the SetMem service.
1903 * See the Unified Extensible Firmware Interface (UEFI) specification
1904 * for details.
1905 *
1906 * @buffer buffer to fill
1907 * @size size of buffer in bytes
1908 * @value byte to copy to the buffer
1909 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001910static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01001911{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02001912 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01001913 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02001914 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001915}
1916
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001917/*
1918 * Open protocol interface on a handle.
1919 *
1920 * This function implements the OpenProtocol interface.
1921 * See the Unified Extensible Firmware Interface (UEFI) specification
1922 * for details.
1923 *
1924 * @handle handle on which the protocol shall be opened
1925 * @protocol GUID of the protocol
1926 * @protocol_interface interface implementing the protocol
1927 * @agent_handle handle of the driver
1928 * @controller_handle handle of the controller
1929 * @attributes attributes indicating how to open the protocol
1930 * @return status code
1931 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001932static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001933 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001934 void **protocol_interface, void *agent_handle,
1935 void *controller_handle, uint32_t attributes)
1936{
1937 struct list_head *lhandle;
1938 int i;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001939 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01001940
Rob Clark238f88c2017-09-13 18:05:41 -04001941 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001942 protocol_interface, agent_handle, controller_handle,
1943 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001944
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001945 if (!handle || !protocol ||
1946 (!protocol_interface && attributes !=
1947 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001948 goto out;
1949 }
1950
Heinrich Schuchardt4d664892017-08-18 17:45:16 +02001951 EFI_PRINT_GUID("protocol", protocol);
1952
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001953 switch (attributes) {
1954 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1955 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1956 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1957 break;
1958 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1959 if (controller_handle == handle)
1960 goto out;
1961 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1962 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1963 if (controller_handle == NULL)
1964 goto out;
1965 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1966 if (agent_handle == NULL)
1967 goto out;
1968 break;
1969 default:
1970 goto out;
1971 }
1972
Alexander Grafc15d9212016-03-04 01:09:59 +01001973 list_for_each(lhandle, &efi_obj_list) {
1974 struct efi_object *efiobj;
1975 efiobj = list_entry(lhandle, struct efi_object, link);
1976
1977 if (efiobj->handle != handle)
1978 continue;
1979
1980 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1981 struct efi_handler *handler = &efiobj->protocols[i];
1982 const efi_guid_t *hprotocol = handler->guid;
1983 if (!hprotocol)
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001984 continue;
Alexander Grafc15d9212016-03-04 01:09:59 +01001985 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02001986 if (attributes !=
1987 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1988 *protocol_interface =
1989 handler->protocol_interface;
1990 }
1991 r = EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001992 goto out;
1993 }
1994 }
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001995 goto unsupported;
Alexander Grafc15d9212016-03-04 01:09:59 +01001996 }
1997
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02001998unsupported:
1999 r = EFI_UNSUPPORTED;
Alexander Grafc15d9212016-03-04 01:09:59 +01002000out:
2001 return EFI_EXIT(r);
2002}
2003
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002004/*
2005 * Get interface of a protocol on a handle.
2006 *
2007 * This function implements the HandleProtocol service.
2008 * See the Unified Extensible Firmware Interface (UEFI) specification
2009 * for details.
2010 *
2011 * @handle handle on which the protocol shall be opened
2012 * @protocol GUID of the protocol
2013 * @protocol_interface interface implementing the protocol
2014 * @return status code
2015 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002016static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002017 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002018 void **protocol_interface)
2019{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002020 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2021 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002022}
2023
2024static const struct efi_boot_services efi_boot_services = {
2025 .hdr = {
2026 .headersize = sizeof(struct efi_table_hdr),
2027 },
2028 .raise_tpl = efi_raise_tpl,
2029 .restore_tpl = efi_restore_tpl,
2030 .allocate_pages = efi_allocate_pages_ext,
2031 .free_pages = efi_free_pages_ext,
2032 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02002033 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02002034 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02002035 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02002036 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002037 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02002038 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002039 .close_event = efi_close_event,
2040 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002041 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002042 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002043 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01002044 .handle_protocol = efi_handle_protocol,
2045 .reserved = NULL,
2046 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02002047 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002048 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02002049 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01002050 .load_image = efi_load_image,
2051 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02002052 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01002053 .unload_image = efi_unload_image,
2054 .exit_boot_services = efi_exit_boot_services,
2055 .get_next_monotonic_count = efi_get_next_monotonic_count,
2056 .stall = efi_stall,
2057 .set_watchdog_timer = efi_set_watchdog_timer,
2058 .connect_controller = efi_connect_controller,
2059 .disconnect_controller = efi_disconnect_controller,
2060 .open_protocol = efi_open_protocol,
2061 .close_protocol = efi_close_protocol,
2062 .open_protocol_information = efi_open_protocol_information,
2063 .protocols_per_handle = efi_protocols_per_handle,
2064 .locate_handle_buffer = efi_locate_handle_buffer,
2065 .locate_protocol = efi_locate_protocol,
2066 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2067 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2068 .calculate_crc32 = efi_calculate_crc32,
2069 .copy_mem = efi_copy_mem,
2070 .set_mem = efi_set_mem,
2071};
2072
2073
Alexander Graf393dd912016-10-14 13:45:30 +02002074static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafc15d9212016-03-04 01:09:59 +01002075 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2076
Alexander Graf393dd912016-10-14 13:45:30 +02002077struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01002078 .hdr = {
2079 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2080 .revision = 0x20005, /* 2.5 */
2081 .headersize = sizeof(struct efi_table_hdr),
2082 },
2083 .fw_vendor = (long)firmware_vendor,
2084 .con_in = (void*)&efi_con_in,
2085 .con_out = (void*)&efi_con_out,
2086 .std_err = (void*)&efi_con_out,
2087 .runtime = (void*)&efi_runtime_services,
2088 .boottime = (void*)&efi_boot_services,
2089 .nr_tables = 0,
2090 .tables = (void*)efi_conf_table,
2091};