xref: /openbmc/linux/drivers/nvmem/u-boot-env.c (revision a2cab953)
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 };
20 
21 struct u_boot_env {
22 	struct device *dev;
23 	enum u_boot_env_format format;
24 
25 	struct mtd_info *mtd;
26 
27 	/* Cells */
28 	struct nvmem_cell_info *cells;
29 	int ncells;
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 static int u_boot_env_read(void *context, unsigned int offset, void *val,
44 			   size_t bytes)
45 {
46 	struct u_boot_env *priv = context;
47 	struct device *dev = priv->dev;
48 	size_t bytes_read;
49 	int err;
50 
51 	err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
52 	if (err && !mtd_is_bitflip(err)) {
53 		dev_err(dev, "Failed to read from mtd: %d\n", err);
54 		return err;
55 	}
56 
57 	if (bytes_read != bytes) {
58 		dev_err(dev, "Failed to read %zu bytes\n", bytes);
59 		return -EIO;
60 	}
61 
62 	return 0;
63 }
64 
65 static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,
66 				size_t data_offset, size_t data_len)
67 {
68 	struct device *dev = priv->dev;
69 	char *data = buf + data_offset;
70 	char *var, *value, *eq;
71 	int idx;
72 
73 	priv->ncells = 0;
74 	for (var = data; var < data + data_len && *var; var += strlen(var) + 1)
75 		priv->ncells++;
76 
77 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
78 	if (!priv->cells)
79 		return -ENOMEM;
80 
81 	for (var = data, idx = 0;
82 	     var < data + data_len && *var;
83 	     var = value + strlen(value) + 1, idx++) {
84 		eq = strchr(var, '=');
85 		if (!eq)
86 			break;
87 		*eq = '\0';
88 		value = eq + 1;
89 
90 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
91 		if (!priv->cells[idx].name)
92 			return -ENOMEM;
93 		priv->cells[idx].offset = data_offset + value - data;
94 		priv->cells[idx].bytes = strlen(value);
95 		priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
96 	}
97 
98 	if (WARN_ON(idx != priv->ncells))
99 		priv->ncells = idx;
100 
101 	return 0;
102 }
103 
104 static int u_boot_env_parse(struct u_boot_env *priv)
105 {
106 	struct device *dev = priv->dev;
107 	size_t crc32_data_offset;
108 	size_t crc32_data_len;
109 	size_t crc32_offset;
110 	size_t data_offset;
111 	size_t data_len;
112 	uint32_t crc32;
113 	uint32_t calc;
114 	size_t bytes;
115 	uint8_t *buf;
116 	int err;
117 
118 	buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
119 	if (!buf) {
120 		err = -ENOMEM;
121 		goto err_out;
122 	}
123 
124 	err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf);
125 	if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) {
126 		dev_err(dev, "Failed to read from mtd: %d\n", err);
127 		goto err_kfree;
128 	}
129 
130 	switch (priv->format) {
131 	case U_BOOT_FORMAT_SINGLE:
132 		crc32_offset = offsetof(struct u_boot_env_image_single, crc32);
133 		crc32_data_offset = offsetof(struct u_boot_env_image_single, data);
134 		data_offset = offsetof(struct u_boot_env_image_single, data);
135 		break;
136 	case U_BOOT_FORMAT_REDUNDANT:
137 		crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32);
138 		crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark);
139 		data_offset = offsetof(struct u_boot_env_image_redundant, data);
140 		break;
141 	}
142 	crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset));
143 	crc32_data_len = priv->mtd->size - crc32_data_offset;
144 	data_len = priv->mtd->size - data_offset;
145 
146 	calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
147 	if (calc != crc32) {
148 		dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32);
149 		err = -EINVAL;
150 		goto err_kfree;
151 	}
152 
153 	buf[priv->mtd->size - 1] = '\0';
154 	err = u_boot_env_add_cells(priv, buf, data_offset, data_len);
155 	if (err)
156 		dev_err(dev, "Failed to add cells: %d\n", err);
157 
158 err_kfree:
159 	kfree(buf);
160 err_out:
161 	return err;
162 }
163 
164 static int u_boot_env_probe(struct platform_device *pdev)
165 {
166 	struct nvmem_config config = {
167 		.name = "u-boot-env",
168 		.reg_read = u_boot_env_read,
169 	};
170 	struct device *dev = &pdev->dev;
171 	struct device_node *np = dev->of_node;
172 	struct u_boot_env *priv;
173 	int err;
174 
175 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
176 	if (!priv)
177 		return -ENOMEM;
178 	priv->dev = dev;
179 
180 	priv->format = (uintptr_t)of_device_get_match_data(dev);
181 
182 	priv->mtd = of_get_mtd_device_by_node(np);
183 	if (IS_ERR(priv->mtd)) {
184 		dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
185 		return PTR_ERR(priv->mtd);
186 	}
187 
188 	err = u_boot_env_parse(priv);
189 	if (err)
190 		return err;
191 
192 	config.dev = dev;
193 	config.cells = priv->cells;
194 	config.ncells = priv->ncells;
195 	config.priv = priv;
196 	config.size = priv->mtd->size;
197 
198 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
199 }
200 
201 static const struct of_device_id u_boot_env_of_match_table[] = {
202 	{ .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
203 	{ .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
204 	{ .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
205 	{},
206 };
207 
208 static struct platform_driver u_boot_env_driver = {
209 	.probe = u_boot_env_probe,
210 	.driver = {
211 		.name = "u_boot_env",
212 		.of_match_table = u_boot_env_of_match_table,
213 	},
214 };
215 module_platform_driver(u_boot_env_driver);
216 
217 MODULE_AUTHOR("Rafał Miłecki");
218 MODULE_LICENSE("GPL");
219 MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);
220