blob: 0fb2848ecf9a70b6b8f0eaff472106ca695f2526 [file] [log] [blame]
Alexander Grafbee91162016-03-04 01:09:59 +01001/*
2 * EFI application boot time services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Alexander Grafbee91162016-03-04 01:09:59 +01009#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +020010#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010011#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040012#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010013#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <u-boot/crc.h>
17#include <bootm.h>
18#include <inttypes.h>
19#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020025
Alexander Grafbee91162016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
29/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
37/*
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
41 *
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
44 */
Alexander Graf3c63db92016-10-14 13:45:30 +020045static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafbee91162016-03-04 01:09:59 +010046
Simon Glass65e4c0b2016-09-25 15:27:35 -060047#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010048/*
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
53 */
54static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060055#endif
Alexander Grafbee91162016-03-04 01:09:59 +010056
Rob Clarkc160d2f2017-07-27 08:04:18 -040057static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040058static int nesting_level;
Rob Clarkc160d2f2017-07-27 08:04:18 -040059
60/* Called on every callback entry */
61int __efi_entry_check(void)
62{
63 int ret = entry_count++ == 0;
64#ifdef CONFIG_ARM
65 assert(efi_gd);
66 app_gd = gd;
67 gd = efi_gd;
68#endif
69 return ret;
70}
71
72/* Called on every callback exit */
73int __efi_exit_check(void)
74{
75 int ret = --entry_count == 0;
76#ifdef CONFIG_ARM
77 gd = app_gd;
78#endif
79 return ret;
80}
81
Alexander Grafbee91162016-03-04 01:09:59 +010082/* Called from do_bootefi_exec() */
83void efi_save_gd(void)
84{
Simon Glass65e4c0b2016-09-25 15:27:35 -060085#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010086 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060087#endif
Alexander Grafbee91162016-03-04 01:09:59 +010088}
89
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/*
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
94 */
Alexander Grafbee91162016-03-04 01:09:59 +010095void efi_restore_gd(void)
96{
Simon Glass65e4c0b2016-09-25 15:27:35 -060097#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010098 /* Only restore if we're already in EFI context */
99 if (!efi_gd)
100 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100101 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600102#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100103}
104
Rob Clarkaf65db82017-07-27 08:04:19 -0400105/*
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
108 */
109static const char *indent_string(int level)
110{
111 const char *indent = " ";
112 const int max = strlen(indent);
113 level = min(max, level * 2);
114 return &indent[max - level];
115}
116
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200117const char *__efi_nesting(void)
118{
119 return indent_string(nesting_level);
120}
121
Rob Clarkaf65db82017-07-27 08:04:19 -0400122const char *__efi_nesting_inc(void)
123{
124 return indent_string(nesting_level++);
125}
126
127const char *__efi_nesting_dec(void)
128{
129 return indent_string(--nesting_level);
130}
131
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200132/*
133 * Queue an EFI event.
134 *
135 * This function queues the notification function of the event for future
136 * execution.
137 *
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
140 *
141 * For the SignalEvent service see efi_signal_event_ext.
142 *
143 * @event event to signal
144 */
xypron.glpk@gmx.de91be9a72017-07-18 20:17:22 +0200145void efi_signal_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200146{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200147 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200148 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200149 /* Check TPL */
150 if (efi_tpl >= event->notify_tpl)
151 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200152 EFI_CALL_VOID(event->notify_function(event,
153 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200154 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200155 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200156}
157
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200158/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 * Raise the task priority level.
160 *
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
163 * for details.
164 *
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
167 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100168static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100169{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100170 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200171
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200172 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173
174 if (new_tpl < efi_tpl)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 efi_tpl = new_tpl;
177 if (efi_tpl > TPL_HIGH_LEVEL)
178 efi_tpl = TPL_HIGH_LEVEL;
179
180 EFI_EXIT(EFI_SUCCESS);
181 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100182}
183
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200184/*
185 * Lower the task priority level.
186 *
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
189 * for details.
190 *
191 * @old_tpl value of the task priority level to be restored
192 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100193static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100194{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200195 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200196
197 if (old_tpl > efi_tpl)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 efi_tpl = old_tpl;
200 if (efi_tpl > TPL_HIGH_LEVEL)
201 efi_tpl = TPL_HIGH_LEVEL;
202
203 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100204}
205
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200206/*
207 * Allocate memory pages.
208 *
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
211 * for details.
212 *
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
218 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900219static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100220 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900221 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100222{
223 efi_status_t r;
224
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100225 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100226 r = efi_allocate_pages(type, memory_type, pages, memory);
227 return EFI_EXIT(r);
228}
229
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200230/*
231 * Free memory pages.
232 *
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
235 * for details.
236 *
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
240 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900241static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100242 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100243{
244 efi_status_t r;
245
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100246 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100247 r = efi_free_pages(memory, pages);
248 return EFI_EXIT(r);
249}
250
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200251/*
252 * Get map describing memory usage.
253 *
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
256 * for details.
257 *
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
265 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900266static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100267 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900268 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100269 efi_uintn_t *map_key,
270 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900271 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100272{
273 efi_status_t r;
274
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 map_key, descriptor_size, descriptor_version);
277 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 descriptor_size, descriptor_version);
279 return EFI_EXIT(r);
280}
281
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200282/*
283 * Allocate memory from pool.
284 *
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
287 * for details.
288 *
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
293 */
Stefan Brünsead12742016-10-09 22:17:18 +0200294static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100295 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200296 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100297{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100298 efi_status_t r;
299
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100300 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200301 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100302 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100303}
304
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200305/*
306 * Free memory from pool.
307 *
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @buffer start of memory to be freed
313 * @return status code
314 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200315static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100316{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100317 efi_status_t r;
318
319 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200320 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100321 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100322}
323
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200324/*
325 * Create handle.
326 *
327 * @handle new handle
328 * @return status code
329 */
330efi_status_t efi_create_handle(void **handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200331{
332 struct efi_object *obj;
333 efi_status_t r;
334
335 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
336 sizeof(struct efi_object),
337 (void **)&obj);
338 if (r != EFI_SUCCESS)
339 return r;
340 memset(obj, 0, sizeof(struct efi_object));
341 obj->handle = obj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100342 INIT_LIST_HEAD(&obj->protocols);
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200343 list_add_tail(&obj->link, &efi_obj_list);
344 *handle = obj;
345 return r;
346}
347
Alexander Grafbee91162016-03-04 01:09:59 +0100348/*
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200349 * Our event capabilities are very limited. Only a small limited
350 * number of events is allowed to coexist.
Alexander Grafbee91162016-03-04 01:09:59 +0100351 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200352static struct efi_event efi_events[16];
Alexander Grafbee91162016-03-04 01:09:59 +0100353
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200354/*
355 * Create an event.
356 *
357 * This function is used inside U-Boot code to create an event.
358 *
359 * For the API function implementing the CreateEvent service see
360 * efi_create_event_ext.
361 *
362 * @type type of the event to create
363 * @notify_tpl task priority level of the event
364 * @notify_function notification function of the event
365 * @notify_context pointer passed to the notification function
366 * @event created event
367 * @return status code
368 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100369efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200370 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200371 struct efi_event *event,
372 void *context),
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200373 void *notify_context, struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100374{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200375 int i;
376
Jonathan Graya95343b2017-03-12 19:26:07 +1100377 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200378 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100379
380 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200381 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100382
383 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
384 notify_function == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200385 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100386
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200387 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
388 if (efi_events[i].type)
389 continue;
390 efi_events[i].type = type;
391 efi_events[i].notify_tpl = notify_tpl;
392 efi_events[i].notify_function = notify_function;
393 efi_events[i].notify_context = notify_context;
394 /* Disable timers on bootup */
395 efi_events[i].trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200396 efi_events[i].is_queued = false;
397 efi_events[i].is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200398 *event = &efi_events[i];
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200399 return EFI_SUCCESS;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200400 }
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200401 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100402}
403
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200404/*
405 * Create an event.
406 *
407 * This function implements the CreateEvent service.
408 * See the Unified Extensible Firmware Interface (UEFI) specification
409 * for details.
410 *
411 * @type type of the event to create
412 * @notify_tpl task priority level of the event
413 * @notify_function notification function of the event
414 * @notify_context pointer passed to the notification function
415 * @event created event
416 * @return status code
417 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200418static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100419 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200420 void (EFIAPI *notify_function) (
421 struct efi_event *event,
422 void *context),
423 void *notify_context, struct efi_event **event)
424{
425 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
426 notify_context);
427 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
428 notify_context, event));
429}
430
431
Alexander Grafbee91162016-03-04 01:09:59 +0100432/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200433 * Check if a timer event has occurred or a queued notification function should
434 * be called.
435 *
Alexander Grafbee91162016-03-04 01:09:59 +0100436 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200437 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100438 */
439void efi_timer_check(void)
440{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200441 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100442 u64 now = timer_get_us();
443
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200444 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200445 if (!efi_events[i].type)
446 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200447 if (efi_events[i].is_queued)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200448 efi_signal_event(&efi_events[i]);
449 if (!(efi_events[i].type & EVT_TIMER) ||
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200450 now < efi_events[i].trigger_next)
451 continue;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200452 switch (efi_events[i].trigger_type) {
453 case EFI_TIMER_RELATIVE:
454 efi_events[i].trigger_type = EFI_TIMER_STOP;
455 break;
456 case EFI_TIMER_PERIODIC:
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200457 efi_events[i].trigger_next +=
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200458 efi_events[i].trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200459 break;
460 default:
461 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200462 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200463 efi_events[i].is_signaled = true;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200464 efi_signal_event(&efi_events[i]);
Alexander Grafbee91162016-03-04 01:09:59 +0100465 }
Alexander Grafbee91162016-03-04 01:09:59 +0100466 WATCHDOG_RESET();
467}
468
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200469/*
470 * Set the trigger time for a timer event or stop the event.
471 *
472 * This is the function for internal usage in U-Boot. For the API function
473 * implementing the SetTimer service see efi_set_timer_ext.
474 *
475 * @event event for which the timer is set
476 * @type type of the timer
477 * @trigger_time trigger period in multiples of 100ns
478 * @return status code
479 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200480efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200481 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100482{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200483 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100484
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200485 /*
486 * The parameter defines a multiple of 100ns.
487 * We use multiples of 1000ns. So divide by 10.
488 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200489 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100490
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200491 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
492 if (event != &efi_events[i])
493 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100494
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200495 if (!(event->type & EVT_TIMER))
496 break;
497 switch (type) {
498 case EFI_TIMER_STOP:
499 event->trigger_next = -1ULL;
500 break;
501 case EFI_TIMER_PERIODIC:
502 case EFI_TIMER_RELATIVE:
503 event->trigger_next =
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200504 timer_get_us() + trigger_time;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200505 break;
506 default:
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200507 return EFI_INVALID_PARAMETER;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200508 }
509 event->trigger_type = type;
510 event->trigger_time = trigger_time;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200511 event->is_signaled = false;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200512 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100513 }
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200514 return EFI_INVALID_PARAMETER;
515}
516
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200517/*
518 * Set the trigger time for a timer event or stop the event.
519 *
520 * This function implements the SetTimer service.
521 * See the Unified Extensible Firmware Interface (UEFI) specification
522 * for details.
523 *
524 * @event event for which the timer is set
525 * @type type of the timer
526 * @trigger_time trigger period in multiples of 100ns
527 * @return status code
528 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200529static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
530 enum efi_timer_delay type,
531 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200532{
533 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
534 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100535}
536
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200537/*
538 * Wait for events to be signaled.
539 *
540 * This function implements the WaitForEvent service.
541 * See the Unified Extensible Firmware Interface (UEFI) specification
542 * for details.
543 *
544 * @num_events number of events to be waited for
545 * @events events to be waited for
546 * @index index of the event that was signaled
547 * @return status code
548 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100549static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200550 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100551 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100552{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200553 int i, j;
Alexander Grafbee91162016-03-04 01:09:59 +0100554
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100555 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100556
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200557 /* Check parameters */
558 if (!num_events || !event)
559 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200560 /* Check TPL */
561 if (efi_tpl != TPL_APPLICATION)
562 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200563 for (i = 0; i < num_events; ++i) {
564 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
565 if (event[i] == &efi_events[j])
566 goto known_event;
567 }
568 return EFI_EXIT(EFI_INVALID_PARAMETER);
569known_event:
570 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
571 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200572 if (!event[i]->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200573 efi_signal_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200574 }
575
576 /* Wait for signal */
577 for (;;) {
578 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200579 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200580 goto out;
581 }
582 /* Allow events to occur. */
583 efi_timer_check();
584 }
585
586out:
587 /*
588 * Reset the signal which is passed to the caller to allow periodic
589 * events to occur.
590 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200591 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200592 if (index)
593 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100594
595 return EFI_EXIT(EFI_SUCCESS);
596}
597
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200598/*
599 * Signal an EFI event.
600 *
601 * This function implements the SignalEvent service.
602 * See the Unified Extensible Firmware Interface (UEFI) specification
603 * for details.
604 *
605 * This functions sets the signaled state of the event and queues the
606 * notification function for execution.
607 *
608 * @event event to signal
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +0200609 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200610 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200611static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100612{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200613 int i;
614
Alexander Grafbee91162016-03-04 01:09:59 +0100615 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200616 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
617 if (event != &efi_events[i])
618 continue;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200619 if (event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200620 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200621 event->is_signaled = true;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200622 if (event->type & EVT_NOTIFY_SIGNAL)
623 efi_signal_event(event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200624 break;
625 }
Alexander Grafbee91162016-03-04 01:09:59 +0100626 return EFI_EXIT(EFI_SUCCESS);
627}
628
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200629/*
630 * Close an EFI event.
631 *
632 * This function implements the CloseEvent service.
633 * See the Unified Extensible Firmware Interface (UEFI) specification
634 * for details.
635 *
636 * @event event to close
637 * @return status code
638 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200639static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100640{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200641 int i;
642
Alexander Grafbee91162016-03-04 01:09:59 +0100643 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200644 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
645 if (event == &efi_events[i]) {
646 event->type = 0;
647 event->trigger_next = -1ULL;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200648 event->is_queued = false;
649 event->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200650 return EFI_EXIT(EFI_SUCCESS);
651 }
652 }
653 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100654}
655
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200656/*
657 * Check if an event is signaled.
658 *
659 * This function implements the CheckEvent service.
660 * See the Unified Extensible Firmware Interface (UEFI) specification
661 * for details.
662 *
663 * If an event is not signaled yet the notification function is queued.
664 *
665 * @event event to check
666 * @return status code
667 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200668static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100669{
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200670 int i;
671
Alexander Grafbee91162016-03-04 01:09:59 +0100672 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200673 efi_timer_check();
674 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
675 if (event != &efi_events[i])
676 continue;
677 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
678 break;
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200679 if (!event->is_signaled)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200680 efi_signal_event(event);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200681 if (event->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200682 return EFI_EXIT(EFI_SUCCESS);
683 return EFI_EXIT(EFI_NOT_READY);
684 }
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +0100686}
687
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200688/*
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200689 * Find the internal EFI object for a handle.
690 *
691 * @handle handle to find
692 * @return EFI object
693 */
Heinrich Schuchardt085d07c2017-10-26 19:25:50 +0200694struct efi_object *efi_search_obj(const void *handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200695{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100696 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200697
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100698 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200699 if (efiobj->handle == handle)
700 return efiobj;
701 }
702
703 return NULL;
704}
705
706/*
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200707 * Find a protocol on a handle.
708 *
709 * @handle handle
710 * @protocol_guid GUID of the protocol
711 * @handler reference to the protocol
712 * @return status code
713 */
714efi_status_t efi_search_protocol(const void *handle,
715 const efi_guid_t *protocol_guid,
716 struct efi_handler **handler)
717{
718 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100719 struct list_head *lhandle;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200720
721 if (!handle || !protocol_guid)
722 return EFI_INVALID_PARAMETER;
723 efiobj = efi_search_obj(handle);
724 if (!efiobj)
725 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100726 list_for_each(lhandle, &efiobj->protocols) {
727 struct efi_handler *protocol;
728
729 protocol = list_entry(lhandle, struct efi_handler, link);
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200730 if (!guidcmp(protocol->guid, protocol_guid)) {
731 if (handler)
732 *handler = protocol;
733 return EFI_SUCCESS;
734 }
735 }
736 return EFI_NOT_FOUND;
737}
738
739/*
740 * Install new protocol on a handle.
741 *
742 * @handle handle on which the protocol shall be installed
743 * @protocol GUID of the protocol to be installed
744 * @protocol_interface interface of the protocol implementation
745 * @return status code
746 */
747efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
748 void *protocol_interface)
749{
750 struct efi_object *efiobj;
751 struct efi_handler *handler;
752 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200753
754 efiobj = efi_search_obj(handle);
755 if (!efiobj)
756 return EFI_INVALID_PARAMETER;
757 ret = efi_search_protocol(handle, protocol, NULL);
758 if (ret != EFI_NOT_FOUND)
759 return EFI_INVALID_PARAMETER;
760 handler = calloc(1, sizeof(struct efi_handler));
761 if (!handler)
762 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100763 handler->guid = protocol;
764 handler->protocol_interface = protocol_interface;
765 list_add_tail(&handler->link, &efiobj->protocols);
766 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200767}
768
769/*
770 * Delete protocol from a handle.
771 *
772 * @handle handle from which the protocol shall be deleted
773 * @protocol GUID of the protocol to be deleted
774 * @protocol_interface interface of the protocol implementation
775 * @return status code
776 */
777efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
778 void *protocol_interface)
779{
780 struct efi_handler *handler;
781 efi_status_t ret;
782
783 ret = efi_search_protocol(handle, protocol, &handler);
784 if (ret != EFI_SUCCESS)
785 return ret;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100786 if (guidcmp(handler->guid, protocol))
787 return EFI_INVALID_PARAMETER;
788 list_del(&handler->link);
789 free(handler);
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200790 return EFI_SUCCESS;
791}
792
793/*
794 * Delete all protocols from a handle.
795 *
796 * @handle handle from which the protocols shall be deleted
797 * @return status code
798 */
799efi_status_t efi_remove_all_protocols(const void *handle)
800{
801 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100802 struct list_head *lhandle;
803 struct list_head *pos;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200804
805 efiobj = efi_search_obj(handle);
806 if (!efiobj)
807 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100808 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
809 struct efi_handler *protocol;
810 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200811
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +0100812 protocol = list_entry(lhandle, struct efi_handler, link);
813
814 ret = efi_remove_protocol(handle, protocol->guid,
815 protocol->protocol_interface);
816 if (ret != EFI_SUCCESS)
817 return ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200818 }
819 return EFI_SUCCESS;
820}
821
822/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200823 * Install protocol interface.
824 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100825 * This function implements the InstallProtocolInterface service.
826 * See the Unified Extensible Firmware Interface (UEFI) specification
827 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200828 *
829 * @handle handle on which the protocol shall be installed
830 * @protocol GUID of the protocol to be installed
831 * @protocol_interface_type type of the interface to be installed,
832 * always EFI_NATIVE_INTERFACE
833 * @protocol_interface interface of the protocol implementation
834 * @return status code
835 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100836static efi_status_t EFIAPI efi_install_protocol_interface(
837 void **handle, const efi_guid_t *protocol,
838 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100839{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200840 efi_status_t r;
841
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100842 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
843 protocol_interface);
844
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200845 if (!handle || !protocol ||
846 protocol_interface_type != EFI_NATIVE_INTERFACE) {
847 r = EFI_INVALID_PARAMETER;
848 goto out;
849 }
850
851 /* Create new handle if requested. */
852 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200853 r = efi_create_handle(handle);
854 if (r != EFI_SUCCESS)
855 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +0200856 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
857 *handle);
858 } else {
859 debug("%sEFI: handle %p\n", indent_string(nesting_level),
860 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200861 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +0200862 /* Add new protocol */
863 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200864out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +0100865 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100866}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200867
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200868/*
869 * Reinstall protocol interface.
870 *
871 * This function implements the ReinstallProtocolInterface service.
872 * See the Unified Extensible Firmware Interface (UEFI) specification
873 * for details.
874 *
875 * @handle handle on which the protocol shall be
876 * reinstalled
877 * @protocol GUID of the protocol to be installed
878 * @old_interface interface to be removed
879 * @new_interface interface to be installed
880 * @return status code
881 */
Alexander Grafbee91162016-03-04 01:09:59 +0100882static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200883 const efi_guid_t *protocol, void *old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100884 void *new_interface)
885{
Rob Clark778e6af2017-09-13 18:05:41 -0400886 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
Alexander Grafbee91162016-03-04 01:09:59 +0100887 new_interface);
888 return EFI_EXIT(EFI_ACCESS_DENIED);
889}
890
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200891/*
892 * Uninstall protocol interface.
893 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100894 * This function implements the UninstallProtocolInterface service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification
896 * for details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200897 *
898 * @handle handle from which the protocol shall be removed
899 * @protocol GUID of the protocol to be removed
900 * @protocol_interface interface to be removed
901 * @return status code
902 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100903static efi_status_t EFIAPI efi_uninstall_protocol_interface(
904 void *handle, const efi_guid_t *protocol,
905 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +0100906{
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200907 struct efi_handler *handler;
908 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200909
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100910 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
911
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200912 if (!handle || !protocol) {
913 r = EFI_INVALID_PARAMETER;
914 goto out;
915 }
916
Heinrich Schuchardt58105112017-10-26 19:25:56 +0200917 /* Find the protocol on the handle */
918 r = efi_search_protocol(handle, protocol, &handler);
919 if (r != EFI_SUCCESS)
920 goto out;
921 if (handler->protocol_interface) {
922 /* TODO disconnect controllers */
923 r = EFI_ACCESS_DENIED;
924 } else {
925 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200926 }
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200927out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +0100928 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100929}
930
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200931/*
932 * Register an event for notification when a protocol is installed.
933 *
934 * This function implements the RegisterProtocolNotify service.
935 * See the Unified Extensible Firmware Interface (UEFI) specification
936 * for details.
937 *
938 * @protocol GUID of the protocol whose installation shall be
939 * notified
940 * @event event to be signaled upon installation of the protocol
941 * @registration key for retrieving the registration information
942 * @return status code
943 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200944static efi_status_t EFIAPI efi_register_protocol_notify(
945 const efi_guid_t *protocol,
946 struct efi_event *event,
947 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +0100948{
Rob Clark778e6af2017-09-13 18:05:41 -0400949 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +0100950 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
951}
952
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200953/*
954 * Determine if an EFI handle implements a protocol.
955 *
956 * See the documentation of the LocateHandle service in the UEFI specification.
957 *
958 * @search_type selection criterion
959 * @protocol GUID of the protocol
960 * @search_key registration key
961 * @efiobj handle
962 * @return 0 if the handle implements the protocol
963 */
Alexander Grafbee91162016-03-04 01:09:59 +0100964static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +0200965 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +0100966 struct efi_object *efiobj)
967{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200968 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +0100969
970 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100971 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +0100972 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100973 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200974 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +0100975 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +0100976 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +0200977 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
978 return (ret != EFI_SUCCESS);
979 default:
980 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +0100981 return -1;
982 }
Alexander Grafbee91162016-03-04 01:09:59 +0100983}
984
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200985/*
986 * Locate handles implementing a protocol.
987 *
988 * This function is meant for U-Boot internal calls. For the API implementation
989 * of the LocateHandle service see efi_locate_handle_ext.
990 *
991 * @search_type selection criterion
992 * @protocol GUID of the protocol
993 * @search_key registration key
994 * @buffer_size size of the buffer to receive the handles in bytes
995 * @buffer buffer to receive the relevant handles
996 * @return status code
997 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +0200998static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +0100999 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001000 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001001 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001002{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001003 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001004 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001005
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001006 /* Check parameters */
1007 switch (search_type) {
1008 case ALL_HANDLES:
1009 break;
1010 case BY_REGISTER_NOTIFY:
1011 if (!search_key)
1012 return EFI_INVALID_PARAMETER;
1013 /* RegisterProtocolNotify is not implemented yet */
1014 return EFI_UNSUPPORTED;
1015 case BY_PROTOCOL:
1016 if (!protocol)
1017 return EFI_INVALID_PARAMETER;
1018 break;
1019 default:
1020 return EFI_INVALID_PARAMETER;
1021 }
1022
1023 /*
1024 * efi_locate_handle_buffer uses this function for
1025 * the calculation of the necessary buffer size.
1026 * So do not require a buffer for buffersize == 0.
1027 */
1028 if (!buffer_size || (*buffer_size && !buffer))
1029 return EFI_INVALID_PARAMETER;
1030
Alexander Grafbee91162016-03-04 01:09:59 +01001031 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001032 list_for_each_entry(efiobj, &efi_obj_list, link) {
1033 if (!efi_search(search_type, protocol, search_key, efiobj))
Alexander Grafbee91162016-03-04 01:09:59 +01001034 size += sizeof(void*);
Alexander Grafbee91162016-03-04 01:09:59 +01001035 }
1036
1037 if (*buffer_size < size) {
1038 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001039 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001040 }
1041
Rob Clark796a78c2017-08-06 14:10:07 -04001042 *buffer_size = size;
1043 if (size == 0)
1044 return EFI_NOT_FOUND;
1045
Alexander Grafbee91162016-03-04 01:09:59 +01001046 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001047 list_for_each_entry(efiobj, &efi_obj_list, link) {
1048 if (!efi_search(search_type, protocol, search_key, efiobj))
1049 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001050 }
1051
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001052 return EFI_SUCCESS;
1053}
1054
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001055/*
1056 * Locate handles implementing a protocol.
1057 *
1058 * This function implements the LocateHandle service.
1059 * See the Unified Extensible Firmware Interface (UEFI) specification
1060 * for details.
1061 *
1062 * @search_type selection criterion
1063 * @protocol GUID of the protocol
1064 * @search_key registration key
1065 * @buffer_size size of the buffer to receive the handles in bytes
1066 * @buffer buffer to receive the relevant handles
1067 * @return 0 if the handle implements the protocol
1068 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001069static efi_status_t EFIAPI efi_locate_handle_ext(
1070 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001071 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001072 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001073{
Rob Clark778e6af2017-09-13 18:05:41 -04001074 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001075 buffer_size, buffer);
1076
1077 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1078 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001079}
1080
Alexander Grafd98cdf62017-07-26 13:41:04 +02001081/* Collapses configuration table entries, removing index i */
1082static void efi_remove_configuration_table(int i)
1083{
1084 struct efi_configuration_table *this = &efi_conf_table[i];
1085 struct efi_configuration_table *next = &efi_conf_table[i+1];
1086 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1087
1088 memmove(this, next, (ulong)end - (ulong)next);
1089 systab.nr_tables--;
1090}
1091
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001092/*
1093 * Adds, updates, or removes a configuration table.
1094 *
1095 * This function is used for internal calls. For the API implementation of the
1096 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1097 *
1098 * @guid GUID of the installed table
1099 * @table table to be installed
1100 * @return status code
1101 */
Alexander Graf488bf122016-08-19 01:23:24 +02001102efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001103{
1104 int i;
1105
Alexander Grafbee91162016-03-04 01:09:59 +01001106 /* Check for guid override */
1107 for (i = 0; i < systab.nr_tables; i++) {
1108 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001109 if (table)
1110 efi_conf_table[i].table = table;
1111 else
1112 efi_remove_configuration_table(i);
Alexander Graf488bf122016-08-19 01:23:24 +02001113 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +01001114 }
1115 }
1116
Alexander Grafd98cdf62017-07-26 13:41:04 +02001117 if (!table)
1118 return EFI_NOT_FOUND;
1119
Alexander Grafbee91162016-03-04 01:09:59 +01001120 /* No override, check for overflow */
1121 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +02001122 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001123
1124 /* Add a new entry */
1125 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1126 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001127 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001128
Alexander Graf488bf122016-08-19 01:23:24 +02001129 return EFI_SUCCESS;
1130}
1131
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001132/*
1133 * Adds, updates, or removes a configuration table.
1134 *
1135 * This function implements the InstallConfigurationTable service.
1136 * See the Unified Extensible Firmware Interface (UEFI) specification
1137 * for details.
1138 *
1139 * @guid GUID of the installed table
1140 * @table table to be installed
1141 * @return status code
1142 */
Alexander Graf488bf122016-08-19 01:23:24 +02001143static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1144 void *table)
1145{
Rob Clark778e6af2017-09-13 18:05:41 -04001146 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001147 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001148}
1149
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001150/*
1151 * Initialize a loaded_image_info + loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001152 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001153 *
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001154 * @info loaded image info to be passed to the entry point of the
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001155 * image
1156 * @obj internal object associated with the loaded image
1157 * @device_path device path of the loaded image
1158 * @file_path file path of the loaded image
Rob Clark95c55532017-09-13 18:05:33 -04001159 */
1160void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1161 struct efi_device_path *device_path,
1162 struct efi_device_path *file_path)
1163{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001164 efi_status_t ret;
1165
Rob Clark95c55532017-09-13 18:05:33 -04001166 obj->handle = info;
1167
Rob Clark95c55532017-09-13 18:05:33 -04001168 info->file_path = file_path;
Heinrich Schuchardt1a2c8d22017-10-08 06:57:26 +02001169 if (device_path)
1170 info->device_handle = efi_dp_find_obj(device_path, NULL);
Rob Clark95c55532017-09-13 18:05:33 -04001171
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001172 INIT_LIST_HEAD(&obj->protocols);
Rob Clark95c55532017-09-13 18:05:33 -04001173 list_add_tail(&obj->link, &efi_obj_list);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001174 /*
1175 * When asking for the device path interface, return
1176 * bootefi_device_path
1177 */
1178 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1179 if (ret != EFI_SUCCESS)
1180 goto failure;
1181
1182 /*
1183 * When asking for the loaded_image interface, just
1184 * return handle which points to loaded_image_info
1185 */
1186 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1187 if (ret != EFI_SUCCESS)
1188 goto failure;
1189
1190 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1191 (void *)&efi_console_control);
1192 if (ret != EFI_SUCCESS)
1193 goto failure;
1194
1195 ret = efi_add_protocol(obj->handle,
1196 &efi_guid_device_path_to_text_protocol,
1197 (void *)&efi_device_path_to_text);
1198 if (ret != EFI_SUCCESS)
1199 goto failure;
1200
1201 return;
1202failure:
1203 printf("ERROR: Failure to install protocols for loaded image\n");
Rob Clark95c55532017-09-13 18:05:33 -04001204}
1205
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001206/*
1207 * Load an image using a file path.
1208 *
1209 * @file_path the path of the image to load
1210 * @buffer buffer containing the loaded image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001211 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001212 */
Rob Clark9975fe92017-09-13 18:05:38 -04001213efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1214 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001215{
1216 struct efi_file_info *info = NULL;
1217 struct efi_file_handle *f;
1218 static efi_status_t ret;
1219 uint64_t bs;
1220
1221 f = efi_file_from_path(file_path);
1222 if (!f)
1223 return EFI_DEVICE_ERROR;
1224
1225 bs = 0;
1226 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1227 &bs, info));
1228 if (ret == EFI_BUFFER_TOO_SMALL) {
1229 info = malloc(bs);
1230 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1231 &bs, info));
1232 }
1233 if (ret != EFI_SUCCESS)
1234 goto error;
1235
1236 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1237 if (ret)
1238 goto error;
1239
1240 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1241
1242error:
1243 free(info);
1244 EFI_CALL(f->close(f));
1245
1246 if (ret != EFI_SUCCESS) {
1247 efi_free_pool(*buffer);
1248 *buffer = NULL;
1249 }
1250
1251 return ret;
1252}
1253
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001254/*
1255 * Load an EFI image into memory.
1256 *
1257 * This function implements the LoadImage service.
1258 * See the Unified Extensible Firmware Interface (UEFI) specification
1259 * for details.
1260 *
1261 * @boot_policy true for request originating from the boot manager
1262 * @parent_image the calles's image handle
1263 * @file_path the path of the image to load
1264 * @source_buffer memory location from which the image is installed
1265 * @source_size size of the memory area from which the image is
1266 * installed
1267 * @image_handle handle for the newly installed image
1268 * @return status code
1269 */
Alexander Grafbee91162016-03-04 01:09:59 +01001270static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1271 efi_handle_t parent_image,
1272 struct efi_device_path *file_path,
1273 void *source_buffer,
1274 unsigned long source_size,
1275 efi_handle_t *image_handle)
1276{
Alexander Grafbee91162016-03-04 01:09:59 +01001277 struct efi_loaded_image *info;
1278 struct efi_object *obj;
1279
1280 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1281 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001282
1283 info = calloc(1, sizeof(*info));
1284 obj = calloc(1, sizeof(*obj));
1285
1286 if (!source_buffer) {
1287 struct efi_device_path *dp, *fp;
1288 efi_status_t ret;
1289
Rob Clark9975fe92017-09-13 18:05:38 -04001290 ret = efi_load_image_from_path(file_path, &source_buffer);
Rob Clark838ee4b2017-09-13 18:05:35 -04001291 if (ret != EFI_SUCCESS) {
1292 free(info);
1293 free(obj);
1294 return EFI_EXIT(ret);
1295 }
1296
1297 /*
1298 * split file_path which contains both the device and
1299 * file parts:
1300 */
1301 efi_dp_split_file_path(file_path, &dp, &fp);
1302
1303 efi_setup_loaded_image(info, obj, dp, fp);
1304 } else {
1305 /* In this case, file_path is the "device" path, ie.
1306 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1307 */
1308 efi_setup_loaded_image(info, obj, file_path, NULL);
1309 }
1310
Alexander Grafbee91162016-03-04 01:09:59 +01001311 info->reserved = efi_load_pe(source_buffer, info);
1312 if (!info->reserved) {
1313 free(info);
1314 free(obj);
1315 return EFI_EXIT(EFI_UNSUPPORTED);
1316 }
1317
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001318 info->system_table = &systab;
1319 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001320 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001321
1322 return EFI_EXIT(EFI_SUCCESS);
1323}
1324
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001325/*
1326 * Call the entry point of an image.
1327 *
1328 * This function implements the StartImage service.
1329 * See the Unified Extensible Firmware Interface (UEFI) specification
1330 * for details.
1331 *
1332 * @image_handle handle of the image
1333 * @exit_data_size size of the buffer
1334 * @exit_data buffer to receive the exit data of the called image
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001335 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001336 */
Alexander Grafbee91162016-03-04 01:09:59 +01001337static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1338 unsigned long *exit_data_size,
1339 s16 **exit_data)
1340{
1341 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1342 struct efi_loaded_image *info = image_handle;
1343
1344 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1345 entry = info->reserved;
1346
1347 efi_is_direct_boot = false;
1348
1349 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001350 if (setjmp(&info->exit_jmp)) {
1351 /* We returned from the child image */
1352 return EFI_EXIT(info->exit_status);
1353 }
1354
Rob Clarkaf65db82017-07-27 08:04:19 -04001355 __efi_nesting_dec();
Rob Clarkc160d2f2017-07-27 08:04:18 -04001356 __efi_exit_check();
Alexander Grafbee91162016-03-04 01:09:59 +01001357 entry(image_handle, &systab);
Rob Clarkc160d2f2017-07-27 08:04:18 -04001358 __efi_entry_check();
Rob Clarkaf65db82017-07-27 08:04:19 -04001359 __efi_nesting_inc();
Alexander Grafbee91162016-03-04 01:09:59 +01001360
1361 /* Should usually never get here */
1362 return EFI_EXIT(EFI_SUCCESS);
1363}
1364
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001365/*
1366 * Leave an EFI application or driver.
1367 *
1368 * This function implements the Exit service.
1369 * See the Unified Extensible Firmware Interface (UEFI) specification
1370 * for details.
1371 *
1372 * @image_handle handle of the application or driver that is exiting
1373 * @exit_status status code
1374 * @exit_data_size size of the buffer in bytes
1375 * @exit_data buffer with data describing an error
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001376 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001377 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001378static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1379 efi_status_t exit_status, unsigned long exit_data_size,
1380 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001381{
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001382 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1383
Alexander Grafbee91162016-03-04 01:09:59 +01001384 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1385 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001386
Alexander Grafa1489202017-09-03 14:14:17 +02001387 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001388 __efi_exit_check();
1389
Alexander Grafa1489202017-09-03 14:14:17 +02001390 /*
1391 * But longjmp out with the U-Boot gd, not the application's, as
1392 * the other end is a setjmp call inside EFI context.
1393 */
1394 efi_restore_gd();
1395
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001396 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001397 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001398
1399 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001400}
1401
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001402/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001403 * Unload an EFI image.
1404 *
1405 * This function implements the UnloadImage service.
1406 * See the Unified Extensible Firmware Interface (UEFI) specification
1407 * for details.
1408 *
1409 * @image_handle handle of the image to be unloaded
1410 * @return status code
1411 */
Alexander Grafbee91162016-03-04 01:09:59 +01001412static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1413{
1414 struct efi_object *efiobj;
1415
1416 EFI_ENTRY("%p", image_handle);
1417 efiobj = efi_search_obj(image_handle);
1418 if (efiobj)
1419 list_del(&efiobj->link);
1420
1421 return EFI_EXIT(EFI_SUCCESS);
1422}
1423
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001424/*
1425 * Fix up caches for EFI payloads if necessary.
1426 */
Alexander Grafbee91162016-03-04 01:09:59 +01001427static void efi_exit_caches(void)
1428{
1429#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1430 /*
1431 * Grub on 32bit ARM needs to have caches disabled before jumping into
1432 * a zImage, but does not know of all cache layers. Give it a hand.
1433 */
1434 if (efi_is_direct_boot)
1435 cleanup_before_linux();
1436#endif
1437}
1438
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001439/*
1440 * Stop boot services.
1441 *
1442 * This function implements the ExitBootServices service.
1443 * See the Unified Extensible Firmware Interface (UEFI) specification
1444 * for details.
1445 *
1446 * @image_handle handle of the loaded image
1447 * @map_key key of the memory map
1448 * @return status code
1449 */
Alexander Grafbee91162016-03-04 01:09:59 +01001450static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1451 unsigned long map_key)
1452{
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001453 int i;
1454
Alexander Grafbee91162016-03-04 01:09:59 +01001455 EFI_ENTRY("%p, %ld", image_handle, map_key);
1456
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001457 /* Notify that ExitBootServices is invoked. */
1458 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1459 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1460 continue;
1461 efi_signal_event(&efi_events[i]);
1462 }
1463 /* Make sure that notification functions are not called anymore */
1464 efi_tpl = TPL_HIGH_LEVEL;
1465
Alexander Graf40583562017-10-19 23:23:50 +02001466 /* XXX Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001467
Alexander Grafb7b84102016-11-17 01:02:57 +01001468 board_quiesce_devices();
1469
Alexander Grafbee91162016-03-04 01:09:59 +01001470 /* Fix up caches for EFI payloads if necessary */
1471 efi_exit_caches();
1472
1473 /* This stops all lingering devices */
1474 bootm_disable_interrupts();
1475
1476 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001477 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001478 WATCHDOG_RESET();
1479
1480 return EFI_EXIT(EFI_SUCCESS);
1481}
1482
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001483/*
1484 * Get next value of the counter.
1485 *
1486 * This function implements the NextMonotonicCount service.
1487 * See the Unified Extensible Firmware Interface (UEFI) specification
1488 * for details.
1489 *
1490 * @count returned value of the counter
1491 * @return status code
1492 */
Alexander Grafbee91162016-03-04 01:09:59 +01001493static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1494{
1495 static uint64_t mono = 0;
1496 EFI_ENTRY("%p", count);
1497 *count = mono++;
1498 return EFI_EXIT(EFI_SUCCESS);
1499}
1500
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001501/*
1502 * Sleep.
1503 *
1504 * This function implements the Stall sercive.
1505 * See the Unified Extensible Firmware Interface (UEFI) specification
1506 * for details.
1507 *
1508 * @microseconds period to sleep in microseconds
1509 * @return status code
1510 */
Alexander Grafbee91162016-03-04 01:09:59 +01001511static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1512{
1513 EFI_ENTRY("%ld", microseconds);
1514 udelay(microseconds);
1515 return EFI_EXIT(EFI_SUCCESS);
1516}
1517
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001518/*
1519 * Reset the watchdog timer.
1520 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001521 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001522 * See the Unified Extensible Firmware Interface (UEFI) specification
1523 * for details.
1524 *
1525 * @timeout seconds before reset by watchdog
1526 * @watchdog_code code to be logged when resetting
1527 * @data_size size of buffer in bytes
1528 * @watchdog_data buffer with data describing the reset reason
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001529 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001530 */
Alexander Grafbee91162016-03-04 01:09:59 +01001531static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1532 uint64_t watchdog_code,
1533 unsigned long data_size,
1534 uint16_t *watchdog_data)
1535{
1536 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1537 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001538 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001539}
1540
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001541/*
1542 * Connect a controller to a driver.
1543 *
1544 * This function implements the ConnectController service.
1545 * See the Unified Extensible Firmware Interface (UEFI) specification
1546 * for details.
1547 *
1548 * @controller_handle handle of the controller
1549 * @driver_image_handle handle of the driver
1550 * @remain_device_path device path of a child controller
1551 * @recursive true to connect all child controllers
1552 * @return status code
1553 */
Alexander Grafbee91162016-03-04 01:09:59 +01001554static efi_status_t EFIAPI efi_connect_controller(
1555 efi_handle_t controller_handle,
1556 efi_handle_t *driver_image_handle,
1557 struct efi_device_path *remain_device_path,
1558 bool recursive)
1559{
1560 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1561 remain_device_path, recursive);
1562 return EFI_EXIT(EFI_NOT_FOUND);
1563}
1564
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001565/*
1566 * Disconnect a controller from a driver.
1567 *
1568 * This function implements the DisconnectController service.
1569 * See the Unified Extensible Firmware Interface (UEFI) specification
1570 * for details.
1571 *
1572 * @controller_handle handle of the controller
1573 * @driver_image_handle handle of the driver
1574 * @child_handle handle of the child to destroy
1575 * @return status code
1576 */
Alexander Grafbee91162016-03-04 01:09:59 +01001577static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1578 void *driver_image_handle,
1579 void *child_handle)
1580{
1581 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1582 child_handle);
1583 return EFI_EXIT(EFI_INVALID_PARAMETER);
1584}
1585
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001586/*
1587 * Close a protocol.
1588 *
1589 * This function implements the CloseProtocol service.
1590 * See the Unified Extensible Firmware Interface (UEFI) specification
1591 * for details.
1592 *
1593 * @handle handle on which the protocol shall be closed
1594 * @protocol GUID of the protocol to close
1595 * @agent_handle handle of the driver
1596 * @controller_handle handle of the controller
1597 * @return status code
1598 */
Alexander Grafbee91162016-03-04 01:09:59 +01001599static efi_status_t EFIAPI efi_close_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001600 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001601 void *agent_handle,
1602 void *controller_handle)
1603{
Rob Clark778e6af2017-09-13 18:05:41 -04001604 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001605 controller_handle);
1606 return EFI_EXIT(EFI_NOT_FOUND);
1607}
1608
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001609/*
1610 * Provide information about then open status of a protocol on a handle
1611 *
1612 * This function implements the OpenProtocolInformation service.
1613 * See the Unified Extensible Firmware Interface (UEFI) specification
1614 * for details.
1615 *
1616 * @handle handle for which the information shall be retrieved
1617 * @protocol GUID of the protocol
1618 * @entry_buffer buffer to receive the open protocol information
1619 * @entry_count number of entries available in the buffer
1620 * @return status code
1621 */
Alexander Grafbee91162016-03-04 01:09:59 +01001622static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001623 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001624 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001625 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001626{
Rob Clark778e6af2017-09-13 18:05:41 -04001627 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001628 entry_count);
1629 return EFI_EXIT(EFI_NOT_FOUND);
1630}
1631
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001632/*
1633 * Get protocols installed on a handle.
1634 *
1635 * This function implements the ProtocolsPerHandleService.
1636 * See the Unified Extensible Firmware Interface (UEFI) specification
1637 * for details.
1638 *
1639 * @handle handle for which the information is retrieved
1640 * @protocol_buffer buffer with protocol GUIDs
1641 * @protocol_buffer_count number of entries in the buffer
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001642 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001643 */
Alexander Grafbee91162016-03-04 01:09:59 +01001644static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1645 efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001646 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001647{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001648 unsigned long buffer_size;
1649 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001650 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001651 efi_status_t r;
1652
Alexander Grafbee91162016-03-04 01:09:59 +01001653 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1654 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001655
1656 if (!handle || !protocol_buffer || !protocol_buffer_count)
1657 return EFI_EXIT(EFI_INVALID_PARAMETER);
1658
1659 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04001660 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001661
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001662 efiobj = efi_search_obj(handle);
1663 if (!efiobj)
1664 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001665
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001666 /* Count protocols */
1667 list_for_each(protocol_handle, &efiobj->protocols) {
1668 ++*protocol_buffer_count;
1669 }
1670
1671 /* Copy guids */
1672 if (*protocol_buffer_count) {
1673 size_t j = 0;
1674
1675 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1676 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1677 (void **)protocol_buffer);
1678 if (r != EFI_SUCCESS)
1679 return EFI_EXIT(r);
1680 list_for_each(protocol_handle, &efiobj->protocols) {
1681 struct efi_handler *protocol;
1682
1683 protocol = list_entry(protocol_handle,
1684 struct efi_handler, link);
1685 (*protocol_buffer)[j] = (void *)protocol->guid;
1686 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001687 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02001688 }
1689
1690 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001691}
1692
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001693/*
1694 * Locate handles implementing a protocol.
1695 *
1696 * This function implements the LocateHandleBuffer service.
1697 * See the Unified Extensible Firmware Interface (UEFI) specification
1698 * for details.
1699 *
1700 * @search_type selection criterion
1701 * @protocol GUID of the protocol
1702 * @search_key registration key
1703 * @no_handles number of returned handles
1704 * @buffer buffer with the returned handles
1705 * @return status code
1706 */
Alexander Grafbee91162016-03-04 01:09:59 +01001707static efi_status_t EFIAPI efi_locate_handle_buffer(
1708 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001709 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001710 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001711{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001712 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001713 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001714
Rob Clark778e6af2017-09-13 18:05:41 -04001715 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001716 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02001717
1718 if (!no_handles || !buffer) {
1719 r = EFI_INVALID_PARAMETER;
1720 goto out;
1721 }
1722 *no_handles = 0;
1723 *buffer = NULL;
1724 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1725 *buffer);
1726 if (r != EFI_BUFFER_TOO_SMALL)
1727 goto out;
1728 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1729 (void **)buffer);
1730 if (r != EFI_SUCCESS)
1731 goto out;
1732 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1733 *buffer);
1734 if (r == EFI_SUCCESS)
1735 *no_handles = buffer_size / sizeof(void *);
1736out:
1737 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001738}
1739
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001740/*
1741 * Find an interface implementing a protocol.
1742 *
1743 * This function implements the LocateProtocol service.
1744 * See the Unified Extensible Firmware Interface (UEFI) specification
1745 * for details.
1746 *
1747 * @protocol GUID of the protocol
1748 * @registration registration key passed to the notification function
1749 * @protocol_interface interface implementing the protocol
Heinrich Schuchardt10a08c42017-10-08 06:57:27 +02001750 * @return status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001751 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001752static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001753 void *registration,
1754 void **protocol_interface)
1755{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001756 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001757 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001758
Rob Clark778e6af2017-09-13 18:05:41 -04001759 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001760
1761 if (!protocol || !protocol_interface)
1762 return EFI_EXIT(EFI_INVALID_PARAMETER);
1763
1764 list_for_each(lhandle, &efi_obj_list) {
1765 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001766 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001767
1768 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001769
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02001770 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1771 if (ret == EFI_SUCCESS) {
1772 *protocol_interface = handler->protocol_interface;
1773 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01001774 }
1775 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02001776 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001777
1778 return EFI_EXIT(EFI_NOT_FOUND);
1779}
1780
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001781/*
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01001782 * Get the device path and handle of an device implementing a protocol.
1783 *
1784 * This function implements the LocateDevicePath service.
1785 * See the Unified Extensible Firmware Interface (UEFI) specification
1786 * for details.
1787 *
1788 * @protocol GUID of the protocol
1789 * @device_path device path
1790 * @device handle of the device
1791 * @return status code
1792 */
1793static efi_status_t EFIAPI efi_locate_device_path(
1794 const efi_guid_t *protocol,
1795 struct efi_device_path **device_path,
1796 efi_handle_t *device)
1797{
1798 struct efi_device_path *dp;
1799 size_t i;
1800 struct efi_handler *handler;
1801 efi_handle_t *handles;
1802 size_t len, len_dp;
1803 size_t len_best = 0;
1804 efi_uintn_t no_handles;
1805 u8 *remainder;
1806 efi_status_t ret;
1807
1808 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1809
1810 if (!protocol || !device_path || !*device_path || !device) {
1811 ret = EFI_INVALID_PARAMETER;
1812 goto out;
1813 }
1814
1815 /* Find end of device path */
1816 len = efi_dp_size(*device_path);
1817
1818 /* Get all handles implementing the protocol */
1819 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1820 &no_handles, &handles));
1821 if (ret != EFI_SUCCESS)
1822 goto out;
1823
1824 for (i = 0; i < no_handles; ++i) {
1825 /* Find the device path protocol */
1826 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1827 &handler);
1828 if (ret != EFI_SUCCESS)
1829 continue;
1830 dp = (struct efi_device_path *)handler->protocol_interface;
1831 len_dp = efi_dp_size(dp);
1832 /*
1833 * This handle can only be a better fit
1834 * if its device path length is longer than the best fit and
1835 * if its device path length is shorter of equal the searched
1836 * device path.
1837 */
1838 if (len_dp <= len_best || len_dp > len)
1839 continue;
1840 /* Check if dp is a subpath of device_path */
1841 if (memcmp(*device_path, dp, len_dp))
1842 continue;
1843 *device = handles[i];
1844 len_best = len_dp;
1845 }
1846 if (len_best) {
1847 remainder = (u8 *)*device_path + len_best;
1848 *device_path = (struct efi_device_path *)remainder;
1849 ret = EFI_SUCCESS;
1850 } else {
1851 ret = EFI_NOT_FOUND;
1852 }
1853out:
1854 return EFI_EXIT(ret);
1855}
1856
1857/*
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001858 * Install multiple protocol interfaces.
1859 *
1860 * This function implements the MultipleProtocolInterfaces service.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification
1862 * for details.
1863 *
1864 * @handle handle on which the protocol interfaces shall be installed
1865 * @... NULL terminated argument list with pairs of protocol GUIDS and
1866 * interfaces
1867 * @return status code
1868 */
Alexander Grafbee91162016-03-04 01:09:59 +01001869static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1870 void **handle, ...)
1871{
1872 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001873
1874 va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001875 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001876 void *protocol_interface;
1877 efi_status_t r = EFI_SUCCESS;
1878 int i = 0;
1879
1880 if (!handle)
1881 return EFI_EXIT(EFI_INVALID_PARAMETER);
1882
1883 va_start(argptr, handle);
1884 for (;;) {
1885 protocol = va_arg(argptr, efi_guid_t*);
1886 if (!protocol)
1887 break;
1888 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001889 r = EFI_CALL(efi_install_protocol_interface(
1890 handle, protocol,
1891 EFI_NATIVE_INTERFACE,
1892 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001893 if (r != EFI_SUCCESS)
1894 break;
1895 i++;
1896 }
1897 va_end(argptr);
1898 if (r == EFI_SUCCESS)
1899 return EFI_EXIT(r);
1900
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02001901 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001902 va_start(argptr, handle);
1903 for (; i; --i) {
1904 protocol = va_arg(argptr, efi_guid_t*);
1905 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001906 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1907 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02001908 }
1909 va_end(argptr);
1910
1911 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001912}
1913
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001914/*
1915 * Uninstall multiple protocol interfaces.
1916 *
1917 * This function implements the UninstallMultipleProtocolInterfaces service.
1918 * See the Unified Extensible Firmware Interface (UEFI) specification
1919 * for details.
1920 *
1921 * @handle handle from which the protocol interfaces shall be removed
1922 * @... NULL terminated argument list with pairs of protocol GUIDS and
1923 * interfaces
1924 * @return status code
1925 */
Alexander Grafbee91162016-03-04 01:09:59 +01001926static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1927 void *handle, ...)
1928{
1929 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02001930
1931 va_list argptr;
1932 const efi_guid_t *protocol;
1933 void *protocol_interface;
1934 efi_status_t r = EFI_SUCCESS;
1935 size_t i = 0;
1936
1937 if (!handle)
1938 return EFI_EXIT(EFI_INVALID_PARAMETER);
1939
1940 va_start(argptr, handle);
1941 for (;;) {
1942 protocol = va_arg(argptr, efi_guid_t*);
1943 if (!protocol)
1944 break;
1945 protocol_interface = va_arg(argptr, void*);
1946 r = EFI_CALL(efi_uninstall_protocol_interface(
1947 handle, protocol,
1948 protocol_interface));
1949 if (r != EFI_SUCCESS)
1950 break;
1951 i++;
1952 }
1953 va_end(argptr);
1954 if (r == EFI_SUCCESS)
1955 return EFI_EXIT(r);
1956
1957 /* If an error occurred undo all changes. */
1958 va_start(argptr, handle);
1959 for (; i; --i) {
1960 protocol = va_arg(argptr, efi_guid_t*);
1961 protocol_interface = va_arg(argptr, void*);
1962 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1963 EFI_NATIVE_INTERFACE,
1964 protocol_interface));
1965 }
1966 va_end(argptr);
1967
1968 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001969}
1970
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001971/*
1972 * Calculate cyclic redundancy code.
1973 *
1974 * This function implements the CalculateCrc32 service.
1975 * See the Unified Extensible Firmware Interface (UEFI) specification
1976 * for details.
1977 *
1978 * @data buffer with data
1979 * @data_size size of buffer in bytes
1980 * @crc32_p cyclic redundancy code
1981 * @return status code
1982 */
Alexander Grafbee91162016-03-04 01:09:59 +01001983static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1984 unsigned long data_size,
1985 uint32_t *crc32_p)
1986{
1987 EFI_ENTRY("%p, %ld", data, data_size);
1988 *crc32_p = crc32(0, data, data_size);
1989 return EFI_EXIT(EFI_SUCCESS);
1990}
1991
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001992/*
1993 * Copy memory.
1994 *
1995 * This function implements the CopyMem service.
1996 * See the Unified Extensible Firmware Interface (UEFI) specification
1997 * for details.
1998 *
1999 * @destination destination of the copy operation
2000 * @source source of the copy operation
2001 * @length number of bytes to copy
2002 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002003static void EFIAPI efi_copy_mem(void *destination, const void *source,
2004 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002005{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002006 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002007 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002008 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002009}
2010
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002011/*
2012 * Fill memory with a byte value.
2013 *
2014 * This function implements the SetMem service.
2015 * See the Unified Extensible Firmware Interface (UEFI) specification
2016 * for details.
2017 *
2018 * @buffer buffer to fill
2019 * @size size of buffer in bytes
2020 * @value byte to copy to the buffer
2021 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002022static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002023{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002024 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002025 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002026 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002027}
2028
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002029/*
2030 * Open protocol interface on a handle.
2031 *
2032 * This function implements the OpenProtocol interface.
2033 * See the Unified Extensible Firmware Interface (UEFI) specification
2034 * for details.
2035 *
2036 * @handle handle on which the protocol shall be opened
2037 * @protocol GUID of the protocol
2038 * @protocol_interface interface implementing the protocol
2039 * @agent_handle handle of the driver
2040 * @controller_handle handle of the controller
2041 * @attributes attributes indicating how to open the protocol
2042 * @return status code
2043 */
Alexander Grafbee91162016-03-04 01:09:59 +01002044static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002045 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002046 void **protocol_interface, void *agent_handle,
2047 void *controller_handle, uint32_t attributes)
2048{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002049 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002050 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002051
Rob Clark778e6af2017-09-13 18:05:41 -04002052 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002053 protocol_interface, agent_handle, controller_handle,
2054 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002055
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002056 if (!handle || !protocol ||
2057 (!protocol_interface && attributes !=
2058 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2059 goto out;
2060 }
2061
2062 switch (attributes) {
2063 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2064 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2065 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2066 break;
2067 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2068 if (controller_handle == handle)
2069 goto out;
2070 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2071 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2072 if (controller_handle == NULL)
2073 goto out;
2074 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2075 if (agent_handle == NULL)
2076 goto out;
2077 break;
2078 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002079 goto out;
2080 }
2081
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002082 r = efi_search_protocol(handle, protocol, &handler);
2083 if (r != EFI_SUCCESS)
2084 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002085
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002086 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2087 *protocol_interface = handler->protocol_interface;
Alexander Grafbee91162016-03-04 01:09:59 +01002088out:
2089 return EFI_EXIT(r);
2090}
2091
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002092/*
2093 * Get interface of a protocol on a handle.
2094 *
2095 * This function implements the HandleProtocol service.
2096 * See the Unified Extensible Firmware Interface (UEFI) specification
2097 * for details.
2098 *
2099 * @handle handle on which the protocol shall be opened
2100 * @protocol GUID of the protocol
2101 * @protocol_interface interface implementing the protocol
2102 * @return status code
2103 */
Alexander Grafbee91162016-03-04 01:09:59 +01002104static efi_status_t EFIAPI efi_handle_protocol(void *handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002105 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002106 void **protocol_interface)
2107{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002108 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2109 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002110}
2111
2112static const struct efi_boot_services efi_boot_services = {
2113 .hdr = {
2114 .headersize = sizeof(struct efi_table_hdr),
2115 },
2116 .raise_tpl = efi_raise_tpl,
2117 .restore_tpl = efi_restore_tpl,
2118 .allocate_pages = efi_allocate_pages_ext,
2119 .free_pages = efi_free_pages_ext,
2120 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02002121 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02002122 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02002123 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02002124 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002125 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02002126 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002127 .close_event = efi_close_event,
2128 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002129 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002130 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002131 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01002132 .handle_protocol = efi_handle_protocol,
2133 .reserved = NULL,
2134 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02002135 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002136 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02002137 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01002138 .load_image = efi_load_image,
2139 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02002140 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01002141 .unload_image = efi_unload_image,
2142 .exit_boot_services = efi_exit_boot_services,
2143 .get_next_monotonic_count = efi_get_next_monotonic_count,
2144 .stall = efi_stall,
2145 .set_watchdog_timer = efi_set_watchdog_timer,
2146 .connect_controller = efi_connect_controller,
2147 .disconnect_controller = efi_disconnect_controller,
2148 .open_protocol = efi_open_protocol,
2149 .close_protocol = efi_close_protocol,
2150 .open_protocol_information = efi_open_protocol_information,
2151 .protocols_per_handle = efi_protocols_per_handle,
2152 .locate_handle_buffer = efi_locate_handle_buffer,
2153 .locate_protocol = efi_locate_protocol,
2154 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2155 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2156 .calculate_crc32 = efi_calculate_crc32,
2157 .copy_mem = efi_copy_mem,
2158 .set_mem = efi_set_mem,
2159};
2160
2161
Alexander Graf3c63db92016-10-14 13:45:30 +02002162static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafbee91162016-03-04 01:09:59 +01002163 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2164
Alexander Graf3c63db92016-10-14 13:45:30 +02002165struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01002166 .hdr = {
2167 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2168 .revision = 0x20005, /* 2.5 */
2169 .headersize = sizeof(struct efi_table_hdr),
2170 },
2171 .fw_vendor = (long)firmware_vendor,
2172 .con_in = (void*)&efi_con_in,
2173 .con_out = (void*)&efi_con_out,
2174 .std_err = (void*)&efi_con_out,
2175 .runtime = (void*)&efi_runtime_services,
2176 .boottime = (void*)&efi_boot_services,
2177 .nr_tables = 0,
2178 .tables = (void*)efi_conf_table,
2179};