1 From d5542923f200f95bddf524f36fd495f78aa28e3c Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Fri, 16 Sep 2022 13:20:48 +0100
4 Subject: [PATCH] nvmem: add driver handling U-Boot environment variables
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 U-Boot stores its setup as environment variables. It's a list of
10 key-value pairs stored on flash device with a custom header.
12 This commit adds an NVMEM driver that:
13 1. Provides NVMEM access to environment vars binary data
14 2. Extracts variables as NVMEM cells
16 Current Linux's NVMEM sysfs API allows reading whole NVMEM data block.
17 It can be used by user-space tools for reading U-Boot env vars block
18 without the hassle of finding its location. Parsing will still need to
21 Kernel-parsed NVMEM cells can be read however by Linux drivers. This may
22 be useful for Ethernet drivers for reading device MAC address which is
23 often stored as U-Boot env variable.
25 Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
26 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
27 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
28 Link: https://lore.kernel.org/r/20220916122100.170016-2-srinivas.kandagatla@linaro.org
29 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
32 drivers/nvmem/Kconfig | 13 +++
33 drivers/nvmem/Makefile | 2 +
34 drivers/nvmem/u-boot-env.c | 218 +++++++++++++++++++++++++++++++++++++
35 4 files changed, 234 insertions(+)
36 create mode 100644 drivers/nvmem/u-boot-env.c
38 --- a/drivers/nvmem/Kconfig
39 +++ b/drivers/nvmem/Kconfig
40 @@ -344,4 +344,17 @@ config NVMEM_APPLE_EFUSES
41 This driver can also be built as a module. If so, the module will
42 be called nvmem-apple-efuses.
44 +config NVMEM_U_BOOT_ENV
45 + tristate "U-Boot environment variables support"
46 + depends on OF && MTD
49 + U-Boot stores its setup as environment variables. This driver adds
50 + support for verifying & exporting such data. It also exposes variables
51 + as NVMEM cells so they can be referenced by other drivers.
53 + Currently this drivers works only with env variables on top of MTD.
55 + If compiled as module it will be called nvmem_u-boot-env.
58 --- a/drivers/nvmem/Makefile
59 +++ b/drivers/nvmem/Makefile
60 @@ -69,3 +69,5 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvme
61 nvmem-apple-efuses-y := apple-efuses.o
62 obj-$(CONFIG_MICROCHIP_OTPC) += nvmem-microchip-otpc.o
63 nvmem-microchip-otpc-y := microchip-otpc.o
64 +obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o
65 +nvmem_u-boot-env-y := u-boot-env.o
67 +++ b/drivers/nvmem/u-boot-env.c
69 +// SPDX-License-Identifier: GPL-2.0-only
71 + * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
74 +#include <linux/crc32.h>
75 +#include <linux/mod_devicetable.h>
76 +#include <linux/module.h>
77 +#include <linux/mtd/mtd.h>
78 +#include <linux/nvmem-consumer.h>
79 +#include <linux/nvmem-provider.h>
80 +#include <linux/of_device.h>
81 +#include <linux/platform_device.h>
82 +#include <linux/slab.h>
84 +enum u_boot_env_format {
85 + U_BOOT_FORMAT_SINGLE,
86 + U_BOOT_FORMAT_REDUNDANT,
91 + enum u_boot_env_format format;
93 + struct mtd_info *mtd;
96 + struct nvmem_cell_info *cells;
100 +struct u_boot_env_image_single {
105 +struct u_boot_env_image_redundant {
111 +static int u_boot_env_read(void *context, unsigned int offset, void *val,
114 + struct u_boot_env *priv = context;
115 + struct device *dev = priv->dev;
119 + err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
120 + if (err && !mtd_is_bitflip(err)) {
121 + dev_err(dev, "Failed to read from mtd: %d\n", err);
125 + if (bytes_read != bytes) {
126 + dev_err(dev, "Failed to read %zu bytes\n", bytes);
133 +static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
134 + size_t data_offset, size_t data_len)
136 + struct device *dev = priv->dev;
137 + char *data = buf + data_offset;
138 + char *var, *value, *eq;
142 + for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
145 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
149 + for (var = data, idx = 0;
150 + var < data + data_len && *var;
151 + var = value + strlen(value) + 1, idx++) {
152 + eq = strchr(var, '=');
158 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
159 + if (!priv->cells[idx].name)
161 + priv->cells[idx].offset = data_offset + value - data;
162 + priv->cells[idx].bytes = strlen(value);
165 + if (WARN_ON(idx != priv->ncells))
166 + priv->ncells = idx;
171 +static int u_boot_env_parse(struct u_boot_env *priv)
173 + struct device *dev = priv->dev;
174 + size_t crc32_data_offset;
175 + size_t crc32_data_len;
176 + size_t crc32_offset;
177 + size_t data_offset;
185 + buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
191 + err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
192 + if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
193 + dev_err(dev, "Failed to read from mtd: %d\n", err);
197 + switch (priv->format) {
198 + case U_BOOT_FORMAT_SINGLE:
199 + crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
200 + crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
201 + data_offset = offsetof(struct u_boot_env_image_single, data);
203 + case U_BOOT_FORMAT_REDUNDANT:
204 + crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
205 + crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
206 + data_offset = offsetof(struct u_boot_env_image_redundant, data);
209 + crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset));
210 + crc32_data_len = priv->mtd->size - crc32_data_offset;
211 + data_len = priv->mtd->size - data_offset;
213 + calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
214 + if (calc != crc32) {
215 + dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
220 + buf[priv->mtd->size - 1] = '\0';
221 + err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
223 + dev_err(dev, "Failed to add cells: %d\n", err);
231 +static int u_boot_env_probe(struct platform_device *pdev)
233 + struct nvmem_config config = {
234 + .name = "u-boot-env",
235 + .reg_read = u_boot_env_read,
237 + struct device *dev = &pdev->dev;
238 + struct device_node *np = dev->of_node;
239 + struct u_boot_env *priv;
242 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
247 + priv->format = (uintptr_t)of_device_get_match_data(dev);
249 + priv->mtd = of_get_mtd_device_by_node(np);
250 + if (IS_ERR(priv->mtd)) {
251 + dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
252 + return PTR_ERR(priv->mtd);
255 + err = u_boot_env_parse(priv);
260 + config.cells = priv->cells;
261 + config.ncells = priv->ncells;
262 + config.priv = priv;
263 + config.size = priv->mtd->size;
265 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
268 +static const struct of_device_id u_boot_env_of_match_table[] = {
269 + { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
270 + { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
271 + { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
275 +static struct platform_driver u_boot_env_driver = {
276 + .probe = u_boot_env_probe,
278 + .name = "u_boot_env",
279 + .of_match_table = u_boot_env_of_match_table,
282 +module_platform_driver(u_boot_env_driver);
284 +MODULE_AUTHOR("Rafał Miłecki");
285 +MODULE_LICENSE("GPL");
286 +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);