blob: 5c3ccf878cfc93bd4cfd56023930318f23734311 [file] [log] [blame]
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +01001/*
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +01002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8
9#include <common/debug.h>
Manish V Badarkhec885d5c2021-07-02 20:29:56 +010010#include <common/tf_crc32.h>
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010011#include <common/tbbr/tbbr_img_def.h>
12#include <drivers/fwu/fwu.h>
13#include <drivers/fwu/fwu_metadata.h>
14#include <drivers/io/io_storage.h>
15
16#include <plat/common/platform.h>
17
18/*
19 * Assert that crc_32 is the first member of fwu_metadata structure.
20 * It avoids accessing data outside of the metadata structure during
21 * CRC32 computation if the crc_32 field gets moved due the structure
22 * member(s) addition in the future.
23 */
24CASSERT((offsetof(struct fwu_metadata, crc_32) == 0),
25 crc_32_must_be_first_member_of_structure);
26
Sughosh Ganud2566cf2024-01-17 16:38:01 +053027/*
28 * Ensure that the NR_OF_FW_BANKS selected by the platform is not
29 * zero and not greater than the maximum number of banks allowed
30 * by the specification.
31 */
32CASSERT((NR_OF_FW_BANKS > 0) && (NR_OF_FW_BANKS <= NR_OF_MAX_FW_BANKS),
33 assert_fwu_num_banks_invalid_value);
34
35#define FWU_METADATA_VERSION 2U
36#define FWU_FW_STORE_DESC_OFFSET 0x20U
37
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010038static struct fwu_metadata metadata;
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +010039static bool is_metadata_initialized __unused;
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010040
41/*******************************************************************************
42 * Compute CRC32 of the FWU metadata, and check it against the CRC32 value
43 * present in the FWU metadata.
44 *
45 * return -1 on error, otherwise 0
46 ******************************************************************************/
47static int fwu_metadata_crc_check(void)
48{
49 unsigned char *data = (unsigned char *)&metadata;
50
Manish V Badarkhec885d5c2021-07-02 20:29:56 +010051 uint32_t calc_crc = tf_crc32(0U, data + sizeof(metadata.crc_32),
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010052 (sizeof(metadata) -
53 sizeof(metadata.crc_32)));
54
55 if (metadata.crc_32 != calc_crc) {
56 return -1;
57 }
58
59 return 0;
60}
61
62/*******************************************************************************
63 * Check the sanity of FWU metadata.
64 *
Sughosh Ganud2566cf2024-01-17 16:38:01 +053065 * return -EINVAL on error, otherwise 0
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010066 ******************************************************************************/
67static int fwu_metadata_sanity_check(void)
68{
Sughosh Ganud2566cf2024-01-17 16:38:01 +053069 if (metadata.version != FWU_METADATA_VERSION) {
70 WARN("Incorrect FWU Metadata version of %u\n",
71 metadata.version);
72 return -EINVAL;
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010073 }
74
Sughosh Ganud2566cf2024-01-17 16:38:01 +053075 if (metadata.active_index >= NR_OF_FW_BANKS) {
76 WARN("Active Index value(%u) greater than the configured value(%d)",
77 metadata.active_index, NR_OF_FW_BANKS);
78 return -EINVAL;
79 }
80
81 if (metadata.previous_active_index >= NR_OF_FW_BANKS) {
82 WARN("Previous Active Index value(%u) greater than the configured value(%d)",
83 metadata.previous_active_index, NR_OF_FW_BANKS);
84 return -EINVAL;
85 }
86
87#if PSA_FWU_METADATA_FW_STORE_DESC
88 if (metadata.fw_desc.num_banks != NR_OF_FW_BANKS) {
89 WARN("Number of Banks(%u) in FWU Metadata different from the configured value(%d)",
90 metadata.fw_desc.num_banks, NR_OF_FW_BANKS);
91 return -EINVAL;
92 }
93
94 if (metadata.fw_desc.num_images != NR_OF_IMAGES_IN_FW_BANK) {
95 WARN("Number of Images(%u) in FWU Metadata different from the configured value(%d)",
96 metadata.fw_desc.num_images, NR_OF_IMAGES_IN_FW_BANK);
97 return -EINVAL;
98 }
99
100 if (metadata.desc_offset != FWU_FW_STORE_DESC_OFFSET) {
101 WARN("Descriptor Offset(0x%x) in the FWU Metadata not equal to 0x20\n",
102 metadata.desc_offset);
103 return -EINVAL;
104 }
105#else
106 if (metadata.desc_offset != 0U) {
107 WARN("Descriptor offset has non zero value of 0x%x\n",
108 metadata.desc_offset);
109 return -EINVAL;
110 }
111#endif
112
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100113 return 0;
114}
115
116/*******************************************************************************
117 * Verify and load specified FWU metadata image to local FWU metadata structure.
118 *
119 * @image_id: FWU metadata image id (either FWU_METADATA_IMAGE_ID or
120 * BKUP_FWU_METADATA_IMAGE_ID)
121 *
122 * return a negative value on error, otherwise 0
123 ******************************************************************************/
124static int fwu_metadata_load(unsigned int image_id)
125{
126 int result;
127 uintptr_t dev_handle, image_handle, image_spec;
128 size_t bytes_read;
129
130 assert((image_id == FWU_METADATA_IMAGE_ID) ||
131 (image_id == BKUP_FWU_METADATA_IMAGE_ID));
132
133 result = plat_fwu_set_metadata_image_source(image_id,
134 &dev_handle,
135 &image_spec);
136 if (result != 0) {
137 WARN("Failed to set reference to image id=%u (%i)\n",
138 image_id, result);
139 return result;
140 }
141
142 result = io_open(dev_handle, image_spec, &image_handle);
143 if (result != 0) {
144 WARN("Failed to load image id id=%u (%i)\n",
145 image_id, result);
146 return result;
147 }
148
149 result = io_read(image_handle, (uintptr_t)&metadata,
150 sizeof(struct fwu_metadata), &bytes_read);
151
152 if (result != 0) {
153 WARN("Failed to read image id=%u (%i)\n", image_id, result);
154 goto exit;
155 }
156
157 if (sizeof(struct fwu_metadata) != bytes_read) {
158 /* return -1 in case of partial/no read */
159 result = -1;
160 WARN("Read bytes (%zu) instead of expected (%zu) bytes\n",
161 bytes_read, sizeof(struct fwu_metadata));
162 goto exit;
163 }
164
165 /* sanity check on loaded parameters */
166 result = fwu_metadata_sanity_check();
167 if (result != 0) {
168 WARN("Sanity %s\n", "check failed on FWU metadata");
169 goto exit;
170 }
171
172 /* CRC check on loaded parameters */
173 result = fwu_metadata_crc_check();
174 if (result != 0) {
175 WARN("CRC %s\n", "check failed on FWU metadata");
176 }
177
178exit:
179 (void)io_close(image_handle);
180
181 return result;
182}
183
184/*******************************************************************************
Sughosh Ganu56724d02024-02-01 16:59:01 +0530185 * The platform can be in one of Valid, Invalid or Accepted states.
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100186 *
Sughosh Ganu56724d02024-02-01 16:59:01 +0530187 * Invalid - One or more images in the bank are corrupted, or partially
188 * overwritten. The bank is not to be used for booting.
189 *
190 * Valid - All images of the bank are valid but at least one image has not
191 * been accepted. This implies that the platform is in Trial State.
192 *
193 * Accepted - All images of the bank are valid and accepted.
194 *
195 * Returns the state of the current active bank
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100196 ******************************************************************************/
Sughosh Ganu56724d02024-02-01 16:59:01 +0530197uint32_t fwu_get_active_bank_state(void)
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100198{
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100199 assert(is_metadata_initialized);
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100200
Sughosh Ganu56724d02024-02-01 16:59:01 +0530201 return metadata.bank_state[metadata.active_index];
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100202}
203
Sughosh Ganu9adce872021-12-01 11:50:22 +0530204const struct fwu_metadata *fwu_get_metadata(void)
205{
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100206 assert(is_metadata_initialized);
Sughosh Ganu9adce872021-12-01 11:50:22 +0530207
208 return &metadata;
209}
210
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100211/*******************************************************************************
212 * Load verified copy of FWU metadata image kept in the platform NV storage
213 * into local FWU metadata structure.
214 * Also, update platform I/O policies with the offset address and length of
215 * firmware-updated images kept in the platform NV storage.
216 ******************************************************************************/
217void fwu_init(void)
218{
219 /* Load FWU metadata which will be used to load the images in the
220 * active bank as per PSA FWU specification
221 */
222 int result = fwu_metadata_load(FWU_METADATA_IMAGE_ID);
223
224 if (result != 0) {
225 WARN("loading of FWU-Metadata failed, "
226 "using Bkup-FWU-Metadata\n");
227
228 result = fwu_metadata_load(BKUP_FWU_METADATA_IMAGE_ID);
229 if (result != 0) {
230 ERROR("loading of Bkup-FWU-Metadata failed\n");
231 panic();
232 }
233 }
234
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100235 is_metadata_initialized = true;
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100236
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100237 plat_fwu_set_images_source(&metadata);
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100238}