/src/barebox/drivers/misc/ubootvar.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | |
3 | | /* |
4 | | * U-Boot environment vriable blob driver |
5 | | * |
6 | | * Copyright (C) 2019 Zodiac Inflight Innovations |
7 | | */ |
8 | | |
9 | | #include <common.h> |
10 | | #include <init.h> |
11 | | #include <io.h> |
12 | | #include <of.h> |
13 | | #include <malloc.h> |
14 | | #include <envfs.h> |
15 | | #include <fs.h> |
16 | | #include <libfile.h> |
17 | | #include <command.h> |
18 | | #include <crc.h> |
19 | | |
20 | | enum ubootvar_flag_scheme { |
21 | | FLAG_NONE, |
22 | | FLAG_BOOLEAN, |
23 | | FLAG_INCREMENTAL, |
24 | | }; |
25 | | |
26 | | struct ubootvar_data { |
27 | | struct cdev cdev; |
28 | | char *path[2]; |
29 | | bool current; |
30 | | uint8_t flag; |
31 | | int count; |
32 | | void *data; |
33 | | size_t size; |
34 | | }; |
35 | | |
36 | | static int ubootvar_flush(struct cdev *cdev) |
37 | 0 | { |
38 | 0 | struct device *dev = cdev->dev; |
39 | 0 | struct ubootvar_data *ubdata = dev->priv; |
40 | 0 | const char *path = ubdata->path[!ubdata->current]; |
41 | 0 | uint32_t crc = 0xffffffff; |
42 | 0 | resource_size_t size; |
43 | 0 | const void *data; |
44 | 0 | int fd, ret = 0; |
45 | |
|
46 | 0 | fd = open(path, O_WRONLY); |
47 | 0 | if (fd < 0) { |
48 | 0 | dev_err(dev, "Failed to open %s\n", path); |
49 | 0 | return -errno; |
50 | 0 | } |
51 | | /* |
52 | | * FIXME: This code needs to do a proper protect/unprotect and |
53 | | * erase calls to work on MTD devices |
54 | | */ |
55 | | |
56 | | /* |
57 | | * Write a dummy CRC first as a way of invalidating the |
58 | | * environment in case we fail mid-flushing |
59 | | */ |
60 | 0 | if (write_full(fd, &crc, sizeof(crc)) != sizeof(crc)) { |
61 | 0 | dev_err(dev, "Failed to write dummy CRC\n"); |
62 | 0 | ret = -errno; |
63 | 0 | goto close_fd; |
64 | 0 | } |
65 | | |
66 | 0 | if (ubdata->count > 1) { |
67 | | /* |
68 | | * FIXME: This assumes FLAG_INCREMENTAL |
69 | | */ |
70 | 0 | const uint8_t flag = ++ubdata->flag; |
71 | |
|
72 | 0 | if (write_full(fd, &flag, sizeof(flag)) != sizeof(flag)) { |
73 | 0 | dev_dbg(dev, "Failed to write flag\n"); |
74 | 0 | ret = -errno; |
75 | 0 | goto close_fd; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | 0 | data = (const void *)ubdata->data; |
80 | 0 | size = ubdata->size; |
81 | | |
82 | | /* |
83 | | * Write out and flush all of the new environment data |
84 | | */ |
85 | 0 | if (write_full(fd, data, size) != size) { |
86 | 0 | dev_dbg(dev, "Failed to write data\n"); |
87 | 0 | ret = -errno; |
88 | 0 | goto close_fd; |
89 | 0 | } |
90 | | |
91 | 0 | if (flush(fd)) { |
92 | 0 | dev_dbg(dev, "Failed to flush written data\n"); |
93 | 0 | ret = -errno; |
94 | 0 | goto close_fd; |
95 | 0 | } |
96 | | /* |
97 | | * Now that all of the environment data is out, we can go back |
98 | | * to the start of the block and write correct CRC, to finish |
99 | | * the processs. |
100 | | */ |
101 | 0 | if (lseek(fd, 0, SEEK_SET) != 0) { |
102 | 0 | dev_dbg(dev, "lseek() failed\n"); |
103 | 0 | ret = -errno; |
104 | 0 | goto close_fd; |
105 | 0 | } |
106 | | |
107 | 0 | crc = crc32(0, data, size); |
108 | 0 | if (write_full(fd, &crc, sizeof(crc)) != sizeof(crc)) { |
109 | 0 | dev_dbg(dev, "Failed to write valid CRC\n"); |
110 | 0 | ret = -errno; |
111 | 0 | goto close_fd; |
112 | 0 | } |
113 | | /* |
114 | | * Now that we've successfully written new environment blob |
115 | | * out, switch current partition. |
116 | | */ |
117 | 0 | ubdata->current = !ubdata->current; |
118 | |
|
119 | 0 | close_fd: |
120 | 0 | close(fd); |
121 | 0 | return ret; |
122 | 0 | } |
123 | | |
124 | | static ssize_t |
125 | | ubootvar_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, |
126 | | unsigned long flags) |
127 | 0 | { |
128 | 0 | struct device *dev = cdev->dev; |
129 | 0 | struct ubootvar_data *ubdata = dev->priv; |
130 | |
|
131 | 0 | WARN_ON(flags & O_RWSIZE_MASK); |
132 | |
|
133 | 0 | memcpy(buf, ubdata->data + offset, count); |
134 | |
|
135 | 0 | return count; |
136 | 0 | } |
137 | | |
138 | | static ssize_t |
139 | | ubootvar_write(struct cdev *cdev, const void *buf, size_t count, |
140 | | loff_t offset, unsigned long flags) |
141 | 0 | { |
142 | 0 | struct device *dev = cdev->dev; |
143 | 0 | struct ubootvar_data *ubdata = dev->priv; |
144 | |
|
145 | 0 | WARN_ON(flags & O_RWSIZE_MASK); |
146 | |
|
147 | 0 | memcpy(ubdata->data + offset, buf, count); |
148 | |
|
149 | 0 | return count; |
150 | 0 | } |
151 | | |
152 | | static int ubootvar_memmap(struct cdev *cdev, void **map, int flags) |
153 | 0 | { |
154 | 0 | struct device *dev = cdev->dev; |
155 | 0 | struct ubootvar_data *ubdata = dev->priv; |
156 | |
|
157 | 0 | *map = ubdata->data; |
158 | |
|
159 | 0 | return 0; |
160 | 0 | } |
161 | | |
162 | | static struct cdev_operations ubootvar_ops = { |
163 | | .read = ubootvar_read, |
164 | | .write = ubootvar_write, |
165 | | .memmap = ubootvar_memmap, |
166 | | .flush = ubootvar_flush, |
167 | | }; |
168 | | |
169 | | static void ubootenv_info(struct device *dev) |
170 | 0 | { |
171 | 0 | struct ubootvar_data *ubdata = dev->priv; |
172 | |
|
173 | 0 | printf("Current environment copy: %s\n", |
174 | 0 | ubdata->path[ubdata->current]); |
175 | 0 | } |
176 | | |
177 | | static int ubootenv_probe(struct device *dev) |
178 | 0 | { |
179 | 0 | struct ubootvar_data *ubdata; |
180 | 0 | unsigned int crc_ok = 0; |
181 | 0 | int ret, i, current, count = 0; |
182 | 0 | uint32_t crc[2]; |
183 | 0 | uint8_t flag[2] = { FLAG_NONE, FLAG_NONE }; |
184 | 0 | size_t size[2]; |
185 | 0 | void *blob[2] = { NULL, NULL }; |
186 | 0 | uint8_t *data[2]; |
187 | | |
188 | | /* |
189 | | * FIXME: Flag scheme is determined by the type of underlined |
190 | | * non-volatible device, so it should probably come from |
191 | | * Device Tree binding. Currently we just assume incremental |
192 | | * scheme since that is what is used on SD/eMMC devices. |
193 | | */ |
194 | 0 | enum ubootvar_flag_scheme flag_scheme = FLAG_INCREMENTAL; |
195 | |
|
196 | 0 | ubdata = xzalloc(sizeof(*ubdata)); |
197 | |
|
198 | 0 | ret = of_find_path(dev->of_node, "device-path-0", |
199 | 0 | &ubdata->path[0], |
200 | 0 | OF_FIND_PATH_FLAGS_BB); |
201 | 0 | if (ret) |
202 | 0 | ret = of_find_path(dev->of_node, "device-path", |
203 | 0 | &ubdata->path[0], |
204 | 0 | OF_FIND_PATH_FLAGS_BB); |
205 | |
|
206 | 0 | if (ret) { |
207 | 0 | dev_err(dev, "Failed to find first device\n"); |
208 | 0 | goto out; |
209 | 0 | } |
210 | | |
211 | 0 | count++; |
212 | |
|
213 | 0 | if (!of_find_path(dev->of_node, "device-path-1", |
214 | 0 | &ubdata->path[1], |
215 | 0 | OF_FIND_PATH_FLAGS_BB)) { |
216 | 0 | count++; |
217 | 0 | } else { |
218 | | /* |
219 | | * If there's no redundant environment partition we |
220 | | * configure both paths to point to the same device, |
221 | | * so that writing logic could stay the same for both |
222 | | * redundant and non-redundant cases |
223 | | */ |
224 | 0 | ubdata->path[1] = strdup(ubdata->path[0]); |
225 | 0 | } |
226 | |
|
227 | 0 | for (i = 0; i < count; i++) { |
228 | 0 | data[i] = blob[i] = read_file(ubdata->path[i], &size[i]); |
229 | 0 | if (!blob[i]) { |
230 | 0 | dev_err(dev, "Failed to read U-Boot environment\n"); |
231 | 0 | ret = -EIO; |
232 | 0 | goto out; |
233 | 0 | } |
234 | | |
235 | 0 | if (size[i] < sizeof(uint32_t) + (count > 1)) { |
236 | 0 | dev_err(dev, "U-Boot environment %s is too small\n", |
237 | 0 | ubdata->path[i]); |
238 | 0 | ret = -EINVAL; |
239 | 0 | goto out; |
240 | 0 | } |
241 | | |
242 | 0 | crc[i] = *(uint32_t *)data[i]; |
243 | |
|
244 | 0 | size[i] -= sizeof(uint32_t); |
245 | 0 | data[i] += sizeof(uint32_t); |
246 | |
|
247 | 0 | if (count > 1) { |
248 | | /* |
249 | | * When used in primary/redundant |
250 | | * configuration, environment header has an |
251 | | * additional "flag" byte |
252 | | */ |
253 | 0 | flag[i] = *data[i]; |
254 | 0 | size[i] -= sizeof(uint8_t); |
255 | 0 | data[i] += sizeof(uint8_t); |
256 | 0 | } |
257 | |
|
258 | 0 | crc_ok |= (crc32(0, data[i], size[i]) == crc[i]) << i; |
259 | 0 | } |
260 | | |
261 | 0 | switch (crc_ok) { |
262 | 0 | case 0b00: |
263 | 0 | current = 0; |
264 | 0 | memset(data[0], 0, size[0]); |
265 | 0 | dev_info(dev, "No good partitions found, creating an empty one\n"); |
266 | 0 | break; |
267 | 0 | case 0b11: |
268 | | /* |
269 | | * Both partition are valid, so we need to examine |
270 | | * flags to determine which one to use as current |
271 | | */ |
272 | 0 | switch (flag_scheme) { |
273 | 0 | case FLAG_INCREMENTAL: |
274 | 0 | if ((flag[0] == 0xff && flag[1] == 0) || |
275 | 0 | (flag[1] == 0xff && flag[0] == 0)) { |
276 | | /* |
277 | | * When flag overflow happens current |
278 | | * partition is the one whose counter |
279 | | * reached zero first. That is if |
280 | | * flag[1] == 0 is true (1), then i |
281 | | * would be 1 as well |
282 | | */ |
283 | 0 | current = flag[1] == 0; |
284 | 0 | } else { |
285 | | /* |
286 | | * In no-overflow case the partition |
287 | | * with higher flag value is |
288 | | * considered current |
289 | | */ |
290 | 0 | current = flag[1] > flag[0]; |
291 | 0 | } |
292 | 0 | break; |
293 | 0 | default: |
294 | 0 | ret = -EINVAL; |
295 | 0 | dev_err(dev, "Unknown flag scheme %u\n", flag_scheme); |
296 | 0 | goto out; |
297 | 0 | } |
298 | 0 | break; |
299 | 0 | default: |
300 | | /* |
301 | | * Only one partition is valid, so the choice of the |
302 | | * current one is obvious |
303 | | */ |
304 | 0 | current = __ffs(crc_ok); |
305 | 0 | break; |
306 | 0 | }; |
307 | |
|
308 | 0 | ubdata->data = data[current]; |
309 | 0 | ubdata->size = size[current]; |
310 | |
|
311 | 0 | ubdata->cdev.name = basprintf("ubootvar%d", |
312 | 0 | cdev_find_free_index("ubootvar")); |
313 | 0 | ubdata->cdev.size = size[current]; |
314 | 0 | ubdata->cdev.ops = &ubootvar_ops; |
315 | 0 | ubdata->cdev.dev = dev; |
316 | 0 | ubdata->cdev.filetype = filetype_ubootvar; |
317 | 0 | ubdata->current = current; |
318 | 0 | ubdata->count = count; |
319 | 0 | ubdata->flag = flag[current]; |
320 | |
|
321 | 0 | dev->priv = ubdata; |
322 | |
|
323 | 0 | ret = devfs_create(&ubdata->cdev); |
324 | 0 | if (ret) { |
325 | 0 | dev_err(dev, "Failed to create corresponding cdev\n"); |
326 | 0 | goto out; |
327 | 0 | } |
328 | | |
329 | 0 | cdev_create_default_automount(&ubdata->cdev); |
330 | |
|
331 | 0 | if (count > 1) { |
332 | | /* |
333 | | * We won't be using read data from redundant |
334 | | * parttion, so we may as well free at this point |
335 | | */ |
336 | 0 | free(blob[!current]); |
337 | 0 | } |
338 | |
|
339 | 0 | devinfo_add(dev, ubootenv_info); |
340 | |
|
341 | 0 | return 0; |
342 | 0 | out: |
343 | 0 | for (i = 0; i < count; i++) |
344 | 0 | free(blob[i]); |
345 | |
|
346 | 0 | free(ubdata->path[0]); |
347 | 0 | free(ubdata->path[1]); |
348 | 0 | free(ubdata); |
349 | |
|
350 | 0 | return ret; |
351 | 0 | } |
352 | | |
353 | | static struct of_device_id ubootenv_dt_ids[] = { |
354 | | { |
355 | | .compatible = "barebox,uboot-environment", |
356 | | }, { |
357 | | /* sentinel */ |
358 | | } |
359 | | }; |
360 | | MODULE_DEVICE_TABLE(of, ubootenv_dt_ids); |
361 | | |
362 | | static struct driver ubootenv_driver = { |
363 | | .name = "uboot-environment", |
364 | | .probe = ubootenv_probe, |
365 | | .of_compatible = ubootenv_dt_ids, |
366 | | }; |
367 | | late_platform_driver(ubootenv_driver); |