[RFC PATCH 08/14] FWU: Move FWU metadata operation code in fwu_mdata.c

Masami Hiramatsu masami.hiramatsu at linaro.org
Thu Jan 20 16:30:40 CET 2022


Remove some FWU metadata operations which only access or
modify the metadata itself from fwu_mdata_ops and move
it in fwu_mdata.c.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu at linaro.org>
---
 include/fwu.h                       |    9 ---
 lib/fwu_updates/fwu_mdata.c         |  105 ++++++++++++++++++++++++-----------
 lib/fwu_updates/fwu_mdata_gpt_blk.c |   85 ----------------------------
 3 files changed, 71 insertions(+), 128 deletions(-)

diff --git a/include/fwu.h b/include/fwu.h
index 6c9b64a9f1..7af94988b7 100644
--- a/include/fwu.h
+++ b/include/fwu.h
@@ -14,26 +14,17 @@
 struct fwu_mdata;
 
 /**
- * @get_active_index: get the current active_index value
  * @get_image_alt_num: get the alt number to be used for the image
  * @mdata_check: check the validity of the FWU metadata partitions
- * @set_accept_image: set the accepted bit for the image
- * @clear_accept_image: clear the accepted bit for the image
  * @get_mdata() - Get a FWU metadata copy
  * @update_mdata() - Update the FWU metadata copy
  */
 struct fwu_mdata_ops {
-	int (*get_active_index)(u32 *active_idx);
-
 	int (*get_image_alt_num)(efi_guid_t image_type_id, u32 update_bank,
 				 int *alt_num);
 
 	int (*mdata_check)(void);
 
-	int (*set_accept_image)(efi_guid_t *img_type_id, u32 bank);
-
-	int (*clear_accept_image)(efi_guid_t *img_type_id, u32 bank);
-
 	int (*get_mdata)(struct fwu_mdata **mdata);
 
 	int (*update_mdata)(struct fwu_mdata *mdata);
diff --git a/lib/fwu_updates/fwu_mdata.c b/lib/fwu_updates/fwu_mdata.c
index 1324771a71..505f4f3630 100644
--- a/lib/fwu_updates/fwu_mdata.c
+++ b/lib/fwu_updates/fwu_mdata.c
@@ -3,6 +3,7 @@
  * Copyright (c) 2021, Linaro Limited
  */
 
+#include <efi_loader.h>
 #include <fwu.h>
 #include <fwu_mdata.h>
 #include <log.h>
@@ -12,6 +13,9 @@
 #include <linux/types.h>
 #include <u-boot/crc.h>
 
+#define IMAGE_ACCEPT_SET	BIT(0)
+#define IMAGE_ACCEPT_CLEAR	BIT(1)
+
 static struct fwu_mdata_ops *get_fwu_mdata_ops(void)
 {
 	struct fwu_mdata_ops *ops;
@@ -66,18 +70,28 @@ int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
  */
 int fwu_get_active_index(u32 *active_idx)
 {
-	struct fwu_mdata_ops *ops;
+	int ret;
+	struct fwu_mdata *mdata = NULL;
 
-	ops = get_fwu_mdata_ops();
-	if (!ops)
-		return -EPROTONOSUPPORT;
+	ret = fwu_get_mdata(&mdata);
+	if (ret < 0) {
+		log_err("Unable to get valid FWU metadata\n");
+		return ret;
+	}
 
-	if (!ops->get_active_index) {
-		log_err("get_active_index() method not defined for the platform\n");
-		return -ENOSYS;
+	/*
+	 * Found the FWU metadata partition, now read the active_index
+	 * value
+	 */
+	*active_idx = mdata->active_index;
+	if (*active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+		printf("Active index value read is incorrect\n");
+		ret = -EINVAL;
 	}
 
-	return ops->get_active_index(active_idx);
+	free(mdata);
+
+	return ret;
 }
 
 /**
@@ -197,12 +211,12 @@ int fwu_revert_boot_index(void)
 {
 	int ret;
 	u32 cur_active_index;
-	struct fwu_mdata *mdata = NULL;
+	struct fwu_mdata *mdata;
 
 	ret = fwu_get_mdata(&mdata);
 	if (ret < 0) {
 		log_err("Unable to get valid FWU metadata\n");
-		goto out;
+		return ret;
 	}
 
 	/*
@@ -223,6 +237,49 @@ int fwu_revert_boot_index(void)
 		ret = -EIO;
 	}
 
+	free(mdata);
+
+	return ret;
+}
+
+static int fwu_set_clear_image_accept(efi_guid_t *img_type_id,
+				      u32 bank, u8 action)
+{
+	void *buf;
+	int ret, i;
+	u32 nimages;
+	struct fwu_mdata *mdata = NULL;
+	struct fwu_image_entry *img_entry;
+	struct fwu_image_bank_info *img_bank_info;
+
+	ret = fwu_get_mdata(&mdata);
+	if (ret < 0) {
+		log_err("Unable to get valid FWU metadata\n");
+		return ret;
+	}
+
+	nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK;
+	img_entry = &mdata->img_entry[0];
+	for (i = 0; i < nimages; i++) {
+		if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) {
+			img_bank_info = &img_entry[i].img_bank_info[bank];
+			if (action == IMAGE_ACCEPT_SET)
+				img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
+			else
+				img_bank_info->accepted = 0;
+
+			buf = &mdata->version;
+			mdata->crc32 = crc32(0, buf, sizeof(*mdata) -
+					     sizeof(u32));
+
+			ret = fwu_update_mdata(mdata);
+			goto out;
+		}
+	}
+
+	/* Image not found */
+	ret = -EINVAL;
+
 out:
 	free(mdata);
 
@@ -244,18 +301,8 @@ out:
  */
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
 {
-	struct fwu_mdata_ops *ops;
-
-	ops = get_fwu_mdata_ops();
-	if (!ops)
-		return -EPROTONOSUPPORT;
-
-	if (!ops->set_accept_image) {
-		log_err("set_accept_image() method not defined for the platform\n");
-		return -ENOSYS;
-	}
-
-	return ops->set_accept_image(img_type_id, bank);
+	return fwu_set_clear_image_accept(img_type_id, bank,
+					  IMAGE_ACCEPT_SET);
 }
 
 /**
@@ -274,18 +321,8 @@ int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
  */
 int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
 {
-	struct fwu_mdata_ops *ops;
-
-	ops = get_fwu_mdata_ops();
-	if (!ops)
-		return -EPROTONOSUPPORT;
-
-	if (!ops->clear_accept_image) {
-		log_err("clear_accept_image() method not defined for the platform\n");
-		return -ENOSYS;
-	}
-
-	return ops->clear_accept_image(img_type_id, bank);
+	return fwu_set_clear_image_accept(img_type_id, bank,
+					  IMAGE_ACCEPT_CLEAR);
 }
 
 /**
diff --git a/lib/fwu_updates/fwu_mdata_gpt_blk.c b/lib/fwu_updates/fwu_mdata_gpt_blk.c
index 7f8b8b68fc..af9b1adf94 100644
--- a/lib/fwu_updates/fwu_mdata_gpt_blk.c
+++ b/lib/fwu_updates/fwu_mdata_gpt_blk.c
@@ -24,9 +24,6 @@
 #define MDATA_READ		BIT(0)
 #define MDATA_WRITE		BIT(1)
 
-#define IMAGE_ACCEPT_SET	BIT(0)
-#define IMAGE_ACCEPT_CLEAR	BIT(1)
-
 static int gpt_get_mdata_partitions(struct blk_desc *desc,
 				    u16 *primary_mpart,
 				    u16 *secondary_mpart)
@@ -335,34 +332,6 @@ out:
 	return ret;
 }
 
-int fwu_gpt_get_active_index(u32 *active_idx)
-{
-	int ret;
-	struct fwu_mdata *mdata = NULL;
-
-	ret = gpt_get_mdata(&mdata);
-	if (ret < 0) {
-		log_err("Unable to get valid FWU metadata\n");
-		goto out;
-	}
-
-	/*
-	 * Found the FWU metadata partition, now read the active_index
-	 * value
-	 */
-	*active_idx = mdata->active_index;
-	if (*active_idx > CONFIG_FWU_NUM_BANKS - 1) {
-		printf("Active index value read is incorrect\n");
-		ret = -EINVAL;
-		goto out;
-	}
-
-out:
-	free(mdata);
-
-	return ret;
-}
-
 static int gpt_get_image_alt_num(struct blk_desc *desc,
 				 efi_guid_t image_type_id,
 				 u32 update_bank, int *alt_no)
@@ -467,63 +436,9 @@ int fwu_gpt_get_mdata(struct fwu_mdata **mdata)
 	return gpt_get_mdata(mdata);
 }
 
-static int fwu_gpt_set_clear_image_accept(efi_guid_t *img_type_id,
-					  u32 bank, u8 action)
-{
-	int ret, i;
-	u32 nimages;
-	struct fwu_mdata *mdata = NULL;
-	struct fwu_image_entry *img_entry;
-	struct fwu_image_bank_info *img_bank_info;
-
-	ret = gpt_get_mdata(&mdata);
-	if (ret < 0) {
-		log_err("Unable to get valid FWU metadata\n");
-		goto out;
-	}
-
-	nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK;
-	img_entry = &mdata->img_entry[0];
-	for (i = 0; i < nimages; i++) {
-		if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) {
-			img_bank_info = &img_entry[i].img_bank_info[bank];
-			if (action == IMAGE_ACCEPT_SET)
-				img_bank_info->accepted |= FWU_IMAGE_ACCEPTED;
-			else
-				img_bank_info->accepted = 0;
-
-			ret = fwu_gpt_update_mdata(mdata);
-			goto out;
-		}
-	}
-
-	/* Image not found */
-	ret = -EINVAL;
-
-out:
-	free(mdata);
-
-	return ret;
-}
-
-static int fwu_gpt_accept_image(efi_guid_t *img_type_id, u32 bank)
-{
-	return fwu_gpt_set_clear_image_accept(img_type_id, bank,
-					      IMAGE_ACCEPT_SET);
-}
-
-static int fwu_gpt_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
-{
-	return fwu_gpt_set_clear_image_accept(img_type_id, bank,
-					      IMAGE_ACCEPT_CLEAR);
-}
-
 struct fwu_mdata_ops fwu_gpt_blk_ops = {
-	.get_active_index = fwu_gpt_get_active_index,
 	.get_image_alt_num = fwu_gpt_get_image_alt_num,
 	.mdata_check = fwu_gpt_mdata_check,
-	.set_accept_image = fwu_gpt_accept_image,
-	.clear_accept_image = fwu_gpt_clear_accept_image,
 	.get_mdata = fwu_gpt_get_mdata,
 	.update_mdata = fwu_gpt_update_mdata,
 };



More information about the U-Boot mailing list