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,
19 U_BOOT_FORMAT_BROADCOM,
24 enum u_boot_env_format format;
29 struct nvmem_cell_info *cells;
33 struct u_boot_env_image_single {
38 struct u_boot_env_image_redundant {
44 struct u_boot_env_image_broadcom {
51 static int u_boot_env_read(void *context, unsigned int offset, void *val,
54 struct u_boot_env *priv = context;
55 struct device *dev = priv->dev;
59 err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
60 if (err && !mtd_is_bitflip(err)) {
61 dev_err(dev, "Failed to read from mtd: %d\n", err);
65 if (bytes_read != bytes) {
66 dev_err(dev, "Failed to read %zu bytes\n", bytes);
73 static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
74 size_t data_offset, size_t data_len)
76 struct device *dev = priv->dev;
77 char *data = buf + data_offset;
78 char *var, *value, *eq;
82 for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
85 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
89 for (var = data, idx = 0;
90 var < data + data_len && *var;
91 var = value + strlen(value) + 1, idx++) {
92 eq = strchr(var, '=');
98 priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
99 if (!priv->cells[idx].name)
101 priv->cells[idx].offset = data_offset + value - data;
102 priv->cells[idx].bytes = strlen(value);
103 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
106 if (WARN_ON(idx != priv->ncells))
112 static int u_boot_env_parse(struct u_boot_env *priv)
114 struct device *dev = priv->dev;
115 size_t crc32_data_offset;
116 size_t crc32_data_len;
126 buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
132 err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
133 if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
134 dev_err(dev, "Failed to read from mtd: %d\n", err);
138 switch (priv->format) {
139 case U_BOOT_FORMAT_SINGLE:
140 crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
141 crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
142 data_offset = offsetof(struct u_boot_env_image_single, data);
144 case U_BOOT_FORMAT_REDUNDANT:
145 crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
146 crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
147 data_offset = offsetof(struct u_boot_env_image_redundant, data);
149 case U_BOOT_FORMAT_BROADCOM:
150 crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
151 crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
152 data_offset = offsetof(struct u_boot_env_image_broadcom, data);
155 crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset));
156 crc32_data_len = priv->mtd->size - crc32_data_offset;
157 data_len = priv->mtd->size - data_offset;
159 calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
161 dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
166 buf[priv->mtd->size - 1] = '\0';
167 err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
169 dev_err(dev, "Failed to add cells: %d\n", err);
177 static int u_boot_env_probe(struct platform_device *pdev)
179 struct nvmem_config config = {
180 .name = "u-boot-env",
181 .reg_read = u_boot_env_read,
183 struct device *dev = &pdev->dev;
184 struct device_node *np = dev->of_node;
185 struct u_boot_env *priv;
188 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
193 priv->format = (uintptr_t)of_device_get_match_data(dev);
195 priv->mtd = of_get_mtd_device_by_node(np);
196 if (IS_ERR(priv->mtd)) {
197 dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
198 return PTR_ERR(priv->mtd);
201 err = u_boot_env_parse(priv);
206 config.cells = priv->cells;
207 config.ncells = priv->ncells;
209 config.size = priv->mtd->size;
211 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
214 static const struct of_device_id u_boot_env_of_match_table[] = {
215 { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
216 { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
217 { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
218 { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
222 static struct platform_driver u_boot_env_driver = {
223 .probe = u_boot_env_probe,
225 .name = "u_boot_env",
226 .of_match_table = u_boot_env_of_match_table,
229 module_platform_driver(u_boot_env_driver);
231 MODULE_AUTHOR("Rafał Miłecki");
232 MODULE_LICENSE("GPL");
233 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);