summaryrefslogtreecommitdiff
path: root/meta-arm/meta-arm-bsp/recipes-bsp/trusted-firmware-m/files/corstone1000/0002-Platform-corstone1000-get-fwu-and-private-metadata-f.patch
blob: 3d7fc4b3d101569072794a4f2fdf70cae65bb665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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