Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 2 | /* |
Simon Glass | 2a1cf03 | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 3 | * Functions shared by the app and stub |
| 4 | * |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 5 | * Copyright (c) 2015 Google, Inc |
| 6 | * |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 7 | * EFI information obtained here: |
| 8 | * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES |
| 9 | * |
| 10 | * Common EFI functions |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <debug_uart.h> |
| 15 | #include <errno.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 17 | #include <linux/err.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <efi.h> |
| 20 | #include <efi_api.h> |
| 21 | |
Simon Glass | 2a1cf03 | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 22 | static struct efi_priv *global_priv; |
| 23 | |
| 24 | struct efi_priv *efi_get_priv(void) |
| 25 | { |
| 26 | return global_priv; |
| 27 | } |
| 28 | |
| 29 | void efi_set_priv(struct efi_priv *priv) |
| 30 | { |
| 31 | global_priv = priv; |
| 32 | } |
| 33 | |
| 34 | struct efi_system_table *efi_get_sys_table(void) |
| 35 | { |
| 36 | return global_priv->sys_table; |
| 37 | } |
| 38 | |
| 39 | struct efi_boot_services *efi_get_boot(void) |
| 40 | { |
| 41 | return global_priv->boot; |
| 42 | } |
| 43 | |
| 44 | unsigned long efi_get_ram_base(void) |
| 45 | { |
| 46 | return global_priv->ram_base; |
| 47 | } |
| 48 | |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 49 | /* |
Simon Glass | 9c297a3 | 2021-11-03 21:09:08 -0600 | [diff] [blame] | 50 | * Global declaration of gd. |
| 51 | * |
| 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. |
| 55 | */ |
| 56 | struct global_data *global_data_ptr = (struct global_data *)~0; |
| 57 | |
| 58 | /* |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 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. |
| 62 | */ |
| 63 | static void efi_memset(void *ptr, int ch, int size) |
| 64 | { |
| 65 | char *dest = ptr; |
| 66 | |
| 67 | while (size-- > 0) |
| 68 | *dest++ = ch; |
| 69 | } |
| 70 | |
| 71 | /* |
| 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. |
| 75 | */ |
| 76 | void efi_putc(struct efi_priv *priv, const char ch) |
| 77 | { |
| 78 | struct efi_simple_text_output_protocol *con = priv->sys_table->con_out; |
| 79 | uint16_t ucode[2]; |
| 80 | |
| 81 | ucode[0] = ch; |
| 82 | ucode[1] = '\0'; |
| 83 | con->output_string(con, ucode); |
| 84 | } |
| 85 | |
| 86 | void efi_puts(struct efi_priv *priv, const char *str) |
| 87 | { |
| 88 | while (*str) |
| 89 | efi_putc(priv, *str++); |
| 90 | } |
| 91 | |
| 92 | int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image, |
| 93 | struct efi_system_table *sys_table) |
| 94 | { |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 95 | efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 96 | struct efi_boot_services *boot = sys_table->boottime; |
| 97 | struct efi_loaded_image *loaded_image; |
| 98 | int ret; |
| 99 | |
| 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; |
| 105 | |
| 106 | efi_puts(priv, "U-Boot EFI "); |
| 107 | efi_puts(priv, banner); |
| 108 | efi_putc(priv, ' '); |
| 109 | |
| 110 | ret = boot->open_protocol(priv->parent_image, &loaded_image_guid, |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 111 | (void **)&loaded_image, priv->parent_image, |
Simon Glass | 867a6ac | 2015-07-31 09:31:36 -0600 | [diff] [blame] | 112 | NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 113 | if (ret) { |
| 114 | efi_puts(priv, "Failed to get loaded image protocol\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | priv->image_data_type = loaded_image->image_data_type; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp) |
| 123 | { |
| 124 | struct efi_boot_services *boot = priv->boot; |
| 125 | void *buf = NULL; |
| 126 | |
| 127 | *retp = boot->allocate_pool(priv->image_data_type, size, &buf); |
| 128 | |
| 129 | return buf; |
| 130 | } |
| 131 | |
| 132 | void efi_free(struct efi_priv *priv, void *ptr) |
| 133 | { |
| 134 | struct efi_boot_services *boot = priv->boot; |
| 135 | |
| 136 | boot->free_pool(ptr); |
| 137 | } |
Simon Glass | 866e2ac | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 138 | |
| 139 | int efi_store_memory_map(struct efi_priv *priv) |
| 140 | { |
| 141 | struct efi_boot_services *boot = priv->sys_table->boottime; |
| 142 | efi_uintn_t size, desc_size; |
| 143 | efi_status_t ret; |
| 144 | |
| 145 | /* Get the memory map so we can switch off EFI */ |
| 146 | size = 0; |
| 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) { |
| 151 | /* |
| 152 | * Note this function avoids using printf() since it is not |
| 153 | * available in the stub |
| 154 | */ |
| 155 | printhex2(EFI_BITS_PER_LONG); |
| 156 | putc(' '); |
| 157 | printhex2(ret); |
| 158 | puts(" No memory map\n"); |
| 159 | return ret; |
| 160 | } |
| 161 | /* |
| 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 |
| 165 | */ |
| 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) { |
| 170 | printhex2(ret); |
| 171 | puts(" No memory for memory descriptor\n"); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | ret = boot->get_memory_map(&priv->memmap_size, priv->memmap_desc, |
| 176 | &priv->memmap_key, &desc_size, |
| 177 | &priv->memmap_version); |
| 178 | if (ret) { |
| 179 | printhex2(ret); |
| 180 | puts(" Can't get memory map\n"); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int efi_call_exit_boot_services(void) |
| 188 | { |
| 189 | struct efi_priv *priv = efi_get_priv(); |
| 190 | const struct efi_boot_services *boot = priv->boot; |
| 191 | efi_uintn_t size; |
| 192 | u32 version; |
| 193 | efi_status_t ret; |
| 194 | |
| 195 | size = priv->memmap_alloc; |
| 196 | ret = boot->get_memory_map(&size, priv->memmap_desc, |
| 197 | &priv->memmap_key, |
| 198 | &priv->memmap_desc_size, &version); |
| 199 | if (ret) { |
| 200 | printhex2(ret); |
| 201 | puts(" Can't get memory map\n"); |
| 202 | return ret; |
| 203 | } |
| 204 | ret = boot->exit_boot_services(priv->parent_image, priv->memmap_key); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
| 208 | return 0; |
| 209 | } |