blob: b47d11024f2beb007d049bc0cb54a43268d8bde1 [file] [log] [blame]
Simon Glass53fbb7e2013-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 Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Simon Glass53fbb7e2013-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 Glass782cfbb2013-05-16 13:53:21 +000018#include <errno.h>
19#include <asm/io.h>
20DECLARE_GLOBAL_DATA_PTR;
Simon Glass53fbb7e2013-05-07 06:11:53 +000021#endif /* !USE_HOSTCC*/
22
23#include <bootstage.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000024#include <u-boot/crc.h>
25#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020026#include <u-boot/sha1.h>
27#include <u-boot/sha256.h>
Simon Glass53fbb7e2013-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 Yamada069d5942013-09-18 09:36:38 +090062 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-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 Yamada069d5942013-09-18 09:36:38 +090088 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-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 Ferreira39931f92015-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 Glass87ebee32013-05-08 08:05:59 +0000142#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glass53fbb7e2013-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 Glass1fe7d932013-05-08 08:05:58 +0000168 /* Indent string is defined in header image.h */
169 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-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 Glassd8b75362013-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 Glass56518e72013-06-13 15:10:01 -0700249 * @type: Type of information to print ("hash" or "sign")
Simon Glassd8b75362013-05-07 06:12:02 +0000250 *
251 * fit_image_print_data() lists properies for the processed hash node
252 *
Simon Glass56518e72013-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 Glassd8b75362013-05-07 06:12:02 +0000256 * returns:
257 * no returned results
258 */
Simon Glass56518e72013-06-13 15:10:01 -0700259static void fit_image_print_data(const void *fit, int noffset, const char *p,
260 const char *type)
Simon Glassd8b75362013-05-07 06:12:02 +0000261{
Simon Glass56518e72013-06-13 15:10:01 -0700262 const char *keyname;
Simon Glassd8b75362013-05-07 06:12:02 +0000263 uint8_t *value;
264 int value_len;
Simon Glass56518e72013-06-13 15:10:01 -0700265 char *algo;
266 int required;
267 int ret, i;
Simon Glassd8b75362013-05-07 06:12:02 +0000268
Simon Glass56518e72013-06-13 15:10:01 -0700269 debug("%s %s node: '%s'\n", p, type,
Simon Glassd8b75362013-05-07 06:12:02 +0000270 fit_get_name(fit, noffset, NULL));
Simon Glass56518e72013-06-13 15:10:01 -0700271 printf("%s %s algo: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000272 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
273 printf("invalid/unsupported\n");
274 return;
275 }
Simon Glass56518e72013-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 Glassd8b75362013-05-07 06:12:02 +0000284
285 ret = fit_image_hash_get_value(fit, noffset, &value,
286 &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700287 printf("%s %s value: ", p, type);
Simon Glassd8b75362013-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 Glass56518e72013-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 Glassd8b75362013-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 Glass56518e72013-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 Glassd8b75362013-05-07 06:12:02 +0000338}
339
340/**
Simon Glass53fbb7e2013-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 Glass1fd1e2f2013-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 Glass53fbb7e2013-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 Glassc6ac13b2013-05-16 13:53:26 +0000395 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000396 printf("unavailable\n");
Simon Glassc6ac13b2013-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 Glass53fbb7e2013-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 Glassd8b75362013-05-07 06:12:02 +0000449 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000450 }
451 }
452}
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200453
454#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000455
456/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000457 * 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 Glass53fbb7e2013-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 Glassab9efc62013-05-07 06:11:58 +0000839static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-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 Glass53fbb7e2013-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 Glass4f427a42014-06-02 22:04:51 -0600864 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-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 Glass4f427a42014-06-02 22:04:51 -0600878 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-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 Glass604f23d2013-05-07 06:11:54 +0000902int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000903 uint8_t *value, int *value_len)
904{
Simon Glass87ebee32013-05-08 08:05:59 +0000905 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-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 Glass87ebee32013-05-08 08:05:59 +0000910 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-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 Schocher2842c1c2014-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 Glass87ebee32013-05-08 08:05:59 +0000918 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-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 Glassab9efc62013-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 Glasse754da22013-05-07 06:11:59 +0000941 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-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 Glasse754da22013-05-07 06:11:59 +0000956 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +0000957 return -1;
958 }
959
960 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +0000961 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +0000962 return -1;
963 }
964
965 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +0000966 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +0000967 return -1;
968 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +0000969 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +0000970 return -1;
971 }
972
973 return 0;
974}
975
Simon Glass53fbb7e2013-05-07 06:11:53 +0000976/**
Simon Glassb8da8362013-05-07 06:11:57 +0000977 * fit_image_verify - verify data intergity
Simon Glass53fbb7e2013-05-07 06:11:53 +0000978 * @fit: pointer to the FIT format image header
979 * @image_noffset: component image node offset
980 *
Simon Glassb8da8362013-05-07 06:11:57 +0000981 * fit_image_verify() goes over component image hash nodes,
Simon Glass53fbb7e2013-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 Glassb8da8362013-05-07 06:11:57 +0000989int fit_image_verify(const void *fit, int image_noffset)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000990{
991 const void *data;
992 size_t size;
Simon Glass56518e72013-06-13 15:10:01 -0700993 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000994 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -0700995 int verify_all = 1;
996 int ret;
Simon Glass53fbb7e2013-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 Glasse754da22013-05-07 06:11:59 +00001000 err_msg = "Can't get image data/size";
Simon Glass56518e72013-06-13 15:10:01 -07001001 goto error;
1002 }
1003
1004 /* 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;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001010 }
1011
1012 /* Process all hash subnodes of the component image node */
Simon Glassab9efc62013-05-07 06:11:58 +00001013 for (noffset = fdt_first_subnode(fit, image_noffset);
1014 noffset >= 0;
1015 noffset = fdt_next_subnode(fit, noffset)) {
1016 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001017
Simon Glassab9efc62013-05-07 06:11:58 +00001018 /*
1019 * Check subnode name, must be equal to "hash".
1020 * Multiple hash nodes require unique unit node
1021 * names, e.g. hash@1, hash@2, etc.
1022 */
1023 if (!strncmp(name, FIT_HASH_NODENAME,
1024 strlen(FIT_HASH_NODENAME))) {
1025 if (fit_image_check_hash(fit, noffset, data, size,
1026 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001027 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001028 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001029 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1030 !strncmp(name, FIT_SIG_NODENAME,
1031 strlen(FIT_SIG_NODENAME))) {
1032 ret = fit_image_check_sig(fit, noffset, data,
1033 size, -1, &err_msg);
1034 if (ret)
1035 puts("- ");
1036 else
1037 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001038 }
1039 }
1040
1041 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001042 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001043 goto error;
1044 }
1045
1046 return 1;
1047
1048error:
Simon Glasse754da22013-05-07 06:11:59 +00001049 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001050 err_msg, fit_get_name(fit, noffset, NULL),
1051 fit_get_name(fit, image_noffset, NULL));
1052 return 0;
1053}
1054
1055/**
Simon Glassb8da8362013-05-07 06:11:57 +00001056 * fit_all_image_verify - verify data intergity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001057 * @fit: pointer to the FIT format image header
1058 *
Simon Glassb8da8362013-05-07 06:11:57 +00001059 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001060 * for every images checks if all it's hashes are valid.
1061 *
1062 * returns:
1063 * 1, if all hashes of all images are valid
1064 * 0, otherwise (or on error)
1065 */
Simon Glassb8da8362013-05-07 06:11:57 +00001066int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001067{
1068 int images_noffset;
1069 int noffset;
1070 int ndepth;
1071 int count;
1072
1073 /* Find images parent node offset */
1074 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1075 if (images_noffset < 0) {
1076 printf("Can't find images parent node '%s' (%s)\n",
1077 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1078 return 0;
1079 }
1080
1081 /* Process all image subnodes, check hashes for each */
1082 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1083 (ulong)fit);
1084 for (ndepth = 0, count = 0,
1085 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1086 (noffset >= 0) && (ndepth > 0);
1087 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1088 if (ndepth == 1) {
1089 /*
1090 * Direct child node of the images parent node,
1091 * i.e. component image node.
1092 */
1093 printf(" Hash(es) for Image %u (%s): ", count++,
1094 fit_get_name(fit, noffset, NULL));
1095
Simon Glassb8da8362013-05-07 06:11:57 +00001096 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001097 return 0;
1098 printf("\n");
1099 }
1100 }
1101 return 1;
1102}
1103
1104/**
1105 * fit_image_check_os - check whether image node is of a given os type
1106 * @fit: pointer to the FIT format image header
1107 * @noffset: component image node offset
1108 * @os: requested image os
1109 *
1110 * fit_image_check_os() reads image os property and compares its numeric
1111 * id with the requested os. Comparison result is returned to the caller.
1112 *
1113 * returns:
1114 * 1 if image is of given os type
1115 * 0 otherwise (or on error)
1116 */
1117int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1118{
1119 uint8_t image_os;
1120
1121 if (fit_image_get_os(fit, noffset, &image_os))
1122 return 0;
1123 return (os == image_os);
1124}
1125
1126/**
1127 * fit_image_check_arch - check whether image node is of a given arch
1128 * @fit: pointer to the FIT format image header
1129 * @noffset: component image node offset
1130 * @arch: requested imagearch
1131 *
1132 * fit_image_check_arch() reads image arch property and compares its numeric
1133 * id with the requested arch. Comparison result is returned to the caller.
1134 *
1135 * returns:
1136 * 1 if image is of given arch
1137 * 0 otherwise (or on error)
1138 */
1139int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1140{
1141 uint8_t image_arch;
1142
1143 if (fit_image_get_arch(fit, noffset, &image_arch))
1144 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001145 return (arch == image_arch) ||
1146 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001147}
1148
1149/**
1150 * fit_image_check_type - check whether image node is of a given type
1151 * @fit: pointer to the FIT format image header
1152 * @noffset: component image node offset
1153 * @type: requested image type
1154 *
1155 * fit_image_check_type() reads image type property and compares its numeric
1156 * id with the requested type. Comparison result is returned to the caller.
1157 *
1158 * returns:
1159 * 1 if image is of given type
1160 * 0 otherwise (or on error)
1161 */
1162int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1163{
1164 uint8_t image_type;
1165
1166 if (fit_image_get_type(fit, noffset, &image_type))
1167 return 0;
1168 return (type == image_type);
1169}
1170
1171/**
1172 * fit_image_check_comp - check whether image node uses given compression
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: component image node offset
1175 * @comp: requested image compression type
1176 *
1177 * fit_image_check_comp() reads image compression property and compares its
1178 * numeric id with the requested compression type. Comparison result is
1179 * returned to the caller.
1180 *
1181 * returns:
1182 * 1 if image uses requested compression
1183 * 0 otherwise (or on error)
1184 */
1185int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1186{
1187 uint8_t image_comp;
1188
1189 if (fit_image_get_comp(fit, noffset, &image_comp))
1190 return 0;
1191 return (comp == image_comp);
1192}
1193
1194/**
1195 * fit_check_format - sanity check FIT image format
1196 * @fit: pointer to the FIT format image header
1197 *
1198 * fit_check_format() runs a basic sanity FIT image verification.
1199 * Routine checks for mandatory properties, nodes, etc.
1200 *
1201 * returns:
1202 * 1, on success
1203 * 0, on failure
1204 */
1205int fit_check_format(const void *fit)
1206{
1207 /* mandatory / node 'description' property */
1208 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1209 debug("Wrong FIT format: no description\n");
1210 return 0;
1211 }
1212
1213 if (IMAGE_ENABLE_TIMESTAMP) {
1214 /* mandatory / node 'timestamp' property */
1215 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1216 debug("Wrong FIT format: no timestamp\n");
1217 return 0;
1218 }
1219 }
1220
1221 /* mandatory subimages parent '/images' node */
1222 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1223 debug("Wrong FIT format: no images parent node\n");
1224 return 0;
1225 }
1226
1227 return 1;
1228}
1229
1230
1231/**
1232 * fit_conf_find_compat
1233 * @fit: pointer to the FIT format image header
1234 * @fdt: pointer to the device tree to compare against
1235 *
1236 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1237 * most compatible with the passed in device tree.
1238 *
1239 * Example:
1240 *
1241 * / o image-tree
1242 * |-o images
1243 * | |-o fdt@1
1244 * | |-o fdt@2
1245 * |
1246 * |-o configurations
1247 * |-o config@1
1248 * | |-fdt = fdt@1
1249 * |
1250 * |-o config@2
1251 * |-fdt = fdt@2
1252 *
1253 * / o U-Boot fdt
1254 * |-compatible = "foo,bar", "bim,bam"
1255 *
1256 * / o kernel fdt1
1257 * |-compatible = "foo,bar",
1258 *
1259 * / o kernel fdt2
1260 * |-compatible = "bim,bam", "baz,biz"
1261 *
1262 * Configuration 1 would be picked because the first string in U-Boot's
1263 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1264 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1265 *
1266 * returns:
1267 * offset to the configuration to use if one was found
1268 * -1 otherwise
1269 */
1270int fit_conf_find_compat(const void *fit, const void *fdt)
1271{
1272 int ndepth = 0;
1273 int noffset, confs_noffset, images_noffset;
1274 const void *fdt_compat;
1275 int fdt_compat_len;
1276 int best_match_offset = 0;
1277 int best_match_pos = 0;
1278
1279 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1280 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1281 if (confs_noffset < 0 || images_noffset < 0) {
1282 debug("Can't find configurations or images nodes.\n");
1283 return -1;
1284 }
1285
1286 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1287 if (!fdt_compat) {
1288 debug("Fdt for comparison has no \"compatible\" property.\n");
1289 return -1;
1290 }
1291
1292 /*
1293 * Loop over the configurations in the FIT image.
1294 */
1295 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1296 (noffset >= 0) && (ndepth > 0);
1297 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1298 const void *kfdt;
1299 const char *kfdt_name;
1300 int kfdt_noffset;
1301 const char *cur_fdt_compat;
1302 int len;
1303 size_t size;
1304 int i;
1305
1306 if (ndepth > 1)
1307 continue;
1308
1309 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1310 if (!kfdt_name) {
1311 debug("No fdt property found.\n");
1312 continue;
1313 }
1314 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1315 kfdt_name);
1316 if (kfdt_noffset < 0) {
1317 debug("No image node named \"%s\" found.\n",
1318 kfdt_name);
1319 continue;
1320 }
1321 /*
1322 * Get a pointer to this configuration's fdt.
1323 */
1324 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1325 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1326 continue;
1327 }
1328
1329 len = fdt_compat_len;
1330 cur_fdt_compat = fdt_compat;
1331 /*
1332 * Look for a match for each U-Boot compatibility string in
1333 * turn in this configuration's fdt.
1334 */
1335 for (i = 0; len > 0 &&
1336 (!best_match_offset || best_match_pos > i); i++) {
1337 int cur_len = strlen(cur_fdt_compat) + 1;
1338
1339 if (!fdt_node_check_compatible(kfdt, 0,
1340 cur_fdt_compat)) {
1341 best_match_offset = noffset;
1342 best_match_pos = i;
1343 break;
1344 }
1345 len -= cur_len;
1346 cur_fdt_compat += cur_len;
1347 }
1348 }
1349 if (!best_match_offset) {
1350 debug("No match found.\n");
1351 return -1;
1352 }
1353
1354 return best_match_offset;
1355}
1356
1357/**
1358 * fit_conf_get_node - get node offset for configuration of a given unit name
1359 * @fit: pointer to the FIT format image header
1360 * @conf_uname: configuration node unit name
1361 *
1362 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1363 * parant node) of a provided unit name. If configuration is found its node
1364 * offset is returned to the caller.
1365 *
1366 * When NULL is provided in second argument fit_conf_get_node() will search
1367 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001368 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001369 * node.
1370 *
1371 * returns:
1372 * configuration node offset when found (>=0)
1373 * negative number on failure (FDT_ERR_* code)
1374 */
1375int fit_conf_get_node(const void *fit, const char *conf_uname)
1376{
1377 int noffset, confs_noffset;
1378 int len;
1379
1380 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1381 if (confs_noffset < 0) {
1382 debug("Can't find configurations parent node '%s' (%s)\n",
1383 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1384 return confs_noffset;
1385 }
1386
1387 if (conf_uname == NULL) {
1388 /* get configuration unit name from the default property */
1389 debug("No configuration specified, trying default...\n");
1390 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1391 FIT_DEFAULT_PROP, &len);
1392 if (conf_uname == NULL) {
1393 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1394 len);
1395 return len;
1396 }
1397 debug("Found default configuration: '%s'\n", conf_uname);
1398 }
1399
1400 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1401 if (noffset < 0) {
1402 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1403 conf_uname, fdt_strerror(noffset));
1404 }
1405
1406 return noffset;
1407}
1408
Simon Glass003efd72013-05-07 06:12:00 +00001409int fit_conf_get_prop_node(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001410 const char *prop_name)
1411{
1412 char *uname;
1413 int len;
1414
1415 /* get kernel image unit name from configuration kernel property */
1416 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1417 if (uname == NULL)
1418 return len;
1419
1420 return fit_image_get_node(fit, uname);
1421}
1422
1423/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001424 * fit_conf_print - prints out the FIT configuration details
1425 * @fit: pointer to the FIT format image header
1426 * @noffset: offset of the configuration node
1427 * @p: pointer to prefix string
1428 *
1429 * fit_conf_print() lists all mandatory properies for the processed
1430 * configuration node.
1431 *
1432 * returns:
1433 * no returned results
1434 */
1435void fit_conf_print(const void *fit, int noffset, const char *p)
1436{
1437 char *desc;
1438 char *uname;
1439 int ret;
1440
1441 /* Mandatory properties */
1442 ret = fit_get_desc(fit, noffset, &desc);
1443 printf("%s Description: ", p);
1444 if (ret)
1445 printf("unavailable\n");
1446 else
1447 printf("%s\n", desc);
1448
1449 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1450 printf("%s Kernel: ", p);
1451 if (uname == NULL)
1452 printf("unavailable\n");
1453 else
1454 printf("%s\n", uname);
1455
1456 /* Optional properties */
1457 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1458 if (uname)
1459 printf("%s Init Ramdisk: %s\n", p, uname);
1460
1461 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1462 if (uname)
1463 printf("%s FDT: %s\n", p, uname);
1464}
1465
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001466static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001467{
1468 fit_image_print(fit, rd_noffset, " ");
1469
1470 if (verify) {
1471 puts(" Verifying Hash Integrity ... ");
1472 if (!fit_image_verify(fit, rd_noffset)) {
1473 puts("Bad Data Hash\n");
1474 return -EACCES;
1475 }
1476 puts("OK\n");
1477 }
1478
1479 return 0;
1480}
1481
Simon Glass782cfbb2013-05-16 13:53:21 +00001482int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1483 ulong addr)
1484{
1485 int cfg_noffset;
1486 void *fit_hdr;
1487 int noffset;
1488
1489 debug("* %s: using config '%s' from image at 0x%08lx\n",
1490 prop_name, images->fit_uname_cfg, addr);
1491
1492 /* Check whether configuration has this property defined */
1493 fit_hdr = map_sysmem(addr, 0);
1494 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1495 if (cfg_noffset < 0) {
1496 debug("* %s: no such config\n", prop_name);
1497 return -ENOENT;
1498 }
1499
1500 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1501 if (noffset < 0) {
1502 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1503 return -ENOLINK;
1504 }
1505
1506 return noffset;
1507}
1508
Simon Glass126cc862014-06-12 07:24:47 -06001509/**
1510 * fit_get_image_type_property() - get property name for IH_TYPE_...
1511 *
1512 * @return the properly name where we expect to find the image in the
1513 * config node
1514 */
1515static const char *fit_get_image_type_property(int type)
1516{
1517 /*
1518 * This is sort-of available in the uimage_type[] table in image.c
1519 * but we don't have access to the sohrt name, and "fdt" is different
1520 * anyway. So let's just keep it here.
1521 */
1522 switch (type) {
1523 case IH_TYPE_FLATDT:
1524 return FIT_FDT_PROP;
1525 case IH_TYPE_KERNEL:
1526 return FIT_KERNEL_PROP;
1527 case IH_TYPE_RAMDISK:
1528 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001529 case IH_TYPE_X86_SETUP:
1530 return FIT_SETUP_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001531 }
1532
1533 return "unknown";
1534}
1535
1536int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001537 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001538 int arch, int image_type, int bootstage_id,
1539 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1540{
1541 int cfg_noffset, noffset;
1542 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001543 const char *fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001544 const void *fit;
1545 const void *buf;
1546 size_t size;
1547 int type_ok, os_ok;
1548 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001549 uint8_t os;
Simon Glass126cc862014-06-12 07:24:47 -06001550 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001551 int ret;
1552
1553 fit = map_sysmem(addr, 0);
1554 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001555 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001556 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001557 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1558
1559 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1560 if (!fit_check_format(fit)) {
1561 printf("Bad FIT %s image format!\n", prop_name);
1562 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1563 return -ENOEXEC;
1564 }
1565 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1566 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001567 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001568 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1569 noffset = fit_image_get_node(fit, fit_uname);
1570 } else {
1571 /*
1572 * no image node unit name, try to get config
1573 * node first. If config unit node name is NULL
1574 * fit_conf_get_node() will try to find default config node
1575 */
1576 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1577 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1578 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1579 } else {
1580 cfg_noffset = fit_conf_get_node(fit,
1581 fit_uname_config);
1582 }
1583 if (cfg_noffset < 0) {
1584 puts("Could not find configuration node\n");
1585 bootstage_error(bootstage_id +
1586 BOOTSTAGE_SUB_NO_UNIT_NAME);
1587 return -ENOENT;
1588 }
1589 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1590 printf(" Using '%s' configuration\n", fit_uname_config);
1591 if (image_type == IH_TYPE_KERNEL) {
1592 /* Remember (and possibly verify) this config */
1593 images->fit_uname_cfg = fit_uname_config;
1594 if (IMAGE_ENABLE_VERIFY && images->verify) {
1595 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001596 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001597 puts("Bad Data Hash\n");
1598 bootstage_error(bootstage_id +
1599 BOOTSTAGE_SUB_HASH);
1600 return -EACCES;
1601 }
1602 puts("OK\n");
1603 }
1604 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1605 }
1606
1607 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1608 prop_name);
1609 fit_uname = fit_get_name(fit, noffset, NULL);
1610 }
1611 if (noffset < 0) {
1612 puts("Could not find subimage node\n");
1613 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1614 return -ENOENT;
1615 }
1616
1617 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1618
1619 ret = fit_image_select(fit, noffset, images->verify);
1620 if (ret) {
1621 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1622 return ret;
1623 }
1624
1625 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001626#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001627 if (!fit_image_check_target_arch(fit, noffset)) {
1628 puts("Unsupported Architecture\n");
1629 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1630 return -ENOEXEC;
1631 }
Simon Glassce1400f2014-06-12 07:24:53 -06001632#endif
Simon Glass782cfbb2013-05-16 13:53:21 +00001633 if (image_type == IH_TYPE_FLATDT &&
1634 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1635 puts("FDT image is compressed");
1636 return -EPROTONOSUPPORT;
1637 }
1638
1639 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1640 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1641 (image_type == IH_TYPE_KERNEL &&
1642 fit_image_check_type(fit, noffset,
1643 IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001644
Simon Glass782cfbb2013-05-16 13:53:21 +00001645 os_ok = image_type == IH_TYPE_FLATDT ||
Marek Vasut38117232014-12-16 14:07:22 +01001646 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1647 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Simon Glass782cfbb2013-05-16 13:53:21 +00001648 if (!type_ok || !os_ok) {
Marek Vasut38117232014-12-16 14:07:22 +01001649 fit_image_get_os(fit, noffset, &os);
1650 printf("No %s %s %s Image\n",
1651 genimg_get_os_name(os),
1652 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001653 genimg_get_type_name(image_type));
1654 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1655 return -EIO;
1656 }
1657
1658 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1659
1660 /* get image data address and length */
1661 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1662 printf("Could not find %s subimage data!\n", prop_name);
1663 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001664 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001665 }
1666 len = (ulong)size;
1667
1668 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001669 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001670 puts("Subimage data is not a FDT");
1671 return -ENOEXEC;
1672 }
1673
1674 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1675
1676 /*
1677 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001678 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001679 * warning: initialization discards qualifiers from pointer target type
1680 */
1681 {
1682 void *vbuf = (void *)buf;
1683
1684 data = map_to_sysmem(vbuf);
1685 }
1686
1687 if (load_op == FIT_LOAD_IGNORED) {
1688 /* Don't load */
1689 } else if (fit_image_get_load(fit, noffset, &load)) {
1690 if (load_op == FIT_LOAD_REQUIRED) {
1691 printf("Can't get %s subimage load address!\n",
1692 prop_name);
1693 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1694 return -EBADF;
1695 }
Simon Glassfe20a812014-08-22 14:26:43 -06001696 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001697 ulong image_start, image_end;
1698 ulong load_end;
1699 void *dst;
1700
1701 /*
1702 * move image data to the load address,
1703 * make sure we don't overwrite initial image
1704 */
1705 image_start = addr;
1706 image_end = addr + fit_get_size(fit);
1707
1708 load_end = load + len;
1709 if (image_type != IH_TYPE_KERNEL &&
1710 load < image_end && load_end > image_start) {
1711 printf("Error: %s overwritten\n", prop_name);
1712 return -EXDEV;
1713 }
1714
1715 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1716 prop_name, data, load);
1717
1718 dst = map_sysmem(load, len);
1719 memmove(dst, buf, len);
1720 data = load;
1721 }
1722 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1723
1724 *datap = data;
1725 *lenp = len;
1726 if (fit_unamep)
1727 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001728 if (fit_uname_configp)
1729 *fit_uname_configp = (char *)fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001730
1731 return noffset;
1732}
Simon Glass90268b82014-10-19 21:11:24 -06001733
1734int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1735 ulong *setup_start, ulong *setup_len)
1736{
1737 int noffset;
1738 ulong addr;
1739 ulong len;
1740 int ret;
1741
1742 addr = map_to_sysmem(images->fit_hdr_os);
1743 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1744 if (noffset < 0)
1745 return noffset;
1746
1747 ret = fit_image_load(images, addr, NULL, NULL, arch,
1748 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1749 FIT_LOAD_REQUIRED, setup_start, &len);
1750
1751 return ret;
1752}