blob: 3d7fc4b3d101569072794a4f2fdf70cae65bb665 [file] [log] [blame]
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001From 4a4d1b0a5a2455ad799a45f7f87c0c9fd0173034 Mon Sep 17 00:00:00 2001
2From: Rui Miguel Silva <rui.silva@linaro.org>
3Date: Wed, 29 Mar 2023 10:58:32 +0100
4Subject: [PATCH] Platform: Corstone1000: get fwu and private metadata from gpt
5
6Read and Write the FWU metadata and private metadata using instead
7static flash offsets get the partitions and start address from gpt
8partition table.
9
10Upstream-Status: Submitted [https://review.trustedfirmware.org/c/TF-M/trusted-firmware-m/+/20551]
11Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
12---
13 .../target/arm/corstone1000/CMakeLists.txt | 7 ++
14 .../corstone1000/fw_update_agent/fwu_agent.c | 90 +++++++++++++++----
15 .../target/arm/corstone1000/partition/efi.h | 1 +
16 .../arm/corstone1000/partition/partition.c | 14 +++
17 .../arm/corstone1000/partition/partition.h | 1 +
18 .../ext/target/arm/corstone1000/platform.h | 5 ++
19 6 files changed, 99 insertions(+), 19 deletions(-)
20
21diff --git a/platform/ext/target/arm/corstone1000/CMakeLists.txt b/platform/ext/target/arm/corstone1000/CMakeLists.txt
22index 19863bcdb6d2..f232c7639bd5 100644
23--- a/platform/ext/target/arm/corstone1000/CMakeLists.txt
24+++ b/platform/ext/target/arm/corstone1000/CMakeLists.txt
25@@ -64,6 +64,8 @@ target_include_directories(platform_s
26 cc312
27 fw_update_agent
28 soft_crc
29+ io
30+ partition
31 )
32
33 target_sources(platform_s
34@@ -81,6 +83,11 @@ target_sources(platform_s
35 fw_update_agent/fwu_agent.c
36 fw_update_agent/uefi_fmp.c
37 soft_crc/soft_crc.c
38+ io/io_block.c
39+ io/io_flash.c
40+ io/io_storage.c
41+ partition/partition.c
42+ partition/gpt.c
43 $<$<NOT:$<BOOL:${PLATFORM_DEFAULT_OTP}>>:${PLATFORM_DIR}/ext/accelerator/cc312/otp_cc312.c>
44 )
45
46diff --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
47index b6ed656de833..9c76b25a3a38 100644
48--- a/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c
49+++ b/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c
50@@ -14,6 +14,8 @@
51 #include "region_defs.h"
52 #include "uefi_capsule_parser.h"
53 #include "flash_common.h"
54+#include "partition.h"
55+#include "platform.h"
56 #include "platform_base_address.h"
57 #include "platform_description.h"
58 #include "tfm_plat_nv_counters.h"
59@@ -146,6 +148,8 @@ extern ARM_DRIVER_FLASH FWU_METADATA_FLASH_DEV;
60 static enum fwu_agent_error_t private_metadata_read(
61 struct fwu_private_metadata* p_metadata)
62 {
63+ partition_entry_t *part;
64+ uuid_t private_uuid = PRIVATE_METADATA_TYPE_UUID;
65 int ret;
66
67 FWU_LOG_MSG("%s: enter\n\r", __func__);
68@@ -154,7 +158,13 @@ static enum fwu_agent_error_t private_metadata_read(
69 return FWU_AGENT_ERROR;
70 }
71
72- ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET, p_metadata,
73+ part = get_partition_entry_by_type(&private_uuid);
74+ if (!part) {
75+ FWU_LOG_MSG("Private metadata partition not found\n\r");
76+ return FWU_AGENT_ERROR;
77+ }
78+
79+ ret = FWU_METADATA_FLASH_DEV.ReadData(part->start, p_metadata,
80 sizeof(struct fwu_private_metadata));
81 if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
82 return FWU_AGENT_ERROR;
83@@ -169,6 +179,8 @@ static enum fwu_agent_error_t private_metadata_read(
84 static enum fwu_agent_error_t private_metadata_write(
85 struct fwu_private_metadata* p_metadata)
86 {
87+ uuid_t private_uuid = PRIVATE_METADATA_TYPE_UUID;
88+ partition_entry_t *part;
89 int ret;
90
91 FWU_LOG_MSG("%s: enter: boot_index = %u\n\r", __func__,
92@@ -178,12 +190,18 @@ static enum fwu_agent_error_t private_metadata_write(
93 return FWU_AGENT_ERROR;
94 }
95
96- ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET);
97+ part = get_partition_entry_by_type(&private_uuid);
98+ if (!part) {
99+ FWU_LOG_MSG("Private metadata partition not found\n\r");
100+ return FWU_AGENT_ERROR;
101+ }
102+
103+ ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
104 if (ret != ARM_DRIVER_OK) {
105 return FWU_AGENT_ERROR;
106 }
107
108- ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_PRIVATE_METADATA_REPLICA_1_OFFSET,
109+ ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
110 p_metadata, sizeof(struct fwu_private_metadata));
111 if (ret < 0 || ret != sizeof(struct fwu_private_metadata)) {
112 return FWU_AGENT_ERROR;
113@@ -219,16 +237,25 @@ static enum fwu_agent_error_t metadata_validate(struct fwu_metadata *p_metadata)
114
115 static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metadata *p_metadata)
116 {
117+ uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
118+ partition_entry_t *part;
119 int ret;
120
121- FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
122- FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
123-
124 if (!p_metadata) {
125 return FWU_AGENT_ERROR;
126 }
127
128- ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_METADATA_REPLICA_1_OFFSET,
129+ part = get_partition_entry_by_type(&metadata_uuid);
130+ if (!part) {
131+ FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
132+ return FWU_AGENT_ERROR;
133+ }
134+
135+ FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
136+ part->start, sizeof(struct fwu_metadata));
137+
138+
139+ ret = FWU_METADATA_FLASH_DEV.ReadData(part->start,
140 p_metadata, sizeof(struct fwu_metadata));
141 if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
142 return FWU_AGENT_ERROR;
143@@ -242,16 +269,24 @@ static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metada
144
145 static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
146 {
147+ uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
148+ partition_entry_t *part;
149 int ret;
150
151- FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
152- FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
153-
154 if (!p_metadata) {
155 return FWU_AGENT_ERROR;
156 }
157
158- ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_METADATA_REPLICA_1_OFFSET,
159+ part = get_partition_entry_by_type(&metadata_uuid);
160+ if (!part) {
161+ FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
162+ return FWU_AGENT_ERROR;
163+ }
164+
165+ FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
166+ part->start, sizeof(struct fwu_metadata));
167+
168+ ret = FWU_METADATA_FLASH_DEV.ReadData(part->start,
169 p_metadata, sizeof(struct fwu_metadata));
170 if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
171 return FWU_AGENT_ERROR;
172@@ -270,35 +305,49 @@ static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata)
173 static enum fwu_agent_error_t metadata_write(
174 struct fwu_metadata *p_metadata)
175 {
176+ uuid_t metadata_uuid = FWU_METADATA_TYPE_UUID;
177+ partition_entry_t *part;
178 int ret;
179
180- FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
181- FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata));
182-
183 if (!p_metadata) {
184 return FWU_AGENT_ERROR;
185 }
186
187- ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_1_OFFSET);
188+ part = get_partition_entry_by_type(&metadata_uuid);
189+ if (!part) {
190+ FWU_LOG_MSG("%s: FWU metadata partition not found\n\r", __func__);
191+ return FWU_AGENT_ERROR;
192+ }
193+
194+ FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
195+ part->start, sizeof(struct fwu_metadata));
196+
197+ ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
198 if (ret != ARM_DRIVER_OK) {
199 return FWU_AGENT_ERROR;
200 }
201
202- ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_1_OFFSET,
203+ ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
204 p_metadata, sizeof(struct fwu_metadata));
205 if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
206 return FWU_AGENT_ERROR;
207 }
208
209+ part = get_partition_replica_by_type(&metadata_uuid);
210+ if (!part) {
211+ FWU_LOG_MSG("%s: FWU metadata replica partition not found\n\r", __func__);
212+ return FWU_AGENT_ERROR;
213+ }
214+
215 FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__,
216- FWU_METADATA_REPLICA_2_OFFSET, sizeof(struct fwu_metadata));
217+ part->start, sizeof(struct fwu_metadata));
218
219- ret = FWU_METADATA_FLASH_DEV.EraseSector(FWU_METADATA_REPLICA_2_OFFSET);
220+ ret = FWU_METADATA_FLASH_DEV.EraseSector(part->start);
221 if (ret != ARM_DRIVER_OK) {
222 return FWU_AGENT_ERROR;
223 }
224
225- ret = FWU_METADATA_FLASH_DEV.ProgramData(FWU_METADATA_REPLICA_2_OFFSET,
226+ ret = FWU_METADATA_FLASH_DEV.ProgramData(part->start,
227 p_metadata, sizeof(struct fwu_metadata));
228 if (ret < 0 || ret != sizeof(struct fwu_metadata)) {
229 return FWU_AGENT_ERROR;
230@@ -355,6 +404,9 @@ enum fwu_agent_error_t fwu_metadata_provision(void)
231
232 FWU_LOG_MSG("%s: enter\n\r", __func__);
233
234+ plat_io_storage_init();
235+ partition_init(PLATFORM_GPT_IMAGE);
236+
237 ret = fwu_metadata_init();
238 if (ret) {
239 return ret;
240diff --git a/platform/ext/target/arm/corstone1000/partition/efi.h b/platform/ext/target/arm/corstone1000/partition/efi.h
241index f66daffb32d6..7e6a4bc883e6 100644
242--- a/platform/ext/target/arm/corstone1000/partition/efi.h
243+++ b/platform/ext/target/arm/corstone1000/partition/efi.h
244@@ -8,6 +8,7 @@
245 #ifndef DRIVERS_PARTITION_EFI_H
246 #define DRIVERS_PARTITION_EFI_H
247
248+#include <stdint.h>
249 #include <string.h>
250
251 #include "uuid.h"
252diff --git a/platform/ext/target/arm/corstone1000/partition/partition.c b/platform/ext/target/arm/corstone1000/partition/partition.c
253index afc6aa1c5cb8..d76e123d728f 100644
254--- a/platform/ext/target/arm/corstone1000/partition/partition.c
255+++ b/platform/ext/target/arm/corstone1000/partition/partition.c
256@@ -293,6 +293,20 @@ const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_uuid) {
257 return NULL;
258 }
259
260+const partition_entry_t *get_partition_replica_by_type(const uuid_t *type_uuid) {
261+ int count = 0;
262+ int i;
263+
264+ for (i = 0; i < list.entry_count; i++) {
265+ if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) {
266+ if (++count == 2)
267+ return &list.list[i];
268+ }
269+ }
270+
271+ return NULL;
272+}
273+
274 const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid) {
275 int i;
276
277diff --git a/platform/ext/target/arm/corstone1000/partition/partition.h b/platform/ext/target/arm/corstone1000/partition/partition.h
278index 54af47aca415..450cf20a073c 100644
279--- a/platform/ext/target/arm/corstone1000/partition/partition.h
280+++ b/platform/ext/target/arm/corstone1000/partition/partition.h
281@@ -40,6 +40,7 @@ typedef struct partition_entry_list {
282 int load_partition_table(unsigned int image_id);
283 const partition_entry_t *get_partition_entry(const char *name);
284 const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_guid);
285+const partition_entry_t *get_partition_replica_by_type(const uuid_t *type_uuid);
286 const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid);
287 const partition_entry_list_t *get_partition_entry_list(void);
288 void partition_init(unsigned int image_id);
289diff --git a/platform/ext/target/arm/corstone1000/platform.h b/platform/ext/target/arm/corstone1000/platform.h
290index 894f5e309029..a88093ed4f9d 100644
291--- a/platform/ext/target/arm/corstone1000/platform.h
292+++ b/platform/ext/target/arm/corstone1000/platform.h
293@@ -13,6 +13,11 @@ typedef enum {
294 PLATFORM_IMAGE_COUNT,
295 }platform_image_id_t;
296
297+#define FWU_METADATA_TYPE_UUID \
298+ ((uuid_t){{0xa0, 0x84, 0x7a, 0x8a}, {0x87, 0x83}, {0xf6, 0x40}, 0xab, 0x41, {0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23}})
299+#define PRIVATE_METADATA_TYPE_UUID \
300+ ((uuid_t){{0xc3, 0x5d, 0xb5, 0xec}, {0xb7, 0x8a}, {0x84, 0x4a}, 0xab, 0x56, {0xeb, 0x0a, 0x99, 0x74, 0xdb, 0x42}})
301+
302 /* Initialize io storage of the platform */
303 int32_t plat_io_storage_init(void);
304
305--
3062.40.0
307