xref: /openbmc/linux/drivers/nvmem/u-boot-env.c (revision e8254a8e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
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>
15 
16 enum u_boot_env_format {
17 	U_BOOT_FORMAT_SINGLE,
18 	U_BOOT_FORMAT_REDUNDANT,
19 	U_BOOT_FORMAT_BROADCOM,
20 };
21 
22 struct u_boot_env {
23 	struct device *dev;
24 	enum u_boot_env_format format;
25 
26 	struct mtd_info *mtd;
27 
28 	/* Cells */
29 	struct nvmem_cell_info *cells;
30 	int ncells;
31 };
32 
33 struct u_boot_env_image_single {
34 	__le32 crc32;
35 	uint8_t data[];
36 } __packed;
37 
38 struct u_boot_env_image_redundant {
39 	__le32 crc32;
40 	u8 mark;
41 	uint8_t data[];
42 } __packed;
43 
44 struct u_boot_env_image_broadcom {
45 	__le32 magic;
46 	__le32 len;
47 	__le32 crc32;
48 	uint8_t data[0];
49 } __packed;
50 
51 static int u_boot_env_read(void *context, unsigned int offset, void *val,
52 			   size_t bytes)
53 {
54 	struct u_boot_env *priv = context;
55 	struct device *dev = priv->dev;
56 	size_t bytes_read;
57 	int err;
58 
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);
62 		return err;
63 	}
64 
65 	if (bytes_read != bytes) {
66 		dev_err(dev, "Failed to read %zu bytes\n", bytes);
67 		return -EIO;
68 	}
69 
70 	return 0;
71 }
72 
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)
75 {
76 	struct device *dev = priv->dev;
77 	char *data = buf + data_offset;
78 	char *var, *value, *eq;
79 	int idx;
80 
81 	priv->ncells = 0;
82 	for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
83 		priv->ncells++;
84 
85 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
86 	if (!priv->cells)
87 		return -ENOMEM;
88 
89 	for (var = data, idx = 0;
90 	     var < data + data_len && *var;
91 	     var = value + strlen(value) + 1, idx++) {
92 		eq = strchr(var, '=');
93 		if (!eq)
94 			break;
95 		*eq = '\0';
96 		value = eq + 1;
97 
98 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
99 		if (!priv->cells[idx].name)
100 			return -ENOMEM;
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);
104 	}
105 
106 	if (WARN_ON(idx != priv->ncells))
107 		priv->ncells = idx;
108 
109 	return 0;
110 }
111 
112 static int u_boot_env_parse(struct u_boot_env *priv)
113 {
114 	struct device *dev = priv->dev;
115 	size_t crc32_data_offset;
116 	size_t crc32_data_len;
117 	size_t crc32_offset;
118 	size_t data_offset;
119 	size_t data_len;
120 	uint32_t crc32;
121 	uint32_t calc;
122 	size_t bytes;
123 	uint8_t *buf;
124 	int err;
125 
126 	buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
127 	if (!buf) {
128 		err = -ENOMEM;
129 		goto err_out;
130 	}
131 
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);
135 		goto err_kfree;
136 	}
137 
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);
143 		break;
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);
148 		break;
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);
153 		break;
154 	}
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;
158 
159 	calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
160 	if (calc != crc32) {
161 		dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
162 		err = -EINVAL;
163 		goto err_kfree;
164 	}
165 
166 	buf[priv->mtd->size - 1] = '\0';
167 	err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
168 	if (err)
169 		dev_err(dev, "Failed to add cells: %d\n", err);
170 
171 err_kfree:
172 	kfree(buf);
173 err_out:
174 	return err;
175 }
176 
177 static int u_boot_env_probe(struct platform_device *pdev)
178 {
179 	struct nvmem_config config = {
180 		.name = "u-boot-env",
181 		.reg_read = u_boot_env_read,
182 	};
183 	struct device *dev = &pdev->dev;
184 	struct device_node *np = dev->of_node;
185 	struct u_boot_env *priv;
186 	int err;
187 
188 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
189 	if (!priv)
190 		return -ENOMEM;
191 	priv->dev = dev;
192 
193 	priv->format = (uintptr_t)of_device_get_match_data(dev);
194 
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);
199 	}
200 
201 	err = u_boot_env_parse(priv);
202 	if (err)
203 		return err;
204 
205 	config.dev = dev;
206 	config.cells = priv->cells;
207 	config.ncells = priv->ncells;
208 	config.priv = priv;
209 	config.size = priv->mtd->size;
210 
211 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
212 }
213 
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, },
219 	{},
220 };
221 
222 static struct platform_driver u_boot_env_driver = {
223 	.probe = u_boot_env_probe,
224 	.driver = {
225 		.name = "u_boot_env",
226 		.of_match_table = u_boot_env_of_match_table,
227 	},
228 };
229 module_platform_driver(u_boot_env_driver);
230 
231 MODULE_AUTHOR("Rafał Miłecki");
232 MODULE_LICENSE("GPL");
233 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
234