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