xref: /openbmc/linux/drivers/nvmem/u-boot-env.c (revision d8f5ce3c)
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/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>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 
18 enum u_boot_env_format {
19 	U_BOOT_FORMAT_SINGLE,
20 	U_BOOT_FORMAT_REDUNDANT,
21 	U_BOOT_FORMAT_BROADCOM,
22 };
23 
24 struct u_boot_env {
25 	struct device *dev;
26 	enum u_boot_env_format format;
27 
28 	struct mtd_info *mtd;
29 
30 	/* Cells */
31 	struct nvmem_cell_info *cells;
32 	int ncells;
33 };
34 
35 struct u_boot_env_image_single {
36 	__le32 crc32;
37 	uint8_t data[];
38 } __packed;
39 
40 struct u_boot_env_image_redundant {
41 	__le32 crc32;
42 	u8 mark;
43 	uint8_t data[];
44 } __packed;
45 
46 struct u_boot_env_image_broadcom {
47 	__le32 magic;
48 	__le32 len;
49 	__le32 crc32;
50 	DECLARE_FLEX_ARRAY(uint8_t, data);
51 } __packed;
52 
53 static int u_boot_env_read(void *context, unsigned int offset, void *val,
54 			   size_t bytes)
55 {
56 	struct u_boot_env *priv = context;
57 	struct device *dev = priv->dev;
58 	size_t bytes_read;
59 	int err;
60 
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);
64 		return err;
65 	}
66 
67 	if (bytes_read != bytes) {
68 		dev_err(dev, "Failed to read %zu bytes\n", bytes);
69 		return -EIO;
70 	}
71 
72 	return 0;
73 }
74 
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)
77 {
78 	u8 mac[ETH_ALEN];
79 
80 	if (bytes != 3 * ETH_ALEN - 1)
81 		return -EINVAL;
82 
83 	if (!mac_pton(buf, mac))
84 		return -EINVAL;
85 
86 	if (index)
87 		eth_addr_add(mac, index);
88 
89 	ether_addr_copy(buf, mac);
90 
91 	return 0;
92 }
93 
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)
96 {
97 	struct device *dev = priv->dev;
98 	char *data = buf + data_offset;
99 	char *var, *value, *eq;
100 	int idx;
101 
102 	priv->ncells = 0;
103 	for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
104 		priv->ncells++;
105 
106 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
107 	if (!priv->cells)
108 		return -ENOMEM;
109 
110 	for (var = data, idx = 0;
111 	     var < data + data_len && *var;
112 	     var = value + strlen(value) + 1, idx++) {
113 		eq = strchr(var, '=');
114 		if (!eq)
115 			break;
116 		*eq = '\0';
117 		value = eq + 1;
118 
119 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
120 		if (!priv->cells[idx].name)
121 			return -ENOMEM;
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;
129 		}
130 	}
131 
132 	if (WARN_ON(idx != priv->ncells))
133 		priv->ncells = idx;
134 
135 	return 0;
136 }
137 
138 static int u_boot_env_parse(struct u_boot_env *priv)
139 {
140 	struct device *dev = priv->dev;
141 	size_t crc32_data_offset;
142 	size_t crc32_data_len;
143 	size_t crc32_offset;
144 	size_t data_offset;
145 	size_t data_len;
146 	uint32_t crc32;
147 	uint32_t calc;
148 	size_t bytes;
149 	uint8_t *buf;
150 	int err;
151 
152 	buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
153 	if (!buf) {
154 		err = -ENOMEM;
155 		goto err_out;
156 	}
157 
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);
161 		goto err_kfree;
162 	}
163 
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);
169 		break;
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);
174 		break;
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);
179 		break;
180 	}
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;
184 
185 	calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
186 	if (calc != crc32) {
187 		dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
188 		err = -EINVAL;
189 		goto err_kfree;
190 	}
191 
192 	buf[priv->mtd->size - 1] = '\0';
193 	err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
194 	if (err)
195 		dev_err(dev, "Failed to add cells: %d\n", err);
196 
197 err_kfree:
198 	kfree(buf);
199 err_out:
200 	return err;
201 }
202 
203 static int u_boot_env_probe(struct platform_device *pdev)
204 {
205 	struct nvmem_config config = {
206 		.name = "u-boot-env",
207 		.reg_read = u_boot_env_read,
208 	};
209 	struct device *dev = &pdev->dev;
210 	struct device_node *np = dev->of_node;
211 	struct u_boot_env *priv;
212 	int err;
213 
214 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
215 	if (!priv)
216 		return -ENOMEM;
217 	priv->dev = dev;
218 
219 	priv->format = (uintptr_t)of_device_get_match_data(dev);
220 
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);
225 	}
226 
227 	err = u_boot_env_parse(priv);
228 	if (err)
229 		return err;
230 
231 	config.dev = dev;
232 	config.cells = priv->cells;
233 	config.ncells = priv->ncells;
234 	config.priv = priv;
235 	config.size = priv->mtd->size;
236 
237 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
238 }
239 
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, },
245 	{},
246 };
247 
248 static struct platform_driver u_boot_env_driver = {
249 	.probe = u_boot_env_probe,
250 	.driver = {
251 		.name = "u_boot_env",
252 		.of_match_table = u_boot_env_of_match_table,
253 	},
254 };
255 module_platform_driver(u_boot_env_driver);
256 
257 MODULE_AUTHOR("Rafał Miłecki");
258 MODULE_LICENSE("GPL");
259 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
260