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