dt-bindings: nvmem: layouts: add U-Boot environment variables layout
Subsystems (4)
PATCH v2 1/5 dt-bindings: nvmem: layouts: add U-Boot environment variables layout
From: Rafał Miłecki <[email protected]>
U-Boot env data is a way of storing firmware variables. It's a format that can be used of top of various storage devices. Its binding should be an NVMEM layout instead of a standalone device.
This patch adds layout binding which allows using it on top of MTD NVMEM device as well as any other. At the same time it deprecates the old combined binding.
---
V2: Add "-layout" suffix to compatibles to avoid conflict
.../nvmem/layouts/u-boot,env-layout.yaml | 55 +++++++++++++++++++ .../devicetree/bindings/nvmem/u-boot,env.yaml | 6 ++ 2 files changed, 61 insertions(+) create mode 100644 Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml
Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml new file +55 -0
@@ -0,0 +1,55 @@ +# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/layouts/u-boot,env-layout.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: NVMEM layout of U-Boot environment variables + +maintainers: + - Rafał Miłecki <[email protected]> + +description: + U-Boot uses environment variables to store device parameters and + configuration. They may be used for booting process, setup or keeping end user + info. + + Data is stored using U-Boot specific formats (variant specific header and NUL + separated key-value pairs). + +properties: + compatible: + oneOf: + - description: A standalone env data block + const: u-boot,env-layout + - description: Two redundant blocks with active one flagged + const: u-boot,env-redundant-bool-layout + - description: Two redundant blocks with active having higher counter + const: u-boot,env-redundant-count-layout + - description: Broadcom's variant with custom header + const: brcm,env-layout + +additionalProperties: false + +examples: + - | + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + reg = <0x0 0x40000>; + label = "u-boot"; + read-only; + }; + + partition@40000 { + reg = <0x40000 0x10000>; + label = "u-boot-env"; + + nvmem-layout { + compatible = "u-boot,env-layout"; + }; + }; + };
Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +6 -0
@@ -26,9 +26,15 @@ description: | Variables can be defined as NVMEM device subnodes. + Introduction of NVMEM layouts exposed a limitation of this binding design. + Description of NVMEM data content should be separated from NVMEM devices. + Since the introduction of U-Boot env data layout this binding is deprecated. + maintainers: - Rafał Miłecki <[email protected]> +deprecated: true + properties: compatible: oneOf:
Signature · 1 line
2.35.3
PATCH v2 2/5 nvmem: core: add nvmem_dev_size() helper
From: Rafał Miłecki <[email protected]>
This is required by layouts that need to read whole NVMEM content. It's especially useful for NVMEM devices without hardcoded layout (like U-Boot environment data block).
--- drivers/nvmem/core.c | 13 +++++++++++++ include/linux/nvmem-consumer.h | 1 + 2 files changed, 14 insertions(+)
drivers/nvmem/core.c +13 -0
@@ -2163,6 +2163,19 @@ const char *nvmem_dev_name(struct nvmem_device *nvmem) }EXPORT_SYMBOL_GPL (nvmem_dev_name); +/** +* nvmem_dev_size() - Get the size of a given nvmem device. +* +* @nvmem: nvmem device. +* +* Return: size of the nvmem device. +*/ +size_t nvmem_dev_size (struct nvmem_device *nvmem) +{ +return nvmem->size; +} +EXPORT_SYMBOL_GPL (nvmem_dev_size); +static int __init nvmem_init (void ) {int ret;
include/linux/nvmem-consumer.h +1 -0
@@ -81,6 +81,7 @@ int nvmem_device_cell_write(struct nvmem_device *nvmem, struct nvmem_cell_info *info, void *buf);const char *nvmem_dev_name (struct nvmem_device *nvmem); +size_t nvmem_dev_size (struct nvmem_device *nvmem);void nvmem_add_cell_lookups (struct nvmem_cell_lookup *entries,size_t nentries);
Signature · 1 line
2.35.3
PATCH v2 3/5 nvmem: u-boot-env: use more nvmem subsystem helpers
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
1. Use nvmem_dev_size() and nvmem_device_read() to make this driver less mtd dependent
2. Use nvmem_add_one_cell() to simplify adding NVMEM cells
---
V2: Don't introduce memleak when handling nvmem_device_read() failures
drivers/nvmem/u-boot-env.c | 78 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 42 deletions(-)
drivers/nvmem/u-boot-env.c +36 -42
@@ -23,13 +23,10 @@ enum u_boot_env_format {struct u_boot_env {struct device *dev; +struct nvmem_device *nvmem;enum u_boot_env_format format;struct mtd_info *mtd; - -/* Cells */ -struct nvmem_cell_info *cells; -int ncells; };struct u_boot_env_image_single {
@@ -94,70 +91,70 @@ static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, istatic int u_boot_env_add_cells (struct u_boot_env *priv,uint8_t *buf,size_t data_offset,size_t data_len) { +struct nvmem_device *nvmem = priv->nvmem;struct device *dev = priv->dev;char *data = buf + data_offset;char *var, *value, *eq; -int idx; - - priv->ncells =0 ; -for (var = data; var < data + data_len && *var; var +=strlen (var) +1 ) - priv->ncells++; - - priv->cells =devm_kcalloc (dev, priv->ncells,sizeof (*priv->cells),GFP_KERNEL ); -if (!priv->cells) -return -ENOMEM ; -for (var = data, idx =0 ; +for (var = data; var < data + data_len && *var; - var = value +strlen (value) +1 , idx++) { + var = value +strlen (value) +1 ) { +struct nvmem_cell_info info = {}; + eq =strchr (var,'=' );if (!eq)break ; *eq ='\0' ; value = eq +1 ; - priv->cells[idx].name =devm_kstrdup (dev, var,GFP_KERNEL ); -if (!priv->cells[idx].name) + info.name =devm_kstrdup (dev, var,GFP_KERNEL ); +if (!info.name)return -ENOMEM ; - priv->cells[idx].offset = data_offset + value - data; - priv->cells[idx].bytes =strlen (value); - priv->cells[idx].np =of_get_child_by_name (dev->of_node, priv->cells[idx].name); + info.offset = data_offset + value - data; + info.bytes =strlen (value); + info.np =of_get_child_by_name (dev->of_node, info.name);if (!strcmp (var,"ethaddr" )) { - priv->cells[idx].raw_len =strlen (value); - priv->cells[idx].bytes =ETH_ALEN ; - priv->cells[idx].read_post_process = u_boot_env_read_post_process_ethaddr; + info.raw_len =strlen (value); + info.bytes =ETH_ALEN ; + info.read_post_process = u_boot_env_read_post_process_ethaddr; } - } -if (WARN_ON (idx != priv->ncells)) - priv->ncells = idx; +nvmem_add_one_cell (nvmem, &info); + }return 0 ; }static int u_boot_env_parse (struct u_boot_env *priv) { +struct nvmem_device *nvmem = priv->nvmem;struct device *dev = priv->dev;size_t crc32_data_offset;size_t crc32_data_len;size_t crc32_offset;size_t data_offset;size_t data_len; +size_t dev_size;uint32_t crc32;uint32_t calc; -size_t bytes;uint8_t *buf; +int bytes;int err; - buf =kcalloc (1 , priv->mtd->size,GFP_KERNEL ); + dev_size =nvmem_dev_size (nvmem); + + buf =kcalloc (1 , dev_size,GFP_KERNEL );if (!buf) { err = -ENOMEM ; goto err_out; } - err =mtd_read (priv->mtd,0 , priv->mtd->size, &bytes, buf); -if ((err && !mtd_is_bitflip (err)) || bytes != priv->mtd->size) { -dev_err (dev,"Failed to read from mtd: %d\n" , err); + bytes =nvmem_device_read (nvmem,0 , dev_size, buf); +if (bytes <0 ) { + err = bytes; + goto err_kfree; + }else if (bytes != dev_size) { + err = -EIO ; goto err_kfree; }
@@ -179,8 +176,8 @@ static int u_boot_env_parse(struct u_boot_env *priv)break ; } crc32 =le32_to_cpu (*(__le32 *)(buf + crc32_offset)); - crc32_data_len = priv->mtd->size - crc32_data_offset; - data_len = priv->mtd->size - data_offset; + crc32_data_len = dev_size - crc32_data_offset; + data_len = dev_size - data_offset; calc =crc32 (~0 , buf + crc32_data_offset, crc32_data_len) ^ ~0L ;if (calc != crc32) {
@@ -189,7 +186,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) goto err_kfree; } - buf[priv->mtd->size -1 ] ='\0' ; + buf[dev_size -1 ] ='\0' ; err =u_boot_env_add_cells (priv, buf, data_offset, data_len);if (err)dev_err (dev,"Failed to add cells: %d\n" , err);
@@ -209,7 +206,6 @@ static int u_boot_env_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct device_node *np = dev->of_node;struct u_boot_env *priv; -int err; priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL );if (!priv)
@@ -224,17 +220,15 @@ static int u_boot_env_probe(struct platform_device *pdev)return PTR_ERR (priv->mtd); } - err =u_boot_env_parse (priv); -if (err) -return err; - config.dev = dev; - config.cells = priv->cells; - config.ncells = priv->ncells; config.priv = priv; config.size = priv->mtd->size; -return PTR_ERR_OR_ZERO (devm_nvmem_register (dev, &config)); + priv->nvmem =devm_nvmem_register (dev, &config); +if (IS_ERR (priv->nvmem)) +return PTR_ERR (priv->nvmem); + +return u_boot_env_parse (priv); }static const struct of_device_id u_boot_env_of_match_table[] = {
Signature · 1 line
2.35.3
Re: [PATCH V2 3/5] nvmem: u-boot-env: use more nvmem subsystem helpers
Quoted 5 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
1. Use
nvmem_dev_size()andnvmem_device_read()to make this driver less mtd dependent
2. Usenvmem_add_one_cell()to simplify adding NVMEM cells
Shouldn't this be 2 different patches?
thanks,
greg k-h
Re: [PATCH V2 3/5] nvmem: u-boot-env: use more nvmem subsystem helpers
Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
1. Use
nvmem_dev_size()andnvmem_device_read()to make this driver less mtd dependent
2. Usenvmem_add_one_cell()to simplify adding NVMEM cellsShouldn't this be 2 different patches?
I used to maintainers complaining my patches are too small and not the other way ;) I think it happened two or three times with mtd subsys.
Sure, I can split it.
Miquel Raynal
maintainerRe: [PATCH V2 3/5] nvmem: u-boot-env: use more nvmem subsystem helpers
Hi Rafał,
[email protected] wrote on Tue, 19 Dec 2023 19:16:37 +0100:
Quoted 12 lines · Greg Kroah-Hartman
Quoted 8 lines · Rafał Miłecki
Quoted 5 lines
From: Rafał Miłecki <[email protected]>
1. Use
nvmem_dev_size()andnvmem_device_read()to make this driver less mtd dependent
2. Usenvmem_add_one_cell()to simplify adding NVMEM cellsShouldn't this be 2 different patches?
I used to maintainers complaining my patches are too small and not the other way ;) I think it happened two or three times with mtd subsys.
A single patch may be too small if it's alone and we don't see the big picture, otherwise I have no issue with small patches, do I? Anyway, in this case I don't mind the patch being split or kept like this, just keep my R-by applied to both if you do indeed split.
Thanks,
Miquèl
PATCH v2 4/5 nvmem: u-boot-env: improve coding style
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
1. Prefer kzalloc() over kcalloc()
See memory-allocation.rst which says: "to be on the safe side it's best to use routines that set memory to zero, like kzalloc()"
2. Drop dev_err() for u_boot_env_add_cells() fail
It can fail only on -ENOMEM. We don't want to print error then.
3. Add extra "crc32_addr" variable
It makes code reading header's crc32 easier to understand / review.
---
V2: New patch with suggestions from Miquel
drivers/nvmem/u-boot-env.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvmem/u-boot-env.c +4 -4
@@ -132,6 +132,7 @@ static int u_boot_env_parse(struct u_boot_env *priv)size_t crc32_data_offset;size_t crc32_data_len;size_t crc32_offset; +__le32 *crc32_addr;size_t data_offset;size_t data_len;size_t dev_size;
@@ -143,7 +144,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) dev_size =nvmem_dev_size (nvmem); - buf =kcalloc (1 , dev_size,GFP_KERNEL ); + buf =kzalloc (dev_size,GFP_KERNEL );if (!buf) { err = -ENOMEM ; goto err_out;
@@ -175,7 +176,8 @@ static int u_boot_env_parse(struct u_boot_env *priv) data_offset = offsetof(struct u_boot_env_image_broadcom , data);break ; } - crc32 =le32_to_cpu (*(__le32 *)(buf + crc32_offset)); + crc32_addr = (__le32 *)(buf + crc32_offset); + crc32 =le32_to_cpu (*crc32_addr); crc32_data_len = dev_size - crc32_data_offset; data_len = dev_size - data_offset;
@@ -188,8 +190,6 @@ static int u_boot_env_parse(struct u_boot_env *priv) buf[dev_size -1 ] ='\0' ; err =u_boot_env_add_cells (priv, buf, data_offset, data_len); -if (err) -dev_err (dev,"Failed to add cells: %d\n" , err); err_kfree:kfree (buf);
Signature · 1 line
2.35.3
Re: [PATCH V2 4/5] nvmem: u-boot-env: improve coding style
Hi Rafał,
[email protected] wrote on Tue, 19 Dec 2023 18:40:24 +0100:
Quoted 11 lines
From: Rafał Miłecki <[email protected]>
1. Prefer
kzalloc()overkcalloc()
Seememory-allocation.rstwhich says: "to be on the safe side it's best to use routines that set memory to zero, likekzalloc()"
2. Dropdev_err()foru_boot_env_add_cells()fail
It can fail only on-ENOMEM. We don't want to print error then.
3. Add extra "crc32_addr" variable
It makes code reading header's crc32 easier to understand / review.Signed-off-by Rafał Miłecki <[email protected]>
Looks much nicer :)
Thanks,
Miquèl
PATCH v2 5/5 nvmem: layouts: add U-Boot env layout
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
This patch moves all generic (NVMEM devices independent) code from NVMEM device driver to NVMEM layout driver. Then it adds a simple NVMEM layout code on top of it.
Thanks to proper layout it's possible to support U-Boot env data stored on any kind of NVMEM device.
For backward compatibility with old DT bindings we need to keep old NVMEM device driver functional. To avoid code duplication a parsing function is exported and reused in it.
---
V2: Support new compatibles & use device_get_match_data() helper
IMPORTANT:
This is based on top of the:
[PATCH v6.8 1/2] nvmem: layouts: refactor .add_cells() callback arguments
MAINTAINERS | 1 + drivers/nvmem/Kconfig | 3 +- drivers/nvmem/layouts/Kconfig | 11 ++ drivers/nvmem/layouts/Makefile | 1 + drivers/nvmem/layouts/u-boot-env.c | 204 +++++++++++++++++++++++++++++ drivers/nvmem/layouts/u-boot-env.h | 15 +++ drivers/nvmem/u-boot-env.c | 158 +--------------------- 7 files changed, 235 insertions(+), 158 deletions(-) create mode 100644 drivers/nvmem/layouts/u-boot-env.c create mode 100644 drivers/nvmem/layouts/u-boot-env.h
MAINTAINERS +1 -0
@@ -22282,6 +22282,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
@@ -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
@@ -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
@@ -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 +204 -0
204 lines not on this page · raw message
drivers/nvmem/layouts/u-boot-env.h new file +15 -0
@@ -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 -156
190 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [PATCH V2 5/5] nvmem: layouts: add U-Boot env layout
Hi Rafał,
[email protected] wrote on Tue, 19 Dec 2023 18:40:25 +0100:
Quoted 3 lines
From: Rafał Miłecki <[email protected]>
This patch moves all generic (NVMEM devices independent) code from NVMEM
Nit: In general we avoid starting with "This patch does..." and instead use the imperative form, like: "Move all generic code..."
Quoted 1 line
device driver to NVMEM layout driver. Then it adds a simple NVMEM layout
Then add...
Quoted 9 lines
code on top of it.
Thanks to proper layout it's possible to support U-Boot env data stored on any kind of NVMEM device.
For backward compatibility with old DT bindings we need to keep old NVMEM device driver functional. To avoid code duplication a parsing function is exported and reused in it.
Quoted 7 lines
Signed-off-by Rafał Miłecki <[email protected]>---
V2: Support new compatibles & usedevice_get_match_data()helperIMPORTANT:
This is based on top of the:
[PATCH v6.8 1/2] nvmem: layouts: refactor .add_cells() callback arguments
Thanks for the move. Looks good to me:
Thanks,
Miquèl