xref: /openbmc/u-boot/lib/efi/efi.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  *
5  * EFI information obtained here:
6  * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
7  *
8  * Common EFI functions
9  */
10 
11 #include <common.h>
12 #include <debug_uart.h>
13 #include <errno.h>
14 #include <linux/err.h>
15 #include <linux/types.h>
16 #include <efi.h>
17 #include <efi_api.h>
18 
19 /*
20  * Unfortunately we cannot access any code outside what is built especially
21  * for the stub. lib/string.c is already being built for the U-Boot payload
22  * so it uses the wrong compiler flags. Add our own memset() here.
23  */
24 static void efi_memset(void *ptr, int ch, int size)
25 {
26 	char *dest = ptr;
27 
28 	while (size-- > 0)
29 		*dest++ = ch;
30 }
31 
32 /*
33  * Since the EFI stub cannot access most of the U-Boot code, add our own
34  * simple console output functions here. The EFI app will not use these since
35  * it can use the normal console.
36  */
37 void efi_putc(struct efi_priv *priv, const char ch)
38 {
39 	struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
40 	uint16_t ucode[2];
41 
42 	ucode[0] = ch;
43 	ucode[1] = '\0';
44 	con->output_string(con, ucode);
45 }
46 
47 void efi_puts(struct efi_priv *priv, const char *str)
48 {
49 	while (*str)
50 		efi_putc(priv, *str++);
51 }
52 
53 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
54 	     struct efi_system_table *sys_table)
55 {
56 	efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID;
57 	struct efi_boot_services *boot = sys_table->boottime;
58 	struct efi_loaded_image *loaded_image;
59 	int ret;
60 
61 	efi_memset(priv, '\0', sizeof(*priv));
62 	priv->sys_table = sys_table;
63 	priv->boot = sys_table->boottime;
64 	priv->parent_image = image;
65 	priv->run = sys_table->runtime;
66 
67 	efi_puts(priv, "U-Boot EFI ");
68 	efi_puts(priv, banner);
69 	efi_putc(priv, ' ');
70 
71 	ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
72 				  (void **)&loaded_image, &priv->parent_image,
73 				  NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
74 	if (ret) {
75 		efi_puts(priv, "Failed to get loaded image protocol\n");
76 		return ret;
77 	}
78 	priv->image_data_type = loaded_image->image_data_type;
79 
80 	return 0;
81 }
82 
83 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
84 {
85 	struct efi_boot_services *boot = priv->boot;
86 	void *buf = NULL;
87 
88 	*retp = boot->allocate_pool(priv->image_data_type, size, &buf);
89 
90 	return buf;
91 }
92 
93 void efi_free(struct efi_priv *priv, void *ptr)
94 {
95 	struct efi_boot_services *boot = priv->boot;
96 
97 	boot->free_pool(ptr);
98 }
99