efi: Add start-up library code

When running as an EFI application, U-Boot must request memory from EFI,
and provide access to the boot services U-Boot needs.

Add library code to perform these tasks. This includes efi_main() which is
the entry point from EFI. U-Boot is built as a shared library.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
diff --git a/lib/efi/Kconfig b/lib/efi/Kconfig
new file mode 100644
index 0000000..b23ba5b
--- /dev/null
+++ b/lib/efi/Kconfig
@@ -0,0 +1,33 @@
+config EFI
+	bool "Support running U-Boot from EFI"
+	depends on X86
+	help
+	  U-Boot can be started from EFI on certain platforms. This allows
+	  EFI to perform most of the system init and then jump to U-Boot for
+	  final system boot. Another option is to run U-Boot as an EFI
+	  application, with U-Boot using EFI's drivers instead of its own.
+
+choice
+	prompt "Select EFI mode to use"
+	depends on X86 && EFI
+
+config EFI_APP
+	bool "Support running as an EFI application"
+	help
+	  Build U-Boot as an application which can be started from EFI. This
+	  is useful for examining a platform in the early stages of porting
+	  U-Boot to it. It allows only very basic functionality, such as a
+	  command prompt and memory and I/O functions. Use 'reset' to return
+	  to EFI.
+
+config EFI_RAM_SIZE
+	hex "Amount of EFI RAM for U-Boot"
+	depends on EFI_APP
+	default 0x2000000
+	help
+	  Set the amount of EFI RAM which is claimed by U-Boot for its own
+	  use. U-Boot allocates this from EFI on start-up (along with a few
+	  other smaller amounts) and it can never be increased after that.
+	  It is used as the RAM size in with U-Boot.
+
+endchoice
diff --git a/lib/efi/Makefile b/lib/efi/Makefile
new file mode 100644
index 0000000..5ee344c
--- /dev/null
+++ b/lib/efi/Makefile
@@ -0,0 +1,7 @@
+#
+# (C) Copyright 2015 Google, Inc
+#
+# SPDX-License-Identifier:	GPL-2.0+
+#
+
+obj-$(CONFIG_EFI_APP) += efi_app.o efi.o
diff --git a/lib/efi/efi.c b/lib/efi/efi.c
new file mode 100644
index 0000000..81af27c
--- /dev/null
+++ b/lib/efi/efi.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2015 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ *
+ * EFI information obtained here:
+ * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
+ *
+ * Common EFI functions
+ */
+
+#include <common.h>
+#include <debug_uart.h>
+#include <errno.h>
+#include <linux/err.h>
+#include <linux/types.h>
+#include <efi.h>
+#include <efi_api.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/*
+ * Unfortunately we cannot access any code outside what is built especially
+ * for the stub. lib/string.c is already being built for the U-Boot payload
+ * so it uses the wrong compiler flags. Add our own memset() here.
+ */
+static void efi_memset(void *ptr, int ch, int size)
+{
+	char *dest = ptr;
+
+	while (size-- > 0)
+		*dest++ = ch;
+}
+
+/*
+ * Since the EFI stub cannot access most of the U-Boot code, add our own
+ * simple console output functions here. The EFI app will not use these since
+ * it can use the normal console.
+ */
+void efi_putc(struct efi_priv *priv, const char ch)
+{
+	struct efi_simple_text_output_protocol *con = priv->sys_table->con_out;
+	uint16_t ucode[2];
+
+	ucode[0] = ch;
+	ucode[1] = '\0';
+	con->output_string(con, ucode);
+}
+
+void efi_puts(struct efi_priv *priv, const char *str)
+{
+	while (*str)
+		efi_putc(priv, *str++);
+}
+
+int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
+	     struct efi_system_table *sys_table)
+{
+	efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID;
+	struct efi_boot_services *boot = sys_table->boottime;
+	struct efi_loaded_image *loaded_image;
+	int ret;
+
+	efi_memset(priv, '\0', sizeof(*priv));
+	priv->sys_table = sys_table;
+	priv->boot = sys_table->boottime;
+	priv->parent_image = image;
+	priv->run = sys_table->runtime;
+
+	efi_puts(priv, "U-Boot EFI ");
+	efi_puts(priv, banner);
+	efi_putc(priv, ' ');
+
+	ret = boot->open_protocol(priv->parent_image, &loaded_image_guid,
+				  (void **)&loaded_image, &priv->parent_image,
+				  NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret) {
+		efi_puts(priv, "Failed to get loaded image protocol\n");
+		return ret;
+	}
+	priv->image_data_type = loaded_image->image_data_type;
+
+	return 0;
+}
+
+void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp)
+{
+	struct efi_boot_services *boot = priv->boot;
+	void *buf = NULL;
+
+	*retp = boot->allocate_pool(priv->image_data_type, size, &buf);
+
+	return buf;
+}
+
+void efi_free(struct efi_priv *priv, void *ptr)
+{
+	struct efi_boot_services *boot = priv->boot;
+
+	boot->free_pool(ptr);
+}
diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c
new file mode 100644
index 0000000..452ab5d
--- /dev/null
+++ b/lib/efi/efi_app.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2015 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ *
+ * EFI information obtained here:
+ * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
+ *
+ * This file implements U-Boot running as an EFI application.
+ */
+
+#include <common.h>
+#include <debug_uart.h>
+#include <errno.h>
+#include <linux/err.h>
+#include <linux/types.h>
+#include <efi.h>
+#include <efi_api.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static struct efi_priv *global_priv;
+
+struct efi_system_table *efi_get_sys_table(void)
+{
+	return global_priv->sys_table;
+}
+
+unsigned long efi_get_ram_base(void)
+{
+	return global_priv->ram_base;
+}
+
+static efi_status_t setup_memory(struct efi_priv *priv)
+{
+	struct efi_boot_services *boot = priv->boot;
+	efi_physical_addr_t addr;
+	efi_status_t ret;
+	int pages;
+
+	/*
+	 * Use global_data_ptr instead of gd since it is an assignment. There
+	 * are very few assignments to global_data in U-Boot and this makes
+	 * it easier to find them.
+	 */
+	global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
+	if (!global_data_ptr)
+		return ret;
+	memset(gd, '\0', sizeof(*gd));
+
+	gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_SYS_MALLOC_F_LEN,
+					    &ret);
+	if (!gd->malloc_base)
+		return ret;
+	pages = CONFIG_EFI_RAM_SIZE >> 12;
+
+	/*
+	 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
+	 * so we want it to load below 4GB.
+	 */
+	addr = 1ULL << 32;
+	ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
+				   priv->image_data_type, pages, &addr);
+	if (ret) {
+		printf("(using pool %lx) ", ret);
+		priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
+						   &ret);
+		if (!priv->ram_base)
+			return ret;
+		priv->use_pool_for_malloc = true;
+	} else {
+		priv->ram_base = addr;
+	}
+	gd->ram_size = pages << 12;
+
+	return 0;
+}
+
+static void free_memory(struct efi_priv *priv)
+{
+	struct efi_boot_services *boot = priv->boot;
+
+	if (priv->use_pool_for_malloc)
+		efi_free(priv, (void *)priv->ram_base);
+	else
+		boot->free_pages(priv->ram_base, gd->ram_size >> 12);
+
+	efi_free(priv, (void *)gd->malloc_base);
+	efi_free(priv, gd);
+	global_data_ptr = NULL;
+}
+
+/**
+ * efi_main() - Start an EFI image
+ *
+ * This function is called by our EFI start-up code. It handles running
+ * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
+ * is via reset_cpu().
+ */
+efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table)
+{
+	struct efi_priv local_priv, *priv = &local_priv;
+	efi_status_t ret;
+
+	/* Set up access to EFI data structures */
+	efi_init(priv, "App", image, sys_table);
+
+	global_priv = priv;
+
+	/*
+	 * Set up the EFI debug UART so that printf() works. This is
+	 * implemented in the EFI serial driver, serial_efi.c. The application
+	 * can use printf() freely.
+	 */
+	debug_uart_init();
+
+	ret = setup_memory(priv);
+	if (ret) {
+		printf("Failed to set up memory: ret=%lx\n", ret);
+		return ret;
+	}
+
+	printf("starting\n");
+
+	board_init_f(GD_FLG_SKIP_RELOC);
+	board_init_r(NULL, 0);
+	free_memory(priv);
+
+	return EFI_SUCCESS;
+}
+
+void reset_cpu(ulong addr)
+{
+	struct efi_priv *priv = global_priv;
+
+	free_memory(priv);
+	printf("U-Boot EFI exiting\n");
+	priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
+}