diff mbox series

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

Message ID 20220207182001.31270-2-sughosh.ganu@linaro.org
State New
Headers show
Series FWU: Add support for FWU Multi Bank Update feature | expand

Commit Message

Sughosh Ganu Feb. 7, 2022, 6:19 p.m. UTC
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.

Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
---

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

Comments

Masami Hiramatsu Feb. 8, 2022, 9:33 a.m. UTC | #1
Hi Sughosh,

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

2022年2月8日(火) 3:21 Sughosh Ganu <sughosh.ganu@linaro.org>:
[snip]
> +
> +/**
> + * 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;

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.

> +       }
> +
> +       /*
> +        * 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));

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,
Sughosh Ganu Feb. 8, 2022, 10:24 a.m. UTC | #2
hi Masami,

On Tue, 8 Feb 2022 at 15:04, Masami Hiramatsu
<masami.hiramatsu@linaro.org> wrote:
>
> Hi Sughosh,
>
> Thanks for updating the series. I have some comment on this patch.
>
> 2022年2月8日(火) 3:21 Sughosh Ganu <sughosh.ganu@linaro.org>:
> [snip]
> > +
> > +/**
> > + * 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;
>
> 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.

>
> > +       }
> > +
> > +       /*
> > +        * 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));
>
> 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

>
> Thank you,
>
>
>
> --
> Masami Hiramatsu
Michal Simek Feb. 8, 2022, 10:56 a.m. UTC | #3
po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>
> 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.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>
> 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
>
> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> index 06ef3a4095..3bec6107f7 100644
> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> @@ -4,3 +4,10 @@
>   */
>
>  #include "stm32mp157a-dk1-u-boot.dtsi"
> +
> +/ {
> +       fwu-mdata {
> +               compatible = "u-boot,fwu-mdata";
> +               fwu-mdata-store = <&sdmmc1>;
> +       };
> +};
> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> new file mode 100644
> index 0000000000..c766b595ef
> --- /dev/null
> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> @@ -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.

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.

> +
> +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 Feb. 8, 2022, 11:23 a.m. UTC | #4
Hi Sughosh,

2022年2月8日(火) 19:24 Sughosh Ganu <sughosh.ganu@linaro.org>:
>
> hi Masami,
>
> On Tue, 8 Feb 2022 at 15:04, Masami Hiramatsu
> <masami.hiramatsu@linaro.org> wrote:
> >
> > Hi Sughosh,
> >
> > Thanks for updating the series. I have some comment on this patch.
> >
> > 2022年2月8日(火) 3:21 Sughosh Ganu <sughosh.ganu@linaro.org>:
> > [snip]
> > > +
> > > +/**
> > > + * 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;
> >
> > 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.

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,
Michal Simek Feb. 8, 2022, 11:31 a.m. UTC | #5
po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>
> 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.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>
> 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
>
> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> index 06ef3a4095..3bec6107f7 100644
> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> @@ -4,3 +4,10 @@
>   */
>
>  #include "stm32mp157a-dk1-u-boot.dtsi"
> +
> +/ {
> +       fwu-mdata {
> +               compatible = "u-boot,fwu-mdata";
> +               fwu-mdata-store = <&sdmmc1>;
> +       };
> +};
> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> new file mode 100644
> index 0000000000..c766b595ef
> --- /dev/null
> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> @@ -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>;
> +       };
> diff --git a/drivers/Kconfig b/drivers/Kconfig
> index b26ca8cf70..adc6079ecf 100644
> --- a/drivers/Kconfig
> +++ b/drivers/Kconfig
> @@ -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"
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 4e7cf28440..56f0f04874 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -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/
> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
> new file mode 100644
> index 0000000000..d6a21c8e19
> --- /dev/null
> +++ b/drivers/fwu-mdata/Kconfig
> @@ -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.
> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
> new file mode 100644
> index 0000000000..7fec7171f4
> --- /dev/null
> +++ b/drivers/fwu-mdata/Makefile
> @@ -0,0 +1,6 @@
> +# SPDX-License-Identifier: GPL-2.0+
> +#
> +# Copyright (c) 2022, Linaro Limited
> +#
> +
> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
> new file mode 100644
> index 0000000000..64b3051ecf
> --- /dev/null
> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
> @@ -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",
> +};
> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> index 0e26e1d138..d0ab1c9235 100644
> --- a/include/dm/uclass-id.h
> +++ b/include/dm/uclass-id.h
> @@ -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 */
> diff --git a/include/fwu.h b/include/fwu.h
> new file mode 100644
> index 0000000000..5a99c579fc
> --- /dev/null
> +++ b/include/fwu.h
> @@ -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_ */
> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
> new file mode 100644
> index 0000000000..701efbba03
> --- /dev/null
> +++ b/include/fwu_mdata.h
> @@ -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_ */
> --
> 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
Sughosh Ganu Feb. 8, 2022, 11:35 a.m. UTC | #6
On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
>
> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >
> > 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.
> >
> > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > ---
> >
> > 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
> >
> > diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > index 06ef3a4095..3bec6107f7 100644
> > --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > @@ -4,3 +4,10 @@
> >   */
> >
> >  #include "stm32mp157a-dk1-u-boot.dtsi"
> > +
> > +/ {
> > +       fwu-mdata {
> > +               compatible = "u-boot,fwu-mdata";
> > +               fwu-mdata-store = <&sdmmc1>;
> > +       };
> > +};
> > diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > new file mode 100644
> > index 0000000000..c766b595ef
> > --- /dev/null
> > +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > @@ -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.
>
> 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

 >
> > +
> > +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
Sughosh Ganu Feb. 8, 2022, 11:38 a.m. UTC | #7
On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
>
> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >
> > 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.
> >
> > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > ---
> >
> > 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
> >
> > diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > index 06ef3a4095..3bec6107f7 100644
> > --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > @@ -4,3 +4,10 @@
> >   */
> >
> >  #include "stm32mp157a-dk1-u-boot.dtsi"
> > +
> > +/ {
> > +       fwu-mdata {
> > +               compatible = "u-boot,fwu-mdata";
> > +               fwu-mdata-store = <&sdmmc1>;
> > +       };
> > +};
> > diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > new file mode 100644
> > index 0000000000..c766b595ef
> > --- /dev/null
> > +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > @@ -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>;
> > +       };
> > diff --git a/drivers/Kconfig b/drivers/Kconfig
> > index b26ca8cf70..adc6079ecf 100644
> > --- a/drivers/Kconfig
> > +++ b/drivers/Kconfig
> > @@ -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"
> > diff --git a/drivers/Makefile b/drivers/Makefile
> > index 4e7cf28440..56f0f04874 100644
> > --- a/drivers/Makefile
> > +++ b/drivers/Makefile
> > @@ -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/
> > diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
> > new file mode 100644
> > index 0000000000..d6a21c8e19
> > --- /dev/null
> > +++ b/drivers/fwu-mdata/Kconfig
> > @@ -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.
> > diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
> > new file mode 100644
> > index 0000000000..7fec7171f4
> > --- /dev/null
> > +++ b/drivers/fwu-mdata/Makefile
> > @@ -0,0 +1,6 @@
> > +# SPDX-License-Identifier: GPL-2.0+
> > +#
> > +# Copyright (c) 2022, Linaro Limited
> > +#
> > +
> > +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
> > diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
> > new file mode 100644
> > index 0000000000..64b3051ecf
> > --- /dev/null
> > +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
> > @@ -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",
> > +};
> > diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> > index 0e26e1d138..d0ab1c9235 100644
> > --- a/include/dm/uclass-id.h
> > +++ b/include/dm/uclass-id.h
> > @@ -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 */
> > diff --git a/include/fwu.h b/include/fwu.h
> > new file mode 100644
> > index 0000000000..5a99c579fc
> > --- /dev/null
> > +++ b/include/fwu.h
> > @@ -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_ */
> > diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
> > new file mode 100644
> > index 0000000000..701efbba03
> > --- /dev/null
> > +++ b/include/fwu_mdata.h
> > @@ -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_ */
> > --
> > 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.


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.

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

Okay.

-sughosh

>
> 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
Michal Simek Feb. 8, 2022, 11:39 a.m. UTC | #8
On 2/8/22 12:35, Sughosh Ganu wrote:
> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
>>
>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>
>>> 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.
>>>
>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>> ---
>>>
>>> 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
>>>
>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> index 06ef3a4095..3bec6107f7 100644
>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> @@ -4,3 +4,10 @@
>>>    */
>>>
>>>   #include "stm32mp157a-dk1-u-boot.dtsi"
>>> +
>>> +/ {
>>> +       fwu-mdata {
>>> +               compatible = "u-boot,fwu-mdata";
>>> +               fwu-mdata-store = <&sdmmc1>;
>>> +       };
>>> +};
>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>> new file mode 100644
>>> index 0000000000..c766b595ef
>>> --- /dev/null
>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>> @@ -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.
>>
>> 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.

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
Michal Simek Feb. 8, 2022, 11:44 a.m. UTC | #9
On 2/8/22 12:38, Sughosh Ganu wrote:
> On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
>>
>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>
>>> 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.
>>>
>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>> ---
>>>
>>> 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
>>>
>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> index 06ef3a4095..3bec6107f7 100644
>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>> @@ -4,3 +4,10 @@
>>>    */
>>>
>>>   #include "stm32mp157a-dk1-u-boot.dtsi"
>>> +
>>> +/ {
>>> +       fwu-mdata {
>>> +               compatible = "u-boot,fwu-mdata";
>>> +               fwu-mdata-store = <&sdmmc1>;
>>> +       };
>>> +};
>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>> new file mode 100644
>>> index 0000000000..c766b595ef
>>> --- /dev/null
>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>> @@ -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>;
>>> +       };
>>> diff --git a/drivers/Kconfig b/drivers/Kconfig
>>> index b26ca8cf70..adc6079ecf 100644
>>> --- a/drivers/Kconfig
>>> +++ b/drivers/Kconfig
>>> @@ -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"
>>> diff --git a/drivers/Makefile b/drivers/Makefile
>>> index 4e7cf28440..56f0f04874 100644
>>> --- a/drivers/Makefile
>>> +++ b/drivers/Makefile
>>> @@ -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/
>>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
>>> new file mode 100644
>>> index 0000000000..d6a21c8e19
>>> --- /dev/null
>>> +++ b/drivers/fwu-mdata/Kconfig
>>> @@ -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.
>>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
>>> new file mode 100644
>>> index 0000000000..7fec7171f4
>>> --- /dev/null
>>> +++ b/drivers/fwu-mdata/Makefile
>>> @@ -0,0 +1,6 @@
>>> +# SPDX-License-Identifier: GPL-2.0+
>>> +#
>>> +# Copyright (c) 2022, Linaro Limited
>>> +#
>>> +
>>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
>>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>> new file mode 100644
>>> index 0000000000..64b3051ecf
>>> --- /dev/null
>>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>> @@ -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",
>>> +};
>>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
>>> index 0e26e1d138..d0ab1c9235 100644
>>> --- a/include/dm/uclass-id.h
>>> +++ b/include/dm/uclass-id.h
>>> @@ -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 */
>>> diff --git a/include/fwu.h b/include/fwu.h
>>> new file mode 100644
>>> index 0000000000..5a99c579fc
>>> --- /dev/null
>>> +++ b/include/fwu.h
>>> @@ -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_ */
>>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
>>> new file mode 100644
>>> index 0000000000..701efbba03
>>> --- /dev/null
>>> +++ b/include/fwu_mdata.h
>>> @@ -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_ */
>>> --
>>> 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.
> 
> 
> 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
Sughosh Ganu Feb. 8, 2022, 11:54 a.m. UTC | #10
On Tue, 8 Feb 2022 at 17:15, Michal Simek <michal.simek@xilinx.com> wrote:
>
>
>
> On 2/8/22 12:38, Sughosh Ganu wrote:
> > On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
> >>
> >> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >>>
> >>> 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.
> >>>
> >>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> >>> ---
> >>>
> >>> 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
> >>>
> >>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>> index 06ef3a4095..3bec6107f7 100644
> >>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>> @@ -4,3 +4,10 @@
> >>>    */
> >>>
> >>>   #include "stm32mp157a-dk1-u-boot.dtsi"
> >>> +
> >>> +/ {
> >>> +       fwu-mdata {
> >>> +               compatible = "u-boot,fwu-mdata";
> >>> +               fwu-mdata-store = <&sdmmc1>;
> >>> +       };
> >>> +};
> >>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>> new file mode 100644
> >>> index 0000000000..c766b595ef
> >>> --- /dev/null
> >>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>> @@ -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>;
> >>> +       };
> >>> diff --git a/drivers/Kconfig b/drivers/Kconfig
> >>> index b26ca8cf70..adc6079ecf 100644
> >>> --- a/drivers/Kconfig
> >>> +++ b/drivers/Kconfig
> >>> @@ -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"
> >>> diff --git a/drivers/Makefile b/drivers/Makefile
> >>> index 4e7cf28440..56f0f04874 100644
> >>> --- a/drivers/Makefile
> >>> +++ b/drivers/Makefile
> >>> @@ -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/
> >>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
> >>> new file mode 100644
> >>> index 0000000000..d6a21c8e19
> >>> --- /dev/null
> >>> +++ b/drivers/fwu-mdata/Kconfig
> >>> @@ -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.
> >>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
> >>> new file mode 100644
> >>> index 0000000000..7fec7171f4
> >>> --- /dev/null
> >>> +++ b/drivers/fwu-mdata/Makefile
> >>> @@ -0,0 +1,6 @@
> >>> +# SPDX-License-Identifier: GPL-2.0+
> >>> +#
> >>> +# Copyright (c) 2022, Linaro Limited
> >>> +#
> >>> +
> >>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
> >>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>> new file mode 100644
> >>> index 0000000000..64b3051ecf
> >>> --- /dev/null
> >>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>> @@ -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",
> >>> +};
> >>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> >>> index 0e26e1d138..d0ab1c9235 100644
> >>> --- a/include/dm/uclass-id.h
> >>> +++ b/include/dm/uclass-id.h
> >>> @@ -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 */
> >>> diff --git a/include/fwu.h b/include/fwu.h
> >>> new file mode 100644
> >>> index 0000000000..5a99c579fc
> >>> --- /dev/null
> >>> +++ b/include/fwu.h
> >>> @@ -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_ */
> >>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
> >>> new file mode 100644
> >>> index 0000000000..701efbba03
> >>> --- /dev/null
> >>> +++ b/include/fwu_mdata.h
> >>> @@ -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_ */
> >>> --
> >>> 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.
> >
> >
> > 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)

You can download the mkeficapsule series from linaro's patchwork[1].
>
> 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


> It is not that far from using the same defconfig with bisicting tree.
> You can definitely avoid it in your series.
>
> Thanks,
> Michal
>
Michal Simek Feb. 8, 2022, 11:59 a.m. UTC | #11
On 2/8/22 12:54, Sughosh Ganu wrote:
> On Tue, 8 Feb 2022 at 17:15, Michal Simek <michal.simek@xilinx.com> wrote:
>>
>>
>>
>> On 2/8/22 12:38, Sughosh Ganu wrote:
>>> On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
>>>>
>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>>>
>>>>> 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.
>>>>>
>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>>>> ---
>>>>>
>>>>> 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
>>>>>
>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>> index 06ef3a4095..3bec6107f7 100644
>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>> @@ -4,3 +4,10 @@
>>>>>     */
>>>>>
>>>>>    #include "stm32mp157a-dk1-u-boot.dtsi"
>>>>> +
>>>>> +/ {
>>>>> +       fwu-mdata {
>>>>> +               compatible = "u-boot,fwu-mdata";
>>>>> +               fwu-mdata-store = <&sdmmc1>;
>>>>> +       };
>>>>> +};
>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>> new file mode 100644
>>>>> index 0000000000..c766b595ef
>>>>> --- /dev/null
>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>> @@ -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>;
>>>>> +       };
>>>>> diff --git a/drivers/Kconfig b/drivers/Kconfig
>>>>> index b26ca8cf70..adc6079ecf 100644
>>>>> --- a/drivers/Kconfig
>>>>> +++ b/drivers/Kconfig
>>>>> @@ -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"
>>>>> diff --git a/drivers/Makefile b/drivers/Makefile
>>>>> index 4e7cf28440..56f0f04874 100644
>>>>> --- a/drivers/Makefile
>>>>> +++ b/drivers/Makefile
>>>>> @@ -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/
>>>>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
>>>>> new file mode 100644
>>>>> index 0000000000..d6a21c8e19
>>>>> --- /dev/null
>>>>> +++ b/drivers/fwu-mdata/Kconfig
>>>>> @@ -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.
>>>>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
>>>>> new file mode 100644
>>>>> index 0000000000..7fec7171f4
>>>>> --- /dev/null
>>>>> +++ b/drivers/fwu-mdata/Makefile
>>>>> @@ -0,0 +1,6 @@
>>>>> +# SPDX-License-Identifier: GPL-2.0+
>>>>> +#
>>>>> +# Copyright (c) 2022, Linaro Limited
>>>>> +#
>>>>> +
>>>>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
>>>>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>>>> new file mode 100644
>>>>> index 0000000000..64b3051ecf
>>>>> --- /dev/null
>>>>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>>>> @@ -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",
>>>>> +};
>>>>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
>>>>> index 0e26e1d138..d0ab1c9235 100644
>>>>> --- a/include/dm/uclass-id.h
>>>>> +++ b/include/dm/uclass-id.h
>>>>> @@ -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 */
>>>>> diff --git a/include/fwu.h b/include/fwu.h
>>>>> new file mode 100644
>>>>> index 0000000000..5a99c579fc
>>>>> --- /dev/null
>>>>> +++ b/include/fwu.h
>>>>> @@ -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_ */
>>>>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
>>>>> new file mode 100644
>>>>> index 0000000000..701efbba03
>>>>> --- /dev/null
>>>>> +++ b/include/fwu_mdata.h
>>>>> @@ -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_ */
>>>>> --
>>>>> 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.
>>>
>>>
>>> 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)
> 
> 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.

>>
>> 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.

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
Sughosh Ganu Feb. 8, 2022, 12:07 p.m. UTC | #12
On Tue, 8 Feb 2022 at 17:29, Michal Simek <michal.simek@xilinx.com> wrote:
>
>
>
> On 2/8/22 12:54, Sughosh Ganu wrote:
> > On Tue, 8 Feb 2022 at 17:15, Michal Simek <michal.simek@xilinx.com> wrote:
> >>
> >>
> >>
> >> On 2/8/22 12:38, Sughosh Ganu wrote:
> >>> On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
> >>>>
> >>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >>>>>
> >>>>> 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.
> >>>>>
> >>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> >>>>> ---
> >>>>>
> >>>>> 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
> >>>>>
> >>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>> index 06ef3a4095..3bec6107f7 100644
> >>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>> @@ -4,3 +4,10 @@
> >>>>>     */
> >>>>>
> >>>>>    #include "stm32mp157a-dk1-u-boot.dtsi"
> >>>>> +
> >>>>> +/ {
> >>>>> +       fwu-mdata {
> >>>>> +               compatible = "u-boot,fwu-mdata";
> >>>>> +               fwu-mdata-store = <&sdmmc1>;
> >>>>> +       };
> >>>>> +};
> >>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>> new file mode 100644
> >>>>> index 0000000000..c766b595ef
> >>>>> --- /dev/null
> >>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>> @@ -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>;
> >>>>> +       };
> >>>>> diff --git a/drivers/Kconfig b/drivers/Kconfig
> >>>>> index b26ca8cf70..adc6079ecf 100644
> >>>>> --- a/drivers/Kconfig
> >>>>> +++ b/drivers/Kconfig
> >>>>> @@ -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"
> >>>>> diff --git a/drivers/Makefile b/drivers/Makefile
> >>>>> index 4e7cf28440..56f0f04874 100644
> >>>>> --- a/drivers/Makefile
> >>>>> +++ b/drivers/Makefile
> >>>>> @@ -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/
> >>>>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
> >>>>> new file mode 100644
> >>>>> index 0000000000..d6a21c8e19
> >>>>> --- /dev/null
> >>>>> +++ b/drivers/fwu-mdata/Kconfig
> >>>>> @@ -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.
> >>>>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
> >>>>> new file mode 100644
> >>>>> index 0000000000..7fec7171f4
> >>>>> --- /dev/null
> >>>>> +++ b/drivers/fwu-mdata/Makefile
> >>>>> @@ -0,0 +1,6 @@
> >>>>> +# SPDX-License-Identifier: GPL-2.0+
> >>>>> +#
> >>>>> +# Copyright (c) 2022, Linaro Limited
> >>>>> +#
> >>>>> +
> >>>>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
> >>>>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>>>> new file mode 100644
> >>>>> index 0000000000..64b3051ecf
> >>>>> --- /dev/null
> >>>>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>>>> @@ -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",
> >>>>> +};
> >>>>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> >>>>> index 0e26e1d138..d0ab1c9235 100644
> >>>>> --- a/include/dm/uclass-id.h
> >>>>> +++ b/include/dm/uclass-id.h
> >>>>> @@ -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 */
> >>>>> diff --git a/include/fwu.h b/include/fwu.h
> >>>>> new file mode 100644
> >>>>> index 0000000000..5a99c579fc
> >>>>> --- /dev/null
> >>>>> +++ b/include/fwu.h
> >>>>> @@ -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_ */
> >>>>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
> >>>>> new file mode 100644
> >>>>> index 0000000000..701efbba03
> >>>>> --- /dev/null
> >>>>> +++ b/include/fwu_mdata.h
> >>>>> @@ -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_ */
> >>>>> --
> >>>>> 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.
> >>>
> >>>
> >>> 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)
> >
> > 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.
>
> >>
> >> 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.
>
> 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

>
> 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
>
>
Michal Simek Feb. 8, 2022, 12:14 p.m. UTC | #13
On 2/8/22 13:07, Sughosh Ganu wrote:
> On Tue, 8 Feb 2022 at 17:29, Michal Simek <michal.simek@xilinx.com> wrote:
>>
>>
>>
>> On 2/8/22 12:54, Sughosh Ganu wrote:
>>> On Tue, 8 Feb 2022 at 17:15, Michal Simek <michal.simek@xilinx.com> wrote:
>>>>
>>>>
>>>>
>>>> On 2/8/22 12:38, Sughosh Ganu wrote:
>>>>> On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
>>>>>>
>>>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>>>>>
>>>>>>> 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.
>>>>>>>
>>>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>>>>>> ---
>>>>>>>
>>>>>>> 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
>>>>>>>
>>>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>> index 06ef3a4095..3bec6107f7 100644
>>>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>> @@ -4,3 +4,10 @@
>>>>>>>      */
>>>>>>>
>>>>>>>     #include "stm32mp157a-dk1-u-boot.dtsi"
>>>>>>> +
>>>>>>> +/ {
>>>>>>> +       fwu-mdata {
>>>>>>> +               compatible = "u-boot,fwu-mdata";
>>>>>>> +               fwu-mdata-store = <&sdmmc1>;
>>>>>>> +       };
>>>>>>> +};
>>>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..c766b595ef
>>>>>>> --- /dev/null
>>>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>>> @@ -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>;
>>>>>>> +       };
>>>>>>> diff --git a/drivers/Kconfig b/drivers/Kconfig
>>>>>>> index b26ca8cf70..adc6079ecf 100644
>>>>>>> --- a/drivers/Kconfig
>>>>>>> +++ b/drivers/Kconfig
>>>>>>> @@ -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"
>>>>>>> diff --git a/drivers/Makefile b/drivers/Makefile
>>>>>>> index 4e7cf28440..56f0f04874 100644
>>>>>>> --- a/drivers/Makefile
>>>>>>> +++ b/drivers/Makefile
>>>>>>> @@ -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/
>>>>>>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..d6a21c8e19
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/fwu-mdata/Kconfig
>>>>>>> @@ -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.
>>>>>>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..7fec7171f4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/fwu-mdata/Makefile
>>>>>>> @@ -0,0 +1,6 @@
>>>>>>> +# SPDX-License-Identifier: GPL-2.0+
>>>>>>> +#
>>>>>>> +# Copyright (c) 2022, Linaro Limited
>>>>>>> +#
>>>>>>> +
>>>>>>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
>>>>>>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..64b3051ecf
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
>>>>>>> @@ -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",
>>>>>>> +};
>>>>>>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
>>>>>>> index 0e26e1d138..d0ab1c9235 100644
>>>>>>> --- a/include/dm/uclass-id.h
>>>>>>> +++ b/include/dm/uclass-id.h
>>>>>>> @@ -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 */
>>>>>>> diff --git a/include/fwu.h b/include/fwu.h
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..5a99c579fc
>>>>>>> --- /dev/null
>>>>>>> +++ b/include/fwu.h
>>>>>>> @@ -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_ */
>>>>>>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
>>>>>>> new file mode 100644
>>>>>>> index 0000000000..701efbba03
>>>>>>> --- /dev/null
>>>>>>> +++ b/include/fwu_mdata.h
>>>>>>> @@ -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_ */
>>>>>>> --
>>>>>>> 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.
>>>>>
>>>>>
>>>>> 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)
>>>
>>> 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.
>>
>>>>
>>>> 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.
>>
>> 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.

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
Sughosh Ganu Feb. 8, 2022, 12:49 p.m. UTC | #14
On Tue, 8 Feb 2022 at 17:44, Michal Simek <michal.simek@xilinx.com> wrote:
>
>
>
> On 2/8/22 13:07, Sughosh Ganu wrote:
> > On Tue, 8 Feb 2022 at 17:29, Michal Simek <michal.simek@xilinx.com> wrote:
> >>
> >>
> >>
> >> On 2/8/22 12:54, Sughosh Ganu wrote:
> >>> On Tue, 8 Feb 2022 at 17:15, Michal Simek <michal.simek@xilinx.com> wrote:
> >>>>
> >>>>
> >>>>
> >>>> On 2/8/22 12:38, Sughosh Ganu wrote:
> >>>>> On Tue, 8 Feb 2022 at 17:01, Michal Simek <monstr@monstr.eu> wrote:
> >>>>>>
> >>>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >>>>>>>
> >>>>>>> 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.
> >>>>>>>
> >>>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> >>>>>>> ---
> >>>>>>>
> >>>>>>> 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
> >>>>>>>
> >>>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>>> index 06ef3a4095..3bec6107f7 100644
> >>>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>>> @@ -4,3 +4,10 @@
> >>>>>>>      */
> >>>>>>>
> >>>>>>>     #include "stm32mp157a-dk1-u-boot.dtsi"
> >>>>>>> +
> >>>>>>> +/ {
> >>>>>>> +       fwu-mdata {
> >>>>>>> +               compatible = "u-boot,fwu-mdata";
> >>>>>>> +               fwu-mdata-store = <&sdmmc1>;
> >>>>>>> +       };
> >>>>>>> +};
> >>>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..c766b595ef
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>>>> @@ -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>;
> >>>>>>> +       };
> >>>>>>> diff --git a/drivers/Kconfig b/drivers/Kconfig
> >>>>>>> index b26ca8cf70..adc6079ecf 100644
> >>>>>>> --- a/drivers/Kconfig
> >>>>>>> +++ b/drivers/Kconfig
> >>>>>>> @@ -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"
> >>>>>>> diff --git a/drivers/Makefile b/drivers/Makefile
> >>>>>>> index 4e7cf28440..56f0f04874 100644
> >>>>>>> --- a/drivers/Makefile
> >>>>>>> +++ b/drivers/Makefile
> >>>>>>> @@ -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/
> >>>>>>> diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..d6a21c8e19
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/fwu-mdata/Kconfig
> >>>>>>> @@ -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.
> >>>>>>> diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..7fec7171f4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/fwu-mdata/Makefile
> >>>>>>> @@ -0,0 +1,6 @@
> >>>>>>> +# SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +#
> >>>>>>> +# Copyright (c) 2022, Linaro Limited
> >>>>>>> +#
> >>>>>>> +
> >>>>>>> +obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
> >>>>>>> diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..64b3051ecf
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
> >>>>>>> @@ -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",
> >>>>>>> +};
> >>>>>>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> >>>>>>> index 0e26e1d138..d0ab1c9235 100644
> >>>>>>> --- a/include/dm/uclass-id.h
> >>>>>>> +++ b/include/dm/uclass-id.h
> >>>>>>> @@ -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 */
> >>>>>>> diff --git a/include/fwu.h b/include/fwu.h
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..5a99c579fc
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/include/fwu.h
> >>>>>>> @@ -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_ */
> >>>>>>> diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
> >>>>>>> new file mode 100644
> >>>>>>> index 0000000000..701efbba03
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/include/fwu_mdata.h
> >>>>>>> @@ -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_ */
> >>>>>>> --
> >>>>>>> 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.
> >>>>>
> >>>>>
> >>>>> 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)
> >>>
> >>> 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.
> >>
> >>>>
> >>>> 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.
> >>
> >> 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.
>
> 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

>
> Thanks,
> Michal
>
Masami Hiramatsu Feb. 8, 2022, 1:36 p.m. UTC | #15
2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
>
> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
> >
> > po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> > >
> > > 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.
> > >
> > > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > > ---
> > >
> > > 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
> > >
> > > diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > > index 06ef3a4095..3bec6107f7 100644
> > > --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > > +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> > > @@ -4,3 +4,10 @@
> > >   */
> > >
> > >  #include "stm32mp157a-dk1-u-boot.dtsi"
> > > +
> > > +/ {
> > > +       fwu-mdata {
> > > +               compatible = "u-boot,fwu-mdata";
> > > +               fwu-mdata-store = <&sdmmc1>;
> > > +       };
> > > +};
> > > diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > > new file mode 100644
> > > index 0000000000..c766b595ef
> > > --- /dev/null
> > > +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> > > @@ -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.
> >
> > 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.

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,



>
> -sughosh
>
>  >
> > > +
> > > +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
Michal Simek Feb. 8, 2022, 1:45 p.m. UTC | #16
On 2/8/22 14:36, Masami Hiramatsu wrote:
> 2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
>>
>> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
>>>
>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>>
>>>> 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.
>>>>
>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>>> ---
>>>>
>>>> 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
>>>>
>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>> index 06ef3a4095..3bec6107f7 100644
>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>> @@ -4,3 +4,10 @@
>>>>    */
>>>>
>>>>   #include "stm32mp157a-dk1-u-boot.dtsi"
>>>> +
>>>> +/ {
>>>> +       fwu-mdata {
>>>> +               compatible = "u-boot,fwu-mdata";
>>>> +               fwu-mdata-store = <&sdmmc1>;
>>>> +       };
>>>> +};
>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>> new file mode 100644
>>>> index 0000000000..c766b595ef
>>>> --- /dev/null
>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>> @@ -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.
>>>
>>> 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.
> 
> 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.

> 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
Masami Hiramatsu Feb. 8, 2022, 2:14 p.m. UTC | #17
2022年2月8日(火) 22:45 Michal Simek <michal.simek@xilinx.com>:
>
>
>
> On 2/8/22 14:36, Masami Hiramatsu wrote:
> > 2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
> >>
> >> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
> >>>
> >>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >>>>
> >>>> 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.
> >>>>
> >>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> >>>> ---
> >>>>
> >>>> 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
> >>>>
> >>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>> index 06ef3a4095..3bec6107f7 100644
> >>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>> @@ -4,3 +4,10 @@
> >>>>    */
> >>>>
> >>>>   #include "stm32mp157a-dk1-u-boot.dtsi"
> >>>> +
> >>>> +/ {
> >>>> +       fwu-mdata {
> >>>> +               compatible = "u-boot,fwu-mdata";
> >>>> +               fwu-mdata-store = <&sdmmc1>;
> >>>> +       };
> >>>> +};
> >>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>> new file mode 100644
> >>>> index 0000000000..c766b595ef
> >>>> --- /dev/null
> >>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>> @@ -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.
> >>>
> >>> 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.
> >
> > 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.

>
> > 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,


>
> Thanks,
> Michal
Michal Simek Feb. 8, 2022, 2:27 p.m. UTC | #18
On 2/8/22 15:14, Masami Hiramatsu wrote:
> 2022年2月8日(火) 22:45 Michal Simek <michal.simek@xilinx.com>:
>>
>>
>>
>> On 2/8/22 14:36, Masami Hiramatsu wrote:
>>> 2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
>>>>
>>>> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
>>>>>
>>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>>>>
>>>>>> 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.
>>>>>>
>>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>>>>> ---
>>>>>>
>>>>>> 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
>>>>>>
>>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>> index 06ef3a4095..3bec6107f7 100644
>>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>> @@ -4,3 +4,10 @@
>>>>>>     */
>>>>>>
>>>>>>    #include "stm32mp157a-dk1-u-boot.dtsi"
>>>>>> +
>>>>>> +/ {
>>>>>> +       fwu-mdata {
>>>>>> +               compatible = "u-boot,fwu-mdata";
>>>>>> +               fwu-mdata-store = <&sdmmc1>;
>>>>>> +       };
>>>>>> +};
>>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>> new file mode 100644
>>>>>> index 0000000000..c766b595ef
>>>>>> --- /dev/null
>>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>> @@ -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.
>>>>>
>>>>> 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.
>>>
>>> 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.
> 
>>
>>> 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>;

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
Masami Hiramatsu Feb. 8, 2022, 11:39 p.m. UTC | #19
Hi Michal,

2022年2月8日(火) 23:27 Michal Simek <michal.simek@xilinx.com>:
>
>
>
> On 2/8/22 15:14, Masami Hiramatsu wrote:
> > 2022年2月8日(火) 22:45 Michal Simek <michal.simek@xilinx.com>:
> >>
> >>
> >>
> >> On 2/8/22 14:36, Masami Hiramatsu wrote:
> >>> 2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
> >>>>
> >>>> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
> >>>>>
> >>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
> >>>>>>
> >>>>>> 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.
> >>>>>>
> >>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> >>>>>> ---
> >>>>>>
> >>>>>> 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
> >>>>>>
> >>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>> index 06ef3a4095..3bec6107f7 100644
> >>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
> >>>>>> @@ -4,3 +4,10 @@
> >>>>>>     */
> >>>>>>
> >>>>>>    #include "stm32mp157a-dk1-u-boot.dtsi"
> >>>>>> +
> >>>>>> +/ {
> >>>>>> +       fwu-mdata {
> >>>>>> +               compatible = "u-boot,fwu-mdata";
> >>>>>> +               fwu-mdata-store = <&sdmmc1>;
> >>>>>> +       };
> >>>>>> +};
> >>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>>> new file mode 100644
> >>>>>> index 0000000000..c766b595ef
> >>>>>> --- /dev/null
> >>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
> >>>>>> @@ -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.
> >>>>>
> >>>>> 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.
> >>>
> >>> 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.
> >
> >>
> >>> 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>;
>
> 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)

> 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,


>
> Thanks,
> Michal
Michal Simek Feb. 9, 2022, 7:21 a.m. UTC | #20
Hi,

On 2/9/22 00:39, Masami Hiramatsu wrote:
> Hi Michal,
> 
> 2022年2月8日(火) 23:27 Michal Simek <michal.simek@xilinx.com>:
>>
>>
>>
>> On 2/8/22 15:14, Masami Hiramatsu wrote:
>>> 2022年2月8日(火) 22:45 Michal Simek <michal.simek@xilinx.com>:
>>>>
>>>>
>>>>
>>>> On 2/8/22 14:36, Masami Hiramatsu wrote:
>>>>> 2022年2月8日(火) 20:35 Sughosh Ganu <sughosh.ganu@linaro.org>:
>>>>>>
>>>>>> On Tue, 8 Feb 2022 at 16:26, Michal Simek <monstr@monstr.eu> wrote:
>>>>>>>
>>>>>>> po 7. 2. 2022 v 19:21 odesílatel Sughosh Ganu <sughosh.ganu@linaro.org> napsal:
>>>>>>>>
>>>>>>>> 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.
>>>>>>>>
>>>>>>>> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
>>>>>>>> ---
>>>>>>>>
>>>>>>>> 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
>>>>>>>>
>>>>>>>> diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>>> index 06ef3a4095..3bec6107f7 100644
>>>>>>>> --- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>>> +++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
>>>>>>>> @@ -4,3 +4,10 @@
>>>>>>>>      */
>>>>>>>>
>>>>>>>>     #include "stm32mp157a-dk1-u-boot.dtsi"
>>>>>>>> +
>>>>>>>> +/ {
>>>>>>>> +       fwu-mdata {
>>>>>>>> +               compatible = "u-boot,fwu-mdata";
>>>>>>>> +               fwu-mdata-store = <&sdmmc1>;
>>>>>>>> +       };
>>>>>>>> +};
>>>>>>>> diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000000..c766b595ef
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
>>>>>>>> @@ -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.
>>>>>>>
>>>>>>> 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.
>>>>>
>>>>> 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.
>>>
>>>>
>>>>> 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>;
>>
>> 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.


>> 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
diff mbox series

Patch

diff --git a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
index 06ef3a4095..3bec6107f7 100644
--- a/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
+++ b/arch/arm/dts/stm32mp157c-dk2-u-boot.dtsi
@@ -4,3 +4,10 @@ 
  */
 
 #include "stm32mp157a-dk1-u-boot.dtsi"
+
+/ {
+	fwu-mdata {
+		compatible = "u-boot,fwu-mdata";
+		fwu-mdata-store = <&sdmmc1>;
+	};
+};
diff --git a/doc/device-tree-bindings/firmware/fwu-mdata.txt b/doc/device-tree-bindings/firmware/fwu-mdata.txt
new file mode 100644
index 0000000000..c766b595ef
--- /dev/null
+++ b/doc/device-tree-bindings/firmware/fwu-mdata.txt
@@ -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>;
+	};
diff --git a/drivers/Kconfig b/drivers/Kconfig
index b26ca8cf70..adc6079ecf 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -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"
diff --git a/drivers/Makefile b/drivers/Makefile
index 4e7cf28440..56f0f04874 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -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/
diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig
new file mode 100644
index 0000000000..d6a21c8e19
--- /dev/null
+++ b/drivers/fwu-mdata/Kconfig
@@ -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.
diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile
new file mode 100644
index 0000000000..7fec7171f4
--- /dev/null
+++ b/drivers/fwu-mdata/Makefile
@@ -0,0 +1,6 @@ 
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2022, Linaro Limited
+#
+
+obj-$(CONFIG_DM_FWU_MDATA) += fwu-mdata-uclass.o
diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c
new file mode 100644
index 0000000000..64b3051ecf
--- /dev/null
+++ b/drivers/fwu-mdata/fwu-mdata-uclass.c
@@ -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",
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 0e26e1d138..d0ab1c9235 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -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 */
diff --git a/include/fwu.h b/include/fwu.h
new file mode 100644
index 0000000000..5a99c579fc
--- /dev/null
+++ b/include/fwu.h
@@ -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_ */
diff --git a/include/fwu_mdata.h b/include/fwu_mdata.h
new file mode 100644
index 0000000000..701efbba03
--- /dev/null
+++ b/include/fwu_mdata.h
@@ -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_ */