Skip to content

Commit d554292

Browse files
Rafał Miłeckigregkh
authored andcommitted
nvmem: add driver handling U-Boot environment variables
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 Current Linux's NVMEM sysfs API allows reading whole NVMEM data block. It can be used by user-space tools for reading U-Boot env vars block without the hassle of finding its location. Parsing will still need to be re-done there. Kernel-parsed NVMEM cells can be read however by Linux drivers. This may be useful for Ethernet drivers for reading device MAC address which is often stored as U-Boot env variable. Reviewed-by: Ahmad Fatoum <[email protected]> Signed-off-by: Rafał Miłecki <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 42992cf commit d554292

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20754,6 +20754,7 @@ U-BOOT ENVIRONMENT VARIABLES
2075420754
M: Rafał Miłecki <[email protected]>
2075520755
S: Maintained
2075620756
F: Documentation/devicetree/bindings/nvmem/u-boot,env.yaml
20757+
F: drivers/nvmem/u-boot-env.c
2075720758

2075820759
UACCE ACCELERATOR FRAMEWORK
2075920760
M: Zhangfei Gao <[email protected]>

drivers/nvmem/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,17 @@ config NVMEM_APPLE_EFUSES
344344
This driver can also be built as a module. If so, the module will
345345
be called nvmem-apple-efuses.
346346

347+
config NVMEM_U_BOOT_ENV
348+
tristate "U-Boot environment variables support"
349+
depends on OF && MTD
350+
select CRC32
351+
help
352+
U-Boot stores its setup as environment variables. This driver adds
353+
support for verifying & exporting such data. It also exposes variables
354+
as NVMEM cells so they can be referenced by other drivers.
355+
356+
Currently this drivers works only with env variables on top of MTD.
357+
358+
If compiled as module it will be called nvmem_u-boot-env.
359+
347360
endif

drivers/nvmem/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o
6969
nvmem-apple-efuses-y := apple-efuses.o
7070
obj-$(CONFIG_MICROCHIP_OTPC) += nvmem-microchip-otpc.o
7171
nvmem-microchip-otpc-y := microchip-otpc.o
72+
obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o
73+
nvmem_u-boot-env-y := u-boot-env.o

drivers/nvmem/u-boot-env.c

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2022 Rafał Miłecki <[email protected]>
4+
*/
5+
6+
#include <linux/crc32.h>
7+
#include <linux/mod_devicetable.h>
8+
#include <linux/module.h>
9+
#include <linux/mtd/mtd.h>
10+
#include <linux/nvmem-consumer.h>
11+
#include <linux/nvmem-provider.h>
12+
#include <linux/of_device.h>
13+
#include <linux/platform_device.h>
14+
#include <linux/slab.h>
15+
16+
enum u_boot_env_format {
17+
U_BOOT_FORMAT_SINGLE,
18+
U_BOOT_FORMAT_REDUNDANT,
19+
};
20+
21+
struct u_boot_env {
22+
struct device *dev;
23+
enum u_boot_env_format format;
24+
25+
struct mtd_info *mtd;
26+
27+
/* Cells */
28+
struct nvmem_cell_info *cells;
29+
int ncells;
30+
};
31+
32+
struct u_boot_env_image_single {
33+
__le32 crc32;
34+
uint8_t data[];
35+
} __packed;
36+
37+
struct u_boot_env_image_redundant {
38+
__le32 crc32;
39+
u8 mark;
40+
uint8_t data[];
41+
} __packed;
42+
43+
static int u_boot_env_read(void *context, unsigned int offset, void *val,
44+
size_t bytes)
45+
{
46+
struct u_boot_env *priv = context;
47+
struct device *dev = priv->dev;
48+
size_t bytes_read;
49+
int err;
50+
51+
err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
52+
if (err && !mtd_is_bitflip(err)) {
53+
dev_err(dev, "Failed to read from mtd: %d\n", err);
54+
return err;
55+
}
56+
57+
if (bytes_read != bytes) {
58+
dev_err(dev, "Failed to read %zu bytes\n", bytes);
59+
return -EIO;
60+
}
61+
62+
return 0;
63+
}
64+
65+
static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
66+
size_t data_offset, size_t data_len)
67+
{
68+
struct device *dev = priv->dev;
69+
char *data = buf + data_offset;
70+
char *var, *value, *eq;
71+
int idx;
72+
73+
priv->ncells = 0;
74+
for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
75+
priv->ncells++;
76+
77+
priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
78+
if (!priv->cells)
79+
return -ENOMEM;
80+
81+
for (var = data, idx = 0;
82+
var < data + data_len && *var;
83+
var = value + strlen(value) + 1, idx++) {
84+
eq = strchr(var, '=');
85+
if (!eq)
86+
break;
87+
*eq = '\0';
88+
value = eq + 1;
89+
90+
priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
91+
if (!priv->cells[idx].name)
92+
return -ENOMEM;
93+
priv->cells[idx].offset = data_offset + value - data;
94+
priv->cells[idx].bytes = strlen(value);
95+
}
96+
97+
if (WARN_ON(idx != priv->ncells))
98+
priv->ncells = idx;
99+
100+
return 0;
101+
}
102+
103+
static int u_boot_env_parse(struct u_boot_env *priv)
104+
{
105+
struct device *dev = priv->dev;
106+
size_t crc32_data_offset;
107+
size_t crc32_data_len;
108+
size_t crc32_offset;
109+
size_t data_offset;
110+
size_t data_len;
111+
uint32_t crc32;
112+
uint32_t calc;
113+
size_t bytes;
114+
uint8_t *buf;
115+
int err;
116+
117+
buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
118+
if (!buf) {
119+
err = -ENOMEM;
120+
goto err_out;
121+
}
122+
123+
err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
124+
if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
125+
dev_err(dev, "Failed to read from mtd: %d\n", err);
126+
goto err_kfree;
127+
}
128+
129+
switch (priv->format) {
130+
case U_BOOT_FORMAT_SINGLE:
131+
crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
132+
crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
133+
data_offset = offsetof(struct u_boot_env_image_single, data);
134+
break;
135+
case U_BOOT_FORMAT_REDUNDANT:
136+
crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
137+
crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
138+
data_offset = offsetof(struct u_boot_env_image_redundant, data);
139+
break;
140+
}
141+
crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset));
142+
crc32_data_len = priv->mtd->size - crc32_data_offset;
143+
data_len = priv->mtd->size - data_offset;
144+
145+
calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
146+
if (calc != crc32) {
147+
dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
148+
err = -EINVAL;
149+
goto err_kfree;
150+
}
151+
152+
buf[priv->mtd->size - 1] = '\0';
153+
err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
154+
if (err)
155+
dev_err(dev, "Failed to add cells: %d\n", err);
156+
157+
err_kfree:
158+
kfree(buf);
159+
err_out:
160+
return err;
161+
}
162+
163+
static int u_boot_env_probe(struct platform_device *pdev)
164+
{
165+
struct nvmem_config config = {
166+
.name = "u-boot-env",
167+
.reg_read = u_boot_env_read,
168+
};
169+
struct device *dev = &pdev->dev;
170+
struct device_node *np = dev->of_node;
171+
struct u_boot_env *priv;
172+
int err;
173+
174+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
175+
if (!priv)
176+
return -ENOMEM;
177+
priv->dev = dev;
178+
179+
priv->format = (uintptr_t)of_device_get_match_data(dev);
180+
181+
priv->mtd = of_get_mtd_device_by_node(np);
182+
if (IS_ERR(priv->mtd)) {
183+
dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
184+
return PTR_ERR(priv->mtd);
185+
}
186+
187+
err = u_boot_env_parse(priv);
188+
if (err)
189+
return err;
190+
191+
config.dev = dev;
192+
config.cells = priv->cells;
193+
config.ncells = priv->ncells;
194+
config.priv = priv;
195+
config.size = priv->mtd->size;
196+
197+
return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
198+
}
199+
200+
static const struct of_device_id u_boot_env_of_match_table[] = {
201+
{ .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
202+
{ .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
203+
{ .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
204+
{},
205+
};
206+
207+
static struct platform_driver u_boot_env_driver = {
208+
.probe = u_boot_env_probe,
209+
.driver = {
210+
.name = "u_boot_env",
211+
.of_match_table = u_boot_env_of_match_table,
212+
},
213+
};
214+
module_platform_driver(u_boot_env_driver);
215+
216+
MODULE_AUTHOR("Rafał Miłecki");
217+
MODULE_LICENSE("GPL");
218+
MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);

0 commit comments

Comments
 (0)