xref: /rk3399_rockchip-uboot/lib/efi_loader/efi_boottime.c (revision c160d2f5ec9298d545a6e0fab0a68cc1a3e93759)
1 /*
2  *  EFI application boot time services
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8 
9 #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 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 /* This list contains all the EFI objects our payload has access to */
22 LIST_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  */
30 static 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  */
40 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
41 
42 #ifdef CONFIG_ARM
43 /*
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  */
49 static volatile void *efi_gd, *app_gd;
50 #endif
51 
52 static int entry_count;
53 
54 /* Called on every callback entry */
55 int __efi_entry_check(void)
56 {
57 	int ret = entry_count++ == 0;
58 #ifdef CONFIG_ARM
59 	assert(efi_gd);
60 	app_gd = gd;
61 	gd = efi_gd;
62 #endif
63 	return ret;
64 }
65 
66 /* Called on every callback exit */
67 int __efi_exit_check(void)
68 {
69 	int ret = --entry_count == 0;
70 #ifdef CONFIG_ARM
71 	gd = app_gd;
72 #endif
73 	return ret;
74 }
75 
76 /* Called from do_bootefi_exec() */
77 void efi_save_gd(void)
78 {
79 #ifdef CONFIG_ARM
80 	efi_gd = gd;
81 #endif
82 }
83 
84 /*
85  * Special case handler for error/abort that just forces things back
86  * to u-boot world so we can dump out an abort msg, without any care
87  * about returning back to UEFI world.
88  */
89 void efi_restore_gd(void)
90 {
91 #ifdef CONFIG_ARM
92 	/* Only restore if we're already in EFI context */
93 	if (!efi_gd)
94 		return;
95 	gd = efi_gd;
96 #endif
97 }
98 
99 /* Low 32 bit */
100 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL)
101 /* High 32 bit */
102 #define EFI_HIGH32(a) (a >> 32)
103 
104 /*
105  * 64bit division by 10 implemented as multiplication by 1 / 10
106  *
107  * Decimals of one tenth: 0x1 / 0xA = 0x0.19999...
108  */
109 #define EFI_TENTH 0x199999999999999A
110 static u64 efi_div10(u64 a)
111 {
112 	u64 prod;
113 	u64 rem;
114 	u64 ret;
115 
116 	ret  = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH);
117 	prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH);
118 	rem  = EFI_LOW32(prod);
119 	ret += EFI_HIGH32(prod);
120 	prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH);
121 	rem += EFI_LOW32(prod);
122 	ret += EFI_HIGH32(prod);
123 	prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH);
124 	rem += EFI_HIGH32(prod);
125 	ret += EFI_HIGH32(rem);
126 	/* Round to nearest integer */
127 	if (rem >= (1 << 31))
128 		++ret;
129 	return ret;
130 }
131 
132 void efi_signal_event(struct efi_event *event)
133 {
134 	if (event->signaled)
135 		return;
136 	event->signaled = 1;
137 	if (event->type & EVT_NOTIFY_SIGNAL) {
138 		EFI_CALL(event->notify_function(event, event->notify_context));
139 	}
140 }
141 
142 static efi_status_t efi_unsupported(const char *funcname)
143 {
144 	debug("EFI: App called into unimplemented function %s\n", funcname);
145 	return EFI_EXIT(EFI_UNSUPPORTED);
146 }
147 
148 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
149 {
150 	EFI_ENTRY("0x%zx", new_tpl);
151 	return EFI_EXIT(0);
152 }
153 
154 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
155 {
156 	EFI_ENTRY("0x%zx", old_tpl);
157 	efi_unsupported(__func__);
158 }
159 
160 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
161 						  unsigned long pages,
162 						  uint64_t *memory)
163 {
164 	efi_status_t r;
165 
166 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
167 	r = efi_allocate_pages(type, memory_type, pages, memory);
168 	return EFI_EXIT(r);
169 }
170 
171 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
172 					      unsigned long pages)
173 {
174 	efi_status_t r;
175 
176 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
177 	r = efi_free_pages(memory, pages);
178 	return EFI_EXIT(r);
179 }
180 
181 static efi_status_t EFIAPI efi_get_memory_map_ext(
182 					unsigned long *memory_map_size,
183 					struct efi_mem_desc *memory_map,
184 					unsigned long *map_key,
185 					unsigned long *descriptor_size,
186 					uint32_t *descriptor_version)
187 {
188 	efi_status_t r;
189 
190 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
191 		  map_key, descriptor_size, descriptor_version);
192 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
193 			       descriptor_size, descriptor_version);
194 	return EFI_EXIT(r);
195 }
196 
197 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
198 						 unsigned long size,
199 						 void **buffer)
200 {
201 	efi_status_t r;
202 
203 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
204 	r = efi_allocate_pool(pool_type, size, buffer);
205 	return EFI_EXIT(r);
206 }
207 
208 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
209 {
210 	efi_status_t r;
211 
212 	EFI_ENTRY("%p", buffer);
213 	r = efi_free_pool(buffer);
214 	return EFI_EXIT(r);
215 }
216 
217 /*
218  * Our event capabilities are very limited. Only a small limited
219  * number of events is allowed to coexist.
220  */
221 static struct efi_event efi_events[16];
222 
223 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
224 			      void (EFIAPI *notify_function) (
225 					struct efi_event *event,
226 					void *context),
227 			      void *notify_context, struct efi_event **event)
228 {
229 	int i;
230 
231 	if (event == NULL)
232 		return EFI_INVALID_PARAMETER;
233 
234 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
235 		return EFI_INVALID_PARAMETER;
236 
237 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
238 	    notify_function == NULL)
239 		return EFI_INVALID_PARAMETER;
240 
241 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
242 		if (efi_events[i].type)
243 			continue;
244 		efi_events[i].type = type;
245 		efi_events[i].notify_tpl = notify_tpl;
246 		efi_events[i].notify_function = notify_function;
247 		efi_events[i].notify_context = notify_context;
248 		/* Disable timers on bootup */
249 		efi_events[i].trigger_next = -1ULL;
250 		efi_events[i].signaled = 0;
251 		*event = &efi_events[i];
252 		return EFI_SUCCESS;
253 	}
254 	return EFI_OUT_OF_RESOURCES;
255 }
256 
257 static efi_status_t EFIAPI efi_create_event_ext(
258 			uint32_t type, UINTN notify_tpl,
259 			void (EFIAPI *notify_function) (
260 					struct efi_event *event,
261 					void *context),
262 			void *notify_context, struct efi_event **event)
263 {
264 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
265 		  notify_context);
266 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
267 					 notify_context, event));
268 }
269 
270 
271 /*
272  * Our timers have to work without interrupts, so we check whenever keyboard
273  * input or disk accesses happen if enough time elapsed for it to fire.
274  */
275 void efi_timer_check(void)
276 {
277 	int i;
278 	u64 now = timer_get_us();
279 
280 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
281 		if (!efi_events[i].type ||
282 		    !(efi_events[i].type & EVT_TIMER) ||
283 		    efi_events[i].trigger_type == EFI_TIMER_STOP ||
284 		    now < efi_events[i].trigger_next)
285 			continue;
286 		if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) {
287 			efi_events[i].trigger_next +=
288 				efi_events[i].trigger_time;
289 			efi_events[i].signaled = 0;
290 		}
291 		efi_signal_event(&efi_events[i]);
292 	}
293 	WATCHDOG_RESET();
294 }
295 
296 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
297 			   uint64_t trigger_time)
298 {
299 	int i;
300 
301 	/*
302 	 * The parameter defines a multiple of 100ns.
303 	 * We use multiples of 1000ns. So divide by 10.
304 	 */
305 	trigger_time = efi_div10(trigger_time);
306 
307 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
308 		if (event != &efi_events[i])
309 			continue;
310 
311 		if (!(event->type & EVT_TIMER))
312 			break;
313 		switch (type) {
314 		case EFI_TIMER_STOP:
315 			event->trigger_next = -1ULL;
316 			break;
317 		case EFI_TIMER_PERIODIC:
318 		case EFI_TIMER_RELATIVE:
319 			event->trigger_next =
320 				timer_get_us() + trigger_time;
321 			break;
322 		default:
323 			return EFI_INVALID_PARAMETER;
324 		}
325 		event->trigger_type = type;
326 		event->trigger_time = trigger_time;
327 		return EFI_SUCCESS;
328 	}
329 	return EFI_INVALID_PARAMETER;
330 }
331 
332 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
333 					     enum efi_timer_delay type,
334 					     uint64_t trigger_time)
335 {
336 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
337 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
338 }
339 
340 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
341 					      struct efi_event **event,
342 					      unsigned long *index)
343 {
344 	int i, j;
345 
346 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
347 
348 	/* Check parameters */
349 	if (!num_events || !event)
350 		return EFI_EXIT(EFI_INVALID_PARAMETER);
351 	for (i = 0; i < num_events; ++i) {
352 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
353 			if (event[i] == &efi_events[j])
354 				goto known_event;
355 		}
356 		return EFI_EXIT(EFI_INVALID_PARAMETER);
357 known_event:
358 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
359 			return EFI_EXIT(EFI_INVALID_PARAMETER);
360 	}
361 
362 	/* Wait for signal */
363 	for (;;) {
364 		for (i = 0; i < num_events; ++i) {
365 			if (event[i]->signaled)
366 				goto out;
367 		}
368 		/* Allow events to occur. */
369 		efi_timer_check();
370 	}
371 
372 out:
373 	/*
374 	 * Reset the signal which is passed to the caller to allow periodic
375 	 * events to occur.
376 	 */
377 	event[i]->signaled = 0;
378 	if (index)
379 		*index = i;
380 
381 	return EFI_EXIT(EFI_SUCCESS);
382 }
383 
384 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
385 {
386 	int i;
387 
388 	EFI_ENTRY("%p", event);
389 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
390 		if (event != &efi_events[i])
391 			continue;
392 		efi_signal_event(event);
393 		break;
394 	}
395 	return EFI_EXIT(EFI_SUCCESS);
396 }
397 
398 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
399 {
400 	int i;
401 
402 	EFI_ENTRY("%p", event);
403 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
404 		if (event == &efi_events[i]) {
405 			event->type = 0;
406 			event->trigger_next = -1ULL;
407 			event->signaled = 0;
408 			return EFI_EXIT(EFI_SUCCESS);
409 		}
410 	}
411 	return EFI_EXIT(EFI_INVALID_PARAMETER);
412 }
413 
414 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
415 {
416 	int i;
417 
418 	EFI_ENTRY("%p", event);
419 	efi_timer_check();
420 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
421 		if (event != &efi_events[i])
422 			continue;
423 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
424 			break;
425 		if (event->signaled)
426 			return EFI_EXIT(EFI_SUCCESS);
427 		return EFI_EXIT(EFI_NOT_READY);
428 	}
429 	return EFI_EXIT(EFI_INVALID_PARAMETER);
430 }
431 
432 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
433 			efi_guid_t *protocol, int protocol_interface_type,
434 			void *protocol_interface)
435 {
436 	struct list_head *lhandle;
437 	int i;
438 	efi_status_t r;
439 
440 	if (!handle || !protocol ||
441 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
442 		r = EFI_INVALID_PARAMETER;
443 		goto out;
444 	}
445 
446 	/* Create new handle if requested. */
447 	if (!*handle) {
448 		r = EFI_OUT_OF_RESOURCES;
449 		goto out;
450 	}
451 	/* Find object. */
452 	list_for_each(lhandle, &efi_obj_list) {
453 		struct efi_object *efiobj;
454 		efiobj = list_entry(lhandle, struct efi_object, link);
455 
456 		if (efiobj->handle != *handle)
457 			continue;
458 		/* Check if protocol is already installed on the handle. */
459 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
460 			struct efi_handler *handler = &efiobj->protocols[i];
461 
462 			if (!handler->guid)
463 				continue;
464 			if (!guidcmp(handler->guid, protocol)) {
465 				r = EFI_INVALID_PARAMETER;
466 				goto out;
467 			}
468 		}
469 		/* Install protocol in first empty slot. */
470 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
471 			struct efi_handler *handler = &efiobj->protocols[i];
472 
473 			if (handler->guid)
474 				continue;
475 
476 			handler->guid = protocol;
477 			handler->protocol_interface = protocol_interface;
478 			r = EFI_SUCCESS;
479 			goto out;
480 		}
481 		r = EFI_OUT_OF_RESOURCES;
482 		goto out;
483 	}
484 	r = EFI_INVALID_PARAMETER;
485 out:
486 	return r;
487 }
488 
489 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
490 			efi_guid_t *protocol, int protocol_interface_type,
491 			void *protocol_interface)
492 {
493 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
494 		  protocol_interface);
495 
496 	return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
497 						       protocol_interface_type,
498 						       protocol_interface));
499 }
500 
501 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
502 			efi_guid_t *protocol, void *old_interface,
503 			void *new_interface)
504 {
505 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
506 		  new_interface);
507 	return EFI_EXIT(EFI_ACCESS_DENIED);
508 }
509 
510 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
511 			efi_guid_t *protocol, void *protocol_interface)
512 {
513 	struct list_head *lhandle;
514 	int i;
515 	efi_status_t r = EFI_NOT_FOUND;
516 
517 	if (!handle || !protocol) {
518 		r = EFI_INVALID_PARAMETER;
519 		goto out;
520 	}
521 
522 	list_for_each(lhandle, &efi_obj_list) {
523 		struct efi_object *efiobj;
524 		efiobj = list_entry(lhandle, struct efi_object, link);
525 
526 		if (efiobj->handle != handle)
527 			continue;
528 
529 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
530 			struct efi_handler *handler = &efiobj->protocols[i];
531 			const efi_guid_t *hprotocol = handler->guid;
532 
533 			if (!hprotocol)
534 				continue;
535 			if (!guidcmp(hprotocol, protocol)) {
536 				if (handler->protocol_interface) {
537 					r = EFI_ACCESS_DENIED;
538 				} else {
539 					handler->guid = 0;
540 					r = EFI_SUCCESS;
541 				}
542 				goto out;
543 			}
544 		}
545 	}
546 
547 out:
548 	return r;
549 }
550 
551 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
552 			efi_guid_t *protocol, void *protocol_interface)
553 {
554 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
555 
556 	return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
557 							 protocol_interface));
558 }
559 
560 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
561 							struct efi_event *event,
562 							void **registration)
563 {
564 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
565 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
566 }
567 
568 static int efi_search(enum efi_locate_search_type search_type,
569 		      efi_guid_t *protocol, void *search_key,
570 		      struct efi_object *efiobj)
571 {
572 	int i;
573 
574 	switch (search_type) {
575 	case all_handles:
576 		return 0;
577 	case by_register_notify:
578 		return -1;
579 	case by_protocol:
580 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
581 			const efi_guid_t *guid = efiobj->protocols[i].guid;
582 			if (guid && !guidcmp(guid, protocol))
583 				return 0;
584 		}
585 		return -1;
586 	}
587 
588 	return -1;
589 }
590 
591 static efi_status_t EFIAPI efi_locate_handle(
592 			enum efi_locate_search_type search_type,
593 			efi_guid_t *protocol, void *search_key,
594 			unsigned long *buffer_size, efi_handle_t *buffer)
595 {
596 	struct list_head *lhandle;
597 	unsigned long size = 0;
598 
599 	/* Count how much space we need */
600 	list_for_each(lhandle, &efi_obj_list) {
601 		struct efi_object *efiobj;
602 		efiobj = list_entry(lhandle, struct efi_object, link);
603 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
604 			size += sizeof(void*);
605 		}
606 	}
607 
608 	if (*buffer_size < size) {
609 		*buffer_size = size;
610 		return EFI_BUFFER_TOO_SMALL;
611 	}
612 
613 	/* Then fill the array */
614 	list_for_each(lhandle, &efi_obj_list) {
615 		struct efi_object *efiobj;
616 		efiobj = list_entry(lhandle, struct efi_object, link);
617 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
618 			*(buffer++) = efiobj->handle;
619 		}
620 	}
621 
622 	*buffer_size = size;
623 	return EFI_SUCCESS;
624 }
625 
626 static efi_status_t EFIAPI efi_locate_handle_ext(
627 			enum efi_locate_search_type search_type,
628 			efi_guid_t *protocol, void *search_key,
629 			unsigned long *buffer_size, efi_handle_t *buffer)
630 {
631 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
632 		  buffer_size, buffer);
633 
634 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
635 			buffer_size, buffer));
636 }
637 
638 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
639 			struct efi_device_path **device_path,
640 			efi_handle_t *device)
641 {
642 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
643 	return EFI_EXIT(EFI_NOT_FOUND);
644 }
645 
646 /* Collapses configuration table entries, removing index i */
647 static void efi_remove_configuration_table(int i)
648 {
649 	struct efi_configuration_table *this = &efi_conf_table[i];
650 	struct efi_configuration_table *next = &efi_conf_table[i+1];
651 	struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
652 
653 	memmove(this, next, (ulong)end - (ulong)next);
654 	systab.nr_tables--;
655 }
656 
657 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
658 {
659 	int i;
660 
661 	/* Check for guid override */
662 	for (i = 0; i < systab.nr_tables; i++) {
663 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
664 			if (table)
665 				efi_conf_table[i].table = table;
666 			else
667 				efi_remove_configuration_table(i);
668 			return EFI_SUCCESS;
669 		}
670 	}
671 
672 	if (!table)
673 		return EFI_NOT_FOUND;
674 
675 	/* No override, check for overflow */
676 	if (i >= ARRAY_SIZE(efi_conf_table))
677 		return EFI_OUT_OF_RESOURCES;
678 
679 	/* Add a new entry */
680 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
681 	efi_conf_table[i].table = table;
682 	systab.nr_tables = i + 1;
683 
684 	return EFI_SUCCESS;
685 }
686 
687 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
688 							       void *table)
689 {
690 	EFI_ENTRY("%p, %p", guid, table);
691 	return EFI_EXIT(efi_install_configuration_table(guid, table));
692 }
693 
694 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
695 					  efi_handle_t parent_image,
696 					  struct efi_device_path *file_path,
697 					  void *source_buffer,
698 					  unsigned long source_size,
699 					  efi_handle_t *image_handle)
700 {
701 	static struct efi_object loaded_image_info_obj = {
702 		.protocols = {
703 			{
704 				.guid = &efi_guid_loaded_image,
705 			},
706 		},
707 	};
708 	struct efi_loaded_image *info;
709 	struct efi_object *obj;
710 
711 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
712 		  file_path, source_buffer, source_size, image_handle);
713 	info = malloc(sizeof(*info));
714 	loaded_image_info_obj.protocols[0].protocol_interface = info;
715 	obj = malloc(sizeof(loaded_image_info_obj));
716 	memset(info, 0, sizeof(*info));
717 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
718 	obj->handle = info;
719 	info->file_path = file_path;
720 	info->reserved = efi_load_pe(source_buffer, info);
721 	if (!info->reserved) {
722 		free(info);
723 		free(obj);
724 		return EFI_EXIT(EFI_UNSUPPORTED);
725 	}
726 
727 	*image_handle = info;
728 	list_add_tail(&obj->link, &efi_obj_list);
729 
730 	return EFI_EXIT(EFI_SUCCESS);
731 }
732 
733 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
734 					   unsigned long *exit_data_size,
735 					   s16 **exit_data)
736 {
737 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
738 	struct efi_loaded_image *info = image_handle;
739 
740 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
741 	entry = info->reserved;
742 
743 	efi_is_direct_boot = false;
744 
745 	/* call the image! */
746 	if (setjmp(&info->exit_jmp)) {
747 		/* We returned from the child image */
748 		return EFI_EXIT(info->exit_status);
749 	}
750 
751 	__efi_exit_check();
752 	entry(image_handle, &systab);
753 	__efi_entry_check();
754 
755 	/* Should usually never get here */
756 	return EFI_EXIT(EFI_SUCCESS);
757 }
758 
759 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
760 			efi_status_t exit_status, unsigned long exit_data_size,
761 			int16_t *exit_data)
762 {
763 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
764 
765 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
766 		  exit_data_size, exit_data);
767 
768 	loaded_image_info->exit_status = exit_status;
769 	longjmp(&loaded_image_info->exit_jmp, 1);
770 
771 	panic("EFI application exited");
772 }
773 
774 static struct efi_object *efi_search_obj(void *handle)
775 {
776 	struct list_head *lhandle;
777 
778 	list_for_each(lhandle, &efi_obj_list) {
779 		struct efi_object *efiobj;
780 		efiobj = list_entry(lhandle, struct efi_object, link);
781 		if (efiobj->handle == handle)
782 			return efiobj;
783 	}
784 
785 	return NULL;
786 }
787 
788 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
789 {
790 	struct efi_object *efiobj;
791 
792 	EFI_ENTRY("%p", image_handle);
793 	efiobj = efi_search_obj(image_handle);
794 	if (efiobj)
795 		list_del(&efiobj->link);
796 
797 	return EFI_EXIT(EFI_SUCCESS);
798 }
799 
800 static void efi_exit_caches(void)
801 {
802 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
803 	/*
804 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
805 	 * a zImage, but does not know of all cache layers. Give it a hand.
806 	 */
807 	if (efi_is_direct_boot)
808 		cleanup_before_linux();
809 #endif
810 }
811 
812 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
813 						  unsigned long map_key)
814 {
815 	EFI_ENTRY("%p, %ld", image_handle, map_key);
816 
817 	board_quiesce_devices();
818 
819 	/* Fix up caches for EFI payloads if necessary */
820 	efi_exit_caches();
821 
822 	/* This stops all lingering devices */
823 	bootm_disable_interrupts();
824 
825 	/* Give the payload some time to boot */
826 	WATCHDOG_RESET();
827 
828 	return EFI_EXIT(EFI_SUCCESS);
829 }
830 
831 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
832 {
833 	static uint64_t mono = 0;
834 	EFI_ENTRY("%p", count);
835 	*count = mono++;
836 	return EFI_EXIT(EFI_SUCCESS);
837 }
838 
839 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
840 {
841 	EFI_ENTRY("%ld", microseconds);
842 	udelay(microseconds);
843 	return EFI_EXIT(EFI_SUCCESS);
844 }
845 
846 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
847 						  uint64_t watchdog_code,
848 						  unsigned long data_size,
849 						  uint16_t *watchdog_data)
850 {
851 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
852 		  data_size, watchdog_data);
853 	return efi_unsupported(__func__);
854 }
855 
856 static efi_status_t EFIAPI efi_connect_controller(
857 			efi_handle_t controller_handle,
858 			efi_handle_t *driver_image_handle,
859 			struct efi_device_path *remain_device_path,
860 			bool recursive)
861 {
862 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
863 		  remain_device_path, recursive);
864 	return EFI_EXIT(EFI_NOT_FOUND);
865 }
866 
867 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
868 						     void *driver_image_handle,
869 						     void *child_handle)
870 {
871 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
872 		  child_handle);
873 	return EFI_EXIT(EFI_INVALID_PARAMETER);
874 }
875 
876 static efi_status_t EFIAPI efi_close_protocol(void *handle,
877 					      efi_guid_t *protocol,
878 					      void *agent_handle,
879 					      void *controller_handle)
880 {
881 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
882 		  controller_handle);
883 	return EFI_EXIT(EFI_NOT_FOUND);
884 }
885 
886 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
887 			efi_guid_t *protocol,
888 			struct efi_open_protocol_info_entry **entry_buffer,
889 			unsigned long *entry_count)
890 {
891 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
892 		  entry_count);
893 	return EFI_EXIT(EFI_NOT_FOUND);
894 }
895 
896 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
897 			efi_guid_t ***protocol_buffer,
898 			unsigned long *protocol_buffer_count)
899 {
900 	unsigned long buffer_size;
901 	struct efi_object *efiobj;
902 	unsigned long i, j;
903 	struct list_head *lhandle;
904 	efi_status_t r;
905 
906 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
907 		  protocol_buffer_count);
908 
909 	if (!handle || !protocol_buffer || !protocol_buffer_count)
910 		return EFI_EXIT(EFI_INVALID_PARAMETER);
911 
912 	*protocol_buffer = NULL;
913 	*protocol_buffer_count = 0;
914 	list_for_each(lhandle, &efi_obj_list) {
915 		efiobj = list_entry(lhandle, struct efi_object, link);
916 
917 		if (efiobj->handle != handle)
918 			continue;
919 
920 		/* Count protocols */
921 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
922 			if (efiobj->protocols[i].guid)
923 				++*protocol_buffer_count;
924 		}
925 		/* Copy guids */
926 		if (*protocol_buffer_count) {
927 			buffer_size = sizeof(efi_guid_t *) *
928 					*protocol_buffer_count;
929 			r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
930 					      buffer_size,
931 					      (void **)protocol_buffer);
932 			if (r != EFI_SUCCESS)
933 				return EFI_EXIT(r);
934 			j = 0;
935 			for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
936 				if (efiobj->protocols[i].guid) {
937 					(*protocol_buffer)[j] = (void *)
938 						efiobj->protocols[i].guid;
939 					++j;
940 				}
941 			}
942 		}
943 		break;
944 	}
945 
946 	return EFI_EXIT(EFI_SUCCESS);
947 }
948 
949 static efi_status_t EFIAPI efi_locate_handle_buffer(
950 			enum efi_locate_search_type search_type,
951 			efi_guid_t *protocol, void *search_key,
952 			unsigned long *no_handles, efi_handle_t **buffer)
953 {
954 	efi_status_t r;
955 	unsigned long buffer_size = 0;
956 
957 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
958 		  no_handles, buffer);
959 
960 	if (!no_handles || !buffer) {
961 		r = EFI_INVALID_PARAMETER;
962 		goto out;
963 	}
964 	*no_handles = 0;
965 	*buffer = NULL;
966 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
967 			      *buffer);
968 	if (r != EFI_BUFFER_TOO_SMALL)
969 		goto out;
970 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
971 			      (void **)buffer);
972 	if (r != EFI_SUCCESS)
973 		goto out;
974 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
975 			      *buffer);
976 	if (r == EFI_SUCCESS)
977 		*no_handles = buffer_size / sizeof(void *);
978 out:
979 	return EFI_EXIT(r);
980 }
981 
982 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
983 					       void *registration,
984 					       void **protocol_interface)
985 {
986 	struct list_head *lhandle;
987 	int i;
988 
989 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
990 
991 	if (!protocol || !protocol_interface)
992 		return EFI_EXIT(EFI_INVALID_PARAMETER);
993 
994 	list_for_each(lhandle, &efi_obj_list) {
995 		struct efi_object *efiobj;
996 
997 		efiobj = list_entry(lhandle, struct efi_object, link);
998 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
999 			struct efi_handler *handler = &efiobj->protocols[i];
1000 
1001 			if (!handler->guid)
1002 				continue;
1003 			if (!guidcmp(handler->guid, protocol)) {
1004 				*protocol_interface =
1005 					handler->protocol_interface;
1006 				return EFI_EXIT(EFI_SUCCESS);
1007 			}
1008 		}
1009 	}
1010 	*protocol_interface = NULL;
1011 
1012 	return EFI_EXIT(EFI_NOT_FOUND);
1013 }
1014 
1015 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1016 			void **handle, ...)
1017 {
1018 	EFI_ENTRY("%p", handle);
1019 
1020 	va_list argptr;
1021 	efi_guid_t *protocol;
1022 	void *protocol_interface;
1023 	efi_status_t r = EFI_SUCCESS;
1024 	int i = 0;
1025 
1026 	if (!handle)
1027 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1028 
1029 	va_start(argptr, handle);
1030 	for (;;) {
1031 		protocol = va_arg(argptr, efi_guid_t*);
1032 		if (!protocol)
1033 			break;
1034 		protocol_interface = va_arg(argptr, void*);
1035 		r = efi_install_protocol_interface(handle, protocol,
1036 						   EFI_NATIVE_INTERFACE,
1037 						   protocol_interface);
1038 		if (r != EFI_SUCCESS)
1039 			break;
1040 		i++;
1041 	}
1042 	va_end(argptr);
1043 	if (r == EFI_SUCCESS)
1044 		return EFI_EXIT(r);
1045 
1046 	/* If an error occured undo all changes. */
1047 	va_start(argptr, handle);
1048 	for (; i; --i) {
1049 		protocol = va_arg(argptr, efi_guid_t*);
1050 		protocol_interface = va_arg(argptr, void*);
1051 		efi_uninstall_protocol_interface(handle, protocol,
1052 						 protocol_interface);
1053 	}
1054 	va_end(argptr);
1055 
1056 	return EFI_EXIT(r);
1057 }
1058 
1059 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1060 			void *handle, ...)
1061 {
1062 	EFI_ENTRY("%p", handle);
1063 	return EFI_EXIT(EFI_INVALID_PARAMETER);
1064 }
1065 
1066 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1067 					       unsigned long data_size,
1068 					       uint32_t *crc32_p)
1069 {
1070 	EFI_ENTRY("%p, %ld", data, data_size);
1071 	*crc32_p = crc32(0, data, data_size);
1072 	return EFI_EXIT(EFI_SUCCESS);
1073 }
1074 
1075 static void EFIAPI efi_copy_mem(void *destination, void *source,
1076 				unsigned long length)
1077 {
1078 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
1079 	memcpy(destination, source, length);
1080 }
1081 
1082 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1083 {
1084 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1085 	memset(buffer, value, size);
1086 }
1087 
1088 static efi_status_t EFIAPI efi_open_protocol(
1089 			void *handle, efi_guid_t *protocol,
1090 			void **protocol_interface, void *agent_handle,
1091 			void *controller_handle, uint32_t attributes)
1092 {
1093 	struct list_head *lhandle;
1094 	int i;
1095 	efi_status_t r = EFI_INVALID_PARAMETER;
1096 
1097 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
1098 		  protocol_interface, agent_handle, controller_handle,
1099 		  attributes);
1100 
1101 	if (!handle || !protocol ||
1102 	    (!protocol_interface && attributes !=
1103 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1104 		goto out;
1105 	}
1106 
1107 	switch (attributes) {
1108 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1109 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1110 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1111 		break;
1112 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1113 		if (controller_handle == handle)
1114 			goto out;
1115 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
1116 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1117 		if (controller_handle == NULL)
1118 			goto out;
1119 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1120 		if (agent_handle == NULL)
1121 			goto out;
1122 		break;
1123 	default:
1124 		goto out;
1125 	}
1126 
1127 	list_for_each(lhandle, &efi_obj_list) {
1128 		struct efi_object *efiobj;
1129 		efiobj = list_entry(lhandle, struct efi_object, link);
1130 
1131 		if (efiobj->handle != handle)
1132 			continue;
1133 
1134 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1135 			struct efi_handler *handler = &efiobj->protocols[i];
1136 			const efi_guid_t *hprotocol = handler->guid;
1137 			if (!hprotocol)
1138 				continue;
1139 			if (!guidcmp(hprotocol, protocol)) {
1140 				if (attributes !=
1141 				    EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1142 					*protocol_interface =
1143 						handler->protocol_interface;
1144 				}
1145 				r = EFI_SUCCESS;
1146 				goto out;
1147 			}
1148 		}
1149 		goto unsupported;
1150 	}
1151 
1152 unsupported:
1153 	r = EFI_UNSUPPORTED;
1154 out:
1155 	return EFI_EXIT(r);
1156 }
1157 
1158 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1159 					       efi_guid_t *protocol,
1160 					       void **protocol_interface)
1161 {
1162 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1163 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1164 }
1165 
1166 static const struct efi_boot_services efi_boot_services = {
1167 	.hdr = {
1168 		.headersize = sizeof(struct efi_table_hdr),
1169 	},
1170 	.raise_tpl = efi_raise_tpl,
1171 	.restore_tpl = efi_restore_tpl,
1172 	.allocate_pages = efi_allocate_pages_ext,
1173 	.free_pages = efi_free_pages_ext,
1174 	.get_memory_map = efi_get_memory_map_ext,
1175 	.allocate_pool = efi_allocate_pool_ext,
1176 	.free_pool = efi_free_pool_ext,
1177 	.create_event = efi_create_event_ext,
1178 	.set_timer = efi_set_timer_ext,
1179 	.wait_for_event = efi_wait_for_event,
1180 	.signal_event = efi_signal_event_ext,
1181 	.close_event = efi_close_event,
1182 	.check_event = efi_check_event,
1183 	.install_protocol_interface = efi_install_protocol_interface_ext,
1184 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
1185 	.uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
1186 	.handle_protocol = efi_handle_protocol,
1187 	.reserved = NULL,
1188 	.register_protocol_notify = efi_register_protocol_notify,
1189 	.locate_handle = efi_locate_handle_ext,
1190 	.locate_device_path = efi_locate_device_path,
1191 	.install_configuration_table = efi_install_configuration_table_ext,
1192 	.load_image = efi_load_image,
1193 	.start_image = efi_start_image,
1194 	.exit = efi_exit,
1195 	.unload_image = efi_unload_image,
1196 	.exit_boot_services = efi_exit_boot_services,
1197 	.get_next_monotonic_count = efi_get_next_monotonic_count,
1198 	.stall = efi_stall,
1199 	.set_watchdog_timer = efi_set_watchdog_timer,
1200 	.connect_controller = efi_connect_controller,
1201 	.disconnect_controller = efi_disconnect_controller,
1202 	.open_protocol = efi_open_protocol,
1203 	.close_protocol = efi_close_protocol,
1204 	.open_protocol_information = efi_open_protocol_information,
1205 	.protocols_per_handle = efi_protocols_per_handle,
1206 	.locate_handle_buffer = efi_locate_handle_buffer,
1207 	.locate_protocol = efi_locate_protocol,
1208 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
1209 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
1210 	.calculate_crc32 = efi_calculate_crc32,
1211 	.copy_mem = efi_copy_mem,
1212 	.set_mem = efi_set_mem,
1213 };
1214 
1215 
1216 static uint16_t __efi_runtime_data firmware_vendor[] =
1217 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
1218 
1219 struct efi_system_table __efi_runtime_data systab = {
1220 	.hdr = {
1221 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
1222 		.revision = 0x20005, /* 2.5 */
1223 		.headersize = sizeof(struct efi_table_hdr),
1224 	},
1225 	.fw_vendor = (long)firmware_vendor,
1226 	.con_in = (void*)&efi_con_in,
1227 	.con_out = (void*)&efi_con_out,
1228 	.std_err = (void*)&efi_con_out,
1229 	.runtime = (void*)&efi_runtime_services,
1230 	.boottime = (void*)&efi_boot_services,
1231 	.nr_tables = 0,
1232 	.tables = (void*)efi_conf_table,
1233 };
1234