1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022, Linaro Limited
7 #include <efi_loader.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <u-boot/crc.h>
27 static int fwu_get_dev_mdata(struct udevice **dev, struct fwu_mdata *mdata)
31 ret = uclass_first_device_err(UCLASS_FWU_MDATA, dev);
33 log_debug("Cannot find fwu device\n");
40 ret = fwu_get_mdata(*dev, mdata);
42 log_debug("Unable to get valid FWU metadata\n");
47 static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id)
51 struct efi_fw_image *image;
54 image = update_info.images;
55 for (i = 0; i < num_image_type_guids; i++) {
56 if (index == image[i].image_index) {
57 guidcpy(image_type_id, &image[i].image_type_id);
66 * fwu_verify_mdata() - Verify the FWU metadata
67 * @mdata: FWU metadata structure
68 * @pri_part: FWU metadata partition is primary or secondary
70 * Verify the FWU metadata by computing the CRC32 for the metadata
71 * structure and comparing it against the CRC32 value stored as part
74 * Return: 0 if OK, -ve on error
77 int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
82 buf = &mdata->version;
83 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
85 if (calc_crc32 != mdata->crc32) {
86 log_debug("crc32 check failed for %s FWU metadata partition\n",
87 pri_part ? "primary" : "secondary");
95 * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
97 * Read both the metadata copies from the storage media, verify their checksum,
98 * and ascertain that both copies match. If one of the copies has gone bad,
99 * restore it from the good copy.
101 * Return: 0 if OK, -ve on error
104 int fwu_check_mdata_validity(void)
108 struct fwu_mdata pri_mdata;
109 struct fwu_mdata secondary_mdata;
111 uint valid_partitions, invalid_partitions;
113 ret = fwu_get_dev_mdata(&dev, NULL);
118 * Check if the platform has defined its own
119 * function to check the metadata partitions'
120 * validity. If so, that takes precedence.
122 ret = fwu_mdata_check(dev);
123 if (!ret || ret != -ENOSYS)
127 * Two FWU metadata partitions are expected.
128 * If we don't have two, user needs to create
131 valid_partitions = 0;
132 ret = fwu_get_mdata_part_num(dev, mdata_parts);
134 log_debug("Error getting the FWU metadata partitions\n");
138 ret = fwu_read_mdata_partition(dev, &pri_mdata, mdata_parts[0]);
140 ret = fwu_verify_mdata(&pri_mdata, 1);
142 valid_partitions |= PRIMARY_PART;
145 ret = fwu_read_mdata_partition(dev, &secondary_mdata, mdata_parts[1]);
147 ret = fwu_verify_mdata(&secondary_mdata, 0);
149 valid_partitions |= SECONDARY_PART;
152 if (valid_partitions == (PRIMARY_PART | SECONDARY_PART)) {
154 * Before returning, check that both the
155 * FWU metadata copies are the same. If not,
156 * populate the secondary partition from the
157 * primary partition copy.
159 if (!memcmp(&pri_mdata, &secondary_mdata,
160 sizeof(struct fwu_mdata))) {
163 log_info("Both FWU metadata copies are valid but do not match.");
164 log_info(" Restoring the secondary partition from the primary\n");
165 ret = fwu_write_mdata_partition(dev, &pri_mdata,
168 log_debug("Restoring secondary FWU metadata partition failed\n");
173 if (!(valid_partitions & BOTH_PARTS)) {
174 log_info("Both FWU metadata partitions invalid\n");
179 invalid_partitions = valid_partitions ^ BOTH_PARTS;
180 ret = fwu_write_mdata_partition(dev,
181 (invalid_partitions == PRIMARY_PART) ?
182 &secondary_mdata : &pri_mdata,
183 (invalid_partitions == PRIMARY_PART) ?
184 mdata_parts[0] : mdata_parts[1]);
187 log_debug("Restoring %s FWU metadata partition failed\n",
188 (invalid_partitions == PRIMARY_PART) ?
189 "primary" : "secondary");
196 * fwu_get_active_index() - Get active_index from the FWU metadata
197 * @active_idx: active_index value to be read
199 * Read the active_index field from the FWU metadata and place it in
200 * the variable pointed to be the function argument.
202 * Return: 0 if OK, -ve on error
205 int fwu_get_active_index(uint *active_idx)
209 struct fwu_mdata mdata = { 0 };
211 ret = fwu_get_dev_mdata(&dev, &mdata);
216 * Found the FWU metadata partition, now read the active_index
219 *active_idx = mdata.active_index;
220 if (*active_idx >= CONFIG_FWU_NUM_BANKS) {
221 log_debug("Active index value read is incorrect\n");
229 * fwu_set_active_index() - Set active_index in the FWU metadata
230 * @active_idx: active_index value to be set
232 * Update the active_index field in the FWU metadata
234 * Return: 0 if OK, -ve on error
237 int fwu_set_active_index(uint active_idx)
241 struct fwu_mdata mdata = { 0 };
243 if (active_idx >= CONFIG_FWU_NUM_BANKS) {
244 log_debug("Invalid active index value\n");
248 ret = fwu_get_dev_mdata(&dev, &mdata);
253 * Update the active index and previous_active_index fields
254 * in the FWU metadata
256 mdata.previous_active_index = mdata.active_index;
257 mdata.active_index = active_idx;
260 * Now write this updated FWU metadata to both the
261 * FWU metadata partitions
263 ret = fwu_update_mdata(dev, &mdata);
265 log_debug("Failed to update FWU metadata partitions\n");
273 * fwu_get_image_index() - Get the Image Index to be used for capsule update
274 * @image_index: The Image Index for the image
276 * The FWU multi bank update feature computes the value of image_index at
277 * runtime, based on the bank to which the image needs to be written to.
278 * Derive the image_index value for the image.
280 * Currently, the capsule update driver uses the DFU framework for
281 * the updates. This function gets the DFU alt number which is to
282 * be used as the Image Index
284 * Return: 0 if OK, -ve on error
287 int fwu_get_image_index(u8 *image_index)
292 efi_guid_t *image_guid, image_type_id;
294 struct fwu_mdata mdata = { 0 };
295 struct fwu_image_entry *img_entry;
296 struct fwu_image_bank_info *img_bank_info;
298 ret = fwu_get_dev_mdata(&dev, &mdata);
302 ret = fwu_plat_get_update_index(&update_bank);
304 log_debug("Failed to get the FWU update bank\n");
308 ret = fwu_get_image_type_id(image_index, &image_type_id);
310 log_debug("Unable to get image_type_id for image_index %u\n",
317 * The FWU metadata has been read. Now get the image_uuid for the
318 * image with the update_bank.
320 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
321 if (!guidcmp(&image_type_id,
322 &mdata.img_entry[i].image_type_uuid)) {
323 img_entry = &mdata.img_entry[i];
324 img_bank_info = &img_entry->img_bank_info[update_bank];
325 image_guid = &img_bank_info->image_uuid;
326 ret = fwu_plat_get_alt_num(dev, image_guid, &alt_num);
328 log_debug("alt_num not found for partition with GUID %pUs\n",
331 log_debug("alt_num %d for partition %pUs\n",
332 alt_num, image_guid);
333 *image_index = alt_num + 1;
340 log_debug("Partition with the image type %pUs not found\n",
348 * fwu_revert_boot_index() - Revert the active index in the FWU metadata
350 * Revert the active_index value in the FWU metadata, by swapping the values
351 * of active_index and previous_active_index in both copies of the
354 * Return: 0 if OK, -ve on error
357 int fwu_revert_boot_index(void)
360 u32 cur_active_index;
362 struct fwu_mdata mdata = { 0 };
364 ret = fwu_get_dev_mdata(&dev, &mdata);
369 * Swap the active index and previous_active_index fields
370 * in the FWU metadata
372 cur_active_index = mdata.active_index;
373 mdata.active_index = mdata.previous_active_index;
374 mdata.previous_active_index = cur_active_index;
377 * Now write this updated FWU metadata to both the
378 * FWU metadata partitions
380 ret = fwu_update_mdata(dev, &mdata);
382 log_debug("Failed to update FWU metadata partitions\n");
390 * fwu_clrset_image_accept() - Set or Clear the Acceptance bit for the image
391 * @img_type_id: GUID of the image type for which the accepted bit is to be
393 * @bank: Bank of which the image's Accept bit is to be set or cleared
394 * @action: Action which specifies whether image's Accept bit is to be set or
397 * Set/Clear the accepted bit for the image specified by the img_guid parameter.
398 * This indicates acceptance or rejection of image for subsequent boots by some
399 * governing component like OS(or firmware).
401 * Return: 0 if OK, -ve on error
404 static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action)
408 struct fwu_mdata mdata = { 0 };
409 struct fwu_image_entry *img_entry;
410 struct fwu_image_bank_info *img_bank_info;
412 ret = fwu_get_dev_mdata(&dev, &mdata);
416 img_entry = &mdata.img_entry[0];
417 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
418 if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) {
419 img_bank_info = &img_entry[i].img_bank_info[bank];
420 if (action == IMAGE_ACCEPT_SET)
421 img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
423 img_bank_info->accepted = 0;
425 ret = fwu_update_mdata(dev, &mdata);
430 /* Image not found */
438 * fwu_accept_image() - Set the Acceptance bit for the image
439 * @img_type_id: GUID of the image type for which the accepted bit is to be
441 * @bank: Bank of which the image's Accept bit is to be set
443 * Set the accepted bit for the image specified by the img_guid parameter. This
444 * indicates acceptance of image for subsequent boots by some governing component
445 * like OS(or firmware).
447 * Return: 0 if OK, -ve on error
450 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
452 return fwu_clrset_image_accept(img_type_id, bank,
457 * fwu_clear_accept_image() - Clear the Acceptance bit for the image
458 * @img_type_id: GUID of the image type for which the accepted bit is to be
460 * @bank: Bank of which the image's Accept bit is to be cleared
462 * Clear the accepted bit for the image type specified by the img_type_id parameter.
463 * This function is called after the image has been updated. The accepted bit is
464 * cleared to be set subsequently after passing the image acceptance criteria, by
465 * either the OS(or firmware)
467 * Return: 0 if OK, -ve on error
470 int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
472 return fwu_clrset_image_accept(img_type_id, bank,
477 * fwu_plat_get_update_index() - Get the value of the update bank
478 * @update_idx: Bank number to which images are to be updated
480 * Get the value of the bank(partition) to which the update needs to be
483 * Note: This is a weak function and platforms can override this with
484 * their own implementation for selection of the update bank.
486 * Return: 0 if OK, -ve on error
489 __weak int fwu_plat_get_update_index(uint *update_idx)
494 ret = fwu_get_active_index(&active_idx);
498 *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS;