2 * Copyright (c) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
6 * EFI information obtained here:
7 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
9 * This file implements U-Boot running as an EFI application.
13 #include <debug_uart.h>
15 #include <linux/err.h>
16 #include <linux/types.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 static struct efi_priv *global_priv;
24 struct efi_system_table *efi_get_sys_table(void)
26 return global_priv->sys_table;
29 unsigned long efi_get_ram_base(void)
31 return global_priv->ram_base;
34 static efi_status_t setup_memory(struct efi_priv *priv)
36 struct efi_boot_services *boot = priv->boot;
37 efi_physical_addr_t addr;
42 * Use global_data_ptr instead of gd since it is an assignment. There
43 * are very few assignments to global_data in U-Boot and this makes
44 * it easier to find them.
46 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
49 memset(gd, '\0', sizeof(*gd));
51 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_SYS_MALLOC_F_LEN,
55 pages = CONFIG_EFI_RAM_SIZE >> 12;
58 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
59 * so we want it to load below 4GB.
62 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
63 priv->image_data_type, pages, &addr);
65 printf("(using pool %lx) ", ret);
66 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
70 priv->use_pool_for_malloc = true;
72 priv->ram_base = addr;
74 gd->ram_size = pages << 12;
79 static void free_memory(struct efi_priv *priv)
81 struct efi_boot_services *boot = priv->boot;
83 if (priv->use_pool_for_malloc)
84 efi_free(priv, (void *)priv->ram_base);
86 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
88 efi_free(priv, (void *)gd->malloc_base);
90 global_data_ptr = NULL;
94 * efi_main() - Start an EFI image
96 * This function is called by our EFI start-up code. It handles running
97 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
100 efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table)
102 struct efi_priv local_priv, *priv = &local_priv;
105 /* Set up access to EFI data structures */
106 efi_init(priv, "App", image, sys_table);
111 * Set up the EFI debug UART so that printf() works. This is
112 * implemented in the EFI serial driver, serial_efi.c. The application
113 * can use printf() freely.
117 ret = setup_memory(priv);
119 printf("Failed to set up memory: ret=%lx\n", ret);
123 printf("starting\n");
125 board_init_f(GD_FLG_SKIP_RELOC);
126 board_init_r(NULL, 0);
132 void reset_cpu(ulong addr)
134 struct efi_priv *priv = global_priv;
137 printf("U-Boot EFI exiting\n");
138 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);