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