blob: 7bb76933f93158991629228a8d5e3ae521e8720a [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
27static struct fwu_metadata metadata;
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +010028static bool is_metadata_initialized __unused;
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010029
30/*******************************************************************************
31 * Compute CRC32 of the FWU metadata, and check it against the CRC32 value
32 * present in the FWU metadata.
33 *
34 * return -1 on error, otherwise 0
35 ******************************************************************************/
36static int fwu_metadata_crc_check(void)
37{
38 unsigned char *data = (unsigned char *)&metadata;
39
Manish V Badarkhec885d5c2021-07-02 20:29:56 +010040 uint32_t calc_crc = tf_crc32(0U, data + sizeof(metadata.crc_32),
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +010041 (sizeof(metadata) -
42 sizeof(metadata.crc_32)));
43
44 if (metadata.crc_32 != calc_crc) {
45 return -1;
46 }
47
48 return 0;
49}
50
51/*******************************************************************************
52 * Check the sanity of FWU metadata.
53 *
54 * return -1 on error, otherwise 0
55 ******************************************************************************/
56static int fwu_metadata_sanity_check(void)
57{
58 /* ToDo: add more conditions for sanity check */
59 if ((metadata.active_index >= NR_OF_FW_BANKS) ||
60 (metadata.previous_active_index >= NR_OF_FW_BANKS)) {
61 return -1;
62 }
63
64 return 0;
65}
66
67/*******************************************************************************
68 * Verify and load specified FWU metadata image to local FWU metadata structure.
69 *
70 * @image_id: FWU metadata image id (either FWU_METADATA_IMAGE_ID or
71 * BKUP_FWU_METADATA_IMAGE_ID)
72 *
73 * return a negative value on error, otherwise 0
74 ******************************************************************************/
75static int fwu_metadata_load(unsigned int image_id)
76{
77 int result;
78 uintptr_t dev_handle, image_handle, image_spec;
79 size_t bytes_read;
80
81 assert((image_id == FWU_METADATA_IMAGE_ID) ||
82 (image_id == BKUP_FWU_METADATA_IMAGE_ID));
83
84 result = plat_fwu_set_metadata_image_source(image_id,
85 &dev_handle,
86 &image_spec);
87 if (result != 0) {
88 WARN("Failed to set reference to image id=%u (%i)\n",
89 image_id, result);
90 return result;
91 }
92
93 result = io_open(dev_handle, image_spec, &image_handle);
94 if (result != 0) {
95 WARN("Failed to load image id id=%u (%i)\n",
96 image_id, result);
97 return result;
98 }
99
100 result = io_read(image_handle, (uintptr_t)&metadata,
101 sizeof(struct fwu_metadata), &bytes_read);
102
103 if (result != 0) {
104 WARN("Failed to read image id=%u (%i)\n", image_id, result);
105 goto exit;
106 }
107
108 if (sizeof(struct fwu_metadata) != bytes_read) {
109 /* return -1 in case of partial/no read */
110 result = -1;
111 WARN("Read bytes (%zu) instead of expected (%zu) bytes\n",
112 bytes_read, sizeof(struct fwu_metadata));
113 goto exit;
114 }
115
116 /* sanity check on loaded parameters */
117 result = fwu_metadata_sanity_check();
118 if (result != 0) {
119 WARN("Sanity %s\n", "check failed on FWU metadata");
120 goto exit;
121 }
122
123 /* CRC check on loaded parameters */
124 result = fwu_metadata_crc_check();
125 if (result != 0) {
126 WARN("CRC %s\n", "check failed on FWU metadata");
127 }
128
129exit:
130 (void)io_close(image_handle);
131
132 return result;
133}
134
135/*******************************************************************************
Sughosh Ganu56724d02024-02-01 16:59:01 +0530136 * The platform can be in one of Valid, Invalid or Accepted states.
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100137 *
Sughosh Ganu56724d02024-02-01 16:59:01 +0530138 * Invalid - One or more images in the bank are corrupted, or partially
139 * overwritten. The bank is not to be used for booting.
140 *
141 * Valid - All images of the bank are valid but at least one image has not
142 * been accepted. This implies that the platform is in Trial State.
143 *
144 * Accepted - All images of the bank are valid and accepted.
145 *
146 * Returns the state of the current active bank
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100147 ******************************************************************************/
Sughosh Ganu56724d02024-02-01 16:59:01 +0530148uint32_t fwu_get_active_bank_state(void)
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100149{
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100150 assert(is_metadata_initialized);
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100151
Sughosh Ganu56724d02024-02-01 16:59:01 +0530152 return metadata.bank_state[metadata.active_index];
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100153}
154
Sughosh Ganu9adce872021-12-01 11:50:22 +0530155const struct fwu_metadata *fwu_get_metadata(void)
156{
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100157 assert(is_metadata_initialized);
Sughosh Ganu9adce872021-12-01 11:50:22 +0530158
159 return &metadata;
160}
161
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100162/*******************************************************************************
163 * Load verified copy of FWU metadata image kept in the platform NV storage
164 * into local FWU metadata structure.
165 * Also, update platform I/O policies with the offset address and length of
166 * firmware-updated images kept in the platform NV storage.
167 ******************************************************************************/
168void fwu_init(void)
169{
170 /* Load FWU metadata which will be used to load the images in the
171 * active bank as per PSA FWU specification
172 */
173 int result = fwu_metadata_load(FWU_METADATA_IMAGE_ID);
174
175 if (result != 0) {
176 WARN("loading of FWU-Metadata failed, "
177 "using Bkup-FWU-Metadata\n");
178
179 result = fwu_metadata_load(BKUP_FWU_METADATA_IMAGE_ID);
180 if (result != 0) {
181 ERROR("loading of Bkup-FWU-Metadata failed\n");
182 panic();
183 }
184 }
185
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100186 is_metadata_initialized = true;
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100187
Sebastien Pasdeloupaae7c962022-03-01 14:13:21 +0100188 plat_fwu_set_images_source(&metadata);
Manish V Badarkhe0ec3ac62021-06-20 20:35:25 +0100189}