From 27d3ce79128478cd163a2db113326c873bda8d08 Mon Sep 17 00:00:00 2001
From: Harsimran Singh Tungal <harsimransingh.tungal@arm.com>
Date: Fri, 28 Feb 2025 21:12:56 +0000
Subject: [PATCH 6/8] platform: corstone1000: Enable ESRT support

Enable ESRT support for Corstone-1000.
Introduce ESRT image UUID and its component number and
set TFM_FWU_MAX_DIGEST_SIZE to ESRT data size.

Upstream-Status: Pending
Signed-off-by: Harsimran Singh Tungal <harsimransingh.tungal@arm.com>
---
 .../infra/corstone1000/corstone1000_config.h  |  3 +--
 .../corstone1000/service_proxy_factory.c      | 22 +++++++++++++++++--
 .../providers/arm/corstone1000/platform.cmake |  9 ++++++++
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/deployments/se-proxy/infra/corstone1000/corstone1000_config.h b/deployments/se-proxy/infra/corstone1000/corstone1000_config.h
index 319401f3..4a68c2fa 100644
--- a/deployments/se-proxy/infra/corstone1000/corstone1000_config.h
+++ b/deployments/se-proxy/infra/corstone1000/corstone1000_config.h
@@ -8,8 +8,6 @@
 #ifndef CORSTONE1000_CONFIG_H
 #define CORSTONE1000_CONFIG_H
 
-#define FWU_IMAGE_COUNT 1
-
 /* Maximum payload size to be transferred at once to Secure Enclave */
 #define MAX_PAYLOAD_SIZE 4096
 
@@ -23,6 +21,7 @@
 enum fwu_image_index
 {
 	FWU_IMAGE_INDEX_FULL_CAPSULE = 1,
+	FWU_IMAGE_INDEX_ESRT,
 };
 
 #endif /* CORSTONE1000_CONFIG_H */
diff --git a/deployments/se-proxy/infra/corstone1000/service_proxy_factory.c b/deployments/se-proxy/infra/corstone1000/service_proxy_factory.c
index 6e5f1221..f0a4853e 100644
--- a/deployments/se-proxy/infra/corstone1000/service_proxy_factory.c
+++ b/deployments/se-proxy/infra/corstone1000/service_proxy_factory.c
@@ -18,6 +18,7 @@
 #include "service/fwu/psa_fwu_m/interface/psa_ipc/psa_fwu_ipc.h"
 #include <service/secure_storage/frontend/secure_storage_provider/secure_storage_provider.h>
 #include "service/secure_storage/frontend/secure_storage_provider/secure_storage_uuid.h"
+#include <protocols/service/fwu/fwu_proto.h>
 #include <trace.h>
 #include "corstone1000_config.h"
 
@@ -26,9 +27,17 @@
 #include <service/secure_storage/backend/secure_storage_ipc/secure_storage_ipc.h>
 #include <service/attestation/client/psa/iat_client.h>
 
+/* IMAGE_MAPPING_ELEMENT_COUNT includes the number of images to be updated and ESRT image */
+#define IMAGE_MAPPING_ELEMENT_COUNT (FWU_IMAGE_CAPSULE_COUNT + 1)
+
+/* The index to access the ESRT image in the psa_fwu_m_image_mapping structure
+ * collection. The ESRT image is always accessed at the end of the collection.
+ */
+#define IMAGE_MAPPING_ESRT_INDEX (IMAGE_MAPPING_ELEMENT_COUNT - 1)
+
 static const struct rpc_uuid dummy_uuid = { 0 };
 
-static const struct psa_fwu_m_image_mapping img_mapping[FWU_IMAGE_COUNT] = {
+static struct psa_fwu_m_image_mapping img_mapping[IMAGE_MAPPING_ELEMENT_COUNT] = {
 #if PLATFORM_IS_FVP
 	{
 		.uuid = CORSTONE1000_FVP_FULL_CAPSULE_UUID,
@@ -42,6 +51,13 @@ static const struct psa_fwu_m_image_mapping img_mapping[FWU_IMAGE_COUNT] = {
 #endif
 };
 
+/* Every platform needs to define esrt image mapping, if ESRT image UUID is to be used to extract ESRT data */
+static void define_esrt_image_mapping()
+{
+	uuid_octets_from_canonical(&img_mapping[IMAGE_MAPPING_ESRT_INDEX].uuid, EFI_SYSTEM_RESOURCE_TABLE_CANONICAL_UUID);
+	img_mapping[IMAGE_MAPPING_ESRT_INDEX].component = FWU_IMAGE_INDEX_ESRT;
+}
+
 struct rpc_service_interface *attest_proxy_create(void)
 {
 	struct rpc_service_interface *attest_iface = NULL;
@@ -169,7 +185,9 @@ struct rpc_service_interface *fwu_proxy_create(void)
 
 	if (psa_fwu_ipc_init(&rpc_session) != PSA_SUCCESS)
 		return NULL;
-	agent = psa_fwu_m_update_agent_init(img_mapping, FWU_IMAGE_COUNT, MAX_PAYLOAD_SIZE);
+
+	define_esrt_image_mapping();
+	agent = psa_fwu_m_update_agent_init(img_mapping, IMAGE_MAPPING_ELEMENT_COUNT, MAX_PAYLOAD_SIZE);
 
 	return fwu_provider_init(&fwu_provider, agent);
 }
diff --git a/platform/providers/arm/corstone1000/platform.cmake b/platform/providers/arm/corstone1000/platform.cmake
index 60bc208b..db1e9743 100644
--- a/platform/providers/arm/corstone1000/platform.cmake
+++ b/platform/providers/arm/corstone1000/platform.cmake
@@ -6,11 +6,18 @@
 # Platform definition for the Corstone-1000 platform.
 #-------------------------------------------------------------------------------
 
+# For ESRT v1 details : https://uefi.org/specs/UEFI/2.9_A/23_Firmware_Update_and_Reporting.html#efi-system-resource-table
+set(FWU_IMAGE_CAPSULE_COUNT 4 CACHE STRING "Maximum number of FWU Images in a capsule to be updated")
+set(ESRT_IMAGE_ENTRY_SIZE 40 CACHE STRING "Size of one ESRT v1 Image entry structure object")
+set(ESRT_REMAINING_FIELDS_SIZE 16 CACHE STRING "Size of remaining fields of ESRT v1 table structure")
+math(EXPR TOTAL_ESRT_SIZE "${FWU_IMAGE_CAPSULE_COUNT} * ${ESRT_IMAGE_ENTRY_SIZE} + ${ESRT_REMAINING_FIELDS_SIZE}" OUTPUT_FORMAT DECIMAL)
+
 set(SMM_GATEWAY_MAX_UEFI_VARIABLES 80 CACHE STRING "Maximum UEFI variable count")
 set(SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE 4*4096 CACHE STRING "RPC caller buffer size in SMMGW")
 set(SMM_SP_HEAP_SIZE 80*1024 CACHE STRING "SMM gateway SP heap size")
 set(PLAT_RSE_COMMS_PAYLOAD_MAX_SIZE 0x43C0 CACHE STRING "Size of the RSE_COMMS_PAYLOAD buffer")
 set(COMMS_MHU_MSG_SIZE 0x4500 CACHE STRING "Max message size that can be transfered via MHU")
+set(TFM_FWU_MAX_DIGEST_SIZE ${TOTAL_ESRT_SIZE} CACHE STRING "Maximum size of ESRT entries of all the images in a bank")
 
 target_compile_definitions(${TGT} PRIVATE
 	SMM_VARIABLE_INDEX_STORAGE_UID=0x787
@@ -18,6 +25,8 @@ target_compile_definitions(${TGT} PRIVATE
  	COMMS_MHU_MSG_SIZE=${COMMS_MHU_MSG_SIZE}
 	MBEDTLS_ECP_DP_SECP521R1_ENABLED
 	PLATFORM_IS_FVP=${PLATFORM_IS_FVP}
+	TFM_FWU_MAX_DIGEST_SIZE=${TFM_FWU_MAX_DIGEST_SIZE}
+	FWU_IMAGE_CAPSULE_COUNT=${FWU_IMAGE_CAPSULE_COUNT}
 )
 
 get_property(_platform_driver_dependencies TARGET ${TGT}
-- 
2.25.1

