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