blob: b79b3aed49e67c083b3b04a6898f581d9880d1d3 [file] [log] [blame]
Alexander Grafbee91162016-03-04 01:09:59 +01001/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafbee91162016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020025
Alexander Grafbee91162016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
29/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
37/*
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
41 *
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
44 */
Alexander Graf3c63db92016-10-14 13:45:30 +020045static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafbee91162016-03-04 01:09:59 +010046
Simon Glass65e4c0b2016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010048/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060055#endif
Alexander Grafbee91162016-03-04 01:09:59 +010056
Rob Clarkc160d2f2017-07-27 08:04:18 -040057static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040058static int nesting_level;
Rob Clarkc160d2f2017-07-27 08:04:18 -040059
60/* Called on every callback entry */
61int __efi_entry_check(void)
62{
63 int ret = entry_count++ == 0;
64#ifdef CONFIG_ARM
65 assert(efi_gd);
66 app_gd = gd;
67 gd = efi_gd;
68#endif
69 return ret;
70}
71
72/* Called on every callback exit */
73int __efi_exit_check(void)
74{
75 int ret = --entry_count == 0;
76#ifdef CONFIG_ARM
77 gd = app_gd;
78#endif
79 return ret;
80}
81
Alexander Grafbee91162016-03-04 01:09:59 +010082/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
Simon Glass65e4c0b2016-09-25 15:27:35 -060085#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010086 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060087#endif
Alexander Grafbee91162016-03-04 01:09:59 +010088}
89
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/*
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
94 */
Alexander Grafbee91162016-03-04 01:09:59 +010095void efi_restore_gd(void)
96{
Simon Glass65e4c0b2016-09-25 15:27:35 -060097#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010098 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100101 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600102#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100103}
104
Rob Clarkaf65db82017-07-27 08:04:19 -0400105/*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109static const char *indent_string(int level)
110{
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
115}
116
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
Rob Clarkaf65db82017-07-27 08:04:19 -0400122const char *__efi_nesting_inc(void)
123{
124 return indent_string(nesting_level++);
125}
126
127const char *__efi_nesting_dec(void)
128{
129 return indent_string(--nesting_level);
130}
131
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200132/*
133 * Queue an EFI event.
134 *
135 * This function queues the notification function of the event for future
136 * execution.
137 *
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
140 *
141 * For the SignalEvent service see efi_signal_event_ext.
142 *
143 * @event event to signal
144 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200145void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200146{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200147 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200148 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200154 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200155 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200156}
157
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200158/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 * Raise the task priority level.
160 *
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
163 * for details.
164 *
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
167 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100169{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100170 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200171
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200172 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 efi_tpl = new_tpl;
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
179
180 EFI_EXIT(EFI_SUCCESS);
181 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100182}
183
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200184/*
185 * Lower the task priority level.
186 *
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
189 * for details.
190 *
191 * @old_tpl value of the task priority level to be restored
192 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100194{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200195 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200196
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 efi_tpl = old_tpl;
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
202
203 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100204}
205
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200206/*
207 * Allocate memory pages.
208 *
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
211 * for details.
212 *
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
218 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100220 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900221 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100222{
223 efi_status_t r;
224
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200230/*
231 * Free memory pages.
232 *
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
235 * for details.
236 *
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
240 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100242 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100243{
244 efi_status_t r;
245
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200251/*
252 * Get map describing memory usage.
253 *
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
256 * for details.
257 *
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
265 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900266static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100267 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900268 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900271 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100272{
273 efi_status_t r;
274
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
279 return EFI_EXIT(r);
280}
281
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200282/*
283 * Allocate memory from pool.
284 *
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
287 * for details.
288 *
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
293 */
Stefan Brünsead12742016-10-09 22:17:18 +0200294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100295 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200296 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100297{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100298 efi_status_t r;
299
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200301 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100302 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100303}
304
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200305/*
306 * Free memory from pool.
307 *
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @buffer start of memory to be freed
313 * @return status code
314 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100316{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200320 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100321 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100322}
323
Heinrich Schuchardt3cc6e3f2017-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 Grafbee91162016-03-04 01:09:59 +0100341/*
xypron.glpk@gmx.dec6841592017-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 Grafbee91162016-03-04 01:09:59 +0100344 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200345static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100346
Heinrich Schuchardt332468f2017-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 Schuchardt152cade2017-11-06 21:17:47 +0100362efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200363 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200364 struct efi_event *event,
365 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200366 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100367{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200368 int i;
369
Jonathan Graya95343b2017-03-12 19:26:07 +1100370 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200371 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100372
373 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200374 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100375
376 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
377 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200378 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100379
xypron.glpk@gmx.dec6841592017-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 Schuchardte190e892017-10-04 15:03:24 +0200389 efi_events[i].is_queued = false;
390 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200391 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200392 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200393 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200394 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100395}
396
Heinrich Schuchardt332468f2017-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.de49deb452017-07-18 20:17:20 +0200411static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100412 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-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 Grafbee91162016-03-04 01:09:59 +0100425/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200426 * Check if a timer event has occurred or a queued notification function should
427 * be called.
428 *
Alexander Grafbee91162016-03-04 01:09:59 +0100429 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200430 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100431 */
432void efi_timer_check(void)
433{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200434 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100435 u64 now = timer_get_us();
436
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200437 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200438 if (!efi_events[i].type)
439 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200440 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200441 efi_signal_event(&efi_events[i]);
442 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200443 now < efi_events[i].trigger_next)
444 continue;
Heinrich Schuchardtca62a4f2017-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.dec6841592017-07-18 20:17:18 +0200450 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200451 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200452 break;
453 default:
454 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200455 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200456 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200457 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100458 }
Alexander Grafbee91162016-03-04 01:09:59 +0100459 WATCHDOG_RESET();
460}
461
Heinrich Schuchardt332468f2017-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.deb521d292017-07-19 19:22:34 +0200473efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200474 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100475{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200476 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100477
xypron.glpk@gmx.de8787b022017-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 Schuchardt7d963322017-10-05 16:14:14 +0200482 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100483
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200484 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
485 if (event != &efi_events[i])
486 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100487
xypron.glpk@gmx.dec6841592017-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.de8787b022017-07-18 20:17:23 +0200497 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200498 break;
499 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200500 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200501 }
502 event->trigger_type = type;
503 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200504 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200505 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100506 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200507 return EFI_INVALID_PARAMETER;
508}
509
Heinrich Schuchardt332468f2017-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.deb521d292017-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.debfc72462017-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));
Alexander Grafbee91162016-03-04 01:09:59 +0100528}
529
Heinrich Schuchardt332468f2017-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 Schuchardtf5a2a932017-11-06 21:17:48 +0100542static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200543 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100544 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100545{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200546 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100547
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100548 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100549
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200550 /* Check parameters */
551 if (!num_events || !event)
552 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200553 /* Check TPL */
554 if (efi_tpl != TPL_APPLICATION)
555 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-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 Schuchardte190e892017-10-04 15:03:24 +0200565 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200566 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200567 }
568
569 /* Wait for signal */
570 for (;;) {
571 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200572 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-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 Schuchardte190e892017-10-04 15:03:24 +0200584 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200585 if (index)
586 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100587
588 return EFI_EXIT(EFI_SUCCESS);
589}
590
Heinrich Schuchardt332468f2017-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 Schuchardt10a08c42017-10-08 06:57:27 +0200602 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200603 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200604static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100605{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200606 int i;
607
Alexander Grafbee91162016-03-04 01:09:59 +0100608 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200609 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
610 if (event != &efi_events[i])
611 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200612 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200613 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200614 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200615 if (event->type & EVT_NOTIFY_SIGNAL)
616 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200617 break;
618 }
Alexander Grafbee91162016-03-04 01:09:59 +0100619 return EFI_EXIT(EFI_SUCCESS);
620}
621
Heinrich Schuchardt332468f2017-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.de2fd945f2017-07-18 20:17:17 +0200632static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100633{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200634 int i;
635
Alexander Grafbee91162016-03-04 01:09:59 +0100636 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-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 Schuchardte190e892017-10-04 15:03:24 +0200641 event->is_queued = false;
642 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200643 return EFI_EXIT(EFI_SUCCESS);
644 }
645 }
646 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100647}
648
Heinrich Schuchardt332468f2017-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.de2fd945f2017-07-18 20:17:17 +0200661static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100662{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200663 int i;
664
Alexander Grafbee91162016-03-04 01:09:59 +0100665 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-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 Schuchardte190e892017-10-04 15:03:24 +0200672 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200673 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200674 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-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 Grafbee91162016-03-04 01:09:59 +0100679}
680
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200681/*
Heinrich Schuchardt7b9f8ad2017-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 */
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100687struct efi_object *efi_search_obj(void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200688{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100689 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200690
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100691 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200692 if (efiobj->handle == handle)
693 return efiobj;
694 }
695
696 return NULL;
697}
698
699/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200700 * Install protocol interface.
701 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100702 * This function implements the InstallProtocolInterface service.
703 * See the Unified Extensible Firmware Interface (UEFI) specification
704 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200705 *
706 * @handle handle on which the protocol shall be installed
707 * @protocol GUID of the protocol to be installed
708 * @protocol_interface_type type of the interface to be installed,
709 * always EFI_NATIVE_INTERFACE
710 * @protocol_interface interface of the protocol implementation
711 * @return status code
712 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100713static efi_status_t EFIAPI efi_install_protocol_interface(
714 void **handle, const efi_guid_t *protocol,
715 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100716{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200717 struct list_head *lhandle;
718 int i;
719 efi_status_t r;
720
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100721 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
722 protocol_interface);
723
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200724 if (!handle || !protocol ||
725 protocol_interface_type != EFI_NATIVE_INTERFACE) {
726 r = EFI_INVALID_PARAMETER;
727 goto out;
728 }
729
730 /* Create new handle if requested. */
731 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200732 r = efi_create_handle(handle);
733 if (r != EFI_SUCCESS)
734 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200735 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
736 *handle);
737 } else {
738 debug("%sEFI: handle %p\n", indent_string(nesting_level),
739 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200740 }
741 /* Find object. */
742 list_for_each(lhandle, &efi_obj_list) {
743 struct efi_object *efiobj;
744 efiobj = list_entry(lhandle, struct efi_object, link);
745
746 if (efiobj->handle != *handle)
747 continue;
748 /* Check if protocol is already installed on the handle. */
749 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
750 struct efi_handler *handler = &efiobj->protocols[i];
751
752 if (!handler->guid)
753 continue;
754 if (!guidcmp(handler->guid, protocol)) {
755 r = EFI_INVALID_PARAMETER;
756 goto out;
757 }
758 }
759 /* Install protocol in first empty slot. */
760 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
761 struct efi_handler *handler = &efiobj->protocols[i];
762
763 if (handler->guid)
764 continue;
765
766 handler->guid = protocol;
767 handler->protocol_interface = protocol_interface;
768 r = EFI_SUCCESS;
769 goto out;
770 }
771 r = EFI_OUT_OF_RESOURCES;
772 goto out;
773 }
774 r = EFI_INVALID_PARAMETER;
775out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100776 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100777}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200778
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200779/*
780 * Reinstall protocol interface.
781 *
782 * This function implements the ReinstallProtocolInterface service.
783 * See the Unified Extensible Firmware Interface (UEFI) specification
784 * for details.
785 *
786 * @handle handle on which the protocol shall be
787 * reinstalled
788 * @protocol GUID of the protocol to be installed
789 * @old_interface interface to be removed
790 * @new_interface interface to be installed
791 * @return status code
792 */
Alexander Grafbee91162016-03-04 01:09:59 +0100793static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200794 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100795 void *new_interface)
796{
Rob Clark778e6af2017-09-13 18:05:41 -0400797 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100798 new_interface);
799 return EFI_EXIT(EFI_ACCESS_DENIED);
800}
801
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200802/*
803 * Uninstall protocol interface.
804 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100805 * This function implements the UninstallProtocolInterface service.
806 * See the Unified Extensible Firmware Interface (UEFI) specification
807 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200808 *
809 * @handle handle from which the protocol shall be removed
810 * @protocol GUID of the protocol to be removed
811 * @protocol_interface interface to be removed
812 * @return status code
813 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100814static efi_status_t EFIAPI efi_uninstall_protocol_interface(
815 void *handle, const efi_guid_t *protocol,
816 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100817{
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200818 struct list_head *lhandle;
819 int i;
820 efi_status_t r = EFI_NOT_FOUND;
821
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100822 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
823
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200824 if (!handle || !protocol) {
825 r = EFI_INVALID_PARAMETER;
826 goto out;
827 }
828
829 list_for_each(lhandle, &efi_obj_list) {
830 struct efi_object *efiobj;
831 efiobj = list_entry(lhandle, struct efi_object, link);
832
833 if (efiobj->handle != handle)
834 continue;
835
836 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
837 struct efi_handler *handler = &efiobj->protocols[i];
838 const efi_guid_t *hprotocol = handler->guid;
839
840 if (!hprotocol)
841 continue;
842 if (!guidcmp(hprotocol, protocol)) {
843 if (handler->protocol_interface) {
844 r = EFI_ACCESS_DENIED;
845 } else {
846 handler->guid = 0;
847 r = EFI_SUCCESS;
848 }
849 goto out;
850 }
851 }
852 }
853
854out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100855 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100856}
857
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200858/*
859 * Register an event for notification when a protocol is installed.
860 *
861 * This function implements the RegisterProtocolNotify service.
862 * See the Unified Extensible Firmware Interface (UEFI) specification
863 * for details.
864 *
865 * @protocol GUID of the protocol whose installation shall be
866 * notified
867 * @event event to be signaled upon installation of the protocol
868 * @registration key for retrieving the registration information
869 * @return status code
870 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200871static efi_status_t EFIAPI efi_register_protocol_notify(
872 const efi_guid_t *protocol,
873 struct efi_event *event,
874 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +0100875{
Rob Clark778e6af2017-09-13 18:05:41 -0400876 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +0100877 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
878}
879
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200880/*
881 * Determine if an EFI handle implements a protocol.
882 *
883 * See the documentation of the LocateHandle service in the UEFI specification.
884 *
885 * @search_type selection criterion
886 * @protocol GUID of the protocol
887 * @search_key registration key
888 * @efiobj handle
889 * @return 0 if the handle implements the protocol
890 */
Alexander Grafbee91162016-03-04 01:09:59 +0100891static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200892 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +0100893 struct efi_object *efiobj)
894{
895 int i;
896
897 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100898 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +0100899 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100900 case BY_REGISTER_NOTIFY:
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +0100901 /* RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +0100902 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100903 case BY_PROTOCOL:
Alexander Grafbee91162016-03-04 01:09:59 +0100904 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
905 const efi_guid_t *guid = efiobj->protocols[i].guid;
906 if (guid && !guidcmp(guid, protocol))
907 return 0;
908 }
909 return -1;
910 }
911
912 return -1;
913}
914
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200915/*
916 * Locate handles implementing a protocol.
917 *
918 * This function is meant for U-Boot internal calls. For the API implementation
919 * of the LocateHandle service see efi_locate_handle_ext.
920 *
921 * @search_type selection criterion
922 * @protocol GUID of the protocol
923 * @search_key registration key
924 * @buffer_size size of the buffer to receive the handles in bytes
925 * @buffer buffer to receive the relevant handles
926 * @return status code
927 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +0200928static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +0100929 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200930 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100931 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100932{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +0100933 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100934 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +0100935
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +0100936 /* Check parameters */
937 switch (search_type) {
938 case ALL_HANDLES:
939 break;
940 case BY_REGISTER_NOTIFY:
941 if (!search_key)
942 return EFI_INVALID_PARAMETER;
943 /* RegisterProtocolNotify is not implemented yet */
944 return EFI_UNSUPPORTED;
945 case BY_PROTOCOL:
946 if (!protocol)
947 return EFI_INVALID_PARAMETER;
948 break;
949 default:
950 return EFI_INVALID_PARAMETER;
951 }
952
953 /*
954 * efi_locate_handle_buffer uses this function for
955 * the calculation of the necessary buffer size.
956 * So do not require a buffer for buffersize == 0.
957 */
958 if (!buffer_size || (*buffer_size && !buffer))
959 return EFI_INVALID_PARAMETER;
960
Alexander Grafbee91162016-03-04 01:09:59 +0100961 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +0100962 list_for_each_entry(efiobj, &efi_obj_list, link) {
963 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +0100964 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +0100965 }
966
967 if (*buffer_size < size) {
968 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +0200969 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +0100970 }
971
Rob Clark796a78c2017-08-06 14:10:07 -0400972 *buffer_size = size;
973 if (size == 0)
974 return EFI_NOT_FOUND;
975
Alexander Grafbee91162016-03-04 01:09:59 +0100976 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +0100977 list_for_each_entry(efiobj, &efi_obj_list, link) {
978 if (!efi_search(search_type, protocol, search_key, efiobj))
979 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +0100980 }
981
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +0200982 return EFI_SUCCESS;
983}
984
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200985/*
986 * Locate handles implementing a protocol.
987 *
988 * This function implements the LocateHandle service.
989 * See the Unified Extensible Firmware Interface (UEFI) specification
990 * for details.
991 *
992 * @search_type selection criterion
993 * @protocol GUID of the protocol
994 * @search_key registration key
995 * @buffer_size size of the buffer to receive the handles in bytes
996 * @buffer buffer to receive the relevant handles
997 * @return 0 if the handle implements the protocol
998 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +0200999static efi_status_t EFIAPI efi_locate_handle_ext(
1000 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001001 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001002 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001003{
Rob Clark778e6af2017-09-13 18:05:41 -04001004 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001005 buffer_size, buffer);
1006
1007 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1008 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001009}
1010
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001011/*
1012 * Get the device path and handle of an device implementing a protocol.
1013 *
1014 * This function implements the LocateDevicePath service.
1015 * See the Unified Extensible Firmware Interface (UEFI) specification
1016 * for details.
1017 *
1018 * @protocol GUID of the protocol
1019 * @device_path device path
1020 * @device handle of the device
1021 * @return status code
1022 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001023static efi_status_t EFIAPI efi_locate_device_path(
1024 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001025 struct efi_device_path **device_path,
1026 efi_handle_t *device)
1027{
Rob Clarkb66c60d2017-09-13 18:05:28 -04001028 struct efi_object *efiobj;
1029
1030 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1031
1032 efiobj = efi_dp_find_obj(*device_path, device_path);
1033 if (!efiobj)
1034 return EFI_EXIT(EFI_NOT_FOUND);
1035
1036 *device = efiobj->handle;
1037
1038 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001039}
1040
Alexander Grafd98cdf62017-07-26 13:41:04 +02001041/* Collapses configuration table entries, removing index i */
1042static void efi_remove_configuration_table(int i)
1043{
1044 struct efi_configuration_table *this = &efi_conf_table[i];
1045 struct efi_configuration_table *next = &efi_conf_table[i+1];
1046 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1047
1048 memmove(this, next, (ulong)end - (ulong)next);
1049 systab.nr_tables--;
1050}
1051
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001052/*
1053 * Adds, updates, or removes a configuration table.
1054 *
1055 * This function is used for internal calls. For the API implementation of the
1056 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1057 *
1058 * @guid GUID of the installed table
1059 * @table table to be installed
1060 * @return status code
1061 */
Alexander Graf488bf122016-08-19 01:23:24 +02001062efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001063{
1064 int i;
1065
Alexander Grafbee91162016-03-04 01:09:59 +01001066 /* Check for guid override */
1067 for (i = 0; i < systab.nr_tables; i++) {
1068 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001069 if (table)
1070 efi_conf_table[i].table = table;
1071 else
1072 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001073 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001074 }
1075 }
1076
Alexander Grafd98cdf62017-07-26 13:41:04 +02001077 if (!table)
1078 return EFI_NOT_FOUND;
1079
Alexander Grafbee91162016-03-04 01:09:59 +01001080 /* No override, check for overflow */
1081 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001082 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001083
1084 /* Add a new entry */
1085 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1086 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001087 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001088
Alexander Graf488bf122016-08-19 01:23:24 +02001089 return EFI_SUCCESS;
1090}
1091
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001092/*
1093 * Adds, updates, or removes a configuration table.
1094 *
1095 * This function implements the InstallConfigurationTable service.
1096 * See the Unified Extensible Firmware Interface (UEFI) specification
1097 * for details.
1098 *
1099 * @guid GUID of the installed table
1100 * @table table to be installed
1101 * @return status code
1102 */
Alexander Graf488bf122016-08-19 01:23:24 +02001103static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1104 void *table)
1105{
Rob Clark778e6af2017-09-13 18:05:41 -04001106 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001107 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001108}
1109
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001110/*
1111 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001112 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001113 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001114 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001115 * image
1116 * @obj internal object associated with the loaded image
1117 * @device_path device path of the loaded image
1118 * @file_path file path of the loaded image
Rob Clark95c55532017-09-13 18:05:33 -04001119 */
1120void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1121 struct efi_device_path *device_path,
1122 struct efi_device_path *file_path)
1123{
1124 obj->handle = info;
1125
1126 /*
1127 * When asking for the device path interface, return
1128 * bootefi_device_path
1129 */
1130 obj->protocols[0].guid = &efi_guid_device_path;
1131 obj->protocols[0].protocol_interface = device_path;
1132
1133 /*
1134 * When asking for the loaded_image interface, just
1135 * return handle which points to loaded_image_info
1136 */
1137 obj->protocols[1].guid = &efi_guid_loaded_image;
1138 obj->protocols[1].protocol_interface = info;
1139
1140 obj->protocols[2].guid = &efi_guid_console_control;
1141 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1142
1143 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1144 obj->protocols[3].protocol_interface =
1145 (void *)&efi_device_path_to_text;
1146
1147 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001148 if (device_path)
1149 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001150
1151 list_add_tail(&obj->link, &efi_obj_list);
1152}
1153
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001154/*
1155 * Load an image using a file path.
1156 *
1157 * @file_path the path of the image to load
1158 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001159 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001160 */
Rob Clark9975fe92017-09-13 18:05:38 -04001161efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1162 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001163{
1164 struct efi_file_info *info = NULL;
1165 struct efi_file_handle *f;
1166 static efi_status_t ret;
1167 uint64_t bs;
1168
1169 f = efi_file_from_path(file_path);
1170 if (!f)
1171 return EFI_DEVICE_ERROR;
1172
1173 bs = 0;
1174 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1175 &bs, info));
1176 if (ret == EFI_BUFFER_TOO_SMALL) {
1177 info = malloc(bs);
1178 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1179 &bs, info));
1180 }
1181 if (ret != EFI_SUCCESS)
1182 goto error;
1183
1184 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1185 if (ret)
1186 goto error;
1187
1188 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1189
1190error:
1191 free(info);
1192 EFI_CALL(f->close(f));
1193
1194 if (ret != EFI_SUCCESS) {
1195 efi_free_pool(*buffer);
1196 *buffer = NULL;
1197 }
1198
1199 return ret;
1200}
1201
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001202/*
1203 * Load an EFI image into memory.
1204 *
1205 * This function implements the LoadImage service.
1206 * See the Unified Extensible Firmware Interface (UEFI) specification
1207 * for details.
1208 *
1209 * @boot_policy true for request originating from the boot manager
1210 * @parent_image the calles's image handle
1211 * @file_path the path of the image to load
1212 * @source_buffer memory location from which the image is installed
1213 * @source_size size of the memory area from which the image is
1214 * installed
1215 * @image_handle handle for the newly installed image
1216 * @return status code
1217 */
Alexander Grafbee91162016-03-04 01:09:59 +01001218static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1219 efi_handle_t parent_image,
1220 struct efi_device_path *file_path,
1221 void *source_buffer,
1222 unsigned long source_size,
1223 efi_handle_t *image_handle)
1224{
Alexander Grafbee91162016-03-04 01:09:59 +01001225 struct efi_loaded_image *info;
1226 struct efi_object *obj;
1227
1228 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1229 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001230
1231 info = calloc(1, sizeof(*info));
1232 obj = calloc(1, sizeof(*obj));
1233
1234 if (!source_buffer) {
1235 struct efi_device_path *dp, *fp;
1236 efi_status_t ret;
1237
Rob Clark9975fe92017-09-13 18:05:38 -04001238 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark838ee4b2017-09-13 18:05:35 -04001239 if (ret != EFI_SUCCESS) {
1240 free(info);
1241 free(obj);
1242 return EFI_EXIT(ret);
1243 }
1244
1245 /*
1246 * split file_path which contains both the device and
1247 * file parts:
1248 */
1249 efi_dp_split_file_path(file_path, &dp, &fp);
1250
1251 efi_setup_loaded_image(info, obj, dp, fp);
1252 } else {
1253 /* In this case, file_path is the "device" path, ie.
1254 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1255 */
1256 efi_setup_loaded_image(info, obj, file_path, NULL);
1257 }
1258
Alexander Grafbee91162016-03-04 01:09:59 +01001259 info->reserved = efi_load_pe(source_buffer, info);
1260 if (!info->reserved) {
1261 free(info);
1262 free(obj);
1263 return EFI_EXIT(EFI_UNSUPPORTED);
1264 }
1265
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001266 info->system_table = &systab;
1267 info->parent_handle = parent_image;
Alexander Grafbee91162016-03-04 01:09:59 +01001268 *image_handle = info;
Alexander Grafbee91162016-03-04 01:09:59 +01001269
1270 return EFI_EXIT(EFI_SUCCESS);
1271}
1272
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001273/*
1274 * Call the entry point of an image.
1275 *
1276 * This function implements the StartImage service.
1277 * See the Unified Extensible Firmware Interface (UEFI) specification
1278 * for details.
1279 *
1280 * @image_handle handle of the image
1281 * @exit_data_size size of the buffer
1282 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001283 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001284 */
Alexander Grafbee91162016-03-04 01:09:59 +01001285static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1286 unsigned long *exit_data_size,
1287 s16 **exit_data)
1288{
1289 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1290 struct efi_loaded_image *info = image_handle;
1291
1292 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1293 entry = info->reserved;
1294
1295 efi_is_direct_boot = false;
1296
1297 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001298 if (setjmp(&info->exit_jmp)) {
1299 /* We returned from the child image */
1300 return EFI_EXIT(info->exit_status);
1301 }
1302
Rob Clarkaf65db82017-07-27 08:04:19 -04001303 __efi_nesting_dec();
Rob Clarkc160d2f2017-07-27 08:04:18 -04001304 __efi_exit_check();
Alexander Grafbee91162016-03-04 01:09:59 +01001305 entry(image_handle, &systab);
Rob Clarkc160d2f2017-07-27 08:04:18 -04001306 __efi_entry_check();
Rob Clarkaf65db82017-07-27 08:04:19 -04001307 __efi_nesting_inc();
Alexander Grafbee91162016-03-04 01:09:59 +01001308
1309 /* Should usually never get here */
1310 return EFI_EXIT(EFI_SUCCESS);
1311}
1312
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001313/*
1314 * Leave an EFI application or driver.
1315 *
1316 * This function implements the Exit service.
1317 * See the Unified Extensible Firmware Interface (UEFI) specification
1318 * for details.
1319 *
1320 * @image_handle handle of the application or driver that is exiting
1321 * @exit_status status code
1322 * @exit_data_size size of the buffer in bytes
1323 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001324 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001325 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001326static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1327 efi_status_t exit_status, unsigned long exit_data_size,
1328 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001329{
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001330 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1331
Alexander Grafbee91162016-03-04 01:09:59 +01001332 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1333 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001334
Alexander Grafa1489202017-09-03 14:14:17 +02001335 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001336 __efi_exit_check();
1337
Alexander Grafa1489202017-09-03 14:14:17 +02001338 /*
1339 * But longjmp out with the U-Boot gd, not the application's, as
1340 * the other end is a setjmp call inside EFI context.
1341 */
1342 efi_restore_gd();
1343
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001344 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001345 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001346
1347 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001348}
1349
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001350/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001351 * Unload an EFI image.
1352 *
1353 * This function implements the UnloadImage service.
1354 * See the Unified Extensible Firmware Interface (UEFI) specification
1355 * for details.
1356 *
1357 * @image_handle handle of the image to be unloaded
1358 * @return status code
1359 */
Alexander Grafbee91162016-03-04 01:09:59 +01001360static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1361{
1362 struct efi_object *efiobj;
1363
1364 EFI_ENTRY("%p", image_handle);
1365 efiobj = efi_search_obj(image_handle);
1366 if (efiobj)
1367 list_del(&efiobj->link);
1368
1369 return EFI_EXIT(EFI_SUCCESS);
1370}
1371
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001372/*
1373 * Fix up caches for EFI payloads if necessary.
1374 */
Alexander Grafbee91162016-03-04 01:09:59 +01001375static void efi_exit_caches(void)
1376{
1377#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1378 /*
1379 * Grub on 32bit ARM needs to have caches disabled before jumping into
1380 * a zImage, but does not know of all cache layers. Give it a hand.
1381 */
1382 if (efi_is_direct_boot)
1383 cleanup_before_linux();
1384#endif
1385}
1386
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001387/*
1388 * Stop boot services.
1389 *
1390 * This function implements the ExitBootServices service.
1391 * See the Unified Extensible Firmware Interface (UEFI) specification
1392 * for details.
1393 *
1394 * @image_handle handle of the loaded image
1395 * @map_key key of the memory map
1396 * @return status code
1397 */
Alexander Grafbee91162016-03-04 01:09:59 +01001398static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1399 unsigned long map_key)
1400{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001401 int i;
1402
Alexander Grafbee91162016-03-04 01:09:59 +01001403 EFI_ENTRY("%p, %ld", image_handle, map_key);
1404
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001405 /* Notify that ExitBootServices is invoked. */
1406 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1407 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1408 continue;
1409 efi_signal_event(&efi_events[i]);
1410 }
1411 /* Make sure that notification functions are not called anymore */
1412 efi_tpl = TPL_HIGH_LEVEL;
1413
Alexander Graf40583562017-10-19 23:23:50 +02001414 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001415
Alexander Grafb7b84102016-11-17 01:02:57 +01001416 board_quiesce_devices();
1417
Alexander Grafbee91162016-03-04 01:09:59 +01001418 /* Fix up caches for EFI payloads if necessary */
1419 efi_exit_caches();
1420
1421 /* This stops all lingering devices */
1422 bootm_disable_interrupts();
1423
1424 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001425 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001426 WATCHDOG_RESET();
1427
1428 return EFI_EXIT(EFI_SUCCESS);
1429}
1430
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001431/*
1432 * Get next value of the counter.
1433 *
1434 * This function implements the NextMonotonicCount service.
1435 * See the Unified Extensible Firmware Interface (UEFI) specification
1436 * for details.
1437 *
1438 * @count returned value of the counter
1439 * @return status code
1440 */
Alexander Grafbee91162016-03-04 01:09:59 +01001441static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1442{
1443 static uint64_t mono = 0;
1444 EFI_ENTRY("%p", count);
1445 *count = mono++;
1446 return EFI_EXIT(EFI_SUCCESS);
1447}
1448
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001449/*
1450 * Sleep.
1451 *
1452 * This function implements the Stall sercive.
1453 * See the Unified Extensible Firmware Interface (UEFI) specification
1454 * for details.
1455 *
1456 * @microseconds period to sleep in microseconds
1457 * @return status code
1458 */
Alexander Grafbee91162016-03-04 01:09:59 +01001459static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1460{
1461 EFI_ENTRY("%ld", microseconds);
1462 udelay(microseconds);
1463 return EFI_EXIT(EFI_SUCCESS);
1464}
1465
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001466/*
1467 * Reset the watchdog timer.
1468 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001469 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001470 * See the Unified Extensible Firmware Interface (UEFI) specification
1471 * for details.
1472 *
1473 * @timeout seconds before reset by watchdog
1474 * @watchdog_code code to be logged when resetting
1475 * @data_size size of buffer in bytes
1476 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001477 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001478 */
Alexander Grafbee91162016-03-04 01:09:59 +01001479static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1480 uint64_t watchdog_code,
1481 unsigned long data_size,
1482 uint16_t *watchdog_data)
1483{
1484 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1485 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001486 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001487}
1488
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001489/*
1490 * Connect a controller to a driver.
1491 *
1492 * This function implements the ConnectController service.
1493 * See the Unified Extensible Firmware Interface (UEFI) specification
1494 * for details.
1495 *
1496 * @controller_handle handle of the controller
1497 * @driver_image_handle handle of the driver
1498 * @remain_device_path device path of a child controller
1499 * @recursive true to connect all child controllers
1500 * @return status code
1501 */
Alexander Grafbee91162016-03-04 01:09:59 +01001502static efi_status_t EFIAPI efi_connect_controller(
1503 efi_handle_t controller_handle,
1504 efi_handle_t *driver_image_handle,
1505 struct efi_device_path *remain_device_path,
1506 bool recursive)
1507{
1508 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1509 remain_device_path, recursive);
1510 return EFI_EXIT(EFI_NOT_FOUND);
1511}
1512
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001513/*
1514 * Disconnect a controller from a driver.
1515 *
1516 * This function implements the DisconnectController service.
1517 * See the Unified Extensible Firmware Interface (UEFI) specification
1518 * for details.
1519 *
1520 * @controller_handle handle of the controller
1521 * @driver_image_handle handle of the driver
1522 * @child_handle handle of the child to destroy
1523 * @return status code
1524 */
Alexander Grafbee91162016-03-04 01:09:59 +01001525static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1526 void *driver_image_handle,
1527 void *child_handle)
1528{
1529 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1530 child_handle);
1531 return EFI_EXIT(EFI_INVALID_PARAMETER);
1532}
1533
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001534/*
1535 * Close a protocol.
1536 *
1537 * This function implements the CloseProtocol service.
1538 * See the Unified Extensible Firmware Interface (UEFI) specification
1539 * for details.
1540 *
1541 * @handle handle on which the protocol shall be closed
1542 * @protocol GUID of the protocol to close
1543 * @agent_handle handle of the driver
1544 * @controller_handle handle of the controller
1545 * @return status code
1546 */
Alexander Grafbee91162016-03-04 01:09:59 +01001547static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001548 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001549 void *agent_handle,
1550 void *controller_handle)
1551{
Rob Clark778e6af2017-09-13 18:05:41 -04001552 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001553 controller_handle);
1554 return EFI_EXIT(EFI_NOT_FOUND);
1555}
1556
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001557/*
1558 * Provide information about then open status of a protocol on a handle
1559 *
1560 * This function implements the OpenProtocolInformation service.
1561 * See the Unified Extensible Firmware Interface (UEFI) specification
1562 * for details.
1563 *
1564 * @handle handle for which the information shall be retrieved
1565 * @protocol GUID of the protocol
1566 * @entry_buffer buffer to receive the open protocol information
1567 * @entry_count number of entries available in the buffer
1568 * @return status code
1569 */
Alexander Grafbee91162016-03-04 01:09:59 +01001570static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001571 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001572 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001573 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001574{
Rob Clark778e6af2017-09-13 18:05:41 -04001575 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001576 entry_count);
1577 return EFI_EXIT(EFI_NOT_FOUND);
1578}
1579
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001580/*
1581 * Get protocols installed on a handle.
1582 *
1583 * This function implements the ProtocolsPerHandleService.
1584 * See the Unified Extensible Firmware Interface (UEFI) specification
1585 * for details.
1586 *
1587 * @handle handle for which the information is retrieved
1588 * @protocol_buffer buffer with protocol GUIDs
1589 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001590 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001591 */
Alexander Grafbee91162016-03-04 01:09:59 +01001592static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1593 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001594 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001595{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001596 unsigned long buffer_size;
1597 struct efi_object *efiobj;
1598 unsigned long i, j;
1599 struct list_head *lhandle;
1600 efi_status_t r;
1601
Alexander Grafbee91162016-03-04 01:09:59 +01001602 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1603 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001604
1605 if (!handle || !protocol_buffer || !protocol_buffer_count)
1606 return EFI_EXIT(EFI_INVALID_PARAMETER);
1607
1608 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001609 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001610 list_for_each(lhandle, &efi_obj_list) {
1611 efiobj = list_entry(lhandle, struct efi_object, link);
1612
1613 if (efiobj->handle != handle)
1614 continue;
1615
1616 /* Count protocols */
1617 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1618 if (efiobj->protocols[i].guid)
1619 ++*protocol_buffer_count;
1620 }
1621 /* Copy guids */
1622 if (*protocol_buffer_count) {
1623 buffer_size = sizeof(efi_guid_t *) *
1624 *protocol_buffer_count;
1625 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1626 buffer_size,
1627 (void **)protocol_buffer);
1628 if (r != EFI_SUCCESS)
1629 return EFI_EXIT(r);
1630 j = 0;
1631 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1632 if (efiobj->protocols[i].guid) {
1633 (*protocol_buffer)[j] = (void *)
1634 efiobj->protocols[i].guid;
1635 ++j;
1636 }
1637 }
1638 }
1639 break;
1640 }
1641
1642 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001643}
1644
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001645/*
1646 * Locate handles implementing a protocol.
1647 *
1648 * This function implements the LocateHandleBuffer service.
1649 * See the Unified Extensible Firmware Interface (UEFI) specification
1650 * for details.
1651 *
1652 * @search_type selection criterion
1653 * @protocol GUID of the protocol
1654 * @search_key registration key
1655 * @no_handles number of returned handles
1656 * @buffer buffer with the returned handles
1657 * @return status code
1658 */
Alexander Grafbee91162016-03-04 01:09:59 +01001659static efi_status_t EFIAPI efi_locate_handle_buffer(
1660 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001661 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001662 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001663{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001664 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001665 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001666
Rob Clark778e6af2017-09-13 18:05:41 -04001667 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001668 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001669
1670 if (!no_handles || !buffer) {
1671 r = EFI_INVALID_PARAMETER;
1672 goto out;
1673 }
1674 *no_handles = 0;
1675 *buffer = NULL;
1676 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1677 *buffer);
1678 if (r != EFI_BUFFER_TOO_SMALL)
1679 goto out;
1680 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1681 (void **)buffer);
1682 if (r != EFI_SUCCESS)
1683 goto out;
1684 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1685 *buffer);
1686 if (r == EFI_SUCCESS)
1687 *no_handles = buffer_size / sizeof(void *);
1688out:
1689 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001690}
1691
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001692/*
1693 * Find an interface implementing a protocol.
1694 *
1695 * This function implements the LocateProtocol service.
1696 * See the Unified Extensible Firmware Interface (UEFI) specification
1697 * for details.
1698 *
1699 * @protocol GUID of the protocol
1700 * @registration registration key passed to the notification function
1701 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001702 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001703 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001704static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001705 void *registration,
1706 void **protocol_interface)
1707{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001708 struct list_head *lhandle;
Alexander Grafbee91162016-03-04 01:09:59 +01001709 int i;
1710
Rob Clark778e6af2017-09-13 18:05:41 -04001711 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001712
1713 if (!protocol || !protocol_interface)
1714 return EFI_EXIT(EFI_INVALID_PARAMETER);
1715
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +02001716 EFI_PRINT_GUID("protocol", protocol);
1717
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001718 list_for_each(lhandle, &efi_obj_list) {
1719 struct efi_object *efiobj;
1720
1721 efiobj = list_entry(lhandle, struct efi_object, link);
1722 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1723 struct efi_handler *handler = &efiobj->protocols[i];
1724
1725 if (!handler->guid)
1726 continue;
1727 if (!guidcmp(handler->guid, protocol)) {
1728 *protocol_interface =
1729 handler->protocol_interface;
1730 return EFI_EXIT(EFI_SUCCESS);
1731 }
Alexander Grafbee91162016-03-04 01:09:59 +01001732 }
1733 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001734 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001735
1736 return EFI_EXIT(EFI_NOT_FOUND);
1737}
1738
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001739/*
1740 * Install multiple protocol interfaces.
1741 *
1742 * This function implements the MultipleProtocolInterfaces service.
1743 * See the Unified Extensible Firmware Interface (UEFI) specification
1744 * for details.
1745 *
1746 * @handle handle on which the protocol interfaces shall be installed
1747 * @... NULL terminated argument list with pairs of protocol GUIDS and
1748 * interfaces
1749 * @return status code
1750 */
Alexander Grafbee91162016-03-04 01:09:59 +01001751static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1752 void **handle, ...)
1753{
1754 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001755
1756 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001757 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001758 void *protocol_interface;
1759 efi_status_t r = EFI_SUCCESS;
1760 int i = 0;
1761
1762 if (!handle)
1763 return EFI_EXIT(EFI_INVALID_PARAMETER);
1764
1765 va_start(argptr, handle);
1766 for (;;) {
1767 protocol = va_arg(argptr, efi_guid_t*);
1768 if (!protocol)
1769 break;
1770 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001771 r = EFI_CALL(efi_install_protocol_interface(
1772 handle, protocol,
1773 EFI_NATIVE_INTERFACE,
1774 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001775 if (r != EFI_SUCCESS)
1776 break;
1777 i++;
1778 }
1779 va_end(argptr);
1780 if (r == EFI_SUCCESS)
1781 return EFI_EXIT(r);
1782
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02001783 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001784 va_start(argptr, handle);
1785 for (; i; --i) {
1786 protocol = va_arg(argptr, efi_guid_t*);
1787 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001788 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1789 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001790 }
1791 va_end(argptr);
1792
1793 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001794}
1795
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001796/*
1797 * Uninstall multiple protocol interfaces.
1798 *
1799 * This function implements the UninstallMultipleProtocolInterfaces service.
1800 * See the Unified Extensible Firmware Interface (UEFI) specification
1801 * for details.
1802 *
1803 * @handle handle from which the protocol interfaces shall be removed
1804 * @... NULL terminated argument list with pairs of protocol GUIDS and
1805 * interfaces
1806 * @return status code
1807 */
Alexander Grafbee91162016-03-04 01:09:59 +01001808static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1809 void *handle, ...)
1810{
1811 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02001812
1813 va_list argptr;
1814 const efi_guid_t *protocol;
1815 void *protocol_interface;
1816 efi_status_t r = EFI_SUCCESS;
1817 size_t i = 0;
1818
1819 if (!handle)
1820 return EFI_EXIT(EFI_INVALID_PARAMETER);
1821
1822 va_start(argptr, handle);
1823 for (;;) {
1824 protocol = va_arg(argptr, efi_guid_t*);
1825 if (!protocol)
1826 break;
1827 protocol_interface = va_arg(argptr, void*);
1828 r = EFI_CALL(efi_uninstall_protocol_interface(
1829 handle, protocol,
1830 protocol_interface));
1831 if (r != EFI_SUCCESS)
1832 break;
1833 i++;
1834 }
1835 va_end(argptr);
1836 if (r == EFI_SUCCESS)
1837 return EFI_EXIT(r);
1838
1839 /* If an error occurred undo all changes. */
1840 va_start(argptr, handle);
1841 for (; i; --i) {
1842 protocol = va_arg(argptr, efi_guid_t*);
1843 protocol_interface = va_arg(argptr, void*);
1844 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1845 EFI_NATIVE_INTERFACE,
1846 protocol_interface));
1847 }
1848 va_end(argptr);
1849
1850 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001851}
1852
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001853/*
1854 * Calculate cyclic redundancy code.
1855 *
1856 * This function implements the CalculateCrc32 service.
1857 * See the Unified Extensible Firmware Interface (UEFI) specification
1858 * for details.
1859 *
1860 * @data buffer with data
1861 * @data_size size of buffer in bytes
1862 * @crc32_p cyclic redundancy code
1863 * @return status code
1864 */
Alexander Grafbee91162016-03-04 01:09:59 +01001865static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1866 unsigned long data_size,
1867 uint32_t *crc32_p)
1868{
1869 EFI_ENTRY("%p, %ld", data, data_size);
1870 *crc32_p = crc32(0, data, data_size);
1871 return EFI_EXIT(EFI_SUCCESS);
1872}
1873
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001874/*
1875 * Copy memory.
1876 *
1877 * This function implements the CopyMem service.
1878 * See the Unified Extensible Firmware Interface (UEFI) specification
1879 * for details.
1880 *
1881 * @destination destination of the copy operation
1882 * @source source of the copy operation
1883 * @length number of bytes to copy
1884 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02001885static void EFIAPI efi_copy_mem(void *destination, const void *source,
1886 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01001887{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02001888 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01001889 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02001890 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001891}
1892
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001893/*
1894 * Fill memory with a byte value.
1895 *
1896 * This function implements the SetMem service.
1897 * See the Unified Extensible Firmware Interface (UEFI) specification
1898 * for details.
1899 *
1900 * @buffer buffer to fill
1901 * @size size of buffer in bytes
1902 * @value byte to copy to the buffer
1903 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02001904static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01001905{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02001906 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01001907 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02001908 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001909}
1910
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001911/*
1912 * Open protocol interface on a handle.
1913 *
1914 * This function implements the OpenProtocol interface.
1915 * See the Unified Extensible Firmware Interface (UEFI) specification
1916 * for details.
1917 *
1918 * @handle handle on which the protocol shall be opened
1919 * @protocol GUID of the protocol
1920 * @protocol_interface interface implementing the protocol
1921 * @agent_handle handle of the driver
1922 * @controller_handle handle of the controller
1923 * @attributes attributes indicating how to open the protocol
1924 * @return status code
1925 */
Alexander Grafbee91162016-03-04 01:09:59 +01001926static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001927 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001928 void **protocol_interface, void *agent_handle,
1929 void *controller_handle, uint32_t attributes)
1930{
1931 struct list_head *lhandle;
1932 int i;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02001933 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01001934
Rob Clark778e6af2017-09-13 18:05:41 -04001935 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001936 protocol_interface, agent_handle, controller_handle,
1937 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02001938
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02001939 if (!handle || !protocol ||
1940 (!protocol_interface && attributes !=
1941 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1942 goto out;
1943 }
1944
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +02001945 EFI_PRINT_GUID("protocol", protocol);
1946
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02001947 switch (attributes) {
1948 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1949 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1950 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1951 break;
1952 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1953 if (controller_handle == handle)
1954 goto out;
1955 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1956 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1957 if (controller_handle == NULL)
1958 goto out;
1959 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1960 if (agent_handle == NULL)
1961 goto out;
1962 break;
1963 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02001964 goto out;
1965 }
1966
Alexander Grafbee91162016-03-04 01:09:59 +01001967 list_for_each(lhandle, &efi_obj_list) {
1968 struct efi_object *efiobj;
1969 efiobj = list_entry(lhandle, struct efi_object, link);
1970
1971 if (efiobj->handle != handle)
1972 continue;
1973
1974 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1975 struct efi_handler *handler = &efiobj->protocols[i];
1976 const efi_guid_t *hprotocol = handler->guid;
1977 if (!hprotocol)
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001978 continue;
Alexander Grafbee91162016-03-04 01:09:59 +01001979 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02001980 if (attributes !=
1981 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1982 *protocol_interface =
1983 handler->protocol_interface;
1984 }
1985 r = EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001986 goto out;
1987 }
1988 }
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02001989 goto unsupported;
Alexander Grafbee91162016-03-04 01:09:59 +01001990 }
1991
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02001992unsupported:
1993 r = EFI_UNSUPPORTED;
Alexander Grafbee91162016-03-04 01:09:59 +01001994out:
1995 return EFI_EXIT(r);
1996}
1997
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001998/*
1999 * Get interface of a protocol on a handle.
2000 *
2001 * This function implements the HandleProtocol service.
2002 * See the Unified Extensible Firmware Interface (UEFI) specification
2003 * for details.
2004 *
2005 * @handle handle on which the protocol shall be opened
2006 * @protocol GUID of the protocol
2007 * @protocol_interface interface implementing the protocol
2008 * @return status code
2009 */
Alexander Grafbee91162016-03-04 01:09:59 +01002010static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002011 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002012 void **protocol_interface)
2013{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002014 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2015 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002016}
2017
2018static const struct efi_boot_services efi_boot_services = {
2019 .hdr = {
2020 .headersize = sizeof(struct efi_table_hdr),
2021 },
2022 .raise_tpl = efi_raise_tpl,
2023 .restore_tpl = efi_restore_tpl,
2024 .allocate_pages = efi_allocate_pages_ext,
2025 .free_pages = efi_free_pages_ext,
2026 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002027 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002028 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002029 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002030 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002031 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002032 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002033 .close_event = efi_close_event,
2034 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002035 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002036 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002037 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002038 .handle_protocol = efi_handle_protocol,
2039 .reserved = NULL,
2040 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002041 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002042 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002043 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002044 .load_image = efi_load_image,
2045 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002046 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002047 .unload_image = efi_unload_image,
2048 .exit_boot_services = efi_exit_boot_services,
2049 .get_next_monotonic_count = efi_get_next_monotonic_count,
2050 .stall = efi_stall,
2051 .set_watchdog_timer = efi_set_watchdog_timer,
2052 .connect_controller = efi_connect_controller,
2053 .disconnect_controller = efi_disconnect_controller,
2054 .open_protocol = efi_open_protocol,
2055 .close_protocol = efi_close_protocol,
2056 .open_protocol_information = efi_open_protocol_information,
2057 .protocols_per_handle = efi_protocols_per_handle,
2058 .locate_handle_buffer = efi_locate_handle_buffer,
2059 .locate_protocol = efi_locate_protocol,
2060 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2061 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2062 .calculate_crc32 = efi_calculate_crc32,
2063 .copy_mem = efi_copy_mem,
2064 .set_mem = efi_set_mem,
2065};
2066
2067
Alexander Graf3c63db92016-10-14 13:45:30 +02002068static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafbee91162016-03-04 01:09:59 +01002069 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2070
Alexander Graf3c63db92016-10-14 13:45:30 +02002071struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002072 .hdr = {
2073 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2074 .revision = 0x20005, /* 2.5 */
2075 .headersize = sizeof(struct efi_table_hdr),
2076 },
2077 .fw_vendor = (long)firmware_vendor,
2078 .con_in = (void*)&efi_con_in,
2079 .con_out = (void*)&efi_con_out,
2080 .std_err = (void*)&efi_con_out,
2081 .runtime = (void*)&efi_runtime_services,
2082 .boottime = (void*)&efi_boot_services,
2083 .nr_tables = 0,
2084 .tables = (void*)efi_conf_table,
2085};