]>
Commit | Line | Data |
---|---|---|
53fbb7e8 SG |
1 | /* |
2 | * Copyright (c) 2013, Google Inc. | |
3 | * | |
4 | * (C) Copyright 2008 Semihalf | |
5 | * | |
6 | * (C) Copyright 2000-2006 | |
7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
8 | * | |
1a459660 | 9 | * SPDX-License-Identifier: GPL-2.0+ |
53fbb7e8 SG |
10 | */ |
11 | ||
12 | #ifdef USE_HOSTCC | |
13 | #include "mkimage.h" | |
14 | #include <image.h> | |
15 | #include <time.h> | |
16 | #else | |
17 | #include <common.h> | |
782cfbb2 SG |
18 | #include <errno.h> |
19 | #include <asm/io.h> | |
20 | DECLARE_GLOBAL_DATA_PTR; | |
53fbb7e8 SG |
21 | #endif /* !USE_HOSTCC*/ |
22 | ||
23 | #include <bootstage.h> | |
24 | #include <sha1.h> | |
25 | #include <u-boot/crc.h> | |
26 | #include <u-boot/md5.h> | |
27 | ||
28 | /*****************************************************************************/ | |
29 | /* New uImage format routines */ | |
30 | /*****************************************************************************/ | |
31 | #ifndef USE_HOSTCC | |
32 | static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, | |
33 | ulong *addr, const char **name) | |
34 | { | |
35 | const char *sep; | |
36 | ||
37 | *addr = addr_curr; | |
38 | *name = NULL; | |
39 | ||
40 | sep = strchr(spec, sepc); | |
41 | if (sep) { | |
42 | if (sep - spec > 0) | |
43 | *addr = simple_strtoul(spec, NULL, 16); | |
44 | ||
45 | *name = sep + 1; | |
46 | return 1; | |
47 | } | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
52 | /** | |
53 | * fit_parse_conf - parse FIT configuration spec | |
54 | * @spec: input string, containing configuration spec | |
55 | * @add_curr: current image address (to be used as a possible default) | |
56 | * @addr: pointer to a ulong variable, will hold FIT image address of a given | |
57 | * configuration | |
58 | * @conf_name double pointer to a char, will hold pointer to a configuration | |
59 | * unit name | |
60 | * | |
61 | * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>, | |
62 | * where <addr> is a FIT image address that contains configuration | |
63 | * with a <conf> unit name. | |
64 | * | |
65 | * Address part is optional, and if omitted default add_curr will | |
66 | * be used instead. | |
67 | * | |
68 | * returns: | |
69 | * 1 if spec is a valid configuration string, | |
70 | * addr and conf_name are set accordingly | |
71 | * 0 otherwise | |
72 | */ | |
73 | int fit_parse_conf(const char *spec, ulong addr_curr, | |
74 | ulong *addr, const char **conf_name) | |
75 | { | |
76 | return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); | |
77 | } | |
78 | ||
79 | /** | |
80 | * fit_parse_subimage - parse FIT subimage spec | |
81 | * @spec: input string, containing subimage spec | |
82 | * @add_curr: current image address (to be used as a possible default) | |
83 | * @addr: pointer to a ulong variable, will hold FIT image address of a given | |
84 | * subimage | |
85 | * @image_name: double pointer to a char, will hold pointer to a subimage name | |
86 | * | |
87 | * fit_parse_subimage() expects subimage spec in the for of | |
88 | * [<addr>]:<subimage>, where <addr> is a FIT image address that contains | |
89 | * subimage with a <subimg> unit name. | |
90 | * | |
91 | * Address part is optional, and if omitted default add_curr will | |
92 | * be used instead. | |
93 | * | |
94 | * returns: | |
95 | * 1 if spec is a valid subimage string, | |
96 | * addr and image_name are set accordingly | |
97 | * 0 otherwise | |
98 | */ | |
99 | int fit_parse_subimage(const char *spec, ulong addr_curr, | |
100 | ulong *addr, const char **image_name) | |
101 | { | |
102 | return fit_parse_spec(spec, ':', addr_curr, addr, image_name); | |
103 | } | |
104 | #endif /* !USE_HOSTCC */ | |
105 | ||
106 | static void fit_get_debug(const void *fit, int noffset, | |
107 | char *prop_name, int err) | |
108 | { | |
109 | debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", | |
110 | prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), | |
111 | fdt_strerror(err)); | |
112 | } | |
113 | ||
87ebee39 | 114 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) |
53fbb7e8 SG |
115 | /** |
116 | * fit_print_contents - prints out the contents of the FIT format image | |
117 | * @fit: pointer to the FIT format image header | |
118 | * @p: pointer to prefix string | |
119 | * | |
120 | * fit_print_contents() formats a multi line FIT image contents description. | |
121 | * The routine prints out FIT image properties (root node level) follwed by | |
122 | * the details of each component image. | |
123 | * | |
124 | * returns: | |
125 | * no returned results | |
126 | */ | |
127 | void fit_print_contents(const void *fit) | |
128 | { | |
129 | char *desc; | |
130 | char *uname; | |
131 | int images_noffset; | |
132 | int confs_noffset; | |
133 | int noffset; | |
134 | int ndepth; | |
135 | int count = 0; | |
136 | int ret; | |
137 | const char *p; | |
138 | time_t timestamp; | |
139 | ||
1fe7d938 SG |
140 | /* Indent string is defined in header image.h */ |
141 | p = IMAGE_INDENT_STRING; | |
53fbb7e8 SG |
142 | |
143 | /* Root node properties */ | |
144 | ret = fit_get_desc(fit, 0, &desc); | |
145 | printf("%sFIT description: ", p); | |
146 | if (ret) | |
147 | printf("unavailable\n"); | |
148 | else | |
149 | printf("%s\n", desc); | |
150 | ||
151 | if (IMAGE_ENABLE_TIMESTAMP) { | |
152 | ret = fit_get_timestamp(fit, 0, ×tamp); | |
153 | printf("%sCreated: ", p); | |
154 | if (ret) | |
155 | printf("unavailable\n"); | |
156 | else | |
157 | genimg_print_time(timestamp); | |
158 | } | |
159 | ||
160 | /* Find images parent node offset */ | |
161 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
162 | if (images_noffset < 0) { | |
163 | printf("Can't find images parent node '%s' (%s)\n", | |
164 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); | |
165 | return; | |
166 | } | |
167 | ||
168 | /* Process its subnodes, print out component images details */ | |
169 | for (ndepth = 0, count = 0, | |
170 | noffset = fdt_next_node(fit, images_noffset, &ndepth); | |
171 | (noffset >= 0) && (ndepth > 0); | |
172 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
173 | if (ndepth == 1) { | |
174 | /* | |
175 | * Direct child node of the images parent node, | |
176 | * i.e. component image node. | |
177 | */ | |
178 | printf("%s Image %u (%s)\n", p, count++, | |
179 | fit_get_name(fit, noffset, NULL)); | |
180 | ||
181 | fit_image_print(fit, noffset, p); | |
182 | } | |
183 | } | |
184 | ||
185 | /* Find configurations parent node offset */ | |
186 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); | |
187 | if (confs_noffset < 0) { | |
188 | debug("Can't get configurations parent node '%s' (%s)\n", | |
189 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); | |
190 | return; | |
191 | } | |
192 | ||
193 | /* get default configuration unit name from default property */ | |
194 | uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); | |
195 | if (uname) | |
196 | printf("%s Default Configuration: '%s'\n", p, uname); | |
197 | ||
198 | /* Process its subnodes, print out configurations details */ | |
199 | for (ndepth = 0, count = 0, | |
200 | noffset = fdt_next_node(fit, confs_noffset, &ndepth); | |
201 | (noffset >= 0) && (ndepth > 0); | |
202 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
203 | if (ndepth == 1) { | |
204 | /* | |
205 | * Direct child node of the configurations parent node, | |
206 | * i.e. configuration node. | |
207 | */ | |
208 | printf("%s Configuration %u (%s)\n", p, count++, | |
209 | fit_get_name(fit, noffset, NULL)); | |
210 | ||
211 | fit_conf_print(fit, noffset, p); | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
d8b75360 SG |
216 | /** |
217 | * fit_image_print_data() - prints out the hash node details | |
218 | * @fit: pointer to the FIT format image header | |
219 | * @noffset: offset of the hash node | |
220 | * @p: pointer to prefix string | |
56518e71 | 221 | * @type: Type of information to print ("hash" or "sign") |
d8b75360 SG |
222 | * |
223 | * fit_image_print_data() lists properies for the processed hash node | |
224 | * | |
56518e71 SG |
225 | * This function avoid using puts() since it prints a newline on the host |
226 | * but does not in U-Boot. | |
227 | * | |
d8b75360 SG |
228 | * returns: |
229 | * no returned results | |
230 | */ | |
56518e71 SG |
231 | static void fit_image_print_data(const void *fit, int noffset, const char *p, |
232 | const char *type) | |
d8b75360 | 233 | { |
56518e71 | 234 | const char *keyname; |
d8b75360 SG |
235 | uint8_t *value; |
236 | int value_len; | |
56518e71 SG |
237 | char *algo; |
238 | int required; | |
239 | int ret, i; | |
d8b75360 | 240 | |
56518e71 | 241 | debug("%s %s node: '%s'\n", p, type, |
d8b75360 | 242 | fit_get_name(fit, noffset, NULL)); |
56518e71 | 243 | printf("%s %s algo: ", p, type); |
d8b75360 SG |
244 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
245 | printf("invalid/unsupported\n"); | |
246 | return; | |
247 | } | |
56518e71 SG |
248 | printf("%s", algo); |
249 | keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); | |
250 | required = fdt_getprop(fit, noffset, "required", NULL) != NULL; | |
251 | if (keyname) | |
252 | printf(":%s", keyname); | |
253 | if (required) | |
254 | printf(" (required)"); | |
255 | printf("\n"); | |
d8b75360 SG |
256 | |
257 | ret = fit_image_hash_get_value(fit, noffset, &value, | |
258 | &value_len); | |
56518e71 | 259 | printf("%s %s value: ", p, type); |
d8b75360 SG |
260 | if (ret) { |
261 | printf("unavailable\n"); | |
262 | } else { | |
263 | for (i = 0; i < value_len; i++) | |
264 | printf("%02x", value[i]); | |
265 | printf("\n"); | |
266 | } | |
267 | ||
56518e71 SG |
268 | debug("%s %s len: %d\n", p, type, value_len); |
269 | ||
270 | /* Signatures have a time stamp */ | |
271 | if (IMAGE_ENABLE_TIMESTAMP && keyname) { | |
272 | time_t timestamp; | |
273 | ||
274 | printf("%s Timestamp: ", p); | |
275 | if (fit_get_timestamp(fit, noffset, ×tamp)) | |
276 | printf("unavailable\n"); | |
277 | else | |
278 | genimg_print_time(timestamp); | |
279 | } | |
d8b75360 SG |
280 | } |
281 | ||
282 | /** | |
283 | * fit_image_print_verification_data() - prints out the hash/signature details | |
284 | * @fit: pointer to the FIT format image header | |
285 | * @noffset: offset of the hash or signature node | |
286 | * @p: pointer to prefix string | |
287 | * | |
288 | * This lists properies for the processed hash node | |
289 | * | |
290 | * returns: | |
291 | * no returned results | |
292 | */ | |
293 | static void fit_image_print_verification_data(const void *fit, int noffset, | |
294 | const char *p) | |
295 | { | |
296 | const char *name; | |
297 | ||
298 | /* | |
299 | * Check subnode name, must be equal to "hash" or "signature". | |
300 | * Multiple hash/signature nodes require unique unit node | |
301 | * names, e.g. hash@1, hash@2, signature@1, signature@2, etc. | |
302 | */ | |
303 | name = fit_get_name(fit, noffset, NULL); | |
56518e71 SG |
304 | if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) { |
305 | fit_image_print_data(fit, noffset, p, "Hash"); | |
306 | } else if (!strncmp(name, FIT_SIG_NODENAME, | |
307 | strlen(FIT_SIG_NODENAME))) { | |
308 | fit_image_print_data(fit, noffset, p, "Sign"); | |
309 | } | |
d8b75360 SG |
310 | } |
311 | ||
53fbb7e8 SG |
312 | /** |
313 | * fit_image_print - prints out the FIT component image details | |
314 | * @fit: pointer to the FIT format image header | |
315 | * @image_noffset: offset of the component image node | |
316 | * @p: pointer to prefix string | |
317 | * | |
318 | * fit_image_print() lists all mandatory properies for the processed component | |
319 | * image. If present, hash nodes are printed out as well. Load | |
320 | * address for images of type firmware is also printed out. Since the load | |
321 | * address is not mandatory for firmware images, it will be output as | |
322 | * "unavailable" when not present. | |
323 | * | |
324 | * returns: | |
325 | * no returned results | |
326 | */ | |
327 | void fit_image_print(const void *fit, int image_noffset, const char *p) | |
328 | { | |
329 | char *desc; | |
330 | uint8_t type, arch, os, comp; | |
331 | size_t size; | |
332 | ulong load, entry; | |
333 | const void *data; | |
334 | int noffset; | |
335 | int ndepth; | |
336 | int ret; | |
337 | ||
338 | /* Mandatory properties */ | |
339 | ret = fit_get_desc(fit, image_noffset, &desc); | |
340 | printf("%s Description: ", p); | |
341 | if (ret) | |
342 | printf("unavailable\n"); | |
343 | else | |
344 | printf("%s\n", desc); | |
345 | ||
346 | fit_image_get_type(fit, image_noffset, &type); | |
347 | printf("%s Type: %s\n", p, genimg_get_type_name(type)); | |
348 | ||
349 | fit_image_get_comp(fit, image_noffset, &comp); | |
350 | printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); | |
351 | ||
352 | ret = fit_image_get_data(fit, image_noffset, &data, &size); | |
353 | ||
354 | #ifndef USE_HOSTCC | |
355 | printf("%s Data Start: ", p); | |
c6ac13bd | 356 | if (ret) { |
53fbb7e8 | 357 | printf("unavailable\n"); |
c6ac13bd SG |
358 | } else { |
359 | void *vdata = (void *)data; | |
360 | ||
361 | printf("0x%08lx\n", (ulong)map_to_sysmem(vdata)); | |
362 | } | |
53fbb7e8 SG |
363 | #endif |
364 | ||
365 | printf("%s Data Size: ", p); | |
366 | if (ret) | |
367 | printf("unavailable\n"); | |
368 | else | |
369 | genimg_print_size(size); | |
370 | ||
371 | /* Remaining, type dependent properties */ | |
372 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || | |
373 | (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || | |
374 | (type == IH_TYPE_FLATDT)) { | |
375 | fit_image_get_arch(fit, image_noffset, &arch); | |
376 | printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); | |
377 | } | |
378 | ||
379 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { | |
380 | fit_image_get_os(fit, image_noffset, &os); | |
381 | printf("%s OS: %s\n", p, genimg_get_os_name(os)); | |
382 | } | |
383 | ||
384 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || | |
385 | (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) { | |
386 | ret = fit_image_get_load(fit, image_noffset, &load); | |
387 | printf("%s Load Address: ", p); | |
388 | if (ret) | |
389 | printf("unavailable\n"); | |
390 | else | |
391 | printf("0x%08lx\n", load); | |
392 | } | |
393 | ||
394 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || | |
395 | (type == IH_TYPE_RAMDISK)) { | |
396 | fit_image_get_entry(fit, image_noffset, &entry); | |
397 | printf("%s Entry Point: ", p); | |
398 | if (ret) | |
399 | printf("unavailable\n"); | |
400 | else | |
401 | printf("0x%08lx\n", entry); | |
402 | } | |
403 | ||
404 | /* Process all hash subnodes of the component image node */ | |
405 | for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); | |
406 | (noffset >= 0) && (ndepth > 0); | |
407 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
408 | if (ndepth == 1) { | |
409 | /* Direct child node of the component image node */ | |
d8b75360 | 410 | fit_image_print_verification_data(fit, noffset, p); |
53fbb7e8 SG |
411 | } |
412 | } | |
413 | } | |
87ebee39 | 414 | #endif |
53fbb7e8 | 415 | |
53fbb7e8 SG |
416 | /** |
417 | * fit_get_desc - get node description property | |
418 | * @fit: pointer to the FIT format image header | |
419 | * @noffset: node offset | |
420 | * @desc: double pointer to the char, will hold pointer to the descrption | |
421 | * | |
422 | * fit_get_desc() reads description property from a given node, if | |
423 | * description is found pointer to it is returened in third call argument. | |
424 | * | |
425 | * returns: | |
426 | * 0, on success | |
427 | * -1, on failure | |
428 | */ | |
429 | int fit_get_desc(const void *fit, int noffset, char **desc) | |
430 | { | |
431 | int len; | |
432 | ||
433 | *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); | |
434 | if (*desc == NULL) { | |
435 | fit_get_debug(fit, noffset, FIT_DESC_PROP, len); | |
436 | return -1; | |
437 | } | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
442 | /** | |
443 | * fit_get_timestamp - get node timestamp property | |
444 | * @fit: pointer to the FIT format image header | |
445 | * @noffset: node offset | |
446 | * @timestamp: pointer to the time_t, will hold read timestamp | |
447 | * | |
448 | * fit_get_timestamp() reads timestamp poperty from given node, if timestamp | |
449 | * is found and has a correct size its value is retured in third call | |
450 | * argument. | |
451 | * | |
452 | * returns: | |
453 | * 0, on success | |
454 | * -1, on property read failure | |
455 | * -2, on wrong timestamp size | |
456 | */ | |
457 | int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) | |
458 | { | |
459 | int len; | |
460 | const void *data; | |
461 | ||
462 | data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); | |
463 | if (data == NULL) { | |
464 | fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); | |
465 | return -1; | |
466 | } | |
467 | if (len != sizeof(uint32_t)) { | |
468 | debug("FIT timestamp with incorrect size of (%u)\n", len); | |
469 | return -2; | |
470 | } | |
471 | ||
472 | *timestamp = uimage_to_cpu(*((uint32_t *)data)); | |
473 | return 0; | |
474 | } | |
475 | ||
476 | /** | |
477 | * fit_image_get_node - get node offset for component image of a given unit name | |
478 | * @fit: pointer to the FIT format image header | |
479 | * @image_uname: component image node unit name | |
480 | * | |
481 | * fit_image_get_node() finds a component image (withing the '/images' | |
482 | * node) of a provided unit name. If image is found its node offset is | |
483 | * returned to the caller. | |
484 | * | |
485 | * returns: | |
486 | * image node offset when found (>=0) | |
487 | * negative number on failure (FDT_ERR_* code) | |
488 | */ | |
489 | int fit_image_get_node(const void *fit, const char *image_uname) | |
490 | { | |
491 | int noffset, images_noffset; | |
492 | ||
493 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
494 | if (images_noffset < 0) { | |
495 | debug("Can't find images parent node '%s' (%s)\n", | |
496 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); | |
497 | return images_noffset; | |
498 | } | |
499 | ||
500 | noffset = fdt_subnode_offset(fit, images_noffset, image_uname); | |
501 | if (noffset < 0) { | |
502 | debug("Can't get node offset for image unit name: '%s' (%s)\n", | |
503 | image_uname, fdt_strerror(noffset)); | |
504 | } | |
505 | ||
506 | return noffset; | |
507 | } | |
508 | ||
509 | /** | |
510 | * fit_image_get_os - get os id for a given component image node | |
511 | * @fit: pointer to the FIT format image header | |
512 | * @noffset: component image node offset | |
513 | * @os: pointer to the uint8_t, will hold os numeric id | |
514 | * | |
515 | * fit_image_get_os() finds os property in a given component image node. | |
516 | * If the property is found, its (string) value is translated to the numeric | |
517 | * id which is returned to the caller. | |
518 | * | |
519 | * returns: | |
520 | * 0, on success | |
521 | * -1, on failure | |
522 | */ | |
523 | int fit_image_get_os(const void *fit, int noffset, uint8_t *os) | |
524 | { | |
525 | int len; | |
526 | const void *data; | |
527 | ||
528 | /* Get OS name from property data */ | |
529 | data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); | |
530 | if (data == NULL) { | |
531 | fit_get_debug(fit, noffset, FIT_OS_PROP, len); | |
532 | *os = -1; | |
533 | return -1; | |
534 | } | |
535 | ||
536 | /* Translate OS name to id */ | |
537 | *os = genimg_get_os_id(data); | |
538 | return 0; | |
539 | } | |
540 | ||
541 | /** | |
542 | * fit_image_get_arch - get arch id for a given component image node | |
543 | * @fit: pointer to the FIT format image header | |
544 | * @noffset: component image node offset | |
545 | * @arch: pointer to the uint8_t, will hold arch numeric id | |
546 | * | |
547 | * fit_image_get_arch() finds arch property in a given component image node. | |
548 | * If the property is found, its (string) value is translated to the numeric | |
549 | * id which is returned to the caller. | |
550 | * | |
551 | * returns: | |
552 | * 0, on success | |
553 | * -1, on failure | |
554 | */ | |
555 | int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) | |
556 | { | |
557 | int len; | |
558 | const void *data; | |
559 | ||
560 | /* Get architecture name from property data */ | |
561 | data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); | |
562 | if (data == NULL) { | |
563 | fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); | |
564 | *arch = -1; | |
565 | return -1; | |
566 | } | |
567 | ||
568 | /* Translate architecture name to id */ | |
569 | *arch = genimg_get_arch_id(data); | |
570 | return 0; | |
571 | } | |
572 | ||
573 | /** | |
574 | * fit_image_get_type - get type id for a given component image node | |
575 | * @fit: pointer to the FIT format image header | |
576 | * @noffset: component image node offset | |
577 | * @type: pointer to the uint8_t, will hold type numeric id | |
578 | * | |
579 | * fit_image_get_type() finds type property in a given component image node. | |
580 | * If the property is found, its (string) value is translated to the numeric | |
581 | * id which is returned to the caller. | |
582 | * | |
583 | * returns: | |
584 | * 0, on success | |
585 | * -1, on failure | |
586 | */ | |
587 | int fit_image_get_type(const void *fit, int noffset, uint8_t *type) | |
588 | { | |
589 | int len; | |
590 | const void *data; | |
591 | ||
592 | /* Get image type name from property data */ | |
593 | data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); | |
594 | if (data == NULL) { | |
595 | fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); | |
596 | *type = -1; | |
597 | return -1; | |
598 | } | |
599 | ||
600 | /* Translate image type name to id */ | |
601 | *type = genimg_get_type_id(data); | |
602 | return 0; | |
603 | } | |
604 | ||
605 | /** | |
606 | * fit_image_get_comp - get comp id for a given component image node | |
607 | * @fit: pointer to the FIT format image header | |
608 | * @noffset: component image node offset | |
609 | * @comp: pointer to the uint8_t, will hold comp numeric id | |
610 | * | |
611 | * fit_image_get_comp() finds comp property in a given component image node. | |
612 | * If the property is found, its (string) value is translated to the numeric | |
613 | * id which is returned to the caller. | |
614 | * | |
615 | * returns: | |
616 | * 0, on success | |
617 | * -1, on failure | |
618 | */ | |
619 | int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) | |
620 | { | |
621 | int len; | |
622 | const void *data; | |
623 | ||
624 | /* Get compression name from property data */ | |
625 | data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); | |
626 | if (data == NULL) { | |
627 | fit_get_debug(fit, noffset, FIT_COMP_PROP, len); | |
628 | *comp = -1; | |
629 | return -1; | |
630 | } | |
631 | ||
632 | /* Translate compression name to id */ | |
633 | *comp = genimg_get_comp_id(data); | |
634 | return 0; | |
635 | } | |
636 | ||
637 | /** | |
638 | * fit_image_get_load() - get load addr property for given component image node | |
639 | * @fit: pointer to the FIT format image header | |
640 | * @noffset: component image node offset | |
641 | * @load: pointer to the uint32_t, will hold load address | |
642 | * | |
643 | * fit_image_get_load() finds load address property in a given component | |
644 | * image node. If the property is found, its value is returned to the caller. | |
645 | * | |
646 | * returns: | |
647 | * 0, on success | |
648 | * -1, on failure | |
649 | */ | |
650 | int fit_image_get_load(const void *fit, int noffset, ulong *load) | |
651 | { | |
652 | int len; | |
653 | const uint32_t *data; | |
654 | ||
655 | data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len); | |
656 | if (data == NULL) { | |
657 | fit_get_debug(fit, noffset, FIT_LOAD_PROP, len); | |
658 | return -1; | |
659 | } | |
660 | ||
661 | *load = uimage_to_cpu(*data); | |
662 | return 0; | |
663 | } | |
664 | ||
665 | /** | |
666 | * fit_image_get_entry() - get entry point address property | |
667 | * @fit: pointer to the FIT format image header | |
668 | * @noffset: component image node offset | |
669 | * @entry: pointer to the uint32_t, will hold entry point address | |
670 | * | |
671 | * This gets the entry point address property for a given component image | |
672 | * node. | |
673 | * | |
674 | * fit_image_get_entry() finds entry point address property in a given | |
675 | * component image node. If the property is found, its value is returned | |
676 | * to the caller. | |
677 | * | |
678 | * returns: | |
679 | * 0, on success | |
680 | * -1, on failure | |
681 | */ | |
682 | int fit_image_get_entry(const void *fit, int noffset, ulong *entry) | |
683 | { | |
684 | int len; | |
685 | const uint32_t *data; | |
686 | ||
687 | data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len); | |
688 | if (data == NULL) { | |
689 | fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len); | |
690 | return -1; | |
691 | } | |
692 | ||
693 | *entry = uimage_to_cpu(*data); | |
694 | return 0; | |
695 | } | |
696 | ||
697 | /** | |
698 | * fit_image_get_data - get data property and its size for a given component image node | |
699 | * @fit: pointer to the FIT format image header | |
700 | * @noffset: component image node offset | |
701 | * @data: double pointer to void, will hold data property's data address | |
702 | * @size: pointer to size_t, will hold data property's data size | |
703 | * | |
704 | * fit_image_get_data() finds data property in a given component image node. | |
705 | * If the property is found its data start address and size are returned to | |
706 | * the caller. | |
707 | * | |
708 | * returns: | |
709 | * 0, on success | |
710 | * -1, on failure | |
711 | */ | |
712 | int fit_image_get_data(const void *fit, int noffset, | |
713 | const void **data, size_t *size) | |
714 | { | |
715 | int len; | |
716 | ||
717 | *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); | |
718 | if (*data == NULL) { | |
719 | fit_get_debug(fit, noffset, FIT_DATA_PROP, len); | |
720 | *size = 0; | |
721 | return -1; | |
722 | } | |
723 | ||
724 | *size = len; | |
725 | return 0; | |
726 | } | |
727 | ||
728 | /** | |
729 | * fit_image_hash_get_algo - get hash algorithm name | |
730 | * @fit: pointer to the FIT format image header | |
731 | * @noffset: hash node offset | |
732 | * @algo: double pointer to char, will hold pointer to the algorithm name | |
733 | * | |
734 | * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. | |
735 | * If the property is found its data start address is returned to the caller. | |
736 | * | |
737 | * returns: | |
738 | * 0, on success | |
739 | * -1, on failure | |
740 | */ | |
741 | int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) | |
742 | { | |
743 | int len; | |
744 | ||
745 | *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); | |
746 | if (*algo == NULL) { | |
747 | fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); | |
748 | return -1; | |
749 | } | |
750 | ||
751 | return 0; | |
752 | } | |
753 | ||
754 | /** | |
755 | * fit_image_hash_get_value - get hash value and length | |
756 | * @fit: pointer to the FIT format image header | |
757 | * @noffset: hash node offset | |
758 | * @value: double pointer to uint8_t, will hold address of a hash value data | |
759 | * @value_len: pointer to an int, will hold hash data length | |
760 | * | |
761 | * fit_image_hash_get_value() finds hash value property in a given hash node. | |
762 | * If the property is found its data start address and size are returned to | |
763 | * the caller. | |
764 | * | |
765 | * returns: | |
766 | * 0, on success | |
767 | * -1, on failure | |
768 | */ | |
769 | int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, | |
770 | int *value_len) | |
771 | { | |
772 | int len; | |
773 | ||
774 | *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); | |
775 | if (*value == NULL) { | |
776 | fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); | |
777 | *value_len = 0; | |
778 | return -1; | |
779 | } | |
780 | ||
781 | *value_len = len; | |
782 | return 0; | |
783 | } | |
784 | ||
53fbb7e8 SG |
785 | /** |
786 | * fit_image_hash_get_ignore - get hash ignore flag | |
787 | * @fit: pointer to the FIT format image header | |
788 | * @noffset: hash node offset | |
789 | * @ignore: pointer to an int, will hold hash ignore flag | |
790 | * | |
791 | * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. | |
792 | * If the property is found and non-zero, the hash algorithm is not verified by | |
793 | * u-boot automatically. | |
794 | * | |
795 | * returns: | |
796 | * 0, on ignore not found | |
797 | * value, on ignore found | |
798 | */ | |
ab9efc66 | 799 | static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) |
53fbb7e8 SG |
800 | { |
801 | int len; | |
802 | int *value; | |
803 | ||
804 | value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); | |
805 | if (value == NULL || len != sizeof(int)) | |
806 | *ignore = 0; | |
807 | else | |
808 | *ignore = *value; | |
809 | ||
810 | return 0; | |
811 | } | |
53fbb7e8 SG |
812 | |
813 | /** | |
814 | * fit_set_timestamp - set node timestamp property | |
815 | * @fit: pointer to the FIT format image header | |
816 | * @noffset: node offset | |
817 | * @timestamp: timestamp value to be set | |
818 | * | |
819 | * fit_set_timestamp() attempts to set timestamp property in the requested | |
820 | * node and returns operation status to the caller. | |
821 | * | |
822 | * returns: | |
823 | * 0, on success | |
824 | * -1, on property read failure | |
825 | */ | |
826 | int fit_set_timestamp(void *fit, int noffset, time_t timestamp) | |
827 | { | |
828 | uint32_t t; | |
829 | int ret; | |
830 | ||
831 | t = cpu_to_uimage(timestamp); | |
832 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, | |
833 | sizeof(uint32_t)); | |
834 | if (ret) { | |
835 | printf("Can't set '%s' property for '%s' node (%s)\n", | |
836 | FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), | |
837 | fdt_strerror(ret)); | |
838 | return -1; | |
839 | } | |
840 | ||
841 | return 0; | |
842 | } | |
843 | ||
844 | /** | |
845 | * calculate_hash - calculate and return hash for provided input data | |
846 | * @data: pointer to the input data | |
847 | * @data_len: data length | |
848 | * @algo: requested hash algorithm | |
849 | * @value: pointer to the char, will hold hash value data (caller must | |
850 | * allocate enough free space) | |
851 | * value_len: length of the calculated hash | |
852 | * | |
853 | * calculate_hash() computes input data hash according to the requested | |
854 | * algorithm. | |
855 | * Resulting hash value is placed in caller provided 'value' buffer, length | |
856 | * of the calculated hash is returned via value_len pointer argument. | |
857 | * | |
858 | * returns: | |
859 | * 0, on success | |
860 | * -1, when algo is unsupported | |
861 | */ | |
604f23dd | 862 | int calculate_hash(const void *data, int data_len, const char *algo, |
53fbb7e8 SG |
863 | uint8_t *value, int *value_len) |
864 | { | |
87ebee39 | 865 | if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { |
53fbb7e8 SG |
866 | *((uint32_t *)value) = crc32_wd(0, data, data_len, |
867 | CHUNKSZ_CRC32); | |
868 | *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); | |
869 | *value_len = 4; | |
87ebee39 | 870 | } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { |
53fbb7e8 SG |
871 | sha1_csum_wd((unsigned char *)data, data_len, |
872 | (unsigned char *)value, CHUNKSZ_SHA1); | |
873 | *value_len = 20; | |
87ebee39 | 874 | } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { |
53fbb7e8 SG |
875 | md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); |
876 | *value_len = 16; | |
877 | } else { | |
878 | debug("Unsupported hash alogrithm\n"); | |
879 | return -1; | |
880 | } | |
881 | return 0; | |
882 | } | |
883 | ||
ab9efc66 SG |
884 | static int fit_image_check_hash(const void *fit, int noffset, const void *data, |
885 | size_t size, char **err_msgp) | |
886 | { | |
887 | uint8_t value[FIT_MAX_HASH_LEN]; | |
888 | int value_len; | |
889 | char *algo; | |
890 | uint8_t *fit_value; | |
891 | int fit_value_len; | |
892 | int ignore; | |
893 | ||
894 | *err_msgp = NULL; | |
895 | ||
896 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { | |
e754da2a | 897 | *err_msgp = "Can't get hash algo property"; |
ab9efc66 SG |
898 | return -1; |
899 | } | |
900 | printf("%s", algo); | |
901 | ||
902 | if (IMAGE_ENABLE_IGNORE) { | |
903 | fit_image_hash_get_ignore(fit, noffset, &ignore); | |
904 | if (ignore) { | |
905 | printf("-skipped "); | |
906 | return 0; | |
907 | } | |
908 | } | |
909 | ||
910 | if (fit_image_hash_get_value(fit, noffset, &fit_value, | |
911 | &fit_value_len)) { | |
e754da2a | 912 | *err_msgp = "Can't get hash value property"; |
ab9efc66 SG |
913 | return -1; |
914 | } | |
915 | ||
916 | if (calculate_hash(data, size, algo, value, &value_len)) { | |
e754da2a | 917 | *err_msgp = "Unsupported hash algorithm"; |
ab9efc66 SG |
918 | return -1; |
919 | } | |
920 | ||
921 | if (value_len != fit_value_len) { | |
e754da2a | 922 | *err_msgp = "Bad hash value len"; |
ab9efc66 SG |
923 | return -1; |
924 | } else if (memcmp(value, fit_value, value_len) != 0) { | |
e754da2a | 925 | *err_msgp = "Bad hash value"; |
ab9efc66 SG |
926 | return -1; |
927 | } | |
928 | ||
929 | return 0; | |
930 | } | |
931 | ||
53fbb7e8 | 932 | /** |
b8da8366 | 933 | * fit_image_verify - verify data intergity |
53fbb7e8 SG |
934 | * @fit: pointer to the FIT format image header |
935 | * @image_noffset: component image node offset | |
936 | * | |
b8da8366 | 937 | * fit_image_verify() goes over component image hash nodes, |
53fbb7e8 SG |
938 | * re-calculates each data hash and compares with the value stored in hash |
939 | * node. | |
940 | * | |
941 | * returns: | |
942 | * 1, if all hashes are valid | |
943 | * 0, otherwise (or on error) | |
944 | */ | |
b8da8366 | 945 | int fit_image_verify(const void *fit, int image_noffset) |
53fbb7e8 SG |
946 | { |
947 | const void *data; | |
948 | size_t size; | |
56518e71 | 949 | int noffset = 0; |
53fbb7e8 | 950 | char *err_msg = ""; |
56518e71 SG |
951 | int verify_all = 1; |
952 | int ret; | |
53fbb7e8 SG |
953 | |
954 | /* Get image data and data length */ | |
955 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { | |
e754da2a | 956 | err_msg = "Can't get image data/size"; |
56518e71 SG |
957 | goto error; |
958 | } | |
959 | ||
960 | /* Verify all required signatures */ | |
961 | if (IMAGE_ENABLE_VERIFY && | |
962 | fit_image_verify_required_sigs(fit, image_noffset, data, size, | |
963 | gd_fdt_blob(), &verify_all)) { | |
964 | err_msg = "Unable to verify required signature"; | |
965 | goto error; | |
53fbb7e8 SG |
966 | } |
967 | ||
968 | /* Process all hash subnodes of the component image node */ | |
ab9efc66 SG |
969 | for (noffset = fdt_first_subnode(fit, image_noffset); |
970 | noffset >= 0; | |
971 | noffset = fdt_next_subnode(fit, noffset)) { | |
972 | const char *name = fit_get_name(fit, noffset, NULL); | |
973 | ||
974 | /* | |
975 | * Check subnode name, must be equal to "hash". | |
976 | * Multiple hash nodes require unique unit node | |
977 | * names, e.g. hash@1, hash@2, etc. | |
978 | */ | |
979 | if (!strncmp(name, FIT_HASH_NODENAME, | |
980 | strlen(FIT_HASH_NODENAME))) { | |
981 | if (fit_image_check_hash(fit, noffset, data, size, | |
982 | &err_msg)) | |
53fbb7e8 | 983 | goto error; |
ab9efc66 | 984 | puts("+ "); |
56518e71 SG |
985 | } else if (IMAGE_ENABLE_VERIFY && verify_all && |
986 | !strncmp(name, FIT_SIG_NODENAME, | |
987 | strlen(FIT_SIG_NODENAME))) { | |
988 | ret = fit_image_check_sig(fit, noffset, data, | |
989 | size, -1, &err_msg); | |
990 | if (ret) | |
991 | puts("- "); | |
992 | else | |
993 | puts("+ "); | |
53fbb7e8 SG |
994 | } |
995 | } | |
996 | ||
997 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { | |
e754da2a | 998 | err_msg = "Corrupted or truncated tree"; |
53fbb7e8 SG |
999 | goto error; |
1000 | } | |
1001 | ||
1002 | return 1; | |
1003 | ||
1004 | error: | |
e754da2a | 1005 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
53fbb7e8 SG |
1006 | err_msg, fit_get_name(fit, noffset, NULL), |
1007 | fit_get_name(fit, image_noffset, NULL)); | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | /** | |
b8da8366 | 1012 | * fit_all_image_verify - verify data intergity for all images |
53fbb7e8 SG |
1013 | * @fit: pointer to the FIT format image header |
1014 | * | |
b8da8366 | 1015 | * fit_all_image_verify() goes over all images in the FIT and |
53fbb7e8 SG |
1016 | * for every images checks if all it's hashes are valid. |
1017 | * | |
1018 | * returns: | |
1019 | * 1, if all hashes of all images are valid | |
1020 | * 0, otherwise (or on error) | |
1021 | */ | |
b8da8366 | 1022 | int fit_all_image_verify(const void *fit) |
53fbb7e8 SG |
1023 | { |
1024 | int images_noffset; | |
1025 | int noffset; | |
1026 | int ndepth; | |
1027 | int count; | |
1028 | ||
1029 | /* Find images parent node offset */ | |
1030 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
1031 | if (images_noffset < 0) { | |
1032 | printf("Can't find images parent node '%s' (%s)\n", | |
1033 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); | |
1034 | return 0; | |
1035 | } | |
1036 | ||
1037 | /* Process all image subnodes, check hashes for each */ | |
1038 | printf("## Checking hash(es) for FIT Image at %08lx ...\n", | |
1039 | (ulong)fit); | |
1040 | for (ndepth = 0, count = 0, | |
1041 | noffset = fdt_next_node(fit, images_noffset, &ndepth); | |
1042 | (noffset >= 0) && (ndepth > 0); | |
1043 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
1044 | if (ndepth == 1) { | |
1045 | /* | |
1046 | * Direct child node of the images parent node, | |
1047 | * i.e. component image node. | |
1048 | */ | |
1049 | printf(" Hash(es) for Image %u (%s): ", count++, | |
1050 | fit_get_name(fit, noffset, NULL)); | |
1051 | ||
b8da8366 | 1052 | if (!fit_image_verify(fit, noffset)) |
53fbb7e8 SG |
1053 | return 0; |
1054 | printf("\n"); | |
1055 | } | |
1056 | } | |
1057 | return 1; | |
1058 | } | |
1059 | ||
1060 | /** | |
1061 | * fit_image_check_os - check whether image node is of a given os type | |
1062 | * @fit: pointer to the FIT format image header | |
1063 | * @noffset: component image node offset | |
1064 | * @os: requested image os | |
1065 | * | |
1066 | * fit_image_check_os() reads image os property and compares its numeric | |
1067 | * id with the requested os. Comparison result is returned to the caller. | |
1068 | * | |
1069 | * returns: | |
1070 | * 1 if image is of given os type | |
1071 | * 0 otherwise (or on error) | |
1072 | */ | |
1073 | int fit_image_check_os(const void *fit, int noffset, uint8_t os) | |
1074 | { | |
1075 | uint8_t image_os; | |
1076 | ||
1077 | if (fit_image_get_os(fit, noffset, &image_os)) | |
1078 | return 0; | |
1079 | return (os == image_os); | |
1080 | } | |
1081 | ||
1082 | /** | |
1083 | * fit_image_check_arch - check whether image node is of a given arch | |
1084 | * @fit: pointer to the FIT format image header | |
1085 | * @noffset: component image node offset | |
1086 | * @arch: requested imagearch | |
1087 | * | |
1088 | * fit_image_check_arch() reads image arch property and compares its numeric | |
1089 | * id with the requested arch. Comparison result is returned to the caller. | |
1090 | * | |
1091 | * returns: | |
1092 | * 1 if image is of given arch | |
1093 | * 0 otherwise (or on error) | |
1094 | */ | |
1095 | int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) | |
1096 | { | |
1097 | uint8_t image_arch; | |
1098 | ||
1099 | if (fit_image_get_arch(fit, noffset, &image_arch)) | |
1100 | return 0; | |
1101 | return (arch == image_arch); | |
1102 | } | |
1103 | ||
1104 | /** | |
1105 | * fit_image_check_type - check whether image node is of a given type | |
1106 | * @fit: pointer to the FIT format image header | |
1107 | * @noffset: component image node offset | |
1108 | * @type: requested image type | |
1109 | * | |
1110 | * fit_image_check_type() reads image type property and compares its numeric | |
1111 | * id with the requested type. Comparison result is returned to the caller. | |
1112 | * | |
1113 | * returns: | |
1114 | * 1 if image is of given type | |
1115 | * 0 otherwise (or on error) | |
1116 | */ | |
1117 | int fit_image_check_type(const void *fit, int noffset, uint8_t type) | |
1118 | { | |
1119 | uint8_t image_type; | |
1120 | ||
1121 | if (fit_image_get_type(fit, noffset, &image_type)) | |
1122 | return 0; | |
1123 | return (type == image_type); | |
1124 | } | |
1125 | ||
1126 | /** | |
1127 | * fit_image_check_comp - check whether image node uses given compression | |
1128 | * @fit: pointer to the FIT format image header | |
1129 | * @noffset: component image node offset | |
1130 | * @comp: requested image compression type | |
1131 | * | |
1132 | * fit_image_check_comp() reads image compression property and compares its | |
1133 | * numeric id with the requested compression type. Comparison result is | |
1134 | * returned to the caller. | |
1135 | * | |
1136 | * returns: | |
1137 | * 1 if image uses requested compression | |
1138 | * 0 otherwise (or on error) | |
1139 | */ | |
1140 | int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) | |
1141 | { | |
1142 | uint8_t image_comp; | |
1143 | ||
1144 | if (fit_image_get_comp(fit, noffset, &image_comp)) | |
1145 | return 0; | |
1146 | return (comp == image_comp); | |
1147 | } | |
1148 | ||
1149 | /** | |
1150 | * fit_check_format - sanity check FIT image format | |
1151 | * @fit: pointer to the FIT format image header | |
1152 | * | |
1153 | * fit_check_format() runs a basic sanity FIT image verification. | |
1154 | * Routine checks for mandatory properties, nodes, etc. | |
1155 | * | |
1156 | * returns: | |
1157 | * 1, on success | |
1158 | * 0, on failure | |
1159 | */ | |
1160 | int fit_check_format(const void *fit) | |
1161 | { | |
1162 | /* mandatory / node 'description' property */ | |
1163 | if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { | |
1164 | debug("Wrong FIT format: no description\n"); | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | if (IMAGE_ENABLE_TIMESTAMP) { | |
1169 | /* mandatory / node 'timestamp' property */ | |
1170 | if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { | |
1171 | debug("Wrong FIT format: no timestamp\n"); | |
1172 | return 0; | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /* mandatory subimages parent '/images' node */ | |
1177 | if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { | |
1178 | debug("Wrong FIT format: no images parent node\n"); | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | return 1; | |
1183 | } | |
1184 | ||
1185 | ||
1186 | /** | |
1187 | * fit_conf_find_compat | |
1188 | * @fit: pointer to the FIT format image header | |
1189 | * @fdt: pointer to the device tree to compare against | |
1190 | * | |
1191 | * fit_conf_find_compat() attempts to find the configuration whose fdt is the | |
1192 | * most compatible with the passed in device tree. | |
1193 | * | |
1194 | * Example: | |
1195 | * | |
1196 | * / o image-tree | |
1197 | * |-o images | |
1198 | * | |-o fdt@1 | |
1199 | * | |-o fdt@2 | |
1200 | * | | |
1201 | * |-o configurations | |
1202 | * |-o config@1 | |
1203 | * | |-fdt = fdt@1 | |
1204 | * | | |
1205 | * |-o config@2 | |
1206 | * |-fdt = fdt@2 | |
1207 | * | |
1208 | * / o U-Boot fdt | |
1209 | * |-compatible = "foo,bar", "bim,bam" | |
1210 | * | |
1211 | * / o kernel fdt1 | |
1212 | * |-compatible = "foo,bar", | |
1213 | * | |
1214 | * / o kernel fdt2 | |
1215 | * |-compatible = "bim,bam", "baz,biz" | |
1216 | * | |
1217 | * Configuration 1 would be picked because the first string in U-Boot's | |
1218 | * compatible list, "foo,bar", matches a compatible string in the root of fdt1. | |
1219 | * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. | |
1220 | * | |
1221 | * returns: | |
1222 | * offset to the configuration to use if one was found | |
1223 | * -1 otherwise | |
1224 | */ | |
1225 | int fit_conf_find_compat(const void *fit, const void *fdt) | |
1226 | { | |
1227 | int ndepth = 0; | |
1228 | int noffset, confs_noffset, images_noffset; | |
1229 | const void *fdt_compat; | |
1230 | int fdt_compat_len; | |
1231 | int best_match_offset = 0; | |
1232 | int best_match_pos = 0; | |
1233 | ||
1234 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); | |
1235 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
1236 | if (confs_noffset < 0 || images_noffset < 0) { | |
1237 | debug("Can't find configurations or images nodes.\n"); | |
1238 | return -1; | |
1239 | } | |
1240 | ||
1241 | fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); | |
1242 | if (!fdt_compat) { | |
1243 | debug("Fdt for comparison has no \"compatible\" property.\n"); | |
1244 | return -1; | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * Loop over the configurations in the FIT image. | |
1249 | */ | |
1250 | for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); | |
1251 | (noffset >= 0) && (ndepth > 0); | |
1252 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
1253 | const void *kfdt; | |
1254 | const char *kfdt_name; | |
1255 | int kfdt_noffset; | |
1256 | const char *cur_fdt_compat; | |
1257 | int len; | |
1258 | size_t size; | |
1259 | int i; | |
1260 | ||
1261 | if (ndepth > 1) | |
1262 | continue; | |
1263 | ||
1264 | kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); | |
1265 | if (!kfdt_name) { | |
1266 | debug("No fdt property found.\n"); | |
1267 | continue; | |
1268 | } | |
1269 | kfdt_noffset = fdt_subnode_offset(fit, images_noffset, | |
1270 | kfdt_name); | |
1271 | if (kfdt_noffset < 0) { | |
1272 | debug("No image node named \"%s\" found.\n", | |
1273 | kfdt_name); | |
1274 | continue; | |
1275 | } | |
1276 | /* | |
1277 | * Get a pointer to this configuration's fdt. | |
1278 | */ | |
1279 | if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { | |
1280 | debug("Failed to get fdt \"%s\".\n", kfdt_name); | |
1281 | continue; | |
1282 | } | |
1283 | ||
1284 | len = fdt_compat_len; | |
1285 | cur_fdt_compat = fdt_compat; | |
1286 | /* | |
1287 | * Look for a match for each U-Boot compatibility string in | |
1288 | * turn in this configuration's fdt. | |
1289 | */ | |
1290 | for (i = 0; len > 0 && | |
1291 | (!best_match_offset || best_match_pos > i); i++) { | |
1292 | int cur_len = strlen(cur_fdt_compat) + 1; | |
1293 | ||
1294 | if (!fdt_node_check_compatible(kfdt, 0, | |
1295 | cur_fdt_compat)) { | |
1296 | best_match_offset = noffset; | |
1297 | best_match_pos = i; | |
1298 | break; | |
1299 | } | |
1300 | len -= cur_len; | |
1301 | cur_fdt_compat += cur_len; | |
1302 | } | |
1303 | } | |
1304 | if (!best_match_offset) { | |
1305 | debug("No match found.\n"); | |
1306 | return -1; | |
1307 | } | |
1308 | ||
1309 | return best_match_offset; | |
1310 | } | |
1311 | ||
1312 | /** | |
1313 | * fit_conf_get_node - get node offset for configuration of a given unit name | |
1314 | * @fit: pointer to the FIT format image header | |
1315 | * @conf_uname: configuration node unit name | |
1316 | * | |
1317 | * fit_conf_get_node() finds a configuration (withing the '/configurations' | |
1318 | * parant node) of a provided unit name. If configuration is found its node | |
1319 | * offset is returned to the caller. | |
1320 | * | |
1321 | * When NULL is provided in second argument fit_conf_get_node() will search | |
1322 | * for a default configuration node instead. Default configuration node unit | |
1323 | * name is retrived from FIT_DEFAULT_PROP property of the '/configurations' | |
1324 | * node. | |
1325 | * | |
1326 | * returns: | |
1327 | * configuration node offset when found (>=0) | |
1328 | * negative number on failure (FDT_ERR_* code) | |
1329 | */ | |
1330 | int fit_conf_get_node(const void *fit, const char *conf_uname) | |
1331 | { | |
1332 | int noffset, confs_noffset; | |
1333 | int len; | |
1334 | ||
1335 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); | |
1336 | if (confs_noffset < 0) { | |
1337 | debug("Can't find configurations parent node '%s' (%s)\n", | |
1338 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); | |
1339 | return confs_noffset; | |
1340 | } | |
1341 | ||
1342 | if (conf_uname == NULL) { | |
1343 | /* get configuration unit name from the default property */ | |
1344 | debug("No configuration specified, trying default...\n"); | |
1345 | conf_uname = (char *)fdt_getprop(fit, confs_noffset, | |
1346 | FIT_DEFAULT_PROP, &len); | |
1347 | if (conf_uname == NULL) { | |
1348 | fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, | |
1349 | len); | |
1350 | return len; | |
1351 | } | |
1352 | debug("Found default configuration: '%s'\n", conf_uname); | |
1353 | } | |
1354 | ||
1355 | noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); | |
1356 | if (noffset < 0) { | |
1357 | debug("Can't get node offset for configuration unit name: '%s' (%s)\n", | |
1358 | conf_uname, fdt_strerror(noffset)); | |
1359 | } | |
1360 | ||
1361 | return noffset; | |
1362 | } | |
1363 | ||
003efd7d | 1364 | int fit_conf_get_prop_node(const void *fit, int noffset, |
53fbb7e8 SG |
1365 | const char *prop_name) |
1366 | { | |
1367 | char *uname; | |
1368 | int len; | |
1369 | ||
1370 | /* get kernel image unit name from configuration kernel property */ | |
1371 | uname = (char *)fdt_getprop(fit, noffset, prop_name, &len); | |
1372 | if (uname == NULL) | |
1373 | return len; | |
1374 | ||
1375 | return fit_image_get_node(fit, uname); | |
1376 | } | |
1377 | ||
53fbb7e8 SG |
1378 | /** |
1379 | * fit_conf_print - prints out the FIT configuration details | |
1380 | * @fit: pointer to the FIT format image header | |
1381 | * @noffset: offset of the configuration node | |
1382 | * @p: pointer to prefix string | |
1383 | * | |
1384 | * fit_conf_print() lists all mandatory properies for the processed | |
1385 | * configuration node. | |
1386 | * | |
1387 | * returns: | |
1388 | * no returned results | |
1389 | */ | |
1390 | void fit_conf_print(const void *fit, int noffset, const char *p) | |
1391 | { | |
1392 | char *desc; | |
1393 | char *uname; | |
1394 | int ret; | |
1395 | ||
1396 | /* Mandatory properties */ | |
1397 | ret = fit_get_desc(fit, noffset, &desc); | |
1398 | printf("%s Description: ", p); | |
1399 | if (ret) | |
1400 | printf("unavailable\n"); | |
1401 | else | |
1402 | printf("%s\n", desc); | |
1403 | ||
1404 | uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); | |
1405 | printf("%s Kernel: ", p); | |
1406 | if (uname == NULL) | |
1407 | printf("unavailable\n"); | |
1408 | else | |
1409 | printf("%s\n", uname); | |
1410 | ||
1411 | /* Optional properties */ | |
1412 | uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); | |
1413 | if (uname) | |
1414 | printf("%s Init Ramdisk: %s\n", p, uname); | |
1415 | ||
1416 | uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL); | |
1417 | if (uname) | |
1418 | printf("%s FDT: %s\n", p, uname); | |
1419 | } | |
1420 | ||
782cfbb2 SG |
1421 | int fit_image_select(const void *fit, int rd_noffset, int verify) |
1422 | { | |
1423 | fit_image_print(fit, rd_noffset, " "); | |
1424 | ||
1425 | if (verify) { | |
1426 | puts(" Verifying Hash Integrity ... "); | |
1427 | if (!fit_image_verify(fit, rd_noffset)) { | |
1428 | puts("Bad Data Hash\n"); | |
1429 | return -EACCES; | |
1430 | } | |
1431 | puts("OK\n"); | |
1432 | } | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
782cfbb2 SG |
1437 | int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, |
1438 | ulong addr) | |
1439 | { | |
1440 | int cfg_noffset; | |
1441 | void *fit_hdr; | |
1442 | int noffset; | |
1443 | ||
1444 | debug("* %s: using config '%s' from image at 0x%08lx\n", | |
1445 | prop_name, images->fit_uname_cfg, addr); | |
1446 | ||
1447 | /* Check whether configuration has this property defined */ | |
1448 | fit_hdr = map_sysmem(addr, 0); | |
1449 | cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); | |
1450 | if (cfg_noffset < 0) { | |
1451 | debug("* %s: no such config\n", prop_name); | |
1452 | return -ENOENT; | |
1453 | } | |
1454 | ||
1455 | noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); | |
1456 | if (noffset < 0) { | |
1457 | debug("* %s: no '%s' in config\n", prop_name, prop_name); | |
1458 | return -ENOLINK; | |
1459 | } | |
1460 | ||
1461 | return noffset; | |
1462 | } | |
1463 | ||
1464 | int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr, | |
f320a4d8 | 1465 | const char **fit_unamep, const char **fit_uname_configp, |
782cfbb2 SG |
1466 | int arch, int image_type, int bootstage_id, |
1467 | enum fit_load_op load_op, ulong *datap, ulong *lenp) | |
1468 | { | |
1469 | int cfg_noffset, noffset; | |
1470 | const char *fit_uname; | |
f320a4d8 | 1471 | const char *fit_uname_config; |
782cfbb2 SG |
1472 | const void *fit; |
1473 | const void *buf; | |
1474 | size_t size; | |
1475 | int type_ok, os_ok; | |
1476 | ulong load, data, len; | |
1477 | int ret; | |
1478 | ||
1479 | fit = map_sysmem(addr, 0); | |
1480 | fit_uname = fit_unamep ? *fit_unamep : NULL; | |
f320a4d8 | 1481 | fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; |
782cfbb2 SG |
1482 | printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); |
1483 | ||
1484 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); | |
1485 | if (!fit_check_format(fit)) { | |
1486 | printf("Bad FIT %s image format!\n", prop_name); | |
1487 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); | |
1488 | return -ENOEXEC; | |
1489 | } | |
1490 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); | |
1491 | if (fit_uname) { | |
1492 | /* get ramdisk component image node offset */ | |
1493 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); | |
1494 | noffset = fit_image_get_node(fit, fit_uname); | |
1495 | } else { | |
1496 | /* | |
1497 | * no image node unit name, try to get config | |
1498 | * node first. If config unit node name is NULL | |
1499 | * fit_conf_get_node() will try to find default config node | |
1500 | */ | |
1501 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); | |
1502 | if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { | |
1503 | cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); | |
1504 | } else { | |
1505 | cfg_noffset = fit_conf_get_node(fit, | |
1506 | fit_uname_config); | |
1507 | } | |
1508 | if (cfg_noffset < 0) { | |
1509 | puts("Could not find configuration node\n"); | |
1510 | bootstage_error(bootstage_id + | |
1511 | BOOTSTAGE_SUB_NO_UNIT_NAME); | |
1512 | return -ENOENT; | |
1513 | } | |
1514 | fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL); | |
1515 | printf(" Using '%s' configuration\n", fit_uname_config); | |
1516 | if (image_type == IH_TYPE_KERNEL) { | |
1517 | /* Remember (and possibly verify) this config */ | |
1518 | images->fit_uname_cfg = fit_uname_config; | |
1519 | if (IMAGE_ENABLE_VERIFY && images->verify) { | |
1520 | puts(" Verifying Hash Integrity ... "); | |
1521 | if (!fit_config_verify(fit, cfg_noffset)) { | |
1522 | puts("Bad Data Hash\n"); | |
1523 | bootstage_error(bootstage_id + | |
1524 | BOOTSTAGE_SUB_HASH); | |
1525 | return -EACCES; | |
1526 | } | |
1527 | puts("OK\n"); | |
1528 | } | |
1529 | bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); | |
1530 | } | |
1531 | ||
1532 | noffset = fit_conf_get_prop_node(fit, cfg_noffset, | |
1533 | prop_name); | |
1534 | fit_uname = fit_get_name(fit, noffset, NULL); | |
1535 | } | |
1536 | if (noffset < 0) { | |
1537 | puts("Could not find subimage node\n"); | |
1538 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); | |
1539 | return -ENOENT; | |
1540 | } | |
1541 | ||
1542 | printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); | |
1543 | ||
1544 | ret = fit_image_select(fit, noffset, images->verify); | |
1545 | if (ret) { | |
1546 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); | |
1547 | return ret; | |
1548 | } | |
1549 | ||
1550 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); | |
1551 | if (!fit_image_check_target_arch(fit, noffset)) { | |
1552 | puts("Unsupported Architecture\n"); | |
1553 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); | |
1554 | return -ENOEXEC; | |
1555 | } | |
1556 | ||
1557 | if (image_type == IH_TYPE_FLATDT && | |
1558 | !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { | |
1559 | puts("FDT image is compressed"); | |
1560 | return -EPROTONOSUPPORT; | |
1561 | } | |
1562 | ||
1563 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); | |
1564 | type_ok = fit_image_check_type(fit, noffset, image_type) || | |
1565 | (image_type == IH_TYPE_KERNEL && | |
1566 | fit_image_check_type(fit, noffset, | |
1567 | IH_TYPE_KERNEL_NOLOAD)); | |
1568 | os_ok = image_type == IH_TYPE_FLATDT || | |
1569 | fit_image_check_os(fit, noffset, IH_OS_LINUX); | |
1570 | if (!type_ok || !os_ok) { | |
1571 | printf("No Linux %s %s Image\n", genimg_get_arch_name(arch), | |
1572 | genimg_get_type_name(image_type)); | |
1573 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); | |
1574 | return -EIO; | |
1575 | } | |
1576 | ||
1577 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); | |
1578 | ||
1579 | /* get image data address and length */ | |
1580 | if (fit_image_get_data(fit, noffset, &buf, &size)) { | |
1581 | printf("Could not find %s subimage data!\n", prop_name); | |
1582 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); | |
2f998071 | 1583 | return -ENOENT; |
782cfbb2 SG |
1584 | } |
1585 | len = (ulong)size; | |
1586 | ||
1587 | /* verify that image data is a proper FDT blob */ | |
1588 | if (image_type == IH_TYPE_FLATDT && fdt_check_header((char *)buf)) { | |
1589 | puts("Subimage data is not a FDT"); | |
1590 | return -ENOEXEC; | |
1591 | } | |
1592 | ||
1593 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); | |
1594 | ||
1595 | /* | |
1596 | * Work-around for eldk-4.2 which gives this warning if we try to | |
1597 | * case in the unmap_sysmem() call: | |
1598 | * warning: initialization discards qualifiers from pointer target type | |
1599 | */ | |
1600 | { | |
1601 | void *vbuf = (void *)buf; | |
1602 | ||
1603 | data = map_to_sysmem(vbuf); | |
1604 | } | |
1605 | ||
1606 | if (load_op == FIT_LOAD_IGNORED) { | |
1607 | /* Don't load */ | |
1608 | } else if (fit_image_get_load(fit, noffset, &load)) { | |
1609 | if (load_op == FIT_LOAD_REQUIRED) { | |
1610 | printf("Can't get %s subimage load address!\n", | |
1611 | prop_name); | |
1612 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); | |
1613 | return -EBADF; | |
1614 | } | |
1615 | } else { | |
1616 | ulong image_start, image_end; | |
1617 | ulong load_end; | |
1618 | void *dst; | |
1619 | ||
1620 | /* | |
1621 | * move image data to the load address, | |
1622 | * make sure we don't overwrite initial image | |
1623 | */ | |
1624 | image_start = addr; | |
1625 | image_end = addr + fit_get_size(fit); | |
1626 | ||
1627 | load_end = load + len; | |
1628 | if (image_type != IH_TYPE_KERNEL && | |
1629 | load < image_end && load_end > image_start) { | |
1630 | printf("Error: %s overwritten\n", prop_name); | |
1631 | return -EXDEV; | |
1632 | } | |
1633 | ||
1634 | printf(" Loading %s from 0x%08lx to 0x%08lx\n", | |
1635 | prop_name, data, load); | |
1636 | ||
1637 | dst = map_sysmem(load, len); | |
1638 | memmove(dst, buf, len); | |
1639 | data = load; | |
1640 | } | |
1641 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); | |
1642 | ||
1643 | *datap = data; | |
1644 | *lenp = len; | |
1645 | if (fit_unamep) | |
1646 | *fit_unamep = (char *)fit_uname; | |
f320a4d8 SG |
1647 | if (fit_uname_configp) |
1648 | *fit_uname_configp = (char *)fit_uname_config; | |
782cfbb2 SG |
1649 | |
1650 | return noffset; | |
1651 | } |