/src/barebox/common/imd.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * (C) Copyright 2014 Sascha Hauer, Pengutronix |
4 | | */ |
5 | | #ifdef __BAREBOX__ |
6 | | #include <common.h> |
7 | | #include <image-metadata.h> |
8 | | #include <libfile.h> |
9 | | #include <getopt.h> |
10 | | #include <malloc.h> |
11 | | #include <fs.h> |
12 | | #include <crc.h> |
13 | | |
14 | | #ifndef CONFIG_CMD_IMD |
15 | | int imd_command_setenv(const char *variable_name, const char *value) |
16 | | { |
17 | | return -ENOSYS; |
18 | | } |
19 | | #endif |
20 | | static inline void read_file_2_free(void *buf) |
21 | 0 | { |
22 | 0 | free(buf); |
23 | 0 | } |
24 | | |
25 | | static int imd_read_file(const char *filename, size_t *size, void **outbuf, |
26 | | bool allow_mmap) |
27 | 0 | { |
28 | 0 | return read_file_2(filename, size, outbuf, 0x400000); |
29 | 0 | } |
30 | | #endif |
31 | | |
32 | | /* |
33 | | * imd_next - return a pointer to the next metadata field. |
34 | | * @imd The current metadata field |
35 | | */ |
36 | | const struct imd_header *imd_next(const struct imd_header *imd) |
37 | 0 | { |
38 | 0 | int length; |
39 | |
|
40 | 0 | length = imd_read_length(imd); |
41 | 0 | length = ALIGN(length, 4); |
42 | 0 | length += sizeof(struct imd_header); |
43 | |
|
44 | 0 | return (const void *)imd + length; |
45 | 0 | } |
46 | | |
47 | | const struct imd_header *imd_find_type(const struct imd_header *imd, |
48 | | uint32_t type) |
49 | 0 | { |
50 | 0 | imd_for_each(imd, imd) |
51 | 0 | if (imd_read_type(imd) == type) |
52 | 0 | return imd; |
53 | | |
54 | 0 | return NULL; |
55 | 0 | } |
56 | | |
57 | | static int imd_next_validate(const void *buf, int bufsize, int start_ofs) |
58 | 0 | { |
59 | 0 | int length, size; |
60 | 0 | const struct imd_header *imd = buf + start_ofs; |
61 | |
|
62 | 0 | size = bufsize - start_ofs; |
63 | |
|
64 | 0 | if (size < sizeof(struct imd_header)) { |
65 | 0 | debug("trunkated tag at offset %dd\n", start_ofs); |
66 | 0 | return -EINVAL; |
67 | 0 | } |
68 | | |
69 | 0 | length = imd_read_length(imd); |
70 | 0 | length = ALIGN(length, 4); |
71 | 0 | length += sizeof(struct imd_header); |
72 | |
|
73 | 0 | if (size < length) { |
74 | 0 | debug("tag at offset %d with size %d exceeds bufsize %d\n", |
75 | 0 | start_ofs, size, bufsize); |
76 | 0 | return -EINVAL; |
77 | 0 | } |
78 | | |
79 | 0 | debug("tag at offset %d has length %d\n", start_ofs, length); |
80 | |
|
81 | 0 | return length; |
82 | 0 | } |
83 | | |
84 | | static int imd_validate_tags(const void *buf, int bufsize, int start_ofs) |
85 | 0 | { |
86 | 0 | int ret; |
87 | 0 | const struct imd_header *imd = buf + start_ofs; |
88 | |
|
89 | 0 | while (1) { |
90 | 0 | uint32_t type; |
91 | |
|
92 | 0 | ret = imd_next_validate(buf, bufsize, start_ofs); |
93 | 0 | if (ret < 0) { |
94 | 0 | debug("Invalid tag at offset %d\n", start_ofs); |
95 | 0 | return -EINVAL; |
96 | 0 | } |
97 | | |
98 | 0 | imd = (void *)imd + ret; |
99 | 0 | start_ofs += ret; |
100 | |
|
101 | 0 | type = imd_read_type(imd); |
102 | |
|
103 | 0 | if (!imd_type_valid(type)) { |
104 | 0 | debug("Invalid: tag: 0x%08x\n", type); |
105 | 0 | return -EINVAL; |
106 | 0 | } |
107 | | |
108 | 0 | if (type == IMD_TYPE_END) |
109 | 0 | return 0; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | /* |
114 | | * imd_get - find valid metadata in a buffer |
115 | | * @buf the buffer |
116 | | * @size buffer size |
117 | | * |
118 | | * This iterates over a buffer and searches for metadata. The metadata |
119 | | * is checked for consistency (length fields not exceeding buffer and |
120 | | * presence of end header) and returned. |
121 | | * |
122 | | * Return: a pointer to the image metadata or a ERR_PTR |
123 | | */ |
124 | | const struct imd_header *imd_get(const void *buf, int size) |
125 | 0 | { |
126 | 0 | int start_ofs = 0; |
127 | 0 | int i, ret; |
128 | |
|
129 | 0 | for (i = start_ofs; i < size - 32; i++) { |
130 | 0 | uint32_t type; |
131 | |
|
132 | 0 | type = imd_read_le32(buf + i); |
133 | |
|
134 | 0 | if (type != IMD_TYPE_START) |
135 | 0 | continue; |
136 | | |
137 | 0 | debug("Start tag found at offset %d\n", i); |
138 | |
|
139 | 0 | ret = imd_validate_tags(buf, size, i); |
140 | 0 | if (!ret) |
141 | 0 | return buf + i; |
142 | 0 | } |
143 | | |
144 | 0 | return ERR_PTR(-EINVAL); |
145 | 0 | } |
146 | | |
147 | | struct imd_type_names { |
148 | | uint32_t type; |
149 | | const char *name; |
150 | | }; |
151 | | |
152 | | static struct imd_type_names imd_types[] = { |
153 | | { |
154 | | .type = IMD_TYPE_RELEASE, |
155 | | .name = "release", |
156 | | }, { |
157 | | .type = IMD_TYPE_BUILD, |
158 | | .name = "build", |
159 | | }, { |
160 | | .type = IMD_TYPE_MODEL, |
161 | | .name = "model", |
162 | | }, { |
163 | | .type = IMD_TYPE_PARAMETER, |
164 | | .name = "parameter", |
165 | | }, { |
166 | | .type = IMD_TYPE_OF_COMPATIBLE, |
167 | | .name = "of_compatible", |
168 | | }, { |
169 | | .type = IMD_TYPE_CRC32, |
170 | | .name = "crc32", |
171 | | }, { |
172 | | .type = IMD_TYPE_BUILDSYSTEM, |
173 | | .name = "buildsystem version", |
174 | | }, |
175 | | }; |
176 | | |
177 | | /** |
178 | | * imd_type_to_name - convert a imd type to a name |
179 | | * @type: The imd type |
180 | | * |
181 | | * This function returns a string representation of the imd type |
182 | | */ |
183 | | const char *imd_type_to_name(uint32_t type) |
184 | 0 | { |
185 | 0 | int i; |
186 | |
|
187 | 0 | for (i = 0; i < ARRAY_SIZE(imd_types); i++) |
188 | 0 | if (imd_types[i].type == type) |
189 | 0 | return imd_types[i].name; |
190 | | |
191 | 0 | return "unknown"; |
192 | 0 | } |
193 | | |
194 | | static uint32_t imd_name_to_type(const char *name) |
195 | 0 | { |
196 | 0 | int i; |
197 | |
|
198 | 0 | for (i = 0; i < ARRAY_SIZE(imd_types); i++) |
199 | 0 | if (!strcmp(imd_types[i].name, name)) |
200 | 0 | return imd_types[i].type; |
201 | | |
202 | 0 | return IMD_TYPE_INVALID; |
203 | 0 | } |
204 | | |
205 | | /** |
206 | | * imd_string_data - get string data |
207 | | * @imd: The IMD entry |
208 | | * @index: The index of the string |
209 | | * |
210 | | * This function returns the string in @imd indexed by @index. |
211 | | * |
212 | | * Return: A pointer to the string or NULL if the string is not found |
213 | | */ |
214 | | const char *imd_string_data(const struct imd_header *imd, int index) |
215 | 0 | { |
216 | 0 | int i, total = 0, l = 0; |
217 | 0 | int len = imd_read_length(imd); |
218 | 0 | char *p = (char *)(imd + 1); |
219 | |
|
220 | 0 | if (!imd_is_string(imd_read_type(imd))) |
221 | 0 | return NULL; |
222 | | |
223 | 0 | for (i = 0; total < len; total += l, p += l) { |
224 | 0 | l = strlen(p) + 1; |
225 | 0 | if (i++ == index) |
226 | 0 | return p; |
227 | 0 | } |
228 | | |
229 | 0 | return NULL; |
230 | 0 | } |
231 | | |
232 | | /** |
233 | | * imd_concat_strings - get string data |
234 | | * @imd: The IMD entry |
235 | | * |
236 | | * This function returns the concatenated strings in @imd. The string |
237 | | * returned is allocated with malloc() and the caller has to free() it. |
238 | | * |
239 | | * Return: A pointer to the string or NULL if the string is not found |
240 | | */ |
241 | | char *imd_concat_strings(const struct imd_header *imd) |
242 | 0 | { |
243 | 0 | int i, len = imd_read_length(imd); |
244 | 0 | char *str; |
245 | 0 | char *data = (char *)(imd + 1); |
246 | |
|
247 | 0 | if (!imd_is_string(imd_read_type(imd))) |
248 | 0 | return NULL; |
249 | | |
250 | 0 | str = malloc(len); |
251 | 0 | if (!str) |
252 | 0 | return NULL; |
253 | | |
254 | 0 | memcpy(str, data, len); |
255 | |
|
256 | 0 | for (i = 0; i < len - 1; i++) |
257 | 0 | if (str[i] == 0) |
258 | 0 | str[i] = ' '; |
259 | |
|
260 | 0 | return str; |
261 | 0 | } |
262 | | |
263 | | /** |
264 | | * imd_uint32_flags - get uint32 flags |
265 | | * @imd: The IMD entry |
266 | | * |
267 | | * This function returns the flags in @imd. |
268 | | * |
269 | | * Return: A pointer to the flags or NULL if the entry is not uint32. |
270 | | */ |
271 | 0 | static uint32_t *imd_crc32_flags(const struct imd_header *imd) { |
272 | 0 | char *p = (char *)(imd + 1); |
273 | |
|
274 | 0 | if (!imd_is_crc32(imd_read_type(imd))) |
275 | 0 | return NULL; |
276 | | |
277 | 0 | return (uint32_t *)(p + sizeof(uint32_t)); |
278 | 0 | } |
279 | | |
280 | | /** |
281 | | * imd_get_param - get a parameter |
282 | | * @imd: The IMD entry |
283 | | * @name: The name of the parameter. |
284 | | * |
285 | | * Parameters have the IMD type IMD_TYPE_PARAMETER and the form |
286 | | * "key=value". This function iterates over the IMD entries and when |
287 | | * it finds a parameter with name "key" it returns the value found. |
288 | | * |
289 | | * Return: A pointer to the value or NULL if the string is not found |
290 | | */ |
291 | | const char *imd_get_param(const struct imd_header *imd, const char *name) |
292 | 0 | { |
293 | 0 | const struct imd_header *cur; |
294 | 0 | int namelen = strlen(name); |
295 | |
|
296 | 0 | imd_for_each(imd, cur) { |
297 | 0 | char *data = (char *)(cur + 1); |
298 | |
|
299 | 0 | if (cur->type != IMD_TYPE_PARAMETER) |
300 | 0 | continue; |
301 | 0 | if (strncmp(name, data, namelen)) |
302 | 0 | continue; |
303 | 0 | if (data[namelen] != '=') |
304 | 0 | continue; |
305 | 0 | return data + namelen + 1; |
306 | 0 | } |
307 | | |
308 | 0 | return NULL; |
309 | 0 | } |
310 | | |
311 | | static int imd_calculate_crc32(void *input, const struct imd_header *imd_start, |
312 | | struct imd_header **imd_crc, uint32_t *crc, |
313 | | size_t size) |
314 | 0 | { |
315 | 0 | const struct imd_header *imd; |
316 | 0 | int length; |
317 | 0 | int end_ofs = (char *)imd_start - (char *)input + sizeof(char) * 8; |
318 | 0 | *imd_crc = NULL; |
319 | | |
320 | | /* search the checksum imd token */ |
321 | 0 | imd_for_each(imd_start, imd) { |
322 | 0 | length = imd_read_length(imd); |
323 | 0 | length = ALIGN(length, 4); |
324 | 0 | length += sizeof(struct imd_header); |
325 | |
|
326 | 0 | if (imd_is_crc32(imd_read_type(imd))) { |
327 | 0 | *imd_crc = (struct imd_header *)imd; |
328 | 0 | debug("Found crc token at %d\n", end_ofs); |
329 | 0 | break; |
330 | 0 | } |
331 | | |
332 | 0 | end_ofs += length; |
333 | 0 | } |
334 | | |
335 | | /* |
336 | | * Calculate checksum from start of input up to the checksum. |
337 | | * The checksum and the flags field in the imd_entry_crc32 entry are |
338 | | * modified after the checksum is calculated. Therefore skip them here |
339 | | * or the checksum will become invalid once it is written to the |
340 | | * checksum tag. |
341 | | */ |
342 | 0 | length = sizeof(struct imd_header); |
343 | 0 | end_ofs += length; |
344 | |
|
345 | 0 | *crc = crc32(*crc, input, end_ofs); |
346 | 0 | debug("Calculated checksum from %d to %d: 0x%08x\n", 0, end_ofs, *crc); |
347 | | |
348 | | /* move past the checksum data and flags field */ |
349 | 0 | end_ofs += sizeof(uint32_t) + sizeof(char); |
350 | 0 | input += end_ofs; |
351 | |
|
352 | 0 | *crc = crc32(*crc, input, size - end_ofs); |
353 | 0 | debug("Calculated checksum from %d to %zu: 0x%08x\n", end_ofs, |
354 | 0 | end_ofs + (size - end_ofs), *crc); |
355 | |
|
356 | 0 | return 0; |
357 | 0 | } |
358 | | |
359 | | static int imd_write_crc32(void *buf, const struct imd_header *imd_start, |
360 | | const char *filename, size_t size) |
361 | 0 | { |
362 | 0 | struct imd_header *imd_crc; |
363 | 0 | uint32_t crc = 0; |
364 | |
|
365 | 0 | imd_calculate_crc32(buf, imd_start, &imd_crc, &crc, size); |
366 | 0 | debug("Calculated crc: 0x%08x\n", crc); |
367 | |
|
368 | 0 | if (!imd_crc) { |
369 | 0 | debug("No tag of type 0x%08x found\n", IMD_TYPE_CRC32); |
370 | |
|
371 | 0 | return -ENODATA; |
372 | 0 | } else { |
373 | 0 | uint32_t *p = (uint32_t *)(imd_crc + 1); |
374 | 0 | int ret; |
375 | |
|
376 | 0 | if (*p != crc) { |
377 | 0 | uint32_t *flags = imd_crc32_flags(imd_crc); |
378 | 0 | *flags |= IMD_CRC32_FLAG_TAG_VALID; |
379 | 0 | debug("Update crc token from 0x%08x to 0x%08x (flags 0x%08x)\n", *p, crc, *flags); |
380 | 0 | *p = crc; |
381 | |
|
382 | 0 | ret = write_file(filename, buf, size); |
383 | 0 | if (ret < 0) { |
384 | 0 | eprintf("CRC: write crc token to %s failed: %d\n", filename, ret); |
385 | 0 | return ret; |
386 | 0 | } |
387 | 0 | } |
388 | 0 | } |
389 | | |
390 | 0 | return 0; |
391 | 0 | }; |
392 | | |
393 | | int imd_verify_crc32(void *buf, size_t size) |
394 | 0 | { |
395 | 0 | const struct imd_header *imd_start; |
396 | 0 | struct imd_header *imd_crc; |
397 | 0 | uint32_t crc = 0; |
398 | |
|
399 | 0 | imd_start = imd_get(buf, size); |
400 | 0 | if (IS_ERR(imd_start)) |
401 | 0 | return PTR_ERR(imd_start); |
402 | | |
403 | 0 | imd_calculate_crc32(buf, imd_start, &imd_crc, &crc, size); |
404 | 0 | debug("Calculated crc: 0x%08x\n", crc); |
405 | |
|
406 | 0 | if (!imd_crc) { |
407 | 0 | debug("No tag of type 0x%08x found\n", IMD_TYPE_CRC32); |
408 | |
|
409 | 0 | return -ENOENT; |
410 | 0 | } else { |
411 | 0 | uint32_t *p = (uint32_t *)(imd_crc + 1); |
412 | 0 | uint32_t *flags = imd_crc32_flags(imd_crc); |
413 | |
|
414 | 0 | if (*p != crc && imd_crc32_is_valid(*flags)) { |
415 | 0 | eprintf("CRC: image corrupted. Found checksum 0x%08x instead of 0x%08x\n", |
416 | 0 | *p, crc); |
417 | 0 | return -EILSEQ; |
418 | 0 | } else if (*p != crc && !imd_crc32_is_valid(*flags)) { |
419 | 0 | debug("CRC: is invalid, but the checksum tag is not enabled\n"); |
420 | 0 | return -EINVAL; |
421 | 0 | } else { |
422 | 0 | printf("CRC: valid\n"); |
423 | 0 | } |
424 | 0 | } |
425 | | |
426 | 0 | return 0; |
427 | 0 | }; |
428 | | |
429 | | int imd_command_verbose; |
430 | | |
431 | | int imd_command(int argc, char *argv[]) |
432 | 0 | { |
433 | 0 | int ret, opt, strno = -1; |
434 | 0 | void *buf; |
435 | 0 | size_t size; |
436 | 0 | uint32_t type = IMD_TYPE_INVALID; |
437 | 0 | const struct imd_header *imd_start, *imd; |
438 | 0 | const char *filename; |
439 | 0 | const char *variable_name = NULL; |
440 | 0 | char *str; |
441 | 0 | uint32_t checksum = 0; |
442 | 0 | uint32_t verify = 0; |
443 | 0 | bool allow_mmap = true; |
444 | |
|
445 | 0 | imd_command_verbose = 0; |
446 | |
|
447 | 0 | while ((opt = getopt(argc, argv, "vt:s:n:cV")) > 0) { |
448 | 0 | switch(opt) { |
449 | 0 | case 't': |
450 | 0 | type = imd_name_to_type(optarg); |
451 | 0 | if (type == IMD_TYPE_INVALID) { |
452 | 0 | eprintf("no such type: %s\n", optarg); |
453 | 0 | return -ENOSYS; |
454 | 0 | } |
455 | 0 | break; |
456 | 0 | case 's': |
457 | 0 | variable_name = optarg; |
458 | 0 | break; |
459 | 0 | case 'v': |
460 | 0 | imd_command_verbose = 1; |
461 | 0 | break; |
462 | 0 | case 'n': |
463 | 0 | strno = simple_strtoul(optarg, NULL, 0); |
464 | 0 | break; |
465 | 0 | case 'c': |
466 | 0 | checksum = 1; |
467 | 0 | allow_mmap = false; |
468 | 0 | break; |
469 | 0 | case 'V': |
470 | 0 | verify = 1; |
471 | 0 | break; |
472 | 0 | default: |
473 | 0 | return -ENOSYS; |
474 | 0 | } |
475 | 0 | } |
476 | | |
477 | 0 | if (optind == argc) { |
478 | 0 | eprintf("No image given\n"); |
479 | 0 | return -ENOSYS; |
480 | 0 | } |
481 | | |
482 | 0 | filename = argv[optind]; |
483 | |
|
484 | 0 | ret = imd_read_file(filename, &size, &buf, allow_mmap); |
485 | 0 | if (ret && ret != -EFBIG) |
486 | 0 | return -errno; |
487 | | |
488 | 0 | imd_start = imd_get(buf, size); |
489 | 0 | if (IS_ERR(imd_start)) { |
490 | 0 | ret = PTR_ERR(imd_start); |
491 | 0 | goto out; |
492 | 0 | } |
493 | | |
494 | 0 | if (checksum) { |
495 | 0 | ret = imd_write_crc32(buf, imd_start, filename, size); |
496 | 0 | goto out; |
497 | 0 | } |
498 | | |
499 | 0 | if (verify) { |
500 | 0 | ret = imd_verify_crc32(buf, size); |
501 | 0 | goto out; |
502 | 0 | } |
503 | | |
504 | 0 | if (type == IMD_TYPE_INVALID) { |
505 | 0 | imd_for_each(imd_start, imd) { |
506 | 0 | uint32_t type = imd_read_type(imd); |
507 | |
|
508 | 0 | if (imd_is_string(type)) { |
509 | 0 | str = imd_concat_strings(imd); |
510 | |
|
511 | 0 | printf("%s: %s\n", imd_type_to_name(type), str); |
512 | 0 | } else if (imd_is_crc32(type)) { |
513 | 0 | uint32_t *p = (uint32_t *)(imd + 1); |
514 | |
|
515 | 0 | printf("%s: 0x%08x\n", imd_type_to_name(type), *p); |
516 | 0 | } else { |
517 | 0 | debug("Unknown tag 0x%08x\n", type); |
518 | 0 | } |
519 | 0 | } |
520 | 0 | } else { |
521 | 0 | imd = imd_find_type(imd_start, type); |
522 | 0 | if (!imd) { |
523 | 0 | debug("No tag of type 0x%08x found\n", type); |
524 | 0 | ret = -ENODATA; |
525 | 0 | goto out; |
526 | 0 | } |
527 | | |
528 | 0 | if (imd_is_string(type)) { |
529 | 0 | if (strno >= 0) { |
530 | 0 | const char *s = imd_string_data(imd, strno); |
531 | 0 | if (s) |
532 | 0 | str = strdup(s); |
533 | 0 | else |
534 | 0 | str = NULL; |
535 | 0 | } else { |
536 | 0 | str = imd_concat_strings(imd); |
537 | 0 | } |
538 | |
|
539 | 0 | if (!str) { |
540 | 0 | ret = -ENODATA; |
541 | 0 | goto out; |
542 | 0 | } |
543 | | |
544 | 0 | if (variable_name) |
545 | 0 | imd_command_setenv(variable_name, str); |
546 | 0 | else |
547 | 0 | printf("%s\n", str); |
548 | |
|
549 | 0 | free(str); |
550 | 0 | } else { |
551 | 0 | printf("tag 0x%08x present\n", type); |
552 | 0 | } |
553 | 0 | } |
554 | | |
555 | 0 | ret = 0; |
556 | 0 | out: |
557 | 0 | read_file_2_free(buf); |
558 | 0 | return ret; |
559 | 0 | } |