blob: d3fad30c34389b630f21fe98a786a1526976b07f [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)) {
436 fit_image_get_entry(fit, image_noffset, &entry);
437 printf("%s Entry Point: ", p);
438 if (ret)
439 printf("unavailable\n");
440 else
441 printf("0x%08lx\n", entry);
442 }
443
444 /* Process all hash subnodes of the component image node */
445 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
446 (noffset >= 0) && (ndepth > 0);
447 noffset = fdt_next_node(fit, noffset, &ndepth)) {
448 if (ndepth == 1) {
449 /* Direct child node of the component image node */
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
678/**
679 * fit_image_get_load() - get load addr property for given component image node
680 * @fit: pointer to the FIT format image header
681 * @noffset: component image node offset
682 * @load: pointer to the uint32_t, will hold load address
683 *
684 * fit_image_get_load() finds load address property in a given component
685 * image node. If the property is found, its value is returned to the caller.
686 *
687 * returns:
688 * 0, on success
689 * -1, on failure
690 */
691int fit_image_get_load(const void *fit, int noffset, ulong *load)
692{
693 int len;
694 const uint32_t *data;
695
696 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
697 if (data == NULL) {
698 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
699 return -1;
700 }
701
702 *load = uimage_to_cpu(*data);
703 return 0;
704}
705
706/**
707 * fit_image_get_entry() - get entry point address property
708 * @fit: pointer to the FIT format image header
709 * @noffset: component image node offset
710 * @entry: pointer to the uint32_t, will hold entry point address
711 *
712 * This gets the entry point address property for a given component image
713 * node.
714 *
715 * fit_image_get_entry() finds entry point address property in a given
716 * component image node. If the property is found, its value is returned
717 * to the caller.
718 *
719 * returns:
720 * 0, on success
721 * -1, on failure
722 */
723int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
724{
725 int len;
726 const uint32_t *data;
727
728 data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
729 if (data == NULL) {
730 fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
731 return -1;
732 }
733
734 *entry = uimage_to_cpu(*data);
735 return 0;
736}
737
738/**
739 * fit_image_get_data - get data property and its size for a given component image node
740 * @fit: pointer to the FIT format image header
741 * @noffset: component image node offset
742 * @data: double pointer to void, will hold data property's data address
743 * @size: pointer to size_t, will hold data property's data size
744 *
745 * fit_image_get_data() finds data property in a given component image node.
746 * If the property is found its data start address and size are returned to
747 * the caller.
748 *
749 * returns:
750 * 0, on success
751 * -1, on failure
752 */
753int fit_image_get_data(const void *fit, int noffset,
754 const void **data, size_t *size)
755{
756 int len;
757
758 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
759 if (*data == NULL) {
760 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
761 *size = 0;
762 return -1;
763 }
764
765 *size = len;
766 return 0;
767}
768
769/**
770 * fit_image_hash_get_algo - get hash algorithm name
771 * @fit: pointer to the FIT format image header
772 * @noffset: hash node offset
773 * @algo: double pointer to char, will hold pointer to the algorithm name
774 *
775 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
776 * If the property is found its data start address is returned to the caller.
777 *
778 * returns:
779 * 0, on success
780 * -1, on failure
781 */
782int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
783{
784 int len;
785
786 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
787 if (*algo == NULL) {
788 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
789 return -1;
790 }
791
792 return 0;
793}
794
795/**
796 * fit_image_hash_get_value - get hash value and length
797 * @fit: pointer to the FIT format image header
798 * @noffset: hash node offset
799 * @value: double pointer to uint8_t, will hold address of a hash value data
800 * @value_len: pointer to an int, will hold hash data length
801 *
802 * fit_image_hash_get_value() finds hash value property in a given hash node.
803 * If the property is found its data start address and size are returned to
804 * the caller.
805 *
806 * returns:
807 * 0, on success
808 * -1, on failure
809 */
810int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
811 int *value_len)
812{
813 int len;
814
815 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
816 if (*value == NULL) {
817 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
818 *value_len = 0;
819 return -1;
820 }
821
822 *value_len = len;
823 return 0;
824}
825
Simon Glass53fbb7e2013-05-07 06:11:53 +0000826/**
827 * fit_image_hash_get_ignore - get hash ignore flag
828 * @fit: pointer to the FIT format image header
829 * @noffset: hash node offset
830 * @ignore: pointer to an int, will hold hash ignore flag
831 *
832 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
833 * If the property is found and non-zero, the hash algorithm is not verified by
834 * u-boot automatically.
835 *
836 * returns:
837 * 0, on ignore not found
838 * value, on ignore found
839 */
Simon Glassab9efc62013-05-07 06:11:58 +0000840static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000841{
842 int len;
843 int *value;
844
845 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
846 if (value == NULL || len != sizeof(int))
847 *ignore = 0;
848 else
849 *ignore = *value;
850
851 return 0;
852}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000853
Simon Glass7a80de42016-02-24 09:14:42 -0700854ulong fit_get_end(const void *fit)
855{
856 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
857}
858
Simon Glass53fbb7e2013-05-07 06:11:53 +0000859/**
860 * fit_set_timestamp - set node timestamp property
861 * @fit: pointer to the FIT format image header
862 * @noffset: node offset
863 * @timestamp: timestamp value to be set
864 *
865 * fit_set_timestamp() attempts to set timestamp property in the requested
866 * node and returns operation status to the caller.
867 *
868 * returns:
869 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -0600870 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +0000871 */
872int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
873{
874 uint32_t t;
875 int ret;
876
877 t = cpu_to_uimage(timestamp);
878 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
879 sizeof(uint32_t));
880 if (ret) {
881 printf("Can't set '%s' property for '%s' node (%s)\n",
882 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
883 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -0600884 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000885 }
886
887 return 0;
888}
889
890/**
891 * calculate_hash - calculate and return hash for provided input data
892 * @data: pointer to the input data
893 * @data_len: data length
894 * @algo: requested hash algorithm
895 * @value: pointer to the char, will hold hash value data (caller must
896 * allocate enough free space)
897 * value_len: length of the calculated hash
898 *
899 * calculate_hash() computes input data hash according to the requested
900 * algorithm.
901 * Resulting hash value is placed in caller provided 'value' buffer, length
902 * of the calculated hash is returned via value_len pointer argument.
903 *
904 * returns:
905 * 0, on success
906 * -1, when algo is unsupported
907 */
Simon Glass604f23d2013-05-07 06:11:54 +0000908int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000909 uint8_t *value, int *value_len)
910{
Simon Glass87ebee32013-05-08 08:05:59 +0000911 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000912 *((uint32_t *)value) = crc32_wd(0, data, data_len,
913 CHUNKSZ_CRC32);
914 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
915 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +0000916 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000917 sha1_csum_wd((unsigned char *)data, data_len,
918 (unsigned char *)value, CHUNKSZ_SHA1);
919 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +0100920 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
921 sha256_csum_wd((unsigned char *)data, data_len,
922 (unsigned char *)value, CHUNKSZ_SHA256);
923 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +0000924 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000925 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
926 *value_len = 16;
927 } else {
928 debug("Unsupported hash alogrithm\n");
929 return -1;
930 }
931 return 0;
932}
933
Simon Glassab9efc62013-05-07 06:11:58 +0000934static int fit_image_check_hash(const void *fit, int noffset, const void *data,
935 size_t size, char **err_msgp)
936{
937 uint8_t value[FIT_MAX_HASH_LEN];
938 int value_len;
939 char *algo;
940 uint8_t *fit_value;
941 int fit_value_len;
942 int ignore;
943
944 *err_msgp = NULL;
945
946 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +0000947 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +0000948 return -1;
949 }
950 printf("%s", algo);
951
952 if (IMAGE_ENABLE_IGNORE) {
953 fit_image_hash_get_ignore(fit, noffset, &ignore);
954 if (ignore) {
955 printf("-skipped ");
956 return 0;
957 }
958 }
959
960 if (fit_image_hash_get_value(fit, noffset, &fit_value,
961 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +0000962 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +0000963 return -1;
964 }
965
966 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +0000967 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +0000968 return -1;
969 }
970
971 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +0000972 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +0000973 return -1;
974 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +0000975 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +0000976 return -1;
977 }
978
979 return 0;
980}
981
Simon Glass53fbb7e2013-05-07 06:11:53 +0000982/**
Simon Glassb8da8362013-05-07 06:11:57 +0000983 * fit_image_verify - verify data intergity
Simon Glass53fbb7e2013-05-07 06:11:53 +0000984 * @fit: pointer to the FIT format image header
985 * @image_noffset: component image node offset
986 *
Simon Glassb8da8362013-05-07 06:11:57 +0000987 * fit_image_verify() goes over component image hash nodes,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000988 * re-calculates each data hash and compares with the value stored in hash
989 * node.
990 *
991 * returns:
992 * 1, if all hashes are valid
993 * 0, otherwise (or on error)
994 */
Simon Glassb8da8362013-05-07 06:11:57 +0000995int fit_image_verify(const void *fit, int image_noffset)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000996{
997 const void *data;
998 size_t size;
Simon Glass56518e72013-06-13 15:10:01 -0700999 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001000 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001001 int verify_all = 1;
1002 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001003
1004 /* Get image data and data length */
1005 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Simon Glasse754da22013-05-07 06:11:59 +00001006 err_msg = "Can't get image data/size";
Simon Glass56518e72013-06-13 15:10:01 -07001007 goto error;
1008 }
1009
1010 /* Verify all required signatures */
1011 if (IMAGE_ENABLE_VERIFY &&
1012 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1013 gd_fdt_blob(), &verify_all)) {
1014 err_msg = "Unable to verify required signature";
1015 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001016 }
1017
1018 /* Process all hash subnodes of the component image node */
Axel Lin364ac5b2015-02-07 09:12:39 +08001019 fdt_for_each_subnode(fit, noffset, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001020 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001021
Simon Glassab9efc62013-05-07 06:11:58 +00001022 /*
1023 * Check subnode name, must be equal to "hash".
1024 * Multiple hash nodes require unique unit node
1025 * names, e.g. hash@1, hash@2, etc.
1026 */
1027 if (!strncmp(name, FIT_HASH_NODENAME,
1028 strlen(FIT_HASH_NODENAME))) {
1029 if (fit_image_check_hash(fit, noffset, data, size,
1030 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001031 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001032 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001033 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1034 !strncmp(name, FIT_SIG_NODENAME,
1035 strlen(FIT_SIG_NODENAME))) {
1036 ret = fit_image_check_sig(fit, noffset, data,
1037 size, -1, &err_msg);
Andrej Rosano84ca65a2015-10-14 17:45:40 +02001038 if (ret) {
Simon Glass56518e72013-06-13 15:10:01 -07001039 puts("- ");
Andrej Rosano84ca65a2015-10-14 17:45:40 +02001040 goto error;
1041 }
Simon Glass56518e72013-06-13 15:10:01 -07001042 else
1043 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001044 }
1045 }
1046
1047 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001048 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001049 goto error;
1050 }
1051
1052 return 1;
1053
1054error:
Simon Glasse754da22013-05-07 06:11:59 +00001055 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001056 err_msg, fit_get_name(fit, noffset, NULL),
1057 fit_get_name(fit, image_noffset, NULL));
1058 return 0;
1059}
1060
1061/**
Simon Glassb8da8362013-05-07 06:11:57 +00001062 * fit_all_image_verify - verify data intergity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001063 * @fit: pointer to the FIT format image header
1064 *
Simon Glassb8da8362013-05-07 06:11:57 +00001065 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001066 * for every images checks if all it's hashes are valid.
1067 *
1068 * returns:
1069 * 1, if all hashes of all images are valid
1070 * 0, otherwise (or on error)
1071 */
Simon Glassb8da8362013-05-07 06:11:57 +00001072int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001073{
1074 int images_noffset;
1075 int noffset;
1076 int ndepth;
1077 int count;
1078
1079 /* Find images parent node offset */
1080 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1081 if (images_noffset < 0) {
1082 printf("Can't find images parent node '%s' (%s)\n",
1083 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1084 return 0;
1085 }
1086
1087 /* Process all image subnodes, check hashes for each */
1088 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1089 (ulong)fit);
1090 for (ndepth = 0, count = 0,
1091 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1092 (noffset >= 0) && (ndepth > 0);
1093 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1094 if (ndepth == 1) {
1095 /*
1096 * Direct child node of the images parent node,
1097 * i.e. component image node.
1098 */
1099 printf(" Hash(es) for Image %u (%s): ", count++,
1100 fit_get_name(fit, noffset, NULL));
1101
Simon Glassb8da8362013-05-07 06:11:57 +00001102 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001103 return 0;
1104 printf("\n");
1105 }
1106 }
1107 return 1;
1108}
1109
1110/**
1111 * fit_image_check_os - check whether image node is of a given os type
1112 * @fit: pointer to the FIT format image header
1113 * @noffset: component image node offset
1114 * @os: requested image os
1115 *
1116 * fit_image_check_os() reads image os property and compares its numeric
1117 * id with the requested os. Comparison result is returned to the caller.
1118 *
1119 * returns:
1120 * 1 if image is of given os type
1121 * 0 otherwise (or on error)
1122 */
1123int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1124{
1125 uint8_t image_os;
1126
1127 if (fit_image_get_os(fit, noffset, &image_os))
1128 return 0;
1129 return (os == image_os);
1130}
1131
1132/**
1133 * fit_image_check_arch - check whether image node is of a given arch
1134 * @fit: pointer to the FIT format image header
1135 * @noffset: component image node offset
1136 * @arch: requested imagearch
1137 *
1138 * fit_image_check_arch() reads image arch property and compares its numeric
1139 * id with the requested arch. Comparison result is returned to the caller.
1140 *
1141 * returns:
1142 * 1 if image is of given arch
1143 * 0 otherwise (or on error)
1144 */
1145int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1146{
1147 uint8_t image_arch;
1148
1149 if (fit_image_get_arch(fit, noffset, &image_arch))
1150 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001151 return (arch == image_arch) ||
1152 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001153}
1154
1155/**
1156 * fit_image_check_type - check whether image node is of a given type
1157 * @fit: pointer to the FIT format image header
1158 * @noffset: component image node offset
1159 * @type: requested image type
1160 *
1161 * fit_image_check_type() reads image type property and compares its numeric
1162 * id with the requested type. Comparison result is returned to the caller.
1163 *
1164 * returns:
1165 * 1 if image is of given type
1166 * 0 otherwise (or on error)
1167 */
1168int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1169{
1170 uint8_t image_type;
1171
1172 if (fit_image_get_type(fit, noffset, &image_type))
1173 return 0;
1174 return (type == image_type);
1175}
1176
1177/**
1178 * fit_image_check_comp - check whether image node uses given compression
1179 * @fit: pointer to the FIT format image header
1180 * @noffset: component image node offset
1181 * @comp: requested image compression type
1182 *
1183 * fit_image_check_comp() reads image compression property and compares its
1184 * numeric id with the requested compression type. Comparison result is
1185 * returned to the caller.
1186 *
1187 * returns:
1188 * 1 if image uses requested compression
1189 * 0 otherwise (or on error)
1190 */
1191int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1192{
1193 uint8_t image_comp;
1194
1195 if (fit_image_get_comp(fit, noffset, &image_comp))
1196 return 0;
1197 return (comp == image_comp);
1198}
1199
1200/**
1201 * fit_check_format - sanity check FIT image format
1202 * @fit: pointer to the FIT format image header
1203 *
1204 * fit_check_format() runs a basic sanity FIT image verification.
1205 * Routine checks for mandatory properties, nodes, etc.
1206 *
1207 * returns:
1208 * 1, on success
1209 * 0, on failure
1210 */
1211int fit_check_format(const void *fit)
1212{
1213 /* mandatory / node 'description' property */
1214 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1215 debug("Wrong FIT format: no description\n");
1216 return 0;
1217 }
1218
1219 if (IMAGE_ENABLE_TIMESTAMP) {
1220 /* mandatory / node 'timestamp' property */
1221 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1222 debug("Wrong FIT format: no timestamp\n");
1223 return 0;
1224 }
1225 }
1226
1227 /* mandatory subimages parent '/images' node */
1228 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1229 debug("Wrong FIT format: no images parent node\n");
1230 return 0;
1231 }
1232
1233 return 1;
1234}
1235
1236
1237/**
1238 * fit_conf_find_compat
1239 * @fit: pointer to the FIT format image header
1240 * @fdt: pointer to the device tree to compare against
1241 *
1242 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1243 * most compatible with the passed in device tree.
1244 *
1245 * Example:
1246 *
1247 * / o image-tree
1248 * |-o images
1249 * | |-o fdt@1
1250 * | |-o fdt@2
1251 * |
1252 * |-o configurations
1253 * |-o config@1
1254 * | |-fdt = fdt@1
1255 * |
1256 * |-o config@2
1257 * |-fdt = fdt@2
1258 *
1259 * / o U-Boot fdt
1260 * |-compatible = "foo,bar", "bim,bam"
1261 *
1262 * / o kernel fdt1
1263 * |-compatible = "foo,bar",
1264 *
1265 * / o kernel fdt2
1266 * |-compatible = "bim,bam", "baz,biz"
1267 *
1268 * Configuration 1 would be picked because the first string in U-Boot's
1269 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1270 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1271 *
1272 * returns:
1273 * offset to the configuration to use if one was found
1274 * -1 otherwise
1275 */
1276int fit_conf_find_compat(const void *fit, const void *fdt)
1277{
1278 int ndepth = 0;
1279 int noffset, confs_noffset, images_noffset;
1280 const void *fdt_compat;
1281 int fdt_compat_len;
1282 int best_match_offset = 0;
1283 int best_match_pos = 0;
1284
1285 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1286 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1287 if (confs_noffset < 0 || images_noffset < 0) {
1288 debug("Can't find configurations or images nodes.\n");
1289 return -1;
1290 }
1291
1292 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1293 if (!fdt_compat) {
1294 debug("Fdt for comparison has no \"compatible\" property.\n");
1295 return -1;
1296 }
1297
1298 /*
1299 * Loop over the configurations in the FIT image.
1300 */
1301 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1302 (noffset >= 0) && (ndepth > 0);
1303 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1304 const void *kfdt;
1305 const char *kfdt_name;
1306 int kfdt_noffset;
1307 const char *cur_fdt_compat;
1308 int len;
1309 size_t size;
1310 int i;
1311
1312 if (ndepth > 1)
1313 continue;
1314
1315 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1316 if (!kfdt_name) {
1317 debug("No fdt property found.\n");
1318 continue;
1319 }
1320 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1321 kfdt_name);
1322 if (kfdt_noffset < 0) {
1323 debug("No image node named \"%s\" found.\n",
1324 kfdt_name);
1325 continue;
1326 }
1327 /*
1328 * Get a pointer to this configuration's fdt.
1329 */
1330 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1331 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1332 continue;
1333 }
1334
1335 len = fdt_compat_len;
1336 cur_fdt_compat = fdt_compat;
1337 /*
1338 * Look for a match for each U-Boot compatibility string in
1339 * turn in this configuration's fdt.
1340 */
1341 for (i = 0; len > 0 &&
1342 (!best_match_offset || best_match_pos > i); i++) {
1343 int cur_len = strlen(cur_fdt_compat) + 1;
1344
1345 if (!fdt_node_check_compatible(kfdt, 0,
1346 cur_fdt_compat)) {
1347 best_match_offset = noffset;
1348 best_match_pos = i;
1349 break;
1350 }
1351 len -= cur_len;
1352 cur_fdt_compat += cur_len;
1353 }
1354 }
1355 if (!best_match_offset) {
1356 debug("No match found.\n");
1357 return -1;
1358 }
1359
1360 return best_match_offset;
1361}
1362
1363/**
1364 * fit_conf_get_node - get node offset for configuration of a given unit name
1365 * @fit: pointer to the FIT format image header
1366 * @conf_uname: configuration node unit name
1367 *
1368 * fit_conf_get_node() finds a configuration (withing the '/configurations'
1369 * parant node) of a provided unit name. If configuration is found its node
1370 * offset is returned to the caller.
1371 *
1372 * When NULL is provided in second argument fit_conf_get_node() will search
1373 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001374 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001375 * node.
1376 *
1377 * returns:
1378 * configuration node offset when found (>=0)
1379 * negative number on failure (FDT_ERR_* code)
1380 */
1381int fit_conf_get_node(const void *fit, const char *conf_uname)
1382{
1383 int noffset, confs_noffset;
1384 int len;
1385
1386 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1387 if (confs_noffset < 0) {
1388 debug("Can't find configurations parent node '%s' (%s)\n",
1389 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1390 return confs_noffset;
1391 }
1392
1393 if (conf_uname == NULL) {
1394 /* get configuration unit name from the default property */
1395 debug("No configuration specified, trying default...\n");
1396 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1397 FIT_DEFAULT_PROP, &len);
1398 if (conf_uname == NULL) {
1399 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1400 len);
1401 return len;
1402 }
1403 debug("Found default configuration: '%s'\n", conf_uname);
1404 }
1405
1406 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1407 if (noffset < 0) {
1408 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1409 conf_uname, fdt_strerror(noffset));
1410 }
1411
1412 return noffset;
1413}
1414
Simon Glass003efd72013-05-07 06:12:00 +00001415int fit_conf_get_prop_node(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001416 const char *prop_name)
1417{
1418 char *uname;
1419 int len;
1420
1421 /* get kernel image unit name from configuration kernel property */
1422 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1423 if (uname == NULL)
1424 return len;
1425
1426 return fit_image_get_node(fit, uname);
1427}
1428
1429/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001430 * fit_conf_print - prints out the FIT configuration details
1431 * @fit: pointer to the FIT format image header
1432 * @noffset: offset of the configuration node
1433 * @p: pointer to prefix string
1434 *
1435 * fit_conf_print() lists all mandatory properies for the processed
1436 * configuration node.
1437 *
1438 * returns:
1439 * no returned results
1440 */
1441void fit_conf_print(const void *fit, int noffset, const char *p)
1442{
1443 char *desc;
1444 char *uname;
1445 int ret;
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001446 int loadables_index;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001447
1448 /* Mandatory properties */
1449 ret = fit_get_desc(fit, noffset, &desc);
1450 printf("%s Description: ", p);
1451 if (ret)
1452 printf("unavailable\n");
1453 else
1454 printf("%s\n", desc);
1455
1456 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1457 printf("%s Kernel: ", p);
1458 if (uname == NULL)
1459 printf("unavailable\n");
1460 else
1461 printf("%s\n", uname);
1462
1463 /* Optional properties */
1464 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1465 if (uname)
1466 printf("%s Init Ramdisk: %s\n", p, uname);
1467
1468 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1469 if (uname)
1470 printf("%s FDT: %s\n", p, uname);
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001471
1472 /* Print out all of the specified loadables */
1473 for (loadables_index = 0;
Hans de Goede5bcec542015-05-29 15:09:48 +02001474 fdt_get_string_index(fit, noffset,
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001475 FIT_LOADABLE_PROP,
1476 loadables_index,
Hans de Goede5bcec542015-05-29 15:09:48 +02001477 (const char **)&uname) == 0;
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001478 loadables_index++)
1479 {
1480 if (loadables_index == 0) {
1481 printf("%s Loadables: ", p);
1482 } else {
1483 printf("%s ", p);
1484 }
1485 printf("%s\n", uname);
1486 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001487}
1488
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001489static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001490{
1491 fit_image_print(fit, rd_noffset, " ");
1492
1493 if (verify) {
1494 puts(" Verifying Hash Integrity ... ");
1495 if (!fit_image_verify(fit, rd_noffset)) {
1496 puts("Bad Data Hash\n");
1497 return -EACCES;
1498 }
1499 puts("OK\n");
1500 }
1501
1502 return 0;
1503}
1504
Simon Glass782cfbb2013-05-16 13:53:21 +00001505int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1506 ulong addr)
1507{
1508 int cfg_noffset;
1509 void *fit_hdr;
1510 int noffset;
1511
1512 debug("* %s: using config '%s' from image at 0x%08lx\n",
1513 prop_name, images->fit_uname_cfg, addr);
1514
1515 /* Check whether configuration has this property defined */
1516 fit_hdr = map_sysmem(addr, 0);
1517 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1518 if (cfg_noffset < 0) {
1519 debug("* %s: no such config\n", prop_name);
1520 return -ENOENT;
1521 }
1522
1523 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1524 if (noffset < 0) {
1525 debug("* %s: no '%s' in config\n", prop_name, prop_name);
1526 return -ENOLINK;
1527 }
1528
1529 return noffset;
1530}
1531
Simon Glass126cc862014-06-12 07:24:47 -06001532/**
1533 * fit_get_image_type_property() - get property name for IH_TYPE_...
1534 *
1535 * @return the properly name where we expect to find the image in the
1536 * config node
1537 */
1538static const char *fit_get_image_type_property(int type)
1539{
1540 /*
1541 * This is sort-of available in the uimage_type[] table in image.c
1542 * but we don't have access to the sohrt name, and "fdt" is different
1543 * anyway. So let's just keep it here.
1544 */
1545 switch (type) {
1546 case IH_TYPE_FLATDT:
1547 return FIT_FDT_PROP;
1548 case IH_TYPE_KERNEL:
1549 return FIT_KERNEL_PROP;
1550 case IH_TYPE_RAMDISK:
1551 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001552 case IH_TYPE_X86_SETUP:
1553 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001554 case IH_TYPE_LOADABLE:
1555 return FIT_LOADABLE_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001556 }
1557
1558 return "unknown";
1559}
1560
1561int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001562 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001563 int arch, int image_type, int bootstage_id,
1564 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1565{
1566 int cfg_noffset, noffset;
1567 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001568 const char *fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001569 const void *fit;
1570 const void *buf;
1571 size_t size;
1572 int type_ok, os_ok;
1573 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001574 uint8_t os;
Simon Glass126cc862014-06-12 07:24:47 -06001575 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001576 int ret;
1577
1578 fit = map_sysmem(addr, 0);
1579 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001580 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001581 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001582 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1583
1584 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1585 if (!fit_check_format(fit)) {
1586 printf("Bad FIT %s image format!\n", prop_name);
1587 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1588 return -ENOEXEC;
1589 }
1590 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1591 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001592 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001593 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1594 noffset = fit_image_get_node(fit, fit_uname);
1595 } else {
1596 /*
1597 * no image node unit name, try to get config
1598 * node first. If config unit node name is NULL
1599 * fit_conf_get_node() will try to find default config node
1600 */
1601 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1602 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1603 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1604 } else {
1605 cfg_noffset = fit_conf_get_node(fit,
1606 fit_uname_config);
1607 }
1608 if (cfg_noffset < 0) {
1609 puts("Could not find configuration node\n");
1610 bootstage_error(bootstage_id +
1611 BOOTSTAGE_SUB_NO_UNIT_NAME);
1612 return -ENOENT;
1613 }
1614 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1615 printf(" Using '%s' configuration\n", fit_uname_config);
1616 if (image_type == IH_TYPE_KERNEL) {
1617 /* Remember (and possibly verify) this config */
1618 images->fit_uname_cfg = fit_uname_config;
1619 if (IMAGE_ENABLE_VERIFY && images->verify) {
1620 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001621 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001622 puts("Bad Data Hash\n");
1623 bootstage_error(bootstage_id +
1624 BOOTSTAGE_SUB_HASH);
1625 return -EACCES;
1626 }
1627 puts("OK\n");
1628 }
1629 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1630 }
1631
1632 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1633 prop_name);
1634 fit_uname = fit_get_name(fit, noffset, NULL);
1635 }
1636 if (noffset < 0) {
1637 puts("Could not find subimage node\n");
1638 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1639 return -ENOENT;
1640 }
1641
1642 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1643
1644 ret = fit_image_select(fit, noffset, images->verify);
1645 if (ret) {
1646 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1647 return ret;
1648 }
1649
1650 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001651#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001652 if (!fit_image_check_target_arch(fit, noffset)) {
1653 puts("Unsupported Architecture\n");
1654 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1655 return -ENOEXEC;
1656 }
Simon Glassce1400f2014-06-12 07:24:53 -06001657#endif
Simon Glass782cfbb2013-05-16 13:53:21 +00001658 if (image_type == IH_TYPE_FLATDT &&
1659 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1660 puts("FDT image is compressed");
1661 return -EPROTONOSUPPORT;
1662 }
1663
1664 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1665 type_ok = fit_image_check_type(fit, noffset, image_type) ||
1666 (image_type == IH_TYPE_KERNEL &&
1667 fit_image_check_type(fit, noffset,
1668 IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001669
Simon Glass782cfbb2013-05-16 13:53:21 +00001670 os_ok = image_type == IH_TYPE_FLATDT ||
Marek Vasut38117232014-12-16 14:07:22 +01001671 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
1672 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001673
1674 /*
1675 * If either of the checks fail, we should report an error, but
1676 * if the image type is coming from the "loadables" field, we
1677 * don't care what it is
1678 */
1679 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001680 fit_image_get_os(fit, noffset, &os);
1681 printf("No %s %s %s Image\n",
1682 genimg_get_os_name(os),
1683 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001684 genimg_get_type_name(image_type));
1685 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1686 return -EIO;
1687 }
1688
1689 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1690
1691 /* get image data address and length */
1692 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1693 printf("Could not find %s subimage data!\n", prop_name);
1694 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001695 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001696 }
1697 len = (ulong)size;
1698
1699 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001700 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001701 puts("Subimage data is not a FDT");
1702 return -ENOEXEC;
1703 }
1704
1705 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1706
1707 /*
1708 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001709 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001710 * warning: initialization discards qualifiers from pointer target type
1711 */
1712 {
1713 void *vbuf = (void *)buf;
1714
1715 data = map_to_sysmem(vbuf);
1716 }
1717
1718 if (load_op == FIT_LOAD_IGNORED) {
1719 /* Don't load */
1720 } else if (fit_image_get_load(fit, noffset, &load)) {
1721 if (load_op == FIT_LOAD_REQUIRED) {
1722 printf("Can't get %s subimage load address!\n",
1723 prop_name);
1724 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1725 return -EBADF;
1726 }
Simon Glassfe20a812014-08-22 14:26:43 -06001727 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001728 ulong image_start, image_end;
1729 ulong load_end;
1730 void *dst;
1731
1732 /*
1733 * move image data to the load address,
1734 * make sure we don't overwrite initial image
1735 */
1736 image_start = addr;
1737 image_end = addr + fit_get_size(fit);
1738
1739 load_end = load + len;
1740 if (image_type != IH_TYPE_KERNEL &&
1741 load < image_end && load_end > image_start) {
1742 printf("Error: %s overwritten\n", prop_name);
1743 return -EXDEV;
1744 }
1745
1746 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1747 prop_name, data, load);
1748
1749 dst = map_sysmem(load, len);
1750 memmove(dst, buf, len);
1751 data = load;
1752 }
1753 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1754
1755 *datap = data;
1756 *lenp = len;
1757 if (fit_unamep)
1758 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001759 if (fit_uname_configp)
1760 *fit_uname_configp = (char *)fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001761
1762 return noffset;
1763}
Simon Glass90268b82014-10-19 21:11:24 -06001764
1765int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1766 ulong *setup_start, ulong *setup_len)
1767{
1768 int noffset;
1769 ulong addr;
1770 ulong len;
1771 int ret;
1772
1773 addr = map_to_sysmem(images->fit_hdr_os);
1774 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1775 if (noffset < 0)
1776 return noffset;
1777
1778 ret = fit_image_load(images, addr, NULL, NULL, arch,
1779 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1780 FIT_LOAD_REQUIRED, setup_start, &len);
1781
1782 return ret;
1783}