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