Skip to contentSkip to conversation

nvmem: u-boot-env: error if NVMEM device is too small

PATCH Applied Rafał Miłecki 2 participants 4 messages Jul 29, 2024 – Aug 7, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 nvmem: u-boot-env: error if NVMEM device is too small

From: John Thomson <[email protected]>

Verify data size before trying to parse it to avoid reading out of buffer. This could happen in case of problems at MTD level or invalid DT bindings.

Fixes d5542923f200 ("nvmem: add driver handling U-Boot environment variables")
[rmilecki: simplify commit description & rebase]

---
V2: New patch in the series

 drivers/nvmem/u-boot-env.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvmem/u-boot-env.c +7 -0
index 936e39b20b38..593f0bf4a395 100644
@@ -176,6 +176,13 @@ static int u_boot_env_parse(struct u_boot_env *priv)
 		data_offset = offsetof(struct u_boot_env_image_broadcom, data);
 		break;
 	}
+
+	if (dev_size < data_offset) {
+		dev_err(dev, "Device too small for u-boot-env\n");
+		err = -EIO;
+		goto err_kfree;
+	}
+
 	crc32_addr = (__le32 *)(buf + crc32_offset);
 	crc32 = le32_to_cpu(*crc32_addr);
 	crc32_data_len = dev_size - crc32_data_offset;
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 2/3 dt-bindings: nvmem: convert U-Boot env to a layout

From: Rafał Miłecki <[email protected]>

U-Boot environment variables can be stored in various data sources. MTD is just one of available options. Refactor DT binding into a layout so it can be used with UBI volumes and other NVMEM devices.

---
 .../bindings/nvmem/layouts/nvmem-layout.yaml  |  1 +
 .../nvmem/{ => layouts}/u-boot,env.yaml       | 39 ++++++++++++++++---
 2 files changed, 35 insertions(+), 5 deletions(-)
 rename Documentation/devicetree/bindings/nvmem/{ => layouts}/u-boot,env.yaml (75%)
Documentation/devicetree/bindings/nvmem/layouts/nvmem-layout.yaml +1 -0
index 3b40f7880774..382507060651 100644
@@ -21,6 +21,7 @@ oneOf:
   - $ref: fixed-layout.yaml
   - $ref: kontron,sl28-vpd.yaml
   - $ref: onie,tlv-layout.yaml
+  - $ref: u-boot,env.yaml
 
 properties:
   compatible: true
Documentation/devicetree/bindings/nvmem/layouts/u-boot,env.yaml renamed from Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +34 -5
similarity index 75%
rename from Documentation/devicetree/bindings/nvmem/u-boot,env.yaml
rename to Documentation/devicetree/bindings/nvmem/layouts/u-boot,env.yaml
index 9c36afc7084b..56a8f55d4a09 100644
@@ -1,10 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
 %YAML 1.2
 ---
-$id: http://devicetree.org/schemas/nvmem/u-boot,env.yaml#
+$id: http://devicetree.org/schemas/nvmem/layouts/u-boot,env.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: U-Boot environment variables
+title: U-Boot environment variables layout
 
 description: |
   U-Boot uses environment variables to store device parameters and
@@ -21,9 +21,6 @@ description: |
   This binding allows marking storage device (as containing env data) and
   specifying used format.
 
-  Right now only flash partition case is covered but it may be extended to e.g.
-  UBI volumes in the future.
-
   Variables can be defined as NVMEM device subnodes.
 
 maintainers:
@@ -42,6 +39,7 @@ properties:
         const: brcm,env
 
   reg:
+    description: Partition offset and size for env on top of MTD
     maxItems: 1
 
   bootcmd:
@@ -58,6 +56,17 @@ properties:
         description: The first argument is a MAC address offset.
         const: 1
 
+allOf:
+  - if:
+      properties:
+        $nodename:
+          not:
+            contains:
+              pattern: "^partition@[0-9a-f]+$"
+    then:
+      properties:
+        reg: false
+
 additionalProperties: false
 
 examples:
@@ -101,3 +110,23 @@ examples:
             };
         };
     };
+  - |
+    partition@0 {
+        reg = <0x0 0x100000>;
+        label = "ubi";
+        compatible = "linux,ubi";
+
+        volumes {
+            ubi-volume-u-boot-env {
+                volname = "env";
+
+                nvmem-layout {
+                    compatible = "u-boot,env";
+
+                    ethaddr {
+                        #nvmem-cell-cells = <1>;
+                    };
+                };
+            };
+        };
+    };
Signature · 1 line
2.35.3
#2 lore raw

PATCH v2 3/3 nvmem: layouts: add U-Boot env layout

In reply to Rafał Miłecki

From: Rafał Miłecki <[email protected]>

U-Boot environment variables are stored in a specific format. Actual data can be placed in various storage sources (MTD, UBI volume, EEPROM, NVRAM, etc.).

Move all generic (NVMEM device independent) code from NVMEM device driver to an NVMEM layout driver. Then add a simple NVMEM layout code on top of it.

This allows using NVMEM layout for parsing U-Boot env data stored in any kind of NVMEM device.

The old NVMEM glue driver stays in place for handling bindings in the MTD context. To avoid code duplication it uses exported layout parsing function. Please note that handling MTD & NVMEM layout bindings may be refactored in the future.

---
V2: Add MODULE_DESCRIPTION()

This change was originally sent (and approved by Miquel) as a [PATCH V3 6/6] nvmem: layouts: add U-Boot env layout

I just adjusted it to the approved binding and updated commit message. I kept Miquel's Reviewed-by tag due to minimal changes.

I've successfully tested this code using it in both ways: as NVMEM device driver & NVMEM layout.

 MAINTAINERS                        |   1 +
 drivers/nvmem/Kconfig              |   3 +-
 drivers/nvmem/layouts/Kconfig      |  11 ++
 drivers/nvmem/layouts/Makefile     |   1 +
 drivers/nvmem/layouts/u-boot-env.c | 211 +++++++++++++++++++++++++++++
 drivers/nvmem/layouts/u-boot-env.h |  15 ++
 drivers/nvmem/u-boot-env.c         | 165 +---------------------
 7 files changed, 242 insertions(+), 165 deletions(-)
 create mode 100644 drivers/nvmem/layouts/u-boot-env.c
 create mode 100644 drivers/nvmem/layouts/u-boot-env.h
MAINTAINERS +1 -0
index 12b870712da4..b785ef03dda3 100644
@@ -23355,6 +23355,7 @@ U-BOOT ENVIRONMENT VARIABLES
 M:	Rafał Miłecki <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/nvmem/u-boot,env.yaml
+F:	drivers/nvmem/layouts/u-boot-env.c
 F:	drivers/nvmem/u-boot-env.c
 
 UACCE ACCELERATOR FRAMEWORK
drivers/nvmem/Kconfig +1 -2
index 283134498fbc..d2c384f58028 100644
@@ -363,8 +363,7 @@ config NVMEM_SUNXI_SID
 config NVMEM_U_BOOT_ENV
 	tristate "U-Boot environment variables support"
 	depends on OF && MTD
-	select CRC32
-	select GENERIC_NET_UTILS
+	select NVMEM_LAYOUT_U_BOOT_ENV
 	help
 	  U-Boot stores its setup as environment variables. This driver adds
 	  support for verifying & exporting such data. It also exposes variables
drivers/nvmem/layouts/Kconfig +11 -0
index 9c6e672fc350..5e586dfebe47 100644
@@ -26,6 +26,17 @@ config NVMEM_LAYOUT_ONIE_TLV
 
 	  If unsure, say N.
 
+config NVMEM_LAYOUT_U_BOOT_ENV
+	tristate "U-Boot environment variables layout"
+	select CRC32
+	select GENERIC_NET_UTILS
+	help
+	  U-Boot stores its setup as environment variables. This driver adds
+	  support for verifying & exporting such data. It also exposes variables
+	  as NVMEM cells so they can be referenced by other drivers.
+
+	  If unsure, say N.
+
 endmenu
 
 endif
drivers/nvmem/layouts/Makefile +1 -0
index 2974bd7d33ed..4940c9db0665 100644
@@ -5,3 +5,4 @@
 
 obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
 obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o
+obj-$(CONFIG_NVMEM_LAYOUT_U_BOOT_ENV) += u-boot-env.o
drivers/nvmem/layouts/u-boot-env.c new file +211 -0

211 lines not on this page · raw message

drivers/nvmem/layouts/u-boot-env.h new file +15 -0
new file mode 100644
index 000000000000..dd5f280ac047
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H
+#define _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H
+
+enum u_boot_env_format {
+	U_BOOT_FORMAT_SINGLE,
+	U_BOOT_FORMAT_REDUNDANT,
+	U_BOOT_FORMAT_BROADCOM,
+};
+
+int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
+		     enum u_boot_env_format format);
+
+#endif  /* ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H */
drivers/nvmem/u-boot-env.c +2 -163

197 lines not on this page · raw message

Signature · 1 line
2.35.3
#3 lore raw
In reply to Rafał Miłecki
Quoted 5 lines · Rafał Miłecki

Verify data size before trying to parse it to avoid reading out of buffer. This could happen in case of problems at MTD level or invalid DT bindings.

Applied, thanks!

[1/3] nvmem: u-boot-env: error if NVMEM device is too small

      commit: a80f2ebe78d7a81354b1c9d59fcb79b77982df2d
[2/3] dt-bindings: nvmem: convert U-Boot env to a layout
      commit: 1b3f5b88c541318a95fc74db80083b87c1020e03
[3/3] nvmem: layouts: add U-Boot env layout
      commit: 641d6abbe15f96a4d2cce5dc48940fd22916d15e

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>