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 <div64.h>
11 #include <efi_loader.h>
12 #include <environment.h>
13 #include <malloc.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <u-boot/crc.h>
17 #include <bootm.h>
18 #include <inttypes.h>
19 #include <watchdog.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
25 
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
28 
29 /*
30  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31  * we need to do trickery with caches. Since we don't want to break the EFI
32  * aware boot path, only apply hacks when loading exiting directly (breaking
33  * direct Linux EFI booting along the way - oh well).
34  */
35 static bool efi_is_direct_boot = true;
36 
37 /*
38  * EFI can pass arbitrary additional "tables" containing vendor specific
39  * information to the payload. One such table is the FDT table which contains
40  * a pointer to a flattened device tree blob.
41  *
42  * In most cases we want to pass an FDT to the payload, so reserve one slot of
43  * config table space for it. The pointer gets populated by do_bootefi_exec().
44  */
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
46 
47 #ifdef CONFIG_ARM
48 /*
49  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50  * fixed when compiling U-Boot. However, the payload does not know about that
51  * restriction so we need to manually swap its and our view of that register on
52  * EFI callback entry/exit.
53  */
54 static volatile void *efi_gd, *app_gd;
55 #endif
56 
57 static int entry_count;
58 static int nesting_level;
59 
60 /* Called on every callback entry */
61 int __efi_entry_check(void)
62 {
63 	int ret = entry_count++ == 0;
64 #ifdef CONFIG_ARM
65 	assert(efi_gd);
66 	app_gd = gd;
67 	gd = efi_gd;
68 #endif
69 	return ret;
70 }
71 
72 /* Called on every callback exit */
73 int __efi_exit_check(void)
74 {
75 	int ret = --entry_count == 0;
76 #ifdef CONFIG_ARM
77 	gd = app_gd;
78 #endif
79 	return ret;
80 }
81 
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
84 {
85 #ifdef CONFIG_ARM
86 	efi_gd = gd;
87 #endif
88 }
89 
90 /*
91  * Special case handler for error/abort that just forces things back
92  * to u-boot world so we can dump out an abort msg, without any care
93  * about returning back to UEFI world.
94  */
95 void efi_restore_gd(void)
96 {
97 #ifdef CONFIG_ARM
98 	/* Only restore if we're already in EFI context */
99 	if (!efi_gd)
100 		return;
101 	gd = efi_gd;
102 #endif
103 }
104 
105 /*
106  * Two spaces per indent level, maxing out at 10.. which ought to be
107  * enough for anyone ;-)
108  */
109 static const char *indent_string(int level)
110 {
111 	const char *indent = "                    ";
112 	const int max = strlen(indent);
113 	level = min(max, level * 2);
114 	return &indent[max - level];
115 }
116 
117 const char *__efi_nesting(void)
118 {
119 	return indent_string(nesting_level);
120 }
121 
122 const char *__efi_nesting_inc(void)
123 {
124 	return indent_string(nesting_level++);
125 }
126 
127 const char *__efi_nesting_dec(void)
128 {
129 	return indent_string(--nesting_level);
130 }
131 
132 /*
133  * Queue an EFI event.
134  *
135  * This function queues the notification function of the event for future
136  * execution.
137  *
138  * The notification function is called if the task priority level of the
139  * event is higher than the current task priority level.
140  *
141  * For the SignalEvent service see efi_signal_event_ext.
142  *
143  * @event	event to signal
144  */
145 void efi_signal_event(struct efi_event *event)
146 {
147 	if (event->notify_function) {
148 		event->is_queued = true;
149 		/* Check TPL */
150 		if (efi_tpl >= event->notify_tpl)
151 			return;
152 		EFI_CALL_VOID(event->notify_function(event,
153 						     event->notify_context));
154 	}
155 	event->is_queued = false;
156 }
157 
158 /*
159  * Raise the task priority level.
160  *
161  * This function implements the RaiseTpl service.
162  * See the Unified Extensible Firmware Interface (UEFI) specification
163  * for details.
164  *
165  * @new_tpl	new value of the task priority level
166  * @return	old value of the task priority level
167  */
168 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
169 {
170 	efi_uintn_t old_tpl = efi_tpl;
171 
172 	EFI_ENTRY("0x%zx", new_tpl);
173 
174 	if (new_tpl < efi_tpl)
175 		debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
176 	efi_tpl = new_tpl;
177 	if (efi_tpl > TPL_HIGH_LEVEL)
178 		efi_tpl = TPL_HIGH_LEVEL;
179 
180 	EFI_EXIT(EFI_SUCCESS);
181 	return old_tpl;
182 }
183 
184 /*
185  * Lower the task priority level.
186  *
187  * This function implements the RestoreTpl service.
188  * See the Unified Extensible Firmware Interface (UEFI) specification
189  * for details.
190  *
191  * @old_tpl	value of the task priority level to be restored
192  */
193 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
194 {
195 	EFI_ENTRY("0x%zx", old_tpl);
196 
197 	if (old_tpl > efi_tpl)
198 		debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
199 	efi_tpl = old_tpl;
200 	if (efi_tpl > TPL_HIGH_LEVEL)
201 		efi_tpl = TPL_HIGH_LEVEL;
202 
203 	EFI_EXIT(EFI_SUCCESS);
204 }
205 
206 /*
207  * Allocate memory pages.
208  *
209  * This function implements the AllocatePages service.
210  * See the Unified Extensible Firmware Interface (UEFI) specification
211  * for details.
212  *
213  * @type		type of allocation to be performed
214  * @memory_type		usage type of the allocated memory
215  * @pages		number of pages to be allocated
216  * @memory		allocated memory
217  * @return		status code
218  */
219 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
220 						  efi_uintn_t pages,
221 						  uint64_t *memory)
222 {
223 	efi_status_t r;
224 
225 	EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
226 	r = efi_allocate_pages(type, memory_type, pages, memory);
227 	return EFI_EXIT(r);
228 }
229 
230 /*
231  * Free memory pages.
232  *
233  * This function implements the FreePages service.
234  * See the Unified Extensible Firmware Interface (UEFI) specification
235  * for details.
236  *
237  * @memory	start of the memory area to be freed
238  * @pages	number of pages to be freed
239  * @return	status code
240  */
241 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
242 					      efi_uintn_t pages)
243 {
244 	efi_status_t r;
245 
246 	EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
247 	r = efi_free_pages(memory, pages);
248 	return EFI_EXIT(r);
249 }
250 
251 /*
252  * Get map describing memory usage.
253  *
254  * This function implements the GetMemoryMap service.
255  * See the Unified Extensible Firmware Interface (UEFI) specification
256  * for details.
257  *
258  * @memory_map_size	on entry the size, in bytes, of the memory map buffer,
259  *			on exit the size of the copied memory map
260  * @memory_map		buffer to which the memory map is written
261  * @map_key		key for the memory map
262  * @descriptor_size	size of an individual memory descriptor
263  * @descriptor_version	version number of the memory descriptor structure
264  * @return		status code
265  */
266 static efi_status_t EFIAPI efi_get_memory_map_ext(
267 					efi_uintn_t *memory_map_size,
268 					struct efi_mem_desc *memory_map,
269 					efi_uintn_t *map_key,
270 					efi_uintn_t *descriptor_size,
271 					uint32_t *descriptor_version)
272 {
273 	efi_status_t r;
274 
275 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
276 		  map_key, descriptor_size, descriptor_version);
277 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
278 			       descriptor_size, descriptor_version);
279 	return EFI_EXIT(r);
280 }
281 
282 /*
283  * Allocate memory from pool.
284  *
285  * This function implements the AllocatePool service.
286  * See the Unified Extensible Firmware Interface (UEFI) specification
287  * for details.
288  *
289  * @pool_type	type of the pool from which memory is to be allocated
290  * @size	number of bytes to be allocated
291  * @buffer	allocated memory
292  * @return	status code
293  */
294 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
295 						 efi_uintn_t size,
296 						 void **buffer)
297 {
298 	efi_status_t r;
299 
300 	EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
301 	r = efi_allocate_pool(pool_type, size, buffer);
302 	return EFI_EXIT(r);
303 }
304 
305 /*
306  * Free memory from pool.
307  *
308  * This function implements the FreePool service.
309  * See the Unified Extensible Firmware Interface (UEFI) specification
310  * for details.
311  *
312  * @buffer	start of memory to be freed
313  * @return	status code
314  */
315 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
316 {
317 	efi_status_t r;
318 
319 	EFI_ENTRY("%p", buffer);
320 	r = efi_free_pool(buffer);
321 	return EFI_EXIT(r);
322 }
323 
324 /*
325  * Add a new object to the object list.
326  *
327  * The protocols list is initialized.
328  * The object handle is set.
329  *
330  * @obj	object to be added
331  */
332 void efi_add_handle(struct efi_object *obj)
333 {
334 	if (!obj)
335 		return;
336 	INIT_LIST_HEAD(&obj->protocols);
337 	obj->handle = obj;
338 	list_add_tail(&obj->link, &efi_obj_list);
339 }
340 
341 /*
342  * Create handle.
343  *
344  * @handle	new handle
345  * @return	status code
346  */
347 efi_status_t efi_create_handle(void **handle)
348 {
349 	struct efi_object *obj;
350 	efi_status_t r;
351 
352 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
353 			      sizeof(struct efi_object),
354 			      (void **)&obj);
355 	if (r != EFI_SUCCESS)
356 		return r;
357 	efi_add_handle(obj);
358 	*handle = obj->handle;
359 	return r;
360 }
361 
362 /*
363  * Our event capabilities are very limited. Only a small limited
364  * number of events is allowed to coexist.
365  */
366 static struct efi_event efi_events[16];
367 
368 /*
369  * Create an event.
370  *
371  * This function is used inside U-Boot code to create an event.
372  *
373  * For the API function implementing the CreateEvent service see
374  * efi_create_event_ext.
375  *
376  * @type		type of the event to create
377  * @notify_tpl		task priority level of the event
378  * @notify_function	notification function of the event
379  * @notify_context	pointer passed to the notification function
380  * @event		created event
381  * @return		status code
382  */
383 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
384 			      void (EFIAPI *notify_function) (
385 					struct efi_event *event,
386 					void *context),
387 			      void *notify_context, struct efi_event **event)
388 {
389 	int i;
390 
391 	if (event == NULL)
392 		return EFI_INVALID_PARAMETER;
393 
394 	if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
395 		return EFI_INVALID_PARAMETER;
396 
397 	if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
398 	    notify_function == NULL)
399 		return EFI_INVALID_PARAMETER;
400 
401 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
402 		if (efi_events[i].type)
403 			continue;
404 		efi_events[i].type = type;
405 		efi_events[i].notify_tpl = notify_tpl;
406 		efi_events[i].notify_function = notify_function;
407 		efi_events[i].notify_context = notify_context;
408 		/* Disable timers on bootup */
409 		efi_events[i].trigger_next = -1ULL;
410 		efi_events[i].is_queued = false;
411 		efi_events[i].is_signaled = false;
412 		*event = &efi_events[i];
413 		return EFI_SUCCESS;
414 	}
415 	return EFI_OUT_OF_RESOURCES;
416 }
417 
418 /*
419  * Create an event.
420  *
421  * This function implements the CreateEvent service.
422  * See the Unified Extensible Firmware Interface (UEFI) specification
423  * for details.
424  *
425  * @type		type of the event to create
426  * @notify_tpl		task priority level of the event
427  * @notify_function	notification function of the event
428  * @notify_context	pointer passed to the notification function
429  * @event		created event
430  * @return		status code
431  */
432 static efi_status_t EFIAPI efi_create_event_ext(
433 			uint32_t type, efi_uintn_t notify_tpl,
434 			void (EFIAPI *notify_function) (
435 					struct efi_event *event,
436 					void *context),
437 			void *notify_context, struct efi_event **event)
438 {
439 	EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
440 		  notify_context);
441 	return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
442 					 notify_context, event));
443 }
444 
445 
446 /*
447  * Check if a timer event has occurred or a queued notification function should
448  * be called.
449  *
450  * Our timers have to work without interrupts, so we check whenever keyboard
451  * input or disk accesses happen if enough time elapsed for them to fire.
452  */
453 void efi_timer_check(void)
454 {
455 	int i;
456 	u64 now = timer_get_us();
457 
458 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
459 		if (!efi_events[i].type)
460 			continue;
461 		if (efi_events[i].is_queued)
462 			efi_signal_event(&efi_events[i]);
463 		if (!(efi_events[i].type & EVT_TIMER) ||
464 		    now < efi_events[i].trigger_next)
465 			continue;
466 		switch (efi_events[i].trigger_type) {
467 		case EFI_TIMER_RELATIVE:
468 			efi_events[i].trigger_type = EFI_TIMER_STOP;
469 			break;
470 		case EFI_TIMER_PERIODIC:
471 			efi_events[i].trigger_next +=
472 				efi_events[i].trigger_time;
473 			break;
474 		default:
475 			continue;
476 		}
477 		efi_events[i].is_signaled = true;
478 		efi_signal_event(&efi_events[i]);
479 	}
480 	WATCHDOG_RESET();
481 }
482 
483 /*
484  * Set the trigger time for a timer event or stop the event.
485  *
486  * This is the function for internal usage in U-Boot. For the API function
487  * implementing the SetTimer service see efi_set_timer_ext.
488  *
489  * @event		event for which the timer is set
490  * @type		type of the timer
491  * @trigger_time	trigger period in multiples of 100ns
492  * @return		status code
493  */
494 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
495 			   uint64_t trigger_time)
496 {
497 	int i;
498 
499 	/*
500 	 * The parameter defines a multiple of 100ns.
501 	 * We use multiples of 1000ns. So divide by 10.
502 	 */
503 	do_div(trigger_time, 10);
504 
505 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
506 		if (event != &efi_events[i])
507 			continue;
508 
509 		if (!(event->type & EVT_TIMER))
510 			break;
511 		switch (type) {
512 		case EFI_TIMER_STOP:
513 			event->trigger_next = -1ULL;
514 			break;
515 		case EFI_TIMER_PERIODIC:
516 		case EFI_TIMER_RELATIVE:
517 			event->trigger_next =
518 				timer_get_us() + trigger_time;
519 			break;
520 		default:
521 			return EFI_INVALID_PARAMETER;
522 		}
523 		event->trigger_type = type;
524 		event->trigger_time = trigger_time;
525 		event->is_signaled = false;
526 		return EFI_SUCCESS;
527 	}
528 	return EFI_INVALID_PARAMETER;
529 }
530 
531 /*
532  * Set the trigger time for a timer event or stop the event.
533  *
534  * This function implements the SetTimer service.
535  * See the Unified Extensible Firmware Interface (UEFI) specification
536  * for details.
537  *
538  * @event		event for which the timer is set
539  * @type		type of the timer
540  * @trigger_time	trigger period in multiples of 100ns
541  * @return		status code
542  */
543 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
544 					     enum efi_timer_delay type,
545 					     uint64_t trigger_time)
546 {
547 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
548 	return EFI_EXIT(efi_set_timer(event, type, trigger_time));
549 }
550 
551 /*
552  * Wait for events to be signaled.
553  *
554  * This function implements the WaitForEvent service.
555  * See the Unified Extensible Firmware Interface (UEFI) specification
556  * for details.
557  *
558  * @num_events	number of events to be waited for
559  * @events	events to be waited for
560  * @index	index of the event that was signaled
561  * @return	status code
562  */
563 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
564 					      struct efi_event **event,
565 					      efi_uintn_t *index)
566 {
567 	int i, j;
568 
569 	EFI_ENTRY("%zd, %p, %p", num_events, event, index);
570 
571 	/* Check parameters */
572 	if (!num_events || !event)
573 		return EFI_EXIT(EFI_INVALID_PARAMETER);
574 	/* Check TPL */
575 	if (efi_tpl != TPL_APPLICATION)
576 		return EFI_EXIT(EFI_UNSUPPORTED);
577 	for (i = 0; i < num_events; ++i) {
578 		for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
579 			if (event[i] == &efi_events[j])
580 				goto known_event;
581 		}
582 		return EFI_EXIT(EFI_INVALID_PARAMETER);
583 known_event:
584 		if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
585 			return EFI_EXIT(EFI_INVALID_PARAMETER);
586 		if (!event[i]->is_signaled)
587 			efi_signal_event(event[i]);
588 	}
589 
590 	/* Wait for signal */
591 	for (;;) {
592 		for (i = 0; i < num_events; ++i) {
593 			if (event[i]->is_signaled)
594 				goto out;
595 		}
596 		/* Allow events to occur. */
597 		efi_timer_check();
598 	}
599 
600 out:
601 	/*
602 	 * Reset the signal which is passed to the caller to allow periodic
603 	 * events to occur.
604 	 */
605 	event[i]->is_signaled = false;
606 	if (index)
607 		*index = i;
608 
609 	return EFI_EXIT(EFI_SUCCESS);
610 }
611 
612 /*
613  * Signal an EFI event.
614  *
615  * This function implements the SignalEvent service.
616  * See the Unified Extensible Firmware Interface (UEFI) specification
617  * for details.
618  *
619  * This functions sets the signaled state of the event and queues the
620  * notification function for execution.
621  *
622  * @event	event to signal
623  * @return	status code
624  */
625 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
626 {
627 	int i;
628 
629 	EFI_ENTRY("%p", event);
630 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
631 		if (event != &efi_events[i])
632 			continue;
633 		if (event->is_signaled)
634 			break;
635 		event->is_signaled = true;
636 		if (event->type & EVT_NOTIFY_SIGNAL)
637 			efi_signal_event(event);
638 		break;
639 	}
640 	return EFI_EXIT(EFI_SUCCESS);
641 }
642 
643 /*
644  * Close an EFI event.
645  *
646  * This function implements the CloseEvent service.
647  * See the Unified Extensible Firmware Interface (UEFI) specification
648  * for details.
649  *
650  * @event	event to close
651  * @return	status code
652  */
653 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
654 {
655 	int i;
656 
657 	EFI_ENTRY("%p", event);
658 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
659 		if (event == &efi_events[i]) {
660 			event->type = 0;
661 			event->trigger_next = -1ULL;
662 			event->is_queued = false;
663 			event->is_signaled = false;
664 			return EFI_EXIT(EFI_SUCCESS);
665 		}
666 	}
667 	return EFI_EXIT(EFI_INVALID_PARAMETER);
668 }
669 
670 /*
671  * Check if an event is signaled.
672  *
673  * This function implements the CheckEvent service.
674  * See the Unified Extensible Firmware Interface (UEFI) specification
675  * for details.
676  *
677  * If an event is not signaled yet the notification function is queued.
678  *
679  * @event	event to check
680  * @return	status code
681  */
682 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
683 {
684 	int i;
685 
686 	EFI_ENTRY("%p", event);
687 	efi_timer_check();
688 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
689 		if (event != &efi_events[i])
690 			continue;
691 		if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
692 			break;
693 		if (!event->is_signaled)
694 			efi_signal_event(event);
695 		if (event->is_signaled)
696 			return EFI_EXIT(EFI_SUCCESS);
697 		return EFI_EXIT(EFI_NOT_READY);
698 	}
699 	return EFI_EXIT(EFI_INVALID_PARAMETER);
700 }
701 
702 /*
703  * Find the internal EFI object for a handle.
704  *
705  * @handle	handle to find
706  * @return	EFI object
707  */
708 struct efi_object *efi_search_obj(const void *handle)
709 {
710 	struct efi_object *efiobj;
711 
712 	list_for_each_entry(efiobj, &efi_obj_list, link) {
713 		if (efiobj->handle == handle)
714 			return efiobj;
715 	}
716 
717 	return NULL;
718 }
719 
720 /*
721  * Find a protocol on a handle.
722  *
723  * @handle		handle
724  * @protocol_guid	GUID of the protocol
725  * @handler		reference to the protocol
726  * @return		status code
727  */
728 efi_status_t efi_search_protocol(const void *handle,
729 				 const efi_guid_t *protocol_guid,
730 				 struct efi_handler **handler)
731 {
732 	struct efi_object *efiobj;
733 	struct list_head *lhandle;
734 
735 	if (!handle || !protocol_guid)
736 		return EFI_INVALID_PARAMETER;
737 	efiobj = efi_search_obj(handle);
738 	if (!efiobj)
739 		return EFI_INVALID_PARAMETER;
740 	list_for_each(lhandle, &efiobj->protocols) {
741 		struct efi_handler *protocol;
742 
743 		protocol = list_entry(lhandle, struct efi_handler, link);
744 		if (!guidcmp(protocol->guid, protocol_guid)) {
745 			if (handler)
746 				*handler = protocol;
747 			return EFI_SUCCESS;
748 		}
749 	}
750 	return EFI_NOT_FOUND;
751 }
752 
753 /*
754  * Install new protocol on a handle.
755  *
756  * @handle			handle on which the protocol shall be installed
757  * @protocol			GUID of the protocol to be installed
758  * @protocol_interface		interface of the protocol implementation
759  * @return			status code
760  */
761 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
762 			      void *protocol_interface)
763 {
764 	struct efi_object *efiobj;
765 	struct efi_handler *handler;
766 	efi_status_t ret;
767 
768 	efiobj = efi_search_obj(handle);
769 	if (!efiobj)
770 		return EFI_INVALID_PARAMETER;
771 	ret = efi_search_protocol(handle, protocol, NULL);
772 	if (ret != EFI_NOT_FOUND)
773 		return EFI_INVALID_PARAMETER;
774 	handler = calloc(1, sizeof(struct efi_handler));
775 	if (!handler)
776 		return EFI_OUT_OF_RESOURCES;
777 	handler->guid = protocol;
778 	handler->protocol_interface = protocol_interface;
779 	list_add_tail(&handler->link, &efiobj->protocols);
780 	return EFI_SUCCESS;
781 }
782 
783 /*
784  * Delete protocol from a handle.
785  *
786  * @handle			handle from which the protocol shall be deleted
787  * @protocol			GUID of the protocol to be deleted
788  * @protocol_interface		interface of the protocol implementation
789  * @return			status code
790  */
791 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
792 				 void *protocol_interface)
793 {
794 	struct efi_handler *handler;
795 	efi_status_t ret;
796 
797 	ret = efi_search_protocol(handle, protocol, &handler);
798 	if (ret != EFI_SUCCESS)
799 		return ret;
800 	if (guidcmp(handler->guid, protocol))
801 		return EFI_INVALID_PARAMETER;
802 	list_del(&handler->link);
803 	free(handler);
804 	return EFI_SUCCESS;
805 }
806 
807 /*
808  * Delete all protocols from a handle.
809  *
810  * @handle			handle from which the protocols shall be deleted
811  * @return			status code
812  */
813 efi_status_t efi_remove_all_protocols(const void *handle)
814 {
815 	struct efi_object *efiobj;
816 	struct list_head *lhandle;
817 	struct list_head *pos;
818 
819 	efiobj = efi_search_obj(handle);
820 	if (!efiobj)
821 		return EFI_INVALID_PARAMETER;
822 	list_for_each_safe(lhandle, pos, &efiobj->protocols) {
823 		struct efi_handler *protocol;
824 		efi_status_t ret;
825 
826 		protocol = list_entry(lhandle, struct efi_handler, link);
827 
828 		ret = efi_remove_protocol(handle, protocol->guid,
829 					  protocol->protocol_interface);
830 		if (ret != EFI_SUCCESS)
831 			return ret;
832 	}
833 	return EFI_SUCCESS;
834 }
835 
836 /*
837  * Install protocol interface.
838  *
839  * This function implements the InstallProtocolInterface service.
840  * See the Unified Extensible Firmware Interface (UEFI) specification
841  * for details.
842  *
843  * @handle			handle on which the protocol shall be installed
844  * @protocol			GUID of the protocol to be installed
845  * @protocol_interface_type	type of the interface to be installed,
846  *				always EFI_NATIVE_INTERFACE
847  * @protocol_interface		interface of the protocol implementation
848  * @return			status code
849  */
850 static efi_status_t EFIAPI efi_install_protocol_interface(
851 			void **handle, const efi_guid_t *protocol,
852 			int protocol_interface_type, void *protocol_interface)
853 {
854 	efi_status_t r;
855 
856 	EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
857 		  protocol_interface);
858 
859 	if (!handle || !protocol ||
860 	    protocol_interface_type != EFI_NATIVE_INTERFACE) {
861 		r = EFI_INVALID_PARAMETER;
862 		goto out;
863 	}
864 
865 	/* Create new handle if requested. */
866 	if (!*handle) {
867 		r = efi_create_handle(handle);
868 		if (r != EFI_SUCCESS)
869 			goto out;
870 		debug("%sEFI: new handle %p\n", indent_string(nesting_level),
871 		      *handle);
872 	} else {
873 		debug("%sEFI: handle %p\n", indent_string(nesting_level),
874 		      *handle);
875 	}
876 	/* Add new protocol */
877 	r = efi_add_protocol(*handle, protocol, protocol_interface);
878 out:
879 	return EFI_EXIT(r);
880 }
881 
882 /*
883  * Reinstall protocol interface.
884  *
885  * This function implements the ReinstallProtocolInterface service.
886  * See the Unified Extensible Firmware Interface (UEFI) specification
887  * for details.
888  *
889  * @handle			handle on which the protocol shall be
890  *				reinstalled
891  * @protocol			GUID of the protocol to be installed
892  * @old_interface		interface to be removed
893  * @new_interface		interface to be installed
894  * @return			status code
895  */
896 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
897 			const efi_guid_t *protocol, void *old_interface,
898 			void *new_interface)
899 {
900 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
901 		  new_interface);
902 	return EFI_EXIT(EFI_ACCESS_DENIED);
903 }
904 
905 /*
906  * Uninstall protocol interface.
907  *
908  * This function implements the UninstallProtocolInterface service.
909  * See the Unified Extensible Firmware Interface (UEFI) specification
910  * for details.
911  *
912  * @handle			handle from which the protocol shall be removed
913  * @protocol			GUID of the protocol to be removed
914  * @protocol_interface		interface to be removed
915  * @return			status code
916  */
917 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
918 				void *handle, const efi_guid_t *protocol,
919 				void *protocol_interface)
920 {
921 	struct efi_handler *handler;
922 	efi_status_t r;
923 
924 	EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
925 
926 	if (!handle || !protocol) {
927 		r = EFI_INVALID_PARAMETER;
928 		goto out;
929 	}
930 
931 	/* Find the protocol on the handle */
932 	r = efi_search_protocol(handle, protocol, &handler);
933 	if (r != EFI_SUCCESS)
934 		goto out;
935 	if (handler->protocol_interface) {
936 		/* TODO disconnect controllers */
937 		r =  EFI_ACCESS_DENIED;
938 	} else {
939 		r = efi_remove_protocol(handle, protocol, protocol_interface);
940 	}
941 out:
942 	return EFI_EXIT(r);
943 }
944 
945 /*
946  * Register an event for notification when a protocol is installed.
947  *
948  * This function implements the RegisterProtocolNotify service.
949  * See the Unified Extensible Firmware Interface (UEFI) specification
950  * for details.
951  *
952  * @protocol		GUID of the protocol whose installation shall be
953  *			notified
954  * @event		event to be signaled upon installation of the protocol
955  * @registration	key for retrieving the registration information
956  * @return		status code
957  */
958 static efi_status_t EFIAPI efi_register_protocol_notify(
959 						const efi_guid_t *protocol,
960 						struct efi_event *event,
961 						void **registration)
962 {
963 	EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
964 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
965 }
966 
967 /*
968  * Determine if an EFI handle implements a protocol.
969  *
970  * See the documentation of the LocateHandle service in the UEFI specification.
971  *
972  * @search_type		selection criterion
973  * @protocol		GUID of the protocol
974  * @search_key		registration key
975  * @efiobj		handle
976  * @return		0 if the handle implements the protocol
977  */
978 static int efi_search(enum efi_locate_search_type search_type,
979 		      const efi_guid_t *protocol, void *search_key,
980 		      struct efi_object *efiobj)
981 {
982 	efi_status_t ret;
983 
984 	switch (search_type) {
985 	case ALL_HANDLES:
986 		return 0;
987 	case BY_REGISTER_NOTIFY:
988 		/* TODO: RegisterProtocolNotify is not implemented yet */
989 		return -1;
990 	case BY_PROTOCOL:
991 		ret = efi_search_protocol(efiobj->handle, protocol, NULL);
992 		return (ret != EFI_SUCCESS);
993 	default:
994 		/* Invalid search type */
995 		return -1;
996 	}
997 }
998 
999 /*
1000  * Locate handles implementing a protocol.
1001  *
1002  * This function is meant for U-Boot internal calls. For the API implementation
1003  * of the LocateHandle service see efi_locate_handle_ext.
1004  *
1005  * @search_type		selection criterion
1006  * @protocol		GUID of the protocol
1007  * @search_key		registration key
1008  * @buffer_size		size of the buffer to receive the handles in bytes
1009  * @buffer		buffer to receive the relevant handles
1010  * @return		status code
1011  */
1012 static efi_status_t efi_locate_handle(
1013 			enum efi_locate_search_type search_type,
1014 			const efi_guid_t *protocol, void *search_key,
1015 			efi_uintn_t *buffer_size, efi_handle_t *buffer)
1016 {
1017 	struct efi_object *efiobj;
1018 	efi_uintn_t size = 0;
1019 
1020 	/* Check parameters */
1021 	switch (search_type) {
1022 	case ALL_HANDLES:
1023 		break;
1024 	case BY_REGISTER_NOTIFY:
1025 		if (!search_key)
1026 			return EFI_INVALID_PARAMETER;
1027 		/* RegisterProtocolNotify is not implemented yet */
1028 		return EFI_UNSUPPORTED;
1029 	case BY_PROTOCOL:
1030 		if (!protocol)
1031 			return EFI_INVALID_PARAMETER;
1032 		break;
1033 	default:
1034 		return EFI_INVALID_PARAMETER;
1035 	}
1036 
1037 	/*
1038 	 * efi_locate_handle_buffer uses this function for
1039 	 * the calculation of the necessary buffer size.
1040 	 * So do not require a buffer for buffersize == 0.
1041 	 */
1042 	if (!buffer_size || (*buffer_size && !buffer))
1043 		return EFI_INVALID_PARAMETER;
1044 
1045 	/* Count how much space we need */
1046 	list_for_each_entry(efiobj, &efi_obj_list, link) {
1047 		if (!efi_search(search_type, protocol, search_key, efiobj))
1048 			size += sizeof(void*);
1049 	}
1050 
1051 	if (*buffer_size < size) {
1052 		*buffer_size = size;
1053 		return EFI_BUFFER_TOO_SMALL;
1054 	}
1055 
1056 	*buffer_size = size;
1057 	if (size == 0)
1058 		return EFI_NOT_FOUND;
1059 
1060 	/* Then fill the array */
1061 	list_for_each_entry(efiobj, &efi_obj_list, link) {
1062 		if (!efi_search(search_type, protocol, search_key, efiobj))
1063 			*buffer++ = efiobj->handle;
1064 	}
1065 
1066 	return EFI_SUCCESS;
1067 }
1068 
1069 /*
1070  * Locate handles implementing a protocol.
1071  *
1072  * This function implements the LocateHandle service.
1073  * See the Unified Extensible Firmware Interface (UEFI) specification
1074  * for details.
1075  *
1076  * @search_type		selection criterion
1077  * @protocol		GUID of the protocol
1078  * @search_key		registration key
1079  * @buffer_size		size of the buffer to receive the handles in bytes
1080  * @buffer		buffer to receive the relevant handles
1081  * @return		0 if the handle implements the protocol
1082  */
1083 static efi_status_t EFIAPI efi_locate_handle_ext(
1084 			enum efi_locate_search_type search_type,
1085 			const efi_guid_t *protocol, void *search_key,
1086 			efi_uintn_t *buffer_size, efi_handle_t *buffer)
1087 {
1088 	EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1089 		  buffer_size, buffer);
1090 
1091 	return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1092 			buffer_size, buffer));
1093 }
1094 
1095 /* Collapses configuration table entries, removing index i */
1096 static void efi_remove_configuration_table(int i)
1097 {
1098 	struct efi_configuration_table *this = &efi_conf_table[i];
1099 	struct efi_configuration_table *next = &efi_conf_table[i+1];
1100 	struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1101 
1102 	memmove(this, next, (ulong)end - (ulong)next);
1103 	systab.nr_tables--;
1104 }
1105 
1106 /*
1107  * Adds, updates, or removes a configuration table.
1108  *
1109  * This function is used for internal calls. For the API implementation of the
1110  * InstallConfigurationTable service see efi_install_configuration_table_ext.
1111  *
1112  * @guid		GUID of the installed table
1113  * @table		table to be installed
1114  * @return		status code
1115  */
1116 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1117 {
1118 	int i;
1119 
1120 	/* Check for guid override */
1121 	for (i = 0; i < systab.nr_tables; i++) {
1122 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1123 			if (table)
1124 				efi_conf_table[i].table = table;
1125 			else
1126 				efi_remove_configuration_table(i);
1127 			return EFI_SUCCESS;
1128 		}
1129 	}
1130 
1131 	if (!table)
1132 		return EFI_NOT_FOUND;
1133 
1134 	/* No override, check for overflow */
1135 	if (i >= ARRAY_SIZE(efi_conf_table))
1136 		return EFI_OUT_OF_RESOURCES;
1137 
1138 	/* Add a new entry */
1139 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1140 	efi_conf_table[i].table = table;
1141 	systab.nr_tables = i + 1;
1142 
1143 	return EFI_SUCCESS;
1144 }
1145 
1146 /*
1147  * Adds, updates, or removes a configuration table.
1148  *
1149  * This function implements the InstallConfigurationTable service.
1150  * See the Unified Extensible Firmware Interface (UEFI) specification
1151  * for details.
1152  *
1153  * @guid		GUID of the installed table
1154  * @table		table to be installed
1155  * @return		status code
1156  */
1157 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1158 							       void *table)
1159 {
1160 	EFI_ENTRY("%pUl, %p", guid, table);
1161 	return EFI_EXIT(efi_install_configuration_table(guid, table));
1162 }
1163 
1164 /*
1165  * Initialize a loaded_image_info + loaded_image_info object with correct
1166  * protocols, boot-device, etc.
1167  *
1168  * @info		loaded image info to be passed to the entry point of the
1169  *			image
1170  * @obj			internal object associated with the loaded image
1171  * @device_path		device path of the loaded image
1172  * @file_path		file path of the loaded image
1173  */
1174 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1175 			    struct efi_device_path *device_path,
1176 			    struct efi_device_path *file_path)
1177 {
1178 	efi_status_t ret;
1179 
1180 	/* Add internal object to object list */
1181 	efi_add_handle(obj);
1182 	/* efi_exit() assumes that the handle points to the info */
1183 	obj->handle = info;
1184 
1185 	info->file_path = file_path;
1186 	if (device_path)
1187 		info->device_handle = efi_dp_find_obj(device_path, NULL);
1188 
1189 	/*
1190 	 * When asking for the device path interface, return
1191 	 * bootefi_device_path
1192 	 */
1193 	ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1194 	if (ret != EFI_SUCCESS)
1195 		goto failure;
1196 
1197 	/*
1198 	 * When asking for the loaded_image interface, just
1199 	 * return handle which points to loaded_image_info
1200 	 */
1201 	ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1202 	if (ret != EFI_SUCCESS)
1203 		goto failure;
1204 
1205 	ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1206 			       (void *)&efi_console_control);
1207 	if (ret != EFI_SUCCESS)
1208 		goto failure;
1209 
1210 	ret = efi_add_protocol(obj->handle,
1211 			       &efi_guid_device_path_to_text_protocol,
1212 			       (void *)&efi_device_path_to_text);
1213 	if (ret != EFI_SUCCESS)
1214 		goto failure;
1215 
1216 	return;
1217 failure:
1218 	printf("ERROR: Failure to install protocols for loaded image\n");
1219 }
1220 
1221 /*
1222  * Load an image using a file path.
1223  *
1224  * @file_path		the path of the image to load
1225  * @buffer		buffer containing the loaded image
1226  * @return		status code
1227  */
1228 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1229 				      void **buffer)
1230 {
1231 	struct efi_file_info *info = NULL;
1232 	struct efi_file_handle *f;
1233 	static efi_status_t ret;
1234 	uint64_t bs;
1235 
1236 	f = efi_file_from_path(file_path);
1237 	if (!f)
1238 		return EFI_DEVICE_ERROR;
1239 
1240 	bs = 0;
1241 	EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1242 				  &bs, info));
1243 	if (ret == EFI_BUFFER_TOO_SMALL) {
1244 		info = malloc(bs);
1245 		EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1246 					  &bs, info));
1247 	}
1248 	if (ret != EFI_SUCCESS)
1249 		goto error;
1250 
1251 	ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1252 	if (ret)
1253 		goto error;
1254 
1255 	EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1256 
1257 error:
1258 	free(info);
1259 	EFI_CALL(f->close(f));
1260 
1261 	if (ret != EFI_SUCCESS) {
1262 		efi_free_pool(*buffer);
1263 		*buffer = NULL;
1264 	}
1265 
1266 	return ret;
1267 }
1268 
1269 /*
1270  * Load an EFI image into memory.
1271  *
1272  * This function implements the LoadImage service.
1273  * See the Unified Extensible Firmware Interface (UEFI) specification
1274  * for details.
1275  *
1276  * @boot_policy		true for request originating from the boot manager
1277  * @parent_image	the calles's image handle
1278  * @file_path		the path of the image to load
1279  * @source_buffer	memory location from which the image is installed
1280  * @source_size		size of the memory area from which the image is
1281  *			installed
1282  * @image_handle	handle for the newly installed image
1283  * @return		status code
1284  */
1285 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1286 					  efi_handle_t parent_image,
1287 					  struct efi_device_path *file_path,
1288 					  void *source_buffer,
1289 					  unsigned long source_size,
1290 					  efi_handle_t *image_handle)
1291 {
1292 	struct efi_loaded_image *info;
1293 	struct efi_object *obj;
1294 
1295 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1296 		  file_path, source_buffer, source_size, image_handle);
1297 
1298 	info = calloc(1, sizeof(*info));
1299 	obj = calloc(1, sizeof(*obj));
1300 
1301 	if (!source_buffer) {
1302 		struct efi_device_path *dp, *fp;
1303 		efi_status_t ret;
1304 
1305 		ret = efi_load_image_from_path(file_path, &source_buffer);
1306 		if (ret != EFI_SUCCESS) {
1307 			free(info);
1308 			free(obj);
1309 			return EFI_EXIT(ret);
1310 		}
1311 
1312 		/*
1313 		 * split file_path which contains both the device and
1314 		 * file parts:
1315 		 */
1316 		efi_dp_split_file_path(file_path, &dp, &fp);
1317 
1318 		efi_setup_loaded_image(info, obj, dp, fp);
1319 	} else {
1320 		/* In this case, file_path is the "device" path, ie.
1321 		 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1322 		 */
1323 		efi_setup_loaded_image(info, obj, file_path, NULL);
1324 	}
1325 
1326 	info->reserved = efi_load_pe(source_buffer, info);
1327 	if (!info->reserved) {
1328 		free(info);
1329 		free(obj);
1330 		return EFI_EXIT(EFI_UNSUPPORTED);
1331 	}
1332 
1333 	info->system_table = &systab;
1334 	info->parent_handle = parent_image;
1335 	*image_handle = obj->handle;
1336 
1337 	return EFI_EXIT(EFI_SUCCESS);
1338 }
1339 
1340 /*
1341  * Call the entry point of an image.
1342  *
1343  * This function implements the StartImage service.
1344  * See the Unified Extensible Firmware Interface (UEFI) specification
1345  * for details.
1346  *
1347  * @image_handle	handle of the image
1348  * @exit_data_size	size of the buffer
1349  * @exit_data		buffer to receive the exit data of the called image
1350  * @return		status code
1351  */
1352 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1353 					   unsigned long *exit_data_size,
1354 					   s16 **exit_data)
1355 {
1356 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
1357 	struct efi_loaded_image *info = image_handle;
1358 
1359 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1360 	entry = info->reserved;
1361 
1362 	efi_is_direct_boot = false;
1363 
1364 	/* call the image! */
1365 	if (setjmp(&info->exit_jmp)) {
1366 		/* We returned from the child image */
1367 		return EFI_EXIT(info->exit_status);
1368 	}
1369 
1370 	__efi_nesting_dec();
1371 	__efi_exit_check();
1372 	entry(image_handle, &systab);
1373 	__efi_entry_check();
1374 	__efi_nesting_inc();
1375 
1376 	/* Should usually never get here */
1377 	return EFI_EXIT(EFI_SUCCESS);
1378 }
1379 
1380 /*
1381  * Leave an EFI application or driver.
1382  *
1383  * This function implements the Exit service.
1384  * See the Unified Extensible Firmware Interface (UEFI) specification
1385  * for details.
1386  *
1387  * @image_handle	handle of the application or driver that is exiting
1388  * @exit_status		status code
1389  * @exit_data_size	size of the buffer in bytes
1390  * @exit_data		buffer with data describing an error
1391  * @return		status code
1392  */
1393 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1394 			efi_status_t exit_status, unsigned long exit_data_size,
1395 			int16_t *exit_data)
1396 {
1397 	/*
1398 	 * We require that the handle points to the original loaded
1399 	 * image protocol interface.
1400 	 *
1401 	 * For getting the longjmp address this is safer than locating
1402 	 * the protocol because the protocol may have been reinstalled
1403 	 * pointing to another memory location.
1404 	 *
1405 	 * TODO: We should call the unload procedure of the loaded
1406 	 *	 image protocol.
1407 	 */
1408 	struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1409 
1410 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1411 		  exit_data_size, exit_data);
1412 
1413 	/* Make sure entry/exit counts for EFI world cross-overs match */
1414 	__efi_exit_check();
1415 
1416 	/*
1417 	 * But longjmp out with the U-Boot gd, not the application's, as
1418 	 * the other end is a setjmp call inside EFI context.
1419 	 */
1420 	efi_restore_gd();
1421 
1422 	loaded_image_info->exit_status = exit_status;
1423 	longjmp(&loaded_image_info->exit_jmp, 1);
1424 
1425 	panic("EFI application exited");
1426 }
1427 
1428 /*
1429  * Unload an EFI image.
1430  *
1431  * This function implements the UnloadImage service.
1432  * See the Unified Extensible Firmware Interface (UEFI) specification
1433  * for details.
1434  *
1435  * @image_handle	handle of the image to be unloaded
1436  * @return		status code
1437  */
1438 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1439 {
1440 	struct efi_object *efiobj;
1441 
1442 	EFI_ENTRY("%p", image_handle);
1443 	efiobj = efi_search_obj(image_handle);
1444 	if (efiobj)
1445 		list_del(&efiobj->link);
1446 
1447 	return EFI_EXIT(EFI_SUCCESS);
1448 }
1449 
1450 /*
1451  * Fix up caches for EFI payloads if necessary.
1452  */
1453 static void efi_exit_caches(void)
1454 {
1455 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1456 	/*
1457 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
1458 	 * a zImage, but does not know of all cache layers. Give it a hand.
1459 	 */
1460 	if (efi_is_direct_boot)
1461 		cleanup_before_linux();
1462 #endif
1463 }
1464 
1465 /*
1466  * Stop boot services.
1467  *
1468  * This function implements the ExitBootServices service.
1469  * See the Unified Extensible Firmware Interface (UEFI) specification
1470  * for details.
1471  *
1472  * @image_handle	handle of the loaded image
1473  * @map_key		key of the memory map
1474  * @return		status code
1475  */
1476 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1477 						  unsigned long map_key)
1478 {
1479 	int i;
1480 
1481 	EFI_ENTRY("%p, %ld", image_handle, map_key);
1482 
1483 	/* Notify that ExitBootServices is invoked. */
1484 	for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1485 		if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1486 			continue;
1487 		efi_signal_event(&efi_events[i]);
1488 	}
1489 	/* Make sure that notification functions are not called anymore */
1490 	efi_tpl = TPL_HIGH_LEVEL;
1491 
1492 	/* XXX Should persist EFI variables here */
1493 
1494 	board_quiesce_devices();
1495 
1496 	/* Fix up caches for EFI payloads if necessary */
1497 	efi_exit_caches();
1498 
1499 	/* This stops all lingering devices */
1500 	bootm_disable_interrupts();
1501 
1502 	/* Give the payload some time to boot */
1503 	efi_set_watchdog(0);
1504 	WATCHDOG_RESET();
1505 
1506 	return EFI_EXIT(EFI_SUCCESS);
1507 }
1508 
1509 /*
1510  * Get next value of the counter.
1511  *
1512  * This function implements the NextMonotonicCount service.
1513  * See the Unified Extensible Firmware Interface (UEFI) specification
1514  * for details.
1515  *
1516  * @count	returned value of the counter
1517  * @return	status code
1518  */
1519 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1520 {
1521 	static uint64_t mono = 0;
1522 	EFI_ENTRY("%p", count);
1523 	*count = mono++;
1524 	return EFI_EXIT(EFI_SUCCESS);
1525 }
1526 
1527 /*
1528  * Sleep.
1529  *
1530  * This function implements the Stall sercive.
1531  * See the Unified Extensible Firmware Interface (UEFI) specification
1532  * for details.
1533  *
1534  * @microseconds	period to sleep in microseconds
1535  * @return		status code
1536  */
1537 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1538 {
1539 	EFI_ENTRY("%ld", microseconds);
1540 	udelay(microseconds);
1541 	return EFI_EXIT(EFI_SUCCESS);
1542 }
1543 
1544 /*
1545  * Reset the watchdog timer.
1546  *
1547  * This function implements the SetWatchdogTimer service.
1548  * See the Unified Extensible Firmware Interface (UEFI) specification
1549  * for details.
1550  *
1551  * @timeout		seconds before reset by watchdog
1552  * @watchdog_code	code to be logged when resetting
1553  * @data_size		size of buffer in bytes
1554  * @watchdog_data	buffer with data describing the reset reason
1555  * @return		status code
1556  */
1557 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1558 						  uint64_t watchdog_code,
1559 						  unsigned long data_size,
1560 						  uint16_t *watchdog_data)
1561 {
1562 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1563 		  data_size, watchdog_data);
1564 	return EFI_EXIT(efi_set_watchdog(timeout));
1565 }
1566 
1567 /*
1568  * Connect a controller to a driver.
1569  *
1570  * This function implements the ConnectController service.
1571  * See the Unified Extensible Firmware Interface (UEFI) specification
1572  * for details.
1573  *
1574  * @controller_handle	handle of the controller
1575  * @driver_image_handle	handle of the driver
1576  * @remain_device_path	device path of a child controller
1577  * @recursive		true to connect all child controllers
1578  * @return		status code
1579  */
1580 static efi_status_t EFIAPI efi_connect_controller(
1581 			efi_handle_t controller_handle,
1582 			efi_handle_t *driver_image_handle,
1583 			struct efi_device_path *remain_device_path,
1584 			bool recursive)
1585 {
1586 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1587 		  remain_device_path, recursive);
1588 	return EFI_EXIT(EFI_NOT_FOUND);
1589 }
1590 
1591 /*
1592  * Disconnect a controller from a driver.
1593  *
1594  * This function implements the DisconnectController service.
1595  * See the Unified Extensible Firmware Interface (UEFI) specification
1596  * for details.
1597  *
1598  * @controller_handle	handle of the controller
1599  * @driver_image_handle handle of the driver
1600  * @child_handle	handle of the child to destroy
1601  * @return		status code
1602  */
1603 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1604 						     void *driver_image_handle,
1605 						     void *child_handle)
1606 {
1607 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1608 		  child_handle);
1609 	return EFI_EXIT(EFI_INVALID_PARAMETER);
1610 }
1611 
1612 /*
1613  * Close a protocol.
1614  *
1615  * This function implements the CloseProtocol service.
1616  * See the Unified Extensible Firmware Interface (UEFI) specification
1617  * for details.
1618  *
1619  * @handle		handle on which the protocol shall be closed
1620  * @protocol		GUID of the protocol to close
1621  * @agent_handle	handle of the driver
1622  * @controller_handle	handle of the controller
1623  * @return		status code
1624  */
1625 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1626 					      const efi_guid_t *protocol,
1627 					      void *agent_handle,
1628 					      void *controller_handle)
1629 {
1630 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1631 		  controller_handle);
1632 	return EFI_EXIT(EFI_NOT_FOUND);
1633 }
1634 
1635 /*
1636  * Provide information about then open status of a protocol on a handle
1637  *
1638  * This function implements the OpenProtocolInformation service.
1639  * See the Unified Extensible Firmware Interface (UEFI) specification
1640  * for details.
1641  *
1642  * @handle		handle for which the information shall be retrieved
1643  * @protocol		GUID of the protocol
1644  * @entry_buffer	buffer to receive the open protocol information
1645  * @entry_count		number of entries available in the buffer
1646  * @return		status code
1647  */
1648 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1649 			const efi_guid_t *protocol,
1650 			struct efi_open_protocol_info_entry **entry_buffer,
1651 			efi_uintn_t *entry_count)
1652 {
1653 	EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1654 		  entry_count);
1655 	return EFI_EXIT(EFI_NOT_FOUND);
1656 }
1657 
1658 /*
1659  * Get protocols installed on a handle.
1660  *
1661  * This function implements the ProtocolsPerHandleService.
1662  * See the Unified Extensible Firmware Interface (UEFI) specification
1663  * for details.
1664  *
1665  * @handle			handle for which the information is retrieved
1666  * @protocol_buffer		buffer with protocol GUIDs
1667  * @protocol_buffer_count	number of entries in the buffer
1668  * @return			status code
1669  */
1670 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1671 			efi_guid_t ***protocol_buffer,
1672 			efi_uintn_t *protocol_buffer_count)
1673 {
1674 	unsigned long buffer_size;
1675 	struct efi_object *efiobj;
1676 	struct list_head *protocol_handle;
1677 	efi_status_t r;
1678 
1679 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1680 		  protocol_buffer_count);
1681 
1682 	if (!handle || !protocol_buffer || !protocol_buffer_count)
1683 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1684 
1685 	*protocol_buffer = NULL;
1686 	*protocol_buffer_count = 0;
1687 
1688 	efiobj = efi_search_obj(handle);
1689 	if (!efiobj)
1690 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1691 
1692 	/* Count protocols */
1693 	list_for_each(protocol_handle, &efiobj->protocols) {
1694 		++*protocol_buffer_count;
1695 	}
1696 
1697 	/* Copy guids */
1698 	if (*protocol_buffer_count) {
1699 		size_t j = 0;
1700 
1701 		buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1702 		r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1703 				      (void **)protocol_buffer);
1704 		if (r != EFI_SUCCESS)
1705 			return EFI_EXIT(r);
1706 		list_for_each(protocol_handle, &efiobj->protocols) {
1707 			struct efi_handler *protocol;
1708 
1709 			protocol = list_entry(protocol_handle,
1710 					      struct efi_handler, link);
1711 			(*protocol_buffer)[j] = (void *)protocol->guid;
1712 			++j;
1713 		}
1714 	}
1715 
1716 	return EFI_EXIT(EFI_SUCCESS);
1717 }
1718 
1719 /*
1720  * Locate handles implementing a protocol.
1721  *
1722  * This function implements the LocateHandleBuffer service.
1723  * See the Unified Extensible Firmware Interface (UEFI) specification
1724  * for details.
1725  *
1726  * @search_type		selection criterion
1727  * @protocol		GUID of the protocol
1728  * @search_key		registration key
1729  * @no_handles		number of returned handles
1730  * @buffer		buffer with the returned handles
1731  * @return		status code
1732  */
1733 static efi_status_t EFIAPI efi_locate_handle_buffer(
1734 			enum efi_locate_search_type search_type,
1735 			const efi_guid_t *protocol, void *search_key,
1736 			efi_uintn_t *no_handles, efi_handle_t **buffer)
1737 {
1738 	efi_status_t r;
1739 	efi_uintn_t buffer_size = 0;
1740 
1741 	EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1742 		  no_handles, buffer);
1743 
1744 	if (!no_handles || !buffer) {
1745 		r = EFI_INVALID_PARAMETER;
1746 		goto out;
1747 	}
1748 	*no_handles = 0;
1749 	*buffer = NULL;
1750 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1751 			      *buffer);
1752 	if (r != EFI_BUFFER_TOO_SMALL)
1753 		goto out;
1754 	r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1755 			      (void **)buffer);
1756 	if (r != EFI_SUCCESS)
1757 		goto out;
1758 	r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1759 			      *buffer);
1760 	if (r == EFI_SUCCESS)
1761 		*no_handles = buffer_size / sizeof(void *);
1762 out:
1763 	return EFI_EXIT(r);
1764 }
1765 
1766 /*
1767  * Find an interface implementing a protocol.
1768  *
1769  * This function implements the LocateProtocol service.
1770  * See the Unified Extensible Firmware Interface (UEFI) specification
1771  * for details.
1772  *
1773  * @protocol		GUID of the protocol
1774  * @registration	registration key passed to the notification function
1775  * @protocol_interface	interface implementing the protocol
1776  * @return		status code
1777  */
1778 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1779 					       void *registration,
1780 					       void **protocol_interface)
1781 {
1782 	struct list_head *lhandle;
1783 	efi_status_t ret;
1784 
1785 	EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1786 
1787 	if (!protocol || !protocol_interface)
1788 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1789 
1790 	list_for_each(lhandle, &efi_obj_list) {
1791 		struct efi_object *efiobj;
1792 		struct efi_handler *handler;
1793 
1794 		efiobj = list_entry(lhandle, struct efi_object, link);
1795 
1796 		ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1797 		if (ret == EFI_SUCCESS) {
1798 			*protocol_interface = handler->protocol_interface;
1799 			return EFI_EXIT(EFI_SUCCESS);
1800 		}
1801 	}
1802 	*protocol_interface = NULL;
1803 
1804 	return EFI_EXIT(EFI_NOT_FOUND);
1805 }
1806 
1807 /*
1808  * Get the device path and handle of an device implementing a protocol.
1809  *
1810  * This function implements the LocateDevicePath service.
1811  * See the Unified Extensible Firmware Interface (UEFI) specification
1812  * for details.
1813  *
1814  * @protocol		GUID of the protocol
1815  * @device_path		device path
1816  * @device		handle of the device
1817  * @return		status code
1818  */
1819 static efi_status_t EFIAPI efi_locate_device_path(
1820 			const efi_guid_t *protocol,
1821 			struct efi_device_path **device_path,
1822 			efi_handle_t *device)
1823 {
1824 	struct efi_device_path *dp;
1825 	size_t i;
1826 	struct efi_handler *handler;
1827 	efi_handle_t *handles;
1828 	size_t len, len_dp;
1829 	size_t len_best = 0;
1830 	efi_uintn_t no_handles;
1831 	u8 *remainder;
1832 	efi_status_t ret;
1833 
1834 	EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1835 
1836 	if (!protocol || !device_path || !*device_path || !device) {
1837 		ret = EFI_INVALID_PARAMETER;
1838 		goto out;
1839 	}
1840 
1841 	/* Find end of device path */
1842 	len = efi_dp_size(*device_path);
1843 
1844 	/* Get all handles implementing the protocol */
1845 	ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1846 						&no_handles, &handles));
1847 	if (ret != EFI_SUCCESS)
1848 		goto out;
1849 
1850 	for (i = 0; i < no_handles; ++i) {
1851 		/* Find the device path protocol */
1852 		ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1853 					  &handler);
1854 		if (ret != EFI_SUCCESS)
1855 			continue;
1856 		dp = (struct efi_device_path *)handler->protocol_interface;
1857 		len_dp = efi_dp_size(dp);
1858 		/*
1859 		 * This handle can only be a better fit
1860 		 * if its device path length is longer than the best fit and
1861 		 * if its device path length is shorter of equal the searched
1862 		 * device path.
1863 		 */
1864 		if (len_dp <= len_best || len_dp > len)
1865 			continue;
1866 		/* Check if dp is a subpath of device_path */
1867 		if (memcmp(*device_path, dp, len_dp))
1868 			continue;
1869 		*device = handles[i];
1870 		len_best = len_dp;
1871 	}
1872 	if (len_best) {
1873 		remainder = (u8 *)*device_path + len_best;
1874 		*device_path = (struct efi_device_path *)remainder;
1875 		ret = EFI_SUCCESS;
1876 	} else {
1877 		ret = EFI_NOT_FOUND;
1878 	}
1879 out:
1880 	return EFI_EXIT(ret);
1881 }
1882 
1883 /*
1884  * Install multiple protocol interfaces.
1885  *
1886  * This function implements the MultipleProtocolInterfaces service.
1887  * See the Unified Extensible Firmware Interface (UEFI) specification
1888  * for details.
1889  *
1890  * @handle	handle on which the protocol interfaces shall be installed
1891  * @...		NULL terminated argument list with pairs of protocol GUIDS and
1892  *		interfaces
1893  * @return	status code
1894  */
1895 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1896 			void **handle, ...)
1897 {
1898 	EFI_ENTRY("%p", handle);
1899 
1900 	va_list argptr;
1901 	const efi_guid_t *protocol;
1902 	void *protocol_interface;
1903 	efi_status_t r = EFI_SUCCESS;
1904 	int i = 0;
1905 
1906 	if (!handle)
1907 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1908 
1909 	va_start(argptr, handle);
1910 	for (;;) {
1911 		protocol = va_arg(argptr, efi_guid_t*);
1912 		if (!protocol)
1913 			break;
1914 		protocol_interface = va_arg(argptr, void*);
1915 		r = EFI_CALL(efi_install_protocol_interface(
1916 						handle, protocol,
1917 						EFI_NATIVE_INTERFACE,
1918 						protocol_interface));
1919 		if (r != EFI_SUCCESS)
1920 			break;
1921 		i++;
1922 	}
1923 	va_end(argptr);
1924 	if (r == EFI_SUCCESS)
1925 		return EFI_EXIT(r);
1926 
1927 	/* If an error occurred undo all changes. */
1928 	va_start(argptr, handle);
1929 	for (; i; --i) {
1930 		protocol = va_arg(argptr, efi_guid_t*);
1931 		protocol_interface = va_arg(argptr, void*);
1932 		EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1933 							  protocol_interface));
1934 	}
1935 	va_end(argptr);
1936 
1937 	return EFI_EXIT(r);
1938 }
1939 
1940 /*
1941  * Uninstall multiple protocol interfaces.
1942  *
1943  * This function implements the UninstallMultipleProtocolInterfaces service.
1944  * See the Unified Extensible Firmware Interface (UEFI) specification
1945  * for details.
1946  *
1947  * @handle	handle from which the protocol interfaces shall be removed
1948  * @...		NULL terminated argument list with pairs of protocol GUIDS and
1949  *		interfaces
1950  * @return	status code
1951  */
1952 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1953 			void *handle, ...)
1954 {
1955 	EFI_ENTRY("%p", handle);
1956 
1957 	va_list argptr;
1958 	const efi_guid_t *protocol;
1959 	void *protocol_interface;
1960 	efi_status_t r = EFI_SUCCESS;
1961 	size_t i = 0;
1962 
1963 	if (!handle)
1964 		return EFI_EXIT(EFI_INVALID_PARAMETER);
1965 
1966 	va_start(argptr, handle);
1967 	for (;;) {
1968 		protocol = va_arg(argptr, efi_guid_t*);
1969 		if (!protocol)
1970 			break;
1971 		protocol_interface = va_arg(argptr, void*);
1972 		r = EFI_CALL(efi_uninstall_protocol_interface(
1973 						handle, protocol,
1974 						protocol_interface));
1975 		if (r != EFI_SUCCESS)
1976 			break;
1977 		i++;
1978 	}
1979 	va_end(argptr);
1980 	if (r == EFI_SUCCESS)
1981 		return EFI_EXIT(r);
1982 
1983 	/* If an error occurred undo all changes. */
1984 	va_start(argptr, handle);
1985 	for (; i; --i) {
1986 		protocol = va_arg(argptr, efi_guid_t*);
1987 		protocol_interface = va_arg(argptr, void*);
1988 		EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1989 							EFI_NATIVE_INTERFACE,
1990 							protocol_interface));
1991 	}
1992 	va_end(argptr);
1993 
1994 	return EFI_EXIT(r);
1995 }
1996 
1997 /*
1998  * Calculate cyclic redundancy code.
1999  *
2000  * This function implements the CalculateCrc32 service.
2001  * See the Unified Extensible Firmware Interface (UEFI) specification
2002  * for details.
2003  *
2004  * @data	buffer with data
2005  * @data_size	size of buffer in bytes
2006  * @crc32_p	cyclic redundancy code
2007  * @return	status code
2008  */
2009 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2010 					       unsigned long data_size,
2011 					       uint32_t *crc32_p)
2012 {
2013 	EFI_ENTRY("%p, %ld", data, data_size);
2014 	*crc32_p = crc32(0, data, data_size);
2015 	return EFI_EXIT(EFI_SUCCESS);
2016 }
2017 
2018 /*
2019  * Copy memory.
2020  *
2021  * This function implements the CopyMem service.
2022  * See the Unified Extensible Firmware Interface (UEFI) specification
2023  * for details.
2024  *
2025  * @destination		destination of the copy operation
2026  * @source		source of the copy operation
2027  * @length		number of bytes to copy
2028  */
2029 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2030 				size_t length)
2031 {
2032 	EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2033 	memcpy(destination, source, length);
2034 	EFI_EXIT(EFI_SUCCESS);
2035 }
2036 
2037 /*
2038  * Fill memory with a byte value.
2039  *
2040  * This function implements the SetMem service.
2041  * See the Unified Extensible Firmware Interface (UEFI) specification
2042  * for details.
2043  *
2044  * @buffer		buffer to fill
2045  * @size		size of buffer in bytes
2046  * @value		byte to copy to the buffer
2047  */
2048 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2049 {
2050 	EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2051 	memset(buffer, value, size);
2052 	EFI_EXIT(EFI_SUCCESS);
2053 }
2054 
2055 /*
2056  * Open protocol interface on a handle.
2057  *
2058  * This function implements the OpenProtocol interface.
2059  * See the Unified Extensible Firmware Interface (UEFI) specification
2060  * for details.
2061  *
2062  * @handle		handle on which the protocol shall be opened
2063  * @protocol		GUID of the protocol
2064  * @protocol_interface	interface implementing the protocol
2065  * @agent_handle	handle of the driver
2066  * @controller_handle	handle of the controller
2067  * @attributes		attributes indicating how to open the protocol
2068  * @return		status code
2069  */
2070 static efi_status_t EFIAPI efi_open_protocol(
2071 			void *handle, const efi_guid_t *protocol,
2072 			void **protocol_interface, void *agent_handle,
2073 			void *controller_handle, uint32_t attributes)
2074 {
2075 	struct efi_handler *handler;
2076 	efi_status_t r = EFI_INVALID_PARAMETER;
2077 
2078 	EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2079 		  protocol_interface, agent_handle, controller_handle,
2080 		  attributes);
2081 
2082 	if (!handle || !protocol ||
2083 	    (!protocol_interface && attributes !=
2084 	     EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2085 		goto out;
2086 	}
2087 
2088 	switch (attributes) {
2089 	case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2090 	case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2091 	case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2092 		break;
2093 	case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2094 		if (controller_handle == handle)
2095 			goto out;
2096 	case EFI_OPEN_PROTOCOL_BY_DRIVER:
2097 	case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2098 		if (controller_handle == NULL)
2099 			goto out;
2100 	case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2101 		if (agent_handle == NULL)
2102 			goto out;
2103 		break;
2104 	default:
2105 		goto out;
2106 	}
2107 
2108 	r = efi_search_protocol(handle, protocol, &handler);
2109 	if (r != EFI_SUCCESS)
2110 		goto out;
2111 
2112 	if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2113 		*protocol_interface = handler->protocol_interface;
2114 out:
2115 	return EFI_EXIT(r);
2116 }
2117 
2118 /*
2119  * Get interface of a protocol on a handle.
2120  *
2121  * This function implements the HandleProtocol service.
2122  * See the Unified Extensible Firmware Interface (UEFI) specification
2123  * for details.
2124  *
2125  * @handle		handle on which the protocol shall be opened
2126  * @protocol		GUID of the protocol
2127  * @protocol_interface  interface implementing the protocol
2128  * @return		status code
2129  */
2130 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2131 					       const efi_guid_t *protocol,
2132 					       void **protocol_interface)
2133 {
2134 	return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2135 				 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2136 }
2137 
2138 static const struct efi_boot_services efi_boot_services = {
2139 	.hdr = {
2140 		.headersize = sizeof(struct efi_table_hdr),
2141 	},
2142 	.raise_tpl = efi_raise_tpl,
2143 	.restore_tpl = efi_restore_tpl,
2144 	.allocate_pages = efi_allocate_pages_ext,
2145 	.free_pages = efi_free_pages_ext,
2146 	.get_memory_map = efi_get_memory_map_ext,
2147 	.allocate_pool = efi_allocate_pool_ext,
2148 	.free_pool = efi_free_pool_ext,
2149 	.create_event = efi_create_event_ext,
2150 	.set_timer = efi_set_timer_ext,
2151 	.wait_for_event = efi_wait_for_event,
2152 	.signal_event = efi_signal_event_ext,
2153 	.close_event = efi_close_event,
2154 	.check_event = efi_check_event,
2155 	.install_protocol_interface = efi_install_protocol_interface,
2156 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
2157 	.uninstall_protocol_interface = efi_uninstall_protocol_interface,
2158 	.handle_protocol = efi_handle_protocol,
2159 	.reserved = NULL,
2160 	.register_protocol_notify = efi_register_protocol_notify,
2161 	.locate_handle = efi_locate_handle_ext,
2162 	.locate_device_path = efi_locate_device_path,
2163 	.install_configuration_table = efi_install_configuration_table_ext,
2164 	.load_image = efi_load_image,
2165 	.start_image = efi_start_image,
2166 	.exit = efi_exit,
2167 	.unload_image = efi_unload_image,
2168 	.exit_boot_services = efi_exit_boot_services,
2169 	.get_next_monotonic_count = efi_get_next_monotonic_count,
2170 	.stall = efi_stall,
2171 	.set_watchdog_timer = efi_set_watchdog_timer,
2172 	.connect_controller = efi_connect_controller,
2173 	.disconnect_controller = efi_disconnect_controller,
2174 	.open_protocol = efi_open_protocol,
2175 	.close_protocol = efi_close_protocol,
2176 	.open_protocol_information = efi_open_protocol_information,
2177 	.protocols_per_handle = efi_protocols_per_handle,
2178 	.locate_handle_buffer = efi_locate_handle_buffer,
2179 	.locate_protocol = efi_locate_protocol,
2180 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2181 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2182 	.calculate_crc32 = efi_calculate_crc32,
2183 	.copy_mem = efi_copy_mem,
2184 	.set_mem = efi_set_mem,
2185 };
2186 
2187 
2188 static uint16_t __efi_runtime_data firmware_vendor[] =
2189 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
2190 
2191 struct efi_system_table __efi_runtime_data systab = {
2192 	.hdr = {
2193 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
2194 		.revision = 0x20005, /* 2.5 */
2195 		.headersize = sizeof(struct efi_table_hdr),
2196 	},
2197 	.fw_vendor = (long)firmware_vendor,
2198 	.con_in = (void*)&efi_con_in,
2199 	.con_out = (void*)&efi_con_out,
2200 	.std_err = (void*)&efi_con_out,
2201 	.runtime = (void*)&efi_runtime_services,
2202 	.boottime = (void*)&efi_boot_services,
2203 	.nr_tables = 0,
2204 	.tables = (void*)efi_conf_table,
2205 };
2206