xref: /linux/drivers/nvmem/u-boot-env.c (revision e6a901a00822659181c93c86d8bbc2a17779fddc)
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 	crc32_addr = (__le32 *)(buf + crc32_offset);
180 	crc32 = le32_to_cpu(*crc32_addr);
181 	crc32_data_len = dev_size - crc32_data_offset;
182 	data_len = dev_size - data_offset;
183 
184 	calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
185 	if (calc != crc32) {
186 		dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
187 		err = -EINVAL;
188 		goto err_kfree;
189 	}
190 
191 	buf[dev_size - 1] = '\0';
192 	err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
193 
194 err_kfree:
195 	kfree(buf);
196 err_out:
197 	return err;
198 }
199 
200 static int u_boot_env_probe(struct platform_device *pdev)
201 {
202 	struct nvmem_config config = {
203 		.name = "u-boot-env",
204 		.reg_read = u_boot_env_read,
205 	};
206 	struct device *dev = &pdev->dev;
207 	struct device_node *np = dev->of_node;
208 	struct u_boot_env *priv;
209 
210 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
211 	if (!priv)
212 		return -ENOMEM;
213 	priv->dev = dev;
214 
215 	priv->format = (uintptr_t)of_device_get_match_data(dev);
216 
217 	priv->mtd = of_get_mtd_device_by_node(np);
218 	if (IS_ERR(priv->mtd)) {
219 		dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
220 		return PTR_ERR(priv->mtd);
221 	}
222 
223 	config.dev = dev;
224 	config.priv = priv;
225 	config.size = priv->mtd->size;
226 
227 	priv->nvmem = devm_nvmem_register(dev, &config);
228 	if (IS_ERR(priv->nvmem))
229 		return PTR_ERR(priv->nvmem);
230 
231 	return u_boot_env_parse(priv);
232 }
233 
234 static const struct of_device_id u_boot_env_of_match_table[] = {
235 	{ .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
236 	{ .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
237 	{ .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
238 	{ .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
239 	{},
240 };
241 
242 static struct platform_driver u_boot_env_driver = {
243 	.probe = u_boot_env_probe,
244 	.driver = {
245 		.name = "u_boot_env",
246 		.of_match_table = u_boot_env_of_match_table,
247 	},
248 };
249 module_platform_driver(u_boot_env_driver);
250 
251 MODULE_AUTHOR("Rafał Miłecki");
252 MODULE_LICENSE("GPL");
253 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
254