xref: /openbmc/linux/drivers/nvmem/u-boot-env.c (revision 3c39f253)
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 	struct nvmem_device *nvmem;
27 	enum u_boot_env_format format;
28 
29 	struct mtd_info *mtd;
30 };
31 
32 struct u_boot_env_image_single {
33 	__le32 crc32;
34 	uint8_t data[];
35 } __packed;
36 
37 struct u_boot_env_image_redundant {
38 	__le32 crc32;
39 	u8 mark;
40 	uint8_t data[];
41 } __packed;
42 
43 struct u_boot_env_image_broadcom {
44 	__le32 magic;
45 	__le32 len;
46 	__le32 crc32;
47 	DECLARE_FLEX_ARRAY(uint8_t, data);
48 } __packed;
49 
50 static int u_boot_env_read(void *context, unsigned int offset, void *val,
51 			   size_t bytes)
52 {
53 	struct u_boot_env *priv = context;
54 	struct device *dev = priv->dev;
55 	size_t bytes_read;
56 	int err;
57 
58 	err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
59 	if (err && !mtd_is_bitflip(err)) {
60 		dev_err(dev, "Failed to read from mtd: %d\n", err);
61 		return err;
62 	}
63 
64 	if (bytes_read != bytes) {
65 		dev_err(dev, "Failed to read %zu bytes\n", bytes);
66 		return -EIO;
67 	}
68 
69 	return 0;
70 }
71 
72 static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index,
73 						unsigned int offset, void *buf, size_t bytes)
74 {
75 	u8 mac[ETH_ALEN];
76 
77 	if (bytes != 3 * ETH_ALEN - 1)
78 		return -EINVAL;
79 
80 	if (!mac_pton(buf, mac))
81 		return -EINVAL;
82 
83 	if (index)
84 		eth_addr_add(mac, index);
85 
86 	ether_addr_copy(buf, mac);
87 
88 	return 0;
89 }
90 
91 static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
92 				size_t data_offset, size_t data_len)
93 {
94 	struct nvmem_device *nvmem = priv->nvmem;
95 	struct device *dev = priv->dev;
96 	char *data = buf + data_offset;
97 	char *var, *value, *eq;
98 
99 	for (var = data;
100 	     var < data + data_len && *var;
101 	     var = value + strlen(value) + 1) {
102 		struct nvmem_cell_info info = {};
103 
104 		eq = strchr(var, '=');
105 		if (!eq)
106 			break;
107 		*eq = '\0';
108 		value = eq + 1;
109 
110 		info.name = devm_kstrdup(dev, var, GFP_KERNEL);
111 		if (!info.name)
112 			return -ENOMEM;
113 		info.offset = data_offset + value - data;
114 		info.bytes = strlen(value);
115 		info.np = of_get_child_by_name(dev->of_node, info.name);
116 		if (!strcmp(var, "ethaddr")) {
117 			info.raw_len = strlen(value);
118 			info.bytes = ETH_ALEN;
119 			info.read_post_process = u_boot_env_read_post_process_ethaddr;
120 		}
121 
122 		nvmem_add_one_cell(nvmem, &info);
123 	}
124 
125 	return 0;
126 }
127 
128 static int u_boot_env_parse(struct u_boot_env *priv)
129 {
130 	struct nvmem_device *nvmem = priv->nvmem;
131 	struct device *dev = priv->dev;
132 	size_t crc32_data_offset;
133 	size_t crc32_data_len;
134 	size_t crc32_offset;
135 	__le32 *crc32_addr;
136 	size_t data_offset;
137 	size_t data_len;
138 	size_t dev_size;
139 	uint32_t crc32;
140 	uint32_t calc;
141 	uint8_t *buf;
142 	int bytes;
143 	int err;
144 
145 	dev_size = nvmem_dev_size(nvmem);
146 
147 	buf = kzalloc(dev_size, GFP_KERNEL);
148 	if (!buf) {
149 		err = -ENOMEM;
150 		goto err_out;
151 	}
152 
153 	bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
154 	if (bytes < 0) {
155 		err = bytes;
156 		goto err_kfree;
157 	} else if (bytes != dev_size) {
158 		err = -EIO;
159 		goto err_kfree;
160 	}
161 
162 	switch (priv->format) {
163 	case U_BOOT_FORMAT_SINGLE:
164 		crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
165 		crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
166 		data_offset = offsetof(struct u_boot_env_image_single, data);
167 		break;
168 	case U_BOOT_FORMAT_REDUNDANT:
169 		crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
170 		crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data);
171 		data_offset = offsetof(struct u_boot_env_image_redundant, data);
172 		break;
173 	case U_BOOT_FORMAT_BROADCOM:
174 		crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32);
175 		crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data);
176 		data_offset = offsetof(struct u_boot_env_image_broadcom, data);
177 		break;
178 	}
179 
180 	if (dev_size < data_offset) {
181 		dev_err(dev, "Device too small for u-boot-env\n");
182 		err = -EIO;
183 		goto err_kfree;
184 	}
185 
186 	crc32_addr = (__le32 *)(buf + crc32_offset);
187 	crc32 = le32_to_cpu(*crc32_addr);
188 	crc32_data_len = dev_size - crc32_data_offset;
189 	data_len = dev_size - data_offset;
190 
191 	calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
192 	if (calc != crc32) {
193 		dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
194 		err = -EINVAL;
195 		goto err_kfree;
196 	}
197 
198 	buf[dev_size - 1] = '\0';
199 	err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
200 
201 err_kfree:
202 	kfree(buf);
203 err_out:
204 	return err;
205 }
206 
207 static int u_boot_env_probe(struct platform_device *pdev)
208 {
209 	struct nvmem_config config = {
210 		.name = "u-boot-env",
211 		.reg_read = u_boot_env_read,
212 	};
213 	struct device *dev = &pdev->dev;
214 	struct device_node *np = dev->of_node;
215 	struct u_boot_env *priv;
216 
217 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
218 	if (!priv)
219 		return -ENOMEM;
220 	priv->dev = dev;
221 
222 	priv->format = (uintptr_t)of_device_get_match_data(dev);
223 
224 	priv->mtd = of_get_mtd_device_by_node(np);
225 	if (IS_ERR(priv->mtd)) {
226 		dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
227 		return PTR_ERR(priv->mtd);
228 	}
229 
230 	config.dev = dev;
231 	config.priv = priv;
232 	config.size = priv->mtd->size;
233 
234 	priv->nvmem = devm_nvmem_register(dev, &config);
235 	if (IS_ERR(priv->nvmem))
236 		return PTR_ERR(priv->nvmem);
237 
238 	return u_boot_env_parse(priv);
239 }
240 
241 static const struct of_device_id u_boot_env_of_match_table[] = {
242 	{ .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
243 	{ .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
244 	{ .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
245 	{ .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
246 	{},
247 };
248 
249 static struct platform_driver u_boot_env_driver = {
250 	.probe = u_boot_env_probe,
251 	.driver = {
252 		.name = "u_boot_env",
253 		.of_match_table = u_boot_env_of_match_table,
254 	},
255 };
256 module_platform_driver(u_boot_env_driver);
257 
258 MODULE_AUTHOR("Rafał Miłecki");
259 MODULE_LICENSE("GPL");
260 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
261