]> Git Repo - u-boot.git/blob - lib/efi/efi.c
Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh
[u-boot.git] / lib / efi / efi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Functions shared by the app and stub
4  *
5  * Copyright (c) 2015 Google, Inc
6  *
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 <debug_uart.h>
14 #include <errno.h>
15 #include <malloc.h>
16 #include <linux/err.h>
17 #include <linux/types.h>
18 #include <efi.h>
19 #include <efi_api.h>
20
21 static struct efi_priv *global_priv;
22
23 struct efi_priv *efi_get_priv(void)
24 {
25         return global_priv;
26 }
27
28 void efi_set_priv(struct efi_priv *priv)
29 {
30         global_priv = priv;
31 }
32
33 struct efi_system_table *efi_get_sys_table(void)
34 {
35         return global_priv->sys_table;
36 }
37
38 struct efi_boot_services *efi_get_boot(void)
39 {
40         return global_priv->boot;
41 }
42
43 unsigned long efi_get_ram_base(void)
44 {
45         return global_priv->ram_base;
46 }
47
48 /*
49  * Global declaration of gd.
50  *
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.
54  */
55 struct global_data *global_data_ptr = (struct global_data *)~0;
56
57 /*
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.
61  */
62 static void efi_memset(void *ptr, int ch, int size)
63 {
64         char *dest = ptr;
65
66         while (size-- > 0)
67                 *dest++ = ch;
68 }
69
70 /*
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.
74  */
75 void efi_putc(struct efi_priv *priv, const char ch)
76 {
77         struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
78         uint16_t ucode[2];
79
80         ucode[0] = ch;
81         ucode[1] = '\0';
82         con->output_string(con, ucode);
83 }
84
85 void efi_puts(struct efi_priv *priv, const char *str)
86 {
87         while (*str)
88                 efi_putc(priv, *str++);
89 }
90
91 int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
92              struct efi_system_table *sys_table)
93 {
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;
97         int ret;
98
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;
104
105         efi_puts(priv, "U-Boot EFI ");
106         efi_puts(priv, banner);
107         efi_putc(priv, ' ');
108
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);
112         if (ret) {
113                 efi_puts(priv, "Failed to get loaded image protocol\n");
114                 return ret;
115         }
116         priv->image_data_type = loaded_image->image_data_type;
117
118         return 0;
119 }
120
121 void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
122 {
123         struct efi_boot_services *boot = priv->boot;
124         void *buf = NULL;
125
126         *retp = boot->allocate_pool(priv->image_data_type, size, &buf);
127
128         return buf;
129 }
130
131 void efi_free(struct efi_priv *priv, void *ptr)
132 {
133         struct efi_boot_services *boot = priv->boot;
134
135         boot->free_pool(ptr);
136 }
137
138 int efi_store_memory_map(struct efi_priv *priv)
139 {
140         struct efi_boot_services *boot = priv->sys_table->boottime;
141         efi_uintn_t size, desc_size;
142         efi_status_t ret;
143
144         /* Get the memory map so we can switch off EFI */
145         size = 0;
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) {
150                 /*
151                  * Note this function avoids using printf() since it is not
152                  * available in the stub
153                  */
154                 printhex2(EFI_BITS_PER_LONG);
155                 putc(' ');
156                 printhex2(ret);
157                 puts(" No memory map\n");
158                 return ret;
159         }
160         /*
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
164          */
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) {
169                 printhex2(ret);
170                 puts(" No memory for memory descriptor\n");
171                 return ret;
172         }
173
174         ret = boot->get_memory_map(&priv->memmap_size, priv->memmap_desc,
175                                    &priv->memmap_key, &desc_size,
176                                    &priv->memmap_version);
177         if (ret) {
178                 printhex2(ret);
179                 puts(" Can't get memory map\n");
180                 return ret;
181         }
182
183         return 0;
184 }
185
186 int efi_call_exit_boot_services(void)
187 {
188         struct efi_priv *priv = efi_get_priv();
189         const struct efi_boot_services *boot = priv->boot;
190         efi_uintn_t size;
191         u32 version;
192         efi_status_t ret;
193
194         size = priv->memmap_alloc;
195         ret = boot->get_memory_map(&size, priv->memmap_desc,
196                                    &priv->memmap_key,
197                                    &priv->memmap_desc_size, &version);
198         if (ret) {
199                 printhex2(ret);
200                 puts(" Can't get memory map\n");
201                 return ret;
202         }
203         ret = boot->exit_boot_services(priv->parent_image, priv->memmap_key);
204         if (ret)
205                 return ret;
206
207         return 0;
208 }
This page took 0.037873 seconds and 4 git commands to generate.