blob: cd6bf47b18032a2a5cc65314bf15ad817222ae5f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9539e692015-07-31 09:31:36 -06002/*
Simon Glassb13771f2021-12-29 11:57:45 -07003 * Functions shared by the app and stub
4 *
Simon Glass9539e692015-07-31 09:31:36 -06005 * Copyright (c) 2015 Google, Inc
6 *
Simon Glass9539e692015-07-31 09:31:36 -06007 * 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 Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass9539e692015-07-31 09:31:36 -060017#include <linux/err.h>
18#include <linux/types.h>
19#include <efi.h>
20#include <efi_api.h>
21
Simon Glassb13771f2021-12-29 11:57:45 -070022static struct efi_priv *global_priv;
23
24struct efi_priv *efi_get_priv(void)
25{
26 return global_priv;
27}
28
29void efi_set_priv(struct efi_priv *priv)
30{
31 global_priv = priv;
32}
33
34struct efi_system_table *efi_get_sys_table(void)
35{
36 return global_priv->sys_table;
37}
38
39struct efi_boot_services *efi_get_boot(void)
40{
41 return global_priv->boot;
42}
43
44unsigned long efi_get_ram_base(void)
45{
46 return global_priv->ram_base;
47}
48
Simon Glass9539e692015-07-31 09:31:36 -060049/*
Simon Glass86ed9c52021-11-03 21:09:08 -060050 * 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 */
56struct global_data *global_data_ptr = (struct global_data *)~0;
57
58/*
Simon Glass9539e692015-07-31 09:31:36 -060059 * 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 */
63static 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 */
76void 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
86void efi_puts(struct efi_priv *priv, const char *str)
87{
88 while (*str)
89 efi_putc(priv, *str++);
90}
91
92int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
93 struct efi_system_table *sys_table)
94{
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020095 efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
Simon Glass9539e692015-07-31 09:31:36 -060096 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 Schuchardt4f94ec52018-09-26 05:27:54 +0200111 (void **)&loaded_image, priv->parent_image,
Simon Glass9539e692015-07-31 09:31:36 -0600112 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
122void *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
132void efi_free(struct efi_priv *priv, void *ptr)
133{
134 struct efi_boot_services *boot = priv->boot;
135
136 boot->free_pool(ptr);
137}