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.
12 #include <debug_uart.h>
14 #include <linux/err.h>
15 #include <linux/types.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static struct efi_priv *global_priv;
23 struct efi_system_table *efi_get_sys_table(void)
25 return global_priv->sys_table;
28 unsigned long efi_get_ram_base(void)
30 return global_priv->ram_base;
33 static efi_status_t setup_memory(struct efi_priv *priv)
35 struct efi_boot_services *boot = priv->boot;
36 efi_physical_addr_t addr;
41 * Use global_data_ptr instead of gd since it is an assignment. There
42 * are very few assignments to global_data in U-Boot and this makes
43 * it easier to find them.
45 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
48 memset(gd, '\0', sizeof(*gd));
50 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
54 pages = CONFIG_EFI_RAM_SIZE >> 12;
57 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
58 * so we want it to load below 4GB.
61 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
62 priv->image_data_type, pages, &addr);
64 printf("(using pool %lx) ", ret);
65 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
69 priv->use_pool_for_malloc = true;
71 priv->ram_base = addr;
73 gd->ram_size = pages << 12;
78 static void free_memory(struct efi_priv *priv)
80 struct efi_boot_services *boot = priv->boot;
82 if (priv->use_pool_for_malloc)
83 efi_free(priv, (void *)priv->ram_base);
85 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
87 efi_free(priv, (void *)gd->malloc_base);
89 global_data_ptr = NULL;
93 * efi_main() - Start an EFI image
95 * This function is called by our EFI start-up code. It handles running
96 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
99 efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table)
101 struct efi_priv local_priv, *priv = &local_priv;
104 /* Set up access to EFI data structures */
105 efi_init(priv, "App", image, sys_table);
110 * Set up the EFI debug UART so that printf() works. This is
111 * implemented in the EFI serial driver, serial_efi.c. The application
112 * can use printf() freely.
116 ret = setup_memory(priv);
118 printf("Failed to set up memory: ret=%lx\n", ret);
122 printf("starting\n");
124 board_init_f(GD_FLG_SKIP_RELOC);
125 board_init_r(NULL, 0);
131 void reset_cpu(ulong addr)
133 struct efi_priv *priv = global_priv;
136 printf("U-Boot EFI exiting\n");
137 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);