Skip to contentSkip to conversation

FWU: Add support for FWU Multi Bank Update feature

PATCH Superseded Sughosh Ganu 7 participants 64 messages Feb 7, 2022 – Mar 9, 2022 Page 1 of 2

Subsystems (3)

v4 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v4 cover FWU: Add support for FWU Multi Bank Update feature

The patchset adds support for the FWU Multi Bank Update[1] feature. Certain aspects of the Dependable Boot[2] specification have also been implemented.

The FWU multi bank update feature is used for supporting multiple sets(also called banks) of firmware image(s), allowing the platform to boot from a different bank, in case it fails to boot from the active bank. This functionality is supported by keeping the relevant information in a structure called metadata, which provides information on the images. Among other parameters, the metadata structure contains information on the currect active bank that is being used to boot image(s).

Functionality is being added to work with the UEFI capsule driver in u-boot. The metadata is read to gather information on the update bank, which is the bank to which the firmware images would be flashed to. On a successful completion of the update of all components, the active bank field in the metadata is updated, to reflect the bank from which the platform will boot on the subsequent boots.

Currently, the feature is being enabled on the STM32MP157C-DK2 board which boots a FIP image from a uSD card partitioned with the GPT partioning scheme. This also requires changes in the previous stage of bootloader, which parses the metadata and selects the bank to boot the image(s) from. Support is being added in tf-a(BL2 stage) for the STM32MP157C-DK2 board to boot the active bank images. These changes have been merged to the upstream tf-a's integration branch[3].

These patches are based on top of the series from Takahiro to add Authentication support to mkeficapsule utility[4] and a couple of other patches[5][6]

TODO's
------
* Add a unit test case for the newly added FWU_MDATA uclass. Some involved effort is needed on this since the host device interface on sandbox cannot be used with the UT framework.
* Add test case for the feature with the python test suite, along the lines of capsule update testing.

[1] - https://developer.arm.com/documentation/den0118/a
[2] - https://git.codelinaro.org/linaro/dependable-boot/mbfw/uploads/6f7ddfe3be24e18d4319e108a758d02e/mbfw.pdf
[3] - https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/log/?h=integration
[4] - https://patchwork.ozlabs.org/project/uboot/list/?series=281549
[5] - https://patchwork.ozlabs.org/project/uboot/patch/164388019634.446835.18271480521485569016.stgit@localhost/
[6] - https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function
* Move the metadata access driver for GPT partitioned block devices under drivers/fwu-mdata/ directory, complying with driver model.
* Move functionality to get the active index under the common function instead of the GPT block device specific driver.
* Remove function for getting the storage device containing the metadata as the information is now obtained from the device tree.
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs
* Define the function fill_image_type_guid_array for the ST DK2 board for GPT partitioned devices.
* Change the TrialStateCtr efi variable attribute to remove the runtime attribute
* Rebase the change on top of the patch from Masami to call efi_capsule_update_firmware directly.
* Put the FWU related checks which were earlier in efi_update_capsule function to separate functions fwu_empty_capsule and fwu_empty_capsule_process.
* Use the device model api uclass_get_device to probe and get the FWU Metadata device.
* Add related documentation for empty capsules in the mkeficapsule man page.
* Add separate usage for empty capsules, with corresponding valid options.
* Use ternary operators where possible.
* Put a exclusivity check for the empty capsule options.

Sughosh Ganu (11):
FWU: Add FWU metadata structure and driver for accessing metadata
FWU: Add FWU metadata access driver for GPT partitioned block devices
FWU: stm32mp1: Add helper functions for accessing FWU metadata
FWU: STM32MP1: Add support to read boot index from backup register
EFI: FMP: Add provision to update image's ImageTypeId in image
descriptor
stm32mp1: Populate ImageTypeId values in EFI_FIRMWARE_IMAGE_DESCRIPTOR
array
FWU: Add boot time checks as highlighted by the FWU specification
FWU: Add support for FWU Multi Bank Update feature
FWU: cmd: Add a command to read FWU metadata
mkeficapsule: Add support for generating empty capsules
FWU: doc: Add documentation for the FWU feature

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 board/st/stm32mp1/stm32mp1.c                  | 178 +++++++
 cmd/Kconfig                                   |   7 +
 cmd/Makefile                                  |   1 +
 cmd/fwu_mdata.c                               |  74 +++
 common/board_r.c                              |   6 +
 doc/develop/uefi/fwu_updates.rst              | 142 +++++
 doc/develop/uefi/index.rst                    |   1 +
 doc/develop/uefi/uefi.rst                     |   2 +
 .../firmware/fwu-mdata.txt                    |  18 +
 doc/mkeficapsule.1                            |  23 +-
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |  16 +
 drivers/fwu-mdata/Makefile                    |   7 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 +++++++++++++++
 drivers/fwu-mdata/fwu_mdata_gpt_blk.c         | 501 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/efi_loader.h                          |   2 +
 include/fwu.h                                 |  70 +++
 include/fwu_mdata.h                           |  67 +++
 lib/Kconfig                                   |   6 +
 lib/Makefile                                  |   1 +
 lib/efi_loader/efi_capsule.c                  | 221 +++++++-
 lib/efi_loader/efi_firmware.c                 |  71 ++-
 lib/efi_loader/efi_setup.c                    |   3 +-
 lib/fwu_updates/Kconfig                       |  31 ++
 lib/fwu_updates/Makefile                      |   6 +
 lib/fwu_updates/fwu.c                         | 204 +++++++
 tools/eficapsule.h                            |   8 +
 tools/mkeficapsule.c                          | 131 ++++-
 31 files changed, 2208 insertions(+), 34 deletions(-)
 create mode 100644 cmd/fwu_mdata.c
 create mode 100644 doc/develop/uefi/fwu_updates.rst
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 drivers/fwu-mdata/fwu_mdata_gpt_blk.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
 create mode 100644 lib/fwu_updates/Kconfig
 create mode 100644 lib/fwu_updates/Makefile
 create mode 100644 lib/fwu_updates/fwu.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/11 FWU: Add FWU metadata structure and driver for accessing metadata

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+	fwu-mdata {
+		compatible = "u-boot,fwu-mdata";
+		fwu-mdata-store = <&sdmmc1>;
+	};
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+		    the FWU metadata partition.
+
+Example :
+	fwu-mdata {
+		compatible = "u-boot,fwu-mdata";
+		fwu-mdata-store = <&sdmmc1>;
+	};
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
 source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
 source "drivers/gpio/Kconfig"
 
 source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
 obj-$(CONFIG_CPU) += cpu/
 obj-y += crypto/
 obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+	bool "Driver support for accessing FWU Metadata"
+	depends on DM
+	help
+	  Enable support for accessing FWU Metadata partitions. The
+	  FWU Metadata partitions reside on the same storage device
+	  which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0

434 lines not on this page · raw message

include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
 	UCLASS_ETH_PHY,		/* Ethernet PHY device */
 	UCLASS_FIRMWARE,	/* Firmware */
 	UCLASS_FS_FIRMWARE_LOADER,		/* Generic loader */
+	UCLASS_FWU_MDATA,	/* FWU Metadata Access */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
 	UCLASS_HASH,		/* Hash device */
 	UCLASS_HWSPINLOCK,	/* Hardware semaphores */
include/fwu.h new file +51 -0

51 lines not on this page · raw message

include/fwu_mdata.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Hi Sughosh,

Thanks for updating the series. I have some comment on this patch.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:
[snip]

Quoted 10 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +10 -0
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *

drivers/fwu-mdata/fwu-mdata-uclass.c line 87

Quoted 10 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +10 -0
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;

Again, as we discussed previous series, please don't return unused allocated memory if the function returns an error.
That is something like putting a burden on the callers. They always needs to initialize the pointer before call and free it even if the function is failed.

Quoted 48 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +48 -0
+       }
+
+       /*
+        * Found the FWU metadata partition, now read the active_index
+        * value
+        */
+       *active_idx = mdata->active_index;
+       if (*active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */

drivers/fwu-mdata/fwu-mdata-uclass.c line 145

Quoted 10 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +10 -0
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));

I would like to suggest moving this crc32 calculation in the fwu_update_mdata(). If the crc32 is for detecting a broken metadata on the "storage media", I think it should be calculated in the fwu_update_mdata() to simplify the code and avoid unexpected bugs from mistakes. If the crc32 is calculated right before updating metadata, we can ensure that the crc32 is always sane except for someone breaking it on the storage media.

Thank you,

Signature · 1 line
Masami Hiramatsu
#3 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

hi Masami,

Quoted 25 lines · Masami Hiramatsu

Hi Sughosh,

Thanks for updating the series. I have some comment on this patch.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:
[snip]

Quoted 18 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +18 -0
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;

Again, as we discussed previous series, please don't return unused allocated memory if the function returns an error.
That is something like putting a burden on the callers. They always needs to initialize the pointer before call and free it even if the function is failed.

I would like to keep the convention consistent. The function that declares the pointer will also be responsible for free'ing it up. I find the alternative to be more confusing, where in some instances the callee frees up the memory, while in some cases it is the caller that frees it up. If there is no stated convention in u-boot which forbids this style of memory handling, I would like to keep this as is. I would say that this makes the implementation easier for the callee, since it is the responsibility of the caller to free up the memory.

Quoted 60 lines
Quoted 49 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +48 -0
+       }
+
+       /*
+        * Found the FWU metadata partition, now read the active_index
+        * value
+        */
+       *active_idx = mdata->active_index;
+       if (*active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */

drivers/fwu-mdata/fwu-mdata-uclass.c line 145

Quoted 10 lines
drivers/fwu-mdata/fwu-mdata-uclass.c new file +10 -0
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
Quoted 7 lines

I would like to suggest moving this crc32 calculation in the fwu_update_mdata(). If the crc32 is for detecting a broken metadata on the "storage media", I think it should be calculated in the fwu_update_mdata() to simplify the code and avoid unexpected bugs from mistakes. If the crc32 is calculated right before updating metadata, we can ensure that the crc32 is always sane except for someone breaking it on the storage media.

Makes sense. I will make the change as you are suggesting. Thanks.

-sughosh

Quoted 7 lines

Thank you,

--
Masami Hiramatsu

#4 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Hi Sughosh,

2022年2月8日(火) 19:24 Sughosh Ganu <[email protected]>:

Quoted 39 lines

hi Masami,

Quoted 25 lines · Masami Hiramatsu

Hi Sughosh,

Thanks for updating the series. I have some comment on this patch.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:
[snip]

Quoted 18 lines
(quoted diff) +18 -0
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;

Again, as we discussed previous series, please don't return unused allocated memory if the function returns an error.
That is something like putting a burden on the callers. They always needs to initialize the pointer before call and free it even if the function is failed.

Quoted 8 lines

I would like to keep the convention consistent. The function that declares the pointer will also be responsible for free'ing it up. I find the alternative to be more confusing, where in some instances the callee frees up the memory, while in some cases it is the caller that frees it up. If there is no stated convention in u-boot which forbids this style of memory handling, I would like to keep this as is. I would say that this makes the implementation easier for the callee, since it is the responsibility of the caller to free up the memory.

Hmm, ... I'm not convinced yet. Please give me the last chance to argue. I think the pointer is the pointer, that is not the resource itself.

Please see man asprintf for example.
Usually, if the function, which allocates any resource including memory, returns an error, the resource pointer is undefined. Of course, there is no need to unallocate the memory for undefined address.
That is I think the standard way to handle the resource allocation.

And actually, the callee implementation isn't simplified. In my case, it forces me to re-initialize the pointer to NULL if an error occurs. additional 1-line is needed :-) (maybe I need more comment lines to explain why this NULL setting is required)

BTW, I attached a patch to change this code. You can see the gpt_get_mdata() is refined into 2 gpt_get_mdata_part() and many gotos are removed.
This shows how both of callee and caller can be simplified with the convention which I suggested.

Thank you,

Signature · 1 line
Masami Hiramatsu

From 695b4276283654bcf0440889cf214b887174345d Mon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Thu, 20 Jan 2022 15:41:49 +0900
Subject: [PATCH] FWU: Free metadata copy if gpt_get_mdata() failed

It is better if a function which returns an error then release all allocated memory resources. This simplifies the mind model and less chance to forgot to free memory and double free.

---
 drivers/fwu-mdata/fwu-mdata-uclass.c  | 24 +++++--------
 drivers/fwu-mdata/fwu_mdata_gpt_blk.c | 49 +++++++++++++++------------
 2 files changed, 36 insertions(+), 37 deletions(-)
drivers/fwu-mdata/fwu-mdata-uclass.c +9 -15
index fad44b25d1..512ce157e9 100644
@@ -79,12 +79,12 @@ int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
 int fwu_get_active_index(u32 *active_idx)
 {
 	int ret;
-	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;
 	}
 
 	/*
@@ -96,8 +96,6 @@ int fwu_get_active_index(u32 *active_idx)
 		log_err("Active index value read is incorrect\n");
 		ret = -EINVAL;
 	}
-
-out:
 	free(mdata);
 
 	return ret;
@@ -115,17 +113,17 @@ out:
 int fwu_update_active_index(u32 active_idx)
 {
 	int ret;
-	struct fwu_mdata *mdata = NULL;
+	struct fwu_mdata *mdata;
 
 	if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
 		log_err("Active index value to be updated is incorrect\n");
-		return -1;
+		return -EINVAL;
 	}
 
 	ret = fwu_get_mdata(&mdata);
 	if (ret < 0) {
 		log_err("Unable to get valid FWU metadata\n");
-		goto out;
+		return ret;
 	}
 
 	/*
@@ -144,8 +142,6 @@ int fwu_update_active_index(u32 active_idx)
 		log_err("Failed to update FWU metadata partitions\n");
 		ret = -EIO;
 	}
-
-out:
 	free(mdata);
 
 	return ret;
@@ -225,12 +221,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;
 	}
 
 	/*
@@ -250,8 +246,6 @@ int fwu_revert_boot_index(void)
 		log_err("Failed to update FWU metadata partitions\n");
 		ret = -EIO;
 	}
-
-out:
 	free(mdata);
 
 	return ret;
@@ -277,14 +271,14 @@ static int fwu_set_clear_image_accept(efi_guid_t *img_type_id,
 {
 	int ret, i;
 	u32 nimages;
-	struct fwu_mdata *mdata = NULL;
+	struct fwu_mdata *mdata;
 	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");
-		goto out;
+		return ret;
 	}
 
 	nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK;
drivers/fwu-mdata/fwu_mdata_gpt_blk.c +27 -22

73 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

In reply to Sughosh Ganu

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

doc/device-tree-bindings/firmware/fwu-mdata.txt line 18

Quoted 6 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +6 -0
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };

I don't think this is aligned with getting these u-boot nodes to any standard locations.
Simon had that discussion with Rob some time ago. Was there any outcome from this discussion where u-boot nodes should go?

Thanks,
Michal

#6 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 76 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

-sughosh

Quoted 15 lines

doc/device-tree-bindings/firmware/fwu-mdata.txt line 18

Quoted 7 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +6 -0
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };

I don't think this is aligned with getting these u-boot nodes to any standard locations.
Simon had that discussion with Rob some time ago. Was there any outcome from this discussion where u-boot nodes should go?

Thanks,
Michal

#7 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 86 lines · Sughosh Ganu
Quoted 76 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

  arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
  .../firmware/fwu-mdata.txt                    |  18 +
  drivers/Kconfig                               |   2 +
  drivers/Makefile                              |   1 +
  drivers/fwu-mdata/Kconfig                     |   7 +
  drivers/fwu-mdata/Makefile                    |   6 +
  drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
  include/dm/uclass-id.h                        |   1 +
  include/fwu.h                                 |  51 ++
  include/fwu_mdata.h                           |  67 +++
  10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
   */
 
  #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

Quoted 5 lines

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

I think you should list all possible devices which can be used to have generic description for it. It is sort of the same list of devices which could be used for storing u-boot internal variables. That's why the same description can be used for it too.

Thanks,
Michal

#8 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

In reply to Sughosh Ganu

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 87 lines · Michal Simek
Quoted 76 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

Quoted 5 lines

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".
It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Thank you,

Quoted 18 lines

-sughosh

Quoted 15 lines

doc/device-tree-bindings/firmware/fwu-mdata.txt line 18

Quoted 7 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +6 -0
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };

I don't think this is aligned with getting these u-boot nodes to any standard locations.
Simon had that discussion with Rob some time ago. Was there any outcome from this discussion where u-boot nodes should go?

Thanks,
Michal

--
Masami Hiramatsu

#9 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 88 lines · Masami Hiramatsu

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 87 lines · Michal Simek
Quoted 84 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

  arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
  .../firmware/fwu-mdata.txt                    |  18 +
  drivers/Kconfig                               |   2 +
  drivers/Makefile                              |   1 +
  drivers/fwu-mdata/Kconfig                     |   7 +
  drivers/fwu-mdata/Makefile                    |   6 +
  drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
  include/dm/uclass-id.h                        |   1 +
  include/fwu.h                                 |  51 ++
  include/fwu_mdata.h                           |  67 +++
  10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
   */
 
  #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

Quoted 8 lines
Quoted 5 lines

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".

backend is fine. What does this compatible string have to do with it? I didn't see any fwu-mdata-gpt in this series.

Quoted 2 lines

It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Can you describe it?

Something like for raw accesses?
fwu-mdta-store = <&qspi 0 XXXX>;

Thanks,
Michal

#10 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

2022年2月8日(火) 22:45 Michal Simek <[email protected]>:

Quoted 98 lines · Masami Hiramatsu
Quoted 94 lines

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 92 lines · Michal Simek
Quoted 76 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

  arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
  .../firmware/fwu-mdata.txt                    |  18 +
  drivers/Kconfig                               |   2 +
  drivers/Makefile                              |   1 +
  drivers/fwu-mdata/Kconfig                     |   7 +
  drivers/fwu-mdata/Makefile                    |   6 +
  drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
  include/dm/uclass-id.h                        |   1 +
  include/fwu.h                                 |  51 ++
  include/fwu_mdata.h                           |  67 +++
  10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
   */
 
  #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Quoted 5 lines
Quoted 2 lines

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".

backend is fine. What does this compatible string have to do with it? I didn't see any fwu-mdata-gpt in this series.

Sughosh made it "fwu-mdata" but I think it should be "fwu-mdata-gpt" if the required parameters are different.

Quoted 8 lines
Quoted 3 lines

It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Can you describe it?

Something like for raw accesses?
fwu-mdta-store = <&qspi 0 XXXX>;

I'm still not sure what is the best way. What I thought was,

fwu-mdata-store = <&spi-flash>;
fwu-mdata-offsets = <500000, 520000>;

Thanks,

Quoted 3 lines

Thanks,
Michal

Signature · 1 line
Masami Hiramatsu
#11 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 114 lines · Masami Hiramatsu

2022年2月8日(火) 22:45 Michal Simek <[email protected]>:

Quoted 98 lines · Masami Hiramatsu
Quoted 94 lines

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 92 lines · Michal Simek
Quoted 76 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

   arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
   .../firmware/fwu-mdata.txt                    |  18 +
   drivers/Kconfig                               |   2 +
   drivers/Makefile                              |   1 +
   drivers/fwu-mdata/Kconfig                     |   7 +
   drivers/fwu-mdata/Makefile                    |   6 +
   drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
   include/dm/uclass-id.h                        |   1 +
   include/fwu.h                                 |  51 ++
   include/fwu_mdata.h                           |  67 +++
   10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
    */
 
   #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Quoted 5 lines
Quoted 2 lines

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".

backend is fine. What does this compatible string have to do with it? I didn't see any fwu-mdata-gpt in this series.

Sughosh made it "fwu-mdata" but I think it should be "fwu-mdata-gpt" if the required parameters are different.

Quoted 6 lines
Quoted 3 lines

It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Can you describe it?

Quoted 7 lines
Quoted 2 lines

Something like for raw accesses?
fwu-mdta-store = <&qspi 0 XXXX>;

I'm still not sure what is the best way. What I thought was,

fwu-mdata-store = <&spi-flash>;
fwu-mdata-offsets = <500000, 520000>;

as I said. Before this is applied I think we should work on generic proposal how to describe it. Because the same way can be used for u-boot variables and maybe others. Definitely I would prefer to ask Rob for helping with this and get this to yaml to be able to use the whole infrastructure to check it.

Thanks,
Michal

#12 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Hi Michal,

2022年2月8日(火) 23:27 Michal Simek <[email protected]>:

Quoted 121 lines · Masami Hiramatsu
Quoted 117 lines

2022年2月8日(火) 22:45 Michal Simek <[email protected]>:

Quoted 98 lines · Masami Hiramatsu
Quoted 94 lines

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 92 lines · Michal Simek
Quoted 76 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

   arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
   .../firmware/fwu-mdata.txt                    |  18 +
   drivers/Kconfig                               |   2 +
   drivers/Makefile                              |   1 +
   drivers/fwu-mdata/Kconfig                     |   7 +
   drivers/fwu-mdata/Makefile                    |   6 +
   drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
   include/dm/uclass-id.h                        |   1 +
   include/fwu.h                                 |  51 ++
   include/fwu_mdata.h                           |  67 +++
   10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
    */
 
   #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Quoted 5 lines
Quoted 2 lines

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".

backend is fine. What does this compatible string have to do with it? I didn't see any fwu-mdata-gpt in this series.

Sughosh made it "fwu-mdata" but I think it should be "fwu-mdata-gpt" if the required parameters are different.

Quoted 8 lines
Quoted 3 lines

It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Can you describe it?

Something like for raw accesses?
fwu-mdta-store = <&qspi 0 XXXX>;

Quoted 8 lines
Quoted 4 lines

I'm still not sure what is the best way. What I thought was,

fwu-mdata-store = <&spi-flash>;
fwu-mdata-offsets = <500000, 520000>;

as I said. Before this is applied I think we should work on generic proposal how to describe it. Because the same way can be used for u-boot variables and maybe others.

Agreed. This also reminds me the dfu_alt_info. Currently the dfu_alt_info is passed via u-boot variables, but this should be a (important) part of firmware information. I think it should be defined in the devicetree too. (actually, stm32 builds dfu_alt_info from the device information already)

Quoted 2 lines

Definitely I would prefer to ask Rob for helping with this and get this to yaml to be able to use the whole infrastructure to check it.

Yeah, and at first we should define what information should be in the devicetree.

Thank you,

Quoted 3 lines

Thanks,
Michal

Signature · 1 line
Masami Hiramatsu
#13 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Hi,

Quoted 129 lines · Masami Hiramatsu

Hi Michal,

2022年2月8日(火) 23:27 Michal Simek <[email protected]>:

Quoted 126 lines · Masami Hiramatsu
Quoted 121 lines

2022年2月8日(火) 22:45 Michal Simek <[email protected]>:

Quoted 98 lines · Masami Hiramatsu
Quoted 94 lines

2022年2月8日(火) 20:35 Sughosh Ganu <[email protected]>:

Quoted 92 lines · Michal Simek
Quoted 76 lines

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 63 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi | 7 +
.../firmware/fwu-mdata.txt | 18 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/fwu-mdata/Kconfig | 7 +
drivers/fwu-mdata/Makefile | 6 +
drivers/fwu-mdata/fwu-mdata-uclass.c | 434 ++++++++++++++++++
include/dm/uclass-id.h | 1 +
include/fwu.h | 51 ++
include/fwu_mdata.h | 67 +++
10 files changed, 594 insertions(+)
create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
     */
 
    #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +2 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+

doc/device-tree-bindings/firmware/fwu-mdata.txt line 12

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +10 -0
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
Quoted 8 lines

In 0/11 you are describing GPT partitions but I don't think this binding is generic enough to describe
other cases. It is saying device but not where exactly it is. I didn't read the whole series yet but arm spec recommends GPT but dt binding should be generic enough to describe
also other cases.
We are saving this structure to qspi for example but current binding can't be used for it.

I would wait to see Masami's implementation of this feature. If I am not wrong, his platform too is enabling this feature with a spi as the storage device. Maybe we can instead put a list of <device partition> tuples which can then list the device and partition number of the metadata partitions.

Quoted 5 lines
Quoted 2 lines

Yes, I would like to define new compatible for sf backend, e.g. "u-boot,fwu-mdata-sf".

backend is fine. What does this compatible string have to do with it? I didn't see any fwu-mdata-gpt in this series.

Sughosh made it "fwu-mdata" but I think it should be "fwu-mdata-gpt" if the required parameters are different.

Quoted 8 lines
Quoted 3 lines

It will have the fwu-mdata-store to point the SPI flash device, and will have a list of offset information for the metadata.

Can you describe it?

Something like for raw accesses?
fwu-mdta-store = <&qspi 0 XXXX>;

I'm still not sure what is the best way. What I thought was,

fwu-mdata-store = <&spi-flash>;
fwu-mdata-offsets = <500000, 520000>;

Quoted 8 lines
Quoted 3 lines

as I said. Before this is applied I think we should work on generic proposal how to describe it. Because the same way can be used for u-boot variables and maybe others.

Agreed. This also reminds me the dfu_alt_info. Currently the dfu_alt_info is passed via u-boot variables, but this should be a (important) part of firmware information. I think it should be defined in the devicetree too. (actually, stm32 builds dfu_alt_info from the device information already)

yes that capsule update via dfu_alt_info and upgrading all that structure should be also the part of it. I am not saying in this series. On the top of this one should be fine but it should be clear how this is supposed to work.

Quoted 5 lines
Quoted 2 lines

Definitely I would prefer to ask Rob for helping with this and get this to yaml to be able to use the whole infrastructure to check it.

Yeah, and at first we should define what information should be in the devicetree.

yes.

Thanks,
Michal

#14 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

In reply to Sughosh Ganu

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
 source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
 source "drivers/gpio/Kconfig"
 
 source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
 obj-$(CONFIG_CPU) += cpu/
 obj-y += crypto/
 obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
        UCLASS_ETH_PHY,         /* Ethernet PHY device */
        UCLASS_FIRMWARE,        /* Firmware */
        UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
        UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
        UCLASS_HASH,            /* Hash device */
        UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

And also I would separate dt binding, board dt update from actual feature.

Thanks,
Michal

Signature · 5 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs
#15 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

 arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
 .../firmware/fwu-mdata.txt                    |  18 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/fwu-mdata/Kconfig                     |   7 +
 drivers/fwu-mdata/Makefile                    |   6 +
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
 include/dm/uclass-id.h                        |   1 +
 include/fwu.h                                 |  51 ++
 include/fwu_mdata.h                           |  67 +++
 10 files changed, 594 insertions(+)
 create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt
 create mode 100644 drivers/fwu-mdata/Kconfig
 create mode 100644 drivers/fwu-mdata/Makefile
 create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c
 create mode 100644 include/fwu.h
 create mode 100644 include/fwu_mdata.h
arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
 source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
 source "drivers/gpio/Kconfig"
 
 source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
 obj-$(CONFIG_CPU) += cpu/
 obj-y += crypto/
 obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
 obj-y += misc/
 obj-$(CONFIG_MMC) += mmc/
 obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
        UCLASS_ETH_PHY,         /* Ethernet PHY device */
        UCLASS_FIRMWARE,        /* Firmware */
        UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
        UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
        UCLASS_HASH,            /* Hash device */
        UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 2 lines

And also I would separate dt binding, board dt update from actual feature.

Okay.

-sughosh

Quoted 11 lines

Thanks,
Michal

--
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs

#16 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 751 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

  arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
  .../firmware/fwu-mdata.txt                    |  18 +
  drivers/Kconfig                               |   2 +
  drivers/Makefile                              |   1 +
  drivers/fwu-mdata/Kconfig                     |   7 +
  drivers/fwu-mdata/Makefile                    |   6 +
  drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
  include/dm/uclass-id.h                        |   1 +
  include/fwu.h                                 |  51 ++
  include/fwu_mdata.h                           |  67 +++
  10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
   */
 
  #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
  source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
  source "drivers/gpio/Kconfig"
 
  source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
  obj-$(CONFIG_CPU) += cpu/
  obj-y += crypto/
  obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
  obj-y += misc/
  obj-$(CONFIG_MMC) += mmc/
  obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
         UCLASS_ETH_PHY,         /* Ethernet PHY device */
         UCLASS_FIRMWARE,        /* Firmware */
         UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
         UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
         UCLASS_HASH,            /* Hash device */
         UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

Quoted 6 lines

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them. It is not that far from using the same defconfig with bisicting tree.
You can definitely avoid it in your series.

Thanks,
Michal

#17 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 762 lines · Michal Simek
Quoted 751 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

  arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
  .../firmware/fwu-mdata.txt                    |  18 +
  drivers/Kconfig                               |   2 +
  drivers/Makefile                              |   1 +
  drivers/fwu-mdata/Kconfig                     |   7 +
  drivers/fwu-mdata/Makefile                    |   6 +
  drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
  include/dm/uclass-id.h                        |   1 +
  include/fwu.h                                 |  51 ++
  include/fwu_mdata.h                           |  67 +++
  10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
   */
 
  #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
  source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
  source "drivers/gpio/Kconfig"
 
  source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
  obj-$(CONFIG_CPU) += cpu/
  obj-y += crypto/
  obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
  obj-y += misc/
  obj-$(CONFIG_MMC) += mmc/
  obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
         UCLASS_ETH_PHY,         /* Ethernet PHY device */
         UCLASS_FIRMWARE,        /* Firmware */
         UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
         UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
         UCLASS_HASH,            /* Hash device */
         UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

Quoted 6 lines

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 3 lines

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

You can download the mkeficapsule series from linaro's patchwork[1].

Quoted 6 lines

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them.

Okay my understanding of a patch being bisectable was if the build does not break after applying a patch. Please note that on top of my series, you will need to define CONFIG_FWU_NUM_BANKS and CONFIG_FWU_NUM_IMAGES_PER_BANK. I did not define that on purpose since each platform would have it's own specific values.

-sughosh

[1] - https://patches.linaro.org/project/u-boot/list/?series=166236

Quoted 6 lines

It is not that far from using the same defconfig with bisicting tree. You can definitely avoid it in your series.

Thanks,
Michal

#18 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 763 lines · Sughosh Ganu
Quoted 762 lines · Michal Simek
Quoted 757 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

   arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
   .../firmware/fwu-mdata.txt                    |  18 +
   drivers/Kconfig                               |   2 +
   drivers/Makefile                              |   1 +
   drivers/fwu-mdata/Kconfig                     |   7 +
   drivers/fwu-mdata/Makefile                    |   6 +
   drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
   include/dm/uclass-id.h                        |   1 +
   include/fwu.h                                 |  51 ++
   include/fwu_mdata.h                           |  67 +++
   10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
    */
 
   #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
   source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
   source "drivers/gpio/Kconfig"
 
   source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
   obj-$(CONFIG_CPU) += cpu/
   obj-y += crypto/
   obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
   obj-y += misc/
   obj-$(CONFIG_MMC) += mmc/
   obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
          UCLASS_ETH_PHY,         /* Ethernet PHY device */
          UCLASS_FIRMWARE,        /* Firmware */
          UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
          UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
          UCLASS_HASH,            /* Hash device */
          UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 5 lines
Quoted 3 lines

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

You can download the mkeficapsule series from linaro's patchwork[1].

you depend on 3 series. Much easier is to create a branch and push it somewhere.

Quoted 7 lines
Quoted 6 lines

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them.

Quoted 5 lines

Okay my understanding of a patch being bisectable was if the build does not break after applying a patch. Please note that on top of my series, you will need to define CONFIG_FWU_NUM_BANKS and CONFIG_FWU_NUM_IMAGES_PER_BANK. I did not define that on purpose since each platform would have it's own specific values.

Even if this is true you can't use in any patch a macro/value which is not defined and it is not clear what it is. I apply patch 1 and review patch 1 and not looking at patch 8 to get what that values are for.

Define them as reasonable default for common case or your usecase. Platform can set them up when they adopt this approach and they are not happy with your defaults.

Thanks,
Michal

#19 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 788 lines · Michal Simek
Quoted 763 lines · Sughosh Ganu
Quoted 762 lines · Michal Simek
Quoted 757 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

   arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi      |   7 +
   .../firmware/fwu-mdata.txt                    |  18 +
   drivers/Kconfig                               |   2 +
   drivers/Makefile                              |   1 +
   drivers/fwu-mdata/Kconfig                     |   7 +
   drivers/fwu-mdata/Makefile                    |   6 +
   drivers/fwu-mdata/fwu-mdata-uclass.c          | 434 ++++++++++++++++++
   include/dm/uclass-id.h                        |   1 +
   include/fwu.h                                 |  51 ++
   include/fwu_mdata.h                           |  67 +++
   10 files changed, 594 insertions(+)

create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
    */
 
   #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
   source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
   source "drivers/gpio/Kconfig"
 
   source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
   obj-$(CONFIG_CPU) += cpu/
   obj-y += crypto/
   obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
   obj-y += misc/
   obj-$(CONFIG_MMC) += mmc/
   obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
          UCLASS_ETH_PHY,         /* Ethernet PHY device */
          UCLASS_FIRMWARE,        /* Firmware */
          UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
          UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
          UCLASS_HASH,            /* Hash device */
          UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 5 lines
Quoted 3 lines

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

You can download the mkeficapsule series from linaro's patchwork[1].

you depend on 3 series. Much easier is to create a branch and push it somewhere.

Quoted 7 lines
Quoted 6 lines

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them.

Quoted 5 lines

Okay my understanding of a patch being bisectable was if the build does not break after applying a patch. Please note that on top of my series, you will need to define CONFIG_FWU_NUM_BANKS and CONFIG_FWU_NUM_IMAGES_PER_BANK. I did not define that on purpose since each platform would have it's own specific values.

Quoted 3 lines

Even if this is true you can't use in any patch a macro/value which is not defined and it is not clear what it is. I apply patch 1 and review patch 1 and not looking at patch 8 to get what that values are for.

My point was that I have not defined these values at all. These will have to be defined on a per platform level. But if you insist, I will define default values, but in my opinion it is better to get a build failure and then set correct values, rather than the build going through with some values which are incorrect for the platform.

If you apply the patches as they are, they should not result in any build failures.

-sughosh

Quoted 8 lines

Define them as reasonable default for common case or your usecase. Platform can set them up when they adopt this approach and they are not happy with your defaults.

Thanks,
Michal

#20 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 793 lines · Sughosh Ganu
Quoted 788 lines · Michal Simek
Quoted 763 lines · Sughosh Ganu
Quoted 762 lines · Michal Simek
Quoted 757 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi | 7 +
.../firmware/fwu-mdata.txt | 18 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/fwu-mdata/Kconfig | 7 +
drivers/fwu-mdata/Makefile | 6 +
drivers/fwu-mdata/fwu-mdata-uclass.c | 434 ++++++++++++++++++
include/dm/uclass-id.h | 1 +
include/fwu.h | 51 ++
include/fwu_mdata.h | 67 +++
10 files changed, 594 insertions(+)
create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
     */
 
    #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
    source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
    source "drivers/gpio/Kconfig"
 
    source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
    obj-$(CONFIG_CPU) += cpu/
    obj-y += crypto/
    obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
    obj-y += misc/
    obj-$(CONFIG_MMC) += mmc/
    obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
           UCLASS_ETH_PHY,         /* Ethernet PHY device */
           UCLASS_FIRMWARE,        /* Firmware */
           UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
           UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
           UCLASS_HASH,            /* Hash device */
           UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 5 lines
Quoted 3 lines

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

You can download the mkeficapsule series from linaro's patchwork[1].

you depend on 3 series. Much easier is to create a branch and push it somewhere.

Quoted 7 lines
Quoted 6 lines

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them.

Quoted 5 lines

Okay my understanding of a patch being bisectable was if the build does not break after applying a patch. Please note that on top of my series, you will need to define CONFIG_FWU_NUM_BANKS and CONFIG_FWU_NUM_IMAGES_PER_BANK. I did not define that on purpose since each platform would have it's own specific values.

Quoted 3 lines

Even if this is true you can't use in any patch a macro/value which is not defined and it is not clear what it is. I apply patch 1 and review patch 1 and not looking at patch 8 to get what that values are for.

Quoted 8 lines

My point was that I have not defined these values at all. These will have to be defined on a per platform level. But if you insist, I will define default values, but in my opinion it is better to get a build failure and then set correct values, rather than the build going through with some values which are incorrect for the platform.

If you apply the patches as they are, they should not result in any build failures.

This code should be enabled for at least one platform. It means I expect you define that values at least for one platform which you use for testing.
I don't agree that it is better to get build error. Feature will be disabled by default for other platforms and when developer enables it should check all these settings before enablement happens.

Thanks,
Michal

#21 lore raw

Re: [PATCH v4 01/11] FWU: Add FWU metadata structure and driver for accessing metadata

Quoted 803 lines · Michal Simek
Quoted 799 lines · Sughosh Ganu
Quoted 788 lines · Michal Simek
Quoted 763 lines · Sughosh Ganu
Quoted 762 lines · Michal Simek
Quoted 757 lines · Sughosh Ganu
Quoted 743 lines · Michal Simek

po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 712 lines

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, which is stored on a dedicated partition. Add the metadata structure, and a driver model uclass which provides functions to access the metadata. These are generic API's, and implementations can be added based on parameters like how the metadata partition is accessed and what type of storage device houses the metadata.

A device tree node fwu-mdata has been added, which is used for pointing to the storage device which contains the FWU metadata. The fwu-mdata node is u-boot specific, and can be added the platform's u-boot dtsi file.

---

Changes since V3:
* Move the FWU metadata access to driver model
* Get the storage device containing the metadata from a device tree property instead of a platform helper function

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi | 7 +
.../firmware/fwu-mdata.txt | 18 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/fwu-mdata/Kconfig | 7 +
drivers/fwu-mdata/Makefile | 6 +
drivers/fwu-mdata/fwu-mdata-uclass.c | 434 ++++++++++++++++++
include/dm/uclass-id.h | 1 +
include/fwu.h | 51 ++
include/fwu_mdata.h | 67 +++
10 files changed, 594 insertions(+)
create mode 100644 doc/device-tree-bindings/firmware/fwu-mdata.txt create mode 100644 drivers/fwu-mdata/Kconfig
create mode 100644 drivers/fwu-mdata/Makefile
create mode 100644 drivers/fwu-mdata/fwu-mdata-uclass.c create mode 100644 include/fwu.h
create mode 100644 include/fwu_mdata.h

arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi +7 -0
index 06ef3a4095..3bec6107f7 100644
@@ -4,3 +4,10 @@
     */
 
    #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
+};
doc/device-tree-bindings/firmware/fwu-mdata.txt new file +18 -0
new file mode 100644
index 0000000000..c766b595ef
@@ -0,0 +1,18 @@
+FWU Metadata Access Devicetree Binding
+
+The FWU Multi Bank Update feature uses a metadata structure, stored on
+a separate partition for keeping information on the set of updatable
+images. The device tree node provides information on the storage
+device that contains the FWU metadata.
+
+Required properties :
+
+- compatible : "u-boot,fwu-mdata";
+- fwu-mdata-store : should point to the storage device which contains
+                   the FWU metadata partition.
+
+Example :
+       fwu-mdata {
+               compatible = "u-boot,fwu-mdata";
+               fwu-mdata-store = <&sdmmc1>;
+       };
drivers/Kconfig +2 -0
index b26ca8cf70..adc6079ecf 100644
@@ -42,6 +42,8 @@ source "drivers/firmware/Kconfig"
 
    source "drivers/fpga/Kconfig"
 
+source "drivers/fwu-mdata/Kconfig"
+
    source "drivers/gpio/Kconfig"
 
    source "drivers/hwspinlock/Kconfig"
drivers/Makefile +1 -0
index 4e7cf28440..56f0f04874 100644
@@ -81,6 +81,7 @@ obj-y += cache/
    obj-$(CONFIG_CPU) += cpu/
    obj-y += crypto/
    obj-$(CONFIG_FASTBOOT) += fastboot/
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata/
    obj-y += misc/
    obj-$(CONFIG_MMC) += mmc/
    obj-$(CONFIG_NVME) += nvme/
drivers/fwu-mdata/Kconfig new file +7 -0
new file mode 100644
index 0000000000..d6a21c8e19
@@ -0,0 +1,7 @@
+config DM_FWU_MDATA
+       bool "Driver support for accessing FWU Metadata"
+       depends on DM
+       help
+         Enable support for accessing FWU Metadata partitions. The
+         FWU Metadata partitions reside on the same storage device
+         which contains the other FWU updatable firmware images.
drivers/fwu-mdata/Makefile new file +6 -0
new file mode 100644
index 0000000000..7fec7171f4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
drivers/fwu-mdata/fwu-mdata-uclass.c new file +434 -0
new file mode 100644
index 0000000000..64b3051ecf
@@ -0,0 +1,434 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <u-boot/crc.h>
+
+#define IMAGE_ACCEPT_SET       BIT(0)
+#define IMAGE_ACCEPT_CLEAR     BIT(1)
+
+static int fwu_get_dev_ops(struct udevice **dev,
+                          const struct fwu_mdata_ops **ops)
+{
+       int ret;
+
+       ret = uclass_get_device(UCLASS_FWU_MDATA, 0, dev);
+       if (ret) {
+               log_debug("Cannot find fwu device\n");
+               return ret;
+       }
+
+       if ((*ops = device_get_ops(*dev)) == NULL) {
+               log_debug("Cannot get fwu device ops\n");
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_verify_mdata() - Verify the FWU metadata
+ * @mdata: FWU metadata structure
+ * @pri_part: FWU metadata partition is primary or secondary
+ *
+ * Verify the FWU metadata by computing the CRC32 for the metadata
+ * structure and comparing it against the CRC32 value stored as part
+ * of the structure.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part)
+{
+       u32 calc_crc32;
+       void *buf;
+
+       buf = &mdata->version;
+       calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       if (calc_crc32 != mdata->crc32) {
+               log_err("crc32 check failed for %s FWU metadata partition\n",
+                       pri_part ? "primary" : "secondary");
+               return -1;
+       }
+
+       return 0;
+}
+
+/**
+ * fwu_get_active_index() - Get active_index from the FWU metadata
+ * @active_idx: active_index value to be read
+ *
+ * Read the active_index field from the FWU metadata and place it in
+ * the variable pointed to be the function argument.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_active_index(u32 *active_idx)
+{
+       int ret;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_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) {
+               log_err("Active index value read is incorrect\n");
+               ret = -EINVAL;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_update_active_index() - Update active_index from the FWU metadata
+ * @active_idx: active_index value to be updated
+ *
+ * Update the active_index field in the FWU metadata
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_active_index(u32 active_idx)
+{
+       int ret;
+       void *buf;
+       struct fwu_mdata *mdata = NULL;
+
+       if (active_idx > CONFIG_FWU_NUM_BANKS - 1) {
+               log_err("Active index value to be updated is incorrect\n");
+               return -1;
+       }
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Update the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       mdata->previous_active_index = mdata->active_index;
+       mdata->active_index = active_idx;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_get_image_alt_num() - Get the dfu alt number to be used for capsule update
+ * @image_type_id: image guid as passed in the capsule
+ * @update_bank: Bank to which the update is to be made
+ * @alt_num: The alt_num for the image
+ *
+ * Based on the guid value passed in the capsule, along with the bank to which the
+ * image needs to be updated, get the dfu alt number which will be used for the
+ * capsule update
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num)
+{
+       int ret;
+       const struct fwu_mdata_ops *ops = NULL;
+       struct udevice *dev = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_image_alt_num) {
+               log_err("get_image_alt_num() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_image_alt_num(dev, image_type_id,
+                                     update_bank, alt_num);
+}
+
+/**
+ * fwu_mdata_check() - Check if the FWU metadata is valid
+ *
+ * Validate both copies of the FWU metadata. If one of the copies
+ * has gone bad, restore it from the other bad copy.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_mdata_check(void)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->mdata_check) {
+               log_err("mdata_check() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->mdata_check(dev);
+}
+
+/**
+ * fwu_revert_boot_index() - Revert the active index in the FWU metadata
+ *
+ * Revert the active_index value in the FWU metadata, by swapping the values
+ * of active_index and previous_active_index in both copies of the
+ * FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_revert_boot_index(void)
+{
+       int ret;
+       void *buf;
+       u32 cur_active_index;
+       struct fwu_mdata *mdata = NULL;
+
+       ret = fwu_get_mdata(&mdata);
+       if (ret < 0) {
+               log_err("Unable to get valid FWU metadata\n");
+               goto out;
+       }
+
+       /*
+        * Swap the active index and previous_active_index fields
+        * in the FWU metadata
+        */
+       cur_active_index = mdata->active_index;
+       mdata->active_index = mdata->previous_active_index;
+       mdata->previous_active_index = cur_active_index;
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       buf = &mdata->version;
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       /*
+        * Now write this updated FWU metadata to both the
+        * FWU metadata partitions
+        */
+       ret = fwu_update_mdata(mdata);
+       if (ret < 0) {
+               log_err("Failed to update FWU metadata partitions\n");
+               ret = -EIO;
+       }
+
+out:
+       free(mdata);
+
+       return ret;
+}
+
+/**
+ * fwu_set_clear_image_accept() - Set or Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               set or cleared
+ * @bank: Bank of which the image's Accept bit is to be set or cleared
+ * @action: Action which specifies whether image's Accept bit is to be set or
+ *          cleared
+ *
+ * Set/Clear the accepted bit for the image specified by the img_guid parameter.
+ * This indicates acceptance or rejection of image for subsequent boots by some
+ * governing component like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+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");
+               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;
+
+                       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);
+
+       return ret;
+}
+
+/**
+ * fwu_accept_image() - Set the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be set
+ *
+ * Set the accepted bit for the image specified by the img_guid parameter. This
+ * indicates acceptance of image for subsequent boots by some governing component
+ * like OS(or firmware).
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_SET);
+}
+
+/**
+ * fwu_clear_accept_image() - Clear the Acceptance bit for the image
+ * @img_type_id: Guid of the image type for which the accepted bit is to be
+ *               cleared
+ * @bank: Bank of which the image's Accept bit is to be cleared
+ *
+ * Clear the accepted bit for the image type specified by the img_type_id parameter.
+ * This function is called after the image has been updated. The accepted bit is
+ * cleared to be set subsequently after passing the image acceptance criteria, by
+ * either the OS(or firmware)
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank)
+{
+       return fwu_set_clear_image_accept(img_type_id, bank,
+                                         IMAGE_ACCEPT_CLEAR);
+}
+
+/**
+ * fwu_get_mdata() - Get a FWU metadata copy
+ * @mdata: Copy of the FWU metadata
+ *
+ * Get a valid copy of the FWU metadata.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_get_mdata(struct fwu_mdata **mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->get_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->get_mdata(dev, mdata);
+}
+
+/**
+ * fwu_update_mdata() - Update the FWU metadata
+ * @mdata: Copy of the FWU metadata
+ *
+ * Update the FWU metadata structure by writing to the
+ * FWU metadata partitions.
+ *
+ * Return: 0 if OK, -ve on error
+ *
+ */
+int fwu_update_mdata(struct fwu_mdata *mdata)
+{
+       int ret;
+       struct udevice *dev = NULL;
+       const struct fwu_mdata_ops *ops = NULL;
+
+       ret = fwu_get_dev_ops(&dev, &ops);
+       if (ret)
+               return ret;
+
+       if (!ops->update_mdata) {
+               log_err("get_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->update_mdata(dev, mdata);
+}
+
+UCLASS_DRIVER(fwu_mdata) = {
+       .id             = UCLASS_FWU_MDATA,
+       .name           = "fwu-mdata",
+};
include/dm/uclass-id.h +1 -0
index 0e26e1d138..d0ab1c9235 100644
@@ -54,6 +54,7 @@ enum uclass_id {
           UCLASS_ETH_PHY,         /* Ethernet PHY device */
           UCLASS_FIRMWARE,        /* Firmware */
           UCLASS_FS_FIRMWARE_LOADER,              /* Generic loader */
+       UCLASS_FWU_MDATA,       /* FWU Metadata Access */
           UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
           UCLASS_HASH,            /* Hash device */
           UCLASS_HWSPINLOCK,      /* Hardware semaphores */
include/fwu.h new file +51 -0
new file mode 100644
index 0000000000..5a99c579fc
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_H_
+#define _FWU_H_
+
+#include <blk.h>
+#include <efi.h>
+
+#include <linux/types.h>
+
+struct fwu_mdata;
+struct udevice;
+
+/**
+ * @get_image_alt_num: get the alt number to be used for the image
+ * @mdata_check: check the validity of the FWU metadata partitions
+ * @get_mdata() - Get a FWU metadata copy
+ * @update_mdata() - Update the FWU metadata copy
+ */
+struct fwu_mdata_ops {
+       int (*get_image_alt_num)(struct udevice *dev, efi_guid_t image_type_id,
+                                u32 update_bank, int *alt_num);
+
+       int (*mdata_check)(struct udevice *dev);
+
+       int (*get_mdata)(struct udevice *dev, struct fwu_mdata **mdata);
+
+       int (*update_mdata)(struct udevice *dev, struct fwu_mdata *mdata);
+};
+
+#define FWU_MDATA_VERSION      0x1
+
+#define FWU_MDATA_GUID \
+       EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
+                0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
+
+int fwu_get_mdata(struct fwu_mdata **mdata);
+int fwu_update_mdata(struct fwu_mdata *mdata);
+int fwu_get_active_index(u32 *active_idx);
+int fwu_update_active_index(u32 active_idx);
+int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
+                         int *alt_num);
+int fwu_mdata_check(void);
+int fwu_revert_boot_index(void);
+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);
+
+#endif /* _FWU_H_ */
include/fwu_mdata.h new file +67 -1
new file mode 100644
index 0000000000..701efbba03
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2022, Linaro Limited
+ */
+
+#if !defined _FWU_MDATA_H_
+#define _FWU_MDATA_H_
+
+#include <efi.h>
+
+/**
+ * struct fwu_image_bank_info - firmware image information
+ * @image_uuid: Guid value of the image in this bank
+ * @accepted: Acceptance status of the image
+ * @reserved: Reserved
+ *
+ * The structure contains image specific fields which are
+ * used to identify the image and to specify the image's
+ * acceptance status
+ */
+struct fwu_image_bank_info {
+       efi_guid_t  image_uuid;
+       uint32_t accepted;
+       uint32_t reserved;
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_image_entry - information for a particular type of image
+ * @image_type_uuid: Guid value for identifying the image type
+ * @location_uuid: Guid of the storage volume where the image is located
+ * @img_bank_info: Array containing properties of images
+ *
+ * This structure contains information on various types of updatable
+ * firmware images. Each image type then contains an array of image
+ * information per bank.
+ */
+struct fwu_image_entry {
+       efi_guid_t image_type_uuid;
+       efi_guid_t location_uuid;
+       struct fwu_image_bank_info img_bank_info[CONFIG_FWU_NUM_BANKS];
+} __attribute__((__packed__));
+
+/**
+ * struct fwu_mdata - FWU metadata structure for multi-bank updates
+ * @crc32: crc32 value for the FWU metadata
+ * @version: FWU metadata version
+ * @active_index: Index of the bank currently used for booting images
+ * @previous_active_inde: Index of the bank used before the current bank
+ *                        being used for booting
+ * @img_entry: Array of information on various firmware images that can
+ *             be updated
+ *
+ * This structure is used to store all the needed information for performing
+ * multi bank updates on the platform. This contains info on the bank being
+ * used to boot along with the information needed for identification of
+ * individual images
+ */
+struct fwu_mdata {
+       uint32_t crc32;
+       uint32_t version;
+       uint32_t active_index;
+       uint32_t previous_active_index;
+
+       struct fwu_image_entry img_entry[CONFIG_FWU_NUM_IMAGES_PER_BANK];
+} __attribute__((__packed__));
+
+#endif /* _FWU_MDATA_H_ */
--
Quoted 2 lines

2.17.1

One more thing. run kernel-doc to validate your description. [u-boot](eeee)$ ./scripts/kernel-doc -v -man drivers/fwu-mdata/fwu-mdata-uclass.c 1>/dev/null drivers/fwu-mdata/fwu-mdata-uclass.c:41: info: Scanning doc for fwu_verify_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:70: info: Scanning doc for fwu_get_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:107: info: Scanning doc for fwu_update_active_index
drivers/fwu-mdata/fwu-mdata-uclass.c:164: info: Scanning doc for fwu_get_image_alt_num
drivers/fwu-mdata/fwu-mdata-uclass.c:197: info: Scanning doc for fwu_mdata_check drivers/fwu-mdata/fwu-mdata-uclass.c:202: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:224: info: Scanning doc for fwu_revert_boot_index
drivers/fwu-mdata/fwu-mdata-uclass.c:230: warning: contents before sections drivers/fwu-mdata/fwu-mdata-uclass.c:279: info: Scanning doc for fwu_set_clear_image_accept drivers/fwu-mdata/fwu-mdata-uclass.c:338: info: Scanning doc for fwu_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:357: info: Scanning doc for fwu_clear_accept_image
drivers/fwu-mdata/fwu-mdata-uclass.c:377: info: Scanning doc for fwu_get_mdata drivers/fwu-mdata/fwu-mdata-uclass.c:404: info: Scanning doc for fwu_update_mdata
2 warnings

Quoted 5 lines

when I run buildman over this series it is visible that it is not bisectable at all.
CONFIG_FWU_NUM_BANKS is defined in this patch but the symbol is added much later.
Please make sure every single patch is bisectable.

But how is this driver getting built in the first place -- the driver is not enabling the config symbol which would result in the code getting built. The idea is to add support for the driver and the feature, and enable the functionality in the final patch. I have added each patch separately and built for the qemu arm64 platform without any issues.

Quoted 5 lines
Quoted 3 lines

first of all I wasn't able to apply all patches because this series is not there.
https://patchwork.ozlabs.org/project/uboot/list/?series=281549
(better would be to use links to lore which are much easier to download)

You can download the mkeficapsule series from linaro's patchwork[1].

you depend on 3 series. Much easier is to create a branch and push it somewhere.

Quoted 7 lines
Quoted 6 lines

That's why 2 patches weren't applied.

And I just enabled configs which land in the tree for zynqmp virt and rebase this patch to be the first one. That's why all the time all existing Kconfigs were enabled when buildman build them.

Quoted 5 lines

Okay my understanding of a patch being bisectable was if the build does not break after applying a patch. Please note that on top of my series, you will need to define CONFIG_FWU_NUM_BANKS and CONFIG_FWU_NUM_IMAGES_PER_BANK. I did not define that on purpose since each platform would have it's own specific values.

Quoted 3 lines

Even if this is true you can't use in any patch a macro/value which is not defined and it is not clear what it is. I apply patch 1 and review patch 1 and not looking at patch 8 to get what that values are for.

My point was that I have not defined these values at all. These will have to be defined on a per platform level. But if you insist, I will define default values, but in my opinion it is better to get a build failure and then set correct values, rather than the build going through with some values which are incorrect for the platform.

Quoted 8 lines
Quoted 2 lines

If you apply the patches as they are, they should not result in any build failures.

This code should be enabled for at least one platform. It means I expect you define that values at least for one platform which you use for testing. I don't agree that it is better to get build error. Feature will be disabled by default for other platforms and when developer enables it should check all these settings before enablement happens.

I will leave this to the ST maintainers on whether they want me to define some default values. If they do, I will add a patch doing so.

-sughosh

Quoted 4 lines

Thanks,
Michal

#22 lore raw

PATCH v4 2/11 FWU: Add FWU metadata access driver for GPT partitioned block devices

In reply to Sughosh Ganu

In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, on a separate partition. Add a driver for reading from and writing to the metadata when the updatable images and the metadata are stored on a block device which is formated with GPT based partition scheme.

---

Changes since V3:
* Move the metadata access driver for GPT partitioned block devices under drivers/fwu-mdata/ directory, complying with driver model.
* Move functionality to get the active index under the common function instead of the GPT block device specific driver.

 drivers/fwu-mdata/Kconfig             |   9 +
 drivers/fwu-mdata/Makefile            |   1 +
 drivers/fwu-mdata/fwu_mdata_gpt_blk.c | 501 ++++++++++++++++++++++++++
 include/fwu.h                         |   2 +
 4 files changed, 513 insertions(+)
 create mode 100644 drivers/fwu-mdata/fwu_mdata_gpt_blk.c
drivers/fwu-mdata/Kconfig +9 -0
index d6a21c8e19..d5edef19d6 100644
@@ -5,3 +5,12 @@ config DM_FWU_MDATA
 	  Enable support for accessing FWU Metadata partitions. The
 	  FWU Metadata partitions reside on the same storage device
 	  which contains the other FWU updatable firmware images.
+
+config FWU_MDATA_GPT_BLK
+	bool "FWU Metadata access for GPT partitioned Block devices"
+	select PARTITION_TYPE_GUID
+	select PARTITION_UUIDS
+	depends on DM && HAVE_BLOCK_DEVICE && EFI_PARTITION
+	help
+	  Enable support for accessing FWU Metadata on GPT partitioned
+	  block devices.
drivers/fwu-mdata/Makefile +1 -0
index 7fec7171f4..12a5b4fe04 100644
@@ -4,3 +4,4 @@
 #
 
 obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
+obj-$(CONFIG_FWU_MDATA_GPT_BLK) += fwu_mdata_gpt_blk.o
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +501 -0

501 lines not on this page · raw message

include/fwu.h +2 -0
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
 			  int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
Signature · 1 line
2.17.1
#23 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

Hi Sughosh,

While porting mdata-sf driver, I'm confusing on the devicetree usage.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 441

Quoted 13 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +13 -0
+int fwu_get_mdata_device(struct udevice **mdata_dev)
+{
+       u32 phandle;
+       int ret, size;
+       struct udevice *dev, *child;
+       ofnode fwu_mdata_node;
+       const fdt32_t *phandle_p = NULL;
+
+       fwu_mdata_node = ofnode_path("/fwu-mdata");
+       if (!ofnode_valid(fwu_mdata_node)) {
+               log_err("fwu-node not found\n");
+               return -ENOENT;
+       }

So this seems to get the udevice from path, but I think fwu-mdata node has "u-boot,fwu-mdata" compatible property, in that case probe function already gets the node. Why would you search it again by path?

Quoted 10 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +10 -0
+
+       phandle_p = ofnode_get_property(fwu_mdata_node, "fwu-mdata-store",
+                                       &size);
+       if (!phandle_p) {
+               log_err("fwu-mdata-store property not found\n");
+               return -ENOENT;
+       }
+
+       phandle = fdt32_to_cpu(*phandle_p);
+

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 461

Quoted 10 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +10 -0
+       ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
+                                         &dev);
+       if (ret)
+               return ret;
+
+       ret = -ENODEV;
+       for (device_find_first_child(dev, &child); child;
+            device_find_next_child(&child)) {
+               if (device_get_uclass_id(child) == UCLASS_BLK) {
+                       *mdata_dev = child;

I thought that the blk device node directly passed by the "fwu-mdata-store" property. Would we need to search it from children nodes?

BTW, if we can use the devicetree, can we define the number of banks and the number of images can be described too?
Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too.
(But it maybe different from this discussion)

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

#24 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

hi Masami,

Quoted 17 lines · Masami Hiramatsu

Hi Sughosh,

While porting mdata-sf driver, I'm confusing on the devicetree usage.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:

Quoted 9 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +9 -0
+int fwu_get_mdata_device(struct udevice **mdata_dev)
+{
+       u32 phandle;
+       int ret, size;
+       struct udevice *dev, *child;
+       ofnode fwu_mdata_node;
+       const fdt32_t *phandle_p = NULL;
+
+       fwu_mdata_node = ofnode_path("/fwu-mdata");
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 441

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       if (!ofnode_valid(fwu_mdata_node)) {
+               log_err("fwu-node not found\n");
+               return -ENOENT;
+       }

So this seems to get the udevice from path, but I think fwu-mdata node has "u-boot,fwu-mdata" compatible property, in that case probe function already gets the node. Why would you search it again by path?

Okay. Is there some other api that I can use which is faster than the approach currently taken? If so, do let me know.

Quoted 17 lines
Quoted 17 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +16 -0
+
+       phandle_p = ofnode_get_property(fwu_mdata_node, "fwu-mdata-store",
+                                       &size);
+       if (!phandle_p) {
+               log_err("fwu-mdata-store property not found\n");
+               return -ENOENT;
+       }
+
+       phandle = fdt32_to_cpu(*phandle_p);
+
+       ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
+                                         &dev);
+       if (ret)
+               return ret;
+
+       ret = -ENODEV;
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 461

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       for (device_find_first_child(dev, &child); child;
+            device_find_next_child(&child)) {
+               if (device_get_uclass_id(child) == UCLASS_BLK) {
+                       *mdata_dev = child;

I thought that the blk device node directly passed by the "fwu-mdata-store" property. Would we need to search it from children nodes?

What the device tree is pointing to is the device like the mmc, which is described through a device tree node. The block device is the child of this device, which is not described in the device tree. The driver finally needs the block device.

Quoted 3 lines

BTW, if we can use the devicetree, can we define the number of banks and the number of images can be described too?

It is much easier to handle if these values are defined through Kconfig symbols. That way, this gets directly included in the config file, and can be used directly in the fwu_mdata.h metadata structure.

Quoted 3 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

#25 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

Hi Sughosh,

2022年2月9日(水) 18:03 Sughosh Ganu <[email protected]>:

Quoted 31 lines

hi Masami,

Quoted 17 lines · Masami Hiramatsu

Hi Sughosh,

While porting mdata-sf driver, I'm confusing on the devicetree usage.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:

Quoted 9 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +9 -0
+int fwu_get_mdata_device(struct udevice **mdata_dev)
+{
+       u32 phandle;
+       int ret, size;
+       struct udevice *dev, *child;
+       ofnode fwu_mdata_node;
+       const fdt32_t *phandle_p = NULL;
+
+       fwu_mdata_node = ofnode_path("/fwu-mdata");
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 441

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       if (!ofnode_valid(fwu_mdata_node)) {
+               log_err("fwu-node not found\n");
+               return -ENOENT;
+       }

So this seems to get the udevice from path, but I think fwu-mdata node has "u-boot,fwu-mdata" compatible property, in that case probe function already gets the node. Why would you search it again by path?

Quoted 2 lines

Okay. Is there some other api that I can use which is faster than the approach currently taken? If so, do let me know.

I thought the ofnode which has "u-boot,fwu-mdata" compatible property is passed to fwu_mdata_gpt_blk_probe(), and that is , doesn't it?

If so, as you show in the example,

       fwu-mdata {
               compatible = "u-boot,fwu-mdata";
               fwu-mdata-store = <&sdmmc1>;
       };

you already got the "/fwu-mdata" node. You don't need any API to find that node because you already has that. That is what I meant.

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +16 -0
+
+       phandle_p = ofnode_get_property(fwu_mdata_node, "fwu-mdata-store",
+                                       &size);
+       if (!phandle_p) {
+               log_err("fwu-mdata-store property not found\n");
+               return -ENOENT;
+       }
+
+       phandle = fdt32_to_cpu(*phandle_p);
+
+       ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
+                                         &dev);
+       if (ret)
+               return ret;
+
+       ret = -ENODEV;
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 461

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       for (device_find_first_child(dev, &child); child;
+            device_find_next_child(&child)) {
+               if (device_get_uclass_id(child) == UCLASS_BLK) {
+                       *mdata_dev = child;

I thought that the blk device node directly passed by the "fwu-mdata-store" property. Would we need to search it from children nodes?

Quoted 4 lines

What the device tree is pointing to is the device like the mmc, which is described through a device tree node. The block device is the child of this device, which is not described in the device tree. The driver finally needs the block device.

Ah, OK. Can't we specify the block device node to "fwu-mdata-store" directly?

Quoted 6 lines
Quoted 2 lines

BTW, if we can use the devicetree, can we define the number of banks and the number of images can be described too?

It is much easier to handle if these values are defined through Kconfig symbols. That way, this gets directly included in the config file, and can be used directly in the fwu_mdata.h metadata structure.

Yes, it is easier to implement. And that means the firmware bank configuration is embedded in the firmware binary (code) instead of the configuration data (devicetree).
For the same reason I think dfu_alt_info (or the basement info like device and offsets) should be in the devicetree. Current implementation is very fragile (because dfu_alt_info can be changed by the user) or fixed in the code (like stm32, it generates the dfu_alt_info by the platform driver.)

Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

That is my impression felt from porting AB update on the DeveloperBox platform.

Thank you,

Quoted 68 lines

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

--
Masami Hiramatsu

#26 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

hi Masami,

Quoted 41 lines · Masami Hiramatsu

Hi Sughosh,

2022年2月9日(水) 18:03 Sughosh Ganu <[email protected]>:

Quoted 31 lines

hi Masami,

Quoted 17 lines · Masami Hiramatsu

Hi Sughosh,

While porting mdata-sf driver, I'm confusing on the devicetree usage.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:

Quoted 9 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +9 -0
+int fwu_get_mdata_device(struct udevice **mdata_dev)
+{
+       u32 phandle;
+       int ret, size;
+       struct udevice *dev, *child;
+       ofnode fwu_mdata_node;
+       const fdt32_t *phandle_p = NULL;
+
+       fwu_mdata_node = ofnode_path("/fwu-mdata");
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 441

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       if (!ofnode_valid(fwu_mdata_node)) {
+               log_err("fwu-node not found\n");
+               return -ENOENT;
+       }

So this seems to get the udevice from path, but I think fwu-mdata node has "u-boot,fwu-mdata" compatible property, in that case probe function already gets the node. Why would you search it again by path?

Quoted 2 lines

Okay. Is there some other api that I can use which is faster than the approach currently taken? If so, do let me know.

I thought the ofnode which has "u-boot,fwu-mdata" compatible property is passed to fwu_mdata_gpt_blk_probe(), and that is , doesn't it?

Quoted 8 lines

If so, as you show in the example,

       fwu-mdata {
               compatible = "u-boot,fwu-mdata";
               fwu-mdata-store = <&sdmmc1>;
       };

you already got the "/fwu-mdata" node. You don't need any API to find that node because you already has that. That is what I meant.

The probe function gets passed the corresponding udevice structure. This structure contains node_ member, which should correspond to the device node. I will try to use this member instead of searching for the node.

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 11 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +10 -0
+
+       phandle_p = ofnode_get_property(fwu_mdata_node, "fwu-mdata-store",
+                                       &size);
+       if (!phandle_p) {
+               log_err("fwu-mdata-store property not found\n");
+               return -ENOENT;
+       }
+
+       phandle = fdt32_to_cpu(*phandle_p);
+

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 461

Quoted 10 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +10 -0
+       ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
+                                         &dev);
+       if (ret)
+               return ret;
+
+       ret = -ENODEV;
+       for (device_find_first_child(dev, &child); child;
+            device_find_next_child(&child)) {
+               if (device_get_uclass_id(child) == UCLASS_BLK) {
+                       *mdata_dev = child;

I thought that the blk device node directly passed by the "fwu-mdata-store" property. Would we need to search it from children nodes?

Quoted 7 lines
Quoted 4 lines

What the device tree is pointing to is the device like the mmc, which is described through a device tree node. The block device is the child of this device, which is not described in the device tree. The driver finally needs the block device.

Ah, OK. Can't we specify the block device node to "fwu-mdata-store" directly?

I don't think so, since the fwu-mdata-store is pointing to one of the nodes in the device tree -- the block device does not show up as node on the device tree.

Quoted 27 lines
Quoted 7 lines
Quoted 3 lines

BTW, if we can use the devicetree, can we define the number of banks and the number of images can be described too?

It is much easier to handle if these values are defined through Kconfig symbols. That way, this gets directly included in the config file, and can be used directly in the fwu_mdata.h metadata structure.

Yes, it is easier to implement. And that means the firmware bank configuration is embedded in the firmware binary (code) instead of the configuration data (devicetree).
For the same reason I think dfu_alt_info (or the basement info like device and offsets) should be in the devicetree. Current implementation is very fragile (because dfu_alt_info can be changed by the user) or fixed in the code (like stm32, it generates the dfu_alt_info by the platform driver.)

Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

Quoted 7 lines

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

I get your point. But I think generating the dfu_alt_info at runtime, like how it is done for the ST platforms is in general a better method, as against using a static variable defined in the device tree. With runtime generation of the variable, the same code can be used on multiple platforms and can be generated at runtime -- I feel that is better than defining the variable in every platform's device tree. Btw, there is also provision to define the variable(or part of it) statically through Kconfig variables. As against your concern about the feature using multiple methods for stating information, it is indeed valid. But I guess we can have documentation explaining how each of that information needs to be defined. Thanks.

-sughosh

Quoted 78 lines

That is my impression felt from porting AB update on the DeveloperBox platform.

Thank you,

Quoted 68 lines

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

--
Masami Hiramatsu

#27 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

Hi Sughosh,

2022年2月10日(木) 3:40 Sughosh Ganu <[email protected]>:

Quoted 52 lines

hi Masami,

Quoted 47 lines · Masami Hiramatsu

Hi Sughosh,

2022年2月9日(水) 18:03 Sughosh Ganu <[email protected]>:

Quoted 31 lines

hi Masami,

Quoted 17 lines · Masami Hiramatsu

Hi Sughosh,

While porting mdata-sf driver, I'm confusing on the devicetree usage.

2022年2月8日(火) 3:21 Sughosh Ganu <[email protected]>:

Quoted 9 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +9 -0
+int fwu_get_mdata_device(struct udevice **mdata_dev)
+{
+       u32 phandle;
+       int ret, size;
+       struct udevice *dev, *child;
+       ofnode fwu_mdata_node;
+       const fdt32_t *phandle_p = NULL;
+
+       fwu_mdata_node = ofnode_path("/fwu-mdata");
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 441

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       if (!ofnode_valid(fwu_mdata_node)) {
+               log_err("fwu-node not found\n");
+               return -ENOENT;
+       }

So this seems to get the udevice from path, but I think fwu-mdata node has "u-boot,fwu-mdata" compatible property, in that case probe function already gets the node. Why would you search it again by path?

Quoted 2 lines

Okay. Is there some other api that I can use which is faster than the approach currently taken? If so, do let me know.

I thought the ofnode which has "u-boot,fwu-mdata" compatible property is passed to fwu_mdata_gpt_blk_probe(), and that is , doesn't it?

If so, as you show in the example,

       fwu-mdata {
               compatible = "u-boot,fwu-mdata";
               fwu-mdata-store = <&sdmmc1>;
       };
Quoted 7 lines
Quoted 2 lines

you already got the "/fwu-mdata" node. You don't need any API to find that node because you already has that. That is what I meant.

The probe function gets passed the corresponding udevice structure. This structure contains node_ member, which should correspond to the device node. I will try to use this member instead of searching for the node.

Yeah, you can use "dev_ofnode()".

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +16 -0
+
+       phandle_p = ofnode_get_property(fwu_mdata_node, "fwu-mdata-store",
+                                       &size);
+       if (!phandle_p) {
+               log_err("fwu-mdata-store property not found\n");
+               return -ENOENT;
+       }
+
+       phandle = fdt32_to_cpu(*phandle_p);
+
+       ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
+                                         &dev);
+       if (ret)
+               return ret;
+
+       ret = -ENODEV;
Quoted 8 lines

drivers/fwu-mdata/fwu_mdata_gpt_blk.c line 461

Quoted 4 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +4 -0
+       for (device_find_first_child(dev, &child); child;
+            device_find_next_child(&child)) {
+               if (device_get_uclass_id(child) == UCLASS_BLK) {
+                       *mdata_dev = child;

I thought that the blk device node directly passed by the "fwu-mdata-store" property. Would we need to search it from children nodes?

What the device tree is pointing to is the device like the mmc, which is described through a device tree node. The block device is the child of this device, which is not described in the device tree. The driver finally needs the block device.

Quoted 6 lines
Quoted 2 lines

Ah, OK. Can't we specify the block device node to "fwu-mdata-store" directly?

I don't think so, since the fwu-mdata-store is pointing to one of the nodes in the device tree -- the block device does not show up as node on the device tree.

Ah, I got it. Indeed. In my case spi-nor device will be on the devicetree, but mmc media is not. Sorry about that.

In that case, does fwu-mdata-store refer the controller node? If there are several blk devices under that (I'm not sure it can be) mdata must be in the first device (maybe decided by scanning order?). I think this should be documented for who ports AB-update on another platform.

Quoted 34 lines
Quoted 26 lines
Quoted 6 lines
Quoted 2 lines

BTW, if we can use the devicetree, can we define the number of banks and the number of images can be described too?

It is much easier to handle if these values are defined through Kconfig symbols. That way, this gets directly included in the config file, and can be used directly in the fwu_mdata.h metadata structure.

Yes, it is easier to implement. And that means the firmware bank configuration is embedded in the firmware binary (code) instead of the configuration data (devicetree).
For the same reason I think dfu_alt_info (or the basement info like device and offsets) should be in the devicetree. Current implementation is very fragile (because dfu_alt_info can be changed by the user) or fixed in the code (like stm32, it generates the dfu_alt_info by the platform driver.)

Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

Quoted 7 lines

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

Quoted 3 lines

I get your point. But I think generating the dfu_alt_info at runtime, like how it is done for the ST platforms is in general a better method, as against using a static variable defined in the device tree.

Yeah, the GPT based one is able to store this information on the GPT, and it must be a primary definition.

Quoted 3 lines

With runtime generation of the variable, the same code can be used on multiple platforms and can be generated at runtime -- I feel that is better than defining the variable in every platform's device tree.

I don't agree this point at least for non-GPT devices, since the firmware storage layout depends on the platform hardware configuration statically in such cases.
Of course if the device uses GPT to store the firmware, we have to follow the GPT layout and FWU Metadata to find the corresponding firmware partition.

Quoted 5 lines

Btw, there is also provision to define the variable(or part of it) statically through Kconfig variables. As against your concern about the feature using multiple methods for stating information, it is indeed valid. But I guess we can have documentation explaining how each of that information needs to be defined. Thanks.

Yeah, even using GPT, we need to set correct UUID to the FWU metadata, and the metadata depends on Kconfig if we keep putting the #of images-per-bank and the #of banks in the Kconfig, and storage (controller) is defined in the devicetree.

And I still feel this "chain of definitions" seems a bit fragile. This fragile comes from the FWU metadata is not enough self-described (it has no the #of images-per-bank and the #of banks, without this information we can not decode FWU metadata itself.)
Anyway, if we can define the #of images-per-bank and the #of banks in the devicetree, we don't need to change the binary but just changing the devicetree for the different products which has different firmware layout. I think that is more flexible.

Thank you,

Quoted 81 lines

-sughosh

Quoted 78 lines

That is my impression felt from porting AB update on the DeveloperBox platform.

Thank you,

Quoted 68 lines

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

--
Masami Hiramatsu

Signature · 1 line
Masami Hiramatsu
#28 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

2022年2月10日(木) 10:43 Masami Hiramatsu <[email protected]>:

Quoted 25 lines
Quoted 18 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

Quoted 7 lines

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

Quoted 3 lines

I get your point. But I think generating the dfu_alt_info at runtime, like how it is done for the ST platforms is in general a better method, as against using a static variable defined in the device tree.

Yeah, the GPT based one is able to store this information on the GPT, and it must be a primary definition.

Quoted 7 lines
Quoted 3 lines

With runtime generation of the variable, the same code can be used on multiple platforms and can be generated at runtime -- I feel that is better than defining the variable in every platform's device tree.

I don't agree this point at least for non-GPT devices, since the firmware storage layout depends on the platform hardware configuration statically in such cases.

I changed my mind, it can be solved if we have "uuid" property for each partition in the devicetree. I will explain later.

Quoted 10 lines

Of course if the device uses GPT to store the firmware, we have to follow the GPT layout and FWU Metadata to find the corresponding firmware partition.

Quoted 5 lines

Btw, there is also provision to define the variable(or part of it) statically through Kconfig variables. As against your concern about the feature using multiple methods for stating information, it is indeed valid. But I guess we can have documentation explaining how each of that information needs to be defined. Thanks.

Quoted 3 lines

Yeah, even using GPT, we need to set correct UUID to the FWU metadata, and the metadata depends on Kconfig if we keep putting the #of images-per-bank and the #of banks in the Kconfig, and storage

Sorry, I confused. there are "#of images and #of banks per image".

Quoted 10 lines

(controller) is defined in the devicetree.

And I still feel this "chain of definitions" seems a bit fragile. This fragile comes from the FWU metadata is not enough self-described (it has no the #of images-per-bank and the #of banks, without this information we can not decode FWU metadata itself.)
Anyway, if we can define the #of images-per-bank and the #of banks in the devicetree, we don't need to change the binary but just changing the devicetree for the different products which has different firmware layout. I think that is more flexible.

What I would like to suggest is

/* For GPT BLK backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-gpt";
    fwu-mdata-store = <&mmc1>;
    /* No need to specify the mdata partition, because it finds the
mdata by partition type uuid. */
    banks = <2>;
    images-per-bank = <1>;
};

/* For SF backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-sf";
    fwu-mdata-store = <&spi-flash0>;
    mdata-offsets = <500000, 520000>; /* Or specified by partition label? */
    banks = <6>;
    images-per-bank = <1>;
};

Note that this is only for the metadata, the real firmware layout issue still exists. If we can add "uuid" property for the fixed-partitions node as a additional property, e.g.

spi-flash@0 {

   partitions {
       compatible = "fixed-partitions";
       ...
       uuid = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffgggg";
       ...
       partition@600000 {
           label = "Firmware-Bank0";
           reg = <600000, 400000>;
           uuid = "12345678-aaaa-bbbb-cccc-0123456789ab";
       };
       ...

};
};

Then we can decode the real fwu-mdata and find corresponding partitions, and able to build dfu_alt_info in runtime.

What would you think?

Thank you,

Quoted 82 lines

Thank you,

Quoted 79 lines

-sughosh

Quoted 76 lines

That is my impression felt from porting AB update on the DeveloperBox platform.

Thank you,

Quoted 68 lines

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
drivers/fwu-mdata/fwu_mdata_gpt_blk.c new file +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

--
Masami Hiramatsu

--
Masami Hiramatsu

--
Masami Hiramatsu

#29 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

hi Masami,

Quoted 48 lines · Masami Hiramatsu

2022年2月10日(木) 10:43 Masami Hiramatsu <[email protected]>:

Quoted 25 lines
Quoted 18 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

Quoted 7 lines

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

Quoted 3 lines

I get your point. But I think generating the dfu_alt_info at runtime, like how it is done for the ST platforms is in general a better method, as against using a static variable defined in the device tree.

Yeah, the GPT based one is able to store this information on the GPT, and it must be a primary definition.

Quoted 7 lines
Quoted 3 lines

With runtime generation of the variable, the same code can be used on multiple platforms and can be generated at runtime -- I feel that is better than defining the variable in every platform's device tree.

I don't agree this point at least for non-GPT devices, since the firmware storage layout depends on the platform hardware configuration statically in such cases.

I changed my mind, it can be solved if we have "uuid" property for each partition in the devicetree. I will explain later.

Quoted 10 lines

Of course if the device uses GPT to store the firmware, we have to follow the GPT layout and FWU Metadata to find the corresponding firmware partition.

Quoted 5 lines

Btw, there is also provision to define the variable(or part of it) statically through Kconfig variables. As against your concern about the feature using multiple methods for stating information, it is indeed valid. But I guess we can have documentation explaining how each of that information needs to be defined. Thanks.

Quoted 16 lines
Quoted 3 lines

Yeah, even using GPT, we need to set correct UUID to the FWU metadata, and the metadata depends on Kconfig if we keep putting the #of images-per-bank and the #of banks in the Kconfig, and storage

Sorry, I confused. there are "#of images and #of banks per image".

Quoted 10 lines

(controller) is defined in the devicetree.

And I still feel this "chain of definitions" seems a bit fragile. This fragile comes from the FWU metadata is not enough self-described (it has no the #of images-per-bank and the #of banks, without this information we can not decode FWU metadata itself.)
Anyway, if we can define the #of images-per-bank and the #of banks in the devicetree, we don't need to change the binary but just changing the devicetree for the different products which has different firmware layout. I think that is more flexible.

Do you really feel that using config values for #banks and #images_per_bank is such a bad idea. With the approach that you suggest, we will have to use variable sized arrays, and populate the values in the probe function of every driver. Also, since you are going to use the same fwu_mdata.h header in your other project, will you be able to use variable sized arrays there as well. I feel that we are complicating things without much benefits.

Quoted 13 lines

What I would like to suggest is

/* For GPT BLK backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-gpt";
    fwu-mdata-store = <&mmc1>;
    /* No need to specify the mdata partition, because it finds the
mdata by partition type uuid. */
    banks = <2>;
    images-per-bank = <1>;
};
Quoted 8 lines

/* For SF backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-sf";
    fwu-mdata-store = <&spi-flash0>;
    mdata-offsets = <500000, 520000>; /* Or specified by partition label? */
    banks = <6>;
    images-per-bank = <1>;
};

Looks good overall. Should the mdata-offsets property be instead mdata-parts, where we define the start offset and size of the metadata partitions. Or are we going to figure out the size of the metadata partition through some other mechanism.

Also, do are using a different compatible string for every type of storage device type. Can we not do with a common string instead? I understand your reasoning here, of trying to identify the driver at runtime. Just that I wonder if we are going to build multiple drivers for a platform. Although I do not have a strong opinion on this.

Quoted 20 lines

Note that this is only for the metadata, the real firmware layout issue still exists. If we can add "uuid" property for the fixed-partitions node as a additional property, e.g.

spi-flash@0 {
   partitions {
       compatible = "fixed-partitions";
       ...
       uuid = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffgggg";
       ...
       partition@600000 {
           label = "Firmware-Bank0";
           reg = <600000, 400000>;
           uuid = "12345678-aaaa-bbbb-cccc-0123456789ab";
       };
       ...
   };
};
Quoted 2 lines

Then we can decode the real fwu-mdata and find corresponding partitions, and able to build dfu_alt_info in runtime.

This is a sound idea. With this method, we can still use the GUIDs in the metadata structure, and map those with the partitions using the above nodes. Only question is, are these partition nodes(partition@600000) standard, and are defined for all platforms, for all type of devices(like NOR, Nand). If so, this scheme will work.

-sughosh

Quoted 99 lines

What would you think?

Thank you,

Quoted 82 lines

Thank you,

Quoted 79 lines

-sughosh

Quoted 76 lines

That is my impression felt from porting AB update on the DeveloperBox platform.

Thank you,

Quoted 68 lines

-sughosh

Quoted 65 lines

Thank you,

Quoted 55 lines
(quoted diff) +40 -0
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
+{
+       int ret;
+       struct udevice *mdata_dev = NULL;
+
+       ret = fwu_get_mdata_device(&mdata_dev);
+       if (ret)
+               return ret;
+
+       dev_set_priv(dev, mdata_dev);
+
+       return 0;
+}
+
+static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .get_image_alt_num = fwu_gpt_get_image_alt_num,
+       .mdata_check = fwu_gpt_mdata_check,
+       .get_mdata = fwu_gpt_get_mdata,
+       .update_mdata = fwu_gpt_update_mdata,
+};
+
+static const struct udevice_id fwu_mdata_ids[] = {
+       { .compatible = "u-boot,fwu-mdata" },
 
+       { }
+};
+
+U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
+       .name           = "fwu-mdata-gpt-blk",
+       .id             = UCLASS_FWU_MDATA,
+       .of_match       = fwu_mdata_ids,
+       .ops            = &fwu_gpt_blk_ops,
+       .probe          = fwu_mdata_gpt_blk_probe,
+};
include/fwu.h +2 -1
index 5a99c579fc..2c7db2dff9 100644
@@ -43,6 +43,8 @@ int fwu_get_active_index(u32 *active_idx);
 int fwu_update_active_index(u32 active_idx);
 int fwu_get_image_alt_num(efi_guid_t image_type_id, u32 update_bank,
                          int *alt_num);
+int fwu_get_mdata_device(struct udevice **mdata_dev);
+int fwu_verify_mdata(struct fwu_mdata *mdata, bool pri_part);
 int fwu_mdata_check(void);
 int fwu_revert_boot_index(void);
 int fwu_accept_image(efi_guid_t *img_type_id, u32 bank);
--
Quoted 2 lines

2.17.1

--
Masami Hiramatsu

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

--
Masami Hiramatsu

--
Masami Hiramatsu

--
Masami Hiramatsu

#30 lore raw

Re: [PATCH v4 02/11] FWU: Add FWU metadata access driver for GPT partitioned block devices

Hi Sughosh,

2022年2月10日(木) 21:25 Sughosh Ganu <[email protected]>:

Quoted 70 lines

hi Masami,

Quoted 48 lines · Masami Hiramatsu

2022年2月10日(木) 10:43 Masami Hiramatsu <[email protected]>:

Quoted 25 lines
Quoted 18 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Of course as I said in the other thread, I would like to put the dfu_alt_info like information in the devicetree too. (But it maybe different from this discussion)

Just curious, why do you need to define a variable like dfu_alt_info in a device tree. If this has already been described earlier, you can point me to that discussion. Trying to understand what benefit does having the variables defined in a device tree brings. Thanks.

Quoted 7 lines

If we can consolidate the configuration information related to the firmware layout on the devicetree, it is very easy to understand and manage the firmware update only by checking the devicetree. Current design is very fragile from the consistency viewpoint, because there are 3 different information we are using for FWU, Kconfig, devicetree and u-boot env-variables. If one of them sets inconsistent value, FWU may not work as we expected.

Quoted 3 lines

I get your point. But I think generating the dfu_alt_info at runtime, like how it is done for the ST platforms is in general a better method, as against using a static variable defined in the device tree.

Yeah, the GPT based one is able to store this information on the GPT, and it must be a primary definition.

Quoted 7 lines
Quoted 3 lines

With runtime generation of the variable, the same code can be used on multiple platforms and can be generated at runtime -- I feel that is better than defining the variable in every platform's device tree.

I don't agree this point at least for non-GPT devices, since the firmware storage layout depends on the platform hardware configuration statically in such cases.

I changed my mind, it can be solved if we have "uuid" property for each partition in the devicetree. I will explain later.

Quoted 10 lines

Of course if the device uses GPT to store the firmware, we have to follow the GPT layout and FWU Metadata to find the corresponding firmware partition.

Quoted 5 lines

Btw, there is also provision to define the variable(or part of it) statically through Kconfig variables. As against your concern about the feature using multiple methods for stating information, it is indeed valid. But I guess we can have documentation explaining how each of that information needs to be defined. Thanks.

Quoted 16 lines
Quoted 3 lines

Yeah, even using GPT, we need to set correct UUID to the FWU metadata, and the metadata depends on Kconfig if we keep putting the #of images-per-bank and the #of banks in the Kconfig, and storage

Sorry, I confused. there are "#of images and #of banks per image".

Quoted 10 lines

(controller) is defined in the devicetree.

And I still feel this "chain of definitions" seems a bit fragile. This fragile comes from the FWU metadata is not enough self-described (it has no the #of images-per-bank and the #of banks, without this information we can not decode FWU metadata itself.)
Anyway, if we can define the #of images-per-bank and the #of banks in the devicetree, we don't need to change the binary but just changing the devicetree for the different products which has different firmware layout. I think that is more flexible.

Quoted 7 lines

Do you really feel that using config values for #banks and #images_per_bank is such a bad idea. With the approach that you suggest, we will have to use variable sized arrays, and populate the values in the probe function of every driver. Also, since you are going to use the same fwu_mdata.h header in your other project, will you be able to use variable sized arrays there as well. I feel that we are complicating things without much benefits.

Hm, indeed. It may make us to handle the metadata as a single data structure on memory. We may need allocate several objects (and arrays) and decode/encode the data from/to storage device. OK, then let those parameters keep in the kconfig.

Quoted 22 lines
Quoted 13 lines

What I would like to suggest is

/* For GPT BLK backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-gpt";
    fwu-mdata-store = <&mmc1>;
    /* No need to specify the mdata partition, because it finds the
mdata by partition type uuid. */
    banks = <2>;
    images-per-bank = <1>;
};
Quoted 8 lines

/* For SF backend */

fwu_mdata {
    compatible = "u-boot,fwu-mdata-sf";
    fwu-mdata-store = <&spi-flash0>;
    mdata-offsets = <500000, 520000>; /* Or specified by partition label? */
    banks = <6>;
    images-per-bank = <1>;
};
Quoted 4 lines

Looks good overall. Should the mdata-offsets property be instead mdata-parts, where we define the start offset and size of the metadata partitions. Or are we going to figure out the size of the metadata partition through some other mechanism.

mdata-parts may only need the label names, since we can find the partition by name. The partition size can be defined by partitions node. But do we really need the size? I'm reserving an enough bigger area for mdata. (And if it is NAND, we need to reserve "a page" for mdata)

Quoted 5 lines

Also, do are using a different compatible string for every type of storage device type. Can we not do with a common string instead? I understand your reasoning here, of trying to identify the driver at runtime. Just that I wonder if we are going to build multiple drivers for a platform. Although I do not have a strong opinion on this.

I can't imagine the case that a platform has different mdata store at the same time... Anyway, as you said, we need the different compatible strings for different drivers.

Quoted 19 lines
Quoted 19 lines

Note that this is only for the metadata, the real firmware layout issue still exists. If we can add "uuid" property for the fixed-partitions node as a additional property, e.g.

spi-flash@0 {
   partitions {
       compatible = "fixed-partitions";
       ...
       uuid = "aaaaaaaa-bbbb-cccc-dddd-eeeeffffgggg";
       ...
       partition@600000 {
           label = "Firmware-Bank0";
           reg = <600000, 400000>;
           uuid = "12345678-aaaa-bbbb-cccc-0123456789ab";
       };
       ...
   };
};
Quoted 8 lines
Quoted 2 lines

Then we can decode the real fwu-mdata and find corresponding partitions, and able to build dfu_alt_info in runtime.

This is a sound idea. With this method, we can still use the GUIDs in the metadata structure, and map those with the partitions using the above nodes. Only question is, are these partition nodes(partition@600000) standard, and are defined for all platforms, for all type of devices(like NOR, Nand). If so, this scheme will work.

Yes, as far as I know, now mtd subsystem requires to define the partitions by the devicetree, and that is acceptable both U-Boot and Linux. You can find the "fixed-partitions" definition in the Linux dt-bindings.

https://www.kernel.org/doc/Documentation/devicetree/bindings/mtd/partitions/fixed-partitions.yaml

I think we can add uuid because there is "additionalProperties: true".

We need the image-type-id uuid, but we can get it from fwu mdata.

Thus, the boot sequence will be
1. parse devicetree.
2. probe fwu-mdata driver and load mdata.
3. platform driver builds dfu_alt_info according to the mdata. a. read mdata and identify the partitions ofnode by location uuid. b. find partition ofnodes by image-uuid.
c. build dfu-alt-info by the ofnodes information.
d. build guid-array by mdata's image-type uuid.

Thank you,

--
Masami Hiramatsu

#31 lore raw

PATCH v4 3/11 FWU: stm32mp1: Add helper functions for accessing FWU metadata

In reply to Sughosh Ganu

Add helper functions needed for accessing the FWU metadata which contains information on the updatable images. These functions have been added for the STM32MP157C-DK2 board which has the updatable images on the uSD card, formatted as GPT partitions.

---

Changes since V3:
* Remove function for getting the storage device containing the metadata as the information is now obtained from the device tree.

 board/st/stm32mp1/stm32mp1.c | 78 ++++++++++++++++++++++++++++++++++++
 include/fwu.h                |  3 ++
 2 files changed, 81 insertions(+)
board/st/stm32mp1/stm32mp1.c +78 -0
index 84592677e4..5e8cb29067 100644
@@ -7,10 +7,13 @@
 
 #include <common.h>
 #include <adc.h>
+#include <blk.h>
 #include <bootm.h>
 #include <clk.h>
 #include <config.h>
+#include <dfu.h>
 #include <dm.h>
+#include <efi_loader.h>
 #include <env.h>
 #include <env_internal.h>
 #include <fdt_support.h>
@@ -23,9 +26,11 @@
 #include <log.h>
 #include <malloc.h>
 #include <misc.h>
+#include <mmc.h>
 #include <mtd_node.h>
 #include <net.h>
 #include <netdev.h>
+#include <part.h>
 #include <phy.h>
 #include <remoteproc.h>
 #include <reset.h>
@@ -938,3 +943,76 @@ static void board_copro_image_process(ulong fw_image, size_t fw_size)
 }
 
 U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
+
+#if defined(CONFIG_FWU_MULTI_BANK_UPDATE)
+#include <fwu.h>
+#include <fwu_mdata.h>
+
+static int gpt_plat_get_alt_num(int dev_num, void *identifier)
+{
+	int i;
+	int ret = -1;
+	u32 part;
+	int alt_num = dfu_get_alt_number();
+	struct dfu_entity *dfu;
+
+	part = *(u32 *)identifier;
+	dfu_init_env_entities(NULL, NULL);
+
+	for (i = 0; i < alt_num; i++) {
+		dfu = dfu_get_entity(i);
+
+		if (!dfu)
+			continue;
+
+		/*
+		 * Currently, Multi Bank update
+		 * feature is being supported
+		 * only on GPT partitioned
+		 * MMC/SD devices.
+		 */
+		if (dfu->dev_type != DFU_DEV_MMC)
+			continue;
+
+		if (dfu->layout == DFU_RAW_ADDR &&
+		    dfu->data.mmc.dev_num == dev_num &&
+		    dfu->data.mmc.part == part) {
+			ret = dfu->alt;
+			break;
+		}
+	}
+
+	dfu_free_entities();
+
+	return ret;
+}
+
+int fwu_plat_get_alt_num(struct udevice *dev, void *identifier)
+{
+	struct blk_desc *desc;
+
+	desc = dev_get_uclass_plat(dev);
+	if (!desc) {
+		log_err("Block device not found\n");
+		return -ENODEV;
+	}
+
+	return gpt_plat_get_alt_num(desc->devnum, identifier);
+}
+
+int fwu_plat_get_update_index(u32 *update_idx)
+{
+	int ret;
+	u32 active_idx;
+
+	ret = fwu_get_active_index(&active_idx);
+
+	if (ret < 0)
+		return -1;
+
+	*update_idx = active_idx ^= 0x1;
+
+	return ret;
+}
+
+#endif /* CONFIG_FWU_MULTI_BANK_UPDATE */
include/fwu.h +3 -0
index 2c7db2dff9..b746e616b2 100644
@@ -50,4 +50,7 @@ int fwu_revert_boot_index(void);
 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);
 
+int fwu_plat_get_update_index(u32 *update_idx);
+int fwu_plat_get_alt_num(struct udevice *dev, void *identifier);
+
 #endif /* _FWU_H_ */
Signature · 1 line
2.17.1
#32 lore raw

PATCH v4 4/11 FWU: STM32MP1: Add support to read boot index from backup register

In reply to Sughosh Ganu

The FWU Multi Bank Update feature allows the platform to boot the firmware images from one of the partitions(banks). The first stage bootloader(fsbl) passes the value of the boot index, i.e. the bank from which the firmware images were booted from to U-Boot. On the STM32MP157C-DK2 board, this value is passed through one of the SoC's backup register. Add a function to read the boot index value from the backup register.

---

Changes since V3: None

 board/st/stm32mp1/stm32mp1.c | 6 ++++++
 include/fwu.h                | 1 +
 2 files changed, 7 insertions(+)
board/st/stm32mp1/stm32mp1.c +6 -0
index 5e8cb29067..5525b69392 100644
@@ -1015,4 +1015,10 @@ int fwu_plat_get_update_index(u32 *update_idx)
 	return ret;
 }
 
+void fwu_plat_get_bootidx(void *boot_idx)
+{
+	u32 *bootidx = boot_idx;
+
+	*bootidx = readl(TAMP_BOOTCOUNT);
+}
 #endif /* CONFIG_FWU_MULTI_BANK_UPDATE */
include/fwu.h +1 -0
index b746e616b2..90b8cd41e5 100644
@@ -52,5 +52,6 @@ int fwu_clear_accept_image(efi_guid_t *img_type_id, u32 bank);
 
 int fwu_plat_get_update_index(u32 *update_idx);
 int fwu_plat_get_alt_num(struct udevice *dev, void *identifier);
+void fwu_plat_get_bootidx(void *boot_idx);
 
 #endif /* _FWU_H_ */
Signature · 1 line
2.17.1
#33 lore raw

PATCH v4 5/11 EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

In reply to Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+					efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -7

129 lines not on this page · raw message

Signature · 1 line
2.17.1
#34 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.
2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+					efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -7
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
 	return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+					       efi_guid_t **part_guid_arr)
+{
+	int i;
+	int dfu_num = 0;
+	efi_guid_t *guid_arr;
+	struct dfu_entity *dfu;
+	efi_status_t ret = EFI_SUCCESS;
+
+	dfu_init_env_entities(NULL, NULL);
+
+	dfu_num = 0;
+	list_for_each_entry(dfu, &dfu_list, list) {
+		dfu_num++;
+	}
+
+	if (!dfu_num) {
+		log_warning("Probably dfu_alt_info not defined\n");
+		ret = EFI_NOT_READY;
+		goto out;
+	}
+
+	*part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+	if (!*part_guid_arr) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out;
+	}
+
+	guid_arr = *part_guid_arr;
+	for (i = 0; i < dfu_num; i++) {
+		guidcpy(guid_arr, guid);
+		++guid_arr;
+	}
+
+out:
+	dfu_free_entities();
+
+	return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:			Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:		Pointer to version number
  * @descriptor_count:		Pointer to number of descriptors
  * @descriptor_size:		Pointer to descriptor size
- * package_version:		Package version
- * package_version_name:	Package version's name
- * image_type:			Image type GUID
+ * @package_version:		Package version
+ * @package_version_name:	Package version's name
+ * @guid_array:			Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
 	efi_uintn_t *descriptor_size,
 	u32 *package_version,
 	u16 **package_version_name,
-	const efi_guid_t *image_type)
+	const efi_guid_t *guid_array)
 {
 	struct dfu_entity *dfu;
 	size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
 	next = name;
 	list_for_each_entry(dfu, &dfu_list, list) {
 		image_info[i].image_index = dfu->alt + 1;
-		image_info[i].image_type_id = *image_type;
+		image_info[i].image_type_id = guid_array[i];
 		image_info[i].image_id = dfu->alt;
 
 		/* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
 	u16 **package_version_name)
 {
 	efi_status_t ret;
+	efi_guid_t *part_guid_arr = NULL;
 
 	EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
 		  image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
 	     !descriptor_size || !package_version || !package_version_name))
 		return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+	ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+					 &part_guid_arr);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
 	ret = efi_get_dfu_info(image_info_size, image_info,
 			       descriptor_version, descriptor_count,
 			       descriptor_size,
 			       package_version, package_version_name,
-			       &efi_firmware_image_type_uboot_fit);
+			       part_guid_arr);
 
+out:
+	free(part_guid_arr);
 	return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
 	u16 **package_version_name)
 {
 	efi_status_t ret = EFI_SUCCESS;
+	efi_guid_t *part_guid_arr = NULL;
 
 	EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
 		  image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
 	     !descriptor_size || !package_version || !package_version_name))
 		return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+	ret = fill_image_type_guid_array(
+		&efi_firmware_image_type_uboot_raw,
+		&part_guid_arr);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
 	ret = efi_get_dfu_info(image_info_size, image_info,
 			       descriptor_version, descriptor_count,
 			       descriptor_size,
 			       package_version, package_version_name,
-			       &efi_firmware_image_type_uboot_raw);
+			       part_guid_arr);
 
+out:
+	free(part_guid_arr);
 	return EFI_EXIT(ret);
 }
 
Signature · 1 line
2.17.1
#35 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID, which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Quoted 9 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#36 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 23 lines · Sughosh Ganu

hi Takahiro,

Quoted 19 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

Quoted 5 lines
Quoted 2 lines

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

What does FMP GUID stand for?

Quoted 2 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 10 lines
Quoted 9 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 6 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

As I said, ImageIndex is for that purpose.

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#37 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 25 lines · AKASHI Takahiro
Quoted 23 lines · Sughosh Ganu

hi Takahiro,

Quoted 19 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule. Please check the GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 20 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

Quoted 2 lines

I don't follow your point.
Please elaborate a bit more.

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail. This means that unless the --guid value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 9 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time -- this is determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#38 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Quoted 29 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 20 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

Quoted 2 lines

I don't follow your point.
Please elaborate a bit more.

Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?
If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

Quoted 15 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?
I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#39 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

hi Takahiro,

Quoted 63 lines · AKASHI Takahiro

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

Quoted 7 lines

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Quoted 30 lines
Quoted 30 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

Quoted 17 lines
Quoted 17 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Like I mentioned earlier, this is not breaking the existing behaviour -- for the non A/B updates, the update procedure remains exactly the same, of using the index value to determine the location of the update. I have only extended the behaviour to use the same FMP instance for the A/B update(FWU) feature using ImageTypeId's.

-sughosh

Quoted 190 lines

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#40 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Sughosh,

Quoted 75 lines · Sughosh Ganu

hi Takahiro,

Quoted 63 lines · AKASHI Takahiro

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

Quoted 7 lines

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

Quoted 41 lines
Quoted 30 lines
Quoted 30 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 8 lines
Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Yes, we can :)

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I don't think we need to introduce extra GUIDs.

-Takahiro Akashi

Quoted 198 lines

Like I mentioned earlier, this is not breaking the existing behaviour -- for the non A/B updates, the update procedure remains exactly the same, of using the index value to determine the location of the update. I have only extended the behaviour to use the same FMP instance for the A/B update(FWU) feature using ImageTypeId's.

-sughosh

Quoted 190 lines

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#41 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 86 lines · AKASHI Takahiro

Sughosh,

Quoted 75 lines · Sughosh Ganu

hi Takahiro,

Quoted 63 lines · AKASHI Takahiro

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

Quoted 7 lines

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 32 lines
Quoted 32 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

Quoted 6 lines
Quoted 4 lines

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 12 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

Quoted 7 lines

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

We can use this logic yes. But please note, that with this, we need to
a) Keep a definite order of the images in all the banks, and b) Know the order of the images.

With the way the FWU metadata is designed, we do not have these restrictions -- the firmware images can be placed on the device in any order, irrespective of the bank that they belong to. One might prefer clubbing images of a given bank together, but that is not the restriction put by the FWU spec. That is because the images are being identified using image GUIDs.

I really don't get your opposition to extending the current design. I would like to hear from Heinrich or Ilias on what their thoughts are on this.

-sughosh

Quoted 203 lines

I don't think we need to introduce extra GUIDs.

-Takahiro Akashi

Quoted 198 lines

Like I mentioned earlier, this is not breaking the existing behaviour -- for the non A/B updates, the update procedure remains exactly the same, of using the index value to determine the location of the update. I have only extended the behaviour to use the same FMP instance for the A/B update(FWU) feature using ImageTypeId's.

-sughosh

Quoted 190 lines

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#42 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Hi,

Took me some time to go through the whole thread, but here it goes.

Quoted 206 lines · Sughosh Ganu
Quoted 86 lines · AKASHI Takahiro

Sughosh,

Quoted 75 lines · Sughosh Ganu

hi Takahiro,

Quoted 63 lines · AKASHI Takahiro

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

Quoted 7 lines

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

Regards
/Ilias

Quoted 221 lines

We can use this logic yes. But please note, that with this, we need to
a) Keep a definite order of the images in all the banks, and b) Know the order of the images.

With the way the FWU metadata is designed, we do not have these restrictions -- the firmware images can be placed on the device in any order, irrespective of the bank that they belong to. One might prefer clubbing images of a given bank together, but that is not the restriction put by the FWU spec. That is because the images are being identified using image GUIDs.

I really don't get your opposition to extending the current design. I would like to hear from Heinrich or Ilias on what their thoughts are on this.

-sughosh

Quoted 203 lines

I don't think we need to introduce extra GUIDs.

-Takahiro Akashi

Quoted 198 lines

Like I mentioned earlier, this is not breaking the existing behaviour -- for the non A/B updates, the update procedure remains exactly the same, of using the index value to determine the location of the update. I have only extended the behaviour to use the same FMP instance for the A/B update(FWU) feature using ImageTypeId's.

-sughosh

Quoted 190 lines

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#43 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

hi Ilias,

Quoted 244 lines · Ilias Apalodimas

Hi,

Took me some time to go through the whole thread, but here it goes.

Quoted 206 lines · Sughosh Ganu
Quoted 86 lines · AKASHI Takahiro

Sughosh,

Quoted 75 lines · Sughosh Ganu

hi Takahiro,

Quoted 63 lines · AKASHI Takahiro

Sughosh,

Quoted 33 lines · Sughosh Ganu
Quoted 31 lines · AKASHI Takahiro
Quoted 28 lines · Sughosh Ganu

hi Takahiro,

Quoted 13 lines · AKASHI Takahiro

Hi Sughosh,

Quoted 6 lines · Sughosh Ganu

The FWU Multi Banks Update feature allows updating different types of updatable firmware images on the platform. These image types are identified using the ImageTypeId GUID value. Add support in the GetImageInfo function of the FMP protocol to get the GUID values for the individual images and populate these in the image descriptor for the corresponding images.

After re-thinking of your approach here, I would have to say NAK.

Quoted 8 lines

You use ImageTypeId to identify a particular firmware object.
(By "object," I mean one of firmware instances represented by "dfu_alto_info". Please don't confuse it with the binary blob embedded in a capsule file.) But ImageTypeId is not for that purpose, at least, as my intention in initially implementing capsule framework and FMP drivers.

1) ImageTypeId is used to uniquely identify a corresponding FMP driver, either FIT FMP driver or Raw FMP driver.

I believe the identification of an FMP protocol should be done by the FMP GUID,

Quoted 7 lines
Quoted 1 line

What does FMP GUID stand for?

EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID, defined in include/efi_api.h. What I mean is that even when installing the FMP protocol, the call to efi_install_multiple_protocol_interfaces takes the above FMP GUID as an argument -- nowhere is the ImageTypeId considered when installing the protocol.

Okay.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

which is what is done in efi_fmp_find. The ImageTypeId is nowhere involved in this identification.

Please take a look at efi_capsule_update_firmware() carefully. efi_find_fmp() is called with the image's update_image_type_id which is to be set to EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID by mkeficapsule (see create_fwbin()).

Quoted 5 lines

I think you are thinking from the point of view of the '--guid' value that is being passed to the capsule generation tool. But the thing is that it is the current design(or limitation) of the tool that it takes only a single guid parameter. So the mkeficapsule tool currently can generate only a single payload capsule.

Quoted 7 lines

That is exactly what I intended to do here.
We have only one FMP driver (either FIT or RAW) which is based on U-Boot's DFU framework and we need only one payload since, for multiple objects of firmware, we can use FIT format as a payload. That is what FIT is aimed for.
Or you can use multiple RAW capsule files with different indexes ("--index" exists for this purpose).

Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

Quoted 225 lines

Regards
/Ilias

Quoted 221 lines

We can use this logic yes. But please note, that with this, we need to
a) Keep a definite order of the images in all the banks, and b) Know the order of the images.

With the way the FWU metadata is designed, we do not have these restrictions -- the firmware images can be placed on the device in any order, irrespective of the bank that they belong to. One might prefer clubbing images of a given bank together, but that is not the restriction put by the FWU spec. That is because the images are being identified using image GUIDs.

I really don't get your opposition to extending the current design. I would like to hear from Heinrich or Ilias on what their thoughts are on this.

-sughosh

Quoted 203 lines

I don't think we need to introduce extra GUIDs.

-Takahiro Akashi

Quoted 198 lines

Like I mentioned earlier, this is not breaking the existing behaviour -- for the non A/B updates, the update procedure remains exactly the same, of using the index value to determine the location of the update. I have only extended the behaviour to use the same FMP instance for the A/B update(FWU) feature using ImageTypeId's.

-sughosh

Quoted 190 lines

-Takahiro Akashi

Quoted 186 lines

-- this is
determined only at runtime, and is based on the bank to which the image is to be updated. Which is why I am finding out the alt_num at runtime in case the FWU Multi Bank feature is enabled. Like I said above, I do not see a reason why the current FMP protocols should be restricted to only the u-boot FIT and u-boot raw image types. It is being extended, without affecting the default non FWU behaviour.

-sughosh

Quoted 176 lines

-Takahiro Akashi

Quoted 173 lines

-sughosh

Quoted 170 lines

-Takahiro Akashi

Quoted 166 lines

---

Changes since V3:
* Define a weak function fill_image_type_guid_array for populating the image descriptor array with u-boot's raw and fit image GUIDs

 include/efi_loader.h          |  2 +
 lib/efi_loader/efi_firmware.c | 71 +++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)
include/efi_loader.h +2 -0
index f4860e87fc..ae60de0be5 100644
@@ -992,4 +992,6 @@ efi_status_t efi_esrt_populate(void);
 efi_status_t efi_load_capsule_drivers(void);
 
 efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz);
+efi_status_t fill_image_type_guid_array(const efi_guid_t *default_guid,
+                                     efi_guid_t **part_guid_arr);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/efi_firmware.c +64 -8
index a1b88dbfc2..5642be9f9a 100644
@@ -96,6 +96,46 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
      return EFI_EXIT(EFI_UNSUPPORTED);
 }
 
+efi_status_t __weak fill_image_type_guid_array(const efi_guid_t *guid,
+                                            efi_guid_t **part_guid_arr)
+{
+     int i;
+     int dfu_num = 0;
+     efi_guid_t *guid_arr;
+     struct dfu_entity *dfu;
+     efi_status_t ret = EFI_SUCCESS;
+
+     dfu_init_env_entities(NULL, NULL);
+
+     dfu_num = 0;
+     list_for_each_entry(dfu, &dfu_list, list) {
+             dfu_num++;
+     }
+
+     if (!dfu_num) {
+             log_warning("Probably dfu_alt_info not defined\n");
+             ret = EFI_NOT_READY;
+             goto out;
+     }
+
+     *part_guid_arr = malloc(sizeof(efi_guid_t) * dfu_num);
+     if (!*part_guid_arr) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+
+     guid_arr = *part_guid_arr;
+     for (i = 0; i < dfu_num; i++) {
+             guidcpy(guid_arr, guid);
+             ++guid_arr;
+     }
+
+out:
+     dfu_free_entities();
+
+     return ret;
+}
+
 /**
  * efi_get_dfu_info - return information about the current firmware image
  * @this:                    Protocol instance
@@ -104,9 +144,9 @@ efi_status_t EFIAPI efi_firmware_set_package_info_unsupported(
  * @descriptor_version:              Pointer to version number
  * @descriptor_count:                Pointer to number of descriptors
  * @descriptor_size:         Pointer to descriptor size
- * package_version:          Package version
- * package_version_name:     Package version's name
- * image_type:                       Image type GUID
+ * @package_version:         Package version
+ * @package_version_name:    Package version's name
+ * @guid_array:                      Image type GUID array
  *
  * Return information bout the current firmware image in @image_info.
  * @image_info will consist of a number of descriptors.
@@ -122,7 +162,7 @@ static efi_status_t efi_get_dfu_info(
      efi_uintn_t *descriptor_size,
      u32 *package_version,
      u16 **package_version_name,
-     const efi_guid_t *image_type)
+     const efi_guid_t *guid_array)
 {
      struct dfu_entity *dfu;
      size_t names_len, total_size;
@@ -172,7 +212,7 @@ static efi_status_t efi_get_dfu_info(
      next = name;
      list_for_each_entry(dfu, &dfu_list, list) {
              image_info[i].image_index = dfu->alt + 1;
-             image_info[i].image_type_id = *image_type;
+             image_info[i].image_type_id = guid_array[i];
              image_info[i].image_id = dfu->alt;
 
              /* copy the DFU entity name */
@@ -250,6 +290,7 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -264,12 +305,19 @@ efi_status_t EFIAPI efi_firmware_fit_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(&efi_firmware_image_type_uboot_fit,
+                                      &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_fit);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
@@ -359,6 +407,7 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
      u16 **package_version_name)
 {
      efi_status_t ret = EFI_SUCCESS;
+     efi_guid_t *part_guid_arr = NULL;
 
      EFI_ENTRY("%p %p %p %p %p %p %p %p\n", this,
                image_info_size, image_info,
@@ -373,12 +422,20 @@ efi_status_t EFIAPI efi_firmware_raw_get_image_info(
           !descriptor_size || !package_version || !package_version_name))
              return EFI_EXIT(EFI_INVALID_PARAMETER);
 
+     ret = fill_image_type_guid_array(
+             &efi_firmware_image_type_uboot_raw,
+             &part_guid_arr);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
      ret = efi_get_dfu_info(image_info_size, image_info,
                             descriptor_version, descriptor_count,
                             descriptor_size,
                             package_version, package_version_name,
-                            &efi_firmware_image_type_uboot_raw);
+                            part_guid_arr);
 
+out:
+     free(part_guid_arr);
      return EFI_EXIT(ret);
 }
 
--

2.17.1

#44 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[...]

Quoted 170 lines
Quoted 157 lines
Quoted 125 lines
Quoted 7 lines
Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Quoted 7 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#45 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 175 lines · Ilias Apalodimas
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 170 lines
Quoted 157 lines
Quoted 125 lines
Quoted 7 lines
Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:
<loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
|<--- 1st set --->|<--- 2nd set --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 7 lines
Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Yes, the platform would have to define the fill_image_type_guid_array function which would populate the ImageTypeId values in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array, and the image GUID values would then show up as part of the ESRT table.

As part of this patchset, I have added this function for the STM32MP1 DK2 board.

-sughosh

Quoted 13 lines
Quoted 8 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#46 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 177 lines · Sughosh Ganu
Quoted 175 lines · Ilias Apalodimas
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 170 lines
Quoted 157 lines
Quoted 125 lines
Quoted 7 lines
Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:

   <loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
   |<---      1st set               --->|<---       2nd set               --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

This is wrong. According to the the UEFI 2.9 specification the UpdateImageTypeId is used to "identify (the) device firmware targeted by this update". It does not identify the format in which the firmware is delivered.

So this needs to be fixed in the next revision of this patch series.

For each firmware part that can be updated provide a unique GUID.

Best regards

Heinrich

Quoted 26 lines
Quoted 3 lines

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Yes, the platform would have to define the fill_image_type_guid_array function which would populate the ImageTypeId values in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array, and the image GUID values would then show up as part of the ESRT table.

As part of this patchset, I have added this function for the STM32MP1 DK2 board.

-sughosh

Quoted 13 lines
Quoted 8 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#47 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

hello Heinrich,

Quoted 184 lines · Heinrich Schuchardt
Quoted 177 lines · Sughosh Ganu
Quoted 175 lines · Ilias Apalodimas
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 170 lines
Quoted 157 lines
Quoted 125 lines
Quoted 7 lines
Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 11 lines
Quoted 10 lines

2) Each firmware object handled by a given FMP driver can further be
identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as
a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and
doesn't meet the UEFI specification.

Quoted 8 lines

So, as per what you are stating, all payloads under a given EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two values, > the check in efi_fmp_find to compare the UpdateImageTypeId with the ImageTypeId retrieved from the image descriptor would simply fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the spec states that the ImageTypeId and the UpdateImageTypeId are fields used to identify the firmware component targeted for the update. If all values in the image descriptor array and the UpdateImageTypeId are the same, why have this field in the first place for individual images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:

   <loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
   |<---      1st set               --->|<---       2nd set               --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

Quoted 6 lines

This is wrong. According to the the UEFI 2.9 specification the UpdateImageTypeId is used to "identify (the) device firmware targeted by this update". It does not identify the format in which the firmware is delivered.

So this needs to be fixed in the next revision of this patch series.

This patch series is actually adding that platform function which populates the image descriptor array with the image GUID's -- patch 6 of this series[1] actually does that for the ST DK2 platform. This discussion was because Takahiro wanted to use the same image GUID(u-boot raw/FIT) for all the images, and use the image index for identifying where the image is to be written.

I guess with what you are stating, along with Ilias's opinion on this, I will send the next version with the same approach, i.e. using a platform function to populate the image GUIDs in the firmware image descriptor array. With this, each firmware image will have a different GUID which can be used to identify the image.

-sughosh

[1] - https://lists.denx.de/pipermail/u-boot/2022-February/474434.html

Quoted 34 lines

For each firmware part that can be updated provide a unique GUID.

Best regards

Heinrich

Quoted 26 lines
Quoted 3 lines

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Yes, the platform would have to define the fill_image_type_guid_array function which would populate the ImageTypeId values in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array, and the image GUID values would then show up as part of the ESRT table.

As part of this patchset, I have added this function for the STM32MP1 DK2 board.

-sughosh

Quoted 13 lines
Quoted 8 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#48 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Quoted 192 lines · Sughosh Ganu

hello Heinrich,

Quoted 189 lines · Heinrich Schuchardt
Quoted 177 lines · Sughosh Ganu
Quoted 175 lines · Ilias Apalodimas
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 170 lines
Quoted 157 lines
Quoted 125 lines
Quoted 7 lines
Quoted 4 lines

Yes, we can use --index when we know the index value corresponding to the firmware image that we need to update. But like I mentioned in my earlier reply, for A/B updates, we do not know what the index value is going to be. That is going to be determined at runtime.

I don't think so. See below for alternative approach.

Quoted 8 lines
Quoted 3 lines

Also, the point I was making is that we can have a capsule which is consumed by an FMP protocol which has more than one image, and those images have different ImageTypeId/UpdateImageTypeId.

Yes, but it is a design choice in my first implementation.
I didn't think that we need to "have a capsule which is consumed by an FMP protocol which has more than one image" as long as we use DFU framework (and FIT as standard format of aggregation on U-Boot).

But this design can be extended without any hassle, and more importantly without any regression, no? What kind of a problem does it create if the FMP can handle more than one image type.

Even as per the UEFI spec, we have the EFI_FIRMWARE_MANAGEMENT_CAPSULE header for all images to be managed by the FMP protocol which has multiple images with different UpdateImageTypeId.

Quoted 55 lines
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines

Please check the
GenerateCapsule script in EDK2. In case of a multi payload based capsule, individual parameters like the UpdateImageTypeId are passed through the json file, where each of the UpdateImageTypeId has a different value per payload.

Quoted 22 lines
Quoted 19 lines

2) Each firmware object handled by a given FMP driver can further be
identified by ImageIndex.

My implementation of efi_fmp_find() does (1) and Raw FMP driver does
(2) in efi_firmware_raw_set_image() which takes "image_index" as
a parameter.

Using ImageTypeId as an identifier is simply wrong in my opinion and
doesn't meet the UEFI specification.

So, as per what you are stating, all payloads under a given
EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER should have the same
ImageTypeId, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or
EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID. Same applies for all images in
the EFI_FIRMWARE_IMAGE_DESCRIPTOR. Because, without one of the two
values, > the check in efi_fmp_find to compare the UpdateImageTypeId
with the ImageTypeId retrieved from the image descriptor would simply
fail.

I don't follow your point.
Please elaborate a bit more.

Quoted 8 lines
Quoted 6 lines

The current implementation of GetImageInfo, passes either of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID for all the images of the image descriptor array. So, in case the capsule is generated with a '--guid' value which is different from these two values, the check in efi_fmp_find on line 204 will fail.

That is an expected behavior, isn't it?

Yes it is. Do not contest that.

Quoted 7 lines
Quoted 2 lines

If you want to use a different FMP driver (with another GUID), you naturally need to add your own FMP driver.

This is where I differ. We can use the same FMP protocol instance for any type of ImageTypeId. I do not see why we need to define a different FMP protocol instance for a GUID value other than what has been defined for u-boot raw and u-boot FIT GUIDs.

I do understand part of your concern a bit.
I thought of using the same ImageType GUID, say IMAGE_TYPE_DFU_GUID, at first but we needed different GUIDs here simply because we need to determine the format of payload, FIT format or raw binary.

Quoted 4 lines
Quoted 2 lines

The platform can give us the image descriptor array, and with that, the same FMP instance can be used for any type of image(ImageTypeId).

"any type of image"? Really?

The raw FMP instance can certainly handle any type of binary payloads right. There is no restriction on what type of payload it is as long as it is all going as a single entity to a given dfu partition.

Quoted 37 lines
Quoted 36 lines
Quoted 18 lines
Quoted 18 lines

This means that unless the --guid
value passed to the capsule generation is either of u-boot FIT or u-boot raw, the current FMP protocol for raw devices cannot be used. Why do we need that restriction. It should be possible to use the raw FMP protocol for any other type of image types as well.

Quoted 8 lines
Quoted 7 lines

I think this interpretation of the UEFI spec is incorrect, since the
spec states that the ImageTypeId and the UpdateImageTypeId are fields
used to identify the firmware component targeted for the update. If
all values in the image descriptor array and the UpdateImageTypeId are
the same, why have this field in the first place for individual
images.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I said, ImageIndex is for that purpose.

Yes, that is one possible way in the scenario where the ImageIndex is determined at the capsule generation time. But, for the A/B update scenario, we do not know the ImageIndex at build time

"Build time" of what?

Of the capsule.

Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:

   <loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
   |<---      1st set               --->|<---       2nd set               --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

This is wrong. According to the the UEFI 2.9 specification the UpdateImageTypeId is used to "identify (the) device firmware targeted by this update". It does not identify the format in which the firmware is delivered.

Quoted 8 lines
Quoted 1 line

So this needs to be fixed in the next revision of this patch series.

This patch series is actually adding that platform function which populates the image descriptor array with the image GUID's -- patch 6 of this series[1] actually does that for the ST DK2 platform. This discussion was because Takahiro wanted to use the same image GUID(u-boot raw/FIT) for all the images, and use the image index for identifying where the image is to be written.

The discussion depends on what the *firmware* means.
With my FMP drivers (either FIT or raw), I intended a *set of firmware* managed with a *single* "dfu_alt_info", which may include various *images* for different target *components* as the original DFU framework does.

-Takahiro Akashi

Quoted 44 lines

I guess with what you are stating, along with Ilias's opinion on this, I will send the next version with the same approach, i.e. using a platform function to populate the image GUIDs in the firmware image descriptor array. With this, each firmware image will have a different GUID which can be used to identify the image.

-sughosh

[1] - https://lists.denx.de/pipermail/u-boot/2022-February/474434.html

Quoted 34 lines

For each firmware part that can be updated provide a unique GUID.

Best regards

Heinrich

Quoted 26 lines
Quoted 3 lines

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Yes, the platform would have to define the fill_image_type_guid_array function which would populate the ImageTypeId values in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array, and the image GUID values would then show up as part of the ESRT table.

As part of this patchset, I have added this function for the STM32MP1 DK2 board.

-sughosh

Quoted 13 lines
Quoted 8 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#49 lore raw

Re: [PATCH v4 05/11] EFI: FMP: Add provision to update image's ImageTypeId in image descriptor

Akashi-san

[...]

Quoted 73 lines
Quoted 64 lines
Quoted 64 lines
Quoted 54 lines
Quoted 54 lines
Quoted 54 lines
Quoted 41 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

I think that users should know how "dfu_alt_info" is defined (in other words, where the firmware be located on the target system) when capsule files are created.

That is true for a non A/B scenario. And that is how it works in the non A/B updates case. But for A/B updates, since the determination of the "location" where the firmware image has to be written will be done only at runtime, we cannot use the --index to differentiate.

Quoted 24 lines
Quoted 1 line

Yes, we can :)

You know what I mean -- if we could use the same logic, I would not have added all that code :)

Quoted 19 lines

First of all, my essential assumption in either FIT or RAW FMP driver is that U-Boot has (somehow conceptually) single firmware blob represented by DFU or dfu_alt_info. As I said, each object or location in dfu_alt_info can be further identified by index or "UpdateImageIndex".

Let's assume that we have two locations of firmware, fw1 and fw2, and that we have two bank A and B.
Then we will define dfu_alt_info as follows:

   <loc of fw1 for A>;<loc of fw2 for A>;<loc of fw1 for B>;<loc of fw2 for B>;
   |<---      1st set               --->|<---       2nd set               --->|

When you want to update bank A, we can use the first set of dfu_alt_info, and use the second set of dfu_alt_info for bank B.
At runtime, you should know which bank you're working on, and therefore you should know the exact physical location from dfu_alt_info.

Please note that you don't have to change the syntax of dfu_alt_info at all. Simply offset the location with 0 for bank A and with 2 for bank B.

I'll try digging a bit more, but I think the current approach is not
working as it was intended wrt to the EFI spec. My reading of the spec
and specifically section 23.3.2 is that a Capsule consists of an
EFI capsule header and a payload. The payload now has an EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER which in it's turn can host multiple firmware images of different type described in EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER.

Quoted 7 lines

An FMP implementation should read the UpdateImageTypeId's used to identify the image you are updating and from that derive the UpdateImageIndex which SetImage will use. That would give you the ability to update the all the firmware components with a single capsule.

Sughosh what about the ESRT table generation? If you use different UpdateImageTypeId those should be reflected on the ESRT tables from the OS

That would depend on the values populated in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array by the GetImageInfo function. The image descriptor structure has an ImageTypeId field. The value of ImageTypeId is what will be reflected in the ESRT table.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In the current implementation, all the images in the ESRT table will show the same ImageTypeId value, either EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID or EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID.

This is wrong. According to the the UEFI 2.9 specification the UpdateImageTypeId is used to "identify (the) device firmware targeted by this update". It does not identify the format in which the firmware is delivered.

Quoted 8 lines
Quoted 1 line

So this needs to be fixed in the next revision of this patch series.

This patch series is actually adding that platform function which populates the image descriptor array with the image GUID's -- patch 6 of this series[1] actually does that for the ST DK2 platform. This discussion was because Takahiro wanted to use the same image GUID(u-boot raw/FIT) for all the images, and use the image index for identifying where the image is to be written.

Quoted 4 lines

The discussion depends on what the *firmware* means.
With my FMP drivers (either FIT or raw), I intended a *set of firmware* managed with a *single* "dfu_alt_info", which may include various *images* for different target *components* as the original DFU framework does.

I still think we should fix that. The current code is working, but it's not what the EFI spec describes. I think we should do it the other way around.

IOW the board defines the UUID's for the firmware partitions it needs to update. The FMP driver should then populate those properly in an ESRT table and also generate an appropriate dfu_alt_info (or similar) on the fly.

Regards
/Ilias

Quoted 48 lines

-Takahiro Akashi

Quoted 44 lines

I guess with what you are stating, along with Ilias's opinion on this, I will send the next version with the same approach, i.e. using a platform function to populate the image GUIDs in the firmware image descriptor array. With this, each firmware image will have a different GUID which can be used to identify the image.

-sughosh

[1] - https://lists.denx.de/pipermail/u-boot/2022-February/474434.html

Quoted 34 lines

For each firmware part that can be updated provide a unique GUID.

Best regards

Heinrich

Quoted 26 lines
Quoted 3 lines

Yea I was mostly asking wrt to A/B updates. Would the correct UUID be shown in the ESRT instead of EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID?

Yes, the platform would have to define the fill_image_type_guid_array function which would populate the ImageTypeId values in the EFI_FIRMWARE_IMAGE_DESCRIPTOR array, and the image GUID values would then show up as part of the ESRT table.

As part of this patchset, I have added this function for the STM32MP1 DK2 board.

-sughosh

Quoted 13 lines
Quoted 8 lines

The UpdateImageTypeId value from the capsule is used to compare with the ImageTypeId values returned by the GetImageInfo function to check if the given FMP protocol can be used for the update.

-sughosh

[...]

Regards
/Ilias

#50 lore raw

PATCH v4 6/11 stm32mp1: Populate ImageTypeId values in EFI_FIRMWARE_IMAGE_DESCRIPTOR array

In reply to Sughosh Ganu

The EFI_FIRMWARE_IMAGE_DESCRIPTOR array is returned by the Firmware Management Protocol's(FMP) GetImageInfo function. The image descriptor array contains the ImageTypeId which is a GUID identifying the firmware images that are supported by the instance of the FMP. These ImageTypeId values are compared against the value in the capsule to identify the FMP instance to be used for the capsule.

Add a function for the GPT partitioned device based ST platforms for filling up the image descriptor array with the ImageTypeId values. For the GPT partitioned devices, the array can be populated by reading the GPT header which contains the Partition Entry Array, where each entry contains the PartitionTypeGUID for the corresponding image.

---

Changes since V3:
* Define the function fill_image_type_guid_array for the ST DK2 board for GPT partitioned devices.

 board/st/stm32mp1/stm32mp1.c | 94 ++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
board/st/stm32mp1/stm32mp1.c +94 -0
index 5525b69392..fc14c9808b 100644
@@ -1021,4 +1021,98 @@ void fwu_plat_get_bootidx(void *boot_idx)
 
 	*bootidx = readl(TAMP_BOOTCOUNT);
 }
+
+static int fill_gpt_partition_guids(struct blk_desc *desc,
+				    efi_guid_t **part_guid_arr)
+{
+	int i;
+	u32 part;
+	int alt_num;
+	struct dfu_entity *dfu;
+	struct disk_partition info;
+	efi_guid_t part_type_guid;
+	efi_guid_t null_guid = NULL_GUID;
+	efi_status_t ret = EFI_SUCCESS;
+
+	dfu_init_env_entities(NULL, NULL);
+
+	alt_num = 0;
+	list_for_each_entry(dfu, &dfu_list, list) {
+		++alt_num;
+	}
+
+	if (!alt_num) {
+		log_warning("Probably dfu_alt_info not defined\n");
+		ret = EFI_NOT_READY;
+		goto out;
+	}
+
+	*part_guid_arr = malloc(sizeof(efi_guid_t) * alt_num);
+	if (!*part_guid_arr) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out;
+	}
+
+	for (i = 0; i < alt_num; i++)
+		guidcpy((*part_guid_arr + i), &null_guid);
+
+	for (i = 0, part = 1; i < alt_num; i++) {
+		dfu = dfu_get_entity(i);
+
+		if (!dfu)
+			continue;
+
+		/*
+		 * Currently, Multi Bank update
+		 * feature is being supported
+		 * only on GPT partitioned
+		 * MMC/SD devices.
+		 */
+		if (dfu->dev_type != DFU_DEV_MMC)
+			continue;
+
+		if (part_get_info(desc, part, &info)) {
+			part++;
+			continue;
+		}
+
+		uuid_str_to_bin(info.type_guid, part_type_guid.b,
+				UUID_STR_FORMAT_GUID);
+		guidcpy((*part_guid_arr + i), &part_type_guid);
+		part++;
+	}
+
+out:
+	dfu_free_entities();
+
+	return ret;
+}
+
+efi_status_t fill_image_type_guid_array(const efi_guid_t __always_unused
+					*default_guid,
+					efi_guid_t **part_guid_arr)
+{
+	int ret;
+	struct udevice *dev;
+	struct blk_desc *desc;
+
+	/*
+	 * Get the storage device on which the
+	 * FWU metadata has been stored
+	 */
+	ret = fwu_get_mdata_device(&dev);
+	if (ret) {
+		log_err("Unable to get FWU metadata device\n");
+		return EFI_DEVICE_ERROR;
+	}
+
+	desc = dev_get_uclass_plat(dev);
+	if (!desc) {
+		log_err("Block device not found\n");
+		return EFI_DEVICE_ERROR;
+	}
+
+	return fill_gpt_partition_guids(desc, part_guid_arr);
+}
+
 #endif /* CONFIG_FWU_MULTI_BANK_UPDATE */
Signature · 1 line
2.17.1
#51 lore raw

PATCH v4 7/11 FWU: Add boot time checks as highlighted by the FWU specification

In reply to Sughosh Ganu

The FWU Multi Bank Update specification requires the Update Agent to carry out certain checks at the time of platform boot. The Update Agent is the component which is responsible for updating the firmware components and maintaining and keeping the metadata in sync.

The spec requires that the Update Agent perform the following checks at the time of boot
* Sanity check of both the metadata copies maintained by the platform.
* Get the boot index passed to U-Boot by the prior stage bootloader and use this value for metadata bookkeeping.
* Check if the system is booting in Trial State. If the system boots in the Trial State for more than a specified number of boot counts, change the Active Bank to be booting the platform from.

Add these checks in the board initialisation sequence, invoked after relocation.

---

Changes since V3:
* Change the TrialStateCtr efi variable attribute to remove the runtime attribute

 common/board_r.c      |   6 ++
 include/fwu.h         |   3 +
 lib/fwu_updates/fwu.c | 178 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 lib/fwu_updates/fwu.c
common/board_r.c +6 -0
index 31a59c585a..81678870b9 100644
@@ -78,6 +78,9 @@
 #ifdef CONFIG_EFI_SETUP_EARLY
 #include <efi_loader.h>
 #endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+#include <fwu.h>
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -805,6 +808,9 @@ static init_fnc_t init_sequence_r[] = {
 #endif
 #ifdef CONFIG_EFI_SETUP_EARLY
 	(init_fnc_t)efi_init_obj_list,
+#endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+	fwu_boottime_checks,
 #endif
 	run_main_loop,
 };
include/fwu.h +3 -0
index 90b8cd41e5..5a329d9ef7 100644
@@ -37,6 +37,9 @@ struct fwu_mdata_ops {
 	EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
 		 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
 
+int fwu_boottime_checks(void);
+u8 fwu_update_checks_pass(void);
+
 int fwu_get_mdata(struct fwu_mdata **mdata);
 int fwu_update_mdata(struct fwu_mdata *mdata);
 int fwu_get_active_index(u32 *active_idx);
lib/fwu_updates/fwu.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.17.1
#52 lore raw

Re: [PATCH v4 07/11] FWU: Add boot time checks as highlighted by the FWU specification

po 7. 2. 2022 v 19:22 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 83 lines

The FWU Multi Bank Update specification requires the Update Agent to carry out certain checks at the time of platform boot. The Update Agent is the component which is responsible for updating the firmware components and maintaining and keeping the metadata in sync.

The spec requires that the Update Agent perform the following checks at the time of boot
* Sanity check of both the metadata copies maintained by the platform.
* Get the boot index passed to U-Boot by the prior stage bootloader and use this value for metadata bookkeeping.
* Check if the system is booting in Trial State. If the system boots in the Trial State for more than a specified number of boot counts, change the Active Bank to be booting the platform from.

Add these checks in the board initialisation sequence, invoked after relocation.

---

Changes since V3:
* Change the TrialStateCtr efi variable attribute to remove the runtime attribute

 common/board_r.c      |   6 ++
 include/fwu.h         |   3 +
 lib/fwu_updates/fwu.c | 178 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 lib/fwu_updates/fwu.c
common/board_r.c +6 -0
index 31a59c585a..81678870b9 100644
@@ -78,6 +78,9 @@
 #ifdef CONFIG_EFI_SETUP_EARLY
 #include <efi_loader.h>
 #endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+#include <fwu.h>
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -805,6 +808,9 @@ static init_fnc_t init_sequence_r[] = {
 #endif
 #ifdef CONFIG_EFI_SETUP_EARLY
        (init_fnc_t)efi_init_obj_list,
+#endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+       fwu_boottime_checks,
 #endif
        run_main_loop,
 };
include/fwu.h +3 -0
index 90b8cd41e5..5a329d9ef7 100644
@@ -37,6 +37,9 @@ struct fwu_mdata_ops {
        EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
                 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
 
+int fwu_boottime_checks(void);
+u8 fwu_update_checks_pass(void);
+
 int fwu_get_mdata(struct fwu_mdata **mdata);
 int fwu_update_mdata(struct fwu_mdata *mdata);
 int fwu_get_active_index(u32 *active_idx);
lib/fwu_updates/fwu.c new file +8 -0
new file mode 100644
index 0000000000..86933123e7
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021, Linaro Limited
+ */
+
+#include <dm.h>
+#include <efi.h>
+#include <efi_loader.h>

lib/fwu_updates/fwu.c line 18

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+#include <efi_variable.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+
+static u8 trial_state = 0;
+static u8 boottime_check = 0;

you don't need to initialize them to 0. They are in bss which is setup to 0 already.

Quoted 4 lines
lib/fwu_updates/fwu.c new file +4 -0
+
+static int fwu_trial_state_check(void)
+{
+       int ret, i;

lib/fwu_updates/fwu.c line 32

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+       efi_status_t status;
+       efi_uintn_t var_size;
+       u16 trial_state_ctr;
+       u32 nimages, active_bank, var_attributes, active_idx;
+       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)

Isn't it easier to have if (ret) here?

lib/fwu_updates/fwu.c line 35

Quoted 3 lines
lib/fwu_updates/fwu.c new file +3 -0
+               return ret;
+
+       ret = 0;

Then you can remove this and the whole ret handling below is weird.

Quoted 14 lines
lib/fwu_updates/fwu.c new file +14 -0
+       nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK;
+       active_bank = mdata->active_index;
+       img_entry = &mdata->img_entry[0];
+       for (i = 0; i < nimages; i++) {
+               img_bank_info = &img_entry[i].img_bank_info[active_bank];
+               if (!img_bank_info->accepted) {
+                       trial_state = 1;
+                       break;
+               }
+       }
+
+       if (trial_state) {
+               var_size = (efi_uintn_t)sizeof(trial_state_ctr);
+               log_info("System booting in Trial State\n");

lib/fwu_updates/fwu.c line 59

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+               var_attributes = EFI_VARIABLE_NON_VOLATILE |
+                       EFI_VARIABLE_BOOTSERVICE_ACCESS;
+               status = efi_get_variable_int(L"TrialStateCtr",
+                                             &efi_global_variable_guid,
+                                             &var_attributes,
+                                             &var_size, &trial_state_ctr,
+                                             NULL);
+               if (status != EFI_SUCCESS) {
+                       log_err("Unable to read TrialStateCtr variable\n");
+                       ret = -1;

Any reason not to use standard return macros? The same I see below too.

lib/fwu_updates/fwu.c line 68

Quoted 9 lines
lib/fwu_updates/fwu.c new file +9 -0
+                       goto out;
+               }
+
+               ++trial_state_ctr;
+               if (trial_state_ctr > CONFIG_FWU_TRIAL_STATE_CNT) {
+                       log_info("Trial State count exceeded. Revert back to previous_active_index\n");
+                       active_idx = mdata->active_index;
+                       ret = fwu_revert_boot_index();
+                       if (ret < 0) {

I would use if (ret) here.

Quoted 17 lines
lib/fwu_updates/fwu.c new file +17 -0
+                               log_err("Unable to revert active_index\n");
+                               goto out;
+                       }
+
+                       trial_state_ctr = 0;
+                       status = efi_set_variable_int(L"TrialStateCtr",
+                                                     &efi_global_variable_guid,
+                                                     var_attributes,
+                                                     0,
+                                                     &trial_state_ctr, false);
+                       if (status != EFI_SUCCESS) {
+                               log_err("Unable to clear TrialStateCtr variable\n");
+                               ret = -1;
+                               goto out;
+                       }
+               } else {
+                       status = efi_set_variable_int(L"TrialStateCtr",

lib/fwu_updates/fwu.c line 95

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+                                                     &efi_global_variable_guid,
+                                                     var_attributes,
+                                                     var_size,
+                                                     &trial_state_ctr, false);
+                       if (status != EFI_SUCCESS) {
+                               log_err("Unable to increment TrialStateCtr variable\n");
+                               ret = -1;
+                               goto out;
+                       } else {
+                               ret = 0;

Why do you need this? ret should be 0 when you get here.

lib/fwu_updates/fwu.c line 100

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+                       }
+               }
+       } else {
+               trial_state_ctr = 0;
+               ret = 0;

the same here. You have ret=0 above that's why I think this is just an additional line here.

Quoted 16 lines
lib/fwu_updates/fwu.c new file +16 -0
+               status = efi_set_variable_int(L"TrialStateCtr",
+                                             &efi_global_variable_guid,
+                                             0,
+                                             0, &trial_state_ctr,
+                                             NULL);
+       }
+
+out:
+       free(mdata);
+       return ret;
+}
+
+u8 fwu_update_checks_pass(void)
+{
+       return !trial_state && boottime_check;
+}

lib/fwu_updates/fwu.c line 126

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+
+int fwu_boottime_checks(void)
+{
+       int ret;
+       struct udevice *dev;
+       u32 boot_idx, active_idx;
+
+       if (uclass_get_device(UCLASS_FWU_MDATA, 0, &dev) || !dev) {
+               log_err("FWU Metadata device not found\n");
+               boottime_check = 0;

All these boottime_check initialization to 0 seems to me useless.

lib/fwu_updates/fwu.c line 131

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+               return 0;
+       }
+
+       ret = fwu_mdata_check();
+       if (ret < 0) {

up to you but I would also use if (ret) here.

lib/fwu_updates/fwu.c line 132

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+               boottime_check = 0;

It is zero already when you get here that's why all these = 0 lines should be IMHO removed.

Quoted 6 lines
lib/fwu_updates/fwu.c new file +6 -0
+               return 0;
+       }
+
+       /*
+        * Get the Boot Index, i.e. the bank from
+        * which the platform has booted. This value

lib/fwu_updates/fwu.c line 148

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+        * gets passed from the ealier stage bootloader
+        * which booted u-boot, e.g. tf-a. If the
+        * boot index is not the same as the
+        * active_index read from the FWU metadata,
+        * update the active_index.
+        */
+       fwu_plat_get_bootidx(&boot_idx);
+       if (boot_idx >= CONFIG_FWU_NUM_BANKS) {
+               log_err("Received incorrect value of boot_index\n");
+               boottime_check = 0;

ditto.

lib/fwu_updates/fwu.c line 153

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+               return 0;
+       }
+
+       ret = fwu_get_active_index(&active_idx);
+       if (ret < 0) {

ditto.

lib/fwu_updates/fwu.c line 155

Quoted 2 lines
lib/fwu_updates/fwu.c new file +2 -0
+               log_err("Unable to read active_index\n");
+               boottime_check = 0;

ditto.

lib/fwu_updates/fwu.c line 163

Quoted 8 lines
lib/fwu_updates/fwu.c new file +8 -0
+               return 0;
+       }
+
+       if (boot_idx != active_idx) {
+               log_info("Boot idx %u is not matching active idx %u, changing active_idx\n",
+                        boot_idx, active_idx);
+               ret = fwu_update_active_index(boot_idx);
+               if (ret < 0)

ditto.

lib/fwu_updates/fwu.c line 164

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+                       boottime_check = 0;

ditto

lib/fwu_updates/fwu.c line 172

Quoted 8 lines
lib/fwu_updates/fwu.c new file +8 -0
+               else
+                       boottime_check = 1;
+
+               return 0;
+       }
+
+       ret = fwu_trial_state_check();
+       if (ret < 0)

ditto

lib/fwu_updates/fwu.c line 173

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+               boottime_check = 0;

ditto.

Quoted 8 lines
lib/fwu_updates/fwu.c new file +5 -1
+       else
+               boottime_check = 1;
+
+       return 0;
+}
--

2.17.1

Thanks,
Michal

Signature · 5 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs
#53 lore raw

Re: [PATCH v4 07/11] FWU: Add boot time checks as highlighted by the FWU specification

Quoted 148 lines · Michal Simek

po 7. 2. 2022 v 19:22 odesílatel Sughosh Ganu <[email protected]> napsal:

Quoted 83 lines

The FWU Multi Bank Update specification requires the Update Agent to carry out certain checks at the time of platform boot. The Update Agent is the component which is responsible for updating the firmware components and maintaining and keeping the metadata in sync.

The spec requires that the Update Agent perform the following checks at the time of boot
* Sanity check of both the metadata copies maintained by the platform.
* Get the boot index passed to U-Boot by the prior stage bootloader and use this value for metadata bookkeeping.
* Check if the system is booting in Trial State. If the system boots in the Trial State for more than a specified number of boot counts, change the Active Bank to be booting the platform from.

Add these checks in the board initialisation sequence, invoked after relocation.

---

Changes since V3:
* Change the TrialStateCtr efi variable attribute to remove the runtime attribute

 common/board_r.c      |   6 ++
 include/fwu.h         |   3 +
 lib/fwu_updates/fwu.c | 178 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 lib/fwu_updates/fwu.c
common/board_r.c +6 -0
index 31a59c585a..81678870b9 100644
@@ -78,6 +78,9 @@
 #ifdef CONFIG_EFI_SETUP_EARLY
 #include <efi_loader.h>
 #endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+#include <fwu.h>
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -805,6 +808,9 @@ static init_fnc_t init_sequence_r[] = {
 #endif
 #ifdef CONFIG_EFI_SETUP_EARLY
        (init_fnc_t)efi_init_obj_list,
+#endif
+#ifdef CONFIG_FWU_MULTI_BANK_UPDATE
+       fwu_boottime_checks,
 #endif
        run_main_loop,
 };
include/fwu.h +3 -0
index 90b8cd41e5..5a329d9ef7 100644
@@ -37,6 +37,9 @@ struct fwu_mdata_ops {
        EFI_GUID(0x8a7a84a0, 0x8387, 0x40f6, 0xab, 0x41, \
                 0xa8, 0xb9, 0xa5, 0xa6, 0x0d, 0x23)
 
+int fwu_boottime_checks(void);
+u8 fwu_update_checks_pass(void);
+
 int fwu_get_mdata(struct fwu_mdata **mdata);
 int fwu_update_mdata(struct fwu_mdata *mdata);
 int fwu_get_active_index(u32 *active_idx);
lib/fwu_updates/fwu.c new file +8 -0
new file mode 100644
index 0000000000..86933123e7
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2021, Linaro Limited
+ */
+
+#include <dm.h>
+#include <efi.h>
+#include <efi_loader.h>

lib/fwu_updates/fwu.c line 18

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+#include <efi_variable.h>
+#include <fwu.h>
+#include <fwu_mdata.h>
+#include <malloc.h>
+
+#include <linux/errno.h>
+#include <linux/types.h>
+
+static u8 trial_state = 0;
+static u8 boottime_check = 0;

you don't need to initialize them to 0. They are in bss which is setup to 0 already.

Quoted 4 lines
lib/fwu_updates/fwu.c new file +4 -0
+
+static int fwu_trial_state_check(void)
+{
+       int ret, i;

lib/fwu_updates/fwu.c line 32

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+       efi_status_t status;
+       efi_uintn_t var_size;
+       u16 trial_state_ctr;
+       u32 nimages, active_bank, var_attributes, active_idx;
+       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)

Isn't it easier to have if (ret) here?

lib/fwu_updates/fwu.c line 35

Quoted 3 lines
lib/fwu_updates/fwu.c new file +3 -0
+               return ret;
+
+       ret = 0;

Then you can remove this and the whole ret handling below is weird.

Quoted 14 lines
lib/fwu_updates/fwu.c new file +14 -0
+       nimages = CONFIG_FWU_NUM_IMAGES_PER_BANK;
+       active_bank = mdata->active_index;
+       img_entry = &mdata->img_entry[0];
+       for (i = 0; i < nimages; i++) {
+               img_bank_info = &img_entry[i].img_bank_info[active_bank];
+               if (!img_bank_info->accepted) {
+                       trial_state = 1;
+                       break;
+               }
+       }
+
+       if (trial_state) {
+               var_size = (efi_uintn_t)sizeof(trial_state_ctr);
+               log_info("System booting in Trial State\n");

lib/fwu_updates/fwu.c line 59

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+               var_attributes = EFI_VARIABLE_NON_VOLATILE |
+                       EFI_VARIABLE_BOOTSERVICE_ACCESS;
+               status = efi_get_variable_int(L"TrialStateCtr",
+                                             &efi_global_variable_guid,
+                                             &var_attributes,
+                                             &var_size, &trial_state_ctr,
+                                             NULL);
+               if (status != EFI_SUCCESS) {
+                       log_err("Unable to read TrialStateCtr variable\n");
+                       ret = -1;
Quoted 1 line

Any reason not to use standard return macros? The same I see below too.

This is because the ret is just used to indicate whether the function has returned successfully, or not. The value is not getting passed anywhere from the caller function -- it is only used to either return back or continue with further processing. And the error reason is also being printed in all cases.

I have incorporated all your other comments. Thanks.

-sughosh

Quoted 174 lines

lib/fwu_updates/fwu.c line 68

Quoted 10 lines
lib/fwu_updates/fwu.c new file +9 -0
+                       goto out;
+               }
+
+               ++trial_state_ctr;
+               if (trial_state_ctr > CONFIG_FWU_TRIAL_STATE_CNT) {
+                       log_info("Trial State count exceeded. Revert back to previous_active_index\n");
+                       active_idx = mdata->active_index;
+                       ret = fwu_revert_boot_index();
+                       if (ret < 0) {

I would use if (ret) here.

Quoted 17 lines
lib/fwu_updates/fwu.c new file +17 -0
+                               log_err("Unable to revert active_index\n");
+                               goto out;
+                       }
+
+                       trial_state_ctr = 0;
+                       status = efi_set_variable_int(L"TrialStateCtr",
+                                                     &efi_global_variable_guid,
+                                                     var_attributes,
+                                                     0,
+                                                     &trial_state_ctr, false);
+                       if (status != EFI_SUCCESS) {
+                               log_err("Unable to clear TrialStateCtr variable\n");
+                               ret = -1;
+                               goto out;
+                       }
+               } else {
+                       status = efi_set_variable_int(L"TrialStateCtr",

lib/fwu_updates/fwu.c line 95

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+                                                     &efi_global_variable_guid,
+                                                     var_attributes,
+                                                     var_size,
+                                                     &trial_state_ctr, false);
+                       if (status != EFI_SUCCESS) {
+                               log_err("Unable to increment TrialStateCtr variable\n");
+                               ret = -1;
+                               goto out;
+                       } else {
+                               ret = 0;

Why do you need this? ret should be 0 when you get here.

lib/fwu_updates/fwu.c line 100

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+                       }
+               }
+       } else {
+               trial_state_ctr = 0;
+               ret = 0;

the same here. You have ret=0 above that's why I think this is just an additional line here.

Quoted 16 lines
lib/fwu_updates/fwu.c new file +16 -0
+               status = efi_set_variable_int(L"TrialStateCtr",
+                                             &efi_global_variable_guid,
+                                             0,
+                                             0, &trial_state_ctr,
+                                             NULL);
+       }
+
+out:
+       free(mdata);
+       return ret;
+}
+
+u8 fwu_update_checks_pass(void)
+{
+       return !trial_state && boottime_check;
+}

lib/fwu_updates/fwu.c line 126

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+
+int fwu_boottime_checks(void)
+{
+       int ret;
+       struct udevice *dev;
+       u32 boot_idx, active_idx;
+
+       if (uclass_get_device(UCLASS_FWU_MDATA, 0, &dev) || !dev) {
+               log_err("FWU Metadata device not found\n");
+               boottime_check = 0;

All these boottime_check initialization to 0 seems to me useless.

lib/fwu_updates/fwu.c line 131

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+               return 0;
+       }
+
+       ret = fwu_mdata_check();
+       if (ret < 0) {

up to you but I would also use if (ret) here.

lib/fwu_updates/fwu.c line 132

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+               boottime_check = 0;

It is zero already when you get here that's why all these = 0 lines should be IMHO removed.

Quoted 6 lines
lib/fwu_updates/fwu.c new file +6 -0
+               return 0;
+       }
+
+       /*
+        * Get the Boot Index, i.e. the bank from
+        * which the platform has booted. This value

lib/fwu_updates/fwu.c line 148

Quoted 10 lines
lib/fwu_updates/fwu.c new file +10 -0
+        * gets passed from the ealier stage bootloader
+        * which booted u-boot, e.g. tf-a. If the
+        * boot index is not the same as the
+        * active_index read from the FWU metadata,
+        * update the active_index.
+        */
+       fwu_plat_get_bootidx(&boot_idx);
+       if (boot_idx >= CONFIG_FWU_NUM_BANKS) {
+               log_err("Received incorrect value of boot_index\n");
+               boottime_check = 0;

ditto.

lib/fwu_updates/fwu.c line 153

Quoted 5 lines
lib/fwu_updates/fwu.c new file +5 -0
+               return 0;
+       }
+
+       ret = fwu_get_active_index(&active_idx);
+       if (ret < 0) {

ditto.

lib/fwu_updates/fwu.c line 155

Quoted 2 lines
lib/fwu_updates/fwu.c new file +2 -0
+               log_err("Unable to read active_index\n");
+               boottime_check = 0;

ditto.

lib/fwu_updates/fwu.c line 163

Quoted 8 lines
lib/fwu_updates/fwu.c new file +8 -0
+               return 0;
+       }
+
+       if (boot_idx != active_idx) {
+               log_info("Boot idx %u is not matching active idx %u, changing active_idx\n",
+                        boot_idx, active_idx);
+               ret = fwu_update_active_index(boot_idx);
+               if (ret < 0)

ditto.

lib/fwu_updates/fwu.c line 164

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+                       boottime_check = 0;

ditto

lib/fwu_updates/fwu.c line 172

Quoted 8 lines
lib/fwu_updates/fwu.c new file +8 -0
+               else
+                       boottime_check = 1;
+
+               return 0;
+       }
+
+       ret = fwu_trial_state_check();
+       if (ret < 0)

ditto

lib/fwu_updates/fwu.c line 173

Quoted 1 line
lib/fwu_updates/fwu.c new file +1 -0
+               boottime_check = 0;

ditto.

Quoted 8 lines
lib/fwu_updates/fwu.c new file +5 -1
+       else
+               boottime_check = 1;
+
+       return 0;
+}
--

2.17.1

Thanks,
Michal

--
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs