Skip to contentSkip to conversation

FWU: Handle meta-data in common code

PATCH Superseded [email protected] 5 participants 17 messages Feb 28, 2023 – Mar 6, 2023

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover FWU: Handle meta-data in common code

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Changes since v3:
* Fix error log wording
* call fwu_write_mdata() with part & PRIMARY_PART ? true: false

Changes since v2:
* Drop whitespace changes
* Fix missing mdata copy before return

Changes since v1:
* Fix typos and misc cosmetic changes
* Catch error returns

Jassi Brar (6):
dt/bindings: fwu-mdata-mtd: drop changes outside FWU
fwu: gpt: use cached meta-data partition numbers
fwu: move meta-data management in core
fwu: gpt: implement read_mdata and write_mdata callbacks
fwu: meta-data: switch to management by common code
fwu: rename fwu_get_verified_mdata to fwu_get_mdata

 cmd/fwu_mdata.c                               |  17 +-
 .../firmware/fwu-mdata-mtd.yaml               | 105 ++++++-
 drivers/fwu-mdata/fwu-mdata-uclass.c          | 151 +--------
 drivers/fwu-mdata/gpt_blk.c                   | 175 +++--------
 include/fwu.h                                 | 198 ++----------
 lib/fwu_updates/fwu.c                         | 295 +++++++-----------
 6 files changed, 295 insertions(+), 646 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/6 dt/bindings: fwu-mdata-mtd: drop changes outside FWU

From: Jassi Brar <[email protected]>

Any requirement of FWU should not require changes to bindings of other subsystems. For example, for mtd-backed storage we can do without requiring 'fixed-partitions' children to also carry 'uuid', a property which is non-standard and not in the bindings.

There exists no code yet, so we can change the fwu-mtd bindings
to contain all properties within the fwu-mdata node.

---
 .../firmware/fwu-mdata-mtd.yaml               | 105 +++++++++++++++---
 1 file changed, 91 insertions(+), 14 deletions(-)
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +91 -14
index 4f5404f999..4b87fb8624 100644
@@ -1,13 +1,13 @@
 # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
 %YAML 1.2
 ---
-$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-sf.yaml#
-$schema: http://devicetree.org/meta-schemas/core.yaml#
+$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-mtd.yaml#
+$schema: http://devicetree.org/meta-schemas/base.yaml#
 
 title: FWU metadata on MTD device without GPT
 
 maintainers:
- - Masami Hiramatsu <[email protected]>
+ - Jassi Brar <[email protected]>
 
 properties:
   compatible:
@@ -15,24 +15,101 @@ properties:
       - const: u-boot,fwu-mdata-mtd
 
   fwu-mdata-store:
-    maxItems: 1
-    description: Phandle of the MTD device which contains the FWU medatata.
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle of the MTD device which contains the FWU MetaData and Banks.
 
-  mdata-offsets:
+  mdata-parts:
+    $ref: /schemas/types.yaml#/definitions/non-unique-string-array
     minItems: 2
-    description: Offsets of the primary and secondary FWU metadata in the NOR flash.
+    maxItems: 2
+    description: labels of the primary and secondary FWU metadata partitions in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node.
+
+  patternProperties:
+    "fwu-bank@[0-9]":
+    type: object
+    description: List of FWU mtd-backed banks. Typically two banks.
+
+    properties:
+      id:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description: Index of the bank.
+
+      label:
+        $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+        minItems: 1
+        maxItems: 1
+        description: label of the partition, in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node, that holds this bank.
+
+      patternProperties:
+        "fwu-image@[0-9]":
+        type: object
+        description: List of images in the FWU mtd-backed bank.
+
+        properties:
+          id:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Index of the bank.
+
+          offset:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Offset, from start of the bank, where the image is located.
+
+          size:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Size reserved for the image.
+
+          uuid:
+            $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+            minItems: 1
+            maxItems: 1
+            description: UUID of the image.
+
+        required:
+          - id
+          - offset
+          - size
+          - uuid
+        additionalProperties: false
+
+    required:
+      - id
+      - label
+      - fwu-images
+    additionalProperties: false
 
 required:
   - compatible
   - fwu-mdata-store
-  - mdata-offsets
-
+  - mdata-parts
+  - fwu-banks
 additionalProperties: false
 
 examples:
   - |
-    fwu-mdata {
-        compatible = "u-boot,fwu-mdata-mtd";
-        fwu-mdata-store = <&spi-flash>;
-        mdata-offsets = <0x500000 0x530000>;
-    };
+	fwu-mdata {
+		compatible = "u-boot,fwu-mdata-mtd";
+		fwu-mdata-store = <&flash0>;
+		mdata-parts = "MDATA-Pri", "MDATA-Sec";
+
+		fwu-bank@0 {
+			id = <0>;
+			label = "FIP-Bank0";
+			fwu-image@0 {
+				id = <0>;
+				offset = <0x0>;
+				size = <0x400000>;
+				uuid = "5a66a702-99fd-4fef-a392-c26e261a2828";
+			};
+		};
+		fwu-bank@1 {
+			id = <1>;
+			label = "FIP-Bank1";
+			fwu-image@0 {
+				id = <0>;
+				offset = <0x0>;
+				size = <0x400000>;
+				uuid = "a8f868a1-6e5c-4757-878d-ce63375ef2c0";
+			};
+		};
+	};
+...
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v5 1/6] dt/bindings: fwu-mdata-mtd: drop changes outside FWU

Quoted 116 lines · On 2/28/23 01:52, [email protected] wrote:

From: Jassi Brar <[email protected]>

Any requirement of FWU should not require changes to bindings of other subsystems. For example, for mtd-backed storage we can do without requiring 'fixed-partitions' children to also carry 'uuid', a property which is non-standard and not in the bindings.

There exists no code yet, so we can change the fwu-mtd bindings
to contain all properties within the fwu-mdata node.

---
  .../firmware/fwu-mdata-mtd.yaml               | 105 +++++++++++++++---
  1 file changed, 91 insertions(+), 14 deletions(-)
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +64 -9
index 4f5404f999..4b87fb8624 100644
@@ -1,13 +1,13 @@
  # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
  %YAML 1.2
  ---
-$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-sf.yaml#
-$schema: http://devicetree.org/meta-schemas/core.yaml#
+$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-mtd.yaml#
+$schema: http://devicetree.org/meta-schemas/base.yaml#
 
  title: FWU metadata on MTD device without GPT
 
  maintainers:
- - Masami Hiramatsu <[email protected]>
+ - Jassi Brar <[email protected]>
 
  properties:
    compatible:
@@ -15,24 +15,101 @@ properties:
        - const: u-boot,fwu-mdata-mtd
 
    fwu-mdata-store:
-    maxItems: 1
-    description: Phandle of the MTD device which contains the FWU medatata.
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle of the MTD device which contains the FWU MetaData and Banks.
 
-  mdata-offsets:
+  mdata-parts:
+    $ref: /schemas/types.yaml#/definitions/non-unique-string-array
      minItems: 2
-    description: Offsets of the primary and secondary FWU metadata in the NOR flash.
+    maxItems: 2
+    description: labels of the primary and secondary FWU metadata partitions in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node.
+
+  patternProperties:
+    "fwu-bank@[0-9]":
+    type: object
+    description: List of FWU mtd-backed banks. Typically two banks.
+
+    properties:
+      id:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description: Index of the bank.
+
+      label:
+        $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+        minItems: 1
+        maxItems: 1
+        description: label of the partition, in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node, that holds this bank.
+
+      patternProperties:
+        "fwu-image@[0-9]":
+        type: object
+        description: List of images in the FWU mtd-backed bank.
+
+        properties:
+          id:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Index of the bank.
+
+          offset:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Offset, from start of the bank, where the image is located.
+
+          size:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Size reserved for the image.
+
+          uuid:
+            $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+            minItems: 1
+            maxItems: 1
+            description: UUID of the image.
+
+        required:
+          - id
+          - offset
+          - size
+          - uuid
+        additionalProperties: false
+
+    required:
+      - id
+      - label
+      - fwu-images
+    additionalProperties: false
 
  required:
    - compatible
    - fwu-mdata-store
-  - mdata-offsets
-
+  - mdata-parts
+  - fwu-banks
  additionalProperties: false
 

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 91

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +3 -5
  examples:
    - |
-    fwu-mdata {
-        compatible = "u-boot,fwu-mdata-mtd";
-        fwu-mdata-store = <&spi-flash>;
-        mdata-offsets = <0x500000 0x530000>;
-    };
+	fwu-mdata {
+		compatible = "u-boot,fwu-mdata-mtd";
+		fwu-mdata-store = <&flash0>;

This is primary saying that both copies are in flash0.
Isn't it better to also via DT binding to support that it can be different physical devices?

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 92

Quoted 1 line
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +1 -0
+		mdata-parts = "MDATA-Pri", "MDATA-Sec";

This is not clear to me what this is used for. Can you please explain what you want to use this for?

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 95

Quoted 3 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +3 -0
+
+		fwu-bank@0 {
+			id = <0>;

I though that @X is all the time associated with reg property. It means maybe you wanted to use fwu-bank0 instead or switch id to reg.

The same applies below to fwuimage.

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +10 -0
+			label = "FIP-Bank0";
+			fwu-image@0 {
+				id = <0>;
+				offset = <0x0>;
+				size = <0x400000>;
+				uuid = "5a66a702-99fd-4fef-a392-c26e261a2828";
+			};
+		};
+		fwu-bank@1 {
+			id = <1>;

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 115

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +10 -0
+			label = "FIP-Bank1";
+			fwu-image@0 {
+				id = <0>;
+				offset = <0x0>;
+				size = <0x400000>;
+				uuid = "a8f868a1-6e5c-4757-878d-ce63375ef2c0";
+			};
+		};
+	};
+...

I think it will be good to get review from Krzysztof Kozlowski or Rob to make sure that it will never change.

Thanks,
Michal

author
#3 lore raw

Re: [PATCH v5 1/6] dt/bindings: fwu-mdata-mtd: drop changes outside FWU

Quoted 117 lines · Michal Simek
Quoted 116 lines · On 2/28/23 01:52, [email protected] wrote:

From: Jassi Brar <[email protected]>

Any requirement of FWU should not require changes to bindings of other subsystems. For example, for mtd-backed storage we can do without requiring 'fixed-partitions' children to also carry 'uuid', a property which is non-standard and not in the bindings.

There exists no code yet, so we can change the fwu-mtd bindings
to contain all properties within the fwu-mdata node.

---
  .../firmware/fwu-mdata-mtd.yaml               | 105 +++++++++++++++---
  1 file changed, 91 insertions(+), 14 deletions(-)
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +64 -9
index 4f5404f999..4b87fb8624 100644
@@ -1,13 +1,13 @@
  # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
  %YAML 1.2
  ---
-$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-sf.yaml#
-$schema: http://devicetree.org/meta-schemas/core.yaml#
+$id: http://devicetree.org/schemas/firmware/u-boot,fwu-mdata-mtd.yaml#
+$schema: http://devicetree.org/meta-schemas/base.yaml#
 
  title: FWU metadata on MTD device without GPT
 
  maintainers:
- - Masami Hiramatsu <[email protected]>
+ - Jassi Brar <[email protected]>
 
  properties:
    compatible:
@@ -15,24 +15,101 @@ properties:
        - const: u-boot,fwu-mdata-mtd
 
    fwu-mdata-store:
-    maxItems: 1
-    description: Phandle of the MTD device which contains the FWU medatata.
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle of the MTD device which contains the FWU MetaData and Banks.
 
-  mdata-offsets:
+  mdata-parts:
+    $ref: /schemas/types.yaml#/definitions/non-unique-string-array
      minItems: 2
-    description: Offsets of the primary and secondary FWU metadata in the NOR flash.
+    maxItems: 2
+    description: labels of the primary and secondary FWU metadata partitions in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node.
+
+  patternProperties:
+    "fwu-bank@[0-9]":
+    type: object
+    description: List of FWU mtd-backed banks. Typically two banks.
+
+    properties:
+      id:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description: Index of the bank.
+
+      label:
+        $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+        minItems: 1
+        maxItems: 1
+        description: label of the partition, in the 'fixed-partitions' subnode of the 'jedec,spi-nor' flash device node, that holds this bank.
+
+      patternProperties:
+        "fwu-image@[0-9]":
+        type: object
+        description: List of images in the FWU mtd-backed bank.
+
+        properties:
+          id:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Index of the bank.
+
+          offset:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Offset, from start of the bank, where the image is located.
+
+          size:
+            $ref: /schemas/types.yaml#/definitions/uint32
+            description: Size reserved for the image.
+
+          uuid:
+            $ref: /schemas/types.yaml#/definitions/non-unique-string-array
+            minItems: 1
+            maxItems: 1
+            description: UUID of the image.
+
+        required:
+          - id
+          - offset
+          - size
+          - uuid
+        additionalProperties: false
+
+    required:
+      - id
+      - label
+      - fwu-images
+    additionalProperties: false
 
  required:
    - compatible
    - fwu-mdata-store
-  - mdata-offsets
-
+  - mdata-parts
+  - fwu-banks
  additionalProperties: false
 
Quoted 15 lines

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 91

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +3 -5
  examples:
    - |
-    fwu-mdata {
-        compatible = "u-boot,fwu-mdata-mtd";
-        fwu-mdata-store = <&spi-flash>;
-        mdata-offsets = <0x500000 0x530000>;
-    };
+     fwu-mdata {
+             compatible = "u-boot,fwu-mdata-mtd";
+             fwu-mdata-store = <&flash0>;

This is primary saying that both copies are in flash0.
Isn't it better to also via DT binding to support that it can be different physical devices?

That could be done, but currently the FWU code assumes all the artifacts are in the same storage.
Same for GPT based layout, do we go change that as well? So I kept it simple for now.

Quoted 5 lines

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 92

Quoted 1 line
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +1 -0
+             mdata-parts = "MDATA-Pri", "MDATA-Sec";

This is not clear to me what this is used for. Can you please explain what you want to use this for?

These are the two required meta-data partitions, primary and secondary (backup of primary).
Meta-Data partitions store information about the current setup of banks/images.

Quoted 9 lines

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 95

Quoted 3 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +3 -0
+
+             fwu-bank@0 {
+                     id = <0>;

I though that @X is all the time associated with reg property. It means maybe you wanted to use fwu-bank0 instead or switch id to reg.

The same applies below to fwuimage.

Yes, fwu-bank0 and fwu-image0 seems better.

Quoted 21 lines
Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +10 -0
+                     label = "FIP-Bank0";
+                     fwu-image@0 {
+                             id = <0>;
+                             offset = <0x0>;
+                             size = <0x400000>;
+                             uuid = "5a66a702-99fd-4fef-a392-c26e261a2828";
+                     };
+             };
+             fwu-bank@1 {
+                     id = <1>;

doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml properties: · line 115

Quoted 10 lines
doc/device-tree-bindings/firmware/fwu-mdata-mtd.yaml +10 -0
+                     label = "FIP-Bank1";
+                     fwu-image@0 {
+                             id = <0>;
+                             offset = <0x0>;
+                             size = <0x400000>;
+                             uuid = "a8f868a1-6e5c-4757-878d-ce63375ef2c0";
+                     };
+             };
+     };
+...
Quoted 3 lines

I think it will be good to get review from Krzysztof Kozlowski or Rob to make sure that it will never change.

I will CC them.

cheers.

#4 lore raw

PATCH v5 2/6 fwu: gpt: use cached meta-data partition numbers

In reply to [email protected]

From: Jassi Brar <[email protected]>

Use cached values and avoid parsing and scanning through partitions everytime for meta-data partitions because they can't change after bootup.

---
 drivers/fwu-mdata/gpt_blk.c | 43 +++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 19 deletions(-)
drivers/fwu-mdata/gpt_blk.c +24 -19
index d35ce49c5c..28f5d23e1e 100644
@@ -24,8 +24,9 @@ enum {
 	MDATA_WRITE,
 };
 
-static int gpt_get_mdata_partitions(struct blk_desc *desc,
-				    uint mdata_parts[2])
+static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */
+
+static int gpt_get_mdata_partitions(struct blk_desc *desc)
 {
 	int i, ret;
 	u32 nparts;
@@ -33,18 +34,19 @@ static int gpt_get_mdata_partitions(struct blk_desc *desc,
 	struct disk_partition info;
 	const efi_guid_t fwu_mdata_guid = FWU_MDATA_GUID;
 
+	/* if primary and secondary partitions already found */
+	if (g_mdata_part[0] && g_mdata_part[1])
+		return 0;
+
 	nparts = 0;
-	for (i = 1; i < MAX_SEARCH_PARTITIONS; i++) {
+	for (i = 1; i < MAX_SEARCH_PARTITIONS && nparts < 2; i++) {
 		if (part_get_info(desc, i, &info))
 			continue;
 		uuid_str_to_bin(info.type_guid, part_type_guid.b,
 				UUID_STR_FORMAT_GUID);
 
-		if (!guidcmp(&fwu_mdata_guid, &part_type_guid)) {
-			if (nparts < 2)
-				mdata_parts[nparts] = i;
-			++nparts;
-		}
+		if (!guidcmp(&fwu_mdata_guid, &part_type_guid))
+			g_mdata_part[nparts++] = i;
 	}
 
 	if (nparts != 2) {
@@ -127,26 +129,25 @@ static int fwu_gpt_update_mdata(struct udevice *dev, struct fwu_mdata *mdata)
 {
 	int ret;
 	struct blk_desc *desc;
-	uint mdata_parts[2];
 	struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
 
 	desc = dev_get_uclass_plat(priv->blk_dev);
 
-	ret = gpt_get_mdata_partitions(desc, mdata_parts);
+	ret = gpt_get_mdata_partitions(desc);
 	if (ret < 0) {
 		log_debug("Error getting the FWU metadata partitions\n");
 		return -ENOENT;
 	}
 
 	/* First write the primary partition */
-	ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, mdata_parts[0]);
+	ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, g_mdata_part[0]);
 	if (ret < 0) {
 		log_debug("Updating primary FWU metadata partition failed\n");
 		return ret;
 	}
 
 	/* And now the replica */
-	ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, mdata_parts[1]);
+	ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, g_mdata_part[1]);
 	if (ret < 0) {
 		log_debug("Updating secondary FWU metadata partition failed\n");
 		return ret;
@@ -158,16 +159,14 @@ static int fwu_gpt_update_mdata(struct udevice *dev, struct fwu_mdata *mdata)
 static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata *mdata)
 {
 	int ret;
-	uint mdata_parts[2];
-
-	ret = gpt_get_mdata_partitions(desc, mdata_parts);
 
+	ret = gpt_get_mdata_partitions(desc);
 	if (ret < 0) {
 		log_debug("Error getting the FWU metadata partitions\n");
 		return -ENOENT;
 	}
 
-	ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, mdata_parts[0]);
+	ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, g_mdata_part[0]);
 	if (ret < 0) {
 		log_debug("Failed to read the FWU metadata from the device\n");
 		return -EIO;
@@ -182,7 +181,7 @@ static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata *mdata)
 	 * Try to read the replica.
 	 */
 	memset(mdata, '\0', sizeof(struct fwu_mdata));
-	ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, mdata_parts[1]);
+	ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, g_mdata_part[1]);
 	if (ret < 0) {
 		log_debug("Failed to read the FWU metadata from the device\n");
 		return -EIO;
@@ -206,9 +205,15 @@ static int fwu_gpt_get_mdata(struct udevice *dev, struct fwu_mdata *mdata)
 static int fwu_gpt_get_mdata_partitions(struct udevice *dev, uint *mdata_parts)
 {
 	struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+	int err;
+
+	err = gpt_get_mdata_partitions(dev_get_uclass_plat(priv->blk_dev));
+	if (!err) {
+		mdata_parts[0] = g_mdata_part[0];
+		mdata_parts[1] = g_mdata_part[1];
+	}
 
-	return gpt_get_mdata_partitions(dev_get_uclass_plat(priv->blk_dev),
-					mdata_parts);
+	return err;
 }
 
 static int fwu_gpt_read_mdata_partition(struct udevice *dev,
Signature · 1 line
2.34.1
#5 lore raw

PATCH v5 3/6 fwu: move meta-data management in core

In reply to [email protected]

From: Jassi Brar <[email protected]>

Instead of each i/f having to implement their own meta-data verification and storage, move the logic in common code. This simplifies the i/f code much simpler and compact.

---
 drivers/fwu-mdata/fwu-mdata-uclass.c |  34 +++++++
 include/fwu.h                        |  41 +++++++++
 lib/fwu_updates/fwu.c                | 130 +++++++++++++++++++++++++--
 3 files changed, 200 insertions(+), 5 deletions(-)
drivers/fwu-mdata/fwu-mdata-uclass.c +34 -0
index b477e9603f..e03773c584 100644
@@ -16,6 +16,40 @@
 #include <linux/types.h>
 #include <u-boot/crc.h>
 
+/**
+ * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary)
+{
+	const struct fwu_mdata_ops *ops = device_get_ops(dev);
+
+	if (!ops->read_mdata) {
+		log_debug("read_mdata() method not defined\n");
+		return -ENOSYS;
+	}
+
+	return ops->read_mdata(dev, mdata, primary);
+}
+
+/**
+ * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata()
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary)
+{
+	const struct fwu_mdata_ops *ops = device_get_ops(dev);
+
+	if (!ops->write_mdata) {
+		log_debug("write_mdata() method not defined\n");
+		return -ENOSYS;
+	}
+
+	return ops->write_mdata(dev, mdata, primary);
+}
+
 /**
  * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
  * @dev: FWU metadata device
include/fwu.h +41 -0

53 lines not on this page · raw message

lib/fwu_updates/fwu.c +125 -5

151 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v5 3/6] fwu: move meta-data management in core

Hello Jassi,

Quoted 19 lines · On Tue, 28 Feb 2023 at 01:52, <[email protected]> wrote:

From: Jassi Brar <[email protected]>

Instead of each i/f having to implement their own meta-data verification and storage, move the logic in common code. This simplifies the i/f code much simpler and compact.

---
 drivers/fwu-mdata/fwu-mdata-uclass.c |  34 +++++++
 include/fwu.h                        |  41 +++++++++
 lib/fwu_updates/fwu.c                | 130 +++++++++++++++++++++++++--
 3 files changed, 200 insertions(+), 5 deletions(-)
drivers/fwu-mdata/fwu-mdata-uclass.c +0 -0
index b477e9603f..e03773c584 100644

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

Quoted 8 lines
drivers/fwu-mdata/fwu-mdata-uclass.c +5 -0
@@ -16,6 +16,40 @@
 #include <linux/types.h>
 #include <u-boot/crc.h>
 
+/**
+ * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
+ *
+ * Return: 0 if OK, -ve on error
+ */

Inline description only in header file, or duplicated in source and header files?

Quoted 37 lines
drivers/fwu-mdata/fwu-mdata-uclass.c +29 -0
+int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary)
+{
+       const struct fwu_mdata_ops *ops = device_get_ops(dev);
+
+       if (!ops->read_mdata) {
+               log_debug("read_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->read_mdata(dev, mdata, primary);
+}
+
+/**
+ * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata()
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary)
+{
+       const struct fwu_mdata_ops *ops = device_get_ops(dev);
+
+       if (!ops->write_mdata) {
+               log_debug("write_mdata() method not defined\n");
+               return -ENOSYS;
+       }
+
+       return ops->write_mdata(dev, mdata, primary);
+}
+
 /**
  * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers
  * @dev: FWU metadata device
include/fwu.h +0 -0
index 0919ced812..1a700c9e6a 100644

include/fwu.h struct fwu_mdata_gpt_blk_priv { · line 30

Quoted 7 lines
include/fwu.h +4 -0
@@ -24,6 +24,26 @@ struct fwu_mdata_gpt_blk_priv {
  * @update_mdata() - Update the FWU metadata copy
  */
 struct fwu_mdata_ops {
+       /**
+        * read_mdata() - Populate the asked FWU metadata copy
+        * @dev: FWU metadata device
+        * @mdata: Copy of the FWU metadata

@mdata: Output FWU mdata read

include/fwu.h struct fwu_mdata_gpt_blk_priv { · line 31

Quoted 1 line
include/fwu.h +1 -0
+        * @primary: If primary or secondary copy of meta-data is to be read

s/meta-data/FWU metadata/
Ditto in .write_mdata description

Quoted 28 lines
include/fwu.h +21 -0
+        *
+        * Return: 0 if OK, -ve on error
+        */
+       int (*read_mdata)(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
+
+       /**
+        * write_mdata() - Write the given FWU metadata copy
+        * @dev: FWU metadata device
+        * @mdata: Copy of the FWU metadata
+        * @primary: If primary or secondary copy of meta-data is to be written
+        *
+        * Return: 0 if OK, -ve on error
+        */
+       int (*write_mdata)(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
+
        /**
         * check_mdata() - Check if the FWU metadata is valid
         * @dev:        FWU device
@@ -126,6 +146,27 @@ struct fwu_mdata_ops {
        EFI_GUID(0x0c996046, 0xbcc0, 0x4d04, 0x85, 0xec, \
                 0xe1, 0xfc, 0xed, 0xf1, 0xc6, 0xf8)
 
+/**
+ * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
+ */
+int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
+
+/**

include/fwu.h struct fwu_mdata_ops { · line 164

Quoted 10 lines
include/fwu.h +10 -0
+ * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata()
+ */
+int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
+
+/**
+ * fwu_get_verified_mdata() - Read, verify and return the FWU metadata
+ *
+ * Read both the metadata copies from the storage media, verify their checksum,
+ * and ascertain that both copies match. If one of the copies has gone bad,
+ * restore it from the good copy.

@mdata: Output FWU metadata read or NULL

Quoted 51 lines
include/fwu.h +5 -0
+ *
+ * Return: 0 if OK, -ve on error
+*/
+int fwu_get_verified_mdata(struct fwu_mdata *mdata);
+
 /**
  * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies
  *
lib/fwu_updates/fwu.c +21 -4
index 5313d07302..7d195964a2 100644
@@ -15,13 +15,13 @@
 #include <linux/errno.h>
 #include <linux/types.h>
 
+#include <u-boot/crc.h>
+
+static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */
+static struct udevice *g_dev;
 static u8 in_trial;
 static u8 boottime_check;
 
-#include <linux/errno.h>
-#include <linux/types.h>
-#include <u-boot/crc.h>
-
 enum {
        IMAGE_ACCEPT_SET = 1,
        IMAGE_ACCEPT_CLEAR,
@@ -161,6 +161,126 @@ static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id)
        return -ENOENT;
 }
 
+/**
+ * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided
+ * @mdata: FWU metadata structure
+ * @part: Bitmask of FWU metadata partitions to be written to
+ *
+ * Return: 0 if OK, -ve on error
+ */
+static int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
+{
+       void *buf = &mdata->version;
+       int err;
+
+       if (part == BOTH_PARTS) {
+               err = fwu_sync_mdata(mdata, SECONDARY_PART);
+               if (err)
+                       return err;
+               part = PRIMARY_PART;

lib/fwu_updates/fwu.c static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id) · line 190

Quoted 10 lines
lib/fwu_updates/fwu.c +10 -0
+       }
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       err = fwu_write_mdata(g_dev, mdata, part & PRIMARY_PART ? true : false);

Since this expects part is either PRIMARY_PART or SECONDARY_PART, prefer:
err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART);

And ditto below:

      part == PRIMARY_PART ? "primary": "secondary");
Quoted 35 lines
lib/fwu_updates/fwu.c +35 -0
+       if (err) {
+               log_err("Unable to write %s mdata\n",
+                               part & PRIMARY_PART ?  "primary": "secondary");
+               return err;
+       }
+
+       /* update the cached copy of meta-data */
+       memcpy(&g_mdata, mdata, sizeof(struct fwu_mdata));
+
+       return 0;
+}
+
+static inline int mdata_crc_check(struct fwu_mdata *mdata)
+{
+       void *buf = &mdata->version;
+       u32 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       return calc_crc32 == mdata->crc32 ? 0 : -EINVAL;
+}
+
+/**
+ * fwu_get_verified_mdata() - Read, verify and return the FWU metadata
+ *
+ * Read both the metadata copies from the storage media, verify their checksum,
+ * and ascertain that both copies match. If one of the copies has gone bad,
+ * restore it from the good copy.
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int fwu_get_verified_mdata(struct fwu_mdata *mdata)
+{
+       int err;
+       bool parts_ok[2] = { false };
+       struct fwu_mdata s, *parts_mdata[2];
+

lib/fwu_updates/fwu.c static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id) · line 235

Quoted 10 lines
lib/fwu_updates/fwu.c +10 -0
+       parts_mdata[0] = &g_mdata;
+       parts_mdata[1] = &s;
+
+       /* if mdata already read and ready */
+       err = mdata_crc_check(parts_mdata[0]);
+       if (!err)
+               goto ret_mdata;
+       /* else read, verify and, if needed, fix mdata */
+
+       for (int i = 0; i < 2; i++) {

Define "int i;" at function block entry?

Quoted 63 lines
lib/fwu_updates/fwu.c +49 -2
+               parts_ok[i] = false;
+               err = fwu_read_mdata(g_dev, parts_mdata[i], !i);
+               if (!err) {
+                       err = mdata_crc_check(parts_mdata[i]);
+                       if (!err)
+                               parts_ok[i] = true;
+                       else
+                               log_debug("mdata : %s crc32 failed\n", i ? "secondary": "primary");
+               }
+       }
+
+       if (parts_ok[0] && parts_ok[1]) {
+               /*
+                * Before returning, check that both the
+                * FWU metadata copies are the same.
+                */
+               err = memcmp(parts_mdata[0], parts_mdata[1], sizeof(struct fwu_mdata));
+               if (!err)
+                       goto ret_mdata;
+
+               /*
+                * If not, populate the secondary partition from the
+                * primary partition copy.
+                */
+               log_info("Both FWU metadata copies are valid but do not match.");
+               log_info(" Restoring the secondary partition from the primary\n");
+               parts_ok[1] = false;
+       }
+
+       for (int i = 0; i < 2; i++) {
+               if (parts_ok[i])
+                       continue;
+
+               memcpy(parts_mdata[i], parts_mdata[1-i], sizeof(struct fwu_mdata));
+               err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART);
+               if (err) {
+                       log_debug("mdata : %s write failed\n", i ? "secondary": "primary");
+                       return err;
+               }
+       }
+
+ret_mdata:
+       if (!err && mdata)
+               memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata));
+
+       return err;
+}
+
 /**
  * fwu_verify_mdata() - Verify the FWU metadata
  * @mdata: FWU metadata structure
@@ -436,7 +556,7 @@ int fwu_get_image_index(u8 *image_index)
                }
        }
 
-       log_debug("Partition with the image type %pUs not found\n",
+       log_err("Partition with the image type %pUs not found\n",
                  &image_type_id);
 
 out:
--

2.34.1

author
#7 lore raw

Re: [PATCH v5 3/6] fwu: move meta-data management in core

Quoted 8 lines · Etienne Carriere
Quoted 7 lines · On Tue, 28 Feb 2023 at 01:52, <[email protected]> wrote:

From: Jassi Brar <[email protected]>

Instead of each i/f having to implement their own meta-data verification and storage, move the logic in common code. This simplifies the i/f code much simpler and compact.

...

Quoted 8 lines
Quoted 8 lines
drivers/fwu-mdata/fwu-mdata-uclass.c +0 -0
index b477e9603f..e03773c584 100644
@@ -16,6 +16,40 @@
 #include <linux/types.h>
 #include <u-boot/crc.h>
 
Quoted 9 lines

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

Quoted 5 lines
drivers/fwu-mdata/fwu-mdata-uclass.c +5 -0
+/**
+ * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata()
+ *
+ * Return: 0 if OK, -ve on error
+ */

Inline description only in header file, or duplicated in source and header files?

This is the original practice in FWU stack - to have the description in header as well as source code. I just didn't want to stick out.

Quoted 15 lines

include/fwu.h struct fwu_mdata_gpt_blk_priv { · line 30

Quoted 12 lines
include/fwu.h +4 -0
index 0919ced812..1a700c9e6a 100644
@@ -24,6 +24,26 @@ struct fwu_mdata_gpt_blk_priv {
  * @update_mdata() - Update the FWU metadata copy
  */
 struct fwu_mdata_ops {
+       /**
+        * read_mdata() - Populate the asked FWU metadata copy
+        * @dev: FWU metadata device
+        * @mdata: Copy of the FWU metadata

@mdata: Output FWU mdata read

Quoted 5 lines

include/fwu.h struct fwu_mdata_gpt_blk_priv { · line 31

Quoted 1 line
include/fwu.h +1 -0
+        * @primary: If primary or secondary copy of meta-data is to be read

s/meta-data/FWU metadata/
Ditto in .write_mdata description

ok

Quoted 9 lines

include/fwu.h struct fwu_mdata_ops { · line 164

Quoted 6 lines
include/fwu.h +6 -0
+/**
+ * fwu_get_verified_mdata() - Read, verify and return the FWU metadata
+ *
+ * Read both the metadata copies from the storage media, verify their checksum,
+ * and ascertain that both copies match. If one of the copies has gone bad,
+ * restore it from the good copy.

@mdata: Output FWU metadata read or NULL

ok

Quoted 24 lines
Quoted 10 lines
include/fwu.h +10 -0
+static int fwu_sync_mdata(struct fwu_mdata *mdata, int part)
+{
+       void *buf = &mdata->version;
+       int err;
+
+       if (part == BOTH_PARTS) {
+               err = fwu_sync_mdata(mdata, SECONDARY_PART);
+               if (err)
+                       return err;
+               part = PRIMARY_PART;

include/fwu.h struct fwu_mdata_gpt_blk_priv {

Quoted 10 lines
include/fwu.h +10 -0
+       }
+
+       /*
+        * Calculate the crc32 for the updated FWU metadata
+        * and put the updated value in the FWU metadata crc32
+        * field
+        */
+       mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32));
+
+       err = fwu_write_mdata(g_dev, mdata, part & PRIMARY_PART ? true : false);

Since this expects part is either PRIMARY_PART or SECONDARY_PART, prefer:
err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART);

Quoted 3 lines

And ditto below:

      part == PRIMARY_PART ? "primary": "secondary");

yes, now that we handle out the BOTH_PARTS case already.

Quoted 10 lines
Quoted 10 lines
include/fwu.h +10 -0
+int fwu_get_verified_mdata(struct fwu_mdata *mdata)
+{
+       int err;
+       bool parts_ok[2] = { false };
+       struct fwu_mdata s, *parts_mdata[2];
+
+       parts_mdata[0] = &g_mdata;
+       parts_mdata[1] = &s;
+
+       /* if mdata already read and ready */
Quoted 9 lines

include/fwu.h

Quoted 6 lines
include/fwu.h +6 -0
+       err = mdata_crc_check(parts_mdata[0]);
+       if (!err)
+               goto ret_mdata;
+       /* else read, verify and, if needed, fix mdata */
+
+       for (int i = 0; i < 2; i++) {

Define "int i;" at function block entry?

Hmm... I prefer this way - limiting scope of the scratch variables.

thanks.

#8 lore raw

PATCH v5 4/6 fwu: gpt: implement read_mdata and write_mdata callbacks

In reply to [email protected]

From: Jassi Brar <[email protected]>

Moving towards using common code for meta-data management, implement the read/write mdata hooks.

---
 drivers/fwu-mdata/gpt_blk.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/fwu-mdata/gpt_blk.c +36 -0
index 28f5d23e1e..bdaa10cd1d 100644
@@ -272,7 +272,43 @@ static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
 	return 0;
 }
 
+static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
+						 bool primary)
+{
+	struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+	struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
+	int ret;
+
+	ret = gpt_get_mdata_partitions(desc);
+	if (ret < 0) {
+		log_debug("Error getting the FWU metadata partitions\n");
+		return -ENOENT;
+	}
+
+	return gpt_read_write_mdata(desc, mdata, MDATA_READ,
+                                primary ? g_mdata_part[0] : g_mdata_part[1]);
+}
+
+static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
+						 bool primary)
+{
+	struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+	struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
+	int ret;
+
+	ret = gpt_get_mdata_partitions(desc);
+	if (ret < 0) {
+		log_debug("Error getting the FWU metadata partitions\n");
+		return -ENOENT;
+	}
+
+	return gpt_read_write_mdata(desc, mdata, MDATA_WRITE,
+                                primary ? g_mdata_part[0] : g_mdata_part[1]);
+}
+
 static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+	.read_mdata = fwu_gpt_read_mdata,
+	.write_mdata = fwu_gpt_write_mdata,
 	.get_mdata = fwu_gpt_get_mdata,
 	.update_mdata = fwu_gpt_update_mdata,
 	.get_mdata_part_num = fwu_gpt_get_mdata_partitions,
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v5 4/6] fwu: gpt: implement read_mdata and write_mdata callbacks

Quoted 26 lines · On Tue, 28 Feb 2023 at 01:52, <[email protected]> wrote:

From: Jassi Brar <[email protected]>

Moving towards using common code for meta-data management, implement the read/write mdata hooks.

---
 drivers/fwu-mdata/gpt_blk.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/fwu-mdata/gpt_blk.c +5 -0
index 28f5d23e1e..bdaa10cd1d 100644
@@ -272,7 +272,43 @@ static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
        return 0;
 }
 
+static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
+                                                bool primary)
+{
+       struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+       struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);

drivers/fwu-mdata/gpt_blk.c static int fwu_mdata_gpt_blk_probe(struct udevice *dev) · line 289

Quoted 10 lines
drivers/fwu-mdata/gpt_blk.c +10 -0
+       int ret;
+
+       ret = gpt_get_mdata_partitions(desc);
+       if (ret < 0) {
+               log_debug("Error getting the FWU metadata partitions\n");
+               return -ENOENT;
+       }
+
+       return gpt_read_write_mdata(desc, mdata, MDATA_READ,
+                                primary ? g_mdata_part[0] : g_mdata_part[1]);

Indentation to fix.

Quoted 7 lines
drivers/fwu-mdata/gpt_blk.c +7 -0
+}
+
+static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
+                                                bool primary)
+{
+       struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
+       struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);

drivers/fwu-mdata/gpt_blk.c static int fwu_mdata_gpt_blk_probe(struct udevice *dev) · line 306

Quoted 10 lines
drivers/fwu-mdata/gpt_blk.c +10 -0
+       int ret;
+
+       ret = gpt_get_mdata_partitions(desc);
+       if (ret < 0) {
+               log_debug("Error getting the FWU metadata partitions\n");
+               return -ENOENT;
+       }
+
+       return gpt_read_write_mdata(desc, mdata, MDATA_WRITE,
+                                primary ? g_mdata_part[0] : g_mdata_part[1]);

Indentation to fix.
With these 2 comments addressed, my R-B tag applies.

Quoted 11 lines
drivers/fwu-mdata/gpt_blk.c +4 -1
+}
+
 static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
+       .read_mdata = fwu_gpt_read_mdata,
+       .write_mdata = fwu_gpt_write_mdata,
        .get_mdata = fwu_gpt_get_mdata,
        .update_mdata = fwu_gpt_update_mdata,
        .get_mdata_part_num = fwu_gpt_get_mdata_partitions,
--

2.34.1

#10 lore raw

PATCH v5 5/6 fwu: meta-data: switch to management by common code

In reply to [email protected]

From: Jassi Brar <[email protected]>

The common code can now read, verify and fix meta-data copies while exposing one consistent structure to users.
Only the .read_mdata() and .write_mdata() callbacks of fwu_mdata_ops
are needed. Get rid of .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() and .write_mdata_partition() and also the corresponding wrapper functions.

---
 cmd/fwu_mdata.c                      |  17 +-
 drivers/fwu-mdata/fwu-mdata-uclass.c | 165 -------------------
 drivers/fwu-mdata/gpt_blk.c          | 124 +-------------
 include/fwu.h                        | 199 -----------------------
 lib/fwu_updates/fwu.c                | 235 ++++-----------------------
 5 files changed, 38 insertions(+), 702 deletions(-)
cmd/fwu_mdata.c +2 -15
index f04af27de6..9b70340368 100644
@@ -43,23 +43,10 @@ static void print_mdata(struct fwu_mdata *mdata)
 int do_fwu_mdata_read(struct cmd_tbl *cmdtp, int flag,
 		     int argc, char * const argv[])
 {
-	struct udevice *dev;
 	int ret = CMD_RET_SUCCESS, res;
-	struct fwu_mdata mdata = { 0 };
+	struct fwu_mdata mdata;
 
-	if (uclass_get_device(UCLASS_FWU_MDATA, 0, &dev) || !dev) {
-		log_err("Unable to get FWU metadata device\n");
-		return CMD_RET_FAILURE;
-	}
-
-	res = fwu_check_mdata_validity();
-	if (res < 0) {
-		log_err("FWU Metadata check failed\n");
-		ret = CMD_RET_FAILURE;
-		goto out;
-	}
-
-	res = fwu_get_mdata(dev, &mdata);
+	res = fwu_get_verified_mdata(&mdata);
 	if (res < 0) {
 		log_err("Unable to get valid FWU metadata\n");
 		ret = CMD_RET_FAILURE;
drivers/fwu-mdata/fwu-mdata-uclass.c +0 -165

177 lines not on this page · raw message

drivers/fwu-mdata/gpt_blk.c +5 -119

150 lines not on this page · raw message

include/fwu.h +0 -199

229 lines not on this page · raw message

lib/fwu_updates/fwu.c +31 -204

345 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

PATCH v5 6/6 fwu: rename fwu_get_verified_mdata to fwu_get_mdata

In reply to [email protected]

From: Jassi Brar <[email protected]>

fwu_get_mdata() sounds more appropriate than fwu_get_verified_mdata()

---
 cmd/fwu_mdata.c       | 2 +-
 include/fwu.h         | 4 ++--
 lib/fwu_updates/fwu.c | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)
cmd/fwu_mdata.c +1 -1
index 9b70340368..5ecda455df 100644
@@ -46,7 +46,7 @@ int do_fwu_mdata_read(struct cmd_tbl *cmdtp, int flag,
 	int ret = CMD_RET_SUCCESS, res;
 	struct fwu_mdata mdata;
 
-	res = fwu_get_verified_mdata(&mdata);
+	res = fwu_get_mdata(&mdata);
 	if (res < 0) {
 		log_err("Unable to get valid FWU metadata\n");
 		ret = CMD_RET_FAILURE;
include/fwu.h +2 -2
index 23bd97fe86..ea25aca2cd 100644
@@ -80,7 +80,7 @@ int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
 int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
 
 /**
- * fwu_get_verified_mdata() - Read, verify and return the FWU metadata
+ * fwu_get_mdata() - Read, verify and return the FWU metadata
  *
  * Read both the metadata copies from the storage media, verify their checksum,
  * and ascertain that both copies match. If one of the copies has gone bad,
@@ -88,7 +88,7 @@ int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary);
  *
  * Return: 0 if OK, -ve on error
 */
-int fwu_get_verified_mdata(struct fwu_mdata *mdata);
+int fwu_get_mdata(struct fwu_mdata *mdata);
 
 /**
  * fwu_get_active_index() - Get active_index from the FWU metadata
lib/fwu_updates/fwu.c +3 -3
index 14a2af2c74..562aa7df8c 100644
@@ -189,7 +189,7 @@ static inline int mdata_crc_check(struct fwu_mdata *mdata)
 }
 
 /**
- * fwu_get_verified_mdata() - Read, verify and return the FWU metadata
+ * fwu_get_mdata() - Read, verify and return the FWU metadata
  *
  * Read both the metadata copies from the storage media, verify their checksum,
  * and ascertain that both copies match. If one of the copies has gone bad,
@@ -197,7 +197,7 @@ static inline int mdata_crc_check(struct fwu_mdata *mdata)
  *
  * Return: 0 if OK, -ve on error
  */
-int fwu_get_verified_mdata(struct fwu_mdata *mdata)
+int fwu_get_mdata(struct fwu_mdata *mdata)
 {
 	int err;
 	bool parts_ok[2] = { false };
@@ -614,7 +614,7 @@ static int fwu_boottime_checks(void *ctx, struct event *event)
 		return ret;
 	}
 
-	ret = fwu_get_verified_mdata(NULL);
+	ret = fwu_get_mdata(NULL);
 	if (ret) {
 		log_debug("Unable to read meta-data\n");
 		return ret;
Signature · 1 line
2.34.1
#12 lore raw
In reply to [email protected]
Quoted 23 lines · On Mon, Feb 27, 2023 at 06:51:35PM -0600, [email protected] wrote:

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Quoted 8 lines

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Did you run this through CI / build sandbox? This doesn't read like you fixed the problem I reported in CI, when I was trying to merge v4.

Signature · 1 line
Tom
#13 lore raw
Quoted 30 lines · Tom Rini
Quoted 27 lines · On Mon, Feb 27, 2023 at 06:51:35PM -0600, [email protected] wrote:

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Quoted 8 lines
Quoted 4 lines

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Did you run this through CI / build sandbox? This doesn't read like you fixed the problem I reported in CI, when I was trying to merge v4.

I know that remains to be done.
The dt-bindings for fwu-mdata is changed in this patchset and I thought any testcase may be impacted by it.

thanks.

#14 lore raw
Quoted 36 lines · Jassi Brar
Quoted 35 lines · Tom Rini
Quoted 31 lines · On Mon, Feb 27, 2023 at 06:51:35PM -0600, [email protected] wrote:

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Quoted 6 lines
Quoted 3 lines

Did you run this through CI / build sandbox? This doesn't read like you fixed the problem I reported in CI, when I was trying to merge v4.

I know that remains to be done.
The dt-bindings for fwu-mdata is changed in this patchset and I thought any testcase may be impacted by it.

So you're not expecting this iteration to be merged, as CI doesn't pass, and that's known? OK.

Signature · 1 line
Tom
author
#15 lore raw
Quoted 41 lines · Tom Rini
Quoted 39 lines · Jassi Brar
Quoted 38 lines · Tom Rini
Quoted 23 lines · On Mon, Feb 27, 2023 at 06:51:35PM -0600, [email protected] wrote:

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Quoted 8 lines

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Did you run this through CI / build sandbox? This doesn't read like you fixed the problem I reported in CI, when I was trying to merge v4.

Quoted 7 lines
Quoted 3 lines

I know that remains to be done.
The dt-bindings for fwu-mdata is changed in this patchset and I thought any testcase may be impacted by it.

So you're not expecting this iteration to be merged, as CI doesn't pass, and that's known? OK.

I am more concerned if the bindings displease someone and I have to drop any code.
If I get acks on all patches, the next revision will only add the test cases.

thanks

author
#16 lore raw
In reply to Tom Rini
Quoted 41 lines · Tom Rini
Quoted 39 lines · Jassi Brar
Quoted 38 lines · Tom Rini
Quoted 23 lines · On Mon, Feb 27, 2023 at 06:51:35PM -0600, [email protected] wrote:

From: Jassi Brar <[email protected]>

The patchset reduces ~400 lines of code, while keeping the functionality same and making meta-data operations much faster (by using cached structures).

Issue:
meta-data copies (primary and secondary) are being handled by the backend/storage layer
instead of the common core in fwu.c (as also noted by Ilias) that is, gpt_blk.c manages meta-data and similarly raw_mtd.c will have to do the same when it arrives. The code could by make smaller, cleaner and optimised.

Basic idea:
Introduce .read_mdata() and .write_mdata() in fwu_mdata_ops that simply read/write
meta-data copy. The core code takes care of integrity and redundancy of the meta-data, as a result we can get rid of every other callback .get_mdata() .update_mdata() .get_mdata_part_num() .read_mdata_partition() .write_mdata_partition() and the corresponding wrapper functions thereby making the code 100s of LOC smaller.

Get rid of fwu_check_mdata_validity() and fwu_mdata_check() which expected underlying layer to manage and verify mdata copies.
Implement fwu_get_verified_mdata(struct fwu_mdata *mdata) public function that reads, verifies and, if needed, fixes the meta-data copies.

Quoted 8 lines

Verified copy of meta-data is now cached as 'g_mdata' in fwu.c, which avoids multiple
low-level expensive read and parse calls.
gpt meta-data partition numbers are now cached in gpt_blk.c, so that we don't have to do expensive part_get_info() and uid ops.

Changes since v4:
* Change fwu-mdata-mtd bindings to not require external changes
* Handle 'part == BOTH_PARTS' in fwu_sync_mdata
* use parts_ok[] and parts_mdata[] instead of pri/sec_ok and p/s_mdata

Did you run this through CI / build sandbox? This doesn't read like you fixed the problem I reported in CI, when I was trying to merge v4.

Quoted 7 lines
Quoted 3 lines

I know that remains to be done.
The dt-bindings for fwu-mdata is changed in this patchset and I thought any testcase may be impacted by it.

So you're not expecting this iteration to be merged, as CI doesn't pass, and that's known? OK.

Sorry I got confused. I thought new test cases are expected to be written, whereas you only wanted the sandbox compilation breakage fixed. My replies must have sounded so stupid :(
I have a patch to fix sandbox compilation in the v6 revision.

thanks.