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