1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
6 #include <linux/crc32.h>
7 #include <linux/etherdevice.h>
8 #include <linux/if_ether.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/nvmem-consumer.h>
13 #include <linux/nvmem-provider.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 enum u_boot_env_format {
20 U_BOOT_FORMAT_REDUNDANT,
21 U_BOOT_FORMAT_BROADCOM,
26 enum u_boot_env_format format;
31 struct nvmem_cell_info *cells;
35 struct u_boot_env_image_single {
40 struct u_boot_env_image_redundant {
46 struct u_boot_env_image_broadcom {
50 DECLARE_FLEX_ARRAY(uint8_t, data);
53 static int u_boot_env_read(void *context, unsigned int offset, void *val,
56 struct u_boot_env *priv = context;
57 struct device *dev = priv->dev;
61 err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
62 if (err && !mtd_is_bitflip(err)) {
63 dev_err(dev, "Failed to read from mtd: %d\n", err);
67 if (bytes_read != bytes) {
68 dev_err(dev, "Failed to read %zu bytes\n", bytes);
75 static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
76 unsigned int offset, void *buf, size_t bytes)
80 if (bytes != 3 * ETH_ALEN - 1)
83 if (!mac_pton(buf, mac))
87 eth_addr_add(mac, index);
89 ether_addr_copy(buf, mac);
94 static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
95 size_t data_offset, size_t data_len)
97 struct device *dev = priv->dev;
98 char *data = buf + data_offset;
99 char *var, *value, *eq;
103 for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
106 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
110 for (var = data, idx = 0;
111 var < data + data_len && *var;
112 var = value + strlen(value) + 1, idx++) {
113 eq = strchr(var, '=');
119 priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
120 if (!priv->cells[idx].name)
122 priv->cells[idx].offset = data_offset + value - data;
123 priv->cells[idx].bytes = strlen(value);
124 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
125 if (!strcmp(var, "ethaddr")) {
126 priv->cells[idx].raw_len = strlen(value);
127 priv->cells[idx].bytes = ETH_ALEN;
128 priv->cells[idx].read_post_process = u_boot_env_read_post_process_ethaddr;
132 if (WARN_ON(idx != priv->ncells))
138 static int u_boot_env_parse(struct u_boot_env *priv)
140 struct device *dev = priv->dev;
141 size_t crc32_data_offset;
142 size_t crc32_data_len;
152 buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
158 err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
159 if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
160 dev_err(dev, "Failed to read from mtd: %d\n", err);
164 switch (priv->format) {
165 case U_BOOT_FORMAT_SINGLE:
166 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
167 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
168 data_offset = offsetof(struct u_boot_env_image_single, data);
170 case U_BOOT_FORMAT_REDUNDANT:
171 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
172 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
173 data_offset = offsetof(struct u_boot_env_image_redundant, data);
175 case U_BOOT_FORMAT_BROADCOM:
176 crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
177 crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
178 data_offset = offsetof(struct u_boot_env_image_broadcom, data);
181 crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset));
182 crc32_data_len = priv->mtd->size - crc32_data_offset;
183 data_len = priv->mtd->size - data_offset;
185 calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
187 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
192 buf[priv->mtd->size - 1] = '\0';
193 err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
195 dev_err(dev, "Failed to add cells: %d\n", err);
203 static int u_boot_env_probe(struct platform_device *pdev)
205 struct nvmem_config config = {
206 .name = "u-boot-env",
207 .reg_read = u_boot_env_read,
209 struct device *dev = &pdev->dev;
210 struct device_node *np = dev->of_node;
211 struct u_boot_env *priv;
214 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
219 priv->format = (uintptr_t)of_device_get_match_data(dev);
221 priv->mtd = of_get_mtd_device_by_node(np);
222 if (IS_ERR(priv->mtd)) {
223 dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
224 return PTR_ERR(priv->mtd);
227 err = u_boot_env_parse(priv);
232 config.cells = priv->cells;
233 config.ncells = priv->ncells;
235 config.size = priv->mtd->size;
237 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
240 static const struct of_device_id u_boot_env_of_match_table[] = {
241 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
242 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
243 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
244 { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
248 static struct platform_driver u_boot_env_driver = {
249 .probe = u_boot_env_probe,
251 .name = "u_boot_env",
252 .of_match_table = u_boot_env_of_match_table,
255 module_platform_driver(u_boot_env_driver);
257 MODULE_AUTHOR("Rafał Miłecki");
258 MODULE_LICENSE("GPL");
259 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);