From 11f6af40dc322630031511146763cc9059bdb805 Mon Sep 17 00:00:00 2001
From: Emekcan Aras <emekcan.aras@arm.com>
Date: Fri, 14 Apr 2023 16:35:55 +0100
Subject: [PATCH] Platform: corstone1000: Adds compiler flags to FWU agent for
 BL1

Adds compiler flags for BL1 to fwu_agent.c functions to not use GPT parser and
IO libraries in BL1 rom code.

Upstream-Status: Submitted [https://review.trustedfirmware.org/c/TF-M/trusted-firmware-m/+/20559]
Signed-off-by: Emekcan Aras <emekcan.aras@arm.com>
---
 .../corstone1000/fw_update_agent/fwu_agent.c  | 176 +++++++++++++++++-
 1 file changed, 174 insertions(+), 2 deletions(-)

diff --git a/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c b/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c
index 8ecb03d157..afd8d66e42 100644
--- a/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c
+++ b/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c
@@ -14,8 +14,6 @@
 #include "region_defs.h"
 #include "uefi_capsule_parser.h"
 #include "flash_common.h"
-#include "partition.h"
-#include "platform.h"
 #include "platform_base_address.h"
 #include "platform_description.h"
 #include "tfm_plat_nv_counters.h"
@@ -23,6 +21,10 @@
 #include "uefi_fmp.h"
 #include "uart_stdout.h"
 #include "soft_crc.h"
+#if !BL1
+#include "partition.h"
+#include "platform.h"
+#endif
 
 /* Properties of image in a bank */
 struct fwu_image_properties {
@@ -145,6 +147,30 @@ extern ARM_DRIVER_FLASH FWU_METADATA_FLASH_DEV;
 
 #define HOST_ACK_TIMEOUT_SEC    (6 * 60) /* ~seconds, not exact */
 
+#if BL1
+static enum fwu_agent_error_t private_metadata_read(
+        struct fwu_private_metadata* p_metadata)
+{
+    int ret;
+
+    FWU_LOG_MSG("%s: enter\n\r", __func__);
+
+    if (!p_metadata) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET, p_metadata,
+                                          sizeof(struct fwu_private_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: success: boot_index = %u\n\r", __func__,
+                        p_metadata->boot_index);
+
+    return FWU_AGENT_SUCCESS;
+}
+#elif
 static enum fwu_agent_error_t private_metadata_read(
         struct fwu_private_metadata* p_metadata)
 {
@@ -175,7 +201,36 @@ static enum fwu_agent_error_t private_metadata_read(
 
     return FWU_AGENT_SUCCESS;
 }
+#endif
 
+#if BL1
+static enum fwu_agent_error_t private_metadata_write(
+        struct fwu_private_metadata* p_metadata)
+{
+    int ret;
+
+    FWU_LOG_MSG("%s: enter: boot_index = %u\n\r", __func__,
+                        p_metadata->boot_index);
+
+    if (!p_metadata) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET);
+    if (ret != ARM_DRIVER_OK) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET,
+                                p_metadata, sizeof(struct fwu_private_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: success\n\r", __func__);
+    return FWU_AGENT_SUCCESS;
+}
+#elif
 static enum fwu_agent_error_t private_metadata_write(
         struct fwu_private_metadata* p_metadata)
 {
@@ -210,6 +265,7 @@ static enum fwu_agent_error_t private_metadata_write(
     FWU_LOG_MSG("%s: success\n\r", __func__);
     return FWU_AGENT_SUCCESS;
 }
+#endif
 
 static enum fwu_agent_error_t metadata_validate(struct fwu_metadata *p_metadata)
 {
@@ -235,6 +291,30 @@ static enum fwu_agent_error_t metadata_validate(struct fwu_metadata *p_metadata)
     return FWU_AGENT_SUCCESS;
 }
 
+#if BL1
+static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metadata *p_metadata)
+{
+    int ret;
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
+
+    if (!p_metadata) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_METADATA_REPLICA_1_OFFSET,
+                                p_metadata, sizeof(struct fwu_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: success: active = %u, previous = %d\n\r", __func__,
+                  p_metadata->active_index, p_metadata->previous_active_index);
+
+    return FWU_AGENT_SUCCESS;
+}
+#elif
 static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metadata *p_metadata)
 {
     uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
@@ -266,7 +346,36 @@ static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metada
 
     return FWU_AGENT_SUCCESS;
 }
+#endif
+
+#if BL1
+static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
+{
+    int ret;
 
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
+
+    if (!p_metadata) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_METADATA_REPLICA_1_OFFSET,
+                                p_metadata, sizeof(struct fwu_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    if (metadata_validate(p_metadata) != FWU_AGENT_SUCCESS) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: success: active = %u, previous = %d\n\r", __func__,
+                  p_metadata->active_index, p_metadata->previous_active_index);
+
+    return FWU_AGENT_SUCCESS;
+}
+#elif
 static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
 {
     uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
@@ -301,7 +410,66 @@ static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
 
     return FWU_AGENT_SUCCESS;
 }
+#endif
+
 
+#if BL1
+static enum fwu_agent_error_t metadata_write(
+                        struct fwu_metadata *p_metadata)
+{
+    int ret;
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
+
+    if (!p_metadata) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_1_OFFSET);
+    if (ret != ARM_DRIVER_OK) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_1_OFFSET,
+                                p_metadata, sizeof(struct fwu_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  FWU_METADATA_REPLICA_2_OFFSET, sizeof(struct fwu_metadata));
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_2_OFFSET);
+    if (ret != ARM_DRIVER_OK) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_2_OFFSET,
+                                p_metadata, sizeof(struct fwu_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  FWU_METADATA_REPLICA_2_OFFSET, sizeof(struct fwu_metadata));
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_2_OFFSET);
+    if (ret != ARM_DRIVER_OK) {
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_2_OFFSET,
+                                p_metadata, sizeof(struct fwu_metadata));
+    if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: success: active = %u, previous = %d\n\r", __func__,
+                  p_metadata->active_index, p_metadata->previous_active_index);
+    return FWU_AGENT_SUCCESS;
+}
+#elif
 static enum fwu_agent_error_t metadata_write(
                         struct fwu_metadata *p_metadata)
 {
@@ -371,6 +539,8 @@ static enum fwu_agent_error_t metadata_write(
                   p_metadata->active_index, p_metadata->previous_active_index);
     return FWU_AGENT_SUCCESS;
 }
+#endif
+
 
 enum fwu_agent_error_t fwu_metadata_init(void)
 {
@@ -418,8 +588,10 @@ enum fwu_agent_error_t fwu_metadata_provision(void)
 
     FWU_LOG_MSG("%s: enter\n\r", __func__);
 
+#if !BL1
     plat_io_storage_init();
     partition_init(PLATFORM_GPT_IMAGE);
+#endif
 
     ret = fwu_metadata_init();
     if (ret) {
-- 
2.17.1

