9138807bc91cd9a176421b92cc1dd38d2f95d469
[openwrt/openwrt.git] /
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
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 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>
30 ---
31 MAINTAINERS | 1 +
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
37
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.
43
44 +config NVMEM_U_BOOT_ENV
45 + tristate "U-Boot environment variables support"
46 + depends on OF && MTD
47 + select CRC32
48 + help
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.
52 +
53 + Currently this drivers works only with env variables on top of MTD.
54 +
55 + If compiled as module it will be called nvmem_u-boot-env.
56 +
57 endif
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
66 --- /dev/null
67 +++ b/drivers/nvmem/u-boot-env.c
68 @@ -0,0 +1,218 @@
69 +// SPDX-License-Identifier: GPL-2.0-only
70 +/*
71 + * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
72 + */
73 +
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>
83 +
84 +enum u_boot_env_format {
85 + U_BOOT_FORMAT_SINGLE,
86 + U_BOOT_FORMAT_REDUNDANT,
87 +};
88 +
89 +struct u_boot_env {
90 + struct device *dev;
91 + enum u_boot_env_format format;
92 +
93 + struct mtd_info *mtd;
94 +
95 + /* Cells */
96 + struct nvmem_cell_info *cells;
97 + int ncells;
98 +};
99 +
100 +struct u_boot_env_image_single {
101 + __le32 crc32;
102 + uint8_t data[];
103 +} __packed;
104 +
105 +struct u_boot_env_image_redundant {
106 + __le32 crc32;
107 + u8 mark;
108 + uint8_t data[];
109 +} __packed;
110 +
111 +static int u_boot_env_read(void *context, unsigned int offset, void *val,
112 + size_t bytes)
113 +{
114 + struct u_boot_env *priv = context;
115 + struct device *dev = priv->dev;
116 + size_t bytes_read;
117 + int err;
118 +
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);
122 + return err;
123 + }
124 +
125 + if (bytes_read != bytes) {
126 + dev_err(dev, "Failed to read %zu bytes\n", bytes);
127 + return -EIO;
128 + }
129 +
130 + return 0;
131 +}
132 +
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)
135 +{
136 + struct device *dev = priv->dev;
137 + char *data = buf + data_offset;
138 + char *var, *value, *eq;
139 + int idx;
140 +
141 + priv->ncells = 0;
142 + for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
143 + priv->ncells++;
144 +
145 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
146 + if (!priv->cells)
147 + return -ENOMEM;
148 +
149 + for (var = data, idx = 0;
150 + var < data + data_len && *var;
151 + var = value + strlen(value) + 1, idx++) {
152 + eq = strchr(var, '=');
153 + if (!eq)
154 + break;
155 + *eq = '\0';
156 + value = eq + 1;
157 +
158 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
159 + if (!priv->cells[idx].name)
160 + return -ENOMEM;
161 + priv->cells[idx].offset = data_offset + value - data;
162 + priv->cells[idx].bytes = strlen(value);
163 + }
164 +
165 + if (WARN_ON(idx != priv->ncells))
166 + priv->ncells = idx;
167 +
168 + return 0;
169 +}
170 +
171 +static int u_boot_env_parse(struct u_boot_env *priv)
172 +{
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;
178 + size_t data_len;
179 + uint32_t crc32;
180 + uint32_t calc;
181 + size_t bytes;
182 + uint8_t *buf;
183 + int err;
184 +
185 + buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
186 + if (!buf) {
187 + err = -ENOMEM;
188 + goto err_out;
189 + }
190 +
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);
194 + goto err_kfree;
195 + }
196 +
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);
202 + break;
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);
207 + break;
208 + }
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;
212 +
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);
216 + err = -EINVAL;
217 + goto err_kfree;
218 + }
219 +
220 + buf[priv->mtd->size - 1] = '\0';
221 + err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
222 + if (err)
223 + dev_err(dev, "Failed to add cells: %d\n", err);
224 +
225 +err_kfree:
226 + kfree(buf);
227 +err_out:
228 + return err;
229 +}
230 +
231 +static int u_boot_env_probe(struct platform_device *pdev)
232 +{
233 + struct nvmem_config config = {
234 + .name = "u-boot-env",
235 + .reg_read = u_boot_env_read,
236 + };
237 + struct device *dev = &pdev->dev;
238 + struct device_node *np = dev->of_node;
239 + struct u_boot_env *priv;
240 + int err;
241 +
242 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
243 + if (!priv)
244 + return -ENOMEM;
245 + priv->dev = dev;
246 +
247 + priv->format = (uintptr_t)of_device_get_match_data(dev);
248 +
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);
253 + }
254 +
255 + err = u_boot_env_parse(priv);
256 + if (err)
257 + return err;
258 +
259 + config.dev = dev;
260 + config.cells = priv->cells;
261 + config.ncells = priv->ncells;
262 + config.priv = priv;
263 + config.size = priv->mtd->size;
264 +
265 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
266 +}
267 +
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, },
272 + {},
273 +};
274 +
275 +static struct platform_driver u_boot_env_driver = {
276 + .probe = u_boot_env_probe,
277 + .driver = {
278 + .name = "u_boot_env",
279 + .of_match_table = u_boot_env_of_match_table,
280 + },
281 +};
282 +module_platform_driver(u_boot_env_driver);
283 +
284 +MODULE_AUTHOR("Rafał Miłecki");
285 +MODULE_LICENSE("GPL");
286 +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);