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
13 #include <debug_uart.h>
16 #include <linux/err.h>
17 #include <linux/types.h>
21 static struct efi_priv *global_priv;
23 struct efi_priv *efi_get_priv(void)
28 void efi_set_priv(struct efi_priv *priv)
33 struct efi_system_table *efi_get_sys_table(void)
35 return global_priv->sys_table;
38 struct efi_boot_services *efi_get_boot(void)
40 return global_priv->boot;
43 unsigned long efi_get_ram_base(void)
45 return global_priv->ram_base;
49 * Global declaration of gd.
51 * As we write to it before relocation we have to make sure it is not put into
52 * a .bss section which may overlap a .rela section. Initialization forces it
53 * into a .data section which cannot overlap any .rela section.
55 struct global_data *global_data_ptr = (struct global_data *)~0;
58 * Unfortunately we cannot access any code outside what is built especially
59 * for the stub. lib/string.c is already being built for the U-Boot payload
60 * so it uses the wrong compiler flags. Add our own memset() here.
62 static void efi_memset(void *ptr, int ch, int size)
71 * Since the EFI stub cannot access most of the U-Boot code, add our own
72 * simple console output functions here. The EFI app will not use these since
73 * it can use the normal console.
75 void efi_putc(struct efi_priv *priv, const char ch)
77 struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
82 con->output_string(con, ucode);
85 void efi_puts(struct efi_priv *priv, const char *str)
88 efi_putc(priv, *str++);
91 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
92 struct efi_system_table *sys_table)
94 efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
95 struct efi_boot_services *boot = sys_table->boottime;
96 struct efi_loaded_image *loaded_image;
99 efi_memset(priv, '\0', sizeof(*priv));
100 priv->sys_table = sys_table;
101 priv->boot = sys_table->boottime;
102 priv->parent_image = image;
103 priv->run = sys_table->runtime;
105 efi_puts(priv, "U-Boot EFI ");
106 efi_puts(priv, banner);
109 ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
110 (void **)&loaded_image, priv->parent_image,
111 NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
113 efi_puts(priv, "Failed to get loaded image protocol\n");
116 priv->image_data_type = loaded_image->image_data_type;
121 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
123 struct efi_boot_services *boot = priv->boot;
126 *retp = boot->allocate_pool(priv->image_data_type, size, &buf);
131 void efi_free(struct efi_priv *priv, void *ptr)
133 struct efi_boot_services *boot = priv->boot;
135 boot->free_pool(ptr);
138 int efi_store_memory_map(struct efi_priv *priv)
140 struct efi_boot_services *boot = priv->sys_table->boottime;
141 efi_uintn_t size, desc_size;
144 /* Get the memory map so we can switch off EFI */
146 ret = boot->get_memory_map(&size, NULL, &priv->memmap_key,
147 &priv->memmap_desc_size,
148 &priv->memmap_version);
149 if (ret != EFI_BUFFER_TOO_SMALL) {
151 * Note this function avoids using printf() since it is not
152 * available in the stub
154 printhex2(EFI_BITS_PER_LONG);
157 puts(" No memory map\n");
161 * Since doing a malloc() may change the memory map and also we want to
162 * be able to read the memory map in efi_call_exit_boot_services()
163 * below, after more changes have happened
165 priv->memmap_alloc = size + 1024;
166 priv->memmap_size = priv->memmap_alloc;
167 priv->memmap_desc = efi_malloc(priv, size, &ret);
168 if (!priv->memmap_desc) {
170 puts(" No memory for memory descriptor\n");
174 ret = boot->get_memory_map(&priv->memmap_size, priv->memmap_desc,
175 &priv->memmap_key, &desc_size,
176 &priv->memmap_version);
179 puts(" Can't get memory map\n");
186 int efi_call_exit_boot_services(void)
188 struct efi_priv *priv = efi_get_priv();
189 const struct efi_boot_services *boot = priv->boot;
194 size = priv->memmap_alloc;
195 ret = boot->get_memory_map(&size, priv->memmap_desc,
197 &priv->memmap_desc_size, &version);
200 puts(" Can't get memory map\n");
203 ret = boot->exit_boot_services(priv->parent_image, priv->memmap_key);