1459d1184eb7d2a9b026dcf7182ae6dbb13fc5ef
[openwrt/staging/svanheule.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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.
11
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
15
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
19 be re-done there.
20
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.
24
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>
28 ---
29
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.
35
36 +config NVMEM_U_BOOT_ENV
37 + tristate "U-Boot environment variables support"
38 + depends on OF && MTD
39 + select CRC32
40 + help
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.
44 +
45 + Currently this drivers works only with env variables on top of MTD.
46 +
47 + If compiled as module it will be called nvmem_u-boot-env.
48 +
49 endif
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
58 --- /dev/null
59 +++ b/drivers/nvmem/u-boot-env.c
60 @@ -0,0 +1,218 @@
61 +// SPDX-License-Identifier: GPL-2.0-only
62 +/*
63 + * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
64 + */
65 +
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>
75 +
76 +enum u_boot_env_format {
77 + U_BOOT_FORMAT_SINGLE,
78 + U_BOOT_FORMAT_REDUNDANT,
79 +};
80 +
81 +struct u_boot_env {
82 + struct device *dev;
83 + enum u_boot_env_format format;
84 +
85 + struct mtd_info *mtd;
86 +
87 + /* Cells */
88 + struct nvmem_cell_info *cells;
89 + int ncells;
90 +};
91 +
92 +struct u_boot_env_image_single {
93 + __le32 crc32;
94 + uint8_t data[];
95 +} __packed;
96 +
97 +struct u_boot_env_image_redundant {
98 + __le32 crc32;
99 + u8 mark;
100 + uint8_t data[];
101 +} __packed;
102 +
103 +static int u_boot_env_read(void *context, unsigned int offset, void *val,
104 + size_t bytes)
105 +{
106 + struct u_boot_env *priv = context;
107 + struct device *dev = priv->dev;
108 + size_t bytes_read;
109 + int err;
110 +
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);
114 + return err;
115 + }
116 +
117 + if (bytes_read != bytes) {
118 + dev_err(dev, "Failed to read %zu bytes\n", bytes);
119 + return -EIO;
120 + }
121 +
122 + return 0;
123 +}
124 +
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)
127 +{
128 + struct device *dev = priv->dev;
129 + char *data = buf + data_offset;
130 + char *var, *value, *eq;
131 + int idx;
132 +
133 + priv->ncells = 0;
134 + for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
135 + priv->ncells++;
136 +
137 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
138 + if (!priv->cells)
139 + return -ENOMEM;
140 +
141 + for (var = data, idx = 0;
142 + var < data + data_len && *var;
143 + var = value + strlen(value) + 1, idx++) {
144 + eq = strchr(var, '=');
145 + if (!eq)
146 + break;
147 + *eq = '\0';
148 + value = eq + 1;
149 +
150 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
151 + if (!priv->cells[idx].name)
152 + return -ENOMEM;
153 + priv->cells[idx].offset = data_offset + value - data;
154 + priv->cells[idx].bytes = strlen(value);
155 + }
156 +
157 + if (WARN_ON(idx != priv->ncells))
158 + priv->ncells = idx;
159 +
160 + return 0;
161 +}
162 +
163 +static int u_boot_env_parse(struct u_boot_env *priv)
164 +{
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;
170 + size_t data_len;
171 + uint32_t crc32;
172 + uint32_t calc;
173 + size_t bytes;
174 + uint8_t *buf;
175 + int err;
176 +
177 + buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
178 + if (!buf) {
179 + err = -ENOMEM;
180 + goto err_out;
181 + }
182 +
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);
186 + goto err_kfree;
187 + }
188 +
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);
194 + break;
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);
199 + break;
200 + }
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;
204 +
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);
208 + err = -EINVAL;
209 + goto err_kfree;
210 + }
211 +
212 + buf[priv->mtd->size - 1] = '\0';
213 + err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
214 + if (err)
215 + dev_err(dev, "Failed to add cells: %d\n", err);
216 +
217 +err_kfree:
218 + kfree(buf);
219 +err_out:
220 + return err;
221 +}
222 +
223 +static int u_boot_env_probe(struct platform_device *pdev)
224 +{
225 + struct nvmem_config config = {
226 + .name = "u-boot-env",
227 + .reg_read = u_boot_env_read,
228 + };
229 + struct device *dev = &pdev->dev;
230 + struct device_node *np = dev->of_node;
231 + struct u_boot_env *priv;
232 + int err;
233 +
234 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
235 + if (!priv)
236 + return -ENOMEM;
237 + priv->dev = dev;
238 +
239 + priv->format = (uintptr_t)of_device_get_match_data(dev);
240 +
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);
245 + }
246 +
247 + err = u_boot_env_parse(priv);
248 + if (err)
249 + return err;
250 +
251 + config.dev = dev;
252 + config.cells = priv->cells;
253 + config.ncells = priv->ncells;
254 + config.priv = priv;
255 + config.size = priv->mtd->size;
256 +
257 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
258 +}
259 +
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, },
264 + {},
265 +};
266 +
267 +static struct platform_driver u_boot_env_driver = {
268 + .probe = u_boot_env_probe,
269 + .driver = {
270 + .name = "u_boot_env",
271 + .of_match_table = u_boot_env_of_match_table,
272 + },
273 +};
274 +module_platform_driver(u_boot_env_driver);
275 +
276 +MODULE_AUTHOR("Rafał Miłecki");
277 +MODULE_LICENSE("GPL");
278 +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);