blob: 9ce68f1c21d4408611877fbe7fe88a64de7939c0 [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"
Simon Glass53fbb7e2013-05-07 06:11:53 +000014#include <time.h>
15#else
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050016#include <linux/compiler.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000017#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
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -050024#include <image.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000025#include <bootstage.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000026#include <u-boot/crc.h>
27#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020028#include <u-boot/sha1.h>
29#include <u-boot/sha256.h>
Simon Glass53fbb7e2013-05-07 06:11:53 +000030
31/*****************************************************************************/
32/* New uImage format routines */
33/*****************************************************************************/
34#ifndef USE_HOSTCC
35static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
36 ulong *addr, const char **name)
37{
38 const char *sep;
39
40 *addr = addr_curr;
41 *name = NULL;
42
43 sep = strchr(spec, sepc);
44 if (sep) {
45 if (sep - spec > 0)
46 *addr = simple_strtoul(spec, NULL, 16);
47
48 *name = sep + 1;
49 return 1;
50 }
51
52 return 0;
53}
54
55/**
56 * fit_parse_conf - parse FIT configuration spec
57 * @spec: input string, containing configuration spec
58 * @add_curr: current image address (to be used as a possible default)
59 * @addr: pointer to a ulong variable, will hold FIT image address of a given
60 * configuration
61 * @conf_name double pointer to a char, will hold pointer to a configuration
62 * unit name
63 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090064 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
Simon Glass53fbb7e2013-05-07 06:11:53 +000065 * where <addr> is a FIT image address that contains configuration
66 * with a <conf> unit name.
67 *
68 * Address part is optional, and if omitted default add_curr will
69 * be used instead.
70 *
71 * returns:
72 * 1 if spec is a valid configuration string,
73 * addr and conf_name are set accordingly
74 * 0 otherwise
75 */
76int fit_parse_conf(const char *spec, ulong addr_curr,
77 ulong *addr, const char **conf_name)
78{
79 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
80}
81
82/**
83 * fit_parse_subimage - parse FIT subimage spec
84 * @spec: input string, containing subimage spec
85 * @add_curr: current image address (to be used as a possible default)
86 * @addr: pointer to a ulong variable, will hold FIT image address of a given
87 * subimage
88 * @image_name: double pointer to a char, will hold pointer to a subimage name
89 *
Masahiro Yamada069d5942013-09-18 09:36:38 +090090 * fit_parse_subimage() expects subimage spec in the form of
Simon Glass53fbb7e2013-05-07 06:11:53 +000091 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
92 * subimage with a <subimg> unit name.
93 *
94 * Address part is optional, and if omitted default add_curr will
95 * be used instead.
96 *
97 * returns:
98 * 1 if spec is a valid subimage string,
99 * addr and image_name are set accordingly
100 * 0 otherwise
101 */
102int fit_parse_subimage(const char *spec, ulong addr_curr,
103 ulong *addr, const char **image_name)
104{
105 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
106}
107#endif /* !USE_HOSTCC */
108
109static void fit_get_debug(const void *fit, int noffset,
110 char *prop_name, int err)
111{
112 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
113 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
114 fdt_strerror(err));
115}
116
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200117/**
118 * fit_get_subimage_count - get component (sub-image) count
119 * @fit: pointer to the FIT format image header
120 * @images_noffset: offset of images node
121 *
122 * returns:
123 * number of image components
124 */
125int fit_get_subimage_count(const void *fit, int images_noffset)
126{
127 int noffset;
128 int ndepth;
129 int count = 0;
130
131 /* Process its subnodes, print out component images details */
132 for (ndepth = 0, count = 0,
133 noffset = fdt_next_node(fit, images_noffset, &ndepth);
134 (noffset >= 0) && (ndepth > 0);
135 noffset = fdt_next_node(fit, noffset, &ndepth)) {
136 if (ndepth == 1) {
137 count++;
138 }
139 }
140
141 return count;
142}
143
Simon Glass87ebee32013-05-08 08:05:59 +0000144#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000145/**
146 * fit_print_contents - prints out the contents of the FIT format image
147 * @fit: pointer to the FIT format image header
148 * @p: pointer to prefix string
149 *
150 * fit_print_contents() formats a multi line FIT image contents description.
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500151 * The routine prints out FIT image properties (root node level) followed by
Simon Glass53fbb7e2013-05-07 06:11:53 +0000152 * the details of each component image.
153 *
154 * returns:
155 * no returned results
156 */
157void fit_print_contents(const void *fit)
158{
159 char *desc;
160 char *uname;
161 int images_noffset;
162 int confs_noffset;
163 int noffset;
164 int ndepth;
165 int count = 0;
166 int ret;
167 const char *p;
168 time_t timestamp;
169
Simon Glass1fe7d932013-05-08 08:05:58 +0000170 /* Indent string is defined in header image.h */
171 p = IMAGE_INDENT_STRING;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000172
173 /* Root node properties */
174 ret = fit_get_desc(fit, 0, &desc);
175 printf("%sFIT description: ", p);
176 if (ret)
177 printf("unavailable\n");
178 else
179 printf("%s\n", desc);
180
181 if (IMAGE_ENABLE_TIMESTAMP) {
182 ret = fit_get_timestamp(fit, 0, &timestamp);
183 printf("%sCreated: ", p);
184 if (ret)
185 printf("unavailable\n");
186 else
187 genimg_print_time(timestamp);
188 }
189
190 /* Find images parent node offset */
191 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
192 if (images_noffset < 0) {
193 printf("Can't find images parent node '%s' (%s)\n",
194 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
195 return;
196 }
197
198 /* Process its subnodes, print out component images details */
199 for (ndepth = 0, count = 0,
200 noffset = fdt_next_node(fit, images_noffset, &ndepth);
201 (noffset >= 0) && (ndepth > 0);
202 noffset = fdt_next_node(fit, noffset, &ndepth)) {
203 if (ndepth == 1) {
204 /*
205 * Direct child node of the images parent node,
206 * i.e. component image node.
207 */
208 printf("%s Image %u (%s)\n", p, count++,
209 fit_get_name(fit, noffset, NULL));
210
211 fit_image_print(fit, noffset, p);
212 }
213 }
214
215 /* Find configurations parent node offset */
216 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
217 if (confs_noffset < 0) {
218 debug("Can't get configurations parent node '%s' (%s)\n",
219 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
220 return;
221 }
222
223 /* get default configuration unit name from default property */
224 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
225 if (uname)
226 printf("%s Default Configuration: '%s'\n", p, uname);
227
228 /* Process its subnodes, print out configurations details */
229 for (ndepth = 0, count = 0,
230 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
231 (noffset >= 0) && (ndepth > 0);
232 noffset = fdt_next_node(fit, noffset, &ndepth)) {
233 if (ndepth == 1) {
234 /*
235 * Direct child node of the configurations parent node,
236 * i.e. configuration node.
237 */
238 printf("%s Configuration %u (%s)\n", p, count++,
239 fit_get_name(fit, noffset, NULL));
240
241 fit_conf_print(fit, noffset, p);
242 }
243 }
244}
245
246/**
Simon Glassd8b75362013-05-07 06:12:02 +0000247 * fit_image_print_data() - prints out the hash node details
248 * @fit: pointer to the FIT format image header
249 * @noffset: offset of the hash node
250 * @p: pointer to prefix string
Simon Glass56518e72013-06-13 15:10:01 -0700251 * @type: Type of information to print ("hash" or "sign")
Simon Glassd8b75362013-05-07 06:12:02 +0000252 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500253 * fit_image_print_data() lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000254 *
Simon Glass56518e72013-06-13 15:10:01 -0700255 * This function avoid using puts() since it prints a newline on the host
256 * but does not in U-Boot.
257 *
Simon Glassd8b75362013-05-07 06:12:02 +0000258 * returns:
259 * no returned results
260 */
Simon Glass56518e72013-06-13 15:10:01 -0700261static void fit_image_print_data(const void *fit, int noffset, const char *p,
262 const char *type)
Simon Glassd8b75362013-05-07 06:12:02 +0000263{
Simon Glass56518e72013-06-13 15:10:01 -0700264 const char *keyname;
Simon Glassd8b75362013-05-07 06:12:02 +0000265 uint8_t *value;
266 int value_len;
Simon Glass56518e72013-06-13 15:10:01 -0700267 char *algo;
268 int required;
269 int ret, i;
Simon Glassd8b75362013-05-07 06:12:02 +0000270
Simon Glass56518e72013-06-13 15:10:01 -0700271 debug("%s %s node: '%s'\n", p, type,
Simon Glassd8b75362013-05-07 06:12:02 +0000272 fit_get_name(fit, noffset, NULL));
Simon Glass56518e72013-06-13 15:10:01 -0700273 printf("%s %s algo: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000274 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
275 printf("invalid/unsupported\n");
276 return;
277 }
Simon Glass56518e72013-06-13 15:10:01 -0700278 printf("%s", algo);
279 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
280 required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
281 if (keyname)
282 printf(":%s", keyname);
283 if (required)
284 printf(" (required)");
285 printf("\n");
Simon Glassd8b75362013-05-07 06:12:02 +0000286
287 ret = fit_image_hash_get_value(fit, noffset, &value,
288 &value_len);
Simon Glass56518e72013-06-13 15:10:01 -0700289 printf("%s %s value: ", p, type);
Simon Glassd8b75362013-05-07 06:12:02 +0000290 if (ret) {
291 printf("unavailable\n");
292 } else {
293 for (i = 0; i < value_len; i++)
294 printf("%02x", value[i]);
295 printf("\n");
296 }
297
Simon Glass56518e72013-06-13 15:10:01 -0700298 debug("%s %s len: %d\n", p, type, value_len);
299
300 /* Signatures have a time stamp */
301 if (IMAGE_ENABLE_TIMESTAMP && keyname) {
302 time_t timestamp;
303
304 printf("%s Timestamp: ", p);
305 if (fit_get_timestamp(fit, noffset, &timestamp))
306 printf("unavailable\n");
307 else
308 genimg_print_time(timestamp);
309 }
Simon Glassd8b75362013-05-07 06:12:02 +0000310}
311
312/**
313 * fit_image_print_verification_data() - prints out the hash/signature details
314 * @fit: pointer to the FIT format image header
315 * @noffset: offset of the hash or signature node
316 * @p: pointer to prefix string
317 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500318 * This lists properties for the processed hash node
Simon Glassd8b75362013-05-07 06:12:02 +0000319 *
320 * returns:
321 * no returned results
322 */
323static void fit_image_print_verification_data(const void *fit, int noffset,
324 const char *p)
325{
326 const char *name;
327
328 /*
329 * Check subnode name, must be equal to "hash" or "signature".
330 * Multiple hash/signature nodes require unique unit node
331 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc.
332 */
333 name = fit_get_name(fit, noffset, NULL);
Simon Glass56518e72013-06-13 15:10:01 -0700334 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
335 fit_image_print_data(fit, noffset, p, "Hash");
336 } else if (!strncmp(name, FIT_SIG_NODENAME,
337 strlen(FIT_SIG_NODENAME))) {
338 fit_image_print_data(fit, noffset, p, "Sign");
339 }
Simon Glassd8b75362013-05-07 06:12:02 +0000340}
341
342/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000343 * fit_image_print - prints out the FIT component image details
344 * @fit: pointer to the FIT format image header
345 * @image_noffset: offset of the component image node
346 * @p: pointer to prefix string
347 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500348 * fit_image_print() lists all mandatory properties for the processed component
Simon Glass53fbb7e2013-05-07 06:11:53 +0000349 * image. If present, hash nodes are printed out as well. Load
350 * address for images of type firmware is also printed out. Since the load
351 * address is not mandatory for firmware images, it will be output as
352 * "unavailable" when not present.
353 *
354 * returns:
355 * no returned results
356 */
357void fit_image_print(const void *fit, int image_noffset, const char *p)
358{
359 char *desc;
360 uint8_t type, arch, os, comp;
361 size_t size;
362 ulong load, entry;
363 const void *data;
364 int noffset;
365 int ndepth;
366 int ret;
367
368 /* Mandatory properties */
369 ret = fit_get_desc(fit, image_noffset, &desc);
370 printf("%s Description: ", p);
371 if (ret)
372 printf("unavailable\n");
373 else
374 printf("%s\n", desc);
375
Simon Glass1fd1e2f2013-07-16 20:10:01 -0700376 if (IMAGE_ENABLE_TIMESTAMP) {
377 time_t timestamp;
378
379 ret = fit_get_timestamp(fit, 0, &timestamp);
380 printf("%s Created: ", p);
381 if (ret)
382 printf("unavailable\n");
383 else
384 genimg_print_time(timestamp);
385 }
386
Simon Glass53fbb7e2013-05-07 06:11:53 +0000387 fit_image_get_type(fit, image_noffset, &type);
388 printf("%s Type: %s\n", p, genimg_get_type_name(type));
389
390 fit_image_get_comp(fit, image_noffset, &comp);
391 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
392
393 ret = fit_image_get_data(fit, image_noffset, &data, &size);
394
395#ifndef USE_HOSTCC
396 printf("%s Data Start: ", p);
Simon Glassc6ac13b2013-05-16 13:53:26 +0000397 if (ret) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000398 printf("unavailable\n");
Simon Glassc6ac13b2013-05-16 13:53:26 +0000399 } else {
400 void *vdata = (void *)data;
401
402 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
403 }
Simon Glass53fbb7e2013-05-07 06:11:53 +0000404#endif
405
406 printf("%s Data Size: ", p);
407 if (ret)
408 printf("unavailable\n");
409 else
410 genimg_print_size(size);
411
412 /* Remaining, type dependent properties */
413 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
414 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
415 (type == IH_TYPE_FLATDT)) {
416 fit_image_get_arch(fit, image_noffset, &arch);
417 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
418 }
419
420 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
421 fit_image_get_os(fit, image_noffset, &os);
422 printf("%s OS: %s\n", p, genimg_get_os_name(os));
423 }
424
425 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
Michal Simek62afc602016-05-17 14:03:50 +0200426 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
427 (type == IH_TYPE_FPGA)) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000428 ret = fit_image_get_load(fit, image_noffset, &load);
429 printf("%s Load Address: ", p);
430 if (ret)
431 printf("unavailable\n");
432 else
433 printf("0x%08lx\n", load);
434 }
435
436 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
437 (type == IH_TYPE_RAMDISK)) {
York Sun60047652016-02-29 15:48:40 -0800438 ret = fit_image_get_entry(fit, image_noffset, &entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000439 printf("%s Entry Point: ", p);
440 if (ret)
441 printf("unavailable\n");
442 else
443 printf("0x%08lx\n", entry);
444 }
445
446 /* Process all hash subnodes of the component image node */
447 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
448 (noffset >= 0) && (ndepth > 0);
449 noffset = fdt_next_node(fit, noffset, &ndepth)) {
450 if (ndepth == 1) {
451 /* Direct child node of the component image node */
Simon Glassd8b75362013-05-07 06:12:02 +0000452 fit_image_print_verification_data(fit, noffset, p);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000453 }
454 }
455}
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200456
457#endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
Simon Glass53fbb7e2013-05-07 06:11:53 +0000458
459/**
Simon Glass53fbb7e2013-05-07 06:11:53 +0000460 * fit_get_desc - get node description property
461 * @fit: pointer to the FIT format image header
462 * @noffset: node offset
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500463 * @desc: double pointer to the char, will hold pointer to the description
Simon Glass53fbb7e2013-05-07 06:11:53 +0000464 *
465 * fit_get_desc() reads description property from a given node, if
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500466 * description is found pointer to it is returned in third call argument.
Simon Glass53fbb7e2013-05-07 06:11:53 +0000467 *
468 * returns:
469 * 0, on success
470 * -1, on failure
471 */
472int fit_get_desc(const void *fit, int noffset, char **desc)
473{
474 int len;
475
476 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
477 if (*desc == NULL) {
478 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
479 return -1;
480 }
481
482 return 0;
483}
484
485/**
486 * fit_get_timestamp - get node timestamp property
487 * @fit: pointer to the FIT format image header
488 * @noffset: node offset
489 * @timestamp: pointer to the time_t, will hold read timestamp
490 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500491 * fit_get_timestamp() reads timestamp property from given node, if timestamp
492 * is found and has a correct size its value is returned in third call
Simon Glass53fbb7e2013-05-07 06:11:53 +0000493 * argument.
494 *
495 * returns:
496 * 0, on success
497 * -1, on property read failure
498 * -2, on wrong timestamp size
499 */
500int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
501{
502 int len;
503 const void *data;
504
505 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
506 if (data == NULL) {
507 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
508 return -1;
509 }
510 if (len != sizeof(uint32_t)) {
511 debug("FIT timestamp with incorrect size of (%u)\n", len);
512 return -2;
513 }
514
515 *timestamp = uimage_to_cpu(*((uint32_t *)data));
516 return 0;
517}
518
519/**
520 * fit_image_get_node - get node offset for component image of a given unit name
521 * @fit: pointer to the FIT format image header
522 * @image_uname: component image node unit name
523 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500524 * fit_image_get_node() finds a component image (within the '/images'
Simon Glass53fbb7e2013-05-07 06:11:53 +0000525 * node) of a provided unit name. If image is found its node offset is
526 * returned to the caller.
527 *
528 * returns:
529 * image node offset when found (>=0)
530 * negative number on failure (FDT_ERR_* code)
531 */
532int fit_image_get_node(const void *fit, const char *image_uname)
533{
534 int noffset, images_noffset;
535
536 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
537 if (images_noffset < 0) {
538 debug("Can't find images parent node '%s' (%s)\n",
539 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
540 return images_noffset;
541 }
542
543 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
544 if (noffset < 0) {
545 debug("Can't get node offset for image unit name: '%s' (%s)\n",
546 image_uname, fdt_strerror(noffset));
547 }
548
549 return noffset;
550}
551
552/**
553 * fit_image_get_os - get os id for a given component image node
554 * @fit: pointer to the FIT format image header
555 * @noffset: component image node offset
556 * @os: pointer to the uint8_t, will hold os numeric id
557 *
558 * fit_image_get_os() finds os property in a given component image node.
559 * If the property is found, its (string) value is translated to the numeric
560 * id which is returned to the caller.
561 *
562 * returns:
563 * 0, on success
564 * -1, on failure
565 */
566int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
567{
568 int len;
569 const void *data;
570
571 /* Get OS name from property data */
572 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
573 if (data == NULL) {
574 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
575 *os = -1;
576 return -1;
577 }
578
579 /* Translate OS name to id */
580 *os = genimg_get_os_id(data);
581 return 0;
582}
583
584/**
585 * fit_image_get_arch - get arch id for a given component image node
586 * @fit: pointer to the FIT format image header
587 * @noffset: component image node offset
588 * @arch: pointer to the uint8_t, will hold arch numeric id
589 *
590 * fit_image_get_arch() finds arch property in a given component image node.
591 * If the property is found, its (string) value is translated to the numeric
592 * id which is returned to the caller.
593 *
594 * returns:
595 * 0, on success
596 * -1, on failure
597 */
598int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
599{
600 int len;
601 const void *data;
602
603 /* Get architecture name from property data */
604 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
605 if (data == NULL) {
606 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
607 *arch = -1;
608 return -1;
609 }
610
611 /* Translate architecture name to id */
612 *arch = genimg_get_arch_id(data);
613 return 0;
614}
615
616/**
617 * fit_image_get_type - get type id for a given component image node
618 * @fit: pointer to the FIT format image header
619 * @noffset: component image node offset
620 * @type: pointer to the uint8_t, will hold type numeric id
621 *
622 * fit_image_get_type() finds type property in a given component image node.
623 * If the property is found, its (string) value is translated to the numeric
624 * id which is returned to the caller.
625 *
626 * returns:
627 * 0, on success
628 * -1, on failure
629 */
630int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
631{
632 int len;
633 const void *data;
634
635 /* Get image type name from property data */
636 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
637 if (data == NULL) {
638 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
639 *type = -1;
640 return -1;
641 }
642
643 /* Translate image type name to id */
644 *type = genimg_get_type_id(data);
645 return 0;
646}
647
648/**
649 * fit_image_get_comp - get comp id for a given component image node
650 * @fit: pointer to the FIT format image header
651 * @noffset: component image node offset
652 * @comp: pointer to the uint8_t, will hold comp numeric id
653 *
654 * fit_image_get_comp() finds comp property in a given component image node.
655 * If the property is found, its (string) value is translated to the numeric
656 * id which is returned to the caller.
657 *
658 * returns:
659 * 0, on success
660 * -1, on failure
661 */
662int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
663{
664 int len;
665 const void *data;
666
667 /* Get compression name from property data */
668 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
669 if (data == NULL) {
670 fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
671 *comp = -1;
672 return -1;
673 }
674
675 /* Translate compression name to id */
676 *comp = genimg_get_comp_id(data);
677 return 0;
678}
679
York Sun60047652016-02-29 15:48:40 -0800680static int fit_image_get_address(const void *fit, int noffset, char *name,
681 ulong *load)
682{
York Sunc1913cb2016-02-29 15:48:41 -0800683 int len, cell_len;
684 const fdt32_t *cell;
685 uint64_t load64 = 0;
York Sun60047652016-02-29 15:48:40 -0800686
York Sunc1913cb2016-02-29 15:48:41 -0800687 cell = fdt_getprop(fit, noffset, name, &len);
688 if (cell == NULL) {
York Sun60047652016-02-29 15:48:40 -0800689 fit_get_debug(fit, noffset, name, len);
690 return -1;
691 }
692
York Sunc1913cb2016-02-29 15:48:41 -0800693 if (len > sizeof(ulong)) {
694 printf("Unsupported %s address size\n", name);
695 return -1;
696 }
697
698 cell_len = len >> 2;
699 /* Use load64 to avoid compiling warning for 32-bit target */
700 while (cell_len--) {
701 load64 = (load64 << 32) | uimage_to_cpu(*cell);
702 cell++;
703 }
704 *load = (ulong)load64;
York Sun60047652016-02-29 15:48:40 -0800705
706 return 0;
707}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000708/**
709 * fit_image_get_load() - get load addr property for given component image node
710 * @fit: pointer to the FIT format image header
711 * @noffset: component image node offset
712 * @load: pointer to the uint32_t, will hold load address
713 *
714 * fit_image_get_load() finds load address property in a given component
715 * image node. If the property is found, its value is returned to the caller.
716 *
717 * returns:
718 * 0, on success
719 * -1, on failure
720 */
721int fit_image_get_load(const void *fit, int noffset, ulong *load)
722{
York Sun60047652016-02-29 15:48:40 -0800723 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000724}
725
726/**
727 * fit_image_get_entry() - get entry point address property
728 * @fit: pointer to the FIT format image header
729 * @noffset: component image node offset
730 * @entry: pointer to the uint32_t, will hold entry point address
731 *
732 * This gets the entry point address property for a given component image
733 * node.
734 *
735 * fit_image_get_entry() finds entry point address property in a given
736 * component image node. If the property is found, its value is returned
737 * to the caller.
738 *
739 * returns:
740 * 0, on success
741 * -1, on failure
742 */
743int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
744{
York Sun60047652016-02-29 15:48:40 -0800745 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
Simon Glass53fbb7e2013-05-07 06:11:53 +0000746}
747
748/**
749 * fit_image_get_data - get data property and its size for a given component image node
750 * @fit: pointer to the FIT format image header
751 * @noffset: component image node offset
752 * @data: double pointer to void, will hold data property's data address
753 * @size: pointer to size_t, will hold data property's data size
754 *
755 * fit_image_get_data() finds data property in a given component image node.
756 * If the property is found its data start address and size are returned to
757 * the caller.
758 *
759 * returns:
760 * 0, on success
761 * -1, on failure
762 */
763int fit_image_get_data(const void *fit, int noffset,
764 const void **data, size_t *size)
765{
766 int len;
767
768 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
769 if (*data == NULL) {
770 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
771 *size = 0;
772 return -1;
773 }
774
775 *size = len;
776 return 0;
777}
778
779/**
780 * fit_image_hash_get_algo - get hash algorithm name
781 * @fit: pointer to the FIT format image header
782 * @noffset: hash node offset
783 * @algo: double pointer to char, will hold pointer to the algorithm name
784 *
785 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
786 * If the property is found its data start address is returned to the caller.
787 *
788 * returns:
789 * 0, on success
790 * -1, on failure
791 */
792int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
793{
794 int len;
795
796 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
797 if (*algo == NULL) {
798 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
799 return -1;
800 }
801
802 return 0;
803}
804
805/**
806 * fit_image_hash_get_value - get hash value and length
807 * @fit: pointer to the FIT format image header
808 * @noffset: hash node offset
809 * @value: double pointer to uint8_t, will hold address of a hash value data
810 * @value_len: pointer to an int, will hold hash data length
811 *
812 * fit_image_hash_get_value() finds hash value property in a given hash node.
813 * If the property is found its data start address and size are returned to
814 * the caller.
815 *
816 * returns:
817 * 0, on success
818 * -1, on failure
819 */
820int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
821 int *value_len)
822{
823 int len;
824
825 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
826 if (*value == NULL) {
827 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
828 *value_len = 0;
829 return -1;
830 }
831
832 *value_len = len;
833 return 0;
834}
835
Simon Glass53fbb7e2013-05-07 06:11:53 +0000836/**
837 * fit_image_hash_get_ignore - get hash ignore flag
838 * @fit: pointer to the FIT format image header
839 * @noffset: hash node offset
840 * @ignore: pointer to an int, will hold hash ignore flag
841 *
842 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
843 * If the property is found and non-zero, the hash algorithm is not verified by
844 * u-boot automatically.
845 *
846 * returns:
847 * 0, on ignore not found
848 * value, on ignore found
849 */
Simon Glassab9efc62013-05-07 06:11:58 +0000850static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
Simon Glass53fbb7e2013-05-07 06:11:53 +0000851{
852 int len;
853 int *value;
854
855 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
856 if (value == NULL || len != sizeof(int))
857 *ignore = 0;
858 else
859 *ignore = *value;
860
861 return 0;
862}
Simon Glass53fbb7e2013-05-07 06:11:53 +0000863
Simon Glass7a80de42016-02-24 09:14:42 -0700864ulong fit_get_end(const void *fit)
865{
866 return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
867}
868
Simon Glass53fbb7e2013-05-07 06:11:53 +0000869/**
870 * fit_set_timestamp - set node timestamp property
871 * @fit: pointer to the FIT format image header
872 * @noffset: node offset
873 * @timestamp: timestamp value to be set
874 *
875 * fit_set_timestamp() attempts to set timestamp property in the requested
876 * node and returns operation status to the caller.
877 *
878 * returns:
879 * 0, on success
Simon Glass4f427a42014-06-02 22:04:51 -0600880 * -ENOSPC if no space in device tree, -1 for other error
Simon Glass53fbb7e2013-05-07 06:11:53 +0000881 */
882int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
883{
884 uint32_t t;
885 int ret;
886
887 t = cpu_to_uimage(timestamp);
888 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
889 sizeof(uint32_t));
890 if (ret) {
Simon Glass8df81e12016-05-01 13:55:37 -0600891 debug("Can't set '%s' property for '%s' node (%s)\n",
892 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
893 fdt_strerror(ret));
Simon Glass4f427a42014-06-02 22:04:51 -0600894 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
Simon Glass53fbb7e2013-05-07 06:11:53 +0000895 }
896
897 return 0;
898}
899
900/**
901 * calculate_hash - calculate and return hash for provided input data
902 * @data: pointer to the input data
903 * @data_len: data length
904 * @algo: requested hash algorithm
905 * @value: pointer to the char, will hold hash value data (caller must
906 * allocate enough free space)
907 * value_len: length of the calculated hash
908 *
909 * calculate_hash() computes input data hash according to the requested
910 * algorithm.
911 * Resulting hash value is placed in caller provided 'value' buffer, length
912 * of the calculated hash is returned via value_len pointer argument.
913 *
914 * returns:
915 * 0, on success
916 * -1, when algo is unsupported
917 */
Simon Glass604f23d2013-05-07 06:11:54 +0000918int calculate_hash(const void *data, int data_len, const char *algo,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000919 uint8_t *value, int *value_len)
920{
Simon Glass87ebee32013-05-08 08:05:59 +0000921 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000922 *((uint32_t *)value) = crc32_wd(0, data, data_len,
923 CHUNKSZ_CRC32);
924 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
925 *value_len = 4;
Simon Glass87ebee32013-05-08 08:05:59 +0000926 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000927 sha1_csum_wd((unsigned char *)data, data_len,
928 (unsigned char *)value, CHUNKSZ_SHA1);
929 *value_len = 20;
Heiko Schocher2842c1c2014-03-03 12:19:25 +0100930 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
931 sha256_csum_wd((unsigned char *)data, data_len,
932 (unsigned char *)value, CHUNKSZ_SHA256);
933 *value_len = SHA256_SUM_LEN;
Simon Glass87ebee32013-05-08 08:05:59 +0000934 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
Simon Glass53fbb7e2013-05-07 06:11:53 +0000935 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
936 *value_len = 16;
937 } else {
938 debug("Unsupported hash alogrithm\n");
939 return -1;
940 }
941 return 0;
942}
943
Simon Glassab9efc62013-05-07 06:11:58 +0000944static int fit_image_check_hash(const void *fit, int noffset, const void *data,
945 size_t size, char **err_msgp)
946{
947 uint8_t value[FIT_MAX_HASH_LEN];
948 int value_len;
949 char *algo;
950 uint8_t *fit_value;
951 int fit_value_len;
952 int ignore;
953
954 *err_msgp = NULL;
955
956 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Simon Glasse754da22013-05-07 06:11:59 +0000957 *err_msgp = "Can't get hash algo property";
Simon Glassab9efc62013-05-07 06:11:58 +0000958 return -1;
959 }
960 printf("%s", algo);
961
962 if (IMAGE_ENABLE_IGNORE) {
963 fit_image_hash_get_ignore(fit, noffset, &ignore);
964 if (ignore) {
965 printf("-skipped ");
966 return 0;
967 }
968 }
969
970 if (fit_image_hash_get_value(fit, noffset, &fit_value,
971 &fit_value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +0000972 *err_msgp = "Can't get hash value property";
Simon Glassab9efc62013-05-07 06:11:58 +0000973 return -1;
974 }
975
976 if (calculate_hash(data, size, algo, value, &value_len)) {
Simon Glasse754da22013-05-07 06:11:59 +0000977 *err_msgp = "Unsupported hash algorithm";
Simon Glassab9efc62013-05-07 06:11:58 +0000978 return -1;
979 }
980
981 if (value_len != fit_value_len) {
Simon Glasse754da22013-05-07 06:11:59 +0000982 *err_msgp = "Bad hash value len";
Simon Glassab9efc62013-05-07 06:11:58 +0000983 return -1;
984 } else if (memcmp(value, fit_value, value_len) != 0) {
Simon Glasse754da22013-05-07 06:11:59 +0000985 *err_msgp = "Bad hash value";
Simon Glassab9efc62013-05-07 06:11:58 +0000986 return -1;
987 }
988
989 return 0;
990}
991
Simon Glass53fbb7e2013-05-07 06:11:53 +0000992/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -0500993 * fit_image_verify - verify data integrity
Simon Glass53fbb7e2013-05-07 06:11:53 +0000994 * @fit: pointer to the FIT format image header
995 * @image_noffset: component image node offset
996 *
Simon Glassb8da8362013-05-07 06:11:57 +0000997 * fit_image_verify() goes over component image hash nodes,
Simon Glass53fbb7e2013-05-07 06:11:53 +0000998 * re-calculates each data hash and compares with the value stored in hash
999 * node.
1000 *
1001 * returns:
1002 * 1, if all hashes are valid
1003 * 0, otherwise (or on error)
1004 */
Simon Glassb8da8362013-05-07 06:11:57 +00001005int fit_image_verify(const void *fit, int image_noffset)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001006{
1007 const void *data;
1008 size_t size;
Simon Glass56518e72013-06-13 15:10:01 -07001009 int noffset = 0;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001010 char *err_msg = "";
Simon Glass56518e72013-06-13 15:10:01 -07001011 int verify_all = 1;
1012 int ret;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001013
1014 /* Get image data and data length */
1015 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
Simon Glasse754da22013-05-07 06:11:59 +00001016 err_msg = "Can't get image data/size";
Simon Glass56518e72013-06-13 15:10:01 -07001017 goto error;
1018 }
1019
1020 /* Verify all required signatures */
1021 if (IMAGE_ENABLE_VERIFY &&
1022 fit_image_verify_required_sigs(fit, image_noffset, data, size,
1023 gd_fdt_blob(), &verify_all)) {
1024 err_msg = "Unable to verify required signature";
1025 goto error;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001026 }
1027
1028 /* Process all hash subnodes of the component image node */
Axel Lin364ac5b2015-02-07 09:12:39 +08001029 fdt_for_each_subnode(fit, noffset, image_noffset) {
Simon Glassab9efc62013-05-07 06:11:58 +00001030 const char *name = fit_get_name(fit, noffset, NULL);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001031
Simon Glassab9efc62013-05-07 06:11:58 +00001032 /*
1033 * Check subnode name, must be equal to "hash".
1034 * Multiple hash nodes require unique unit node
1035 * names, e.g. hash@1, hash@2, etc.
1036 */
1037 if (!strncmp(name, FIT_HASH_NODENAME,
1038 strlen(FIT_HASH_NODENAME))) {
1039 if (fit_image_check_hash(fit, noffset, data, size,
1040 &err_msg))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001041 goto error;
Simon Glassab9efc62013-05-07 06:11:58 +00001042 puts("+ ");
Simon Glass56518e72013-06-13 15:10:01 -07001043 } else if (IMAGE_ENABLE_VERIFY && verify_all &&
1044 !strncmp(name, FIT_SIG_NODENAME,
1045 strlen(FIT_SIG_NODENAME))) {
1046 ret = fit_image_check_sig(fit, noffset, data,
1047 size, -1, &err_msg);
Simon Glass2e33e762016-02-24 09:14:43 -07001048
1049 /*
1050 * Show an indication on failure, but do not return
1051 * an error. Only keys marked 'required' can cause
1052 * an image validation failure. See the call to
1053 * fit_image_verify_required_sigs() above.
1054 */
1055 if (ret)
Simon Glass56518e72013-06-13 15:10:01 -07001056 puts("- ");
1057 else
1058 puts("+ ");
Simon Glass53fbb7e2013-05-07 06:11:53 +00001059 }
1060 }
1061
1062 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
Simon Glasse754da22013-05-07 06:11:59 +00001063 err_msg = "Corrupted or truncated tree";
Simon Glass53fbb7e2013-05-07 06:11:53 +00001064 goto error;
1065 }
1066
1067 return 1;
1068
1069error:
Simon Glasse754da22013-05-07 06:11:59 +00001070 printf(" error!\n%s for '%s' hash node in '%s' image node\n",
Simon Glass53fbb7e2013-05-07 06:11:53 +00001071 err_msg, fit_get_name(fit, noffset, NULL),
1072 fit_get_name(fit, image_noffset, NULL));
1073 return 0;
1074}
1075
1076/**
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001077 * fit_all_image_verify - verify data integrity for all images
Simon Glass53fbb7e2013-05-07 06:11:53 +00001078 * @fit: pointer to the FIT format image header
1079 *
Simon Glassb8da8362013-05-07 06:11:57 +00001080 * fit_all_image_verify() goes over all images in the FIT and
Simon Glass53fbb7e2013-05-07 06:11:53 +00001081 * for every images checks if all it's hashes are valid.
1082 *
1083 * returns:
1084 * 1, if all hashes of all images are valid
1085 * 0, otherwise (or on error)
1086 */
Simon Glassb8da8362013-05-07 06:11:57 +00001087int fit_all_image_verify(const void *fit)
Simon Glass53fbb7e2013-05-07 06:11:53 +00001088{
1089 int images_noffset;
1090 int noffset;
1091 int ndepth;
1092 int count;
1093
1094 /* Find images parent node offset */
1095 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1096 if (images_noffset < 0) {
1097 printf("Can't find images parent node '%s' (%s)\n",
1098 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1099 return 0;
1100 }
1101
1102 /* Process all image subnodes, check hashes for each */
1103 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
1104 (ulong)fit);
1105 for (ndepth = 0, count = 0,
1106 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1107 (noffset >= 0) && (ndepth > 0);
1108 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1109 if (ndepth == 1) {
1110 /*
1111 * Direct child node of the images parent node,
1112 * i.e. component image node.
1113 */
Simon Glass73223f02016-02-22 22:55:43 -07001114 printf(" Hash(es) for Image %u (%s): ", count,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001115 fit_get_name(fit, noffset, NULL));
Simon Glass73223f02016-02-22 22:55:43 -07001116 count++;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001117
Simon Glassb8da8362013-05-07 06:11:57 +00001118 if (!fit_image_verify(fit, noffset))
Simon Glass53fbb7e2013-05-07 06:11:53 +00001119 return 0;
1120 printf("\n");
1121 }
1122 }
1123 return 1;
1124}
1125
1126/**
1127 * fit_image_check_os - check whether image node is of a given os type
1128 * @fit: pointer to the FIT format image header
1129 * @noffset: component image node offset
1130 * @os: requested image os
1131 *
1132 * fit_image_check_os() reads image os property and compares its numeric
1133 * id with the requested os. Comparison result is returned to the caller.
1134 *
1135 * returns:
1136 * 1 if image is of given os type
1137 * 0 otherwise (or on error)
1138 */
1139int fit_image_check_os(const void *fit, int noffset, uint8_t os)
1140{
1141 uint8_t image_os;
1142
1143 if (fit_image_get_os(fit, noffset, &image_os))
1144 return 0;
1145 return (os == image_os);
1146}
1147
1148/**
1149 * fit_image_check_arch - check whether image node is of a given arch
1150 * @fit: pointer to the FIT format image header
1151 * @noffset: component image node offset
1152 * @arch: requested imagearch
1153 *
1154 * fit_image_check_arch() reads image arch property and compares its numeric
1155 * id with the requested arch. Comparison result is returned to the caller.
1156 *
1157 * returns:
1158 * 1 if image is of given arch
1159 * 0 otherwise (or on error)
1160 */
1161int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
1162{
1163 uint8_t image_arch;
1164
1165 if (fit_image_get_arch(fit, noffset, &image_arch))
1166 return 0;
Simon Glass5bda35c2014-10-10 08:21:57 -06001167 return (arch == image_arch) ||
1168 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
Simon Glass53fbb7e2013-05-07 06:11:53 +00001169}
1170
1171/**
1172 * fit_image_check_type - check whether image node is of a given type
1173 * @fit: pointer to the FIT format image header
1174 * @noffset: component image node offset
1175 * @type: requested image type
1176 *
1177 * fit_image_check_type() reads image type property and compares its numeric
1178 * id with the requested type. Comparison result is returned to the caller.
1179 *
1180 * returns:
1181 * 1 if image is of given type
1182 * 0 otherwise (or on error)
1183 */
1184int fit_image_check_type(const void *fit, int noffset, uint8_t type)
1185{
1186 uint8_t image_type;
1187
1188 if (fit_image_get_type(fit, noffset, &image_type))
1189 return 0;
1190 return (type == image_type);
1191}
1192
1193/**
1194 * fit_image_check_comp - check whether image node uses given compression
1195 * @fit: pointer to the FIT format image header
1196 * @noffset: component image node offset
1197 * @comp: requested image compression type
1198 *
1199 * fit_image_check_comp() reads image compression property and compares its
1200 * numeric id with the requested compression type. Comparison result is
1201 * returned to the caller.
1202 *
1203 * returns:
1204 * 1 if image uses requested compression
1205 * 0 otherwise (or on error)
1206 */
1207int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
1208{
1209 uint8_t image_comp;
1210
1211 if (fit_image_get_comp(fit, noffset, &image_comp))
1212 return 0;
1213 return (comp == image_comp);
1214}
1215
1216/**
1217 * fit_check_format - sanity check FIT image format
1218 * @fit: pointer to the FIT format image header
1219 *
1220 * fit_check_format() runs a basic sanity FIT image verification.
1221 * Routine checks for mandatory properties, nodes, etc.
1222 *
1223 * returns:
1224 * 1, on success
1225 * 0, on failure
1226 */
1227int fit_check_format(const void *fit)
1228{
1229 /* mandatory / node 'description' property */
1230 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
1231 debug("Wrong FIT format: no description\n");
1232 return 0;
1233 }
1234
1235 if (IMAGE_ENABLE_TIMESTAMP) {
1236 /* mandatory / node 'timestamp' property */
1237 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
1238 debug("Wrong FIT format: no timestamp\n");
1239 return 0;
1240 }
1241 }
1242
1243 /* mandatory subimages parent '/images' node */
1244 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
1245 debug("Wrong FIT format: no images parent node\n");
1246 return 0;
1247 }
1248
1249 return 1;
1250}
1251
1252
1253/**
1254 * fit_conf_find_compat
1255 * @fit: pointer to the FIT format image header
1256 * @fdt: pointer to the device tree to compare against
1257 *
1258 * fit_conf_find_compat() attempts to find the configuration whose fdt is the
1259 * most compatible with the passed in device tree.
1260 *
1261 * Example:
1262 *
1263 * / o image-tree
1264 * |-o images
1265 * | |-o fdt@1
1266 * | |-o fdt@2
1267 * |
1268 * |-o configurations
1269 * |-o config@1
1270 * | |-fdt = fdt@1
1271 * |
1272 * |-o config@2
1273 * |-fdt = fdt@2
1274 *
1275 * / o U-Boot fdt
1276 * |-compatible = "foo,bar", "bim,bam"
1277 *
1278 * / o kernel fdt1
1279 * |-compatible = "foo,bar",
1280 *
1281 * / o kernel fdt2
1282 * |-compatible = "bim,bam", "baz,biz"
1283 *
1284 * Configuration 1 would be picked because the first string in U-Boot's
1285 * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
1286 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
1287 *
1288 * returns:
1289 * offset to the configuration to use if one was found
1290 * -1 otherwise
1291 */
1292int fit_conf_find_compat(const void *fit, const void *fdt)
1293{
1294 int ndepth = 0;
1295 int noffset, confs_noffset, images_noffset;
1296 const void *fdt_compat;
1297 int fdt_compat_len;
1298 int best_match_offset = 0;
1299 int best_match_pos = 0;
1300
1301 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1302 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1303 if (confs_noffset < 0 || images_noffset < 0) {
1304 debug("Can't find configurations or images nodes.\n");
1305 return -1;
1306 }
1307
1308 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
1309 if (!fdt_compat) {
1310 debug("Fdt for comparison has no \"compatible\" property.\n");
1311 return -1;
1312 }
1313
1314 /*
1315 * Loop over the configurations in the FIT image.
1316 */
1317 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1318 (noffset >= 0) && (ndepth > 0);
1319 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1320 const void *kfdt;
1321 const char *kfdt_name;
1322 int kfdt_noffset;
1323 const char *cur_fdt_compat;
1324 int len;
1325 size_t size;
1326 int i;
1327
1328 if (ndepth > 1)
1329 continue;
1330
1331 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
1332 if (!kfdt_name) {
1333 debug("No fdt property found.\n");
1334 continue;
1335 }
1336 kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
1337 kfdt_name);
1338 if (kfdt_noffset < 0) {
1339 debug("No image node named \"%s\" found.\n",
1340 kfdt_name);
1341 continue;
1342 }
1343 /*
1344 * Get a pointer to this configuration's fdt.
1345 */
1346 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
1347 debug("Failed to get fdt \"%s\".\n", kfdt_name);
1348 continue;
1349 }
1350
1351 len = fdt_compat_len;
1352 cur_fdt_compat = fdt_compat;
1353 /*
1354 * Look for a match for each U-Boot compatibility string in
1355 * turn in this configuration's fdt.
1356 */
1357 for (i = 0; len > 0 &&
1358 (!best_match_offset || best_match_pos > i); i++) {
1359 int cur_len = strlen(cur_fdt_compat) + 1;
1360
1361 if (!fdt_node_check_compatible(kfdt, 0,
1362 cur_fdt_compat)) {
1363 best_match_offset = noffset;
1364 best_match_pos = i;
1365 break;
1366 }
1367 len -= cur_len;
1368 cur_fdt_compat += cur_len;
1369 }
1370 }
1371 if (!best_match_offset) {
1372 debug("No match found.\n");
1373 return -1;
1374 }
1375
1376 return best_match_offset;
1377}
1378
1379/**
1380 * fit_conf_get_node - get node offset for configuration of a given unit name
1381 * @fit: pointer to the FIT format image header
1382 * @conf_uname: configuration node unit name
1383 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001384 * fit_conf_get_node() finds a configuration (within the '/configurations'
1385 * parent node) of a provided unit name. If configuration is found its node
Simon Glass53fbb7e2013-05-07 06:11:53 +00001386 * offset is returned to the caller.
1387 *
1388 * When NULL is provided in second argument fit_conf_get_node() will search
1389 * for a default configuration node instead. Default configuration node unit
Robert P. J. Day1f8b5462013-08-21 11:39:19 -04001390 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
Simon Glass53fbb7e2013-05-07 06:11:53 +00001391 * node.
1392 *
1393 * returns:
1394 * configuration node offset when found (>=0)
1395 * negative number on failure (FDT_ERR_* code)
1396 */
1397int fit_conf_get_node(const void *fit, const char *conf_uname)
1398{
1399 int noffset, confs_noffset;
1400 int len;
1401
1402 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1403 if (confs_noffset < 0) {
1404 debug("Can't find configurations parent node '%s' (%s)\n",
1405 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1406 return confs_noffset;
1407 }
1408
1409 if (conf_uname == NULL) {
1410 /* get configuration unit name from the default property */
1411 debug("No configuration specified, trying default...\n");
1412 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
1413 FIT_DEFAULT_PROP, &len);
1414 if (conf_uname == NULL) {
1415 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
1416 len);
1417 return len;
1418 }
1419 debug("Found default configuration: '%s'\n", conf_uname);
1420 }
1421
1422 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
1423 if (noffset < 0) {
1424 debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
1425 conf_uname, fdt_strerror(noffset));
1426 }
1427
1428 return noffset;
1429}
1430
Simon Glass003efd72013-05-07 06:12:00 +00001431int fit_conf_get_prop_node(const void *fit, int noffset,
Simon Glass53fbb7e2013-05-07 06:11:53 +00001432 const char *prop_name)
1433{
1434 char *uname;
1435 int len;
1436
1437 /* get kernel image unit name from configuration kernel property */
1438 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
1439 if (uname == NULL)
1440 return len;
1441
1442 return fit_image_get_node(fit, uname);
1443}
1444
1445/**
Simon Glass53fbb7e2013-05-07 06:11:53 +00001446 * fit_conf_print - prints out the FIT configuration details
1447 * @fit: pointer to the FIT format image header
1448 * @noffset: offset of the configuration node
1449 * @p: pointer to prefix string
1450 *
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001451 * fit_conf_print() lists all mandatory properties for the processed
Simon Glass53fbb7e2013-05-07 06:11:53 +00001452 * configuration node.
1453 *
1454 * returns:
1455 * no returned results
1456 */
1457void fit_conf_print(const void *fit, int noffset, const char *p)
1458{
1459 char *desc;
1460 char *uname;
1461 int ret;
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001462 int loadables_index;
Simon Glass53fbb7e2013-05-07 06:11:53 +00001463
1464 /* Mandatory properties */
1465 ret = fit_get_desc(fit, noffset, &desc);
1466 printf("%s Description: ", p);
1467 if (ret)
1468 printf("unavailable\n");
1469 else
1470 printf("%s\n", desc);
1471
1472 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
1473 printf("%s Kernel: ", p);
1474 if (uname == NULL)
1475 printf("unavailable\n");
1476 else
1477 printf("%s\n", uname);
1478
1479 /* Optional properties */
1480 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
1481 if (uname)
1482 printf("%s Init Ramdisk: %s\n", p, uname);
1483
1484 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
1485 if (uname)
1486 printf("%s FDT: %s\n", p, uname);
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001487
Michal Simeked0cea72016-05-17 13:58:44 +02001488 uname = (char *)fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
1489 if (uname)
1490 printf("%s FPGA: %s\n", p, uname);
1491
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001492 /* Print out all of the specified loadables */
1493 for (loadables_index = 0;
Hans de Goede5bcec542015-05-29 15:09:48 +02001494 fdt_get_string_index(fit, noffset,
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001495 FIT_LOADABLE_PROP,
1496 loadables_index,
Hans de Goede5bcec542015-05-29 15:09:48 +02001497 (const char **)&uname) == 0;
Karl Apsiteecf8cd62015-05-21 09:52:47 -04001498 loadables_index++)
1499 {
1500 if (loadables_index == 0) {
1501 printf("%s Loadables: ", p);
1502 } else {
1503 printf("%s ", p);
1504 }
1505 printf("%s\n", uname);
1506 }
Simon Glass53fbb7e2013-05-07 06:11:53 +00001507}
1508
Jeroen Hofstee718feca2014-10-08 22:57:38 +02001509static int fit_image_select(const void *fit, int rd_noffset, int verify)
Simon Glass782cfbb2013-05-16 13:53:21 +00001510{
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -05001511#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1512 const void *data;
1513 size_t size;
1514 int ret;
1515#endif
1516
Simon Glass782cfbb2013-05-16 13:53:21 +00001517 fit_image_print(fit, rd_noffset, " ");
1518
1519 if (verify) {
1520 puts(" Verifying Hash Integrity ... ");
1521 if (!fit_image_verify(fit, rd_noffset)) {
1522 puts("Bad Data Hash\n");
1523 return -EACCES;
1524 }
1525 puts("OK\n");
1526 }
1527
Andreas Dannenbergeba3fbd2016-07-27 12:12:39 -05001528#if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1529 ret = fit_image_get_data(fit, rd_noffset, &data, &size);
1530 if (ret)
1531 return ret;
1532
1533 /* perform any post-processing on the image data */
1534 board_fit_image_post_process((void **)&data, &size);
1535
1536 /*
1537 * update U-Boot's understanding of the "data" property start address
1538 * and size according to the performed post-processing
1539 */
1540 ret = fdt_setprop((void *)fit, rd_noffset, FIT_DATA_PROP, data, size);
1541 if (ret)
1542 return ret;
1543#endif
1544
Simon Glass782cfbb2013-05-16 13:53:21 +00001545 return 0;
1546}
1547
Simon Glass782cfbb2013-05-16 13:53:21 +00001548int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
1549 ulong addr)
1550{
1551 int cfg_noffset;
1552 void *fit_hdr;
1553 int noffset;
1554
1555 debug("* %s: using config '%s' from image at 0x%08lx\n",
1556 prop_name, images->fit_uname_cfg, addr);
1557
1558 /* Check whether configuration has this property defined */
1559 fit_hdr = map_sysmem(addr, 0);
1560 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
1561 if (cfg_noffset < 0) {
1562 debug("* %s: no such config\n", prop_name);
1563 return -ENOENT;
1564 }
1565
1566 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
1567 if (noffset < 0) {
1568 debug("* %s: no '%s' in config\n", prop_name, prop_name);
Jonathan Graybac17b72016-09-03 08:30:14 +10001569 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001570 }
1571
1572 return noffset;
1573}
1574
Simon Glass126cc862014-06-12 07:24:47 -06001575/**
1576 * fit_get_image_type_property() - get property name for IH_TYPE_...
1577 *
1578 * @return the properly name where we expect to find the image in the
1579 * config node
1580 */
1581static const char *fit_get_image_type_property(int type)
1582{
1583 /*
1584 * This is sort-of available in the uimage_type[] table in image.c
Andreas Dannenberge17adbb2016-06-15 17:00:19 -05001585 * but we don't have access to the short name, and "fdt" is different
Simon Glass126cc862014-06-12 07:24:47 -06001586 * anyway. So let's just keep it here.
1587 */
1588 switch (type) {
1589 case IH_TYPE_FLATDT:
1590 return FIT_FDT_PROP;
1591 case IH_TYPE_KERNEL:
1592 return FIT_KERNEL_PROP;
1593 case IH_TYPE_RAMDISK:
1594 return FIT_RAMDISK_PROP;
Simon Glass90268b82014-10-19 21:11:24 -06001595 case IH_TYPE_X86_SETUP:
1596 return FIT_SETUP_PROP;
Karl Apsite84a07db2015-05-21 09:52:48 -04001597 case IH_TYPE_LOADABLE:
1598 return FIT_LOADABLE_PROP;
Michal Simek62afc602016-05-17 14:03:50 +02001599 case IH_TYPE_FPGA:
1600 return FIT_FPGA_PROP;
Simon Glass126cc862014-06-12 07:24:47 -06001601 }
1602
1603 return "unknown";
1604}
1605
1606int fit_image_load(bootm_headers_t *images, ulong addr,
Simon Glassf320a4d2013-07-10 23:08:10 -07001607 const char **fit_unamep, const char **fit_uname_configp,
Simon Glass782cfbb2013-05-16 13:53:21 +00001608 int arch, int image_type, int bootstage_id,
1609 enum fit_load_op load_op, ulong *datap, ulong *lenp)
1610{
1611 int cfg_noffset, noffset;
1612 const char *fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001613 const char *fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001614 const void *fit;
1615 const void *buf;
1616 size_t size;
1617 int type_ok, os_ok;
1618 ulong load, data, len;
Marek Vasut38117232014-12-16 14:07:22 +01001619 uint8_t os;
Simon Glass126cc862014-06-12 07:24:47 -06001620 const char *prop_name;
Simon Glass782cfbb2013-05-16 13:53:21 +00001621 int ret;
1622
1623 fit = map_sysmem(addr, 0);
1624 fit_uname = fit_unamep ? *fit_unamep : NULL;
Simon Glassf320a4d2013-07-10 23:08:10 -07001625 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
Simon Glass126cc862014-06-12 07:24:47 -06001626 prop_name = fit_get_image_type_property(image_type);
Simon Glass782cfbb2013-05-16 13:53:21 +00001627 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
1628
1629 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1630 if (!fit_check_format(fit)) {
1631 printf("Bad FIT %s image format!\n", prop_name);
1632 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
1633 return -ENOEXEC;
1634 }
1635 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
1636 if (fit_uname) {
Masahiro Yamadaf150c832014-02-18 15:39:21 +09001637 /* get FIT component image node offset */
Simon Glass782cfbb2013-05-16 13:53:21 +00001638 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
1639 noffset = fit_image_get_node(fit, fit_uname);
1640 } else {
1641 /*
1642 * no image node unit name, try to get config
1643 * node first. If config unit node name is NULL
1644 * fit_conf_get_node() will try to find default config node
1645 */
1646 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
1647 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
1648 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
1649 } else {
1650 cfg_noffset = fit_conf_get_node(fit,
1651 fit_uname_config);
1652 }
1653 if (cfg_noffset < 0) {
1654 puts("Could not find configuration node\n");
1655 bootstage_error(bootstage_id +
1656 BOOTSTAGE_SUB_NO_UNIT_NAME);
1657 return -ENOENT;
1658 }
1659 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1660 printf(" Using '%s' configuration\n", fit_uname_config);
1661 if (image_type == IH_TYPE_KERNEL) {
1662 /* Remember (and possibly verify) this config */
1663 images->fit_uname_cfg = fit_uname_config;
1664 if (IMAGE_ENABLE_VERIFY && images->verify) {
1665 puts(" Verifying Hash Integrity ... ");
Simon Glass12df2ab2014-06-12 07:24:45 -06001666 if (fit_config_verify(fit, cfg_noffset)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001667 puts("Bad Data Hash\n");
1668 bootstage_error(bootstage_id +
1669 BOOTSTAGE_SUB_HASH);
1670 return -EACCES;
1671 }
1672 puts("OK\n");
1673 }
1674 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
1675 }
1676
1677 noffset = fit_conf_get_prop_node(fit, cfg_noffset,
1678 prop_name);
1679 fit_uname = fit_get_name(fit, noffset, NULL);
1680 }
1681 if (noffset < 0) {
1682 puts("Could not find subimage node\n");
1683 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
1684 return -ENOENT;
1685 }
1686
1687 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
1688
1689 ret = fit_image_select(fit, noffset, images->verify);
1690 if (ret) {
1691 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
1692 return ret;
1693 }
1694
1695 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
Simon Glass5ba63dd2014-10-19 21:11:22 -06001696#if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
Simon Glass782cfbb2013-05-16 13:53:21 +00001697 if (!fit_image_check_target_arch(fit, noffset)) {
1698 puts("Unsupported Architecture\n");
1699 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
1700 return -ENOEXEC;
1701 }
Simon Glassce1400f2014-06-12 07:24:53 -06001702#endif
Simon Glass782cfbb2013-05-16 13:53:21 +00001703 if (image_type == IH_TYPE_FLATDT &&
1704 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
1705 puts("FDT image is compressed");
1706 return -EPROTONOSUPPORT;
1707 }
1708
1709 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1710 type_ok = fit_image_check_type(fit, noffset, image_type) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001711 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
1712 (image_type == IH_TYPE_KERNEL &&
1713 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
Marek Vasut38117232014-12-16 14:07:22 +01001714
Andreas Bießmann950fe262016-08-14 20:31:24 +02001715 os_ok = image_type == IH_TYPE_FLATDT ||
1716 image_type == IH_TYPE_FPGA ||
Marek Vasut38117232014-12-16 14:07:22 +01001717 fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
mario.six@gdsys.cce8fb4352016-07-20 08:32:50 +02001718 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
Marek Vasut38117232014-12-16 14:07:22 +01001719 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
Karl Apsite84a07db2015-05-21 09:52:48 -04001720
1721 /*
1722 * If either of the checks fail, we should report an error, but
1723 * if the image type is coming from the "loadables" field, we
1724 * don't care what it is
1725 */
1726 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
Marek Vasut38117232014-12-16 14:07:22 +01001727 fit_image_get_os(fit, noffset, &os);
1728 printf("No %s %s %s Image\n",
1729 genimg_get_os_name(os),
1730 genimg_get_arch_name(arch),
Simon Glass782cfbb2013-05-16 13:53:21 +00001731 genimg_get_type_name(image_type));
1732 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
1733 return -EIO;
1734 }
1735
1736 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
1737
1738 /* get image data address and length */
1739 if (fit_image_get_data(fit, noffset, &buf, &size)) {
1740 printf("Could not find %s subimage data!\n", prop_name);
1741 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
Simon Glass2f998072013-06-16 07:46:49 -07001742 return -ENOENT;
Simon Glass782cfbb2013-05-16 13:53:21 +00001743 }
1744 len = (ulong)size;
1745
1746 /* verify that image data is a proper FDT blob */
Masahiro Yamada2f0877c2013-09-19 12:10:18 +09001747 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001748 puts("Subimage data is not a FDT");
1749 return -ENOEXEC;
1750 }
1751
1752 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
1753
1754 /*
1755 * Work-around for eldk-4.2 which gives this warning if we try to
Simon Glasse3c83c02014-06-12 07:24:49 -06001756 * cast in the unmap_sysmem() call:
Simon Glass782cfbb2013-05-16 13:53:21 +00001757 * warning: initialization discards qualifiers from pointer target type
1758 */
1759 {
1760 void *vbuf = (void *)buf;
1761
1762 data = map_to_sysmem(vbuf);
1763 }
1764
1765 if (load_op == FIT_LOAD_IGNORED) {
1766 /* Don't load */
1767 } else if (fit_image_get_load(fit, noffset, &load)) {
1768 if (load_op == FIT_LOAD_REQUIRED) {
1769 printf("Can't get %s subimage load address!\n",
1770 prop_name);
1771 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
1772 return -EBADF;
1773 }
Simon Glassfe20a812014-08-22 14:26:43 -06001774 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
Simon Glass782cfbb2013-05-16 13:53:21 +00001775 ulong image_start, image_end;
1776 ulong load_end;
1777 void *dst;
1778
1779 /*
1780 * move image data to the load address,
1781 * make sure we don't overwrite initial image
1782 */
1783 image_start = addr;
1784 image_end = addr + fit_get_size(fit);
1785
1786 load_end = load + len;
1787 if (image_type != IH_TYPE_KERNEL &&
1788 load < image_end && load_end > image_start) {
1789 printf("Error: %s overwritten\n", prop_name);
1790 return -EXDEV;
1791 }
1792
1793 printf(" Loading %s from 0x%08lx to 0x%08lx\n",
1794 prop_name, data, load);
1795
1796 dst = map_sysmem(load, len);
1797 memmove(dst, buf, len);
1798 data = load;
1799 }
1800 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
1801
1802 *datap = data;
1803 *lenp = len;
1804 if (fit_unamep)
1805 *fit_unamep = (char *)fit_uname;
Simon Glassf320a4d2013-07-10 23:08:10 -07001806 if (fit_uname_configp)
1807 *fit_uname_configp = (char *)fit_uname_config;
Simon Glass782cfbb2013-05-16 13:53:21 +00001808
1809 return noffset;
1810}
Simon Glass90268b82014-10-19 21:11:24 -06001811
1812int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
1813 ulong *setup_start, ulong *setup_len)
1814{
1815 int noffset;
1816 ulong addr;
1817 ulong len;
1818 int ret;
1819
1820 addr = map_to_sysmem(images->fit_hdr_os);
1821 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
1822 if (noffset < 0)
1823 return noffset;
1824
1825 ret = fit_image_load(images, addr, NULL, NULL, arch,
1826 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
1827 FIT_LOAD_REQUIRED, setup_start, &len);
1828
1829 return ret;
1830}