blob: 7c64077d42e8b1188859d3fc6ce625795a34260a [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 * This file implements U-Boot running as an EFI application.
9 */
10
11#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -070012#include <cpu_func.h>
Simon Glass9539e692015-07-31 09:31:36 -060013#include <debug_uart.h>
Bin Meng268bc9e2018-07-19 03:07:29 -070014#include <dm.h>
Simon Glass9539e692015-07-31 09:31:36 -060015#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>
Bin Meng268bc9e2018-07-19 03:07:29 -070021#include <sysreset.h>
Simon Glass9539e692015-07-31 09:31:36 -060022
23DECLARE_GLOBAL_DATA_PTR;
24
25static struct efi_priv *global_priv;
26
27struct efi_system_table *efi_get_sys_table(void)
28{
29 return global_priv->sys_table;
30}
31
32unsigned long efi_get_ram_base(void)
33{
34 return global_priv->ram_base;
35}
36
37static efi_status_t setup_memory(struct efi_priv *priv)
38{
39 struct efi_boot_services *boot = priv->boot;
40 efi_physical_addr_t addr;
41 efi_status_t ret;
42 int pages;
43
44 /*
45 * Use global_data_ptr instead of gd since it is an assignment. There
46 * are very few assignments to global_data in U-Boot and this makes
47 * it easier to find them.
48 */
49 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
50 if (!global_data_ptr)
51 return ret;
52 memset(gd, '\0', sizeof(*gd));
53
Andy Yan1fa20e4d2017-07-24 17:43:34 +080054 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
Simon Glass9539e692015-07-31 09:31:36 -060055 &ret);
56 if (!gd->malloc_base)
57 return ret;
58 pages = CONFIG_EFI_RAM_SIZE >> 12;
59
60 /*
61 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
62 * so we want it to load below 4GB.
63 */
64 addr = 1ULL << 32;
65 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
66 priv->image_data_type, pages, &addr);
67 if (ret) {
68 printf("(using pool %lx) ", ret);
69 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
70 &ret);
71 if (!priv->ram_base)
72 return ret;
73 priv->use_pool_for_malloc = true;
74 } else {
75 priv->ram_base = addr;
76 }
77 gd->ram_size = pages << 12;
78
79 return 0;
80}
81
82static void free_memory(struct efi_priv *priv)
83{
84 struct efi_boot_services *boot = priv->boot;
85
86 if (priv->use_pool_for_malloc)
87 efi_free(priv, (void *)priv->ram_base);
88 else
89 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
90
91 efi_free(priv, (void *)gd->malloc_base);
92 efi_free(priv, gd);
93 global_data_ptr = NULL;
94}
95
96/**
97 * efi_main() - Start an EFI image
98 *
99 * This function is called by our EFI start-up code. It handles running
100 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
101 * is via reset_cpu().
102 */
Ivan Gorinov41236622018-06-13 17:27:39 -0700103efi_status_t EFIAPI efi_main(efi_handle_t image,
104 struct efi_system_table *sys_table)
Simon Glass9539e692015-07-31 09:31:36 -0600105{
106 struct efi_priv local_priv, *priv = &local_priv;
107 efi_status_t ret;
108
109 /* Set up access to EFI data structures */
110 efi_init(priv, "App", image, sys_table);
111
112 global_priv = priv;
113
114 /*
115 * Set up the EFI debug UART so that printf() works. This is
116 * implemented in the EFI serial driver, serial_efi.c. The application
117 * can use printf() freely.
118 */
119 debug_uart_init();
120
121 ret = setup_memory(priv);
122 if (ret) {
123 printf("Failed to set up memory: ret=%lx\n", ret);
124 return ret;
125 }
126
127 printf("starting\n");
128
129 board_init_f(GD_FLG_SKIP_RELOC);
130 board_init_r(NULL, 0);
131 free_memory(priv);
132
133 return EFI_SUCCESS;
134}
135
Bin Meng268bc9e2018-07-19 03:07:29 -0700136static void efi_exit(void)
Simon Glass9539e692015-07-31 09:31:36 -0600137{
138 struct efi_priv *priv = global_priv;
139
140 free_memory(priv);
141 printf("U-Boot EFI exiting\n");
142 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
143}
Bin Meng268bc9e2018-07-19 03:07:29 -0700144
145static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
146{
147 efi_exit();
148
149 return -EINPROGRESS;
150}
151
152static const struct udevice_id efi_sysreset_ids[] = {
153 { .compatible = "efi,reset" },
154 { }
155};
156
157static struct sysreset_ops efi_sysreset_ops = {
158 .request = efi_sysreset_request,
159};
160
161U_BOOT_DRIVER(efi_sysreset) = {
162 .name = "efi-sysreset",
163 .id = UCLASS_SYSRESET,
164 .of_match = efi_sysreset_ids,
165 .ops = &efi_sysreset_ops,
Bin Meng268bc9e2018-07-19 03:07:29 -0700166};