1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
5 * EFI information obtained here:
6 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
8 * This file implements U-Boot running as an EFI application.
13 #include <debug_uart.h>
17 #include <linux/err.h>
18 #include <linux/types.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static struct efi_priv *global_priv;
27 struct efi_system_table *efi_get_sys_table(void)
29 return global_priv->sys_table;
32 unsigned long efi_get_ram_base(void)
34 return global_priv->ram_base;
37 static efi_status_t setup_memory(struct efi_priv *priv)
39 struct efi_boot_services *boot = priv->boot;
40 efi_physical_addr_t addr;
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.
49 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
52 memset(gd, '\0', sizeof(*gd));
54 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
58 pages = CONFIG_EFI_RAM_SIZE >> 12;
61 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
62 * so we want it to load below 4GB.
65 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
66 priv->image_data_type, pages, &addr);
68 printf("(using pool %lx) ", ret);
69 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
73 priv->use_pool_for_malloc = true;
75 priv->ram_base = addr;
77 gd->ram_size = pages << 12;
82 static void free_memory(struct efi_priv *priv)
84 struct efi_boot_services *boot = priv->boot;
86 if (priv->use_pool_for_malloc)
87 efi_free(priv, (void *)priv->ram_base);
89 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
91 efi_free(priv, (void *)gd->malloc_base);
93 global_data_ptr = NULL;
97 * efi_main() - Start an EFI image
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().
103 efi_status_t EFIAPI efi_main(efi_handle_t image,
104 struct efi_system_table *sys_table)
106 struct efi_priv local_priv, *priv = &local_priv;
109 /* Set up access to EFI data structures */
110 efi_init(priv, "App", image, sys_table);
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.
121 ret = setup_memory(priv);
123 printf("Failed to set up memory: ret=%lx\n", ret);
127 printf("starting\n");
129 board_init_f(GD_FLG_SKIP_RELOC);
130 board_init_r(NULL, 0);
136 static void efi_exit(void)
138 struct efi_priv *priv = global_priv;
141 printf("U-Boot EFI exiting\n");
142 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
145 static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
152 static const struct udevice_id efi_sysreset_ids[] = {
153 { .compatible = "efi,reset" },
157 static struct sysreset_ops efi_sysreset_ops = {
158 .request = efi_sysreset_request,
161 U_BOOT_DRIVER(efi_sysreset) = {
162 .name = "efi-sysreset",
163 .id = UCLASS_SYSRESET,
164 .of_match = efi_sysreset_ids,
165 .ops = &efi_sysreset_ops,