Skip to contentSkip to conversation

nvmem: add driver handling U-Boot environment variables

PATCH In review Rafał Miłecki 2 participants 4 messages May 3, 2022 – May 6, 2022

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

PATCH v2 1/1 nvmem: add driver handling U-Boot environment variables

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

U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header.

This commit adds an NVMEM driver that:
1. Provides NVMEM access to environment vars binary data
2. Extracts variables as NVMEM cells

It can be used for:
1. Accessing env variables from user-space
2. Reading NVMEM cells by Linux drivers

---
V2: Drop ARCH_BCM4908 dependency as there are plenty architectures using
U-Boot bootloader. Thanks Srinivas.

---
 MAINTAINERS                |   1 +
 drivers/nvmem/Kconfig      |  11 ++
 drivers/nvmem/Makefile     |   2 +
 drivers/nvmem/u-boot-env.c | 236 +++++++++++++++++++++++++++++++++++++
 4 files changed, 250 insertions(+)
 create mode 100644 drivers/nvmem/u-boot-env.c
MAINTAINERS +1 -0
index 2869a958f5e4..368e532444d0 100644
@@ -20193,6 +20193,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/u-boot-env.c
 
 UACCE ACCELERATOR FRAMEWORK
 M:	Zhangfei Gao <[email protected]>
drivers/nvmem/Kconfig +11 -0
index 967d0084800e..eee3ff668629 100644
@@ -337,4 +337,15 @@ config NVMEM_APPLE_EFUSES
 	  This driver can also be built as a module. If so, the module will
 	  be called nvmem-apple-efuses.
 
+config NVMEM_U_BOOT_ENV
+	tristate "U-Boot environment variables support"
+	depends on OF && MTD
+	select CRC32
+	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 compiled as module it will be called nvmem_u-boot-env.
+
 endif
drivers/nvmem/Makefile +2 -0
index 00e136a0a123..d201a02a09ec 100644
@@ -67,3 +67,5 @@ obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP)	+= nvmem_sunplus_ocotp.o
 nvmem_sunplus_ocotp-y		:= sunplus-ocotp.o
 obj-$(CONFIG_NVMEM_APPLE_EFUSES)	+= nvmem-apple-efuses.o
 nvmem-apple-efuses-y 		:= apple-efuses.o
+obj-$(CONFIG_NVMEM_U_BOOT_ENV)	+= nvmem_u-boot-env.o
+nvmem_u-boot-env-y		:= u-boot-env.o
drivers/nvmem/u-boot-env.c new file +236 -0

236 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

Hello Rafał,

Quoted 11 lines · Rafał Miłecki

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

U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header.

This commit adds an NVMEM driver that:
1. Provides NVMEM access to environment vars binary data
2. Extracts variables as NVMEM cells

It can be used for:
1. Accessing env variables from user-space

Is this already possible? The only interface I know of is the /nvmem file in sysfs, but that one is not per cell, but per device.

drivers/nvmem/u-boot-env.c line 210

Quoted 9 lines
drivers/nvmem/u-boot-env.c new file +9 -0
+	label = of_get_property(np->parent, "label", NULL);
+	if (!label)
+		label = np->parent->name;
+
+	priv->mtd = get_mtd_device_nm(label);
+	if (IS_ERR(priv->mtd)) {
+		dev_err(dev, "Failed to find \"%s\" MTD device: %ld\n", label, PTR_ERR(priv->mtd));
+		return PTR_ERR(priv->mtd);
+	}

I am trying to make sense of this using the binding, but I can't. Do you have an example device tree fragment?

Cheers,
Ahmad

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw
Quoted 12 lines · Ahmad Fatoum

Hello Rafał,

Quoted 9 lines · Rafał Miłecki

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

U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header.

This commit adds an NVMEM driver that:
1. Provides NVMEM access to environment vars binary data
2. Extracts variables as NVMEM cells

Quoted 5 lines
Quoted 2 lines

It can be used for:
1. Accessing env variables from user-space

Is this already possible? The only interface I know of is the /nvmem file in sysfs, but that one is not per cell, but per device.

Maybe that wasn't precise enough, I should probably write:
1. Parsing binary data from user-space

In future I'd like to extend U-Boot's "printenv" tool to support reading env variables blob using Linux's sysfs as documented in the Documentation/ABI/stable/sysfs-bus-nvmem

Quoted 4 lines
Quoted 4 lines
drivers/nvmem/u-boot-env.c new file +4 -0
+	label = of_get_property(np->parent, "label", NULL);
+	if (!label)
+		label = np->parent->name;
+
Quoted 8 lines

drivers/nvmem/u-boot-env.c line 210

Quoted 5 lines
drivers/nvmem/u-boot-env.c new file +5 -0
+	priv->mtd = get_mtd_device_nm(label);
+	if (IS_ERR(priv->mtd)) {
+		dev_err(dev, "Failed to find \"%s\" MTD device: %ld\n", label, PTR_ERR(priv->mtd));
+		return PTR_ERR(priv->mtd);
+	}

I am trying to make sense of this using the binding, but I can't. Do you have an example device tree fragment?

This comes from unreleased yet board I'm working on.

It stores U-Boot env variables in the middle of U-Boot binary.

partitions {
	compatible = "fixed-partitions";
	#address-cells = <1>;
	#size-cells = <1>;

	partition@0 {
		label = "loader";
		reg = <0x0 0x100000>;

		partition@40000 {
			compatible = "u-boot,env";
			label = "u-boot-env";
			reg = <0x40000 0x4000>;
		};
	};

	partition@100000 {
		label = "image";
		reg = <0x100000 0x1fe00000>;
	};
};
#3 lore raw

Hello Rafał,

Quoted 19 lines · Rafał Miłecki
Quoted 12 lines · Ahmad Fatoum

Hello Rafał,

Quoted 9 lines · Rafał Miłecki

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

U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header.

This commit adds an NVMEM driver that:
1. Provides NVMEM access to environment vars binary data
2. Extracts variables as NVMEM cells

Quoted 5 lines
Quoted 2 lines

It can be used for:
1. Accessing env variables from user-space

Is this already possible? The only interface I know of is the /nvmem file in sysfs, but that one is not per cell, but per device.

Quoted 6 lines

Maybe that wasn't precise enough, I should probably write:
1. Parsing binary data from user-space

In future I'd like to extend U-Boot's "printenv" tool to support reading env variables blob using Linux's sysfs as documented in the Documentation/ABI/stable/sysfs-bus-nvmem

So, would you use this interface just to save fw_printenv the hassle of finding the environment (but redoing parsing) or do you intend to preprocess the data too? (e.g. only show the active environment)

For your use case, it sound like teaching NVMEM core to export cells as binary sysfs files would be very useful.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

+    label = of_get_property(np->parent, "label", NULL);
+    if (!label)
+        label = np->parent->name;
+
+    priv->mtd = get_mtd_device_nm(label);
+    if (IS_ERR(priv->mtd)) {
+        dev_err(dev, "Failed to find \"%s\" MTD device: %ld\n", label, PTR_ERR(priv->mtd)); +        return PTR_ERR(priv->mtd);
+    }

Quoted 6 lines
Quoted 2 lines

I am trying to make sense of this using the binding, but I can't. Do you have an example device tree fragment?

This comes from unreleased yet board I'm working on.

It stores U-Boot env variables in the middle of U-Boot binary.

Huh, that's an odd layout. I am not sure whether of_get_property to arrive at the parent is such a good idea though. Doesn't it enforce a limitation that there must not exist two partitions with the same label?

Some systems can have a second recovery bootloader for example. Given that these are device tree nodes, wouldn't it be possible to find the MTD by device tree parent instead of via its label?

Quoted 16 lines

partitions {
    compatible = "fixed-partitions";
    #address-cells = <1>;
    #size-cells = <1>;

    partition@0 {
        label = "loader";
        reg = <0x0 0x100000>;

        partition@40000 {
            compatible = "u-boot,env";
            label = "u-boot-env";
            reg = <0x40000 0x4000>;
        };
    };

Quoted 5 lines

    partition@100000 {
        label = "image";
        reg = <0x100000 0x1fe00000>;
    };
};

Thanks,
Ahmad

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |