1 From f955dc14450695564926711cf9fa8e1d5d854302 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Wed, 15 Jun 2022 21:43:00 +0200
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 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
26 Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
27 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
30 --- a/drivers/nvmem/Kconfig
31 +++ b/drivers/nvmem/Kconfig
32 @@ -270,4 +270,17 @@ config SPRD_EFUSE
33 This driver can also be built as a module. If so, the module
34 will be called nvmem-sprd-efuse.
36 +config NVMEM_U_BOOT_ENV
37 + tristate "U-Boot environment variables support"
38 + depends on OF && MTD
41 + U-Boot stores its setup as environment variables. This driver adds
42 + support for verifying & exporting such data. It also exposes variables
43 + as NVMEM cells so they can be referenced by other drivers.
45 + Currently this drivers works only with env variables on top of MTD.
47 + If compiled as module it will be called nvmem_u-boot-env.
50 --- a/drivers/nvmem/Makefile
51 +++ b/drivers/nvmem/Makefile
52 @@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq
53 nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o
54 obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o
55 nvmem_sprd_efuse-y := sprd-efuse.o
56 +obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o
57 +nvmem_u-boot-env-y := u-boot-env.o
59 +++ b/drivers/nvmem/u-boot-env.c
61 +// SPDX-License-Identifier: GPL-2.0-only
63 + * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
66 +#include <linux/crc32.h>
67 +#include <linux/mod_devicetable.h>
68 +#include <linux/module.h>
69 +#include <linux/mtd/mtd.h>
70 +#include <linux/nvmem-consumer.h>
71 +#include <linux/nvmem-provider.h>
72 +#include <linux/of_device.h>
73 +#include <linux/platform_device.h>
74 +#include <linux/slab.h>
76 +enum u_boot_env_format {
77 + U_BOOT_FORMAT_SINGLE,
78 + U_BOOT_FORMAT_REDUNDANT,
83 + enum u_boot_env_format format;
85 + struct mtd_info *mtd;
88 + struct nvmem_cell_info *cells;
92 +struct u_boot_env_image_single {
97 +struct u_boot_env_image_redundant {
103 +static int u_boot_env_read(void *context, unsigned int offset, void *val,
106 + struct u_boot_env *priv = context;
107 + struct device *dev = priv->dev;
111 + err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
112 + if (err && !mtd_is_bitflip(err)) {
113 + dev_err(dev, "Failed to read from mtd: %d\n", err);
117 + if (bytes_read != bytes) {
118 + dev_err(dev, "Failed to read %zu bytes\n", bytes);
125 +static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
126 + size_t data_offset, size_t data_len)
128 + struct device *dev = priv->dev;
129 + char *data = buf + data_offset;
130 + char *var, *value, *eq;
134 + for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
137 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
141 + for (var = data, idx = 0;
142 + var < data + data_len && *var;
143 + var = value + strlen(value) + 1, idx++) {
144 + eq = strchr(var, '=');
150 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
151 + if (!priv->cells[idx].name)
153 + priv->cells[idx].offset = data_offset + value - data;
154 + priv->cells[idx].bytes = strlen(value);
157 + if (WARN_ON(idx != priv->ncells))
158 + priv->ncells = idx;
163 +static int u_boot_env_parse(struct u_boot_env *priv)
165 + struct device *dev = priv->dev;
166 + size_t crc32_data_offset;
167 + size_t crc32_data_len;
168 + size_t crc32_offset;
169 + size_t data_offset;
177 + buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
183 + err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
184 + if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
185 + dev_err(dev, "Failed to read from mtd: %d\n", err);
189 + switch (priv->format) {
190 + case U_BOOT_FORMAT_SINGLE:
191 + crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
192 + crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
193 + data_offset = offsetof(struct u_boot_env_image_single, data);
195 + case U_BOOT_FORMAT_REDUNDANT:
196 + crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
197 + crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
198 + data_offset = offsetof(struct u_boot_env_image_redundant, data);
201 + crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset));
202 + crc32_data_len = priv->mtd->size - crc32_data_offset;
203 + data_len = priv->mtd->size - data_offset;
205 + calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
206 + if (calc != crc32) {
207 + dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
212 + buf[priv->mtd->size - 1] = '\0';
213 + err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
215 + dev_err(dev, "Failed to add cells: %d\n", err);
223 +static int u_boot_env_probe(struct platform_device *pdev)
225 + struct nvmem_config config = {
226 + .name = "u-boot-env",
227 + .reg_read = u_boot_env_read,
229 + struct device *dev = &pdev->dev;
230 + struct device_node *np = dev->of_node;
231 + struct u_boot_env *priv;
234 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
239 + priv->format = (uintptr_t)of_device_get_match_data(dev);
241 + priv->mtd = of_get_mtd_device_by_node(np);
242 + if (IS_ERR(priv->mtd)) {
243 + dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
244 + return PTR_ERR(priv->mtd);
247 + err = u_boot_env_parse(priv);
252 + config.cells = priv->cells;
253 + config.ncells = priv->ncells;
254 + config.priv = priv;
255 + config.size = priv->mtd->size;
257 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
260 +static const struct of_device_id u_boot_env_of_match_table[] = {
261 + { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
262 + { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
263 + { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
267 +static struct platform_driver u_boot_env_driver = {
268 + .probe = u_boot_env_probe,
270 + .name = "u_boot_env",
271 + .of_match_table = u_boot_env_of_match_table,
274 +module_platform_driver(u_boot_env_driver);
276 +MODULE_AUTHOR("Rafał Miłecki");
277 +MODULE_LICENSE("GPL");
278 +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);