blob: ce46904ff2ecfc06d8580e6254a0b4f18da5cad2 [file] [log] [blame]
Sughosh Ganua56a10b2024-03-22 16:27:20 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2024, Linaro Limited
4 */
5
6#include <fwu.h>
7#include <fwu_mdata.h>
8#include <log.h>
9
10#include <linux/types.h>
11
12#define FWU_MDATA_VERSION 0x2U
Sughosh Ganu0a4651b2024-09-09 16:50:16 +053013#define FWU_IMG_DESC_OFFSET 0x20U
14
15static struct fwu_mdata g_mdata;
Sughosh Ganua56a10b2024-03-22 16:27:20 +053016
17static inline struct fwu_fw_store_desc *fwu_get_fw_desc(struct fwu_mdata *mdata)
18{
19 return (struct fwu_fw_store_desc *)((u8 *)mdata + sizeof(*mdata));
20}
21
22static uint32_t fwu_check_trial_state(struct fwu_mdata *mdata, uint32_t bank)
23{
24 return mdata->bank_state[bank] == FWU_BANK_VALID ? 1 : 0;
25}
26
27static void fwu_data_init(void)
28{
29 int i;
30 size_t image_info_size;
31 void *dst_img_info, *src_img_info;
32 struct fwu_data *data = fwu_get_data();
33 struct fwu_mdata *mdata = data->fwu_mdata;
34
35 data->crc32 = mdata->crc32;
36 data->version = mdata->version;
37 data->active_index = mdata->active_index;
38 data->previous_active_index = mdata->previous_active_index;
39 data->metadata_size = mdata->metadata_size;
40 fwu_plat_get_bootidx(&data->boot_index);
41 data->trial_state = fwu_check_trial_state(mdata, data->boot_index);
42
43 data->num_banks = fwu_get_fw_desc(mdata)->num_banks;
44 data->num_images = fwu_get_fw_desc(mdata)->num_images;
45
46 for (i = 0; i < 4; i++) {
47 data->bank_state[i] = mdata->bank_state[i];
48 }
49
50 image_info_size = sizeof(data->fwu_images);
51 src_img_info = &fwu_get_fw_desc(mdata)->img_entry[0];
52 dst_img_info = &data->fwu_images[0];
53
54 memcpy(dst_img_info, src_img_info, image_info_size);
55}
56
57static int fwu_mdata_sanity_checks(void)
58{
59 uint8_t num_banks;
60 uint16_t num_images;
61 struct fwu_data *data = fwu_get_data();
62 struct fwu_mdata *mdata = data->fwu_mdata;
63
Sughosh Ganua56a10b2024-03-22 16:27:20 +053064 num_banks = fwu_get_fw_desc(mdata)->num_banks;
65 num_images = fwu_get_fw_desc(mdata)->num_images;
66
67 if (num_banks != CONFIG_FWU_NUM_BANKS) {
68 log_err("Number of Banks(%u) in FWU Metadata different from the configured value(%d)",
69 num_banks, CONFIG_FWU_NUM_BANKS);
70 return -EINVAL;
71 }
72
73 if (num_images != CONFIG_FWU_NUM_IMAGES_PER_BANK) {
74 log_err("Number of Images(%u) in FWU Metadata different from the configured value(%d)",
75 num_images, CONFIG_FWU_NUM_IMAGES_PER_BANK);
76 return -EINVAL;
77 }
78
79 return 0;
80}
81
82static int fwu_bank_state_update(bool trial_state, uint32_t bank)
83{
84 int ret;
85 struct fwu_data *data = fwu_get_data();
86 struct fwu_mdata *mdata = data->fwu_mdata;
87
Sughosh Ganu4c63b7e2024-09-09 16:50:18 +053088 if (!trial_state && !fwu_bank_accepted(data, bank))
89 return 0;
90
Sughosh Ganua56a10b2024-03-22 16:27:20 +053091 mdata->bank_state[bank] = data->bank_state[bank] = trial_state ?
92 FWU_BANK_VALID : FWU_BANK_ACCEPTED;
93
94 ret = fwu_sync_mdata(mdata, BOTH_PARTS);
95 if (ret)
96 log_err("Unable to set bank_state for bank %u\n", bank);
97 else
98 data->trial_state = trial_state;
99
100 return ret;
101}
102
103static int fwu_trial_state_start(uint update_index)
104{
105 int ret;
106
107 ret = fwu_trial_state_ctr_start();
108 if (ret)
109 return ret;
110
111 ret = fwu_bank_state_update(1, update_index);
112 if (ret)
113 return ret;
114
115 return 0;
116}
117
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530118static bool fwu_get_mdata_mandatory(uint part)
119{
120 int ret = 0;
121 struct udevice *fwu_dev = fwu_get_dev();
122
123 memset(&g_mdata, 0, sizeof(struct fwu_mdata));
124
125 ret = fwu_read_mdata(fwu_dev, &g_mdata,
126 part == PRIMARY_PART ? true : false,
127 sizeof(struct fwu_mdata));
128 if (ret)
129 return false;
130
131 if (g_mdata.version != FWU_MDATA_VERSION) {
132 log_err("FWU partition %u has metadata version %u. Expected value of %u\n",
133 part, g_mdata.version, FWU_MDATA_VERSION);
134 return false;
135 }
136
137 if (g_mdata.desc_offset != FWU_IMG_DESC_OFFSET) {
138 log_err("Descriptor Offset(0x%x) in the FWU Metadata partition %u not equal to 0x20\n",
139 g_mdata.desc_offset, part);
140 log_err("Image information expected in the metadata\n");
141 return false;
142 }
143
144 return true;
145}
146
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530147/**
148 * fwu_populate_mdata_image_info() - Populate the image information
149 * of the metadata
150 * @data: Version agnostic FWU metadata information
151 *
152 * Populate the image information in the FWU metadata by copying it
153 * from the version agnostic structure. This is done before the
154 * metadata gets written to the storage media.
155 *
156 * Return: None
157 */
158void fwu_populate_mdata_image_info(struct fwu_data *data)
159{
160 size_t image_info_size;
161 struct fwu_mdata *mdata = data->fwu_mdata;
162 void *dst_img_info, *src_img_info;
163
164 image_info_size = sizeof(data->fwu_images);
165 dst_img_info = &fwu_get_fw_desc(mdata)->img_entry[0];
166 src_img_info = &data->fwu_images[0];
167
168 memcpy(dst_img_info, src_img_info, image_info_size);
169}
170
171/**
172 * fwu_state_machine_updates() - Update FWU state of the platform
173 * @trial_state: Is platform transitioning into Trial State
174 * @update_index: Bank number to which images have been updated
175 *
176 * On successful completion of updates, transition the platform to
177 * either Trial State or Regular State.
178 *
179 * To transition the platform to Trial State, start the
180 * TrialStateCtr counter, followed by setting the value of bank_state
181 * field of the metadata to Valid state(applicable only in version 2
182 * of metadata).
183 *
184 * In case, the platform is to transition directly to Regular State,
185 * update the bank_state field of the metadata to Accepted
186 * state(applicable only in version 2 of metadata).
187 *
188 * Return: 0 if OK, -ve on error
189 */
190int fwu_state_machine_updates(bool trial_state, uint32_t update_index)
191{
192 return trial_state ? fwu_trial_state_start(update_index) :
193 fwu_bank_state_update(0, update_index);
194}
195
196/**
197 * fwu_get_mdata_size() - Get the FWU metadata size
198 * @mdata_size: Size of the metadata structure
199 *
200 * Get the size of the FWU metadata from the structure. This is later used
201 * to allocate memory for the structure.
202 *
203 * Return: 0 if OK, -ve on error
204 */
205int fwu_get_mdata_size(uint32_t *mdata_size)
206{
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530207 struct fwu_data *data = fwu_get_data();
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530208
209 if (data->metadata_size) {
210 *mdata_size = data->metadata_size;
211 return 0;
212 }
213
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530214 *mdata_size = g_mdata.metadata_size;
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530215 if (!*mdata_size)
216 return -EINVAL;
217
218 return 0;
219}
220
221/**
222 * fwu_init() - FWU specific initialisations
223 *
224 * Carry out some FWU specific initialisations including allocation
225 * of memory for the metadata copies, and reading the FWU metadata
226 * copies into the allocated memory. The metadata fields are then
227 * copied into a version agnostic structure.
228 *
229 * Return: 0 if OK, -ve on error
230 */
231int fwu_init(void)
232{
233 int ret;
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530234
235 /*
236 * First we read only the top level structure
237 * and get the size of the complete structure.
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530238 * Try reading the first partition first, if
239 * that does not work, try the secondary
240 * partition. The idea is, if one of the
241 * partitions is corrupted, it should be restored
242 * from the intact partition.
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530243 */
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530244 if (!fwu_get_mdata_mandatory(PRIMARY_PART) &&
245 !fwu_get_mdata_mandatory(SECONDARY_PART)) {
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530246 log_err("FWU metadata read failed\n");
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530247 return -1;
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530248 }
249
Sughosh Ganu0a4651b2024-09-09 16:50:16 +0530250 ret = fwu_mdata_copies_allocate(g_mdata.metadata_size);
Sughosh Ganua56a10b2024-03-22 16:27:20 +0530251 if (ret)
252 return ret;
253
254 /*
255 * Now read the entire structure, both copies, and
256 * validate that the copies.
257 */
258 ret = fwu_get_mdata(NULL);
259 if (ret)
260 return ret;
261
262 ret = fwu_mdata_sanity_checks();
263 if (ret)
264 return ret;
265
266 fwu_data_init();
267
268 return 0;
269}