1 // SPDX-License-Identifier: GPL-2.0+
3 * Functions shared by the app and stub
5 * Copyright (c) 2015 Google, Inc
7 * EFI information obtained here:
8 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
10 * Common EFI functions
14 #include <debug_uart.h>
17 #include <linux/err.h>
18 #include <linux/types.h>
22 static struct efi_priv *global_priv;
24 struct efi_priv *efi_get_priv(void)
29 void efi_set_priv(struct efi_priv *priv)
34 struct efi_system_table *efi_get_sys_table(void)
36 return global_priv->sys_table;
39 struct efi_boot_services *efi_get_boot(void)
41 return global_priv->boot;
44 unsigned long efi_get_ram_base(void)
46 return global_priv->ram_base;
50 * Global declaration of gd.
52 * As we write to it before relocation we have to make sure it is not put into
53 * a .bss section which may overlap a .rela section. Initialization forces it
54 * into a .data section which cannot overlap any .rela section.
56 struct global_data *global_data_ptr = (struct global_data *)~0;
59 * Unfortunately we cannot access any code outside what is built especially
60 * for the stub. lib/string.c is already being built for the U-Boot payload
61 * so it uses the wrong compiler flags. Add our own memset() here.
63 static void efi_memset(void *ptr, int ch, int size)
72 * Since the EFI stub cannot access most of the U-Boot code, add our own
73 * simple console output functions here. The EFI app will not use these since
74 * it can use the normal console.
76 void efi_putc(struct efi_priv *priv, const char ch)
78 struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
83 con->output_string(con, ucode);
86 void efi_puts(struct efi_priv *priv, const char *str)
89 efi_putc(priv, *str++);
92 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
93 struct efi_system_table *sys_table)
95 efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
96 struct efi_boot_services *boot = sys_table->boottime;
97 struct efi_loaded_image *loaded_image;
100 efi_memset(priv, '\0', sizeof(*priv));
101 priv->sys_table = sys_table;
102 priv->boot = sys_table->boottime;
103 priv->parent_image = image;
104 priv->run = sys_table->runtime;
106 efi_puts(priv, "U-Boot EFI ");
107 efi_puts(priv, banner);
110 ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
111 (void **)&loaded_image, priv->parent_image,
112 NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
114 efi_puts(priv, "Failed to get loaded image protocol\n");
117 priv->image_data_type = loaded_image->image_data_type;
122 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
124 struct efi_boot_services *boot = priv->boot;
127 *retp = boot->allocate_pool(priv->image_data_type, size, &buf);
132 void efi_free(struct efi_priv *priv, void *ptr)
134 struct efi_boot_services *boot = priv->boot;
136 boot->free_pool(ptr);
139 int efi_store_memory_map(struct efi_priv *priv)
141 struct efi_boot_services *boot = priv->sys_table->boottime;
142 efi_uintn_t size, desc_size;
145 /* Get the memory map so we can switch off EFI */
147 ret = boot->get_memory_map(&size, NULL, &priv->memmap_key,
148 &priv->memmap_desc_size,
149 &priv->memmap_version);
150 if (ret != EFI_BUFFER_TOO_SMALL) {
152 * Note this function avoids using printf() since it is not
153 * available in the stub
155 printhex2(EFI_BITS_PER_LONG);
158 puts(" No memory map\n");
162 * Since doing a malloc() may change the memory map and also we want to
163 * be able to read the memory map in efi_call_exit_boot_services()
164 * below, after more changes have happened
166 priv->memmap_alloc = size + 1024;
167 priv->memmap_size = priv->memmap_alloc;
168 priv->memmap_desc = efi_malloc(priv, size, &ret);
169 if (!priv->memmap_desc) {
171 puts(" No memory for memory descriptor\n");
175 ret = boot->get_memory_map(&priv->memmap_size, priv->memmap_desc,
176 &priv->memmap_key, &desc_size,
177 &priv->memmap_version);
180 puts(" Can't get memory map\n");
187 int efi_call_exit_boot_services(void)
189 struct efi_priv *priv = efi_get_priv();
190 const struct efi_boot_services *boot = priv->boot;
195 size = priv->memmap_alloc;
196 ret = boot->get_memory_map(&size, priv->memmap_desc,
198 &priv->memmap_desc_size, &version);
201 puts(" Can't get memory map\n");
204 ret = boot->exit_boot_services(priv->parent_image, priv->memmap_key);