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