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