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