blob: 69e52e457486af29ce04387c36fefe7c1d341aff [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/*
3 * Copyright (c) 2015 Google, Inc
4 *
Simon Glass9539e692015-07-31 09:31:36 -06005 * EFI information obtained here:
6 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
7 *
8 * Common EFI functions
9 */
10
11#include <common.h>
12#include <debug_uart.h>
13#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass9539e692015-07-31 09:31:36 -060015#include <linux/err.h>
16#include <linux/types.h>
17#include <efi.h>
18#include <efi_api.h>
19
Simon Glass9539e692015-07-31 09:31:36 -060020/*
Simon Glass86ed9c52021-11-03 21:09:08 -060021 * Global declaration of gd.
22 *
23 * As we write to it before relocation we have to make sure it is not put into
24 * a .bss section which may overlap a .rela section. Initialization forces it
25 * into a .data section which cannot overlap any .rela section.
26 */
27struct global_data *global_data_ptr = (struct global_data *)~0;
28
29/*
Simon Glass9539e692015-07-31 09:31:36 -060030 * Unfortunately we cannot access any code outside what is built especially
31 * for the stub. lib/string.c is already being built for the U-Boot payload
32 * so it uses the wrong compiler flags. Add our own memset() here.
33 */
34static void efi_memset(void *ptr, int ch, int size)
35{
36 char *dest = ptr;
37
38 while (size-- > 0)
39 *dest++ = ch;
40}
41
42/*
43 * Since the EFI stub cannot access most of the U-Boot code, add our own
44 * simple console output functions here. The EFI app will not use these since
45 * it can use the normal console.
46 */
47void efi_putc(struct efi_priv *priv, const char ch)
48{
49 struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
50 uint16_t ucode[2];
51
52 ucode[0] = ch;
53 ucode[1] = '\0';
54 con->output_string(con, ucode);
55}
56
57void efi_puts(struct efi_priv *priv, const char *str)
58{
59 while (*str)
60 efi_putc(priv, *str++);
61}
62
63int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
64 struct efi_system_table *sys_table)
65{
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020066 efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
Simon Glass9539e692015-07-31 09:31:36 -060067 struct efi_boot_services *boot = sys_table->boottime;
68 struct efi_loaded_image *loaded_image;
69 int ret;
70
71 efi_memset(priv, '\0', sizeof(*priv));
72 priv->sys_table = sys_table;
73 priv->boot = sys_table->boottime;
74 priv->parent_image = image;
75 priv->run = sys_table->runtime;
76
77 efi_puts(priv, "U-Boot EFI ");
78 efi_puts(priv, banner);
79 efi_putc(priv, ' ');
80
81 ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +020082 (void **)&loaded_image, priv->parent_image,
Simon Glass9539e692015-07-31 09:31:36 -060083 NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
84 if (ret) {
85 efi_puts(priv, "Failed to get loaded image protocol\n");
86 return ret;
87 }
88 priv->image_data_type = loaded_image->image_data_type;
89
90 return 0;
91}
92
93void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
94{
95 struct efi_boot_services *boot = priv->boot;
96 void *buf = NULL;
97
98 *retp = boot->allocate_pool(priv->image_data_type, size, &buf);
99
100 return buf;
101}
102
103void efi_free(struct efi_priv *priv, void *ptr)
104{
105 struct efi_boot_services *boot = priv->boot;
106
107 boot->free_pool(ptr);
108}