]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/image-fit.c
Kconfig: Move CONFIG_FIT and related options to Kconfig
[people/ms/u-boot.git] / common / image-fit.c
CommitLineData
53fbb7e8
SG
1/*
2 * Copyright (c) 2013, Google Inc.
3 *
4 * (C) Copyright 2008 Semihalf
5 *
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
53fbb7e8
SG
10 */
11
12#ifdef USE_HOSTCC
13#include "mkimage.h"
14#include <image.h>
15#include <time.h>
16#else
17#include <common.h>
782cfbb2 18#include <errno.h>
0eb25b61 19#include <mapmem.h>
782cfbb2
SG
20#include <asm/io.h>
21DECLARE_GLOBAL_DATA_PTR;
53fbb7e8
SG
22#endif /* !USE_HOSTCC*/
23
24#include <bootstage.h>
53fbb7e8
SG
25#include <u-boot/crc.h>
26#include <u-boot/md5.h>
2b9912e6
JH
27#include <u-boot/sha1.h>
28#include <u-boot/sha256.h>
53fbb7e8
SG
29
30/*****************************************************************************/
31/* New uImage format routines */
32/*****************************************************************************/
33#ifndef USE_HOSTCC
34static 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 *
069d5945 63 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
53fbb7e8
SG
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 */
75int 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 *
069d5945 89 * fit_parse_subimage() expects subimage spec in the form of
53fbb7e8
SG
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 */
101int 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
108static 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
39931f96
GMF
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 */
124int 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
87ebee39 143#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
53fbb7e8
SG
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 */
156void 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
1fe7d938
SG
169 /* Indent string is defined in header image.h */
170 p = IMAGE_INDENT_STRING;
53fbb7e8
SG
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
d8b75360
SG
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
56518e71 250 * @type: Type of information to print ("hash" or "sign")
d8b75360
SG
251 *
252 * fit_image_print_data() lists properies for the processed hash node
253 *
56518e71
SG
254 * This function avoid using puts() since it prints a newline on the host
255 * but does not in U-Boot.
256 *
d8b75360
SG
257 * returns:
258 * no returned results
259 */
56518e71
SG
260static void fit_image_print_data(const void *fit, int noffset, const char *p,
261 const char *type)
d8b75360 262{
56518e71 263 const char *keyname;
d8b75360
SG
264 uint8_t *value;
265 int value_len;
56518e71
SG
266 char *algo;
267 int required;
268 int ret, i;
d8b75360 269
56518e71 270 debug("%s %s node: '%s'\n", p, type,
d8b75360 271 fit_get_name(fit, noffset, NULL));
56518e71 272 printf("%s %s algo: ", p, type);
d8b75360
SG
273 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
274 printf("invalid/unsupported\n");
275 return;
276 }
56518e71
SG
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");
d8b75360
SG
285
286 ret = fit_image_hash_get_value(fit, noffset, &value,
287 &value_len);
56518e71 288 printf("%s %s value: ", p, type);
d8b75360
SG
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
56518e71
SG
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 }
d8b75360
SG
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 */
322static 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);
56518e71
SG
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 }
d8b75360
SG
339}
340
53fbb7e8
SG
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 */
356void 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
1fd1e2f6
SG
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
53fbb7e8
SG
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);
c6ac13bd 396 if (ret) {
53fbb7e8 397 printf("unavailable\n");
c6ac13bd
SG
398 } else {
399 void *vdata = (void *)data;
400
401 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
402 }
53fbb7e8
SG
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 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 */
d8b75360 450 fit_image_print_verification_data(fit, noffset, p);
53fbb7e8
SG
451 }
452 }
453}
39931f96
GMF
454
455#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
53fbb7e8 456
53fbb7e8
SG
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 */
470int 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 */
498int 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 */
530int 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 */
564int 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 */
596int 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 */
628int 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 */
660int 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/**
679 * fit_image_get_load() - get load addr property for given component image node
680 * @fit: pointer to the FIT format image header
681 * @noffset: component image node offset
682 * @load: pointer to the uint32_t, will hold load address
683 *
684 * fit_image_get_load() finds load address property in a given component
685 * image node. If the property is found, its value is returned to the caller.
686 *
687 * returns:
688 * 0, on success
689 * -1, on failure
690 */
691int fit_image_get_load(const void *fit, int noffset, ulong *load)
692{
693 int len;
694 const uint32_t *data;
695
696 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
697 if (data == NULL) {
698 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
699 return -1;
700 }
701
702 *load = uimage_to_cpu(*data);
703 return 0;
704}
705
706/**
707 * fit_image_get_entry() - get entry point address property
708 * @fit: pointer to the FIT format image header
709 * @noffset: component image node offset
710 * @entry: pointer to the uint32_t, will hold entry point address
711 *
712 * This gets the entry point address property for a given component image
713 * node.
714 *
715 * fit_image_get_entry() finds entry point address property in a given
716 * component image node. If the property is found, its value is returned
717 * to the caller.
718 *
719 * returns:
720 * 0, on success
721 * -1, on failure
722 */
723int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
724{
725 int len;
726 const uint32_t *data;
727
728 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
729 if (data == NULL) {
730 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
731 return -1;
732 }
733
734 *entry = uimage_to_cpu(*data);
735 return 0;
736}
737
738/**
739 * fit_image_get_data - get data property and its size for a given component image node
740 * @fit: pointer to the FIT format image header
741 * @noffset: component image node offset
742 * @data: double pointer to void, will hold data property's data address
743 * @size: pointer to size_t, will hold data property's data size
744 *
745 * fit_image_get_data() finds data property in a given component image node.
746 * If the property is found its data start address and size are returned to
747 * the caller.
748 *
749 * returns:
750 * 0, on success
751 * -1, on failure
752 */
753int fit_image_get_data(const void *fit, int noffset,
754 const void **data, size_t *size)
755{
756 int len;
757
758 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
759 if (*data == NULL) {
760 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
761 *size = 0;
762 return -1;
763 }
764
765 *size = len;
766 return 0;
767}
768
769/**
770 * fit_image_hash_get_algo - get hash algorithm name
771 * @fit: pointer to the FIT format image header
772 * @noffset: hash node offset
773 * @algo: double pointer to char, will hold pointer to the algorithm name
774 *
775 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
776 * If the property is found its data start address is returned to the caller.
777 *
778 * returns:
779 * 0, on success
780 * -1, on failure
781 */
782int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
783{
784 int len;
785
786 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
787 if (*algo == NULL) {
788 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
789 return -1;
790 }
791
792 return 0;
793}
794
795/**
796 * fit_image_hash_get_value - get hash value and length
797 * @fit: pointer to the FIT format image header
798 * @noffset: hash node offset
799 * @value: double pointer to uint8_t, will hold address of a hash value data
800 * @value_len: pointer to an int, will hold hash data length
801 *
802 * fit_image_hash_get_value() finds hash value property in a given hash node.
803 * If the property is found its data start address and size are returned to
804 * the caller.
805 *
806 * returns:
807 * 0, on success
808 * -1, on failure
809 */
810int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
811 int *value_len)
812{
813 int len;
814
815 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
816 if (*value == NULL) {
817 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
818 *value_len = 0;
819 return -1;
820 }
821
822 *value_len = len;
823 return 0;
824}
825
53fbb7e8
SG
826/**
827 * fit_image_hash_get_ignore - get hash ignore flag
828 * @fit: pointer to the FIT format image header
829 * @noffset: hash node offset
830 * @ignore: pointer to an int, will hold hash ignore flag
831 *
832 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
833 * If the property is found and non-zero, the hash algorithm is not verified by
834 * u-boot automatically.
835 *
836 * returns:
837 * 0, on ignore not found
838 * value, on ignore found
839 */
ab9efc66 840static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
53fbb7e8
SG
841{
842 int len;
843 int *value;
844
845 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
846 if (value == NULL || len != sizeof(int))
847 *ignore = 0;
848 else
849 *ignore = *value;
850
851 return 0;
852}
53fbb7e8 853
7a80de46
SG
854ulong fit_get_end(const void *fit)
855{
856 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
857}
858
53fbb7e8
SG
859/**
860 * fit_set_timestamp - set node timestamp property
861 * @fit: pointer to the FIT format image header
862 * @noffset: node offset
863 * @timestamp: timestamp value to be set
864 *
865 * fit_set_timestamp() attempts to set timestamp property in the requested
866 * node and returns operation status to the caller.
867 *
868 * returns:
869 * 0, on success
4f427a42 870 * -ENOSPC if no space in device tree, -1 for other error
53fbb7e8
SG
871 */
872int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
873{
874 uint32_t t;
875 int ret;
876
877 t = cpu_to_uimage(timestamp);
878 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
879 sizeof(uint32_t));
880 if (ret) {
881 printf("Can't set '%s' property for '%s' node (%s)\n",
882 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
883 fdt_strerror(ret));
4f427a42 884 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
53fbb7e8
SG
885 }
886
887 return 0;
888}
889
890/**
891 * calculate_hash - calculate and return hash for provided input data
892 * @data: pointer to the input data
893 * @data_len: data length
894 * @algo: requested hash algorithm
895 * @value: pointer to the char, will hold hash value data (caller must
896 * allocate enough free space)
897 * value_len: length of the calculated hash
898 *
899 * calculate_hash() computes input data hash according to the requested
900 * algorithm.
901 * Resulting hash value is placed in caller provided 'value' buffer, length
902 * of the calculated hash is returned via value_len pointer argument.
903 *
904 * returns:
905 * 0, on success
906 * -1, when algo is unsupported
907 */
604f23dd 908int calculate_hash(const void *data, int data_len, const char *algo,
53fbb7e8
SG
909 uint8_t *value, int *value_len)
910{
87ebee39 911 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
53fbb7e8
SG
912 *((uint32_t *)value) = crc32_wd(0, data, data_len,
913 CHUNKSZ_CRC32);
914 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
915 *value_len = 4;
87ebee39 916 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
53fbb7e8
SG
917 sha1_csum_wd((unsigned char *)data, data_len,
918 (unsigned char *)value, CHUNKSZ_SHA1);
919 *value_len = 20;
2842c1c2
HS
920 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
921 sha256_csum_wd((unsigned char *)data, data_len,
922 (unsigned char *)value, CHUNKSZ_SHA256);
923 *value_len = SHA256_SUM_LEN;
87ebee39 924 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
53fbb7e8
SG
925 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
926 *value_len = 16;
927 } else {
928 debug("Unsupported hash alogrithm\n");
929 return -1;
930 }
931 return 0;
932}
933
ab9efc66
SG
934static int fit_image_check_hash(const void *fit, int noffset, const void *data,
935 size_t size, char **err_msgp)
936{
937 uint8_t value[FIT_MAX_HASH_LEN];
938 int value_len;
939 char *algo;
940 uint8_t *fit_value;
941 int fit_value_len;
942 int ignore;
943
944 *err_msgp = NULL;
945
946 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
e754da2a 947 *err_msgp = "Can't get hash algo property";
ab9efc66
SG
948 return -1;
949 }
950 printf("%s", algo);
951
952 if (IMAGE_ENABLE_IGNORE) {
953 fit_image_hash_get_ignore(fit, noffset, &ignore);
954 if (ignore) {
955 printf("-skipped ");
956 return 0;
957 }
958 }
959
960 if (fit_image_hash_get_value(fit, noffset, &fit_value,
961 &fit_value_len)) {
e754da2a 962 *err_msgp = "Can't get hash value property";
ab9efc66
SG
963 return -1;
964 }
965
966 if (calculate_hash(data, size, algo, value, &value_len)) {
e754da2a 967 *err_msgp = "Unsupported hash algorithm";
ab9efc66
SG
968 return -1;
969 }
970
971 if (value_len != fit_value_len) {
e754da2a 972 *err_msgp = "Bad hash value len";
ab9efc66
SG
973 return -1;
974 } else if (memcmp(value, fit_value, value_len) != 0) {
e754da2a 975 *err_msgp = "Bad hash value";
ab9efc66
SG
976 return -1;
977 }
978
979 return 0;
980}
981
53fbb7e8 982/**
b8da8366 983 * fit_image_verify - verify data intergity
53fbb7e8
SG
984 * @fit: pointer to the FIT format image header
985 * @image_noffset: component image node offset
986 *
b8da8366 987 * fit_image_verify() goes over component image hash nodes,
53fbb7e8
SG
988 * re-calculates each data hash and compares with the value stored in hash
989 * node.
990 *
991 * returns:
992 * 1, if all hashes are valid
993 * 0, otherwise (or on error)
994 */
b8da8366 995int fit_image_verify(const void *fit, int image_noffset)
53fbb7e8
SG
996{
997 const void *data;
998 size_t size;
56518e71 999 int noffset = 0;
53fbb7e8 1000 char *err_msg = "";
56518e71
SG
1001 int verify_all = 1;
1002 int ret;
53fbb7e8
SG
1003
1004 /* Get image data and data length */
1005 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
e754da2a 1006 err_msg = "Can't get image data/size";
56518e71
SG
1007 goto error;
1008 }
1009
1010 /* Verify all required signatures */
1011 if (IMAGE_ENABLE_VERIFY &&
1012 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1013 gd_fdt_blob(), &verify_all)) {
1014 err_msg = "Unable to verify required signature";
1015 goto error;
53fbb7e8
SG
1016 }
1017
1018 /* Process all hash subnodes of the component image node */
364ac5b5 1019 fdt_for_each_subnode(fit, noffset, image_noffset) {
ab9efc66
SG
1020 const char *name = fit_get_name(fit, noffset, NULL);
1021
1022 /*
1023 * Check subnode name, must be equal to "hash".
1024 * Multiple hash nodes require unique unit node
1025 * names, e.g. hash@1, hash@2, etc.
1026 */
1027 if (!strncmp(name, FIT_HASH_NODENAME,
1028 strlen(FIT_HASH_NODENAME))) {
1029 if (fit_image_check_hash(fit, noffset, data, size,
1030 &err_msg))
53fbb7e8 1031 goto error;
ab9efc66 1032 puts("+ ");
56518e71
SG
1033 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1034 !strncmp(name, FIT_SIG_NODENAME,
1035 strlen(FIT_SIG_NODENAME))) {
1036 ret = fit_image_check_sig(fit, noffset, data,
1037 size, -1, &err_msg);
2e33e761
SG
1038
1039 /*
1040 * Show an indication on failure, but do not return
1041 * an error. Only keys marked 'required' can cause
1042 * an image validation failure. See the call to
1043 * fit_image_verify_required_sigs() above.
1044 */
1045 if (ret)
56518e71
SG
1046 puts("- ");
1047 else
1048 puts("+ ");
53fbb7e8
SG
1049 }
1050 }
1051
1052 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
e754da2a 1053 err_msg = "Corrupted or truncated tree";
53fbb7e8
SG
1054 goto error;
1055 }
1056
1057 return 1;
1058
1059error:
e754da2a 1060 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
53fbb7e8
SG
1061 err_msg, fit_get_name(fit, noffset, NULL),
1062 fit_get_name(fit, image_noffset, NULL));
1063 return 0;
1064}
1065
1066/**
b8da8366 1067 * fit_all_image_verify - verify data intergity for all images
53fbb7e8
SG
1068 * @fit: pointer to the FIT format image header
1069 *
b8da8366 1070 * fit_all_image_verify() goes over all images in the FIT and
53fbb7e8
SG
1071 * for every images checks if all it's hashes are valid.
1072 *
1073 * returns:
1074 * 1, if all hashes of all images are valid
1075 * 0, otherwise (or on error)
1076 */
b8da8366 1077int fit_all_image_verify(const void *fit)
53fbb7e8
SG
1078{
1079 int images_noffset;
1080 int noffset;
1081 int ndepth;
1082 int count;
1083
1084 /* Find images parent node offset */
1085 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1086 if (images_noffset < 0) {
1087 printf("Can't find images parent node '%s' (%s)\n",
1088 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1089 return 0;
1090 }
1091
1092 /* Process all image subnodes, check hashes for each */
1093 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1094 (ulong)fit);
1095 for (ndepth = 0, count = 0,
1096 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1097 (noffset >= 0) && (ndepth > 0);
1098 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1099 if (ndepth == 1) {
1100 /*
1101 * Direct child node of the images parent node,
1102 * i.e. component image node.
1103 */
73223f0e 1104 printf(" Hash(es) for Image %u (%s): ", count,
53fbb7e8 1105 fit_get_name(fit, noffset, NULL));
73223f0e 1106 count++;
53fbb7e8 1107
b8da8366 1108 if (!fit_image_verify(fit, noffset))
53fbb7e8
SG
1109 return 0;
1110 printf("\n");
1111 }
1112 }
1113 return 1;
1114}
1115
1116/**
1117 * fit_image_check_os - check whether image node is of a given os type
1118 * @fit: pointer to the FIT format image header
1119 * @noffset: component image node offset
1120 * @os: requested image os
1121 *
1122 * fit_image_check_os() reads image os property and compares its numeric
1123 * id with the requested os. Comparison result is returned to the caller.
1124 *
1125 * returns:
1126 * 1 if image is of given os type
1127 * 0 otherwise (or on error)
1128 */
1129int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1130{
1131 uint8_t image_os;
1132
1133 if (fit_image_get_os(fit, noffset, &image_os))
1134 return 0;
1135 return (os == image_os);
1136}
1137
1138/**
1139 * fit_image_check_arch - check whether image node is of a given arch
1140 * @fit: pointer to the FIT format image header
1141 * @noffset: component image node offset
1142 * @arch: requested imagearch
1143 *
1144 * fit_image_check_arch() reads image arch property and compares its numeric
1145 * id with the requested arch. Comparison result is returned to the caller.
1146 *
1147 * returns:
1148 * 1 if image is of given arch
1149 * 0 otherwise (or on error)
1150 */
1151int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1152{
1153 uint8_t image_arch;
1154
1155 if (fit_image_get_arch(fit, noffset, &image_arch))
1156 return 0;
5bda35cf
SG
1157 return (arch == image_arch) ||
1158 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
53fbb7e8
SG
1159}
1160
1161/**
1162 * fit_image_check_type - check whether image node is of a given type
1163 * @fit: pointer to the FIT format image header
1164 * @noffset: component image node offset
1165 * @type: requested image type
1166 *
1167 * fit_image_check_type() reads image type property and compares its numeric
1168 * id with the requested type. Comparison result is returned to the caller.
1169 *
1170 * returns:
1171 * 1 if image is of given type
1172 * 0 otherwise (or on error)
1173 */
1174int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1175{
1176 uint8_t image_type;
1177
1178 if (fit_image_get_type(fit, noffset, &image_type))
1179 return 0;
1180 return (type == image_type);
1181}
1182
1183/**
1184 * fit_image_check_comp - check whether image node uses given compression
1185 * @fit: pointer to the FIT format image header
1186 * @noffset: component image node offset
1187 * @comp: requested image compression type
1188 *
1189 * fit_image_check_comp() reads image compression property and compares its
1190 * numeric id with the requested compression type. Comparison result is
1191 * returned to the caller.
1192 *
1193 * returns:
1194 * 1 if image uses requested compression
1195 * 0 otherwise (or on error)
1196 */
1197int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1198{
1199 uint8_t image_comp;
1200
1201 if (fit_image_get_comp(fit, noffset, &image_comp))
1202 return 0;
1203 return (comp == image_comp);
1204}
1205
1206/**
1207 * fit_check_format - sanity check FIT image format
1208 * @fit: pointer to the FIT format image header
1209 *
1210 * fit_check_format() runs a basic sanity FIT image verification.
1211 * Routine checks for mandatory properties, nodes, etc.
1212 *
1213 * returns:
1214 * 1, on success
1215 * 0, on failure
1216 */
1217int fit_check_format(const void *fit)
1218{
1219 /* mandatory / node 'description' property */
1220 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1221 debug("Wrong FIT format: no description\n");
1222 return 0;
1223 }
1224
1225 if (IMAGE_ENABLE_TIMESTAMP) {
1226 /* mandatory / node 'timestamp' property */
1227 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1228 debug("Wrong FIT format: no timestamp\n");
1229 return 0;
1230 }
1231 }
1232
1233 /* mandatory subimages parent '/images' node */
1234 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1235 debug("Wrong FIT format: no images parent node\n");
1236 return 0;
1237 }
1238
1239 return 1;
1240}
1241
1242
1243/**
1244 * fit_conf_find_compat
1245 * @fit: pointer to the FIT format image header
1246 * @fdt: pointer to the device tree to compare against
1247 *
1248 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1249 * most compatible with the passed in device tree.
1250 *
1251 * Example:
1252 *
1253 * / o image-tree
1254 * |-o images
1255 * | |-o fdt@1
1256 * | |-o fdt@2
1257 * |
1258 * |-o configurations
1259 * |-o config@1
1260 * | |-fdt = fdt@1
1261 * |
1262 * |-o config@2
1263 * |-fdt = fdt@2
1264 *
1265 * / o U-Boot fdt
1266 * |-compatible = "foo,bar", "bim,bam"
1267 *
1268 * / o kernel fdt1
1269 * |-compatible = "foo,bar",
1270 *
1271 * / o kernel fdt2
1272 * |-compatible = "bim,bam", "baz,biz"
1273 *
1274 * Configuration 1 would be picked because the first string in U-Boot's
1275 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1276 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1277 *
1278 * returns:
1279 * offset to the configuration to use if one was found
1280 * -1 otherwise
1281 */
1282int fit_conf_find_compat(const void *fit, const void *fdt)
1283{
1284 int ndepth = 0;
1285 int noffset, confs_noffset, images_noffset;
1286 const void *fdt_compat;
1287 int fdt_compat_len;
1288 int best_match_offset = 0;
1289 int best_match_pos = 0;
1290
1291 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1292 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1293 if (confs_noffset < 0 || images_noffset < 0) {
1294 debug("Can't find configurations or images nodes.\n");
1295 return -1;
1296 }
1297
1298 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1299 if (!fdt_compat) {
1300 debug("Fdt for comparison has no \"compatible\" property.\n");
1301 return -1;
1302 }
1303
1304 /*
1305 * Loop over the configurations in the FIT image.
1306 */
1307 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1308 (noffset >= 0) && (ndepth > 0);
1309 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1310 const void *kfdt;
1311 const char *kfdt_name;
1312 int kfdt_noffset;
1313 const char *cur_fdt_compat;
1314 int len;
1315 size_t size;
1316 int i;
1317
1318 if (ndepth > 1)
1319 continue;
1320
1321 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1322 if (!kfdt_name) {
1323 debug("No fdt property found.\n");
1324 continue;
1325 }
1326 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1327 kfdt_name);
1328 if (kfdt_noffset < 0) {
1329 debug("No image node named \"%s\" found.\n",
1330 kfdt_name);
1331 continue;
1332 }
1333 /*
1334 * Get a pointer to this configuration's fdt.
1335 */
1336 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1337 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1338 continue;
1339 }
1340
1341 len = fdt_compat_len;
1342 cur_fdt_compat = fdt_compat;
1343 /*
1344 * Look for a match for each U-Boot compatibility string in
1345 * turn in this configuration's fdt.
1346 */
1347 for (i = 0; len > 0 &&
1348 (!best_match_offset || best_match_pos > i); i++) {
1349 int cur_len = strlen(cur_fdt_compat) + 1;
1350
1351 if (!fdt_node_check_compatible(kfdt, 0,
1352 cur_fdt_compat)) {
1353 best_match_offset = noffset;
1354 best_match_pos = i;
1355 break;
1356 }
1357 len -= cur_len;
1358 cur_fdt_compat += cur_len;
1359 }
1360 }
1361 if (!best_match_offset) {
1362 debug("No match found.\n");
1363 return -1;
1364 }
1365
1366 return best_match_offset;
1367}
1368
1369/**
1370 * fit_conf_get_node - get node offset for configuration of a given unit name
1371 * @fit: pointer to the FIT format image header
1372 * @conf_uname: configuration node unit name
1373 *
1374 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1375 * parant node) of a provided unit name. If configuration is found its node
1376 * offset is returned to the caller.
1377 *
1378 * When NULL is provided in second argument fit_conf_get_node() will search
1379 * for a default configuration node instead. Default configuration node unit
1f8b546f 1380 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
53fbb7e8
SG
1381 * node.
1382 *
1383 * returns:
1384 * configuration node offset when found (>=0)
1385 * negative number on failure (FDT_ERR_* code)
1386 */
1387int fit_conf_get_node(const void *fit, const char *conf_uname)
1388{
1389 int noffset, confs_noffset;
1390 int len;
1391
1392 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1393 if (confs_noffset < 0) {
1394 debug("Can't find configurations parent node '%s' (%s)\n",
1395 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1396 return confs_noffset;
1397 }
1398
1399 if (conf_uname == NULL) {
1400 /* get configuration unit name from the default property */
1401 debug("No configuration specified, trying default...\n");
1402 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1403 FIT_DEFAULT_PROP, &len);
1404 if (conf_uname == NULL) {
1405 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1406 len);
1407 return len;
1408 }
1409 debug("Found default configuration: '%s'\n", conf_uname);
1410 }
1411
1412 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1413 if (noffset < 0) {
1414 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1415 conf_uname, fdt_strerror(noffset));
1416 }
1417
1418 return noffset;
1419}
1420
003efd7d 1421int fit_conf_get_prop_node(const void *fit, int noffset,
53fbb7e8
SG
1422 const char *prop_name)
1423{
1424 char *uname;
1425 int len;
1426
1427 /* get kernel image unit name from configuration kernel property */
1428 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1429 if (uname == NULL)
1430 return len;
1431
1432 return fit_image_get_node(fit, uname);
1433}
1434
53fbb7e8
SG
1435/**
1436 * fit_conf_print - prints out the FIT configuration details
1437 * @fit: pointer to the FIT format image header
1438 * @noffset: offset of the configuration node
1439 * @p: pointer to prefix string
1440 *
1441 * fit_conf_print() lists all mandatory properies for the processed
1442 * configuration node.
1443 *
1444 * returns:
1445 * no returned results
1446 */
1447void fit_conf_print(const void *fit, int noffset, const char *p)
1448{
1449 char *desc;
1450 char *uname;
1451 int ret;
ecf8cd65 1452 int loadables_index;
53fbb7e8
SG
1453
1454 /* Mandatory properties */
1455 ret = fit_get_desc(fit, noffset, &desc);
1456 printf("%s Description: ", p);
1457 if (ret)
1458 printf("unavailable\n");
1459 else
1460 printf("%s\n", desc);
1461
1462 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1463 printf("%s Kernel: ", p);
1464 if (uname == NULL)
1465 printf("unavailable\n");
1466 else
1467 printf("%s\n", uname);
1468
1469 /* Optional properties */
1470 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1471 if (uname)
1472 printf("%s Init Ramdisk: %s\n", p, uname);
1473
1474 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1475 if (uname)
1476 printf("%s FDT: %s\n", p, uname);
ecf8cd65
KA
1477
1478 /* Print out all of the specified loadables */
1479 for (loadables_index = 0;
5bcec545 1480 fdt_get_string_index(fit, noffset,
ecf8cd65
KA
1481 FIT_LOADABLE_PROP,
1482 loadables_index,
5bcec545 1483 (const char **)&uname) == 0;
ecf8cd65
KA
1484 loadables_index++)
1485 {
1486 if (loadables_index == 0) {
1487 printf("%s Loadables: ", p);
1488 } else {
1489 printf("%s ", p);
1490 }
1491 printf("%s\n", uname);
1492 }
53fbb7e8
SG
1493}
1494
718fecae 1495static int fit_image_select(const void *fit, int rd_noffset, int verify)
782cfbb2
SG
1496{
1497 fit_image_print(fit, rd_noffset, " ");
1498
1499 if (verify) {
1500 puts(" Verifying Hash Integrity ... ");
1501 if (!fit_image_verify(fit, rd_noffset)) {
1502 puts("Bad Data Hash\n");
1503 return -EACCES;
1504 }
1505 puts("OK\n");
1506 }
1507
1508 return 0;
1509}
1510
782cfbb2
SG
1511int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1512 ulong addr)
1513{
1514 int cfg_noffset;
1515 void *fit_hdr;
1516 int noffset;
1517
1518 debug("* %s: using config '%s' from image at 0x%08lx\n",
1519 prop_name, images->fit_uname_cfg, addr);
1520
1521 /* Check whether configuration has this property defined */
1522 fit_hdr = map_sysmem(addr, 0);
1523 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1524 if (cfg_noffset < 0) {
1525 debug("* %s: no such config\n", prop_name);
1526 return -ENOENT;
1527 }
1528
1529 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1530 if (noffset < 0) {
1531 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1532 return -ENOLINK;
1533 }
1534
1535 return noffset;
1536}
1537
126cc864
SG
1538/**
1539 * fit_get_image_type_property() - get property name for IH_TYPE_...
1540 *
1541 * @return the properly name where we expect to find the image in the
1542 * config node
1543 */
1544static const char *fit_get_image_type_property(int type)
1545{
1546 /*
1547 * This is sort-of available in the uimage_type[] table in image.c
1548 * but we don't have access to the sohrt name, and "fdt" is different
1549 * anyway. So let's just keep it here.
1550 */
1551 switch (type) {
1552 case IH_TYPE_FLATDT:
1553 return FIT_FDT_PROP;
1554 case IH_TYPE_KERNEL:
1555 return FIT_KERNEL_PROP;
1556 case IH_TYPE_RAMDISK:
1557 return FIT_RAMDISK_PROP;
90268b87
SG
1558 case IH_TYPE_X86_SETUP:
1559 return FIT_SETUP_PROP;
84a07dbf
KA
1560 case IH_TYPE_LOADABLE:
1561 return FIT_LOADABLE_PROP;
126cc864
SG
1562 }
1563
1564 return "unknown";
1565}
1566
1567int fit_image_load(bootm_headers_t *images, ulong addr,
f320a4d8 1568 const char **fit_unamep, const char **fit_uname_configp,
782cfbb2
SG
1569 int arch, int image_type, int bootstage_id,
1570 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1571{
1572 int cfg_noffset, noffset;
1573 const char *fit_uname;
f320a4d8 1574 const char *fit_uname_config;
782cfbb2
SG
1575 const void *fit;
1576 const void *buf;
1577 size_t size;
1578 int type_ok, os_ok;
1579 ulong load, data, len;
38117231 1580 uint8_t os;
126cc864 1581 const char *prop_name;
782cfbb2
SG
1582 int ret;
1583
1584 fit = map_sysmem(addr, 0);
1585 fit_uname = fit_unamep ? *fit_unamep : NULL;
f320a4d8 1586 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
126cc864 1587 prop_name = fit_get_image_type_property(image_type);
782cfbb2
SG
1588 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1589
1590 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1591 if (!fit_check_format(fit)) {
1592 printf("Bad FIT %s image format!\n", prop_name);
1593 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1594 return -ENOEXEC;
1595 }
1596 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1597 if (fit_uname) {
f150c837 1598 /* get FIT component image node offset */
782cfbb2
SG
1599 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1600 noffset = fit_image_get_node(fit, fit_uname);
1601 } else {
1602 /*
1603 * no image node unit name, try to get config
1604 * node first. If config unit node name is NULL
1605 * fit_conf_get_node() will try to find default config node
1606 */
1607 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1608 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1609 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1610 } else {
1611 cfg_noffset = fit_conf_get_node(fit,
1612 fit_uname_config);
1613 }
1614 if (cfg_noffset < 0) {
1615 puts("Could not find configuration node\n");
1616 bootstage_error(bootstage_id +
1617 BOOTSTAGE_SUB_NO_UNIT_NAME);
1618 return -ENOENT;
1619 }
1620 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1621 printf(" Using '%s' configuration\n", fit_uname_config);
1622 if (image_type == IH_TYPE_KERNEL) {
1623 /* Remember (and possibly verify) this config */
1624 images->fit_uname_cfg = fit_uname_config;
1625 if (IMAGE_ENABLE_VERIFY && images->verify) {
1626 puts(" Verifying Hash Integrity ... ");
12df2abe 1627 if (fit_config_verify(fit, cfg_noffset)) {
782cfbb2
SG
1628 puts("Bad Data Hash\n");
1629 bootstage_error(bootstage_id +
1630 BOOTSTAGE_SUB_HASH);
1631 return -EACCES;
1632 }
1633 puts("OK\n");
1634 }
1635 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1636 }
1637
1638 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1639 prop_name);
1640 fit_uname = fit_get_name(fit, noffset, NULL);
1641 }
1642 if (noffset < 0) {
1643 puts("Could not find subimage node\n");
1644 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1645 return -ENOENT;
1646 }
1647
1648 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1649
1650 ret = fit_image_select(fit, noffset, images->verify);
1651 if (ret) {
1652 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1653 return ret;
1654 }
1655
1656 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
5ba63dd4 1657#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
782cfbb2
SG
1658 if (!fit_image_check_target_arch(fit, noffset)) {
1659 puts("Unsupported Architecture\n");
1660 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1661 return -ENOEXEC;
1662 }
ce1400f6 1663#endif
782cfbb2
SG
1664 if (image_type == IH_TYPE_FLATDT &&
1665 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1666 puts("FDT image is compressed");
1667 return -EPROTONOSUPPORT;
1668 }
1669
1670 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1671 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1672 (image_type == IH_TYPE_KERNEL &&
1673 fit_image_check_type(fit, noffset,
1674 IH_TYPE_KERNEL_NOLOAD));
38117231 1675
782cfbb2 1676 os_ok = image_type == IH_TYPE_FLATDT ||
38117231
MV
1677 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1678 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
84a07dbf
KA
1679
1680 /*
1681 * If either of the checks fail, we should report an error, but
1682 * if the image type is coming from the "loadables" field, we
1683 * don't care what it is
1684 */
1685 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
38117231
MV
1686 fit_image_get_os(fit, noffset, &os);
1687 printf("No %s %s %s Image\n",
1688 genimg_get_os_name(os),
1689 genimg_get_arch_name(arch),
782cfbb2
SG
1690 genimg_get_type_name(image_type));
1691 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1692 return -EIO;
1693 }
1694
1695 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1696
1697 /* get image data address and length */
1698 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1699 printf("Could not find %s subimage data!\n", prop_name);
1700 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
2f998071 1701 return -ENOENT;
782cfbb2
SG
1702 }
1703 len = (ulong)size;
1704
1705 /* verify that image data is a proper FDT blob */
2f0877c7 1706 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
782cfbb2
SG
1707 puts("Subimage data is not a FDT");
1708 return -ENOEXEC;
1709 }
1710
1711 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1712
1713 /*
1714 * Work-around for eldk-4.2 which gives this warning if we try to
e3c83c0a 1715 * cast in the unmap_sysmem() call:
782cfbb2
SG
1716 * warning: initialization discards qualifiers from pointer target type
1717 */
1718 {
1719 void *vbuf = (void *)buf;
1720
1721 data = map_to_sysmem(vbuf);
1722 }
1723
1724 if (load_op == FIT_LOAD_IGNORED) {
1725 /* Don't load */
1726 } else if (fit_image_get_load(fit, noffset, &load)) {
1727 if (load_op == FIT_LOAD_REQUIRED) {
1728 printf("Can't get %s subimage load address!\n",
1729 prop_name);
1730 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1731 return -EBADF;
1732 }
fe20a81a 1733 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
782cfbb2
SG
1734 ulong image_start, image_end;
1735 ulong load_end;
1736 void *dst;
1737
1738 /*
1739 * move image data to the load address,
1740 * make sure we don't overwrite initial image
1741 */
1742 image_start = addr;
1743 image_end = addr + fit_get_size(fit);
1744
1745 load_end = load + len;
1746 if (image_type != IH_TYPE_KERNEL &&
1747 load < image_end && load_end > image_start) {
1748 printf("Error: %s overwritten\n", prop_name);
1749 return -EXDEV;
1750 }
1751
1752 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1753 prop_name, data, load);
1754
1755 dst = map_sysmem(load, len);
1756 memmove(dst, buf, len);
1757 data = load;
1758 }
1759 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1760
1761 *datap = data;
1762 *lenp = len;
1763 if (fit_unamep)
1764 *fit_unamep = (char *)fit_uname;
f320a4d8
SG
1765 if (fit_uname_configp)
1766 *fit_uname_configp = (char *)fit_uname_config;
782cfbb2
SG
1767
1768 return noffset;
1769}
90268b87
SG
1770
1771int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1772 ulong *setup_start, ulong *setup_len)
1773{
1774 int noffset;
1775 ulong addr;
1776 ulong len;
1777 int ret;
1778
1779 addr = map_to_sysmem(images->fit_hdr_os);
1780 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1781 if (noffset < 0)
1782 return noffset;
1783
1784 ret = fit_image_load(images, addr, NULL, NULL, arch,
1785 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1786 FIT_LOAD_REQUIRED, setup_start, &len);
1787
1788 return ret;
1789}