blob: 91db12e774bb827462fb12e0104c93b3bceed2e9 [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>
10#include <efi_loader.h>
11#include <malloc.h>
12#include <asm/global_data.h>
13#include <libfdt_env.h>
14#include <u-boot/crc.h>
15#include <bootm.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/* This list contains all the EFI objects our payload has access to */
22LIST_HEAD(efi_obj_list);
23
24/*
25 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
26 * we need to do trickery with caches. Since we don't want to break the EFI
27 * aware boot path, only apply hacks when loading exiting directly (breaking
28 * direct Linux EFI booting along the way - oh well).
29 */
30static bool efi_is_direct_boot = true;
31
32/*
33 * EFI can pass arbitrary additional "tables" containing vendor specific
34 * information to the payload. One such table is the FDT table which contains
35 * a pointer to a flattened device tree blob.
36 *
37 * In most cases we want to pass an FDT to the payload, so reserve one slot of
38 * config table space for it. The pointer gets populated by do_bootefi_exec().
39 */
Alexander Graf3c63db92016-10-14 13:45:30 +020040static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
Alexander Grafbee91162016-03-04 01:09:59 +010041
Simon Glass65e4c0b2016-09-25 15:27:35 -060042#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010043/*
44 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45 * fixed when compiling U-Boot. However, the payload does not know about that
46 * restriction so we need to manually swap its and our view of that register on
47 * EFI callback entry/exit.
48 */
49static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060050#endif
Alexander Grafbee91162016-03-04 01:09:59 +010051
52/* Called from do_bootefi_exec() */
53void efi_save_gd(void)
54{
Simon Glass65e4c0b2016-09-25 15:27:35 -060055#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010056 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060057#endif
Alexander Grafbee91162016-03-04 01:09:59 +010058}
59
60/* Called on every callback entry */
61void efi_restore_gd(void)
62{
Simon Glass65e4c0b2016-09-25 15:27:35 -060063#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010064 /* Only restore if we're already in EFI context */
65 if (!efi_gd)
66 return;
67
68 if (gd != efi_gd)
69 app_gd = gd;
70 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060071#endif
Alexander Grafbee91162016-03-04 01:09:59 +010072}
73
74/* Called on every callback exit */
75efi_status_t efi_exit_func(efi_status_t ret)
76{
Simon Glass65e4c0b2016-09-25 15:27:35 -060077#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010078 gd = app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060079#endif
80
Alexander Grafbee91162016-03-04 01:09:59 +010081 return ret;
82}
83
84static efi_status_t efi_unsupported(const char *funcname)
85{
Alexander Grafedcef3b2016-06-02 11:38:27 +020086 debug("EFI: App called into unimplemented function %s\n", funcname);
Alexander Grafbee91162016-03-04 01:09:59 +010087 return EFI_EXIT(EFI_UNSUPPORTED);
88}
89
90static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
91{
92 return memcmp(g1, g2, sizeof(efi_guid_t));
93}
94
95static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl)
96{
97 EFI_ENTRY("0x%lx", new_tpl);
98 return EFI_EXIT(0);
99}
100
101static void EFIAPI efi_restore_tpl(unsigned long old_tpl)
102{
103 EFI_ENTRY("0x%lx", old_tpl);
104 EFI_EXIT(efi_unsupported(__func__));
105}
106
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900107static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
108 unsigned long pages,
109 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100110{
111 efi_status_t r;
112
113 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
114 r = efi_allocate_pages(type, memory_type, pages, memory);
115 return EFI_EXIT(r);
116}
117
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900118static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
119 unsigned long pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100120{
121 efi_status_t r;
122
123 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
124 r = efi_free_pages(memory, pages);
125 return EFI_EXIT(r);
126}
127
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900128static efi_status_t EFIAPI efi_get_memory_map_ext(
129 unsigned long *memory_map_size,
130 struct efi_mem_desc *memory_map,
131 unsigned long *map_key,
132 unsigned long *descriptor_size,
133 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100134{
135 efi_status_t r;
136
137 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
138 map_key, descriptor_size, descriptor_version);
139 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
140 descriptor_size, descriptor_version);
141 return EFI_EXIT(r);
142}
143
Stefan Brünsead12742016-10-09 22:17:18 +0200144static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
145 unsigned long size,
146 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100147{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100148 efi_status_t r;
149
150 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200151 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100152 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100153}
154
Stefan Brüns42417bc2016-10-09 22:17:26 +0200155static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100156{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100157 efi_status_t r;
158
159 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200160 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100161 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100162}
163
164/*
165 * Our event capabilities are very limited. Only support a single
166 * event to exist, so we don't need to maintain lists.
167 */
168static struct {
169 enum efi_event_type type;
170 u32 trigger_type;
171 u32 trigger_time;
172 u64 trigger_next;
173 unsigned long notify_tpl;
Simon Glasse2754582016-09-25 15:27:32 -0600174 void (EFIAPI *notify_function) (void *event, void *context);
Alexander Grafbee91162016-03-04 01:09:59 +0100175 void *notify_context;
176} efi_event = {
177 /* Disable timers on bootup */
178 .trigger_next = -1ULL,
179};
180
181static efi_status_t EFIAPI efi_create_event(
182 enum efi_event_type type, ulong notify_tpl,
Simon Glasse2754582016-09-25 15:27:32 -0600183 void (EFIAPI *notify_function) (void *event,
184 void *context),
Alexander Grafbee91162016-03-04 01:09:59 +0100185 void *notify_context, void **event)
186{
187 EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function,
188 notify_context);
189 if (efi_event.notify_function) {
190 /* We only support one event at a time */
191 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
192 }
193
Jonathan Graya95343b2017-03-12 19:26:07 +1100194 if (event == NULL)
195 return EFI_EXIT(EFI_INVALID_PARAMETER);
196
197 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
198 return EFI_EXIT(EFI_INVALID_PARAMETER);
199
200 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
201 notify_function == NULL)
202 return EFI_EXIT(EFI_INVALID_PARAMETER);
203
Alexander Grafbee91162016-03-04 01:09:59 +0100204 efi_event.type = type;
205 efi_event.notify_tpl = notify_tpl;
206 efi_event.notify_function = notify_function;
207 efi_event.notify_context = notify_context;
208 *event = &efi_event;
209
210 return EFI_EXIT(EFI_SUCCESS);
211}
212
213/*
214 * Our timers have to work without interrupts, so we check whenever keyboard
215 * input or disk accesses happen if enough time elapsed for it to fire.
216 */
217void efi_timer_check(void)
218{
219 u64 now = timer_get_us();
220
221 if (now >= efi_event.trigger_next) {
222 /* Triggering! */
223 if (efi_event.trigger_type == EFI_TIMER_PERIODIC)
224 efi_event.trigger_next += efi_event.trigger_time / 10;
Jonathan Gray37a980b2017-03-12 19:26:06 +1100225 if (efi_event.type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL))
226 efi_event.notify_function(&efi_event,
227 efi_event.notify_context);
Alexander Grafbee91162016-03-04 01:09:59 +0100228 }
229
230 WATCHDOG_RESET();
231}
232
233static efi_status_t EFIAPI efi_set_timer(void *event, int type,
234 uint64_t trigger_time)
235{
236 /* We don't have 64bit division available everywhere, so limit timer
237 * distances to 32bit bits. */
238 u32 trigger32 = trigger_time;
239
240 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
241
242 if (trigger32 < trigger_time) {
243 printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
244 trigger_time, trigger32);
245 }
246
247 if (event != &efi_event) {
248 /* We only support one event at a time */
249 return EFI_EXIT(EFI_INVALID_PARAMETER);
250 }
251
252 switch (type) {
253 case EFI_TIMER_STOP:
254 efi_event.trigger_next = -1ULL;
255 break;
256 case EFI_TIMER_PERIODIC:
257 case EFI_TIMER_RELATIVE:
258 efi_event.trigger_next = timer_get_us() + (trigger32 / 10);
259 break;
260 default:
261 return EFI_EXIT(EFI_INVALID_PARAMETER);
262 }
263 efi_event.trigger_type = type;
264 efi_event.trigger_time = trigger_time;
265
266 return EFI_EXIT(EFI_SUCCESS);
267}
268
269static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
270 void *event, unsigned long *index)
271{
272 u64 now;
273
274 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
275
276 now = timer_get_us();
277 while (now < efi_event.trigger_next) { }
278 efi_timer_check();
279
280 return EFI_EXIT(EFI_SUCCESS);
281}
282
283static efi_status_t EFIAPI efi_signal_event(void *event)
284{
285 EFI_ENTRY("%p", event);
286 return EFI_EXIT(EFI_SUCCESS);
287}
288
289static efi_status_t EFIAPI efi_close_event(void *event)
290{
291 EFI_ENTRY("%p", event);
292 efi_event.trigger_next = -1ULL;
293 return EFI_EXIT(EFI_SUCCESS);
294}
295
296static efi_status_t EFIAPI efi_check_event(void *event)
297{
298 EFI_ENTRY("%p", event);
299 return EFI_EXIT(EFI_NOT_READY);
300}
301
302static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
303 efi_guid_t *protocol, int protocol_interface_type,
304 void *protocol_interface)
305{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200306 struct list_head *lhandle;
307 int i;
308 efi_status_t r;
309
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200310 if (!handle || !protocol ||
311 protocol_interface_type != EFI_NATIVE_INTERFACE) {
312 r = EFI_INVALID_PARAMETER;
313 goto out;
314 }
315
316 /* Create new handle if requested. */
317 if (!*handle) {
318 r = EFI_OUT_OF_RESOURCES;
319 goto out;
320 }
321 /* Find object. */
322 list_for_each(lhandle, &efi_obj_list) {
323 struct efi_object *efiobj;
324 efiobj = list_entry(lhandle, struct efi_object, link);
325
326 if (efiobj->handle != *handle)
327 continue;
328 /* Check if protocol is already installed on the handle. */
329 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
330 struct efi_handler *handler = &efiobj->protocols[i];
331
332 if (!handler->guid)
333 continue;
334 if (!guidcmp(handler->guid, protocol)) {
335 r = EFI_INVALID_PARAMETER;
336 goto out;
337 }
338 }
339 /* Install protocol in first empty slot. */
340 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
341 struct efi_handler *handler = &efiobj->protocols[i];
342
343 if (handler->guid)
344 continue;
345
346 handler->guid = protocol;
347 handler->protocol_interface = protocol_interface;
348 r = EFI_SUCCESS;
349 goto out;
350 }
351 r = EFI_OUT_OF_RESOURCES;
352 goto out;
353 }
354 r = EFI_INVALID_PARAMETER;
355out:
xypron.glpk@gmx.de8bee5a32017-07-11 22:06:18 +0200356 return r;
357}
358
359static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
360 efi_guid_t *protocol, int protocol_interface_type,
361 void *protocol_interface)
362{
363 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
364 protocol_interface);
365
366 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
367 protocol_interface_type,
368 protocol_interface));
Alexander Grafbee91162016-03-04 01:09:59 +0100369}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +0200370
Alexander Grafbee91162016-03-04 01:09:59 +0100371static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
372 efi_guid_t *protocol, void *old_interface,
373 void *new_interface)
374{
375 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
376 new_interface);
377 return EFI_EXIT(EFI_ACCESS_DENIED);
378}
379
380static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
381 efi_guid_t *protocol, void *protocol_interface)
382{
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200383 struct list_head *lhandle;
384 int i;
385 efi_status_t r = EFI_NOT_FOUND;
386
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200387 if (!handle || !protocol) {
388 r = EFI_INVALID_PARAMETER;
389 goto out;
390 }
391
392 list_for_each(lhandle, &efi_obj_list) {
393 struct efi_object *efiobj;
394 efiobj = list_entry(lhandle, struct efi_object, link);
395
396 if (efiobj->handle != handle)
397 continue;
398
399 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
400 struct efi_handler *handler = &efiobj->protocols[i];
401 const efi_guid_t *hprotocol = handler->guid;
402
403 if (!hprotocol)
404 continue;
405 if (!guidcmp(hprotocol, protocol)) {
406 if (handler->protocol_interface) {
407 r = EFI_ACCESS_DENIED;
408 } else {
409 handler->guid = 0;
410 r = EFI_SUCCESS;
411 }
412 goto out;
413 }
414 }
415 }
416
417out:
xypron.glpk@gmx.de3d8e1452017-07-11 22:06:19 +0200418 return r;
419}
420
421static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
422 efi_guid_t *protocol, void *protocol_interface)
423{
424 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
425
426 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
427 protocol_interface));
Alexander Grafbee91162016-03-04 01:09:59 +0100428}
429
430static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
431 void *event,
432 void **registration)
433{
434 EFI_ENTRY("%p, %p, %p", protocol, event, registration);
435 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
436}
437
438static int efi_search(enum efi_locate_search_type search_type,
439 efi_guid_t *protocol, void *search_key,
440 struct efi_object *efiobj)
441{
442 int i;
443
444 switch (search_type) {
445 case all_handles:
446 return 0;
447 case by_register_notify:
448 return -1;
449 case by_protocol:
450 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
451 const efi_guid_t *guid = efiobj->protocols[i].guid;
452 if (guid && !guidcmp(guid, protocol))
453 return 0;
454 }
455 return -1;
456 }
457
458 return -1;
459}
460
461static efi_status_t EFIAPI efi_locate_handle(
462 enum efi_locate_search_type search_type,
463 efi_guid_t *protocol, void *search_key,
464 unsigned long *buffer_size, efi_handle_t *buffer)
465{
466 struct list_head *lhandle;
467 unsigned long size = 0;
468
469 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
470 buffer_size, buffer);
471
472 /* Count how much space we need */
473 list_for_each(lhandle, &efi_obj_list) {
474 struct efi_object *efiobj;
475 efiobj = list_entry(lhandle, struct efi_object, link);
476 if (!efi_search(search_type, protocol, search_key, efiobj)) {
477 size += sizeof(void*);
478 }
479 }
480
481 if (*buffer_size < size) {
482 *buffer_size = size;
483 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
484 }
485
486 /* Then fill the array */
487 list_for_each(lhandle, &efi_obj_list) {
488 struct efi_object *efiobj;
489 efiobj = list_entry(lhandle, struct efi_object, link);
490 if (!efi_search(search_type, protocol, search_key, efiobj)) {
491 *(buffer++) = efiobj->handle;
492 }
493 }
494
495 *buffer_size = size;
496 return EFI_EXIT(EFI_SUCCESS);
497}
498
499static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
500 struct efi_device_path **device_path,
501 efi_handle_t *device)
502{
503 EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
504 return EFI_EXIT(EFI_NOT_FOUND);
505}
506
Alexander Graf488bf122016-08-19 01:23:24 +0200507efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
Alexander Grafbee91162016-03-04 01:09:59 +0100508{
509 int i;
510
Alexander Grafbee91162016-03-04 01:09:59 +0100511 /* Check for guid override */
512 for (i = 0; i < systab.nr_tables; i++) {
513 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
514 efi_conf_table[i].table = table;
Alexander Graf488bf122016-08-19 01:23:24 +0200515 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100516 }
517 }
518
519 /* No override, check for overflow */
520 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Graf488bf122016-08-19 01:23:24 +0200521 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +0100522
523 /* Add a new entry */
524 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
525 efi_conf_table[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +0200526 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +0100527
Alexander Graf488bf122016-08-19 01:23:24 +0200528 return EFI_SUCCESS;
529}
530
531static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
532 void *table)
533{
534 EFI_ENTRY("%p, %p", guid, table);
535 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +0100536}
537
538static efi_status_t EFIAPI efi_load_image(bool boot_policy,
539 efi_handle_t parent_image,
540 struct efi_device_path *file_path,
541 void *source_buffer,
542 unsigned long source_size,
543 efi_handle_t *image_handle)
544{
545 static struct efi_object loaded_image_info_obj = {
546 .protocols = {
547 {
548 .guid = &efi_guid_loaded_image,
Alexander Grafbee91162016-03-04 01:09:59 +0100549 },
550 },
551 };
552 struct efi_loaded_image *info;
553 struct efi_object *obj;
554
555 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
556 file_path, source_buffer, source_size, image_handle);
557 info = malloc(sizeof(*info));
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200558 loaded_image_info_obj.protocols[0].protocol_interface = info;
Alexander Grafbee91162016-03-04 01:09:59 +0100559 obj = malloc(sizeof(loaded_image_info_obj));
560 memset(info, 0, sizeof(*info));
561 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
562 obj->handle = info;
563 info->file_path = file_path;
564 info->reserved = efi_load_pe(source_buffer, info);
565 if (!info->reserved) {
566 free(info);
567 free(obj);
568 return EFI_EXIT(EFI_UNSUPPORTED);
569 }
570
571 *image_handle = info;
572 list_add_tail(&obj->link, &efi_obj_list);
573
574 return EFI_EXIT(EFI_SUCCESS);
575}
576
577static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
578 unsigned long *exit_data_size,
579 s16 **exit_data)
580{
581 ulong (*entry)(void *image_handle, struct efi_system_table *st);
582 struct efi_loaded_image *info = image_handle;
583
584 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
585 entry = info->reserved;
586
587 efi_is_direct_boot = false;
588
589 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200590 if (setjmp(&info->exit_jmp)) {
591 /* We returned from the child image */
592 return EFI_EXIT(info->exit_status);
593 }
594
Alexander Grafbee91162016-03-04 01:09:59 +0100595 entry(image_handle, &systab);
596
597 /* Should usually never get here */
598 return EFI_EXIT(EFI_SUCCESS);
599}
600
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200601static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
602 efi_status_t exit_status, unsigned long exit_data_size,
603 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +0100604{
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200605 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
606
Alexander Grafbee91162016-03-04 01:09:59 +0100607 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
608 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200609
610 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +0200611 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200612
613 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +0100614}
615
616static struct efi_object *efi_search_obj(void *handle)
617{
618 struct list_head *lhandle;
619
620 list_for_each(lhandle, &efi_obj_list) {
621 struct efi_object *efiobj;
622 efiobj = list_entry(lhandle, struct efi_object, link);
623 if (efiobj->handle == handle)
624 return efiobj;
625 }
626
627 return NULL;
628}
629
630static efi_status_t EFIAPI efi_unload_image(void *image_handle)
631{
632 struct efi_object *efiobj;
633
634 EFI_ENTRY("%p", image_handle);
635 efiobj = efi_search_obj(image_handle);
636 if (efiobj)
637 list_del(&efiobj->link);
638
639 return EFI_EXIT(EFI_SUCCESS);
640}
641
642static void efi_exit_caches(void)
643{
644#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
645 /*
646 * Grub on 32bit ARM needs to have caches disabled before jumping into
647 * a zImage, but does not know of all cache layers. Give it a hand.
648 */
649 if (efi_is_direct_boot)
650 cleanup_before_linux();
651#endif
652}
653
654static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
655 unsigned long map_key)
656{
657 EFI_ENTRY("%p, %ld", image_handle, map_key);
658
Alexander Grafb7b84102016-11-17 01:02:57 +0100659 board_quiesce_devices();
660
Alexander Grafbee91162016-03-04 01:09:59 +0100661 /* Fix up caches for EFI payloads if necessary */
662 efi_exit_caches();
663
664 /* This stops all lingering devices */
665 bootm_disable_interrupts();
666
667 /* Give the payload some time to boot */
668 WATCHDOG_RESET();
669
670 return EFI_EXIT(EFI_SUCCESS);
671}
672
673static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
674{
675 static uint64_t mono = 0;
676 EFI_ENTRY("%p", count);
677 *count = mono++;
678 return EFI_EXIT(EFI_SUCCESS);
679}
680
681static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
682{
683 EFI_ENTRY("%ld", microseconds);
684 udelay(microseconds);
685 return EFI_EXIT(EFI_SUCCESS);
686}
687
688static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
689 uint64_t watchdog_code,
690 unsigned long data_size,
691 uint16_t *watchdog_data)
692{
693 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
694 data_size, watchdog_data);
695 return EFI_EXIT(efi_unsupported(__func__));
696}
697
698static efi_status_t EFIAPI efi_connect_controller(
699 efi_handle_t controller_handle,
700 efi_handle_t *driver_image_handle,
701 struct efi_device_path *remain_device_path,
702 bool recursive)
703{
704 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
705 remain_device_path, recursive);
706 return EFI_EXIT(EFI_NOT_FOUND);
707}
708
709static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
710 void *driver_image_handle,
711 void *child_handle)
712{
713 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
714 child_handle);
715 return EFI_EXIT(EFI_INVALID_PARAMETER);
716}
717
718static efi_status_t EFIAPI efi_close_protocol(void *handle,
719 efi_guid_t *protocol,
720 void *agent_handle,
721 void *controller_handle)
722{
723 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
724 controller_handle);
725 return EFI_EXIT(EFI_NOT_FOUND);
726}
727
728static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
729 efi_guid_t *protocol,
730 struct efi_open_protocol_info_entry **entry_buffer,
731 unsigned long *entry_count)
732{
733 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
734 entry_count);
735 return EFI_EXIT(EFI_NOT_FOUND);
736}
737
738static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
739 efi_guid_t ***protocol_buffer,
740 unsigned long *protocol_buffer_count)
741{
742 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
743 protocol_buffer_count);
744 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
745}
746
747static efi_status_t EFIAPI efi_locate_handle_buffer(
748 enum efi_locate_search_type search_type,
749 efi_guid_t *protocol, void *search_key,
750 unsigned long *no_handles, efi_handle_t **buffer)
751{
752 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
753 no_handles, buffer);
754 return EFI_EXIT(EFI_NOT_FOUND);
755}
756
757static struct efi_class_map efi_class_maps[] = {
758 {
759 .guid = &efi_guid_console_control,
760 .interface = &efi_console_control
761 },
762};
763
764static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
765 void *registration,
766 void **protocol_interface)
767{
768 int i;
769
770 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
771 for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) {
772 struct efi_class_map *curmap = &efi_class_maps[i];
773 if (!guidcmp(protocol, curmap->guid)) {
774 *protocol_interface = (void*)curmap->interface;
775 return EFI_EXIT(EFI_SUCCESS);
776 }
777 }
778
779 return EFI_EXIT(EFI_NOT_FOUND);
780}
781
782static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
783 void **handle, ...)
784{
785 EFI_ENTRY("%p", handle);
786 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
787}
788
789static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
790 void *handle, ...)
791{
792 EFI_ENTRY("%p", handle);
793 return EFI_EXIT(EFI_INVALID_PARAMETER);
794}
795
796static efi_status_t EFIAPI efi_calculate_crc32(void *data,
797 unsigned long data_size,
798 uint32_t *crc32_p)
799{
800 EFI_ENTRY("%p, %ld", data, data_size);
801 *crc32_p = crc32(0, data, data_size);
802 return EFI_EXIT(EFI_SUCCESS);
803}
804
805static void EFIAPI efi_copy_mem(void *destination, void *source,
806 unsigned long length)
807{
808 EFI_ENTRY("%p, %p, %ld", destination, source, length);
809 memcpy(destination, source, length);
810}
811
812static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
813{
814 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
815 memset(buffer, value, size);
816}
817
818static efi_status_t EFIAPI efi_open_protocol(
819 void *handle, efi_guid_t *protocol,
820 void **protocol_interface, void *agent_handle,
821 void *controller_handle, uint32_t attributes)
822{
823 struct list_head *lhandle;
824 int i;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +0200825 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100826
827 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
828 protocol_interface, agent_handle, controller_handle,
829 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200830
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +0200831 if (!handle || !protocol ||
832 (!protocol_interface && attributes !=
833 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
834 goto out;
835 }
836
837 switch (attributes) {
838 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
839 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
840 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
841 break;
842 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
843 if (controller_handle == handle)
844 goto out;
845 case EFI_OPEN_PROTOCOL_BY_DRIVER:
846 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
847 if (controller_handle == NULL)
848 goto out;
849 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
850 if (agent_handle == NULL)
851 goto out;
852 break;
853 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200854 goto out;
855 }
856
Alexander Grafbee91162016-03-04 01:09:59 +0100857 list_for_each(lhandle, &efi_obj_list) {
858 struct efi_object *efiobj;
859 efiobj = list_entry(lhandle, struct efi_object, link);
860
861 if (efiobj->handle != handle)
862 continue;
863
864 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
865 struct efi_handler *handler = &efiobj->protocols[i];
866 const efi_guid_t *hprotocol = handler->guid;
867 if (!hprotocol)
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +0200868 continue;
Alexander Grafbee91162016-03-04 01:09:59 +0100869 if (!guidcmp(hprotocol, protocol)) {
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +0200870 if (attributes !=
871 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
872 *protocol_interface =
873 handler->protocol_interface;
874 }
875 r = EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100876 goto out;
877 }
878 }
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +0200879 goto unsupported;
Alexander Grafbee91162016-03-04 01:09:59 +0100880 }
881
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +0200882unsupported:
883 r = EFI_UNSUPPORTED;
Alexander Grafbee91162016-03-04 01:09:59 +0100884out:
885 return EFI_EXIT(r);
886}
887
888static efi_status_t EFIAPI efi_handle_protocol(void *handle,
889 efi_guid_t *protocol,
890 void **protocol_interface)
891{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +0200892 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
893 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +0100894}
895
896static const struct efi_boot_services efi_boot_services = {
897 .hdr = {
898 .headersize = sizeof(struct efi_table_hdr),
899 },
900 .raise_tpl = efi_raise_tpl,
901 .restore_tpl = efi_restore_tpl,
902 .allocate_pages = efi_allocate_pages_ext,
903 .free_pages = efi_free_pages_ext,
904 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +0200905 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +0200906 .free_pool = efi_free_pool_ext,
Alexander Grafbee91162016-03-04 01:09:59 +0100907 .create_event = efi_create_event,
908 .set_timer = efi_set_timer,
909 .wait_for_event = efi_wait_for_event,
910 .signal_event = efi_signal_event,
911 .close_event = efi_close_event,
912 .check_event = efi_check_event,
xypron.glpk@gmx.de8bee5a32017-07-11 22:06:18 +0200913 .install_protocol_interface = efi_install_protocol_interface_ext,
Alexander Grafbee91162016-03-04 01:09:59 +0100914 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
xypron.glpk@gmx.de3d8e1452017-07-11 22:06:19 +0200915 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
Alexander Grafbee91162016-03-04 01:09:59 +0100916 .handle_protocol = efi_handle_protocol,
917 .reserved = NULL,
918 .register_protocol_notify = efi_register_protocol_notify,
919 .locate_handle = efi_locate_handle,
920 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +0200921 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +0100922 .load_image = efi_load_image,
923 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200924 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +0100925 .unload_image = efi_unload_image,
926 .exit_boot_services = efi_exit_boot_services,
927 .get_next_monotonic_count = efi_get_next_monotonic_count,
928 .stall = efi_stall,
929 .set_watchdog_timer = efi_set_watchdog_timer,
930 .connect_controller = efi_connect_controller,
931 .disconnect_controller = efi_disconnect_controller,
932 .open_protocol = efi_open_protocol,
933 .close_protocol = efi_close_protocol,
934 .open_protocol_information = efi_open_protocol_information,
935 .protocols_per_handle = efi_protocols_per_handle,
936 .locate_handle_buffer = efi_locate_handle_buffer,
937 .locate_protocol = efi_locate_protocol,
938 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
939 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
940 .calculate_crc32 = efi_calculate_crc32,
941 .copy_mem = efi_copy_mem,
942 .set_mem = efi_set_mem,
943};
944
945
Alexander Graf3c63db92016-10-14 13:45:30 +0200946static uint16_t __efi_runtime_data firmware_vendor[] =
Alexander Grafbee91162016-03-04 01:09:59 +0100947 { 'D','a','s',' ','U','-','b','o','o','t',0 };
948
Alexander Graf3c63db92016-10-14 13:45:30 +0200949struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +0100950 .hdr = {
951 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
952 .revision = 0x20005, /* 2.5 */
953 .headersize = sizeof(struct efi_table_hdr),
954 },
955 .fw_vendor = (long)firmware_vendor,
956 .con_in = (void*)&efi_con_in,
957 .con_out = (void*)&efi_con_out,
958 .std_err = (void*)&efi_con_out,
959 .runtime = (void*)&efi_runtime_services,
960 .boottime = (void*)&efi_boot_services,
961 .nr_tables = 0,
962 .tables = (void*)efi_conf_table,
963};