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/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>
16 enum u_boot_env_format {
18 U_BOOT_FORMAT_REDUNDANT,
23 enum u_boot_env_format format;
28 struct nvmem_cell_info *cells;
32 struct u_boot_env_image_single {
37 struct u_boot_env_image_redundant {
43 static int u_boot_env_read(void *context, unsigned int offset, void *val,
46 struct u_boot_env *priv = context;
47 struct device *dev = priv->dev;
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);
57 if (bytes_read != bytes) {
58 dev_err(dev, "Failed to read %zu bytes\n", bytes);
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)
68 struct device *dev = priv->dev;
69 char *data = buf + data_offset;
70 char *var, *value, *eq;
74 for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
77 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
81 for (var = data, idx = 0;
82 var < data + data_len && *var;
83 var = value + strlen(value) + 1, idx++) {
84 eq = strchr(var, '=');
90 priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
91 if (!priv->cells[idx].name)
93 priv->cells[idx].offset = data_offset + value - data;
94 priv->cells[idx].bytes = strlen(value);
95 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
98 if (WARN_ON(idx != priv->ncells))
104 static int u_boot_env_parse(struct u_boot_env *priv)
106 struct device *dev = priv->dev;
107 size_t crc32_data_offset;
108 size_t crc32_data_len;
118 buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
124 err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
125 if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
126 dev_err(dev, "Failed to read from mtd: %d\n", err);
130 switch (priv->format) {
131 case U_BOOT_FORMAT_SINGLE:
132 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
133 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
134 data_offset = offsetof(struct u_boot_env_image_single, data);
136 case U_BOOT_FORMAT_REDUNDANT:
137 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
138 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
139 data_offset = offsetof(struct u_boot_env_image_redundant, data);
142 crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset));
143 crc32_data_len = priv->mtd->size - crc32_data_offset;
144 data_len = priv->mtd->size - data_offset;
146 calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
148 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
153 buf[priv->mtd->size - 1] = '\0';
154 err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
156 dev_err(dev, "Failed to add cells: %d\n", err);
164 static int u_boot_env_probe(struct platform_device *pdev)
166 struct nvmem_config config = {
167 .name = "u-boot-env",
168 .reg_read = u_boot_env_read,
170 struct device *dev = &pdev->dev;
171 struct device_node *np = dev->of_node;
172 struct u_boot_env *priv;
175 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
180 priv->format = (uintptr_t)of_device_get_match_data(dev);
182 priv->mtd = of_get_mtd_device_by_node(np);
183 if (IS_ERR(priv->mtd)) {
184 dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
185 return PTR_ERR(priv->mtd);
188 err = u_boot_env_parse(priv);
193 config.cells = priv->cells;
194 config.ncells = priv->ncells;
196 config.size = priv->mtd->size;
198 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
201 static const struct of_device_id u_boot_env_of_match_table[] = {
202 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
203 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
204 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
208 static struct platform_driver u_boot_env_driver = {
209 .probe = u_boot_env_probe,
211 .name = "u_boot_env",
212 .of_match_table = u_boot_env_of_match_table,
215 module_platform_driver(u_boot_env_driver);
217 MODULE_AUTHOR("Rafał Miłecki");
218 MODULE_LICENSE("GPL");
219 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);