From 4a4d1b0a5a2455ad799a45f7f87c0c9fd0173034 Mon Sep 17 00:00:00 2001
From: Rui Miguel Silva <rui.silva@linaro.org>
Date: Wed, 29 Mar 2023 10:58:32 +0100
Subject: [PATCH] Platform: Corstone1000: get fwu and private metadata from gpt

Read and Write the FWU metadata and private metadata using instead
static flash offsets get the partitions and start address from gpt
partition table.

Upstream-Status: Submitted [https://review.trustedfirmware.org/c/TF-M/trusted-firmware-m/+/20551]
Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
---
 .../target/arm/corstone1000/CMakeLists.txt    |  7 ++
 .../corstone1000/fw_update_agent/fwu_agent.c  | 90 +++++++++++++++----
 .../target/arm/corstone1000/partition/efi.h   |  1 +
 .../arm/corstone1000/partition/partition.c    | 14 +++
 .../arm/corstone1000/partition/partition.h    |  1 +
 .../ext/target/arm/corstone1000/platform.h    |  5 ++
 6 files changed, 99 insertions(+), 19 deletions(-)

diff --git a/platform/ext/target/arm/corstone1000/CMakeLists.txt b/platform/ext/target/arm/corstone1000/CMakeLists.txt
index 19863bcdb6d2..f232c7639bd5 100644
--- a/platform/ext/target/arm/corstone1000/CMakeLists.txt
+++ b/platform/ext/target/arm/corstone1000/CMakeLists.txt
@@ -64,6 +64,8 @@ target_include_directories(platform_s
         cc312
         fw_update_agent
         soft_crc
+        io
+        partition
 )
 
 target_sources(platform_s
@@ -81,6 +83,11 @@ target_sources(platform_s
         fw_update_agent/fwu_agent.c
         fw_update_agent/uefi_fmp.c
         soft_crc/soft_crc.c
+        io/io_block.c
+        io/io_flash.c
+        io/io_storage.c
+        partition/partition.c
+        partition/gpt.c
         $<$<NOT:$<BOOL:${PLATFORM_DEFAULT_OTP}>>:${PLATFORM_DIR}/ext/accelerator/cc312/otp_cc312.c>
 )
 
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 b6ed656de833..9c76b25a3a38 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,6 +14,8 @@
 #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"
@@ -146,6 +148,8 @@ extern ARM_DRIVER_FLASH FWU_METADATA_FLASH_DEV;
 static enum fwu_agent_error_t private_metadata_read(
         struct fwu_private_metadata* p_metadata)
 {
+    partition_entry_t *part;
+    uuid_t private_uuid = PRIVATE_METADATA_TYPE_UUID;
     int ret;
 
     FWU_LOG_MSG("%s: enter\n\r", __func__);
@@ -154,7 +158,13 @@ static enum fwu_agent_error_t private_metadata_read(
         return FWU_AGENT_ERROR;
     }
 
-    ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET, p_metadata,
+    part = get_partition_entry_by_type(&private_uuid);
+    if (!part) {
+        FWU_LOG_MSG("Private metadata partition not found\n\r");
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(part->start, p_metadata,
                                           sizeof(struct fwu_private_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
         return FWU_AGENT_ERROR;
@@ -169,6 +179,8 @@ static enum fwu_agent_error_t private_metadata_read(
 static enum fwu_agent_error_t private_metadata_write(
         struct fwu_private_metadata* p_metadata)
 {
+    uuid_t private_uuid = PRIVATE_METADATA_TYPE_UUID;
+    partition_entry_t *part;
     int ret;
 
     FWU_LOG_MSG("%s: enter: boot_index = %u\n\r", __func__,
@@ -178,12 +190,18 @@ static enum fwu_agent_error_t private_metadata_write(
         return FWU_AGENT_ERROR;
     }
 
-    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET);
+    part = get_partition_entry_by_type(&private_uuid);
+    if (!part) {
+        FWU_LOG_MSG("Private metadata partition not found\n\r");
+        return FWU_AGENT_ERROR;
+    }
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
     if (ret != ARM_DRIVER_OK) {
         return FWU_AGENT_ERROR;
     }
 
-    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET,
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
                                 p_metadata, sizeof(struct fwu_private_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
         return FWU_AGENT_ERROR;
@@ -219,16 +237,25 @@ static enum fwu_agent_error_t metadata_validate(struct fwu_metadata *p_metadata)
 
 static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metadata *p_metadata)
 {
+    uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
+    partition_entry_t *part;
     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,
+    part = get_partition_entry_by_type(&metadata_uuid);
+    if (!part) {
+        FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  part->start, sizeof(struct fwu_metadata));
+
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(part->start,
                                 p_metadata, sizeof(struct fwu_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
         return FWU_AGENT_ERROR;
@@ -242,16 +269,24 @@ static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metada
 
 static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
 {
+    uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
+    partition_entry_t *part;
     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,
+    part = get_partition_entry_by_type(&metadata_uuid);
+    if (!part) {
+        FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  part->start, sizeof(struct fwu_metadata));
+
+    ret = FWU_METADATA_FLASH_DEV.ReadData(part->start,
                                 p_metadata, sizeof(struct fwu_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
         return FWU_AGENT_ERROR;
@@ -270,35 +305,49 @@ static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
 static enum fwu_agent_error_t metadata_write(
                         struct fwu_metadata *p_metadata)
 {
+    uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
+    partition_entry_t *part;
     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);
+    part = get_partition_entry_by_type(&metadata_uuid);
+    if (!part) {
+        FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
+        return FWU_AGENT_ERROR;
+    }
+
+    FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
+                  part->start, sizeof(struct fwu_metadata));
+
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
     if (ret != ARM_DRIVER_OK) {
         return FWU_AGENT_ERROR;
     }
 
-    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_1_OFFSET,
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
                                 p_metadata, sizeof(struct fwu_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
         return FWU_AGENT_ERROR;
     }
 
+    part = get_partition_replica_by_type(&metadata_uuid);
+    if (!part) {
+        FWU_LOG_MSG("%s: FWU metadata replica partition not found\n\r", __func__);
+        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));
+                  part->start, sizeof(struct fwu_metadata));
 
-    ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_2_OFFSET);
+    ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
     if (ret != ARM_DRIVER_OK) {
         return FWU_AGENT_ERROR;
     }
 
-    ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_2_OFFSET,
+    ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
                                 p_metadata, sizeof(struct fwu_metadata));
     if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
         return FWU_AGENT_ERROR;
@@ -355,6 +404,9 @@ enum fwu_agent_error_t fwu_metadata_provision(void)
 
     FWU_LOG_MSG("%s: enter\n\r", __func__);
 
+    plat_io_storage_init();
+    partition_init(PLATFORM_GPT_IMAGE);
+
     ret = fwu_metadata_init();
     if (ret) {
         return ret;
diff --git a/platform/ext/target/arm/corstone1000/partition/efi.h b/platform/ext/target/arm/corstone1000/partition/efi.h
index f66daffb32d6..7e6a4bc883e6 100644
--- a/platform/ext/target/arm/corstone1000/partition/efi.h
+++ b/platform/ext/target/arm/corstone1000/partition/efi.h
@@ -8,6 +8,7 @@
 #ifndef DRIVERS_PARTITION_EFI_H
 #define DRIVERS_PARTITION_EFI_H
 
+#include <stdint.h>
 #include <string.h>
 
 #include "uuid.h"
diff --git a/platform/ext/target/arm/corstone1000/partition/partition.c b/platform/ext/target/arm/corstone1000/partition/partition.c
index afc6aa1c5cb8..d76e123d728f 100644
--- a/platform/ext/target/arm/corstone1000/partition/partition.c
+++ b/platform/ext/target/arm/corstone1000/partition/partition.c
@@ -293,6 +293,20 @@ const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_uuid) {
     return NULL;
 }
 
+const partition_entry_t *get_partition_replica_by_type(const uuid_t *type_uuid) {
+    int count = 0;
+    int i;
+
+    for (i = 0; i < list.entry_count; i++) {
+        if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) {
+            if (++count == 2)
+                 return &list.list[i];
+        }
+    }
+
+    return NULL;
+}
+
 const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid) {
     int i;
 
diff --git a/platform/ext/target/arm/corstone1000/partition/partition.h b/platform/ext/target/arm/corstone1000/partition/partition.h
index 54af47aca415..450cf20a073c 100644
--- a/platform/ext/target/arm/corstone1000/partition/partition.h
+++ b/platform/ext/target/arm/corstone1000/partition/partition.h
@@ -40,6 +40,7 @@ typedef struct partition_entry_list {
 int load_partition_table(unsigned int image_id);
 const partition_entry_t *get_partition_entry(const char *name);
 const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_guid);
+const partition_entry_t *get_partition_replica_by_type(const uuid_t *type_uuid);
 const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid);
 const partition_entry_list_t *get_partition_entry_list(void);
 void partition_init(unsigned int image_id);
diff --git a/platform/ext/target/arm/corstone1000/platform.h b/platform/ext/target/arm/corstone1000/platform.h
index 894f5e309029..a88093ed4f9d 100644
--- a/platform/ext/target/arm/corstone1000/platform.h
+++ b/platform/ext/target/arm/corstone1000/platform.h
@@ -13,6 +13,11 @@ typedef enum {
     PLATFORM_IMAGE_COUNT,
 }platform_image_id_t;
 
+#define FWU_METADATA_TYPE_UUID \
+     ((uuid_t){{0xa0, 0x84, 0x7a, 0x8a}, {0x87, 0x83}, {0xf6, 0x40}, 0xab,  0x41, {0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23}})
+#define PRIVATE_METADATA_TYPE_UUID \
+     ((uuid_t){{0xc3, 0x5d, 0xb5, 0xec}, {0xb7, 0x8a}, {0x84, 0x4a}, 0xab,  0x56, {0xeb, 0x0a, 0x99, 0x74, 0xdb, 0x42}})
+
 /* Initialize io storage of the platform */
 int32_t plat_io_storage_init(void);
 
-- 
2.40.0

