From 5866edd29215e939eaad38956886df6c7af5ab96 Mon Sep 17 00:00:00 2001
From: Vishnu Banavath <vishnu.banavath@arm.com>
Date: Mon, 15 Aug 2022 15:46:18 +0100
Subject: [PATCH 18/27] efi_loader: send bootcomplete message to secure enclave

On corstone1000 platform, Secure Enclave will be expecting
an event from uboot when it performs capsule update. Previously,
an event is sent at exitbootservice level. This will create a problem
when user wants to interrupt at UEFI shell, hence, it is required
to send an uboot efi initialized event at efi sub-system initialization
stage.

Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
Upstream-Status: Pending [Not submitted to upstream yet]
---
 include/configs/corstone1000.h |  2 +-
 lib/efi_loader/efi_boottime.c  | 43 ----------------------------------
 lib/efi_loader/efi_firmware.c  |  2 +-
 lib/efi_loader/efi_setup.c     | 39 ++++++++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 45 deletions(-)

diff --git a/include/configs/corstone1000.h b/include/configs/corstone1000.h
index e4c7bcb96f27..be13b98d4829 100644
--- a/include/configs/corstone1000.h
+++ b/include/configs/corstone1000.h
@@ -22,7 +22,7 @@
 
 /* Notification events used with SE Proxy update service */
 #define CORSTONE1000_BUFFER_READY_EVT		(0x1)
-#define CORSTONE1000_KERNEL_STARTED_EVT		(0x2)
+#define CORSTONE1000_UBOOT_EFI_STARTED_EVT	(0x2)
 
 #define PREP_SEPROXY_SVC_ID_MASK	GENMASK(31, 16)
 #define PREP_SEPROXY_SVC_ID(x)	 (FIELD_PREP(PREP_SEPROXY_SVC_ID_MASK, (x)))
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 36a0be7ba104..fea4eb7a342e 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -30,11 +30,6 @@
 #include <arm_ffa.h>
 #endif
 
-#if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
-#include <linux/bitfield.h>
-#include <linux/bitops.h>
-#endif
-
 DECLARE_GLOBAL_DATA_PTR;
 
 /* Task priority level */
@@ -2108,35 +2103,6 @@ static void efi_exit_caches(void)
 #endif
 }
 
-#if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
-/**
- * efi_corstone1000_kernel_started_event - notifies SE Proxy FW update service
- *
- * This function notifies the SE Proxy update service that the kernel has already started
- *
- * Return:
- *
- * 0: on success, otherwise failure
- */
-static int efi_corstone1000_kernel_started_event(void)
-{
-	struct ffa_send_direct_data msg = {0};
-
-	log_debug("[%s]\n", __func__);
-
-	/*
-	 * setting the kernel started  event arguments:
-	 * setting capsule update interface ID(31:16)
-	 * the kernel started event ID(15:0)
-	 */
-	msg.data1 = PREP_SEPROXY_SVC_ID(CORSTONE1000_SEPROXY_UPDATE_SVC_ID) |
-		PREP_SEPROXY_EVT(CORSTONE1000_KERNEL_STARTED_EVT); /* w4 */
-
-	return ffa_bus_ops_get()->sync_send_receive(CORSTONE1000_SEPROXY_PART_ID, &msg, 0);
-}
-
-#endif
-
 /**
  * efi_exit_boot_services() - stop all boot services
  * @image_handle: handle of the loaded image
@@ -2243,15 +2209,6 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 	/* Recalculate CRC32 */
 	efi_update_table_header_crc32(&systab.hdr);
 
-#if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
-	/* Notifying SE Proxy FW update service */
-	ffa_ret = efi_corstone1000_kernel_started_event();
-	if (ffa_ret)
-		debug("[efi_boottime][ERROR]: Failure to notify SE Proxy FW update service\n");
-	else
-		debug("[efi_boottime][INFO]: SE Proxy FW update service notified\n");
-#endif
-
 	/* Give the payload some time to boot */
 	efi_set_watchdog(0);
 	schedule();
diff --git a/lib/efi_loader/efi_firmware.c b/lib/efi_loader/efi_firmware.c
index 0a38a96351ff..70568f22909e 100644
--- a/lib/efi_loader/efi_firmware.c
+++ b/lib/efi_loader/efi_firmware.c
@@ -48,7 +48,7 @@ static efi_status_t efi_corstone1000_img_info_get (
 		*package_version_name = NULL; /* not supported */
 
 	if(image_info == NULL) {
-		log_warning("image_info is null\n");
+		log_info("image_info is null\n");
 		return EFI_BUFFER_TOO_SMALL;
 	}
 
diff --git a/lib/efi_loader/efi_setup.c b/lib/efi_loader/efi_setup.c
index 1ad2fa52d7e9..89f988b09c44 100644
--- a/lib/efi_loader/efi_setup.c
+++ b/lib/efi_loader/efi_setup.c
@@ -17,6 +17,9 @@
 efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
 
 #if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <arm_ffa.h>
 /**
  * efi_corstone1000_alloc_capsule_shared_buf - allocate capsule shared buffer
  */
@@ -126,6 +129,34 @@ static efi_status_t efi_init_secure_boot(void)
 }
 #endif /* CONFIG_EFI_SECURE_BOOT */
 
+#if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
+/**
+ * efi_corstone1000_uboot-efi_started_event - notifies SE Proxy FW update service
+ *
+ * This function notifies the SE Proxy update service that uboot efi has already started
+ *
+ * Return:
+ *
+ * 0: on success, otherwise failure
+ * */
+static int efi_corstone1000_uboot_efi_started_event(void)
+{
+	struct ffa_send_direct_data msg = {0};
+
+	log_debug("[%s]\n", __func__);
+
+	/*
+	 * setting the kernel started  event arguments:
+	 * setting capsule update interface ID(31:16)
+	 * the kernel started event ID(15:0)
+	 */
+	msg.data1 = PREP_SEPROXY_SVC_ID(CORSTONE1000_SEPROXY_UPDATE_SVC_ID) |
+			PREP_SEPROXY_EVT(CORSTONE1000_UBOOT_EFI_STARTED_EVT); /* w4 */
+
+	return ffa_bus_ops_get()->sync_send_receive(NULL, CORSTONE1000_SEPROXY_PART_ID, &msg, 0);
+}
+#endif
+
 /**
  * efi_init_capsule - initialize capsule update state
  *
@@ -136,6 +167,14 @@ static efi_status_t efi_init_capsule(void)
 	efi_status_t ret = EFI_SUCCESS;
 
 #if IS_ENABLED(CONFIG_TARGET_CORSTONE1000)
+	int ffa_ret;
+
+	ffa_ret = efi_corstone1000_uboot_efi_started_event();
+	if (ffa_ret)
+		debug("[efi_boottime][ERROR]: Failure to notify SE Proxy FW update service\n");
+	else
+		debug("[efi_boottime][INFO]: SE Proxy FW update service notified\n");
+
 	ret = efi_corstone1000_alloc_capsule_shared_buf();
 	if (ret != EFI_SUCCESS) {
 		printf("EFI: Corstone-1000: cannot allocate caspsule shared buffer\n");
-- 
2.39.1

