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