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 /* #define DEBUG_EFI */
10 
11 #include <common.h>
12 #include <efi_loader.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 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list);
25 
26 /*
27  * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
28  * we need to do trickery with caches. Since we don't want to break the EFI
29  * aware boot path, only apply hacks when loading exiting directly (breaking
30  * direct Linux EFI booting along the way - oh well).
31  */
32 static bool efi_is_direct_boot = true;
33 
34 /*
35  * EFI can pass arbitrary additional "tables" containing vendor specific
36  * information to the payload. One such table is the FDT table which contains
37  * a pointer to a flattened device tree blob.
38  *
39  * In most cases we want to pass an FDT to the payload, so reserve one slot of
40  * config table space for it. The pointer gets populated by do_bootefi_exec().
41  */
42 static struct efi_configuration_table EFI_RUNTIME_DATA efi_conf_table[1];
43 
44 /*
45  * The "gd" pointer lives in a register on ARM and AArch64 that we declare
46  * fixed when compiling U-Boot. However, the payload does not know about that
47  * restriction so we need to manually swap its and our view of that register on
48  * EFI callback entry/exit.
49  */
50 static volatile void *efi_gd, *app_gd;
51 
52 /* Called from do_bootefi_exec() */
53 void efi_save_gd(void)
54 {
55 	efi_gd = gd;
56 }
57 
58 /* Called on every callback entry */
59 void efi_restore_gd(void)
60 {
61 	/* Only restore if we're already in EFI context */
62 	if (!efi_gd)
63 		return;
64 
65 	if (gd != efi_gd)
66 		app_gd = gd;
67 	gd = efi_gd;
68 }
69 
70 /* Called on every callback exit */
71 efi_status_t efi_exit_func(efi_status_t ret)
72 {
73 	gd = app_gd;
74 	return ret;
75 }
76 
77 static efi_status_t efi_unsupported(const char *funcname)
78 {
79 #ifdef DEBUG_EFI
80 	printf("EFI: App called into unimplemented function %s\n", funcname);
81 #endif
82 	return EFI_EXIT(EFI_UNSUPPORTED);
83 }
84 
85 static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
86 {
87 	return memcmp(g1, g2, sizeof(efi_guid_t));
88 }
89 
90 static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl)
91 {
92 	EFI_ENTRY("0x%lx", new_tpl);
93 	return EFI_EXIT(0);
94 }
95 
96 static void EFIAPI efi_restore_tpl(unsigned long old_tpl)
97 {
98 	EFI_ENTRY("0x%lx", old_tpl);
99 	EFI_EXIT(efi_unsupported(__func__));
100 }
101 
102 efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
103 					   unsigned long pages,
104 					   uint64_t *memory)
105 {
106 	efi_status_t r;
107 
108 	EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
109 	r = efi_allocate_pages(type, memory_type, pages, memory);
110 	return EFI_EXIT(r);
111 }
112 
113 efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, unsigned long pages)
114 {
115 	efi_status_t r;
116 
117 	EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
118 	r = efi_free_pages(memory, pages);
119 	return EFI_EXIT(r);
120 }
121 
122 efi_status_t EFIAPI efi_get_memory_map_ext(unsigned long *memory_map_size,
123 					   struct efi_mem_desc *memory_map,
124 					   unsigned long *map_key,
125 					   unsigned long *descriptor_size,
126 					   uint32_t *descriptor_version)
127 {
128 	efi_status_t r;
129 
130 	EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
131 		  map_key, descriptor_size, descriptor_version);
132 	r = efi_get_memory_map(memory_map_size, memory_map, map_key,
133 			       descriptor_size, descriptor_version);
134 	return EFI_EXIT(r);
135 }
136 
137 static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long size,
138 					     void **buffer)
139 {
140 	efi_status_t r;
141 
142 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
143 	r = efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, (void*)buffer);
144 	return EFI_EXIT(r);
145 }
146 
147 static efi_status_t EFIAPI efi_free_pool(void *buffer)
148 {
149 	efi_status_t r;
150 
151 	EFI_ENTRY("%p", buffer);
152 	r = efi_free_pages((ulong)buffer, 0);
153 	return EFI_EXIT(r);
154 }
155 
156 /*
157  * Our event capabilities are very limited. Only support a single
158  * event to exist, so we don't need to maintain lists.
159  */
160 static struct {
161 	enum efi_event_type type;
162 	u32 trigger_type;
163 	u32 trigger_time;
164 	u64 trigger_next;
165 	unsigned long notify_tpl;
166 	void (*notify_function) (void *event, void *context);
167 	void *notify_context;
168 } efi_event = {
169 	/* Disable timers on bootup */
170 	.trigger_next = -1ULL,
171 };
172 
173 static efi_status_t EFIAPI efi_create_event(
174 			enum efi_event_type type, ulong notify_tpl,
175 			void (*notify_function) (void *event, void *context),
176 			void *notify_context, void **event)
177 {
178 	EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function,
179 		  notify_context);
180 	if (efi_event.notify_function) {
181 		/* We only support one event at a time */
182 		return EFI_EXIT(EFI_OUT_OF_RESOURCES);
183 	}
184 
185 	efi_event.type = type;
186 	efi_event.notify_tpl = notify_tpl;
187 	efi_event.notify_function = notify_function;
188 	efi_event.notify_context = notify_context;
189 	*event = &efi_event;
190 
191 	return EFI_EXIT(EFI_SUCCESS);
192 }
193 
194 /*
195  * Our timers have to work without interrupts, so we check whenever keyboard
196  * input or disk accesses happen if enough time elapsed for it to fire.
197  */
198 void efi_timer_check(void)
199 {
200 	u64 now = timer_get_us();
201 
202 	if (now >= efi_event.trigger_next) {
203 		/* Triggering! */
204 		if (efi_event.trigger_type == EFI_TIMER_PERIODIC)
205 			efi_event.trigger_next += efi_event.trigger_time / 10;
206 		efi_event.notify_function(&efi_event, efi_event.notify_context);
207 	}
208 
209 	WATCHDOG_RESET();
210 }
211 
212 static efi_status_t EFIAPI efi_set_timer(void *event, int type,
213 					 uint64_t trigger_time)
214 {
215 	/* We don't have 64bit division available everywhere, so limit timer
216 	 * distances to 32bit bits. */
217 	u32 trigger32 = trigger_time;
218 
219 	EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
220 
221 	if (trigger32 < trigger_time) {
222 		printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
223 		       trigger_time, trigger32);
224 	}
225 
226 	if (event != &efi_event) {
227 		/* We only support one event at a time */
228 		return EFI_EXIT(EFI_INVALID_PARAMETER);
229 	}
230 
231 	switch (type) {
232 	case EFI_TIMER_STOP:
233 		efi_event.trigger_next = -1ULL;
234 		break;
235 	case EFI_TIMER_PERIODIC:
236 	case EFI_TIMER_RELATIVE:
237 		efi_event.trigger_next = timer_get_us() + (trigger32 / 10);
238 		break;
239 	default:
240 		return EFI_EXIT(EFI_INVALID_PARAMETER);
241 	}
242 	efi_event.trigger_type = type;
243 	efi_event.trigger_time = trigger_time;
244 
245 	return EFI_EXIT(EFI_SUCCESS);
246 }
247 
248 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
249 					      void *event, unsigned long *index)
250 {
251 	u64 now;
252 
253 	EFI_ENTRY("%ld, %p, %p", num_events, event, index);
254 
255 	now = timer_get_us();
256 	while (now < efi_event.trigger_next) { }
257 	efi_timer_check();
258 
259 	return EFI_EXIT(EFI_SUCCESS);
260 }
261 
262 static efi_status_t EFIAPI efi_signal_event(void *event)
263 {
264 	EFI_ENTRY("%p", event);
265 	return EFI_EXIT(EFI_SUCCESS);
266 }
267 
268 static efi_status_t EFIAPI efi_close_event(void *event)
269 {
270 	EFI_ENTRY("%p", event);
271 	efi_event.trigger_next = -1ULL;
272 	return EFI_EXIT(EFI_SUCCESS);
273 }
274 
275 static efi_status_t EFIAPI efi_check_event(void *event)
276 {
277 	EFI_ENTRY("%p", event);
278 	return EFI_EXIT(EFI_NOT_READY);
279 }
280 
281 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
282 			efi_guid_t *protocol, int protocol_interface_type,
283 			void *protocol_interface)
284 {
285 	EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
286 		  protocol_interface);
287 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
288 }
289 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
290 			efi_guid_t *protocol, void *old_interface,
291 			void *new_interface)
292 {
293 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
294 		  new_interface);
295 	return EFI_EXIT(EFI_ACCESS_DENIED);
296 }
297 
298 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
299 			efi_guid_t *protocol, void *protocol_interface)
300 {
301 	EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
302 	return EFI_EXIT(EFI_NOT_FOUND);
303 }
304 
305 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
306 							void *event,
307 							void **registration)
308 {
309 	EFI_ENTRY("%p, %p, %p", protocol, event, registration);
310 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
311 }
312 
313 static int efi_search(enum efi_locate_search_type search_type,
314 		      efi_guid_t *protocol, void *search_key,
315 		      struct efi_object *efiobj)
316 {
317 	int i;
318 
319 	switch (search_type) {
320 	case all_handles:
321 		return 0;
322 	case by_register_notify:
323 		return -1;
324 	case by_protocol:
325 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
326 			const efi_guid_t *guid = efiobj->protocols[i].guid;
327 			if (guid && !guidcmp(guid, protocol))
328 				return 0;
329 		}
330 		return -1;
331 	}
332 
333 	return -1;
334 }
335 
336 static efi_status_t EFIAPI efi_locate_handle(
337 			enum efi_locate_search_type search_type,
338 			efi_guid_t *protocol, void *search_key,
339 			unsigned long *buffer_size, efi_handle_t *buffer)
340 {
341 	struct list_head *lhandle;
342 	unsigned long size = 0;
343 
344 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
345 		  buffer_size, buffer);
346 
347 	/* Count how much space we need */
348 	list_for_each(lhandle, &efi_obj_list) {
349 		struct efi_object *efiobj;
350 		efiobj = list_entry(lhandle, struct efi_object, link);
351 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
352 			size += sizeof(void*);
353 		}
354 	}
355 
356 	if (*buffer_size < size) {
357 		*buffer_size = size;
358 		return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
359 	}
360 
361 	/* Then fill the array */
362 	list_for_each(lhandle, &efi_obj_list) {
363 		struct efi_object *efiobj;
364 		efiobj = list_entry(lhandle, struct efi_object, link);
365 		if (!efi_search(search_type, protocol, search_key, efiobj)) {
366 			*(buffer++) = efiobj->handle;
367 		}
368 	}
369 
370 	*buffer_size = size;
371 	return EFI_EXIT(EFI_SUCCESS);
372 }
373 
374 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
375 			struct efi_device_path **device_path,
376 			efi_handle_t *device)
377 {
378 	EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
379 	return EFI_EXIT(EFI_NOT_FOUND);
380 }
381 
382 static efi_status_t EFIAPI efi_install_configuration_table(efi_guid_t *guid,
383 							   void *table)
384 {
385 	int i;
386 
387 	EFI_ENTRY("%p, %p", guid, table);
388 
389 	/* Check for guid override */
390 	for (i = 0; i < systab.nr_tables; i++) {
391 		if (!guidcmp(guid, &efi_conf_table[i].guid)) {
392 			efi_conf_table[i].table = table;
393 			return EFI_EXIT(EFI_SUCCESS);
394 		}
395 	}
396 
397 	/* No override, check for overflow */
398 	if (i >= ARRAY_SIZE(efi_conf_table))
399 		return EFI_EXIT(EFI_OUT_OF_RESOURCES);
400 
401 	/* Add a new entry */
402 	memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
403 	efi_conf_table[i].table = table;
404 	systab.nr_tables = i;
405 
406 	return EFI_EXIT(EFI_SUCCESS);
407 }
408 
409 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
410 					  efi_handle_t parent_image,
411 					  struct efi_device_path *file_path,
412 					  void *source_buffer,
413 					  unsigned long source_size,
414 					  efi_handle_t *image_handle)
415 {
416 	static struct efi_object loaded_image_info_obj = {
417 		.protocols = {
418 			{
419 				.guid = &efi_guid_loaded_image,
420 				.open = &efi_return_handle,
421 			},
422 		},
423 	};
424 	struct efi_loaded_image *info;
425 	struct efi_object *obj;
426 
427 	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
428 		  file_path, source_buffer, source_size, image_handle);
429 	info = malloc(sizeof(*info));
430 	obj = malloc(sizeof(loaded_image_info_obj));
431 	memset(info, 0, sizeof(*info));
432 	memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
433 	obj->handle = info;
434 	info->file_path = file_path;
435 	info->reserved = efi_load_pe(source_buffer, info);
436 	if (!info->reserved) {
437 		free(info);
438 		free(obj);
439 		return EFI_EXIT(EFI_UNSUPPORTED);
440 	}
441 
442 	*image_handle = info;
443 	list_add_tail(&obj->link, &efi_obj_list);
444 
445 	return EFI_EXIT(EFI_SUCCESS);
446 }
447 
448 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
449 					   unsigned long *exit_data_size,
450 					   s16 **exit_data)
451 {
452 	ulong (*entry)(void *image_handle, struct efi_system_table *st);
453 	struct efi_loaded_image *info = image_handle;
454 
455 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
456 	entry = info->reserved;
457 
458 	efi_is_direct_boot = false;
459 
460 	/* call the image! */
461 	entry(image_handle, &systab);
462 
463 	/* Should usually never get here */
464 	return EFI_EXIT(EFI_SUCCESS);
465 }
466 
467 static efi_status_t EFIAPI efi_exit(void *image_handle, long exit_status,
468 				    unsigned long exit_data_size,
469 				    uint16_t *exit_data)
470 {
471 	EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
472 		  exit_data_size, exit_data);
473 	return EFI_EXIT(efi_unsupported(__func__));
474 }
475 
476 static struct efi_object *efi_search_obj(void *handle)
477 {
478 	struct list_head *lhandle;
479 
480 	list_for_each(lhandle, &efi_obj_list) {
481 		struct efi_object *efiobj;
482 		efiobj = list_entry(lhandle, struct efi_object, link);
483 		if (efiobj->handle == handle)
484 			return efiobj;
485 	}
486 
487 	return NULL;
488 }
489 
490 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
491 {
492 	struct efi_object *efiobj;
493 
494 	EFI_ENTRY("%p", image_handle);
495 	efiobj = efi_search_obj(image_handle);
496 	if (efiobj)
497 		list_del(&efiobj->link);
498 
499 	return EFI_EXIT(EFI_SUCCESS);
500 }
501 
502 static void efi_exit_caches(void)
503 {
504 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
505 	/*
506 	 * Grub on 32bit ARM needs to have caches disabled before jumping into
507 	 * a zImage, but does not know of all cache layers. Give it a hand.
508 	 */
509 	if (efi_is_direct_boot)
510 		cleanup_before_linux();
511 #endif
512 }
513 
514 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
515 						  unsigned long map_key)
516 {
517 	EFI_ENTRY("%p, %ld", image_handle, map_key);
518 
519 	/* Fix up caches for EFI payloads if necessary */
520 	efi_exit_caches();
521 
522 	/* This stops all lingering devices */
523 	bootm_disable_interrupts();
524 
525 	/* Give the payload some time to boot */
526 	WATCHDOG_RESET();
527 
528 	return EFI_EXIT(EFI_SUCCESS);
529 }
530 
531 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
532 {
533 	static uint64_t mono = 0;
534 	EFI_ENTRY("%p", count);
535 	*count = mono++;
536 	return EFI_EXIT(EFI_SUCCESS);
537 }
538 
539 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
540 {
541 	EFI_ENTRY("%ld", microseconds);
542 	udelay(microseconds);
543 	return EFI_EXIT(EFI_SUCCESS);
544 }
545 
546 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
547 						  uint64_t watchdog_code,
548 						  unsigned long data_size,
549 						  uint16_t *watchdog_data)
550 {
551 	EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
552 		  data_size, watchdog_data);
553 	return EFI_EXIT(efi_unsupported(__func__));
554 }
555 
556 static efi_status_t EFIAPI efi_connect_controller(
557 			efi_handle_t controller_handle,
558 			efi_handle_t *driver_image_handle,
559 			struct efi_device_path *remain_device_path,
560 			bool recursive)
561 {
562 	EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
563 		  remain_device_path, recursive);
564 	return EFI_EXIT(EFI_NOT_FOUND);
565 }
566 
567 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
568 						     void *driver_image_handle,
569 						     void *child_handle)
570 {
571 	EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
572 		  child_handle);
573 	return EFI_EXIT(EFI_INVALID_PARAMETER);
574 }
575 
576 static efi_status_t EFIAPI efi_close_protocol(void *handle,
577 					      efi_guid_t *protocol,
578 					      void *agent_handle,
579 					      void *controller_handle)
580 {
581 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
582 		  controller_handle);
583 	return EFI_EXIT(EFI_NOT_FOUND);
584 }
585 
586 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
587 			efi_guid_t *protocol,
588 			struct efi_open_protocol_info_entry **entry_buffer,
589 			unsigned long *entry_count)
590 {
591 	EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
592 		  entry_count);
593 	return EFI_EXIT(EFI_NOT_FOUND);
594 }
595 
596 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
597 			efi_guid_t ***protocol_buffer,
598 			unsigned long *protocol_buffer_count)
599 {
600 	EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
601 		  protocol_buffer_count);
602 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
603 }
604 
605 static efi_status_t EFIAPI efi_locate_handle_buffer(
606 			enum efi_locate_search_type search_type,
607 			efi_guid_t *protocol, void *search_key,
608 			unsigned long *no_handles, efi_handle_t **buffer)
609 {
610 	EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
611 		  no_handles, buffer);
612 	return EFI_EXIT(EFI_NOT_FOUND);
613 }
614 
615 static struct efi_class_map efi_class_maps[] = {
616 	{
617 		.guid = &efi_guid_console_control,
618 		.interface = &efi_console_control
619 	},
620 };
621 
622 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
623 					       void *registration,
624 					       void **protocol_interface)
625 {
626 	int i;
627 
628 	EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
629 	for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) {
630 		struct efi_class_map *curmap = &efi_class_maps[i];
631 		if (!guidcmp(protocol, curmap->guid)) {
632 			*protocol_interface = (void*)curmap->interface;
633 			return EFI_EXIT(EFI_SUCCESS);
634 		}
635 	}
636 
637 	return EFI_EXIT(EFI_NOT_FOUND);
638 }
639 
640 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
641 			void **handle, ...)
642 {
643 	EFI_ENTRY("%p", handle);
644 	return EFI_EXIT(EFI_OUT_OF_RESOURCES);
645 }
646 
647 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
648 			void *handle, ...)
649 {
650 	EFI_ENTRY("%p", handle);
651 	return EFI_EXIT(EFI_INVALID_PARAMETER);
652 }
653 
654 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
655 					       unsigned long data_size,
656 					       uint32_t *crc32_p)
657 {
658 	EFI_ENTRY("%p, %ld", data, data_size);
659 	*crc32_p = crc32(0, data, data_size);
660 	return EFI_EXIT(EFI_SUCCESS);
661 }
662 
663 static void EFIAPI efi_copy_mem(void *destination, void *source,
664 				unsigned long length)
665 {
666 	EFI_ENTRY("%p, %p, %ld", destination, source, length);
667 	memcpy(destination, source, length);
668 }
669 
670 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
671 {
672 	EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
673 	memset(buffer, value, size);
674 }
675 
676 static efi_status_t EFIAPI efi_open_protocol(
677 			void *handle, efi_guid_t *protocol,
678 			void **protocol_interface, void *agent_handle,
679 			void *controller_handle, uint32_t attributes)
680 {
681 	struct list_head *lhandle;
682 	int i;
683 	efi_status_t r = EFI_UNSUPPORTED;
684 
685 	EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
686 		  protocol_interface, agent_handle, controller_handle,
687 		  attributes);
688 	list_for_each(lhandle, &efi_obj_list) {
689 		struct efi_object *efiobj;
690 		efiobj = list_entry(lhandle, struct efi_object, link);
691 
692 		if (efiobj->handle != handle)
693 			continue;
694 
695 		for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
696 			struct efi_handler *handler = &efiobj->protocols[i];
697 			const efi_guid_t *hprotocol = handler->guid;
698 			if (!hprotocol)
699 				break;
700 			if (!guidcmp(hprotocol, protocol)) {
701 				r = handler->open(handle, protocol,
702 				    protocol_interface, agent_handle,
703 				    controller_handle, attributes);
704 				goto out;
705 			}
706 		}
707 	}
708 
709 out:
710 	return EFI_EXIT(r);
711 }
712 
713 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
714 					       efi_guid_t *protocol,
715 					       void **protocol_interface)
716 {
717 	return efi_open_protocol(handle, protocol, protocol_interface,
718 				 NULL, NULL, 0);
719 }
720 
721 static const struct efi_boot_services efi_boot_services = {
722 	.hdr = {
723 		.headersize = sizeof(struct efi_table_hdr),
724 	},
725 	.raise_tpl = efi_raise_tpl,
726 	.restore_tpl = efi_restore_tpl,
727 	.allocate_pages = efi_allocate_pages_ext,
728 	.free_pages = efi_free_pages_ext,
729 	.get_memory_map = efi_get_memory_map_ext,
730 	.allocate_pool = efi_allocate_pool,
731 	.free_pool = efi_free_pool,
732 	.create_event = efi_create_event,
733 	.set_timer = efi_set_timer,
734 	.wait_for_event = efi_wait_for_event,
735 	.signal_event = efi_signal_event,
736 	.close_event = efi_close_event,
737 	.check_event = efi_check_event,
738 	.install_protocol_interface = efi_install_protocol_interface,
739 	.reinstall_protocol_interface = efi_reinstall_protocol_interface,
740 	.uninstall_protocol_interface = efi_uninstall_protocol_interface,
741 	.handle_protocol = efi_handle_protocol,
742 	.reserved = NULL,
743 	.register_protocol_notify = efi_register_protocol_notify,
744 	.locate_handle = efi_locate_handle,
745 	.locate_device_path = efi_locate_device_path,
746 	.install_configuration_table = efi_install_configuration_table,
747 	.load_image = efi_load_image,
748 	.start_image = efi_start_image,
749 	.exit = (void*)efi_exit,
750 	.unload_image = efi_unload_image,
751 	.exit_boot_services = efi_exit_boot_services,
752 	.get_next_monotonic_count = efi_get_next_monotonic_count,
753 	.stall = efi_stall,
754 	.set_watchdog_timer = efi_set_watchdog_timer,
755 	.connect_controller = efi_connect_controller,
756 	.disconnect_controller = efi_disconnect_controller,
757 	.open_protocol = efi_open_protocol,
758 	.close_protocol = efi_close_protocol,
759 	.open_protocol_information = efi_open_protocol_information,
760 	.protocols_per_handle = efi_protocols_per_handle,
761 	.locate_handle_buffer = efi_locate_handle_buffer,
762 	.locate_protocol = efi_locate_protocol,
763 	.install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
764 	.uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
765 	.calculate_crc32 = efi_calculate_crc32,
766 	.copy_mem = efi_copy_mem,
767 	.set_mem = efi_set_mem,
768 };
769 
770 
771 static uint16_t EFI_RUNTIME_DATA firmware_vendor[] =
772 	{ 'D','a','s',' ','U','-','b','o','o','t',0 };
773 
774 struct efi_system_table EFI_RUNTIME_DATA systab = {
775 	.hdr = {
776 		.signature = EFI_SYSTEM_TABLE_SIGNATURE,
777 		.revision = 0x20005, /* 2.5 */
778 		.headersize = sizeof(struct efi_table_hdr),
779 	},
780 	.fw_vendor = (long)firmware_vendor,
781 	.con_in = (void*)&efi_con_in,
782 	.con_out = (void*)&efi_con_out,
783 	.std_err = (void*)&efi_con_out,
784 	.runtime = (void*)&efi_runtime_services,
785 	.boottime = (void*)&efi_boot_services,
786 	.nr_tables = 0,
787 	.tables = (void*)efi_conf_table,
788 };
789